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 packed_icons = display_format == LISTBOX_ICONS_PACKED ? 1 : 0;
373 rect_x1 = rect_x2 = 0;
374 rect_y1 = rect_y2 = 0;
376 // reset the search engine
377 //printf("BC_ListBox::BC_ListBox 4\n");
380 //printf("BC_ListBox::BC_ListBox 5\n");
383 BC_ListBox::~BC_ListBox()
385 expanders.remove_all_objects();
386 if( bg_surface ) delete bg_surface;
387 if( bg_pixmap ) delete bg_pixmap;
388 if( xscrollbar ) delete xscrollbar;
389 if( yscrollbar ) delete yscrollbar;
390 for( int i=0; i<3; ++i ) delete column_bg[i];
391 for( int i=0; i<4; ++i ) delete button_images[i];
392 for( int i=0; i<5; ++i ) delete toggle_images[i];
393 if( column_sort_up ) delete column_sort_up;
394 if( column_sort_dn ) delete column_sort_dn;
397 if( drag_popup ) delete drag_popup;
400 int BC_ListBox::enable()
407 int BC_ListBox::disable()
414 void BC_ListBox::reset_query()
416 query[0] = 0; // reset query
419 int BC_ListBox::evaluate_query(char *string)
421 for( int i=0; i<data[search_column].size(); ++i ) {
422 if( strcmp(string, data[search_column].get(i)->text) <= 0 &&
423 data[search_column].get(i)->searchable ) {
431 int BC_ListBox::query_list()
433 if( query[0] == 0 ) return 0;
437 int selection_changed = 0;
438 int prev_selection = -1;
439 result = evaluate_query(query);
440 if( result >= 0 ) done = 1;
444 for( int i=0; i<data[0].total; ++i ) {
445 for( int j=0; j<columns; ++j ) {
446 if( data[j].values[i]->selected ) prev_selection = i;
447 data[j].values[i]->selected = 0;
452 if( prev_selection != result )
453 selection_changed = 1;
454 for( int j=0; j<columns; ++j ) {
455 data[j].values[result]->selected = 1;
457 center_selection(result);
460 return selection_changed;
463 void BC_ListBox::init_column_width()
465 if( !column_width && data ) {
467 for( int i=0; i<data[0].total; ++i ) {
468 wd = get_text_w(data[0].values[i]);
469 if( wd > widest ) widest = wd;
471 default_column_width[0] = widest + 2 * LISTBOX_MARGIN;
475 int BC_ListBox::initialize()
479 for( int i=0; i<4; ++i ) {
480 button_images[i] = new BC_Pixmap(parent_window,
481 BC_WindowBase::get_resources()->listbox_button[i],
484 w = button_images[0]->get_w();
485 h = button_images[0]->get_h();
489 current_operation = NO_OPERATION;
494 current_operation = NO_OPERATION;
497 for( int i=0; i<3; ++i ) {
498 column_bg[i] = new BC_Pixmap(parent_window,
499 get_resources()->listbox_column[i],
502 for( int i=0; i<5; ++i ) {
503 toggle_images[i] = new BC_Pixmap(parent_window,
504 get_resources()->listbox_expand[i],
508 column_sort_up = new BC_Pixmap(parent_window,
509 BC_WindowBase::get_resources()->listbox_up,
511 column_sort_dn = new BC_Pixmap(parent_window,
512 BC_WindowBase::get_resources()->listbox_dn,
515 //printf("BC_ListBox::initialize 10\n");
516 drag_icon_vframe = get_resources()->type_to_icon[ICON_UNKNOWN];
517 drag_column_icon_vframe = get_resources()->type_to_icon[ICON_COLUMN];
518 // = new BC_Pixmap(parent_window,
519 // get_resources()->type_to_icon[ICON_UNKNOWN],
521 // drag_column_icon = new BC_Pixmap(parent_window,
522 // get_resources()->type_to_icon[ICON_COLUMN],
524 BC_SubWindow::initialize();
528 if( top_level->get_resources()->listbox_bg )
529 bg_pixmap = new BC_Pixmap(this,
530 get_resources()->listbox_bg,
536 if( !use_button && is_popup ) {
544 void BC_ListBox::deactivate_selection()
546 current_operation = NO_OPERATION;
549 int BC_ListBox::draw_button(int flush)
551 // Draw the button for a popup listbox
552 if( use_button && is_popup ) {
553 int image_number = 0;
555 draw_top_background(parent_window, 0, 0, w, h);
557 if( button_highlighted )
559 if( current_operation == BUTTON_DN )
564 pixmap->draw_pixmap(button_images[image_number],
571 int BC_ListBox::calculate_item_coords()
573 if( !data ) return 0;
579 // Change the display_format to get the right item dimensions for both
581 temp_display_format = display_format;
584 // Scan the first column for lowest y coord of all text
585 // and lowest right x and y coord for all icons which aren't auto placable
586 calculate_last_coords_recursive(data,
587 &icon_x, &next_icon_x, &next_icon_y, &next_text_y, 1);
589 // Reset last column width. It's recalculated based on text width.
590 calculate_item_coords_recursive(data,
591 &icon_x, &next_icon_x, &next_icon_y, &next_text_y, 1);
593 display_format = temp_display_format;
598 void BC_ListBox::calculate_last_coords_recursive(
599 ArrayList<BC_ListBoxItem*> *data,
606 for( int i=0; i<data[0].size(); ++i ) {
607 int current_text_y = 0;
608 int current_icon_x = 0;
609 int current_icon_y = 0;
610 BC_ListBoxItem *item = data[0].get(i);
613 if( !item->autoplace_text ) {
614 // Lowest text coordinate
615 display_format = LISTBOX_TEXT;
616 current_text_y = item->text_y + get_text_h(item);
617 if( current_text_y > *next_text_y )
618 *next_text_y = current_text_y;
620 // Add sublist depth if it is expanded
621 if( item->get_sublist() && item->get_columns() &&
622 item->get_expand() ) {
623 calculate_last_coords_recursive(item->get_sublist(),
624 icon_x, next_icon_x, next_icon_y, next_text_y, 0);
628 // Get next_icon coordinate
630 BC_ListBoxItem *item = data[master_column].get(i);
631 if( !item->autoplace_icon ) {
632 display_format = LISTBOX_ICONS;
633 // Lowest right icon coordinate.
634 current_icon_x = item->icon_x;
635 if( current_icon_x > *icon_x ) *icon_x = current_icon_x;
636 if( current_icon_x + get_item_w(item) > *next_icon_x ) {
637 *next_icon_x = current_icon_x + get_item_w(item);
640 current_icon_y = item->icon_y + get_item_h(item);
641 if( current_icon_y > *next_icon_y )
642 *next_icon_y = current_icon_y;
649 void BC_ListBox::calculate_item_coords_recursive(
650 ArrayList<BC_ListBoxItem*> *data,
651 int *icon_x, int *next_icon_x, int *next_icon_y, int *next_text_y,
654 // get maximum height of an icon
655 row_height = get_text_height(MEDIUMFONT);
656 if( temp_display_format == LISTBOX_ICON_LIST ) {
657 for( int i=0; i<data[0].size(); ++i ) {
658 if( data[0].get(i)->icon ) {
659 if( data[0].get(i)->icon->get_h() > row_height )
660 row_height = data[0].get(i)->icon->get_h();
666 // Set up items which need autoplacement.
667 // Should fill icons down and then across
668 for( int i=0; i<data[0].size(); ++i ) {
669 // Don't increase y unless the row requires autoplacing.
670 int total_autoplaced_columns = 0;
672 // Set up icons in first column
674 BC_ListBoxItem *item = data[master_column].get(i);
675 if( item->autoplace_icon ) {
676 // 1 column only if icons are used
677 display_format = LISTBOX_ICONS;
680 if( *next_icon_y + get_item_h(item) >= get_h() &&
682 *icon_x = *next_icon_x;
686 if( *icon_x + get_item_w(item) > *next_icon_x )
687 *next_icon_x = *icon_x + get_item_w(item);
690 item->set_icon_x(*icon_x);
691 item->set_icon_y(*next_icon_y);
693 *next_icon_y += get_item_h(item);
699 row_ascent = row_descent = 0;
700 // row_height still holds icon max height
701 for( int j=0; j<columns; ++j ) {
702 BC_ListBoxItem *item = data[j].get(i);
703 if( item->autoplace_text ) {
704 display_format = LISTBOX_TEXT;
705 item->set_text_x(next_text_x);
706 item->set_text_y(*next_text_y);
707 int ht = get_text_h(item);
708 if( ht > row_height ) row_height = ht;
709 int bl = get_baseline(item);
710 if( bl > row_ascent ) row_ascent = bl;
712 if( dt > row_descent ) row_descent = dt;
714 // printf("BC_ListBox::calculate_item_coords_recursive %p %d %d %d %d %s \n",
715 // item->get_sublist(), item->get_columns(), item->get_expand(),
716 // next_text_x, *next_text_y, item->get_text());
717 // Increment position of next column
718 if( j < columns - 1 ) {
719 next_text_x += (column_width ?
721 default_column_width[j]);
723 // Set last column width based on text width
725 int new_w = get_item_w(item);
727 int *previous_w = (column_width ?
729 &default_column_width[j]);
730 if( new_w > *previous_w )
732 //printf("BC_ListBox::calculate_item_coords_recursive 1 %d\n", new_w);
734 total_autoplaced_columns++;
738 // Increase the text vertical position
739 if( total_autoplaced_columns ) {
740 display_format = LISTBOX_TEXT;
741 *next_text_y += row_height;
745 BC_ListBoxItem *item = data[master_column].values[i];
746 if( item->get_sublist() && item->get_columns() &&
747 item->get_expand() ) {
748 calculate_item_coords_recursive( item->get_sublist(),
749 icon_x, next_icon_x, next_icon_y, next_text_y, 0);
754 void BC_ListBox::set_is_suggestions(int value)
756 this->is_suggestions = value;
758 void BC_ListBox::set_scroll_repeat()
760 if( scroll_repeat ) return;
762 set_repeat(get_resources()->scroll_repeat);
765 void BC_ListBox::unset_scroll_repeat()
767 if( !scroll_repeat ) return;
769 unset_repeat(get_resources()->scroll_repeat);
772 void BC_ListBox::set_use_button(int value)
774 this->use_button = value;
777 void BC_ListBox::set_justify(int value)
779 this->justify = value;
782 void BC_ListBox::set_allow_drag_column(int value)
784 this->allow_drag_column = value;
787 void BC_ListBox::set_process_drag(int value)
789 this->process_drag = value;
792 void BC_ListBox::set_master_column(int value, int redraw)
794 this->master_column = value;
800 void BC_ListBox::set_search_column(int value)
802 this->search_column = value;
805 int BC_ListBox::get_sort_column()
810 void BC_ListBox::set_sort_column(int value, int redraw)
818 int BC_ListBox::get_sort_order()
823 void BC_ListBox::set_sort_order(int value, int redraw)
832 int BC_ListBox::get_display_mode()
834 return display_format;
837 int BC_ListBox::get_yposition()
842 int BC_ListBox::get_xposition()
847 int BC_ListBox::get_highlighted_item()
849 return highlighted_item;
853 int BC_ListBox::get_item_x(BC_ListBoxItem *item)
855 switch( display_format ) {
857 case LISTBOX_ICON_LIST:
858 return item->text_x - xposition + 2;
860 case LISTBOX_ICONS_PACKED:
861 return item->icon_x - xposition + 2;
866 int BC_ListBox::get_item_y(BC_ListBoxItem *item)
868 switch( display_format ) {
870 case LISTBOX_ICON_LIST:
871 return item->text_y - yposition + title_h + 2;
873 case LISTBOX_ICONS_PACKED:
874 return item->icon_y - yposition + 2;
879 int BC_ListBox::get_item_w(BC_ListBoxItem *item)
881 switch( display_format ) {
883 case LISTBOX_ICON_LIST: {
884 return get_text_w(item) + 2 * LISTBOX_MARGIN; }
886 case LISTBOX_ICONS_PACKED: {
888 get_icon_mask(item, x, y, w, h);
890 get_text_mask(item, x, y, w, h);
893 return icon_position == ICON_LEFT ? icon_w + text_w :
894 icon_w > text_w ? icon_w : text_w;
900 int BC_ListBox::get_item_h(BC_ListBoxItem *item)
902 switch( display_format ) {
904 case LISTBOX_ICON_LIST:
905 return get_text_h(item);
907 case LISTBOX_ICONS_PACKED: {
909 get_icon_mask(item, x, y, w, h);
911 get_text_mask(item, x, y, w, h);
914 return icon_position != ICON_LEFT ? icon_h + text_h :
915 icon_h > text_h ? icon_h : text_h; }
921 int BC_ListBox::get_icon_w(BC_ListBoxItem *item)
923 return item->get_icon_w();
926 int BC_ListBox::get_icon_h(BC_ListBoxItem *item)
928 return item->get_icon_h();
931 int BC_ListBox::get_text_w(BC_ListBoxItem *item)
933 int w = item->get_text_w();
934 if( w < 0 ) item->set_text_w(w = get_text_width(MEDIUMFONT, item->get_text()));
938 int BC_ListBox::get_text_h(BC_ListBoxItem *item)
940 int h = item->get_text_h();
941 if( h < 0 ) item->set_text_h(h = get_text_height(MEDIUMFONT, item->get_text()));
945 int BC_ListBox::get_baseline(BC_ListBoxItem *item)
947 int b = item->get_baseline();
948 if( b < 0 ) item->set_baseline(b = get_text_ascent(MEDIUMFONT));
952 int BC_ListBox::get_items_width()
954 switch( display_format ) {
956 return get_column_offset(columns);
958 case LISTBOX_ICONS_PACKED: {
960 for( int i=0; i<columns; ++i ) {
961 for( int j=0; j<data[i].total; ++j ) {
963 BC_ListBoxItem *item = data[i].values[j];
966 get_icon_mask(item, x, y, w, h);
967 if( x1 + w > widest ) widest = x1 + w;
969 if( icon_position == ICON_LEFT )
972 get_text_mask(item, x, y, w, h);
973 if( x1 + w > widest ) widest = x1 + w;
977 case LISTBOX_ICON_LIST:
978 return get_column_offset(columns);
983 int BC_ListBox::get_items_height(ArrayList<BC_ListBoxItem*> *data, int columns,
994 for( int j=0; j<(data?data[master_column].total:0); ++j ) {
996 BC_ListBoxItem *item = data[master_column].values[j];
998 switch( display_format ) {
1000 get_text_mask(item, x, y, w, h);
1002 // Descend into sublist
1003 if( item->get_sublist() && item->get_expand() ) {
1004 get_items_height(item->get_sublist(),
1005 item->get_columns(),
1010 case LISTBOX_ICONS_PACKED:
1011 case LISTBOX_ICON_LIST:
1012 get_icon_mask(item, x, y, w, h);
1013 if( y + h + yposition > highest ) highest = y + h + yposition;
1015 get_text_mask(item, x, y, w, h);
1016 if( y + h + yposition > highest ) highest = y + h + yposition;
1021 if( display_format == LISTBOX_TEXT && top_level ) {
1022 highest = LISTBOX_MARGIN + *result;
1028 int BC_ListBox::set_yposition(int position, int draw_items)
1030 this->yposition = position;
1032 this->draw_items(1);
1037 int BC_ListBox::set_xposition(int position)
1039 this->xposition = position;
1044 void BC_ListBox::expand_item(BC_ListBoxItem *item, int expand)
1047 item->expand = expand;
1048 // Collapse sublists if this is collapsed to make it easier to calculate
1050 if( item->get_sublist() )
1051 collapse_recursive(item->get_sublist(), master_column);
1053 // Set everything for autoplacement
1054 set_autoplacement(data, 0, 1);
1059 void BC_ListBox::collapse_recursive(ArrayList<BC_ListBoxItem*> *data,
1062 for( int i=0; i<data[master_column].total; ++i ) {
1063 BC_ListBoxItem *item = data[master_column].values[i];
1064 if( item->get_sublist() && item->expand ) {
1066 collapse_recursive(item->get_sublist(), master_column);
1071 void BC_ListBox::set_autoplacement(ArrayList<BC_ListBoxItem*> *data,
1075 for( int i=0; i<data[0].total; ++i ) {
1076 for( int j=0; j<columns; ++j ) {
1077 if( do_icons ) data[j].values[i]->autoplace_icon = 1;
1078 if( do_text ) data[j].values[i]->autoplace_text = 1;
1081 BC_ListBoxItem *item = data[master_column].values[i];
1082 if( item->get_sublist() ) {
1083 set_autoplacement(item->get_sublist(), do_icons, do_text);
1089 void BC_ListBox::set_scroll_stretch(int xv, int yv)
1091 if( xv >= 0 ) xscroll_orientation =
1092 !xv ? SCROLL_HORIZ : SCROLL_HORIZ + SCROLL_STRETCH;
1093 if( yv >= 0 ) yscroll_orientation =
1094 !yv ? SCROLL_VERT : SCROLL_VERT + SCROLL_STRETCH;
1097 int BC_ListBox::get_yscroll_x()
1100 return popup_w - get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w();
1102 return get_x() + popup_w -
1103 get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w();
1106 int BC_ListBox::get_yscroll_y()
1108 return is_popup ? 0 : get_y();
1111 int BC_ListBox::get_yscroll_height()
1113 return popup_h - (need_xscroll ?
1114 get_resources()->hscroll_data[SCROLL_HANDLE_UP]->get_h() :
1118 int BC_ListBox::get_xscroll_x()
1120 return is_popup ? 0 : get_x();
1123 int BC_ListBox::get_xscroll_y()
1125 return (is_popup ? popup_h : get_y() + popup_h) -
1126 get_resources()->hscroll_data[SCROLL_HANDLE_UP]->get_h();
1129 int BC_ListBox::get_xscroll_width()
1131 return popup_w - (need_yscroll ?
1132 get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w() : 0);
1135 int BC_ListBox::get_column_offset(int column)
1138 while( column > 0 ) {
1140 column_width[--column] :
1141 default_column_width[--column];
1146 void BC_ListBox::column_width_boundaries()
1148 if( column_width ) {
1149 for( int i=0; i<columns; ++i ) {
1150 if( column_width[i] < MIN_COLUMN_WIDTH )
1151 column_width[i] = MIN_COLUMN_WIDTH;
1155 for( int i=0; i<columns; ++i ) {
1156 if( default_column_width[i] < MIN_COLUMN_WIDTH )
1157 default_column_width[i] = MIN_COLUMN_WIDTH;
1162 int BC_ListBox::get_column_width(int column, int clamp_right)
1164 if( column < columns - 1 || !clamp_right )
1165 return column_width ? column_width[column] : default_column_width[column];
1166 return popup_w + xposition - get_column_offset(column);
1169 int BC_ListBox::get_icon_mask(BC_ListBoxItem *item,
1170 int &x, int &y, int &w, int &h)
1172 switch( display_format ) {
1174 case LISTBOX_ICONS_PACKED:
1175 case LISTBOX_ICON_LIST: {
1176 x = get_item_x(item);
1177 y = get_item_y(item);
1178 w = get_icon_w(item) + ICON_MARGIN * 2;
1179 h = get_icon_h(item) + ICON_MARGIN * 2;
1189 int BC_ListBox::get_text_mask(BC_ListBoxItem *item,
1190 int &x, int &y, int &w, int &h)
1192 x = get_item_x(item);
1193 y = get_item_y(item);
1195 switch( display_format ) {
1196 case LISTBOX_TEXT: {
1197 w = get_text_w(item) + LISTBOX_MARGIN * 2;
1198 h = get_text_h(item);
1201 case LISTBOX_ICONS_PACKED: {
1202 if( icon_position == ICON_LEFT ) {
1203 x += get_icon_w(item) + ICON_MARGIN * 2;
1204 y += get_icon_h(item) - get_text_h(item);
1207 y += get_icon_h(item) + ICON_MARGIN;
1211 get_icon_w(item) + ICON_MARGIN * 4 :
1212 get_text_w(item) + ICON_MARGIN * 2 ;
1213 h = get_text_h(item) + ICON_MARGIN * 2;
1215 case LISTBOX_ICON_LIST: {
1216 w = get_text_width(MEDIUMFONT, item->text) + LISTBOX_MARGIN * 2;
1225 int BC_ListBox::get_item_highlight(ArrayList<BC_ListBoxItem*> *data,
1226 int column, int item)
1228 BC_Resources *resources = get_resources();
1229 if( data[column].values[item]->selected )
1230 return resources->listbox_selected;
1231 if( highlighted_item >= 0 &&
1232 highlighted_ptr == data[master_column].values[item] )
1233 return resources->listbox_highlighted;
1234 return resources->listbox_inactive;
1237 int BC_ListBox::get_item_color(ArrayList<BC_ListBoxItem*> *data,
1238 int column, int item)
1240 int color = data[column].values[item]->color;
1241 if( color == -1 ) color = get_resources()->listbox_text;
1242 if( get_item_highlight(data, column, item) == color )
1247 int BC_ListBox::get_from_column()
1249 return dragged_title;
1252 int BC_ListBox::get_to_column()
1254 return highlighted_title;
1258 BC_ListBoxItem* BC_ListBox::get_selection(int column, int selection_number)
1260 return get_selection_recursive(data,
1265 BC_ListBoxItem* BC_ListBox::get_selection_recursive(ArrayList<BC_ListBoxItem*> *data,
1266 int column, int selection_number)
1268 if( !data ) return 0;
1270 for( int i=0; i<data[master_column].total; ++i ) {
1271 BC_ListBoxItem *item = data[master_column].values[i];
1272 if( item->selected ) {
1273 //printf("BC_ListBox::get_selection_recursive %d\n", __LINE__);
1275 if( selection_number < 0 ) {
1277 return data[column].values[i];
1281 if( item->get_sublist() ) {
1282 BC_ListBoxItem *result = get_selection_recursive(item->get_sublist(),
1285 if( result ) return result;
1293 int BC_ListBox::get_selection_number(int column, int selection_number)
1295 return get_selection_number_recursive(data, column, selection_number);
1298 int BC_ListBox::get_selection_number_recursive(ArrayList<BC_ListBoxItem*> *data,
1299 int column, int selection_number, int *counter)
1302 if( !data ) return 0;
1303 if( !counter ) counter = &temp;
1305 for( int i=0; i<data[master_column].total; ++i ) {
1307 BC_ListBoxItem *item = data[master_column].values[i];
1308 if( item->selected ) {
1310 if( selection_number < 0 ) {
1314 if( item->get_sublist() ) {
1315 int result = get_selection_number_recursive(item->get_sublist(),
1316 column, selection_number, counter);
1317 if( result >= 0 ) return result;
1324 int BC_ListBox::set_selection_mode(int mode)
1326 this->selection_mode = mode;
1330 void BC_ListBox::delete_columns()
1332 if( column_titles ) {
1333 for( int i=0; i<columns; ++i ) {
1334 delete [] column_titles[i];
1336 delete [] column_titles;
1339 if( column_width ) delete [] column_width;
1345 // Need to copy titles so EDL can change
1346 void BC_ListBox::set_columns(const char **column_titles, int *column_width, int columns)
1348 if( (!column_titles && column_width) ||
1349 (column_titles && !column_width) ) {
1350 printf("BC_ListBox::set_columns either column_titles or column_width == NULL but not both.\n");
1355 if( column_titles ) {
1356 this->column_titles = new char*[columns];
1357 for( int i=0; i<columns; ++i ) {
1358 this->column_titles[i] = new char[strlen(column_titles[i]) + 1];
1359 strcpy(this->column_titles[i], column_titles[i]);
1362 if( column_width ) {
1363 this->column_width = new int[columns];
1364 for( int i=0; i<columns; ++i ) {
1365 this->column_width[i] = column_width[i];
1369 this->columns = columns;
1374 int BC_ListBox::update(ArrayList<BC_ListBoxItem*> *data,
1375 const char **column_titles, int *column_widths, int columns,
1376 int xposition, int yposition, int highlighted_number,
1377 int recalc_positions, int draw)
1379 set_columns(column_titles, column_widths, columns);
1381 this->yposition = yposition;
1382 this->xposition = xposition;
1383 this->highlighted_item = highlighted_number;
1384 this->highlighted_ptr = index_to_item(data, highlighted_number, 0);
1386 if( recalc_positions )
1387 set_autoplacement(data, 1, 1);
1389 init_column_width();
1393 update_scrollbars(1);
1399 void BC_ListBox::center_selection()
1401 int selection = get_selection_number(0, 0);
1402 calculate_item_coords();
1403 center_selection(selection);
1407 update_scrollbars(0);
1408 gui->show_window(1);
1412 void BC_ListBox::move_vertical(int pixels)
1416 void BC_ListBox::move_horizontal(int pixels)
1420 int BC_ListBox::select_previous(int skip, BC_ListBoxItem *selected_item, int *counter,
1421 ArrayList<BC_ListBoxItem*> *data, int *got_first, int *got_second)
1424 if( !selected_item )
1425 selected_item = get_selection(0, 0);
1436 got_second = &temp3;
1440 // Scan backwards to item pointer. Then count visible items to get
1441 // destination. No wraparound.
1443 for( int i=data[master_column].total-1; i>=0; --i ) {
1444 BC_ListBoxItem *current_item = data[master_column].values[i];
1445 if( current_item->get_sublist() &&
1446 current_item->get_expand() ) {
1447 int result = select_previous(skip, selected_item, counter,
1448 current_item->get_sublist(), got_first, got_second);
1455 if( (*counter) >= skip ) {
1456 for( int j=0; j<columns; ++j )
1457 data[j].values[i]->selected = 1;
1459 return item_to_index(this->data, current_item);
1463 if( current_item->selected ) {
1464 for( int j=0; j<columns; ++j )
1465 data[j].values[i]->selected = 0;
1472 // Hit top of top level without finding a selected item.
1474 // Select first item in top level and quit
1475 BC_ListBoxItem *current_item;
1477 current_item = data[master_column].values[0];
1479 for( int j=0; j<columns; ++j )
1480 data[j].values[0]->selected = 1;
1482 return item_to_index(this->data, current_item);
1484 } while( top_level && data[master_column].total );
1489 int BC_ListBox::select_next(int skip, BC_ListBoxItem *selected_item, int *counter,
1490 ArrayList<BC_ListBoxItem*> *data, int *got_first, int *got_second)
1493 if( !selected_item )
1494 selected_item = get_selection(0, 0);
1505 got_second = &temp3;
1509 // Scan forwards to currently selected item pointer.
1510 // Then count visible items to get destination. No wraparound.
1512 for( int i=0; i<data[master_column].total; ++i ) {
1513 BC_ListBoxItem *current_item = data[master_column].values[i];
1515 // Select next item once the number items after the currently selected item
1516 // have been passed.
1519 if( (*counter) >= skip ) {
1520 for( int j=0; j<columns; ++j )
1521 data[j].values[i]->selected = 1;
1523 return item_to_index(this->data, current_item);
1527 // Got currently selected item. Deselect it.
1528 if( current_item->selected ) {
1529 for( int j=0; j<columns; ++j )
1530 data[j].values[i]->selected = 0;
1536 // Descend into expanded level
1537 if( current_item->get_sublist() &&
1538 current_item->get_expand() ) {
1539 int result = select_next(skip, selected_item, counter,
1540 current_item->get_sublist(), got_first, got_second);
1547 // Hit bottom of top level without finding the next item.
1549 BC_ListBoxItem *current_item;
1550 // Select first item in top level and quit
1551 if( !(*got_first) ) {
1553 current_item = data[master_column].values[0];
1555 for( int j=0; j<columns; ++j )
1556 data[j].values[0]->selected = 1;
1560 // Select last item in top level and quit
1562 int current_row = data[master_column].total - 1;
1563 current_item = data[master_column].values[current_row];
1565 for( int j=0; j<columns; ++j )
1566 data[j].values[current_row]->selected = 1;
1570 return item_to_index(this->data, current_item);
1572 } while( top_level && data[master_column].total );
1578 void BC_ListBox::clamp_positions()
1580 items_w = get_items_width();
1581 if( xscroll_orientation & SCROLL_STRETCH )
1582 items_w += view_w / 4;
1583 items_h = get_items_height(data, columns);
1584 if( yscroll_orientation & SCROLL_STRETCH )
1585 items_h += view_h / 4;
1587 if( yposition < 0 ) yposition = 0;
1589 if( yposition > items_h - view_h )
1590 yposition = items_h - view_h;
1592 if( yposition < 0 ) yposition = 0;
1594 if( xposition < 0 ) xposition = 0;
1596 if( xposition >= items_w - view_w )
1597 xposition = items_w - view_w;
1599 if( xposition < 0 ) xposition = 0;
1602 int BC_ListBox::center_selection(int selection,
1603 ArrayList<BC_ListBoxItem*> *data, int *counter)
1606 if( !data ) data = this->data;
1607 if( !counter ) counter = &temp;
1609 for( int i=0; i<data[master_column].total; ++i ) {
1613 BC_ListBoxItem *item = data[master_column].values[i];
1614 if( (*counter) == selection ) {
1615 BC_ListBoxItem *top_item = this->data[master_column].values[0];
1616 switch( display_format ) {
1618 case LISTBOX_ICONS_PACKED: {
1619 // Icon is out of window
1620 if( item->icon_y-yposition > view_h-get_text_h(item) ||
1621 item->icon_y-yposition < 0 ) {
1622 yposition = item->icon_y - view_h / 2;
1625 if( data[master_column].values[selection]->icon_x - xposition > view_w ||
1626 data[master_column].values[selection]->icon_x - xposition < 0 ) {
1627 xposition = item->icon_x - view_w / 2;
1631 case LISTBOX_ICON_LIST:
1632 // Text coordinate is out of window
1633 if( item->text_y-yposition > view_h-get_text_h(item) ||
1634 item->text_y-yposition < 0 ) {
1635 yposition = item->text_y - top_item->text_y - view_h / 2;
1642 if( item->get_sublist() ) {
1643 int result = center_selection(selection,
1644 item->get_sublist(),
1646 if( result ) return result;
1652 void BC_ListBox::update_scrollbars(int flush)
1654 int h_needed = items_h = get_items_height(data, columns);
1655 int w_needed = items_w = get_items_width();
1657 // if( columns > 0 && column_width )
1658 // printf("BC_ListBox::update_scrollbars 1 %d %d\n", column_width[columns - 1], w_needed);
1661 if( xposition != xscrollbar->get_value() )
1662 xscrollbar->update_value(xposition);
1664 if( w_needed != xscrollbar->get_length() ||
1665 view_w != xscrollbar->get_handlelength() )
1666 xscrollbar->update_length(w_needed, xposition, view_w, 0);
1670 if( yposition != yscrollbar->get_value() )
1671 yscrollbar->update_value(yposition);
1673 if( h_needed != yscrollbar->get_length() || view_h != yscrollbar->get_handlelength() )
1674 yscrollbar->update_length(h_needed, yposition, view_h, 0);
1677 if( flush ) this->flush();
1680 int BC_ListBox::get_scrollbars()
1682 int h_needed = items_h = get_items_height(data, columns);
1683 int w_needed = items_w = get_items_width();
1686 title_h = get_title_h();
1687 view_h = popup_h - title_h - 4;
1688 view_w = popup_w - 4;
1690 // Create scrollbars as needed
1691 for( int i=0; i<2; ++i ) {
1692 if( w_needed > view_w ) {
1696 get_resources()->hscroll_data[SCROLL_HANDLE_UP]->get_h() -
1703 if( h_needed > view_h ) {
1706 get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w() -
1714 // Update subwindow size
1715 int new_w = popup_w;
1716 int new_h = popup_h;
1717 if( need_xscroll ) new_h -= get_resources()->hscroll_data[SCROLL_HANDLE_UP]->get_h();
1718 if( need_yscroll ) new_w -= get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w();
1721 if( new_w != BC_WindowBase::get_w() || new_h != BC_WindowBase::get_h() )
1722 gui->resize_window(new_w, new_h);
1724 BC_WindowBase *destination = (is_popup ? gui : parent_window);
1725 if( need_xscroll ) {
1727 xscrollbar = new BC_ListBoxXScroll(this);
1728 destination->add_subwindow(xscrollbar);
1729 xscrollbar->show_window(0);
1730 xscrollbar->bound_to = this;
1733 xscrollbar->update_length(w_needed, xposition, view_w, flush);
1734 xscrollbar->reposition_window(get_xscroll_x(),
1736 get_xscroll_width());
1740 if( xscrollbar ) delete xscrollbar;
1745 if( need_yscroll ) {
1747 yscrollbar = new BC_ListBoxYScroll(this);
1748 destination->add_subwindow(yscrollbar);
1749 yscrollbar->show_window(0);
1750 yscrollbar->bound_to = this;
1753 yscrollbar->update_length(h_needed, yposition, view_h, flush);
1754 yscrollbar->reposition_window(get_yscroll_x(),
1756 get_yscroll_height());
1760 if( yscrollbar ) delete yscrollbar;
1766 view_w + 4 != bg_surface->get_w() ||
1767 view_h + 4 != bg_surface->get_h() ) {
1768 if( bg_surface ) delete bg_surface;
1769 bg_surface = new BC_Pixmap(gui, view_w + 4, view_h + 4);
1778 void BC_ListBox::set_drag_scroll(int value)
1780 allow_drag_scroll = value;
1783 // Test for scrolling by dragging
1785 int BC_ListBox::test_drag_scroll(int cursor_x, int cursor_y)
1788 if( allow_drag_scroll ||
1789 current_operation == SELECT_RECT ) {
1791 int top_boundary = get_title_h();
1793 if( cursor_y < top_boundary ||
1794 cursor_y >= view_h + title_h + LISTBOX_BORDER * 2 ||
1795 cursor_x < LISTBOX_BORDER ||
1796 cursor_x >= view_w + LISTBOX_BORDER ) {
1803 int BC_ListBox::drag_scroll_event()
1805 int top_boundary = get_title_h();
1808 if( get_cursor_y() < top_boundary ) {
1809 yposition -= top_boundary - get_cursor_y();
1813 if( get_cursor_y() >= view_h + title_h + 4 ) {
1814 yposition += get_cursor_y() - (view_h + title_h + 4);
1818 if( get_cursor_x() < 2 ) {
1819 xposition -= 2 - get_cursor_x();
1823 if( get_cursor_x() >= view_w + 2 ) {
1824 xposition += get_cursor_x() - (view_w + 2);
1834 int BC_ListBox::rectangle_scroll_event()
1836 int old_xposition = xposition;
1837 int old_yposition = yposition;
1838 int result = drag_scroll_event();
1841 rect_x1 += old_xposition - xposition;
1842 rect_y1 += old_yposition - yposition;
1843 rect_x2 = get_cursor_x();
1844 rect_y2 = get_cursor_y();
1846 int x1 = MIN(rect_x1, rect_x2);
1847 int x2 = MAX(rect_x1, rect_x2);
1848 int y1 = MIN(rect_y1, rect_y2);
1849 int y2 = MAX(rect_y1, rect_y2);
1851 if( select_rectangle(data, x1, y1, x2, y2) ) {
1852 selection_changed();
1857 update_scrollbars(1);
1862 int BC_ListBox::select_scroll_event()
1864 int result = drag_scroll_event();
1866 highlighted_item = selection_number = get_cursor_item(data,
1867 get_cursor_x(), get_cursor_y(), &highlighted_ptr);
1870 update_scrollbars(1);
1871 selection_changed();
1876 int BC_ListBox::select_rectangle(ArrayList<BC_ListBoxItem*> *data,
1877 int x1, int y1, int x2, int y2)
1880 for( int i=0; i<data[master_column].total; ++i ) {
1881 for( int j=0; j<columns; ++j ) {
1882 BC_ListBoxItem *item = data[j].values[i];
1883 switch( display_format ) {
1885 case LISTBOX_ICONS_PACKED: {
1886 int icon_x, icon_y, icon_w, icon_h;
1887 int text_x, text_y, text_w, text_h;
1888 get_icon_mask(item, icon_x, icon_y, icon_w, icon_h);
1889 get_text_mask(item, text_x, text_y, text_w, text_h);
1891 if( (x2 >= icon_x && x1 < icon_x + icon_w &&
1892 y2 >= icon_y && y1 < icon_y + icon_h) ||
1893 (x2 >= text_x && x1 < text_x + text_w &&
1894 y2 >= text_y && y1 < text_y + text_h) ) {
1895 if( !item->selected ) {
1901 if( item->selected ) {
1908 case LISTBOX_ICON_LIST: {
1911 gui->get_w() - get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w() :
1912 gui->get_w()) && y2 > 0 &&
1913 y1 < gui->get_h() && y2 >= get_item_y(item) &&
1914 y1 < get_item_y(item) + get_item_h(item) ) {
1915 if( !item->selected ) {
1921 if( item->selected ) {
1930 BC_ListBoxItem *item = data[master_column].values[i];
1931 if( item->get_sublist() &&
1932 item->get_expand() )
1933 result |= select_rectangle(item->get_sublist(),
1939 int BC_ListBox::reposition_item(ArrayList<BC_ListBoxItem*> *data,
1940 int selection_number, int x, int y, int *counter)
1943 if( !counter ) counter = &temp;
1945 for( int i=0; i<data[master_column].total; ++i ) {
1946 BC_ListBoxItem *item = data[master_column].values[i];
1948 if( (*counter) == selection_number ) {
1953 // Not recursive because it's only used for icons
1958 void BC_ListBox::move_selection(ArrayList<BC_ListBoxItem*> *dst,
1959 ArrayList<BC_ListBoxItem*> *src)
1961 for( int i=0; i<src[master_column].total; ) {
1962 BC_ListBoxItem *item = src[master_column].values[i];
1965 if( item->selected ) {
1966 for( int j=0; j<columns; ++j ) {
1967 dst[j].append(src[j].values[i]);
1968 src[j].remove_number(i);
1972 // Descend into sublist
1973 if( item->get_sublist() ) {
1975 item->get_sublist());
1981 int BC_ListBox::put_selection(ArrayList<BC_ListBoxItem*> *data,
1982 ArrayList<BC_ListBoxItem*> *src, int destination, int *counter)
1985 if( !counter ) counter = &temp;
1987 if( destination < 0 || destination >= data[master_column].total ) {
1988 for( int j=0; j<columns; ++j ) {
1989 for( int i=0; i<src[j].total; ++i ) {
1990 data[j].append(src[j].values[i]);
1996 for( int i=0; i<data[master_column].total; ++i ) {
1998 if( (*counter) == destination ) {
1999 for( int j=0; j<columns; ++j ) {
2000 for( int k=0; k<src[j].total; ++k ) {
2001 data[j].insert(src[j].values[k], destination + k);
2007 BC_ListBoxItem *item = data[master_column].values[i];
2008 if( item->get_sublist() ) {
2009 if( put_selection(item->get_sublist(),
2021 int BC_ListBox::item_to_index(ArrayList<BC_ListBoxItem*> *data,
2022 BC_ListBoxItem *item, int *counter)
2025 if( !counter ) counter = &temp;
2027 for( int i=0; i<data[master_column].total; ++i ) {
2029 for( int j=0; j<columns; ++j ) {
2030 BC_ListBoxItem *new_item = data[j].values[i];
2031 //printf("BC_ListBox::item_to_index 1 %d %d %p\n", j, i, new_item);
2032 if( new_item == item ) {
2037 BC_ListBoxItem *new_item = data[master_column].values[i];
2038 if( new_item->get_sublist() ) {
2039 if( item_to_index(new_item->get_sublist(),
2049 BC_ListBoxItem* BC_ListBox::index_to_item(ArrayList<BC_ListBoxItem*> *data,
2050 int number, int column, int *counter)
2053 if( !counter ) counter = &temp;
2054 for( int i=0; i<data[master_column].total; ++i ) {
2056 if( (*counter) == number ) {
2057 return data[column].values[i];
2059 BC_ListBoxItem *item = data[master_column].values[i];
2060 if( item->get_sublist() ) {
2061 BC_ListBoxItem *result = index_to_item(item->get_sublist(),
2062 number, column, counter);
2063 if( result ) return result;
2069 int BC_ListBox::get_cursor_item(ArrayList<BC_ListBoxItem*> *data, int cursor_x, int cursor_y,
2070 BC_ListBoxItem **item_return, int *counter, int expanded)
2073 if( !data ) return -1;
2074 if( !counter ) counter = &temp;
2076 // Icons are not treed
2077 switch( display_format ) {
2079 case LISTBOX_ICONS_PACKED:
2080 case LISTBOX_ICON_LIST: {
2081 for( int j=data[master_column].total; --j>=0; ) {
2082 int icon_x, icon_y, icon_w, icon_h;
2083 int text_x, text_y, text_w, text_h;
2084 BC_ListBoxItem *item = data[master_column].values[j];
2085 get_icon_mask(item, icon_x, icon_y, icon_w, icon_h);
2086 get_text_mask(item, text_x, text_y, text_w, text_h);
2088 if( (cursor_x >= icon_x && cursor_x < icon_x + icon_w &&
2089 cursor_y >= icon_y && cursor_y < icon_y + icon_h) ||
2090 (cursor_x >= text_x && cursor_x < text_x + text_w &&
2091 cursor_y >= text_y && cursor_y < text_y + text_h) ) {
2092 if( item_return ) (*item_return) = item;
2096 if( display_format == LISTBOX_ICONS ) return -1;
2101 // Cursor is inside items rectangle
2102 if( cursor_x >= 0 &&
2103 cursor_x < (yscrollbar ?
2104 gui->get_w() - get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w() :
2106 // Only clamp y if we're not in a SELECT operation.
2107 (current_operation == BC_ListBox::SELECT ||
2108 (cursor_y > get_title_h() + LISTBOX_BORDER &&
2109 cursor_y < gui->get_h())) ) {
2110 // Search table for cursor obstruction
2111 for( int i=0; i<data[master_column].total; ++i ) {
2112 BC_ListBoxItem *item = data[master_column].values[i];
2115 // Cursor is inside item on current level
2116 if( expanded && item->selectable &&
2117 cursor_y >= get_item_y(item) &&
2118 cursor_y < get_item_y(item) + get_item_h(item) ) {
2119 //printf("BC_ListBox::get_cursor_item %d %d %p\n", master_column, i, item);
2120 if( item_return ) (*item_return) = item;
2124 // Descend into sublist
2125 if( item->get_sublist() ) {
2126 if( get_cursor_item(item->get_sublist(),
2127 cursor_x, cursor_y, item_return, counter,
2128 item->get_expand()) >= 0 )
2139 int BC_ListBox::repeat_event(int64_t duration)
2141 switch( current_operation ) {
2142 // Repeat out of bounds selection
2144 if( duration != get_resources()->scroll_repeat ) break;
2145 return rectangle_scroll_event();
2148 if( duration != get_resources()->scroll_repeat ) break;
2149 if( !drag_scroll_event() ) break;
2152 update_scrollbars(1);
2156 if( duration != get_resources()->scroll_repeat ) break;
2157 return select_scroll_event();
2161 if( duration != get_resources()->tooltip_delay ) break;
2162 if( !button_highlighted || !is_popup ) break;
2163 if( !tooltip_text || !tooltip_text[0] ) break;
2171 int BC_ListBox::cursor_enter_event()
2175 switch( current_operation ) {
2176 // Cursor moved over button, pressed, and exited.
2177 case BUTTON_DOWN_SELECT:
2178 if( top_level->event_win == win ) {
2179 current_operation = BUTTON_DN;
2181 button_highlighted = 1;
2187 // Cursor entered button
2188 if( is_popup && top_level->event_win == win ) {
2189 button_highlighted = 1;
2194 // TODO: Need to get the highlighted column title or item
2195 if( gui && top_level->event_win == gui->win ) {
2196 list_highlighted = 1;
2206 int BC_ListBox::cursor_leave_event()
2208 if( current_operation == COLUMN_DRAG ) return 0;
2211 if( button_highlighted ) {
2212 button_highlighted = 0;
2217 if( list_highlighted ) {
2218 list_highlighted = 0;
2219 highlighted_item = -1;
2220 highlighted_ptr = 0;
2221 highlighted_title = -1;
2222 int redraw_toggles = 0;
2223 for( int i=0; i<expanders.total; ++i )
2224 expanders.values[i]->cursor_leave_event(&redraw_toggles);
2232 int BC_ListBox::get_first_selection(ArrayList<BC_ListBoxItem*> *data, int *result)
2235 if( !result ) result = &temp;
2237 for( int i=0; i<data[master_column].total; ++i ) {
2238 BC_ListBoxItem *item = data[master_column].values[i];
2240 if( item->selected ) return (*result);
2241 if( item->get_sublist() ) {
2242 if( get_first_selection(item->get_sublist(), result) >= 0 )
2249 int BC_ListBox::get_total_items(ArrayList<BC_ListBoxItem*> *data,
2254 if( !result ) result = &temp;
2256 for( int i=0; i<data[master_column].total; ++i ) {
2258 if( data[master_column].values[i]->get_sublist() )
2259 get_total_items(data[master_column].values[i]->get_sublist(),
2268 int BC_ListBox::get_last_selection(ArrayList<BC_ListBoxItem*> *data,
2278 for( int i=data[master_column].total-1; i>=0; --i ) {
2279 BC_ListBoxItem *item = data[master_column].values[i];
2281 if( item->selected ) {
2283 return get_total_items(data, 0, master_column) - (*result) /* - 1 */;
2288 if( item->get_sublist() ) {
2289 if( get_last_selection(item->get_sublist(), result) >= 0 ) {
2291 return get_total_items(data, 0, master_column) - (*result) /* - 1 */;
2300 void BC_ListBox::select_range(ArrayList<BC_ListBoxItem*> *data,
2301 int start, int end, int *current)
2304 if( !current ) current = &temp;
2306 for( int i=0; i<data[master_column].total; ++i ) {
2308 if( (*current) >= start && (*current) < end ) {
2309 for( int j=0; j<columns; ++j )
2310 data[j].values[i]->selected = 1;
2312 BC_ListBoxItem *item = data[master_column].values[i];
2313 if( item->get_sublist() )
2314 select_range(item->get_sublist(), start, end, current);
2319 // Fill items between current selection and new selection
2320 int BC_ListBox::expand_selection(int button_press, int selection_number)
2322 int old_selection_start = selection_start;
2323 int old_selection_end = selection_end;
2325 // printf("BC_ListBox::expand_selection %d %d\n",
2326 // selection_center,
2327 // selection_number);
2329 // Calculate the range to select based on selection_center and selection_number
2330 if( selection_number < selection_center ) {
2331 selection_start = selection_number;
2334 selection_end = selection_number + 1;
2337 //printf("BC_ListBox::expand_selection %d %d %d %d\n", old_selection_start, old_selection_end, selection_start, selection_end);
2338 // Recurse through all the items and select the desired range
2339 select_range(data, selection_start, selection_end);
2342 return (old_selection_start != selection_start ||
2343 old_selection_end != selection_end);
2346 int BC_ListBox::toggle_item_selection(ArrayList<BC_ListBoxItem*> *data,
2347 int selection_number, int *counter)
2350 if( !counter ) counter = &temp;
2352 for( int i=0; i<data[master_column].total; ++i ) {
2353 BC_ListBoxItem *item = data[master_column].values[i];
2355 if( (*counter) == selection_number ) {
2356 // Get new value for selection
2357 int selected = !item->selected;
2359 for( int j=0; j<columns; ++j )
2360 data[j].values[i]->selected = selected;
2364 // Descend into sublist
2365 if( item->get_sublist() ) {
2366 if( toggle_item_selection(item->get_sublist(),
2367 selection_number, counter) )
2376 void BC_ListBox::set_all_selected(ArrayList<BC_ListBoxItem*> *data, int value)
2378 for( int i=0; i<data[master_column].total; ++i ) {
2379 for( int j=0; j<columns; ++j ) {
2380 BC_ListBoxItem *item = data[j].values[i];
2381 item->selected = value;
2383 BC_ListBoxItem *item = data[master_column].values[i];
2384 if( item->get_sublist() ) {
2385 set_all_selected(item->get_sublist(), value);
2390 void BC_ListBox::set_selected(ArrayList<BC_ListBoxItem*> *data,
2391 int item_number, int value, int *counter)
2394 if( !counter ) counter = &temp;
2395 for( int i=0; i<data[master_column].total&&(*counter)!=item_number; ++i ) {
2397 if( (*counter) == item_number ) {
2398 for( int j=0; j<columns; ++j ) {
2399 BC_ListBoxItem *item = data[j].values[i];
2400 item->selected = value;
2405 BC_ListBoxItem *item = data[master_column].values[i];
2406 if( item->get_sublist() ) {
2407 set_selected(item->get_sublist(), item_number, value, counter);
2412 int BC_ListBox::update_selection(ArrayList<BC_ListBoxItem*> *data,
2413 int selection_number, int *counter)
2417 if( !counter ) counter = &temp;
2419 for( int i=0; i<data[master_column].total; ++i ) {
2420 BC_ListBoxItem *item = data[master_column].values[i];
2422 if( (*counter) == selection_number && !item->selected ) {
2424 for( int j=0; j<columns; ++j )
2425 data[j].values[i]->selected = 1;
2428 if( (*counter) != selection_number && item->selected ) {
2430 for( int j=0; j<columns; ++j )
2431 data[j].values[i]->selected = 0;
2433 if( item->get_sublist() )
2434 result |= update_selection(item->get_sublist(),
2441 void BC_ListBox::promote_selections(ArrayList<BC_ListBoxItem*> *data,
2442 int old_value, int new_value)
2444 for( int i=0; i<data[master_column].total; ++i ) {
2445 for( int j=0; j<columns; ++j ) {
2446 BC_ListBoxItem *item = data[j].values[i];
2447 if( item->selected == old_value ) item->selected = new_value;
2449 BC_ListBoxItem *item = data[master_column].values[i];
2450 if( item->get_sublist() )
2451 promote_selections(item->get_sublist(), old_value, new_value);
2455 int BC_ListBox::focus_out_event()
2461 int BC_ListBox::button_press_event()
2464 BC_ListBoxItem *current_item = 0;
2466 int do_selection_change = 0;
2467 const int debug = 0;
2470 if( debug ) printf("BC_ListBox::button_press_event %d this=%p event_win=%p %p %p %p\n",
2471 __LINE__, this, (void*)top_level->event_win,
2472 (void*)(gui ? gui->win : 0), (void*)win, (void*)parent_window->win);
2474 // Pressed in button
2475 if( is_popup && top_level->event_win == win ) {
2476 if( debug ) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2477 current_operation = BUTTON_DN;
2481 if( !active && !disabled ) {
2482 top_level->deactivate();
2489 // Pressed in scrollbar
2490 if( (xscrollbar && top_level->event_win == xscrollbar->win) ||
2491 (yscrollbar && top_level->event_win == yscrollbar->win) ) {
2492 if( debug ) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2497 if( gui && top_level->event_win == gui->win ) {
2498 if( debug ) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2500 // Activate list items
2501 // If it is a suggestion popup, it is visible without being active
2503 if( debug ) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2504 if( !is_suggestions ) top_level->deactivate();
2505 if( debug ) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2507 if( debug ) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2510 if( current_operation != NO_OPERATION ) {
2511 switch( current_operation ) {
2514 return drag_stop_event();
2518 // Wheel mouse pressed
2519 if( get_buttonpress() == 4 ) {
2520 if( current_operation == NO_OPERATION ) {
2521 current_operation = WHEEL;
2523 set_yposition(yposition - gui->get_h() / 10, 0);
2525 update_scrollbars(0);
2526 highlighted_ptr = 0;
2527 highlighted_item = get_cursor_item(data,
2528 top_level->cursor_x, top_level->cursor_y,
2536 if( get_buttonpress() == 5 ) {
2537 if( current_operation == NO_OPERATION ) {
2538 current_operation = WHEEL;
2540 set_yposition(yposition + gui->get_h() / 10, 0);
2542 update_scrollbars(0);
2543 highlighted_ptr = 0;
2544 highlighted_item = get_cursor_item(data,
2545 top_level->cursor_x, top_level->cursor_y,
2553 // Pressed over column title division
2554 if( test_column_divisions(gui->get_cursor_x(),
2555 gui->get_cursor_y(),
2557 drag_cursor_x = gui->get_cursor_x() + xposition;
2559 drag_column_w = column_width[highlighted_division - 1];
2561 drag_column_w = default_column_width[highlighted_division - 1];
2563 current_operation = DRAG_DIVISION;
2567 // Pressed in column title
2568 if( test_column_titles(gui->get_cursor_x(), gui->get_cursor_y()) ) {
2569 current_operation = COLUMN_DN;
2570 button_highlighted = 0;
2571 list_highlighted = 1;
2576 // Pressed in expander
2577 if( test_expanders() ) {
2578 current_operation = EXPAND_DN;
2579 // Need to redraw items because of alpha
2584 // Pressed over item
2585 if( (selection_number = get_cursor_item(data,
2586 gui->get_cursor_x(), gui->get_cursor_y(),
2587 ¤t_item)) >= 0 ) {
2588 if( debug ) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2590 // Get item button was pressed over
2591 selection_number2 = selection_number1;
2592 selection_number1 = selection_number;
2594 selection_start = -1;
2598 // Multiple item selection is possible
2599 if( selection_mode == LISTBOX_MULTIPLE &&
2600 (ctrl_down() || shift_down()) ) {
2601 // Expand text selection.
2602 // Fill items between selected region and current item.
2604 (display_format == LISTBOX_TEXT ||
2605 display_format == LISTBOX_ICON_LIST) ) {
2606 // Get first item selected
2607 selection_start = get_first_selection(data);
2608 // Get last item selected
2609 selection_end = get_last_selection(data);
2610 // Get center of selected region
2611 if( selection_end > selection_start ) {
2612 selection_center = (selection_end + selection_start) >> 1;
2615 selection_center = selection_number;
2617 // Deselect everything.
2618 set_all_selected(data, 0);
2619 // Select just the items
2620 expand_selection(1, selection_number);
2623 // Toggle a single item on or off
2625 toggle_item_selection(data, selection_number);
2626 new_value = current_item->selected;
2629 // Select single item
2631 if( !current_item->selected ) {
2632 set_all_selected(data, 0);
2641 current_operation = SELECT;
2642 highlighted_item = selection_number;
2643 highlighted_ptr = current_item;
2644 button_highlighted = 0;
2645 list_highlighted = 1;
2648 do_selection_change = 1;
2652 // Pressed over nothing. Start rectangle selection.
2654 if( get_buttonpress() == 1 &&
2655 selection_mode == LISTBOX_MULTIPLE ) {
2656 if( !shift_down() ) {
2657 // Deselect all and redraw if anything was selected
2658 if( get_selection_number(0, 0) >= 0 ) {
2659 set_all_selected(data, 0);
2661 do_selection_change = 1;
2666 // Promote selections to protect from a rectangle selection
2667 promote_selections(data, 1, 2);
2670 // Start rectangle selection
2671 current_operation = SELECT_RECT;
2672 rect_x1 = rect_x2 = get_cursor_x();
2673 rect_y1 = rect_y2 = get_cursor_y();
2681 // Suggestion box is not active but visible, so lie to get it to deactivate
2682 if( is_popup && (active || (is_suggestions && gui)) ) {
2684 if( debug ) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2690 if( do_selection_change ) selection_changed();
2691 if( debug ) printf("BC_ListBox::button_press_event %d %d\n", __LINE__, result);
2696 int BC_ListBox::button_release_event()
2699 int cursor_x, cursor_y;
2702 unset_scroll_repeat();
2704 //printf("BC_ListBox::button_release_event 1 %d\n", current_operation);
2705 switch( current_operation ) {
2707 current_operation = NO_OPERATION;
2712 current_operation = NO_OPERATION;
2716 // Release item selection
2717 case BUTTON_DOWN_SELECT:
2719 //printf("BC_ListBox::button_release_event 10\n");
2720 current_operation = NO_OPERATION;
2722 translate_coordinates(top_level->event_win, gui->win,
2723 gui->get_cursor_x(), gui->get_cursor_y(),
2724 &cursor_x, &cursor_y);
2725 selection_number1 = selection_number =
2726 get_cursor_item(data, cursor_x, cursor_y);
2727 //printf("BC_ListBox::button_release_event %d %d\n", selection_number2, selection_number1);
2732 if( selection_number >= 0 ) {
2737 // Second button release outside button
2738 if( button_releases > 1 ) {
2743 if( top_level->get_double_click() &&
2744 selection_number2 == selection_number1 &&
2745 selection_number2 >= 0 && selection_number1 >= 0 ) {
2755 // Demote selections from rectangle selection
2756 promote_selections(data, 2, 1);
2759 // Hide rectangle overlay
2761 current_operation = NO_OPERATION;
2765 // Release popup button
2768 current_operation = NO_OPERATION;
2772 // Second button release inside button
2773 if( button_releases > 1 ) {
2780 current_operation = NO_OPERATION;
2781 // Update the sort column and the sort order for the user only if the existing
2782 // sort column is valid.
2783 if( sort_column >= 0 ) {
2784 // Invert order only if column is the same
2785 if( highlighted_title == sort_column )
2786 sort_order = sort_order == SORT_ASCENDING ?
2787 SORT_DESCENDING : SORT_ASCENDING;
2788 // Set the new sort column
2789 sort_column = highlighted_title;
2790 if( !sort_order_event() ) {
2795 // Sorting not enabled. Redraw the title state.
2803 int redraw_toggles = 0;
2804 for( int i=0; i<expanders.total&&!result; ++i ) {
2805 if( expanders.values[i]->button_release_event(&redraw_toggles) ) {
2809 // Need to redraw items because of alpha
2810 if( redraw_toggles ) draw_items(1);
2811 current_operation = NO_OPERATION;
2815 // Can't default to NO_OPERATION because it may be used in a drag event.
2820 if( do_event ) handle_event();
2822 //printf("BC_ListBox::button_release_event %d %d\n", __LINE__, get_window_lock());
2826 int BC_ListBox::get_title_h()
2828 if( display_format == LISTBOX_TEXT ||
2829 display_format == LISTBOX_ICON_LIST )
2830 return column_titles ? column_bg[0]->get_h() : 0;
2834 void BC_ListBox::reset_cursor(int new_cursor)
2837 if( gui->get_cursor() != new_cursor ) {
2838 gui->set_cursor(new_cursor, 0, 0);
2842 if( get_cursor() != new_cursor ) {
2843 set_cursor(new_cursor, 0, 0);
2847 int BC_ListBox::test_column_divisions(int cursor_x, int cursor_y, int &new_cursor)
2849 if( gui && column_titles &&
2850 cursor_y >= 0 && cursor_y < get_title_h() &&
2851 cursor_x >= 0 && cursor_x < gui->get_w() ) {
2852 for( int i=1; i<columns; ++i ) {
2853 if( cursor_x >= -xposition + get_column_offset(i) - 5 &&
2854 cursor_x < -xposition + get_column_offset(i) +
2855 get_resources()->listbox_title_hotspot ) {
2856 highlighted_item = -1;
2857 highlighted_ptr = 0;
2858 highlighted_division = i;
2859 highlighted_title = -1;
2860 list_highlighted = 1;
2861 new_cursor = HSEPARATE_CURSOR;
2866 highlighted_division = -1;
2870 int BC_ListBox::test_column_titles(int cursor_x, int cursor_y)
2872 if( gui && column_titles &&
2873 cursor_y >= 0 && cursor_y < get_title_h() &&
2874 cursor_x >= 0 && cursor_x < gui->get_w() ) {
2875 for( int i=0; i<columns; ++i ) {
2876 if( cursor_x >= -xposition + get_column_offset(i) &&
2877 (cursor_x < -xposition + get_column_offset(i + 1) ||
2878 i == columns - 1) ) {
2879 highlighted_item = -1;
2880 highlighted_ptr = 0;
2881 highlighted_division = -1;
2882 highlighted_title = i;
2883 list_highlighted = 1;
2888 highlighted_title = -1;
2892 int BC_ListBox::test_expanders()
2894 for( int i=0; i<expanders.total; ++i ) {
2895 if( expanders.values[i]->button_press_event() ) {
2896 current_operation = EXPAND_DN;
2904 int BC_ListBox::cursor_motion_event()
2906 int redraw = 0, result = 0;
2907 int new_cursor = ARROW_CURSOR;
2909 selection_number = -1;
2912 switch( current_operation ) {
2914 // Button pressed and slid off button
2915 if( !cursor_inside() ) {
2916 current_operation = BUTTON_DOWN_SELECT;
2922 case DRAG_DIVISION: {
2923 // int new_w = get_cursor_x() +
2925 // get_column_offset(highlighted_division - 1);
2926 int difference = get_cursor_x() + xposition - drag_cursor_x;
2927 int new_w = drag_column_w + difference;
2929 new_cursor = HSEPARATE_CURSOR;
2931 if( column_width ) {
2932 column_width[highlighted_division - 1] = new_w;
2935 default_column_width[highlighted_division - 1] = new_w;
2938 column_width_boundaries();
2940 // Force update of coords
2941 set_autoplacement(data, 0, 1);
2942 column_resize_event();
2946 update_scrollbars(1);
2951 if( test_drag_scroll(get_cursor_x(), get_cursor_y()) )
2952 set_scroll_repeat();
2954 int old_x1 = MIN(rect_x1, rect_x2);
2955 int old_x2 = MAX(rect_x1, rect_x2);
2956 int old_y1 = MIN(rect_y1, rect_y2);
2957 int old_y2 = MAX(rect_y1, rect_y2);
2959 int new_rect_x2 = get_cursor_x();
2960 int new_rect_y2 = get_cursor_y();
2962 int x1 = MIN(rect_x1, new_rect_x2);
2963 int x2 = MAX(rect_x1, new_rect_x2);
2964 int y1 = MIN(rect_y1, new_rect_y2);
2965 int y2 = MAX(rect_y1, new_rect_y2);
2967 // Adjust rectangle coverage
2968 if( old_x1 != x1 || old_x2 != x2 ||
2969 old_y1 != y1 || old_y2 != y2 ) {
2971 redraw = select_rectangle(data, x1, y1, x2, y2);
2976 //printf("BC_ListBox::cursor_motion_event %d\n", __LINE__);
2981 rect_x2 = get_cursor_x();
2982 rect_y2 = get_cursor_y();
2986 update_scrollbars(1);
2987 selection_changed();
2988 //printf("BC_ListBox::cursor_motion_event %d\n", __LINE__);
2991 if( old_x1 != x1 || old_x2 != x2 ||
2992 old_y1 != y1 || old_y2 != y2 ) {
2993 //printf("BC_ListBox::cursor_motion_event %d\n", __LINE__);
3001 int old_highlighted_item = highlighted_item;
3003 if( test_drag_scroll(get_cursor_x(), get_cursor_y()) )
3004 set_scroll_repeat();
3006 highlighted_item = selection_number = get_cursor_item(data,
3007 get_cursor_x(), get_cursor_y(), &highlighted_ptr);
3010 // Deselect all items and select just the one we're over
3011 if( selection_number >= 0 && !allow_drag &&
3012 ((!shift_down() && !ctrl_down()) ||
3013 selection_mode == LISTBOX_SINGLE) ) {
3014 redraw = update_selection(data, selection_number);
3017 // Expand multiple selection
3018 if( selection_mode == LISTBOX_MULTIPLE &&
3019 (shift_down() || ctrl_down()) ) {
3020 // Expand selected region in text mode centered around initial range
3021 if( (display_format == LISTBOX_TEXT ||
3022 display_format == LISTBOX_ICON_LIST) &&
3024 // Deselect everything.
3025 set_all_selected(data, 0);
3027 // Select just the items
3028 redraw = expand_selection(0, selection_number);
3030 // Set the one item we're over to the selection value determined in
3031 // button_press_event.
3033 set_selected(data, selection_number, new_value);
3037 if( highlighted_item != old_highlighted_item ) {
3040 update_scrollbars(1);
3041 //printf("BC_ListBox::cursor_motion_event %d %d\n", highlighted_item, old_highlighted_item);
3042 selection_changed();
3046 case BUTTON_DOWN_SELECT:
3047 // Went back into button area
3048 if( cursor_inside() ) {
3049 current_operation = BUTTON_DN;
3054 // Went into item area
3056 int cursor_x = 0, cursor_y = 0;
3057 translate_coordinates(top_level->event_win, gui->win,
3058 top_level->cursor_x, top_level->cursor_y,
3059 &cursor_x, &cursor_y);
3060 int old_highlighted_item = highlighted_item;
3061 highlighted_item = selection_number = get_cursor_item(data,
3066 if( highlighted_item != old_highlighted_item ) {
3067 update_selection(data, selection_number);
3069 selection_changed();
3075 int redraw_toggles = 0;
3076 for( int i=0; i<expanders.total&&!result; ++i ) {
3077 result = expanders.values[i]->cursor_motion_event(
3080 if( redraw_toggles ) {
3081 // Need to redraw items because of the alpha
3086 case NO_OPERATION: {
3087 int cursor_x = get_cursor_x(), cursor_y = get_cursor_y();
3088 if( gui && top_level->event_win == gui->win ) {
3089 int old_highlighted_title = highlighted_title;
3090 int old_list_highlighted = list_highlighted;
3091 int old_highlighted_item = highlighted_item;
3092 int redraw_titles = 0;
3093 int redraw_border = 0;
3094 int redraw_items = 0;
3095 int redraw_toggles = 0;
3098 // Test if cursor moved over a title division
3099 test_column_divisions(cursor_x, cursor_y, new_cursor);
3101 // Test if cursor moved over a title
3102 if( highlighted_division < 0 ) {
3103 test_column_titles(cursor_x, cursor_y);
3106 // Test if cursor moved over expander
3107 if( highlighted_division < 0 && highlighted_title < 0 &&
3108 (display_format == LISTBOX_TEXT ||
3109 display_format == LISTBOX_ICON_LIST) ) {
3110 for( int i=0; i<expanders.total; ++i ) {
3111 expanders.values[i]->cursor_motion_event(
3114 //printf("BC_ListBox::cursor_motion_event %d\n", redraw_toggles);
3117 // Test if cursor moved over an item
3118 if( highlighted_division < 0 && highlighted_title < 0 ) {
3119 highlighted_item = get_cursor_item(data,
3126 // Clear title highlighting if moved over division
3127 if( old_highlighted_title != highlighted_title ) {
3131 // Highlight list border
3132 if( old_list_highlighted != list_highlighted ) {
3136 // Moved out of item area
3137 if( old_highlighted_item != highlighted_item ) {
3141 //printf("BC_ListBox::cursor_motion_event 1 %d\n", highlighted_item);
3143 // Change cursor to title division adjustment
3144 reset_cursor(new_cursor);
3146 if( redraw_items ) {
3154 if( redraw_toggles )
3158 if( redraw_items || redraw_titles ||
3159 redraw_border || redraw_toggles ) {
3166 if( !result && list_highlighted ) {
3167 list_highlighted = 0;
3168 highlighted_item = -1;
3169 highlighted_ptr = 0;
3170 highlighted_title = -1;
3171 highlighted_division = -1;
3181 int BC_ListBox::drag_start_event()
3183 switch( current_operation ) {
3185 if( gui && gui->is_event_win() && allow_drag ) {
3186 BC_ListBoxItem *item_return = 0;
3187 selection_number = get_cursor_item(data,
3188 top_level->cursor_x,
3189 top_level->cursor_y,
3192 if( selection_number >= 0 ) {
3194 get_abs_cursor(cx, cy);
3195 if( item_return->icon_vframe ) {
3196 drag_popup = new BC_DragWindow(this,
3197 item_return->icon_vframe, cx, cy);
3200 if( item_return->icon ) {
3201 drag_popup = new BC_DragWindow(this,
3202 item_return->icon, cx, cy);
3205 drag_popup = new BC_DragWindow(this,
3206 drag_icon_vframe, cx, cy);
3208 current_operation = DRAG_ITEM;
3209 // require shift down for scrolling
3210 if( allow_drag < 0 && shift_down() )
3211 set_scroll_repeat();
3218 if( gui && gui->is_event_win() && allow_drag_column ) {
3220 get_abs_cursor(cx, cy);
3221 cx -= drag_column_icon_vframe->get_w() / 2,
3222 cy -= drag_column_icon_vframe->get_h() / 2;
3223 drag_popup = new BC_DragWindow(this,
3224 drag_column_icon_vframe, cx, cy);
3225 dragged_title = highlighted_title;
3226 current_operation = COLUMN_DRAG;
3236 int BC_ListBox::drag_motion_event()
3238 //printf("BC_ListBox::drag_motion_event 1 %d\n", current_operation);
3239 switch( current_operation ) {
3242 int new_highlighted_item = -1;
3243 BC_ListBoxItem *new_highlighted_ptr = 0;
3244 new_highlighted_item = get_cursor_item(data,
3245 top_level->cursor_x, top_level->cursor_y,
3246 &new_highlighted_ptr);
3248 if( new_highlighted_item != highlighted_item ) {
3252 // Always update highlighted value for drag_stop
3253 highlighted_item = new_highlighted_item;
3254 highlighted_ptr = new_highlighted_ptr;
3255 //printf("BC_ListBox::drag_motion_event 1 %p\n", highlighted_ptr);
3259 update_scrollbars(1);
3262 return drag_popup->cursor_motion_event(); }
3265 int old_highlighted_title = highlighted_title;
3266 test_column_titles(get_cursor_x(), get_cursor_y());
3267 if( old_highlighted_title != highlighted_title ) {
3270 return drag_popup->cursor_motion_event(); }
3276 int BC_ListBox::drag_stop_event()
3279 unset_scroll_repeat();
3280 switch( current_operation ) {
3282 // Inside window boundary
3283 if( top_level->cursor_x > 0 &&
3284 top_level->cursor_x < gui->get_w() - drag_popup->get_w() / 2 &&
3285 top_level->cursor_y > 0 &&
3286 top_level->cursor_y < gui->get_h() - drag_popup->get_h() / 2 ) {
3290 if( display_format == LISTBOX_ICONS ||
3291 display_format == LISTBOX_ICONS_PACKED ) {
3292 reposition_item(data,
3294 top_level->cursor_x - drag_popup->get_w() / 2 -
3295 LISTBOX_MARGIN - 2 + xposition,
3296 top_level->cursor_y - drag_popup->get_h() / 2 -
3297 LISTBOX_MARGIN - 2 + yposition);
3301 if( process_drag ) {
3302 // Move selected items from data to temporary
3303 ArrayList<BC_ListBoxItem*> *src_items =
3304 new ArrayList<BC_ListBoxItem*>[columns];
3305 move_selection(src_items, data);
3307 int destination = highlighted_item = item_to_index(data,
3309 // Insert items from temporary to data
3310 put_selection(data, src_items, destination);
3312 delete [] src_items;
3313 set_autoplacement(data, 0, 1);
3319 drag_popup->drag_failure_event();
3324 if( dragged_title != highlighted_title ) {
3325 if( highlighted_title >= 0 ) {
3326 if( !move_column_event() ) draw_titles(1);
3329 drag_popup->drag_failure_event();
3335 current_operation = NO_OPERATION;
3345 BC_DragWindow* BC_ListBox::get_drag_popup()
3350 int BC_ListBox::translation_event()
3352 if( is_popup && gui ) {
3353 int new_x = gui->get_x() +
3354 (top_level->last_translate_x - top_level->prev_x -
3355 BC_DisplayInfo::get_left_border());
3356 int new_y = gui->get_y() +
3357 (top_level->last_translate_y - top_level->prev_y -
3358 BC_DisplayInfo::get_top_border());
3360 gui->reposition_window(new_x, new_y);
3366 int BC_ListBox::reposition_window(int x, int y, int w, int h, int flush)
3370 if( h != -1 ) popup_h = h;
3371 //printf("BC_ListBox::reposition_window %d %d\n", popup_w, popup_h);
3375 xscrollbar->reposition_window(get_xscroll_x(),
3377 get_xscroll_width());
3379 yscrollbar->reposition_window(get_yscroll_x(),
3381 get_yscroll_height());
3385 BC_WindowBase::reposition_window(x, y, w, h);
3391 int BC_ListBox::deactivate()
3394 // printf("BC_ListBox::deactivate %d this=%p gui=%p active=%d\n",
3395 // __LINE__, this, gui, active);
3398 //printf("BC_ListBox::deactivate %d this=%p gui=%p\n", __LINE__, this, gui);
3400 delete gui; gui = 0;
3405 highlighted_item = -1;
3406 highlighted_ptr = 0;
3409 //printf("BC_ListBox::deactivate %d this=%p\n", __LINE__, this);
3411 top_level->active_subwindow = 0;
3417 int BC_ListBox::activate(int take_focus)
3419 if( active ) return 0;
3422 set_active_subwindow(this);
3423 button_releases = 0;
3424 if( !is_popup || gui ) return 0;
3425 int wx = get_x(), wy = get_y() + get_h();
3426 if( justify == LISTBOX_RIGHT ) wx += get_w() - popup_w;
3429 XTranslateCoordinates(top_level->display,
3430 parent_window->win, top_level->rootwin,
3431 wx, wy, &abs_x, &abs_y, &xwin);
3434 return activate(abs_x, abs_y);
3437 int BC_ListBox::activate(int x, int y, int w, int h)
3439 if( !is_popup || gui ) return 0;
3441 if( w != -1 ) popup_w = w;
3442 if( h != -1 ) popup_h = h;
3444 if( y + popup_h > top_level->get_root_h(0) )
3445 y -= get_h() + popup_h;
3446 add_subwindow(gui = new BC_Popup(this,
3447 x, y, popup_w, popup_h, -1, 0, 0));
3449 gui->show_window(1);
3453 int BC_ListBox::is_active()
3458 int BC_ListBox::keypress_event()
3460 if( !active ) return 0;
3462 //printf("BC_ListBox::keypress_event %d\n", __LINE__);
3464 int result = 0, redraw = 0;
3465 int view_items = last_in_view - first_in_view + 1;
3466 if( view_items <= 0 ) view_items = view_h / get_text_height(MEDIUMFONT);
3467 int new_item = -1, new_selection = -1;
3469 switch( top_level->get_keypress() ) {
3472 top_level->deactivate();
3474 // If user is manipulating popup with keyboard, don't pass on event.
3476 //printf("BC_ListBox::keypress_event %d %p\n", __LINE__, get_selection(0, 0));
3477 if( top_level->get_keypress() == RETURN )
3479 //printf("BC_ListBox::keypress_event %d %p\n", __LINE__, get_selection(0, 0));
3487 new_selection = new_item = select_previous(0);
3489 //printf("BC_ListBox::keypress_event 1 %d\n", new_item);
3490 if( new_item >= 0 ) {
3491 center_selection(new_item);
3499 new_selection = new_item = select_next(0);
3501 if( new_item >= 0 ) {
3502 center_selection(new_item);
3510 new_selection = new_item = select_previous(view_items - 1);
3512 if( new_item >= 0 ) {
3513 center_selection(new_item);
3521 new_selection = new_item = select_next(view_items - 1);
3523 if( new_item >= 0 ) {
3524 center_selection(new_item);
3550 if( show_query && !ctrl_down() ) {
3551 int query_len = strlen(query);
3552 if( query_len < (int)sizeof(query)-1 &&
3553 top_level->get_keypress() > 30 &&
3554 top_level->get_keypress() < 127 ) {
3555 query[query_len++] = top_level->get_keypress();
3556 query[query_len] = 0;
3557 new_selection = query_list();
3560 if( top_level->get_keypress() == BACKSPACE ) {
3561 if( query_len > 0 ) query[--query_len] = 0;
3562 new_selection = query_list();
3565 show_tooltip(query);
3577 update_scrollbars(1);
3580 //printf("BC_ListBox::keypress_event %d new_selection=%d\n", __LINE__, new_selection);
3581 if( new_selection >= 0 && !is_suggestions ) {
3582 //printf("BC_ListBox::keypress_event %d\n", __LINE__);
3583 selection_changed();
3584 //printf("BC_ListBox::keypress_event %d\n", __LINE__);
3591 BC_Pixmap* BC_ListBox::get_bg_surface()
3597 void BC_ListBox::draw_background()
3599 if( !bg_draw ) return;
3601 // White background pixmap
3602 set_color(top_level->get_resources()->listbox_inactive);
3603 draw_box(0, 0, bg_surface->get_w(), bg_surface->get_h(), bg_surface);
3605 // Optional heroine pixmap
3607 bg_surface->draw_pixmap(bg_pixmap,
3608 bg_surface->get_w() - top_level->get_resources()->listbox_bg->get_w(),
3612 void BC_ListBox::clear_listbox(int x, int y, int w, int h)
3614 gui->draw_pixmap(bg_surface, x, y, w, h, x, y - title_h);
3617 void BC_ListBox::update_format(int display_format, int redraw)
3619 this->display_format = display_format;
3620 packed_icons = display_format == LISTBOX_ICONS_PACKED ? 1 : 0;
3621 xposition = 0; yposition = 0;
3622 if( redraw && gui ) draw_items(1, 1);
3625 int BC_ListBox::get_format()
3627 return display_format;
3632 int BC_ListBox::draw_items(int flush, int draw_bg)
3635 BC_Resources *resources = get_resources();
3637 //dump(data, columns);
3639 // Calculate items width
3640 calculate_item_coords();
3641 // Create and destroy scrollbars as needed
3644 if( bg_draw ) this->bg_draw = 1;
3650 if( display_format == LISTBOX_ICONS ||
3651 display_format == LISTBOX_ICONS_PACKED ) {
3652 clear_listbox(2, 2 + title_h, view_w, view_h);
3654 set_font(MEDIUMFONT);
3655 for( int i=0; i<data[master_column].size(); ++i ) {
3656 BC_ListBoxItem *item = data[master_column].get(i);
3657 if( get_item_x(item) >= -get_item_w(item) &&
3658 get_item_x(item) < view_w &&
3659 get_item_y(item) >= -get_item_h(item) + title_h &&
3660 get_item_y(item) < view_h + title_h ) {
3661 item->set_in_view(1);
3662 if( first_in_view < 0 ) first_in_view = i;
3664 int item_color = get_item_highlight(data, 0, i);
3665 int icon_x, icon_y, icon_w, icon_h;
3666 int text_x, text_y, text_w, text_h;
3669 get_icon_mask(item, icon_x, icon_y, icon_w, icon_h);
3670 get_text_mask(item, text_x, text_y, text_w, text_h);
3672 if( item_color != resources->listbox_inactive ) {
3673 gui->set_color(BLACK);
3674 gui->draw_rectangle(icon_x, icon_y, icon_w, icon_h);
3675 gui->set_color(item_color);
3676 gui->draw_box(icon_x + 1, icon_y + 1, icon_w - 2, icon_h - 2);
3677 gui->set_color(BLACK);
3678 gui->draw_rectangle(text_x, text_y, text_w, text_h);
3679 gui->set_color(item_color);
3680 gui->draw_box(text_x + 1, text_y + 1, text_w - 2, text_h - 2);
3682 if( icon_position == ICON_LEFT )
3683 gui->draw_box(text_x - 1, text_y + 1, 2, text_h - 2);
3685 if( icon_position == ICON_TOP )
3686 gui->draw_line(text_x + 1, text_y, text_x + icon_w - 2, text_y);
3687 if( text_x + text_w < icon_x + icon_w ) {
3688 gui->set_color(BLACK);
3689 gui->draw_line(text_x + text_w, icon_y + icon_h,
3690 icon_x + icon_w, icon_y + icon_h);
3694 gui->set_color(get_item_color(data, 0, i));
3696 gui->pixmap->draw_pixmap(item->icon,
3697 icon_x + ICON_MARGIN, icon_y + ICON_MARGIN);
3698 char item_text[BCTEXTLEN];
3699 if( display_format == LISTBOX_ICONS_PACKED )
3700 gui->truncate_text(item_text, item->text, text_w);
3702 strcpy(item_text, item->text);
3703 gui->draw_text(text_x + ICON_MARGIN,
3704 text_y + ICON_MARGIN + get_baseline(item), item_text);
3707 item->set_in_view(0);
3712 // Draw one column at a time so text overruns don't go into the next column
3713 // clear column backgrounds
3714 int current_toggle = 0;
3715 for( int j=0; j<columns; ++j ) {
3716 clear_listbox(LISTBOX_BORDER + get_column_offset(j) - xposition,
3717 LISTBOX_BORDER + title_h,
3718 get_column_width(j, 1),
3720 // Draw rows in the column recursively
3721 draw_text_recursive(data, j, 0, ¤t_toggle);
3724 // Delete excess expanders
3725 while( expanders.total > current_toggle ) {
3726 expanders.remove_object();
3730 // draw user images if available
3732 // Draw titles on top of rows for superposition effect
3735 // Clear garbage from bottom right corner
3736 if( xscrollbar && yscrollbar && is_popup ) {
3737 gui->draw_top_background(parent_window,
3738 popup_w - get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w(),
3739 popup_h - get_resources()->hscroll_data[SCROLL_HANDLE_UP]->get_h(),
3740 get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w(),
3741 get_resources()->hscroll_data[SCROLL_HANDLE_UP]->get_h());
3748 if( current_operation == SELECT_RECT )
3758 void BC_ListBox::draw_text_recursive(ArrayList<BC_ListBoxItem*> *data,
3759 int column, int indent, int *current_toggle)
3762 BC_Resources *resources = get_resources();
3764 set_font(MEDIUMFONT);
3767 // Search for a branch and make room for toggle if there is one
3769 for( int i=0; i<data[column].size(); ++i ) {
3770 if( data[column].get(i)->get_sublist() ) {
3771 subindent = BC_WindowBase::get_resources()->listbox_expand[0]->get_w();
3777 row_height = row_ascent = row_descent = 0;
3778 for( int i=0; i<data[column].total; ++i ) {
3779 BC_ListBoxItem *item = data[column].values[i];
3780 int ht = get_text_h(item);
3781 if( ht > row_height ) row_height = ht;
3782 int bl = get_baseline(item);
3783 if( bl > row_ascent ) row_ascent = bl;
3785 if( dt > row_descent ) row_descent = dt;
3788 for( int i=0; i<data[column].size(); ++i ) {
3790 BC_ListBoxItem *item = data[column].values[i];
3791 BC_ListBoxItem *first_item = data[master_column].values[i];
3793 if( get_item_y(item) >= -get_item_h(item) + title_h &&
3794 get_item_y(item) < view_h + title_h ) {
3795 int row_color = get_item_highlight(data, 0, i);
3796 int x, y, w, h, column_width;
3798 get_text_mask(item, x, y, w, h);
3799 column_width = get_column_width(column, 1);
3800 if( x + column_width > view_w + LISTBOX_BORDER * 2 )
3801 column_width = view_w + LISTBOX_BORDER * 2 - x;
3803 if( row_color != resources->listbox_inactive ) {
3804 gui->set_color(row_color);
3805 gui->draw_box(x, y, column_width, h);
3806 gui->set_color(BLACK);
3807 int xx = x + column_width-1;
3808 gui->draw_line(x, y, xx, y);
3810 if( display_format == LISTBOX_ICON_LIST ) {
3811 int ih = get_icon_h(item);
3812 if( ih > hh ) hh = ih;
3815 gui->draw_line(x, yy, xx, yy);
3818 gui->set_color(get_item_color(data, column, i));
3821 if( column == 0 && display_format == LISTBOX_ICON_LIST ) {
3823 gui->pixmap->draw_pixmap(item->icon, x, y);
3824 x += item->icon->get_w() + ICON_MARGIN;
3829 // Indent only applies to first column
3831 x + LISTBOX_BORDER + LISTBOX_MARGIN +
3832 (column == 0 ? indent + subindent : 0),
3833 y + get_baseline(item), item->text);
3834 item->set_in_view(1);
3836 if( first_in_view < 0 ) first_in_view = i;
3841 if( column == 0 && item->get_sublist() && item->get_columns() ) {
3842 // Create new expander
3843 if( *current_toggle >= expanders.total ) {
3844 BC_ListBoxToggle *toggle =
3845 new BC_ListBoxToggle(this, item,
3846 x + LISTBOX_BORDER + LISTBOX_MARGIN + indent, y);
3848 expanders.append(toggle);
3850 // Reposition existing expander
3852 BC_ListBoxToggle *toggle = expanders.values[*current_toggle];
3853 //printf("BC_ListBox::draw_text_recursive 1 %d\n", *current_toggle);
3854 toggle->update(item,
3855 x + LISTBOX_BORDER + LISTBOX_MARGIN + indent, y, 0);
3857 (*current_toggle)++;
3864 item->set_in_view(0);
3866 // Descend into sublist
3867 if( first_item->get_expand() ) {
3868 draw_text_recursive(first_item->get_sublist(),
3870 indent + LISTBOX_INDENT,
3876 int BC_ListBox::draw_border(int flash)
3878 BC_Resources *resources = top_level->get_resources();
3879 gui->draw_3d_border(0, 0,
3880 view_w + LISTBOX_BORDER * 2, view_h + title_h + LISTBOX_BORDER * 2,
3881 resources->listbox_border1,
3883 resources->listbox_border2_hi : resources->listbox_border2,
3885 resources->listbox_border3_hi : resources->listbox_border3,
3886 resources->listbox_border4);
3895 void BC_ListBox::draw_title(int number)
3897 // Column title background
3898 int image_number = 0;
3899 if( number == highlighted_title ) {
3901 if( current_operation == COLUMN_DN )
3905 gui->draw_3segmenth(get_column_offset(number) - xposition + LISTBOX_BORDER,
3907 get_column_width(number, 1) + get_resources()->listbox_title_overlap,
3908 column_bg[image_number]);
3910 int title_x = -xposition + get_column_offset(number) +
3911 LISTBOX_MARGIN + LISTBOX_BORDER;
3912 title_x += get_resources()->listbox_title_margin;
3914 gui->set_color(get_resources()->listbox_title_color);
3915 gui->draw_text(title_x,
3916 LISTBOX_MARGIN + LISTBOX_BORDER + get_text_ascent(MEDIUMFONT),
3917 column_titles[number]);
3919 // Column sort order
3920 if( number == sort_column ) {
3921 BC_Pixmap *src = sort_order == SORT_ASCENDING ?
3922 column_sort_dn : column_sort_up;
3924 // int column_offset = get_column_offset(number) - xposition + LISTBOX_BORDER;
3925 // int column_width = get_column_width(number, 1);
3926 // int toggle_x = column_offset + column_width - LISTBOX_BORDER;
3927 // if( toggle_x > items_w ) toggle_x = items_w;
3928 // toggle_x -= 5 + src->get_w();
3931 get_text_width(MEDIUMFONT, column_titles[number]) +
3934 gui->draw_pixmap(src,
3936 title_h / 2 - src->get_h() / 2 + LISTBOX_BORDER);
3940 int BC_ListBox::draw_titles(int flash)
3942 if( column_titles &&
3943 (display_format == LISTBOX_TEXT ||
3944 display_format == LISTBOX_ICON_LIST) ) {
3945 //printf("BC_ListBox::draw_titles 1 %d\n", highlighted_title);
3946 for( int i=0; i<columns; ++i ) {
3947 if( i != highlighted_title )
3951 if( highlighted_title >= 0 ) draw_title(highlighted_title);
3961 void BC_ListBox::draw_toggles(int flash)
3963 for( int i=0; i<expanders.total; ++i )
3964 expanders.values[i]->draw(0);
3966 //printf("BC_ListBox::draw_toggles 1 %d\n", flash);
3967 if( flash && expanders.total ) {
3972 int BC_ListBox::draw_rectangle(int flash)
3974 int x1 = MIN(rect_x1, rect_x2);
3975 int x2 = MAX(rect_x1, rect_x2);
3976 int y1 = MIN(rect_y1, rect_y2);
3977 int y2 = MAX(rect_y1, rect_y2);
3979 if( x1 == x2 || y1 == y2 ) return 0;
3982 gui->set_color(WHITE);
3983 gui->draw_rectangle(x1, y1, x2 - x1, y2 - y1);
3993 void BC_ListBox::dump(ArrayList<BC_ListBoxItem*> *data, int columns,
3994 int indent, int master_column)
3997 printf("BC_ListBox::dump 1\n");
4000 for( int i=0; i<data[master_column].total; ++i ) {
4001 for( int k=0; k<indent; ++k )
4003 for( int j=0; j<columns; ++j ) {
4004 BC_ListBoxItem *item = data[j].values[i];
4005 printf("%d,%d,%d=%s ",
4006 item->get_text_x(), item->get_text_y(),
4007 item->autoplace_text, item->get_text());
4011 if( data[master_column].values[i]->get_sublist() ) {
4012 dump(data[master_column].values[i]->get_sublist(),
4013 data[master_column].values[i]->get_columns(),