2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
4 * All source code herein is the property of Volition, Inc. You may not sell
5 * or otherwise commercially exploit the source or things you created based on
10 * $Logfile: /Freespace2/code/UI/SCROLL.cpp $
15 * Code for vertical scrollbars.
18 * Revision 1.3 2004/09/20 01:31:45 theoddone33
21 * Revision 1.2 2002/06/09 04:41:29 relnev
22 * added copyright header
24 * Revision 1.1.1.1 2002/05/03 03:28:11 root
28 * 4 12/02/98 5:47p Dave
29 * Put in interface xstr code. Converted barracks screen to new format.
31 * 3 10/13/98 9:29a Dave
32 * Started neatening up freespace.h. Many variables renamed and
33 * reorganized. Added AlphaColors.[h,cpp]
35 * 2 10/07/98 10:54a Dave
38 * 1 10/07/98 10:51a Dave
40 * 15 3/23/98 5:48p Hoffoss
41 * Improved listbox handling. Most notibly the scrollbar arrows work now.
43 * 14 2/03/98 4:21p Hoffoss
44 * Made UI controls draw white text when disabled.
46 * 13 1/14/98 6:44p Hoffoss
47 * Massive changes to UI code. A lot cleaner and better now. Did all
48 * this to get the new UI_DOT_SLIDER to work properly, which the old code
49 * wasn't flexible enough to handle.
51 * 12 8/24/97 5:24p Lawrance
52 * improve drawing of buttons
54 * 11 6/12/97 12:39p John
55 * made ui use freespace colors
57 * 10 6/11/97 1:13p John
58 * Started fixing all the text colors in the game.
60 * 9 5/26/97 10:26a Lawrance
61 * get slider control working 100%
63 * 8 5/22/97 5:36p Lawrance
64 * allowing custom art for scrollbars
66 * 7 1/28/97 4:58p Lawrance
67 * allowing hidden UI components
69 * 6 12/04/96 3:00p John
70 * Added code to allow adjusting of HUD colors and saved it to the player
73 * 5 12/03/96 11:29a John
74 * Made scroll buttons on listbox scroll once, then delay, then repeat
75 * when the buttons are held down.
77 * 4 12/02/96 2:50p John
78 * Made list box not scroll instantly.
80 * 3 12/02/96 2:17p John
81 * Made right button drag UI gadgets around and
82 * Ctrl+Shift+Alt+F12 dumps out where they are.
84 * 2 11/15/96 11:43a John
86 * 1 11/14/96 6:55p John
94 #include "alphacolors.h"
98 // --------------------------------------------------------------------
99 // UI_SCROLLBAR::link_hotspot
102 void UI_SCROLLBAR::link_hotspot(int up_button_num, int down_button_num)
104 up_button.link_hotspot(up_button_num);
105 down_button.link_hotspot(down_button_num);
108 // --------------------------------------------------------------------
109 // UI_SCROLLBAR::set_bmaps
111 // Call the UI_GADGET::set_bmaps() function for the child components
112 // of a scroll bar (the up and down button). Set up the bmaps for the
115 // We also need to get the dimensions of the bitmap button so we can update
116 // the dimensions of the scrollbar.
118 // returns: -1 ==> error
121 int UI_SCROLLBAR::set_bmaps(const char *up_button_fname, const char *down_button_fname, const char *line_fname)
125 up_button.set_bmaps(up_button_fname);
126 down_button.set_bmaps(down_button_fname);
127 up_button.get_dimensions(&bx, &by, &bw, &bh);
129 // set the bitmaps for the rectangle that is the scrollbar itself
130 ((UI_GADGET*)this)->set_bmaps(line_fname);
133 update_dimensions(x,y+bw,bw,h-bw*2);
138 void UI_SCROLLBAR::hide(int n)
145 void UI_SCROLLBAR::unhide()
149 down_button.unhide();
152 int UI_SCROLLBAR::get_hidden()
157 void UI_SCROLLBAR::create(UI_WINDOW *wnd, int _x, int _y, int _h, int _start, int _stop, int _position, int _window_size)
159 const char *up = "^";
160 const char *down = "v";
163 base_create( wnd, UI_KIND_SCROLLBAR, _x, _y + bw, bw, _h - bw * 2 );
165 up_button.create( wnd, up, _x, _y, bw, bw, 1 );
166 up_button.set_parent(this);
167 up_button.set_hotkey_if_focus(SDLK_UP);
169 down_button.create( wnd, down, _x, _y + _h - bw, bw, bw, 1 );
170 down_button.set_parent(this);
171 down_button.set_hotkey_if_focus(SDLK_DOWN);
176 position = _position;
177 window_size = _window_size;
181 SDL_assert( stop >= 0 );
184 bar_size = (window_size * h) / (stop - start + window_size + 1);
191 bar_position = position - start;
192 bar_position *= h - bar_size;
193 bar_position /= stop - start;
200 void UI_SCROLLBAR::draw()
207 if ( bmap_ids[SB_DISABLED] != -1 ) {
208 gr_set_bitmap(bmap_ids[SB_DISABLED], GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
213 if ( bmap_ids[SB_NORMAL] != -1 ) {
214 gr_set_bitmap(bmap_ids[SB_NORMAL], GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
219 gr_set_clip( x, y, w, h );
220 ui_draw_box_out( 0, bar_position, w - 1, bar_position + bar_size - 1 );
223 gr_set_font(my_wnd->f_id);
224 gr_set_clip( x, y, w, h );
226 if (my_wnd->selected_gadget == this)
227 gr_set_color_fast(&CBRIGHT_GREEN);
229 gr_set_color_fast(&CGRAY);
232 ui_rect( 0, 0, w-1, bar_position-1 );
233 ui_rect( 0, bar_position+bar_size, w-1, h-1);
235 ui_rect( 0, 0, w-1, h-1 );
236 ui_draw_box_out( 0, bar_position, w - 1, bar_position + bar_size - 1 );
240 void UI_SCROLLBAR::process(int focus)
250 if (my_wnd->selected_gadget == this)
253 up_button.process(focus);
254 down_button.process(focus);
264 if (up_button.pressed()) {
266 if (position < start)
269 bar_position = position - start;
270 bar_position *= h - bar_size;
271 bar_position /= stop - start;
275 if ( (up_button.position != 0) || (focus && key_pressed(SDLK_UP)) ) {
276 if ( (timer_get_milliseconds() > last_scrolled + 50) || up_button.just_pressed() ) {
277 if ( up_button.just_pressed() ) {
278 last_scrolled = timer_get_milliseconds() + 300;
280 last_scrolled = timer_get_milliseconds();
283 if (position < start)
286 bar_position = position - start;
287 bar_position *= h - bar_size;
288 bar_position /= stop - start;
292 if (down_button.pressed()) {
297 bar_position = position - start;
298 bar_position *= h - bar_size;
299 bar_position /= stop - start;
303 /* if ( down_button.position || (keyfocus && key_pressed(SDLK_DOWN)) ) {
304 if ( (timer_get_milliseconds() > last_scrolled + 50) || down_button.just_pressed() ) {
305 if ( down_button.just_pressed() )
306 last_scrolled = timer_get_milliseconds() + 300;
308 last_scrolled = timer_get_milliseconds();
311 if (position > stop )
314 bar_position = position-start;
315 bar_position *= h-bar_size;
316 bar_position /= (stop-start);
320 OnMe = is_mouse_on();
326 if ( (ui_mouse.y >= bar_position + y) && (ui_mouse.y < bar_position + y + bar_size) && OnMe )
329 if (B1_JUST_PRESSED && OnSlider) {
333 drag_starting = bar_position;
337 if ( B1_PRESSED && OnMe && !OnSlider && (timer_get_milliseconds() > last_scrolled + 1000 / (18*4)) ) {
338 last_scrolled = timer_get_milliseconds();
340 if ( ui_mouse.y < bar_position+y ) {
342 position -= window_size;
343 if (position < start)
348 position += window_size;
353 bar_position = position - start;
354 bar_position *= h - bar_size;
355 bar_position /= stop - start;
359 if (B1_PRESSED && dragging) {
360 bar_position = drag_starting + ui_mouse.y - drag_y;
362 if (bar_position < 0) {
366 if (bar_position > h - bar_size) {
367 bar_position = h - bar_size;
370 position = bar_position;
371 position *= stop - start;
372 position /= h - bar_size;
378 if (position < start)
390 int UI_SCROLLBAR::getpos()
395 int UI_SCROLLBAR::changed()