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.3 2002/06/09 04:41:29 relnev
19 * added copyright header
21 * Revision 1.2 2002/05/07 03:16:53 theoddone33
22 * The Great Newline Fix
24 * Revision 1.1.1.1 2002/05/03 03:28:11 root
28 * 4 12/02/98 5:47p Dave
29 * Put in interface xstr code. Converted barracks screen to new format.
31 * 3 10/13/98 9:29a Dave
32 * Started neatening up freespace.h. Many variables renamed and
33 * reorganized. Added AlphaColors.[h,cpp]
35 * 2 10/07/98 10:54a Dave
38 * 1 10/07/98 10:51a Dave
40 * 20 3/23/98 5:48p Hoffoss
41 * Improved listbox handling. Most notibly the scrollbar arrows work now.
43 * 19 2/03/98 4:21p Hoffoss
44 * Made UI controls draw white text when disabled.
46 * 18 1/20/98 10:36a Hoffoss
47 * Fixed optimized warnings.
49 * 17 1/14/98 6:43p Hoffoss
50 * Massive changes to UI code. A lot cleaner and better now. Did all
51 * this to get the new UI_DOT_SLIDER to work properly, which the old code
52 * wasn't flexible enough to handle.
54 * 16 9/18/97 10:31p Lawrance
55 * allow listbox to change text
57 * 15 9/09/97 4:32p Dave
58 * Added sel_changed() function to UI_LISTBOX to check is the selection
61 * 14 9/09/97 3:39p Sandeep
62 * warning level 4 bugs
64 * 13 8/19/97 1:27p Dave
65 * Made it possible to create an empty list box and summarily add items.
67 * 12 8/14/97 5:23p Dave
68 * Added clear_all_items() to the UI_LISTBOX
70 * 11 6/26/97 5:53p Lawrance
71 * change code slightly, so current_item is not forced to 0 when was -1
73 * 10 6/12/97 12:39p John
74 * made ui use freespace colors
76 * 9 6/11/97 1:13p John
77 * Started fixing all the text colors in the game.
79 * 8 5/26/97 10:26a Lawrance
80 * get slider control working 100%
82 * 7 4/22/97 10:11a John
83 * Added checkbox lists to listboxes
85 * 6 4/15/97 3:47p Allender
86 * moved type selection of list box items into actual UI code. Made it
87 * behave more like windows listboxes do
89 * 5 12/23/96 2:42p Lawrance
90 * allowing keys to select list box items in the mission load screen
92 * 4 12/02/96 2:17p John
93 * Made right button drag UI gadgets around and
94 * Ctrl+Shift+Alt+F12 dumps out where they are.
96 * 3 12/01/96 3:48a Lawrance
97 * added function set_current to UI_LISTBOX
99 * 2 11/15/96 11:43a John
101 * 1 11/14/96 6:55p John
110 #include "alphacolors.h"
112 #define KEY_BUFFER_TIMEOUT 1000 // time to clear buffer in milliseconds
114 #define DEFAULT_LISTBOX_ITEM_LENGTH 40
116 // --------------------------------------------------------------------
117 // UI_LISTBOX::link_hotspot
120 void UI_LISTBOX::link_hotspot(int up_button_num, int down_button_num)
122 scrollbar.link_hotspot(up_button_num,down_button_num);
125 // --------------------------------------------------------------------
126 // UI_LISTBOX::set_bmaps
128 // Call the UI_SCROLLBAR::set_bmaps() function for the scroll bar gadget.
130 // returns: -1 ==> error
133 int UI_LISTBOX::set_bmaps(char *lbox_fname, char *b_up_fname, char *b_down_fname, char *sb_fname)
136 scrollbar.set_bmaps(b_up_fname, b_down_fname, sb_fname);
139 // set the bitmaps for the list box rectangle
140 UI_GADGET::set_bmaps(lbox_fname);
145 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)
150 gr_set_font(wnd->f_id);
151 gr_get_string_size(&tw, &th, "*");
156 base_create( wnd, UI_KIND_LISTBOX, _x, _y, _w, real_h );
158 max_items = _max_items;
160 num_items = _numitems;
161 check_list = _check_list;
162 num_items_displayed = nrows;
171 key_buffer_count = 0;
172 last_typed = timer_get_milliseconds();
174 if (_numitems > nrows) {
175 scrollbar.create( wnd, _x+_w+3, _y, real_h, 0, _numitems-nrows, 0, nrows );
176 scrollbar.set_parent(this);
184 void UI_LISTBOX::draw()
190 gr_set_font(my_wnd->f_id);
194 if ( bmap_ids[LBOX_DISABLED] >= 0 ) {
195 gr_set_bitmap(bmap_ids[LBOX_DISABLED]);
200 if ( bmap_ids[LBOX_NORMAL] >= 0 ) {
201 gr_set_bitmap(bmap_ids[LBOX_NORMAL]);
207 gr_set_color_fast(&CBLACK);
208 gr_set_clip( x, y, w, h );
209 ui_rect( 0, 0, w-1, h-1 );
212 ui_draw_sunken_border( x-2, y-2, x+w+scrollbar.w+4, y+h+1 );
215 ui_draw_sunken_border( x-2, y-2, x+w+4, y+h+1 );
219 stop = first_item+num_items_displayed;
220 if (stop>num_items) stop = num_items;
223 gr_set_clip( x, y, w, h );
225 for ( i=first_item; i<stop; i++ ) {
226 gr_get_string_size( &w1, &h1,list[i] );
231 if (i !=current_item) {
233 if ((current_item == -1) && (my_wnd->selected_gadget == this ) && (i == first_item) ) {
235 gr_set_color_fast( &CBLACK );
236 gr_rect( x1, y1, w1+2, h1 );
238 current_item = first_item;
239 gr_set_color_fast( &CBRIGHT_GREEN );
242 gr_set_color_fast( &CBLACK );
243 gr_rect( x1, y1, w1+2, h1 );
245 gr_set_color_fast( &CWHITE );
249 gr_set_color_fast( &CBLACK );
250 gr_rect( x1, y1, w1+2, h1 );
253 gr_set_color_fast(&CWHITE);
256 if (my_wnd->selected_gadget == this) {
257 gr_set_color_fast( &CGRAY );
258 gr_rect( x1, y1, w1+2, h1 );
259 gr_set_color_fast( &CBRIGHT_GREEN );
262 gr_set_color_fast( &CGRAY );
263 gr_rect( x1, y1, w1+2, h1 );
264 gr_set_color_fast( &CBLACK );
269 if ( check_list[i] ) {
270 gr_string( x1+2, y1, "X" );
273 gr_string( x1+16, y1, list[i] );
276 gr_string( x1+2, y1, list[i] );
279 gr_set_color_fast( &CGRAY );
281 gr_set_color_fast( &CBLACK );
284 ui_rect( x1+w1+2, y1, w-1, y1+h1-1 );
285 ui_rect( x1, y1, x1+1, y1+h1-1 );
291 if (stop < num_items_displayed-1 && !uses_bmaps) {
292 gr_set_color_fast(&CBLACK);
293 ui_rect( x1, y1, w-1, h-1 );
297 void UI_LISTBOX::process(int focus)
299 int OnMe, mitem, oldbarpos, kf = 0;
308 if (my_wnd->selected_gadget == this)
312 scrollbar.process(0);
313 if (my_wnd->selected_gadget == &scrollbar) {
322 old_current_item = current_item;
323 old_first_item = first_item;
325 // if (my_wnd->selected_gadget == this) {
326 // my_wnd->selected_gadget == get_next();
332 old_current_item = current_item;
333 old_first_item = first_item;
335 OnMe = is_mouse_on();
338 if (scrollbar.moved) {
339 first_item = scrollbar.position;
340 Assert(first_item >= 0);
342 if (current_item<first_item)
343 current_item = first_item;
345 if (current_item > first_item + num_items_displayed - 1)
346 current_item = first_item + num_items_displayed - 1;
353 if (B1_PRESSED && OnMe) {
358 if ( key_buffer_count && (timer_get_milliseconds() > last_typed + KEY_BUFFER_TIMEOUT) )
359 key_buffer_count = 0;
362 if (my_wnd->keypress) {
365 switch (my_wnd->keypress) {
367 selected_item = current_item;
371 toggled_item = current_item;
390 current_item=num_items - 1;
395 current_item -= num_items_displayed;
400 current_item += num_items_displayed;
404 default: // enter the key in the key buffer
405 if (my_wnd->keypress == KEY_BACKSP) {
406 key_buffer_count = 0;
408 } else if (key_buffer_count < MAX_KEY_BUFFER) {
409 key_buffer[key_buffer_count++] = (char) my_wnd->keypress;
410 last_typed = timer_get_milliseconds();
413 if (!key_buffer_count)
416 for (i=0; i<num_items; i++) {
419 current_text = get_string(i);
420 for (j=0; j<key_buffer_count; j++)
421 if ( (current_text[j] != ascii_table[key_buffer[j]]) && (current_text[j] != shifted_ascii_table[key_buffer[j]]) )
424 if (j == key_buffer_count) {
425 set_first_item(i - num_items_displayed / 2);
434 if (current_item < 0)
437 if (current_item >= num_items)
438 current_item = num_items - 1;
440 if (current_item < first_item)
441 first_item = current_item;
443 if (current_item >= first_item + num_items_displayed)
444 first_item = current_item - num_items_displayed + 1;
446 if (num_items <= num_items_displayed ) {
451 oldbarpos = scrollbar.position;
452 scrollbar.position = first_item;
454 scrollbar.bar_position = scrollbar.position - scrollbar.start;
455 scrollbar.bar_position *= scrollbar.h - scrollbar.bar_size;
456 scrollbar.bar_position /= scrollbar.stop - scrollbar.start;
458 if (scrollbar.bar_position < 0) {
459 scrollbar.bar_position = 0;
462 if (scrollbar.bar_position > scrollbar.h - scrollbar.bar_size) {
463 scrollbar.bar_position = scrollbar.h - scrollbar.bar_size;
472 if (B1_PRESSED && dragging) {
476 mitem = (ui_mouse.y - y)/textheight;
478 if ( (mitem < 0) && (timer_get_milliseconds() > last_scrolled + 1000 / 18) ) {
480 last_scrolled = timer_get_milliseconds();
483 if ( (mitem >= num_items_displayed) && (timer_get_milliseconds() > last_scrolled + 1000 / 18) ) {
485 last_scrolled = timer_get_milliseconds();
488 if ((mitem >= 0) && (mitem<num_items_displayed)) {
489 current_item = mitem + first_item;
492 if (current_item < 0)
495 if (current_item >= num_items)
496 current_item = num_items - 1;
498 if (current_item < first_item)
499 first_item = current_item;
501 if (current_item >= first_item + num_items_displayed)
502 first_item = current_item - num_items_displayed + 1;
504 if (num_items <= num_items_displayed) {
507 } else if (has_scrollbar) {
508 oldbarpos = scrollbar.position;
509 scrollbar.position = first_item;
511 scrollbar.bar_position = scrollbar.position - scrollbar.start;
512 scrollbar.bar_position *= scrollbar.h - scrollbar.bar_size;
513 scrollbar.bar_position /= scrollbar.stop - scrollbar.start;
515 if (scrollbar.bar_position < 0) {
516 scrollbar.bar_position = 0;
519 if (scrollbar.bar_position > scrollbar.h - scrollbar.bar_size) {
520 scrollbar.bar_position = scrollbar.h - scrollbar.bar_size;
526 if (B1_JUST_RELEASED)
527 toggled_item = current_item;
530 if (B1_DOUBLE_CLICKED) {
531 selected_item = current_item;
536 int UI_LISTBOX::toggled()
545 int UI_LISTBOX::selected()
550 return selected_item;
554 int UI_LISTBOX::current()
559 void UI_LISTBOX::set_current(int _index)
561 current_item = _index;
564 void UI_LISTBOX::set_first_item(int _index)
568 else if (_index > num_items)
574 char *UI_LISTBOX::get_string(int _index)
579 void UI_LISTBOX::clear_all_items()
583 for ( idx=0; idx<num_items; idx++ )
590 void UI_LISTBOX::set_new_list(int _numitems, char **_list)
592 num_items = _numitems;
597 int UI_LISTBOX::add_string(char *str)
599 if (max_items < 0) // only if we created an "empty" listbox can we add items
603 if ( (num_items == max_items - 1) || (strlen(str) > DEFAULT_LISTBOX_ITEM_LENGTH) )
604 return 0; // we've reached our limit
607 list[num_items] = strdup(str);
614 int UI_LISTBOX::sel_changed()
616 return old_current_item == current_item ? 0 : 1;