2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
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
10 * $Logfile: /Freespace2/code/UI/LISTBOX.cpp $
15 * Code to implement a listbox gadget.
18 * Revision 1.4 2004/07/04 11:39:06 taylor
19 * fix missing debrief text, crash on exit, path separator's, warning fixes, no GR_SOFT
21 * Revision 1.3 2002/06/09 04:41:29 relnev
22 * added copyright header
24 * Revision 1.2 2002/05/07 03:16:53 theoddone33
25 * The Great Newline Fix
27 * Revision 1.1.1.1 2002/05/03 03:28:11 root
31 * 4 12/02/98 5:47p Dave
32 * Put in interface xstr code. Converted barracks screen to new format.
34 * 3 10/13/98 9:29a Dave
35 * Started neatening up freespace.h. Many variables renamed and
36 * reorganized. Added AlphaColors.[h,cpp]
38 * 2 10/07/98 10:54a Dave
41 * 1 10/07/98 10:51a Dave
43 * 20 3/23/98 5:48p Hoffoss
44 * Improved listbox handling. Most notibly the scrollbar arrows work now.
46 * 19 2/03/98 4:21p Hoffoss
47 * Made UI controls draw white text when disabled.
49 * 18 1/20/98 10:36a Hoffoss
50 * Fixed optimized warnings.
52 * 17 1/14/98 6:43p Hoffoss
53 * Massive changes to UI code. A lot cleaner and better now. Did all
54 * this to get the new UI_DOT_SLIDER to work properly, which the old code
55 * wasn't flexible enough to handle.
57 * 16 9/18/97 10:31p Lawrance
58 * allow listbox to change text
60 * 15 9/09/97 4:32p Dave
61 * Added sel_changed() function to UI_LISTBOX to check is the selection
64 * 14 9/09/97 3:39p Sandeep
65 * warning level 4 bugs
67 * 13 8/19/97 1:27p Dave
68 * Made it possible to create an empty list box and summarily add items.
70 * 12 8/14/97 5:23p Dave
71 * Added clear_all_items() to the UI_LISTBOX
73 * 11 6/26/97 5:53p Lawrance
74 * change code slightly, so current_item is not forced to 0 when was -1
76 * 10 6/12/97 12:39p John
77 * made ui use freespace colors
79 * 9 6/11/97 1:13p John
80 * Started fixing all the text colors in the game.
82 * 8 5/26/97 10:26a Lawrance
83 * get slider control working 100%
85 * 7 4/22/97 10:11a John
86 * Added checkbox lists to listboxes
88 * 6 4/15/97 3:47p Allender
89 * moved type selection of list box items into actual UI code. Made it
90 * behave more like windows listboxes do
92 * 5 12/23/96 2:42p Lawrance
93 * allowing keys to select list box items in the mission load screen
95 * 4 12/02/96 2:17p John
96 * Made right button drag UI gadgets around and
97 * Ctrl+Shift+Alt+F12 dumps out where they are.
99 * 3 12/01/96 3:48a Lawrance
100 * added function set_current to UI_LISTBOX
102 * 2 11/15/96 11:43a John
104 * 1 11/14/96 6:55p John
113 #include "alphacolors.h"
115 #define KEY_BUFFER_TIMEOUT 1000 // time to clear buffer in milliseconds
117 #define DEFAULT_LISTBOX_ITEM_LENGTH 40
119 // --------------------------------------------------------------------
120 // UI_LISTBOX::link_hotspot
123 void UI_LISTBOX::link_hotspot(int up_button_num, int down_button_num)
125 scrollbar.link_hotspot(up_button_num,down_button_num);
128 // --------------------------------------------------------------------
129 // UI_LISTBOX::set_bmaps
131 // Call the UI_SCROLLBAR::set_bmaps() function for the scroll bar gadget.
133 // returns: -1 ==> error
136 int UI_LISTBOX::set_bmaps(char *lbox_fname, char *b_up_fname, char *b_down_fname, char *sb_fname)
139 scrollbar.set_bmaps(b_up_fname, b_down_fname, sb_fname);
142 // set the bitmaps for the list box rectangle
143 UI_GADGET::set_bmaps(lbox_fname);
148 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 gr_set_font(wnd->f_id);
154 gr_get_string_size(&tw, &th, "*");
159 base_create( wnd, UI_KIND_LISTBOX, _x, _y, _w, real_h );
161 max_items = _max_items;
163 num_items = _numitems;
164 check_list = _check_list;
165 num_items_displayed = nrows;
174 key_buffer_count = 0;
175 last_typed = timer_get_milliseconds();
177 if (_numitems > nrows) {
178 scrollbar.create( wnd, _x+_w+3, _y, real_h, 0, _numitems-nrows, 0, nrows );
179 scrollbar.set_parent(this);
187 void UI_LISTBOX::draw()
193 gr_set_font(my_wnd->f_id);
197 if ( bmap_ids[LBOX_DISABLED] >= 0 ) {
198 gr_set_bitmap(bmap_ids[LBOX_DISABLED]);
203 if ( bmap_ids[LBOX_NORMAL] >= 0 ) {
204 gr_set_bitmap(bmap_ids[LBOX_NORMAL]);
210 gr_set_color_fast(&CBLACK);
211 gr_set_clip( x, y, w, h );
212 ui_rect( 0, 0, w-1, h-1 );
215 ui_draw_sunken_border( x-2, y-2, x+w+scrollbar.w+4, y+h+1 );
218 ui_draw_sunken_border( x-2, y-2, x+w+4, y+h+1 );
222 stop = first_item+num_items_displayed;
223 if (stop>num_items) stop = num_items;
226 gr_set_clip( x, y, w, h );
228 for ( i=first_item; i<stop; i++ ) {
229 gr_get_string_size( &w1, &h1,list[i] );
234 if (i !=current_item) {
236 if ((current_item == -1) && (my_wnd->selected_gadget == this ) && (i == first_item) ) {
238 gr_set_color_fast( &CBLACK );
239 gr_rect( x1, y1, w1+2, h1 );
241 current_item = first_item;
242 gr_set_color_fast( &CBRIGHT_GREEN );
245 gr_set_color_fast( &CBLACK );
246 gr_rect( x1, y1, w1+2, h1 );
248 gr_set_color_fast( &CWHITE );
252 gr_set_color_fast( &CBLACK );
253 gr_rect( x1, y1, w1+2, h1 );
256 gr_set_color_fast(&CWHITE);
259 if (my_wnd->selected_gadget == this) {
260 gr_set_color_fast( &CGRAY );
261 gr_rect( x1, y1, w1+2, h1 );
262 gr_set_color_fast( &CBRIGHT_GREEN );
265 gr_set_color_fast( &CGRAY );
266 gr_rect( x1, y1, w1+2, h1 );
267 gr_set_color_fast( &CBLACK );
272 if ( check_list[i] ) {
273 gr_string( x1+2, y1, "X" );
276 gr_string( x1+16, y1, list[i] );
279 gr_string( x1+2, y1, list[i] );
282 gr_set_color_fast( &CGRAY );
284 gr_set_color_fast( &CBLACK );
287 ui_rect( x1+w1+2, y1, w-1, y1+h1-1 );
288 ui_rect( x1, y1, x1+1, y1+h1-1 );
294 if (stop < num_items_displayed-1 && !uses_bmaps) {
295 gr_set_color_fast(&CBLACK);
296 ui_rect( x1, y1, w-1, h-1 );
300 void UI_LISTBOX::process(int focus)
302 int OnMe, mitem, oldbarpos, kf = 0;
311 if (my_wnd->selected_gadget == this)
315 scrollbar.process(0);
316 if (my_wnd->selected_gadget == &scrollbar) {
325 old_current_item = current_item;
326 old_first_item = first_item;
328 // if (my_wnd->selected_gadget == this) {
329 // my_wnd->selected_gadget == get_next();
335 old_current_item = current_item;
336 old_first_item = first_item;
338 OnMe = is_mouse_on();
341 if (scrollbar.moved) {
342 first_item = scrollbar.position;
343 Assert(first_item >= 0);
345 if (current_item<first_item)
346 current_item = first_item;
348 if (current_item > first_item + num_items_displayed - 1)
349 current_item = first_item + num_items_displayed - 1;
356 if (B1_PRESSED && OnMe) {
361 if ( key_buffer_count && (timer_get_milliseconds() > last_typed + KEY_BUFFER_TIMEOUT) )
362 key_buffer_count = 0;
365 if (my_wnd->keypress) {
368 switch (my_wnd->keypress) {
370 selected_item = current_item;
374 toggled_item = current_item;
393 current_item=num_items - 1;
398 current_item -= num_items_displayed;
403 current_item += num_items_displayed;
407 default: // enter the key in the key buffer
408 if (my_wnd->keypress == KEY_BACKSP) {
409 key_buffer_count = 0;
411 } else if (key_buffer_count < MAX_KEY_BUFFER) {
412 key_buffer[key_buffer_count++] = (char) my_wnd->keypress;
413 last_typed = timer_get_milliseconds();
416 if (!key_buffer_count)
419 for (i=0; i<num_items; i++) {
422 current_text = get_string(i);
423 for (j=0; j<key_buffer_count; j++)
424 if ( (current_text[j] != ascii_table[(int)key_buffer[j]]) && (current_text[j] != shifted_ascii_table[(int)key_buffer[j]]) )
427 if (j == key_buffer_count) {
428 set_first_item(i - num_items_displayed / 2);
437 if (current_item < 0)
440 if (current_item >= num_items)
441 current_item = num_items - 1;
443 if (current_item < first_item)
444 first_item = current_item;
446 if (current_item >= first_item + num_items_displayed)
447 first_item = current_item - num_items_displayed + 1;
449 if (num_items <= num_items_displayed ) {
454 oldbarpos = scrollbar.position;
455 scrollbar.position = first_item;
457 scrollbar.bar_position = scrollbar.position - scrollbar.start;
458 scrollbar.bar_position *= scrollbar.h - scrollbar.bar_size;
459 scrollbar.bar_position /= scrollbar.stop - scrollbar.start;
461 if (scrollbar.bar_position < 0) {
462 scrollbar.bar_position = 0;
465 if (scrollbar.bar_position > scrollbar.h - scrollbar.bar_size) {
466 scrollbar.bar_position = scrollbar.h - scrollbar.bar_size;
475 if (B1_PRESSED && dragging) {
479 mitem = (ui_mouse.y - y)/textheight;
481 if ( (mitem < 0) && (timer_get_milliseconds() > last_scrolled + 1000 / 18) ) {
483 last_scrolled = timer_get_milliseconds();
486 if ( (mitem >= num_items_displayed) && (timer_get_milliseconds() > last_scrolled + 1000 / 18) ) {
488 last_scrolled = timer_get_milliseconds();
491 if ((mitem >= 0) && (mitem<num_items_displayed)) {
492 current_item = mitem + first_item;
495 if (current_item < 0)
498 if (current_item >= num_items)
499 current_item = num_items - 1;
501 if (current_item < first_item)
502 first_item = current_item;
504 if (current_item >= first_item + num_items_displayed)
505 first_item = current_item - num_items_displayed + 1;
507 if (num_items <= num_items_displayed) {
510 } else if (has_scrollbar) {
511 oldbarpos = scrollbar.position;
512 scrollbar.position = first_item;
514 scrollbar.bar_position = scrollbar.position - scrollbar.start;
515 scrollbar.bar_position *= scrollbar.h - scrollbar.bar_size;
516 scrollbar.bar_position /= scrollbar.stop - scrollbar.start;
518 if (scrollbar.bar_position < 0) {
519 scrollbar.bar_position = 0;
522 if (scrollbar.bar_position > scrollbar.h - scrollbar.bar_size) {
523 scrollbar.bar_position = scrollbar.h - scrollbar.bar_size;
529 if (B1_JUST_RELEASED)
530 toggled_item = current_item;
533 if (B1_DOUBLE_CLICKED) {
534 selected_item = current_item;
539 int UI_LISTBOX::toggled()
548 int UI_LISTBOX::selected()
553 return selected_item;
557 int UI_LISTBOX::current()
562 void UI_LISTBOX::set_current(int _index)
564 current_item = _index;
567 void UI_LISTBOX::set_first_item(int _index)
571 else if (_index > num_items)
577 char *UI_LISTBOX::get_string(int _index)
582 void UI_LISTBOX::clear_all_items()
586 for ( idx=0; idx<num_items; idx++ )
593 void UI_LISTBOX::set_new_list(int _numitems, char **_list)
595 num_items = _numitems;
600 int UI_LISTBOX::add_string(char *str)
602 if (max_items < 0) // only if we created an "empty" listbox can we add items
606 if ( (num_items == max_items - 1) || (strlen(str) > DEFAULT_LISTBOX_ITEM_LENGTH) )
607 return 0; // we've reached our limit
610 list[num_items] = strdup(str);
617 int UI_LISTBOX::sel_changed()
619 return old_current_item == current_item ? 0 : 1;