]> icculus.org git repositories - taylor/freespace2.git/blob - src/ui/scroll.cpp
Initial revision
[taylor/freespace2.git] / src / ui / scroll.cpp
1 /*
2  * $Logfile: /Freespace2/code/UI/SCROLL.cpp $
3  * $Revision$
4  * $Date$
5  * $Author$
6  *
7  * Code for vertical scrollbars.
8  *
9  * $Log$
10  * Revision 1.1  2002/05/03 03:28:11  root
11  * Initial revision
12  *
13  * 
14  * 4     12/02/98 5:47p Dave
15  * Put in interface xstr code. Converted barracks screen to new format.
16  * 
17  * 3     10/13/98 9:29a Dave
18  * Started neatening up freespace.h. Many variables renamed and
19  * reorganized. Added AlphaColors.[h,cpp]
20  * 
21  * 2     10/07/98 10:54a Dave
22  * Initial checkin.
23  * 
24  * 1     10/07/98 10:51a Dave
25  * 
26  * 15    3/23/98 5:48p Hoffoss
27  * Improved listbox handling.  Most notibly the scrollbar arrows work now.
28  * 
29  * 14    2/03/98 4:21p Hoffoss
30  * Made UI controls draw white text when disabled.
31  * 
32  * 13    1/14/98 6:44p Hoffoss
33  * Massive changes to UI code.  A lot cleaner and better now.  Did all
34  * this to get the new UI_DOT_SLIDER to work properly, which the old code
35  * wasn't flexible enough to handle.
36  * 
37  * 12    8/24/97 5:24p Lawrance
38  * improve drawing of buttons 
39  * 
40  * 11    6/12/97 12:39p John
41  * made ui use freespace colors
42  * 
43  * 10    6/11/97 1:13p John
44  * Started fixing all the text colors in the game.
45  * 
46  * 9     5/26/97 10:26a Lawrance
47  * get slider control working 100%
48  * 
49  * 8     5/22/97 5:36p Lawrance
50  * allowing custom art for scrollbars
51  * 
52  * 7     1/28/97 4:58p Lawrance
53  * allowing hidden UI components
54  * 
55  * 6     12/04/96 3:00p John
56  * Added code to allow adjusting of HUD colors and saved it to the player
57  * config file.
58  * 
59  * 5     12/03/96 11:29a John
60  * Made scroll buttons on listbox scroll once, then delay, then repeat
61  * when the buttons are held down.
62  * 
63  * 4     12/02/96 2:50p John
64  * Made list box not scroll instantly.
65  * 
66  * 3     12/02/96 2:17p John
67  * Made right button drag UI gadgets around and
68  * Ctrl+Shift+Alt+F12 dumps out where they are.
69  * 
70  * 2     11/15/96 11:43a John
71  * 
72  * 1     11/14/96 6:55p John
73  *
74  * $NoKeywords: $
75  */
76
77 #include "uidefs.h"
78 #include "ui.h"
79 #include "timer.h"
80 #include "alphacolors.h"
81
82
83 // --------------------------------------------------------------------
84 // UI_SCROLLBAR::link_hotspot
85 //
86 //
87 void UI_SCROLLBAR::link_hotspot(int up_button_num, int down_button_num)
88 {
89         up_button.link_hotspot(up_button_num);
90         down_button.link_hotspot(down_button_num);
91 }
92
93 // --------------------------------------------------------------------
94 // UI_SCROLLBAR::set_bmaps
95 //
96 // Call the UI_GADGET::set_bmaps() function for the child components
97 // of a scroll bar (the up and down button).  Set up the bmaps for the
98 // line itself.
99 //
100 // We also need to get the dimensions of the bitmap button so we can update
101 // the dimensions of the scrollbar.
102 //
103 // returns:             -1 ==> error
104 //                                       0 ==> success
105 //
106 int UI_SCROLLBAR::set_bmaps(char *up_button_fname, char *down_button_fname, char *line_fname)
107 {
108         int bx, by, bh, bw;
109
110         up_button.set_bmaps(up_button_fname);
111         down_button.set_bmaps(down_button_fname);
112         up_button.get_dimensions(&bx, &by, &bw, &bh);
113         
114         // set the bitmaps for the rectangle that is the scrollbar itself
115         ((UI_GADGET*)this)->set_bmaps(line_fname);
116         uses_bmaps = 1;
117
118         update_dimensions(x,y+bw,bw,h-bw*2);
119
120         return 0;
121 }
122
123 void UI_SCROLLBAR::hide()
124 {
125         hidden = 1;
126         up_button.hide();
127         down_button.hide();
128 }
129
130 void UI_SCROLLBAR::unhide()
131 {
132         hidden = 0;
133         up_button.unhide();
134         down_button.unhide();
135 }
136
137 int UI_SCROLLBAR::get_hidden()
138 {
139         return hidden;
140 }
141
142 void UI_SCROLLBAR::create(UI_WINDOW *wnd, int _x, int _y, int _h, int _start, int _stop, int _position, int _window_size)
143 {
144         char *up = "^";
145         char *down = "v";
146         int bw = 20;
147
148         base_create( wnd, UI_KIND_SCROLLBAR, _x, _y + bw, bw, _h - bw * 2 );
149
150         up_button.create( wnd, up, _x, _y, bw, bw, 1 );
151         up_button.set_parent(this);
152         up_button.set_hotkey_if_focus(KEY_UP);
153
154         down_button.create( wnd, down, _x, _y + _h - bw, bw, bw, 1 );
155         down_button.set_parent(this);
156         down_button.set_hotkey_if_focus(KEY_DOWN);
157
158         horz = 0;
159         start = _start;
160         stop = _stop;
161         position = _position;
162         window_size = _window_size;
163         bar_length = h;
164         bar_position =  0;
165
166         Assert( stop >= 0 );
167
168         if (stop != start)
169                 bar_size = (window_size * h) / (stop - start + window_size + 1);
170         else
171                 bar_size = h;
172
173         if (bar_size < 7)
174                 bar_size = 7;
175
176         bar_position = position - start;
177         bar_position *= h - bar_size;
178         bar_position /= stop - start;
179
180         dragging = 0;
181         last_scrolled = 0;
182         moved = 1;
183 };
184
185 void UI_SCROLLBAR::draw()
186 {
187         UI_GADGET::draw();
188
189         if (uses_bmaps) {
190                 gr_reset_clip();
191                 if (disabled_flag) {
192                         if ( bmap_ids[SB_DISABLED] != -1 ) {
193                                 gr_set_bitmap(bmap_ids[SB_DISABLED]);
194                                 gr_bitmap(x,y);
195                         }
196
197                 } else {
198                         if ( bmap_ids[SB_NORMAL] != -1 ) {
199                                 gr_set_bitmap(bmap_ids[SB_NORMAL]);
200                                 gr_bitmap(x,y);
201                         }
202                 }
203
204                 gr_set_clip( x, y, w, h );
205                 ui_draw_box_out( 0, bar_position, w - 1, bar_position + bar_size - 1 );
206
207         } else {
208                 gr_set_font(my_wnd->f_id);
209                 gr_set_clip( x, y, w, h );
210
211                 if (my_wnd->selected_gadget == this)
212                         gr_set_color_fast(&CBRIGHT_GREEN);
213                 else
214                         gr_set_color_fast(&CGRAY);
215
216         /*
217                 ui_rect( 0, 0, w-1, bar_position-1 );
218                 ui_rect( 0, bar_position+bar_size, w-1, h-1);
219         */
220                 ui_rect( 0, 0, w-1, h-1 );
221                 ui_draw_box_out( 0, bar_position, w - 1, bar_position + bar_size - 1 );
222         }
223 }
224
225 void UI_SCROLLBAR::process(int focus)
226 {
227         int OnMe, OnSlider;
228         int oldpos, op;
229
230         moved = 0;
231         if (disabled_flag) {
232                 return;
233         }
234
235         if (my_wnd->selected_gadget == this)
236                 focus = 1;
237
238         up_button.process(focus);
239         down_button.process(focus);
240
241         if (start == stop) {
242                 position = 0;
243                 bar_position = 0;
244                 return;
245         }
246
247         op = position;
248         oldpos = bar_position;
249
250         if (up_button.pressed()) {
251                 position--;
252                 if (position < start)
253                         position = start;
254
255                 bar_position = position - start;
256                 bar_position *= h - bar_size;
257                 bar_position /= stop - start;
258                 set_focus();
259         }
260 /*
261         if ( (up_button.position != 0) || (focus && keyd_pressed[KEY_UP]) ) {
262                 if ( (timer_get_milliseconds() > last_scrolled + 50) || up_button.just_pressed() ) {
263                         if ( up_button.just_pressed() ) {
264                                 last_scrolled = timer_get_milliseconds() + 300;
265                         } else
266                                 last_scrolled = timer_get_milliseconds();
267
268                         position--;
269                         if (position < start)
270                                 position = start;
271
272                         bar_position = position - start;
273                         bar_position *= h - bar_size;
274                         bar_position /= stop - start;
275                 }
276         }*/
277
278         if (down_button.pressed()) {
279                 position++;
280                 if (position > stop)
281                         position = stop;
282
283                 bar_position = position - start;
284                 bar_position *= h - bar_size;
285                 bar_position /= stop - start;
286                 set_focus();
287         }
288
289 /*      if ( down_button.position || (keyfocus && keyd_pressed[KEY_DOWN]) ) {
290                 if ( (timer_get_milliseconds() > last_scrolled + 50) || down_button.just_pressed() ) {
291                         if ( down_button.just_pressed() )
292                                 last_scrolled = timer_get_milliseconds() + 300;
293                         else
294                                 last_scrolled = timer_get_milliseconds();
295
296                         position++;
297                         if (position > stop )
298                                 position = stop;
299
300                         bar_position = position-start;
301                         bar_position *= h-bar_size;
302                         bar_position /= (stop-start);
303                 }
304         }*/
305
306         OnMe = is_mouse_on();
307
308         if (!B1_PRESSED)
309                 dragging = 0;
310
311         OnSlider = 0;
312         if ( (ui_mouse.y >= bar_position + y) && (ui_mouse.y < bar_position + y + bar_size) && OnMe )
313                 OnSlider = 1;
314
315         if (B1_JUST_PRESSED && OnSlider) {
316                 dragging = 1;
317                 drag_x = ui_mouse.x;
318                 drag_y = ui_mouse.y;
319                 drag_starting = bar_position;
320                 set_focus();
321         }
322
323         if ( B1_PRESSED && OnMe && !OnSlider && (timer_get_milliseconds() > last_scrolled + 1000 / (18*4)) ) {
324                 last_scrolled = timer_get_milliseconds();
325
326                 if ( ui_mouse.y < bar_position+y )      {
327                         // Page Up
328                         position -= window_size;
329                         if (position < start)
330                                 position = start;
331
332                 } else {
333                         // Page Down
334                         position += window_size;
335                         if (position > stop)
336                                 position = stop;
337                 }
338
339                 bar_position = position - start;
340                 bar_position *= h - bar_size;
341                 bar_position /= stop - start;
342                 set_focus();
343         }
344
345         if (B1_PRESSED && dragging) {
346                 bar_position = drag_starting + ui_mouse.y - drag_y;
347
348                 if (bar_position < 0) {
349                         bar_position = 0;
350                 }
351
352                 if (bar_position > h - bar_size) {
353                         bar_position = h - bar_size;
354                 }
355
356                 position = bar_position;
357                 position *= stop - start;
358                 position /= h - bar_size;
359                 position += start;
360
361                 if (position > stop)
362                         position = stop;
363
364                 if (position < start)
365                         position = start;
366
367                 set_focus();
368         }
369
370         if (op != position)
371                 moved = 1;
372         else
373                 moved = 0;
374 }
375
376 int UI_SCROLLBAR::getpos()
377 {
378         return position;
379 }
380
381 int UI_SCROLLBAR::changed()
382 {
383         return moved;
384 }
385