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 )
2139 int BC_ListBox::get_cursor_item()
2142 gui->get_relative_cursor(rx, ry);
2143 return get_cursor_item(data, rx, ry, 0, 0);
2146 int BC_ListBox::repeat_event(int64_t duration)
2148 switch( current_operation ) {
2149 // Repeat out of bounds selection
2151 if( duration != get_resources()->scroll_repeat ) break;
2152 return rectangle_scroll_event();
2155 if( duration != get_resources()->scroll_repeat ) break;
2156 if( !drag_scroll_event() ) break;
2159 update_scrollbars(1);
2163 if( duration != get_resources()->scroll_repeat ) break;
2164 return select_scroll_event();
2168 if( duration != get_resources()->tooltip_delay ) break;
2169 if( !button_highlighted || !is_popup ) break;
2170 if( !tooltip_text || !tooltip_text[0] ) break;
2178 int BC_ListBox::cursor_enter_event()
2182 switch( current_operation ) {
2183 // Cursor moved over button, pressed, and exited.
2184 case BUTTON_DOWN_SELECT:
2185 if( top_level->event_win == win ) {
2186 current_operation = BUTTON_DN;
2188 button_highlighted = 1;
2194 // Cursor entered button
2195 if( is_popup && top_level->event_win == win ) {
2196 button_highlighted = 1;
2201 // TODO: Need to get the highlighted column title or item
2202 if( gui && top_level->event_win == gui->win ) {
2203 list_highlighted = 1;
2213 int BC_ListBox::cursor_leave_event()
2215 if( current_operation == COLUMN_DRAG ) return 0;
2218 if( button_highlighted ) {
2219 button_highlighted = 0;
2224 if( list_highlighted ) {
2225 list_highlighted = 0;
2226 highlighted_item = -1;
2227 highlighted_ptr = 0;
2228 highlighted_title = -1;
2229 int redraw_toggles = 0;
2230 for( int i=0; i<expanders.total; ++i )
2231 expanders.values[i]->cursor_leave_event(&redraw_toggles);
2239 int BC_ListBox::get_first_selection(ArrayList<BC_ListBoxItem*> *data, int *result)
2242 if( !result ) result = &temp;
2244 for( int i=0; i<data[master_column].total; ++i ) {
2245 BC_ListBoxItem *item = data[master_column].values[i];
2247 if( item->selected ) return (*result);
2248 if( item->get_sublist() ) {
2249 if( get_first_selection(item->get_sublist(), result) >= 0 )
2256 int BC_ListBox::get_total_items(ArrayList<BC_ListBoxItem*> *data,
2261 if( !result ) result = &temp;
2263 for( int i=0; i<data[master_column].total; ++i ) {
2265 if( data[master_column].values[i]->get_sublist() )
2266 get_total_items(data[master_column].values[i]->get_sublist(),
2275 int BC_ListBox::get_last_selection(ArrayList<BC_ListBoxItem*> *data,
2285 for( int i=data[master_column].total-1; i>=0; --i ) {
2286 BC_ListBoxItem *item = data[master_column].values[i];
2288 if( item->selected ) {
2290 return get_total_items(data, 0, master_column) - (*result) /* - 1 */;
2295 if( item->get_sublist() ) {
2296 if( get_last_selection(item->get_sublist(), result) >= 0 ) {
2298 return get_total_items(data, 0, master_column) - (*result) /* - 1 */;
2307 void BC_ListBox::select_range(ArrayList<BC_ListBoxItem*> *data,
2308 int start, int end, int *current)
2311 if( !current ) current = &temp;
2313 for( int i=0; i<data[master_column].total; ++i ) {
2315 if( (*current) >= start && (*current) < end ) {
2316 for( int j=0; j<columns; ++j )
2317 data[j].values[i]->selected = 1;
2319 BC_ListBoxItem *item = data[master_column].values[i];
2320 if( item->get_sublist() )
2321 select_range(item->get_sublist(), start, end, current);
2326 // Fill items between current selection and new selection
2327 int BC_ListBox::expand_selection(int button_press, int selection_number)
2329 int old_selection_start = selection_start;
2330 int old_selection_end = selection_end;
2332 // printf("BC_ListBox::expand_selection %d %d\n",
2333 // selection_center,
2334 // selection_number);
2336 // Calculate the range to select based on selection_center and selection_number
2337 if( selection_number < selection_center ) {
2338 selection_start = selection_number;
2341 selection_end = selection_number + 1;
2344 //printf("BC_ListBox::expand_selection %d %d %d %d\n", old_selection_start, old_selection_end, selection_start, selection_end);
2345 // Recurse through all the items and select the desired range
2346 select_range(data, selection_start, selection_end);
2349 return (old_selection_start != selection_start ||
2350 old_selection_end != selection_end);
2353 int BC_ListBox::toggle_item_selection(ArrayList<BC_ListBoxItem*> *data,
2354 int selection_number, int *counter)
2357 if( !counter ) counter = &temp;
2359 for( int i=0; i<data[master_column].total; ++i ) {
2360 BC_ListBoxItem *item = data[master_column].values[i];
2362 if( (*counter) == selection_number ) {
2363 // Get new value for selection
2364 int selected = !item->selected;
2366 for( int j=0; j<columns; ++j )
2367 data[j].values[i]->selected = selected;
2371 // Descend into sublist
2372 if( item->get_sublist() ) {
2373 if( toggle_item_selection(item->get_sublist(),
2374 selection_number, counter) )
2383 void BC_ListBox::set_all_selected(ArrayList<BC_ListBoxItem*> *data, int value)
2385 for( int i=0; i<data[master_column].total; ++i ) {
2386 for( int j=0; j<columns; ++j ) {
2387 BC_ListBoxItem *item = data[j].values[i];
2388 item->selected = value;
2390 BC_ListBoxItem *item = data[master_column].values[i];
2391 if( item->get_sublist() ) {
2392 set_all_selected(item->get_sublist(), value);
2397 void BC_ListBox::set_selected(ArrayList<BC_ListBoxItem*> *data,
2398 int item_number, int value, int *counter)
2401 if( !counter ) counter = &temp;
2402 for( int i=0; i<data[master_column].total&&(*counter)!=item_number; ++i ) {
2404 if( (*counter) == item_number ) {
2405 for( int j=0; j<columns; ++j ) {
2406 BC_ListBoxItem *item = data[j].values[i];
2407 item->selected = value;
2412 BC_ListBoxItem *item = data[master_column].values[i];
2413 if( item->get_sublist() ) {
2414 set_selected(item->get_sublist(), item_number, value, counter);
2419 int BC_ListBox::update_selection(ArrayList<BC_ListBoxItem*> *data,
2420 int selection_number, int *counter)
2424 if( !counter ) counter = &temp;
2426 for( int i=0; i<data[master_column].total; ++i ) {
2427 BC_ListBoxItem *item = data[master_column].values[i];
2429 if( (*counter) == selection_number && !item->selected ) {
2431 for( int j=0; j<columns; ++j )
2432 data[j].values[i]->selected = 1;
2435 if( (*counter) != selection_number && item->selected ) {
2437 for( int j=0; j<columns; ++j )
2438 data[j].values[i]->selected = 0;
2440 if( item->get_sublist() )
2441 result |= update_selection(item->get_sublist(),
2448 void BC_ListBox::promote_selections(ArrayList<BC_ListBoxItem*> *data,
2449 int old_value, int new_value)
2451 for( int i=0; i<data[master_column].total; ++i ) {
2452 for( int j=0; j<columns; ++j ) {
2453 BC_ListBoxItem *item = data[j].values[i];
2454 if( item->selected == old_value ) item->selected = new_value;
2456 BC_ListBoxItem *item = data[master_column].values[i];
2457 if( item->get_sublist() )
2458 promote_selections(item->get_sublist(), old_value, new_value);
2462 int BC_ListBox::focus_out_event()
2468 int BC_ListBox::button_press_event()
2471 BC_ListBoxItem *current_item = 0;
2473 int do_selection_change = 0;
2474 const int debug = 0;
2477 if( debug ) printf("BC_ListBox::button_press_event %d this=%p event_win=%p %p %p %p\n",
2478 __LINE__, this, (void*)top_level->event_win,
2479 (void*)(gui ? gui->win : 0), (void*)win, (void*)parent_window->win);
2481 // Pressed in button
2482 if( is_popup && top_level->event_win == win ) {
2483 if( debug ) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2484 current_operation = BUTTON_DN;
2488 if( !active && !disabled ) {
2489 top_level->deactivate();
2496 // Pressed in scrollbar
2497 if( (xscrollbar && top_level->event_win == xscrollbar->win) ||
2498 (yscrollbar && top_level->event_win == yscrollbar->win) ) {
2499 if( debug ) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2504 if( gui && top_level->event_win == gui->win ) {
2505 if( debug ) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2507 // Activate list items
2508 // If it is a suggestion popup, it is visible without being active
2510 if( debug ) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2511 if( !is_suggestions ) top_level->deactivate();
2512 if( debug ) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2514 if( debug ) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2517 if( current_operation != NO_OPERATION ) {
2518 switch( current_operation ) {
2521 return drag_stop_event();
2525 // Wheel mouse pressed
2526 if( get_buttonpress() == 4 ) {
2527 if( current_operation == NO_OPERATION ) {
2528 current_operation = WHEEL;
2530 set_yposition(yposition - gui->get_h() / 10, 0);
2532 update_scrollbars(0);
2533 highlighted_ptr = 0;
2534 highlighted_item = get_cursor_item(data,
2535 top_level->cursor_x, top_level->cursor_y,
2543 if( get_buttonpress() == 5 ) {
2544 if( current_operation == NO_OPERATION ) {
2545 current_operation = WHEEL;
2547 set_yposition(yposition + gui->get_h() / 10, 0);
2549 update_scrollbars(0);
2550 highlighted_ptr = 0;
2551 highlighted_item = get_cursor_item(data,
2552 top_level->cursor_x, top_level->cursor_y,
2560 // Pressed over column title division
2561 if( test_column_divisions(gui->get_cursor_x(),
2562 gui->get_cursor_y(),
2564 drag_cursor_x = gui->get_cursor_x() + xposition;
2566 drag_column_w = column_width[highlighted_division - 1];
2568 drag_column_w = default_column_width[highlighted_division - 1];
2570 current_operation = DRAG_DIVISION;
2574 // Pressed in column title
2575 if( test_column_titles(gui->get_cursor_x(), gui->get_cursor_y()) ) {
2576 current_operation = COLUMN_DN;
2577 button_highlighted = 0;
2578 list_highlighted = 1;
2583 // Pressed in expander
2584 if( test_expanders() ) {
2585 current_operation = EXPAND_DN;
2586 // Need to redraw items because of alpha
2591 // Pressed over item
2592 if( (selection_number = get_cursor_item(data,
2593 gui->get_cursor_x(), gui->get_cursor_y(),
2594 ¤t_item)) >= 0 ) {
2595 if( debug ) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2597 // Get item button was pressed over
2598 selection_number2 = selection_number1;
2599 selection_number1 = selection_number;
2601 selection_start = -1;
2605 // Multiple item selection is possible
2606 if( selection_mode == LISTBOX_MULTIPLE &&
2607 (ctrl_down() || shift_down()) ) {
2608 // Expand text selection.
2609 // Fill items between selected region and current item.
2611 (display_format == LISTBOX_TEXT ||
2612 display_format == LISTBOX_ICON_LIST) ) {
2613 // Get first item selected
2614 selection_start = get_first_selection(data);
2615 // Get last item selected
2616 selection_end = get_last_selection(data);
2617 // Get center of selected region
2618 if( selection_end > selection_start ) {
2619 selection_center = (selection_end + selection_start) >> 1;
2622 selection_center = selection_number;
2624 // Deselect everything.
2625 set_all_selected(data, 0);
2626 // Select just the items
2627 expand_selection(1, selection_number);
2630 // Toggle a single item on or off
2632 toggle_item_selection(data, selection_number);
2633 new_value = current_item->selected;
2636 // Select single item
2638 if( !current_item->selected ) {
2639 set_all_selected(data, 0);
2648 current_operation = SELECT;
2649 highlighted_item = selection_number;
2650 highlighted_ptr = current_item;
2651 button_highlighted = 0;
2652 list_highlighted = 1;
2655 do_selection_change = 1;
2659 // Pressed over nothing. Start rectangle selection.
2661 if( get_buttonpress() == 1 &&
2662 selection_mode == LISTBOX_MULTIPLE ) {
2663 if( !shift_down() ) {
2664 // Deselect all and redraw if anything was selected
2665 if( get_selection_number(0, 0) >= 0 ) {
2666 set_all_selected(data, 0);
2668 do_selection_change = 1;
2673 // Promote selections to protect from a rectangle selection
2674 promote_selections(data, 1, 2);
2677 // Start rectangle selection
2678 current_operation = SELECT_RECT;
2679 rect_x1 = rect_x2 = get_cursor_x();
2680 rect_y1 = rect_y2 = get_cursor_y();
2688 // Suggestion box is not active but visible, so lie to get it to deactivate
2689 if( is_popup && (active || (is_suggestions && gui)) ) {
2691 if( debug ) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2697 if( do_selection_change ) selection_changed();
2698 if( debug ) printf("BC_ListBox::button_press_event %d %d\n", __LINE__, result);
2703 int BC_ListBox::button_release_event()
2708 unset_scroll_repeat();
2710 //printf("BC_ListBox::button_release_event 1 %d\n", current_operation);
2711 switch( current_operation ) {
2713 current_operation = NO_OPERATION;
2718 current_operation = NO_OPERATION;
2722 // Release item selection
2723 case BUTTON_DOWN_SELECT:
2725 //printf("BC_ListBox::button_release_event 10\n");
2726 current_operation = NO_OPERATION;
2728 selection_number1 = selection_number = get_cursor_item();
2729 //printf("BC_ListBox::button_release_event %d %d\n", selection_number2, selection_number1);
2734 if( selection_number >= 0 ) {
2739 // Second button release outside button
2740 if( button_releases > 1 ) {
2745 if( top_level->get_double_click() &&
2746 selection_number2 == selection_number1 &&
2747 selection_number2 >= 0 && selection_number1 >= 0 ) {
2757 // Demote selections from rectangle selection
2758 promote_selections(data, 2, 1);
2761 // Hide rectangle overlay
2763 current_operation = NO_OPERATION;
2767 // Release popup button
2770 current_operation = NO_OPERATION;
2774 // Second button release inside button
2775 if( button_releases > 1 ) {
2782 current_operation = NO_OPERATION;
2783 // Update the sort column and the sort order for the user only if the existing
2784 // sort column is valid.
2785 if( sort_column >= 0 ) {
2786 // Invert order only if column is the same
2787 if( highlighted_title == sort_column )
2788 sort_order = sort_order == SORT_ASCENDING ?
2789 SORT_DESCENDING : SORT_ASCENDING;
2790 // Set the new sort column
2791 sort_column = highlighted_title;
2792 if( !sort_order_event() ) {
2797 // Sorting not enabled. Redraw the title state.
2805 int redraw_toggles = 0;
2806 for( int i=0; i<expanders.total&&!result; ++i ) {
2807 if( expanders.values[i]->button_release_event(&redraw_toggles) ) {
2811 // Need to redraw items because of alpha
2812 if( redraw_toggles ) draw_items(1);
2813 current_operation = NO_OPERATION;
2817 // Can't default to NO_OPERATION because it may be used in a drag event.
2822 if( do_event ) handle_event();
2824 //printf("BC_ListBox::button_release_event %d %d\n", __LINE__, get_window_lock());
2828 int BC_ListBox::get_title_h()
2830 if( display_format == LISTBOX_TEXT ||
2831 display_format == LISTBOX_ICON_LIST )
2832 return column_titles ? column_bg[0]->get_h() : 0;
2836 void BC_ListBox::reset_cursor(int new_cursor)
2839 if( gui->get_cursor() != new_cursor ) {
2840 gui->set_cursor(new_cursor, 0, 0);
2844 if( get_cursor() != new_cursor ) {
2845 set_cursor(new_cursor, 0, 0);
2849 int BC_ListBox::test_column_divisions(int cursor_x, int cursor_y, int &new_cursor)
2851 if( gui && column_titles &&
2852 cursor_y >= 0 && cursor_y < get_title_h() &&
2853 cursor_x >= 0 && cursor_x < gui->get_w() ) {
2854 for( int i=1; i<columns; ++i ) {
2855 if( cursor_x >= -xposition + get_column_offset(i) - 5 &&
2856 cursor_x < -xposition + get_column_offset(i) +
2857 get_resources()->listbox_title_hotspot ) {
2858 highlighted_item = -1;
2859 highlighted_ptr = 0;
2860 highlighted_division = i;
2861 highlighted_title = -1;
2862 list_highlighted = 1;
2863 new_cursor = HSEPARATE_CURSOR;
2868 highlighted_division = -1;
2872 int BC_ListBox::test_column_titles(int cursor_x, int cursor_y)
2874 if( gui && column_titles &&
2875 cursor_y >= 0 && cursor_y < get_title_h() &&
2876 cursor_x >= 0 && cursor_x < gui->get_w() ) {
2877 for( int i=0; i<columns; ++i ) {
2878 if( cursor_x >= -xposition + get_column_offset(i) &&
2879 (cursor_x < -xposition + get_column_offset(i + 1) ||
2880 i == columns - 1) ) {
2881 highlighted_item = -1;
2882 highlighted_ptr = 0;
2883 highlighted_division = -1;
2884 highlighted_title = i;
2885 list_highlighted = 1;
2890 highlighted_title = -1;
2894 int BC_ListBox::test_expanders()
2896 for( int i=0; i<expanders.total; ++i ) {
2897 if( expanders.values[i]->button_press_event() ) {
2898 current_operation = EXPAND_DN;
2906 int BC_ListBox::cursor_motion_event()
2908 int redraw = 0, result = 0;
2909 int new_cursor = ARROW_CURSOR;
2911 selection_number = -1;
2914 switch( current_operation ) {
2916 // Button pressed and slid off button
2917 if( !cursor_inside() ) {
2918 current_operation = BUTTON_DOWN_SELECT;
2924 case DRAG_DIVISION: {
2925 // int new_w = get_cursor_x() +
2927 // get_column_offset(highlighted_division - 1);
2928 int difference = get_cursor_x() + xposition - drag_cursor_x;
2929 int new_w = drag_column_w + difference;
2931 new_cursor = HSEPARATE_CURSOR;
2933 if( column_width ) {
2934 column_width[highlighted_division - 1] = new_w;
2937 default_column_width[highlighted_division - 1] = new_w;
2940 column_width_boundaries();
2942 // Force update of coords
2943 set_autoplacement(data, 0, 1);
2944 column_resize_event();
2948 update_scrollbars(1);
2953 if( test_drag_scroll(get_cursor_x(), get_cursor_y()) )
2954 set_scroll_repeat();
2956 int old_x1 = MIN(rect_x1, rect_x2);
2957 int old_x2 = MAX(rect_x1, rect_x2);
2958 int old_y1 = MIN(rect_y1, rect_y2);
2959 int old_y2 = MAX(rect_y1, rect_y2);
2961 int new_rect_x2 = get_cursor_x();
2962 int new_rect_y2 = get_cursor_y();
2964 int x1 = MIN(rect_x1, new_rect_x2);
2965 int x2 = MAX(rect_x1, new_rect_x2);
2966 int y1 = MIN(rect_y1, new_rect_y2);
2967 int y2 = MAX(rect_y1, new_rect_y2);
2969 // Adjust rectangle coverage
2970 if( old_x1 != x1 || old_x2 != x2 ||
2971 old_y1 != y1 || old_y2 != y2 ) {
2973 redraw = select_rectangle(data, x1, y1, x2, y2);
2978 //printf("BC_ListBox::cursor_motion_event %d\n", __LINE__);
2983 rect_x2 = get_cursor_x();
2984 rect_y2 = get_cursor_y();
2988 update_scrollbars(1);
2989 selection_changed();
2990 //printf("BC_ListBox::cursor_motion_event %d\n", __LINE__);
2993 if( old_x1 != x1 || old_x2 != x2 ||
2994 old_y1 != y1 || old_y2 != y2 ) {
2995 //printf("BC_ListBox::cursor_motion_event %d\n", __LINE__);
3003 int old_highlighted_item = highlighted_item;
3005 if( test_drag_scroll(get_cursor_x(), get_cursor_y()) )
3006 set_scroll_repeat();
3008 highlighted_item = selection_number = get_cursor_item(data,
3009 get_cursor_x(), get_cursor_y(), &highlighted_ptr);
3012 // Deselect all items and select just the one we're over
3013 if( selection_number >= 0 && !allow_drag &&
3014 ((!shift_down() && !ctrl_down()) ||
3015 selection_mode == LISTBOX_SINGLE) ) {
3016 redraw = update_selection(data, selection_number);
3019 // Expand multiple selection
3020 if( selection_mode == LISTBOX_MULTIPLE &&
3021 (shift_down() || ctrl_down()) ) {
3022 // Expand selected region in text mode centered around initial range
3023 if( (display_format == LISTBOX_TEXT ||
3024 display_format == LISTBOX_ICON_LIST) &&
3026 // Deselect everything.
3027 set_all_selected(data, 0);
3029 // Select just the items
3030 redraw = expand_selection(0, selection_number);
3032 // Set the one item we're over to the selection value determined in
3033 // button_press_event.
3035 set_selected(data, selection_number, new_value);
3039 if( highlighted_item != old_highlighted_item ) {
3042 update_scrollbars(1);
3043 //printf("BC_ListBox::cursor_motion_event %d %d\n", highlighted_item, old_highlighted_item);
3044 selection_changed();
3048 case BUTTON_DOWN_SELECT:
3049 // Went back into button area
3050 if( cursor_inside() ) {
3051 current_operation = BUTTON_DN;
3056 // Went into item area
3058 int cursor_x = 0, cursor_y = 0;
3059 translate_coordinates(top_level->event_win, gui->win,
3060 top_level->cursor_x, top_level->cursor_y,
3061 &cursor_x, &cursor_y);
3062 int old_highlighted_item = highlighted_item;
3063 highlighted_item = selection_number = get_cursor_item(data,
3068 if( highlighted_item != old_highlighted_item ) {
3069 update_selection(data, selection_number);
3071 selection_changed();
3077 int redraw_toggles = 0;
3078 for( int i=0; i<expanders.total&&!result; ++i ) {
3079 result = expanders.values[i]->cursor_motion_event(
3082 if( redraw_toggles ) {
3083 // Need to redraw items because of the alpha
3088 case NO_OPERATION: {
3089 int cursor_x = get_cursor_x(), cursor_y = get_cursor_y();
3090 if( gui && top_level->event_win == gui->win ) {
3091 int old_highlighted_title = highlighted_title;
3092 int old_list_highlighted = list_highlighted;
3093 int old_highlighted_item = highlighted_item;
3094 int redraw_titles = 0;
3095 int redraw_border = 0;
3096 int redraw_items = 0;
3097 int redraw_toggles = 0;
3100 // Test if cursor moved over a title division
3101 test_column_divisions(cursor_x, cursor_y, new_cursor);
3103 // Test if cursor moved over a title
3104 if( highlighted_division < 0 ) {
3105 test_column_titles(cursor_x, cursor_y);
3108 // Test if cursor moved over expander
3109 if( highlighted_division < 0 && highlighted_title < 0 &&
3110 (display_format == LISTBOX_TEXT ||
3111 display_format == LISTBOX_ICON_LIST) ) {
3112 for( int i=0; i<expanders.total; ++i ) {
3113 expanders.values[i]->cursor_motion_event(
3116 //printf("BC_ListBox::cursor_motion_event %d\n", redraw_toggles);
3119 // Test if cursor moved over an item
3120 if( highlighted_division < 0 && highlighted_title < 0 ) {
3121 highlighted_item = get_cursor_item(data,
3128 // Clear title highlighting if moved over division
3129 if( old_highlighted_title != highlighted_title ) {
3133 // Highlight list border
3134 if( old_list_highlighted != list_highlighted ) {
3138 // Moved out of item area
3139 if( old_highlighted_item != highlighted_item ) {
3143 //printf("BC_ListBox::cursor_motion_event 1 %d\n", highlighted_item);
3145 // Change cursor to title division adjustment
3146 reset_cursor(new_cursor);
3148 if( redraw_items ) {
3156 if( redraw_toggles )
3160 if( redraw_items || redraw_titles ||
3161 redraw_border || redraw_toggles ) {
3168 if( !result && list_highlighted ) {
3169 list_highlighted = 0;
3170 highlighted_item = -1;
3171 highlighted_ptr = 0;
3172 highlighted_title = -1;
3173 highlighted_division = -1;
3183 int BC_ListBox::drag_start_event()
3185 switch( current_operation ) {
3187 if( gui && gui->is_event_win() && allow_drag ) {
3188 BC_ListBoxItem *item_return = 0;
3189 selection_number = get_cursor_item(data,
3190 top_level->cursor_x,
3191 top_level->cursor_y,
3194 if( selection_number >= 0 ) {
3196 get_abs_cursor(cx, cy);
3197 if( item_return->icon_vframe ) {
3198 drag_popup = new BC_DragWindow(this,
3199 item_return->icon_vframe, cx, cy);
3202 if( item_return->icon ) {
3203 drag_popup = new BC_DragWindow(this,
3204 item_return->icon, cx, cy);
3207 drag_popup = new BC_DragWindow(this,
3208 drag_icon_vframe, cx, cy);
3210 current_operation = DRAG_ITEM;
3211 // require shift down for scrolling
3212 if( allow_drag < 0 && shift_down() )
3213 set_scroll_repeat();
3220 if( gui && gui->is_event_win() && allow_drag_column ) {
3222 get_abs_cursor(cx, cy);
3223 cx -= drag_column_icon_vframe->get_w() / 2,
3224 cy -= drag_column_icon_vframe->get_h() / 2;
3225 drag_popup = new BC_DragWindow(this,
3226 drag_column_icon_vframe, cx, cy);
3227 dragged_title = highlighted_title;
3228 current_operation = COLUMN_DRAG;
3238 int BC_ListBox::drag_motion_event()
3240 //printf("BC_ListBox::drag_motion_event 1 %d\n", current_operation);
3241 switch( current_operation ) {
3244 int new_highlighted_item = -1;
3245 BC_ListBoxItem *new_highlighted_ptr = 0;
3246 new_highlighted_item = get_cursor_item(data,
3247 top_level->cursor_x, top_level->cursor_y,
3248 &new_highlighted_ptr);
3250 if( new_highlighted_item != highlighted_item ) {
3254 // Always update highlighted value for drag_stop
3255 highlighted_item = new_highlighted_item;
3256 highlighted_ptr = new_highlighted_ptr;
3257 //printf("BC_ListBox::drag_motion_event 1 %p\n", highlighted_ptr);
3261 update_scrollbars(1);
3264 return drag_popup->cursor_motion_event(); }
3267 int old_highlighted_title = highlighted_title;
3268 test_column_titles(get_cursor_x(), get_cursor_y());
3269 if( old_highlighted_title != highlighted_title ) {
3272 return drag_popup->cursor_motion_event(); }
3278 int BC_ListBox::drag_stop_event()
3281 unset_scroll_repeat();
3282 switch( current_operation ) {
3284 // Inside window boundary
3285 if( top_level->cursor_x > 0 &&
3286 top_level->cursor_x < gui->get_w() - drag_popup->get_w() / 2 &&
3287 top_level->cursor_y > 0 &&
3288 top_level->cursor_y < gui->get_h() - drag_popup->get_h() / 2 ) {
3292 if( display_format == LISTBOX_ICONS ||
3293 display_format == LISTBOX_ICONS_PACKED ) {
3294 reposition_item(data,
3296 top_level->cursor_x - drag_popup->get_w() / 2 -
3297 LISTBOX_MARGIN - 2 + xposition,
3298 top_level->cursor_y - drag_popup->get_h() / 2 -
3299 LISTBOX_MARGIN - 2 + yposition);
3303 if( process_drag ) {
3304 // Move selected items from data to temporary
3305 ArrayList<BC_ListBoxItem*> *src_items =
3306 new ArrayList<BC_ListBoxItem*>[columns];
3307 move_selection(src_items, data);
3309 int destination = highlighted_item = item_to_index(data,
3311 // Insert items from temporary to data
3312 put_selection(data, src_items, destination);
3314 delete [] src_items;
3315 set_autoplacement(data, 0, 1);
3321 drag_popup->drag_failure_event();
3326 if( dragged_title != highlighted_title ) {
3327 if( highlighted_title >= 0 ) {
3328 if( !move_column_event() ) draw_titles(1);
3331 drag_popup->drag_failure_event();
3337 current_operation = NO_OPERATION;
3347 BC_DragWindow* BC_ListBox::get_drag_popup()
3352 int BC_ListBox::translation_event()
3354 if( is_popup && gui ) {
3355 int new_x = gui->get_x() +
3356 (top_level->last_translate_x - top_level->prev_x -
3357 BC_DisplayInfo::get_left_border());
3358 int new_y = gui->get_y() +
3359 (top_level->last_translate_y - top_level->prev_y -
3360 BC_DisplayInfo::get_top_border());
3362 gui->reposition_window(new_x, new_y);
3368 int BC_ListBox::reposition_window(int x, int y, int w, int h, int flush)
3372 if( h != -1 ) popup_h = h;
3373 //printf("BC_ListBox::reposition_window %d %d\n", popup_w, popup_h);
3377 xscrollbar->reposition_window(get_xscroll_x(),
3379 get_xscroll_width());
3381 yscrollbar->reposition_window(get_yscroll_x(),
3383 get_yscroll_height());
3387 BC_WindowBase::reposition_window(x, y, w, h);
3393 int BC_ListBox::deactivate()
3396 // printf("BC_ListBox::deactivate %d this=%p gui=%p active=%d\n",
3397 // __LINE__, this, gui, active);
3400 //printf("BC_ListBox::deactivate %d this=%p gui=%p\n", __LINE__, this, gui);
3402 delete gui; gui = 0;
3407 highlighted_item = -1;
3408 highlighted_ptr = 0;
3411 //printf("BC_ListBox::deactivate %d this=%p\n", __LINE__, this);
3413 top_level->active_subwindow = 0;
3419 int BC_ListBox::activate(int take_focus)
3421 if( active ) return 0;
3424 set_active_subwindow(this);
3425 button_releases = 0;
3426 if( !is_popup || gui ) return 0;
3427 int wx = get_x(), wy = get_y() + get_h();
3428 if( justify == LISTBOX_RIGHT ) wx += get_w() - popup_w;
3431 XTranslateCoordinates(top_level->display,
3432 parent_window->win, top_level->rootwin,
3433 wx, wy, &abs_x, &abs_y, &xwin);
3436 return activate(abs_x, abs_y);
3439 int BC_ListBox::activate(int x, int y, int w, int h)
3441 if( !is_popup || gui ) return 0;
3443 if( w != -1 ) popup_w = w;
3444 if( h != -1 ) popup_h = h;
3446 if( y + popup_h > top_level->get_root_h(0) )
3447 y -= get_h() + popup_h;
3448 add_subwindow(gui = new BC_Popup(this,
3449 x, y, popup_w, popup_h, -1, 0, 0));
3451 gui->show_window(1);
3455 int BC_ListBox::is_active()
3460 int BC_ListBox::keypress_event()
3462 if( !active ) return 0;
3464 //printf("BC_ListBox::keypress_event %d\n", __LINE__);
3466 int result = 0, redraw = 0;
3467 int view_items = last_in_view - first_in_view + 1;
3468 if( view_items <= 0 ) view_items = view_h / get_text_height(MEDIUMFONT);
3469 int new_item = -1, new_selection = -1;
3471 switch( top_level->get_keypress() ) {
3474 top_level->deactivate();
3476 // If user is manipulating popup with keyboard, don't pass on event.
3478 //printf("BC_ListBox::keypress_event %d %p\n", __LINE__, get_selection(0, 0));
3479 if( top_level->get_keypress() == RETURN )
3481 //printf("BC_ListBox::keypress_event %d %p\n", __LINE__, get_selection(0, 0));
3489 new_selection = new_item = select_previous(0);
3491 //printf("BC_ListBox::keypress_event 1 %d\n", new_item);
3492 if( new_item >= 0 ) {
3493 center_selection(new_item);
3501 new_selection = new_item = select_next(0);
3503 if( new_item >= 0 ) {
3504 center_selection(new_item);
3512 new_selection = new_item = select_previous(view_items - 1);
3514 if( new_item >= 0 ) {
3515 center_selection(new_item);
3523 new_selection = new_item = select_next(view_items - 1);
3525 if( new_item >= 0 ) {
3526 center_selection(new_item);
3552 if( show_query && !ctrl_down() ) {
3553 int query_len = strlen(query);
3554 if( query_len < (int)sizeof(query)-1 &&
3555 top_level->get_keypress() > 30 &&
3556 top_level->get_keypress() < 127 ) {
3557 query[query_len++] = top_level->get_keypress();
3558 query[query_len] = 0;
3559 new_selection = query_list();
3562 if( top_level->get_keypress() == BACKSPACE ) {
3563 if( query_len > 0 ) query[--query_len] = 0;
3564 new_selection = query_list();
3567 show_tooltip(query);
3579 update_scrollbars(1);
3582 //printf("BC_ListBox::keypress_event %d new_selection=%d\n", __LINE__, new_selection);
3583 if( new_selection >= 0 && !is_suggestions ) {
3584 //printf("BC_ListBox::keypress_event %d\n", __LINE__);
3585 selection_changed();
3586 //printf("BC_ListBox::keypress_event %d\n", __LINE__);
3593 BC_Pixmap* BC_ListBox::get_bg_surface()
3599 void BC_ListBox::draw_background()
3601 if( !bg_draw ) return;
3603 // White background pixmap
3604 set_color(top_level->get_resources()->listbox_inactive);
3605 draw_box(0, 0, bg_surface->get_w(), bg_surface->get_h(), bg_surface);
3607 // Optional heroine pixmap
3609 bg_surface->draw_pixmap(bg_pixmap,
3610 bg_surface->get_w() - top_level->get_resources()->listbox_bg->get_w(),
3614 void BC_ListBox::clear_listbox(int x, int y, int w, int h)
3616 gui->draw_pixmap(bg_surface, x, y, w, h, x, y - title_h);
3619 void BC_ListBox::update_format(int display_format, int redraw)
3621 this->display_format = display_format;
3622 packed_icons = display_format == LISTBOX_ICONS_PACKED ? 1 : 0;
3623 xposition = 0; yposition = 0;
3624 if( redraw && gui ) draw_items(1, 1);
3627 int BC_ListBox::get_format()
3629 return display_format;
3634 int BC_ListBox::draw_items(int flush, int draw_bg)
3637 BC_Resources *resources = get_resources();
3639 //dump(data, columns);
3641 // Calculate items width
3642 calculate_item_coords();
3643 // Create and destroy scrollbars as needed
3646 if( bg_draw ) this->bg_draw = 1;
3652 if( display_format == LISTBOX_ICONS ||
3653 display_format == LISTBOX_ICONS_PACKED ) {
3654 clear_listbox(2, 2 + title_h, view_w, view_h);
3656 set_font(MEDIUMFONT);
3657 for( int i=0; i<data[master_column].size(); ++i ) {
3658 BC_ListBoxItem *item = data[master_column].get(i);
3659 if( get_item_x(item) >= -get_item_w(item) &&
3660 get_item_x(item) < view_w &&
3661 get_item_y(item) >= -get_item_h(item) + title_h &&
3662 get_item_y(item) < view_h + title_h ) {
3663 item->set_in_view(1);
3664 if( first_in_view < 0 ) first_in_view = i;
3666 int item_color = get_item_highlight(data, 0, i);
3667 int icon_x, icon_y, icon_w, icon_h;
3668 int text_x, text_y, text_w, text_h;
3671 get_icon_mask(item, icon_x, icon_y, icon_w, icon_h);
3672 get_text_mask(item, text_x, text_y, text_w, text_h);
3674 if( item_color != resources->listbox_inactive ) {
3675 gui->set_color(BLACK);
3676 gui->draw_rectangle(icon_x, icon_y, icon_w, icon_h);
3677 gui->set_color(item_color);
3678 gui->draw_box(icon_x + 1, icon_y + 1, icon_w - 2, icon_h - 2);
3679 gui->set_color(BLACK);
3680 gui->draw_rectangle(text_x, text_y, text_w, text_h);
3681 gui->set_color(item_color);
3682 gui->draw_box(text_x + 1, text_y + 1, text_w - 2, text_h - 2);
3684 if( icon_position == ICON_LEFT )
3685 gui->draw_box(text_x - 1, text_y + 1, 2, text_h - 2);
3687 if( icon_position == ICON_TOP )
3688 gui->draw_line(text_x + 1, text_y, text_x + icon_w - 2, text_y);
3689 if( text_x + text_w < icon_x + icon_w ) {
3690 gui->set_color(BLACK);
3691 gui->draw_line(text_x + text_w, icon_y + icon_h,
3692 icon_x + icon_w, icon_y + icon_h);
3696 gui->set_color(get_item_color(data, 0, i));
3698 gui->pixmap->draw_pixmap(item->icon,
3699 icon_x + ICON_MARGIN, icon_y + ICON_MARGIN);
3700 char item_text[BCTEXTLEN];
3701 if( display_format == LISTBOX_ICONS_PACKED )
3702 gui->truncate_text(item_text, item->text, text_w);
3704 strcpy(item_text, item->text);
3705 gui->draw_text(text_x + ICON_MARGIN,
3706 text_y + ICON_MARGIN + get_baseline(item), item_text);
3709 item->set_in_view(0);
3714 // Draw one column at a time so text overruns don't go into the next column
3715 // clear column backgrounds
3716 int current_toggle = 0;
3717 for( int j=0; j<columns; ++j ) {
3718 clear_listbox(LISTBOX_BORDER + get_column_offset(j) - xposition,
3719 LISTBOX_BORDER + title_h,
3720 get_column_width(j, 1),
3722 // Draw rows in the column recursively
3723 draw_text_recursive(data, j, 0, ¤t_toggle);
3726 // Delete excess expanders
3727 while( expanders.total > current_toggle ) {
3728 expanders.remove_object();
3732 // draw user images if available
3734 // Draw titles on top of rows for superposition effect
3737 // Clear garbage from bottom right corner
3738 if( xscrollbar && yscrollbar && is_popup ) {
3739 gui->draw_top_background(parent_window,
3740 popup_w - get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w(),
3741 popup_h - get_resources()->hscroll_data[SCROLL_HANDLE_UP]->get_h(),
3742 get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w(),
3743 get_resources()->hscroll_data[SCROLL_HANDLE_UP]->get_h());
3750 if( current_operation == SELECT_RECT )
3760 void BC_ListBox::draw_text_recursive(ArrayList<BC_ListBoxItem*> *data,
3761 int column, int indent, int *current_toggle)
3764 BC_Resources *resources = get_resources();
3766 set_font(MEDIUMFONT);
3769 // Search for a branch and make room for toggle if there is one
3771 for( int i=0; i<data[column].size(); ++i ) {
3772 if( data[column].get(i)->get_sublist() ) {
3773 subindent = BC_WindowBase::get_resources()->listbox_expand[0]->get_w();
3779 row_height = row_ascent = row_descent = 0;
3780 for( int i=0; i<data[column].total; ++i ) {
3781 BC_ListBoxItem *item = data[column].values[i];
3782 int ht = get_text_h(item);
3783 if( ht > row_height ) row_height = ht;
3784 int bl = get_baseline(item);
3785 if( bl > row_ascent ) row_ascent = bl;
3787 if( dt > row_descent ) row_descent = dt;
3790 for( int i=0; i<data[column].size(); ++i ) {
3792 BC_ListBoxItem *item = data[column].values[i];
3793 BC_ListBoxItem *first_item = data[master_column].values[i];
3795 if( get_item_y(item) >= -get_item_h(item) + title_h &&
3796 get_item_y(item) < view_h + title_h ) {
3797 int row_color = get_item_highlight(data, 0, i);
3798 int x, y, w, h, column_width;
3800 get_text_mask(item, x, y, w, h);
3801 column_width = get_column_width(column, 1);
3802 if( x + column_width > view_w + LISTBOX_BORDER * 2 )
3803 column_width = view_w + LISTBOX_BORDER * 2 - x;
3805 if( row_color != resources->listbox_inactive ) {
3806 gui->set_color(row_color);
3807 gui->draw_box(x, y, column_width, h);
3808 gui->set_color(BLACK);
3809 int xx = x + column_width-1;
3810 gui->draw_line(x, y, xx, y);
3812 if( display_format == LISTBOX_ICON_LIST ) {
3813 int ih = get_icon_h(item);
3814 if( ih > hh ) hh = ih;
3817 gui->draw_line(x, yy, xx, yy);
3820 gui->set_color(get_item_color(data, column, i));
3823 if( column == 0 && display_format == LISTBOX_ICON_LIST ) {
3825 gui->pixmap->draw_pixmap(item->icon, x, y);
3826 x += item->icon->get_w() + ICON_MARGIN;
3831 // Indent only applies to first column
3833 x + LISTBOX_BORDER + LISTBOX_MARGIN +
3834 (column == 0 ? indent + subindent : 0),
3835 y + get_baseline(item), item->text);
3836 item->set_in_view(1);
3838 if( first_in_view < 0 ) first_in_view = i;
3843 if( column == 0 && item->get_sublist() && item->get_columns() ) {
3844 // Create new expander
3845 if( *current_toggle >= expanders.total ) {
3846 BC_ListBoxToggle *toggle =
3847 new BC_ListBoxToggle(this, item,
3848 x + LISTBOX_BORDER + LISTBOX_MARGIN + indent, y);
3850 expanders.append(toggle);
3852 // Reposition existing expander
3854 BC_ListBoxToggle *toggle = expanders.values[*current_toggle];
3855 //printf("BC_ListBox::draw_text_recursive 1 %d\n", *current_toggle);
3856 toggle->update(item,
3857 x + LISTBOX_BORDER + LISTBOX_MARGIN + indent, y, 0);
3859 (*current_toggle)++;
3866 item->set_in_view(0);
3868 // Descend into sublist
3869 if( first_item->get_expand() ) {
3870 draw_text_recursive(first_item->get_sublist(),
3872 indent + LISTBOX_INDENT,
3878 int BC_ListBox::draw_border(int flash)
3880 BC_Resources *resources = top_level->get_resources();
3881 gui->draw_3d_border(0, 0,
3882 view_w + LISTBOX_BORDER * 2, view_h + title_h + LISTBOX_BORDER * 2,
3883 resources->listbox_border1,
3885 resources->listbox_border2_hi : resources->listbox_border2,
3887 resources->listbox_border3_hi : resources->listbox_border3,
3888 resources->listbox_border4);
3897 void BC_ListBox::draw_title(int number)
3899 // Column title background
3900 int image_number = 0;
3901 if( number == highlighted_title ) {
3903 if( current_operation == COLUMN_DN )
3907 gui->draw_3segmenth(get_column_offset(number) - xposition + LISTBOX_BORDER,
3909 get_column_width(number, 1) + get_resources()->listbox_title_overlap,
3910 column_bg[image_number]);
3912 int title_x = -xposition + get_column_offset(number) +
3913 LISTBOX_MARGIN + LISTBOX_BORDER;
3914 title_x += get_resources()->listbox_title_margin;
3916 gui->set_color(get_resources()->listbox_title_color);
3917 gui->draw_text(title_x,
3918 LISTBOX_MARGIN + LISTBOX_BORDER + get_text_ascent(MEDIUMFONT),
3919 column_titles[number]);
3921 // Column sort order
3922 if( number == sort_column ) {
3923 BC_Pixmap *src = sort_order == SORT_ASCENDING ?
3924 column_sort_dn : column_sort_up;
3926 // int column_offset = get_column_offset(number) - xposition + LISTBOX_BORDER;
3927 // int column_width = get_column_width(number, 1);
3928 // int toggle_x = column_offset + column_width - LISTBOX_BORDER;
3929 // if( toggle_x > items_w ) toggle_x = items_w;
3930 // toggle_x -= 5 + src->get_w();
3933 get_text_width(MEDIUMFONT, column_titles[number]) +
3936 gui->draw_pixmap(src,
3938 title_h / 2 - src->get_h() / 2 + LISTBOX_BORDER);
3942 int BC_ListBox::draw_titles(int flash)
3944 if( column_titles &&
3945 (display_format == LISTBOX_TEXT ||
3946 display_format == LISTBOX_ICON_LIST) ) {
3947 //printf("BC_ListBox::draw_titles 1 %d\n", highlighted_title);
3948 for( int i=0; i<columns; ++i ) {
3949 if( i != highlighted_title )
3953 if( highlighted_title >= 0 ) draw_title(highlighted_title);
3963 void BC_ListBox::draw_toggles(int flash)
3965 for( int i=0; i<expanders.total; ++i )
3966 expanders.values[i]->draw(0);
3968 //printf("BC_ListBox::draw_toggles 1 %d\n", flash);
3969 if( flash && expanders.total ) {
3974 int BC_ListBox::draw_rectangle(int flash)
3976 int x1 = MIN(rect_x1, rect_x2);
3977 int x2 = MAX(rect_x1, rect_x2);
3978 int y1 = MIN(rect_y1, rect_y2);
3979 int y2 = MAX(rect_y1, rect_y2);
3981 if( x1 == x2 || y1 == y2 ) return 0;
3984 gui->set_color(WHITE);
3985 gui->draw_rectangle(x1, y1, x2 - x1, y2 - y1);
3995 void BC_ListBox::dump(ArrayList<BC_ListBoxItem*> *data, int columns,
3996 int indent, int master_column)
3999 printf("BC_ListBox::dump 1\n");
4002 for( int i=0; i<data[master_column].total; ++i ) {
4003 for( int k=0; k<indent; ++k )
4005 for( int j=0; j<columns; ++j ) {
4006 BC_ListBoxItem *item = data[j].values[i];
4007 printf("%d,%d,%d=%s ",
4008 item->get_text_x(), item->get_text_y(),
4009 item->autoplace_text, item->get_text());
4013 if( data[master_column].values[i]->get_sublist() ) {
4014 dump(data[master_column].values[i]->get_sublist(),
4015 data[master_column].values[i]->get_columns(),