1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
/*
* Copyright (C) Volition, Inc. 1999. All rights reserved.
*
* All source code herein is the property of Volition, Inc. You may not sell
* or otherwise commercially exploit the source or things you created based on the
* source.
*
*/
#include "ui/uidefs.h"
#include "ui/ui.h"
#include "io/timer.h"
#include "globalincs/alphacolors.h"
// --------------------------------------------------------------------
// UI_SCROLLBAR::link_hotspot
//
//
void UI_SCROLLBAR::link_hotspot(int up_button_num, int down_button_num)
{
up_button.link_hotspot(up_button_num);
down_button.link_hotspot(down_button_num);
}
// --------------------------------------------------------------------
// UI_SCROLLBAR::set_bmaps
//
// Call the UI_GADGET::set_bmaps() function for the child components
// of a scroll bar (the up and down button). Set up the bmaps for the
// line itself.
//
// We also need to get the dimensions of the bitmap button so we can update
// the dimensions of the scrollbar.
//
// returns: -1 ==> error
// 0 ==> success
//
int UI_SCROLLBAR::set_bmaps(char *up_button_fname, char *down_button_fname, char *line_fname)
{
int bx, by, bh, bw;
up_button.set_bmaps(up_button_fname);
down_button.set_bmaps(down_button_fname);
up_button.get_dimensions(&bx, &by, &bw, &bh);
// set the bitmaps for the rectangle that is the scrollbar itself
((UI_GADGET*)this)->set_bmaps(line_fname);
uses_bmaps = 1;
update_dimensions(x,y+bw,bw,h-bw*2);
return 0;
}
void UI_SCROLLBAR::hide()
{
hidden = 1;
up_button.hide();
down_button.hide();
}
void UI_SCROLLBAR::unhide()
{
hidden = 0;
up_button.unhide();
down_button.unhide();
}
int UI_SCROLLBAR::get_hidden()
{
return hidden;
}
void UI_SCROLLBAR::create(UI_WINDOW *wnd, int _x, int _y, int _h, int _start, int _stop, int _position, int _window_size)
{
char *up = "^";
char *down = "v";
int bw = 20;
base_create( wnd, UI_KIND_SCROLLBAR, _x, _y + bw, bw, _h - bw * 2 );
up_button.create( wnd, up, _x, _y, bw, bw, 1 );
up_button.set_parent(this);
up_button.set_hotkey_if_focus(KEY_UP);
down_button.create( wnd, down, _x, _y + _h - bw, bw, bw, 1 );
down_button.set_parent(this);
down_button.set_hotkey_if_focus(KEY_DOWN);
horz = 0;
start = _start;
stop = _stop;
position = _position;
window_size = _window_size;
bar_length = h;
bar_position = 0;
Assert( stop >= 0 );
if (stop != start)
bar_size = (window_size * h) / (stop - start + window_size + 1);
else
bar_size = h;
if (bar_size < 7)
bar_size = 7;
bar_position = position - start;
bar_position *= h - bar_size;
bar_position /= stop - start;
dragging = 0;
last_scrolled = 0;
moved = 1;
}
void UI_SCROLLBAR::draw()
{
UI_GADGET::draw();
if (uses_bmaps) {
gr_reset_clip();
if (disabled_flag) {
if ( bmap_ids[SB_DISABLED] != -1 ) {
gr_set_bitmap(bmap_ids[SB_DISABLED]);
gr_bitmap(x,y);
}
} else {
if ( bmap_ids[SB_NORMAL] != -1 ) {
gr_set_bitmap(bmap_ids[SB_NORMAL]);
gr_bitmap(x,y);
}
}
gr_set_clip( x, y, w, h );
ui_draw_box_out( 0, bar_position, w - 1, bar_position + bar_size - 1 );
} else {
gr_set_font(my_wnd->f_id);
gr_set_clip( x, y, w, h );
if (my_wnd->selected_gadget == this)
gr_set_color_fast(&CBRIGHT_GREEN);
else
gr_set_color_fast(&CGRAY);
ui_rect( 0, 0, w-1, h-1 );
ui_draw_box_out( 0, bar_position, w - 1, bar_position + bar_size - 1 );
}
}
void UI_SCROLLBAR::process(int focus)
{
int OnMe, OnSlider;
int op;
moved = 0;
if (disabled_flag) {
return;
}
if (my_wnd->selected_gadget == this)
focus = 1;
up_button.process(focus);
down_button.process(focus);
if (start == stop) {
position = 0;
bar_position = 0;
return;
}
op = position;
if (up_button.pressed()) {
position--;
if (position < start)
position = start;
bar_position = position - start;
bar_position *= h - bar_size;
bar_position /= stop - start;
set_focus();
}
if (down_button.pressed()) {
position++;
if (position > stop)
position = stop;
bar_position = position - start;
bar_position *= h - bar_size;
bar_position /= stop - start;
set_focus();
}
OnMe = is_mouse_on();
if (!B1_PRESSED)
dragging = 0;
OnSlider = 0;
if ( (ui_mouse.y >= bar_position + y) && (ui_mouse.y < bar_position + y + bar_size) && OnMe )
OnSlider = 1;
if (B1_JUST_PRESSED && OnSlider) {
dragging = 1;
drag_x = ui_mouse.x;
drag_y = ui_mouse.y;
drag_starting = bar_position;
set_focus();
}
if ( B1_PRESSED && OnMe && !OnSlider && (timer_get_milliseconds() > last_scrolled + 1000 / (18*4)) ) {
last_scrolled = timer_get_milliseconds();
if ( ui_mouse.y < bar_position+y ) {
// Page Up
position -= window_size;
if (position < start)
position = start;
} else {
// Page Down
position += window_size;
if (position > stop)
position = stop;
}
bar_position = position - start;
bar_position *= h - bar_size;
bar_position /= stop - start;
set_focus();
}
if (B1_PRESSED && dragging) {
bar_position = drag_starting + ui_mouse.y - drag_y;
if (bar_position < 0) {
bar_position = 0;
}
if (bar_position > h - bar_size) {
bar_position = h - bar_size;
}
position = bar_position;
position *= stop - start;
position /= h - bar_size;
position += start;
if (position > stop)
position = stop;
if (position < start)
position = start;
set_focus();
}
if (op != position)
moved = 1;
else
moved = 0;
}
int UI_SCROLLBAR::getpos()
{
return position;
}
int UI_SCROLLBAR::changed()
{
return moved;
}
|