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
41 BC_ListBoxYScroll::BC_ListBoxYScroll(BC_ListBox *listbox)
42 : BC_ScrollBar(listbox->get_yscroll_x(), listbox->get_yscroll_y(),
43 listbox->yscroll_orientation, listbox->get_yscroll_height(),
44 listbox->items_h, listbox->yposition, listbox->view_h)
46 this->listbox = listbox;
49 BC_ListBoxYScroll::~BC_ListBoxYScroll()
53 int BC_ListBoxYScroll::handle_event()
55 listbox->set_yposition(get_value());
59 BC_ListBoxXScroll::BC_ListBoxXScroll(BC_ListBox *listbox)
60 : BC_ScrollBar(listbox->get_xscroll_x(), listbox->get_xscroll_y(),
61 listbox->xscroll_orientation, listbox->get_xscroll_width(),
62 listbox->items_w, listbox->xposition, listbox->view_w)
64 this->listbox = listbox;
67 BC_ListBoxXScroll::~BC_ListBoxXScroll()
71 int BC_ListBoxXScroll::handle_event()
73 listbox->set_xposition(get_value());
78 BC_ListBoxToggle::BC_ListBoxToggle(BC_ListBox *listbox,
83 this->listbox = listbox;
87 this->value = item->get_expand();
89 state = BC_Toggle::TOGGLE_CHECKED;
91 state = BC_Toggle::TOGGLE_UP;
94 void BC_ListBoxToggle::update(BC_ListBoxItem *item,
99 this->value = item->get_expand();
109 state = TOGGLE_CHECKED;
114 state = TOGGLE_CHECKEDHI;
125 case TOGGLE_CHECKEDHI:
130 case TOGGLE_DOWN_EXIT:
138 int BC_ListBoxToggle::cursor_motion_event(int *redraw_toggles)
140 int w = listbox->toggle_images[0]->get_w();
141 int h = listbox->toggle_images[0]->get_h();
142 int cursor_inside = listbox->get_cursor_x() >= x &&
143 listbox->get_cursor_x() < x + w &&
144 listbox->get_cursor_y() >= y &&
145 listbox->get_cursor_y() < y + h;
150 case BC_ListBoxToggle::TOGGLE_UPHI:
153 state = BC_ListBoxToggle::TOGGLE_UP;
158 case BC_ListBoxToggle::TOGGLE_CHECKEDHI:
161 state = BC_ListBoxToggle::TOGGLE_CHECKED;
166 case BC_ListBoxToggle::TOGGLE_DOWN:
169 state = BC_ListBoxToggle::TOGGLE_DOWN_EXIT;
175 case BC_ListBoxToggle::TOGGLE_DOWN_EXIT:
178 state = BC_ListBoxToggle::TOGGLE_DOWN;
188 state = BC_ListBoxToggle::TOGGLE_CHECKEDHI;
190 state = BC_ListBoxToggle::TOGGLE_UPHI;
198 int BC_ListBoxToggle::cursor_leave_event(int *redraw_toggles)
201 state = BC_ListBoxToggle::TOGGLE_CHECKED;
203 state = BC_ListBoxToggle::TOGGLE_UP;
207 int BC_ListBoxToggle::button_press_event()
209 int w = listbox->toggle_images[0]->get_w();
210 int h = listbox->toggle_images[0]->get_h();
212 if(listbox->gui->get_cursor_x() >= x &&
213 listbox->gui->get_cursor_x() < x + w &&
214 listbox->gui->get_cursor_y() >= y &&
215 listbox->gui->get_cursor_y() < y + h)
217 state = BC_ListBoxToggle::TOGGLE_DOWN;
223 int BC_ListBoxToggle::button_release_event(int *redraw_toggles)
229 case BC_ListBoxToggle::TOGGLE_DOWN:
232 state = BC_ListBoxToggle::TOGGLE_CHECKEDHI;
234 state = BC_ListBoxToggle::TOGGLE_UPHI;
235 listbox->expand_item(item, value);
239 case BC_ListBoxToggle::TOGGLE_DOWN_EXIT:
241 state = BC_ListBoxToggle::TOGGLE_CHECKED;
243 state = BC_ListBoxToggle::TOGGLE_UP;
251 void BC_ListBoxToggle::draw(int flash)
255 int image_number = 0;
256 int w = listbox->toggle_images[0]->get_w();
257 int h = listbox->toggle_images[0]->get_h();
261 case BC_ListBoxToggle::TOGGLE_UP: image_number = 0; break;
262 case BC_ListBoxToggle::TOGGLE_UPHI: image_number = 1; break;
263 case BC_ListBoxToggle::TOGGLE_CHECKED: image_number = 2; break;
264 case BC_ListBoxToggle::TOGGLE_DOWN: image_number = 3; break;
265 case BC_ListBoxToggle::TOGGLE_CHECKEDHI: image_number = 4; break;
266 case BC_ListBoxToggle::TOGGLE_DOWN_EXIT:
274 //printf("BC_ListBoxToggle::draw 1 %d\n", state);
275 listbox->gui->draw_pixmap(listbox->toggle_images[image_number],
282 listbox->gui->flash(x, y, w, h);
283 listbox->gui->flush();
301 // ====================================================== box
303 BC_ListBox::BC_ListBox(int x,
308 ArrayList<BC_ListBoxItem*> *data,
309 const char **column_titles,
317 : BC_SubWindow(x, y, w, h, -1)
319 justify = LISTBOX_RIGHT;
321 highlighted_item = -1;
322 highlighted_title = -1;
323 highlighted_division = -1;
327 current_cursor = ARROW_CURSOR;
337 xscroll_orientation = SCROLL_HORIZ;
338 yscroll_orientation = SCROLL_VERT;
342 selection_number1 = -1;
343 selection_number2 = -1;
346 row_height = row_ascent = row_descent = 0;
349 current_operation = NO_OPERATION;
350 button_highlighted = 0;
351 list_highlighted = 0;
354 allow_drag_scroll = 1;
360 allow_drag_column = 0;
367 for(int i = 0; i < 3; i++) column_bg[i] = 0;
368 for(int i = 0; i < 4; i++) button_images[i] = 0;
369 for(int i = 0; i < 5; i++) toggle_images[i] = 0;
374 //printf("BC_ListBox::BC_ListBox 1\n");
376 this->columns = columns;
377 this->yposition = yposition;
378 this->is_popup = is_popup;
379 this->use_button = 1;
380 this->display_format = display_format;
381 this->selection_mode = selection_mode;
382 this->icon_position = icon_position;
383 this->allow_drag = allow_drag;
384 this->column_titles = 0;
385 this->column_width = 0;
386 this->first_in_view = -1;
387 this->last_in_view = 0;
388 //printf("BC_ListBox::BC_ListBox 1\n");
390 if((!column_titles && column_width) ||
391 (column_titles && !column_width))
393 printf("BC_ListBox::BC_ListBox either column_titles or column_widths == NULL but not both.\n");
395 //printf("BC_ListBox::BC_ListBox 2 %p %p\n", column_titles, column_width);
397 set_columns(column_titles,
401 //printf("BC_ListBox::BC_ListBox 3\n");
403 drag_icon_vframe = 0;
404 drag_column_icon_vframe = 0;
408 // reset the search engine
409 //printf("BC_ListBox::BC_ListBox 4\n");
412 //printf("BC_ListBox::BC_ListBox 5\n");
415 BC_ListBox::~BC_ListBox()
417 expanders.remove_all_objects();
418 if(bg_surface) delete bg_surface;
419 if(bg_pixmap) delete bg_pixmap;
420 if(xscrollbar) delete xscrollbar;
421 if(yscrollbar) delete yscrollbar;
422 for(int i = 0; i < 3; i++) delete column_bg[i];
423 for(int i = 0; i < 4; i++) delete button_images[i];
424 for(int i = 0; i < 5; i++) delete toggle_images[i];
425 if(column_sort_up) delete column_sort_up;
426 if(column_sort_dn) delete column_sort_dn;
429 if(drag_popup) delete drag_popup;
432 int BC_ListBox::enable()
439 int BC_ListBox::disable()
446 void BC_ListBox::reset_query()
448 query[0] = 0; // reset query
451 int BC_ListBox::evaluate_query(char *string)
453 for(int i = 0; i < data[search_column].size(); i++)
455 if(strcmp(string, data[search_column].get(i)->text) <= 0 &&
456 data[search_column].get(i)->searchable)
465 int BC_ListBox::query_list()
467 if(query[0] == 0) return 0;
471 int selection_changed = 0;
472 int prev_selection = -1;
473 result = evaluate_query(query);
474 if(result >= 0) done = 1;
479 for(int i = 0; i < data[0].total; i++)
481 for(int j = 0; j < columns; j++)
483 if(data[j].values[i]->selected) prev_selection = i;
484 data[j].values[i]->selected = 0;
489 if(prev_selection != result)
490 selection_changed = 1;
491 for(int j = 0; j < columns; j++)
493 data[j].values[result]->selected = 1;
495 center_selection(result);
498 return selection_changed;
501 void BC_ListBox::init_column_width()
503 if(!column_width && data)
506 for(int i = 0; i < data[0].total; i++)
508 wd = get_text_w(data[0].values[i]);
509 if( wd > widest ) widest = wd;
511 default_column_width[0] = widest + 2 * LISTBOX_MARGIN;
515 int BC_ListBox::initialize()
521 for( int i = 0; i < 4; ++i )
523 button_images[i] = new BC_Pixmap(parent_window,
524 BC_WindowBase::get_resources()->listbox_button[i],
527 w = button_images[0]->get_w();
528 h = button_images[0]->get_h();
532 current_operation = NO_OPERATION;
538 current_operation = NO_OPERATION;
541 for(int i = 0; i < 3; i++)
543 column_bg[i] = new BC_Pixmap(parent_window,
544 get_resources()->listbox_column[i],
547 for(int i = 0; i < 5; i++)
549 toggle_images[i] = new BC_Pixmap(parent_window,
550 get_resources()->listbox_expand[i],
554 column_sort_up = new BC_Pixmap(parent_window,
555 BC_WindowBase::get_resources()->listbox_up,
557 column_sort_dn = new BC_Pixmap(parent_window,
558 BC_WindowBase::get_resources()->listbox_dn,
561 //printf("BC_ListBox::initialize 10\n");
562 drag_icon_vframe = get_resources()->type_to_icon[ICON_UNKNOWN];
563 drag_column_icon_vframe = get_resources()->type_to_icon[ICON_COLUMN];
564 // = new BC_Pixmap(parent_window,
565 // get_resources()->type_to_icon[ICON_UNKNOWN],
567 // drag_column_icon = new BC_Pixmap(parent_window,
568 // get_resources()->type_to_icon[ICON_COLUMN],
570 BC_SubWindow::initialize();
574 if(top_level->get_resources()->listbox_bg)
575 bg_pixmap = new BC_Pixmap(this,
576 get_resources()->listbox_bg,
582 if(!use_button && is_popup)
591 void BC_ListBox::deactivate_selection()
593 current_operation = NO_OPERATION;
596 int BC_ListBox::draw_button(int flush)
598 // Draw the button for a popup listbox
599 if(use_button && is_popup)
601 int image_number = 0;
603 draw_top_background(parent_window, 0, 0, w, h);
605 if(button_highlighted)
607 if(current_operation == BUTTON_DN)
613 pixmap->draw_pixmap(button_images[image_number],
625 int BC_ListBox::calculate_item_coords()
633 // Change the display_format to get the right item dimensions for both
635 temp_display_format = display_format;
638 // Scan the first column for lowest y coord of all text
639 // and lowest right x and y coord for all icons which aren't auto placable
640 calculate_last_coords_recursive(data,
647 // Reset last column width. It's recalculated based on text width.
649 calculate_item_coords_recursive(data,
658 display_format = temp_display_format;
663 void BC_ListBox::calculate_last_coords_recursive(
664 ArrayList<BC_ListBoxItem*> *data,
671 for(int i = 0; i < data[0].size(); i++)
673 int current_text_y = 0;
674 int current_icon_x = 0;
675 int current_icon_y = 0;
676 BC_ListBoxItem *item = data[0].get(i);
679 if(!item->autoplace_text)
681 // Lowest text coordinate
682 display_format = LISTBOX_TEXT;
683 current_text_y = item->text_y + get_text_h(item);
684 if(current_text_y > *next_text_y)
685 *next_text_y = current_text_y;
687 // Add sublist depth if it is expanded
688 if(item->get_sublist() &&
689 item->get_columns() &&
692 calculate_last_coords_recursive(item->get_sublist(),
701 // Get next_icon coordinate
704 BC_ListBoxItem *item = data[master_column].get(i);
705 if(!item->autoplace_icon)
707 display_format = LISTBOX_ICONS;
708 // Lowest right icon coordinate.
709 current_icon_x = item->icon_x;
710 if(current_icon_x > *icon_x) *icon_x = current_icon_x;
711 if(current_icon_x + get_item_w(item) > *next_icon_x) {
712 *next_icon_x = current_icon_x + get_item_w(item);
715 current_icon_y = item->icon_y + get_item_h(item);
716 if(current_icon_y > *next_icon_y)
717 *next_icon_y = current_icon_y;
724 void BC_ListBox::calculate_item_coords_recursive(
725 ArrayList<BC_ListBoxItem*> *data,
732 // get maximum height of an icon
733 row_height = get_text_height(MEDIUMFONT);
734 if(temp_display_format == LISTBOX_ICON_LIST)
736 for(int i = 0; i < data[0].size(); i++)
738 if(data[0].get(i)->icon)
740 if(data[0].get(i)->icon->get_h() > row_height)
741 row_height = data[0].get(i)->icon->get_h();
747 // Set up items which need autoplacement.
748 // Should fill icons down and then across
749 for(int i = 0; i < data[0].size(); i++)
751 // Don't increase y unless the row requires autoplacing.
752 int total_autoplaced_columns = 0;
754 // Set up icons in first column
757 BC_ListBoxItem *item = data[master_column].get(i);
758 if(item->autoplace_icon)
760 // 1 column only if icons are used
761 display_format = LISTBOX_ICONS;
764 if(*next_icon_y + get_item_h(item) >= get_h() &&
767 *icon_x = *next_icon_x;
771 if(*icon_x + get_item_w(item) > *next_icon_x)
772 *next_icon_x = *icon_x + get_item_w(item);
775 item->set_icon_x(*icon_x);
776 item->set_icon_y(*next_icon_y);
778 *next_icon_y += get_item_h(item);
786 row_ascent = row_descent = 0;
787 // row_height still holds icon max height
788 for(int j = 0; j < columns; j++)
790 BC_ListBoxItem *item = data[j].get(i);
791 if(item->autoplace_text)
793 display_format = LISTBOX_TEXT;
794 item->set_text_x(next_text_x);
795 item->set_text_y(*next_text_y);
796 int ht = get_text_h(item);
797 if( ht > row_height ) row_height = ht;
798 int bl = get_baseline(item);
799 if( bl > row_ascent ) row_ascent = bl;
801 if( dt > row_descent ) row_ascent = bl;
803 // printf("BC_ListBox::calculate_item_coords_recursive %p %d %d %d %d %s \n",
804 // item->get_sublist(),
805 // item->get_columns(),
806 // item->get_expand(),
809 // item->get_text());
810 // Increment position of next column
813 next_text_x += (column_width ?
815 default_column_width[j]);
818 // Set last column width based on text width
820 int new_w = get_item_w(item);
822 int *previous_w = (column_width ?
824 &default_column_width[j]);
825 if(new_w > *previous_w)
827 //printf("BC_ListBox::calculate_item_coords_recursive 1 %d\n", new_w);
829 total_autoplaced_columns++;
833 // Increase the text vertical position
834 if(total_autoplaced_columns)
836 display_format = LISTBOX_TEXT;
837 *next_text_y += row_height;
841 BC_ListBoxItem *item = data[master_column].values[i];
842 if(item->get_sublist() &&
843 item->get_columns() &&
846 calculate_item_coords_recursive(
857 void BC_ListBox::set_is_suggestions(int value)
859 this->is_suggestions = value;
862 void BC_ListBox::set_use_button(int value)
864 this->use_button = value;
867 void BC_ListBox::set_justify(int value)
869 this->justify = value;
872 void BC_ListBox::set_allow_drag_column(int value)
874 this->allow_drag_column = value;
877 void BC_ListBox::set_process_drag(int value)
879 this->process_drag = value;
882 void BC_ListBox::set_master_column(int value, int redraw)
884 this->master_column = value;
891 void BC_ListBox::set_search_column(int value)
893 this->search_column = value;
896 int BC_ListBox::get_sort_column()
901 void BC_ListBox::set_sort_column(int value, int redraw)
910 int BC_ListBox::get_sort_order()
915 void BC_ListBox::set_sort_order(int value, int redraw)
928 int BC_ListBox::get_display_mode()
930 return display_format;
933 int BC_ListBox::get_yposition()
938 int BC_ListBox::get_xposition()
943 int BC_ListBox::get_highlighted_item()
945 return highlighted_item;
949 int BC_ListBox::get_item_x(BC_ListBoxItem *item)
951 if(display_format == LISTBOX_TEXT)
953 return item->text_x - xposition + 2;
956 if(display_format == LISTBOX_ICON_LIST)
958 return item->text_x - xposition + 2;
962 return item->icon_x - xposition + 2;
966 int BC_ListBox::get_item_y(BC_ListBoxItem *item)
969 if(display_format == LISTBOX_TEXT)
971 result = item->text_y - yposition + title_h + 2;
974 if(display_format == LISTBOX_ICON_LIST)
976 result = item->text_y - yposition + title_h + 2;
980 result = item->icon_y - yposition + title_h + 2;
986 int BC_ListBox::get_item_w(BC_ListBoxItem *item)
988 if(display_format == LISTBOX_ICONS)
991 get_icon_mask(item, x, y, w, h);
993 get_text_mask(item, x, y, w, h);
996 return icon_position == ICON_LEFT ? icon_w + text_w :
997 icon_w > text_w ? icon_w : text_w;
999 return get_text_w(item) + 2 * LISTBOX_MARGIN;
1002 int BC_ListBox::get_item_h(BC_ListBoxItem *item)
1004 if(display_format == LISTBOX_ICONS)
1007 get_icon_mask(item, x, y, w, h);
1009 get_text_mask(item, x, y, w, h);
1012 return icon_position != ICON_LEFT ? icon_h + text_h :
1013 icon_h > text_h ? icon_h : text_h;
1015 return get_text_h(item);
1019 int BC_ListBox::get_icon_w(BC_ListBoxItem *item)
1021 return item->get_icon_w();
1024 int BC_ListBox::get_icon_h(BC_ListBoxItem *item)
1026 return item->get_icon_h();
1029 int BC_ListBox::get_text_w(BC_ListBoxItem *item)
1031 int w = item->get_text_w();
1032 if( w < 0 ) item->set_text_w(w = get_text_width(MEDIUMFONT, item->get_text()));
1036 int BC_ListBox::get_text_h(BC_ListBoxItem *item)
1038 int h = item->get_text_h();
1039 if( h < 0 ) item->set_text_h(h = get_text_height(MEDIUMFONT, item->get_text()));
1043 int BC_ListBox::get_baseline(BC_ListBoxItem *item)
1045 int b = item->get_baseline();
1046 if( b < 0 ) item->set_baseline(b = get_text_ascent(MEDIUMFONT));
1050 int BC_ListBox::get_items_width()
1054 if(display_format == LISTBOX_ICONS)
1056 for(int i = 0; i < columns; i++)
1058 for(int j = 0; j < data[i].total; j++)
1061 BC_ListBoxItem *item = data[i].values[j];
1064 get_icon_mask(item, x, y, w, h);
1065 if(x1 + w > widest) widest = x1 + w;
1067 if(display_format == LISTBOX_ICONS && icon_position == ICON_LEFT)
1070 get_text_mask(item, x, y, w, h);
1071 if(x1 + w > widest) widest = x1 + w;
1076 if(display_format == LISTBOX_TEXT)
1078 return get_column_offset(columns);
1082 return get_column_offset(columns);
1087 int BC_ListBox::get_items_height(ArrayList<BC_ListBoxItem*> *data,
1104 for(int j = 0; j < (data ? data[master_column].total : 0); j++)
1107 BC_ListBoxItem *item = data[master_column].values[j];
1109 if( display_format == LISTBOX_ICONS ||
1110 display_format == LISTBOX_ICON_LIST ) {
1111 get_icon_mask(item, x, y, w, h);
1112 if(y + h + yposition > highest) highest = y + h + yposition;
1114 get_text_mask(item, x, y, w, h);
1115 if(y + h + yposition > highest) highest = y + h + yposition;
1119 get_text_mask(item, x, y, w, h);
1121 // Descend into sublist
1122 if(item->get_sublist() &&
1125 get_items_height(item->get_sublist(),
1126 item->get_columns(),
1132 if( display_format == LISTBOX_TEXT && top_level )
1134 highest = LISTBOX_MARGIN + *result;
1141 int BC_ListBox::set_yposition(int position, int draw_items)
1143 this->yposition = position;
1146 this->draw_items(1);
1151 int BC_ListBox::set_xposition(int position)
1153 this->xposition = position;
1158 void BC_ListBox::expand_item(BC_ListBoxItem *item, int expand)
1162 item->expand = expand;
1163 // Collapse sublists if this is collapsed to make it easier to calculate
1165 if(item->get_sublist())
1166 collapse_recursive(item->get_sublist(), master_column);
1169 // Set everything for autoplacement
1171 set_autoplacement(data, 0, 1);
1177 void BC_ListBox::collapse_recursive(ArrayList<BC_ListBoxItem*> *data,
1180 for(int i = 0; i < data[master_column].total; i++)
1182 BC_ListBoxItem *item = data[master_column].values[i];
1183 if(item->get_sublist() && item->expand)
1186 collapse_recursive(item->get_sublist(), master_column);
1191 void BC_ListBox::set_autoplacement(ArrayList<BC_ListBoxItem*> *data,
1195 for(int i = 0; i < data[0].total; i++)
1197 for(int j = 0; j < columns; j++)
1199 if(do_icons) data[j].values[i]->autoplace_icon = 1;
1200 if(do_text) data[j].values[i]->autoplace_text = 1;
1203 BC_ListBoxItem *item = data[master_column].values[i];
1204 if(item->get_sublist())
1206 set_autoplacement(item->get_sublist(), do_icons, do_text);
1212 void BC_ListBox::set_scroll_stretch(int xv, int yv)
1214 if( xv >= 0 ) xscroll_orientation =
1215 !xv ? SCROLL_HORIZ : SCROLL_HORIZ + SCROLL_STRETCH;
1216 if( yv >= 0 ) yscroll_orientation =
1217 !yv ? SCROLL_VERT : SCROLL_VERT + SCROLL_STRETCH;
1220 int BC_ListBox::get_yscroll_x()
1223 return popup_w - get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w();
1227 get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w();
1230 int BC_ListBox::get_yscroll_y()
1238 int BC_ListBox::get_yscroll_height()
1240 return popup_h - (need_xscroll ?
1241 get_resources()->hscroll_data[SCROLL_HANDLE_UP]->get_h() :
1245 int BC_ListBox::get_xscroll_x()
1253 int BC_ListBox::get_xscroll_y()
1257 get_resources()->hscroll_data[SCROLL_HANDLE_UP]->get_h();
1261 get_resources()->hscroll_data[SCROLL_HANDLE_UP]->get_h();
1264 int BC_ListBox::get_xscroll_width()
1266 return popup_w - (need_yscroll ?
1267 get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w() :
1271 int BC_ListBox::get_column_offset(int column)
1277 column_width[--column] :
1278 default_column_width[--column];
1283 void BC_ListBox::column_width_boundaries()
1286 for(int i = 0; i < columns; i++) {
1287 if(column_width[i] < MIN_COLUMN_WIDTH)
1288 column_width[i] = MIN_COLUMN_WIDTH;
1292 for(int i = 0; i < columns; i++) {
1293 if(default_column_width[i] < MIN_COLUMN_WIDTH)
1294 default_column_width[i] = MIN_COLUMN_WIDTH;
1299 int BC_ListBox::get_column_width(int column, int clamp_right)
1301 if(column < columns - 1 || !clamp_right)
1302 return column_width ? column_width[column] : default_column_width[column];
1303 return popup_w + xposition - get_column_offset(column);
1306 int BC_ListBox::get_icon_mask(BC_ListBoxItem *item,
1307 int &x, int &y, int &w, int &h)
1309 if( display_format == LISTBOX_ICONS ) {
1310 x = get_item_x(item);
1311 y = get_item_y(item);
1312 w = get_icon_w(item) + ICON_MARGIN * 2;
1313 h = get_icon_h(item) + ICON_MARGIN * 2;
1320 int BC_ListBox::get_text_mask(BC_ListBoxItem *item,
1321 int &x, int &y, int &w, int &h)
1323 x = get_item_x(item);
1324 y = get_item_y(item);
1326 if(display_format == LISTBOX_ICONS) {
1327 if(icon_position == ICON_LEFT) {
1328 x += get_icon_w(item) + ICON_MARGIN * 2;
1329 y += get_icon_h(item) - get_text_h(item);
1332 y += get_icon_h(item) + ICON_MARGIN;
1335 w = get_text_w(item) + ICON_MARGIN * 2;
1336 h = get_text_h(item) + ICON_MARGIN * 2;
1339 if(display_format == LISTBOX_TEXT) {
1340 w = get_text_w(item) + LISTBOX_MARGIN * 2;
1341 h = get_text_h(item);
1345 w = get_text_width(MEDIUMFONT, item->text) + LISTBOX_MARGIN * 2;
1347 int ih = get_icon_h(item);
1348 if( h < ih ) h = ih;
1353 int BC_ListBox::get_item_highlight(ArrayList<BC_ListBoxItem*> *data,
1357 BC_Resources *resources = get_resources();
1358 if(data[column].values[item]->selected)
1359 return resources->listbox_selected;
1360 else if(highlighted_item >= 0 &&
1361 highlighted_ptr == data[master_column].values[item])
1362 return resources->listbox_highlighted;
1363 return resources->listbox_inactive;
1366 int BC_ListBox::get_item_color(ArrayList<BC_ListBoxItem*> *data,
1370 int color = data[column].values[item]->color;
1371 if( color == -1 ) color = get_resources()->listbox_text;
1372 if( get_item_highlight(data, column, item) == color )
1377 int BC_ListBox::get_from_column()
1379 return dragged_title;
1382 int BC_ListBox::get_to_column()
1384 return highlighted_title;
1388 BC_ListBoxItem* BC_ListBox::get_selection(int column,
1389 int selection_number)
1391 return get_selection_recursive(data,
1396 BC_ListBoxItem* BC_ListBox::get_selection_recursive(
1397 ArrayList<BC_ListBoxItem*> *data,
1399 int selection_number)
1403 for(int i = 0; i < data[master_column].total; i++)
1405 BC_ListBoxItem *item = data[master_column].values[i];
1408 //printf("BC_ListBox::get_selection_recursive %d\n", __LINE__);
1410 if(selection_number < 0)
1413 return data[column].values[i];
1417 if(item->get_sublist())
1419 BC_ListBoxItem *result = get_selection_recursive(item->get_sublist(),
1422 if(result) return result;
1430 int BC_ListBox::get_selection_number(int column,
1431 int selection_number)
1433 return get_selection_number_recursive(data,
1438 int BC_ListBox::get_selection_number_recursive(
1439 ArrayList<BC_ListBoxItem*> *data,
1441 int selection_number,
1446 if(!counter) counter = &temp;
1448 for(int i = 0; i < data[master_column].total; i++)
1451 BC_ListBoxItem *item = data[master_column].values[i];
1455 if(selection_number < 0)
1460 if(item->get_sublist())
1462 int result = get_selection_number_recursive(
1463 item->get_sublist(),
1467 if(result >= 0) return result;
1474 int BC_ListBox::set_selection_mode(int mode)
1476 this->selection_mode = mode;
1480 void BC_ListBox::delete_columns()
1484 for(int i = 0; i < columns; i++)
1486 delete [] column_titles[i];
1488 delete [] column_titles;
1491 if(column_width) delete [] column_width;
1497 // Need to copy titles so EDL can change
1498 void BC_ListBox::set_columns(const char **column_titles,
1502 if((!column_titles && column_width) ||
1503 (column_titles && !column_width))
1505 printf("BC_ListBox::set_columns either column_titles or column_width == NULL but not both.\n");
1514 this->column_titles = new char*[columns];
1515 for(int i = 0; i < columns; i++)
1517 this->column_titles[i] = new char[strlen(column_titles[i]) + 1];
1518 strcpy(this->column_titles[i], column_titles[i]);
1524 this->column_width = new int[columns];
1525 for(int i = 0; i < columns; i++)
1527 this->column_width[i] = column_width[i];
1531 this->columns = columns;
1536 int BC_ListBox::update(ArrayList<BC_ListBoxItem*> *data,
1537 const char **column_titles,
1542 int highlighted_number,
1543 int recalc_positions,
1546 set_columns(column_titles,
1552 this->yposition = yposition;
1553 this->xposition = xposition;
1554 this->highlighted_item = highlighted_number;
1555 this->highlighted_ptr = index_to_item(data, highlighted_number, 0);
1557 if(recalc_positions)
1558 set_autoplacement(data, 1, 1);
1560 init_column_width();
1565 update_scrollbars(1);
1571 void BC_ListBox::center_selection()
1573 int selection = get_selection_number(0, 0);
1575 calculate_item_coords();
1576 center_selection(selection);
1582 update_scrollbars(0);
1583 gui->show_window(1);
1587 void BC_ListBox::move_vertical(int pixels)
1591 void BC_ListBox::move_horizontal(int pixels)
1595 int BC_ListBox::select_previous(int skip,
1596 BC_ListBoxItem *selected_item,
1598 ArrayList<BC_ListBoxItem*> *data,
1604 selected_item = get_selection(0, 0);
1616 got_second = &temp3;
1620 // Scan backwards to item pointer. Then count visible items to get
1621 // destination. No wraparound.
1624 for(int i = data[master_column].total - 1; i >= 0; i--)
1626 BC_ListBoxItem *current_item = data[master_column].values[i];
1627 if(current_item->get_sublist() &&
1628 current_item->get_expand())
1630 int result = select_previous(skip,
1633 current_item->get_sublist(),
1645 if((*counter) >= skip)
1647 for(int j = 0; j < columns; j++)
1648 data[j].values[i]->selected = 1;
1650 return item_to_index(this->data, current_item);
1655 if(current_item->selected)
1657 for(int j = 0; j < columns; j++)
1658 data[j].values[i]->selected = 0;
1665 // Hit top of top level without finding a selected item.
1668 // Select first item in top level and quit
1669 BC_ListBoxItem *current_item;
1671 current_item = data[master_column].values[0];
1673 for(int j = 0; j < columns; j++)
1674 data[j].values[0]->selected = 1;
1676 return item_to_index(this->data, current_item);
1678 }while(top_level && data[master_column].total);
1682 int BC_ListBox::select_next(int skip,
1683 BC_ListBoxItem *selected_item,
1685 ArrayList<BC_ListBoxItem*> *data,
1691 selected_item = get_selection(0, 0);
1703 got_second = &temp3;
1707 // Scan forwards to currently selected item pointer.
1708 // Then count visible items to get destination. No wraparound.
1711 for(int i = 0; i < data[master_column].total; i++)
1713 BC_ListBoxItem *current_item = data[master_column].values[i];
1715 // Select next item once the number items after the currently selected item
1716 // have been passed.
1720 if((*counter) >= skip)
1722 for(int j = 0; j < columns; j++)
1723 data[j].values[i]->selected = 1;
1725 return item_to_index(this->data, current_item);
1730 // Got currently selected item. Deselect it.
1731 if(current_item->selected)
1733 for(int j = 0; j < columns; j++)
1734 data[j].values[i]->selected = 0;
1740 // Descend into expanded level
1741 if(current_item->get_sublist() &&
1742 current_item->get_expand())
1744 int result = select_next(skip,
1747 current_item->get_sublist(),
1757 // Hit bottom of top level without finding the next item.
1760 BC_ListBoxItem *current_item;
1761 // Select first item in top level and quit
1765 current_item = data[master_column].values[0];
1767 for(int j = 0; j < columns; j++)
1768 data[j].values[0]->selected = 1;
1773 // Select last item in top level and quit
1775 int current_row = data[master_column].total - 1;
1776 current_item = data[master_column].values[current_row];
1778 for(int j = 0; j < columns; j++)
1779 data[j].values[current_row]->selected = 1;
1783 return item_to_index(this->data, current_item);
1785 }while(top_level && data[master_column].total);
1791 void BC_ListBox::clamp_positions()
1793 items_w = get_items_width();
1794 items_h = get_items_height(data, columns);
1796 if(yposition < 0) yposition = 0;
1798 if(yposition > items_h - view_h)
1799 yposition = items_h - view_h;
1801 if(yposition < 0) yposition = 0;
1803 if(xposition < 0) xposition = 0;
1805 if(xposition >= items_w - view_w)
1806 xposition = items_w - view_w;
1808 if(xposition < 0) xposition = 0;
1811 int BC_ListBox::center_selection(int selection,
1812 ArrayList<BC_ListBoxItem*> *data,
1816 if(!data) data = this->data;
1817 if(!counter) counter = &temp;
1819 for(int i = 0; i < data[master_column].total; i++)
1824 BC_ListBoxItem *item = data[master_column].values[i];
1825 if((*counter) == selection)
1827 BC_ListBoxItem *top_item = this->data[master_column].values[0];
1830 if(display_format == LISTBOX_ICONS)
1832 // Icon is out of window
1833 if( item->icon_y-yposition > view_h-get_text_h(item) ||
1834 item->icon_y-yposition < 0 ) {
1835 yposition = item->icon_y - view_h / 2;
1838 if(data[master_column].values[selection]->icon_x - xposition > view_w ||
1839 data[master_column].values[selection]->icon_x - xposition < 0)
1841 xposition = item->icon_x - view_w / 2;
1846 // Text coordinate is out of window
1847 if( item->text_y-yposition > view_h-get_text_h(item) ||
1848 item->text_y-yposition < 0 ) {
1849 yposition = item->text_y -
1858 if(item->get_sublist())
1860 int result = center_selection(selection,
1861 item->get_sublist(),
1863 if(result) return result;
1869 void BC_ListBox::update_scrollbars(int flush)
1871 int h_needed = items_h = get_items_height(data, columns);
1872 int w_needed = items_w = get_items_width();
1874 // if(columns > 0 && column_width)
1875 // printf("BC_ListBox::update_scrollbars 1 %d %d\n", column_width[columns - 1], w_needed);
1879 if(xposition != xscrollbar->get_value())
1880 xscrollbar->update_value(xposition);
1882 if(w_needed != xscrollbar->get_length() ||
1883 view_w != xscrollbar->get_handlelength())
1884 xscrollbar->update_length(w_needed, xposition, view_w, 0);
1889 if(yposition != yscrollbar->get_value())
1890 yscrollbar->update_value(yposition);
1892 if(h_needed != yscrollbar->get_length() || view_h != yscrollbar->get_handlelength())
1893 yscrollbar->update_length(h_needed, yposition, view_h, 0);
1896 if(flush) this->flush();
1899 int BC_ListBox::get_scrollbars()
1901 int h_needed = items_h = get_items_height(data, columns);
1902 int w_needed = items_w = get_items_width();
1906 title_h = get_title_h();
1908 view_h = popup_h - title_h - 4;
1909 view_w = popup_w - 4;
1911 // Create scrollbars as needed
1912 for(int i = 0; i < 2; i++)
1914 if(w_needed > view_w)
1919 get_resources()->hscroll_data[SCROLL_HANDLE_UP]->get_h() -
1927 if(h_needed > view_h)
1931 get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w() -
1940 // Update subwindow size
1941 int new_w = popup_w;
1942 int new_h = popup_h;
1943 if(need_xscroll) new_h -= get_resources()->hscroll_data[SCROLL_HANDLE_UP]->get_h();
1944 if(need_yscroll) new_w -= get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w();
1947 if(new_w != BC_WindowBase::get_w() || new_h != BC_WindowBase::get_h())
1948 gui->resize_window(new_w, new_h);
1950 BC_WindowBase *destination = (is_popup ? gui : parent_window);
1955 xscrollbar = new BC_ListBoxXScroll(this);
1956 destination->add_subwindow(xscrollbar);
1957 xscrollbar->show_window(0);
1958 xscrollbar->bound_to = this;
1962 xscrollbar->update_length(w_needed, xposition, view_w, flush);
1963 xscrollbar->reposition_window(get_xscroll_x(),
1965 get_xscroll_width());
1970 if(xscrollbar) delete xscrollbar;
1979 yscrollbar = new BC_ListBoxYScroll(this);
1980 destination->add_subwindow(yscrollbar);
1981 yscrollbar->show_window(0);
1982 yscrollbar->bound_to = this;
1986 yscrollbar->update_length(h_needed, yposition, view_h, flush);
1987 yscrollbar->reposition_window(get_yscroll_x(),
1989 get_yscroll_height());
1994 if(yscrollbar) delete yscrollbar;
2000 view_w + 4 != bg_surface->get_w() ||
2001 view_h + 4 != bg_surface->get_h())
2003 if(bg_surface) delete bg_surface;
2004 bg_surface = new BC_Pixmap(gui, view_w + 4, view_h + 4);
2014 void BC_ListBox::set_drag_scroll(int value)
2016 allow_drag_scroll = value;
2020 // Test for scrolling by dragging
2022 int BC_ListBox::test_drag_scroll(int cursor_x, int cursor_y)
2025 if(allow_drag_scroll ||
2026 current_operation == SELECT_RECT)
2029 int top_boundary = get_title_h();
2031 if(cursor_y < top_boundary ||
2032 cursor_y >= view_h + title_h + LISTBOX_BORDER * 2 ||
2033 cursor_x < LISTBOX_BORDER ||
2034 cursor_x >= view_w + LISTBOX_BORDER)
2042 int BC_ListBox::drag_scroll_event()
2044 int top_boundary = get_title_h();
2047 if(get_cursor_y() < top_boundary)
2049 yposition -= top_boundary - get_cursor_y();
2053 if(get_cursor_y() >= view_h + title_h + 4)
2055 yposition += get_cursor_y() - (view_h + title_h + 4);
2059 if(get_cursor_x() < 2)
2061 xposition -= 2 - get_cursor_x();
2065 if(get_cursor_x() >= view_w + 2)
2067 xposition += get_cursor_x() - (view_w + 2);
2070 if(result) clamp_positions();
2074 int BC_ListBox::rectangle_scroll_event()
2076 int old_xposition = xposition;
2077 int old_yposition = yposition;
2078 int result = drag_scroll_event();
2082 rect_x1 += old_xposition - xposition;
2083 rect_y1 += old_yposition - yposition;
2084 rect_x2 = get_cursor_x();
2085 rect_y2 = get_cursor_y();
2087 int x1 = MIN(rect_x1, rect_x2);
2088 int x2 = MAX(rect_x1, rect_x2);
2089 int y1 = MIN(rect_y1, rect_y2);
2090 int y2 = MAX(rect_y1, rect_y2);
2092 if(select_rectangle(data,
2098 selection_changed();
2103 update_scrollbars(1);
2108 int BC_ListBox::select_scroll_event()
2110 int result = drag_scroll_event();
2114 highlighted_item = selection_number = get_cursor_item(data,
2120 update_scrollbars(1);
2121 selection_changed();
2126 int BC_ListBox::select_rectangle(ArrayList<BC_ListBoxItem*> *data,
2133 for(int i = 0; i < data[master_column].total; i++)
2135 for(int j = 0; j < columns; j++)
2137 BC_ListBoxItem *item = data[j].values[i];
2138 if(display_format == LISTBOX_ICONS)
2140 int icon_x, icon_y, icon_w, icon_h;
2141 int text_x, text_y, text_w, text_h;
2142 get_icon_mask(item, icon_x, icon_y, icon_w, icon_h);
2143 get_text_mask(item, text_x, text_y, text_w, text_h);
2145 if((x2 >= icon_x && x1 < icon_x + icon_w &&
2146 y2 >= icon_y && y1 < icon_y + icon_h) ||
2147 (x2 >= text_x && x1 < text_x + text_w &&
2148 y2 >= text_y && y1 < text_y + text_h))
2169 gui->get_w() - get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w() :
2172 y1 < gui->get_h() &&
2173 y2 >= get_item_y(item) &&
2174 y1 < get_item_y(item) + get_item_h(item))
2193 BC_ListBoxItem *item = data[master_column].values[i];
2194 if(item->get_sublist() &&
2196 result |= select_rectangle(item->get_sublist(),
2205 int BC_ListBox::reposition_item(ArrayList<BC_ListBoxItem*> *data,
2206 int selection_number,
2212 if(!counter) counter = &temp;
2215 for(int i = 0; i < data[master_column].total; i++)
2217 BC_ListBoxItem *item = data[master_column].values[i];
2219 if((*counter) == selection_number)
2225 // Not recursive because it's only used for icons
2230 void BC_ListBox::move_selection(ArrayList<BC_ListBoxItem*> *dst,
2231 ArrayList<BC_ListBoxItem*> *src)
2233 for(int i = 0; i < src[master_column].total; i++)
2235 BC_ListBoxItem *item = src[master_column].values[i];
2240 for(int j = 0; j < columns; j++)
2242 dst[j].append(src[j].values[i]);
2243 src[j].remove_number(i);
2247 // Descend into sublist
2248 if(item->get_sublist())
2251 item->get_sublist());
2256 int BC_ListBox::put_selection(ArrayList<BC_ListBoxItem*> *data,
2257 ArrayList<BC_ListBoxItem*> *src,
2262 if(!counter) counter = &temp;
2266 for(int j = 0; j < columns; j++)
2268 for(int i = 0; i < src[j].total; i++)
2270 data[j].append(src[j].values[i]);
2276 for(int i = 0; i < data[master_column].total; i++)
2279 if((*counter) == destination)
2281 for(int j = 0; j < columns; j++)
2283 for(int k = 0; k < src[j].total; k++)
2285 data[j].insert(src[j].values[k], destination + k);
2291 BC_ListBoxItem *item = data[master_column].values[i];
2292 if(item->get_sublist())
2294 if(put_selection(item->get_sublist(),
2306 int BC_ListBox::item_to_index(ArrayList<BC_ListBoxItem*> *data,
2307 BC_ListBoxItem *item,
2311 if(!counter) counter = &temp;
2313 for(int i = 0; i < data[master_column].total; i++)
2316 for(int j = 0; j < columns; j++)
2318 BC_ListBoxItem *new_item = data[j].values[i];
2319 //printf("BC_ListBox::item_to_index 1 %d %d %p\n", j, i, new_item);
2320 if(new_item == item)
2326 BC_ListBoxItem *new_item = data[master_column].values[i];
2327 if(new_item->get_sublist())
2329 if(item_to_index(new_item->get_sublist(),
2339 BC_ListBoxItem* BC_ListBox::index_to_item(ArrayList<BC_ListBoxItem*> *data,
2345 if(!counter) counter = &temp;
2346 for(int i = 0; i < data[master_column].total; i++)
2349 if((*counter) == number)
2351 return data[column].values[i];
2353 BC_ListBoxItem *item = data[master_column].values[i];
2354 if(item->get_sublist())
2356 BC_ListBoxItem *result = index_to_item(item->get_sublist(),
2360 if(result) return result;
2366 int BC_ListBox::get_cursor_item(ArrayList<BC_ListBoxItem*> *data,
2369 BC_ListBoxItem **item_return,
2374 if(!data) return -1;
2375 if(!counter) counter = &temp;
2377 // Icons are not treed
2378 if(display_format == LISTBOX_ICONS)
2380 for(int j = data[master_column].total - 1; j >= 0; j--)
2382 int icon_x, icon_y, icon_w, icon_h;
2383 int text_x, text_y, text_w, text_h;
2384 BC_ListBoxItem *item = data[master_column].values[j];
2385 get_icon_mask(item, icon_x, icon_y, icon_w, icon_h);
2386 get_text_mask(item, text_x, text_y, text_w, text_h);
2388 if((cursor_x >= icon_x && cursor_x < icon_x + icon_w &&
2389 cursor_y >= icon_y && cursor_y < icon_y + icon_h) ||
2390 (cursor_x >= text_x && cursor_x < text_x + text_w &&
2391 cursor_y >= text_y && cursor_y < text_y + text_h))
2393 if(item_return) (*item_return) = item;
2400 // Cursor is inside items rectangle
2402 cursor_x < (yscrollbar ?
2403 gui->get_w() - get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w() :
2405 // Only clamp y if we're not in a SELECT operation.
2406 (current_operation == BC_ListBox::SELECT ||
2407 (cursor_y > get_title_h() + LISTBOX_BORDER &&
2408 cursor_y < gui->get_h())))
2410 // Search table for cursor obstruction
2411 for(int i = 0; i < data[master_column].total; i++)
2413 BC_ListBoxItem *item = data[master_column].values[i];
2416 // Cursor is inside item on current level
2419 cursor_y >= get_item_y(item) &&
2420 cursor_y < get_item_y(item) + get_item_h(item))
2422 //printf("BC_ListBox::get_cursor_item %d %d %p\n", master_column, i, item);
2423 if(item_return) (*item_return) = item;
2427 // Descend into sublist
2428 if(item->get_sublist())
2430 if(get_cursor_item(item->get_sublist(),
2435 item->get_expand()) >= 0)
2444 int BC_ListBox::repeat_event(int64_t duration)
2446 switch(current_operation)
2448 // Repeat out of bounds selection
2450 if(duration == get_resources()->scroll_repeat)
2451 return rectangle_scroll_event();
2455 if(duration == get_resources()->scroll_repeat)
2456 return select_scroll_event();
2461 if(button_highlighted && is_popup &&
2462 tooltip_text && tooltip_text[0] != 0 &&
2463 duration == get_resources()->tooltip_delay)
2474 int BC_ListBox::cursor_enter_event()
2478 switch(current_operation)
2480 // Cursor moved over button, pressed, and exited.
2481 case BUTTON_DOWN_SELECT:
2482 if(top_level->event_win == win)
2484 current_operation = BUTTON_DN;
2486 button_highlighted = 1;
2492 // Cursor entered button
2493 if(is_popup && top_level->event_win == win)
2495 button_highlighted = 1;
2500 // TODO: Need to get the highlighted column title or item
2501 if(gui && top_level->event_win == gui->win)
2503 list_highlighted = 1;
2513 int BC_ListBox::cursor_leave_event()
2515 if(current_operation == COLUMN_DRAG) return 0;
2518 if(button_highlighted)
2520 button_highlighted = 0;
2525 if(list_highlighted)
2527 list_highlighted = 0;
2528 highlighted_item = -1;
2529 highlighted_ptr = 0;
2530 highlighted_title = -1;
2531 int redraw_toggles = 0;
2532 for(int i = 0; i < expanders.total; i++)
2533 expanders.values[i]->cursor_leave_event(&redraw_toggles);
2541 int BC_ListBox::get_first_selection(ArrayList<BC_ListBoxItem*> *data, int *result)
2544 if(!result) result = &temp;
2546 for(int i = 0; i < data[master_column].total; i++)
2548 BC_ListBoxItem *item = data[master_column].values[i];
2550 if(item->selected) return (*result);
2551 if(item->get_sublist())
2553 if(get_first_selection(item->get_sublist(), result) >= 0)
2560 int BC_ListBox::get_total_items(ArrayList<BC_ListBoxItem*> *data,
2565 if(!result) result = &temp;
2567 for(int i = 0; i < data[master_column].total; i++)
2570 if(data[master_column].values[i]->get_sublist())
2571 get_total_items(data[master_column].values[i]->get_sublist(),
2580 int BC_ListBox::get_last_selection(ArrayList<BC_ListBoxItem*> *data,
2591 for(int i = data[master_column].total - 1; i >= 0; i--)
2593 BC_ListBoxItem *item = data[master_column].values[i];
2598 return get_total_items(data, 0, master_column) - (*result) /* - 1 */;
2603 if(item->get_sublist())
2605 if(get_last_selection(item->get_sublist(), result) >= 0)
2608 return get_total_items(data, 0, master_column) - (*result) /* - 1 */;
2617 void BC_ListBox::select_range(ArrayList<BC_ListBoxItem*> *data,
2623 if(!current) current = &temp;
2625 for(int i = 0; i < data[master_column].total; i++)
2628 if((*current) >= start && (*current) < end)
2630 for(int j = 0; j < columns; j++)
2631 data[j].values[i]->selected = 1;
2633 BC_ListBoxItem *item = data[master_column].values[i];
2634 if(item->get_sublist())
2635 select_range(item->get_sublist(),
2643 // Fill items between current selection and new selection
2644 int BC_ListBox::expand_selection(int button_press, int selection_number)
2646 int old_selection_start = selection_start;
2647 int old_selection_end = selection_end;
2649 // printf("BC_ListBox::expand_selection %d %d\n",
2650 // selection_center,
2651 // selection_number);
2653 // Calculate the range to select based on selection_center and selection_number
2654 if(selection_number < selection_center)
2656 selection_start = selection_number;
2660 selection_end = selection_number + 1;
2663 //printf("BC_ListBox::expand_selection %d %d %d %d\n", old_selection_start, old_selection_end, selection_start, selection_end);
2664 // Recurse through all the items and select the desired range
2665 select_range(data, selection_start, selection_end);
2668 return (old_selection_start != selection_start ||
2669 old_selection_end != selection_end);
2672 int BC_ListBox::toggle_item_selection(ArrayList<BC_ListBoxItem*> *data,
2673 int selection_number,
2677 if(!counter) counter = &temp;
2679 for(int i = 0; i < data[master_column].total; i++)
2681 BC_ListBoxItem *item = data[master_column].values[i];
2683 if((*counter) == selection_number)
2685 // Get new value for selection
2686 int selected = !item->selected;
2688 for(int j = 0; j < columns; j++)
2689 data[j].values[i]->selected = selected;
2693 // Descend into sublist
2694 if(item->get_sublist())
2696 if(toggle_item_selection(item->get_sublist(),
2707 void BC_ListBox::set_all_selected(ArrayList<BC_ListBoxItem*> *data, int value)
2709 for(int i = 0; i < data[master_column].total; i++)
2711 for(int j = 0; j < columns; j++)
2713 BC_ListBoxItem *item = data[j].values[i];
2714 item->selected = value;
2716 BC_ListBoxItem *item = data[master_column].values[i];
2717 if(item->get_sublist())
2719 set_all_selected(item->get_sublist(), value);
2724 void BC_ListBox::set_selected(ArrayList<BC_ListBoxItem*> *data,
2730 if(!counter) counter = &temp;
2731 for(int i = 0; i < data[master_column].total && (*counter) != item_number; i++)
2734 if((*counter) == item_number)
2736 for(int j = 0; j < columns; j++)
2738 BC_ListBoxItem *item = data[j].values[i];
2739 item->selected = value;
2744 BC_ListBoxItem *item = data[master_column].values[i];
2745 if(item->get_sublist())
2747 set_selected(item->get_sublist(),
2755 int BC_ListBox::update_selection(ArrayList<BC_ListBoxItem*> *data,
2756 int selection_number,
2761 if(!counter) counter = &temp;
2763 for(int i = 0; i < data[master_column].total; i++)
2765 BC_ListBoxItem *item = data[master_column].values[i];
2767 if((*counter) == selection_number && !item->selected)
2770 for(int j = 0; j < columns; j++)
2771 data[j].values[i]->selected = 1;
2774 if((*counter) != selection_number && item->selected)
2777 for(int j = 0; j < columns; j++)
2778 data[j].values[i]->selected = 0;
2780 if(item->get_sublist())
2781 result |= update_selection(item->get_sublist(),
2788 void BC_ListBox::promote_selections(ArrayList<BC_ListBoxItem*> *data,
2792 for(int i = 0; i < data[master_column].total; i++)
2794 for(int j = 0; j < columns; j++)
2796 BC_ListBoxItem *item = data[j].values[i];
2797 if(item->selected == old_value) item->selected = new_value;
2799 BC_ListBoxItem *item = data[master_column].values[i];
2800 if(item->get_sublist())
2801 promote_selections(item->get_sublist(), old_value, new_value);
2805 int BC_ListBox::focus_out_event()
2811 int BC_ListBox::button_press_event()
2814 BC_ListBoxItem *current_item = 0;
2816 int do_selection_change = 0;
2817 const int debug = 0;
2820 if(debug) printf("BC_ListBox::button_press_event %d this=%p event_win=%p %p %p %p\n",
2823 (void*)top_level->event_win,
2824 (void*)(gui ? gui->win : 0),
2826 (void*)parent_window->win);
2828 // Pressed in button
2829 if(is_popup && top_level->event_win == win)
2831 if(debug) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2832 current_operation = BUTTON_DN;
2836 if(!active && !disabled)
2838 top_level->deactivate();
2845 // Pressed in scrollbar
2846 if((xscrollbar && top_level->event_win == xscrollbar->win) ||
2847 (yscrollbar && top_level->event_win == yscrollbar->win))
2849 if(debug) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2854 if(gui && top_level->event_win == gui->win)
2856 if(debug) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2858 // Activate list items
2859 // If it is a suggestion popup, it is visible without being active
2862 if(debug) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2863 if(!is_suggestions) top_level->deactivate();
2864 if(debug) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2866 if(debug) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2869 // Wheel mouse pressed
2870 if(get_buttonpress() == 4)
2872 if(current_operation == NO_OPERATION)
2874 current_operation = WHEEL;
2877 set_yposition(yposition - gui->get_h() / 10, 0);
2879 update_scrollbars(0);
2880 highlighted_ptr = 0;
2881 highlighted_item = get_cursor_item(data,
2882 top_level->cursor_x,
2883 top_level->cursor_y,
2891 if(get_buttonpress() == 5)
2893 if(current_operation == NO_OPERATION)
2895 current_operation = WHEEL;
2898 set_yposition(yposition + gui->get_h() / 10, 0);
2900 update_scrollbars(0);
2901 highlighted_ptr = 0;
2902 highlighted_item = get_cursor_item(data,
2903 top_level->cursor_x,
2904 top_level->cursor_y,
2912 // Pressed over column title division
2913 if(test_column_divisions(gui->get_cursor_x(),
2914 gui->get_cursor_y(),
2917 drag_cursor_x = gui->get_cursor_x() + xposition;
2919 drag_column_w = column_width[highlighted_division - 1];
2921 drag_column_w = default_column_width[highlighted_division - 1];
2923 current_operation = DRAG_DIVISION;
2927 // Pressed in column title
2928 if(test_column_titles(gui->get_cursor_x(), gui->get_cursor_y()))
2930 current_operation = COLUMN_DN;
2931 button_highlighted = 0;
2932 list_highlighted = 1;
2937 // Pressed in expander
2938 if(test_expanders())
2940 current_operation = EXPAND_DN;
2941 // Need to redraw items because of alpha
2946 // Pressed over item
2947 if((selection_number = get_cursor_item(data,
2948 gui->get_cursor_x(),
2949 gui->get_cursor_y(),
2950 ¤t_item)) >= 0)
2952 if(debug) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2954 // Get item button was pressed over
2955 selection_number2 = selection_number1;
2956 selection_number1 = selection_number;
2958 selection_start = -1;
2962 // Multiple item selection is possible
2963 if(selection_mode == LISTBOX_MULTIPLE &&
2964 (ctrl_down() || shift_down()))
2966 // Expand text selection.
2967 // Fill items between selected region and current item.
2969 (display_format == LISTBOX_TEXT ||
2970 display_format == LISTBOX_ICON_LIST))
2972 // Get first item selected
2973 selection_start = get_first_selection(data);
2974 // Get last item selected
2975 selection_end = get_last_selection(data);
2976 // Get center of selected region
2977 if(selection_end > selection_start)
2979 selection_center = (selection_end + selection_start) >> 1;
2983 selection_center = selection_number;
2987 // Deselect everything.
2988 set_all_selected(data, 0);
2989 // Select just the items
2990 expand_selection(1, selection_number);
2994 // Toggle a single item on or off
2996 toggle_item_selection(data, selection_number);
2997 new_value = current_item->selected;
3001 // Select single item
3003 if(!current_item->selected)
3005 set_all_selected(data, 0);
3014 current_operation = SELECT;
3015 highlighted_item = selection_number;
3016 highlighted_ptr = current_item;
3017 button_highlighted = 0;
3018 list_highlighted = 1;
3021 do_selection_change = 1;
3026 // Pressed over nothing. Start rectangle selection.
3028 if(get_buttonpress() == 1 &&
3029 selection_mode == LISTBOX_MULTIPLE)
3033 // Deselect all and redraw if anything was selected
3034 if(get_selection_number(0, 0) >= 0)
3036 set_all_selected(data, 0);
3038 do_selection_change = 1;
3044 // Promote selections to protect from a rectangle selection
3045 promote_selections(data, 1, 2);
3048 // Start rectangle selection
3049 current_operation = SELECT_RECT;
3050 rect_x1 = rect_x2 = get_cursor_x();
3051 rect_y1 = rect_y2 = get_cursor_y();
3059 // Suggestion box is not active but visible, so lie to get it to deactivate
3060 if(is_popup && (active || (is_suggestions && gui)))
3063 if(debug) printf("BC_ListBox::button_press_event %d\n", __LINE__);
3069 if(do_selection_change) selection_changed();
3070 if(debug) printf("BC_ListBox::button_press_event %d %d\n",
3077 int BC_ListBox::button_release_event()
3080 int cursor_x, cursor_y;
3084 //printf("BC_ListBox::button_release_event 1 %d\n", current_operation);
3085 switch(current_operation)
3088 current_operation = NO_OPERATION;
3093 current_operation = NO_OPERATION;
3097 // Release item selection
3098 case BUTTON_DOWN_SELECT:
3100 //printf("BC_ListBox::button_release_event 10\n");
3101 unset_repeat(get_resources()->scroll_repeat);
3102 current_operation = NO_OPERATION;
3104 translate_coordinates(top_level->event_win, gui->win,
3105 gui->get_cursor_x(), gui->get_cursor_y(),
3106 &cursor_x, &cursor_y);
3107 selection_number1 = selection_number =
3108 get_cursor_item(data, cursor_x, cursor_y);
3109 //printf("BC_ListBox::button_release_event %d %d\n", selection_number2, selection_number1);
3115 if(selection_number >= 0)
3121 // Second button release outside button
3122 if(button_releases > 1)
3129 if(top_level->get_double_click() &&
3130 selection_number2 == selection_number1 &&
3131 selection_number2 >= 0 &&
3132 selection_number1 >= 0)
3142 unset_repeat(get_resources()->scroll_repeat);
3145 // Demote selections from rectangle selection
3146 promote_selections(data, 2, 1);
3149 // Hide rectangle overlay
3151 current_operation = NO_OPERATION;
3155 // Release popup button
3158 current_operation = NO_OPERATION;
3162 // Second button release inside button
3163 if(button_releases > 1)
3171 current_operation = NO_OPERATION;
3172 // Update the sort column and the sort order for the user only if the existing
3173 // sort column is valid.
3174 if(sort_column >= 0)
3176 // Invert order only if column is the same
3177 if(highlighted_title == sort_column)
3179 (sort_order == SORT_ASCENDING) ?
3182 // Set the new sort column
3183 sort_column = highlighted_title;
3184 if(!sort_order_event())
3190 // Sorting not enabled. Redraw the title state.
3199 int redraw_toggles = 0;
3200 for(int i = 0; i < expanders.total && !result; i++)
3202 if(expanders.values[i]->button_release_event(&redraw_toggles))
3207 // Need to redraw items because of alpha
3208 if(redraw_toggles) draw_items(1);
3209 current_operation = NO_OPERATION;
3214 // Can't default to NO_OPERATION because it may be used in a drag event.
3219 if(do_event) handle_event();
3221 //printf("BC_ListBox::button_release_event %d %d\n", __LINE__, get_window_lock());
3225 int BC_ListBox::get_title_h()
3227 if(display_format == LISTBOX_TEXT ||
3228 display_format == LISTBOX_ICON_LIST)
3229 return column_titles ? column_bg[0]->get_h() : 0;
3234 void BC_ListBox::reset_cursor(int new_cursor)
3238 if(gui->get_cursor() != new_cursor)
3240 gui->set_cursor(new_cursor, 0, 0);
3244 if(get_cursor() != new_cursor)
3246 set_cursor(new_cursor, 0, 0);
3250 int BC_ListBox::test_column_divisions(int cursor_x, int cursor_y, int &new_cursor)
3255 cursor_y < get_title_h() &&
3257 cursor_x < gui->get_w())
3259 for(int i = 1; i < columns; i++)
3261 if(cursor_x >= -xposition + get_column_offset(i) - 5 &&
3262 cursor_x < -xposition + get_column_offset(i) +
3263 get_resources()->listbox_title_hotspot)
3265 highlighted_item = -1;
3266 highlighted_ptr = 0;
3267 highlighted_division = i;
3268 highlighted_title = -1;
3269 list_highlighted = 1;
3270 new_cursor = HSEPARATE_CURSOR;
3275 highlighted_division = -1;
3279 int BC_ListBox::test_column_titles(int cursor_x, int cursor_y)
3284 cursor_y < get_title_h() &&
3286 cursor_x < gui->get_w())
3288 for(int i = 0; i < columns; i++)
3290 if(cursor_x >= -xposition + get_column_offset(i) &&
3291 (cursor_x < -xposition + get_column_offset(i + 1) ||
3294 highlighted_item = -1;
3295 highlighted_ptr = 0;
3296 highlighted_division = -1;
3297 highlighted_title = i;
3298 list_highlighted = 1;
3303 highlighted_title = -1;
3307 int BC_ListBox::test_expanders()
3309 for(int i = 0; i < expanders.total; i++)
3311 if(expanders.values[i]->button_press_event())
3313 current_operation = EXPAND_DN;
3321 int BC_ListBox::cursor_motion_event()
3323 int redraw = 0, result = 0;
3324 int new_cursor = ARROW_CURSOR;
3326 selection_number = -1;
3329 switch(current_operation)
3332 // Button pressed and slid off button
3333 if(!cursor_inside())
3335 current_operation = BUTTON_DOWN_SELECT;
3343 // int new_w = get_cursor_x() +
3345 // get_column_offset(highlighted_division - 1);
3346 int difference = get_cursor_x() + xposition - drag_cursor_x;
3347 int new_w = drag_column_w + difference;
3349 new_cursor = HSEPARATE_CURSOR;
3353 column_width[highlighted_division - 1] = new_w;
3357 default_column_width[highlighted_division - 1] = new_w;
3360 column_width_boundaries();
3362 // Force update of coords
3363 set_autoplacement(data, 0, 1);
3364 column_resize_event();
3368 update_scrollbars(1);
3375 if(test_drag_scroll(get_cursor_x(), get_cursor_y()))
3377 set_repeat(get_resources()->scroll_repeat);
3380 int old_x1 = MIN(rect_x1, rect_x2);
3381 int old_x2 = MAX(rect_x1, rect_x2);
3382 int old_y1 = MIN(rect_y1, rect_y2);
3383 int old_y2 = MAX(rect_y1, rect_y2);
3385 int new_rect_x2 = get_cursor_x();
3386 int new_rect_y2 = get_cursor_y();
3388 int x1 = MIN(rect_x1, new_rect_x2);
3389 int x2 = MAX(rect_x1, new_rect_x2);
3390 int y1 = MIN(rect_y1, new_rect_y2);
3391 int y2 = MAX(rect_y1, new_rect_y2);
3393 // Adjust rectangle coverage
3401 redraw = select_rectangle(data,
3411 //printf("BC_ListBox::cursor_motion_event %d\n", __LINE__);
3416 rect_x2 = get_cursor_x();
3417 rect_y2 = get_cursor_y();
3422 update_scrollbars(1);
3423 selection_changed();
3424 //printf("BC_ListBox::cursor_motion_event %d\n", __LINE__);
3432 //printf("BC_ListBox::cursor_motion_event %d\n", __LINE__);
3442 int old_highlighted_item = highlighted_item;
3444 if(test_drag_scroll(get_cursor_x(),
3447 set_repeat(get_resources()->scroll_repeat);
3451 highlighted_item = selection_number = get_cursor_item(data,
3457 // Deselect all items and select just the one we're over
3458 if(selection_number >= 0 &&
3462 selection_mode == LISTBOX_SINGLE))
3464 redraw = update_selection(data, selection_number);
3467 if(selection_mode == LISTBOX_MULTIPLE &&
3468 (shift_down() || ctrl_down()))
3469 // Expand multiple selection
3471 // Expand selected region in text mode centered around initial range
3472 if((display_format == LISTBOX_TEXT ||
3473 display_format == LISTBOX_ICON_LIST) &&
3476 // Deselect everything.
3477 set_all_selected(data, 0);
3479 // Select just the items
3480 redraw = expand_selection(0, selection_number);
3483 // Set the one item we're over to the selection value determined in
3484 // button_press_event.
3492 if(highlighted_item != old_highlighted_item)
3496 update_scrollbars(1);
3497 //printf("BC_ListBox::cursor_motion_event %d %d\n", highlighted_item, old_highlighted_item);
3498 selection_changed();
3503 case BUTTON_DOWN_SELECT:
3504 // Went back into button area
3507 current_operation = BUTTON_DN;
3512 // Went into item area
3515 int cursor_x = 0, cursor_y = 0;
3516 translate_coordinates(top_level->event_win,
3518 top_level->cursor_x,
3519 top_level->cursor_y,
3522 int old_highlighted_item = highlighted_item;
3523 highlighted_item = selection_number = get_cursor_item(data,
3528 if(highlighted_item != old_highlighted_item)
3530 update_selection(data, selection_number);
3532 selection_changed();
3539 int redraw_toggles = 0;
3540 for(int i = 0; i < expanders.total && !result; i++)
3542 result = expanders.values[i]->cursor_motion_event(
3547 // Need to redraw items because of the alpha
3555 int cursor_x = get_cursor_x(), cursor_y = get_cursor_y();
3556 if(gui && top_level->event_win == gui->win)
3558 int old_highlighted_title = highlighted_title;
3559 int old_list_highlighted = list_highlighted;
3560 int old_highlighted_item = highlighted_item;
3561 int redraw_titles = 0;
3562 int redraw_border = 0;
3563 int redraw_items = 0;
3564 int redraw_toggles = 0;
3568 // Test if cursor moved over a title division
3569 test_column_divisions(cursor_x, cursor_y, new_cursor);
3571 // Test if cursor moved over a title
3572 if(highlighted_division < 0)
3574 test_column_titles(cursor_x, cursor_y);
3577 // Test if cursor moved over expander
3578 if(highlighted_division < 0 &&
3579 highlighted_title < 0 &&
3580 (display_format == LISTBOX_TEXT ||
3581 display_format == LISTBOX_ICON_LIST))
3583 for(int i = 0; i < expanders.total; i++)
3585 expanders.values[i]->cursor_motion_event(
3588 //printf("BC_ListBox::cursor_motion_event %d\n", redraw_toggles);
3591 // Test if cursor moved over an item
3592 if(highlighted_division < 0 &&
3593 highlighted_title < 0)
3595 highlighted_item = get_cursor_item(data,
3602 // Clear title highlighting if moved over division
3603 if(old_highlighted_title != highlighted_title)
3608 // Highlight list border
3609 if(old_list_highlighted != list_highlighted)
3614 // Moved out of item area
3615 if(old_highlighted_item != highlighted_item)
3620 //printf("BC_ListBox::cursor_motion_event 1 %d\n", highlighted_item);
3622 // Change cursor to title division adjustment
3623 reset_cursor(new_cursor);
3650 if(!result && list_highlighted)
3652 list_highlighted = 0;
3653 highlighted_item = -1;
3654 highlighted_ptr = 0;
3655 highlighted_title = -1;
3656 highlighted_division = -1;
3668 int BC_ListBox::drag_start_event()
3670 switch(current_operation)
3674 gui->is_event_win() &&
3677 BC_ListBoxItem *item_return = 0;
3678 selection_number = get_cursor_item(data,
3679 top_level->cursor_x,
3680 top_level->cursor_y,
3683 if(selection_number >= 0)
3686 if (item_return->icon_vframe)
3688 drag_popup = new BC_DragWindow(this,
3689 item_return->icon_vframe /*,
3690 get_abs_cursor_x(0) - item_return->icon_vframe->get_w() / 2,
3691 get_abs_cursor_y(0) - item_return->icon_vframe->get_h() / 2 */);
3694 // this probably works not!
3695 if (item_return->icon)
3697 drag_popup = new BC_DragWindow(this,
3698 item_return->icon /*,
3699 get_abs_cursor_x(0) - item_return->icon->get_w() / 2,
3700 get_abs_cursor_y(0) - item_return->icon->get_h() / 2 */);
3704 drag_popup = new BC_DragWindow(this,
3705 drag_icon_vframe /*,
3706 get_abs_cursor_x(0) - drag_icon_vframe->get_w() / 2,
3707 get_abs_cursor_y(0) - drag_icon_vframe->get_h() / 2 */);
3710 current_operation = DRAG_ITEM;
3717 if(gui && gui->is_event_win() && allow_drag_column)
3719 drag_popup = new BC_DragWindow(this,
3720 drag_column_icon_vframe /*,
3721 get_abs_cursor_x(0) - drag_column_icon_vframe->get_w() / 2,
3722 get_abs_cursor_y(0) - drag_column_icon_vframe->get_h() / 2 */);
3723 dragged_title = highlighted_title;
3724 current_operation = COLUMN_DRAG;
3734 int BC_ListBox::drag_motion_event()
3736 //printf("BC_ListBox::drag_motion_event 1 %d\n", current_operation);
3737 switch(current_operation)
3742 int new_highlighted_item = -1;
3743 BC_ListBoxItem *new_highlighted_ptr = 0;
3744 new_highlighted_item = get_cursor_item(data,
3745 top_level->cursor_x, top_level->cursor_y,
3746 &new_highlighted_ptr);
3748 if(new_highlighted_item != highlighted_item)
3753 // Always update highlighted value for drag_stop
3754 highlighted_item = new_highlighted_item;
3755 highlighted_ptr = new_highlighted_ptr;
3756 //printf("BC_ListBox::drag_motion_event 1 %p\n", highlighted_ptr);
3761 update_scrollbars(1);
3764 return drag_popup->cursor_motion_event();
3770 int old_highlighted_title = highlighted_title;
3771 test_column_titles(get_cursor_x(), get_cursor_y());
3772 if(old_highlighted_title != highlighted_title)
3776 return drag_popup->cursor_motion_event();
3783 int BC_ListBox::drag_stop_event()
3785 switch(current_operation)
3788 // Inside window boundary
3789 if(top_level->cursor_x > 0 &&
3790 top_level->cursor_x < gui->get_w() - drag_popup->get_w() / 2 &&
3791 top_level->cursor_y > 0 &&
3792 top_level->cursor_y < gui->get_h() - drag_popup->get_h() / 2)
3797 if(display_format == LISTBOX_ICONS)
3799 reposition_item(data,
3801 top_level->cursor_x +
3802 drag_popup->get_offset_x() -
3806 top_level->cursor_y +
3807 drag_popup->get_offset_y() -
3817 int destination = highlighted_item = item_to_index(data,
3819 //printf("BC_ListBox::drag_stop_event 1 %p %d\n", highlighted_ptr, destination);
3821 // Move selected items from data to temporary
3822 ArrayList<BC_ListBoxItem*> *src_items =
3823 new ArrayList<BC_ListBoxItem*>[columns];
3825 move_selection(src_items, data);
3827 // Insert items from temporary to data
3833 delete [] src_items;
3834 set_autoplacement(data, 0, 1);
3841 drag_popup->drag_failure_event();
3846 current_operation = NO_OPERATION;
3852 if(dragged_title != highlighted_title)
3854 if(highlighted_title >= 0)
3856 if(!move_column_event()) draw_titles(1);
3859 drag_popup->drag_failure_event();
3861 current_operation = NO_OPERATION;
3871 BC_DragWindow* BC_ListBox::get_drag_popup()
3876 int BC_ListBox::translation_event()
3880 int new_x = gui->get_x() +
3881 (top_level->last_translate_x - top_level->prev_x -
3882 BC_DisplayInfo::get_left_border());
3883 int new_y = gui->get_y() +
3884 (top_level->last_translate_y - top_level->prev_y -
3885 BC_DisplayInfo::get_top_border());
3887 gui->reposition_window(new_x, new_y);
3893 int BC_ListBox::reposition_window(int x, int y, int w, int h, int flush)
3897 if(w != -1) popup_w = w;
3898 if(h != -1) popup_h = h;
3899 //printf("BC_ListBox::reposition_window %d %d\n", popup_w, popup_h);
3904 xscrollbar->reposition_window(get_xscroll_x(),
3906 get_xscroll_width());
3908 yscrollbar->reposition_window(get_yscroll_x(),
3910 get_yscroll_height());
3915 BC_WindowBase::reposition_window(x, y, w, h);
3921 int BC_ListBox::deactivate()
3924 // printf("BC_ListBox::deactivate %d this=%p gui=%p active=%d\n",
3934 //printf("BC_ListBox::deactivate %d this=%p gui=%p\n", __LINE__, this, gui);
3943 highlighted_item = -1;
3944 highlighted_ptr = 0;
3949 //printf("BC_ListBox::deactivate %d this=%p\n", __LINE__, this);
3951 top_level->active_subwindow = 0;
3957 int BC_ListBox::activate(int take_focus)
3959 if( active ) return 0;
3962 set_active_subwindow(this);
3963 button_releases = 0;
3964 if( !is_popup || gui ) return 0;
3965 int wx = get_x(), wy = get_y() + get_h();
3966 if( justify == LISTBOX_RIGHT ) wx += get_w() - popup_w;
3969 XTranslateCoordinates(top_level->display,
3970 parent_window->win, top_level->rootwin,
3971 wx, wy, &abs_x, &abs_y, &xwin);
3974 return activate(abs_x, abs_y);
3977 int BC_ListBox::activate(int x, int y, int w, int h)
3979 if( !is_popup || gui ) return 0;
3981 if(w != -1) popup_w = w;
3982 if(h != -1) popup_h = h;
3984 if( y + popup_h > top_level->get_root_h(0) )
3985 y -= get_h() + popup_h;
3986 add_subwindow(gui = new BC_Popup(this,
3987 x, y, popup_w, popup_h, -1, 0, 0));
3989 gui->show_window(1);
3993 int BC_ListBox::is_active()
3998 int BC_ListBox::keypress_event()
4000 if(!active) return 0;
4002 //printf("BC_ListBox::keypress_event %d\n", __LINE__);
4004 int result = 0, redraw = 0;
4005 int view_items = last_in_view - first_in_view + 1;
4006 if( view_items <= 0 ) view_items = view_h / get_text_height(MEDIUMFONT);
4007 int new_item = -1, new_selection = -1;
4009 switch(top_level->get_keypress())
4013 top_level->deactivate();
4015 // If user is manipulating popup with keyboard, don't pass on event.
4018 //printf("BC_ListBox::keypress_event %d %p\n", __LINE__, get_selection(0, 0));
4019 if(top_level->get_keypress() == RETURN)
4021 //printf("BC_ListBox::keypress_event %d %p\n", __LINE__, get_selection(0, 0));
4029 new_selection = new_item = select_previous(0);
4031 //printf("BC_ListBox::keypress_event 1 %d\n", new_item);
4034 center_selection(new_item);
4042 new_selection = new_item = select_next(0);
4046 center_selection(new_item);
4054 new_selection = new_item = select_previous(view_items - 1);
4058 center_selection(new_item);
4066 new_selection = new_item = select_next(view_items - 1);
4070 center_selection(new_item);
4098 int query_len = strlen(query);
4099 if( query_len < (int)sizeof(query)-1 &&
4100 top_level->get_keypress() > 30 &&
4101 top_level->get_keypress() < 127)
4103 query[query_len++] = top_level->get_keypress();
4104 query[query_len] = 0;
4105 new_selection = query_list();
4108 if(top_level->get_keypress() == BACKSPACE)
4110 if(query_len > 0) query[--query_len] = 0;
4111 new_selection = query_list();
4115 show_tooltip(query);
4129 update_scrollbars(1);
4132 //printf("BC_ListBox::keypress_event %d new_selection=%d\n", __LINE__, new_selection);
4133 if(new_selection >= 0 && !is_suggestions)
4135 //printf("BC_ListBox::keypress_event %d\n", __LINE__);
4136 selection_changed();
4137 //printf("BC_ListBox::keypress_event %d\n", __LINE__);
4144 BC_Pixmap* BC_ListBox::get_bg_surface()
4150 void BC_ListBox::draw_background()
4152 if( !bg_draw ) return;
4154 // White background pixmap
4155 set_color(top_level->get_resources()->listbox_inactive);
4156 draw_box(0, 0, bg_surface->get_w(), bg_surface->get_h(), bg_surface);
4158 // Optional heroine pixmap
4160 bg_surface->draw_pixmap(bg_pixmap,
4161 bg_surface->get_w() - top_level->get_resources()->listbox_bg->get_w(),
4165 void BC_ListBox::clear_listbox(int x, int y, int w, int h)
4167 gui->draw_pixmap(bg_surface, x, y, w, h, x, y - title_h);
4170 void BC_ListBox::update_format(int display_format, int redraw)
4172 this->display_format = display_format;
4173 xposition = 0; yposition = 0;
4174 if( redraw && gui ) draw_items(1, 1);
4177 int BC_ListBox::get_format()
4179 return display_format;
4184 int BC_ListBox::draw_items(int flush, int draw_bg)
4188 BC_Resources *resources = get_resources();
4190 //dump(data, columns);
4192 // Calculate items width
4193 calculate_item_coords();
4196 // Create and destroy scrollbars as needed
4201 if( bg_draw ) this->bg_draw = 1;
4207 if(display_format == LISTBOX_ICONS)
4209 clear_listbox(2, 2 + title_h, view_w, view_h);
4211 set_font(MEDIUMFONT);
4212 for(int i = 0; i < data[master_column].size(); i++)
4214 BC_ListBoxItem *item = data[master_column].get(i);
4215 if(get_item_x(item) >= -get_item_w(item) &&
4216 get_item_x(item) < view_w &&
4217 get_item_y(item) >= -get_item_h(item) + title_h &&
4218 get_item_y(item) < view_h + title_h)
4220 item->set_in_view(1);
4221 if( first_in_view < 0 ) first_in_view = i;
4223 int item_color = get_item_highlight(data, 0, i);
4224 int icon_x, icon_y, icon_w, icon_h;
4225 int text_x, text_y, text_w, text_h;
4228 get_icon_mask(item, icon_x, icon_y, icon_w, icon_h);
4229 get_text_mask(item, text_x, text_y, text_w, text_h);
4232 if(item_color != resources->listbox_inactive)
4234 gui->set_color(BLACK);
4235 gui->draw_rectangle(icon_x, icon_y, icon_w, icon_h);
4236 gui->set_color(item_color);
4237 gui->draw_box(icon_x + 1, icon_y + 1, icon_w - 2, icon_h - 2);
4238 gui->set_color(BLACK);
4239 gui->draw_rectangle(text_x, text_y, text_w, text_h);
4240 gui->set_color(item_color);
4241 gui->draw_box(text_x + 1, text_y + 1, text_w - 2, text_h - 2);
4243 if(icon_position == ICON_LEFT)
4244 gui->draw_box(text_x - 1, text_y + 1, 2, text_h - 2);
4246 if(icon_position == ICON_TOP)
4247 gui->draw_line(text_x + 1, text_y, text_x + icon_w - 2, text_y);
4248 if(text_x + text_w < icon_x + icon_w)
4250 gui->set_color(BLACK);
4251 gui->draw_line(text_x + text_w,
4259 gui->set_color(get_item_color(data, 0, i));
4261 gui->pixmap->draw_pixmap(item->icon,
4262 icon_x + ICON_MARGIN,
4263 icon_y + ICON_MARGIN);
4266 gui->draw_text(text_x + ICON_MARGIN,
4267 text_y + ICON_MARGIN + get_baseline(item),
4271 item->set_in_view(0);
4277 // Draw one column at a time so text overruns don't go into the next column
4278 // clear column backgrounds
4279 int current_toggle = 0;
4280 for(int j = 0; j < columns; j++)
4282 clear_listbox(LISTBOX_BORDER + get_column_offset(j) - xposition,
4283 LISTBOX_BORDER + title_h,
4284 get_column_width(j, 1),
4286 // Draw rows in the column recursively
4287 draw_text_recursive(data, j, 0, ¤t_toggle);
4290 // Delete excess expanders
4291 while(expanders.total > current_toggle)
4293 expanders.remove_object();
4297 // draw user images if available
4299 // Draw titles on top of rows for superposition effect
4302 // Clear garbage from bottom right corner
4303 if(xscrollbar && yscrollbar && is_popup)
4305 gui->draw_top_background(parent_window,
4306 popup_w - get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w(),
4307 popup_h - get_resources()->hscroll_data[SCROLL_HANDLE_UP]->get_h(),
4308 get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w(),
4309 get_resources()->hscroll_data[SCROLL_HANDLE_UP]->get_h());
4316 if(current_operation == SELECT_RECT)
4326 void BC_ListBox::draw_text_recursive(ArrayList<BC_ListBoxItem*> *data,
4329 int *current_toggle)
4334 BC_Resources *resources = get_resources();
4336 set_font(MEDIUMFONT);
4339 // Search for a branch and make room for toggle if there is one
4342 for(int i = 0; i < data[column].size(); i++)
4344 if(data[column].get(i)->get_sublist())
4346 subindent = BC_WindowBase::get_resources()->listbox_expand[0]->get_w();
4352 row_height = row_ascent = row_descent = 0;
4353 for(int i = 0; i < data[column].total; i++)
4355 BC_ListBoxItem *item = data[column].values[i];
4356 int ht = get_text_h(item);
4357 if( ht > row_height ) row_height = ht;
4358 int bl = get_baseline(item);
4359 if( bl > row_ascent ) row_ascent = bl;
4361 if( dt > row_descent ) row_ascent = bl;
4364 for(int i = 0; i < data[column].size(); i++)
4367 BC_ListBoxItem *item = data[column].values[i];
4368 BC_ListBoxItem *first_item = data[master_column].values[i];
4370 if(get_item_y(item) >= -get_item_h(item) + title_h &&
4371 get_item_y(item) < view_h + title_h)
4373 int row_color = get_item_highlight(data, 0, i);
4374 int x, y, w, h, column_width;
4376 get_text_mask(item, x, y, w, h);
4377 column_width = get_column_width(column, 1);
4378 if(x + column_width > view_w + LISTBOX_BORDER * 2)
4379 column_width = view_w + LISTBOX_BORDER * 2 - x;
4381 if(row_color != resources->listbox_inactive)
4383 gui->set_color(row_color);
4384 gui->draw_box(x, y, column_width, h);
4385 gui->set_color(BLACK);
4386 int xx = x + column_width-1;
4387 gui->draw_line(x, y, xx, y);
4388 int hh = row_height;
4389 if( display_format == LISTBOX_ICON_LIST ) {
4390 int ih = get_icon_h(item);
4391 if( ih > hh ) hh = ih;
4394 gui->draw_line(x, yy, xx, yy);
4397 gui->set_color(get_item_color(data, column, i));
4400 if(column == 0 && display_format == LISTBOX_ICON_LIST)
4404 gui->pixmap->draw_pixmap(item->icon,
4407 x += item->icon->get_w() + ICON_MARGIN;
4412 // Indent only applies to first column
4414 x + LISTBOX_BORDER + LISTBOX_MARGIN +
4415 (column == 0 ? indent + subindent : 0),
4416 y + get_baseline(item), item->text);
4417 item->set_in_view(1);
4419 if( first_in_view < 0 ) first_in_view = i;
4425 item->get_sublist() &&
4426 item->get_columns())
4428 // Create new expander
4429 if(*current_toggle >= expanders.total)
4431 BC_ListBoxToggle *toggle =
4432 new BC_ListBoxToggle(this,
4434 x + LISTBOX_BORDER + LISTBOX_MARGIN + indent,
4437 expanders.append(toggle);
4440 // Reposition existing expander
4442 BC_ListBoxToggle *toggle = expanders.values[*current_toggle];
4443 //printf("BC_ListBox::draw_text_recursive 1 %d\n", *current_toggle);
4444 toggle->update(item,
4445 x + LISTBOX_BORDER + LISTBOX_MARGIN + indent,
4449 (*current_toggle)++;
4456 item->set_in_view(0);
4458 // Descend into sublist
4459 if(first_item->get_expand())
4461 draw_text_recursive(first_item->get_sublist(),
4463 indent + LISTBOX_INDENT,
4472 int BC_ListBox::draw_border(int flash)
4474 BC_Resources *resources = top_level->get_resources();
4475 gui->draw_3d_border(0,
4477 view_w + LISTBOX_BORDER * 2,
4478 view_h + title_h + LISTBOX_BORDER * 2,
4479 resources->listbox_border1,
4481 resources->listbox_border2_hi :
4482 resources->listbox_border2,
4484 resources->listbox_border3_hi :
4485 resources->listbox_border3,
4486 resources->listbox_border4);
4496 void BC_ListBox::draw_title(int number)
4498 // Column title background
4499 int image_number = 0;
4500 if(number == highlighted_title)
4503 if(current_operation == COLUMN_DN)
4507 int column_offset = get_column_offset(number) - xposition + LISTBOX_BORDER;
4508 int column_width = get_column_width(number, 1);
4509 gui->draw_3segmenth(get_column_offset(number) - xposition + LISTBOX_BORDER,
4511 get_column_width(number, 1) + get_resources()->listbox_title_overlap,
4512 column_bg[image_number]);
4514 // Column title sort order
4515 if(number == sort_column)
4518 if(sort_order == SORT_ASCENDING)
4519 src = column_sort_dn;
4521 src = column_sort_up;
4523 int x = column_offset +
4526 if(x > items_w) x = items_w;
4527 x -= 5 + src->get_w();
4528 gui->draw_pixmap(src,
4530 title_h / 2 - src->get_h() / 2 + LISTBOX_BORDER);
4534 int x = -xposition +
4535 get_column_offset(number) +
4538 x += get_resources()->listbox_title_margin;
4540 gui->set_color(get_resources()->listbox_title_color);
4542 LISTBOX_MARGIN + LISTBOX_BORDER + get_text_ascent(MEDIUMFONT),
4543 _(column_titles[number]));
4546 int BC_ListBox::draw_titles(int flash)
4549 (display_format == LISTBOX_TEXT ||
4550 display_format == LISTBOX_ICON_LIST))
4552 //printf("BC_ListBox::draw_titles 1 %d\n", highlighted_title);
4553 for(int i = 0; i < columns; i++)
4555 if(i != highlighted_title)
4559 if(highlighted_title >= 0) draw_title(highlighted_title);
4570 void BC_ListBox::draw_toggles(int flash)
4572 for(int i = 0; i < expanders.total; i++)
4573 expanders.values[i]->draw(0);
4575 //printf("BC_ListBox::draw_toggles 1 %d\n", flash);
4576 if(flash && expanders.total)
4583 int BC_ListBox::draw_rectangle(int flash)
4585 int x1 = MIN(rect_x1, rect_x2);
4586 int x2 = MAX(rect_x1, rect_x2);
4587 int y1 = MIN(rect_y1, rect_y2);
4588 int y2 = MAX(rect_y1, rect_y2);
4590 if(x1 == x2 || y1 == y2) return 0;
4593 gui->set_color(WHITE);
4594 gui->draw_rectangle(x1, y1, x2 - x1, y2 - y1);
4605 void BC_ListBox::dump(ArrayList<BC_ListBoxItem*> *data,
4612 printf("BC_ListBox::dump 1\n");
4615 for(int i = 0; i < data[master_column].total; i++)
4617 for(int k = 0; k < indent; k++)
4619 for(int j = 0; j < columns; j++)
4621 BC_ListBoxItem *item = data[j].values[i];
4622 printf("%d,%d,%d=%s ",
4625 item->autoplace_text,
4630 if(data[master_column].values[i]->get_sublist())
4632 dump(data[master_column].values[i]->get_sublist(),
4633 data[master_column].values[i]->get_columns(),