]> icculus.org git repositories - taylor/freespace2.git/blob - src/ui/scroll.cpp
silence various clang warnings
[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 #include "font.h"
96
97
98 // --------------------------------------------------------------------
99 // UI_SCROLLBAR::link_hotspot
100 //
101 //
102 void UI_SCROLLBAR::link_hotspot(int up_button_num, int down_button_num)
103 {
104         up_button.link_hotspot(up_button_num);
105         down_button.link_hotspot(down_button_num);
106 }
107
108 // --------------------------------------------------------------------
109 // UI_SCROLLBAR::set_bmaps
110 //
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
113 // line itself.
114 //
115 // We also need to get the dimensions of the bitmap button so we can update
116 // the dimensions of the scrollbar.
117 //
118 // returns:             -1 ==> error
119 //                                       0 ==> success
120 //
121 int UI_SCROLLBAR::set_bmaps(const char *up_button_fname, const char *down_button_fname, const char *line_fname)
122 {
123         int bx, by, bh, bw;
124
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);
128         
129         // set the bitmaps for the rectangle that is the scrollbar itself
130         ((UI_GADGET*)this)->set_bmaps(line_fname);
131         uses_bmaps = 1;
132
133         update_dimensions(x,y+bw,bw,h-bw*2);
134
135         return 0;
136 }
137
138 void UI_SCROLLBAR::hide(int n)
139 {
140         hidden = 1;
141         up_button.hide();
142         down_button.hide();
143 }
144
145 void UI_SCROLLBAR::unhide()
146 {
147         hidden = 0;
148         up_button.unhide();
149         down_button.unhide();
150 }
151
152 int UI_SCROLLBAR::get_hidden()
153 {
154         return hidden;
155 }
156
157 void UI_SCROLLBAR::create(UI_WINDOW *wnd, int _x, int _y, int _h, int _start, int _stop, int _position, int _window_size)
158 {
159         const char *up = "^";
160         const char *down = "v";
161         int bw = 20;
162
163         base_create( wnd, UI_KIND_SCROLLBAR, _x, _y + bw, bw, _h - bw * 2 );
164
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);
168
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);
172
173         horz = 0;
174         start = _start;
175         stop = _stop;
176         position = _position;
177         window_size = _window_size;
178         bar_length = h;
179         bar_position =  0;
180
181         SDL_assert( stop >= 0 );
182
183         if (stop != start)
184                 bar_size = (window_size * h) / (stop - start + window_size + 1);
185         else
186                 bar_size = h;
187
188         if (bar_size < 7)
189                 bar_size = 7;
190
191         bar_position = position - start;
192         bar_position *= h - bar_size;
193         bar_position /= stop - start;
194
195         dragging = 0;
196         last_scrolled = 0;
197         moved = 1;
198 };
199
200 void UI_SCROLLBAR::draw()
201 {
202         UI_GADGET::draw();
203
204         if (uses_bmaps) {
205                 gr_reset_clip();
206                 if (disabled_flag) {
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);
209                                 gr_bitmap(x,y);
210                         }
211
212                 } else {
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);
215                                 gr_bitmap(x,y);
216                         }
217                 }
218
219                 gr_set_clip( x, y, w, h );
220                 ui_draw_box_out( 0, bar_position, w - 1, bar_position + bar_size - 1 );
221
222         } else {
223                 gr_set_font(my_wnd->f_id);
224                 gr_set_clip( x, y, w, h );
225
226                 if (my_wnd->selected_gadget == this)
227                         gr_set_color_fast(&CBRIGHT_GREEN);
228                 else
229                         gr_set_color_fast(&CGRAY);
230
231         /*
232                 ui_rect( 0, 0, w-1, bar_position-1 );
233                 ui_rect( 0, bar_position+bar_size, w-1, h-1);
234         */
235                 ui_rect( 0, 0, w-1, h-1 );
236                 ui_draw_box_out( 0, bar_position, w - 1, bar_position + bar_size - 1 );
237         }
238 }
239
240 void UI_SCROLLBAR::process(int focus)
241 {
242         int OnMe, OnSlider;
243         int oldpos, op;
244
245         moved = 0;
246         if (disabled_flag) {
247                 return;
248         }
249
250         if (my_wnd->selected_gadget == this)
251                 focus = 1;
252
253         up_button.process(focus);
254         down_button.process(focus);
255
256         if (start == stop) {
257                 position = 0;
258                 bar_position = 0;
259                 return;
260         }
261
262         op = position;
263         oldpos = bar_position;
264
265         if (up_button.pressed()) {
266                 position--;
267                 if (position < start)
268                         position = start;
269
270                 bar_position = position - start;
271                 bar_position *= h - bar_size;
272                 bar_position /= stop - start;
273                 set_focus();
274         }
275 /*
276         if ( (up_button.position != 0) || (focus && key_pressed(SDLK_UP)) ) {
277                 if ( (timer_get_milliseconds() > last_scrolled + 50) || up_button.just_pressed() ) {
278                         if ( up_button.just_pressed() ) {
279                                 last_scrolled = timer_get_milliseconds() + 300;
280                         } else
281                                 last_scrolled = timer_get_milliseconds();
282
283                         position--;
284                         if (position < start)
285                                 position = start;
286
287                         bar_position = position - start;
288                         bar_position *= h - bar_size;
289                         bar_position /= stop - start;
290                 }
291         }*/
292
293         if (down_button.pressed()) {
294                 position++;
295                 if (position > stop)
296                         position = stop;
297
298                 bar_position = position - start;
299                 bar_position *= h - bar_size;
300                 bar_position /= stop - start;
301                 set_focus();
302         }
303
304 /*      if ( down_button.position || (keyfocus && key_pressed(SDLK_DOWN)) ) {
305                 if ( (timer_get_milliseconds() > last_scrolled + 50) || down_button.just_pressed() ) {
306                         if ( down_button.just_pressed() )
307                                 last_scrolled = timer_get_milliseconds() + 300;
308                         else
309                                 last_scrolled = timer_get_milliseconds();
310
311                         position++;
312                         if (position > stop )
313                                 position = stop;
314
315                         bar_position = position-start;
316                         bar_position *= h-bar_size;
317                         bar_position /= (stop-start);
318                 }
319         }*/
320
321         OnMe = is_mouse_on();
322
323         if (!B1_PRESSED)
324                 dragging = 0;
325
326         OnSlider = 0;
327         if ( (ui_mouse.y >= bar_position + y) && (ui_mouse.y < bar_position + y + bar_size) && OnMe )
328                 OnSlider = 1;
329
330         if (B1_JUST_PRESSED && OnSlider) {
331                 dragging = 1;
332                 drag_x = ui_mouse.x;
333                 drag_y = ui_mouse.y;
334                 drag_starting = bar_position;
335                 set_focus();
336         }
337
338         if ( B1_PRESSED && OnMe && !OnSlider && (timer_get_milliseconds() > last_scrolled + 1000 / (18*4)) ) {
339                 last_scrolled = timer_get_milliseconds();
340
341                 if ( ui_mouse.y < bar_position+y )      {
342                         // Page Up
343                         position -= window_size;
344                         if (position < start)
345                                 position = start;
346
347                 } else {
348                         // Page Down
349                         position += window_size;
350                         if (position > stop)
351                                 position = stop;
352                 }
353
354                 bar_position = position - start;
355                 bar_position *= h - bar_size;
356                 bar_position /= stop - start;
357                 set_focus();
358         }
359
360         if (B1_PRESSED && dragging) {
361                 bar_position = drag_starting + ui_mouse.y - drag_y;
362
363                 if (bar_position < 0) {
364                         bar_position = 0;
365                 }
366
367                 if (bar_position > h - bar_size) {
368                         bar_position = h - bar_size;
369                 }
370
371                 position = bar_position;
372                 position *= stop - start;
373                 position /= h - bar_size;
374                 position += start;
375
376                 if (position > stop)
377                         position = stop;
378
379                 if (position < start)
380                         position = start;
381
382                 set_focus();
383         }
384
385         if (op != position)
386                 moved = 1;
387         else
388                 moved = 0;
389 }
390
391 int UI_SCROLLBAR::getpos()
392 {
393         return position;
394 }
395
396 int UI_SCROLLBAR::changed()
397 {
398         return moved;
399 }
400