change thread join strategy, fix a few leaks, fix a few bugs
[goodguy/history.git] / cinelerra-5.1 / guicast / bclistbox.h
1 /*
2  * CINELERRA
3  * Copyright (C) 2010 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 #ifndef BCLISTBOX_H
22 #define BCLISTBOX_H
23
24 #include "bcdragwindow.inc"
25 #include "bclistboxitem.inc"
26 #include "bcpixmap.inc"
27 #include "bcscrollbar.h"
28 #include "bcsubwindow.h"
29 #include "bctoggle.h"
30 #include "colors.h"
31
32 #define BCPOPUPLISTBOX_W 25
33 #define BCPOPUPLISTBOX_H 25
34
35
36 #define MIN_COLUMN_WIDTH 10
37
38
39 class BC_ListBoxYScroll : public BC_ScrollBar
40 {
41 public:
42         BC_ListBoxYScroll(BC_ListBox *listbox,
43                           int total_height,
44                                           int view_height,
45                           int position);
46         ~BC_ListBoxYScroll();
47         int handle_event();
48 private:
49         BC_ListBox *listbox;
50 };
51
52 class BC_ListBoxXScroll : public BC_ScrollBar
53 {
54 public:
55         BC_ListBoxXScroll(BC_ListBox *listbox,
56                           int total_width,
57                                           int view_width,
58                           int position);
59         ~BC_ListBoxXScroll();
60         int handle_event();
61 private:
62         BC_ListBox *listbox;
63 };
64
65 class BC_ListBoxToggle
66 {
67 public:
68         BC_ListBoxToggle(BC_ListBox *listbox,
69                 BC_ListBoxItem *item,
70                 int x,
71                 int y);
72
73         int cursor_motion_event(int *redraw_toggles);
74         int cursor_leave_event(int *redraw_toggles);
75         int button_press_event();
76         int button_release_event(int *redraw_toggles);
77         void update(BC_ListBoxItem *item, int x, int y, int flash);
78         void draw(int flash);
79
80         BC_ListBox *listbox;
81         BC_ListBoxItem *item;
82         int x, y;
83         int value, state;
84         enum
85         {
86                 TOGGLE_UP,
87                 TOGGLE_UPHI,
88                 TOGGLE_CHECKED,
89                 TOGGLE_DOWN,
90                 TOGGLE_CHECKEDHI,
91 // Button pressed then moved out
92                 TOGGLE_DOWN_EXIT
93         };
94 };
95
96
97 class BC_ListBox : public BC_SubWindow
98 {
99 public:
100         BC_ListBox(int x,
101                 int y,
102                 int w,
103                 int h,
104                 int display_format,                   // Display text list or icons
105                 ArrayList<BC_ListBoxItem*> *data = 0, // Each column has an ArrayList of BC_ListBoxItems.
106                 const char **column_titles = 0,             // Titles for columns.  Set to 0 for no titles
107                 int *column_width = 0,                // width of each column
108                 int columns = 1,                      // Total columns.  Only 1 in icon mode
109                 int yposition = 0,                    // Pixel of top of window.
110                 int is_popup = 0,                     // If this listbox is a popup window with a button
111                 int selection_mode = LISTBOX_SINGLE,  // Select one item or multiple items
112                 int icon_position = ICON_LEFT,        // Position of icon relative to text of each item
113                 int allow_drag = 0);                  // Allow user to drag icons around
114         virtual ~BC_ListBox();
115
116         friend class BC_ListBoxToggle;
117
118         int initialize();
119
120 // User event handler for new selections
121         virtual int selection_changed() { return 0; };
122 // User event handler for triggering a selection
123         virtual int handle_event() { return 0; };
124 // User event handler for a column resize
125         virtual int column_resize_event() { return 0; };
126 // Draw background on bg_surface
127         virtual void draw_background();
128 // Draw on bg_surface
129         virtual int draw_images() { return 0; }
130 // Column sort order.  This must return 1 or BC_ListBox will perform a default
131 // action.
132         virtual int sort_order_event() { return 0; };
133 // Column moved
134         virtual int move_column_event() { return 0; };
135
136         int enable();
137         int disable();
138
139 // Get the column movement
140         int get_from_column();
141         int get_to_column();
142
143 // Get the item in the given column which is the selection_number of the total
144 // selected rows.  Returns 0 on failure.
145         BC_ListBoxItem* get_selection(int column, int selection_number);
146         BC_ListBoxItem* get_selection_recursive(ArrayList<BC_ListBoxItem*> *data,
147                 int column,
148                 int selection_number);
149
150 // Get the flat index of the item in the given column which is the selection_number
151 // of the total selected rows.  Returns -1 on failure.  The column
152 // argument is really useless because it only checks column 1 and returns the row
153 // number.
154         int get_selection_number(int column, int selection_number);
155         int get_selection_number_recursive(ArrayList<BC_ListBoxItem*> *data,
156                 int column,
157                 int selection_number,
158                 int *counter = 0);
159
160         virtual int evaluate_query(char *string);
161         void expand_item(BC_ListBoxItem *item, int expand);
162 // Collapse all items
163         static void collapse_recursive(ArrayList<BC_ListBoxItem*> *data,
164                 int master_column);
165 // Convert recursive pointer to flat index.
166 // The pointer can be any item in the row corresponding to the index.
167 // Returns -1 if no item was found.
168         int item_to_index(ArrayList<BC_ListBoxItem*> *data,
169                 BC_ListBoxItem *item,
170                 int *counter = 0);
171 // Convert flat index and column to desired item.
172         BC_ListBoxItem* index_to_item(ArrayList<BC_ListBoxItem*> *data,
173                 int number,
174                 int column,
175                 int *counter = 0);
176 // Get all items with recursion for text mode
177         static int get_total_items(ArrayList<BC_ListBoxItem*> *data,
178                 int *result /* = 0 */,
179                 int master_column);
180
181
182         int focus_out_event();
183         virtual int button_press_event();
184         int button_release_event();
185         int cursor_enter_event();
186         int cursor_leave_event();
187         int cursor_motion_event();
188         virtual int drag_start_event();
189         virtual int drag_motion_event();
190         virtual int drag_stop_event();
191         int deactivate();
192 // After popping up a menu call this to interrupt the selection process
193         void deactivate_selection();
194
195 // take_focus - used by the suggestion box to keep it from taking focus from the
196 // textbox
197         int activate(int take_focus = 1);
198
199         virtual int keypress_event();
200         int translation_event();
201         int repeat_event(int64_t duration);
202         BC_DragWindow* get_drag_popup();
203 // If the popup window should show a button.
204 // Must be called in the constructor.
205         void set_use_button(int value);
206         void set_is_suggestions(int value);
207
208
209 // change the contents
210         int update(ArrayList<BC_ListBoxItem*> *data,
211                                                 const char **column_titles,
212                                                 int *column_widths,
213                                                 int columns,
214                                                 int xposition = 0,
215                                                 int yposition = 0,
216                                                 int highlighted_number = -1,  // Flat index of item cursor is over
217                                                 int recalc_positions = 0,     // set all autoplace flags to 1
218                                                 int draw = 1);
219         void center_selection();
220         void update_format(int display_format, int redraw);
221         int get_format();
222
223 // Allow scrolling when dragging items
224         void set_drag_scroll(int value);
225 // Allow column repositioning
226         void set_allow_drag_column(int value);
227 // Allow automatic moving of objects after drag
228         void set_process_drag(int value);
229
230 // Set the column to use for icons and sublists.
231         void set_master_column(int value, int redraw);
232 // Set column to search
233         void set_search_column(int value);
234         int set_selection_mode(int mode);
235         int set_yposition(int position, int draw_items = 1);
236         int get_yposition();
237         int set_xposition(int position);
238         int get_xposition();
239 // Return the flat index of the highlighted item
240         int get_highlighted_item();
241         int get_yscroll_x();
242         int get_yscroll_y();
243         int get_yscroll_height();
244         int get_xscroll_x();
245         int get_xscroll_y();
246         int get_xscroll_width();
247         int get_column_offset(int column);
248         int get_column_width(int column, int clamp_right = 0);
249         int get_title_h();
250         int get_display_mode();
251         void set_justify(int value);
252         int get_w() { return is_popup ? BCPOPUPLISTBOX_W : popup_w; }
253         int get_h() { return is_popup ? BCPOPUPLISTBOX_H : popup_h; }
254         int get_view_w() { return view_w; }
255         int get_view_h() { return view_h; }
256         int get_row_height() { return row_height; }
257         int get_row_ascent() { return row_ascent; }
258         int get_row_descent() { return row_descent; }
259         int get_first_visible() { return first_in_view; }
260         int get_last_visible() { return last_in_view; }
261
262
263         enum
264         {
265                 SORT_ASCENDING,
266                 SORT_DESCENDING
267         };
268
269         int get_sort_column();
270         void set_sort_column(int value, int redraw = 0);
271         int get_sort_order();
272         void set_sort_order(int value, int redraw = 0);
273
274
275         void reset_query();
276         int reposition_window(int x,
277                 int y,
278                 int w = -1,
279                 int h = -1,
280                 int flush = 1);
281         BC_Pixmap* get_bg_surface();
282 // Set all items for autoplacement with recursion into sublists
283         void set_autoplacement(ArrayList<BC_ListBoxItem*> *data,
284                 int do_icon,
285                 int do_text);
286 // Set selection status on all items with recursion
287         void set_all_selected(ArrayList<BC_ListBoxItem*> *data, int value);
288 // Set selection status of single row with recursion
289 // item_number - the flat index of the row to select
290         void set_selected(ArrayList<BC_ListBoxItem*> *data,
291                 int item_number,
292                 int value,
293                 int *counter = 0);
294
295 // Called by keypress_event for cursor up.  Selects previous and next visible
296 // row, skipping desired number of visible rows.
297         int select_previous(int skip,
298                 BC_ListBoxItem *selected_item = 0,
299                 int *counter = 0,
300                 ArrayList<BC_ListBoxItem*> *data = 0,
301                 int *got_it = 0,
302                 int *got_second = 0);
303         int select_next(int skip,
304                 BC_ListBoxItem *selected_item = 0,
305                 int *counter = 0,
306                 ArrayList<BC_ListBoxItem*> *data = 0,
307                 int *got_it = 0,
308                 int *got_second = 0);
309
310 // Called by cursor_motion_event to select different item if selection_number
311 // changed.  Returns 1 if redraw is required.
312         int update_selection(ArrayList<BC_ListBoxItem*> *data,
313                 int selection_number,
314                 int *counter = 0);
315
316         static void dump(ArrayList<BC_ListBoxItem*> *data,
317                 int columns,
318                 int indent /* = 0 */,
319                 int master_column);
320
321         int get_item_x(BC_ListBoxItem *item);
322         int get_item_y(BC_ListBoxItem *item);
323         int get_item_w(BC_ListBoxItem *item);
324         int get_item_h(BC_ListBoxItem *item);
325
326 // Draw the list items
327         int draw_items(int flash, int bg_draw=0);
328
329 private:
330         void delete_columns();
331         void set_columns(const char **column_titles,
332                 int *column_widths,
333                 int columns);
334 // Draw the button for a popup listbox
335         int draw_button(int flush);
336 // Draw list border
337         int draw_border(int flash);
338 // Draw column titles
339         void draw_title(int number);
340         int draw_titles(int flash);
341 // Draw expanders
342         void draw_toggles(int flash);
343 // Draw selection rectangle
344         int draw_rectangle(int flash);
345
346
347         void draw_text_recursive(ArrayList<BC_ListBoxItem*> *data,
348                 int column,
349                 int indent,
350                 int *current_toggle);
351 // Returns 1 if selection changed
352         int query_list();
353         void init_column_width();
354         void reset_cursor(int new_cursor);
355 // Fix boundary conditions after resize
356         void column_width_boundaries();
357 // Recursive function to get the first item selected in text mode.
358 // Returns > -1 only if it got it.  Always increments *result
359         int get_first_selection(ArrayList<BC_ListBoxItem*> *data, int *result = 0);
360 // Recursive function to get the last item selected in text mode.
361 // Returns > -1 only if it got it.  Always increments *result
362         int get_last_selection(ArrayList<BC_ListBoxItem*> *data, int *result = 0);
363 // Called by button_press_event and cursor_motion_event to expand the selection.
364 // Returns 1 if redraw is required.
365         int expand_selection(int button_press, int selection_number);
366 // Called by button_press_event and cursor_motion_event
367 // to select a range in text mode
368         void select_range(ArrayList<BC_ListBoxItem*> *data,
369                 int start,
370                 int end,
371                 int *current = 0);
372 // Called by button_press_event to toggle the selection status of a single item.
373 // Called for both text and icon mode.  In text mode it's recursive and fills
374 // the entire row with the first item's value.  Returns 1 when the item was toggled.
375         int toggle_item_selection(ArrayList<BC_ListBoxItem*> *data,
376                 int selection_number,
377                 int *counter = 0);
378 // Set value of selected in all selected items to new value
379         void promote_selections(ArrayList<BC_ListBoxItem*> *data,
380                 int old_value,
381                 int new_value);
382
383
384         int test_column_divisions(int cursor_x, int cursor_y, int &new_cursor);
385         int test_column_titles(int cursor_x, int cursor_y);
386         int test_expanders();
387
388         int calculate_item_coords();
389         void calculate_last_coords_recursive(
390                 ArrayList<BC_ListBoxItem*> *data,
391                 int *icon_x,
392                 int *next_icon_x,
393                 int *next_icon_y,
394                 int *next_text_y,
395                 int top_level);
396         void calculate_item_coords_recursive(
397                 ArrayList<BC_ListBoxItem*> *data,
398                 int *icon_x,
399                 int *next_icon_x,
400                 int *next_icon_y,
401                 int *next_text_y,
402                 int top_level);
403
404         int get_items_width();
405         int get_items_height(ArrayList<BC_ListBoxItem*> *data,
406                 int columns,
407                 int *result = 0);
408         int get_icon_w(BC_ListBoxItem *item);
409         int get_icon_h(BC_ListBoxItem *item);
410         int get_text_w(BC_ListBoxItem *item);
411         int get_text_h(BC_ListBoxItem *item);
412         int get_baseline(BC_ListBoxItem *item);
413         int get_item_highlight(ArrayList<BC_ListBoxItem*> *data, int column, int item);
414         int get_item_color(ArrayList<BC_ListBoxItem*> *data, int column, int item);
415         int get_icon_mask(BC_ListBoxItem *item, int &x, int &y, int &w, int &h);
416         int get_text_mask(BC_ListBoxItem *item, int &x, int &y, int &w, int &h);
417 // Copy sections of the bg_surface to the gui
418         void clear_listbox(int x, int y, int w, int h);
419
420 // Tests for cursor outside boundaries
421         int test_drag_scroll(int cursor_x, int cursor_y);
422 // Called by select_scroll_event, rectangle_scroll_event to execute for movement
423         int drag_scroll_event();
424         int select_scroll_event();
425         int rectangle_scroll_event();
426
427
428         void move_vertical(int pixels);
429         void move_horizontal(int pixels);
430         void clamp_positions();
431
432         int get_scrollbars();
433         void update_scrollbars(int flush);
434
435 // Flat index of the item the cursor is over.
436 // Points *item_return to the first item in the row or 0 if no item was found.
437 // if it's nonzero.  Returns -1 if no item was found.  Clamps the y coordinate
438 // only if the current operation is not SELECT, so scrolling is possible.
439 // expanded - 1 if items in this table should be tested for cursor coverage
440         int get_cursor_item(ArrayList<BC_ListBoxItem*> *data,
441                 int cursor_x,
442                 int cursor_y,
443                 BC_ListBoxItem **item_return = 0,
444                 int *counter = 0,
445                 int expanded = 1);
446 // Select the items in the rectangle and deselect the items outside of it.
447 // Returns 1 if redraw is required.
448         int select_rectangle(ArrayList<BC_ListBoxItem*> *data,
449                 int x1,
450                 int y1,
451                 int x2,
452                 int y2);
453 // Convert the row of the item to a pointer.
454         BC_ListBoxItem* number_to_item(int row);
455         int reposition_item(ArrayList<BC_ListBoxItem*> *data,
456                 int selection_number,
457                 int x,
458                 int y,
459                 int *counter = 0);
460 // Move selected items to src_items
461         void move_selection(ArrayList<BC_ListBoxItem*> *dst,
462                 ArrayList<BC_ListBoxItem*> *src);
463 // Put items from the src table into the data table starting at flat item number
464 // destination.
465         int put_selection(ArrayList<BC_ListBoxItem*> *data,
466                 ArrayList<BC_ListBoxItem*> *src,
467                 int destination,
468                 int *counter = 0);
469
470         int center_selection(int selection,
471                 ArrayList<BC_ListBoxItem*> *data = 0,
472                 int *counter = 0);
473
474 // Array of one list of pointers for each column
475         ArrayList<BC_ListBoxItem*> *data;
476
477
478 // 1 if a button is used to make the listbox display
479         int is_popup;      // popup
480 // If the button for a popup should be shown
481         int use_button;
482 // background update needed
483         int bg_draw;
484
485 // Dimensions for a popup if there is one
486         int popup_w, popup_h;
487 // pixel of top of display relative to top of list
488         int yposition;
489 // pixel of left display relative to first column
490         int xposition;
491 // dimensions of a row in the list
492         int row_height, row_ascent, row_descent;
493
494
495         int selection_mode;
496         int display_format;
497         int temp_display_format;
498         int icon_position;
499 // Scrollbars are created as needed
500         BC_ListBoxXScroll *xscrollbar;
501         BC_ListBoxYScroll *yscrollbar;
502         ArrayList<BC_ListBoxToggle*> expanders;
503         char query[BCTEXTLEN];
504
505
506 // Window containing the listbox
507         BC_WindowBase *gui;
508         int disabled;
509 // Size of the popup if there is one
510         char **column_titles;
511         int *column_width;
512         int default_column_width[1];
513         int columns;
514         int master_column;
515         int search_column;
516
517         int view_h, view_w;
518         int first_in_view, last_in_view;
519         int title_h;
520 // Maximum width of items.  Calculated by get_items_width
521         int items_w;
522         int items_h;
523 // In BCLISTBOX_SELECT mode determines the value to set items to
524         int new_value;
525         int need_xscroll, need_yscroll;
526 // Move items during drag operation of text items.
527         int process_drag;
528         int allow_drag;
529         int allow_drag_scroll;
530         int allow_drag_column;
531 // Background color of listbox
532         int list_background;
533
534
535
536 // Popup button
537         BC_Pixmap *button_images[4];
538 // Expander
539         BC_Pixmap *toggle_images[5];
540 // Background for drawing on
541         BC_Pixmap *bg_surface;
542 // Background if 9 segment
543         BC_Pixmap *bg_tile;
544 // Drag icon for text mode
545         VFrame *drag_icon_vframe;
546 // Drag column icon
547         VFrame *drag_column_icon_vframe;
548 // Background picon to be drawn in the upper right
549         BC_Pixmap *bg_pixmap;
550
551
552 // Column title backgrounds
553         BC_Pixmap *column_bg[3];
554 // Column sort order
555         BC_Pixmap *column_sort_up;
556         BC_Pixmap *column_sort_dn;
557
558
559
560
561 // Number of column to sort
562         int sort_column;
563 // Sort order.  -1 means no column is being sorted.
564         int sort_order;
565
566
567
568
569
570 // State of the list box and button when the mouse button is pressed.
571         int current_operation;
572
573         enum
574         {
575                 NO_OPERATION,
576                 BUTTON_DOWN_SELECT, // Pressed button and slid off to select items.
577                 BUTTON_DN,
578                 DRAG_DIVISION,    // Dragging column division
579                 DRAG_COLUMN,      // Dragging column
580                 DRAG_ITEM,        // Dragging item
581                 SELECT,                   // Select item
582                 SELECT_RECT,      // Selection rectangle
583                 WHEEL,                    // Wheel mouse
584                 COLUMN_DN,                // column title down
585                 COLUMN_DRAG,      // column title is being dragged
586                 EXPAND_DN         // Expander is down
587         };
588
589
590 // More state variables
591         int button_highlighted;
592         int list_highlighted;
593 // item cursor is over.  May not exist in tables.
594 // Must be an index since this is needed to change the database.
595         int highlighted_item;
596         BC_ListBoxItem* highlighted_ptr;
597 // column title if the cursor is over a column title
598         int highlighted_title;
599 // Division the cursor is operating on when resizing
600         int highlighted_division;
601 // Column title being dragged
602         int dragged_title;
603 // start of column title drag
604         int drag_cursor_x;
605         int drag_column_w;
606
607 // Selection range being extended
608         int selection_start, selection_end, selection_center;
609 // Item being dragged or last item selected in a double click operation
610         int selection_number;
611 // Used in button_press_event and button_release_event to detect double clicks
612         int selection_number1, selection_number2;
613
614
615
616
617         int active;
618 // Popup listboxes have different behavior for suggestion boxes where the
619 // textbox needs to be active when the listbox is visible.
620         int is_suggestions;
621
622 // Button release counter for double clicking
623         int button_releases;
624         int current_cursor;
625 // Starting coordinates of rectangle
626         int rect_x1, rect_y1;
627         int rect_x2, rect_y2;
628
629
630
631 // Window for dragging
632         BC_DragWindow *drag_popup;
633         int justify;
634 };
635
636
637
638
639 #endif