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