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.2 2002/06/09 04:41:29 relnev
19 * added copyright header
21 * Revision 1.1.1.1 2002/05/03 03:28:11 root
25 * 4 12/02/98 5:47p Dave
26 * Put in interface xstr code. Converted barracks screen to new format.
28 * 3 10/13/98 9:29a Dave
29 * Started neatening up freespace.h. Many variables renamed and
30 * reorganized. Added AlphaColors.[h,cpp]
32 * 2 10/07/98 10:54a Dave
35 * 1 10/07/98 10:51a Dave
37 * 15 3/23/98 5:48p Hoffoss
38 * Improved listbox handling. Most notibly the scrollbar arrows work now.
40 * 14 2/03/98 4:21p Hoffoss
41 * Made UI controls draw white text when disabled.
43 * 13 1/14/98 6:44p Hoffoss
44 * Massive changes to UI code. A lot cleaner and better now. Did all
45 * this to get the new UI_DOT_SLIDER to work properly, which the old code
46 * wasn't flexible enough to handle.
48 * 12 8/24/97 5:24p Lawrance
49 * improve drawing of buttons
51 * 11 6/12/97 12:39p John
52 * made ui use freespace colors
54 * 10 6/11/97 1:13p John
55 * Started fixing all the text colors in the game.
57 * 9 5/26/97 10:26a Lawrance
58 * get slider control working 100%
60 * 8 5/22/97 5:36p Lawrance
61 * allowing custom art for scrollbars
63 * 7 1/28/97 4:58p Lawrance
64 * allowing hidden UI components
66 * 6 12/04/96 3:00p John
67 * Added code to allow adjusting of HUD colors and saved it to the player
70 * 5 12/03/96 11:29a John
71 * Made scroll buttons on listbox scroll once, then delay, then repeat
72 * when the buttons are held down.
74 * 4 12/02/96 2:50p John
75 * Made list box not scroll instantly.
77 * 3 12/02/96 2:17p John
78 * Made right button drag UI gadgets around and
79 * Ctrl+Shift+Alt+F12 dumps out where they are.
81 * 2 11/15/96 11:43a John
83 * 1 11/14/96 6:55p John
91 #include "alphacolors.h"
94 // --------------------------------------------------------------------
95 // UI_SCROLLBAR::link_hotspot
98 void UI_SCROLLBAR::link_hotspot(int up_button_num, int down_button_num)
100 up_button.link_hotspot(up_button_num);
101 down_button.link_hotspot(down_button_num);
104 // --------------------------------------------------------------------
105 // UI_SCROLLBAR::set_bmaps
107 // Call the UI_GADGET::set_bmaps() function for the child components
108 // of a scroll bar (the up and down button). Set up the bmaps for the
111 // We also need to get the dimensions of the bitmap button so we can update
112 // the dimensions of the scrollbar.
114 // returns: -1 ==> error
117 int UI_SCROLLBAR::set_bmaps(char *up_button_fname, char *down_button_fname, char *line_fname)
121 up_button.set_bmaps(up_button_fname);
122 down_button.set_bmaps(down_button_fname);
123 up_button.get_dimensions(&bx, &by, &bw, &bh);
125 // set the bitmaps for the rectangle that is the scrollbar itself
126 ((UI_GADGET*)this)->set_bmaps(line_fname);
129 update_dimensions(x,y+bw,bw,h-bw*2);
134 void UI_SCROLLBAR::hide()
141 void UI_SCROLLBAR::unhide()
145 down_button.unhide();
148 int UI_SCROLLBAR::get_hidden()
153 void UI_SCROLLBAR::create(UI_WINDOW *wnd, int _x, int _y, int _h, int _start, int _stop, int _position, int _window_size)
159 base_create( wnd, UI_KIND_SCROLLBAR, _x, _y + bw, bw, _h - bw * 2 );
161 up_button.create( wnd, up, _x, _y, bw, bw, 1 );
162 up_button.set_parent(this);
163 up_button.set_hotkey_if_focus(KEY_UP);
165 down_button.create( wnd, down, _x, _y + _h - bw, bw, bw, 1 );
166 down_button.set_parent(this);
167 down_button.set_hotkey_if_focus(KEY_DOWN);
172 position = _position;
173 window_size = _window_size;
180 bar_size = (window_size * h) / (stop - start + window_size + 1);
187 bar_position = position - start;
188 bar_position *= h - bar_size;
189 bar_position /= stop - start;
196 void UI_SCROLLBAR::draw()
203 if ( bmap_ids[SB_DISABLED] != -1 ) {
204 gr_set_bitmap(bmap_ids[SB_DISABLED]);
209 if ( bmap_ids[SB_NORMAL] != -1 ) {
210 gr_set_bitmap(bmap_ids[SB_NORMAL]);
215 gr_set_clip( x, y, w, h );
216 ui_draw_box_out( 0, bar_position, w - 1, bar_position + bar_size - 1 );
219 gr_set_font(my_wnd->f_id);
220 gr_set_clip( x, y, w, h );
222 if (my_wnd->selected_gadget == this)
223 gr_set_color_fast(&CBRIGHT_GREEN);
225 gr_set_color_fast(&CGRAY);
228 ui_rect( 0, 0, w-1, bar_position-1 );
229 ui_rect( 0, bar_position+bar_size, w-1, h-1);
231 ui_rect( 0, 0, w-1, h-1 );
232 ui_draw_box_out( 0, bar_position, w - 1, bar_position + bar_size - 1 );
236 void UI_SCROLLBAR::process(int focus)
246 if (my_wnd->selected_gadget == this)
249 up_button.process(focus);
250 down_button.process(focus);
259 oldpos = bar_position;
261 if (up_button.pressed()) {
263 if (position < start)
266 bar_position = position - start;
267 bar_position *= h - bar_size;
268 bar_position /= stop - start;
272 if ( (up_button.position != 0) || (focus && keyd_pressed[KEY_UP]) ) {
273 if ( (timer_get_milliseconds() > last_scrolled + 50) || up_button.just_pressed() ) {
274 if ( up_button.just_pressed() ) {
275 last_scrolled = timer_get_milliseconds() + 300;
277 last_scrolled = timer_get_milliseconds();
280 if (position < start)
283 bar_position = position - start;
284 bar_position *= h - bar_size;
285 bar_position /= stop - start;
289 if (down_button.pressed()) {
294 bar_position = position - start;
295 bar_position *= h - bar_size;
296 bar_position /= stop - start;
300 /* if ( down_button.position || (keyfocus && keyd_pressed[KEY_DOWN]) ) {
301 if ( (timer_get_milliseconds() > last_scrolled + 50) || down_button.just_pressed() ) {
302 if ( down_button.just_pressed() )
303 last_scrolled = timer_get_milliseconds() + 300;
305 last_scrolled = timer_get_milliseconds();
308 if (position > stop )
311 bar_position = position-start;
312 bar_position *= h-bar_size;
313 bar_position /= (stop-start);
317 OnMe = is_mouse_on();
323 if ( (ui_mouse.y >= bar_position + y) && (ui_mouse.y < bar_position + y + bar_size) && OnMe )
326 if (B1_JUST_PRESSED && OnSlider) {
330 drag_starting = bar_position;
334 if ( B1_PRESSED && OnMe && !OnSlider && (timer_get_milliseconds() > last_scrolled + 1000 / (18*4)) ) {
335 last_scrolled = timer_get_milliseconds();
337 if ( ui_mouse.y < bar_position+y ) {
339 position -= window_size;
340 if (position < start)
345 position += window_size;
350 bar_position = position - start;
351 bar_position *= h - bar_size;
352 bar_position /= stop - start;
356 if (B1_PRESSED && dragging) {
357 bar_position = drag_starting + ui_mouse.y - drag_y;
359 if (bar_position < 0) {
363 if (bar_position > h - bar_size) {
364 bar_position = h - bar_size;
367 position = bar_position;
368 position *= stop - start;
369 position /= h - bar_size;
375 if (position < start)
387 int UI_SCROLLBAR::getpos()
392 int UI_SCROLLBAR::changed()