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