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;
2100 // Cursor is inside items rectangle
2101 if( cursor_x >= 0 &&
2102 cursor_x < (yscrollbar ?
2103 gui->get_w() - get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w() :
2105 // Only clamp y if we're not in a SELECT operation.
2106 (current_operation == BC_ListBox::SELECT ||
2107 (cursor_y > get_title_h() + LISTBOX_BORDER &&
2108 cursor_y < gui->get_h())) ) {
2109 // Search table for cursor obstruction
2110 for( int i=0; i<data[master_column].total; ++i ) {
2111 BC_ListBoxItem *item = data[master_column].values[i];
2114 // Cursor is inside item on current level
2115 if( expanded && item->selectable &&
2116 cursor_y >= get_item_y(item) &&
2117 cursor_y < get_item_y(item) + get_item_h(item) ) {
2118 //printf("BC_ListBox::get_cursor_item %d %d %p\n", master_column, i, item);
2119 if( item_return ) (*item_return) = item;
2123 // Descend into sublist
2124 if( item->get_sublist() ) {
2125 if( get_cursor_item(item->get_sublist(),
2126 cursor_x, cursor_y, item_return, counter,
2127 item->get_expand()) >= 0 )
2138 int BC_ListBox::repeat_event(int64_t duration)
2140 switch( current_operation ) {
2141 // Repeat out of bounds selection
2143 if( duration != get_resources()->scroll_repeat ) break;
2144 return rectangle_scroll_event();
2147 if( duration != get_resources()->scroll_repeat ) break;
2148 if( !drag_scroll_event() ) break;
2151 update_scrollbars(1);
2155 if( duration != get_resources()->scroll_repeat ) break;
2156 return select_scroll_event();
2160 if( duration != get_resources()->tooltip_delay ) break;
2161 if( !button_highlighted || !is_popup ) break;
2162 if( !tooltip_text || !tooltip_text[0] ) break;
2170 int BC_ListBox::cursor_enter_event()
2174 switch( current_operation ) {
2175 // Cursor moved over button, pressed, and exited.
2176 case BUTTON_DOWN_SELECT:
2177 if( top_level->event_win == win ) {
2178 current_operation = BUTTON_DN;
2180 button_highlighted = 1;
2186 // Cursor entered button
2187 if( is_popup && top_level->event_win == win ) {
2188 button_highlighted = 1;
2193 // TODO: Need to get the highlighted column title or item
2194 if( gui && top_level->event_win == gui->win ) {
2195 list_highlighted = 1;
2205 int BC_ListBox::cursor_leave_event()
2207 if( current_operation == COLUMN_DRAG ) return 0;
2210 if( button_highlighted ) {
2211 button_highlighted = 0;
2216 if( list_highlighted ) {
2217 list_highlighted = 0;
2218 highlighted_item = -1;
2219 highlighted_ptr = 0;
2220 highlighted_title = -1;
2221 int redraw_toggles = 0;
2222 for( int i=0; i<expanders.total; ++i )
2223 expanders.values[i]->cursor_leave_event(&redraw_toggles);
2231 int BC_ListBox::get_first_selection(ArrayList<BC_ListBoxItem*> *data, int *result)
2234 if( !result ) result = &temp;
2236 for( int i=0; i<data[master_column].total; ++i ) {
2237 BC_ListBoxItem *item = data[master_column].values[i];
2239 if( item->selected ) return (*result);
2240 if( item->get_sublist() ) {
2241 if( get_first_selection(item->get_sublist(), result) >= 0 )
2248 int BC_ListBox::get_total_items(ArrayList<BC_ListBoxItem*> *data,
2253 if( !result ) result = &temp;
2255 for( int i=0; i<data[master_column].total; ++i ) {
2257 if( data[master_column].values[i]->get_sublist() )
2258 get_total_items(data[master_column].values[i]->get_sublist(),
2267 int BC_ListBox::get_last_selection(ArrayList<BC_ListBoxItem*> *data,
2277 for( int i=data[master_column].total-1; i>=0; --i ) {
2278 BC_ListBoxItem *item = data[master_column].values[i];
2280 if( item->selected ) {
2282 return get_total_items(data, 0, master_column) - (*result) /* - 1 */;
2287 if( item->get_sublist() ) {
2288 if( get_last_selection(item->get_sublist(), result) >= 0 ) {
2290 return get_total_items(data, 0, master_column) - (*result) /* - 1 */;
2299 void BC_ListBox::select_range(ArrayList<BC_ListBoxItem*> *data,
2300 int start, int end, int *current)
2303 if( !current ) current = &temp;
2305 for( int i=0; i<data[master_column].total; ++i ) {
2307 if( (*current) >= start && (*current) < end ) {
2308 for( int j=0; j<columns; ++j )
2309 data[j].values[i]->selected = 1;
2311 BC_ListBoxItem *item = data[master_column].values[i];
2312 if( item->get_sublist() )
2313 select_range(item->get_sublist(), start, end, current);
2318 // Fill items between current selection and new selection
2319 int BC_ListBox::expand_selection(int button_press, int selection_number)
2321 int old_selection_start = selection_start;
2322 int old_selection_end = selection_end;
2324 // printf("BC_ListBox::expand_selection %d %d\n",
2325 // selection_center,
2326 // selection_number);
2328 // Calculate the range to select based on selection_center and selection_number
2329 if( selection_number < selection_center ) {
2330 selection_start = selection_number;
2333 selection_end = selection_number + 1;
2336 //printf("BC_ListBox::expand_selection %d %d %d %d\n", old_selection_start, old_selection_end, selection_start, selection_end);
2337 // Recurse through all the items and select the desired range
2338 select_range(data, selection_start, selection_end);
2341 return (old_selection_start != selection_start ||
2342 old_selection_end != selection_end);
2345 int BC_ListBox::toggle_item_selection(ArrayList<BC_ListBoxItem*> *data,
2346 int selection_number, int *counter)
2349 if( !counter ) counter = &temp;
2351 for( int i=0; i<data[master_column].total; ++i ) {
2352 BC_ListBoxItem *item = data[master_column].values[i];
2354 if( (*counter) == selection_number ) {
2355 // Get new value for selection
2356 int selected = !item->selected;
2358 for( int j=0; j<columns; ++j )
2359 data[j].values[i]->selected = selected;
2363 // Descend into sublist
2364 if( item->get_sublist() ) {
2365 if( toggle_item_selection(item->get_sublist(),
2366 selection_number, counter) )
2375 void BC_ListBox::set_all_selected(ArrayList<BC_ListBoxItem*> *data, int value)
2377 for( int i=0; i<data[master_column].total; ++i ) {
2378 for( int j=0; j<columns; ++j ) {
2379 BC_ListBoxItem *item = data[j].values[i];
2380 item->selected = value;
2382 BC_ListBoxItem *item = data[master_column].values[i];
2383 if( item->get_sublist() ) {
2384 set_all_selected(item->get_sublist(), value);
2389 void BC_ListBox::set_selected(ArrayList<BC_ListBoxItem*> *data,
2390 int item_number, int value, int *counter)
2393 if( !counter ) counter = &temp;
2394 for( int i=0; i<data[master_column].total&&(*counter)!=item_number; ++i ) {
2396 if( (*counter) == item_number ) {
2397 for( int j=0; j<columns; ++j ) {
2398 BC_ListBoxItem *item = data[j].values[i];
2399 item->selected = value;
2404 BC_ListBoxItem *item = data[master_column].values[i];
2405 if( item->get_sublist() ) {
2406 set_selected(item->get_sublist(), item_number, value, counter);
2411 int BC_ListBox::update_selection(ArrayList<BC_ListBoxItem*> *data,
2412 int selection_number, int *counter)
2416 if( !counter ) counter = &temp;
2418 for( int i=0; i<data[master_column].total; ++i ) {
2419 BC_ListBoxItem *item = data[master_column].values[i];
2421 if( (*counter) == selection_number && !item->selected ) {
2423 for( int j=0; j<columns; ++j )
2424 data[j].values[i]->selected = 1;
2427 if( (*counter) != selection_number && item->selected ) {
2429 for( int j=0; j<columns; ++j )
2430 data[j].values[i]->selected = 0;
2432 if( item->get_sublist() )
2433 result |= update_selection(item->get_sublist(),
2440 void BC_ListBox::promote_selections(ArrayList<BC_ListBoxItem*> *data,
2441 int old_value, int new_value)
2443 for( int i=0; i<data[master_column].total; ++i ) {
2444 for( int j=0; j<columns; ++j ) {
2445 BC_ListBoxItem *item = data[j].values[i];
2446 if( item->selected == old_value ) item->selected = new_value;
2448 BC_ListBoxItem *item = data[master_column].values[i];
2449 if( item->get_sublist() )
2450 promote_selections(item->get_sublist(), old_value, new_value);
2454 int BC_ListBox::focus_out_event()
2460 int BC_ListBox::button_press_event()
2463 BC_ListBoxItem *current_item = 0;
2465 int do_selection_change = 0;
2466 const int debug = 0;
2469 if( debug ) printf("BC_ListBox::button_press_event %d this=%p event_win=%p %p %p %p\n",
2470 __LINE__, this, (void*)top_level->event_win,
2471 (void*)(gui ? gui->win : 0), (void*)win, (void*)parent_window->win);
2473 // Pressed in button
2474 if( is_popup && top_level->event_win == win ) {
2475 if( debug ) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2476 current_operation = BUTTON_DN;
2480 if( !active && !disabled ) {
2481 top_level->deactivate();
2488 // Pressed in scrollbar
2489 if( (xscrollbar && top_level->event_win == xscrollbar->win) ||
2490 (yscrollbar && top_level->event_win == yscrollbar->win) ) {
2491 if( debug ) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2496 if( gui && top_level->event_win == gui->win ) {
2497 if( debug ) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2499 // Activate list items
2500 // If it is a suggestion popup, it is visible without being active
2502 if( debug ) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2503 if( !is_suggestions ) top_level->deactivate();
2504 if( debug ) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2506 if( debug ) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2509 if( current_operation != NO_OPERATION ) {
2510 switch( current_operation ) {
2513 return drag_stop_event();
2517 // Wheel mouse pressed
2518 if( get_buttonpress() == 4 ) {
2519 if( current_operation == NO_OPERATION ) {
2520 current_operation = WHEEL;
2522 set_yposition(yposition - gui->get_h() / 10, 0);
2524 update_scrollbars(0);
2525 highlighted_ptr = 0;
2526 highlighted_item = get_cursor_item(data,
2527 top_level->cursor_x, top_level->cursor_y,
2535 if( get_buttonpress() == 5 ) {
2536 if( current_operation == NO_OPERATION ) {
2537 current_operation = WHEEL;
2539 set_yposition(yposition + gui->get_h() / 10, 0);
2541 update_scrollbars(0);
2542 highlighted_ptr = 0;
2543 highlighted_item = get_cursor_item(data,
2544 top_level->cursor_x, top_level->cursor_y,
2552 // Pressed over column title division
2553 if( test_column_divisions(gui->get_cursor_x(),
2554 gui->get_cursor_y(),
2556 drag_cursor_x = gui->get_cursor_x() + xposition;
2558 drag_column_w = column_width[highlighted_division - 1];
2560 drag_column_w = default_column_width[highlighted_division - 1];
2562 current_operation = DRAG_DIVISION;
2566 // Pressed in column title
2567 if( test_column_titles(gui->get_cursor_x(), gui->get_cursor_y()) ) {
2568 current_operation = COLUMN_DN;
2569 button_highlighted = 0;
2570 list_highlighted = 1;
2575 // Pressed in expander
2576 if( test_expanders() ) {
2577 current_operation = EXPAND_DN;
2578 // Need to redraw items because of alpha
2583 // Pressed over item
2584 if( (selection_number = get_cursor_item(data,
2585 gui->get_cursor_x(), gui->get_cursor_y(),
2586 ¤t_item)) >= 0 ) {
2587 if( debug ) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2589 // Get item button was pressed over
2590 selection_number2 = selection_number1;
2591 selection_number1 = selection_number;
2593 selection_start = -1;
2597 // Multiple item selection is possible
2598 if( selection_mode == LISTBOX_MULTIPLE &&
2599 (ctrl_down() || shift_down()) ) {
2600 // Expand text selection.
2601 // Fill items between selected region and current item.
2603 (display_format == LISTBOX_TEXT ||
2604 display_format == LISTBOX_ICON_LIST) ) {
2605 // Get first item selected
2606 selection_start = get_first_selection(data);
2607 // Get last item selected
2608 selection_end = get_last_selection(data);
2609 // Get center of selected region
2610 if( selection_end > selection_start ) {
2611 selection_center = (selection_end + selection_start) >> 1;
2614 selection_center = selection_number;
2616 // Deselect everything.
2617 set_all_selected(data, 0);
2618 // Select just the items
2619 expand_selection(1, selection_number);
2622 // Toggle a single item on or off
2624 toggle_item_selection(data, selection_number);
2625 new_value = current_item->selected;
2628 // Select single item
2630 if( !current_item->selected ) {
2631 set_all_selected(data, 0);
2640 current_operation = SELECT;
2641 highlighted_item = selection_number;
2642 highlighted_ptr = current_item;
2643 button_highlighted = 0;
2644 list_highlighted = 1;
2647 do_selection_change = 1;
2651 // Pressed over nothing. Start rectangle selection.
2653 if( get_buttonpress() == 1 &&
2654 selection_mode == LISTBOX_MULTIPLE ) {
2655 if( !shift_down() ) {
2656 // Deselect all and redraw if anything was selected
2657 if( get_selection_number(0, 0) >= 0 ) {
2658 set_all_selected(data, 0);
2660 do_selection_change = 1;
2665 // Promote selections to protect from a rectangle selection
2666 promote_selections(data, 1, 2);
2669 // Start rectangle selection
2670 current_operation = SELECT_RECT;
2671 rect_x1 = rect_x2 = get_cursor_x();
2672 rect_y1 = rect_y2 = get_cursor_y();
2680 // Suggestion box is not active but visible, so lie to get it to deactivate
2681 if( is_popup && (active || (is_suggestions && gui)) ) {
2683 if( debug ) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2689 if( do_selection_change ) selection_changed();
2690 if( debug ) printf("BC_ListBox::button_press_event %d %d\n", __LINE__, result);
2695 int BC_ListBox::button_release_event()
2698 int cursor_x, cursor_y;
2701 unset_scroll_repeat();
2703 //printf("BC_ListBox::button_release_event 1 %d\n", current_operation);
2704 switch( current_operation ) {
2706 current_operation = NO_OPERATION;
2711 current_operation = NO_OPERATION;
2715 // Release item selection
2716 case BUTTON_DOWN_SELECT:
2718 //printf("BC_ListBox::button_release_event 10\n");
2719 current_operation = NO_OPERATION;
2721 translate_coordinates(top_level->event_win, gui->win,
2722 gui->get_cursor_x(), gui->get_cursor_y(),
2723 &cursor_x, &cursor_y);
2724 selection_number1 = selection_number =
2725 get_cursor_item(data, cursor_x, cursor_y);
2726 //printf("BC_ListBox::button_release_event %d %d\n", selection_number2, selection_number1);
2731 if( selection_number >= 0 ) {
2736 // Second button release outside button
2737 if( button_releases > 1 ) {
2742 if( top_level->get_double_click() &&
2743 selection_number2 == selection_number1 &&
2744 selection_number2 >= 0 && selection_number1 >= 0 ) {
2754 // Demote selections from rectangle selection
2755 promote_selections(data, 2, 1);
2758 // Hide rectangle overlay
2760 current_operation = NO_OPERATION;
2764 // Release popup button
2767 current_operation = NO_OPERATION;
2771 // Second button release inside button
2772 if( button_releases > 1 ) {
2779 current_operation = NO_OPERATION;
2780 // Update the sort column and the sort order for the user only if the existing
2781 // sort column is valid.
2782 if( sort_column >= 0 ) {
2783 // Invert order only if column is the same
2784 if( highlighted_title == sort_column )
2785 sort_order = sort_order == SORT_ASCENDING ?
2786 SORT_DESCENDING : SORT_ASCENDING;
2787 // Set the new sort column
2788 sort_column = highlighted_title;
2789 if( !sort_order_event() ) {
2794 // Sorting not enabled. Redraw the title state.
2802 int redraw_toggles = 0;
2803 for( int i=0; i<expanders.total&&!result; ++i ) {
2804 if( expanders.values[i]->button_release_event(&redraw_toggles) ) {
2808 // Need to redraw items because of alpha
2809 if( redraw_toggles ) draw_items(1);
2810 current_operation = NO_OPERATION;
2814 // Can't default to NO_OPERATION because it may be used in a drag event.
2819 if( do_event ) handle_event();
2821 //printf("BC_ListBox::button_release_event %d %d\n", __LINE__, get_window_lock());
2825 int BC_ListBox::get_title_h()
2827 if( display_format == LISTBOX_TEXT ||
2828 display_format == LISTBOX_ICON_LIST )
2829 return column_titles ? column_bg[0]->get_h() : 0;
2833 void BC_ListBox::reset_cursor(int new_cursor)
2836 if( gui->get_cursor() != new_cursor ) {
2837 gui->set_cursor(new_cursor, 0, 0);
2841 if( get_cursor() != new_cursor ) {
2842 set_cursor(new_cursor, 0, 0);
2846 int BC_ListBox::test_column_divisions(int cursor_x, int cursor_y, int &new_cursor)
2848 if( gui && column_titles &&
2849 cursor_y >= 0 && cursor_y < get_title_h() &&
2850 cursor_x >= 0 && cursor_x < gui->get_w() ) {
2851 for( int i=1; i<columns; ++i ) {
2852 if( cursor_x >= -xposition + get_column_offset(i) - 5 &&
2853 cursor_x < -xposition + get_column_offset(i) +
2854 get_resources()->listbox_title_hotspot ) {
2855 highlighted_item = -1;
2856 highlighted_ptr = 0;
2857 highlighted_division = i;
2858 highlighted_title = -1;
2859 list_highlighted = 1;
2860 new_cursor = HSEPARATE_CURSOR;
2865 highlighted_division = -1;
2869 int BC_ListBox::test_column_titles(int cursor_x, int cursor_y)
2871 if( gui && column_titles &&
2872 cursor_y >= 0 && cursor_y < get_title_h() &&
2873 cursor_x >= 0 && cursor_x < gui->get_w() ) {
2874 for( int i=0; i<columns; ++i ) {
2875 if( cursor_x >= -xposition + get_column_offset(i) &&
2876 (cursor_x < -xposition + get_column_offset(i + 1) ||
2877 i == columns - 1) ) {
2878 highlighted_item = -1;
2879 highlighted_ptr = 0;
2880 highlighted_division = -1;
2881 highlighted_title = i;
2882 list_highlighted = 1;
2887 highlighted_title = -1;
2891 int BC_ListBox::test_expanders()
2893 for( int i=0; i<expanders.total; ++i ) {
2894 if( expanders.values[i]->button_press_event() ) {
2895 current_operation = EXPAND_DN;
2903 int BC_ListBox::cursor_motion_event()
2905 int redraw = 0, result = 0;
2906 int new_cursor = ARROW_CURSOR;
2908 selection_number = -1;
2911 switch( current_operation ) {
2913 // Button pressed and slid off button
2914 if( !cursor_inside() ) {
2915 current_operation = BUTTON_DOWN_SELECT;
2921 case DRAG_DIVISION: {
2922 // int new_w = get_cursor_x() +
2924 // get_column_offset(highlighted_division - 1);
2925 int difference = get_cursor_x() + xposition - drag_cursor_x;
2926 int new_w = drag_column_w + difference;
2928 new_cursor = HSEPARATE_CURSOR;
2930 if( column_width ) {
2931 column_width[highlighted_division - 1] = new_w;
2934 default_column_width[highlighted_division - 1] = new_w;
2937 column_width_boundaries();
2939 // Force update of coords
2940 set_autoplacement(data, 0, 1);
2941 column_resize_event();
2945 update_scrollbars(1);
2950 if( test_drag_scroll(get_cursor_x(), get_cursor_y()) )
2951 set_scroll_repeat();
2953 int old_x1 = MIN(rect_x1, rect_x2);
2954 int old_x2 = MAX(rect_x1, rect_x2);
2955 int old_y1 = MIN(rect_y1, rect_y2);
2956 int old_y2 = MAX(rect_y1, rect_y2);
2958 int new_rect_x2 = get_cursor_x();
2959 int new_rect_y2 = get_cursor_y();
2961 int x1 = MIN(rect_x1, new_rect_x2);
2962 int x2 = MAX(rect_x1, new_rect_x2);
2963 int y1 = MIN(rect_y1, new_rect_y2);
2964 int y2 = MAX(rect_y1, new_rect_y2);
2966 // Adjust rectangle coverage
2967 if( old_x1 != x1 || old_x2 != x2 ||
2968 old_y1 != y1 || old_y2 != y2 ) {
2970 redraw = select_rectangle(data, x1, y1, x2, y2);
2975 //printf("BC_ListBox::cursor_motion_event %d\n", __LINE__);
2980 rect_x2 = get_cursor_x();
2981 rect_y2 = get_cursor_y();
2985 update_scrollbars(1);
2986 selection_changed();
2987 //printf("BC_ListBox::cursor_motion_event %d\n", __LINE__);
2990 if( old_x1 != x1 || old_x2 != x2 ||
2991 old_y1 != y1 || old_y2 != y2 ) {
2992 //printf("BC_ListBox::cursor_motion_event %d\n", __LINE__);
3000 int old_highlighted_item = highlighted_item;
3002 if( test_drag_scroll(get_cursor_x(), get_cursor_y()) )
3003 set_scroll_repeat();
3005 highlighted_item = selection_number = get_cursor_item(data,
3006 get_cursor_x(), get_cursor_y(), &highlighted_ptr);
3009 // Deselect all items and select just the one we're over
3010 if( selection_number >= 0 && !allow_drag &&
3011 ((!shift_down() && !ctrl_down()) ||
3012 selection_mode == LISTBOX_SINGLE) ) {
3013 redraw = update_selection(data, selection_number);
3016 // Expand multiple selection
3017 if( selection_mode == LISTBOX_MULTIPLE &&
3018 (shift_down() || ctrl_down()) ) {
3019 // Expand selected region in text mode centered around initial range
3020 if( (display_format == LISTBOX_TEXT ||
3021 display_format == LISTBOX_ICON_LIST) &&
3023 // Deselect everything.
3024 set_all_selected(data, 0);
3026 // Select just the items
3027 redraw = expand_selection(0, selection_number);
3029 // Set the one item we're over to the selection value determined in
3030 // button_press_event.
3032 set_selected(data, selection_number, new_value);
3036 if( highlighted_item != old_highlighted_item ) {
3039 update_scrollbars(1);
3040 //printf("BC_ListBox::cursor_motion_event %d %d\n", highlighted_item, old_highlighted_item);
3041 selection_changed();
3045 case BUTTON_DOWN_SELECT:
3046 // Went back into button area
3047 if( cursor_inside() ) {
3048 current_operation = BUTTON_DN;
3053 // Went into item area
3055 int cursor_x = 0, cursor_y = 0;
3056 translate_coordinates(top_level->event_win, gui->win,
3057 top_level->cursor_x, top_level->cursor_y,
3058 &cursor_x, &cursor_y);
3059 int old_highlighted_item = highlighted_item;
3060 highlighted_item = selection_number = get_cursor_item(data,
3065 if( highlighted_item != old_highlighted_item ) {
3066 update_selection(data, selection_number);
3068 selection_changed();
3074 int redraw_toggles = 0;
3075 for( int i=0; i<expanders.total&&!result; ++i ) {
3076 result = expanders.values[i]->cursor_motion_event(
3079 if( redraw_toggles ) {
3080 // Need to redraw items because of the alpha
3085 case NO_OPERATION: {
3086 int cursor_x = get_cursor_x(), cursor_y = get_cursor_y();
3087 if( gui && top_level->event_win == gui->win ) {
3088 int old_highlighted_title = highlighted_title;
3089 int old_list_highlighted = list_highlighted;
3090 int old_highlighted_item = highlighted_item;
3091 int redraw_titles = 0;
3092 int redraw_border = 0;
3093 int redraw_items = 0;
3094 int redraw_toggles = 0;
3097 // Test if cursor moved over a title division
3098 test_column_divisions(cursor_x, cursor_y, new_cursor);
3100 // Test if cursor moved over a title
3101 if( highlighted_division < 0 ) {
3102 test_column_titles(cursor_x, cursor_y);
3105 // Test if cursor moved over expander
3106 if( highlighted_division < 0 && highlighted_title < 0 &&
3107 (display_format == LISTBOX_TEXT ||
3108 display_format == LISTBOX_ICON_LIST) ) {
3109 for( int i=0; i<expanders.total; ++i ) {
3110 expanders.values[i]->cursor_motion_event(
3113 //printf("BC_ListBox::cursor_motion_event %d\n", redraw_toggles);
3116 // Test if cursor moved over an item
3117 if( highlighted_division < 0 && highlighted_title < 0 ) {
3118 highlighted_item = get_cursor_item(data,
3125 // Clear title highlighting if moved over division
3126 if( old_highlighted_title != highlighted_title ) {
3130 // Highlight list border
3131 if( old_list_highlighted != list_highlighted ) {
3135 // Moved out of item area
3136 if( old_highlighted_item != highlighted_item ) {
3140 //printf("BC_ListBox::cursor_motion_event 1 %d\n", highlighted_item);
3142 // Change cursor to title division adjustment
3143 reset_cursor(new_cursor);
3145 if( redraw_items ) {
3153 if( redraw_toggles )
3157 if( redraw_items || redraw_titles ||
3158 redraw_border || redraw_toggles ) {
3165 if( !result && list_highlighted ) {
3166 list_highlighted = 0;
3167 highlighted_item = -1;
3168 highlighted_ptr = 0;
3169 highlighted_title = -1;
3170 highlighted_division = -1;
3180 int BC_ListBox::drag_start_event()
3182 switch( current_operation ) {
3184 if( gui && gui->is_event_win() && allow_drag ) {
3185 BC_ListBoxItem *item_return = 0;
3186 selection_number = get_cursor_item(data,
3187 top_level->cursor_x,
3188 top_level->cursor_y,
3191 if( selection_number >= 0 ) {
3193 get_abs_cursor(cx, cy);
3194 if( item_return->icon_vframe ) {
3195 drag_popup = new BC_DragWindow(this,
3196 item_return->icon_vframe, cx, cy);
3199 if( item_return->icon ) {
3200 drag_popup = new BC_DragWindow(this,
3201 item_return->icon, cx, cy);
3204 drag_popup = new BC_DragWindow(this,
3205 drag_icon_vframe, cx, cy);
3207 current_operation = DRAG_ITEM;
3208 // require shift down for scrolling
3209 if( allow_drag < 0 && shift_down() )
3210 set_scroll_repeat();
3217 if( gui && gui->is_event_win() && allow_drag_column ) {
3219 get_abs_cursor(cx, cy);
3220 cx -= drag_column_icon_vframe->get_w() / 2,
3221 cy -= drag_column_icon_vframe->get_h() / 2;
3222 drag_popup = new BC_DragWindow(this,
3223 drag_column_icon_vframe, cx, cy);
3224 dragged_title = highlighted_title;
3225 current_operation = COLUMN_DRAG;
3235 int BC_ListBox::drag_motion_event()
3237 //printf("BC_ListBox::drag_motion_event 1 %d\n", current_operation);
3238 switch( current_operation ) {
3241 int new_highlighted_item = -1;
3242 BC_ListBoxItem *new_highlighted_ptr = 0;
3243 new_highlighted_item = get_cursor_item(data,
3244 top_level->cursor_x, top_level->cursor_y,
3245 &new_highlighted_ptr);
3247 if( new_highlighted_item != highlighted_item ) {
3251 // Always update highlighted value for drag_stop
3252 highlighted_item = new_highlighted_item;
3253 highlighted_ptr = new_highlighted_ptr;
3254 //printf("BC_ListBox::drag_motion_event 1 %p\n", highlighted_ptr);
3258 update_scrollbars(1);
3261 return drag_popup->cursor_motion_event(); }
3264 int old_highlighted_title = highlighted_title;
3265 test_column_titles(get_cursor_x(), get_cursor_y());
3266 if( old_highlighted_title != highlighted_title ) {
3269 return drag_popup->cursor_motion_event(); }
3275 int BC_ListBox::drag_stop_event()
3278 unset_scroll_repeat();
3279 switch( current_operation ) {
3281 // Inside window boundary
3282 if( top_level->cursor_x > 0 &&
3283 top_level->cursor_x < gui->get_w() - drag_popup->get_w() / 2 &&
3284 top_level->cursor_y > 0 &&
3285 top_level->cursor_y < gui->get_h() - drag_popup->get_h() / 2 ) {
3289 if( display_format == LISTBOX_ICONS ||
3290 display_format == LISTBOX_ICONS_PACKED ) {
3291 reposition_item(data,
3293 top_level->cursor_x - drag_popup->get_w() / 2 -
3294 LISTBOX_MARGIN - 2 + xposition,
3295 top_level->cursor_y - drag_popup->get_h() / 2 -
3296 LISTBOX_MARGIN - 2 + yposition);
3300 if( process_drag ) {
3301 // Move selected items from data to temporary
3302 ArrayList<BC_ListBoxItem*> *src_items =
3303 new ArrayList<BC_ListBoxItem*>[columns];
3304 move_selection(src_items, data);
3306 int destination = highlighted_item = item_to_index(data,
3308 // Insert items from temporary to data
3309 put_selection(data, src_items, destination);
3311 delete [] src_items;
3312 set_autoplacement(data, 0, 1);
3318 drag_popup->drag_failure_event();
3323 if( dragged_title != highlighted_title ) {
3324 if( highlighted_title >= 0 ) {
3325 if( !move_column_event() ) draw_titles(1);
3328 drag_popup->drag_failure_event();
3334 current_operation = NO_OPERATION;
3344 BC_DragWindow* BC_ListBox::get_drag_popup()
3349 int BC_ListBox::translation_event()
3351 if( is_popup && gui ) {
3352 int new_x = gui->get_x() +
3353 (top_level->last_translate_x - top_level->prev_x -
3354 BC_DisplayInfo::get_left_border());
3355 int new_y = gui->get_y() +
3356 (top_level->last_translate_y - top_level->prev_y -
3357 BC_DisplayInfo::get_top_border());
3359 gui->reposition_window(new_x, new_y);
3365 int BC_ListBox::reposition_window(int x, int y, int w, int h, int flush)
3369 if( h != -1 ) popup_h = h;
3370 //printf("BC_ListBox::reposition_window %d %d\n", popup_w, popup_h);
3374 xscrollbar->reposition_window(get_xscroll_x(),
3376 get_xscroll_width());
3378 yscrollbar->reposition_window(get_yscroll_x(),
3380 get_yscroll_height());
3384 BC_WindowBase::reposition_window(x, y, w, h);
3390 int BC_ListBox::deactivate()
3393 // printf("BC_ListBox::deactivate %d this=%p gui=%p active=%d\n",
3394 // __LINE__, this, gui, active);
3397 //printf("BC_ListBox::deactivate %d this=%p gui=%p\n", __LINE__, this, gui);
3399 delete gui; gui = 0;
3404 highlighted_item = -1;
3405 highlighted_ptr = 0;
3408 //printf("BC_ListBox::deactivate %d this=%p\n", __LINE__, this);
3410 top_level->active_subwindow = 0;
3416 int BC_ListBox::activate(int take_focus)
3418 if( active ) return 0;
3421 set_active_subwindow(this);
3422 button_releases = 0;
3423 if( !is_popup || gui ) return 0;
3424 int wx = get_x(), wy = get_y() + get_h();
3425 if( justify == LISTBOX_RIGHT ) wx += get_w() - popup_w;
3428 XTranslateCoordinates(top_level->display,
3429 parent_window->win, top_level->rootwin,
3430 wx, wy, &abs_x, &abs_y, &xwin);
3433 return activate(abs_x, abs_y);
3436 int BC_ListBox::activate(int x, int y, int w, int h)
3438 if( !is_popup || gui ) return 0;
3440 if( w != -1 ) popup_w = w;
3441 if( h != -1 ) popup_h = h;
3443 if( y + popup_h > top_level->get_root_h(0) )
3444 y -= get_h() + popup_h;
3445 add_subwindow(gui = new BC_Popup(this,
3446 x, y, popup_w, popup_h, -1, 0, 0));
3448 gui->show_window(1);
3452 int BC_ListBox::is_active()
3457 int BC_ListBox::keypress_event()
3459 if( !active ) return 0;
3461 //printf("BC_ListBox::keypress_event %d\n", __LINE__);
3463 int result = 0, redraw = 0;
3464 int view_items = last_in_view - first_in_view + 1;
3465 if( view_items <= 0 ) view_items = view_h / get_text_height(MEDIUMFONT);
3466 int new_item = -1, new_selection = -1;
3468 switch( top_level->get_keypress() ) {
3471 top_level->deactivate();
3473 // If user is manipulating popup with keyboard, don't pass on event.
3475 //printf("BC_ListBox::keypress_event %d %p\n", __LINE__, get_selection(0, 0));
3476 if( top_level->get_keypress() == RETURN )
3478 //printf("BC_ListBox::keypress_event %d %p\n", __LINE__, get_selection(0, 0));
3486 new_selection = new_item = select_previous(0);
3488 //printf("BC_ListBox::keypress_event 1 %d\n", new_item);
3489 if( new_item >= 0 ) {
3490 center_selection(new_item);
3498 new_selection = new_item = select_next(0);
3500 if( new_item >= 0 ) {
3501 center_selection(new_item);
3509 new_selection = new_item = select_previous(view_items - 1);
3511 if( new_item >= 0 ) {
3512 center_selection(new_item);
3520 new_selection = new_item = select_next(view_items - 1);
3522 if( new_item >= 0 ) {
3523 center_selection(new_item);
3549 if( show_query && !ctrl_down() ) {
3550 int query_len = strlen(query);
3551 if( query_len < (int)sizeof(query)-1 &&
3552 top_level->get_keypress() > 30 &&
3553 top_level->get_keypress() < 127 ) {
3554 query[query_len++] = top_level->get_keypress();
3555 query[query_len] = 0;
3556 new_selection = query_list();
3559 if( top_level->get_keypress() == BACKSPACE ) {
3560 if( query_len > 0 ) query[--query_len] = 0;
3561 new_selection = query_list();
3564 show_tooltip(query);
3576 update_scrollbars(1);
3579 //printf("BC_ListBox::keypress_event %d new_selection=%d\n", __LINE__, new_selection);
3580 if( new_selection >= 0 && !is_suggestions ) {
3581 //printf("BC_ListBox::keypress_event %d\n", __LINE__);
3582 selection_changed();
3583 //printf("BC_ListBox::keypress_event %d\n", __LINE__);
3590 BC_Pixmap* BC_ListBox::get_bg_surface()
3596 void BC_ListBox::draw_background()
3598 if( !bg_draw ) return;
3600 // White background pixmap
3601 set_color(top_level->get_resources()->listbox_inactive);
3602 draw_box(0, 0, bg_surface->get_w(), bg_surface->get_h(), bg_surface);
3604 // Optional heroine pixmap
3606 bg_surface->draw_pixmap(bg_pixmap,
3607 bg_surface->get_w() - top_level->get_resources()->listbox_bg->get_w(),
3611 void BC_ListBox::clear_listbox(int x, int y, int w, int h)
3613 gui->draw_pixmap(bg_surface, x, y, w, h, x, y - title_h);
3616 void BC_ListBox::update_format(int display_format, int redraw)
3618 this->display_format = display_format;
3619 packed_icons = display_format == LISTBOX_ICONS_PACKED ? 1 : 0;
3620 xposition = 0; yposition = 0;
3621 if( redraw && gui ) draw_items(1, 1);
3624 int BC_ListBox::get_format()
3626 return display_format;
3631 int BC_ListBox::draw_items(int flush, int draw_bg)
3634 BC_Resources *resources = get_resources();
3636 //dump(data, columns);
3638 // Calculate items width
3639 calculate_item_coords();
3640 // Create and destroy scrollbars as needed
3643 if( bg_draw ) this->bg_draw = 1;
3649 if( display_format == LISTBOX_ICONS ||
3650 display_format == LISTBOX_ICONS_PACKED ) {
3651 clear_listbox(2, 2 + title_h, view_w, view_h);
3653 set_font(MEDIUMFONT);
3654 for( int i=0; i<data[master_column].size(); ++i ) {
3655 BC_ListBoxItem *item = data[master_column].get(i);
3656 if( get_item_x(item) >= -get_item_w(item) &&
3657 get_item_x(item) < view_w &&
3658 get_item_y(item) >= -get_item_h(item) + title_h &&
3659 get_item_y(item) < view_h + title_h ) {
3660 item->set_in_view(1);
3661 if( first_in_view < 0 ) first_in_view = i;
3663 int item_color = get_item_highlight(data, 0, i);
3664 int icon_x, icon_y, icon_w, icon_h;
3665 int text_x, text_y, text_w, text_h;
3668 get_icon_mask(item, icon_x, icon_y, icon_w, icon_h);
3669 get_text_mask(item, text_x, text_y, text_w, text_h);
3671 if( item_color != resources->listbox_inactive ) {
3672 gui->set_color(BLACK);
3673 gui->draw_rectangle(icon_x, icon_y, icon_w, icon_h);
3674 gui->set_color(item_color);
3675 gui->draw_box(icon_x + 1, icon_y + 1, icon_w - 2, icon_h - 2);
3676 gui->set_color(BLACK);
3677 gui->draw_rectangle(text_x, text_y, text_w, text_h);
3678 gui->set_color(item_color);
3679 gui->draw_box(text_x + 1, text_y + 1, text_w - 2, text_h - 2);
3681 if( icon_position == ICON_LEFT )
3682 gui->draw_box(text_x - 1, text_y + 1, 2, text_h - 2);
3684 if( icon_position == ICON_TOP )
3685 gui->draw_line(text_x + 1, text_y, text_x + icon_w - 2, text_y);
3686 if( text_x + text_w < icon_x + icon_w ) {
3687 gui->set_color(BLACK);
3688 gui->draw_line(text_x + text_w, icon_y + icon_h,
3689 icon_x + icon_w, icon_y + icon_h);
3693 gui->set_color(get_item_color(data, 0, i));
3695 gui->pixmap->draw_pixmap(item->icon,
3696 icon_x + ICON_MARGIN, icon_y + ICON_MARGIN);
3697 char item_text[BCTEXTLEN];
3698 if( display_format == LISTBOX_ICONS_PACKED )
3699 gui->truncate_text(item_text, item->text, text_w);
3701 strcpy(item_text, item->text);
3702 gui->draw_text(text_x + ICON_MARGIN,
3703 text_y + ICON_MARGIN + get_baseline(item), item_text);
3706 item->set_in_view(0);
3711 // Draw one column at a time so text overruns don't go into the next column
3712 // clear column backgrounds
3713 int current_toggle = 0;
3714 for( int j=0; j<columns; ++j ) {
3715 clear_listbox(LISTBOX_BORDER + get_column_offset(j) - xposition,
3716 LISTBOX_BORDER + title_h,
3717 get_column_width(j, 1),
3719 // Draw rows in the column recursively
3720 draw_text_recursive(data, j, 0, ¤t_toggle);
3723 // Delete excess expanders
3724 while( expanders.total > current_toggle ) {
3725 expanders.remove_object();
3729 // draw user images if available
3731 // Draw titles on top of rows for superposition effect
3734 // Clear garbage from bottom right corner
3735 if( xscrollbar && yscrollbar && is_popup ) {
3736 gui->draw_top_background(parent_window,
3737 popup_w - get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w(),
3738 popup_h - get_resources()->hscroll_data[SCROLL_HANDLE_UP]->get_h(),
3739 get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w(),
3740 get_resources()->hscroll_data[SCROLL_HANDLE_UP]->get_h());
3747 if( current_operation == SELECT_RECT )
3757 void BC_ListBox::draw_text_recursive(ArrayList<BC_ListBoxItem*> *data,
3758 int column, int indent, int *current_toggle)
3761 BC_Resources *resources = get_resources();
3763 set_font(MEDIUMFONT);
3766 // Search for a branch and make room for toggle if there is one
3768 for( int i=0; i<data[column].size(); ++i ) {
3769 if( data[column].get(i)->get_sublist() ) {
3770 subindent = BC_WindowBase::get_resources()->listbox_expand[0]->get_w();
3776 row_height = row_ascent = row_descent = 0;
3777 for( int i=0; i<data[column].total; ++i ) {
3778 BC_ListBoxItem *item = data[column].values[i];
3779 int ht = get_text_h(item);
3780 if( ht > row_height ) row_height = ht;
3781 int bl = get_baseline(item);
3782 if( bl > row_ascent ) row_ascent = bl;
3784 if( dt > row_descent ) row_descent = dt;
3787 for( int i=0; i<data[column].size(); ++i ) {
3789 BC_ListBoxItem *item = data[column].values[i];
3790 BC_ListBoxItem *first_item = data[master_column].values[i];
3792 if( get_item_y(item) >= -get_item_h(item) + title_h &&
3793 get_item_y(item) < view_h + title_h ) {
3794 int row_color = get_item_highlight(data, 0, i);
3795 int x, y, w, h, column_width;
3797 get_text_mask(item, x, y, w, h);
3798 column_width = get_column_width(column, 1);
3799 if( x + column_width > view_w + LISTBOX_BORDER * 2 )
3800 column_width = view_w + LISTBOX_BORDER * 2 - x;
3802 if( row_color != resources->listbox_inactive ) {
3803 gui->set_color(row_color);
3804 gui->draw_box(x, y, column_width, h);
3805 gui->set_color(BLACK);
3806 int xx = x + column_width-1;
3807 gui->draw_line(x, y, xx, y);
3809 if( display_format == LISTBOX_ICON_LIST ) {
3810 int ih = get_icon_h(item);
3811 if( ih > hh ) hh = ih;
3814 gui->draw_line(x, yy, xx, yy);
3817 gui->set_color(get_item_color(data, column, i));
3820 if( column == 0 && display_format == LISTBOX_ICON_LIST ) {
3822 gui->pixmap->draw_pixmap(item->icon, x, y);
3823 x += item->icon->get_w() + ICON_MARGIN;
3828 // Indent only applies to first column
3830 x + LISTBOX_BORDER + LISTBOX_MARGIN +
3831 (column == 0 ? indent + subindent : 0),
3832 y + get_baseline(item), item->text);
3833 item->set_in_view(1);
3835 if( first_in_view < 0 ) first_in_view = i;
3840 if( column == 0 && item->get_sublist() && item->get_columns() ) {
3841 // Create new expander
3842 if( *current_toggle >= expanders.total ) {
3843 BC_ListBoxToggle *toggle =
3844 new BC_ListBoxToggle(this, item,
3845 x + LISTBOX_BORDER + LISTBOX_MARGIN + indent, y);
3847 expanders.append(toggle);
3849 // Reposition existing expander
3851 BC_ListBoxToggle *toggle = expanders.values[*current_toggle];
3852 //printf("BC_ListBox::draw_text_recursive 1 %d\n", *current_toggle);
3853 toggle->update(item,
3854 x + LISTBOX_BORDER + LISTBOX_MARGIN + indent, y, 0);
3856 (*current_toggle)++;
3863 item->set_in_view(0);
3865 // Descend into sublist
3866 if( first_item->get_expand() ) {
3867 draw_text_recursive(first_item->get_sublist(),
3869 indent + LISTBOX_INDENT,
3875 int BC_ListBox::draw_border(int flash)
3877 BC_Resources *resources = top_level->get_resources();
3878 gui->draw_3d_border(0, 0,
3879 view_w + LISTBOX_BORDER * 2, view_h + title_h + LISTBOX_BORDER * 2,
3880 resources->listbox_border1,
3882 resources->listbox_border2_hi : resources->listbox_border2,
3884 resources->listbox_border3_hi : resources->listbox_border3,
3885 resources->listbox_border4);
3894 void BC_ListBox::draw_title(int number)
3896 // Column title background
3897 int image_number = 0;
3898 if( number == highlighted_title ) {
3900 if( current_operation == COLUMN_DN )
3904 gui->draw_3segmenth(get_column_offset(number) - xposition + LISTBOX_BORDER,
3906 get_column_width(number, 1) + get_resources()->listbox_title_overlap,
3907 column_bg[image_number]);
3909 int title_x = -xposition + get_column_offset(number) +
3910 LISTBOX_MARGIN + LISTBOX_BORDER;
3911 title_x += get_resources()->listbox_title_margin;
3913 gui->set_color(get_resources()->listbox_title_color);
3914 gui->draw_text(title_x,
3915 LISTBOX_MARGIN + LISTBOX_BORDER + get_text_ascent(MEDIUMFONT),
3916 column_titles[number]);
3918 // Column sort order
3919 if( number == sort_column ) {
3920 BC_Pixmap *src = sort_order == SORT_ASCENDING ?
3921 column_sort_dn : column_sort_up;
3923 // int column_offset = get_column_offset(number) - xposition + LISTBOX_BORDER;
3924 // int column_width = get_column_width(number, 1);
3925 // int toggle_x = column_offset + column_width - LISTBOX_BORDER;
3926 // if( toggle_x > items_w ) toggle_x = items_w;
3927 // toggle_x -= 5 + src->get_w();
3930 get_text_width(MEDIUMFONT, column_titles[number]) +
3933 gui->draw_pixmap(src,
3935 title_h / 2 - src->get_h() / 2 + LISTBOX_BORDER);
3939 int BC_ListBox::draw_titles(int flash)
3941 if( column_titles &&
3942 (display_format == LISTBOX_TEXT ||
3943 display_format == LISTBOX_ICON_LIST) ) {
3944 //printf("BC_ListBox::draw_titles 1 %d\n", highlighted_title);
3945 for( int i=0; i<columns; ++i ) {
3946 if( i != highlighted_title )
3950 if( highlighted_title >= 0 ) draw_title(highlighted_title);
3960 void BC_ListBox::draw_toggles(int flash)
3962 for( int i=0; i<expanders.total; ++i )
3963 expanders.values[i]->draw(0);
3965 //printf("BC_ListBox::draw_toggles 1 %d\n", flash);
3966 if( flash && expanders.total ) {
3971 int BC_ListBox::draw_rectangle(int flash)
3973 int x1 = MIN(rect_x1, rect_x2);
3974 int x2 = MAX(rect_x1, rect_x2);
3975 int y1 = MIN(rect_y1, rect_y2);
3976 int y2 = MAX(rect_y1, rect_y2);
3978 if( x1 == x2 || y1 == y2 ) return 0;
3981 gui->set_color(WHITE);
3982 gui->draw_rectangle(x1, y1, x2 - x1, y2 - y1);
3992 void BC_ListBox::dump(ArrayList<BC_ListBoxItem*> *data, int columns,
3993 int indent, int master_column)
3996 printf("BC_ListBox::dump 1\n");
3999 for( int i=0; i<data[master_column].total; ++i ) {
4000 for( int k=0; k<indent; ++k )
4002 for( int j=0; j<columns; ++j ) {
4003 BC_ListBoxItem *item = data[j].values[i];
4004 printf("%d,%d,%d=%s ",
4005 item->get_text_x(), item->get_text_y(),
4006 item->autoplace_text, item->get_text());
4010 if( data[master_column].values[i]->get_sublist() ) {
4011 dump(data[master_column].values[i]->get_sublist(),
4012 data[master_column].values[i]->get_columns(),