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