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