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();
108 state = TOGGLE_CHECKED;
113 state = TOGGLE_CHECKEDHI;
124 case TOGGLE_CHECKEDHI:
129 case TOGGLE_DOWN_EXIT:
137 int BC_ListBoxToggle::cursor_motion_event(int *redraw_toggles)
139 int w = listbox->toggle_images[0]->get_w();
140 int h = listbox->toggle_images[0]->get_h();
141 int cursor_inside = listbox->get_cursor_x() >= x &&
142 listbox->get_cursor_x() < x + w &&
143 listbox->get_cursor_y() >= y &&
144 listbox->get_cursor_y() < y + h;
148 case BC_ListBoxToggle::TOGGLE_UPHI:
149 if( !cursor_inside ) {
150 state = BC_ListBoxToggle::TOGGLE_UP;
155 case BC_ListBoxToggle::TOGGLE_CHECKEDHI:
156 if( !cursor_inside ) {
157 state = BC_ListBoxToggle::TOGGLE_CHECKED;
162 case BC_ListBoxToggle::TOGGLE_DOWN:
163 if( !cursor_inside ) {
164 state = BC_ListBoxToggle::TOGGLE_DOWN_EXIT;
170 case BC_ListBoxToggle::TOGGLE_DOWN_EXIT:
171 if( cursor_inside ) {
172 state = BC_ListBoxToggle::TOGGLE_DOWN;
179 if( cursor_inside ) {
181 state = BC_ListBoxToggle::TOGGLE_CHECKEDHI;
183 state = BC_ListBoxToggle::TOGGLE_UPHI;
191 int BC_ListBoxToggle::cursor_leave_event(int *redraw_toggles)
194 state = BC_ListBoxToggle::TOGGLE_CHECKED;
196 state = BC_ListBoxToggle::TOGGLE_UP;
200 int BC_ListBoxToggle::button_press_event()
202 int w = listbox->toggle_images[0]->get_w();
203 int h = listbox->toggle_images[0]->get_h();
205 if( listbox->gui->get_cursor_x() >= x &&
206 listbox->gui->get_cursor_x() < x + w &&
207 listbox->gui->get_cursor_y() >= y &&
208 listbox->gui->get_cursor_y() < y + h ) {
209 state = BC_ListBoxToggle::TOGGLE_DOWN;
215 int BC_ListBoxToggle::button_release_event(int *redraw_toggles)
220 case BC_ListBoxToggle::TOGGLE_DOWN:
223 state = BC_ListBoxToggle::TOGGLE_CHECKEDHI;
225 state = BC_ListBoxToggle::TOGGLE_UPHI;
226 listbox->expand_item(item, value);
230 case BC_ListBoxToggle::TOGGLE_DOWN_EXIT:
232 state = BC_ListBoxToggle::TOGGLE_CHECKED;
234 state = BC_ListBoxToggle::TOGGLE_UP;
242 void BC_ListBoxToggle::draw(int flash)
245 int image_number = 0;
246 int w = listbox->toggle_images[0]->get_w();
247 int h = listbox->toggle_images[0]->get_h();
250 case BC_ListBoxToggle::TOGGLE_UP: image_number = 0; break;
251 case BC_ListBoxToggle::TOGGLE_UPHI: image_number = 1; break;
252 case BC_ListBoxToggle::TOGGLE_CHECKED: image_number = 2; break;
253 case BC_ListBoxToggle::TOGGLE_DOWN: image_number = 3; break;
254 case BC_ListBoxToggle::TOGGLE_CHECKEDHI: image_number = 4; break;
255 case BC_ListBoxToggle::TOGGLE_DOWN_EXIT:
256 image_number = value ? 2 : 0;
260 //printf("BC_ListBoxToggle::draw 1 %d\n", state);
261 listbox->gui->draw_pixmap(listbox->toggle_images[image_number], x, y);
264 listbox->gui->flash(x, y, w, h);
265 listbox->gui->flush();
271 // ====================================================== box
273 BC_ListBox::BC_ListBox(int x, int y, int w, int h,
274 int display_format, ArrayList<BC_ListBoxItem*> *data,
275 const char **column_titles, int *column_width, int columns,
276 int yposition, int is_popup, int selection_mode,
277 int icon_position, int allow_drag)
278 : BC_SubWindow(x, y, w, h, -1)
280 justify = LISTBOX_RIGHT;
282 highlighted_item = -1;
283 highlighted_title = -1;
284 highlighted_division = -1;
288 current_cursor = ARROW_CURSOR;
290 view_w = items_w = 0;
291 view_h = items_h = 0;
298 xscroll_orientation = SCROLL_HORIZ;
299 yscroll_orientation = SCROLL_VERT;
304 selection_number1 = -1;
305 selection_number2 = -1;
308 row_height = row_ascent = row_descent = 0;
310 selection_center = 0;
312 selection_number = -1;
313 current_operation = NO_OPERATION;
314 button_highlighted = 0;
316 list_highlighted = 0;
320 allow_drag_scroll = 1;
322 for( int i=0; i<32; ++i ) default_column_width[i] = 0;
327 allow_drag_column = 0;
334 for( int i=0; i<3; ++i ) column_bg[i] = 0;
335 for( int i=0; i<4; ++i ) button_images[i] = 0;
336 for( int i=0; i<5; ++i ) toggle_images[i] = 0;
341 //printf("BC_ListBox::BC_ListBox 1\n");
343 this->columns = columns;
344 this->yposition = yposition;
345 this->is_popup = is_popup;
346 this->use_button = 1;
347 this->display_format = display_format;
348 this->selection_mode = selection_mode;
349 this->icon_position = icon_position;
350 this->allow_drag = allow_drag;
351 this->column_titles = 0;
352 this->column_width = 0;
353 this->first_in_view = -1;
354 this->last_in_view = 0;
355 //printf("BC_ListBox::BC_ListBox 1\n");
357 if( (!column_titles && column_width) ||
358 (column_titles && !column_width) ) {
359 printf("BC_ListBox::BC_ListBox either column_titles or column_widths == NULL but not both.\n");
361 //printf("BC_ListBox::BC_ListBox 2 %p %p\n", column_titles, column_width);
363 set_columns(column_titles, column_width, columns);
365 //printf("BC_ListBox::BC_ListBox 3\n");
367 drag_icon_vframe = 0;
368 drag_column_icon_vframe = 0;
371 temp_display_format = display_format;
372 rect_x1 = rect_x2 = 0;
373 rect_y1 = rect_y2 = 0;
375 // reset the search engine
376 //printf("BC_ListBox::BC_ListBox 4\n");
379 //printf("BC_ListBox::BC_ListBox 5\n");
382 BC_ListBox::~BC_ListBox()
384 expanders.remove_all_objects();
385 if( bg_surface ) delete bg_surface;
386 if( bg_pixmap ) delete bg_pixmap;
387 if( xscrollbar ) delete xscrollbar;
388 if( yscrollbar ) delete yscrollbar;
389 for( int i=0; i<3; ++i ) delete column_bg[i];
390 for( int i=0; i<4; ++i ) delete button_images[i];
391 for( int i=0; i<5; ++i ) delete toggle_images[i];
392 if( column_sort_up ) delete column_sort_up;
393 if( column_sort_dn ) delete column_sort_dn;
396 if( drag_popup ) delete drag_popup;
399 int BC_ListBox::enable()
406 int BC_ListBox::disable()
413 void BC_ListBox::reset_query()
415 query[0] = 0; // reset query
418 int BC_ListBox::evaluate_query(char *string)
420 for( int i=0; i<data[search_column].size(); ++i ) {
421 if( strcmp(string, data[search_column].get(i)->text) <= 0 &&
422 data[search_column].get(i)->searchable ) {
430 int BC_ListBox::query_list()
432 if( query[0] == 0 ) return 0;
436 int selection_changed = 0;
437 int prev_selection = -1;
438 result = evaluate_query(query);
439 if( result >= 0 ) done = 1;
443 for( int i=0; i<data[0].total; ++i ) {
444 for( int j=0; j<columns; ++j ) {
445 if( data[j].values[i]->selected ) prev_selection = i;
446 data[j].values[i]->selected = 0;
451 if( prev_selection != result )
452 selection_changed = 1;
453 for( int j=0; j<columns; ++j ) {
454 data[j].values[result]->selected = 1;
456 center_selection(result);
459 return selection_changed;
462 void BC_ListBox::init_column_width()
464 if( !column_width && data ) {
466 for( int i=0; i<data[0].total; ++i ) {
467 wd = get_text_w(data[0].values[i]);
468 if( wd > widest ) widest = wd;
470 default_column_width[0] = widest + 2 * LISTBOX_MARGIN;
474 int BC_ListBox::initialize()
478 for( int i=0; i<4; ++i ) {
479 button_images[i] = new BC_Pixmap(parent_window,
480 BC_WindowBase::get_resources()->listbox_button[i],
483 w = button_images[0]->get_w();
484 h = button_images[0]->get_h();
488 current_operation = NO_OPERATION;
493 current_operation = NO_OPERATION;
496 for( int i=0; i<3; ++i ) {
497 column_bg[i] = new BC_Pixmap(parent_window,
498 get_resources()->listbox_column[i],
501 for( int i=0; i<5; ++i ) {
502 toggle_images[i] = new BC_Pixmap(parent_window,
503 get_resources()->listbox_expand[i],
507 column_sort_up = new BC_Pixmap(parent_window,
508 BC_WindowBase::get_resources()->listbox_up,
510 column_sort_dn = new BC_Pixmap(parent_window,
511 BC_WindowBase::get_resources()->listbox_dn,
514 //printf("BC_ListBox::initialize 10\n");
515 drag_icon_vframe = get_resources()->type_to_icon[ICON_UNKNOWN];
516 drag_column_icon_vframe = get_resources()->type_to_icon[ICON_COLUMN];
517 // = new BC_Pixmap(parent_window,
518 // get_resources()->type_to_icon[ICON_UNKNOWN],
520 // drag_column_icon = new BC_Pixmap(parent_window,
521 // get_resources()->type_to_icon[ICON_COLUMN],
523 BC_SubWindow::initialize();
527 if( top_level->get_resources()->listbox_bg )
528 bg_pixmap = new BC_Pixmap(this,
529 get_resources()->listbox_bg,
535 if( !use_button && is_popup ) {
543 void BC_ListBox::deactivate_selection()
545 current_operation = NO_OPERATION;
548 int BC_ListBox::draw_button(int flush)
550 // Draw the button for a popup listbox
551 if( use_button && is_popup ) {
552 int image_number = 0;
554 draw_top_background(parent_window, 0, 0, w, h);
556 if( button_highlighted )
558 if( current_operation == BUTTON_DN )
563 pixmap->draw_pixmap(button_images[image_number],
570 int BC_ListBox::calculate_item_coords()
572 if( !data ) return 0;
578 // Change the display_format to get the right item dimensions for both
580 temp_display_format = display_format;
583 // Scan the first column for lowest y coord of all text
584 // and lowest right x and y coord for all icons which aren't auto placable
585 calculate_last_coords_recursive(data,
586 &icon_x, &next_icon_x, &next_icon_y, &next_text_y, 1);
588 // Reset last column width. It's recalculated based on text width.
589 calculate_item_coords_recursive(data,
590 &icon_x, &next_icon_x, &next_icon_y, &next_text_y, 1);
592 display_format = temp_display_format;
597 void BC_ListBox::calculate_last_coords_recursive(
598 ArrayList<BC_ListBoxItem*> *data,
605 for( int i=0; i<data[0].size(); ++i ) {
606 int current_text_y = 0;
607 int current_icon_x = 0;
608 int current_icon_y = 0;
609 BC_ListBoxItem *item = data[0].get(i);
612 if( !item->autoplace_text ) {
613 // Lowest text coordinate
614 display_format = LISTBOX_TEXT;
615 current_text_y = item->text_y + get_text_h(item);
616 if( current_text_y > *next_text_y )
617 *next_text_y = current_text_y;
619 // Add sublist depth if it is expanded
620 if( item->get_sublist() && item->get_columns() &&
621 item->get_expand() ) {
622 calculate_last_coords_recursive(item->get_sublist(),
623 icon_x, next_icon_x, next_icon_y, next_text_y, 0);
627 // Get next_icon coordinate
629 BC_ListBoxItem *item = data[master_column].get(i);
630 if( !item->autoplace_icon ) {
631 display_format = LISTBOX_ICONS;
632 // Lowest right icon coordinate.
633 current_icon_x = item->icon_x;
634 if( current_icon_x > *icon_x ) *icon_x = current_icon_x;
635 if( current_icon_x + get_item_w(item) > *next_icon_x ) {
636 *next_icon_x = current_icon_x + get_item_w(item);
639 current_icon_y = item->icon_y + get_item_h(item);
640 if( current_icon_y > *next_icon_y )
641 *next_icon_y = current_icon_y;
648 void BC_ListBox::calculate_item_coords_recursive(
649 ArrayList<BC_ListBoxItem*> *data,
650 int *icon_x, int *next_icon_x, int *next_icon_y, int *next_text_y,
653 // get maximum height of an icon
654 row_height = get_text_height(MEDIUMFONT);
655 if( temp_display_format == LISTBOX_ICON_LIST ) {
656 for( int i=0; i<data[0].size(); ++i ) {
657 if( data[0].get(i)->icon ) {
658 if( data[0].get(i)->icon->get_h() > row_height )
659 row_height = data[0].get(i)->icon->get_h();
665 // Set up items which need autoplacement.
666 // Should fill icons down and then across
667 for( int i=0; i<data[0].size(); ++i ) {
668 // Don't increase y unless the row requires autoplacing.
669 int total_autoplaced_columns = 0;
671 // Set up icons in first column
673 BC_ListBoxItem *item = data[master_column].get(i);
674 if( item->autoplace_icon ) {
675 // 1 column only if icons are used
676 display_format = LISTBOX_ICONS;
679 if( *next_icon_y + get_item_h(item) >= get_h() &&
681 *icon_x = *next_icon_x;
685 if( *icon_x + get_item_w(item) > *next_icon_x )
686 *next_icon_x = *icon_x + get_item_w(item);
689 item->set_icon_x(*icon_x);
690 item->set_icon_y(*next_icon_y);
692 *next_icon_y += get_item_h(item);
698 row_ascent = row_descent = 0;
699 // row_height still holds icon max height
700 for( int j=0; j<columns; ++j ) {
701 BC_ListBoxItem *item = data[j].get(i);
702 if( item->autoplace_text ) {
703 display_format = LISTBOX_TEXT;
704 item->set_text_x(next_text_x);
705 item->set_text_y(*next_text_y);
706 int ht = get_text_h(item);
707 if( ht > row_height ) row_height = ht;
708 int bl = get_baseline(item);
709 if( bl > row_ascent ) row_ascent = bl;
711 if( dt > row_descent ) row_ascent = bl;
713 // printf("BC_ListBox::calculate_item_coords_recursive %p %d %d %d %d %s \n",
714 // item->get_sublist(), item->get_columns(), item->get_expand(),
715 // next_text_x, *next_text_y, item->get_text());
716 // Increment position of next column
717 if( j < columns - 1 ) {
718 next_text_x += (column_width ?
720 default_column_width[j]);
722 // Set last column width based on text width
724 int new_w = get_item_w(item);
726 int *previous_w = (column_width ?
728 &default_column_width[j]);
729 if( new_w > *previous_w )
731 //printf("BC_ListBox::calculate_item_coords_recursive 1 %d\n", new_w);
733 total_autoplaced_columns++;
737 // Increase the text vertical position
738 if( total_autoplaced_columns ) {
739 display_format = LISTBOX_TEXT;
740 *next_text_y += row_height;
744 BC_ListBoxItem *item = data[master_column].values[i];
745 if( item->get_sublist() && item->get_columns() &&
746 item->get_expand() ) {
747 calculate_item_coords_recursive( item->get_sublist(),
748 icon_x, next_icon_x, next_icon_y, next_text_y, 0);
753 void BC_ListBox::set_is_suggestions(int value)
755 this->is_suggestions = value;
757 void BC_ListBox::set_scroll_repeat()
759 if( scroll_repeat ) return;
761 set_repeat(get_resources()->scroll_repeat);
764 void BC_ListBox::unset_scroll_repeat()
766 if( !scroll_repeat ) return;
768 unset_repeat(get_resources()->scroll_repeat);
771 void BC_ListBox::set_use_button(int value)
773 this->use_button = value;
776 void BC_ListBox::set_justify(int value)
778 this->justify = value;
781 void BC_ListBox::set_allow_drag_column(int value)
783 this->allow_drag_column = value;
786 void BC_ListBox::set_process_drag(int value)
788 this->process_drag = value;
791 void BC_ListBox::set_master_column(int value, int redraw)
793 this->master_column = value;
799 void BC_ListBox::set_search_column(int value)
801 this->search_column = value;
804 int BC_ListBox::get_sort_column()
809 void BC_ListBox::set_sort_column(int value, int redraw)
817 int BC_ListBox::get_sort_order()
822 void BC_ListBox::set_sort_order(int value, int redraw)
831 int BC_ListBox::get_display_mode()
833 return display_format;
836 int BC_ListBox::get_yposition()
841 int BC_ListBox::get_xposition()
846 int BC_ListBox::get_highlighted_item()
848 return highlighted_item;
852 int BC_ListBox::get_item_x(BC_ListBoxItem *item)
854 if( display_format == LISTBOX_TEXT )
855 return item->text_x - xposition + 2;
856 if( display_format == LISTBOX_ICON_LIST )
857 return item->text_x - xposition + 2;
858 return item->icon_x - xposition + 2;
861 int BC_ListBox::get_item_y(BC_ListBoxItem *item)
863 if( display_format == LISTBOX_TEXT )
864 return item->text_y - yposition + title_h + 2;
865 if( display_format == LISTBOX_ICON_LIST )
866 return item->text_y - yposition + title_h + 2;
867 return item->icon_y - yposition + title_h + 2;
870 int BC_ListBox::get_item_w(BC_ListBoxItem *item)
872 if( display_format == LISTBOX_ICONS ) {
874 get_icon_mask(item, x, y, w, h);
876 get_text_mask(item, x, y, w, h);
879 return icon_position == ICON_LEFT ? icon_w + text_w :
880 icon_w > text_w ? icon_w : text_w;
882 return get_text_w(item) + 2 * LISTBOX_MARGIN;
885 int BC_ListBox::get_item_h(BC_ListBoxItem *item)
887 if( display_format == LISTBOX_ICONS ) {
889 get_icon_mask(item, x, y, w, h);
891 get_text_mask(item, x, y, w, h);
894 return icon_position != ICON_LEFT ? icon_h + text_h :
895 icon_h > text_h ? icon_h : text_h;
897 return get_text_h(item);
901 int BC_ListBox::get_icon_w(BC_ListBoxItem *item)
903 return item->get_icon_w();
906 int BC_ListBox::get_icon_h(BC_ListBoxItem *item)
908 return item->get_icon_h();
911 int BC_ListBox::get_text_w(BC_ListBoxItem *item)
913 int w = item->get_text_w();
914 if( w < 0 ) item->set_text_w(w = get_text_width(MEDIUMFONT, item->get_text()));
918 int BC_ListBox::get_text_h(BC_ListBoxItem *item)
920 int h = item->get_text_h();
921 if( h < 0 ) item->set_text_h(h = get_text_height(MEDIUMFONT, item->get_text()));
925 int BC_ListBox::get_baseline(BC_ListBoxItem *item)
927 int b = item->get_baseline();
928 if( b < 0 ) item->set_baseline(b = get_text_ascent(MEDIUMFONT));
932 int BC_ListBox::get_items_width()
936 if( display_format == LISTBOX_ICONS ) {
937 for( int i=0; i<columns; ++i ) {
938 for( int j=0; j<data[i].total; ++j ) {
940 BC_ListBoxItem *item = data[i].values[j];
943 get_icon_mask(item, x, y, w, h);
944 if( x1 + w > widest ) widest = x1 + w;
946 if( display_format == LISTBOX_ICONS && icon_position == ICON_LEFT )
949 get_text_mask(item, x, y, w, h);
950 if( x1 + w > widest ) widest = x1 + w;
955 if( display_format == LISTBOX_TEXT ) {
956 return get_column_offset(columns);
959 return get_column_offset(columns);
964 int BC_ListBox::get_items_height(ArrayList<BC_ListBoxItem*> *data, int columns,
975 for( int j=0; j<(data?data[master_column].total:0); ++j ) {
977 BC_ListBoxItem *item = data[master_column].values[j];
979 if( display_format == LISTBOX_ICONS ||
980 display_format == LISTBOX_ICON_LIST ) {
981 get_icon_mask(item, x, y, w, h);
982 if( y + h + yposition > highest ) highest = y + h + yposition;
984 get_text_mask(item, x, y, w, h);
985 if( y + h + yposition > highest ) highest = y + h + yposition;
988 get_text_mask(item, x, y, w, h);
990 // Descend into sublist
991 if( item->get_sublist() && item->get_expand() ) {
992 get_items_height(item->get_sublist(),
999 if( display_format == LISTBOX_TEXT && top_level ) {
1000 highest = LISTBOX_MARGIN + *result;
1006 int BC_ListBox::set_yposition(int position, int draw_items)
1008 this->yposition = position;
1010 this->draw_items(1);
1015 int BC_ListBox::set_xposition(int position)
1017 this->xposition = position;
1022 void BC_ListBox::expand_item(BC_ListBoxItem *item, int expand)
1025 item->expand = expand;
1026 // Collapse sublists if this is collapsed to make it easier to calculate
1028 if( item->get_sublist() )
1029 collapse_recursive(item->get_sublist(), master_column);
1031 // Set everything for autoplacement
1032 set_autoplacement(data, 0, 1);
1037 void BC_ListBox::collapse_recursive(ArrayList<BC_ListBoxItem*> *data,
1040 for( int i=0; i<data[master_column].total; ++i ) {
1041 BC_ListBoxItem *item = data[master_column].values[i];
1042 if( item->get_sublist() && item->expand ) {
1044 collapse_recursive(item->get_sublist(), master_column);
1049 void BC_ListBox::set_autoplacement(ArrayList<BC_ListBoxItem*> *data,
1053 for( int i=0; i<data[0].total; ++i ) {
1054 for( int j=0; j<columns; ++j ) {
1055 if( do_icons ) data[j].values[i]->autoplace_icon = 1;
1056 if( do_text ) data[j].values[i]->autoplace_text = 1;
1059 BC_ListBoxItem *item = data[master_column].values[i];
1060 if( item->get_sublist() ) {
1061 set_autoplacement(item->get_sublist(), do_icons, do_text);
1067 void BC_ListBox::set_scroll_stretch(int xv, int yv)
1069 if( xv >= 0 ) xscroll_orientation =
1070 !xv ? SCROLL_HORIZ : SCROLL_HORIZ + SCROLL_STRETCH;
1071 if( yv >= 0 ) yscroll_orientation =
1072 !yv ? SCROLL_VERT : SCROLL_VERT + SCROLL_STRETCH;
1075 int BC_ListBox::get_yscroll_x()
1078 return popup_w - get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w();
1080 return get_x() + popup_w -
1081 get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w();
1084 int BC_ListBox::get_yscroll_y()
1086 return is_popup ? 0 : get_y();
1089 int BC_ListBox::get_yscroll_height()
1091 return popup_h - (need_xscroll ?
1092 get_resources()->hscroll_data[SCROLL_HANDLE_UP]->get_h() :
1096 int BC_ListBox::get_xscroll_x()
1098 return is_popup ? 0 : get_x();
1101 int BC_ListBox::get_xscroll_y()
1103 return (is_popup ? popup_h : get_y() + popup_h) -
1104 get_resources()->hscroll_data[SCROLL_HANDLE_UP]->get_h();
1107 int BC_ListBox::get_xscroll_width()
1109 return popup_w - (need_yscroll ?
1110 get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w() : 0);
1113 int BC_ListBox::get_column_offset(int column)
1116 while( column > 0 ) {
1118 column_width[--column] :
1119 default_column_width[--column];
1124 void BC_ListBox::column_width_boundaries()
1126 if( column_width ) {
1127 for( int i=0; i<columns; ++i ) {
1128 if( column_width[i] < MIN_COLUMN_WIDTH )
1129 column_width[i] = MIN_COLUMN_WIDTH;
1133 for( int i=0; i<columns; ++i ) {
1134 if( default_column_width[i] < MIN_COLUMN_WIDTH )
1135 default_column_width[i] = MIN_COLUMN_WIDTH;
1140 int BC_ListBox::get_column_width(int column, int clamp_right)
1142 if( column < columns - 1 || !clamp_right )
1143 return column_width ? column_width[column] : default_column_width[column];
1144 return popup_w + xposition - get_column_offset(column);
1147 int BC_ListBox::get_icon_mask(BC_ListBoxItem *item,
1148 int &x, int &y, int &w, int &h)
1150 if( display_format == LISTBOX_ICONS ) {
1151 x = get_item_x(item);
1152 y = get_item_y(item);
1153 w = get_icon_w(item) + ICON_MARGIN * 2;
1154 h = get_icon_h(item) + ICON_MARGIN * 2;
1161 int BC_ListBox::get_text_mask(BC_ListBoxItem *item,
1162 int &x, int &y, int &w, int &h)
1164 x = get_item_x(item);
1165 y = get_item_y(item);
1167 if( display_format == LISTBOX_ICONS ) {
1168 if( icon_position == ICON_LEFT ) {
1169 x += get_icon_w(item) + ICON_MARGIN * 2;
1170 y += get_icon_h(item) - get_text_h(item);
1173 y += get_icon_h(item) + ICON_MARGIN;
1176 w = get_text_w(item) + ICON_MARGIN * 2;
1177 h = get_text_h(item) + ICON_MARGIN * 2;
1180 if( display_format == LISTBOX_TEXT ) {
1181 w = get_text_w(item) + LISTBOX_MARGIN * 2;
1182 h = get_text_h(item);
1185 w = get_text_width(MEDIUMFONT, item->text) + LISTBOX_MARGIN * 2;
1187 int ih = get_icon_h(item);
1188 if( h < ih ) h = ih;
1193 int BC_ListBox::get_item_highlight(ArrayList<BC_ListBoxItem*> *data,
1194 int column, int item)
1196 BC_Resources *resources = get_resources();
1197 if( data[column].values[item]->selected )
1198 return resources->listbox_selected;
1199 if( highlighted_item >= 0 &&
1200 highlighted_ptr == data[master_column].values[item] )
1201 return resources->listbox_highlighted;
1202 return resources->listbox_inactive;
1205 int BC_ListBox::get_item_color(ArrayList<BC_ListBoxItem*> *data,
1206 int column, int item)
1208 int color = data[column].values[item]->color;
1209 if( color == -1 ) color = get_resources()->listbox_text;
1210 if( get_item_highlight(data, column, item) == color )
1215 int BC_ListBox::get_from_column()
1217 return dragged_title;
1220 int BC_ListBox::get_to_column()
1222 return highlighted_title;
1226 BC_ListBoxItem* BC_ListBox::get_selection(int column, int selection_number)
1228 return get_selection_recursive(data,
1233 BC_ListBoxItem* BC_ListBox::get_selection_recursive(ArrayList<BC_ListBoxItem*> *data,
1234 int column, int selection_number)
1236 if( !data ) return 0;
1238 for( int i=0; i<data[master_column].total; ++i ) {
1239 BC_ListBoxItem *item = data[master_column].values[i];
1240 if( item->selected ) {
1241 //printf("BC_ListBox::get_selection_recursive %d\n", __LINE__);
1243 if( selection_number < 0 ) {
1245 return data[column].values[i];
1249 if( item->get_sublist() ) {
1250 BC_ListBoxItem *result = get_selection_recursive(item->get_sublist(),
1253 if( result ) return result;
1261 int BC_ListBox::get_selection_number(int column, int selection_number)
1263 return get_selection_number_recursive(data, column, selection_number);
1266 int BC_ListBox::get_selection_number_recursive(ArrayList<BC_ListBoxItem*> *data,
1267 int column, int selection_number, int *counter)
1270 if( !data ) return 0;
1271 if( !counter ) counter = &temp;
1273 for( int i=0; i<data[master_column].total; ++i ) {
1275 BC_ListBoxItem *item = data[master_column].values[i];
1276 if( item->selected ) {
1278 if( selection_number < 0 ) {
1282 if( item->get_sublist() ) {
1283 int result = get_selection_number_recursive(item->get_sublist(),
1284 column, selection_number, counter);
1285 if( result >= 0 ) return result;
1292 int BC_ListBox::set_selection_mode(int mode)
1294 this->selection_mode = mode;
1298 void BC_ListBox::delete_columns()
1300 if( column_titles ) {
1301 for( int i=0; i<columns; ++i ) {
1302 delete [] column_titles[i];
1304 delete [] column_titles;
1307 if( column_width ) delete [] column_width;
1313 // Need to copy titles so EDL can change
1314 void BC_ListBox::set_columns(const char **column_titles, int *column_width, int columns)
1316 if( (!column_titles && column_width) ||
1317 (column_titles && !column_width) ) {
1318 printf("BC_ListBox::set_columns either column_titles or column_width == NULL but not both.\n");
1323 if( column_titles ) {
1324 this->column_titles = new char*[columns];
1325 for( int i=0; i<columns; ++i ) {
1326 this->column_titles[i] = new char[strlen(column_titles[i]) + 1];
1327 strcpy(this->column_titles[i], column_titles[i]);
1330 if( column_width ) {
1331 this->column_width = new int[columns];
1332 for( int i=0; i<columns; ++i ) {
1333 this->column_width[i] = column_width[i];
1337 this->columns = columns;
1342 int BC_ListBox::update(ArrayList<BC_ListBoxItem*> *data,
1343 const char **column_titles, int *column_widths, int columns,
1344 int xposition, int yposition, int highlighted_number,
1345 int recalc_positions, int draw)
1347 set_columns(column_titles, column_widths, columns);
1349 this->yposition = yposition;
1350 this->xposition = xposition;
1351 this->highlighted_item = highlighted_number;
1352 this->highlighted_ptr = index_to_item(data, highlighted_number, 0);
1354 if( recalc_positions )
1355 set_autoplacement(data, 1, 1);
1357 init_column_width();
1361 update_scrollbars(1);
1367 void BC_ListBox::center_selection()
1369 int selection = get_selection_number(0, 0);
1370 calculate_item_coords();
1371 center_selection(selection);
1375 update_scrollbars(0);
1376 gui->show_window(1);
1380 void BC_ListBox::move_vertical(int pixels)
1384 void BC_ListBox::move_horizontal(int pixels)
1388 int BC_ListBox::select_previous(int skip, BC_ListBoxItem *selected_item, int *counter,
1389 ArrayList<BC_ListBoxItem*> *data, int *got_first, int *got_second)
1392 if( !selected_item )
1393 selected_item = get_selection(0, 0);
1404 got_second = &temp3;
1408 // Scan backwards to item pointer. Then count visible items to get
1409 // destination. No wraparound.
1411 for( int i=data[master_column].total-1; i>=0; --i ) {
1412 BC_ListBoxItem *current_item = data[master_column].values[i];
1413 if( current_item->get_sublist() &&
1414 current_item->get_expand() ) {
1415 int result = select_previous(skip, selected_item, counter,
1416 current_item->get_sublist(), got_first, got_second);
1423 if( (*counter) >= skip ) {
1424 for( int j=0; j<columns; ++j )
1425 data[j].values[i]->selected = 1;
1427 return item_to_index(this->data, current_item);
1431 if( current_item->selected ) {
1432 for( int j=0; j<columns; ++j )
1433 data[j].values[i]->selected = 0;
1440 // Hit top of top level without finding a selected item.
1442 // Select first item in top level and quit
1443 BC_ListBoxItem *current_item;
1445 current_item = data[master_column].values[0];
1447 for( int j=0; j<columns; ++j )
1448 data[j].values[0]->selected = 1;
1450 return item_to_index(this->data, current_item);
1452 } while( top_level && data[master_column].total );
1457 int BC_ListBox::select_next(int skip, BC_ListBoxItem *selected_item, int *counter,
1458 ArrayList<BC_ListBoxItem*> *data, int *got_first, int *got_second)
1461 if( !selected_item )
1462 selected_item = get_selection(0, 0);
1473 got_second = &temp3;
1477 // Scan forwards to currently selected item pointer.
1478 // Then count visible items to get destination. No wraparound.
1480 for( int i=0; i<data[master_column].total; ++i ) {
1481 BC_ListBoxItem *current_item = data[master_column].values[i];
1483 // Select next item once the number items after the currently selected item
1484 // have been passed.
1487 if( (*counter) >= skip ) {
1488 for( int j=0; j<columns; ++j )
1489 data[j].values[i]->selected = 1;
1491 return item_to_index(this->data, current_item);
1495 // Got currently selected item. Deselect it.
1496 if( current_item->selected ) {
1497 for( int j=0; j<columns; ++j )
1498 data[j].values[i]->selected = 0;
1504 // Descend into expanded level
1505 if( current_item->get_sublist() &&
1506 current_item->get_expand() ) {
1507 int result = select_next(skip, selected_item, counter,
1508 current_item->get_sublist(), got_first, got_second);
1515 // Hit bottom of top level without finding the next item.
1517 BC_ListBoxItem *current_item;
1518 // Select first item in top level and quit
1519 if( !(*got_first) ) {
1521 current_item = data[master_column].values[0];
1523 for( int j=0; j<columns; ++j )
1524 data[j].values[0]->selected = 1;
1528 // Select last item in top level and quit
1530 int current_row = data[master_column].total - 1;
1531 current_item = data[master_column].values[current_row];
1533 for( int j=0; j<columns; ++j )
1534 data[j].values[current_row]->selected = 1;
1538 return item_to_index(this->data, current_item);
1540 } while( top_level && data[master_column].total );
1546 void BC_ListBox::clamp_positions()
1548 items_w = get_items_width();
1549 if( xscroll_orientation & SCROLL_STRETCH )
1550 items_w += view_w / 4;
1551 items_h = get_items_height(data, columns);
1552 if( yscroll_orientation & SCROLL_STRETCH )
1553 items_h += view_h / 4;
1555 if( yposition < 0 ) yposition = 0;
1557 if( yposition > items_h - view_h )
1558 yposition = items_h - view_h;
1560 if( yposition < 0 ) yposition = 0;
1562 if( xposition < 0 ) xposition = 0;
1564 if( xposition >= items_w - view_w )
1565 xposition = items_w - view_w;
1567 if( xposition < 0 ) xposition = 0;
1570 int BC_ListBox::center_selection(int selection,
1571 ArrayList<BC_ListBoxItem*> *data, int *counter)
1574 if( !data ) data = this->data;
1575 if( !counter ) counter = &temp;
1577 for( int i=0; i<data[master_column].total; ++i ) {
1581 BC_ListBoxItem *item = data[master_column].values[i];
1582 if( (*counter) == selection ) {
1583 BC_ListBoxItem *top_item = this->data[master_column].values[0];
1586 if( display_format == LISTBOX_ICONS ) {
1587 // Icon is out of window
1588 if( item->icon_y-yposition > view_h-get_text_h(item) ||
1589 item->icon_y-yposition < 0 ) {
1590 yposition = item->icon_y - view_h / 2;
1593 if( data[master_column].values[selection]->icon_x - xposition > view_w ||
1594 data[master_column].values[selection]->icon_x - xposition < 0 ) {
1595 xposition = item->icon_x - view_w / 2;
1599 // Text coordinate is out of window
1600 if( item->text_y-yposition > view_h-get_text_h(item) ||
1601 item->text_y-yposition < 0 ) {
1602 yposition = item->text_y -
1611 if( item->get_sublist() ) {
1612 int result = center_selection(selection,
1613 item->get_sublist(),
1615 if( result ) return result;
1621 void BC_ListBox::update_scrollbars(int flush)
1623 int h_needed = items_h = get_items_height(data, columns);
1624 int w_needed = items_w = get_items_width();
1626 // if( columns > 0 && column_width )
1627 // printf("BC_ListBox::update_scrollbars 1 %d %d\n", column_width[columns - 1], w_needed);
1630 if( xposition != xscrollbar->get_value() )
1631 xscrollbar->update_value(xposition);
1633 if( w_needed != xscrollbar->get_length() ||
1634 view_w != xscrollbar->get_handlelength() )
1635 xscrollbar->update_length(w_needed, xposition, view_w, 0);
1639 if( yposition != yscrollbar->get_value() )
1640 yscrollbar->update_value(yposition);
1642 if( h_needed != yscrollbar->get_length() || view_h != yscrollbar->get_handlelength() )
1643 yscrollbar->update_length(h_needed, yposition, view_h, 0);
1646 if( flush ) this->flush();
1649 int BC_ListBox::get_scrollbars()
1651 int h_needed = items_h = get_items_height(data, columns);
1652 int w_needed = items_w = get_items_width();
1655 title_h = get_title_h();
1656 view_h = popup_h - title_h - 4;
1657 view_w = popup_w - 4;
1659 // Create scrollbars as needed
1660 for( int i=0; i<2; ++i ) {
1661 if( w_needed > view_w ) {
1665 get_resources()->hscroll_data[SCROLL_HANDLE_UP]->get_h() -
1672 if( h_needed > view_h ) {
1675 get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w() -
1683 // Update subwindow size
1684 int new_w = popup_w;
1685 int new_h = popup_h;
1686 if( need_xscroll ) new_h -= get_resources()->hscroll_data[SCROLL_HANDLE_UP]->get_h();
1687 if( need_yscroll ) new_w -= get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w();
1690 if( new_w != BC_WindowBase::get_w() || new_h != BC_WindowBase::get_h() )
1691 gui->resize_window(new_w, new_h);
1693 BC_WindowBase *destination = (is_popup ? gui : parent_window);
1694 if( need_xscroll ) {
1696 xscrollbar = new BC_ListBoxXScroll(this);
1697 destination->add_subwindow(xscrollbar);
1698 xscrollbar->show_window(0);
1699 xscrollbar->bound_to = this;
1702 xscrollbar->update_length(w_needed, xposition, view_w, flush);
1703 xscrollbar->reposition_window(get_xscroll_x(),
1705 get_xscroll_width());
1709 if( xscrollbar ) delete xscrollbar;
1714 if( need_yscroll ) {
1716 yscrollbar = new BC_ListBoxYScroll(this);
1717 destination->add_subwindow(yscrollbar);
1718 yscrollbar->show_window(0);
1719 yscrollbar->bound_to = this;
1722 yscrollbar->update_length(h_needed, yposition, view_h, flush);
1723 yscrollbar->reposition_window(get_yscroll_x(),
1725 get_yscroll_height());
1729 if( yscrollbar ) delete yscrollbar;
1735 view_w + 4 != bg_surface->get_w() ||
1736 view_h + 4 != bg_surface->get_h() ) {
1737 if( bg_surface ) delete bg_surface;
1738 bg_surface = new BC_Pixmap(gui, view_w + 4, view_h + 4);
1747 void BC_ListBox::set_drag_scroll(int value)
1749 allow_drag_scroll = value;
1752 // Test for scrolling by dragging
1754 int BC_ListBox::test_drag_scroll(int cursor_x, int cursor_y)
1757 if( allow_drag_scroll ||
1758 current_operation == SELECT_RECT ) {
1760 int top_boundary = get_title_h();
1762 if( cursor_y < top_boundary ||
1763 cursor_y >= view_h + title_h + LISTBOX_BORDER * 2 ||
1764 cursor_x < LISTBOX_BORDER ||
1765 cursor_x >= view_w + LISTBOX_BORDER ) {
1772 int BC_ListBox::drag_scroll_event()
1774 int top_boundary = get_title_h();
1777 if( get_cursor_y() < top_boundary ) {
1778 yposition -= top_boundary - get_cursor_y();
1782 if( get_cursor_y() >= view_h + title_h + 4 ) {
1783 yposition += get_cursor_y() - (view_h + title_h + 4);
1787 if( get_cursor_x() < 2 ) {
1788 xposition -= 2 - get_cursor_x();
1792 if( get_cursor_x() >= view_w + 2 ) {
1793 xposition += get_cursor_x() - (view_w + 2);
1803 int BC_ListBox::rectangle_scroll_event()
1805 int old_xposition = xposition;
1806 int old_yposition = yposition;
1807 int result = drag_scroll_event();
1810 rect_x1 += old_xposition - xposition;
1811 rect_y1 += old_yposition - yposition;
1812 rect_x2 = get_cursor_x();
1813 rect_y2 = get_cursor_y();
1815 int x1 = MIN(rect_x1, rect_x2);
1816 int x2 = MAX(rect_x1, rect_x2);
1817 int y1 = MIN(rect_y1, rect_y2);
1818 int y2 = MAX(rect_y1, rect_y2);
1820 if( select_rectangle(data, x1, y1, x2, y2) ) {
1821 selection_changed();
1826 update_scrollbars(1);
1831 int BC_ListBox::select_scroll_event()
1833 int result = drag_scroll_event();
1835 highlighted_item = selection_number = get_cursor_item(data,
1836 get_cursor_x(), get_cursor_y(), &highlighted_ptr);
1839 update_scrollbars(1);
1840 selection_changed();
1845 int BC_ListBox::select_rectangle(ArrayList<BC_ListBoxItem*> *data,
1846 int x1, int y1, int x2, int y2)
1849 for( int i=0; i<data[master_column].total; ++i ) {
1850 for( int j=0; j<columns; ++j ) {
1851 BC_ListBoxItem *item = data[j].values[i];
1852 if( display_format == LISTBOX_ICONS ) {
1853 int icon_x, icon_y, icon_w, icon_h;
1854 int text_x, text_y, text_w, text_h;
1855 get_icon_mask(item, icon_x, icon_y, icon_w, icon_h);
1856 get_text_mask(item, text_x, text_y, text_w, text_h);
1858 if( (x2 >= icon_x && x1 < icon_x + icon_w &&
1859 y2 >= icon_y && y1 < icon_y + icon_h) ||
1860 (x2 >= text_x && x1 < text_x + text_w &&
1861 y2 >= text_y && y1 < text_y + text_h) ) {
1862 if( !item->selected ) {
1868 if( item->selected ) {
1877 gui->get_w() - get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w() :
1878 gui->get_w()) && y2 > 0 &&
1879 y1 < gui->get_h() && y2 >= get_item_y(item) &&
1880 y1 < get_item_y(item) + get_item_h(item) ) {
1881 if( !item->selected ) {
1887 if( item->selected ) {
1895 BC_ListBoxItem *item = data[master_column].values[i];
1896 if( item->get_sublist() &&
1897 item->get_expand() )
1898 result |= select_rectangle(item->get_sublist(),
1904 int BC_ListBox::reposition_item(ArrayList<BC_ListBoxItem*> *data,
1905 int selection_number, int x, int y, int *counter)
1908 if( !counter ) counter = &temp;
1910 for( int i=0; i<data[master_column].total; ++i ) {
1911 BC_ListBoxItem *item = data[master_column].values[i];
1913 if( (*counter) == selection_number ) {
1918 // Not recursive because it's only used for icons
1923 void BC_ListBox::move_selection(ArrayList<BC_ListBoxItem*> *dst,
1924 ArrayList<BC_ListBoxItem*> *src)
1926 for( int i=0; i<src[master_column].total; ) {
1927 BC_ListBoxItem *item = src[master_column].values[i];
1930 if( item->selected ) {
1931 for( int j=0; j<columns; ++j ) {
1932 dst[j].append(src[j].values[i]);
1933 src[j].remove_number(i);
1937 // Descend into sublist
1938 if( item->get_sublist() ) {
1940 item->get_sublist());
1946 int BC_ListBox::put_selection(ArrayList<BC_ListBoxItem*> *data,
1947 ArrayList<BC_ListBoxItem*> *src, int destination, int *counter)
1950 if( !counter ) counter = &temp;
1952 if( destination < 0 || destination >= data[master_column].total ) {
1953 for( int j=0; j<columns; ++j ) {
1954 for( int i=0; i<src[j].total; ++i ) {
1955 data[j].append(src[j].values[i]);
1961 for( int i=0; i<data[master_column].total; ++i ) {
1963 if( (*counter) == destination ) {
1964 for( int j=0; j<columns; ++j ) {
1965 for( int k=0; k<src[j].total; ++k ) {
1966 data[j].insert(src[j].values[k], destination + k);
1972 BC_ListBoxItem *item = data[master_column].values[i];
1973 if( item->get_sublist() ) {
1974 if( put_selection(item->get_sublist(),
1986 int BC_ListBox::item_to_index(ArrayList<BC_ListBoxItem*> *data,
1987 BC_ListBoxItem *item, int *counter)
1990 if( !counter ) counter = &temp;
1992 for( int i=0; i<data[master_column].total; ++i ) {
1994 for( int j=0; j<columns; ++j ) {
1995 BC_ListBoxItem *new_item = data[j].values[i];
1996 //printf("BC_ListBox::item_to_index 1 %d %d %p\n", j, i, new_item);
1997 if( new_item == item ) {
2002 BC_ListBoxItem *new_item = data[master_column].values[i];
2003 if( new_item->get_sublist() ) {
2004 if( item_to_index(new_item->get_sublist(),
2014 BC_ListBoxItem* BC_ListBox::index_to_item(ArrayList<BC_ListBoxItem*> *data,
2015 int number, int column, int *counter)
2018 if( !counter ) counter = &temp;
2019 for( int i=0; i<data[master_column].total; ++i ) {
2021 if( (*counter) == number ) {
2022 return data[column].values[i];
2024 BC_ListBoxItem *item = data[master_column].values[i];
2025 if( item->get_sublist() ) {
2026 BC_ListBoxItem *result = index_to_item(item->get_sublist(),
2027 number, column, counter);
2028 if( result ) return result;
2034 int BC_ListBox::get_cursor_item(ArrayList<BC_ListBoxItem*> *data, int cursor_x, int cursor_y,
2035 BC_ListBoxItem **item_return, int *counter, int expanded)
2038 if( !data ) return -1;
2039 if( !counter ) counter = &temp;
2041 // Icons are not treed
2042 if( display_format == LISTBOX_ICONS ) {
2043 for( int j=data[master_column].total-1; j>=0; --j ) {
2044 int icon_x, icon_y, icon_w, icon_h;
2045 int text_x, text_y, text_w, text_h;
2046 BC_ListBoxItem *item = data[master_column].values[j];
2047 get_icon_mask(item, icon_x, icon_y, icon_w, icon_h);
2048 get_text_mask(item, text_x, text_y, text_w, text_h);
2050 if( (cursor_x >= icon_x && cursor_x < icon_x + icon_w &&
2051 cursor_y >= icon_y && cursor_y < icon_y + icon_h) ||
2052 (cursor_x >= text_x && cursor_x < text_x + text_w &&
2053 cursor_y >= text_y && cursor_y < text_y + text_h) ) {
2054 if( item_return ) (*item_return) = item;
2061 // Cursor is inside items rectangle
2062 if( cursor_x >= 0 &&
2063 cursor_x < (yscrollbar ?
2064 gui->get_w() - get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w() :
2066 // Only clamp y if we're not in a SELECT operation.
2067 (current_operation == BC_ListBox::SELECT ||
2068 (cursor_y > get_title_h() + LISTBOX_BORDER &&
2069 cursor_y < gui->get_h())) ) {
2070 // Search table for cursor obstruction
2071 for( int i=0; i<data[master_column].total; ++i ) {
2072 BC_ListBoxItem *item = data[master_column].values[i];
2075 // Cursor is inside item on current level
2076 if( expanded && item->selectable &&
2077 cursor_y >= get_item_y(item) &&
2078 cursor_y < get_item_y(item) + get_item_h(item) ) {
2079 //printf("BC_ListBox::get_cursor_item %d %d %p\n", master_column, i, item);
2080 if( item_return ) (*item_return) = item;
2084 // Descend into sublist
2085 if( item->get_sublist() ) {
2086 if( get_cursor_item(item->get_sublist(),
2087 cursor_x, cursor_y, item_return, counter,
2088 item->get_expand()) >= 0 )
2098 int BC_ListBox::repeat_event(int64_t duration)
2100 switch( current_operation ) {
2101 // Repeat out of bounds selection
2103 if( duration != get_resources()->scroll_repeat ) break;
2104 return rectangle_scroll_event();
2107 if( duration != get_resources()->scroll_repeat ) break;
2108 if( !drag_scroll_event() ) break;
2111 update_scrollbars(1);
2115 if( duration != get_resources()->scroll_repeat ) break;
2116 return select_scroll_event();
2120 if( duration != get_resources()->tooltip_delay ) break;
2121 if( !button_highlighted || !is_popup ) break;
2122 if( !tooltip_text || !tooltip_text[0] ) break;
2130 int BC_ListBox::cursor_enter_event()
2134 switch( current_operation ) {
2135 // Cursor moved over button, pressed, and exited.
2136 case BUTTON_DOWN_SELECT:
2137 if( top_level->event_win == win ) {
2138 current_operation = BUTTON_DN;
2140 button_highlighted = 1;
2146 // Cursor entered button
2147 if( is_popup && top_level->event_win == win ) {
2148 button_highlighted = 1;
2153 // TODO: Need to get the highlighted column title or item
2154 if( gui && top_level->event_win == gui->win ) {
2155 list_highlighted = 1;
2165 int BC_ListBox::cursor_leave_event()
2167 if( current_operation == COLUMN_DRAG ) return 0;
2170 if( button_highlighted ) {
2171 button_highlighted = 0;
2176 if( list_highlighted ) {
2177 list_highlighted = 0;
2178 highlighted_item = -1;
2179 highlighted_ptr = 0;
2180 highlighted_title = -1;
2181 int redraw_toggles = 0;
2182 for( int i=0; i<expanders.total; ++i )
2183 expanders.values[i]->cursor_leave_event(&redraw_toggles);
2191 int BC_ListBox::get_first_selection(ArrayList<BC_ListBoxItem*> *data, int *result)
2194 if( !result ) result = &temp;
2196 for( int i=0; i<data[master_column].total; ++i ) {
2197 BC_ListBoxItem *item = data[master_column].values[i];
2199 if( item->selected ) return (*result);
2200 if( item->get_sublist() ) {
2201 if( get_first_selection(item->get_sublist(), result) >= 0 )
2208 int BC_ListBox::get_total_items(ArrayList<BC_ListBoxItem*> *data,
2213 if( !result ) result = &temp;
2215 for( int i=0; i<data[master_column].total; ++i ) {
2217 if( data[master_column].values[i]->get_sublist() )
2218 get_total_items(data[master_column].values[i]->get_sublist(),
2227 int BC_ListBox::get_last_selection(ArrayList<BC_ListBoxItem*> *data,
2237 for( int i=data[master_column].total-1; i>=0; --i ) {
2238 BC_ListBoxItem *item = data[master_column].values[i];
2240 if( item->selected ) {
2242 return get_total_items(data, 0, master_column) - (*result) /* - 1 */;
2247 if( item->get_sublist() ) {
2248 if( get_last_selection(item->get_sublist(), result) >= 0 ) {
2250 return get_total_items(data, 0, master_column) - (*result) /* - 1 */;
2259 void BC_ListBox::select_range(ArrayList<BC_ListBoxItem*> *data,
2260 int start, int end, int *current)
2263 if( !current ) current = &temp;
2265 for( int i=0; i<data[master_column].total; ++i ) {
2267 if( (*current) >= start && (*current) < end ) {
2268 for( int j=0; j<columns; ++j )
2269 data[j].values[i]->selected = 1;
2271 BC_ListBoxItem *item = data[master_column].values[i];
2272 if( item->get_sublist() )
2273 select_range(item->get_sublist(), start, end, current);
2278 // Fill items between current selection and new selection
2279 int BC_ListBox::expand_selection(int button_press, int selection_number)
2281 int old_selection_start = selection_start;
2282 int old_selection_end = selection_end;
2284 // printf("BC_ListBox::expand_selection %d %d\n",
2285 // selection_center,
2286 // selection_number);
2288 // Calculate the range to select based on selection_center and selection_number
2289 if( selection_number < selection_center ) {
2290 selection_start = selection_number;
2293 selection_end = selection_number + 1;
2296 //printf("BC_ListBox::expand_selection %d %d %d %d\n", old_selection_start, old_selection_end, selection_start, selection_end);
2297 // Recurse through all the items and select the desired range
2298 select_range(data, selection_start, selection_end);
2301 return (old_selection_start != selection_start ||
2302 old_selection_end != selection_end);
2305 int BC_ListBox::toggle_item_selection(ArrayList<BC_ListBoxItem*> *data,
2306 int selection_number, int *counter)
2309 if( !counter ) counter = &temp;
2311 for( int i=0; i<data[master_column].total; ++i ) {
2312 BC_ListBoxItem *item = data[master_column].values[i];
2314 if( (*counter) == selection_number ) {
2315 // Get new value for selection
2316 int selected = !item->selected;
2318 for( int j=0; j<columns; ++j )
2319 data[j].values[i]->selected = selected;
2323 // Descend into sublist
2324 if( item->get_sublist() ) {
2325 if( toggle_item_selection(item->get_sublist(),
2326 selection_number, counter) )
2335 void BC_ListBox::set_all_selected(ArrayList<BC_ListBoxItem*> *data, int value)
2337 for( int i=0; i<data[master_column].total; ++i ) {
2338 for( int j=0; j<columns; ++j ) {
2339 BC_ListBoxItem *item = data[j].values[i];
2340 item->selected = value;
2342 BC_ListBoxItem *item = data[master_column].values[i];
2343 if( item->get_sublist() ) {
2344 set_all_selected(item->get_sublist(), value);
2349 void BC_ListBox::set_selected(ArrayList<BC_ListBoxItem*> *data,
2350 int item_number, int value, int *counter)
2353 if( !counter ) counter = &temp;
2354 for( int i=0; i<data[master_column].total&&(*counter)!=item_number; ++i ) {
2356 if( (*counter) == item_number ) {
2357 for( int j=0; j<columns; ++j ) {
2358 BC_ListBoxItem *item = data[j].values[i];
2359 item->selected = value;
2364 BC_ListBoxItem *item = data[master_column].values[i];
2365 if( item->get_sublist() ) {
2366 set_selected(item->get_sublist(), item_number, value, counter);
2371 int BC_ListBox::update_selection(ArrayList<BC_ListBoxItem*> *data,
2372 int selection_number, int *counter)
2376 if( !counter ) counter = &temp;
2378 for( int i=0; i<data[master_column].total; ++i ) {
2379 BC_ListBoxItem *item = data[master_column].values[i];
2381 if( (*counter) == selection_number && !item->selected ) {
2383 for( int j=0; j<columns; ++j )
2384 data[j].values[i]->selected = 1;
2387 if( (*counter) != selection_number && item->selected ) {
2389 for( int j=0; j<columns; ++j )
2390 data[j].values[i]->selected = 0;
2392 if( item->get_sublist() )
2393 result |= update_selection(item->get_sublist(),
2400 void BC_ListBox::promote_selections(ArrayList<BC_ListBoxItem*> *data,
2401 int old_value, int new_value)
2403 for( int i=0; i<data[master_column].total; ++i ) {
2404 for( int j=0; j<columns; ++j ) {
2405 BC_ListBoxItem *item = data[j].values[i];
2406 if( item->selected == old_value ) item->selected = new_value;
2408 BC_ListBoxItem *item = data[master_column].values[i];
2409 if( item->get_sublist() )
2410 promote_selections(item->get_sublist(), old_value, new_value);
2414 int BC_ListBox::focus_out_event()
2420 int BC_ListBox::button_press_event()
2423 BC_ListBoxItem *current_item = 0;
2425 int do_selection_change = 0;
2426 const int debug = 0;
2429 if( debug ) printf("BC_ListBox::button_press_event %d this=%p event_win=%p %p %p %p\n",
2430 __LINE__, this, (void*)top_level->event_win,
2431 (void*)(gui ? gui->win : 0), (void*)win, (void*)parent_window->win);
2433 // Pressed in button
2434 if( is_popup && top_level->event_win == win ) {
2435 if( debug ) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2436 current_operation = BUTTON_DN;
2440 if( !active && !disabled ) {
2441 top_level->deactivate();
2448 // Pressed in scrollbar
2449 if( (xscrollbar && top_level->event_win == xscrollbar->win) ||
2450 (yscrollbar && top_level->event_win == yscrollbar->win) ) {
2451 if( debug ) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2456 if( gui && top_level->event_win == gui->win ) {
2457 if( debug ) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2459 // Activate list items
2460 // If it is a suggestion popup, it is visible without being active
2462 if( debug ) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2463 if( !is_suggestions ) top_level->deactivate();
2464 if( debug ) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2466 if( debug ) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2469 if( current_operation != NO_OPERATION ) {
2470 switch( current_operation ) {
2473 return drag_stop_event();
2477 // Wheel mouse pressed
2478 if( get_buttonpress() == 4 ) {
2479 if( current_operation == NO_OPERATION ) {
2480 current_operation = WHEEL;
2482 set_yposition(yposition - gui->get_h() / 10, 0);
2484 update_scrollbars(0);
2485 highlighted_ptr = 0;
2486 highlighted_item = get_cursor_item(data,
2487 top_level->cursor_x, top_level->cursor_y,
2495 if( get_buttonpress() == 5 ) {
2496 if( current_operation == NO_OPERATION ) {
2497 current_operation = WHEEL;
2499 set_yposition(yposition + gui->get_h() / 10, 0);
2501 update_scrollbars(0);
2502 highlighted_ptr = 0;
2503 highlighted_item = get_cursor_item(data,
2504 top_level->cursor_x, top_level->cursor_y,
2512 // Pressed over column title division
2513 if( test_column_divisions(gui->get_cursor_x(),
2514 gui->get_cursor_y(),
2516 drag_cursor_x = gui->get_cursor_x() + xposition;
2518 drag_column_w = column_width[highlighted_division - 1];
2520 drag_column_w = default_column_width[highlighted_division - 1];
2522 current_operation = DRAG_DIVISION;
2526 // Pressed in column title
2527 if( test_column_titles(gui->get_cursor_x(), gui->get_cursor_y()) ) {
2528 current_operation = COLUMN_DN;
2529 button_highlighted = 0;
2530 list_highlighted = 1;
2535 // Pressed in expander
2536 if( test_expanders() ) {
2537 current_operation = EXPAND_DN;
2538 // Need to redraw items because of alpha
2543 // Pressed over item
2544 if( (selection_number = get_cursor_item(data,
2545 gui->get_cursor_x(), gui->get_cursor_y(),
2546 ¤t_item)) >= 0 ) {
2547 if( debug ) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2549 // Get item button was pressed over
2550 selection_number2 = selection_number1;
2551 selection_number1 = selection_number;
2553 selection_start = -1;
2557 // Multiple item selection is possible
2558 if( selection_mode == LISTBOX_MULTIPLE &&
2559 (ctrl_down() || shift_down()) ) {
2560 // Expand text selection.
2561 // Fill items between selected region and current item.
2563 (display_format == LISTBOX_TEXT ||
2564 display_format == LISTBOX_ICON_LIST) ) {
2565 // Get first item selected
2566 selection_start = get_first_selection(data);
2567 // Get last item selected
2568 selection_end = get_last_selection(data);
2569 // Get center of selected region
2570 if( selection_end > selection_start ) {
2571 selection_center = (selection_end + selection_start) >> 1;
2574 selection_center = selection_number;
2576 // Deselect everything.
2577 set_all_selected(data, 0);
2578 // Select just the items
2579 expand_selection(1, selection_number);
2582 // Toggle a single item on or off
2584 toggle_item_selection(data, selection_number);
2585 new_value = current_item->selected;
2588 // Select single item
2590 if( !current_item->selected ) {
2591 set_all_selected(data, 0);
2600 current_operation = SELECT;
2601 highlighted_item = selection_number;
2602 highlighted_ptr = current_item;
2603 button_highlighted = 0;
2604 list_highlighted = 1;
2607 do_selection_change = 1;
2611 // Pressed over nothing. Start rectangle selection.
2613 if( get_buttonpress() == 1 &&
2614 selection_mode == LISTBOX_MULTIPLE ) {
2615 if( !shift_down() ) {
2616 // Deselect all and redraw if anything was selected
2617 if( get_selection_number(0, 0) >= 0 ) {
2618 set_all_selected(data, 0);
2620 do_selection_change = 1;
2625 // Promote selections to protect from a rectangle selection
2626 promote_selections(data, 1, 2);
2629 // Start rectangle selection
2630 current_operation = SELECT_RECT;
2631 rect_x1 = rect_x2 = get_cursor_x();
2632 rect_y1 = rect_y2 = get_cursor_y();
2640 // Suggestion box is not active but visible, so lie to get it to deactivate
2641 if( is_popup && (active || (is_suggestions && gui)) ) {
2643 if( debug ) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2649 if( do_selection_change ) selection_changed();
2650 if( debug ) printf("BC_ListBox::button_press_event %d %d\n", __LINE__, result);
2655 int BC_ListBox::button_release_event()
2658 int cursor_x, cursor_y;
2661 unset_scroll_repeat();
2663 //printf("BC_ListBox::button_release_event 1 %d\n", current_operation);
2664 switch( current_operation ) {
2666 current_operation = NO_OPERATION;
2671 current_operation = NO_OPERATION;
2675 // Release item selection
2676 case BUTTON_DOWN_SELECT:
2678 //printf("BC_ListBox::button_release_event 10\n");
2679 current_operation = NO_OPERATION;
2681 translate_coordinates(top_level->event_win, gui->win,
2682 gui->get_cursor_x(), gui->get_cursor_y(),
2683 &cursor_x, &cursor_y);
2684 selection_number1 = selection_number =
2685 get_cursor_item(data, cursor_x, cursor_y);
2686 //printf("BC_ListBox::button_release_event %d %d\n", selection_number2, selection_number1);
2691 if( selection_number >= 0 ) {
2696 // Second button release outside button
2697 if( button_releases > 1 ) {
2702 if( top_level->get_double_click() &&
2703 selection_number2 == selection_number1 &&
2704 selection_number2 >= 0 && selection_number1 >= 0 ) {
2714 // Demote selections from rectangle selection
2715 promote_selections(data, 2, 1);
2718 // Hide rectangle overlay
2720 current_operation = NO_OPERATION;
2724 // Release popup button
2727 current_operation = NO_OPERATION;
2731 // Second button release inside button
2732 if( button_releases > 1 ) {
2739 current_operation = NO_OPERATION;
2740 // Update the sort column and the sort order for the user only if the existing
2741 // sort column is valid.
2742 if( sort_column >= 0 ) {
2743 // Invert order only if column is the same
2744 if( highlighted_title == sort_column )
2745 sort_order = sort_order == SORT_ASCENDING ?
2746 SORT_DESCENDING : SORT_ASCENDING;
2747 // Set the new sort column
2748 sort_column = highlighted_title;
2749 if( !sort_order_event() ) {
2754 // Sorting not enabled. Redraw the title state.
2762 int redraw_toggles = 0;
2763 for( int i=0; i<expanders.total&&!result; ++i ) {
2764 if( expanders.values[i]->button_release_event(&redraw_toggles) ) {
2768 // Need to redraw items because of alpha
2769 if( redraw_toggles ) draw_items(1);
2770 current_operation = NO_OPERATION;
2774 // Can't default to NO_OPERATION because it may be used in a drag event.
2779 if( do_event ) handle_event();
2781 //printf("BC_ListBox::button_release_event %d %d\n", __LINE__, get_window_lock());
2785 int BC_ListBox::get_title_h()
2787 if( display_format == LISTBOX_TEXT ||
2788 display_format == LISTBOX_ICON_LIST )
2789 return column_titles ? column_bg[0]->get_h() : 0;
2793 void BC_ListBox::reset_cursor(int new_cursor)
2796 if( gui->get_cursor() != new_cursor ) {
2797 gui->set_cursor(new_cursor, 0, 0);
2801 if( get_cursor() != new_cursor ) {
2802 set_cursor(new_cursor, 0, 0);
2806 int BC_ListBox::test_column_divisions(int cursor_x, int cursor_y, int &new_cursor)
2808 if( gui && column_titles &&
2809 cursor_y >= 0 && cursor_y < get_title_h() &&
2810 cursor_x >= 0 && cursor_x < gui->get_w() ) {
2811 for( int i=1; i<columns; ++i ) {
2812 if( cursor_x >= -xposition + get_column_offset(i) - 5 &&
2813 cursor_x < -xposition + get_column_offset(i) +
2814 get_resources()->listbox_title_hotspot ) {
2815 highlighted_item = -1;
2816 highlighted_ptr = 0;
2817 highlighted_division = i;
2818 highlighted_title = -1;
2819 list_highlighted = 1;
2820 new_cursor = HSEPARATE_CURSOR;
2825 highlighted_division = -1;
2829 int BC_ListBox::test_column_titles(int cursor_x, int cursor_y)
2831 if( gui && column_titles &&
2832 cursor_y >= 0 && cursor_y < get_title_h() &&
2833 cursor_x >= 0 && cursor_x < gui->get_w() ) {
2834 for( int i=0; i<columns; ++i ) {
2835 if( cursor_x >= -xposition + get_column_offset(i) &&
2836 (cursor_x < -xposition + get_column_offset(i + 1) ||
2837 i == columns - 1) ) {
2838 highlighted_item = -1;
2839 highlighted_ptr = 0;
2840 highlighted_division = -1;
2841 highlighted_title = i;
2842 list_highlighted = 1;
2847 highlighted_title = -1;
2851 int BC_ListBox::test_expanders()
2853 for( int i=0; i<expanders.total; ++i ) {
2854 if( expanders.values[i]->button_press_event() ) {
2855 current_operation = EXPAND_DN;
2863 int BC_ListBox::cursor_motion_event()
2865 int redraw = 0, result = 0;
2866 int new_cursor = ARROW_CURSOR;
2868 selection_number = -1;
2871 switch( current_operation ) {
2873 // Button pressed and slid off button
2874 if( !cursor_inside() ) {
2875 current_operation = BUTTON_DOWN_SELECT;
2881 case DRAG_DIVISION: {
2882 // int new_w = get_cursor_x() +
2884 // get_column_offset(highlighted_division - 1);
2885 int difference = get_cursor_x() + xposition - drag_cursor_x;
2886 int new_w = drag_column_w + difference;
2888 new_cursor = HSEPARATE_CURSOR;
2890 if( column_width ) {
2891 column_width[highlighted_division - 1] = new_w;
2894 default_column_width[highlighted_division - 1] = new_w;
2897 column_width_boundaries();
2899 // Force update of coords
2900 set_autoplacement(data, 0, 1);
2901 column_resize_event();
2905 update_scrollbars(1);
2910 if( test_drag_scroll(get_cursor_x(), get_cursor_y()) )
2911 set_scroll_repeat();
2913 int old_x1 = MIN(rect_x1, rect_x2);
2914 int old_x2 = MAX(rect_x1, rect_x2);
2915 int old_y1 = MIN(rect_y1, rect_y2);
2916 int old_y2 = MAX(rect_y1, rect_y2);
2918 int new_rect_x2 = get_cursor_x();
2919 int new_rect_y2 = get_cursor_y();
2921 int x1 = MIN(rect_x1, new_rect_x2);
2922 int x2 = MAX(rect_x1, new_rect_x2);
2923 int y1 = MIN(rect_y1, new_rect_y2);
2924 int y2 = MAX(rect_y1, new_rect_y2);
2926 // Adjust rectangle coverage
2927 if( old_x1 != x1 || old_x2 != x2 ||
2928 old_y1 != y1 || old_y2 != y2 ) {
2930 redraw = select_rectangle(data, x1, y1, x2, y2);
2935 //printf("BC_ListBox::cursor_motion_event %d\n", __LINE__);
2940 rect_x2 = get_cursor_x();
2941 rect_y2 = get_cursor_y();
2945 update_scrollbars(1);
2946 selection_changed();
2947 //printf("BC_ListBox::cursor_motion_event %d\n", __LINE__);
2950 if( old_x1 != x1 || old_x2 != x2 ||
2951 old_y1 != y1 || old_y2 != y2 ) {
2952 //printf("BC_ListBox::cursor_motion_event %d\n", __LINE__);
2960 int old_highlighted_item = highlighted_item;
2962 if( test_drag_scroll(get_cursor_x(), get_cursor_y()) )
2963 set_scroll_repeat();
2965 highlighted_item = selection_number = get_cursor_item(data,
2966 get_cursor_x(), get_cursor_y(), &highlighted_ptr);
2969 // Deselect all items and select just the one we're over
2970 if( selection_number >= 0 && !allow_drag &&
2971 ((!shift_down() && !ctrl_down()) ||
2972 selection_mode == LISTBOX_SINGLE) ) {
2973 redraw = update_selection(data, selection_number);
2976 // Expand multiple selection
2977 if( selection_mode == LISTBOX_MULTIPLE &&
2978 (shift_down() || ctrl_down()) ) {
2979 // Expand selected region in text mode centered around initial range
2980 if( (display_format == LISTBOX_TEXT ||
2981 display_format == LISTBOX_ICON_LIST) &&
2983 // Deselect everything.
2984 set_all_selected(data, 0);
2986 // Select just the items
2987 redraw = expand_selection(0, selection_number);
2989 // Set the one item we're over to the selection value determined in
2990 // button_press_event.
2992 set_selected(data, selection_number, new_value);
2996 if( highlighted_item != old_highlighted_item ) {
2999 update_scrollbars(1);
3000 //printf("BC_ListBox::cursor_motion_event %d %d\n", highlighted_item, old_highlighted_item);
3001 selection_changed();
3005 case BUTTON_DOWN_SELECT:
3006 // Went back into button area
3007 if( cursor_inside() ) {
3008 current_operation = BUTTON_DN;
3013 // Went into item area
3015 int cursor_x = 0, cursor_y = 0;
3016 translate_coordinates(top_level->event_win, gui->win,
3017 top_level->cursor_x, top_level->cursor_y,
3018 &cursor_x, &cursor_y);
3019 int old_highlighted_item = highlighted_item;
3020 highlighted_item = selection_number = get_cursor_item(data,
3025 if( highlighted_item != old_highlighted_item ) {
3026 update_selection(data, selection_number);
3028 selection_changed();
3034 int redraw_toggles = 0;
3035 for( int i=0; i<expanders.total&&!result; ++i ) {
3036 result = expanders.values[i]->cursor_motion_event(
3039 if( redraw_toggles ) {
3040 // Need to redraw items because of the alpha
3045 case NO_OPERATION: {
3046 int cursor_x = get_cursor_x(), cursor_y = get_cursor_y();
3047 if( gui && top_level->event_win == gui->win ) {
3048 int old_highlighted_title = highlighted_title;
3049 int old_list_highlighted = list_highlighted;
3050 int old_highlighted_item = highlighted_item;
3051 int redraw_titles = 0;
3052 int redraw_border = 0;
3053 int redraw_items = 0;
3054 int redraw_toggles = 0;
3057 // Test if cursor moved over a title division
3058 test_column_divisions(cursor_x, cursor_y, new_cursor);
3060 // Test if cursor moved over a title
3061 if( highlighted_division < 0 ) {
3062 test_column_titles(cursor_x, cursor_y);
3065 // Test if cursor moved over expander
3066 if( highlighted_division < 0 && highlighted_title < 0 &&
3067 (display_format == LISTBOX_TEXT ||
3068 display_format == LISTBOX_ICON_LIST) ) {
3069 for( int i=0; i<expanders.total; ++i ) {
3070 expanders.values[i]->cursor_motion_event(
3073 //printf("BC_ListBox::cursor_motion_event %d\n", redraw_toggles);
3076 // Test if cursor moved over an item
3077 if( highlighted_division < 0 && highlighted_title < 0 ) {
3078 highlighted_item = get_cursor_item(data,
3085 // Clear title highlighting if moved over division
3086 if( old_highlighted_title != highlighted_title ) {
3090 // Highlight list border
3091 if( old_list_highlighted != list_highlighted ) {
3095 // Moved out of item area
3096 if( old_highlighted_item != highlighted_item ) {
3100 //printf("BC_ListBox::cursor_motion_event 1 %d\n", highlighted_item);
3102 // Change cursor to title division adjustment
3103 reset_cursor(new_cursor);
3105 if( redraw_items ) {
3113 if( redraw_toggles )
3117 if( redraw_items || redraw_titles ||
3118 redraw_border || redraw_toggles ) {
3125 if( !result && list_highlighted ) {
3126 list_highlighted = 0;
3127 highlighted_item = -1;
3128 highlighted_ptr = 0;
3129 highlighted_title = -1;
3130 highlighted_division = -1;
3140 int BC_ListBox::drag_start_event()
3142 switch( current_operation ) {
3144 if( gui && gui->is_event_win() && allow_drag ) {
3145 BC_ListBoxItem *item_return = 0;
3146 selection_number = get_cursor_item(data,
3147 top_level->cursor_x,
3148 top_level->cursor_y,
3151 if( selection_number >= 0 ) {
3153 get_abs_cursor(cx, cy);
3154 if( item_return->icon_vframe ) {
3155 drag_popup = new BC_DragWindow(this,
3156 item_return->icon_vframe, cx, cy);
3159 if( item_return->icon ) {
3160 drag_popup = new BC_DragWindow(this,
3161 item_return->icon, cx, cy);
3164 drag_popup = new BC_DragWindow(this,
3165 drag_icon_vframe, cx, cy);
3167 current_operation = DRAG_ITEM;
3168 // require shift down for scrolling
3169 if( allow_drag < 0 && shift_down() )
3170 set_scroll_repeat();
3177 if( gui && gui->is_event_win() && allow_drag_column ) {
3179 get_abs_cursor(cx, cy);
3180 cx -= drag_column_icon_vframe->get_w() / 2,
3181 cy -= drag_column_icon_vframe->get_h() / 2;
3182 drag_popup = new BC_DragWindow(this,
3183 drag_column_icon_vframe, cx, cy);
3184 dragged_title = highlighted_title;
3185 current_operation = COLUMN_DRAG;
3195 int BC_ListBox::drag_motion_event()
3197 //printf("BC_ListBox::drag_motion_event 1 %d\n", current_operation);
3198 switch( current_operation ) {
3201 int new_highlighted_item = -1;
3202 BC_ListBoxItem *new_highlighted_ptr = 0;
3203 new_highlighted_item = get_cursor_item(data,
3204 top_level->cursor_x, top_level->cursor_y,
3205 &new_highlighted_ptr);
3207 if( new_highlighted_item != highlighted_item ) {
3211 // Always update highlighted value for drag_stop
3212 highlighted_item = new_highlighted_item;
3213 highlighted_ptr = new_highlighted_ptr;
3214 //printf("BC_ListBox::drag_motion_event 1 %p\n", highlighted_ptr);
3218 update_scrollbars(1);
3221 return drag_popup->cursor_motion_event(); }
3224 int old_highlighted_title = highlighted_title;
3225 test_column_titles(get_cursor_x(), get_cursor_y());
3226 if( old_highlighted_title != highlighted_title ) {
3229 return drag_popup->cursor_motion_event(); }
3235 int BC_ListBox::drag_stop_event()
3238 unset_scroll_repeat();
3239 switch( current_operation ) {
3241 // Inside window boundary
3242 if( top_level->cursor_x > 0 &&
3243 top_level->cursor_x < gui->get_w() - drag_popup->get_w() / 2 &&
3244 top_level->cursor_y > 0 &&
3245 top_level->cursor_y < gui->get_h() - drag_popup->get_h() / 2 ) {
3249 if( display_format == LISTBOX_ICONS ) {
3250 reposition_item(data,
3252 top_level->cursor_x - drag_popup->get_w() / 2 -
3253 LISTBOX_MARGIN - 2 + xposition,
3254 top_level->cursor_y - drag_popup->get_h() / 2 -
3255 LISTBOX_MARGIN - 2 + yposition);
3259 if( process_drag ) {
3260 // Move selected items from data to temporary
3261 ArrayList<BC_ListBoxItem*> *src_items =
3262 new ArrayList<BC_ListBoxItem*>[columns];
3263 move_selection(src_items, data);
3265 int destination = highlighted_item = item_to_index(data,
3267 // Insert items from temporary to data
3268 put_selection(data, src_items, destination);
3270 delete [] src_items;
3271 set_autoplacement(data, 0, 1);
3277 drag_popup->drag_failure_event();
3282 if( dragged_title != highlighted_title ) {
3283 if( highlighted_title >= 0 ) {
3284 if( !move_column_event() ) draw_titles(1);
3287 drag_popup->drag_failure_event();
3293 current_operation = NO_OPERATION;
3303 BC_DragWindow* BC_ListBox::get_drag_popup()
3308 int BC_ListBox::translation_event()
3310 if( is_popup && gui ) {
3311 int new_x = gui->get_x() +
3312 (top_level->last_translate_x - top_level->prev_x -
3313 BC_DisplayInfo::get_left_border());
3314 int new_y = gui->get_y() +
3315 (top_level->last_translate_y - top_level->prev_y -
3316 BC_DisplayInfo::get_top_border());
3318 gui->reposition_window(new_x, new_y);
3324 int BC_ListBox::reposition_window(int x, int y, int w, int h, int flush)
3328 if( h != -1 ) popup_h = h;
3329 //printf("BC_ListBox::reposition_window %d %d\n", popup_w, popup_h);
3333 xscrollbar->reposition_window(get_xscroll_x(),
3335 get_xscroll_width());
3337 yscrollbar->reposition_window(get_yscroll_x(),
3339 get_yscroll_height());
3343 BC_WindowBase::reposition_window(x, y, w, h);
3349 int BC_ListBox::deactivate()
3352 // printf("BC_ListBox::deactivate %d this=%p gui=%p active=%d\n",
3353 // __LINE__, this, gui, active);
3356 //printf("BC_ListBox::deactivate %d this=%p gui=%p\n", __LINE__, this, gui);
3358 delete gui; gui = 0;
3363 highlighted_item = -1;
3364 highlighted_ptr = 0;
3367 //printf("BC_ListBox::deactivate %d this=%p\n", __LINE__, this);
3369 top_level->active_subwindow = 0;
3375 int BC_ListBox::activate(int take_focus)
3377 if( active ) return 0;
3380 set_active_subwindow(this);
3381 button_releases = 0;
3382 if( !is_popup || gui ) return 0;
3383 int wx = get_x(), wy = get_y() + get_h();
3384 if( justify == LISTBOX_RIGHT ) wx += get_w() - popup_w;
3387 XTranslateCoordinates(top_level->display,
3388 parent_window->win, top_level->rootwin,
3389 wx, wy, &abs_x, &abs_y, &xwin);
3392 return activate(abs_x, abs_y);
3395 int BC_ListBox::activate(int x, int y, int w, int h)
3397 if( !is_popup || gui ) return 0;
3399 if( w != -1 ) popup_w = w;
3400 if( h != -1 ) popup_h = h;
3402 if( y + popup_h > top_level->get_root_h(0) )
3403 y -= get_h() + popup_h;
3404 add_subwindow(gui = new BC_Popup(this,
3405 x, y, popup_w, popup_h, -1, 0, 0));
3407 gui->show_window(1);
3411 int BC_ListBox::is_active()
3416 int BC_ListBox::keypress_event()
3418 if( !active ) return 0;
3420 //printf("BC_ListBox::keypress_event %d\n", __LINE__);
3422 int result = 0, redraw = 0;
3423 int view_items = last_in_view - first_in_view + 1;
3424 if( view_items <= 0 ) view_items = view_h / get_text_height(MEDIUMFONT);
3425 int new_item = -1, new_selection = -1;
3427 switch( top_level->get_keypress() ) {
3430 top_level->deactivate();
3432 // If user is manipulating popup with keyboard, don't pass on event.
3434 //printf("BC_ListBox::keypress_event %d %p\n", __LINE__, get_selection(0, 0));
3435 if( top_level->get_keypress() == RETURN )
3437 //printf("BC_ListBox::keypress_event %d %p\n", __LINE__, get_selection(0, 0));
3445 new_selection = new_item = select_previous(0);
3447 //printf("BC_ListBox::keypress_event 1 %d\n", new_item);
3448 if( new_item >= 0 ) {
3449 center_selection(new_item);
3457 new_selection = new_item = select_next(0);
3459 if( new_item >= 0 ) {
3460 center_selection(new_item);
3468 new_selection = new_item = select_previous(view_items - 1);
3470 if( new_item >= 0 ) {
3471 center_selection(new_item);
3479 new_selection = new_item = select_next(view_items - 1);
3481 if( new_item >= 0 ) {
3482 center_selection(new_item);
3508 if( show_query && !ctrl_down() ) {
3509 int query_len = strlen(query);
3510 if( query_len < (int)sizeof(query)-1 &&
3511 top_level->get_keypress() > 30 &&
3512 top_level->get_keypress() < 127 ) {
3513 query[query_len++] = top_level->get_keypress();
3514 query[query_len] = 0;
3515 new_selection = query_list();
3518 if( top_level->get_keypress() == BACKSPACE ) {
3519 if( query_len > 0 ) query[--query_len] = 0;
3520 new_selection = query_list();
3523 show_tooltip(query);
3535 update_scrollbars(1);
3538 //printf("BC_ListBox::keypress_event %d new_selection=%d\n", __LINE__, new_selection);
3539 if( new_selection >= 0 && !is_suggestions ) {
3540 //printf("BC_ListBox::keypress_event %d\n", __LINE__);
3541 selection_changed();
3542 //printf("BC_ListBox::keypress_event %d\n", __LINE__);
3549 BC_Pixmap* BC_ListBox::get_bg_surface()
3555 void BC_ListBox::draw_background()
3557 if( !bg_draw ) return;
3559 // White background pixmap
3560 set_color(top_level->get_resources()->listbox_inactive);
3561 draw_box(0, 0, bg_surface->get_w(), bg_surface->get_h(), bg_surface);
3563 // Optional heroine pixmap
3565 bg_surface->draw_pixmap(bg_pixmap,
3566 bg_surface->get_w() - top_level->get_resources()->listbox_bg->get_w(),
3570 void BC_ListBox::clear_listbox(int x, int y, int w, int h)
3572 gui->draw_pixmap(bg_surface, x, y, w, h, x, y - title_h);
3575 void BC_ListBox::update_format(int display_format, int redraw)
3577 this->display_format = display_format;
3578 xposition = 0; yposition = 0;
3579 if( redraw && gui ) draw_items(1, 1);
3582 int BC_ListBox::get_format()
3584 return display_format;
3589 int BC_ListBox::draw_items(int flush, int draw_bg)
3592 BC_Resources *resources = get_resources();
3594 //dump(data, columns);
3596 // Calculate items width
3597 calculate_item_coords();
3598 // Create and destroy scrollbars as needed
3601 if( bg_draw ) this->bg_draw = 1;
3607 if( display_format == LISTBOX_ICONS ) {
3608 clear_listbox(2, 2 + title_h, view_w, view_h);
3610 set_font(MEDIUMFONT);
3611 for( int i=0; i<data[master_column].size(); ++i ) {
3612 BC_ListBoxItem *item = data[master_column].get(i);
3613 if( get_item_x(item) >= -get_item_w(item) &&
3614 get_item_x(item) < view_w &&
3615 get_item_y(item) >= -get_item_h(item) + title_h &&
3616 get_item_y(item) < view_h + title_h ) {
3617 item->set_in_view(1);
3618 if( first_in_view < 0 ) first_in_view = i;
3620 int item_color = get_item_highlight(data, 0, i);
3621 int icon_x, icon_y, icon_w, icon_h;
3622 int text_x, text_y, text_w, text_h;
3625 get_icon_mask(item, icon_x, icon_y, icon_w, icon_h);
3626 get_text_mask(item, text_x, text_y, text_w, text_h);
3628 if( item_color != resources->listbox_inactive ) {
3629 gui->set_color(BLACK);
3630 gui->draw_rectangle(icon_x, icon_y, icon_w, icon_h);
3631 gui->set_color(item_color);
3632 gui->draw_box(icon_x + 1, icon_y + 1, icon_w - 2, icon_h - 2);
3633 gui->set_color(BLACK);
3634 gui->draw_rectangle(text_x, text_y, text_w, text_h);
3635 gui->set_color(item_color);
3636 gui->draw_box(text_x + 1, text_y + 1, text_w - 2, text_h - 2);
3638 if( icon_position == ICON_LEFT )
3639 gui->draw_box(text_x - 1, text_y + 1, 2, text_h - 2);
3641 if( icon_position == ICON_TOP )
3642 gui->draw_line(text_x + 1, text_y, text_x + icon_w - 2, text_y);
3643 if( text_x + text_w < icon_x + icon_w ) {
3644 gui->set_color(BLACK);
3645 gui->draw_line(text_x + text_w, icon_y + icon_h,
3646 icon_x + icon_w, icon_y + icon_h);
3650 gui->set_color(get_item_color(data, 0, i));
3652 gui->pixmap->draw_pixmap(item->icon,
3653 icon_x + ICON_MARGIN, icon_y + ICON_MARGIN);
3655 gui->draw_text(text_x + ICON_MARGIN,
3656 text_y + ICON_MARGIN + get_baseline(item), item->text);
3659 item->set_in_view(0);
3664 // Draw one column at a time so text overruns don't go into the next column
3665 // clear column backgrounds
3666 int current_toggle = 0;
3667 for( int j=0; j<columns; ++j ) {
3668 clear_listbox(LISTBOX_BORDER + get_column_offset(j) - xposition,
3669 LISTBOX_BORDER + title_h,
3670 get_column_width(j, 1),
3672 // Draw rows in the column recursively
3673 draw_text_recursive(data, j, 0, ¤t_toggle);
3676 // Delete excess expanders
3677 while( expanders.total > current_toggle ) {
3678 expanders.remove_object();
3682 // draw user images if available
3684 // Draw titles on top of rows for superposition effect
3687 // Clear garbage from bottom right corner
3688 if( xscrollbar && yscrollbar && is_popup ) {
3689 gui->draw_top_background(parent_window,
3690 popup_w - get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w(),
3691 popup_h - get_resources()->hscroll_data[SCROLL_HANDLE_UP]->get_h(),
3692 get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w(),
3693 get_resources()->hscroll_data[SCROLL_HANDLE_UP]->get_h());
3700 if( current_operation == SELECT_RECT )
3710 void BC_ListBox::draw_text_recursive(ArrayList<BC_ListBoxItem*> *data,
3711 int column, int indent, int *current_toggle)
3714 BC_Resources *resources = get_resources();
3716 set_font(MEDIUMFONT);
3719 // Search for a branch and make room for toggle if there is one
3721 for( int i=0; i<data[column].size(); ++i ) {
3722 if( data[column].get(i)->get_sublist() ) {
3723 subindent = BC_WindowBase::get_resources()->listbox_expand[0]->get_w();
3729 row_height = row_ascent = row_descent = 0;
3730 for( int i=0; i<data[column].total; ++i ) {
3731 BC_ListBoxItem *item = data[column].values[i];
3732 int ht = get_text_h(item);
3733 if( ht > row_height ) row_height = ht;
3734 int bl = get_baseline(item);
3735 if( bl > row_ascent ) row_ascent = bl;
3737 if( dt > row_descent ) row_ascent = bl;
3740 for( int i=0; i<data[column].size(); ++i ) {
3742 BC_ListBoxItem *item = data[column].values[i];
3743 BC_ListBoxItem *first_item = data[master_column].values[i];
3745 if( get_item_y(item) >= -get_item_h(item) + title_h &&
3746 get_item_y(item) < view_h + title_h ) {
3747 int row_color = get_item_highlight(data, 0, i);
3748 int x, y, w, h, column_width;
3750 get_text_mask(item, x, y, w, h);
3751 column_width = get_column_width(column, 1);
3752 if( x + column_width > view_w + LISTBOX_BORDER * 2 )
3753 column_width = view_w + LISTBOX_BORDER * 2 - x;
3755 if( row_color != resources->listbox_inactive ) {
3756 gui->set_color(row_color);
3757 gui->draw_box(x, y, column_width, h);
3758 gui->set_color(BLACK);
3759 int xx = x + column_width-1;
3760 gui->draw_line(x, y, xx, y);
3762 if( display_format == LISTBOX_ICON_LIST ) {
3763 int ih = get_icon_h(item);
3764 if( ih > hh ) hh = ih;
3767 gui->draw_line(x, yy, xx, yy);
3770 gui->set_color(get_item_color(data, column, i));
3773 if( column == 0 && display_format == LISTBOX_ICON_LIST ) {
3775 gui->pixmap->draw_pixmap(item->icon, x, y);
3776 x += item->icon->get_w() + ICON_MARGIN;
3781 // Indent only applies to first column
3783 x + LISTBOX_BORDER + LISTBOX_MARGIN +
3784 (column == 0 ? indent + subindent : 0),
3785 y + get_baseline(item), item->text);
3786 item->set_in_view(1);
3788 if( first_in_view < 0 ) first_in_view = i;
3793 if( column == 0 && item->get_sublist() && item->get_columns() ) {
3794 // Create new expander
3795 if( *current_toggle >= expanders.total ) {
3796 BC_ListBoxToggle *toggle =
3797 new BC_ListBoxToggle(this, item,
3798 x + LISTBOX_BORDER + LISTBOX_MARGIN + indent, y);
3800 expanders.append(toggle);
3802 // Reposition existing expander
3804 BC_ListBoxToggle *toggle = expanders.values[*current_toggle];
3805 //printf("BC_ListBox::draw_text_recursive 1 %d\n", *current_toggle);
3806 toggle->update(item,
3807 x + LISTBOX_BORDER + LISTBOX_MARGIN + indent, y, 0);
3809 (*current_toggle)++;
3816 item->set_in_view(0);
3818 // Descend into sublist
3819 if( first_item->get_expand() ) {
3820 draw_text_recursive(first_item->get_sublist(),
3822 indent + LISTBOX_INDENT,
3828 int BC_ListBox::draw_border(int flash)
3830 BC_Resources *resources = top_level->get_resources();
3831 gui->draw_3d_border(0, 0,
3832 view_w + LISTBOX_BORDER * 2, view_h + title_h + LISTBOX_BORDER * 2,
3833 resources->listbox_border1,
3835 resources->listbox_border2_hi : resources->listbox_border2,
3837 resources->listbox_border3_hi : resources->listbox_border3,
3838 resources->listbox_border4);
3847 void BC_ListBox::draw_title(int number)
3849 // Column title background
3850 int image_number = 0;
3851 if( number == highlighted_title ) {
3853 if( current_operation == COLUMN_DN )
3857 gui->draw_3segmenth(get_column_offset(number) - xposition + LISTBOX_BORDER,
3859 get_column_width(number, 1) + get_resources()->listbox_title_overlap,
3860 column_bg[image_number]);
3862 int title_x = -xposition + get_column_offset(number) +
3863 LISTBOX_MARGIN + LISTBOX_BORDER;
3864 title_x += get_resources()->listbox_title_margin;
3866 gui->set_color(get_resources()->listbox_title_color);
3867 gui->draw_text(title_x,
3868 LISTBOX_MARGIN + LISTBOX_BORDER + get_text_ascent(MEDIUMFONT),
3869 column_titles[number]);
3871 // Column sort order
3872 if( number == sort_column ) {
3873 BC_Pixmap *src = sort_order == SORT_ASCENDING ?
3874 column_sort_dn : column_sort_up;
3876 // int column_offset = get_column_offset(number) - xposition + LISTBOX_BORDER;
3877 // int column_width = get_column_width(number, 1);
3878 // int toggle_x = column_offset + column_width - LISTBOX_BORDER;
3879 // if( toggle_x > items_w ) toggle_x = items_w;
3880 // toggle_x -= 5 + src->get_w();
3883 get_text_width(MEDIUMFONT, column_titles[number]) +
3886 gui->draw_pixmap(src,
3888 title_h / 2 - src->get_h() / 2 + LISTBOX_BORDER);
3892 int BC_ListBox::draw_titles(int flash)
3894 if( column_titles &&
3895 (display_format == LISTBOX_TEXT ||
3896 display_format == LISTBOX_ICON_LIST) ) {
3897 //printf("BC_ListBox::draw_titles 1 %d\n", highlighted_title);
3898 for( int i=0; i<columns; ++i ) {
3899 if( i != highlighted_title )
3903 if( highlighted_title >= 0 ) draw_title(highlighted_title);
3913 void BC_ListBox::draw_toggles(int flash)
3915 for( int i=0; i<expanders.total; ++i )
3916 expanders.values[i]->draw(0);
3918 //printf("BC_ListBox::draw_toggles 1 %d\n", flash);
3919 if( flash && expanders.total ) {
3924 int BC_ListBox::draw_rectangle(int flash)
3926 int x1 = MIN(rect_x1, rect_x2);
3927 int x2 = MAX(rect_x1, rect_x2);
3928 int y1 = MIN(rect_y1, rect_y2);
3929 int y2 = MAX(rect_y1, rect_y2);
3931 if( x1 == x2 || y1 == y2 ) return 0;
3934 gui->set_color(WHITE);
3935 gui->draw_rectangle(x1, y1, x2 - x1, y2 - y1);
3945 void BC_ListBox::dump(ArrayList<BC_ListBoxItem*> *data, int columns,
3946 int indent, int master_column)
3949 printf("BC_ListBox::dump 1\n");
3952 for( int i=0; i<data[master_column].total; ++i ) {
3953 for( int k=0; k<indent; ++k )
3955 for( int j=0; j<columns; ++j ) {
3956 BC_ListBoxItem *item = data[j].values[i];
3957 printf("%d,%d,%d=%s ",
3958 item->get_text_x(), item->get_text_y(),
3959 item->autoplace_text, item->get_text());
3963 if( data[master_column].values[i]->get_sublist() ) {
3964 dump(data[master_column].values[i]->get_sublist(),
3965 data[master_column].values[i]->get_columns(),