3 * Copyright (C) 2010-2014 Adam Williams <broadcast at earthling dot net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "bcdisplayinfo.h"
22 #include "bcdragwindow.h"
23 #include "bclistbox.h"
24 #include "bclistboxitem.h"
26 #include "bcresources.h"
27 #include "bcsignals.h"
39 // ====================================================== scrollbars
42 BC_ListBoxYScroll::BC_ListBoxYScroll(BC_ListBox *listbox,
46 : BC_ScrollBar(listbox->get_yscroll_x(),
47 listbox->get_yscroll_y(),
49 listbox->get_yscroll_height(),
54 this->listbox = listbox;
57 BC_ListBoxYScroll::~BC_ListBoxYScroll()
61 int BC_ListBoxYScroll::handle_event()
63 listbox->set_yposition(get_value());
73 BC_ListBoxXScroll::BC_ListBoxXScroll(BC_ListBox *listbox,
77 : BC_ScrollBar(listbox->get_xscroll_x(),
78 listbox->get_xscroll_y(),
80 listbox->get_xscroll_width(),
85 this->listbox = listbox;
88 BC_ListBoxXScroll::~BC_ListBoxXScroll()
92 int BC_ListBoxXScroll::handle_event()
94 listbox->set_xposition(get_value());
105 BC_ListBoxToggle::BC_ListBoxToggle(BC_ListBox *listbox,
106 BC_ListBoxItem *item,
110 this->listbox = listbox;
114 this->value = item->get_expand();
116 state = BC_Toggle::TOGGLE_CHECKED;
118 state = BC_Toggle::TOGGLE_UP;
121 void BC_ListBoxToggle::update(BC_ListBoxItem *item,
126 this->value = item->get_expand();
136 state = TOGGLE_CHECKED;
141 state = TOGGLE_CHECKEDHI;
152 case TOGGLE_CHECKEDHI:
157 case TOGGLE_DOWN_EXIT:
165 int BC_ListBoxToggle::cursor_motion_event(int *redraw_toggles)
167 int w = listbox->toggle_images[0]->get_w();
168 int h = listbox->toggle_images[0]->get_h();
169 int cursor_inside = listbox->get_cursor_x() >= x &&
170 listbox->get_cursor_x() < x + w &&
171 listbox->get_cursor_y() >= y &&
172 listbox->get_cursor_y() < y + h;
177 case BC_ListBoxToggle::TOGGLE_UPHI:
180 state = BC_ListBoxToggle::TOGGLE_UP;
185 case BC_ListBoxToggle::TOGGLE_CHECKEDHI:
188 state = BC_ListBoxToggle::TOGGLE_CHECKED;
193 case BC_ListBoxToggle::TOGGLE_DOWN:
196 state = BC_ListBoxToggle::TOGGLE_DOWN_EXIT;
202 case BC_ListBoxToggle::TOGGLE_DOWN_EXIT:
205 state = BC_ListBoxToggle::TOGGLE_DOWN;
215 state = BC_ListBoxToggle::TOGGLE_CHECKEDHI;
217 state = BC_ListBoxToggle::TOGGLE_UPHI;
225 int BC_ListBoxToggle::cursor_leave_event(int *redraw_toggles)
228 state = BC_ListBoxToggle::TOGGLE_CHECKED;
230 state = BC_ListBoxToggle::TOGGLE_UP;
234 int BC_ListBoxToggle::button_press_event()
236 int w = listbox->toggle_images[0]->get_w();
237 int h = listbox->toggle_images[0]->get_h();
239 if(listbox->gui->get_cursor_x() >= x &&
240 listbox->gui->get_cursor_x() < x + w &&
241 listbox->gui->get_cursor_y() >= y &&
242 listbox->gui->get_cursor_y() < y + h)
244 state = BC_ListBoxToggle::TOGGLE_DOWN;
250 int BC_ListBoxToggle::button_release_event(int *redraw_toggles)
256 case BC_ListBoxToggle::TOGGLE_DOWN:
259 state = BC_ListBoxToggle::TOGGLE_CHECKEDHI;
261 state = BC_ListBoxToggle::TOGGLE_UPHI;
262 listbox->expand_item(item, value);
266 case BC_ListBoxToggle::TOGGLE_DOWN_EXIT:
268 state = BC_ListBoxToggle::TOGGLE_CHECKED;
270 state = BC_ListBoxToggle::TOGGLE_UP;
278 void BC_ListBoxToggle::draw(int flash)
282 int image_number = 0;
283 int w = listbox->toggle_images[0]->get_w();
284 int h = listbox->toggle_images[0]->get_h();
288 case BC_ListBoxToggle::TOGGLE_UP: image_number = 0; break;
289 case BC_ListBoxToggle::TOGGLE_UPHI: image_number = 1; break;
290 case BC_ListBoxToggle::TOGGLE_CHECKED: image_number = 2; break;
291 case BC_ListBoxToggle::TOGGLE_DOWN: image_number = 3; break;
292 case BC_ListBoxToggle::TOGGLE_CHECKEDHI: image_number = 4; break;
293 case BC_ListBoxToggle::TOGGLE_DOWN_EXIT:
301 //printf("BC_ListBoxToggle::draw 1 %d\n", state);
302 listbox->gui->draw_pixmap(listbox->toggle_images[image_number],
309 listbox->gui->flash(x, y, w, h);
310 listbox->gui->flush();
328 // ====================================================== box
330 BC_ListBox::BC_ListBox(int x,
335 ArrayList<BC_ListBoxItem*> *data,
336 const char **column_titles,
344 : BC_SubWindow(x, y, w, h, -1)
346 justify = LISTBOX_RIGHT;
348 highlighted_item = -1;
349 highlighted_title = -1;
350 highlighted_division = -1;
354 current_cursor = ARROW_CURSOR;
367 selection_number1 = -1;
368 selection_number2 = -1;
371 row_height = row_ascent = row_descent = 0;
374 current_operation = NO_OPERATION;
375 button_highlighted = 0;
376 list_highlighted = 0;
379 allow_drag_scroll = 1;
385 allow_drag_column = 0;
392 for(int i = 0; i < 3; i++) column_bg[i] = 0;
393 for(int i = 0; i < 4; i++) button_images[i] = 0;
394 for(int i = 0; i < 5; i++) toggle_images[i] = 0;
399 //printf("BC_ListBox::BC_ListBox 1\n");
401 this->columns = columns;
402 this->yposition = yposition;
403 this->is_popup = is_popup;
404 this->use_button = 1;
405 this->display_format = display_format;
406 this->selection_mode = selection_mode;
407 this->icon_position = icon_position;
408 this->allow_drag = allow_drag;
409 this->column_titles = 0;
410 this->column_width = 0;
411 this->first_in_view = -1;
412 this->last_in_view = 0;
413 //printf("BC_ListBox::BC_ListBox 1\n");
415 if((!column_titles && column_width) ||
416 (column_titles && !column_width))
418 printf("BC_ListBox::BC_ListBox either column_titles or column_widths == NULL but not both.\n");
420 //printf("BC_ListBox::BC_ListBox 2 %p %p\n", column_titles, column_width);
422 set_columns(column_titles,
426 //printf("BC_ListBox::BC_ListBox 3\n");
428 drag_icon_vframe = 0;
429 drag_column_icon_vframe = 0;
433 // reset the search engine
434 //printf("BC_ListBox::BC_ListBox 4\n");
436 //printf("BC_ListBox::BC_ListBox 5\n");
439 BC_ListBox::~BC_ListBox()
441 expanders.remove_all_objects();
442 if(bg_surface) delete bg_surface;
443 if(bg_pixmap) delete bg_pixmap;
444 if(xscrollbar) delete xscrollbar;
445 if(yscrollbar) delete yscrollbar;
446 for(int i = 0; i < 3; i++) delete column_bg[i];
447 for(int i = 0; i < 4; i++) delete button_images[i];
448 for(int i = 0; i < 5; i++) delete toggle_images[i];
449 if(column_sort_up) delete column_sort_up;
450 if(column_sort_dn) delete column_sort_dn;
453 if(drag_popup) delete drag_popup;
456 int BC_ListBox::enable()
463 int BC_ListBox::disable()
470 void BC_ListBox::reset_query()
472 query[0] = 0; // reset query
475 int BC_ListBox::evaluate_query(char *string)
477 for(int i = 0; i < data[search_column].size(); i++)
479 if(strcmp(string, data[search_column].get(i)->text) <= 0 &&
480 data[search_column].get(i)->searchable)
489 int BC_ListBox::query_list()
491 if(query[0] == 0) return 0;
495 int selection_changed = 0;
496 int prev_selection = -1;
497 result = evaluate_query(query);
498 if(result >= 0) done = 1;
503 for(int i = 0; i < data[0].total; i++)
505 for(int j = 0; j < columns; j++)
507 if(data[j].values[i]->selected) prev_selection = i;
508 data[j].values[i]->selected = 0;
513 if(prev_selection != result)
514 selection_changed = 1;
515 for(int j = 0; j < columns; j++)
517 data[j].values[result]->selected = 1;
519 center_selection(result);
522 return selection_changed;
525 void BC_ListBox::init_column_width()
527 if(!column_width && data)
530 for(int i = 0; i < data[0].total; i++)
532 wd = get_text_w(data[0].values[i]);
533 if( wd > widest ) widest = wd;
535 default_column_width[0] = widest + 2 * LISTBOX_MARGIN;
539 int BC_ListBox::initialize()
545 for( int i = 0; i < 4; ++i )
547 button_images[i] = new BC_Pixmap(parent_window,
548 BC_WindowBase::get_resources()->listbox_button[i],
551 w = button_images[0]->get_w();
552 h = button_images[0]->get_h();
556 current_operation = NO_OPERATION;
562 current_operation = NO_OPERATION;
565 for(int i = 0; i < 3; i++)
567 column_bg[i] = new BC_Pixmap(parent_window,
568 get_resources()->listbox_column[i],
571 for(int i = 0; i < 5; i++)
573 toggle_images[i] = new BC_Pixmap(parent_window,
574 get_resources()->listbox_expand[i],
578 column_sort_up = new BC_Pixmap(parent_window,
579 BC_WindowBase::get_resources()->listbox_up,
581 column_sort_dn = new BC_Pixmap(parent_window,
582 BC_WindowBase::get_resources()->listbox_dn,
585 //printf("BC_ListBox::initialize 10\n");
586 drag_icon_vframe = get_resources()->type_to_icon[ICON_UNKNOWN];
587 drag_column_icon_vframe = get_resources()->type_to_icon[ICON_COLUMN];
588 // = new BC_Pixmap(parent_window,
589 // get_resources()->type_to_icon[ICON_UNKNOWN],
591 // drag_column_icon = new BC_Pixmap(parent_window,
592 // get_resources()->type_to_icon[ICON_COLUMN],
594 BC_SubWindow::initialize();
598 if(top_level->get_resources()->listbox_bg)
599 bg_pixmap = new BC_Pixmap(this,
600 get_resources()->listbox_bg,
606 if(!use_button && is_popup)
615 void BC_ListBox::deactivate_selection()
617 current_operation = NO_OPERATION;
620 int BC_ListBox::draw_button(int flush)
622 // Draw the button for a popup listbox
623 if(use_button && is_popup)
625 int image_number = 0;
627 draw_top_background(parent_window, 0, 0, w, h);
629 if(button_highlighted)
631 if(current_operation == BUTTON_DN)
637 pixmap->draw_pixmap(button_images[image_number],
649 int BC_ListBox::calculate_item_coords()
657 // Change the display_format to get the right item dimensions for both
659 temp_display_format = display_format;
662 // Scan the first column for lowest y coord of all text
663 // and lowest right x and y coord for all icons which aren't auto placable
664 calculate_last_coords_recursive(data,
671 // Reset last column width. It's recalculated based on text width.
673 calculate_item_coords_recursive(data,
682 display_format = temp_display_format;
687 void BC_ListBox::calculate_last_coords_recursive(
688 ArrayList<BC_ListBoxItem*> *data,
695 for(int i = 0; i < data[0].size(); i++)
697 int current_text_y = 0;
698 int current_icon_x = 0;
699 int current_icon_y = 0;
700 BC_ListBoxItem *item = data[0].get(i);
703 if(!item->autoplace_text)
705 // Lowest text coordinate
706 display_format = LISTBOX_TEXT;
707 current_text_y = item->text_y + get_text_h(item);
708 if(current_text_y > *next_text_y)
709 *next_text_y = current_text_y;
711 // Add sublist depth if it is expanded
712 if(item->get_sublist() &&
713 item->get_columns() &&
716 calculate_last_coords_recursive(item->get_sublist(),
725 // Get next_icon coordinate
728 BC_ListBoxItem *item = data[master_column].get(i);
729 if(!item->autoplace_icon)
731 display_format = LISTBOX_ICONS;
732 // Lowest right icon coordinate.
733 current_icon_x = item->icon_x;
734 if(current_icon_x > *icon_x) *icon_x = current_icon_x;
735 if(current_icon_x + get_item_w(item) > *next_icon_x)
736 *next_icon_x = current_icon_x + get_item_w(item);
738 current_icon_y = item->icon_y + get_item_h(item);
739 if(current_icon_y > *next_icon_y)
740 *next_icon_y = current_icon_y;
747 void BC_ListBox::calculate_item_coords_recursive(
748 ArrayList<BC_ListBoxItem*> *data,
755 // get maximum height of an icon
756 row_height = get_text_height(MEDIUMFONT);
757 if(temp_display_format == LISTBOX_ICON_LIST)
759 for(int i = 0; i < data[0].size(); i++)
761 if(data[0].get(i)->icon)
763 if(data[0].get(i)->icon->get_h() > row_height)
764 row_height = data[0].get(i)->icon->get_h();
770 // Set up items which need autoplacement.
771 // Should fill icons down and then across
772 for(int i = 0; i < data[0].size(); i++)
774 // Don't increase y unless the row requires autoplacing.
775 int total_autoplaced_columns = 0;
777 // Set up icons in first column
780 BC_ListBoxItem *item = data[master_column].get(i);
781 if(item->autoplace_icon)
783 // 1 column only if icons are used
784 display_format = LISTBOX_ICONS;
787 if(*next_icon_y + get_item_h(item) >= get_h() &&
790 *icon_x = *next_icon_x;
794 if(*icon_x + get_item_w(item) > *next_icon_x)
795 *next_icon_x = *icon_x + get_item_w(item);
798 item->set_icon_x(*icon_x);
799 item->set_icon_y(*next_icon_y);
801 *next_icon_y += get_item_h(item);
809 row_ascent = row_descent = 0;
810 // row_height still holds icon max height
811 for(int j = 0; j < columns; j++)
813 BC_ListBoxItem *item = data[j].get(i);
814 if(item->autoplace_text)
816 display_format = LISTBOX_TEXT;
817 item->set_text_x(next_text_x);
818 item->set_text_y(*next_text_y);
819 int ht = get_text_h(item);
820 if( ht > row_height ) row_height = ht;
821 int bl = get_baseline(item);
822 if( bl > row_ascent ) row_ascent = bl;
824 if( dt > row_descent ) row_ascent = bl;
826 // printf("BC_ListBox::calculate_item_coords_recursive %p %d %d %d %d %s \n",
827 // item->get_sublist(),
828 // item->get_columns(),
829 // item->get_expand(),
832 // item->get_text());
833 // Increment position of next column
836 next_text_x += (column_width ?
838 default_column_width[j]);
841 // Set last column width based on text width
843 int new_w = get_item_w(item);
845 int *previous_w = (column_width ?
847 &default_column_width[j]);
848 if(new_w > *previous_w)
850 //printf("BC_ListBox::calculate_item_coords_recursive 1 %d\n", new_w);
852 total_autoplaced_columns++;
856 // Increase the text vertical position
857 if(total_autoplaced_columns)
859 display_format = LISTBOX_TEXT;
860 *next_text_y += row_height;
864 BC_ListBoxItem *item = data[master_column].values[i];
865 if(item->get_sublist() &&
866 item->get_columns() &&
869 calculate_item_coords_recursive(
880 void BC_ListBox::set_is_suggestions(int value)
882 this->is_suggestions = value;
885 void BC_ListBox::set_use_button(int value)
887 this->use_button = value;
890 void BC_ListBox::set_justify(int value)
892 this->justify = value;
895 void BC_ListBox::set_allow_drag_column(int value)
897 this->allow_drag_column = value;
900 void BC_ListBox::set_process_drag(int value)
902 this->process_drag = value;
905 void BC_ListBox::set_master_column(int value, int redraw)
907 this->master_column = value;
914 void BC_ListBox::set_search_column(int value)
916 this->search_column = value;
919 int BC_ListBox::get_sort_column()
924 void BC_ListBox::set_sort_column(int value, int redraw)
933 int BC_ListBox::get_sort_order()
938 void BC_ListBox::set_sort_order(int value, int redraw)
951 int BC_ListBox::get_display_mode()
953 return display_format;
956 int BC_ListBox::get_yposition()
961 int BC_ListBox::get_xposition()
966 int BC_ListBox::get_highlighted_item()
968 return highlighted_item;
972 int BC_ListBox::get_item_x(BC_ListBoxItem *item)
974 if(display_format == LISTBOX_TEXT)
976 return item->text_x - xposition + 2;
979 if(display_format == LISTBOX_ICON_LIST)
981 return item->text_x - xposition + 2;
985 return item->icon_x - xposition + 2;
989 int BC_ListBox::get_item_y(BC_ListBoxItem *item)
992 if(display_format == LISTBOX_TEXT)
994 result = item->text_y - yposition + title_h + 2;
997 if(display_format == LISTBOX_ICON_LIST)
999 result = item->text_y - yposition + title_h + 2;
1003 result = item->icon_y - yposition + title_h + 2;
1009 int BC_ListBox::get_item_w(BC_ListBoxItem *item)
1011 if(display_format == LISTBOX_ICONS)
1014 get_icon_mask(item, x, y, w, h);
1016 get_text_mask(item, x, y, w, h);
1019 return icon_position == ICON_LEFT ? icon_w + text_w :
1020 icon_w > text_w ? icon_w : text_w;
1022 return get_text_w(item) + 2 * LISTBOX_MARGIN;
1025 int BC_ListBox::get_item_h(BC_ListBoxItem *item)
1027 if(display_format == LISTBOX_ICONS)
1030 get_icon_mask(item, x, y, w, h);
1032 get_text_mask(item, x, y, w, h);
1035 return icon_position != ICON_LEFT ? icon_h + text_h :
1036 icon_h > text_h ? icon_h : text_h;
1038 return get_text_h(item);
1042 int BC_ListBox::get_icon_w(BC_ListBoxItem *item)
1044 return item->get_icon_w();
1047 int BC_ListBox::get_icon_h(BC_ListBoxItem *item)
1049 return item->get_icon_h();
1052 int BC_ListBox::get_text_w(BC_ListBoxItem *item)
1054 int w = item->get_text_w();
1055 if( w < 0 ) item->set_text_w(w = get_text_width(MEDIUMFONT, item->get_text()));
1059 int BC_ListBox::get_text_h(BC_ListBoxItem *item)
1061 int h = item->get_text_h();
1062 if( h < 0 ) item->set_text_h(h = get_text_height(MEDIUMFONT));
1066 int BC_ListBox::get_baseline(BC_ListBoxItem *item)
1068 int b = item->get_baseline();
1069 if( b < 0 ) item->set_baseline(b = get_text_ascent(MEDIUMFONT));
1073 int BC_ListBox::get_items_width()
1077 if(display_format == LISTBOX_ICONS)
1079 for(int i = 0; i < columns; i++)
1081 for(int j = 0; j < data[i].total; j++)
1084 BC_ListBoxItem *item = data[i].values[j];
1087 get_icon_mask(item, x, y, w, h);
1088 if(x1 + w > widest) widest = x1 + w;
1090 if(display_format == LISTBOX_ICONS && icon_position == ICON_LEFT)
1093 get_text_mask(item, x, y, w, h);
1094 if(x1 + w > widest) widest = x1 + w;
1099 if(display_format == LISTBOX_TEXT)
1101 return get_column_offset(columns);
1105 return get_column_offset(columns);
1110 int BC_ListBox::get_items_height(ArrayList<BC_ListBoxItem*> *data,
1127 for(int j = 0; j < (data ? data[master_column].total : 0); j++)
1130 BC_ListBoxItem *item = data[master_column].values[j];
1132 if(display_format == LISTBOX_ICONS)
1134 get_icon_mask(item, x, y, w, h);
1135 if(y + h + yposition > highest) highest = y + h + yposition;
1137 get_text_mask(item, x, y, w, h);
1138 if(y + h + yposition > highest) highest = y + h + yposition;
1142 get_text_mask(item, x, y, w, h);
1146 // Descend into sublist
1147 if(item->get_sublist() &&
1150 get_items_height(item->get_sublist(),
1151 item->get_columns(),
1157 if((display_format == LISTBOX_TEXT ||
1158 display_format == LISTBOX_ICON_LIST) &&
1161 highest = LISTBOX_MARGIN + *result;
1168 int BC_ListBox::set_yposition(int position, int draw_items)
1170 this->yposition = position;
1173 this->draw_items(1);
1178 int BC_ListBox::set_xposition(int position)
1180 this->xposition = position;
1185 void BC_ListBox::expand_item(BC_ListBoxItem *item, int expand)
1189 item->expand = expand;
1190 // Collapse sublists if this is collapsed to make it easier to calculate
1192 if(item->get_sublist())
1193 collapse_recursive(item->get_sublist(), master_column);
1196 // Set everything for autoplacement
1198 set_autoplacement(data, 0, 1);
1204 void BC_ListBox::collapse_recursive(ArrayList<BC_ListBoxItem*> *data,
1207 for(int i = 0; i < data[master_column].total; i++)
1209 BC_ListBoxItem *item = data[master_column].values[i];
1210 if(item->get_sublist() && item->expand)
1213 collapse_recursive(item->get_sublist(), master_column);
1218 void BC_ListBox::set_autoplacement(ArrayList<BC_ListBoxItem*> *data,
1222 for(int i = 0; i < data[0].total; i++)
1224 for(int j = 0; j < columns; j++)
1226 if(do_icons) data[j].values[i]->autoplace_icon = 1;
1227 if(do_text) data[j].values[i]->autoplace_text = 1;
1230 BC_ListBoxItem *item = data[master_column].values[i];
1231 if(item->get_sublist())
1233 set_autoplacement(item->get_sublist(), do_icons, do_text);
1240 int BC_ListBox::get_yscroll_x()
1243 return popup_w - get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w();
1247 get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w();
1250 int BC_ListBox::get_yscroll_y()
1258 int BC_ListBox::get_yscroll_height()
1260 return popup_h - (need_xscroll ?
1261 get_resources()->hscroll_data[SCROLL_HANDLE_UP]->get_h() :
1265 int BC_ListBox::get_xscroll_x()
1273 int BC_ListBox::get_xscroll_y()
1277 get_resources()->hscroll_data[SCROLL_HANDLE_UP]->get_h();
1281 get_resources()->hscroll_data[SCROLL_HANDLE_UP]->get_h();
1284 int BC_ListBox::get_xscroll_width()
1286 return popup_w - (need_yscroll ?
1287 get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w() :
1291 int BC_ListBox::get_column_offset(int column)
1297 column_width[--column] :
1298 default_column_width[--column];
1303 void BC_ListBox::column_width_boundaries()
1306 for(int i = 0; i < columns; i++) {
1307 if(column_width[i] < MIN_COLUMN_WIDTH)
1308 column_width[i] = MIN_COLUMN_WIDTH;
1312 for(int i = 0; i < columns; i++) {
1313 if(default_column_width[i] < MIN_COLUMN_WIDTH)
1314 default_column_width[i] = MIN_COLUMN_WIDTH;
1319 int BC_ListBox::get_column_width(int column, int clamp_right)
1321 if(column < columns - 1 || !clamp_right)
1322 return column_width ? column_width[column] : default_column_width[column];
1323 return popup_w + xposition - get_column_offset(column);
1326 int BC_ListBox::get_icon_mask(BC_ListBoxItem *item,
1327 int &x, int &y, int &w, int &h)
1329 if( display_format == LISTBOX_ICONS ) {
1330 x = get_item_x(item);
1331 y = get_item_y(item);
1332 w = get_icon_w(item) + ICON_MARGIN * 2;
1333 h = get_icon_h(item) + ICON_MARGIN * 2;
1340 int BC_ListBox::get_text_mask(BC_ListBoxItem *item,
1341 int &x, int &y, int &w, int &h)
1343 x = get_item_x(item);
1344 y = get_item_y(item);
1346 if(display_format == LISTBOX_ICONS) {
1347 if(icon_position == ICON_LEFT) {
1348 x += get_icon_w(item) + ICON_MARGIN * 2;
1349 y += get_icon_h(item) - get_text_h(item);
1352 y += get_icon_h(item) + ICON_MARGIN;
1355 w = get_text_w(item) + ICON_MARGIN * 2;
1356 h = get_text_h(item) + ICON_MARGIN * 2;
1359 if(display_format == LISTBOX_TEXT) {
1360 w = get_text_w(item) + LISTBOX_MARGIN * 2;
1361 h = get_text_h(item);
1365 w = get_text_width(MEDIUMFONT, item->text) + LISTBOX_MARGIN * 2;
1371 int BC_ListBox::get_item_highlight(ArrayList<BC_ListBoxItem*> *data,
1375 BC_Resources *resources = get_resources();
1376 if(data[column].values[item]->selected)
1377 return resources->listbox_selected;
1378 else if(highlighted_item >= 0 &&
1379 highlighted_ptr == data[master_column].values[item])
1380 return resources->listbox_highlighted;
1381 return resources->listbox_inactive;
1384 int BC_ListBox::get_item_color(ArrayList<BC_ListBoxItem*> *data,
1388 int color = data[column].values[item]->color;
1389 if( color == -1 ) color = get_resources()->listbox_text;
1390 if( get_item_highlight(data, column, item) == color )
1395 int BC_ListBox::get_from_column()
1397 return dragged_title;
1400 int BC_ListBox::get_to_column()
1402 return highlighted_title;
1406 BC_ListBoxItem* BC_ListBox::get_selection(int column,
1407 int selection_number)
1409 return get_selection_recursive(data,
1414 BC_ListBoxItem* BC_ListBox::get_selection_recursive(
1415 ArrayList<BC_ListBoxItem*> *data,
1417 int selection_number)
1421 for(int i = 0; i < data[master_column].total; i++)
1423 BC_ListBoxItem *item = data[master_column].values[i];
1426 //printf("BC_ListBox::get_selection_recursive %d\n", __LINE__);
1428 if(selection_number < 0)
1431 return data[column].values[i];
1435 if(item->get_sublist())
1437 BC_ListBoxItem *result = get_selection_recursive(item->get_sublist(),
1440 if(result) return result;
1448 int BC_ListBox::get_selection_number(int column,
1449 int selection_number)
1451 return get_selection_number_recursive(data,
1456 int BC_ListBox::get_selection_number_recursive(
1457 ArrayList<BC_ListBoxItem*> *data,
1459 int selection_number,
1464 if(!counter) counter = &temp;
1466 for(int i = 0; i < data[master_column].total; i++)
1469 BC_ListBoxItem *item = data[master_column].values[i];
1473 if(selection_number < 0)
1478 if(item->get_sublist())
1480 int result = get_selection_number_recursive(
1481 item->get_sublist(),
1485 if(result >= 0) return result;
1492 int BC_ListBox::set_selection_mode(int mode)
1494 this->selection_mode = mode;
1498 void BC_ListBox::delete_columns()
1502 for(int i = 0; i < columns; i++)
1504 delete [] column_titles[i];
1506 delete [] column_titles;
1509 if(column_width) delete [] column_width;
1515 // Need to copy titles so EDL can change
1516 void BC_ListBox::set_columns(const char **column_titles,
1520 if((!column_titles && column_width) ||
1521 (column_titles && !column_width))
1523 printf("BC_ListBox::set_columns either column_titles or column_width == NULL but not both.\n");
1532 this->column_titles = new char*[columns];
1533 for(int i = 0; i < columns; i++)
1535 this->column_titles[i] = new char[strlen(column_titles[i]) + 1];
1536 strcpy(this->column_titles[i], column_titles[i]);
1542 this->column_width = new int[columns];
1543 for(int i = 0; i < columns; i++)
1545 this->column_width[i] = column_width[i];
1549 this->columns = columns;
1554 int BC_ListBox::update(ArrayList<BC_ListBoxItem*> *data,
1555 const char **column_titles,
1560 int highlighted_number,
1561 int recalc_positions,
1564 set_columns(column_titles,
1570 this->yposition = yposition;
1571 this->xposition = xposition;
1572 this->highlighted_item = highlighted_number;
1573 this->highlighted_ptr = index_to_item(data, highlighted_number, 0);
1575 if(recalc_positions)
1576 set_autoplacement(data, 1, 1);
1578 init_column_width();
1583 update_scrollbars(1);
1589 void BC_ListBox::center_selection()
1591 int selection = get_selection_number(0, 0);
1593 calculate_item_coords();
1594 center_selection(selection);
1600 update_scrollbars(0);
1601 gui->show_window(1);
1605 void BC_ListBox::move_vertical(int pixels)
1609 void BC_ListBox::move_horizontal(int pixels)
1613 int BC_ListBox::select_previous(int skip,
1614 BC_ListBoxItem *selected_item,
1616 ArrayList<BC_ListBoxItem*> *data,
1622 selected_item = get_selection(0, 0);
1634 got_second = &temp3;
1638 // Scan backwards to item pointer. Then count visible items to get
1639 // destination. No wraparound.
1642 for(int i = data[master_column].total - 1; i >= 0; i--)
1644 BC_ListBoxItem *current_item = data[master_column].values[i];
1645 if(current_item->get_sublist() &&
1646 current_item->get_expand())
1648 int result = select_previous(skip,
1651 current_item->get_sublist(),
1663 if((*counter) >= skip)
1665 for(int j = 0; j < columns; j++)
1666 data[j].values[i]->selected = 1;
1668 return item_to_index(this->data, current_item);
1673 if(current_item->selected)
1675 for(int j = 0; j < columns; j++)
1676 data[j].values[i]->selected = 0;
1683 // Hit top of top level without finding a selected item.
1686 // Select first item in top level and quit
1687 BC_ListBoxItem *current_item;
1689 current_item = data[master_column].values[0];
1691 for(int j = 0; j < columns; j++)
1692 data[j].values[0]->selected = 1;
1694 return item_to_index(this->data, current_item);
1696 }while(top_level && data[master_column].total);
1700 int BC_ListBox::select_next(int skip,
1701 BC_ListBoxItem *selected_item,
1703 ArrayList<BC_ListBoxItem*> *data,
1709 selected_item = get_selection(0, 0);
1721 got_second = &temp3;
1725 // Scan forwards to currently selected item pointer.
1726 // Then count visible items to get destination. No wraparound.
1729 for(int i = 0; i < data[master_column].total; i++)
1731 BC_ListBoxItem *current_item = data[master_column].values[i];
1733 // Select next item once the number items after the currently selected item
1734 // have been passed.
1738 if((*counter) >= skip)
1740 for(int j = 0; j < columns; j++)
1741 data[j].values[i]->selected = 1;
1743 return item_to_index(this->data, current_item);
1748 // Got currently selected item. Deselect it.
1749 if(current_item->selected)
1751 for(int j = 0; j < columns; j++)
1752 data[j].values[i]->selected = 0;
1758 // Descend into expanded level
1759 if(current_item->get_sublist() &&
1760 current_item->get_expand())
1762 int result = select_next(skip,
1765 current_item->get_sublist(),
1775 // Hit bottom of top level without finding the next item.
1778 BC_ListBoxItem *current_item;
1779 // Select first item in top level and quit
1783 current_item = data[master_column].values[0];
1785 for(int j = 0; j < columns; j++)
1786 data[j].values[0]->selected = 1;
1791 // Select last item in top level and quit
1793 int current_row = data[master_column].total - 1;
1794 current_item = data[master_column].values[current_row];
1796 for(int j = 0; j < columns; j++)
1797 data[j].values[current_row]->selected = 1;
1801 return item_to_index(this->data, current_item);
1803 }while(top_level && data[master_column].total);
1809 void BC_ListBox::clamp_positions()
1811 items_w = get_items_width();
1812 items_h = get_items_height(data, columns);
1814 if(yposition < 0) yposition = 0;
1816 if(yposition > items_h - view_h)
1817 yposition = items_h - view_h;
1819 if(yposition < 0) yposition = 0;
1821 if(xposition < 0) xposition = 0;
1823 if(xposition >= items_w - view_w)
1824 xposition = items_w - view_w;
1826 if(xposition < 0) xposition = 0;
1829 int BC_ListBox::center_selection(int selection,
1830 ArrayList<BC_ListBoxItem*> *data,
1834 if(!data) data = this->data;
1835 if(!counter) counter = &temp;
1837 for(int i = 0; i < data[master_column].total; i++)
1842 BC_ListBoxItem *item = data[master_column].values[i];
1843 if((*counter) == selection)
1845 BC_ListBoxItem *top_item = this->data[master_column].values[0];
1848 if(display_format == LISTBOX_ICONS)
1850 // Icon is out of window
1851 if( item->icon_y-yposition > view_h-get_text_h(item) ||
1852 item->icon_y-yposition < 0 ) {
1853 yposition = item->icon_y - view_h / 2;
1856 if(data[master_column].values[selection]->icon_x - xposition > view_w ||
1857 data[master_column].values[selection]->icon_x - xposition < 0)
1859 xposition = item->icon_x - view_w / 2;
1864 // Text coordinate is out of window
1865 if( item->text_y-yposition > view_h-get_text_h(item) ||
1866 item->text_y-yposition < 0 ) {
1867 yposition = item->text_y -
1876 if(item->get_sublist())
1878 int result = center_selection(selection,
1879 item->get_sublist(),
1881 if(result) return result;
1887 void BC_ListBox::update_scrollbars(int flush)
1889 int h_needed = items_h = get_items_height(data, columns);
1890 int w_needed = items_w = get_items_width();
1892 // if(columns > 0 && column_width)
1893 // printf("BC_ListBox::update_scrollbars 1 %d %d\n", column_width[columns - 1], w_needed);
1897 if(xposition != xscrollbar->get_value())
1898 xscrollbar->update_value(xposition);
1900 if(w_needed != xscrollbar->get_length() ||
1901 view_w != xscrollbar->get_handlelength())
1902 xscrollbar->update_length(w_needed, xposition, view_w, 0);
1907 if(yposition != yscrollbar->get_value())
1908 yscrollbar->update_value(yposition);
1910 if(h_needed != yscrollbar->get_length() || view_h != yscrollbar->get_handlelength())
1911 yscrollbar->update_length(h_needed, yposition, view_h, 0);
1914 if(flush) this->flush();
1917 int BC_ListBox::get_scrollbars()
1919 int h_needed = items_h = get_items_height(data, columns);
1920 int w_needed = items_w = get_items_width();
1924 title_h = get_title_h();
1926 view_h = popup_h - title_h - 4;
1927 view_w = popup_w - 4;
1929 // Create scrollbars as needed
1930 for(int i = 0; i < 2; i++)
1932 if(w_needed > view_w)
1937 get_resources()->hscroll_data[SCROLL_HANDLE_UP]->get_h() -
1945 if(h_needed > view_h)
1949 get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w() -
1958 // Update subwindow size
1959 int new_w = popup_w;
1960 int new_h = popup_h;
1961 if(need_xscroll) new_h -= get_resources()->hscroll_data[SCROLL_HANDLE_UP]->get_h();
1962 if(need_yscroll) new_w -= get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w();
1965 if(new_w != BC_WindowBase::get_w() || new_h != BC_WindowBase::get_h())
1966 gui->resize_window(new_w, new_h);
1968 BC_WindowBase *destination = (is_popup ? gui : parent_window);
1973 destination->add_subwindow(xscrollbar =
1974 new BC_ListBoxXScroll(this,
1978 xscrollbar->show_window(0);
1979 xscrollbar->bound_to = this;
1983 xscrollbar->update_length(w_needed, xposition, view_w, flush);
1984 xscrollbar->reposition_window(get_xscroll_x(),
1986 get_xscroll_width());
1991 if(xscrollbar) delete xscrollbar;
2000 destination->add_subwindow(yscrollbar =
2001 new BC_ListBoxYScroll(this,
2005 yscrollbar->show_window(0);
2006 yscrollbar->bound_to = this;
2010 yscrollbar->update_length(h_needed, yposition, view_h, flush);
2011 yscrollbar->reposition_window(get_yscroll_x(),
2013 get_yscroll_height());
2018 if(yscrollbar) delete yscrollbar;
2024 view_w + 4 != bg_surface->get_w() ||
2025 view_h + 4 != bg_surface->get_h())
2027 if(bg_surface) delete bg_surface;
2028 bg_surface = new BC_Pixmap(gui, view_w + 4, view_h + 4);
2038 void BC_ListBox::set_drag_scroll(int value)
2040 allow_drag_scroll = value;
2044 // Test for scrolling by dragging
2046 int BC_ListBox::test_drag_scroll(int cursor_x, int cursor_y)
2049 if(allow_drag_scroll ||
2050 current_operation == SELECT_RECT)
2053 int top_boundary = get_title_h();
2055 if(cursor_y < top_boundary ||
2056 cursor_y >= view_h + title_h + LISTBOX_BORDER * 2 ||
2057 cursor_x < LISTBOX_BORDER ||
2058 cursor_x >= view_w + LISTBOX_BORDER)
2066 int BC_ListBox::drag_scroll_event()
2068 int top_boundary = get_title_h();
2071 if(get_cursor_y() < top_boundary)
2073 yposition -= top_boundary - get_cursor_y();
2077 if(get_cursor_y() >= view_h + title_h + 4)
2079 yposition += get_cursor_y() - (view_h + title_h + 4);
2083 if(get_cursor_x() < 2)
2085 xposition -= 2 - get_cursor_x();
2089 if(get_cursor_x() >= view_w + 2)
2091 xposition += get_cursor_x() - (view_w + 2);
2094 if(result) clamp_positions();
2098 int BC_ListBox::rectangle_scroll_event()
2100 int old_xposition = xposition;
2101 int old_yposition = yposition;
2102 int result = drag_scroll_event();
2106 rect_x1 += old_xposition - xposition;
2107 rect_y1 += old_yposition - yposition;
2108 rect_x2 = get_cursor_x();
2109 rect_y2 = get_cursor_y();
2111 int x1 = MIN(rect_x1, rect_x2);
2112 int x2 = MAX(rect_x1, rect_x2);
2113 int y1 = MIN(rect_y1, rect_y2);
2114 int y2 = MAX(rect_y1, rect_y2);
2116 if(select_rectangle(data,
2122 selection_changed();
2127 update_scrollbars(1);
2132 int BC_ListBox::select_scroll_event()
2134 int result = drag_scroll_event();
2138 highlighted_item = selection_number = get_cursor_item(data,
2144 update_scrollbars(1);
2145 selection_changed();
2150 int BC_ListBox::select_rectangle(ArrayList<BC_ListBoxItem*> *data,
2157 for(int i = 0; i < data[master_column].total; i++)
2159 for(int j = 0; j < columns; j++)
2161 BC_ListBoxItem *item = data[j].values[i];
2162 if(display_format == LISTBOX_ICONS)
2164 int icon_x, icon_y, icon_w, icon_h;
2165 int text_x, text_y, text_w, text_h;
2166 get_icon_mask(item, icon_x, icon_y, icon_w, icon_h);
2167 get_text_mask(item, text_x, text_y, text_w, text_h);
2169 if((x2 >= icon_x && x1 < icon_x + icon_w &&
2170 y2 >= icon_y && y1 < icon_y + icon_h) ||
2171 (x2 >= text_x && x1 < text_x + text_w &&
2172 y2 >= text_y && y1 < text_y + text_h))
2193 gui->get_w() - get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w() :
2196 y1 < gui->get_h() &&
2197 y2 >= get_item_y(item) &&
2198 y1 < get_item_y(item) + get_item_h(item))
2217 BC_ListBoxItem *item = data[master_column].values[i];
2218 if(item->get_sublist() &&
2220 result |= select_rectangle(item->get_sublist(),
2229 int BC_ListBox::reposition_item(ArrayList<BC_ListBoxItem*> *data,
2230 int selection_number,
2236 if(!counter) counter = &temp;
2239 for(int i = 0; i < data[master_column].total; i++)
2241 BC_ListBoxItem *item = data[master_column].values[i];
2243 if((*counter) == selection_number)
2249 // Not recursive because it's only used for icons
2254 void BC_ListBox::move_selection(ArrayList<BC_ListBoxItem*> *dst,
2255 ArrayList<BC_ListBoxItem*> *src)
2257 for(int i = 0; i < src[master_column].total; i++)
2259 BC_ListBoxItem *item = src[master_column].values[i];
2264 for(int j = 0; j < columns; j++)
2266 dst[j].append(src[j].values[i]);
2267 src[j].remove_number(i);
2271 // Descend into sublist
2272 if(item->get_sublist())
2275 item->get_sublist());
2280 int BC_ListBox::put_selection(ArrayList<BC_ListBoxItem*> *data,
2281 ArrayList<BC_ListBoxItem*> *src,
2286 if(!counter) counter = &temp;
2290 for(int j = 0; j < columns; j++)
2292 for(int i = 0; i < src[j].total; i++)
2294 data[j].append(src[j].values[i]);
2300 for(int i = 0; i < data[master_column].total; i++)
2303 if((*counter) == destination)
2305 for(int j = 0; j < columns; j++)
2307 for(int k = 0; k < src[j].total; k++)
2309 data[j].insert(src[j].values[k], destination + k);
2315 BC_ListBoxItem *item = data[master_column].values[i];
2316 if(item->get_sublist())
2318 if(put_selection(item->get_sublist(),
2330 int BC_ListBox::item_to_index(ArrayList<BC_ListBoxItem*> *data,
2331 BC_ListBoxItem *item,
2335 if(!counter) counter = &temp;
2337 for(int i = 0; i < data[master_column].total; i++)
2340 for(int j = 0; j < columns; j++)
2342 BC_ListBoxItem *new_item = data[j].values[i];
2343 //printf("BC_ListBox::item_to_index 1 %d %d %p\n", j, i, new_item);
2344 if(new_item == item)
2350 BC_ListBoxItem *new_item = data[master_column].values[i];
2351 if(new_item->get_sublist())
2353 if(item_to_index(new_item->get_sublist(),
2363 BC_ListBoxItem* BC_ListBox::index_to_item(ArrayList<BC_ListBoxItem*> *data,
2369 if(!counter) counter = &temp;
2370 for(int i = 0; i < data[master_column].total; i++)
2373 if((*counter) == number)
2375 return data[column].values[i];
2377 BC_ListBoxItem *item = data[master_column].values[i];
2378 if(item->get_sublist())
2380 BC_ListBoxItem *result = index_to_item(item->get_sublist(),
2384 if(result) return result;
2390 int BC_ListBox::get_cursor_item(ArrayList<BC_ListBoxItem*> *data,
2393 BC_ListBoxItem **item_return,
2398 if(!data) return -1;
2399 if(!counter) counter = &temp;
2401 // Icons are not treed
2402 if(display_format == LISTBOX_ICONS)
2404 for(int j = data[master_column].total - 1; j >= 0; j--)
2406 int icon_x, icon_y, icon_w, icon_h;
2407 int text_x, text_y, text_w, text_h;
2408 BC_ListBoxItem *item = data[master_column].values[j];
2409 get_icon_mask(item, icon_x, icon_y, icon_w, icon_h);
2410 get_text_mask(item, text_x, text_y, text_w, text_h);
2412 if((cursor_x >= icon_x && cursor_x < icon_x + icon_w &&
2413 cursor_y >= icon_y && cursor_y < icon_y + icon_h) ||
2414 (cursor_x >= text_x && cursor_x < text_x + text_w &&
2415 cursor_y >= text_y && cursor_y < text_y + text_h))
2417 if(item_return) (*item_return) = item;
2424 // Cursor is inside items rectangle
2426 cursor_x < (yscrollbar ?
2427 gui->get_w() - get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w() :
2429 // Only clamp y if we're not in a SELECT operation.
2430 (current_operation == BC_ListBox::SELECT ||
2431 (cursor_y > get_title_h() + LISTBOX_BORDER &&
2432 cursor_y < gui->get_h())))
2434 // Search table for cursor obstruction
2435 for(int i = 0; i < data[master_column].total; i++)
2437 BC_ListBoxItem *item = data[master_column].values[i];
2440 // Cursor is inside item on current level
2443 cursor_y >= get_item_y(item) &&
2444 cursor_y < get_item_y(item) + get_item_h(item))
2446 //printf("BC_ListBox::get_cursor_item %d %d %p\n", master_column, i, item);
2447 if(item_return) (*item_return) = item;
2451 // Descend into sublist
2452 if(item->get_sublist())
2454 if(get_cursor_item(item->get_sublist(),
2459 item->get_expand()) >= 0)
2468 int BC_ListBox::repeat_event(int64_t duration)
2470 switch(current_operation)
2472 // Repeat out of bounds selection
2474 if(duration == get_resources()->scroll_repeat)
2475 return rectangle_scroll_event();
2479 if(duration == get_resources()->scroll_repeat)
2480 return select_scroll_event();
2485 if(button_highlighted && is_popup && !tooltip_done &&
2486 tooltip_text && tooltip_text[0] != 0 &&
2487 duration == get_resources()->tooltip_delay)
2499 int BC_ListBox::cursor_enter_event()
2503 switch(current_operation)
2505 // Cursor moved over button, pressed, and exited.
2506 case BUTTON_DOWN_SELECT:
2507 if(top_level->event_win == win)
2509 current_operation = BUTTON_DN;
2511 button_highlighted = 1;
2517 // Cursor entered button
2518 if(is_popup && top_level->event_win == win)
2520 button_highlighted = 1;
2525 // TODO: Need to get the highlighted column title or item
2526 if(gui && top_level->event_win == gui->win)
2528 list_highlighted = 1;
2538 int BC_ListBox::cursor_leave_event()
2540 if(current_operation == COLUMN_DRAG) return 0;
2543 if(button_highlighted)
2545 button_highlighted = 0;
2550 if(list_highlighted)
2552 list_highlighted = 0;
2553 highlighted_item = -1;
2554 highlighted_ptr = 0;
2555 highlighted_title = -1;
2556 int redraw_toggles = 0;
2557 for(int i = 0; i < expanders.total; i++)
2558 expanders.values[i]->cursor_leave_event(&redraw_toggles);
2566 int BC_ListBox::get_first_selection(ArrayList<BC_ListBoxItem*> *data, int *result)
2569 if(!result) result = &temp;
2571 for(int i = 0; i < data[master_column].total; i++)
2573 BC_ListBoxItem *item = data[master_column].values[i];
2575 if(item->selected) return (*result);
2576 if(item->get_sublist())
2578 if(get_first_selection(item->get_sublist(), result) >= 0)
2585 int BC_ListBox::get_total_items(ArrayList<BC_ListBoxItem*> *data,
2590 if(!result) result = &temp;
2592 for(int i = 0; i < data[master_column].total; i++)
2595 if(data[master_column].values[i]->get_sublist())
2596 get_total_items(data[master_column].values[i]->get_sublist(),
2605 int BC_ListBox::get_last_selection(ArrayList<BC_ListBoxItem*> *data,
2616 for(int i = data[master_column].total - 1; i >= 0; i--)
2618 BC_ListBoxItem *item = data[master_column].values[i];
2623 return get_total_items(data, 0, master_column) - (*result) /* - 1 */;
2628 if(item->get_sublist())
2630 if(get_last_selection(item->get_sublist(), result) >= 0)
2633 return get_total_items(data, 0, master_column) - (*result) /* - 1 */;
2642 void BC_ListBox::select_range(ArrayList<BC_ListBoxItem*> *data,
2648 if(!current) current = &temp;
2650 for(int i = 0; i < data[master_column].total; i++)
2653 if((*current) >= start && (*current) < end)
2655 for(int j = 0; j < columns; j++)
2656 data[j].values[i]->selected = 1;
2658 BC_ListBoxItem *item = data[master_column].values[i];
2659 if(item->get_sublist())
2660 select_range(item->get_sublist(),
2668 // Fill items between current selection and new selection
2669 int BC_ListBox::expand_selection(int button_press, int selection_number)
2671 int old_selection_start = selection_start;
2672 int old_selection_end = selection_end;
2674 // printf("BC_ListBox::expand_selection %d %d\n",
2675 // selection_center,
2676 // selection_number);
2678 // Calculate the range to select based on selection_center and selection_number
2679 if(selection_number < selection_center)
2681 selection_start = selection_number;
2685 selection_end = selection_number + 1;
2688 //printf("BC_ListBox::expand_selection %d %d %d %d\n", old_selection_start, old_selection_end, selection_start, selection_end);
2689 // Recurse through all the items and select the desired range
2690 select_range(data, selection_start, selection_end);
2693 return (old_selection_start != selection_start ||
2694 old_selection_end != selection_end);
2697 int BC_ListBox::toggle_item_selection(ArrayList<BC_ListBoxItem*> *data,
2698 int selection_number,
2702 if(!counter) counter = &temp;
2704 for(int i = 0; i < data[master_column].total; i++)
2706 BC_ListBoxItem *item = data[master_column].values[i];
2708 if((*counter) == selection_number)
2710 // Get new value for selection
2711 int selected = !item->selected;
2713 for(int j = 0; j < columns; j++)
2714 data[j].values[i]->selected = selected;
2718 // Descend into sublist
2719 if(item->get_sublist())
2721 if(toggle_item_selection(item->get_sublist(),
2732 void BC_ListBox::set_all_selected(ArrayList<BC_ListBoxItem*> *data, int value)
2734 for(int i = 0; i < data[master_column].total; i++)
2736 for(int j = 0; j < columns; j++)
2738 BC_ListBoxItem *item = data[j].values[i];
2739 item->selected = value;
2741 BC_ListBoxItem *item = data[master_column].values[i];
2742 if(item->get_sublist())
2744 set_all_selected(item->get_sublist(), value);
2749 void BC_ListBox::set_selected(ArrayList<BC_ListBoxItem*> *data,
2755 if(!counter) counter = &temp;
2756 for(int i = 0; i < data[master_column].total && (*counter) != item_number; i++)
2759 if((*counter) == item_number)
2761 for(int j = 0; j < columns; j++)
2763 BC_ListBoxItem *item = data[j].values[i];
2764 item->selected = value;
2769 BC_ListBoxItem *item = data[master_column].values[i];
2770 if(item->get_sublist())
2772 set_selected(item->get_sublist(),
2780 int BC_ListBox::update_selection(ArrayList<BC_ListBoxItem*> *data,
2781 int selection_number,
2786 if(!counter) counter = &temp;
2788 for(int i = 0; i < data[master_column].total; i++)
2790 BC_ListBoxItem *item = data[master_column].values[i];
2792 if((*counter) == selection_number && !item->selected)
2795 for(int j = 0; j < columns; j++)
2796 data[j].values[i]->selected = 1;
2799 if((*counter) != selection_number && item->selected)
2802 for(int j = 0; j < columns; j++)
2803 data[j].values[i]->selected = 0;
2805 if(item->get_sublist())
2806 result |= update_selection(item->get_sublist(),
2813 void BC_ListBox::promote_selections(ArrayList<BC_ListBoxItem*> *data,
2817 for(int i = 0; i < data[master_column].total; i++)
2819 for(int j = 0; j < columns; j++)
2821 BC_ListBoxItem *item = data[j].values[i];
2822 if(item->selected == old_value) item->selected = new_value;
2824 BC_ListBoxItem *item = data[master_column].values[i];
2825 if(item->get_sublist())
2826 promote_selections(item->get_sublist(), old_value, new_value);
2830 int BC_ListBox::focus_out_event()
2836 int BC_ListBox::button_press_event()
2839 BC_ListBoxItem *current_item = 0;
2841 int do_selection_change = 0;
2842 const int debug = 0;
2845 if(debug) printf("BC_ListBox::button_press_event %d this=%p event_win=%p %p %p %p\n",
2848 (void*)top_level->event_win,
2849 (void*)(gui ? gui->win : 0),
2851 (void*)parent_window->win);
2853 // Pressed in button
2854 if(is_popup && top_level->event_win == win)
2856 if(debug) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2857 current_operation = BUTTON_DN;
2861 if(!active && !disabled)
2863 top_level->deactivate();
2870 // Pressed in scrollbar
2871 if((xscrollbar && top_level->event_win == xscrollbar->win) ||
2872 (yscrollbar && top_level->event_win == yscrollbar->win))
2874 if(debug) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2879 if(gui && top_level->event_win == gui->win)
2881 if(debug) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2883 // Activate list items
2884 // If it is a suggestion popup, it is visible without being active
2887 if(debug) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2888 if(!is_suggestions) top_level->deactivate();
2889 if(debug) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2891 if(debug) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2894 // Wheel mouse pressed
2895 if(get_buttonpress() == 4)
2897 if(current_operation == NO_OPERATION)
2899 current_operation = WHEEL;
2902 set_yposition(yposition - gui->get_h() / 10, 0);
2904 update_scrollbars(0);
2905 highlighted_ptr = 0;
2906 highlighted_item = get_cursor_item(data,
2907 top_level->cursor_x,
2908 top_level->cursor_y,
2916 if(get_buttonpress() == 5)
2918 if(current_operation == NO_OPERATION)
2920 current_operation = WHEEL;
2923 set_yposition(yposition + gui->get_h() / 10, 0);
2925 update_scrollbars(0);
2926 highlighted_ptr = 0;
2927 highlighted_item = get_cursor_item(data,
2928 top_level->cursor_x,
2929 top_level->cursor_y,
2937 // Pressed over column title division
2938 if(test_column_divisions(gui->get_cursor_x(),
2939 gui->get_cursor_y(),
2942 drag_cursor_x = gui->get_cursor_x() + xposition;
2944 drag_column_w = column_width[highlighted_division - 1];
2946 drag_column_w = default_column_width[highlighted_division - 1];
2948 current_operation = DRAG_DIVISION;
2952 // Pressed in column title
2953 if(test_column_titles(gui->get_cursor_x(), gui->get_cursor_y()))
2955 current_operation = COLUMN_DN;
2956 button_highlighted = 0;
2957 list_highlighted = 1;
2962 // Pressed in expander
2963 if(test_expanders())
2965 current_operation = EXPAND_DN;
2966 // Need to redraw items because of alpha
2971 // Pressed over item
2972 if((selection_number = get_cursor_item(data,
2973 gui->get_cursor_x(),
2974 gui->get_cursor_y(),
2975 ¤t_item)) >= 0)
2977 if(debug) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2979 // Get item button was pressed over
2980 selection_number2 = selection_number1;
2981 selection_number1 = selection_number;
2983 selection_start = -1;
2987 // Multiple item selection is possible
2988 if(selection_mode == LISTBOX_MULTIPLE &&
2989 (ctrl_down() || shift_down()))
2991 // Expand text selection.
2992 // Fill items between selected region and current item.
2994 (display_format == LISTBOX_TEXT ||
2995 display_format == LISTBOX_ICON_LIST))
2997 // Get first item selected
2998 selection_start = get_first_selection(data);
2999 // Get last item selected
3000 selection_end = get_last_selection(data);
3001 // Get center of selected region
3002 if(selection_end > selection_start)
3004 selection_center = (selection_end + selection_start) >> 1;
3008 selection_center = selection_number;
3012 // Deselect everything.
3013 set_all_selected(data, 0);
3014 // Select just the items
3015 expand_selection(1, selection_number);
3019 // Toggle a single item on or off
3021 toggle_item_selection(data, selection_number);
3022 new_value = current_item->selected;
3026 // Select single item
3028 if(!current_item->selected)
3030 set_all_selected(data, 0);
3039 current_operation = SELECT;
3040 highlighted_item = selection_number;
3041 highlighted_ptr = current_item;
3042 button_highlighted = 0;
3043 list_highlighted = 1;
3046 do_selection_change = 1;
3051 // Pressed over nothing. Start rectangle selection.
3053 if(get_buttonpress() == 1 &&
3054 selection_mode == LISTBOX_MULTIPLE)
3058 // Deselect all and redraw if anything was selected
3059 if(get_selection_number(0, 0) >= 0)
3061 set_all_selected(data, 0);
3063 do_selection_change = 1;
3069 // Promote selections to protect from a rectangle selection
3070 promote_selections(data, 1, 2);
3073 // Start rectangle selection
3074 current_operation = SELECT_RECT;
3075 rect_x1 = rect_x2 = get_cursor_x();
3076 rect_y1 = rect_y2 = get_cursor_y();
3084 // Suggestion box is not active but visible, so lie to get it to deactivate
3085 if(is_popup && (active || (is_suggestions && gui)))
3088 if(debug) printf("BC_ListBox::button_press_event %d\n", __LINE__);
3094 if(do_selection_change) selection_changed();
3095 if(debug) printf("BC_ListBox::button_press_event %d %d\n",
3102 int BC_ListBox::button_release_event()
3105 int cursor_x, cursor_y;
3109 //printf("BC_ListBox::button_release_event 1 %d\n", current_operation);
3110 switch(current_operation)
3113 current_operation = NO_OPERATION;
3118 current_operation = NO_OPERATION;
3122 // Release item selection
3123 case BUTTON_DOWN_SELECT:
3125 //printf("BC_ListBox::button_release_event 10\n");
3126 unset_repeat(get_resources()->scroll_repeat);
3127 current_operation = NO_OPERATION;
3129 translate_coordinates(top_level->event_win, gui->win,
3130 gui->get_cursor_x(), gui->get_cursor_y(),
3131 &cursor_x, &cursor_y);
3132 selection_number1 = selection_number =
3133 get_cursor_item(data, cursor_x, cursor_y);
3134 //printf("BC_ListBox::button_release_event %d %d\n", selection_number2, selection_number1);
3140 if(selection_number >= 0)
3146 // Second button release outside button
3147 if(button_releases > 1)
3154 if(top_level->get_double_click() &&
3155 selection_number2 == selection_number1 &&
3156 selection_number2 >= 0 &&
3157 selection_number1 >= 0)
3167 unset_repeat(get_resources()->scroll_repeat);
3170 // Demote selections from rectangle selection
3171 promote_selections(data, 2, 1);
3174 // Hide rectangle overlay
3176 current_operation = NO_OPERATION;
3180 // Release popup button
3183 current_operation = NO_OPERATION;
3187 // Second button release inside button
3188 if(button_releases > 1)
3196 current_operation = NO_OPERATION;
3197 // Update the sort column and the sort order for the user only if the existing
3198 // sort column is valid.
3199 if(sort_column >= 0)
3201 // Invert order only if column is the same
3202 if(highlighted_title == sort_column)
3204 (sort_order == SORT_ASCENDING) ?
3207 // Set the new sort column
3208 sort_column = highlighted_title;
3209 if(!sort_order_event())
3215 // Sorting not enabled. Redraw the title state.
3224 int redraw_toggles = 0;
3225 for(int i = 0; i < expanders.total && !result; i++)
3227 if(expanders.values[i]->button_release_event(&redraw_toggles))
3232 // Need to redraw items because of alpha
3233 if(redraw_toggles) draw_items(1);
3234 current_operation = NO_OPERATION;
3239 // Can't default to NO_OPERATION because it may be used in a drag event.
3244 if(do_event) handle_event();
3246 //printf("BC_ListBox::button_release_event %d %d\n", __LINE__, get_window_lock());
3250 int BC_ListBox::get_title_h()
3252 if(display_format == LISTBOX_TEXT ||
3253 display_format == LISTBOX_ICON_LIST)
3254 return column_titles ? column_bg[0]->get_h() : 0;
3259 void BC_ListBox::reset_cursor(int new_cursor)
3263 if(gui->get_cursor() != new_cursor)
3265 gui->set_cursor(new_cursor, 0, 0);
3269 if(get_cursor() != new_cursor)
3271 set_cursor(new_cursor, 0, 0);
3275 int BC_ListBox::test_column_divisions(int cursor_x, int cursor_y, int &new_cursor)
3280 cursor_y < get_title_h() &&
3282 cursor_x < gui->get_w())
3284 for(int i = 1; i < columns; i++)
3286 if(cursor_x >= -xposition + get_column_offset(i) - 5 &&
3287 cursor_x < -xposition + get_column_offset(i) +
3288 get_resources()->listbox_title_hotspot)
3290 highlighted_item = -1;
3291 highlighted_ptr = 0;
3292 highlighted_division = i;
3293 highlighted_title = -1;
3294 list_highlighted = 1;
3295 new_cursor = HSEPARATE_CURSOR;
3300 highlighted_division = -1;
3304 int BC_ListBox::test_column_titles(int cursor_x, int cursor_y)
3309 cursor_y < get_title_h() &&
3311 cursor_x < gui->get_w())
3313 for(int i = 0; i < columns; i++)
3315 if(cursor_x >= -xposition + get_column_offset(i) &&
3316 (cursor_x < -xposition + get_column_offset(i + 1) ||
3319 highlighted_item = -1;
3320 highlighted_ptr = 0;
3321 highlighted_division = -1;
3322 highlighted_title = i;
3323 list_highlighted = 1;
3328 highlighted_title = -1;
3332 int BC_ListBox::test_expanders()
3334 for(int i = 0; i < expanders.total; i++)
3336 if(expanders.values[i]->button_press_event())
3338 current_operation = EXPAND_DN;
3346 int BC_ListBox::cursor_motion_event()
3348 int redraw = 0, result = 0;
3349 int new_cursor = ARROW_CURSOR;
3351 selection_number = -1;
3354 switch(current_operation)
3357 // Button pressed and slid off button
3358 if(!cursor_inside())
3360 current_operation = BUTTON_DOWN_SELECT;
3368 // int new_w = get_cursor_x() +
3370 // get_column_offset(highlighted_division - 1);
3371 int difference = get_cursor_x() + xposition - drag_cursor_x;
3372 int new_w = drag_column_w + difference;
3374 new_cursor = HSEPARATE_CURSOR;
3378 column_width[highlighted_division - 1] = new_w;
3382 default_column_width[highlighted_division - 1] = new_w;
3385 column_width_boundaries();
3387 // Force update of coords
3388 set_autoplacement(data, 0, 1);
3389 column_resize_event();
3393 update_scrollbars(1);
3400 if(test_drag_scroll(get_cursor_x(), get_cursor_y()))
3402 set_repeat(get_resources()->scroll_repeat);
3405 int old_x1 = MIN(rect_x1, rect_x2);
3406 int old_x2 = MAX(rect_x1, rect_x2);
3407 int old_y1 = MIN(rect_y1, rect_y2);
3408 int old_y2 = MAX(rect_y1, rect_y2);
3410 int new_rect_x2 = get_cursor_x();
3411 int new_rect_y2 = get_cursor_y();
3413 int x1 = MIN(rect_x1, new_rect_x2);
3414 int x2 = MAX(rect_x1, new_rect_x2);
3415 int y1 = MIN(rect_y1, new_rect_y2);
3416 int y2 = MAX(rect_y1, new_rect_y2);
3418 // Adjust rectangle coverage
3426 redraw = select_rectangle(data,
3436 //printf("BC_ListBox::cursor_motion_event %d\n", __LINE__);
3441 rect_x2 = get_cursor_x();
3442 rect_y2 = get_cursor_y();
3447 update_scrollbars(1);
3448 selection_changed();
3449 //printf("BC_ListBox::cursor_motion_event %d\n", __LINE__);
3457 //printf("BC_ListBox::cursor_motion_event %d\n", __LINE__);
3467 int old_highlighted_item = highlighted_item;
3469 if(test_drag_scroll(get_cursor_x(),
3472 set_repeat(get_resources()->scroll_repeat);
3476 highlighted_item = selection_number = get_cursor_item(data,
3482 // Deselect all items and select just the one we're over
3483 if(selection_number >= 0 &&
3487 selection_mode == LISTBOX_SINGLE))
3489 redraw = update_selection(data, selection_number);
3492 if(selection_mode == LISTBOX_MULTIPLE &&
3493 (shift_down() || ctrl_down()))
3494 // Expand multiple selection
3496 // Expand selected region in text mode centered around initial range
3497 if((display_format == LISTBOX_TEXT ||
3498 display_format == LISTBOX_ICON_LIST) &&
3501 // Deselect everything.
3502 set_all_selected(data, 0);
3504 // Select just the items
3505 redraw = expand_selection(0, selection_number);
3508 // Set the one item we're over to the selection value determined in
3509 // button_press_event.
3517 if(highlighted_item != old_highlighted_item)
3521 update_scrollbars(1);
3522 //printf("BC_ListBox::cursor_motion_event %d %d\n", highlighted_item, old_highlighted_item);
3523 selection_changed();
3528 case BUTTON_DOWN_SELECT:
3529 // Went back into button area
3532 current_operation = BUTTON_DN;
3537 // Went into item area
3540 int cursor_x = 0, cursor_y = 0;
3541 translate_coordinates(top_level->event_win,
3543 top_level->cursor_x,
3544 top_level->cursor_y,
3547 int old_highlighted_item = highlighted_item;
3548 highlighted_item = selection_number = get_cursor_item(data,
3553 if(highlighted_item != old_highlighted_item)
3555 update_selection(data, selection_number);
3557 selection_changed();
3564 int redraw_toggles = 0;
3565 for(int i = 0; i < expanders.total && !result; i++)
3567 result = expanders.values[i]->cursor_motion_event(
3572 // Need to redraw items because of the alpha
3580 int cursor_x = get_cursor_x(), cursor_y = get_cursor_y();
3581 if(gui && top_level->event_win == gui->win)
3583 int old_highlighted_title = highlighted_title;
3584 int old_list_highlighted = list_highlighted;
3585 int old_highlighted_item = highlighted_item;
3586 int redraw_titles = 0;
3587 int redraw_border = 0;
3588 int redraw_items = 0;
3589 int redraw_toggles = 0;
3593 // Test if cursor moved over a title division
3594 test_column_divisions(cursor_x, cursor_y, new_cursor);
3596 // Test if cursor moved over a title
3597 if(highlighted_division < 0)
3599 test_column_titles(cursor_x, cursor_y);
3602 // Test if cursor moved over expander
3603 if(highlighted_division < 0 &&
3604 highlighted_title < 0 &&
3605 (display_format == LISTBOX_TEXT ||
3606 display_format == LISTBOX_ICON_LIST))
3608 for(int i = 0; i < expanders.total; i++)
3610 expanders.values[i]->cursor_motion_event(
3613 //printf("BC_ListBox::cursor_motion_event %d\n", redraw_toggles);
3616 // Test if cursor moved over an item
3617 if(highlighted_division < 0 &&
3618 highlighted_title < 0)
3620 highlighted_item = get_cursor_item(data,
3627 // Clear title highlighting if moved over division
3628 if(old_highlighted_title != highlighted_title)
3633 // Highlight list border
3634 if(old_list_highlighted != list_highlighted)
3639 // Moved out of item area
3640 if(old_highlighted_item != highlighted_item)
3645 //printf("BC_ListBox::cursor_motion_event 1 %d\n", highlighted_item);
3647 // Change cursor to title division adjustment
3648 reset_cursor(new_cursor);
3675 if(!result && list_highlighted)
3677 list_highlighted = 0;
3678 highlighted_item = -1;
3679 highlighted_ptr = 0;
3680 highlighted_title = -1;
3681 highlighted_division = -1;
3693 int BC_ListBox::drag_start_event()
3695 switch(current_operation)
3699 gui->is_event_win() &&
3702 BC_ListBoxItem *item_return = 0;
3703 selection_number = get_cursor_item(data,
3704 top_level->cursor_x,
3705 top_level->cursor_y,
3708 if(selection_number >= 0)
3711 if (item_return->icon_vframe)
3713 drag_popup = new BC_DragWindow(this,
3714 item_return->icon_vframe /*,
3715 get_abs_cursor_x(0) - item_return->icon_vframe->get_w() / 2,
3716 get_abs_cursor_y(0) - item_return->icon_vframe->get_h() / 2 */);
3719 // this probably works not!
3720 if (item_return->icon)
3722 drag_popup = new BC_DragWindow(this,
3723 item_return->icon /*,
3724 get_abs_cursor_x(0) - item_return->icon->get_w() / 2,
3725 get_abs_cursor_y(0) - item_return->icon->get_h() / 2 */);
3729 drag_popup = new BC_DragWindow(this,
3730 drag_icon_vframe /*,
3731 get_abs_cursor_x(0) - drag_icon_vframe->get_w() / 2,
3732 get_abs_cursor_y(0) - drag_icon_vframe->get_h() / 2 */);
3735 current_operation = DRAG_ITEM;
3742 if(gui && gui->is_event_win() && allow_drag_column)
3744 drag_popup = new BC_DragWindow(this,
3745 drag_column_icon_vframe /*,
3746 get_abs_cursor_x(0) - drag_column_icon_vframe->get_w() / 2,
3747 get_abs_cursor_y(0) - drag_column_icon_vframe->get_h() / 2 */);
3748 dragged_title = highlighted_title;
3749 current_operation = COLUMN_DRAG;
3759 int BC_ListBox::drag_motion_event()
3761 //printf("BC_ListBox::drag_motion_event 1 %d\n", current_operation);
3762 switch(current_operation)
3767 int new_highlighted_item = -1;
3768 BC_ListBoxItem *new_highlighted_ptr = 0;
3769 new_highlighted_item = get_cursor_item(data,
3770 top_level->cursor_x, top_level->cursor_y,
3771 &new_highlighted_ptr);
3773 if(new_highlighted_item != highlighted_item)
3778 // Always update highlighted value for drag_stop
3779 highlighted_item = new_highlighted_item;
3780 highlighted_ptr = new_highlighted_ptr;
3781 //printf("BC_ListBox::drag_motion_event 1 %p\n", highlighted_ptr);
3786 update_scrollbars(1);
3789 return drag_popup->cursor_motion_event();
3795 int old_highlighted_title = highlighted_title;
3796 test_column_titles(get_cursor_x(), get_cursor_y());
3797 if(old_highlighted_title != highlighted_title)
3801 return drag_popup->cursor_motion_event();
3808 int BC_ListBox::drag_stop_event()
3810 switch(current_operation)
3813 // Inside window boundary
3814 if(top_level->cursor_x > 0 &&
3815 top_level->cursor_x < gui->get_w() - drag_popup->get_w() / 2 &&
3816 top_level->cursor_y > 0 &&
3817 top_level->cursor_y < gui->get_h() - drag_popup->get_h() / 2)
3822 if(display_format == LISTBOX_ICONS)
3824 reposition_item(data,
3826 top_level->cursor_x +
3827 drag_popup->get_offset_x() -
3831 top_level->cursor_y +
3832 drag_popup->get_offset_y() -
3842 int destination = highlighted_item = item_to_index(data,
3844 //printf("BC_ListBox::drag_stop_event 1 %p %d\n", highlighted_ptr, destination);
3846 // Move selected items from data to temporary
3847 ArrayList<BC_ListBoxItem*> *src_items =
3848 new ArrayList<BC_ListBoxItem*>[columns];
3850 move_selection(src_items, data);
3852 // Insert items from temporary to data
3858 delete [] src_items;
3859 set_autoplacement(data, 0, 1);
3866 drag_popup->drag_failure_event();
3871 current_operation = NO_OPERATION;
3877 if(dragged_title != highlighted_title)
3879 if(highlighted_title >= 0)
3881 if(!move_column_event()) draw_titles(1);
3884 drag_popup->drag_failure_event();
3886 current_operation = NO_OPERATION;
3896 BC_DragWindow* BC_ListBox::get_drag_popup()
3901 int BC_ListBox::translation_event()
3905 int new_x = gui->get_x() +
3906 (top_level->last_translate_x - top_level->prev_x -
3907 BC_DisplayInfo::get_left_border());
3908 int new_y = gui->get_y() +
3909 (top_level->last_translate_y - top_level->prev_y -
3910 BC_DisplayInfo::get_top_border());
3912 gui->reposition_window(new_x, new_y);
3918 int BC_ListBox::reposition_window(int x, int y, int w, int h, int flush)
3922 if(w != -1) popup_w = w;
3923 if(h != -1) popup_h = h;
3924 //printf("BC_ListBox::reposition_window %d %d\n", popup_w, popup_h);
3928 if(w != -1) popup_w = w;
3929 if(h != -1) popup_h = h;
3931 xscrollbar->reposition_window(get_xscroll_x(),
3933 get_xscroll_width());
3935 yscrollbar->reposition_window(get_yscroll_x(),
3937 get_yscroll_height());
3942 BC_WindowBase::reposition_window(x, y, w, h);
3948 int BC_ListBox::deactivate()
3950 // printf("BC_ListBox::deactivate %d this=%p gui=%p active=%d\n",
3960 //printf("BC_ListBox::deactivate %d this=%p gui=%p\n", __LINE__, this, gui);
3969 highlighted_item = -1;
3970 highlighted_ptr = 0;
3975 //printf("BC_ListBox::deactivate %d this=%p\n", __LINE__, this);
3977 top_level->active_subwindow = 0;
3983 int BC_ListBox::activate(int take_focus)
3985 //printf("BC_ListBox::activate %d %p\n", __LINE__, this);
3990 top_level->active_subwindow = this;
3994 button_releases = 0;
3996 // Test for existence of GUI in case this was previously called without
3997 // take_focus & again with take_focus
3998 if(is_popup && !gui)
4003 y = get_y() + get_h();
4004 if(justify == LISTBOX_RIGHT)
4006 x = get_x() - popup_w + get_w();
4014 XTranslateCoordinates(top_level->display,
4023 if(new_x < 0) new_x = 0;
4024 if(new_y + popup_h > top_level->get_root_h(0))
4025 new_y -= get_h() + popup_h;
4027 add_subwindow(gui = new BC_Popup(this,
4035 // Avoid top going out of screen
4038 //printf("BC_ListBox::activate %d this=%p %p\n", __LINE__, this, gui->win);
4040 gui->show_window(1);
4042 //printf("BC_ListBox::activate %d %p\n", __LINE__, this);
4048 int BC_ListBox::keypress_event()
4050 if(!active) return 0;
4052 //printf("BC_ListBox::keypress_event %d\n", __LINE__);
4054 int result = 0, redraw = 0;
4055 int view_items = last_in_view - first_in_view + 1;
4056 if( view_items <= 0 ) view_items = view_h / get_text_height(MEDIUMFONT);
4057 int new_item = -1, new_selection = -1;
4059 switch(top_level->get_keypress())
4064 top_level->deactivate();
4066 // If user is manipulating popup with keyboard, don't pass on event.
4069 //printf("BC_ListBox::keypress_event %d %p\n", __LINE__, get_selection(0, 0));
4070 if(top_level->get_keypress() == RETURN)
4072 //printf("BC_ListBox::keypress_event %d %p\n", __LINE__, get_selection(0, 0));
4080 new_selection = new_item = select_previous(0);
4082 //printf("BC_ListBox::keypress_event 1 %d\n", new_item);
4085 center_selection(new_item);
4093 new_selection = new_item = select_next(0);
4097 center_selection(new_item);
4105 new_selection = new_item = select_previous(view_items - 1);
4109 center_selection(new_item);
4117 new_selection = new_item = select_next(view_items - 1);
4121 center_selection(new_item);
4149 int query_len = strlen(query);
4150 if( query_len < (int)sizeof(query)-1 &&
4151 top_level->get_keypress() > 30 &&
4152 top_level->get_keypress() < 127)
4154 query[query_len++] = top_level->get_keypress();
4155 query[query_len] = 0;
4156 new_selection = query_list();
4159 if(top_level->get_keypress() == BACKSPACE)
4161 if(query_len > 0) query[--query_len] = 0;
4162 new_selection = query_list();
4175 update_scrollbars(1);
4178 //printf("BC_ListBox::keypress_event %d new_selection=%d\n", __LINE__, new_selection);
4179 if(new_selection >= 0 && !is_suggestions)
4181 //printf("BC_ListBox::keypress_event %d\n", __LINE__);
4182 selection_changed();
4183 //printf("BC_ListBox::keypress_event %d\n", __LINE__);
4190 BC_Pixmap* BC_ListBox::get_bg_surface()
4196 void BC_ListBox::draw_background()
4198 if( !bg_draw ) return;
4200 // White background pixmap
4201 set_color(top_level->get_resources()->listbox_inactive);
4202 draw_box(0, 0, bg_surface->get_w(), bg_surface->get_h(), bg_surface);
4204 // Optional heroine pixmap
4206 bg_surface->draw_pixmap(bg_pixmap,
4207 bg_surface->get_w() - top_level->get_resources()->listbox_bg->get_w(),
4211 void BC_ListBox::clear_listbox(int x, int y, int w, int h)
4213 gui->draw_pixmap(bg_surface, x, y, w, h, x, y - title_h);
4216 void BC_ListBox::update_format(int display_format, int redraw)
4218 this->display_format = display_format;
4219 if( redraw && gui ) draw_items(1, 1);
4222 int BC_ListBox::get_format()
4224 return display_format;
4229 int BC_ListBox::draw_items(int flush, int draw_bg)
4233 BC_Resources *resources = get_resources();
4235 //dump(data, columns);
4237 // Calculate items width
4238 calculate_item_coords();
4241 // Create and destroy scrollbars as needed
4246 if( bg_draw ) this->bg_draw = 1;
4252 if(display_format == LISTBOX_ICONS)
4254 clear_listbox(2, 2 + title_h, view_w, view_h);
4256 set_font(MEDIUMFONT);
4257 for(int i = 0; i < data[master_column].size(); i++)
4259 BC_ListBoxItem *item = data[master_column].get(i);
4260 if(get_item_x(item) >= -get_item_w(item) &&
4261 get_item_x(item) < view_w &&
4262 get_item_y(item) >= -get_item_h(item) + title_h &&
4263 get_item_y(item) < view_h + title_h)
4265 item->set_in_view(1);
4266 if( first_in_view < 0 ) first_in_view = i;
4268 int item_color = get_item_highlight(data, 0, i);
4269 int icon_x, icon_y, icon_w, icon_h;
4270 int text_x, text_y, text_w, text_h;
4273 get_icon_mask(item, icon_x, icon_y, icon_w, icon_h);
4274 get_text_mask(item, text_x, text_y, text_w, text_h);
4277 if(item_color != resources->listbox_inactive)
4279 gui->set_color(BLACK);
4280 gui->draw_rectangle(icon_x, icon_y, icon_w, icon_h);
4281 gui->set_color(item_color);
4282 gui->draw_box(icon_x + 1, icon_y + 1, icon_w - 2, icon_h - 2);
4283 gui->set_color(BLACK);
4284 gui->draw_rectangle(text_x, text_y, text_w, text_h);
4285 gui->set_color(item_color);
4286 gui->draw_box(text_x + 1, text_y + 1, text_w - 2, text_h - 2);
4288 if(icon_position == ICON_LEFT)
4289 gui->draw_box(text_x - 1, text_y + 1, 2, text_h - 2);
4291 if(icon_position == ICON_TOP)
4292 gui->draw_line(text_x + 1, text_y, text_x + icon_w - 2, text_y);
4293 if(text_x + text_w < icon_x + icon_w)
4295 gui->set_color(BLACK);
4296 gui->draw_line(text_x + text_w,
4304 gui->set_color(get_item_color(data, 0, i));
4306 gui->pixmap->draw_pixmap(item->icon,
4307 icon_x + ICON_MARGIN,
4308 icon_y + ICON_MARGIN);
4311 gui->draw_text(text_x + ICON_MARGIN,
4312 text_y + ICON_MARGIN + get_baseline(item),
4316 item->set_in_view(0);
4322 // Draw one column at a time so text overruns don't go into the next column
4323 // clear column backgrounds
4324 int current_toggle = 0;
4325 for(int j = 0; j < columns; j++)
4327 clear_listbox(LISTBOX_BORDER + get_column_offset(j) - xposition,
4328 LISTBOX_BORDER + title_h,
4329 get_column_width(j, 1),
4331 // Draw rows in the column recursively
4332 draw_text_recursive(data, j, 0, ¤t_toggle);
4335 // Delete excess expanders
4336 while(expanders.total > current_toggle)
4338 expanders.remove_object();
4342 // draw user images if available
4344 // Draw titles on top of rows for superposition effect
4347 // Clear garbage from bottom right corner
4348 if(xscrollbar && yscrollbar && is_popup)
4350 gui->draw_top_background(parent_window,
4351 popup_w - get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w(),
4352 popup_h - get_resources()->hscroll_data[SCROLL_HANDLE_UP]->get_h(),
4353 get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w(),
4354 get_resources()->hscroll_data[SCROLL_HANDLE_UP]->get_h());
4361 if(current_operation == SELECT_RECT)
4371 void BC_ListBox::draw_text_recursive(ArrayList<BC_ListBoxItem*> *data,
4374 int *current_toggle)
4379 BC_Resources *resources = get_resources();
4381 set_font(MEDIUMFONT);
4384 // Search for a branch and make room for toggle if there is one
4387 for(int i = 0; i < data[column].size(); i++)
4389 if(data[column].get(i)->get_sublist())
4391 subindent = BC_WindowBase::get_resources()->listbox_expand[0]->get_w();
4397 row_height = row_ascent = row_descent = 0;
4398 for(int i = 0; i < data[column].total; i++)
4400 BC_ListBoxItem *item = data[column].values[i];
4401 int ht = get_text_h(item);
4402 if( ht > row_height ) row_height = ht;
4403 int bl = get_baseline(item);
4404 if( bl > row_ascent ) row_ascent = bl;
4406 if( dt > row_descent ) row_ascent = bl;
4409 for(int i = 0; i < data[column].size(); i++)
4412 BC_ListBoxItem *item = data[column].values[i];
4413 BC_ListBoxItem *first_item = data[master_column].values[i];
4415 if(get_item_y(item) >= -get_item_h(item) + title_h &&
4416 get_item_y(item) < view_h + title_h)
4418 int row_color = get_item_highlight(data, 0, i);
4419 int x, y, w, h, column_width;
4421 get_text_mask(item, x, y, w, h);
4422 column_width = get_column_width(column, 1);
4423 if(x + column_width > view_w + LISTBOX_BORDER * 2)
4424 column_width = view_w + LISTBOX_BORDER * 2 - x;
4426 if(row_color != resources->listbox_inactive)
4428 gui->set_color(row_color);
4429 gui->draw_box(x, y, column_width, h);
4430 gui->set_color(BLACK);
4431 int yy = y, xx = x + column_width-1;
4432 gui->draw_line(x, yy, xx, yy);
4433 yy = y + row_height;
4434 gui->draw_line(x, yy, xx, yy);
4437 gui->set_color(get_item_color(data, column, i));
4440 if(column == 0 && display_format == LISTBOX_ICON_LIST)
4444 gui->pixmap->draw_pixmap(item->icon,
4447 x += item->icon->get_w() + ICON_MARGIN;
4452 // Indent only applies to first column
4454 x + LISTBOX_BORDER + LISTBOX_MARGIN +
4455 (column == 0 ? indent + subindent : 0),
4456 y + get_baseline(item), item->text);
4457 item->set_in_view(1);
4459 if( first_in_view < 0 ) first_in_view = i;
4465 item->get_sublist() &&
4466 item->get_columns())
4468 // Create new expander
4469 if(*current_toggle >= expanders.total)
4471 BC_ListBoxToggle *toggle =
4472 new BC_ListBoxToggle(this,
4474 x + LISTBOX_BORDER + LISTBOX_MARGIN + indent,
4477 expanders.append(toggle);
4480 // Reposition existing expander
4482 BC_ListBoxToggle *toggle = expanders.values[*current_toggle];
4483 //printf("BC_ListBox::draw_text_recursive 1 %d\n", *current_toggle);
4484 toggle->update(item,
4485 x + LISTBOX_BORDER + LISTBOX_MARGIN + indent,
4489 (*current_toggle)++;
4496 item->set_in_view(0);
4498 // Descend into sublist
4499 if(first_item->get_expand())
4501 draw_text_recursive(first_item->get_sublist(),
4503 indent + LISTBOX_INDENT,
4512 int BC_ListBox::draw_border(int flash)
4514 BC_Resources *resources = top_level->get_resources();
4515 gui->draw_3d_border(0,
4517 view_w + LISTBOX_BORDER * 2,
4518 view_h + title_h + LISTBOX_BORDER * 2,
4519 resources->listbox_border1,
4521 resources->listbox_border2_hi :
4522 resources->listbox_border2,
4524 resources->listbox_border3_hi :
4525 resources->listbox_border3,
4526 resources->listbox_border4);
4536 void BC_ListBox::draw_title(int number)
4538 // Column title background
4539 int image_number = 0;
4540 if(number == highlighted_title)
4543 if(current_operation == COLUMN_DN)
4547 int column_offset = get_column_offset(number) - xposition + LISTBOX_BORDER;
4548 int column_width = get_column_width(number, 1);
4549 gui->draw_3segmenth(get_column_offset(number) - xposition + LISTBOX_BORDER,
4551 get_column_width(number, 1) + get_resources()->listbox_title_overlap,
4552 column_bg[image_number]);
4554 // Column title sort order
4555 if(number == sort_column)
4558 if(sort_order == SORT_ASCENDING)
4559 src = column_sort_dn;
4561 src = column_sort_up;
4563 int x = column_offset +
4566 if(x > items_w) x = items_w;
4567 x -= 5 + src->get_w();
4568 gui->draw_pixmap(src,
4570 title_h / 2 - src->get_h() / 2 + LISTBOX_BORDER);
4574 int x = -xposition +
4575 get_column_offset(number) +
4578 x += get_resources()->listbox_title_margin;
4580 gui->set_color(get_resources()->listbox_title_color);
4582 LISTBOX_MARGIN + LISTBOX_BORDER + get_text_ascent(MEDIUMFONT),
4583 _(column_titles[number]));
4586 int BC_ListBox::draw_titles(int flash)
4589 (display_format == LISTBOX_TEXT ||
4590 display_format == LISTBOX_ICON_LIST))
4592 //printf("BC_ListBox::draw_titles 1 %d\n", highlighted_title);
4593 for(int i = 0; i < columns; i++)
4595 if(i != highlighted_title)
4599 if(highlighted_title >= 0) draw_title(highlighted_title);
4610 void BC_ListBox::draw_toggles(int flash)
4612 for(int i = 0; i < expanders.total; i++)
4613 expanders.values[i]->draw(0);
4615 //printf("BC_ListBox::draw_toggles 1 %d\n", flash);
4616 if(flash && expanders.total)
4623 int BC_ListBox::draw_rectangle(int flash)
4625 int x1 = MIN(rect_x1, rect_x2);
4626 int x2 = MAX(rect_x1, rect_x2);
4627 int y1 = MIN(rect_y1, rect_y2);
4628 int y2 = MAX(rect_y1, rect_y2);
4630 if(x1 == x2 || y1 == y2) return 0;
4633 gui->set_color(WHITE);
4634 gui->draw_rectangle(x1, y1, x2 - x1, y2 - y1);
4645 void BC_ListBox::dump(ArrayList<BC_ListBoxItem*> *data,
4652 printf("BC_ListBox::dump 1\n");
4655 for(int i = 0; i < data[master_column].total; i++)
4657 for(int k = 0; k < indent; k++)
4659 for(int j = 0; j < columns; j++)
4661 BC_ListBoxItem *item = data[j].values[i];
4662 printf("%d,%d,%d=%s ",
4665 item->autoplace_text,
4670 if(data[master_column].values[i]->get_sublist())
4672 dump(data[master_column].values[i]->get_sublist(),
4673 data[master_column].values[i]->get_columns(),