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