2 * $Logfile: /Freespace2/code/UI/LISTBOX.cpp $
7 * Code to implement a listbox gadget.
10 * Revision 1.2 2002/05/07 03:16:53 theoddone33
11 * The Great Newline Fix
13 * Revision 1.1.1.1 2002/05/03 03:28:11 root
17 * 4 12/02/98 5:47p Dave
18 * Put in interface xstr code. Converted barracks screen to new format.
20 * 3 10/13/98 9:29a Dave
21 * Started neatening up freespace.h. Many variables renamed and
22 * reorganized. Added AlphaColors.[h,cpp]
24 * 2 10/07/98 10:54a Dave
27 * 1 10/07/98 10:51a Dave
29 * 20 3/23/98 5:48p Hoffoss
30 * Improved listbox handling. Most notibly the scrollbar arrows work now.
32 * 19 2/03/98 4:21p Hoffoss
33 * Made UI controls draw white text when disabled.
35 * 18 1/20/98 10:36a Hoffoss
36 * Fixed optimized warnings.
38 * 17 1/14/98 6:43p Hoffoss
39 * Massive changes to UI code. A lot cleaner and better now. Did all
40 * this to get the new UI_DOT_SLIDER to work properly, which the old code
41 * wasn't flexible enough to handle.
43 * 16 9/18/97 10:31p Lawrance
44 * allow listbox to change text
46 * 15 9/09/97 4:32p Dave
47 * Added sel_changed() function to UI_LISTBOX to check is the selection
50 * 14 9/09/97 3:39p Sandeep
51 * warning level 4 bugs
53 * 13 8/19/97 1:27p Dave
54 * Made it possible to create an empty list box and summarily add items.
56 * 12 8/14/97 5:23p Dave
57 * Added clear_all_items() to the UI_LISTBOX
59 * 11 6/26/97 5:53p Lawrance
60 * change code slightly, so current_item is not forced to 0 when was -1
62 * 10 6/12/97 12:39p John
63 * made ui use freespace colors
65 * 9 6/11/97 1:13p John
66 * Started fixing all the text colors in the game.
68 * 8 5/26/97 10:26a Lawrance
69 * get slider control working 100%
71 * 7 4/22/97 10:11a John
72 * Added checkbox lists to listboxes
74 * 6 4/15/97 3:47p Allender
75 * moved type selection of list box items into actual UI code. Made it
76 * behave more like windows listboxes do
78 * 5 12/23/96 2:42p Lawrance
79 * allowing keys to select list box items in the mission load screen
81 * 4 12/02/96 2:17p John
82 * Made right button drag UI gadgets around and
83 * Ctrl+Shift+Alt+F12 dumps out where they are.
85 * 3 12/01/96 3:48a Lawrance
86 * added function set_current to UI_LISTBOX
88 * 2 11/15/96 11:43a John
90 * 1 11/14/96 6:55p John
99 #include "alphacolors.h"
101 #define KEY_BUFFER_TIMEOUT 1000 // time to clear buffer in milliseconds
103 #define DEFAULT_LISTBOX_ITEM_LENGTH 40
105 // --------------------------------------------------------------------
106 // UI_LISTBOX::link_hotspot
109 void UI_LISTBOX::link_hotspot(int up_button_num, int down_button_num)
111 scrollbar.link_hotspot(up_button_num,down_button_num);
114 // --------------------------------------------------------------------
115 // UI_LISTBOX::set_bmaps
117 // Call the UI_SCROLLBAR::set_bmaps() function for the scroll bar gadget.
119 // returns: -1 ==> error
122 int UI_LISTBOX::set_bmaps(char *lbox_fname, char *b_up_fname, char *b_down_fname, char *sb_fname)
125 scrollbar.set_bmaps(b_up_fname, b_down_fname, sb_fname);
128 // set the bitmaps for the list box rectangle
129 UI_GADGET::set_bmaps(lbox_fname);
134 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)
139 gr_set_font(wnd->f_id);
140 gr_get_string_size(&tw, &th, "*");
145 base_create( wnd, UI_KIND_LISTBOX, _x, _y, _w, real_h );
147 max_items = _max_items;
149 num_items = _numitems;
150 check_list = _check_list;
151 num_items_displayed = nrows;
160 key_buffer_count = 0;
161 last_typed = timer_get_milliseconds();
163 if (_numitems > nrows) {
164 scrollbar.create( wnd, _x+_w+3, _y, real_h, 0, _numitems-nrows, 0, nrows );
165 scrollbar.set_parent(this);
173 void UI_LISTBOX::draw()
179 gr_set_font(my_wnd->f_id);
183 if ( bmap_ids[LBOX_DISABLED] >= 0 ) {
184 gr_set_bitmap(bmap_ids[LBOX_DISABLED]);
189 if ( bmap_ids[LBOX_NORMAL] >= 0 ) {
190 gr_set_bitmap(bmap_ids[LBOX_NORMAL]);
196 gr_set_color_fast(&CBLACK);
197 gr_set_clip( x, y, w, h );
198 ui_rect( 0, 0, w-1, h-1 );
201 ui_draw_sunken_border( x-2, y-2, x+w+scrollbar.w+4, y+h+1 );
204 ui_draw_sunken_border( x-2, y-2, x+w+4, y+h+1 );
208 stop = first_item+num_items_displayed;
209 if (stop>num_items) stop = num_items;
212 gr_set_clip( x, y, w, h );
214 for ( i=first_item; i<stop; i++ ) {
215 gr_get_string_size( &w1, &h1,list[i] );
220 if (i !=current_item) {
222 if ((current_item == -1) && (my_wnd->selected_gadget == this ) && (i == first_item) ) {
224 gr_set_color_fast( &CBLACK );
225 gr_rect( x1, y1, w1+2, h1 );
227 current_item = first_item;
228 gr_set_color_fast( &CBRIGHT_GREEN );
231 gr_set_color_fast( &CBLACK );
232 gr_rect( x1, y1, w1+2, h1 );
234 gr_set_color_fast( &CWHITE );
238 gr_set_color_fast( &CBLACK );
239 gr_rect( x1, y1, w1+2, h1 );
242 gr_set_color_fast(&CWHITE);
245 if (my_wnd->selected_gadget == this) {
246 gr_set_color_fast( &CGRAY );
247 gr_rect( x1, y1, w1+2, h1 );
248 gr_set_color_fast( &CBRIGHT_GREEN );
251 gr_set_color_fast( &CGRAY );
252 gr_rect( x1, y1, w1+2, h1 );
253 gr_set_color_fast( &CBLACK );
258 if ( check_list[i] ) {
259 gr_string( x1+2, y1, "X" );
262 gr_string( x1+16, y1, list[i] );
265 gr_string( x1+2, y1, list[i] );
268 gr_set_color_fast( &CGRAY );
270 gr_set_color_fast( &CBLACK );
273 ui_rect( x1+w1+2, y1, w-1, y1+h1-1 );
274 ui_rect( x1, y1, x1+1, y1+h1-1 );
280 if (stop < num_items_displayed-1 && !uses_bmaps) {
281 gr_set_color_fast(&CBLACK);
282 ui_rect( x1, y1, w-1, h-1 );
286 void UI_LISTBOX::process(int focus)
288 int OnMe, mitem, oldbarpos, kf = 0;
297 if (my_wnd->selected_gadget == this)
301 scrollbar.process(0);
302 if (my_wnd->selected_gadget == &scrollbar) {
311 old_current_item = current_item;
312 old_first_item = first_item;
314 // if (my_wnd->selected_gadget == this) {
315 // my_wnd->selected_gadget == get_next();
321 old_current_item = current_item;
322 old_first_item = first_item;
324 OnMe = is_mouse_on();
327 if (scrollbar.moved) {
328 first_item = scrollbar.position;
329 Assert(first_item >= 0);
331 if (current_item<first_item)
332 current_item = first_item;
334 if (current_item > first_item + num_items_displayed - 1)
335 current_item = first_item + num_items_displayed - 1;
342 if (B1_PRESSED && OnMe) {
347 if ( key_buffer_count && (timer_get_milliseconds() > last_typed + KEY_BUFFER_TIMEOUT) )
348 key_buffer_count = 0;
351 if (my_wnd->keypress) {
354 switch (my_wnd->keypress) {
356 selected_item = current_item;
360 toggled_item = current_item;
379 current_item=num_items - 1;
384 current_item -= num_items_displayed;
389 current_item += num_items_displayed;
393 default: // enter the key in the key buffer
394 if (my_wnd->keypress == KEY_BACKSP) {
395 key_buffer_count = 0;
397 } else if (key_buffer_count < MAX_KEY_BUFFER) {
398 key_buffer[key_buffer_count++] = (char) my_wnd->keypress;
399 last_typed = timer_get_milliseconds();
402 if (!key_buffer_count)
405 for (i=0; i<num_items; i++) {
408 current_text = get_string(i);
409 for (j=0; j<key_buffer_count; j++)
410 if ( (current_text[j] != ascii_table[key_buffer[j]]) && (current_text[j] != shifted_ascii_table[key_buffer[j]]) )
413 if (j == key_buffer_count) {
414 set_first_item(i - num_items_displayed / 2);
423 if (current_item < 0)
426 if (current_item >= num_items)
427 current_item = num_items - 1;
429 if (current_item < first_item)
430 first_item = current_item;
432 if (current_item >= first_item + num_items_displayed)
433 first_item = current_item - num_items_displayed + 1;
435 if (num_items <= num_items_displayed ) {
440 oldbarpos = scrollbar.position;
441 scrollbar.position = first_item;
443 scrollbar.bar_position = scrollbar.position - scrollbar.start;
444 scrollbar.bar_position *= scrollbar.h - scrollbar.bar_size;
445 scrollbar.bar_position /= scrollbar.stop - scrollbar.start;
447 if (scrollbar.bar_position < 0) {
448 scrollbar.bar_position = 0;
451 if (scrollbar.bar_position > scrollbar.h - scrollbar.bar_size) {
452 scrollbar.bar_position = scrollbar.h - scrollbar.bar_size;
461 if (B1_PRESSED && dragging) {
465 mitem = (ui_mouse.y - y)/textheight;
467 if ( (mitem < 0) && (timer_get_milliseconds() > last_scrolled + 1000 / 18) ) {
469 last_scrolled = timer_get_milliseconds();
472 if ( (mitem >= num_items_displayed) && (timer_get_milliseconds() > last_scrolled + 1000 / 18) ) {
474 last_scrolled = timer_get_milliseconds();
477 if ((mitem >= 0) && (mitem<num_items_displayed)) {
478 current_item = mitem + first_item;
481 if (current_item < 0)
484 if (current_item >= num_items)
485 current_item = num_items - 1;
487 if (current_item < first_item)
488 first_item = current_item;
490 if (current_item >= first_item + num_items_displayed)
491 first_item = current_item - num_items_displayed + 1;
493 if (num_items <= num_items_displayed) {
496 } else if (has_scrollbar) {
497 oldbarpos = scrollbar.position;
498 scrollbar.position = first_item;
500 scrollbar.bar_position = scrollbar.position - scrollbar.start;
501 scrollbar.bar_position *= scrollbar.h - scrollbar.bar_size;
502 scrollbar.bar_position /= scrollbar.stop - scrollbar.start;
504 if (scrollbar.bar_position < 0) {
505 scrollbar.bar_position = 0;
508 if (scrollbar.bar_position > scrollbar.h - scrollbar.bar_size) {
509 scrollbar.bar_position = scrollbar.h - scrollbar.bar_size;
515 if (B1_JUST_RELEASED)
516 toggled_item = current_item;
519 if (B1_DOUBLE_CLICKED) {
520 selected_item = current_item;
525 int UI_LISTBOX::toggled()
534 int UI_LISTBOX::selected()
539 return selected_item;
543 int UI_LISTBOX::current()
548 void UI_LISTBOX::set_current(int _index)
550 current_item = _index;
553 void UI_LISTBOX::set_first_item(int _index)
557 else if (_index > num_items)
563 char *UI_LISTBOX::get_string(int _index)
568 void UI_LISTBOX::clear_all_items()
572 for ( idx=0; idx<num_items; idx++ )
579 void UI_LISTBOX::set_new_list(int _numitems, char **_list)
581 num_items = _numitems;
586 int UI_LISTBOX::add_string(char *str)
588 if (max_items < 0) // only if we created an "empty" listbox can we add items
592 if ( (num_items == max_items - 1) || (strlen(str) > DEFAULT_LISTBOX_ITEM_LENGTH) )
593 return 0; // we've reached our limit
596 list[num_items] = strdup(str);
603 int UI_LISTBOX::sel_changed()
605 return old_current_item == current_item ? 0 : 1;