2 * $Logfile: /Freespace2/code/UI/LISTBOX.cpp $
7 * Code to implement a listbox gadget.
10 * Revision 1.1 2002/05/03 03:28:11 root
14 * 4 12/02/98 5:47p Dave
15 * Put in interface xstr code. Converted barracks screen to new format.
17 * 3 10/13/98 9:29a Dave
18 * Started neatening up freespace.h. Many variables renamed and
19 * reorganized. Added AlphaColors.[h,cpp]
21 * 2 10/07/98 10:54a Dave
24 * 1 10/07/98 10:51a Dave
26 * 20 3/23/98 5:48p Hoffoss
27 * Improved listbox handling. Most notibly the scrollbar arrows work now.
29 * 19 2/03/98 4:21p Hoffoss
30 * Made UI controls draw white text when disabled.
32 * 18 1/20/98 10:36a Hoffoss
33 * Fixed optimized warnings.
35 * 17 1/14/98 6:43p Hoffoss
36 * Massive changes to UI code. A lot cleaner and better now. Did all
37 * this to get the new UI_DOT_SLIDER to work properly, which the old code
38 * wasn't flexible enough to handle.
40 * 16 9/18/97 10:31p Lawrance
41 * allow listbox to change text
43 * 15 9/09/97 4:32p Dave
44 * Added sel_changed() function to UI_LISTBOX to check is the selection
47 * 14 9/09/97 3:39p Sandeep
48 * warning level 4 bugs
50 * 13 8/19/97 1:27p Dave
51 * Made it possible to create an empty list box and summarily add items.
53 * 12 8/14/97 5:23p Dave
54 * Added clear_all_items() to the UI_LISTBOX
56 * 11 6/26/97 5:53p Lawrance
57 * change code slightly, so current_item is not forced to 0 when was -1
59 * 10 6/12/97 12:39p John
60 * made ui use freespace colors
62 * 9 6/11/97 1:13p John
63 * Started fixing all the text colors in the game.
65 * 8 5/26/97 10:26a Lawrance
66 * get slider control working 100%
68 * 7 4/22/97 10:11a John
69 * Added checkbox lists to listboxes
71 * 6 4/15/97 3:47p Allender
72 * moved type selection of list box items into actual UI code. Made it
73 * behave more like windows listboxes do
75 * 5 12/23/96 2:42p Lawrance
76 * allowing keys to select list box items in the mission load screen
78 * 4 12/02/96 2:17p John
79 * Made right button drag UI gadgets around and
80 * Ctrl+Shift+Alt+F12 dumps out where they are.
82 * 3 12/01/96 3:48a Lawrance
83 * added function set_current to UI_LISTBOX
85 * 2 11/15/96 11:43a John
87 * 1 11/14/96 6:55p John
96 #include "alphacolors.h"
98 #define KEY_BUFFER_TIMEOUT 1000 // time to clear buffer in milliseconds
100 #define DEFAULT_LISTBOX_ITEM_LENGTH 40
102 // --------------------------------------------------------------------
103 // UI_LISTBOX::link_hotspot
106 void UI_LISTBOX::link_hotspot(int up_button_num, int down_button_num)
108 scrollbar.link_hotspot(up_button_num,down_button_num);
111 // --------------------------------------------------------------------
112 // UI_LISTBOX::set_bmaps
114 // Call the UI_SCROLLBAR::set_bmaps() function for the scroll bar gadget.
116 // returns: -1 ==> error
119 int UI_LISTBOX::set_bmaps(char *lbox_fname, char *b_up_fname, char *b_down_fname, char *sb_fname)
122 scrollbar.set_bmaps(b_up_fname, b_down_fname, sb_fname);
125 // set the bitmaps for the list box rectangle
126 UI_GADGET::set_bmaps(lbox_fname);
131 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)
136 gr_set_font(wnd->f_id);
137 gr_get_string_size(&tw, &th, "*");
142 base_create( wnd, UI_KIND_LISTBOX, _x, _y, _w, real_h );
144 max_items = _max_items;
146 num_items = _numitems;
147 check_list = _check_list;
148 num_items_displayed = nrows;
157 key_buffer_count = 0;
158 last_typed = timer_get_milliseconds();
160 if (_numitems > nrows) {
161 scrollbar.create( wnd, _x+_w+3, _y, real_h, 0, _numitems-nrows, 0, nrows );
162 scrollbar.set_parent(this);
170 void UI_LISTBOX::draw()
176 gr_set_font(my_wnd->f_id);
180 if ( bmap_ids[LBOX_DISABLED] >= 0 ) {
181 gr_set_bitmap(bmap_ids[LBOX_DISABLED]);
186 if ( bmap_ids[LBOX_NORMAL] >= 0 ) {
187 gr_set_bitmap(bmap_ids[LBOX_NORMAL]);
193 gr_set_color_fast(&CBLACK);
194 gr_set_clip( x, y, w, h );
195 ui_rect( 0, 0, w-1, h-1 );
198 ui_draw_sunken_border( x-2, y-2, x+w+scrollbar.w+4, y+h+1 );
201 ui_draw_sunken_border( x-2, y-2, x+w+4, y+h+1 );
205 stop = first_item+num_items_displayed;
206 if (stop>num_items) stop = num_items;
209 gr_set_clip( x, y, w, h );
211 for ( i=first_item; i<stop; i++ ) {
212 gr_get_string_size( &w1, &h1,list[i] );
217 if (i !=current_item) {
219 if ((current_item == -1) && (my_wnd->selected_gadget == this ) && (i == first_item) ) {
221 gr_set_color_fast( &CBLACK );
222 gr_rect( x1, y1, w1+2, h1 );
224 current_item = first_item;
225 gr_set_color_fast( &CBRIGHT_GREEN );
228 gr_set_color_fast( &CBLACK );
229 gr_rect( x1, y1, w1+2, h1 );
231 gr_set_color_fast( &CWHITE );
235 gr_set_color_fast( &CBLACK );
236 gr_rect( x1, y1, w1+2, h1 );
239 gr_set_color_fast(&CWHITE);
242 if (my_wnd->selected_gadget == this) {
243 gr_set_color_fast( &CGRAY );
244 gr_rect( x1, y1, w1+2, h1 );
245 gr_set_color_fast( &CBRIGHT_GREEN );
248 gr_set_color_fast( &CGRAY );
249 gr_rect( x1, y1, w1+2, h1 );
250 gr_set_color_fast( &CBLACK );
255 if ( check_list[i] ) {
256 gr_string( x1+2, y1, "X" );
259 gr_string( x1+16, y1, list[i] );
262 gr_string( x1+2, y1, list[i] );
265 gr_set_color_fast( &CGRAY );
267 gr_set_color_fast( &CBLACK );
270 ui_rect( x1+w1+2, y1, w-1, y1+h1-1 );
271 ui_rect( x1, y1, x1+1, y1+h1-1 );
277 if (stop < num_items_displayed-1 && !uses_bmaps) {
278 gr_set_color_fast(&CBLACK);
279 ui_rect( x1, y1, w-1, h-1 );
283 void UI_LISTBOX::process(int focus)
285 int OnMe, mitem, oldbarpos, kf = 0;
294 if (my_wnd->selected_gadget == this)
298 scrollbar.process(0);
299 if (my_wnd->selected_gadget == &scrollbar) {
308 old_current_item = current_item;
309 old_first_item = first_item;
311 // if (my_wnd->selected_gadget == this) {
312 // my_wnd->selected_gadget == get_next();
318 old_current_item = current_item;
319 old_first_item = first_item;
321 OnMe = is_mouse_on();
324 if (scrollbar.moved) {
325 first_item = scrollbar.position;
326 Assert(first_item >= 0);
328 if (current_item<first_item)
329 current_item = first_item;
331 if (current_item > first_item + num_items_displayed - 1)
332 current_item = first_item + num_items_displayed - 1;
339 if (B1_PRESSED && OnMe) {
344 if ( key_buffer_count && (timer_get_milliseconds() > last_typed + KEY_BUFFER_TIMEOUT) )
345 key_buffer_count = 0;
348 if (my_wnd->keypress) {
351 switch (my_wnd->keypress) {
353 selected_item = current_item;
357 toggled_item = current_item;
376 current_item=num_items - 1;
381 current_item -= num_items_displayed;
386 current_item += num_items_displayed;
390 default: // enter the key in the key buffer
391 if (my_wnd->keypress == KEY_BACKSP) {
392 key_buffer_count = 0;
394 } else if (key_buffer_count < MAX_KEY_BUFFER) {
395 key_buffer[key_buffer_count++] = (char) my_wnd->keypress;
396 last_typed = timer_get_milliseconds();
399 if (!key_buffer_count)
402 for (i=0; i<num_items; i++) {
405 current_text = get_string(i);
406 for (j=0; j<key_buffer_count; j++)
407 if ( (current_text[j] != ascii_table[key_buffer[j]]) && (current_text[j] != shifted_ascii_table[key_buffer[j]]) )
410 if (j == key_buffer_count) {
411 set_first_item(i - num_items_displayed / 2);
420 if (current_item < 0)
423 if (current_item >= num_items)
424 current_item = num_items - 1;
426 if (current_item < first_item)
427 first_item = current_item;
429 if (current_item >= first_item + num_items_displayed)
430 first_item = current_item - num_items_displayed + 1;
432 if (num_items <= num_items_displayed ) {
437 oldbarpos = scrollbar.position;
438 scrollbar.position = first_item;
440 scrollbar.bar_position = scrollbar.position - scrollbar.start;
441 scrollbar.bar_position *= scrollbar.h - scrollbar.bar_size;
442 scrollbar.bar_position /= scrollbar.stop - scrollbar.start;
444 if (scrollbar.bar_position < 0) {
445 scrollbar.bar_position = 0;
448 if (scrollbar.bar_position > scrollbar.h - scrollbar.bar_size) {
449 scrollbar.bar_position = scrollbar.h - scrollbar.bar_size;
458 if (B1_PRESSED && dragging) {
462 mitem = (ui_mouse.y - y)/textheight;
464 if ( (mitem < 0) && (timer_get_milliseconds() > last_scrolled + 1000 / 18) ) {
466 last_scrolled = timer_get_milliseconds();
469 if ( (mitem >= num_items_displayed) && (timer_get_milliseconds() > last_scrolled + 1000 / 18) ) {
471 last_scrolled = timer_get_milliseconds();
474 if ((mitem >= 0) && (mitem<num_items_displayed)) {
475 current_item = mitem + first_item;
478 if (current_item < 0)
481 if (current_item >= num_items)
482 current_item = num_items - 1;
484 if (current_item < first_item)
485 first_item = current_item;
487 if (current_item >= first_item + num_items_displayed)
488 first_item = current_item - num_items_displayed + 1;
490 if (num_items <= num_items_displayed) {
493 } else if (has_scrollbar) {
494 oldbarpos = scrollbar.position;
495 scrollbar.position = first_item;
497 scrollbar.bar_position = scrollbar.position - scrollbar.start;
498 scrollbar.bar_position *= scrollbar.h - scrollbar.bar_size;
499 scrollbar.bar_position /= scrollbar.stop - scrollbar.start;
501 if (scrollbar.bar_position < 0) {
502 scrollbar.bar_position = 0;
505 if (scrollbar.bar_position > scrollbar.h - scrollbar.bar_size) {
506 scrollbar.bar_position = scrollbar.h - scrollbar.bar_size;
512 if (B1_JUST_RELEASED)
513 toggled_item = current_item;
516 if (B1_DOUBLE_CLICKED) {
517 selected_item = current_item;
522 int UI_LISTBOX::toggled()
531 int UI_LISTBOX::selected()
536 return selected_item;
540 int UI_LISTBOX::current()
545 void UI_LISTBOX::set_current(int _index)
547 current_item = _index;
550 void UI_LISTBOX::set_first_item(int _index)
554 else if (_index > num_items)
560 char *UI_LISTBOX::get_string(int _index)
565 void UI_LISTBOX::clear_all_items()
569 for ( idx=0; idx<num_items; idx++ )
576 void UI_LISTBOX::set_new_list(int _numitems, char **_list)
578 num_items = _numitems;
583 int UI_LISTBOX::add_string(char *str)
585 if (max_items < 0) // only if we created an "empty" listbox can we add items
589 if ( (num_items == max_items - 1) || (strlen(str) > DEFAULT_LISTBOX_ITEM_LENGTH) )
590 return 0; // we've reached our limit
593 list[num_items] = strdup(str);
600 int UI_LISTBOX::sel_changed()
602 return old_current_item == current_item ? 0 : 1;