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