merged hv7 mod
[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 "bccolors.h"
31
32 #define BCPOPUPLISTBOX_W 25
33 #define BCPOPUPLISTBOX_H 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
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
193         int translation_event();
194         int repeat_event(int64_t duration);
195         BC_DragWindow* get_drag_popup();
196 // If the popup window should show a button.
197 // Must be called in the constructor.
198         void set_use_button(int value);
199         void set_is_suggestions(int value);
200
201
202 // change the contents
203         int update(ArrayList<BC_ListBoxItem*> *data,
204                 const char **column_titles, int *column_widths, int columns,
205                 int xposition = 0, int yposition = 0,
206                 int highlighted_number = -1,  // Flat index of item cursor is over
207                 int recalc_positions = 0,     // set all autoplace flags to 1
208                 int draw = 1);
209         void center_selection();
210         void update_format(int display_format, int redraw);
211         int get_format();
212
213 // Allow scrolling when dragging items
214         void set_drag_scroll(int value);
215 // Allow column repositioning
216         void set_allow_drag_column(int value);
217 // Allow automatic moving of objects after drag
218         void set_process_drag(int value);
219
220 // Set the column to use for icons and sublists.
221         void set_master_column(int value, int redraw);
222 // Set column to search
223         void set_search_column(int value);
224         int set_selection_mode(int mode);
225         int set_yposition(int position, int draw_items = 1);
226         int get_yposition();
227         int set_xposition(int position);
228         int get_xposition();
229 // Return the flat index of the highlighted item
230         int get_highlighted_item();
231         int get_yscroll_x();
232         int get_yscroll_y();
233         int get_yscroll_height();
234         int get_xscroll_x();
235         int get_xscroll_y();
236         int get_xscroll_width();
237         void set_scroll_stretch(int xv, int yv);
238         int get_column_offset(int column);
239         int get_column_width(int column, int clamp_right = 0);
240         int get_title_h();
241         int get_display_mode();
242         void set_justify(int value);
243         int get_w() { return is_popup ? BCPOPUPLISTBOX_W : popup_w; }
244         int get_h() { return is_popup ? BCPOPUPLISTBOX_H : popup_h; }
245         int gui_tooltip(const char *text) {
246                 return is_popup && gui ? gui->show_tooltip(text, gui->get_w(),0, -1,-1) : -1;
247         }
248         int get_view_w() { return view_w; }
249         int get_view_h() { return view_h; }
250         int get_row_height() { return row_height; }
251         int get_row_ascent() { return row_ascent; }
252         int get_row_descent() { return row_descent; }
253         int get_first_visible() { return first_in_view; }
254         int get_last_visible() { return last_in_view; }
255
256         enum
257         {
258                 SORT_ASCENDING,
259                 SORT_DESCENDING
260         };
261
262         int get_sort_column();
263         void set_sort_column(int value, int redraw = 0);
264         int get_sort_order();
265         void set_sort_order(int value, int redraw = 0);
266
267
268         void reset_query();
269         int get_show_query() { return show_query; }
270         void set_show_query(int v) { show_query = v; }
271
272         int reposition_window(int x,
273                 int y,
274                 int w = -1,
275                 int h = -1,
276                 int flush = 1);
277         BC_Pixmap* get_bg_surface();
278 // Set all items for autoplacement with recursion into sublists
279         void set_autoplacement(ArrayList<BC_ListBoxItem*> *data,
280                 int do_icon,
281                 int do_text);
282 // Set selection status on all items with recursion
283         void set_all_selected(ArrayList<BC_ListBoxItem*> *data, int value);
284 // Set selection status of single row with recursion
285 // item_number - the flat index of the row to select
286         void set_selected(ArrayList<BC_ListBoxItem*> *data,
287                 int item_number,
288                 int value,
289                 int *counter = 0);
290
291 // Called by keypress_event for cursor up.  Selects previous and next visible
292 // row, skipping desired number of visible rows.
293         int select_previous(int skip,
294                 BC_ListBoxItem *selected_item = 0,
295                 int *counter = 0,
296                 ArrayList<BC_ListBoxItem*> *data = 0,
297                 int *got_it = 0,
298                 int *got_second = 0);
299         int select_next(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
306 // Called by cursor_motion_event to select different item if selection_number
307 // changed.  Returns 1 if redraw is required.
308         int update_selection(ArrayList<BC_ListBoxItem*> *data,
309                 int selection_number,
310                 int *counter = 0);
311
312         static void dump(ArrayList<BC_ListBoxItem*> *data,
313                 int columns,
314                 int indent /* = 0 */,
315                 int master_column);
316
317         int get_item_x(BC_ListBoxItem *item);
318         int get_item_y(BC_ListBoxItem *item);
319         int get_item_w(BC_ListBoxItem *item);
320         int get_item_h(BC_ListBoxItem *item);
321
322 // Draw the list items
323         int draw_items(int flash, int bg_draw=0);
324
325 private:
326         void delete_columns();
327         void set_columns(const char **column_titles,
328                 int *column_widths,
329                 int columns);
330 // Draw the button for a popup listbox
331         int draw_button(int flush);
332 // Draw list border
333         int draw_border(int flash);
334 // Draw column titles
335         void draw_title(int number);
336         int draw_titles(int flash);
337 // Draw expanders
338         void draw_toggles(int flash);
339 // Draw selection rectangle
340         int draw_rectangle(int flash);
341
342
343         void draw_text_recursive(ArrayList<BC_ListBoxItem*> *data,
344                 int column,
345                 int indent,
346                 int *current_toggle);
347 // Returns 1 if selection changed
348         int query_list();
349         void init_column_width();
350         void reset_cursor(int new_cursor);
351 // Fix boundary conditions after resize
352         void column_width_boundaries();
353 // Recursive function to get the first item selected in text mode.
354 // Returns > -1 only if it got it.  Always increments *result
355         int get_first_selection(ArrayList<BC_ListBoxItem*> *data, int *result = 0);
356 // Recursive function to get the last item selected in text mode.
357 // Returns > -1 only if it got it.  Always increments *result
358         int get_last_selection(ArrayList<BC_ListBoxItem*> *data, int *result = 0);
359 // Called by button_press_event and cursor_motion_event to expand the selection.
360 // Returns 1 if redraw is required.
361         int expand_selection(int button_press, int selection_number);
362 // Called by button_press_event and cursor_motion_event
363 // to select a range in text mode
364         void select_range(ArrayList<BC_ListBoxItem*> *data,
365                 int start,
366                 int end,
367                 int *current = 0);
368 // Called by button_press_event to toggle the selection status of a single item.
369 // Called for both text and icon mode.  In text mode it's recursive and fills
370 // the entire row with the first item's value.  Returns 1 when the item was toggled.
371         int toggle_item_selection(ArrayList<BC_ListBoxItem*> *data,
372                 int selection_number,
373                 int *counter = 0);
374 // Set value of selected in all selected items to new value
375         void promote_selections(ArrayList<BC_ListBoxItem*> *data,
376                 int old_value,
377                 int new_value);
378
379
380         int test_column_divisions(int cursor_x, int cursor_y, int &new_cursor);
381         int test_column_titles(int cursor_x, int cursor_y);
382         int test_expanders();
383
384         int calculate_item_coords();
385         void calculate_last_coords_recursive(
386                 ArrayList<BC_ListBoxItem*> *data,
387                 int *icon_x,
388                 int *next_icon_x,
389                 int *next_icon_y,
390                 int *next_text_y,
391                 int top_level);
392         void calculate_item_coords_recursive(
393                 ArrayList<BC_ListBoxItem*> *data,
394                 int *icon_x,
395                 int *next_icon_x,
396                 int *next_icon_y,
397                 int *next_text_y,
398                 int top_level);
399
400         int get_items_width();
401         int get_items_height(ArrayList<BC_ListBoxItem*> *data,
402                 int columns,
403                 int *result = 0);
404         int get_icon_w(BC_ListBoxItem *item);
405         int get_icon_h(BC_ListBoxItem *item);
406         int get_text_w(BC_ListBoxItem *item);
407         int get_text_h(BC_ListBoxItem *item);
408         int get_baseline(BC_ListBoxItem *item);
409         int get_item_highlight(ArrayList<BC_ListBoxItem*> *data, int column, int item);
410         int get_item_color(ArrayList<BC_ListBoxItem*> *data, int column, int item);
411         int get_icon_mask(BC_ListBoxItem *item, int &x, int &y, int &w, int &h);
412         int get_text_mask(BC_ListBoxItem *item, int &x, int &y, int &w, int &h);
413 // Copy sections of the bg_surface to the gui
414         void clear_listbox(int x, int y, int w, int h);
415
416 // Tests for cursor outside boundaries
417         int test_drag_scroll(int cursor_x, int cursor_y);
418 // Called by select_scroll_event, rectangle_scroll_event to execute for movement
419         int drag_scroll_event();
420         int select_scroll_event();
421         int rectangle_scroll_event();
422
423
424         void move_vertical(int pixels);
425         void move_horizontal(int pixels);
426         void clamp_positions();
427
428         int get_scrollbars();
429         void update_scrollbars(int flush);
430
431 // Flat index of the item the cursor is over.
432 // Points *item_return to the first item in the row or 0 if no item was found.
433 // if it's nonzero.  Returns -1 if no item was found.  Clamps the y coordinate
434 // only if the current operation is not SELECT, so scrolling is possible.
435 // expanded - 1 if items in this table should be tested for cursor coverage
436         int get_cursor_item(ArrayList<BC_ListBoxItem*> *data,
437                 int cursor_x,
438                 int cursor_y,
439                 BC_ListBoxItem **item_return = 0,
440                 int *counter = 0,
441                 int expanded = 1);
442 // Select the items in the rectangle and deselect the items outside of it.
443 // Returns 1 if redraw is required.
444         int select_rectangle(ArrayList<BC_ListBoxItem*> *data,
445                 int x1,
446                 int y1,
447                 int x2,
448                 int y2);
449 // Convert the row of the item to a pointer.
450         BC_ListBoxItem* number_to_item(int row);
451         int reposition_item(ArrayList<BC_ListBoxItem*> *data,
452                 int selection_number,
453                 int x,
454                 int y,
455                 int *counter = 0);
456 // Move selected items to src_items
457         void move_selection(ArrayList<BC_ListBoxItem*> *dst,
458                 ArrayList<BC_ListBoxItem*> *src);
459 // Put items from the src table into the data table starting at flat item number
460 // destination.
461         int put_selection(ArrayList<BC_ListBoxItem*> *data,
462                 ArrayList<BC_ListBoxItem*> *src,
463                 int destination,
464                 int *counter = 0);
465
466         int center_selection(int selection,
467                 ArrayList<BC_ListBoxItem*> *data = 0,
468                 int *counter = 0);
469
470 // Array of one list of pointers for each column
471         ArrayList<BC_ListBoxItem*> *data;
472
473
474 // 1 if a button is used to make the listbox display
475         int is_popup;      // popup
476 // If the button for a popup should be shown
477         int use_button;
478 // background update needed
479         int bg_draw;
480
481 // Dimensions for a popup if there is one
482         int popup_w, popup_h;
483 // pixel of top of display relative to top of list
484         int yposition;
485 // pixel of left display relative to first column
486         int xposition;
487 // dimensions of a row in the list
488         int row_height, row_ascent, row_descent;
489
490
491         int selection_mode;
492         int display_format;
493         int temp_display_format;
494         int icon_position;
495 // Scrollbars are created as needed
496         BC_ListBoxXScroll *xscrollbar;
497         BC_ListBoxYScroll *yscrollbar;
498         ArrayList<BC_ListBoxToggle*> expanders;
499         char query[BCTEXTLEN];
500         int show_query;
501
502 // Window containing the listbox
503         BC_WindowBase *gui;
504         int disabled;
505 // Size of the popup if there is one
506         char **column_titles;
507         int *column_width;
508         int default_column_width[1];
509         int columns;
510         int master_column;
511         int search_column;
512
513         int view_h, view_w;
514         int first_in_view, last_in_view;
515         int title_h;
516 // Maximum width of items.  Calculated by get_items_width
517         int items_w;
518         int items_h;
519 // In BCLISTBOX_SELECT mode determines the value to set items to
520         int new_value;
521         int need_xscroll, need_yscroll;
522         int xscroll_orientation, yscroll_orientation;
523 // Move items during drag operation of text items.
524         int process_drag;
525         int allow_drag;
526         int allow_drag_scroll;
527         int allow_drag_column;
528 // Background color of listbox
529         int list_background;
530
531
532 // Popup button
533         BC_Pixmap *button_images[4];
534 // Expander
535         BC_Pixmap *toggle_images[5];
536 // Background for drawing on
537         BC_Pixmap *bg_surface;
538 // Background if 9 segment
539         BC_Pixmap *bg_tile;
540 // Drag icon for text mode
541         VFrame *drag_icon_vframe;
542 // Drag column icon
543         VFrame *drag_column_icon_vframe;
544 // Background picon to be drawn in the upper right
545         BC_Pixmap *bg_pixmap;
546
547
548 // Column title backgrounds
549         BC_Pixmap *column_bg[3];
550 // Column sort order
551         BC_Pixmap *column_sort_up;
552         BC_Pixmap *column_sort_dn;
553
554
555
556
557 // Number of column to sort
558         int sort_column;
559 // Sort order.  -1 means no column is being sorted.
560         int sort_order;
561
562
563
564
565
566 // State of the list box and button when the mouse button is pressed.
567         int current_operation;
568
569         enum
570         {
571                 NO_OPERATION,
572                 BUTTON_DOWN_SELECT, // Pressed button and slid off to select items.
573                 BUTTON_DN,
574                 DRAG_DIVISION,    // Dragging column division
575                 DRAG_COLUMN,      // Dragging column
576                 DRAG_ITEM,        // Dragging item
577                 SELECT,                   // Select item
578                 SELECT_RECT,      // Selection rectangle
579                 WHEEL,                    // Wheel mouse
580                 COLUMN_DN,                // column title down
581                 COLUMN_DRAG,      // column title is being dragged
582                 EXPAND_DN         // Expander is down
583         };
584
585
586 // More state variables
587         int button_highlighted;
588         int list_highlighted;
589 // item cursor is over.  May not exist in tables.
590 // Must be an index since this is needed to change the database.
591         int highlighted_item;
592         BC_ListBoxItem* highlighted_ptr;
593 // column title if the cursor is over a column title
594         int highlighted_title;
595 // Division the cursor is operating on when resizing
596         int highlighted_division;
597 // Column title being dragged
598         int dragged_title;
599 // start of column title drag
600         int drag_cursor_x;
601         int drag_column_w;
602
603 // Selection range being extended
604         int selection_start, selection_end, selection_center;
605 // Item being dragged or last item selected in a double click operation
606         int selection_number;
607 // Used in button_press_event and button_release_event to detect double clicks
608         int selection_number1, selection_number2;
609
610
611
612
613         int active;
614 // Popup listboxes have different behavior for suggestion boxes where the
615 // textbox needs to be active when the listbox is visible.
616         int is_suggestions;
617
618 // Button release counter for double clicking
619         int button_releases;
620         int current_cursor;
621 // Starting coordinates of rectangle
622         int rect_x1, rect_y1;
623         int rect_x2, rect_y2;
624
625
626
627 // Window for dragging
628         BC_DragWindow *drag_popup;
629         int justify;
630 };
631
632
633
634
635 #endif