e99d7316f3d0a126e1f817c58976e00ee7cf8016
[goodguy/cinelerra.git] / cinelerra-5.1 / guicast / bclistbox.C
1 /*
2  * CINELERRA
3  * Copyright (C) 2010-2014 Adam Williams <broadcast at earthling dot net>
4  *
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.
9  *
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.
14  *
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
18  *
19  */
20
21 #include "bcdisplayinfo.h"
22 #include "bcdragwindow.h"
23 #include "bclistbox.h"
24 #include "bclistboxitem.h"
25 #include "bcpixmap.h"
26 #include "bcresources.h"
27 #include "bcsignals.h"
28 #include "clip.h"
29 #include "cursors.h"
30 #include "fonts.h"
31 #include "keys.h"
32 #include "language.h"
33 #include "bctimer.h"
34 #include "vframe.h"
35
36 #include <string.h>
37 #include <unistd.h>
38
39 // ====================================================== scrollbars
40
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)
45 {
46         this->listbox = listbox;
47 }
48
49 BC_ListBoxYScroll::~BC_ListBoxYScroll()
50 {
51 }
52
53 int BC_ListBoxYScroll::handle_event()
54 {
55         listbox->set_yposition(get_value());
56         return 1;
57 }
58
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)
63 {
64         this->listbox = listbox;
65 }
66
67 BC_ListBoxXScroll::~BC_ListBoxXScroll()
68 {
69 }
70
71 int BC_ListBoxXScroll::handle_event()
72 {
73         listbox->set_xposition(get_value());
74         return 1;
75 }
76
77
78 BC_ListBoxToggle::BC_ListBoxToggle(BC_ListBox *listbox,
79         BC_ListBoxItem *item,
80         int x,
81         int y)
82 {
83         this->listbox = listbox;
84         this->item = item;
85         this->x = x;
86         this->y = y;
87         this->value = item->get_expand();
88         if( this->value )
89                 state = BC_Toggle::TOGGLE_CHECKED;
90         else
91                 state = BC_Toggle::TOGGLE_UP;
92 }
93
94 void BC_ListBoxToggle::update(BC_ListBoxItem *item,
95         int x,
96         int y,
97         int flash)
98 {
99         this->value = item->get_expand();
100         this->item = item;
101         this->x = x;
102         this->y = y;
103
104 // update state
105         switch( state ) {
106         case TOGGLE_UP:
107                 if( value )
108                         state = TOGGLE_CHECKED;
109                 break;
110
111         case TOGGLE_UPHI:
112                 if( value )
113                         state = TOGGLE_CHECKEDHI;
114                 break;
115
116         case TOGGLE_CHECKED:
117                 if( !value )
118                         state = TOGGLE_UP;
119                 break;
120
121         case TOGGLE_DOWN:
122                 break;
123
124         case TOGGLE_CHECKEDHI:
125                 if( !value )
126                         state = TOGGLE_UPHI;
127                 break;
128
129         case TOGGLE_DOWN_EXIT:
130                 break;
131         }
132
133
134         draw(flash);
135 }
136
137 int BC_ListBoxToggle::cursor_motion_event(int *redraw_toggles)
138 {
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;
145         int result = 0;
146
147         switch( state ) {
148         case BC_ListBoxToggle::TOGGLE_UPHI:
149                 if( !cursor_inside ) {
150                         state = BC_ListBoxToggle::TOGGLE_UP;
151                         *redraw_toggles = 1;
152                 }
153                 break;
154
155         case BC_ListBoxToggle::TOGGLE_CHECKEDHI:
156                 if( !cursor_inside ) {
157                         state = BC_ListBoxToggle::TOGGLE_CHECKED;
158                         *redraw_toggles = 1;
159                 }
160                 break;
161
162         case BC_ListBoxToggle::TOGGLE_DOWN:
163                 if( !cursor_inside ) {
164                         state = BC_ListBoxToggle::TOGGLE_DOWN_EXIT;
165                         *redraw_toggles = 1;
166                 }
167                 result = 1;
168                 break;
169
170         case BC_ListBoxToggle::TOGGLE_DOWN_EXIT:
171                 if( cursor_inside ) {
172                         state = BC_ListBoxToggle::TOGGLE_DOWN;
173                         *redraw_toggles = 1;
174                 }
175                 result = 1;
176                 break;
177
178         default:
179                 if( cursor_inside ) {
180                         if( value )
181                                 state = BC_ListBoxToggle::TOGGLE_CHECKEDHI;
182                         else
183                                 state = BC_ListBoxToggle::TOGGLE_UPHI;
184                         *redraw_toggles = 1;
185                 }
186                 break;
187         }
188         return result;
189 }
190
191 int BC_ListBoxToggle::cursor_leave_event(int *redraw_toggles)
192 {
193         if( value )
194                 state = BC_ListBoxToggle::TOGGLE_CHECKED;
195         else
196                 state = BC_ListBoxToggle::TOGGLE_UP;
197         return 0;
198 }
199
200 int BC_ListBoxToggle::button_press_event()
201 {
202         int w = listbox->toggle_images[0]->get_w();
203         int h = listbox->toggle_images[0]->get_h();
204
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;
210                 return 1;
211         }
212         return 0;
213 }
214
215 int BC_ListBoxToggle::button_release_event(int *redraw_toggles)
216 {
217         int result = 0;
218
219         switch( state ) {
220         case BC_ListBoxToggle::TOGGLE_DOWN:
221                 value = !value;
222                 if( value )
223                         state = BC_ListBoxToggle::TOGGLE_CHECKEDHI;
224                 else
225                         state = BC_ListBoxToggle::TOGGLE_UPHI;
226                 listbox->expand_item(item, value);
227                 result = 1;
228                 break;
229
230         case BC_ListBoxToggle::TOGGLE_DOWN_EXIT:
231                 if( value )
232                         state = BC_ListBoxToggle::TOGGLE_CHECKED;
233                 else
234                         state = BC_ListBoxToggle::TOGGLE_UP;
235                 *redraw_toggles = 1;
236                 result = 1;
237                 break;
238         }
239         return result;
240 }
241
242 void BC_ListBoxToggle::draw(int flash)
243 {
244         if( listbox->gui ) {
245                 int image_number = 0;
246                 int w = listbox->toggle_images[0]->get_w();
247                 int h = listbox->toggle_images[0]->get_h();
248
249                 switch( state ) {
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;
257                         break;
258                 }
259
260 //printf("BC_ListBoxToggle::draw 1 %d\n", state);
261                 listbox->gui->draw_pixmap(listbox->toggle_images[image_number], x, y);
262
263                 if( flash ) {
264                         listbox->gui->flash(x, y, w, h);
265                         listbox->gui->flush();
266                 }
267         }
268 }
269
270
271 // ====================================================== box
272
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)
279 {
280         justify = LISTBOX_RIGHT;
281         xposition = 0;
282         highlighted_item = -1;
283         highlighted_title = -1;
284         highlighted_division = -1;
285         highlighted_ptr = 0;
286         xscrollbar = 0;
287         yscrollbar = 0;
288         current_cursor = ARROW_CURSOR;
289         gui = 0;
290         view_w = items_w = 0;
291         view_h = items_h = 0;
292         title_h = 0;
293         active = 0;
294         is_suggestions = 0;
295         new_value = 0;
296         need_xscroll = 0;
297         need_yscroll = 0;
298         xscroll_orientation = SCROLL_HORIZ;
299         yscroll_orientation = SCROLL_VERT;
300         bg_tile = 0;
301         bg_draw = 1;
302         drag_popup = 0;
303         dragged_title = 0;
304         selection_number1 = -1;
305         selection_number2 = -1;
306         bg_surface = 0;
307         bg_pixmap = 0;
308         row_height = row_ascent = row_descent = 0;
309         selection_start = 0;
310         selection_center = 0;
311         selection_end = -1;
312         selection_number = -1;
313         current_operation = NO_OPERATION;
314         button_highlighted = 0;
315         button_releases = 0;
316         list_highlighted = 0;
317         disabled = 0;
318
319         scroll_repeat = 0;
320         allow_drag_scroll = 1;
321         process_drag = 1;
322         for( int i=0; i<32; ++i ) default_column_width[i] = 0;
323
324         sort_column = -1;
325         sort_order = 0;
326
327         allow_drag_column = 0;
328         master_column = 0;
329         search_column = 0;
330
331         popup_w = w;
332         popup_h = h;
333
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;
337
338         column_sort_up = 0;
339         column_sort_dn = 0;
340
341 //printf("BC_ListBox::BC_ListBox 1\n");
342         this->data = data;
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");
356
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");
360         }
361 //printf("BC_ListBox::BC_ListBox 2 %p %p\n", column_titles, column_width);
362
363         set_columns(column_titles, column_width, columns);
364
365 //printf("BC_ListBox::BC_ListBox 3\n");
366
367         drag_icon_vframe = 0;
368         drag_column_icon_vframe = 0;
369         drag_cursor_x = 0;
370         drag_column_w = 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;
375
376 // reset the search engine
377 //printf("BC_ListBox::BC_ListBox 4\n");
378         show_query = 0;
379         reset_query();
380 //printf("BC_ListBox::BC_ListBox 5\n");
381 }
382
383 BC_ListBox::~BC_ListBox()
384 {
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;
395
396         delete_columns();
397         if( drag_popup ) delete drag_popup;
398 }
399
400 int BC_ListBox::enable()
401 {
402         disabled = 0;
403         draw_button(1);
404         return 1;
405 }
406
407 int BC_ListBox::disable()
408 {
409         disabled = 1;
410         draw_button(1);
411         return 1;
412 }
413
414 void BC_ListBox::reset_query()
415 {
416         query[0] = 0;  // reset query
417 }
418
419 int BC_ListBox::evaluate_query(char *string)
420 {
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 ) {
424                         return i;
425                 }
426         }
427
428         return -1;
429 }
430
431 int BC_ListBox::query_list()
432 {
433         if( query[0] == 0 ) return 0;
434
435         int done = 0;
436         int result;
437         int selection_changed = 0;
438         int prev_selection = -1;
439         result = evaluate_query(query);
440         if( result >= 0 ) done = 1;
441
442         if( done ) {
443 // Deselect all
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;
448                         }
449                 }
450
451 // Select one
452                 if( prev_selection != result )
453                         selection_changed = 1;
454                 for( int j=0; j<columns; ++j ) {
455                         data[j].values[result]->selected = 1;
456                 }
457                 center_selection(result);
458         }
459
460         return selection_changed;
461 }
462
463 void BC_ListBox::init_column_width()
464 {
465         if( !column_width && data ) {
466                 int widest = 5, wd;
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;
470                 }
471                 default_column_width[0] = widest + 2 * LISTBOX_MARGIN;
472         }
473 }
474
475 int BC_ListBox::initialize()
476 {
477         if( is_popup ) {
478                 if( use_button ) {
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],
482                                         PIXMAP_ALPHA);
483                         }
484                         w = button_images[0]->get_w();
485                         h = button_images[0]->get_h();
486                 }
487
488                 gui = 0;
489                 current_operation = NO_OPERATION;
490
491         }
492         else {
493                 gui = this;
494                 current_operation = NO_OPERATION;
495         }
496
497         for( int i=0; i<3; ++i ) {
498                 column_bg[i] = new BC_Pixmap(parent_window,
499                         get_resources()->listbox_column[i],
500                         PIXMAP_ALPHA);
501         }
502         for( int i=0; i<5; ++i ) {
503                 toggle_images[i] = new BC_Pixmap(parent_window,
504                         get_resources()->listbox_expand[i],
505                         PIXMAP_ALPHA);
506         }
507
508         column_sort_up = new BC_Pixmap(parent_window,
509                 BC_WindowBase::get_resources()->listbox_up,
510                 PIXMAP_ALPHA);
511         column_sort_dn = new BC_Pixmap(parent_window,
512                 BC_WindowBase::get_resources()->listbox_dn,
513                 PIXMAP_ALPHA);
514
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],
520 //              PIXMAP_ALPHA);
521 //      drag_column_icon = new BC_Pixmap(parent_window,
522 //              get_resources()->type_to_icon[ICON_COLUMN],
523 //              PIXMAP_ALPHA);
524         BC_SubWindow::initialize();
525
526         init_column_width();
527
528         if( top_level->get_resources()->listbox_bg )
529                 bg_pixmap = new BC_Pixmap(this,
530                         get_resources()->listbox_bg,
531                         PIXMAP_OPAQUE);
532
533         draw_button(0);
534         draw_items(0);
535
536         if( !use_button && is_popup ) {
537                 hide_window(1);
538         }
539
540
541         return 0;
542 }
543
544 void BC_ListBox::deactivate_selection()
545 {
546         current_operation = NO_OPERATION;
547 }
548
549 int BC_ListBox::draw_button(int flush)
550 {
551 // Draw the button for a popup listbox
552         if( use_button && is_popup ) {
553                 int image_number = 0;
554
555                 draw_top_background(parent_window, 0, 0, w, h);
556
557                 if( button_highlighted )
558                         image_number = 1;
559                 if( current_operation == BUTTON_DN )
560                         image_number = 2;
561                 if( disabled )
562                         image_number = 3;
563
564                 pixmap->draw_pixmap(button_images[image_number],
565                         0, 0, w, h, 0, 0);
566                 flash(flush);
567         }
568         return 0;
569 }
570
571 int BC_ListBox::calculate_item_coords()
572 {
573         if( !data ) return 0;
574
575         int icon_x = 0;
576         int next_icon_x = 0;
577         int next_icon_y = 0;
578         int next_text_y = 0;
579 // Change the display_format to get the right item dimensions for both
580 // text and icons.
581         temp_display_format = display_format;
582
583
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);
588
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);
592
593         display_format = temp_display_format;
594
595         return 0;
596 }
597
598 void BC_ListBox::calculate_last_coords_recursive(
599         ArrayList<BC_ListBoxItem*> *data,
600         int *icon_x,
601         int *next_icon_x,
602         int *next_icon_y,
603         int *next_text_y,
604         int top_level)
605 {
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);
611
612 // Get next_text_y
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;
619
620 // Add sublist depth if it is expanded
621                         if( item->sublist_active() && item->get_columns() ) {
622                                 calculate_last_coords_recursive(item->get_sublist(),
623                                         icon_x, next_icon_x, next_icon_y, next_text_y, 0);
624                         }
625                 }
626
627 // Get next_icon coordinate
628                 if( top_level ) {
629                         BC_ListBoxItem *item = data[master_column].get(i);
630                         if( !item->autoplace_icon ) {
631                                 display_format = LISTBOX_ICONS;
632 // Lowest right icon coordinate.
633                                 current_icon_x = item->icon_x;
634                                 if( current_icon_x > *icon_x ) *icon_x = current_icon_x;
635                                 if( current_icon_x + get_item_w(item) > *next_icon_x ) {
636                                         *next_icon_x = current_icon_x + get_item_w(item);
637                                         *next_icon_y = 0;
638                                 }
639                                 current_icon_y = item->icon_y + get_item_h(item);
640                                 if( current_icon_y > *next_icon_y )
641                                         *next_icon_y = current_icon_y;
642                         }
643                 }
644         }
645 }
646
647
648 void BC_ListBox::calculate_item_coords_recursive(
649         ArrayList<BC_ListBoxItem*> *data,
650         int *icon_x, int *next_icon_x, int *next_icon_y, int *next_text_y,
651         int top_level)
652 {
653 // get maximum height of an icon
654         row_height = get_text_height(MEDIUMFONT);
655         if( temp_display_format == LISTBOX_ICON_LIST ) {
656                 for( int i=0; i<data[0].size(); ++i ) {
657                         if( data[0].get(i)->icon ) {
658                                 int icon_h = data[0].get(i)->icon->get_h() + 2*ICON_MARGIN;
659                                 if( row_height < icon_h ) row_height = icon_h;
660                         }
661                 }
662         }
663
664
665 // Set up items which need autoplacement.
666 // Should fill icons down and then across
667         for( int i=0; i<data[0].size(); ++i ) {
668 // Don't increase y unless the row requires autoplacing.
669                 int total_autoplaced_columns = 0;
670
671 // Set up icons in first column
672                 if( top_level ) {
673                         BC_ListBoxItem *item = data[master_column].get(i);
674                         if( item->autoplace_icon ) {
675 // 1 column only if icons are used
676                                 display_format = LISTBOX_ICONS;
677 // Test row height
678 // Start new column.
679                                 if( *next_icon_y + get_item_h(item) >= get_h() &&
680                                     *next_icon_y > 0 ) {
681                                         *icon_x = *next_icon_x;
682                                         *next_icon_y = 0;
683                                 }
684
685                                 if( *icon_x + get_item_w(item) > *next_icon_x )
686                                         *next_icon_x = *icon_x + get_item_w(item);
687
688
689                                 item->set_icon_x(*icon_x);
690                                 item->set_icon_y(*next_icon_y);
691
692                                 *next_icon_y += get_item_h(item);
693                         }
694                 }
695
696 // Set up a text row
697                 int next_text_x = 0;
698                 row_ascent = row_descent = 0;
699 // row_height still holds icon max height
700                 for( int j=0; j<columns; ++j ) {
701                         BC_ListBoxItem *item = data[j].get(i);
702                         if( item->autoplace_text ) {
703                                 display_format = LISTBOX_TEXT;
704                                 item->set_text_x(next_text_x);
705                                 item->set_text_y(*next_text_y);
706                                 int ht = get_text_h(item);
707                                 if( ht > row_height ) row_height = ht;
708                                 int bl = get_baseline(item);
709                                 if( bl > row_ascent ) row_ascent = bl;
710                                 int dt = ht - bl;
711                                 if( dt > row_descent ) row_descent = dt;
712
713 // printf("BC_ListBox::calculate_item_coords_recursive %p %d %d %d %d %s \n",
714 // item->sublist, item->get_columns(), item->get_expand(),
715 // next_text_x, *next_text_y, item->get_text());
716 // Increment position of next column
717                                 if( j < columns - 1 ) {
718                                         next_text_x += (column_width ?
719                                                 column_width[j] :
720                                                 default_column_width[j]);
721                                 }
722 // Set last column width based on text width
723                                 else {
724                                         int new_w = get_item_w(item);
725
726                                         int *previous_w = (column_width ?
727                                                 &column_width[j] :
728                                                 &default_column_width[j]);
729                                         if( new_w > *previous_w )
730                                                 *previous_w = new_w;
731 //printf("BC_ListBox::calculate_item_coords_recursive 1 %d\n", new_w);
732                                 }
733                                 total_autoplaced_columns++;
734                         }
735                 }
736
737 // Increase the text vertical position
738                 if( total_autoplaced_columns ) {
739                         display_format = LISTBOX_TEXT;
740                         *next_text_y += row_height;
741                 }
742
743 // Set up a sublist
744                 BC_ListBoxItem *item = data[master_column].values[i];
745                 if( item->sublist_active() && item->get_columns() ) {
746                         calculate_item_coords_recursive( item->get_sublist(),
747                                 icon_x, next_icon_x, next_icon_y, next_text_y, 0);
748                 }
749         }
750 }
751
752 void BC_ListBox::set_is_suggestions(int value)
753 {
754         this->is_suggestions = value;
755 }
756 void BC_ListBox::set_scroll_repeat()
757 {
758         if( scroll_repeat ) return;
759         scroll_repeat = 1;
760         set_repeat(get_resources()->scroll_repeat);
761 }
762
763 void BC_ListBox::unset_scroll_repeat()
764 {
765         if( !scroll_repeat ) return;
766         scroll_repeat = 0;
767         unset_repeat(get_resources()->scroll_repeat);
768 }
769
770 void BC_ListBox::set_use_button(int value)
771 {
772         this->use_button = value;
773 }
774
775 void BC_ListBox::set_justify(int value)
776 {
777         this->justify = value;
778 }
779
780 void BC_ListBox::set_allow_drag_column(int value)
781 {
782         this->allow_drag_column = value;
783 }
784
785 void BC_ListBox::set_process_drag(int value)
786 {
787         this->process_drag = value;
788 }
789
790 void BC_ListBox::set_master_column(int value, int redraw)
791 {
792         this->master_column = value;
793         if( redraw ) {
794                 draw_items(1);
795         }
796 }
797
798 void BC_ListBox::set_search_column(int value)
799 {
800         this->search_column = value;
801 }
802
803 int BC_ListBox::get_sort_column()
804 {
805         return sort_column;
806 }
807
808 void BC_ListBox::set_sort_column(int value, int redraw)
809 {
810         sort_column = value;
811         if( redraw ) {
812                 draw_titles(1);
813         }
814 }
815
816 int BC_ListBox::get_sort_order()
817 {
818         return sort_order;
819 }
820
821 void BC_ListBox::set_sort_order(int value, int redraw)
822 {
823         sort_order = value;
824         if( redraw ) {
825                 draw_titles(1);
826         }
827 }
828
829
830 int BC_ListBox::get_display_mode()
831 {
832         return display_format;
833 }
834
835 int BC_ListBox::get_yposition()
836 {
837         return yposition;
838 }
839
840 int BC_ListBox::get_xposition()
841 {
842         return xposition;
843 }
844
845 int BC_ListBox::get_highlighted_item()
846 {
847         return highlighted_item;
848 }
849
850
851 int BC_ListBox::get_item_x(BC_ListBoxItem *item)
852 {
853         switch( display_format ) {
854         case LISTBOX_TEXT:
855         case LISTBOX_ICON_LIST:
856                 return item->text_x - xposition + 2;
857         case LISTBOX_ICONS:
858         case LISTBOX_ICONS_PACKED:
859                 return item->icon_x - xposition + 2;
860         }
861         return 0;
862 }
863
864 int BC_ListBox::get_item_y(BC_ListBoxItem *item)
865 {
866         switch( display_format ) {
867         case LISTBOX_TEXT:
868         case LISTBOX_ICON_LIST:
869                 return item->text_y - yposition + title_h + 2;
870         case LISTBOX_ICONS:
871         case LISTBOX_ICONS_PACKED:
872                 return item->icon_y - yposition + 2;
873         }
874         return 0;
875 }
876
877 int BC_ListBox::get_item_w(BC_ListBoxItem *item)
878 {
879         switch( display_format ) {
880         case LISTBOX_TEXT:
881         case LISTBOX_ICON_LIST: {
882                 return get_text_w(item) + 2 * LISTBOX_MARGIN; }
883         case LISTBOX_ICONS:
884         case LISTBOX_ICONS_PACKED: {
885                 int x, y, w, h;
886                 get_icon_mask(item, x, y, w, h);
887                 int icon_w = w;
888                 get_text_mask(item, x, y, w, h);
889                 int text_w = w;
890
891                 return icon_position == ICON_LEFT ? icon_w + text_w :
892                         icon_w > text_w ? icon_w : text_w;
893                 }
894         }
895         return 0;
896 }
897
898 int BC_ListBox::get_item_h(BC_ListBoxItem *item)
899 {
900         switch( display_format ) {
901         case LISTBOX_TEXT:
902         case LISTBOX_ICON_LIST:
903                 return get_text_h(item);
904         case LISTBOX_ICONS:
905         case LISTBOX_ICONS_PACKED: {
906                 int x, y, w, h;
907                 get_icon_mask(item, x, y, w, h);
908                 int icon_h = h;
909                 get_text_mask(item, x, y, w, h);
910                 int text_h = h;
911
912                 return icon_position != ICON_LEFT ? icon_h + text_h :
913                         icon_h > text_h ? icon_h : text_h; }
914         }
915         return 0;
916 }
917
918
919 int BC_ListBox::get_icon_x(BC_ListBoxItem *item)
920 {
921         return get_item_x(item) + ICON_MARGIN;
922 }
923
924 int BC_ListBox::get_icon_y(BC_ListBoxItem *item)
925 {
926         return get_item_y(item) + ICON_MARGIN;
927 }
928
929 int BC_ListBox::get_icon_w(BC_ListBoxItem *item)
930 {
931         return item->get_icon_w() + 2*ICON_MARGIN;
932 }
933
934 int BC_ListBox::get_icon_h(BC_ListBoxItem *item)
935 {
936         return item->get_icon_h() + 2*ICON_MARGIN;
937 }
938
939 int BC_ListBox::get_text_w(BC_ListBoxItem *item)
940 {
941         int w = item->get_text_w();
942         if( w < 0 ) item->set_text_w(w = get_text_width(MEDIUMFONT, item->get_text()));
943         return w;
944 }
945
946 int BC_ListBox::get_text_h(BC_ListBoxItem *item)
947 {
948         int h = item->get_text_h();
949         if( h < 0 ) item->set_text_h(h = get_text_height(MEDIUMFONT, item->get_text()));
950         return h;
951 }
952
953 int BC_ListBox::get_baseline(BC_ListBoxItem *item)
954 {
955         int b = item->get_baseline();
956         if( b < 0 ) item->set_baseline(b = get_text_ascent(MEDIUMFONT));
957         return b;
958 }
959
960 int BC_ListBox::get_items_width()
961 {
962         switch( display_format ) {
963         case LISTBOX_TEXT:
964                 return get_column_offset(columns);
965         case LISTBOX_ICONS:
966         case LISTBOX_ICONS_PACKED: {
967                 int widest = 0;
968                 for( int i=0; i<columns; ++i ) {
969                         for( int j=0; j<data[i].total; ++j ) {
970                                 int x1, x, y, w, h;
971                                 BC_ListBoxItem *item = data[i].values[j];
972                                 x1 = item->icon_x;
973
974                                 get_icon_mask(item, x, y, w, h);
975                                 if( x1 + w > widest ) widest = x1 + w;
976
977                                 if( icon_position == ICON_LEFT )
978                                         x1 += w;
979
980                                 get_text_mask(item, x, y, w, h);
981                                 if( x1 + w > widest ) widest = x1 + w;
982                         }
983                 }
984                 return widest; }
985         case LISTBOX_ICON_LIST:
986                 return get_column_offset(columns);
987         }
988         return 0;
989 }
990
991 int BC_ListBox::get_items_height(ArrayList<BC_ListBoxItem*> *data, int columns,
992                 int *result)
993 {
994         int temp = 0;
995         int top_level = 0;
996         int highest = 0;
997         if( !result ) {
998                 result = &temp;
999                 top_level = 1;
1000         }
1001
1002         for( int j=0; j<(data?data[master_column].total:0); ++j ) {
1003                 int x, y, w, h;
1004                 BC_ListBoxItem *item = data[master_column].values[j];
1005
1006                 switch( display_format ) {
1007                 case LISTBOX_TEXT:
1008                         get_text_mask(item, x, y, w, h);
1009                         *result += h;
1010 // Descend into sublist
1011                         if( item->sublist_active() )
1012                                 get_items_height(item->get_sublist(), item->get_columns(), result);
1013                         break;
1014                 case LISTBOX_ICONS:
1015                 case LISTBOX_ICONS_PACKED:
1016                 case LISTBOX_ICON_LIST:
1017                         get_icon_mask(item, x, y, w, h);
1018                         if( y + h + yposition > highest ) highest = y + h + yposition;
1019
1020                         get_text_mask(item, x, y, w, h);
1021                         if( y + h + yposition > highest ) highest = y + h + yposition;
1022                         break;
1023                 }
1024         }
1025
1026         if( display_format == LISTBOX_TEXT && top_level ) {
1027                 highest = LISTBOX_MARGIN + *result;
1028         }
1029
1030         return highest;
1031 }
1032
1033 int BC_ListBox::set_yposition(int position, int draw_items)
1034 {
1035         this->yposition = position;
1036         if( draw_items ) {
1037                 this->draw_items(1);
1038         }
1039         return 0;
1040 }
1041
1042 int BC_ListBox::set_xposition(int position)
1043 {
1044         this->xposition = position;
1045         draw_items(1);
1046         return 0;
1047 }
1048
1049 int BC_ListBox::is_highlighted()
1050 {
1051         return list_highlighted;
1052 }
1053
1054 void BC_ListBox::expand_item(BC_ListBoxItem *item, int expand)
1055 {
1056         if( item ) {
1057                 item->expand = expand;
1058 // Collapse sublists if this is collapsed to make it easier to calculate
1059 // coordinates
1060                 if( item->get_sublist() )
1061                         collapse_recursive(item->get_sublist(), master_column);
1062
1063 // Set everything for autoplacement
1064                 set_autoplacement(data, 0, 1);
1065                 draw_items(1);
1066         }
1067 }
1068
1069 void BC_ListBox::collapse_recursive(ArrayList<BC_ListBoxItem*> *data,
1070                 int master_column)
1071 {
1072         for( int i=0; i<data[master_column].total; ++i ) {
1073                 BC_ListBoxItem *item = data[master_column].values[i];
1074                 if( item->sublist_active() ) {
1075                         item->expand = 0;
1076                         collapse_recursive(item->get_sublist(), master_column);
1077                 }
1078         }
1079 }
1080
1081 void BC_ListBox::set_autoplacement(ArrayList<BC_ListBoxItem*> *data,
1082         int do_icons,
1083         int do_text)
1084 {
1085         for( int i=0; i<data[0].total; ++i ) {
1086                 for( int j=0; j<columns; ++j ) {
1087                         if( do_icons ) data[j].values[i]->autoplace_icon = 1;
1088                         if( do_text ) data[j].values[i]->autoplace_text = 1;
1089                 }
1090
1091                 BC_ListBoxItem *item = data[master_column].values[i];
1092                 if( item->sublist_active() ) {
1093                         set_autoplacement(item->get_sublist(), do_icons, do_text);
1094                 }
1095         }
1096 }
1097
1098
1099 void BC_ListBox::set_scroll_stretch(int xv, int yv)
1100 {
1101         if( xv >= 0 ) xscroll_orientation =
1102                 !xv ? SCROLL_HORIZ : SCROLL_HORIZ + SCROLL_STRETCH;
1103         if( yv >= 0 ) yscroll_orientation =
1104                 !yv ? SCROLL_VERT  : SCROLL_VERT  + SCROLL_STRETCH;
1105 }
1106
1107 int BC_ListBox::get_yscroll_x()
1108 {
1109         if( is_popup )
1110                 return popup_w - get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w();
1111         else
1112                 return get_x() + popup_w -
1113                         get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w();
1114 }
1115
1116 int BC_ListBox::get_yscroll_y()
1117 {
1118         return is_popup ? 0 : get_y();
1119 }
1120
1121 int BC_ListBox::get_yscroll_height()
1122 {
1123         return popup_h - (need_xscroll ?
1124                 get_resources()->hscroll_data[SCROLL_HANDLE_UP]->get_h() :
1125                 0);
1126 }
1127
1128 int BC_ListBox::get_xscroll_x()
1129 {
1130         return is_popup ?  0 : get_x();
1131 }
1132
1133 int BC_ListBox::get_xscroll_y()
1134 {
1135         return (is_popup ? popup_h : get_y() + popup_h) -
1136                 get_resources()->hscroll_data[SCROLL_HANDLE_UP]->get_h();
1137 }
1138
1139 int BC_ListBox::get_xscroll_width()
1140 {
1141         return popup_w - (need_yscroll ?
1142                 get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w() : 0);
1143 }
1144
1145 int BC_ListBox::get_column_offset(int column)
1146 {
1147         int x = 0;
1148         while( column > 0 ) {
1149                 x += column_width ?
1150                         column_width[--column] :
1151                         default_column_width[--column];
1152         }
1153         return x;
1154 }
1155
1156 void BC_ListBox::column_width_boundaries()
1157 {
1158         if( column_width ) {
1159                 for( int i=0; i<columns; ++i ) {
1160                         if( column_width[i] < MIN_COLUMN_WIDTH )
1161                                  column_width[i] = MIN_COLUMN_WIDTH;
1162                 }
1163         }
1164         else {
1165                 for( int i=0; i<columns; ++i ) {
1166                         if( default_column_width[i] < MIN_COLUMN_WIDTH )
1167                                  default_column_width[i] = MIN_COLUMN_WIDTH;
1168                 }
1169         }
1170 }
1171
1172 int BC_ListBox::get_column_width(int column, int clamp_right)
1173 {
1174         if( column < columns - 1 || !clamp_right )
1175                 return column_width ?  column_width[column] : default_column_width[column];
1176         return popup_w + xposition - get_column_offset(column);
1177 }
1178
1179 int BC_ListBox::get_icon_mask(BC_ListBoxItem *item,
1180         int &x, int &y, int &w, int &h)
1181 {
1182         switch( display_format ) {
1183         case LISTBOX_ICONS:
1184         case LISTBOX_ICONS_PACKED:
1185         case LISTBOX_ICON_LIST: {
1186                 x = get_icon_x(item);
1187                 y = get_icon_y(item);
1188                 w = get_icon_w(item);
1189                 h = get_icon_h(item);
1190                 break; }
1191         case LISTBOX_TEXT:
1192         default: {
1193                 x = y = w = h = 0;
1194                 break; }
1195         }
1196         return 0;
1197 }
1198
1199 int BC_ListBox::get_text_mask(BC_ListBoxItem *item,
1200         int &x, int &y, int &w, int &h)
1201 {
1202         x = get_item_x(item);
1203         y = get_item_y(item);
1204
1205         switch( display_format ) {
1206         case LISTBOX_TEXT: {
1207                 w = get_text_w(item) + LISTBOX_MARGIN * 2;
1208                 h = get_text_h(item);
1209                 break; }
1210         case LISTBOX_ICONS:
1211         case LISTBOX_ICONS_PACKED: {
1212                 if( icon_position == ICON_LEFT ) {
1213                         x += get_icon_w(item);
1214                         y += get_icon_h(item) - get_text_h(item);
1215                 }
1216                 else {
1217                         y += get_icon_h(item);
1218                 }
1219
1220                 w = packed_icons ?
1221                         get_icon_w(item) + ICON_MARGIN * 2 :
1222                         get_text_w(item) + ICON_MARGIN * 2 ;
1223                 h = get_text_h(item) + ICON_MARGIN * 2;
1224                 break; }
1225         case LISTBOX_ICON_LIST: {
1226                 w = get_text_width(MEDIUMFONT, item->text) + LISTBOX_MARGIN * 2;
1227                 h = row_height;
1228                 break; }
1229         default:
1230                 w = h = 0;
1231         }
1232         return 0;
1233 }
1234
1235 int BC_ListBox::get_item_highlight(ArrayList<BC_ListBoxItem*> *data,
1236                 int column, int item)
1237 {
1238         BC_Resources *resources = get_resources();
1239         if( data[column].values[item]->selected )
1240                 return resources->listbox_selected;
1241         if( highlighted_item >= 0 &&
1242             highlighted_ptr == data[master_column].values[item] )
1243                 return resources->listbox_highlighted;
1244         return resources->listbox_inactive;
1245 }
1246
1247 int BC_ListBox::get_item_color(ArrayList<BC_ListBoxItem*> *data,
1248                 int column, int item)
1249 {
1250         int color = data[column].values[item]->color;
1251         if( color == -1 ) color = get_resources()->listbox_text;
1252         if( get_item_highlight(data, column, item) == color )
1253                 return BLACK;
1254         return color;
1255 }
1256
1257 int BC_ListBox::get_from_column()
1258 {
1259         return dragged_title;
1260 }
1261
1262 int BC_ListBox::get_to_column()
1263 {
1264         return highlighted_title;
1265 }
1266
1267
1268 BC_ListBoxItem* BC_ListBox::get_selection(int column, int selection_number)
1269 {
1270         return get_selection_recursive(data,
1271                 column,
1272                 selection_number);
1273 }
1274
1275 BC_ListBoxItem* BC_ListBox::get_selection_recursive(ArrayList<BC_ListBoxItem*> *data,
1276                 int column, int selection_number)
1277 {
1278         if( !data ) return 0;
1279
1280         for( int i=0; i<data[master_column].total; ++i ) {
1281                 BC_ListBoxItem *item = data[master_column].values[i];
1282                 if( item->selected ) {
1283 //printf("BC_ListBox::get_selection_recursive %d\n", __LINE__);
1284                         selection_number--;
1285                         if( selection_number < 0 ) {
1286
1287                                 return data[column].values[i];
1288                         }
1289                 }
1290
1291                 if( item->sublist_active() ) {
1292                         BC_ListBoxItem *result = get_selection_recursive(item->get_sublist(),
1293                                 column,
1294                                 selection_number);
1295                         if( result ) return result;
1296                 }
1297         }
1298
1299         return 0;
1300 }
1301
1302
1303 int BC_ListBox::get_selection_number(int column, int selection_number)
1304 {
1305         return get_selection_number_recursive(data, column, selection_number);
1306 }
1307
1308 int BC_ListBox::get_selection_number_recursive(ArrayList<BC_ListBoxItem*> *data,
1309                 int column, int selection_number, int *counter)
1310 {
1311         int temp = -1;
1312         if( !data ) return 0;
1313         if( !counter ) counter = &temp;
1314
1315         for( int i=0; i<data[master_column].total; ++i ) {
1316                 (*counter)++;
1317                 BC_ListBoxItem *item = data[master_column].values[i];
1318                 if( item->selected ) {
1319                         selection_number--;
1320                         if( selection_number < 0 ) {
1321                                 return (*counter);
1322                         }
1323                 }
1324                 if( item->sublist_active() ) {
1325                         int result = get_selection_number_recursive(item->get_sublist(),
1326                                 column, selection_number, counter);
1327                         if( result >= 0 ) return result;
1328                 }
1329         }
1330         return -1;
1331 }
1332
1333
1334 int BC_ListBox::set_selection_mode(int mode)
1335 {
1336         this->selection_mode = mode;
1337         return 0;
1338 }
1339
1340 void BC_ListBox::delete_columns()
1341 {
1342         if( column_titles ) {
1343                 for( int i=0; i<columns; ++i ) {
1344                         delete [] column_titles[i];
1345                 }
1346                 delete [] column_titles;
1347         }
1348
1349         if( column_width ) delete [] column_width;
1350
1351         column_titles = 0;
1352         column_width = 0;
1353 }
1354
1355 // Need to copy titles so EDL can change
1356 void BC_ListBox::set_columns(const char **column_titles, int *column_width, int columns)
1357 {
1358         if( (!column_titles && column_width) ||
1359             (column_titles && !column_width) ) {
1360                 printf("BC_ListBox::set_columns either column_titles or column_width == NULL but not both.\n");
1361                 return;
1362         }
1363
1364         delete_columns();
1365         if( column_titles ) {
1366                 this->column_titles = new char*[columns];
1367                 for( int i=0; i<columns; ++i ) {
1368                         this->column_titles[i] = new char[strlen(column_titles[i]) + 1];
1369                         strcpy(this->column_titles[i], column_titles[i]);
1370                 }
1371         }
1372         if( column_width ) {
1373                 this->column_width = new int[columns];
1374                 for( int i=0; i<columns; ++i ) {
1375                         this->column_width[i] = column_width[i];
1376                 }
1377         }
1378
1379         this->columns = columns;
1380
1381 }
1382
1383
1384 int BC_ListBox::update(ArrayList<BC_ListBoxItem*> *data,
1385         const char **column_titles, int *column_widths, int columns,
1386         int xposition, int yposition, int highlighted_number,
1387         int recalc_positions, int draw)
1388 {
1389         set_columns(column_titles, column_widths, columns);
1390         this->data = data;
1391         this->yposition = yposition;
1392         this->xposition = xposition;
1393         this->highlighted_item = highlighted_number;
1394         this->highlighted_ptr = index_to_item(data, highlighted_number, 0);
1395
1396         if( recalc_positions )
1397                 set_autoplacement(data, 1, 1);
1398
1399         init_column_width();
1400
1401         if( gui && draw ) {
1402                 draw_items(0, 1);
1403                 update_scrollbars(1);
1404         }
1405
1406         return 0;
1407 }
1408
1409 void BC_ListBox::center_selection()
1410 {
1411         int selection = get_selection_number(0, 0);
1412         calculate_item_coords();
1413         center_selection(selection);
1414
1415         if( gui ) {
1416                 draw_items(1, 1);
1417                 update_scrollbars(0);
1418                 gui->show_window(1);
1419         }
1420 }
1421
1422 void BC_ListBox::move_vertical(int pixels)
1423 {
1424 }
1425
1426 void BC_ListBox::move_horizontal(int pixels)
1427 {
1428 }
1429
1430 int BC_ListBox::select_previous(int skip, BC_ListBoxItem *selected_item, int *counter,
1431                 ArrayList<BC_ListBoxItem*> *data, int *got_first, int *got_second)
1432 {
1433         int top_level = 0;
1434         if( !selected_item )
1435                 selected_item = get_selection(0, 0);
1436         int temp = -1;
1437         if( !counter )
1438                 counter = &temp;
1439         int temp2 = 0;
1440         if( !got_first ) {
1441                 got_first = &temp2;
1442                 top_level = 1;
1443         }
1444         int temp3 = 0;
1445         if( !got_second )
1446                 got_second = &temp3;
1447         if( !data )
1448                 data = this->data;
1449
1450 // Scan backwards to item pointer.  Then count visible items to get
1451 // destination.  No wraparound.
1452         do {
1453                 for( int i=data[master_column].total-1; i>=0; --i ) {
1454                         BC_ListBoxItem *current_item = data[master_column].values[i];
1455                         if( current_item->sublist_active() ) {
1456                                 int result = select_previous(skip, selected_item, counter,
1457                                         current_item->get_sublist(), got_first, got_second);
1458                                 if( *got_second )
1459                                         return result;
1460                         }
1461
1462                         if( *got_first ) {
1463                                 (*counter)++;
1464                                 if( (*counter) >= skip ) {
1465                                         for( int j=0; j<columns; ++j )
1466                                                 data[j].values[i]->selected = 1;
1467                                         (*got_second) = 1;
1468                                         return item_to_index(this->data, current_item);
1469                                 }
1470                         }
1471                         else {
1472                                 if( current_item->selected ) {
1473                                         for( int j=0; j<columns; ++j )
1474                                                 data[j].values[i]->selected = 0;
1475                                         (*got_first) = 1;
1476                                         (*counter)++;
1477                                 }
1478                         }
1479                 }
1480
1481 // Hit top of top level without finding a selected item.
1482                 if( top_level ) {
1483 // Select first item in top level and quit
1484                         BC_ListBoxItem *current_item;
1485                         (*got_first) = 1;
1486                         current_item = data[master_column].values[0];
1487
1488                         for( int j=0; j<columns; ++j )
1489                                 data[j].values[0]->selected = 1;
1490                         (*got_second) = 1;
1491                         return item_to_index(this->data, current_item);
1492                 }
1493         } while( top_level && data[master_column].total );
1494
1495         return -1;
1496 }
1497
1498 int BC_ListBox::select_next(int skip, BC_ListBoxItem *selected_item, int *counter,
1499                 ArrayList<BC_ListBoxItem*> *data, int *got_first, int *got_second)
1500 {
1501         int top_level = 0;
1502         if( !selected_item )
1503                 selected_item = get_selection(0, 0);
1504         int temp = -1;
1505         if( !counter )
1506                 counter = &temp;
1507         int temp2 = 0;
1508         if( !got_first ) {
1509                 got_first = &temp2;
1510                 top_level = 1;
1511         }
1512         int temp3 = 0;
1513         if( !got_second )
1514                 got_second = &temp3;
1515         if( !data )
1516                 data = this->data;
1517
1518 // Scan forwards to currently selected item pointer.
1519 // Then count visible items to get destination.  No wraparound.
1520         do {
1521                 for( int i=0; i<data[master_column].total; ++i ) {
1522                         BC_ListBoxItem *current_item = data[master_column].values[i];
1523
1524 // Select next item once the number items after the currently selected item
1525 // have been passed.
1526                         if( *got_first ) {
1527                                 (*counter)++;
1528                                 if( (*counter) >= skip ) {
1529                                         for( int j=0; j<columns; ++j )
1530                                                 data[j].values[i]->selected = 1;
1531                                         (*got_second) = 1;
1532                                         return item_to_index(this->data, current_item);
1533                                 }
1534                         }
1535                         else {
1536 // Got currently selected item.  Deselect it.
1537                                 if( current_item->selected ) {
1538                                         for( int j=0; j<columns; ++j )
1539                                                 data[j].values[i]->selected = 0;
1540                                         (*got_first) = 1;
1541                                         (*counter)++;
1542                                 }
1543                         }
1544
1545 // Descend into expanded level
1546                         if( current_item->sublist_active() ) {
1547                                 int result = select_next(skip, selected_item, counter,
1548                                         current_item->get_sublist(), got_first, got_second);
1549                                 if( *got_second ) {
1550                                         return result;
1551                                 }
1552                         }
1553                 }
1554
1555 // Hit bottom of top level without finding the next item.
1556                 if( top_level ) {
1557                         BC_ListBoxItem *current_item;
1558 // Select first item in top level and quit
1559                         if( !(*got_first) ) {
1560                                 (*got_first) = 1;
1561                                 current_item = data[master_column].values[0];
1562
1563                                 for( int j=0; j<columns; ++j )
1564                                         data[j].values[0]->selected = 1;
1565                                 (*got_second) = 1;
1566                         }
1567                         else {
1568 // Select last item in top level and quit
1569                                 (*got_first) = 1;
1570                                 int current_row = data[master_column].total - 1;
1571                                 current_item = data[master_column].values[current_row];
1572
1573                                 for( int j=0; j<columns; ++j )
1574                                         data[j].values[current_row]->selected = 1;
1575                                 (*got_second) = 1;
1576                         }
1577
1578                         return item_to_index(this->data, current_item);
1579                 }
1580         } while( top_level && data[master_column].total );
1581
1582         return -1;
1583 }
1584
1585
1586 void BC_ListBox::clamp_positions()
1587 {
1588         items_w = get_items_width();
1589         if( xscroll_orientation & SCROLL_STRETCH )
1590                 items_w += view_w / 4;
1591         items_h = get_items_height(data, columns);
1592         if( yscroll_orientation & SCROLL_STRETCH )
1593                 items_h += view_h / 4;
1594
1595         if( yposition < 0 ) yposition = 0;
1596         else
1597         if( yposition > items_h - view_h )
1598                 yposition = items_h - view_h;
1599
1600         if( yposition < 0 ) yposition = 0;
1601
1602         if( xposition < 0 ) xposition = 0;
1603         else
1604         if( xposition >= items_w - view_w )
1605                 xposition = items_w - view_w;
1606
1607         if( xposition < 0 ) xposition = 0;
1608 }
1609
1610 int BC_ListBox::center_selection(int selection,
1611                 ArrayList<BC_ListBoxItem*> *data, int *counter)
1612 {
1613         int temp = -1;
1614         if( !data ) data = this->data;
1615         if( !counter ) counter = &temp;
1616
1617         for( int i=0; i<data[master_column].total; ++i ) {
1618                 (*counter)++;
1619
1620 // Got it
1621                 BC_ListBoxItem *item = data[master_column].values[i];
1622                 if( (*counter) == selection ) {
1623                         BC_ListBoxItem *top_item = this->data[master_column].values[0];
1624                         switch( display_format ) {
1625                         case LISTBOX_ICONS:
1626                         case LISTBOX_ICONS_PACKED: {
1627 // Icon is out of window
1628                                 if( item->icon_y-yposition  > view_h-get_text_h(item) ||
1629                                     item->icon_y-yposition < 0 ) {
1630                                         yposition = item->icon_y - view_h / 2;
1631                                 }
1632
1633                                 if( data[master_column].values[selection]->icon_x - xposition > view_w ||
1634                                     data[master_column].values[selection]->icon_x - xposition < 0 ) {
1635                                         xposition = item->icon_x - view_w / 2;
1636                                 }
1637                                 break; }
1638                         case LISTBOX_TEXT:
1639                         case LISTBOX_ICON_LIST:
1640 // Text coordinate is out of window
1641                                 if( item->text_y-yposition  > view_h-get_text_h(item) ||
1642                                     item->text_y-yposition < 0 ) {
1643                                         yposition = item->text_y - top_item->text_y - view_h / 2;
1644                                 }
1645                         }
1646                         if( yposition < 0 ) yposition = 0;
1647                         return 1;
1648                 }
1649
1650 // Descend
1651                 if( item->sublist_active() ) {
1652                         int result = center_selection(selection, item->get_sublist(), counter);
1653                         if( result ) return result;
1654                 }
1655         }
1656         return 0;
1657 }
1658
1659 void BC_ListBox::update_scrollbars(int flush)
1660 {
1661         int h_needed = items_h = get_items_height(data, columns);
1662         int w_needed = items_w = get_items_width();
1663
1664 // if( columns > 0 && column_width )
1665 // printf("BC_ListBox::update_scrollbars 1 %d %d\n", column_width[columns - 1], w_needed);
1666
1667         if( xscrollbar ) {
1668                 if( xposition != xscrollbar->get_value() )
1669                         xscrollbar->update_value(xposition);
1670
1671                 if( w_needed != xscrollbar->get_length() ||
1672                     view_w != xscrollbar->get_handlelength() )
1673                         xscrollbar->update_length(w_needed, xposition, view_w, 0);
1674         }
1675
1676         if( yscrollbar ) {
1677                 if( yposition != yscrollbar->get_value() )
1678                         yscrollbar->update_value(yposition);
1679
1680                 if( h_needed != yscrollbar->get_length() || view_h != yscrollbar->get_handlelength() )
1681                         yscrollbar->update_length(h_needed, yposition, view_h, 0);
1682         }
1683
1684         if( flush ) this->flush();
1685 }
1686
1687 int BC_ListBox::get_scrollbars()
1688 {
1689         int h_needed = items_h = get_items_height(data, columns);
1690         int w_needed = items_w = get_items_width();
1691         int flush = 0;
1692
1693         title_h = get_title_h();
1694         view_h = popup_h - title_h - 4;
1695         view_w = popup_w - 4;
1696
1697 // Create scrollbars as needed
1698         for( int i=0; i<2; ++i ) {
1699                 if( w_needed > view_w ) {
1700                         need_xscroll = 1;
1701                         view_h = popup_h -
1702                                 title_h -
1703                                 get_resources()->hscroll_data[SCROLL_HANDLE_UP]->get_h() -
1704                                 4;
1705                 }
1706                 else {
1707                         need_xscroll = 0;
1708                 }
1709
1710                 if( h_needed > view_h ) {
1711                         need_yscroll = 1;
1712                         view_w = popup_w -
1713                                 get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w() -
1714                                 4;
1715                 }
1716                 else {
1717                         need_yscroll = 0;
1718                 }
1719         }
1720
1721 // Update subwindow size
1722         int new_w = popup_w;
1723         int new_h = popup_h;
1724         if( need_xscroll ) new_h -= get_resources()->hscroll_data[SCROLL_HANDLE_UP]->get_h();
1725         if( need_yscroll ) new_w -= get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w();
1726
1727         if( !is_popup )
1728                 if( new_w != BC_WindowBase::get_w() || new_h != BC_WindowBase::get_h() )
1729                         gui->resize_window(new_w, new_h);
1730
1731         BC_WindowBase *destination = (is_popup ? gui : parent_window);
1732         if( need_xscroll ) {
1733                 if( !xscrollbar ) {
1734                         xscrollbar = new BC_ListBoxXScroll(this);
1735                         destination->add_subwindow(xscrollbar);
1736                         xscrollbar->show_window(0);
1737                         xscrollbar->bound_to = this;
1738                 }
1739                 else {
1740                     xscrollbar->update_length(w_needed, xposition, view_w, flush);
1741                         xscrollbar->reposition_window(get_xscroll_x(),
1742                                 get_xscroll_y(),
1743                                 get_xscroll_width());
1744                 }
1745         }
1746         else {
1747                 if( xscrollbar ) delete xscrollbar;
1748                 xscrollbar = 0;
1749                 xposition = 0;
1750         }
1751
1752         if( need_yscroll ) {
1753                 if( !yscrollbar ) {
1754                         yscrollbar = new BC_ListBoxYScroll(this);
1755                         destination->add_subwindow(yscrollbar);
1756                         yscrollbar->show_window(0);
1757                         yscrollbar->bound_to = this;
1758                 }
1759                 else {
1760                         yscrollbar->update_length(h_needed, yposition, view_h, flush);
1761                         yscrollbar->reposition_window(get_yscroll_x(),
1762                                 get_yscroll_y(),
1763                                 get_yscroll_height());
1764                 }
1765         }
1766         else {
1767                 if( yscrollbar ) delete yscrollbar;
1768                 yscrollbar = 0;
1769                 yposition = 0;
1770         }
1771
1772         if( !bg_surface ||
1773             view_w + 4 != bg_surface->get_w() ||
1774             view_h + 4 != bg_surface->get_h() ) {
1775                 if( bg_surface ) delete bg_surface;
1776                 bg_surface = new BC_Pixmap(gui, view_w + 4, view_h + 4);
1777                 bg_draw = 1;
1778         }
1779
1780
1781         return 0;
1782 }
1783
1784
1785 void BC_ListBox::set_drag_scroll(int value)
1786 {
1787         allow_drag_scroll = value;
1788 }
1789
1790 // Test for scrolling by dragging
1791
1792 int BC_ListBox::test_drag_scroll(int cursor_x, int cursor_y)
1793 {
1794         int result = 0;
1795         if( allow_drag_scroll ||
1796             current_operation == SELECT_RECT ) {
1797
1798                 int top_boundary = get_title_h();
1799
1800                 if( cursor_y < top_boundary ||
1801                     cursor_y >= view_h + title_h + LISTBOX_BORDER * 2 ||
1802                     cursor_x < LISTBOX_BORDER ||
1803                     cursor_x >= view_w + LISTBOX_BORDER ) {
1804                         result = 1;
1805                 }
1806         }
1807         return result;
1808 }
1809
1810 int BC_ListBox::drag_scroll_event()
1811 {
1812         int top_boundary = get_title_h();
1813         int result = 0;
1814
1815         if( get_cursor_y() < top_boundary ) {
1816                 yposition -= top_boundary - get_cursor_y();
1817                 result = 1;
1818         }
1819         else
1820         if( get_cursor_y() >= view_h + title_h + 4 ) {
1821                 yposition += get_cursor_y() - (view_h + title_h + 4);
1822                 result = 1;
1823         }
1824
1825         if( get_cursor_x() < 2 ) {
1826                 xposition -= 2 - get_cursor_x();
1827                 result = 1;
1828         }
1829         else
1830         if( get_cursor_x() >= view_w + 2 ) {
1831                 xposition += get_cursor_x() - (view_w + 2);
1832                 result = 1;
1833         }
1834
1835         if( result )
1836                 clamp_positions();
1837
1838         return result;
1839 }
1840
1841 int BC_ListBox::rectangle_scroll_event()
1842 {
1843         int old_xposition = xposition;
1844         int old_yposition = yposition;
1845         int result = drag_scroll_event();
1846
1847         if( result ) {
1848                 rect_x1 += old_xposition - xposition;
1849                 rect_y1 += old_yposition - yposition;
1850                 rect_x2 = get_cursor_x();
1851                 rect_y2 = get_cursor_y();
1852
1853                 int x1 = MIN(rect_x1, rect_x2);
1854                 int x2 = MAX(rect_x1, rect_x2);
1855                 int y1 = MIN(rect_y1, rect_y2);
1856                 int y2 = MAX(rect_y1, rect_y2);
1857
1858                 if( select_rectangle(data, x1, y1, x2, y2) ) {
1859                         selection_changed();
1860                 }
1861
1862                 clamp_positions();
1863                 draw_items(0);
1864                 update_scrollbars(1);
1865         }
1866         return result;
1867 }
1868
1869 int BC_ListBox::select_scroll_event()
1870 {
1871         int result = drag_scroll_event();
1872         if( result ) {
1873                 highlighted_item = selection_number = get_cursor_item(data,
1874                         get_cursor_x(), get_cursor_y(), &highlighted_ptr);
1875                 clamp_positions();
1876                 draw_items(0);
1877                 update_scrollbars(1);
1878                 selection_changed();
1879         }
1880         return result;
1881 }
1882
1883 int BC_ListBox::select_rectangle(ArrayList<BC_ListBoxItem*> *data,
1884                 int x1, int y1, int x2, int y2)
1885 {
1886         int result = 0;
1887         for( int i=0; i<data[master_column].total; ++i ) {
1888                 for( int j=0; j<columns; ++j ) {
1889                         BC_ListBoxItem *item = data[j].values[i];
1890                         switch( display_format ) {
1891                         case LISTBOX_ICONS:
1892                         case LISTBOX_ICONS_PACKED: {
1893                                 int icon_x, icon_y, icon_w, icon_h;
1894                                 int text_x, text_y, text_w, text_h;
1895                                 get_icon_mask(item, icon_x, icon_y, icon_w, icon_h);
1896                                 get_text_mask(item, text_x, text_y, text_w, text_h);
1897
1898                                 if( (x2 >= icon_x && x1 < icon_x + icon_w &&
1899                                     y2 >= icon_y && y1 < icon_y + icon_h) ||
1900                                     (x2 >= text_x && x1 < text_x + text_w &&
1901                                      y2 >= text_y && y1 < text_y + text_h) ) {
1902                                         if( !item->selected ) {
1903                                                 item->selected = 1;
1904                                                 result = 1;
1905                                         }
1906                                 }
1907                                 else {
1908                                         if( item->selected ) {
1909                                                 item->selected = 0;
1910                                                 result = 1;
1911                                         }
1912                                 }
1913                                 break; }
1914                         case LISTBOX_TEXT:
1915                         case LISTBOX_ICON_LIST: {
1916                                 if( x2 >= 0 &&
1917                                     x1 < (yscrollbar ?
1918                                         gui->get_w() - get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w() :
1919                                         gui->get_w()) && y2 > 0 &&
1920                                     y1 < gui->get_h() && y2 >= get_item_y(item) &&
1921                                     y1 < get_item_y(item) + get_item_h(item) ) {
1922                                         if( !item->selected ) {
1923                                                 item->selected = 1;
1924                                                 result = 1;
1925                                         }
1926                                 }
1927                                 else {
1928                                         if( item->selected ) {
1929                                                 item->selected = 0;
1930                                                 result = 1;
1931                                         }
1932                                 }
1933                                 break; }
1934                         }
1935                 }
1936
1937                 BC_ListBoxItem *item = data[master_column].values[i];
1938                 if( item->sublist_active() ) {
1939                         result |= select_rectangle(item->get_sublist(),
1940                                 x1, y1, x2, y2);
1941                 }
1942         }
1943         return result;
1944 }
1945
1946 int BC_ListBox::reposition_item(ArrayList<BC_ListBoxItem*> *data,
1947                 int selection_number, int x, int y, int *counter)
1948 {
1949         int temp = -1;
1950         if( !counter ) counter = &temp;
1951
1952         for( int i=0; i<data[master_column].total; ++i ) {
1953                 BC_ListBoxItem *item = data[master_column].values[i];
1954                 (*counter)++;
1955                 if( (*counter) == selection_number ) {
1956                         item->icon_x = x;
1957                         item->icon_y = y;
1958                         return 1;
1959                 }
1960 // Not recursive because it's only used for icons
1961         }
1962         return 0;
1963 }
1964
1965 void BC_ListBox::move_selection(ArrayList<BC_ListBoxItem*> *dst,
1966         ArrayList<BC_ListBoxItem*> *src)
1967 {
1968         for( int i=0; i<src[master_column].total;  ) {
1969                 BC_ListBoxItem *item = src[master_column].values[i];
1970
1971 // Move item to dst
1972                 if( item->selected ) {
1973                         for( int j=0; j<columns; ++j ) {
1974                                 dst[j].append(src[j].values[i]);
1975                                 src[j].remove_number(i);
1976                         }
1977                         continue;
1978                 }
1979 // Descend into sublist
1980                 if( item->sublist_active() ) {
1981                         move_selection(dst, item->get_sublist());
1982                 }
1983                 ++i;
1984         }
1985 }
1986
1987 int BC_ListBox::put_selection(ArrayList<BC_ListBoxItem*> *data,
1988                 ArrayList<BC_ListBoxItem*> *src, int destination, int *counter)
1989 {
1990         int temp = -1;
1991         if( !counter ) counter = &temp;
1992
1993         if( destination < 0 || destination >= data[master_column].total ) {
1994                 for( int j=0; j<columns; ++j ) {
1995                         for( int i=0; i<src[j].total; ++i ) {
1996                                 data[j].append(src[j].values[i]);
1997                         }
1998                 }
1999                 return 1;
2000         }
2001         else
2002         for( int i=0; i<data[master_column].total; ++i ) {
2003                 (*counter)++;
2004                 if( (*counter) == destination ) {
2005                         for( int j=0; j<columns; ++j ) {
2006                                 for( int k=0; k<src[j].total; ++k ) {
2007                                         data[j].insert(src[j].values[k], destination + k);
2008                                 }
2009                         }
2010                         return 1;
2011                 }
2012
2013                 BC_ListBoxItem *item = data[master_column].values[i];
2014                 if( item->sublist_active() ) {
2015                         if( put_selection(item->get_sublist(), src, destination, counter) )
2016                                 return 1;
2017                 }
2018         }
2019         return 0;
2020 }
2021
2022
2023
2024 int BC_ListBox::item_to_index(ArrayList<BC_ListBoxItem*> *data,
2025                 BC_ListBoxItem *item, int *counter)
2026 {
2027         int temp = -1;
2028         if( !counter ) counter = &temp;
2029
2030         for( int i=0; i<data[master_column].total; ++i ) {
2031                 (*counter)++;
2032                 for( int j=0; j<columns; ++j ) {
2033                         BC_ListBoxItem *new_item = data[j].values[i];
2034 //printf("BC_ListBox::item_to_index 1 %d %d %p\n", j, i, new_item);
2035                         if( new_item == item ) {
2036                                 return (*counter);
2037                         }
2038                 }
2039
2040                 BC_ListBoxItem *new_item = data[master_column].values[i];
2041                 if( new_item->sublist_active() ) {
2042                         if( item_to_index(new_item->get_sublist(), item, counter) >= 0 )
2043                                 return (*counter);
2044                 }
2045         }
2046
2047         return -1;
2048 }
2049
2050 BC_ListBoxItem* BC_ListBox::index_to_item(ArrayList<BC_ListBoxItem*> *data,
2051                 int number, int column, int *counter)
2052 {
2053         int temp = -1;
2054         if( !counter ) counter = &temp;
2055         for( int i=0; i<data[master_column].total; ++i ) {
2056                 (*counter)++;
2057                 if( (*counter) == number ) {
2058                         return data[column].values[i];
2059                 }
2060                 BC_ListBoxItem *item = data[master_column].values[i];
2061                 if( item->sublist_active() ) {
2062                         BC_ListBoxItem *result = index_to_item(item->get_sublist(),
2063                                 number, column, counter);
2064                         if( result ) return result;
2065                 }
2066         }
2067         return 0;
2068 }
2069
2070 int BC_ListBox::get_cursor_item(ArrayList<BC_ListBoxItem*> *data, int cursor_x, int cursor_y,
2071                 BC_ListBoxItem **item_return, int *counter, int expanded)
2072 {
2073         int temp = -1;
2074         if( !data ) return -1;
2075         if( !counter ) counter = &temp;
2076
2077 // Icons are not treed
2078         switch( display_format ) {
2079         case LISTBOX_ICONS:
2080         case LISTBOX_ICONS_PACKED:
2081         case LISTBOX_ICON_LIST: {
2082                 for( int j=data[master_column].total; --j>=0; ) {
2083                         int icon_x, icon_y, icon_w, icon_h;
2084                         int text_x, text_y, text_w, text_h;
2085                         BC_ListBoxItem *item = data[master_column].values[j];
2086                         get_icon_mask(item, icon_x, icon_y, icon_w, icon_h);
2087                         get_text_mask(item, text_x, text_y, text_w, text_h);
2088
2089                         if( (cursor_x >= icon_x && cursor_x < icon_x + icon_w &&
2090                              cursor_y >= icon_y && cursor_y < icon_y + icon_h) ||
2091                             (cursor_x >= text_x && cursor_x < text_x + text_w &&
2092                              cursor_y >= text_y && cursor_y < text_y + text_h) ) {
2093                                 if( item_return ) (*item_return) = item;
2094                                 return j;
2095                         }
2096                 }
2097                 return -1; }
2098         case LISTBOX_TEXT:
2099                 if( !gui ) break;
2100 // Text is treed
2101 // Cursor is inside items rectangle
2102                 if( cursor_x >= 0 &&
2103                     cursor_x < (yscrollbar ?
2104                                 gui->get_w() - get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w() :
2105                                 gui->get_w()) &&
2106 // Only clamp y if we're not in a SELECT operation.
2107                     (current_operation == BC_ListBox::SELECT ||
2108                         (cursor_y > get_title_h() + LISTBOX_BORDER &&
2109                          cursor_y < gui->get_h())) ) {
2110 // Search table for cursor obstruction
2111                         for( int i=0; i<data[master_column].total; ++i ) {
2112                                 BC_ListBoxItem *item = data[master_column].values[i];
2113                                 (*counter)++;
2114
2115 // Cursor is inside item on current level
2116                                 if( expanded && item->selectable &&
2117                                     cursor_y >= get_item_y(item) &&
2118                                     cursor_y < get_item_y(item) + get_item_h(item) ) {
2119 //printf("BC_ListBox::get_cursor_item %d %d %p\n", master_column, i, item);
2120                                         if( item_return ) (*item_return) = item;
2121                                         if( expanded < 0 ) (*counter) = i;
2122                                         return (*counter);
2123                                 }
2124
2125 // Descend into sublist
2126                                 if( item->sublist_active() ) {
2127                                         if( get_cursor_item(item->get_sublist(),
2128                                               cursor_x, cursor_y, item_return, counter,
2129                                               item->get_expand()) >= 0 ) {
2130                                                 if( expanded < 0 ) (*counter) = i;
2131                                                 return (*counter);
2132                                         }
2133                                 }
2134                         }
2135                 }
2136                 break;
2137         }
2138
2139         return -1;
2140 }
2141
2142 // short version
2143 int BC_ListBox::get_cursor_item_no()
2144 {
2145         int rx, ry;
2146         gui->get_relative_cursor(rx, ry);
2147         return get_cursor_item(data, rx, ry, 0, 0);
2148 }
2149
2150 int BC_ListBox::get_cursor_data_item_no(BC_ListBoxItem **item_return)
2151 {
2152         int rx, ry;
2153         gui->get_relative_cursor(rx, ry);
2154         return get_cursor_item(data, rx, ry, item_return, 0, -1);
2155 }
2156
2157 int BC_ListBox::repeat_event(int64_t duration)
2158 {
2159         switch( current_operation ) {
2160 // Repeat out of bounds selection
2161         case SELECT_RECT:
2162                 if( duration != get_resources()->scroll_repeat ) break;
2163                 return rectangle_scroll_event();
2164
2165         case DRAG_ITEM:
2166                 if( duration != get_resources()->scroll_repeat ) break;
2167                 if( !drag_scroll_event() ) break;
2168                 clamp_positions();
2169                 draw_items(0);
2170                 update_scrollbars(1);
2171                 return 1;
2172
2173         case SELECT:
2174                 if( duration != get_resources()->scroll_repeat ) break;
2175                 return select_scroll_event();
2176
2177         case NO_OPERATION:
2178 // Show tooltip
2179                 if( duration != get_resources()->tooltip_delay ) break;
2180                 if( !button_highlighted || !is_popup ) break;
2181                 if( !tooltip_text || !tooltip_text[0] ) break;
2182                 show_tooltip();
2183                 return 1;
2184         }
2185         return 0;
2186 }
2187
2188
2189 int BC_ListBox::cursor_enter_event()
2190 {
2191         int result = 0;
2192
2193         switch( current_operation ) {
2194 // Cursor moved over button, pressed, and exited.
2195         case BUTTON_DOWN_SELECT:
2196                 if( top_level->event_win == win ) {
2197                         current_operation = BUTTON_DN;
2198                         result = 1;
2199                         button_highlighted = 1;
2200                         draw_button(1);
2201                 }
2202                 break;
2203
2204         case NO_OPERATION:
2205 // Cursor entered button
2206                 if( is_popup && top_level->event_win == win ) {
2207                         button_highlighted = 1;
2208                         result = 1;
2209                         draw_button(1);
2210                 }
2211                 else
2212 // TODO: Need to get the highlighted column title or item
2213                 if( gui && top_level->event_win == gui->win ) {
2214                         list_highlighted = 1;
2215                         draw_border(1);
2216                         result = 1;
2217                 }
2218                 break;
2219         }
2220
2221         return result;
2222 }
2223
2224 int BC_ListBox::cursor_leave_event()
2225 {
2226         if( current_operation == COLUMN_DRAG ) return 0;
2227
2228 // Left button area
2229         if( button_highlighted ) {
2230                 button_highlighted = 0;
2231                 hide_tooltip();
2232                 draw_button(1);
2233         }
2234
2235         if( list_highlighted ) {
2236                 list_highlighted = 0;
2237                 highlighted_item = -1;
2238                 highlighted_ptr = 0;
2239                 highlighted_title = -1;
2240                 int redraw_toggles = 0;
2241                 for( int i=0; i<expanders.total; ++i )
2242                         expanders.values[i]->cursor_leave_event(&redraw_toggles);
2243
2244                 draw_items(1);
2245         }
2246
2247         return 0;
2248 }
2249
2250 int BC_ListBox::get_first_selection(ArrayList<BC_ListBoxItem*> *data, int *result)
2251 {
2252         int temp = -1;
2253         if( !result ) result = &temp;
2254
2255         for( int i=0; i<data[master_column].total; ++i ) {
2256                 BC_ListBoxItem *item = data[master_column].values[i];
2257                 (*result)++;
2258                 if( item->selected ) return (*result);
2259                 if( item->sublist_active() ) {
2260                         if( get_first_selection(item->get_sublist(), result) >= 0 )
2261                                 return (*result);
2262                 }
2263         }
2264         return -1;
2265 }
2266
2267 int BC_ListBox::get_total_items(ArrayList<BC_ListBoxItem*> *data,
2268         int *result,
2269         int master_column)
2270 {
2271         int temp = 0;
2272         if( !result ) result = &temp;
2273
2274         for( int i=0; i<data[master_column].total; ++i ) {
2275                 (*result)++;
2276                 if( data[master_column].values[i]->sublist_active() )
2277                         get_total_items(data[master_column].values[i]->get_sublist(),
2278                                 result,
2279                                 master_column);
2280         }
2281
2282         return (*result);
2283 }
2284
2285
2286 int BC_ListBox::get_last_selection(ArrayList<BC_ListBoxItem*> *data,
2287         int *result)
2288 {
2289         int temp = -1;
2290         int top_level = 0;
2291         if( !result ) {
2292                 result = &temp;
2293                 top_level = 1;
2294         }
2295
2296         for( int i=data[master_column].total-1; i>=0; --i ) {
2297                 BC_ListBoxItem *item = data[master_column].values[i];
2298                 (*result)++;
2299                 if( item->selected ) {
2300                         if( top_level )
2301                                 return get_total_items(data, 0, master_column) - (*result) /* - 1 */;
2302                         else
2303                                 return (*result);
2304                 }
2305
2306                 if( item->sublist_active() ) {
2307                         if( get_last_selection(item->get_sublist(), result) >= 0 ) {
2308                                 if( top_level )
2309                                         return get_total_items(data, 0, master_column) - (*result) /* - 1 */;
2310                                 else
2311                                         return (*result);
2312                         }
2313                 }
2314         }
2315         return -1;
2316 }
2317
2318 void BC_ListBox::select_range(ArrayList<BC_ListBoxItem*> *data,
2319                 int start, int end, int *current)
2320 {
2321         int temp = -1;
2322         if( !current ) current = &temp;
2323
2324         for( int i=0; i<data[master_column].total; ++i ) {
2325                 (*current)++;
2326                 if( (*current) >= start && (*current) < end ) {
2327                         for( int j=0; j<columns; ++j )
2328                                 data[j].values[i]->selected = 1;
2329                 }
2330                 BC_ListBoxItem *item = data[master_column].values[i];
2331                 if( item->sublist_active() )
2332                         select_range(item->get_sublist(), start, end, current);
2333         }
2334 }
2335
2336
2337 // Fill items between current selection and new selection
2338 int BC_ListBox::expand_selection(int button_press, int selection_number)
2339 {
2340         int old_selection_start = selection_start;
2341         int old_selection_end = selection_end;
2342
2343 // printf("BC_ListBox::expand_selection %d %d\n",
2344 // selection_center,
2345 // selection_number);
2346
2347 // Calculate the range to select based on selection_center and selection_number
2348         if( selection_number < selection_center ) {
2349                 selection_start = selection_number;
2350         }
2351         else {
2352                 selection_end = selection_number + 1;
2353         }
2354
2355 //printf("BC_ListBox::expand_selection %d %d %d %d\n", old_selection_start, old_selection_end, selection_start, selection_end);
2356 // Recurse through all the items and select the desired range
2357         select_range(data, selection_start, selection_end);
2358
2359 // Trigger redraw
2360         return (old_selection_start != selection_start ||
2361                 old_selection_end != selection_end);
2362 }
2363
2364 int BC_ListBox::toggle_item_selection(ArrayList<BC_ListBoxItem*> *data,
2365                 int selection_number, int *counter)
2366 {
2367         int temp = -1;
2368         if( !counter ) counter = &temp;
2369
2370         for( int i=0; i<data[master_column].total; ++i ) {
2371                 BC_ListBoxItem *item = data[master_column].values[i];
2372                 (*counter)++;
2373                 if( (*counter) == selection_number ) {
2374 // Get new value for selection
2375                         int selected = !item->selected;
2376 // Set row
2377                         for( int j=0; j<columns; ++j )
2378                                 data[j].values[i]->selected = selected;
2379                         return 1;
2380                 }
2381
2382 // Descend into sublist
2383                 if( item->sublist_active() ) {
2384                         if( toggle_item_selection(item->get_sublist(),
2385                               selection_number, counter) )
2386                                 return 1;
2387                 }
2388         }
2389
2390         return 0;
2391 }
2392
2393
2394 void BC_ListBox::set_all_selected(ArrayList<BC_ListBoxItem*> *data, int value)
2395 {
2396         for( int i=0; i<data[master_column].total; ++i ) {
2397                 for( int j=0; j<columns; ++j ) {
2398                         BC_ListBoxItem *item = data[j].values[i];
2399                         item->selected = value;
2400                 }
2401                 BC_ListBoxItem *item = data[master_column].values[i];
2402                 if( item->get_sublist() ) {
2403                         set_all_selected(item->get_sublist(), value);
2404                 }
2405         }
2406 }
2407
2408 void BC_ListBox::set_selected(ArrayList<BC_ListBoxItem*> *data,
2409                 int item_number, int value, int *counter)
2410 {
2411         int temp = -1;
2412         if( !counter ) counter = &temp;
2413         for( int i=0; i<data[master_column].total&&(*counter)!=item_number; ++i ) {
2414                 (*counter)++;
2415                 if( (*counter) == item_number ) {
2416                         for( int j=0; j<columns; ++j ) {
2417                                 BC_ListBoxItem *item = data[j].values[i];
2418                                 item->selected = value;
2419                         }
2420                         return;
2421                 }
2422
2423                 BC_ListBoxItem *item = data[master_column].values[i];
2424                 if( item->sublist_active() ) {
2425                         set_selected(item->get_sublist(), item_number, value, counter);
2426                 }
2427         }
2428 }
2429
2430 int BC_ListBox::update_selection(ArrayList<BC_ListBoxItem*> *data,
2431                 int selection_number, int *counter)
2432 {
2433         int temp = -1;
2434         int result = 0;
2435         if( !counter ) counter = &temp;
2436
2437         for( int i=0; i<data[master_column].total; ++i ) {
2438                 BC_ListBoxItem *item = data[master_column].values[i];
2439                 (*counter)++;
2440                 if( (*counter) == selection_number && !item->selected ) {
2441                         result = 1;
2442                         for( int j=0; j<columns; ++j )
2443                                 data[j].values[i]->selected = 1;
2444                 }
2445                 else
2446                 if( (*counter) != selection_number && item->selected ) {
2447                         result = 1;
2448                         for( int j=0; j<columns; ++j )
2449                                 data[j].values[i]->selected = 0;
2450                 }
2451                 if( item->sublist_active() )
2452                         result |= update_selection(item->get_sublist(),
2453                                 selection_number,
2454                                 counter);
2455         }
2456         return result;
2457 }
2458
2459 void BC_ListBox::promote_selections(ArrayList<BC_ListBoxItem*> *data,
2460                 int old_value, int new_value)
2461 {
2462         for( int i=0; i<data[master_column].total; ++i ) {
2463                 for( int j=0; j<columns; ++j ) {
2464                         BC_ListBoxItem *item = data[j].values[i];
2465                         if( item->selected == old_value ) item->selected = new_value;
2466                 }
2467                 BC_ListBoxItem *item = data[master_column].values[i];
2468                 if( item->sublist_active() )
2469                         promote_selections(item->get_sublist(), old_value, new_value);
2470         }
2471 }
2472
2473 int BC_ListBox::focus_out_event()
2474 {
2475         deactivate();
2476         return 0;
2477 }
2478
2479 int BC_ListBox::button_press_event()
2480 {
2481         int result = 0;
2482         BC_ListBoxItem *current_item = 0;
2483         int new_cursor;
2484         int do_selection_change = 0;
2485         const int debug = 0;
2486
2487         hide_tooltip();
2488         if( debug ) printf("BC_ListBox::button_press_event %d this=%p event_win=%p %p %p %p\n",
2489                 __LINE__, this, (void*)top_level->event_win,
2490                 (void*)(gui ? gui->win : 0), (void*)win, (void*)parent_window->win);
2491
2492 // Pressed in button
2493         if( is_popup && top_level->event_win == win ) {
2494                 if( debug ) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2495                 current_operation = BUTTON_DN;
2496                 draw_button(1);
2497
2498 // Deploy listbox
2499                 if( !active && !disabled ) {
2500                         top_level->deactivate();
2501                         activate();
2502                 }
2503
2504                 result = 1;
2505         }
2506         else
2507 // Pressed in scrollbar
2508         if( (xscrollbar && top_level->event_win == xscrollbar->win) ||
2509             (yscrollbar && top_level->event_win == yscrollbar->win) ) {
2510                 if( debug ) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2511                 result = 0;
2512         }
2513         else
2514 // Pressed in items
2515         if( gui && top_level->event_win == gui->win ) {
2516                 if( debug ) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2517
2518 // Activate list items
2519 // If it is a suggestion popup, it is visible without being active
2520                 if( !active ) {
2521                         if( debug ) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2522                         if( !is_suggestions ) top_level->deactivate();
2523                         if( debug ) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2524                         activate();
2525                         if( debug ) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2526                 }
2527
2528                 if( current_operation != NO_OPERATION ) {
2529                         switch( current_operation ) {
2530                         case DRAG_ITEM:
2531                         case COLUMN_DRAG:
2532                                 return drag_stop_event();
2533                         }
2534                 }
2535
2536 // Wheel mouse pressed
2537                 if( get_buttonpress() == 4 ) {
2538                         if( current_operation == NO_OPERATION ) {
2539                                 current_operation = WHEEL;
2540                                 if( yscrollbar ) {
2541                                         set_yposition(yposition - gui->get_h() / 10, 0);
2542                                         clamp_positions();
2543                                         update_scrollbars(0);
2544                                         highlighted_ptr = 0;
2545                                         highlighted_item = get_cursor_item(data,
2546                                                 top_level->cursor_x, top_level->cursor_y,
2547                                                 &highlighted_ptr);
2548                                         draw_items(1);
2549                                         result = 1;
2550                                 }
2551                         }
2552                 }
2553                 else
2554                 if( get_buttonpress() == 5 ) {
2555                         if( current_operation == NO_OPERATION ) {
2556                                 current_operation = WHEEL;
2557                                 if( yscrollbar ) {
2558                                         set_yposition(yposition + gui->get_h() / 10, 0);
2559                                         clamp_positions();
2560                                         update_scrollbars(0);
2561                                         highlighted_ptr = 0;
2562                                         highlighted_item = get_cursor_item(data,
2563                                                 top_level->cursor_x, top_level->cursor_y,
2564                                                 &highlighted_ptr);
2565                                         draw_items(1);
2566                                         result = 1;
2567                                 }
2568                         }
2569                 }
2570                 else
2571 // Pressed over column title division
2572                 if( test_column_divisions(gui->get_cursor_x(),
2573                         gui->get_cursor_y(),
2574                         new_cursor) ) {
2575                         drag_cursor_x = gui->get_cursor_x() + xposition;
2576                         if( column_width )
2577                                 drag_column_w = column_width[highlighted_division - 1];
2578                         else
2579                                 drag_column_w = default_column_width[highlighted_division - 1];
2580
2581                         current_operation = DRAG_DIVISION;
2582                         reset_query();
2583                 }
2584                 else
2585 // Pressed in column title
2586                 if( test_column_titles(gui->get_cursor_x(), gui->get_cursor_y()) ) {
2587                         current_operation = COLUMN_DN;
2588                         button_highlighted = 0;
2589                         list_highlighted = 1;
2590                         draw_items(1);
2591                         result = 1;
2592                 }
2593                 else
2594 // Pressed in expander
2595                 if( test_expanders() ) {
2596                         current_operation = EXPAND_DN;
2597 // Need to redraw items because of alpha
2598                         draw_items(1);
2599                         result = 1;
2600                 }
2601                 else
2602 // Pressed over item
2603                 if( (selection_number = get_cursor_item(data,
2604                                         gui->get_cursor_x(), gui->get_cursor_y(),
2605                                         &current_item)) >= 0 ) {
2606                         if( debug ) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2607
2608 // Get item button was pressed over
2609                         selection_number2 = selection_number1;
2610                         selection_number1 = selection_number;
2611
2612                         selection_start = -1;
2613                         selection_end = -1;
2614
2615
2616 // Multiple item selection is possible
2617                         if( selection_mode == LISTBOX_MULTIPLE &&
2618                             (ctrl_down() || shift_down()) ) {
2619 // Expand text selection.
2620 // Fill items between selected region and current item.
2621                                 if( shift_down() &&
2622                                     (display_format == LISTBOX_TEXT ||
2623                                      display_format == LISTBOX_ICON_LIST) ) {
2624 // Get first item selected
2625                                         selection_start = get_first_selection(data);
2626 // Get last item selected
2627                                         selection_end = get_last_selection(data);
2628 // Get center of selected region
2629                                         if( selection_end > selection_start ) {
2630                                                 selection_center = (selection_end + selection_start) >> 1;
2631                                         }
2632                                         else {
2633                                                 selection_center = selection_number;
2634                                         }
2635 // Deselect everything.
2636                                         set_all_selected(data, 0);
2637 // Select just the items
2638                                         expand_selection(1, selection_number);
2639                                         new_value = 1;
2640                                 }
2641 // Toggle a single item on or off
2642                                 else {
2643                                         toggle_item_selection(data, selection_number);
2644                                         new_value = current_item->selected;
2645                                 }
2646                         }
2647 // Select single item
2648                         else {
2649                                 if( !current_item->selected || (get_buttonpress() == 1 && !new_value) ) {
2650                                         set_all_selected(data, 0);
2651                                         set_selected(data, selection_number, 1);
2652                                 }
2653                                 new_value = 1;
2654                         }
2655
2656
2657                         current_operation = SELECT;
2658                         highlighted_item = selection_number;
2659                         highlighted_ptr = current_item;
2660                         button_highlighted = 0;
2661                         list_highlighted = 1;
2662                         reset_query();
2663                         draw_items(1);
2664                         do_selection_change = 1;
2665                         result = 1;
2666                 }
2667                 else
2668 // Pressed over nothing.  Start rectangle selection.
2669                 if( data ) {
2670                         if( get_buttonpress() == 1 &&
2671                             selection_mode == LISTBOX_MULTIPLE ) {
2672                                 if( !shift_down() ) {
2673 // Deselect all and redraw if anything was selected
2674                                         if( get_selection_number(0, 0) >= 0 ) {
2675                                                 set_all_selected(data, 0);
2676                                                 draw_items(1);
2677                                                 do_selection_change = 1;
2678                                                 result = 1;
2679                                         }
2680                                 }
2681                                 else {
2682 // Promote selections to protect from a rectangle selection
2683                                         promote_selections(data, 1, 2);
2684                                 }
2685
2686 // Start rectangle selection
2687                                 current_operation = SELECT_RECT;
2688                                 rect_x1 = rect_x2 = get_cursor_x();
2689                                 rect_y1 = rect_y2 = get_cursor_y();
2690                         }
2691                 }
2692
2693
2694                 reset_query();
2695         }
2696         else
2697 // Suggestion box is not active but visible, so lie to get it to deactivate
2698         if( is_popup && (active || (is_suggestions && gui)) ) {
2699                 active = 1;
2700                 if( debug ) printf("BC_ListBox::button_press_event %d\n", __LINE__);
2701                 deactivate();
2702                 result = 1;
2703         }
2704
2705
2706         if( do_selection_change ) selection_changed();
2707         if( debug ) printf("BC_ListBox::button_press_event %d %d\n", __LINE__, result);
2708
2709         return result;
2710 }
2711
2712 int BC_ListBox::button_release_event()
2713 {
2714         int result = 0;
2715         int do_event = 0;
2716         new_value = 0;
2717         unset_scroll_repeat();
2718
2719 //printf("BC_ListBox::button_release_event 1 %d\n", current_operation);
2720         switch( current_operation ) {
2721         case DRAG_DIVISION:
2722                 current_operation = NO_OPERATION;
2723                 result = 1;
2724                 break;
2725
2726         case WHEEL:
2727                 current_operation = NO_OPERATION;
2728                 result = 1;
2729                 break;
2730
2731 // Release item selection
2732         case BUTTON_DOWN_SELECT:
2733         case SELECT:
2734 //printf("BC_ListBox::button_release_event 10\n");
2735                 current_operation = NO_OPERATION;
2736                 if( gui ) {
2737                         selection_number1 = selection_number = get_cursor_item_no();
2738 //printf("BC_ListBox::button_release_event %d %d\n", selection_number2, selection_number1);
2739                 }
2740
2741                 if( is_popup ) {
2742                         button_releases++;
2743                         if( selection_number >= 0 ) {
2744                                 deactivate();
2745                                 do_event = 1;
2746                         }
2747                         else
2748 // Second button release outside button
2749                         if( button_releases > 1 ) {
2750                                 deactivate();
2751                         }
2752                 }
2753                 else {
2754                         if( top_level->get_double_click() &&
2755                             selection_number2 == selection_number1 &&
2756                             selection_number2 >= 0 && selection_number1 >= 0 ) {
2757                                 do_event = 1;
2758                         }
2759                         result = 1;
2760                 }
2761                 break;
2762
2763
2764         case SELECT_RECT:
2765                 if( data ) {
2766 // Demote selections from rectangle selection
2767                         promote_selections(data, 2, 1);
2768                 }
2769
2770 // Hide rectangle overlay
2771                 draw_rectangle(1);
2772                 current_operation = NO_OPERATION;
2773                 result = 1;
2774                 break;
2775
2776 // Release popup button
2777         case BUTTON_DN:
2778                 hide_tooltip();
2779                 current_operation = NO_OPERATION;
2780                 button_releases++;
2781                 draw_button(1);
2782
2783 // Second button release inside button
2784                 if( button_releases > 1 ) {
2785                         deactivate();
2786                 }
2787                 result = 1;
2788                 break;
2789
2790         case COLUMN_DN:
2791                 current_operation = NO_OPERATION;
2792 // Update the sort column and the sort order for the user only if the existing
2793 // sort column is valid.
2794                 if( sort_column >= 0 ) {
2795 // Invert order only if column is the same
2796                         if( highlighted_title == sort_column )
2797                                 sort_order = sort_order == SORT_ASCENDING ?
2798                                         SORT_DESCENDING : SORT_ASCENDING;
2799 // Set the new sort column
2800                         sort_column = highlighted_title;
2801                         if( !sort_order_event() ) {
2802                                 draw_titles(1);
2803                         }
2804                 }
2805                 else
2806 // Sorting not enabled.  Redraw the title state.
2807                 {
2808                         draw_titles(1);
2809                 }
2810                 result = 1;
2811                 break;
2812
2813         case EXPAND_DN: {
2814                 int redraw_toggles = 0;
2815                 for( int i=0; i<expanders.total&&!result; ++i ) {
2816                         if( expanders.values[i]->button_release_event(&redraw_toggles) ) {
2817                                 result = 1;
2818                         }
2819                 }
2820 // Need to redraw items because of alpha
2821                 if( redraw_toggles ) draw_items(1);
2822                 current_operation = NO_OPERATION;
2823                 break; }
2824
2825         default:
2826 // Can't default to NO_OPERATION because it may be used in a drag event.
2827                 break;
2828         }
2829
2830
2831         if( do_event ) handle_event();
2832
2833 //printf("BC_ListBox::button_release_event %d %d\n", __LINE__, get_window_lock());
2834         return result;
2835 }
2836
2837 int BC_ListBox::get_title_h()
2838 {
2839         if( display_format == LISTBOX_TEXT ||
2840             display_format == LISTBOX_ICON_LIST )
2841                 return column_titles ? column_bg[0]->get_h() : 0;
2842         return 0;
2843 }
2844
2845 void BC_ListBox::reset_cursor(int new_cursor)
2846 {
2847         if( is_popup ) {
2848                 if( gui->get_cursor() != new_cursor ) {
2849                         gui->set_cursor(new_cursor, 0, 0);
2850                 }
2851         }
2852         else
2853         if( get_cursor() != new_cursor ) {
2854                 set_cursor(new_cursor, 0, 0);
2855         }
2856 }
2857
2858 int BC_ListBox::test_column_divisions(int cursor_x, int cursor_y, int &new_cursor)
2859 {
2860         if( gui && column_titles &&
2861             cursor_y >= 0 && cursor_y < get_title_h() &&
2862             cursor_x >= 0 && cursor_x < gui->get_w() ) {
2863                 for( int i=1; i<columns; ++i ) {
2864                         if( cursor_x >= -xposition + get_column_offset(i) - 5 &&
2865                             cursor_x <  -xposition + get_column_offset(i) +
2866                                         get_resources()->listbox_title_hotspot ) {
2867                                 highlighted_item = -1;
2868                                 highlighted_ptr = 0;
2869                                 highlighted_division = i;
2870                                 highlighted_title = -1;
2871                                 list_highlighted = 1;
2872                                 new_cursor = HSEPARATE_CURSOR;
2873                                 return 1;
2874                         }
2875                 }
2876         }
2877         highlighted_division = -1;
2878         return 0;
2879 }
2880
2881 int BC_ListBox::test_column_titles(int cursor_x, int cursor_y)
2882 {
2883         if( gui && column_titles &&
2884             cursor_y >= 0 && cursor_y < get_title_h() &&
2885             cursor_x >= 0 && cursor_x < gui->get_w() ) {
2886                 for( int i=0; i<columns; ++i ) {
2887                         if( cursor_x >= -xposition + get_column_offset(i) &&
2888                                 (cursor_x < -xposition + get_column_offset(i + 1) ||
2889                                         i == columns - 1) ) {
2890                                 highlighted_item = -1;
2891                                 highlighted_ptr = 0;
2892                                 highlighted_division = -1;
2893                                 highlighted_title = i;
2894                                 list_highlighted = 1;
2895                                 return 1;
2896                         }
2897                 }
2898         }
2899         highlighted_title = -1;
2900         return 0;
2901 }
2902
2903 int BC_ListBox::test_expanders()
2904 {
2905         for( int i=0; i<expanders.total; ++i ) {
2906                 if( expanders.values[i]->button_press_event() ) {
2907                         current_operation = EXPAND_DN;
2908                         draw_toggles(1);
2909                         return 1;
2910                 }
2911         }
2912         return 0 ;
2913 }
2914
2915 int BC_ListBox::cursor_motion_event()
2916 {
2917         int redraw = 0, result = 0;
2918         int new_cursor = ARROW_CURSOR;
2919
2920         selection_number = -1;
2921
2922
2923         switch( current_operation ) {
2924         case BUTTON_DN:
2925 // Button pressed and slid off button
2926                 if( !cursor_inside() ) {
2927                         current_operation = BUTTON_DOWN_SELECT;
2928                         draw_button(1);
2929                         result = 1;
2930                 }
2931                 break;
2932
2933         case DRAG_DIVISION: {
2934 //              int new_w = get_cursor_x() +
2935 //                      xposition -
2936 //                      get_column_offset(highlighted_division - 1);
2937                 int difference = get_cursor_x() + xposition - drag_cursor_x;
2938                 int new_w = drag_column_w + difference;
2939
2940                 new_cursor = HSEPARATE_CURSOR;
2941
2942                 if( column_width ) {
2943                         column_width[highlighted_division - 1] = new_w;
2944                 }
2945                 else {
2946                         default_column_width[highlighted_division - 1] = new_w;
2947                 }
2948
2949                 column_width_boundaries();
2950
2951 // Force update of coords
2952                 set_autoplacement(data, 0, 1);
2953                 column_resize_event();
2954
2955                 clamp_positions();
2956                 draw_items(0);
2957                 update_scrollbars(1);
2958                 result = 1;
2959                 break; }
2960
2961         case SELECT_RECT: {
2962                 if( test_drag_scroll(get_cursor_x(), get_cursor_y()) )
2963                         set_scroll_repeat();
2964
2965                 int old_x1 = MIN(rect_x1, rect_x2);
2966                 int old_x2 = MAX(rect_x1, rect_x2);
2967                 int old_y1 = MIN(rect_y1, rect_y2);
2968                 int old_y2 = MAX(rect_y1, rect_y2);
2969
2970                 int new_rect_x2 = get_cursor_x();
2971                 int new_rect_y2 = get_cursor_y();
2972
2973                 int x1 = MIN(rect_x1, new_rect_x2);
2974                 int x2 = MAX(rect_x1, new_rect_x2);
2975                 int y1 = MIN(rect_y1, new_rect_y2);
2976                 int y2 = MAX(rect_y1, new_rect_y2);
2977
2978 // Adjust rectangle coverage
2979                 if( old_x1 != x1 || old_x2 != x2 ||
2980                     old_y1 != y1 || old_y2 != y2 ) {
2981                         if( data ) {
2982                                 redraw = select_rectangle(data, x1, y1, x2, y2);
2983                         }
2984
2985 // hide rectangle
2986                         if( !redraw ) {
2987 //printf("BC_ListBox::cursor_motion_event %d\n", __LINE__);
2988                                 draw_rectangle(0);
2989                         }
2990                 }
2991
2992                 rect_x2 = get_cursor_x();
2993                 rect_y2 = get_cursor_y();
2994                 if( redraw ) {
2995                         clamp_positions();
2996                         draw_items(0);
2997                         update_scrollbars(1);
2998                         selection_changed();
2999 //printf("BC_ListBox::cursor_motion_event %d\n", __LINE__);
3000                 }
3001                 else
3002                 if( old_x1 != x1 || old_x2 != x2 ||
3003                     old_y1 != y1 || old_y2 != y2 ) {
3004 //printf("BC_ListBox::cursor_motion_event %d\n", __LINE__);
3005                         draw_rectangle(1);
3006                 }
3007
3008                 result = 1;
3009                 break; }
3010
3011         case SELECT: {
3012                 int old_highlighted_item = highlighted_item;
3013
3014                 if( test_drag_scroll(get_cursor_x(), get_cursor_y()) )
3015                         set_scroll_repeat();
3016
3017                 highlighted_item = selection_number = get_cursor_item(data,
3018                         get_cursor_x(), get_cursor_y(), &highlighted_ptr);
3019                 result = 1;
3020
3021 // Deselect all items and select just the one we're over
3022                 if( selection_number >= 0 && !allow_drag &&
3023                     ((!shift_down() && !ctrl_down()) ||
3024                      selection_mode == LISTBOX_SINGLE) ) {
3025                         redraw = update_selection(data, selection_number);
3026                 }
3027                 else
3028 // Expand multiple selection
3029                 if( selection_mode == LISTBOX_MULTIPLE &&
3030                     (shift_down() || ctrl_down()) ) {
3031 // Expand selected region in text mode centered around initial range
3032                         if( (display_format == LISTBOX_TEXT ||
3033                              display_format == LISTBOX_ICON_LIST) &&
3034                                 shift_down() ) {
3035 // Select just the items
3036                                 redraw = expand_selection(0, selection_number);
3037                         }
3038 // Set the one item we're over to the selection value determined in
3039 // button_press_event.
3040                         else {
3041                                 set_selected(data, selection_number, new_value);
3042                         }
3043                 }
3044
3045                 if( highlighted_item != old_highlighted_item ) {
3046                         clamp_positions();
3047                         draw_items(0);
3048                         update_scrollbars(1);
3049 //printf("BC_ListBox::cursor_motion_event %d %d\n", highlighted_item, old_highlighted_item);
3050                         selection_changed();
3051                 }
3052                 break; }
3053
3054         case BUTTON_DOWN_SELECT:
3055 // Went back into button area
3056                 if( cursor_inside() ) {
3057                         current_operation = BUTTON_DN;
3058                         draw_button(1);
3059                         result = 1;
3060                 }
3061                 else
3062 // Went into item area
3063                 if( gui ) {
3064                         int cursor_x = 0, cursor_y = 0;
3065                         translate_coordinates(top_level->event_win, gui->win,
3066                                 top_level->cursor_x, top_level->cursor_y,
3067                                 &cursor_x, &cursor_y);
3068                         int old_highlighted_item = highlighted_item;
3069                         highlighted_item = selection_number = get_cursor_item(data,
3070                                         cursor_x,
3071                                         cursor_y,
3072                                         &highlighted_ptr);
3073
3074                         if( highlighted_item != old_highlighted_item ) {
3075                                 update_selection(data, selection_number);
3076                                 draw_items(1);
3077                                 selection_changed();
3078                         }
3079                 }
3080                 break;
3081
3082         case EXPAND_DN: {
3083                 int redraw_toggles = 0;
3084                 for( int i=0; i<expanders.total&&!result; ++i ) {
3085                         result = expanders.values[i]->cursor_motion_event(
3086                                 &redraw_toggles);
3087                 }
3088                 if( redraw_toggles ) {
3089 // Need to redraw items because of the alpha
3090                         draw_items(1);
3091                 }
3092                 break; }
3093
3094         case NO_OPERATION: {
3095                 int cursor_x = get_cursor_x(), cursor_y = get_cursor_y();
3096                 if( gui && top_level->event_win == gui->win ) {
3097                         int old_highlighted_title = highlighted_title;
3098                         int old_list_highlighted = list_highlighted;
3099                         int old_highlighted_item = highlighted_item;
3100                         int redraw_titles = 0;
3101                         int redraw_border = 0;
3102                         int redraw_items = 0;
3103                         int redraw_toggles = 0;
3104                         result = 1;
3105
3106 // Test if cursor moved over a title division
3107                         test_column_divisions(cursor_x, cursor_y, new_cursor);
3108
3109 // Test if cursor moved over a title
3110                         if( highlighted_division < 0 ) {
3111                                 test_column_titles(cursor_x, cursor_y);
3112                         }
3113
3114 // Test if cursor moved over expander
3115                         if( highlighted_division < 0 && highlighted_title < 0 &&
3116                             (display_format == LISTBOX_TEXT ||
3117                              display_format == LISTBOX_ICON_LIST) ) {
3118                                 for( int i=0; i<expanders.total; ++i ) {
3119                                         expanders.values[i]->cursor_motion_event(
3120                                                 &redraw_toggles);
3121                                 }
3122 //printf("BC_ListBox::cursor_motion_event %d\n", redraw_toggles);
3123                         }
3124
3125 // Test if cursor moved over an item
3126                         if( highlighted_division < 0 && highlighted_title < 0 ) {
3127                                 highlighted_item = get_cursor_item(data,
3128                                         cursor_x,
3129                                         cursor_y,
3130                                         &highlighted_ptr);
3131                         }
3132
3133
3134 // Clear title highlighting if moved over division
3135                         if( old_highlighted_title != highlighted_title ) {
3136                                 redraw_titles = 1;
3137                         }
3138
3139 // Highlight list border
3140                         if( old_list_highlighted != list_highlighted ) {
3141                                 redraw_border = 1;
3142                         }
3143
3144 // Moved out of item area
3145                         if( old_highlighted_item != highlighted_item ) {
3146                                 if( !mouse_over_event(highlighted_item) )
3147                                         redraw_items = 1;
3148                         }
3149
3150 //printf("BC_ListBox::cursor_motion_event 1 %d\n", highlighted_item);
3151
3152 // Change cursor to title division adjustment
3153                         reset_cursor(new_cursor);
3154
3155                         if( redraw_items ) {
3156                                 draw_items(0);
3157                         }
3158                         else {
3159                                 if( redraw_titles )
3160                                         draw_titles(0);
3161                                 if( redraw_border )
3162                                         draw_border(0);
3163                                 if( redraw_toggles )
3164                                         draw_toggles(0);
3165                         }
3166
3167                         if( redraw_items || redraw_titles ||
3168                             redraw_border || redraw_toggles ) {
3169                                 gui->flash();
3170                                 gui->flush();
3171                         }
3172                 }
3173
3174
3175                 if( !result && list_highlighted ) {
3176                         list_highlighted = 0;
3177                         highlighted_item = -1;
3178                         highlighted_ptr = 0;
3179                         highlighted_title = -1;
3180                         highlighted_division = -1;
3181                         draw_items(1);
3182                         result = 0;
3183                 }
3184                 break; }
3185         }
3186
3187         return result;
3188 }
3189
3190 int BC_ListBox::drag_start_event()
3191 {
3192         switch( current_operation ) {
3193         case SELECT:
3194                 if( expander_active() ) break;
3195                 if( gui && gui->is_event_win() && allow_drag ) {
3196                         BC_ListBoxItem *item_return = 0;
3197                         selection_number = get_cursor_item(data,
3198                                 top_level->cursor_x,
3199                                 top_level->cursor_y,
3200                                 &item_return);
3201
3202                         if( selection_number >= 0 ) {
3203                                 int cx, cy;
3204                                 get_abs_cursor(cx, cy);
3205                                 if( item_return->icon_vframe ) {
3206                                         drag_popup = new BC_DragWindow(this,
3207                                                 item_return->icon_vframe, cx, cy);
3208                                 }
3209                                 else
3210                                 if( item_return->icon ) {
3211                                         drag_popup = new BC_DragWindow(this,
3212                                                 item_return->icon, cx, cy);
3213                                 }
3214                                 else {
3215                                         drag_popup = new BC_DragWindow(this,
3216                                                 drag_icon_vframe, cx, cy);
3217                                 }
3218                                 current_operation = DRAG_ITEM;
3219 // require shift down for scrolling
3220                                 if( allow_drag < 0 && shift_down() )
3221                                         set_scroll_repeat();
3222                                 return 1;
3223                         }
3224                 }
3225                 break;
3226
3227         case COLUMN_DN:
3228                 if( gui && gui->is_event_win() && allow_drag_column ) {
3229                         int cx, cy;
3230                         get_abs_cursor(cx, cy);
3231                         cx -= drag_column_icon_vframe->get_w() / 2,
3232                         cy -= drag_column_icon_vframe->get_h() / 2;
3233                         drag_popup = new BC_DragWindow(this,
3234                                 drag_column_icon_vframe, cx, cy);
3235                         dragged_title = highlighted_title;
3236                         current_operation = COLUMN_DRAG;
3237                         draw_titles(1);
3238                         return 1;
3239                 }
3240                 break;
3241         }
3242
3243         return 0;
3244 }
3245
3246 int BC_ListBox::drag_motion_event()
3247 {
3248 //printf("BC_ListBox::drag_motion_event 1 %d\n", current_operation);
3249         switch( current_operation ) {
3250         case DRAG_ITEM: {
3251                 int redraw = 0;
3252                 int new_highlighted_item = -1;
3253                 BC_ListBoxItem *new_highlighted_ptr = 0;
3254                 new_highlighted_item = get_cursor_item(data,
3255                         top_level->cursor_x, top_level->cursor_y,
3256                         &new_highlighted_ptr);
3257
3258                 if( new_highlighted_item != highlighted_item ) {
3259                         redraw = 1;
3260                 }
3261
3262 // Always update highlighted value for drag_stop
3263                 highlighted_item = new_highlighted_item;
3264                 highlighted_ptr = new_highlighted_ptr;
3265 //printf("BC_ListBox::drag_motion_event 1 %p\n", highlighted_ptr);
3266                 if( redraw ) {
3267                         clamp_positions();
3268                         draw_items(0);
3269                         update_scrollbars(1);
3270                 }
3271
3272                 return drag_popup->cursor_motion_event(); }
3273
3274         case COLUMN_DRAG: {
3275                 int old_highlighted_title = highlighted_title;
3276                 test_column_titles(get_cursor_x(), get_cursor_y());
3277                 if( old_highlighted_title != highlighted_title ) {
3278                         draw_titles(1);
3279                 }
3280                 return drag_popup->cursor_motion_event(); }
3281         }
3282
3283         return 0;
3284 }
3285
3286 int BC_ListBox::drag_stop_event()
3287 {
3288         int result = 0;
3289         unset_scroll_repeat();
3290         switch( current_operation ) {
3291         case DRAG_ITEM:
3292 // Inside window boundary
3293                 if( top_level->cursor_x > 0 &&
3294                     top_level->cursor_x < gui->get_w() - drag_popup->get_w() / 2 &&
3295                     top_level->cursor_y > 0 &&
3296                     top_level->cursor_y < gui->get_h() - drag_popup->get_h() / 2 ) {
3297 // Move icon
3298
3299
3300                         if( display_format == LISTBOX_ICONS ||
3301                             display_format == LISTBOX_ICONS_PACKED ) {
3302                                 reposition_item(data,
3303                                         selection_number,
3304                                         top_level->cursor_x - drag_popup->get_w() / 2 -
3305                                                 LISTBOX_MARGIN - 2 + xposition,
3306                                         top_level->cursor_y - drag_popup->get_h() / 2 -
3307                                                 LISTBOX_MARGIN - 2 + yposition);
3308                         }
3309                         else
3310 // Move rows
3311                         if( process_drag ) {
3312 // Move selected items from data to temporary
3313                                 ArrayList<BC_ListBoxItem*> *src_items =
3314                                         new ArrayList<BC_ListBoxItem*>[columns];
3315                                 move_selection(src_items, data);
3316 // Get destination
3317                                 int destination = highlighted_item = item_to_index(data,
3318                                         highlighted_ptr);
3319 // Insert items from temporary to data
3320                                 put_selection(data, src_items, destination);
3321
3322                                 delete [] src_items;
3323                                 set_autoplacement(data, 0, 1);
3324                         }
3325
3326                         draw_items(1);
3327                 }
3328                 else
3329                         drag_popup->drag_failure_event();
3330                 result = 1;
3331                 break;
3332
3333         case COLUMN_DRAG:
3334                 if( dragged_title != highlighted_title ) {
3335                         if( highlighted_title >= 0 ) {
3336                                 if( !move_column_event() ) draw_titles(1);
3337                         }
3338                         else
3339                                 drag_popup->drag_failure_event();
3340                 }
3341                 result = 1;
3342         }
3343
3344         if( result ) {
3345                 current_operation = NO_OPERATION;
3346                 delete drag_popup;
3347                 flush();
3348                 drag_popup = 0;
3349                 new_value = 0;
3350         }
3351
3352         return result;
3353 }
3354
3355 BC_DragWindow* BC_ListBox::get_drag_popup()
3356 {
3357         return drag_popup;
3358 }
3359
3360 int BC_ListBox::translation_event()
3361 {
3362         if( is_popup && gui ) {
3363                 int new_x = gui->get_x() +
3364                         (top_level->last_translate_x - top_level->prev_x -
3365                                 BC_DisplayInfo::get_left_border());
3366                 int new_y = gui->get_y() +
3367                         (top_level->last_translate_y - top_level->prev_y -
3368                                 BC_DisplayInfo::get_top_border());
3369
3370                 gui->reposition_window(new_x, new_y);
3371
3372         }
3373         return 0;
3374 }
3375
3376 int BC_ListBox::reposition_window(int x, int y, int w, int h, int flush)
3377 {
3378         if( w != -1 ) {
3379                 popup_w = w;
3380                 if( h != -1 ) popup_h = h;
3381 //printf("BC_ListBox::reposition_window %d %d\n", popup_w, popup_h);
3382
3383                 if( !is_popup ) {
3384                         if( xscrollbar )
3385                                 xscrollbar->reposition_window(get_xscroll_x(),
3386                                         get_xscroll_y(),
3387                                         get_xscroll_width());
3388                         if( yscrollbar )
3389                                 yscrollbar->reposition_window(get_yscroll_x(),
3390                                         get_yscroll_y(),
3391                                         get_yscroll_height());
3392                 }
3393         }
3394
3395         BC_WindowBase::reposition_window(x, y, w, h);
3396         draw_button(0);
3397         draw_items(flush);
3398         return 0;
3399 }
3400
3401 int BC_ListBox::deactivate()
3402 {
3403         hide_tooltip();
3404 // printf("BC_ListBox::deactivate %d this=%p gui=%p active=%d\n",
3405 // __LINE__, this, gui, active);
3406         if( active ) {
3407                 if( is_popup ) {
3408 //printf("BC_ListBox::deactivate %d this=%p gui=%p\n", __LINE__, this, gui);
3409                         if( gui ) {
3410                                 delete gui;  gui = 0;
3411                                 flush();
3412                         }
3413                         xscrollbar = 0;
3414                         yscrollbar = 0;
3415                         highlighted_item = -1;
3416                         highlighted_ptr = 0;
3417 //sleep(1);
3418                 }
3419 //printf("BC_ListBox::deactivate %d this=%p\n", __LINE__, this);
3420                 active = 0;
3421                 top_level->active_subwindow = 0;
3422         }
3423
3424         return 0;
3425 }
3426
3427 int BC_ListBox::activate(int take_focus)
3428 {
3429         if( active ) return 0;
3430         active = 1;
3431         if( take_focus )
3432                 set_active_subwindow(this);
3433         button_releases = 0;
3434         if( !is_popup || gui ) return 0;
3435         int wx = get_x(), wy = get_y() + get_h();
3436         if( justify == LISTBOX_RIGHT ) wx += get_w() - popup_w;
3437         Window xwin;
3438         int abs_x, abs_y;
3439         XTranslateCoordinates(top_level->display,
3440                 parent_window->win, top_level->rootwin,
3441                 wx, wy, &abs_x, &abs_y, &xwin);
3442         if( x <= 0 ) x = 2;
3443         if( y <= 0 ) y = 2;
3444         return activate(abs_x, abs_y);
3445 }
3446
3447 int BC_ListBox::activate(int x, int y, int w, int h)
3448 {
3449         if( !is_popup || gui ) return 0;
3450         active = 1;
3451         if( w != -1 ) popup_w = w;
3452         if( h != -1 ) popup_h = h;
3453         reset_query();
3454         if( y + popup_h > top_level->get_root_h(0) )
3455                 y -= get_h() + popup_h;
3456         add_subwindow(gui = new BC_Popup(this,
3457                 x, y, popup_w, popup_h, -1, 0, 0));
3458         draw_items(1);
3459         gui->show_window(1);
3460         return 0;
3461 }
3462
3463 int BC_ListBox::is_active()
3464 {
3465         return active;
3466 }
3467
3468 int BC_ListBox::expander_active()
3469 {
3470         for( int i=0; i<expanders.total; ++i ) {
3471                 if( expanders.values[i]->value ) return 1;
3472         }
3473         return 0 ;
3474 }
3475
3476 int BC_ListBox::keypress_event()
3477 {
3478         if( !active ) return 0;
3479
3480 //printf("BC_ListBox::keypress_event %d\n", __LINE__);
3481
3482         int result = 0, redraw = 0;
3483         int view_items = last_in_view - first_in_view + 1;
3484         if( view_items <= 0 ) view_items = view_h / get_text_height(MEDIUMFONT);
3485         int new_item = -1, new_selection = -1;
3486
3487         switch( top_level->get_keypress() ) {
3488         case ESC:
3489         case RETURN:
3490                 top_level->deactivate();
3491
3492 // If user is manipulating popup with keyboard, don't pass on event.
3493                 if( is_popup ) {
3494 //printf("BC_ListBox::keypress_event %d %p\n", __LINE__, get_selection(0, 0));
3495                         if( top_level->get_keypress() == RETURN )
3496                                 handle_event();
3497 //printf("BC_ListBox::keypress_event %d %p\n", __LINE__, get_selection(0, 0));
3498                         result = 1;
3499                 }
3500                 else
3501                         result = 0;
3502                 break;
3503
3504         case UP:
3505                 new_selection = new_item = select_previous(0);
3506
3507 //printf("BC_ListBox::keypress_event 1 %d\n", new_item);
3508                 if( new_item >= 0 ) {
3509                         center_selection(new_item);
3510                         redraw = 1;
3511                 }
3512                 reset_query();
3513                 result = 1;
3514                 break;
3515
3516         case DOWN:
3517                 new_selection = new_item = select_next(0);
3518
3519                 if( new_item >= 0 ) {
3520                         center_selection(new_item);
3521                         redraw = 1;
3522                 }
3523                 reset_query();
3524                 result = 1;
3525                 break;
3526
3527         case PGUP:
3528                 new_selection = new_item = select_previous(view_items - 1);
3529
3530                 if( new_item >= 0 ) {
3531                         center_selection(new_item);
3532                         redraw = 1;
3533                 }
3534                 reset_query();
3535                 result = 1;
3536                 break;
3537
3538         case PGDN:
3539                 new_selection = new_item = select_next(view_items - 1);
3540
3541                 if( new_item >= 0 ) {
3542                         center_selection(new_item);
3543                         redraw = 1;
3544                 }
3545                 reset_query();
3546                 result = 1;
3547                 break;
3548
3549         case LEFT:
3550                 xposition -= 10;
3551                 redraw = 1;
3552                 result = 1;
3553                 break;
3554
3555         case RIGHT:
3556                 xposition += 10;
3557                 redraw = 1;
3558                 result = 1;
3559                 break;
3560
3561         case DELETE:
3562         case HOME:
3563         case END:
3564                 result = 0;
3565                 break;
3566
3567         default:
3568                 if( show_query && !ctrl_down() ) {
3569                         int query_len = strlen(query);
3570                         if( query_len < (int)sizeof(query)-1 &&
3571                             top_level->get_keypress() > 30 &&
3572                             top_level->get_keypress() < 127 ) {
3573                                 query[query_len++] = top_level->get_keypress();
3574                                 query[query_len] = 0;
3575                                 new_selection = query_list();
3576                         }
3577                         else
3578                         if( top_level->get_keypress() == BACKSPACE ) {
3579                                 if( query_len > 0 ) query[--query_len] = 0;
3580                                 new_selection = query_list();
3581                         }
3582                         if( query_len > 0 )
3583                                 show_tooltip(query);
3584                         else
3585                                 hide_tooltip();
3586                         redraw = 1;
3587                         result = 1;
3588                 }
3589                 break;
3590         }
3591
3592         if( redraw ) {
3593                 clamp_positions();
3594                 draw_items(0);
3595                 update_scrollbars(1);
3596         }
3597
3598 //printf("BC_ListBox::keypress_event %d new_selection=%d\n", __LINE__, new_selection);
3599         if( new_selection >= 0 && !is_suggestions ) {
3600 //printf("BC_ListBox::keypress_event %d\n", __LINE__);
3601                 selection_changed();
3602 //printf("BC_ListBox::keypress_event %d\n", __LINE__);
3603         }
3604
3605         return result;
3606 }
3607
3608
3609 BC_Pixmap* BC_ListBox::get_bg_surface()
3610 {
3611         return bg_surface;
3612 }
3613
3614
3615 void BC_ListBox::draw_background()
3616 {
3617         if( !bg_draw ) return;
3618         bg_draw = 0;
3619 // White background pixmap
3620         set_color(top_level->get_resources()->listbox_inactive);
3621         draw_box(0, 0, bg_surface->get_w(), bg_surface->get_h(), bg_surface);
3622
3623 // Optional heroine pixmap
3624         if( bg_pixmap )
3625                 bg_surface->draw_pixmap(bg_pixmap,
3626                         bg_surface->get_w() - top_level->get_resources()->listbox_bg->get_w(),
3627                         0);
3628 }
3629
3630 void BC_ListBox::clear_listbox(int x, int y, int w, int h)
3631 {
3632         gui->draw_pixmap(bg_surface, x, y, w, h, x, y - title_h);
3633 }
3634
3635 void BC_ListBox::update_format(int display_format, int redraw)
3636 {
3637         this->display_format = display_format;
3638         packed_icons = display_format == LISTBOX_ICONS_PACKED ? 1 : 0;
3639         xposition = 0;  yposition = 0;
3640         if( redraw && gui ) draw_items(1, 1);
3641 }
3642
3643 int BC_ListBox::get_format()
3644 {
3645         return display_format;
3646 }
3647
3648
3649
3650 int BC_ListBox::draw_items(int flush, int draw_bg)
3651 {
3652         if( gui ) {
3653                 BC_Resources *resources = get_resources();
3654
3655 //dump(data, columns);
3656
3657 // Calculate items width
3658                 calculate_item_coords();
3659 // Create and destroy scrollbars as needed
3660                 get_scrollbars();
3661
3662                 if( bg_draw ) this->bg_draw = 1;
3663                 draw_background();
3664
3665                 first_in_view = -1;
3666                 last_in_view = 0;
3667 // Icon display
3668                 if( display_format == LISTBOX_ICONS ||
3669                     display_format == LISTBOX_ICONS_PACKED ) {
3670                         clear_listbox(2, 2 + title_h, view_w, view_h);
3671
3672                         set_font(MEDIUMFONT);
3673                         for( int i=0; i<data[master_column].size(); ++i ) {
3674                                 BC_ListBoxItem *item = data[master_column].get(i);
3675                                 if( get_item_x(item) >= -get_item_w(item) &&
3676                                     get_item_x(item) < view_w &&
3677                                     get_item_y(item) >= -get_item_h(item) + title_h &&
3678                                     get_item_y(item) < view_h + title_h ) {
3679                                         item->set_in_view(1);
3680                                         if( first_in_view < 0 ) first_in_view = i;
3681                                         last_in_view = i;
3682                                         int item_color = get_item_highlight(data, 0, i);
3683                                         int icon_x, icon_y, icon_w, icon_h;
3684                                         int text_x, text_y, text_w, text_h;
3685
3686 // Draw highlights
3687                                         get_icon_mask(item, icon_x, icon_y, icon_w, icon_h);
3688                                         get_text_mask(item, text_x, text_y, text_w, text_h);
3689
3690                                         if( item_color != resources->listbox_inactive ) {
3691                                                 gui->set_color(BLACK);
3692                                                 gui->draw_rectangle(icon_x, icon_y, icon_w, icon_h);
3693                                                 gui->set_color(item_color);
3694                                                 gui->draw_box(icon_x + 1, icon_y + 1, icon_w - 2, icon_h - 2);
3695                                                 gui->set_color(BLACK);
3696                                                 gui->draw_rectangle(text_x, text_y, text_w, text_h);
3697                                                 gui->set_color(item_color);
3698                                                 gui->draw_box(text_x + 1, text_y + 1, text_w - 2, text_h - 2);
3699
3700                                                 if( icon_position == ICON_LEFT )
3701                                                         gui->draw_box(text_x - 1, text_y + 1, 2, text_h - 2);
3702                                                 else
3703                                                 if( icon_position == ICON_TOP )
3704                                                         gui->draw_line(text_x + 1, text_y, text_x + icon_w - 2, text_y);
3705                                                 if( text_x + text_w < icon_x + icon_w ) {
3706                                                         gui->set_color(BLACK);
3707                                                         gui->draw_line(text_x + text_w, icon_y + icon_h,
3708                                                                 icon_x + icon_w, icon_y + icon_h);
3709                                                 }
3710                                         }
3711 // Draw icons
3712                                         gui->set_color(get_item_color(data, 0, i));
3713                                         VFrame *vicon = item->get_vicon_frame();
3714                                         if( vicon )
3715                                                 gui->pixmap->draw_vframe(vicon, icon_x, icon_y);
3716                                         else if( item->icon )
3717                                                 gui->pixmap->draw_pixmap(item->icon, icon_x, icon_y);
3718                                         char item_text[BCTEXTLEN];
3719                                         if( display_format == LISTBOX_ICONS_PACKED )
3720                                                 gui->truncate_text(item_text, item->text, text_w);
3721                                         else
3722                                                 strcpy(item_text, item->text);
3723                                         gui->draw_text(text_x, text_y + get_baseline(item), item_text);
3724                                 }
3725                                 else
3726                                         item->set_in_view(0);
3727                         }
3728                 }
3729 // Text display
3730                 else {
3731 // Draw one column at a time so text overruns don't go into the next column
3732 // clear column backgrounds
3733                         int current_toggle = 0;
3734                         for( int j=0; j<columns; ++j ) {
3735                                 clear_listbox(LISTBOX_BORDER + get_column_offset(j) - xposition,
3736                                         LISTBOX_BORDER + title_h,
3737                                         get_column_width(j, 1),
3738                                         view_h);
3739 // Draw rows in the column recursively
3740                                 draw_text_recursive(data, j, 0, &current_toggle);
3741                         }
3742
3743 // Delete excess expanders
3744                         while( expanders.total > current_toggle ) {
3745                                 expanders.remove_object();
3746                         }
3747                 }
3748
3749 // draw user images if available
3750                 draw_images();
3751 // Draw titles on top of rows for superposition effect
3752                 draw_titles(0);
3753
3754 // Clear garbage from bottom right corner
3755                 if( xscrollbar && yscrollbar && is_popup ) {
3756                         gui->draw_top_background(parent_window,
3757                                 popup_w - get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w(),
3758                                 popup_h - get_resources()->hscroll_data[SCROLL_HANDLE_UP]->get_h(),
3759                                 get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w(),
3760                                 get_resources()->hscroll_data[SCROLL_HANDLE_UP]->get_h());
3761                 }
3762
3763 // Draw borders
3764                 draw_border(0);
3765
3766
3767                 if( current_operation == SELECT_RECT )
3768                         draw_rectangle(0);
3769
3770                 gui->flash(flush);
3771         }
3772
3773         return 0;
3774 }
3775
3776
3777 void BC_ListBox::draw_text_recursive(ArrayList<BC_ListBoxItem*> *data,
3778                 int column, int indent, int *current_toggle)
3779 {
3780         if( !data ) return;
3781         BC_Resources *resources = get_resources();
3782
3783         set_font(MEDIUMFONT);
3784         int subindent = 0;
3785
3786 // Search for a branch and make room for toggle if there is one
3787         if( column == 0 ) {
3788                 for( int i=0; i<data[column].size(); ++i ) {
3789                         if( data[column].get(i)->get_sublist() ) {
3790                                 subindent = BC_WindowBase::get_resources()->listbox_expand[0]->get_w();
3791                                 break;
3792                         }
3793                 }
3794         }
3795
3796         row_height = row_ascent = row_descent = 0;
3797         for( int i=0; i<data[column].total; ++i ) {
3798                 BC_ListBoxItem *item = data[column].values[i];
3799                 int ht = get_text_h(item);
3800                 if( ht > row_height ) row_height = ht;
3801                 int bl = get_baseline(item);
3802                 if( bl > row_ascent ) row_ascent = bl;
3803                 int dt = ht - bl;
3804                 if( dt > row_descent ) row_descent = dt;
3805         }
3806
3807         for( int i=0; i<data[column].size(); ++i ) {
3808 // Draw a row
3809                 BC_ListBoxItem *item = data[column].values[i];
3810                 BC_ListBoxItem *first_item = data[master_column].values[i];
3811
3812                 if( get_item_y(item) >= -get_item_h(item) + title_h &&
3813                     get_item_y(item) < view_h + title_h ) {
3814                         int row_color = get_item_highlight(data, 0, i);
3815                         int x, y, w, h, column_width;
3816
3817                         get_text_mask(item, x, y, w, h);
3818                         column_width = get_column_width(column, 1);
3819                         if( x + column_width > view_w + LISTBOX_BORDER * 2 )
3820                                 column_width = view_w + LISTBOX_BORDER * 2 - x;
3821
3822                         if( row_color != resources->listbox_inactive ) {
3823                                 gui->set_color(row_color);
3824                                 gui->draw_box(x, y, column_width, h);
3825                                 gui->set_color(BLACK);
3826                                 int xx = x + column_width-1;
3827                                 gui->draw_line(x, y, xx, y);
3828                                 int hh = h;
3829                                 if( display_format == LISTBOX_ICON_LIST ) {
3830                                         int ih = get_icon_h(item);
3831                                         if( ih > hh ) hh = ih;
3832                                 }
3833                                 int yy = y + hh-1;
3834                                 gui->draw_line(x, yy, xx, yy);
3835                         }
3836
3837                         gui->set_color(get_item_color(data, column, i));
3838
3839
3840                         if( column == 0 && display_format == LISTBOX_ICON_LIST ) {
3841                                 int ix = get_icon_x(item), iy = get_icon_y(item);
3842                                 VFrame *vicon = item->get_vicon_frame();
3843                                 if( vicon ) {
3844                                         gui->pixmap->draw_vframe(vicon, ix, iy);
3845                                         x += vicon->get_w() + ICON_MARGIN;
3846                                 }
3847                                 else if( item->icon ) {
3848                                         gui->pixmap->draw_pixmap(item->icon, ix, iy);
3849                                         x += item->icon->get_w() + ICON_MARGIN;
3850                                 }
3851                         }
3852
3853
3854 // Indent only applies to first column
3855                         gui->draw_text(
3856                                 x + LISTBOX_BORDER + LISTBOX_MARGIN +
3857                                         (column == 0 ? indent + subindent : 0),
3858                                 y + get_baseline(item), item->text);
3859                         item->set_in_view(1);
3860                         if( !indent ) {
3861                                 if( first_in_view < 0 ) first_in_view = i;
3862                                 last_in_view = i;
3863                         }
3864
3865 // Update expander
3866                         if( column == 0 && item->get_sublist() && item->get_columns() ) {
3867 // Create new expander
3868                                 if( *current_toggle >= expanders.total ) {
3869                                         BC_ListBoxToggle *toggle =
3870                                                 new BC_ListBoxToggle(this, item,
3871                                                         x + LISTBOX_BORDER + LISTBOX_MARGIN + indent, y);
3872                                         toggle->draw(0);
3873                                         expanders.append(toggle);
3874                                 }
3875 // Reposition existing expander
3876                                 else {
3877                                         BC_ListBoxToggle *toggle = expanders.values[*current_toggle];
3878 //printf("BC_ListBox::draw_text_recursive 1 %d\n", *current_toggle);
3879                                         toggle->update(item,
3880                                                 x + LISTBOX_BORDER + LISTBOX_MARGIN + indent, y, 0);
3881                                 }
3882                                 (*current_toggle)++;
3883                         }
3884
3885
3886
3887                 }
3888                 else
3889                         item->set_in_view(0);
3890
3891 // Descend into sublist
3892                 if( first_item->sublist_active() ) {
3893                         draw_text_recursive(first_item->get_sublist(),
3894                                 column,
3895                                 indent + LISTBOX_INDENT,
3896                                 current_toggle);
3897                 }
3898         }
3899 }
3900
3901 int BC_ListBox::draw_border(int flash)
3902 {
3903         BC_Resources *resources = top_level->get_resources();
3904         gui->draw_3d_border(0, 0,
3905                 view_w + LISTBOX_BORDER * 2, view_h + title_h + LISTBOX_BORDER * 2,
3906                 resources->listbox_border1,
3907                 list_highlighted ?
3908                         resources->listbox_border2_hi : resources->listbox_border2,
3909                 list_highlighted ?
3910                         resources->listbox_border3_hi : resources->listbox_border3,
3911                 resources->listbox_border4);
3912
3913         if( flash ) {
3914                 gui->flash();
3915                 gui->flush();
3916         }
3917         return 0;
3918 }
3919
3920 void BC_ListBox::draw_title(int number)
3921 {
3922 // Column title background
3923         int image_number = 0;
3924         if( number == highlighted_title ) {
3925                 image_number = 1;
3926                 if( current_operation == COLUMN_DN )
3927                         image_number = 2;
3928         }
3929
3930         gui->draw_3segmenth(get_column_offset(number) - xposition + LISTBOX_BORDER,
3931                 LISTBOX_BORDER,
3932                 get_column_width(number, 1) + get_resources()->listbox_title_overlap,
3933                 column_bg[image_number]);
3934
3935         int title_x = -xposition + get_column_offset(number) + 
3936                 LISTBOX_MARGIN + LISTBOX_BORDER;
3937         title_x += get_resources()->listbox_title_margin;
3938
3939         gui->set_color(get_resources()->listbox_title_color);
3940         gui->draw_text(title_x, 
3941                 LISTBOX_MARGIN + LISTBOX_BORDER + get_text_ascent(MEDIUMFONT), 
3942                 column_titles[number]);
3943
3944 // Column sort order
3945         if( number == sort_column ) {
3946                 BC_Pixmap *src = sort_order == SORT_ASCENDING ?
3947                         column_sort_dn : column_sort_up;
3948
3949 //              int column_offset = get_column_offset(number) - xposition + LISTBOX_BORDER;
3950 //              int column_width = get_column_width(number, 1);
3951 //              int toggle_x = column_offset + column_width - LISTBOX_BORDER;
3952 //              if( toggle_x > items_w ) toggle_x = items_w;
3953 //              toggle_x -= 5 + src->get_w();
3954
3955                 int x = title_x + 
3956                         get_text_width(MEDIUMFONT, column_titles[number]) +
3957                         LISTBOX_MARGIN;
3958                 
3959                 gui->draw_pixmap(src,
3960                         x,
3961                         title_h / 2 - src->get_h() / 2 + LISTBOX_BORDER);
3962         }
3963 }
3964
3965 int BC_ListBox::draw_titles(int flash)
3966 {
3967         if( column_titles &&
3968             (display_format == LISTBOX_TEXT ||
3969              display_format == LISTBOX_ICON_LIST) ) {
3970 //printf("BC_ListBox::draw_titles 1 %d\n", highlighted_title);
3971                 for( int i=0; i<columns; ++i ) {
3972                         if( i != highlighted_title )
3973                                 draw_title(i);
3974                 }
3975
3976                 if( highlighted_title >= 0 ) draw_title(highlighted_title);
3977                 draw_border(0);
3978         }
3979
3980         if( flash ) {
3981                 gui->flash();
3982         }
3983         return 0;
3984 }
3985
3986 void BC_ListBox::draw_toggles(int flash)
3987 {
3988         for( int i=0; i<expanders.total; ++i )
3989                 expanders.values[i]->draw(0);
3990
3991 //printf("BC_ListBox::draw_toggles 1 %d\n", flash);
3992         if( flash && expanders.total ) {
3993                 gui->flash();
3994         }
3995 }
3996
3997 int BC_ListBox::draw_rectangle(int flash)
3998 {
3999         int x1 = MIN(rect_x1, rect_x2);
4000         int x2 = MAX(rect_x1, rect_x2);
4001         int y1 = MIN(rect_y1, rect_y2);
4002         int y2 = MAX(rect_y1, rect_y2);
4003
4004         if( x1 == x2 || y1 == y2 ) return 0;
4005
4006         gui->set_inverse();
4007         gui->set_color(WHITE);
4008         gui->draw_rectangle(x1, y1, x2 - x1, y2 - y1);
4009         gui->set_opaque();
4010
4011
4012         if( flash ) {
4013                 gui->flash();
4014         }
4015         return 0;
4016 }
4017
4018 void BC_ListBox::dump(ArrayList<BC_ListBoxItem*> *data, int columns,
4019                 int indent, int master_column)
4020 {
4021         if( !indent ) {
4022                 printf("BC_ListBox::dump 1\n");
4023         }
4024
4025         for( int i=0; i<data[master_column].total; ++i ) {
4026                 for( int k=0; k<indent; ++k )
4027                         printf(" ");
4028                 for( int j=0; j<columns; ++j ) {
4029                         BC_ListBoxItem *item = data[j].values[i];
4030                         printf("%d,%d,%d=%s ",
4031                                 item->get_text_x(), item->get_text_y(),
4032                                 item->autoplace_text, item->get_text());
4033                 }
4034                 printf("\n");
4035
4036                 if( data[master_column].values[i]->get_sublist() ) {
4037                         dump(data[master_column].values[i]->get_sublist(),
4038                                 data[master_column].values[i]->get_columns(),
4039                                 indent + 4,
4040                                 master_column);
4041                 }
4042         }
4043 }
4044