]> icculus.org git repositories - taylor/freespace2.git/blob - src/ui/listbox.cpp
use a better multi_sw_ok_to_commit() check
[taylor/freespace2.git] / src / ui / listbox.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/LISTBOX.cpp $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
15  * Code to implement a listbox gadget.
16  *
17  * $Log$
18  * Revision 1.5  2004/09/20 01:31:45  theoddone33
19  * GCC 3.4 fixes.
20  *
21  * Revision 1.4  2004/07/04 11:39:06  taylor
22  * fix missing debrief text, crash on exit, path separator's, warning fixes, no GR_SOFT
23  *
24  * Revision 1.3  2002/06/09 04:41:29  relnev
25  * added copyright header
26  *
27  * Revision 1.2  2002/05/07 03:16:53  theoddone33
28  * The Great Newline Fix
29  *
30  * Revision 1.1.1.1  2002/05/03 03:28:11  root
31  * Initial import.
32  *
33  * 
34  * 4     12/02/98 5:47p Dave
35  * Put in interface xstr code. Converted barracks screen to new format.
36  * 
37  * 3     10/13/98 9:29a Dave
38  * Started neatening up freespace.h. Many variables renamed and
39  * reorganized. Added AlphaColors.[h,cpp]
40  * 
41  * 2     10/07/98 10:54a Dave
42  * Initial checkin.
43  * 
44  * 1     10/07/98 10:51a Dave
45  * 
46  * 20    3/23/98 5:48p Hoffoss
47  * Improved listbox handling.  Most notibly the scrollbar arrows work now.
48  * 
49  * 19    2/03/98 4:21p Hoffoss
50  * Made UI controls draw white text when disabled.
51  * 
52  * 18    1/20/98 10:36a Hoffoss
53  * Fixed optimized warnings.
54  * 
55  * 17    1/14/98 6:43p Hoffoss
56  * Massive changes to UI code.  A lot cleaner and better now.  Did all
57  * this to get the new UI_DOT_SLIDER to work properly, which the old code
58  * wasn't flexible enough to handle.
59  * 
60  * 16    9/18/97 10:31p Lawrance
61  * allow listbox to change text
62  * 
63  * 15    9/09/97 4:32p Dave
64  * Added sel_changed() function to UI_LISTBOX to check is the selection
65  * has changed.
66  * 
67  * 14    9/09/97 3:39p Sandeep
68  * warning level 4 bugs
69  * 
70  * 13    8/19/97 1:27p Dave
71  * Made it possible to create an empty list box and summarily add items.
72  * 
73  * 12    8/14/97 5:23p Dave
74  * Added clear_all_items() to the UI_LISTBOX
75  * 
76  * 11    6/26/97 5:53p Lawrance
77  * change code slightly, so current_item is not forced to 0 when was -1
78  * 
79  * 10    6/12/97 12:39p John
80  * made ui use freespace colors
81  * 
82  * 9     6/11/97 1:13p John
83  * Started fixing all the text colors in the game.
84  * 
85  * 8     5/26/97 10:26a Lawrance
86  * get slider control working 100%
87  * 
88  * 7     4/22/97 10:11a John
89  * Added checkbox lists to listboxes
90  * 
91  * 6     4/15/97 3:47p Allender
92  * moved type selection of list box items into actual UI code.  Made it
93  * behave more like windows listboxes do
94  * 
95  * 5     12/23/96 2:42p Lawrance
96  * allowing keys to select list box items in the mission load screen
97  * 
98  * 4     12/02/96 2:17p John
99  * Made right button drag UI gadgets around and
100  * Ctrl+Shift+Alt+F12 dumps out where they are.
101  * 
102  * 3     12/01/96 3:48a Lawrance
103  * added function set_current to UI_LISTBOX
104  * 
105  * 2     11/15/96 11:43a John
106  * 
107  * 1     11/14/96 6:55p John
108  *
109  * $NoKeywords: $
110  */
111
112 #include "uidefs.h"
113 #include "ui.h"
114 #include "timer.h"
115 #include "key.h"
116 #include "alphacolors.h"
117 #include "font.h"
118
119 #define KEY_BUFFER_TIMEOUT              1000            // time to clear buffer in milliseconds
120
121 #define DEFAULT_LISTBOX_ITEM_LENGTH 40
122
123 // --------------------------------------------------------------------
124 // UI_LISTBOX::link_hotspot
125 //
126 //
127 void UI_LISTBOX::link_hotspot(int up_button_num, int down_button_num)
128 {
129         scrollbar.link_hotspot(up_button_num,down_button_num);
130 }
131
132 // --------------------------------------------------------------------
133 // UI_LISTBOX::set_bmaps
134 //
135 // Call the UI_SCROLLBAR::set_bmaps() function for the scroll bar gadget.
136 //
137 // returns:             -1 ==> error
138 //                                       0 ==> success
139 //
140 int UI_LISTBOX::set_bmaps(const char *lbox_fname, const char *b_up_fname, const char *b_down_fname, const char *sb_fname)
141 {
142         if (has_scrollbar) {
143                 scrollbar.set_bmaps(b_up_fname, b_down_fname, sb_fname);
144         }
145         
146         // set the bitmaps for the list box rectangle
147         UI_GADGET::set_bmaps(lbox_fname);
148         uses_bmaps = 1;
149         return 0;
150 }
151
152 void UI_LISTBOX::create(UI_WINDOW *wnd, int _x, int _y, int _w, int _h, int _numitems, char **_list, char *_check_list, int _max_items)
153 {
154         int tw, th, nrows;
155         int real_h;
156
157         gr_set_font(wnd->f_id);
158         gr_get_string_size(&tw, &th, "*");
159
160         nrows = _h / th;
161         real_h = nrows * th;
162
163         base_create( wnd, UI_KIND_LISTBOX, _x, _y, _w, real_h );
164
165    max_items = _max_items;      
166    list = _list;
167         num_items = _numitems;
168         check_list = _check_list;       
169         num_items_displayed = nrows;
170
171         first_item = 0;
172         current_item = -1;
173         toggled_item = -1;
174         last_scrolled = 0;
175         textheight = th;
176         dragging = 0;
177         selected_item = -1;
178         key_buffer_count = 0;
179         last_typed = timer_get_milliseconds();
180
181         if (_numitems > nrows) {
182                 scrollbar.create( wnd, _x+_w+3, _y, real_h, 0, _numitems-nrows, 0, nrows );
183                 scrollbar.set_parent(this);
184                 has_scrollbar = 1;
185
186         } else {
187                 has_scrollbar = 0;
188         }
189 };
190
191 void UI_LISTBOX::draw()
192 {
193         int i, x1, y1, stop;
194         int w1, h1;
195
196         UI_GADGET::draw();
197         gr_set_font(my_wnd->f_id);
198
199         if (uses_bmaps) {
200                 if (disabled_flag) {
201                         if ( bmap_ids[LBOX_DISABLED] >= 0 ) {
202                                 gr_set_bitmap(bmap_ids[LBOX_DISABLED], GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
203                                 gr_bitmap(x, y);
204                         }
205
206                 } else {
207                         if ( bmap_ids[LBOX_NORMAL] >= 0 ) {
208                                 gr_set_bitmap(bmap_ids[LBOX_NORMAL], GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
209                                 gr_bitmap(x, y);
210                         }
211                 }
212
213         } else {
214                 gr_set_color_fast(&CBLACK);
215                 gr_set_clip( x, y, w, h );
216                 ui_rect( 0, 0, w-1, h-1 );
217                 gr_reset_clip();                
218                 if (has_scrollbar) {
219                         ui_draw_sunken_border( x-2, y-2, x+w+scrollbar.w+4, y+h+1 );
220
221                 } else {
222                         ui_draw_sunken_border( x-2, y-2, x+w+4, y+h+1 );
223                 }
224         }
225
226         stop = first_item+num_items_displayed;
227         if (stop>num_items) stop = num_items;
228
229         x1 = y1 = 0;
230         gr_set_clip( x, y, w, h );
231
232         for ( i=first_item; i<stop; i++ ) {
233                 gr_get_string_size( &w1, &h1,list[i] );
234
235                 if (check_list)
236                         w1 += 18;
237
238                 if (i !=current_item) {
239 /*
240                         if ((current_item == -1) && (my_wnd->selected_gadget == this ) && (i == first_item)  )  {
241                                 if ( !uses_bmaps ) {
242                                         gr_set_color_fast( &CBLACK );
243                                         gr_rect( x1, y1, w1+2, h1 );
244                                 }
245                                 current_item = first_item;
246                                 gr_set_color_fast( &CBRIGHT_GREEN );
247                         } else {
248                                 if ( !uses_bmaps ) {
249                                         gr_set_color_fast( &CBLACK );
250                                         gr_rect( x1, y1, w1+2, h1 );
251                                 }
252                                 gr_set_color_fast( &CWHITE );
253                         }
254 */
255                         if (!uses_bmaps) {
256                                 gr_set_color_fast( &CBLACK );
257                                 gr_rect( x1, y1, w1+2, h1 );
258                         }
259
260                         gr_set_color_fast(&CWHITE);
261
262                 } else {
263                         if (my_wnd->selected_gadget == this) {
264                                 gr_set_color_fast( &CGRAY );
265                                 gr_rect( x1, y1, w1+2, h1 );
266                                 gr_set_color_fast( &CBRIGHT_GREEN );
267
268                         } else {
269                                 gr_set_color_fast( &CGRAY );
270                                 gr_rect( x1, y1, w1+2, h1 );
271                                 gr_set_color_fast( &CBLACK );
272                         }
273                 }
274
275                 if ( check_list )       {
276                         if ( check_list[i] )    {
277                                 gr_string( x1+2, y1, "X" );
278                         }
279
280                         gr_string( x1+16, y1, list[i] );
281
282                 } else
283                         gr_string( x1+2, y1, list[i] );
284
285                 if (i==current_item)
286                         gr_set_color_fast( &CGRAY );
287                 else
288                         gr_set_color_fast( &CBLACK );
289
290                 if ( !uses_bmaps ) {
291                         ui_rect( x1+w1+2, y1, w-1, y1+h1-1 );
292                         ui_rect( x1, y1, x1+1, y1+h1-1 );
293                 }
294
295                 y1 += h1;
296         }
297
298         if (stop < num_items_displayed-1 && !uses_bmaps) {
299                 gr_set_color_fast(&CBLACK);
300                 ui_rect( x1, y1, w-1, h-1 );
301         }
302 }
303
304 void UI_LISTBOX::process(int focus)
305 {
306         int OnMe, mitem, kf = 0;
307         int i, j;
308
309         selected_item = -1;
310         toggled_item = -1;
311
312         if (disabled_flag)
313                 return;
314
315         if (my_wnd->selected_gadget == this)
316                 focus = 1;
317
318         if (has_scrollbar) {
319                 scrollbar.process(0);
320                 if (my_wnd->selected_gadget == &scrollbar) {
321                         set_focus();
322                         focus = 1;
323                 }
324         }
325
326         if (num_items < 1) {
327                 current_item = -1;
328                 first_item = 0;
329                 old_current_item = current_item;
330                 old_first_item = first_item;
331                 
332 //              if (my_wnd->selected_gadget == this) {
333 //                      my_wnd->selected_gadget == get_next();
334 //              }
335
336                 return;
337         }
338
339         old_current_item = current_item;
340         old_first_item = first_item;
341
342         OnMe = is_mouse_on();
343
344         if (has_scrollbar) {
345                 if (scrollbar.moved) {
346                         first_item = scrollbar.position;
347                         SDL_assert(first_item >= 0);
348
349                         if (current_item<first_item)
350                                 current_item = first_item;
351
352                         if (current_item > first_item + num_items_displayed - 1)
353                                 current_item = first_item + num_items_displayed - 1;
354                 }
355         }
356
357         if (!B1_PRESSED)
358                 dragging = 0;
359
360         if (B1_PRESSED && OnMe) {
361                 set_focus();
362                 dragging = 1;
363         }
364
365         if ( key_buffer_count && (timer_get_milliseconds() > last_typed + KEY_BUFFER_TIMEOUT) )
366                 key_buffer_count = 0;
367
368         if (focus) {
369                 if (my_wnd->keypress) {
370                         kf = 0;
371
372                         switch (my_wnd->keypress) {
373                                 case SDLK_RETURN:
374                                         selected_item = current_item;
375                                         break;
376
377                                 case SDLK_SPACE:
378                                         toggled_item = current_item;
379                                         break;
380
381                                 case SDLK_UP:
382                                         current_item--;
383                                         kf = 1;
384                                         break;
385
386                                 case SDLK_DOWN:
387                                         current_item++;
388                                         kf = 1;
389                                         break;
390
391                                 case SDLK_HOME:
392                                         current_item = 0;
393                                         kf = 1;
394                                         break;
395
396                                 case SDLK_END:
397                                         current_item=num_items - 1;
398                                         kf = 1;
399                                         break;
400
401                                 case SDLK_PAGEUP:
402                                         current_item -= num_items_displayed;
403                                         kf = 1;
404                                         break;
405
406                                 case SDLK_PAGEDOWN:
407                                         current_item += num_items_displayed;
408                                         kf = 1;
409                                         break;
410
411                                 default:                // enter the key in the key buffer
412                                         if (my_wnd->keypress == SDLK_BACKSPACE) {
413                                                 key_buffer_count = 0;
414
415                                         } else if (key_buffer_count < MAX_KEY_BUFFER) {
416                                                 key_buffer[key_buffer_count++] = (char) my_wnd->keypress_text;
417                                                 last_typed = timer_get_milliseconds();
418                                         }
419
420                                         if (!key_buffer_count)
421                                                 break;
422
423                                         for (i=0; i<num_items; i++) {
424                                                 char *current_text;
425                                                 
426                                                 current_text = get_string(i);
427                                                 for (j=0; j<key_buffer_count; j++)
428                                                         if (current_text[j] != key_buffer[j]) {
429                                                                 break;
430                                                         }
431
432                                                 if (j == key_buffer_count) {
433                                                         set_first_item(i - num_items_displayed / 2);
434                                                         set_current(i);
435                                                         break;
436                                                 }
437                                         }
438                         }
439                 }
440
441                 if (kf == 1) {
442                         if (current_item < 0)
443                                 current_item = 0;
444
445                         if (current_item >= num_items)
446                                 current_item = num_items - 1;
447
448                         if (current_item < first_item)
449                                 first_item = current_item;
450
451                         if (current_item >= first_item + num_items_displayed)
452                                 first_item = current_item - num_items_displayed + 1;
453
454                         if (num_items <= num_items_displayed ) {
455                                 first_item = 0;
456
457                         } else {
458                                 if (has_scrollbar) {
459                                         scrollbar.position = first_item;
460
461                                         scrollbar.bar_position = scrollbar.position - scrollbar.start;
462                                         scrollbar.bar_position *= scrollbar.h - scrollbar.bar_size;
463                                         scrollbar.bar_position /= scrollbar.stop - scrollbar.start;
464
465                                         if (scrollbar.bar_position < 0) {
466                                                 scrollbar.bar_position = 0;
467                                         }
468                 
469                                         if (scrollbar.bar_position > scrollbar.h - scrollbar.bar_size) {
470                                                 scrollbar.bar_position = scrollbar.h - scrollbar.bar_size;
471                                         }
472                                 }       
473         
474                         }
475                 }
476         }
477
478         if (focus) {
479                 if (B1_PRESSED && dragging) {
480                         if (ui_mouse.y < y )
481                                 mitem = -1;
482                         else
483                                 mitem = (ui_mouse.y - y)/textheight;
484
485                         if ( (mitem < 0) && (timer_get_milliseconds() > last_scrolled + 1000 / 18) ) {
486                                 current_item--;
487                                 last_scrolled = timer_get_milliseconds();
488                         }
489
490                         if ( (mitem >= num_items_displayed) && (timer_get_milliseconds() > last_scrolled + 1000 / 18) ) {
491                                 current_item++;
492                                 last_scrolled = timer_get_milliseconds();
493                         }
494
495                         if ((mitem >= 0) && (mitem<num_items_displayed)) {
496                                 current_item = mitem + first_item;
497                         }
498
499                         if (current_item < 0)
500                                 current_item = 0;
501
502                         if (current_item >= num_items)
503                                 current_item = num_items - 1;
504
505                         if (current_item < first_item)
506                                 first_item = current_item;
507
508                         if (current_item >= first_item + num_items_displayed)
509                                 first_item = current_item - num_items_displayed + 1;
510
511                         if (num_items <= num_items_displayed) {
512                                 first_item = 0;
513
514                         } else if (has_scrollbar) {
515                                 scrollbar.position = first_item;
516
517                                 scrollbar.bar_position = scrollbar.position - scrollbar.start;
518                                 scrollbar.bar_position *= scrollbar.h - scrollbar.bar_size;
519                                 scrollbar.bar_position /= scrollbar.stop - scrollbar.start;
520
521                                 if (scrollbar.bar_position < 0) {
522                                         scrollbar.bar_position = 0;
523                                 }
524
525                                 if (scrollbar.bar_position > scrollbar.h - scrollbar.bar_size) {
526                                         scrollbar.bar_position = scrollbar.h - scrollbar.bar_size;
527                                 }
528                         }
529                 }
530
531                 if (check_list) {
532                         if (B1_JUST_RELEASED)
533                                 toggled_item = current_item;
534                 }
535
536                 if (B1_DOUBLE_CLICKED) {
537                         selected_item = current_item;
538                 }
539         }
540 }
541
542 int UI_LISTBOX::toggled()
543 {
544         if (check_list) {
545                 return toggled_item;
546         } else {
547                 return -1;
548         }
549 }
550
551 int UI_LISTBOX::selected()
552 {
553         if (check_list) {
554                 return -1;
555         } else {
556                 return selected_item;
557         }
558 }
559
560 int UI_LISTBOX::current()
561 {
562         return current_item;
563 }
564
565 void UI_LISTBOX::set_current(int _index)
566 {
567         current_item = _index;
568 }
569
570 void UI_LISTBOX::set_first_item(int _index)
571 {
572         if (_index < 0)
573                 _index = 0;
574         else if (_index > num_items)
575                 _index = num_items;
576
577         first_item = _index;
578 }
579
580 char *UI_LISTBOX::get_string(int _index)
581 {
582         return list[_index];
583 }
584
585 void UI_LISTBOX::clear_all_items()
586 {
587    int idx;
588
589         for ( idx=0; idx<num_items; idx++ )
590                 list[idx][0] = 0;
591
592         num_items = 0;
593         first_item = 0;
594 }
595
596 void UI_LISTBOX::set_new_list(int _numitems, char **_list)
597 {
598         num_items = _numitems;
599         list = _list;
600         current_item = 0;
601 }
602
603 int UI_LISTBOX::add_string(char *str)
604 {
605    if (max_items < 0)  // only if we created an "empty" listbox can we add items
606                 return 0;
607
608         else {
609                 if ( (num_items == max_items - 1) || (strlen(str) > DEFAULT_LISTBOX_ITEM_LENGTH) )
610                         return 0;                     // we've reached our limit
611
612                 else {
613                         list[num_items] = strdup(str);
614                         num_items++;
615                         return 1;
616                 }
617         }
618 }
619
620 int UI_LISTBOX::sel_changed()
621 {
622         return old_current_item == current_item ? 0 : 1;
623 }
624