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);
739 current_icon_y = item->icon_y + get_item_h(item);
740 if(current_icon_y > *next_icon_y)
741 *next_icon_y = current_icon_y;
748 void BC_ListBox::calculate_item_coords_recursive(
749 ArrayList<BC_ListBoxItem*> *data,
756 // get maximum height of an icon
757 row_height = get_text_height(MEDIUMFONT);
758 if(temp_display_format == LISTBOX_ICON_LIST)
760 for(int i = 0; i < data[0].size(); i++)
762 if(data[0].get(i)->icon)
764 if(data[0].get(i)->icon->get_h() > row_height)
765 row_height = data[0].get(i)->icon->get_h();
771 // Set up items which need autoplacement.
772 // Should fill icons down and then across
773 for(int i = 0; i < data[0].size(); i++)
775 // Don't increase y unless the row requires autoplacing.
776 int total_autoplaced_columns = 0;
778 // Set up icons in first column
781 BC_ListBoxItem *item = data[master_column].get(i);
782 if(item->autoplace_icon)
784 // 1 column only if icons are used
785 display_format = LISTBOX_ICONS;
788 if(*next_icon_y + get_item_h(item) >= get_h() &&
791 *icon_x = *next_icon_x;
795 if(*icon_x + get_item_w(item) > *next_icon_x)
796 *next_icon_x = *icon_x + get_item_w(item);
799 item->set_icon_x(*icon_x);
800 item->set_icon_y(*next_icon_y);
802 *next_icon_y += get_item_h(item);
810 row_ascent = row_descent = 0;
811 // row_height still holds icon max height
812 for(int j = 0; j < columns; j++)
814 BC_ListBoxItem *item = data[j].get(i);
815 if(item->autoplace_text)
817 display_format = LISTBOX_TEXT;
818 item->set_text_x(next_text_x);
819 item->set_text_y(*next_text_y);
820 int ht = get_text_h(item);
821 if( ht > row_height ) row_height = ht;
822 int bl = get_baseline(item);
823 if( bl > row_ascent ) row_ascent = bl;
825 if( dt > row_descent ) row_ascent = bl;
827 // printf("BC_ListBox::calculate_item_coords_recursive %p %d %d %d %d %s \n",
828 // item->get_sublist(),
829 // item->get_columns(),
830 // item->get_expand(),
833 // item->get_text());
834 // Increment position of next column
837 next_text_x += (column_width ?
839 default_column_width[j]);
842 // Set last column width based on text width
844 int new_w = get_item_w(item);
846 int *previous_w = (column_width ?
848 &default_column_width[j]);
849 if(new_w > *previous_w)
851 //printf("BC_ListBox::calculate_item_coords_recursive 1 %d\n", new_w);
853 total_autoplaced_columns++;
857 // Increase the text vertical position
858 if(total_autoplaced_columns)
860 display_format = LISTBOX_TEXT;
861 *next_text_y += row_height;
865 BC_ListBoxItem *item = data[master_column].values[i];
866 if(item->get_sublist() &&
867 item->get_columns() &&
870 calculate_item_coords_recursive(
881 void BC_ListBox::set_is_suggestions(int value)
883 this->is_suggestions = value;
886 void BC_ListBox::set_use_button(int value)
888 this->use_button = value;
891 void BC_ListBox::set_justify(int value)
893 this->justify = value;
896 void BC_ListBox::set_allow_drag_column(int value)
898 this->allow_drag_column = value;
901 void BC_ListBox::set_process_drag(int value)
903 this->process_drag = value;
906 void BC_ListBox::set_master_column(int value, int redraw)
908 this->master_column = value;
915 void BC_ListBox::set_search_column(int value)
917 this->search_column = value;
920 int BC_ListBox::get_sort_column()
925 void BC_ListBox::set_sort_column(int value, int redraw)
934 int BC_ListBox::get_sort_order()
939 void BC_ListBox::set_sort_order(int value, int redraw)
952 int BC_ListBox::get_display_mode()
954 return display_format;
957 int BC_ListBox::get_yposition()
962 int BC_ListBox::get_xposition()
967 int BC_ListBox::get_highlighted_item()
969 return highlighted_item;
973 int BC_ListBox::get_item_x(BC_ListBoxItem *item)
975 if(display_format == LISTBOX_TEXT)
977 return item->text_x - xposition + 2;
980 if(display_format == LISTBOX_ICON_LIST)
982 return item->text_x - xposition + 2;
986 return item->icon_x - xposition + 2;
990 int BC_ListBox::get_item_y(BC_ListBoxItem *item)
993 if(display_format == LISTBOX_TEXT)
995 result = item->text_y - yposition + title_h + 2;
998 if(display_format == LISTBOX_ICON_LIST)
1000 result = item->text_y - yposition + title_h + 2;
1004 result = item->icon_y - yposition + title_h + 2;
1010 int BC_ListBox::get_item_w(BC_ListBoxItem *item)
1012 if(display_format == LISTBOX_ICONS)
1015 get_icon_mask(item, x, y, w, h);
1017 get_text_mask(item, x, y, w, h);
1020 return icon_position == ICON_LEFT ? icon_w + text_w :
1021 icon_w > text_w ? icon_w : text_w;
1023 return get_text_w(item) + 2 * LISTBOX_MARGIN;
1026 int BC_ListBox::get_item_h(BC_ListBoxItem *item)
1028 if(display_format == LISTBOX_ICONS)
1031 get_icon_mask(item, x, y, w, h);
1033 get_text_mask(item, x, y, w, h);
1036 return icon_position != ICON_LEFT ? icon_h + text_h :
1037 icon_h > text_h ? icon_h : text_h;
1039 return get_text_h(item);
1043 int BC_ListBox::get_icon_w(BC_ListBoxItem *item)
1045 return item->get_icon_w();
1048 int BC_ListBox::get_icon_h(BC_ListBoxItem *item)
1050 return item->get_icon_h();
1053 int BC_ListBox::get_text_w(BC_ListBoxItem *item)
1055 int w = item->get_text_w();
1056 if( w < 0 ) item->set_text_w(w = get_text_width(MEDIUMFONT, item->get_text()));
1060 int BC_ListBox::get_text_h(BC_ListBoxItem *item)
1062 int h = item->get_text_h();
1063 if( h < 0 ) item->set_text_h(h = get_text_height(MEDIUMFONT));
1067 int BC_ListBox::get_baseline(BC_ListBoxItem *item)
1069 int b = item->get_baseline();
1070 if( b < 0 ) item->set_baseline(b = get_text_ascent(MEDIUMFONT));
1074 int BC_ListBox::get_items_width()
1078 if(display_format == LISTBOX_ICONS)
1080 for(int i = 0; i < columns; i++)
1082 for(int j = 0; j < data[i].total; j++)
1085 BC_ListBoxItem *item = data[i].values[j];
1088 get_icon_mask(item, x, y, w, h);
1089 if(x1 + w > widest) widest = x1 + w;
1091 if(display_format == LISTBOX_ICONS && icon_position == ICON_LEFT)
1094 get_text_mask(item, x, y, w, h);
1095 if(x1 + w > widest) widest = x1 + w;
1100 if(display_format == LISTBOX_TEXT)
1102 return get_column_offset(columns);
1106 return get_column_offset(columns);
1111 int BC_ListBox::get_items_height(ArrayList<BC_ListBoxItem*> *data,
1128 for(int j = 0; j < (data ? data[master_column].total : 0); j++)
1131 BC_ListBoxItem *item = data[master_column].values[j];
1133 if( display_format == LISTBOX_ICONS ||
1134 display_format == LISTBOX_ICON_LIST ) {
1135 get_icon_mask(item, x, y, w, h);
1136 if(y + h + yposition > highest) highest = y + h + yposition;
1138 get_text_mask(item, x, y, w, h);
1139 if(y + h + yposition > highest) highest = y + h + yposition;
1143 get_text_mask(item, x, y, w, h);
1145 // Descend into sublist
1146 if(item->get_sublist() &&
1149 get_items_height(item->get_sublist(),
1150 item->get_columns(),
1156 if( display_format == LISTBOX_TEXT && top_level )
1158 highest = LISTBOX_MARGIN + *result;
1165 int BC_ListBox::set_yposition(int position, int draw_items)
1167 this->yposition = position;
1170 this->draw_items(1);
1175 int BC_ListBox::set_xposition(int position)
1177 this->xposition = position;
1182 void BC_ListBox::expand_item(BC_ListBoxItem *item, int expand)
1186 item->expand = expand;
1187 // Collapse sublists if this is collapsed to make it easier to calculate
1189 if(item->get_sublist())
1190 collapse_recursive(item->get_sublist(), master_column);
1193 // Set everything for autoplacement
1195 set_autoplacement(data, 0, 1);
1201 void BC_ListBox::collapse_recursive(ArrayList<BC_ListBoxItem*> *data,
1204 for(int i = 0; i < data[master_column].total; i++)
1206 BC_ListBoxItem *item = data[master_column].values[i];
1207 if(item->get_sublist() && item->expand)
1210 collapse_recursive(item->get_sublist(), master_column);
1215 void BC_ListBox::set_autoplacement(ArrayList<BC_ListBoxItem*> *data,
1219 for(int i = 0; i < data[0].total; i++)
1221 for(int j = 0; j < columns; j++)
1223 if(do_icons) data[j].values[i]->autoplace_icon = 1;
1224 if(do_text) data[j].values[i]->autoplace_text = 1;
1227 BC_ListBoxItem *item = data[master_column].values[i];
1228 if(item->get_sublist())
1230 set_autoplacement(item->get_sublist(), do_icons, do_text);
1237 int BC_ListBox::get_yscroll_x()
1240 return popup_w - get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w();
1244 get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w();
1247 int BC_ListBox::get_yscroll_y()
1255 int BC_ListBox::get_yscroll_height()
1257 return popup_h - (need_xscroll ?
1258 get_resources()->hscroll_data[SCROLL_HANDLE_UP]->get_h() :
1262 int BC_ListBox::get_xscroll_x()
1270 int BC_ListBox::get_xscroll_y()
1274 get_resources()->hscroll_data[SCROLL_HANDLE_UP]->get_h();
1278 get_resources()->hscroll_data[SCROLL_HANDLE_UP]->get_h();
1281 int BC_ListBox::get_xscroll_width()
1283 return popup_w - (need_yscroll ?
1284 get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w() :
1288 int BC_ListBox::get_column_offset(int column)
1294 column_width[--column] :
1295 default_column_width[--column];
1300 void BC_ListBox::column_width_boundaries()
1303 for(int i = 0; i < columns; i++) {
1304 if(column_width[i] < MIN_COLUMN_WIDTH)
1305 column_width[i] = MIN_COLUMN_WIDTH;
1309 for(int i = 0; i < columns; i++) {
1310 if(default_column_width[i] < MIN_COLUMN_WIDTH)
1311 default_column_width[i] = MIN_COLUMN_WIDTH;
1316 int BC_ListBox::get_column_width(int column, int clamp_right)
1318 if(column < columns - 1 || !clamp_right)
1319 return column_width ? column_width[column] : default_column_width[column];
1320 return popup_w + xposition - get_column_offset(column);
1323 int BC_ListBox::get_icon_mask(BC_ListBoxItem *item,
1324 int &x, int &y, int &w, int &h)
1326 if( display_format == LISTBOX_ICONS ) {
1327 x = get_item_x(item);
1328 y = get_item_y(item);
1329 w = get_icon_w(item) + ICON_MARGIN * 2;
1330 h = get_icon_h(item) + ICON_MARGIN * 2;
1337 int BC_ListBox::get_text_mask(BC_ListBoxItem *item,
1338 int &x, int &y, int &w, int &h)
1340 x = get_item_x(item);
1341 y = get_item_y(item);
1343 if(display_format == LISTBOX_ICONS) {
1344 if(icon_position == ICON_LEFT) {
1345 x += get_icon_w(item) + ICON_MARGIN * 2;
1346 y += get_icon_h(item) - get_text_h(item);
1349 y += get_icon_h(item) + ICON_MARGIN;
1352 w = get_text_w(item) + ICON_MARGIN * 2;
1353 h = get_text_h(item) + ICON_MARGIN * 2;
1356 if(display_format == LISTBOX_TEXT) {
1357 w = get_text_w(item) + LISTBOX_MARGIN * 2;
1358 h = get_text_h(item);
1362 w = get_text_width(MEDIUMFONT, item->text) + LISTBOX_MARGIN * 2;
1368 int BC_ListBox::get_item_highlight(ArrayList<BC_ListBoxItem*> *data,
1372 BC_Resources *resources = get_resources();
1373 if(data[column].values[item]->selected)
1374 return resources->listbox_selected;
1375 else if(highlighted_item >= 0 &&
1376 highlighted_ptr == data[master_column].values[item])
1377 return resources->listbox_highlighted;
1378 return resources->listbox_inactive;
1381 int BC_ListBox::get_item_color(ArrayList<BC_ListBoxItem*> *data,
1385 int color = data[column].values[item]->color;
1386 if( color == -1 ) color = get_resources()->listbox_text;
1387 if( get_item_highlight(data, column, item) == color )
1392 int BC_ListBox::get_from_column()
1394 return dragged_title;
1397 int BC_ListBox::get_to_column()
1399 return highlighted_title;
1403 BC_ListBoxItem* BC_ListBox::get_selection(int column,
1404 int selection_number)
1406 return get_selection_recursive(data,
1411 BC_ListBoxItem* BC_ListBox::get_selection_recursive(
1412 ArrayList<BC_ListBoxItem*> *data,
1414 int selection_number)
1418 for(int i = 0; i < data[master_column].total; i++)
1420 BC_ListBoxItem *item = data[master_column].values[i];
1423 //printf("BC_ListBox::get_selection_recursive %d\n", __LINE__);
1425 if(selection_number < 0)
1428 return data[column].values[i];
1432 if(item->get_sublist())
1434 BC_ListBoxItem *result = get_selection_recursive(item->get_sublist(),
1437 if(result) return result;
1445 int BC_ListBox::get_selection_number(int column,
1446 int selection_number)
1448 return get_selection_number_recursive(data,
1453 int BC_ListBox::get_selection_number_recursive(
1454 ArrayList<BC_ListBoxItem*> *data,
1456 int selection_number,
1461 if(!counter) counter = &temp;
1463 for(int i = 0; i < data[master_column].total; i++)
1466 BC_ListBoxItem *item = data[master_column].values[i];
1470 if(selection_number < 0)
1475 if(item->get_sublist())
1477 int result = get_selection_number_recursive(
1478 item->get_sublist(),
1482 if(result >= 0) return result;
1489 int BC_ListBox::set_selection_mode(int mode)
1491 this->selection_mode = mode;
1495 void BC_ListBox::delete_columns()
1499 for(int i = 0; i < columns; i++)
1501 delete [] column_titles[i];
1503 delete [] column_titles;
1506 if(column_width) delete [] column_width;
1512 // Need to copy titles so EDL can change
1513 void BC_ListBox::set_columns(const char **column_titles,
1517 if((!column_titles && column_width) ||
1518 (column_titles && !column_width))
1520 printf("BC_ListBox::set_columns either column_titles or column_width == NULL but not both.\n");
1529 this->column_titles = new char*[columns];
1530 for(int i = 0; i < columns; i++)
1532 this->column_titles[i] = new char[strlen(column_titles[i]) + 1];
1533 strcpy(this->column_titles[i], column_titles[i]);
1539 this->column_width = new int[columns];
1540 for(int i = 0; i < columns; i++)
1542 this->column_width[i] = column_width[i];
1546 this->columns = columns;
1551 int BC_ListBox::update(ArrayList<BC_ListBoxItem*> *data,
1552 const char **column_titles,
1557 int highlighted_number,
1558 int recalc_positions,
1561 set_columns(column_titles,
1567 this->yposition = yposition;
1568 this->xposition = xposition;
1569 this->highlighted_item = highlighted_number;
1570 this->highlighted_ptr = index_to_item(data, highlighted_number, 0);
1572 if(recalc_positions)
1573 set_autoplacement(data, 1, 1);
1575 init_column_width();
1580 update_scrollbars(1);
1586 void BC_ListBox::center_selection()
1588 int selection = get_selection_number(0, 0);
1590 calculate_item_coords();
1591 center_selection(selection);
1597 update_scrollbars(0);
1598 gui->show_window(1);
1602 void BC_ListBox::move_vertical(int pixels)
1606 void BC_ListBox::move_horizontal(int pixels)
1610 int BC_ListBox::select_previous(int skip,
1611 BC_ListBoxItem *selected_item,
1613 ArrayList<BC_ListBoxItem*> *data,
1619 selected_item = get_selection(0, 0);
1631 got_second = &temp3;
1635 // Scan backwards to item pointer. Then count visible items to get
1636 // destination. No wraparound.
1639 for(int i = data[master_column].total - 1; i >= 0; i--)
1641 BC_ListBoxItem *current_item = data[master_column].values[i];
1642 if(current_item->get_sublist() &&
1643 current_item->get_expand())
1645 int result = select_previous(skip,
1648 current_item->get_sublist(),
1660 if((*counter) >= skip)
1662 for(int j = 0; j < columns; j++)
1663 data[j].values[i]->selected = 1;
1665 return item_to_index(this->data, current_item);
1670 if(current_item->selected)
1672 for(int j = 0; j < columns; j++)
1673 data[j].values[i]->selected = 0;
1680 // Hit top of top level without finding a selected item.
1683 // Select first item in top level and quit
1684 BC_ListBoxItem *current_item;
1686 current_item = data[master_column].values[0];
1688 for(int j = 0; j < columns; j++)
1689 data[j].values[0]->selected = 1;
1691 return item_to_index(this->data, current_item);
1693 }while(top_level && data[master_column].total);
1697 int BC_ListBox::select_next(int skip,
1698 BC_ListBoxItem *selected_item,
1700 ArrayList<BC_ListBoxItem*> *data,
1706 selected_item = get_selection(0, 0);
1718 got_second = &temp3;
1722 // Scan forwards to currently selected item pointer.
1723 // Then count visible items to get destination. No wraparound.
1726 for(int i = 0; i < data[master_column].total; i++)
1728 BC_ListBoxItem *current_item = data[master_column].values[i];
1730 // Select next item once the number items after the currently selected item
1731 // have been passed.
1735 if((*counter) >= skip)
1737 for(int j = 0; j < columns; j++)
1738 data[j].values[i]->selected = 1;
1740 return item_to_index(this->data, current_item);
1745 // Got currently selected item. Deselect it.
1746 if(current_item->selected)
1748 for(int j = 0; j < columns; j++)
1749 data[j].values[i]->selected = 0;
1755 // Descend into expanded level
1756 if(current_item->get_sublist() &&
1757 current_item->get_expand())
1759 int result = select_next(skip,
1762 current_item->get_sublist(),
1772 // Hit bottom of top level without finding the next item.
1775 BC_ListBoxItem *current_item;
1776 // Select first item in top level and quit
1780 current_item = data[master_column].values[0];
1782 for(int j = 0; j < columns; j++)
1783 data[j].values[0]->selected = 1;
1788 // Select last item in top level and quit
1790 int current_row = data[master_column].total - 1;
1791 current_item = data[master_column].values[current_row];
1793 for(int j = 0; j < columns; j++)
1794 data[j].values[current_row]->selected = 1;
1798 return item_to_index(this->data, current_item);
1800 }while(top_level && data[master_column].total);
1806 void BC_ListBox::clamp_positions()
1808 items_w = get_items_width();
1809 items_h = get_items_height(data, columns);
1811 if(yposition < 0) yposition = 0;
1813 if(yposition > items_h - view_h)
1814 yposition = items_h - view_h;
1816 if(yposition < 0) yposition = 0;
1818 if(xposition < 0) xposition = 0;
1820 if(xposition >= items_w - view_w)
1821 xposition = items_w - view_w;
1823 if(xposition < 0) xposition = 0;
1826 int BC_ListBox::center_selection(int selection,
1827 ArrayList<BC_ListBoxItem*> *data,
1831 if(!data) data = this->data;
1832 if(!counter) counter = &temp;
1834 for(int i = 0; i < data[master_column].total; i++)
1839 BC_ListBoxItem *item = data[master_column].values[i];
1840 if((*counter) == selection)
1842 BC_ListBoxItem *top_item = this->data[master_column].values[0];
1845 if(display_format == LISTBOX_ICONS)
1847 // Icon is out of window
1848 if( item->icon_y-yposition > view_h-get_text_h(item) ||
1849 item->icon_y-yposition < 0 ) {
1850 yposition = item->icon_y - view_h / 2;
1853 if(data[master_column].values[selection]->icon_x - xposition > view_w ||
1854 data[master_column].values[selection]->icon_x - xposition < 0)
1856 xposition = item->icon_x - view_w / 2;
1861 // Text coordinate is out of window
1862 if( item->text_y-yposition > view_h-get_text_h(item) ||
1863 item->text_y-yposition < 0 ) {
1864 yposition = item->text_y -
1873 if(item->get_sublist())
1875 int result = center_selection(selection,
1876 item->get_sublist(),
1878 if(result) return result;
1884 void BC_ListBox::update_scrollbars(int flush)
1886 int h_needed = items_h = get_items_height(data, columns);
1887 int w_needed = items_w = get_items_width();
1889 // if(columns > 0 && column_width)
1890 // printf("BC_ListBox::update_scrollbars 1 %d %d\n", column_width[columns - 1], w_needed);
1894 if(xposition != xscrollbar->get_value())
1895 xscrollbar->update_value(xposition);
1897 if(w_needed != xscrollbar->get_length() ||
1898 view_w != xscrollbar->get_handlelength())
1899 xscrollbar->update_length(w_needed, xposition, view_w, 0);
1904 if(yposition != yscrollbar->get_value())
1905 yscrollbar->update_value(yposition);
1907 if(h_needed != yscrollbar->get_length() || view_h != yscrollbar->get_handlelength())
1908 yscrollbar->update_length(h_needed, yposition, view_h, 0);
1911 if(flush) this->flush();
1914 int BC_ListBox::get_scrollbars()
1916 int h_needed = items_h = get_items_height(data, columns);
1917 int w_needed = items_w = get_items_width();
1921 title_h = get_title_h();
1923 view_h = popup_h - title_h - 4;
1924 view_w = popup_w - 4;
1926 // Create scrollbars as needed
1927 for(int i = 0; i < 2; i++)
1929 if(w_needed > view_w)
1934 get_resources()->hscroll_data[SCROLL_HANDLE_UP]->get_h() -
1942 if(h_needed > view_h)
1946 get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w() -
1955 // Update subwindow size
1956 int new_w = popup_w;
1957 int new_h = popup_h;
1958 if(need_xscroll) new_h -= get_resources()->hscroll_data[SCROLL_HANDLE_UP]->get_h();
1959 if(need_yscroll) new_w -= get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w();
1962 if(new_w != BC_WindowBase::get_w() || new_h != BC_WindowBase::get_h())
1963 gui->resize_window(new_w, new_h);
1965 BC_WindowBase *destination = (is_popup ? gui : parent_window);
1970 destination->add_subwindow(xscrollbar =
1971 new BC_ListBoxXScroll(this,
1975 xscrollbar->show_window(0);
1976 xscrollbar->bound_to = this;
1980 xscrollbar->update_length(w_needed, xposition, view_w, flush);
1981 xscrollbar->reposition_window(get_xscroll_x(),
1983 get_xscroll_width());
1988 if(xscrollbar) delete xscrollbar;
1997 destination->add_subwindow(yscrollbar =
1998 new BC_ListBoxYScroll(this,
2002 yscrollbar->show_window(0);
2003 yscrollbar->bound_to = this;
2007 yscrollbar->update_length(h_needed, yposition, view_h, flush);
2008 yscrollbar->reposition_window(get_yscroll_x(),
2010 get_yscroll_height());
2015 if(yscrollbar) delete yscrollbar;
2021 view_w + 4 != bg_surface->get_w() ||
2022 view_h + 4 != bg_surface->get_h())
2024 if(bg_surface) delete bg_surface;
2025 bg_surface = new BC_Pixmap(gui, view_w + 4, view_h + 4);
2035 void BC_ListBox::set_drag_scroll(int value)
2037 allow_drag_scroll = value;
2041 // Test for scrolling by dragging
2043 int BC_ListBox::test_drag_scroll(int cursor_x, int cursor_y)
2046 if(allow_drag_scroll ||
2047 current_operation == SELECT_RECT)
2050 int top_boundary = get_title_h();
2052 if(cursor_y < top_boundary ||
2053 cursor_y >= view_h + title_h + LISTBOX_BORDER * 2 ||
2054 cursor_x < LISTBOX_BORDER ||
2055 cursor_x >= view_w + LISTBOX_BORDER)
2063 int BC_ListBox::drag_scroll_event()
2065 int top_boundary = get_title_h();
2068 if(get_cursor_y() < top_boundary)
2070 yposition -= top_boundary - get_cursor_y();
2074 if(get_cursor_y() >= view_h + title_h + 4)
2076 yposition += get_cursor_y() - (view_h + title_h + 4);
2080 if(get_cursor_x() < 2)
2082 xposition -= 2 - get_cursor_x();
2086 if(get_cursor_x() >= view_w + 2)
2088 xposition += get_cursor_x() - (view_w + 2);
2091 if(result) clamp_positions();
2095 int BC_ListBox::rectangle_scroll_event()
2097 int old_xposition = xposition;
2098 int old_yposition = yposition;
2099 int result = drag_scroll_event();
2103 rect_x1 += old_xposition - xposition;
2104 rect_y1 += old_yposition - yposition;
2105 rect_x2 = get_cursor_x();
2106 rect_y2 = get_cursor_y();
2108 int x1 = MIN(rect_x1, rect_x2);
2109 int x2 = MAX(rect_x1, rect_x2);
2110 int y1 = MIN(rect_y1, rect_y2);
2111 int y2 = MAX(rect_y1, rect_y2);
2113 if(select_rectangle(data,
2119 selection_changed();
2124 update_scrollbars(1);
2129 int BC_ListBox::select_scroll_event()
2131 int result = drag_scroll_event();
2135 highlighted_item = selection_number = get_cursor_item(data,
2141 update_scrollbars(1);
2142 selection_changed();
2147 int BC_ListBox::select_rectangle(ArrayList<BC_ListBoxItem*> *data,
2154 for(int i = 0; i < data[master_column].total; i++)
2156 for(int j = 0; j < columns; j++)
2158 BC_ListBoxItem *item = data[j].values[i];
2159 if(display_format == LISTBOX_ICONS)
2161 int icon_x, icon_y, icon_w, icon_h;
2162 int text_x, text_y, text_w, text_h;
2163 get_icon_mask(item, icon_x, icon_y, icon_w, icon_h);
2164 get_text_mask(item, text_x, text_y, text_w, text_h);
2166 if((x2 >= icon_x && x1 < icon_x + icon_w &&
2167 y2 >= icon_y && y1 < icon_y + icon_h) ||
2168 (x2 >= text_x && x1 < text_x + text_w &&
2169 y2 >= text_y && y1 < text_y + text_h))
2190 gui->get_w() - get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w() :
2193 y1 < gui->get_h() &&
2194 y2 >= get_item_y(item) &&
2195 y1 < get_item_y(item) + get_item_h(item))
2214 BC_ListBoxItem *item = data[master_column].values[i];
2215 if(item->get_sublist() &&
2217 result |= select_rectangle(item->get_sublist(),
2226 int BC_ListBox::reposition_item(ArrayList<BC_ListBoxItem*> *data,
2227 int selection_number,
2233 if(!counter) counter = &temp;
2236 for(int i = 0; i < data[master_column].total; i++)
2238 BC_ListBoxItem *item = data[master_column].values[i];
2240 if((*counter) == selection_number)
2246 // Not recursive because it's only used for icons
2251 void BC_ListBox::move_selection(ArrayList<BC_ListBoxItem*> *dst,
2252 ArrayList<BC_ListBoxItem*> *src)
2254 for(int i = 0; i < src[master_column].total; i++)
2256 BC_ListBoxItem *item = src[master_column].values[i];
2261 for(int j = 0; j < columns; j++)
2263 dst[j].append(src[j].values[i]);
2264 src[j].remove_number(i);
2268 // Descend into sublist
2269 if(item->get_sublist())
2272 item->get_sublist());
2277 int BC_ListBox::put_selection(ArrayList<BC_ListBoxItem*> *data,
2278 ArrayList<BC_ListBoxItem*> *src,
2283 if(!counter) counter = &temp;
2287 for(int j = 0; j < columns; j++)
2289 for(int i = 0; i < src[j].total; i++)
2291 data[j].append(src[j].values[i]);
2297 for(int i = 0; i < data[master_column].total; i++)
2300 if((*counter) == destination)
2302 for(int j = 0; j < columns; j++)
2304 for(int k = 0; k < src[j].total; k++)
2306 data[j].insert(src[j].values[k], destination + k);
2312 BC_ListBoxItem *item = data[master_column].values[i];
2313 if(item->get_sublist())
2315 if(put_selection(item->get_sublist(),
2327 int BC_ListBox::item_to_index(ArrayList<BC_ListBoxItem*> *data,
2328 BC_ListBoxItem *item,
2332 if(!counter) counter = &temp;
2334 for(int i = 0; i < data[master_column].total; i++)
2337 for(int j = 0; j < columns; j++)
2339 BC_ListBoxItem *new_item = data[j].values[i];
2340 //printf("BC_ListBox::item_to_index 1 %d %d %p\n", j, i, new_item);
2341 if(new_item == item)
2347 BC_ListBoxItem *new_item = data[master_column].values[i];
2348 if(new_item->get_sublist())
2350 if(item_to_index(new_item->get_sublist(),
2360 BC_ListBoxItem* BC_ListBox::index_to_item(ArrayList<BC_ListBoxItem*> *data,
2366 if(!counter) counter = &temp;
2367 for(int i = 0; i < data[master_column].total; i++)
2370 if((*counter) == number)
2372 return data[column].values[i];
2374 BC_ListBoxItem *item = data[master_column].values[i];
2375 if(item->get_sublist())
2377 BC_ListBoxItem *result = index_to_item(item->get_sublist(),
2381 if(result) return result;
2387 int BC_ListBox::get_cursor_item(ArrayList<BC_ListBoxItem*> *data,
2390 BC_ListBoxItem **item_return,
2395 if(!data) return -1;
2396 if(!counter) counter = &temp;
2398 // Icons are not treed
2399 if(display_format == LISTBOX_ICONS)
2401 for(int j = data[master_column].total - 1; j >= 0; j--)
2403 int icon_x, icon_y, icon_w, icon_h;
2404 int text_x, text_y, text_w, text_h;
2405 BC_ListBoxItem *item = data[master_column].values[j];
2406 get_icon_mask(item, icon_x, icon_y, icon_w, icon_h);
2407 get_text_mask(item, text_x, text_y, text_w, text_h);
2409 if((cursor_x >= icon_x && cursor_x < icon_x + icon_w &&
2410 cursor_y >= icon_y && cursor_y < icon_y + icon_h) ||
2411 (cursor_x >= text_x && cursor_x < text_x + text_w &&
2412 cursor_y >= text_y && cursor_y < text_y + text_h))
2414 if(item_return) (*item_return) = item;
2421 // Cursor is inside items rectangle
2423 cursor_x < (yscrollbar ?
2424 gui->get_w() - get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w() :
2426 // Only clamp y if we're not in a SELECT operation.
2427 (current_operation == BC_ListBox::SELECT ||
2428 (cursor_y > get_title_h() + LISTBOX_BORDER &&
2429 cursor_y < gui->get_h())))
2431 // Search table for cursor obstruction
2432 for(int i = 0; i < data[master_column].total; i++)
2434 BC_ListBoxItem *item = data[master_column].values[i];
2437 // Cursor is inside item on current level
2440 cursor_y >= get_item_y(item) &&
2441 cursor_y < get_item_y(item) + get_item_h(item))
2443 //printf("BC_ListBox::get_cursor_item %d %d %p\n", master_column, i, item);
2444 if(item_return) (*item_return) = item;
2448 // Descend into sublist
2449 if(item->get_sublist())
2451 if(get_cursor_item(item->get_sublist(),
2456 item->get_expand()) >= 0)
2465 int BC_ListBox::repeat_event(int64_t duration)
2467 switch(current_operation)
2469 // Repeat out of bounds selection
2471 if(duration == get_resources()->scroll_repeat)
2472 return rectangle_scroll_event();
2476 if(duration == get_resources()->scroll_repeat)
2477 return select_scroll_event();
2482 if(button_highlighted && is_popup && !tooltip_done &&
2483 tooltip_text && tooltip_text[0] != 0 &&
2484 duration == get_resources()->tooltip_delay)
2496 int BC_ListBox::cursor_enter_event()
2500 switch(current_operation)
2502 // Cursor moved over button, pressed, and exited.
2503 case BUTTON_DOWN_SELECT:
2504 if(top_level->event_win == win)
2506 current_operation = BUTTON_DN;
2508 button_highlighted = 1;
2514 // Cursor entered button
2515 if(is_popup && top_level->event_win == win)
2517 button_highlighted = 1;
2522 // TODO: Need to get the highlighted column title or item
2523 if(gui && top_level->event_win == gui->win)
2525 list_highlighted = 1;
2535 int BC_ListBox::cursor_leave_event()
2537 if(current_operation == COLUMN_DRAG) return 0;
2540 if(button_highlighted)
2542 button_highlighted = 0;
2547 if(list_highlighted)
2549 list_highlighted = 0;
2550 highlighted_item = -1;
2551 highlighted_ptr = 0;
2552 highlighted_title = -1;
2553 int redraw_toggles = 0;
2554 for(int i = 0; i < expanders.total; i++)
2555 expanders.values[i]->cursor_leave_event(&redraw_toggles);
2563 int BC_ListBox::get_first_selection(ArrayList<BC_ListBoxItem*> *data, int *result)
2566 if(!result) result = &temp;
2568 for(int i = 0; i < data[master_column].total; i++)
2570 BC_ListBoxItem *item = data[master_column].values[i];
2572 if(item->selected) return (*result);
2573 if(item->get_sublist())
2575 if(get_first_selection(item->get_sublist(), result) >= 0)
2582 int BC_ListBox::get_total_items(ArrayList<BC_ListBoxItem*> *data,
2587 if(!result) result = &temp;
2589 for(int i = 0; i < data[master_column].total; i++)
2592 if(data[master_column].values[i]->get_sublist())
2593 get_total_items(data[master_column].values[i]->get_sublist(),
2602 int BC_ListBox::get_last_selection(ArrayList<BC_ListBoxItem*> *data,
2613 for(int i = data[master_column].total - 1; i >= 0; i--)
2615 BC_ListBoxItem *item = data[master_column].values[i];
2620 return get_total_items(data, 0, master_column) - (*result) /* - 1 */;
2625 if(item->get_sublist())
2627 if(get_last_selection(item->get_sublist(), result) >= 0)
2630 return get_total_items(data, 0, master_column) - (*result) /* - 1 */;
2639 void BC_ListBox::select_range(ArrayList<BC_ListBoxItem*> *data,
2645 if(!current) current = &temp;
2647 for(int i = 0; i < data[master_column].total; i++)
2650 if((*current) >= start && (*current) < end)
2652 for(int j = 0; j < columns; j++)
2653 data[j].values[i]->selected = 1;
2655 BC_ListBoxItem *item = data[master_column].values[i];
2656 if(item->get_sublist())
2657 select_range(item->get_sublist(),
2665 // Fill items between current selection and new selection
2666 int BC_ListBox::expand_selection(int button_press, int selection_number)
2668 int old_selection_start = selection_start;
2669 int old_selection_end = selection_end;
2671 // printf("BC_ListBox::expand_selection %d %d\n",
2672 // selection_center,
2673 // selection_number);
2675 // Calculate the range to select based on selection_center and selection_number
2676 if(selection_number < selection_center)
2678 selection_start = selection_number;
2682 selection_end = selection_number + 1;
2685 //printf("BC_ListBox::expand_selection %d %d %d %d\n", old_selection_start, old_selection_end, selection_start, selection_end);
2686 // Recurse through all the items and select the desired range
2687 select_range(data, selection_start, selection_end);
2690 return (old_selection_start != selection_start ||
2691 old_selection_end != selection_end);
2694 int BC_ListBox::toggle_item_selection(ArrayList<BC_ListBoxItem*> *data,
2695 int selection_number,
2699 if(!counter) counter = &temp;
2701 for(int i = 0; i < data[master_column].total; i++)
2703 BC_ListBoxItem *item = data[master_column].values[i];
2705 if((*counter) == selection_number)
2707 // Get new value for selection
2708 int selected = !item->selected;
2710 for(int j = 0; j < columns; j++)
2711 data[j].values[i]->selected = selected;
2715 // Descend into sublist
2716 if(item->get_sublist())
2718 if(toggle_item_selection(item->get_sublist(),
2729 void BC_ListBox::set_all_selected(ArrayList<BC_ListBoxItem*> *data, int value)
2731 for(int i = 0; i < data[master_column].total; i++)
2733 for(int j = 0; j < columns; j++)
2735 BC_ListBoxItem *item = data[j].values[i];
2736 item->selected = value;
2738 BC_ListBoxItem *item = data[master_column].values[i];
2739 if(item->get_sublist())
2741 set_all_selected(item->get_sublist(), value);
2746 void BC_ListBox::set_selected(ArrayList<BC_ListBoxItem*> *data,
2752 if(!counter) counter = &temp;
2753 for(int i = 0; i < data[master_column].total && (*counter) != item_number; i++)
2756 if((*counter) == item_number)
2758 for(int j = 0; j < columns; j++)
2760 BC_ListBoxItem *item = data[j].values[i];
2761 item->selected = value;
2766 BC_ListBoxItem *item = data[master_column].values[i];
2767 if(item->get_sublist())
2769 set_selected(item->get_sublist(),
2777 int BC_ListBox::update_selection(ArrayList<BC_ListBoxItem*> *data,
2778 int selection_number,
2783 if(!counter) counter = &temp;
2785 for(int i = 0; i < data[master_column].total; i++)
2787 BC_ListBoxItem *item = data[master_column].values[i];
2789 if((*counter) == selection_number && !item->selected)
2792 for(int j = 0; j < columns; j++)
2793 data[j].values[i]->selected = 1;
2796 if((*counter) != selection_number && item->selected)
2799 for(int j = 0; j < columns; j++)
2800 data[j].values[i]->selected = 0;
2802 if(item->get_sublist())
2803 result |= update_selection(item->get_sublist(),
2810 void BC_ListBox::promote_selections(ArrayList<BC_ListBoxItem*> *data,
2814 for(int i = 0; i < data[master_column].total; i++)
2816 for(int j = 0; j < columns; j++)
2818 BC_ListBoxItem *item = data[j].values[i];
2819 if(item->selected == old_value) item->selected = new_value;
2821 BC_ListBoxItem *item = data[master_column].values[i];
2822 if(item->get_sublist())
2823 promote_selections(item->get_sublist(), old_value, new_value);
2827 int BC_ListBox::focus_out_event()
2833 int BC_ListBox::button_press_event()
2836 BC_ListBoxItem *current_item = 0;
2838 int do_selection_change = 0;
2839 const int debug = 0;
2842 if(debug) printf("BC_ListBox::button_press_event %d this=%p event_win=%p %p %p %p\n",
2845 (void*)top_level->event_win,
2846 (void*)(gui ? gui->win : 0),
2848 (void*)parent_window->win);
2850 // Pressed in button
2851 if(is_popup && top_level->event_win == win)
2853 if(debug) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2854 current_operation = BUTTON_DN;
2858 if(!active && !disabled)
2860 top_level->deactivate();
2867 // Pressed in scrollbar
2868 if((xscrollbar && top_level->event_win == xscrollbar->win) ||
2869 (yscrollbar && top_level->event_win == yscrollbar->win))
2871 if(debug) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2876 if(gui && top_level->event_win == gui->win)
2878 if(debug) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2880 // Activate list items
2881 // If it is a suggestion popup, it is visible without being active
2884 if(debug) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2885 if(!is_suggestions) top_level->deactivate();
2886 if(debug) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2888 if(debug) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2891 // Wheel mouse pressed
2892 if(get_buttonpress() == 4)
2894 if(current_operation == NO_OPERATION)
2896 current_operation = WHEEL;
2899 set_yposition(yposition - gui->get_h() / 10, 0);
2901 update_scrollbars(0);
2902 highlighted_ptr = 0;
2903 highlighted_item = get_cursor_item(data,
2904 top_level->cursor_x,
2905 top_level->cursor_y,
2913 if(get_buttonpress() == 5)
2915 if(current_operation == NO_OPERATION)
2917 current_operation = WHEEL;
2920 set_yposition(yposition + gui->get_h() / 10, 0);
2922 update_scrollbars(0);
2923 highlighted_ptr = 0;
2924 highlighted_item = get_cursor_item(data,
2925 top_level->cursor_x,
2926 top_level->cursor_y,
2934 // Pressed over column title division
2935 if(test_column_divisions(gui->get_cursor_x(),
2936 gui->get_cursor_y(),
2939 drag_cursor_x = gui->get_cursor_x() + xposition;
2941 drag_column_w = column_width[highlighted_division - 1];
2943 drag_column_w = default_column_width[highlighted_division - 1];
2945 current_operation = DRAG_DIVISION;
2949 // Pressed in column title
2950 if(test_column_titles(gui->get_cursor_x(), gui->get_cursor_y()))
2952 current_operation = COLUMN_DN;
2953 button_highlighted = 0;
2954 list_highlighted = 1;
2959 // Pressed in expander
2960 if(test_expanders())
2962 current_operation = EXPAND_DN;
2963 // Need to redraw items because of alpha
2968 // Pressed over item
2969 if((selection_number = get_cursor_item(data,
2970 gui->get_cursor_x(),
2971 gui->get_cursor_y(),
2972 ¤t_item)) >= 0)
2974 if(debug) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2976 // Get item button was pressed over
2977 selection_number2 = selection_number1;
2978 selection_number1 = selection_number;
2980 selection_start = -1;
2984 // Multiple item selection is possible
2985 if(selection_mode == LISTBOX_MULTIPLE &&
2986 (ctrl_down() || shift_down()))
2988 // Expand text selection.
2989 // Fill items between selected region and current item.
2991 (display_format == LISTBOX_TEXT ||
2992 display_format == LISTBOX_ICON_LIST))
2994 // Get first item selected
2995 selection_start = get_first_selection(data);
2996 // Get last item selected
2997 selection_end = get_last_selection(data);
2998 // Get center of selected region
2999 if(selection_end > selection_start)
3001 selection_center = (selection_end + selection_start) >> 1;
3005 selection_center = selection_number;
3009 // Deselect everything.
3010 set_all_selected(data, 0);
3011 // Select just the items
3012 expand_selection(1, selection_number);
3016 // Toggle a single item on or off
3018 toggle_item_selection(data, selection_number);
3019 new_value = current_item->selected;
3023 // Select single item
3025 if(!current_item->selected)
3027 set_all_selected(data, 0);
3036 current_operation = SELECT;
3037 highlighted_item = selection_number;
3038 highlighted_ptr = current_item;
3039 button_highlighted = 0;
3040 list_highlighted = 1;
3043 do_selection_change = 1;
3048 // Pressed over nothing. Start rectangle selection.
3050 if(get_buttonpress() == 1 &&
3051 selection_mode == LISTBOX_MULTIPLE)
3055 // Deselect all and redraw if anything was selected
3056 if(get_selection_number(0, 0) >= 0)
3058 set_all_selected(data, 0);
3060 do_selection_change = 1;
3066 // Promote selections to protect from a rectangle selection
3067 promote_selections(data, 1, 2);
3070 // Start rectangle selection
3071 current_operation = SELECT_RECT;
3072 rect_x1 = rect_x2 = get_cursor_x();
3073 rect_y1 = rect_y2 = get_cursor_y();
3081 // Suggestion box is not active but visible, so lie to get it to deactivate
3082 if(is_popup && (active || (is_suggestions && gui)))
3085 if(debug) printf("BC_ListBox::button_press_event %d\n", __LINE__);
3091 if(do_selection_change) selection_changed();
3092 if(debug) printf("BC_ListBox::button_press_event %d %d\n",
3099 int BC_ListBox::button_release_event()
3102 int cursor_x, cursor_y;
3106 //printf("BC_ListBox::button_release_event 1 %d\n", current_operation);
3107 switch(current_operation)
3110 current_operation = NO_OPERATION;
3115 current_operation = NO_OPERATION;
3119 // Release item selection
3120 case BUTTON_DOWN_SELECT:
3122 //printf("BC_ListBox::button_release_event 10\n");
3123 unset_repeat(get_resources()->scroll_repeat);
3124 current_operation = NO_OPERATION;
3126 translate_coordinates(top_level->event_win, gui->win,
3127 gui->get_cursor_x(), gui->get_cursor_y(),
3128 &cursor_x, &cursor_y);
3129 selection_number1 = selection_number =
3130 get_cursor_item(data, cursor_x, cursor_y);
3131 //printf("BC_ListBox::button_release_event %d %d\n", selection_number2, selection_number1);
3137 if(selection_number >= 0)
3143 // Second button release outside button
3144 if(button_releases > 1)
3151 if(top_level->get_double_click() &&
3152 selection_number2 == selection_number1 &&
3153 selection_number2 >= 0 &&
3154 selection_number1 >= 0)
3164 unset_repeat(get_resources()->scroll_repeat);
3167 // Demote selections from rectangle selection
3168 promote_selections(data, 2, 1);
3171 // Hide rectangle overlay
3173 current_operation = NO_OPERATION;
3177 // Release popup button
3180 current_operation = NO_OPERATION;
3184 // Second button release inside button
3185 if(button_releases > 1)
3193 current_operation = NO_OPERATION;
3194 // Update the sort column and the sort order for the user only if the existing
3195 // sort column is valid.
3196 if(sort_column >= 0)
3198 // Invert order only if column is the same
3199 if(highlighted_title == sort_column)
3201 (sort_order == SORT_ASCENDING) ?
3204 // Set the new sort column
3205 sort_column = highlighted_title;
3206 if(!sort_order_event())
3212 // Sorting not enabled. Redraw the title state.
3221 int redraw_toggles = 0;
3222 for(int i = 0; i < expanders.total && !result; i++)
3224 if(expanders.values[i]->button_release_event(&redraw_toggles))
3229 // Need to redraw items because of alpha
3230 if(redraw_toggles) draw_items(1);
3231 current_operation = NO_OPERATION;
3236 // Can't default to NO_OPERATION because it may be used in a drag event.
3241 if(do_event) handle_event();
3243 //printf("BC_ListBox::button_release_event %d %d\n", __LINE__, get_window_lock());
3247 int BC_ListBox::get_title_h()
3249 if(display_format == LISTBOX_TEXT ||
3250 display_format == LISTBOX_ICON_LIST)
3251 return column_titles ? column_bg[0]->get_h() : 0;
3256 void BC_ListBox::reset_cursor(int new_cursor)
3260 if(gui->get_cursor() != new_cursor)
3262 gui->set_cursor(new_cursor, 0, 0);
3266 if(get_cursor() != new_cursor)
3268 set_cursor(new_cursor, 0, 0);
3272 int BC_ListBox::test_column_divisions(int cursor_x, int cursor_y, int &new_cursor)
3277 cursor_y < get_title_h() &&
3279 cursor_x < gui->get_w())
3281 for(int i = 1; i < columns; i++)
3283 if(cursor_x >= -xposition + get_column_offset(i) - 5 &&
3284 cursor_x < -xposition + get_column_offset(i) +
3285 get_resources()->listbox_title_hotspot)
3287 highlighted_item = -1;
3288 highlighted_ptr = 0;
3289 highlighted_division = i;
3290 highlighted_title = -1;
3291 list_highlighted = 1;
3292 new_cursor = HSEPARATE_CURSOR;
3297 highlighted_division = -1;
3301 int BC_ListBox::test_column_titles(int cursor_x, int cursor_y)
3306 cursor_y < get_title_h() &&
3308 cursor_x < gui->get_w())
3310 for(int i = 0; i < columns; i++)
3312 if(cursor_x >= -xposition + get_column_offset(i) &&
3313 (cursor_x < -xposition + get_column_offset(i + 1) ||
3316 highlighted_item = -1;
3317 highlighted_ptr = 0;
3318 highlighted_division = -1;
3319 highlighted_title = i;
3320 list_highlighted = 1;
3325 highlighted_title = -1;
3329 int BC_ListBox::test_expanders()
3331 for(int i = 0; i < expanders.total; i++)
3333 if(expanders.values[i]->button_press_event())
3335 current_operation = EXPAND_DN;
3343 int BC_ListBox::cursor_motion_event()
3345 int redraw = 0, result = 0;
3346 int new_cursor = ARROW_CURSOR;
3348 selection_number = -1;
3351 switch(current_operation)
3354 // Button pressed and slid off button
3355 if(!cursor_inside())
3357 current_operation = BUTTON_DOWN_SELECT;
3365 // int new_w = get_cursor_x() +
3367 // get_column_offset(highlighted_division - 1);
3368 int difference = get_cursor_x() + xposition - drag_cursor_x;
3369 int new_w = drag_column_w + difference;
3371 new_cursor = HSEPARATE_CURSOR;
3375 column_width[highlighted_division - 1] = new_w;
3379 default_column_width[highlighted_division - 1] = new_w;
3382 column_width_boundaries();
3384 // Force update of coords
3385 set_autoplacement(data, 0, 1);
3386 column_resize_event();
3390 update_scrollbars(1);
3397 if(test_drag_scroll(get_cursor_x(), get_cursor_y()))
3399 set_repeat(get_resources()->scroll_repeat);
3402 int old_x1 = MIN(rect_x1, rect_x2);
3403 int old_x2 = MAX(rect_x1, rect_x2);
3404 int old_y1 = MIN(rect_y1, rect_y2);
3405 int old_y2 = MAX(rect_y1, rect_y2);
3407 int new_rect_x2 = get_cursor_x();
3408 int new_rect_y2 = get_cursor_y();
3410 int x1 = MIN(rect_x1, new_rect_x2);
3411 int x2 = MAX(rect_x1, new_rect_x2);
3412 int y1 = MIN(rect_y1, new_rect_y2);
3413 int y2 = MAX(rect_y1, new_rect_y2);
3415 // Adjust rectangle coverage
3423 redraw = select_rectangle(data,
3433 //printf("BC_ListBox::cursor_motion_event %d\n", __LINE__);
3438 rect_x2 = get_cursor_x();
3439 rect_y2 = get_cursor_y();
3444 update_scrollbars(1);
3445 selection_changed();
3446 //printf("BC_ListBox::cursor_motion_event %d\n", __LINE__);
3454 //printf("BC_ListBox::cursor_motion_event %d\n", __LINE__);
3464 int old_highlighted_item = highlighted_item;
3466 if(test_drag_scroll(get_cursor_x(),
3469 set_repeat(get_resources()->scroll_repeat);
3473 highlighted_item = selection_number = get_cursor_item(data,
3479 // Deselect all items and select just the one we're over
3480 if(selection_number >= 0 &&
3484 selection_mode == LISTBOX_SINGLE))
3486 redraw = update_selection(data, selection_number);
3489 if(selection_mode == LISTBOX_MULTIPLE &&
3490 (shift_down() || ctrl_down()))
3491 // Expand multiple selection
3493 // Expand selected region in text mode centered around initial range
3494 if((display_format == LISTBOX_TEXT ||
3495 display_format == LISTBOX_ICON_LIST) &&
3498 // Deselect everything.
3499 set_all_selected(data, 0);
3501 // Select just the items
3502 redraw = expand_selection(0, selection_number);
3505 // Set the one item we're over to the selection value determined in
3506 // button_press_event.
3514 if(highlighted_item != old_highlighted_item)
3518 update_scrollbars(1);
3519 //printf("BC_ListBox::cursor_motion_event %d %d\n", highlighted_item, old_highlighted_item);
3520 selection_changed();
3525 case BUTTON_DOWN_SELECT:
3526 // Went back into button area
3529 current_operation = BUTTON_DN;
3534 // Went into item area
3537 int cursor_x = 0, cursor_y = 0;
3538 translate_coordinates(top_level->event_win,
3540 top_level->cursor_x,
3541 top_level->cursor_y,
3544 int old_highlighted_item = highlighted_item;
3545 highlighted_item = selection_number = get_cursor_item(data,
3550 if(highlighted_item != old_highlighted_item)
3552 update_selection(data, selection_number);
3554 selection_changed();
3561 int redraw_toggles = 0;
3562 for(int i = 0; i < expanders.total && !result; i++)
3564 result = expanders.values[i]->cursor_motion_event(
3569 // Need to redraw items because of the alpha
3577 int cursor_x = get_cursor_x(), cursor_y = get_cursor_y();
3578 if(gui && top_level->event_win == gui->win)
3580 int old_highlighted_title = highlighted_title;
3581 int old_list_highlighted = list_highlighted;
3582 int old_highlighted_item = highlighted_item;
3583 int redraw_titles = 0;
3584 int redraw_border = 0;
3585 int redraw_items = 0;
3586 int redraw_toggles = 0;
3590 // Test if cursor moved over a title division
3591 test_column_divisions(cursor_x, cursor_y, new_cursor);
3593 // Test if cursor moved over a title
3594 if(highlighted_division < 0)
3596 test_column_titles(cursor_x, cursor_y);
3599 // Test if cursor moved over expander
3600 if(highlighted_division < 0 &&
3601 highlighted_title < 0 &&
3602 (display_format == LISTBOX_TEXT ||
3603 display_format == LISTBOX_ICON_LIST))
3605 for(int i = 0; i < expanders.total; i++)
3607 expanders.values[i]->cursor_motion_event(
3610 //printf("BC_ListBox::cursor_motion_event %d\n", redraw_toggles);
3613 // Test if cursor moved over an item
3614 if(highlighted_division < 0 &&
3615 highlighted_title < 0)
3617 highlighted_item = get_cursor_item(data,
3624 // Clear title highlighting if moved over division
3625 if(old_highlighted_title != highlighted_title)
3630 // Highlight list border
3631 if(old_list_highlighted != list_highlighted)
3636 // Moved out of item area
3637 if(old_highlighted_item != highlighted_item)
3642 //printf("BC_ListBox::cursor_motion_event 1 %d\n", highlighted_item);
3644 // Change cursor to title division adjustment
3645 reset_cursor(new_cursor);
3672 if(!result && list_highlighted)
3674 list_highlighted = 0;
3675 highlighted_item = -1;
3676 highlighted_ptr = 0;
3677 highlighted_title = -1;
3678 highlighted_division = -1;
3690 int BC_ListBox::drag_start_event()
3692 switch(current_operation)
3696 gui->is_event_win() &&
3699 BC_ListBoxItem *item_return = 0;
3700 selection_number = get_cursor_item(data,
3701 top_level->cursor_x,
3702 top_level->cursor_y,
3705 if(selection_number >= 0)
3708 if (item_return->icon_vframe)
3710 drag_popup = new BC_DragWindow(this,
3711 item_return->icon_vframe /*,
3712 get_abs_cursor_x(0) - item_return->icon_vframe->get_w() / 2,
3713 get_abs_cursor_y(0) - item_return->icon_vframe->get_h() / 2 */);
3716 // this probably works not!
3717 if (item_return->icon)
3719 drag_popup = new BC_DragWindow(this,
3720 item_return->icon /*,
3721 get_abs_cursor_x(0) - item_return->icon->get_w() / 2,
3722 get_abs_cursor_y(0) - item_return->icon->get_h() / 2 */);
3726 drag_popup = new BC_DragWindow(this,
3727 drag_icon_vframe /*,
3728 get_abs_cursor_x(0) - drag_icon_vframe->get_w() / 2,
3729 get_abs_cursor_y(0) - drag_icon_vframe->get_h() / 2 */);
3732 current_operation = DRAG_ITEM;
3739 if(gui && gui->is_event_win() && allow_drag_column)
3741 drag_popup = new BC_DragWindow(this,
3742 drag_column_icon_vframe /*,
3743 get_abs_cursor_x(0) - drag_column_icon_vframe->get_w() / 2,
3744 get_abs_cursor_y(0) - drag_column_icon_vframe->get_h() / 2 */);
3745 dragged_title = highlighted_title;
3746 current_operation = COLUMN_DRAG;
3756 int BC_ListBox::drag_motion_event()
3758 //printf("BC_ListBox::drag_motion_event 1 %d\n", current_operation);
3759 switch(current_operation)
3764 int new_highlighted_item = -1;
3765 BC_ListBoxItem *new_highlighted_ptr = 0;
3766 new_highlighted_item = get_cursor_item(data,
3767 top_level->cursor_x, top_level->cursor_y,
3768 &new_highlighted_ptr);
3770 if(new_highlighted_item != highlighted_item)
3775 // Always update highlighted value for drag_stop
3776 highlighted_item = new_highlighted_item;
3777 highlighted_ptr = new_highlighted_ptr;
3778 //printf("BC_ListBox::drag_motion_event 1 %p\n", highlighted_ptr);
3783 update_scrollbars(1);
3786 return drag_popup->cursor_motion_event();
3792 int old_highlighted_title = highlighted_title;
3793 test_column_titles(get_cursor_x(), get_cursor_y());
3794 if(old_highlighted_title != highlighted_title)
3798 return drag_popup->cursor_motion_event();
3805 int BC_ListBox::drag_stop_event()
3807 switch(current_operation)
3810 // Inside window boundary
3811 if(top_level->cursor_x > 0 &&
3812 top_level->cursor_x < gui->get_w() - drag_popup->get_w() / 2 &&
3813 top_level->cursor_y > 0 &&
3814 top_level->cursor_y < gui->get_h() - drag_popup->get_h() / 2)
3819 if(display_format == LISTBOX_ICONS)
3821 reposition_item(data,
3823 top_level->cursor_x +
3824 drag_popup->get_offset_x() -
3828 top_level->cursor_y +
3829 drag_popup->get_offset_y() -
3839 int destination = highlighted_item = item_to_index(data,
3841 //printf("BC_ListBox::drag_stop_event 1 %p %d\n", highlighted_ptr, destination);
3843 // Move selected items from data to temporary
3844 ArrayList<BC_ListBoxItem*> *src_items =
3845 new ArrayList<BC_ListBoxItem*>[columns];
3847 move_selection(src_items, data);
3849 // Insert items from temporary to data
3855 delete [] src_items;
3856 set_autoplacement(data, 0, 1);
3863 drag_popup->drag_failure_event();
3868 current_operation = NO_OPERATION;
3874 if(dragged_title != highlighted_title)
3876 if(highlighted_title >= 0)
3878 if(!move_column_event()) draw_titles(1);
3881 drag_popup->drag_failure_event();
3883 current_operation = NO_OPERATION;
3893 BC_DragWindow* BC_ListBox::get_drag_popup()
3898 int BC_ListBox::translation_event()
3902 int new_x = gui->get_x() +
3903 (top_level->last_translate_x - top_level->prev_x -
3904 BC_DisplayInfo::get_left_border());
3905 int new_y = gui->get_y() +
3906 (top_level->last_translate_y - top_level->prev_y -
3907 BC_DisplayInfo::get_top_border());
3909 gui->reposition_window(new_x, new_y);
3915 int BC_ListBox::reposition_window(int x, int y, int w, int h, int flush)
3919 if(w != -1) popup_w = w;
3920 if(h != -1) popup_h = h;
3921 //printf("BC_ListBox::reposition_window %d %d\n", popup_w, popup_h);
3925 if(w != -1) popup_w = w;
3926 if(h != -1) popup_h = h;
3928 xscrollbar->reposition_window(get_xscroll_x(),
3930 get_xscroll_width());
3932 yscrollbar->reposition_window(get_yscroll_x(),
3934 get_yscroll_height());
3939 BC_WindowBase::reposition_window(x, y, w, h);
3945 int BC_ListBox::deactivate()
3947 // printf("BC_ListBox::deactivate %d this=%p gui=%p active=%d\n",
3957 //printf("BC_ListBox::deactivate %d this=%p gui=%p\n", __LINE__, this, gui);
3966 highlighted_item = -1;
3967 highlighted_ptr = 0;
3972 //printf("BC_ListBox::deactivate %d this=%p\n", __LINE__, this);
3974 top_level->active_subwindow = 0;
3980 int BC_ListBox::activate(int take_focus)
3982 //printf("BC_ListBox::activate %d %p\n", __LINE__, this);
3987 top_level->active_subwindow = this;
3991 button_releases = 0;
3993 // Test for existence of GUI in case this was previously called without
3994 // take_focus & again with take_focus
3995 if(is_popup && !gui)
4000 y = get_y() + get_h();
4001 if(justify == LISTBOX_RIGHT)
4003 x = get_x() - popup_w + get_w();
4011 XTranslateCoordinates(top_level->display,
4020 if(new_x < 0) new_x = 0;
4021 if(new_y + popup_h > top_level->get_root_h(0))
4022 new_y -= get_h() + popup_h;
4024 add_subwindow(gui = new BC_Popup(this,
4032 // Avoid top going out of screen
4035 //printf("BC_ListBox::activate %d this=%p %p\n", __LINE__, this, gui->win);
4037 gui->show_window(1);
4039 //printf("BC_ListBox::activate %d %p\n", __LINE__, this);
4045 int BC_ListBox::keypress_event()
4047 if(!active) return 0;
4049 //printf("BC_ListBox::keypress_event %d\n", __LINE__);
4051 int result = 0, redraw = 0;
4052 int view_items = last_in_view - first_in_view + 1;
4053 if( view_items <= 0 ) view_items = view_h / get_text_height(MEDIUMFONT);
4054 int new_item = -1, new_selection = -1;
4056 switch(top_level->get_keypress())
4061 top_level->deactivate();
4063 // If user is manipulating popup with keyboard, don't pass on event.
4066 //printf("BC_ListBox::keypress_event %d %p\n", __LINE__, get_selection(0, 0));
4067 if(top_level->get_keypress() == RETURN)
4069 //printf("BC_ListBox::keypress_event %d %p\n", __LINE__, get_selection(0, 0));
4077 new_selection = new_item = select_previous(0);
4079 //printf("BC_ListBox::keypress_event 1 %d\n", new_item);
4082 center_selection(new_item);
4090 new_selection = new_item = select_next(0);
4094 center_selection(new_item);
4102 new_selection = new_item = select_previous(view_items - 1);
4106 center_selection(new_item);
4114 new_selection = new_item = select_next(view_items - 1);
4118 center_selection(new_item);
4146 int query_len = strlen(query);
4147 if( query_len < (int)sizeof(query)-1 &&
4148 top_level->get_keypress() > 30 &&
4149 top_level->get_keypress() < 127)
4151 query[query_len++] = top_level->get_keypress();
4152 query[query_len] = 0;
4153 new_selection = query_list();
4156 if(top_level->get_keypress() == BACKSPACE)
4158 if(query_len > 0) query[--query_len] = 0;
4159 new_selection = query_list();
4172 update_scrollbars(1);
4175 //printf("BC_ListBox::keypress_event %d new_selection=%d\n", __LINE__, new_selection);
4176 if(new_selection >= 0 && !is_suggestions)
4178 //printf("BC_ListBox::keypress_event %d\n", __LINE__);
4179 selection_changed();
4180 //printf("BC_ListBox::keypress_event %d\n", __LINE__);
4187 BC_Pixmap* BC_ListBox::get_bg_surface()
4193 void BC_ListBox::draw_background()
4195 if( !bg_draw ) return;
4197 // White background pixmap
4198 set_color(top_level->get_resources()->listbox_inactive);
4199 draw_box(0, 0, bg_surface->get_w(), bg_surface->get_h(), bg_surface);
4201 // Optional heroine pixmap
4203 bg_surface->draw_pixmap(bg_pixmap,
4204 bg_surface->get_w() - top_level->get_resources()->listbox_bg->get_w(),
4208 void BC_ListBox::clear_listbox(int x, int y, int w, int h)
4210 gui->draw_pixmap(bg_surface, x, y, w, h, x, y - title_h);
4213 void BC_ListBox::update_format(int display_format, int redraw)
4215 this->display_format = display_format;
4216 if( redraw && gui ) draw_items(1, 1);
4219 int BC_ListBox::get_format()
4221 return display_format;
4226 int BC_ListBox::draw_items(int flush, int draw_bg)
4230 BC_Resources *resources = get_resources();
4232 //dump(data, columns);
4234 // Calculate items width
4235 calculate_item_coords();
4238 // Create and destroy scrollbars as needed
4243 if( bg_draw ) this->bg_draw = 1;
4249 if(display_format == LISTBOX_ICONS)
4251 clear_listbox(2, 2 + title_h, view_w, view_h);
4253 set_font(MEDIUMFONT);
4254 for(int i = 0; i < data[master_column].size(); i++)
4256 BC_ListBoxItem *item = data[master_column].get(i);
4257 if(get_item_x(item) >= -get_item_w(item) &&
4258 get_item_x(item) < view_w &&
4259 get_item_y(item) >= -get_item_h(item) + title_h &&
4260 get_item_y(item) < view_h + title_h)
4262 item->set_in_view(1);
4263 if( first_in_view < 0 ) first_in_view = i;
4265 int item_color = get_item_highlight(data, 0, i);
4266 int icon_x, icon_y, icon_w, icon_h;
4267 int text_x, text_y, text_w, text_h;
4270 get_icon_mask(item, icon_x, icon_y, icon_w, icon_h);
4271 get_text_mask(item, text_x, text_y, text_w, text_h);
4274 if(item_color != resources->listbox_inactive)
4276 gui->set_color(BLACK);
4277 gui->draw_rectangle(icon_x, icon_y, icon_w, icon_h);
4278 gui->set_color(item_color);
4279 gui->draw_box(icon_x + 1, icon_y + 1, icon_w - 2, icon_h - 2);
4280 gui->set_color(BLACK);
4281 gui->draw_rectangle(text_x, text_y, text_w, text_h);
4282 gui->set_color(item_color);
4283 gui->draw_box(text_x + 1, text_y + 1, text_w - 2, text_h - 2);
4285 if(icon_position == ICON_LEFT)
4286 gui->draw_box(text_x - 1, text_y + 1, 2, text_h - 2);
4288 if(icon_position == ICON_TOP)
4289 gui->draw_line(text_x + 1, text_y, text_x + icon_w - 2, text_y);
4290 if(text_x + text_w < icon_x + icon_w)
4292 gui->set_color(BLACK);
4293 gui->draw_line(text_x + text_w,
4301 gui->set_color(get_item_color(data, 0, i));
4303 gui->pixmap->draw_pixmap(item->icon,
4304 icon_x + ICON_MARGIN,
4305 icon_y + ICON_MARGIN);
4308 gui->draw_text(text_x + ICON_MARGIN,
4309 text_y + ICON_MARGIN + get_baseline(item),
4313 item->set_in_view(0);
4319 // Draw one column at a time so text overruns don't go into the next column
4320 // clear column backgrounds
4321 int current_toggle = 0;
4322 for(int j = 0; j < columns; j++)
4324 clear_listbox(LISTBOX_BORDER + get_column_offset(j) - xposition,
4325 LISTBOX_BORDER + title_h,
4326 get_column_width(j, 1),
4328 // Draw rows in the column recursively
4329 draw_text_recursive(data, j, 0, ¤t_toggle);
4332 // Delete excess expanders
4333 while(expanders.total > current_toggle)
4335 expanders.remove_object();
4339 // draw user images if available
4341 // Draw titles on top of rows for superposition effect
4344 // Clear garbage from bottom right corner
4345 if(xscrollbar && yscrollbar && is_popup)
4347 gui->draw_top_background(parent_window,
4348 popup_w - get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w(),
4349 popup_h - get_resources()->hscroll_data[SCROLL_HANDLE_UP]->get_h(),
4350 get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w(),
4351 get_resources()->hscroll_data[SCROLL_HANDLE_UP]->get_h());
4358 if(current_operation == SELECT_RECT)
4368 void BC_ListBox::draw_text_recursive(ArrayList<BC_ListBoxItem*> *data,
4371 int *current_toggle)
4376 BC_Resources *resources = get_resources();
4378 set_font(MEDIUMFONT);
4381 // Search for a branch and make room for toggle if there is one
4384 for(int i = 0; i < data[column].size(); i++)
4386 if(data[column].get(i)->get_sublist())
4388 subindent = BC_WindowBase::get_resources()->listbox_expand[0]->get_w();
4394 row_height = row_ascent = row_descent = 0;
4395 for(int i = 0; i < data[column].total; i++)
4397 BC_ListBoxItem *item = data[column].values[i];
4398 int ht = get_text_h(item);
4399 if( ht > row_height ) row_height = ht;
4400 int bl = get_baseline(item);
4401 if( bl > row_ascent ) row_ascent = bl;
4403 if( dt > row_descent ) row_ascent = bl;
4406 for(int i = 0; i < data[column].size(); i++)
4409 BC_ListBoxItem *item = data[column].values[i];
4410 BC_ListBoxItem *first_item = data[master_column].values[i];
4412 if(get_item_y(item) >= -get_item_h(item) + title_h &&
4413 get_item_y(item) < view_h + title_h)
4415 int row_color = get_item_highlight(data, 0, i);
4416 int x, y, w, h, column_width;
4418 get_text_mask(item, x, y, w, h);
4419 column_width = get_column_width(column, 1);
4420 if(x + column_width > view_w + LISTBOX_BORDER * 2)
4421 column_width = view_w + LISTBOX_BORDER * 2 - x;
4423 if(row_color != resources->listbox_inactive)
4425 gui->set_color(row_color);
4426 gui->draw_box(x, y, column_width, h);
4427 gui->set_color(BLACK);
4428 int yy = y, xx = x + column_width-1;
4429 gui->draw_line(x, yy, xx, yy);
4430 yy = y + row_height;
4431 gui->draw_line(x, yy, xx, yy);
4434 gui->set_color(get_item_color(data, column, i));
4437 if(column == 0 && display_format == LISTBOX_ICON_LIST)
4441 gui->pixmap->draw_pixmap(item->icon,
4444 x += item->icon->get_w() + ICON_MARGIN;
4449 // Indent only applies to first column
4451 x + LISTBOX_BORDER + LISTBOX_MARGIN +
4452 (column == 0 ? indent + subindent : 0),
4453 y + get_baseline(item), item->text);
4454 item->set_in_view(1);
4456 if( first_in_view < 0 ) first_in_view = i;
4462 item->get_sublist() &&
4463 item->get_columns())
4465 // Create new expander
4466 if(*current_toggle >= expanders.total)
4468 BC_ListBoxToggle *toggle =
4469 new BC_ListBoxToggle(this,
4471 x + LISTBOX_BORDER + LISTBOX_MARGIN + indent,
4474 expanders.append(toggle);
4477 // Reposition existing expander
4479 BC_ListBoxToggle *toggle = expanders.values[*current_toggle];
4480 //printf("BC_ListBox::draw_text_recursive 1 %d\n", *current_toggle);
4481 toggle->update(item,
4482 x + LISTBOX_BORDER + LISTBOX_MARGIN + indent,
4486 (*current_toggle)++;
4493 item->set_in_view(0);
4495 // Descend into sublist
4496 if(first_item->get_expand())
4498 draw_text_recursive(first_item->get_sublist(),
4500 indent + LISTBOX_INDENT,
4509 int BC_ListBox::draw_border(int flash)
4511 BC_Resources *resources = top_level->get_resources();
4512 gui->draw_3d_border(0,
4514 view_w + LISTBOX_BORDER * 2,
4515 view_h + title_h + LISTBOX_BORDER * 2,
4516 resources->listbox_border1,
4518 resources->listbox_border2_hi :
4519 resources->listbox_border2,
4521 resources->listbox_border3_hi :
4522 resources->listbox_border3,
4523 resources->listbox_border4);
4533 void BC_ListBox::draw_title(int number)
4535 // Column title background
4536 int image_number = 0;
4537 if(number == highlighted_title)
4540 if(current_operation == COLUMN_DN)
4544 int column_offset = get_column_offset(number) - xposition + LISTBOX_BORDER;
4545 int column_width = get_column_width(number, 1);
4546 gui->draw_3segmenth(get_column_offset(number) - xposition + LISTBOX_BORDER,
4548 get_column_width(number, 1) + get_resources()->listbox_title_overlap,
4549 column_bg[image_number]);
4551 // Column title sort order
4552 if(number == sort_column)
4555 if(sort_order == SORT_ASCENDING)
4556 src = column_sort_dn;
4558 src = column_sort_up;
4560 int x = column_offset +
4563 if(x > items_w) x = items_w;
4564 x -= 5 + src->get_w();
4565 gui->draw_pixmap(src,
4567 title_h / 2 - src->get_h() / 2 + LISTBOX_BORDER);
4571 int x = -xposition +
4572 get_column_offset(number) +
4575 x += get_resources()->listbox_title_margin;
4577 gui->set_color(get_resources()->listbox_title_color);
4579 LISTBOX_MARGIN + LISTBOX_BORDER + get_text_ascent(MEDIUMFONT),
4580 _(column_titles[number]));
4583 int BC_ListBox::draw_titles(int flash)
4586 (display_format == LISTBOX_TEXT ||
4587 display_format == LISTBOX_ICON_LIST))
4589 //printf("BC_ListBox::draw_titles 1 %d\n", highlighted_title);
4590 for(int i = 0; i < columns; i++)
4592 if(i != highlighted_title)
4596 if(highlighted_title >= 0) draw_title(highlighted_title);
4607 void BC_ListBox::draw_toggles(int flash)
4609 for(int i = 0; i < expanders.total; i++)
4610 expanders.values[i]->draw(0);
4612 //printf("BC_ListBox::draw_toggles 1 %d\n", flash);
4613 if(flash && expanders.total)
4620 int BC_ListBox::draw_rectangle(int flash)
4622 int x1 = MIN(rect_x1, rect_x2);
4623 int x2 = MAX(rect_x1, rect_x2);
4624 int y1 = MIN(rect_y1, rect_y2);
4625 int y2 = MAX(rect_y1, rect_y2);
4627 if(x1 == x2 || y1 == y2) return 0;
4630 gui->set_color(WHITE);
4631 gui->draw_rectangle(x1, y1, x2 - x1, y2 - y1);
4642 void BC_ListBox::dump(ArrayList<BC_ListBoxItem*> *data,
4649 printf("BC_ListBox::dump 1\n");
4652 for(int i = 0; i < data[master_column].total; i++)
4654 for(int k = 0; k < indent; k++)
4656 for(int j = 0; j < columns; j++)
4658 BC_ListBoxItem *item = data[j].values[i];
4659 printf("%d,%d,%d=%s ",
4662 item->autoplace_text,
4667 if(data[master_column].values[i]->get_sublist())
4669 dump(data[master_column].values[i]->get_sublist(),
4670 data[master_column].values[i]->get_columns(),