upgrade libvpx+lv2, fix dbl tap play bug, add multi nest/unnest clips, add del top...
[goodguy/cinelerra.git] / cinelerra-5.1 / guicast / bcmenupopup.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "bcdisplayinfo.h"
23 #include "bcmenubar.h"
24 #include "bcmenuitem.h"
25 #include "bcmenupopup.h"
26 #include "bcpixmap.h"
27 #include "bcpopup.h"
28 #include "bcresources.h"
29 #include "bcwindowbase.h"
30 #include "clip.h"
31
32 #include <string.h>
33
34
35
36 // ==================================== Menu Popup =============================
37
38 // Types of menu popups
39 #define MENUPOPUP_MENUBAR 0
40 #define MENUPOPUP_SUBMENU 1
41 #define MENUPOPUP_POPUP   2
42
43 BC_MenuPopup::BC_MenuPopup()
44 {
45         window_bg = 0;
46         item_bg[0] = 0;
47         item_bg[1] = 0;
48         item_bg[2] = 0;
49         check = 0;
50 }
51
52 BC_MenuPopup::~BC_MenuPopup()
53 {
54         while(menu_items.size())
55         {
56 // Each menuitem recursively removes itself from the arraylist
57                 delete menu_items.get(0);
58         }
59
60         delete window_bg;
61         delete item_bg[0];
62         delete item_bg[1];
63         delete item_bg[2];
64         delete check;
65 }
66
67 int BC_MenuPopup::initialize(BC_WindowBase *top_level,
68                 BC_MenuBar *menu_bar,
69                 BC_Menu *menu,
70                 BC_MenuItem *menu_item,
71                 BC_PopupMenu *popup_menu)
72 {
73         popup = 0;
74         active = 0;
75         this->menu = menu;
76         this->menu_bar = menu_bar;
77         this->menu_item = menu_item;
78         this->popup_menu = popup_menu;
79         this->top_level = top_level;
80
81         if(menu_item) this->type = MENUPOPUP_SUBMENU;
82         else
83         if(menu) this->type = MENUPOPUP_MENUBAR;
84         else
85         if(popup_menu) this->type = MENUPOPUP_POPUP;
86
87         BC_Resources *resources = top_level->get_resources();
88         if(resources->menu_popup_bg)
89         {
90                 window_bg = new BC_Pixmap(top_level, resources->menu_popup_bg);
91         }
92
93         if(resources->menu_item_bg)
94         {
95                 item_bg[0] = new BC_Pixmap(top_level, resources->menu_item_bg[0], PIXMAP_ALPHA);
96                 item_bg[1] = new BC_Pixmap(top_level, resources->menu_item_bg[1], PIXMAP_ALPHA);
97                 item_bg[2] = new BC_Pixmap(top_level, resources->menu_item_bg[2], PIXMAP_ALPHA);
98         }
99
100         if(resources->check)
101         {
102                 check = new BC_Pixmap(top_level, resources->check, PIXMAP_ALPHA);
103         }
104
105         return 0;
106 }
107
108 int BC_MenuPopup::add_item(BC_MenuItem *item)
109 {
110         menu_items.append(item);
111         item->initialize(top_level, menu_bar, this);
112         return 0;
113 }
114
115 int BC_MenuPopup::remove_item(BC_MenuItem *item)
116 {
117         menu_items.remove(item);
118         return 0;
119 }
120
121 int BC_MenuPopup::del_item(BC_MenuItem *item)
122 {
123         if(!item && menu_items.size() > 0)
124         {
125                 item = menu_items.get(menu_items.size() - 1);
126         }
127
128         if(item)
129         {
130                 remove_item(item);
131                 item->menu_popup = 0;
132                 delete item;
133         }
134         return 0;
135 }
136
137 BC_MenuItem *BC_MenuPopup::get_item(int i)
138 {
139         return menu_items[i];
140 }
141
142 int BC_MenuPopup::total_items()
143 {
144         return menu_items.size();
145 }
146
147 int BC_MenuPopup::dispatch_button_press()
148 {
149         int result = 0;
150         if(popup)
151         {
152                 for(int i = 0; i < menu_items.total && !result && popup; i++)
153                 {
154                         result = menu_items.values[i]->dispatch_button_press();
155                 }
156                 if(result) draw_items();
157         }
158         return 0;
159 }
160
161 int BC_MenuPopup::dispatch_button_release()
162 {
163         int result = 0, redraw = 0;
164         if(popup)
165         {
166                 for(int i = 0; i < menu_items.total && !result && popup; i++)
167                 {
168                         result = menu_items.values[i]->dispatch_button_release(redraw);
169                 }
170                 if(redraw) draw_items();
171         }
172         return result;
173 }
174
175 int BC_MenuPopup::dispatch_key_press()
176 {
177         int result = 0;
178         for(int i = 0; i < menu_items.total && !result; i++)
179         {
180                 result = menu_items.values[i]->dispatch_key_press();
181         }
182         return result;
183 }
184
185 int BC_MenuPopup::dispatch_motion_event()
186 {
187         int i, result = 0, redraw = 0;
188
189         if(popup)
190         {
191 // Try submenus and items
192                 for(i = 0; i < menu_items.total; i++)
193                 {
194                         result |= menu_items.values[i]->dispatch_motion_event(redraw);
195                 }
196
197                 if(redraw) draw_items();
198         }
199
200         return result;
201 }
202
203 int BC_MenuPopup::dispatch_translation_event()
204 {
205         if(popup)
206         {
207                 int new_x = x +
208                         (top_level->last_translate_x - top_level->prev_x -
209                         BC_DisplayInfo::get_left_border());
210                 int new_y = y +
211                         (top_level->last_translate_y - top_level->prev_y -
212                         BC_DisplayInfo::get_top_border());
213
214 // printf("BC_MenuPopup::dispatch_translation_event %d %d %d %d\n",
215 // top_level->prev_x,
216 // top_level->last_translate_x,
217 // top_level->prev_y,
218 // top_level->last_translate_y);
219                 popup->reposition_window(new_x, new_y, popup->get_w(), popup->get_h());
220                 top_level->flush();
221                 this->x = new_x;
222                 this->y = new_y;
223
224                 for(int i = 0; i < menu_items.total; i++)
225                 {
226                         menu_items.values[i]->dispatch_translation_event();
227                 }
228         }
229         return 0;
230 }
231
232
233 int BC_MenuPopup::dispatch_cursor_leave()
234 {
235         int result = 0;
236
237         if(popup)
238         {
239                 for(int i = 0; i < menu_items.total; i++)
240                 {
241                         result |= menu_items.values[i]->dispatch_cursor_leave();
242                 }
243                 if(result) draw_items();
244         }
245         return 0;
246 }
247
248 int BC_MenuPopup::activate_menu(int x,
249         int y,
250         int w,
251         int h,
252         int top_window_coords,
253         int vertical_justify)
254 {
255         Window tempwin;
256         int new_x, new_y;
257         int top_x0 = top_level->get_screen_x(0, -1);
258         int top_y0 = top_level->get_screen_y(0, -1);
259         int top_x1 = top_x0 + top_level->get_screen_w(0, -1);
260         int top_y1 = top_y0 + top_level->get_screen_h(0, -1);
261
262         get_dimensions();
263
264 // Coords are relative to the main window
265         if(top_window_coords)
266                 XTranslateCoordinates(top_level->display,
267                         top_level->win,
268                         top_level->rootwin,
269                         x,
270                         y,
271                         &new_x,
272                         &new_y,
273                         &tempwin);
274         else
275 // Coords are absolute
276         {
277                 new_x = x;
278                 new_y = y;
279         }
280
281 // All coords are now relative to root window.
282         if(vertical_justify)
283         {
284                 this->x = new_x;
285                 this->y = new_y + h;
286                 if( this->x < top_x0 ) this->x = top_x0;
287                 if( this->y < top_y0 ) this->y = top_y0;
288                 if(this->x + this->w > top_x1) this->x -= this->x + this->w - top_x1; // Right justify
289                 if(this->y + this->h > top_y1) this->y -= this->h + h; // Bottom justify
290 // Avoid top of menu going out of screen
291                 if(this->y < 0)
292                         this->y = 2;
293         }
294         else
295         {
296                 this->x = new_x + w;
297                 this->y = new_y;
298                 if( this->x < top_x0 ) this->x = top_x0;
299                 if( this->y < top_y0 ) this->y = top_y0;
300                 if(this->x + this->w > top_x1) this->x = new_x - this->w;
301                 if(this->y + this->h > top_y1) this->y = new_y + h - this->h;
302         }
303         top_x0 += 2;  top_y0 += 2;
304         if( this->x < top_x0 ) this->x = top_x0;
305         if( this->y < top_y0 ) this->y = top_y0;
306
307         active = 1;
308         if(menu_bar)
309         {
310                 popup = new BC_Popup(menu_bar, this->x, this->y, this->w, this->h,
311                                         top_level->get_resources()->menu_up,
312                                         1,
313                                         menu_bar->bg_pixmap);
314         }
315         else
316         {
317                 popup = new BC_Popup(top_level, this->x, this->y, this->w, this->h,
318                                         top_level->get_resources()->menu_up,
319                                         1,
320                                         0);
321 //              popup->set_background(top_level->get_resources()->menu_bg);
322         }
323         draw_items();
324         popup->show_window();
325         return 0;
326 }
327
328 int BC_MenuPopup::deactivate_submenus(BC_MenuPopup *exclude)
329 {
330         for(int i = 0; i < menu_items.total; i++)
331         {
332                 menu_items.values[i]->deactivate_submenus(exclude);
333         }
334         return 0;
335 }
336
337 int BC_MenuPopup::deactivate_menu()
338 {
339         deactivate_submenus(0);
340
341         if(popup) delete popup;
342         popup = 0;
343         active = 0;
344
345         return 0;
346 }
347
348 int BC_MenuPopup::draw_items()
349 {
350         if(menu_bar)
351                 popup->draw_top_tiles(menu_bar, 0, 0, w, h);
352         else
353                 popup->draw_top_tiles(popup, 0, 0, w, h);
354
355         if(window_bg)
356         {
357                 popup->draw_9segment(0, 0, w, h, window_bg);
358         }
359         else
360         {
361                 popup->draw_3d_border(0, 0, w, h,
362                         top_level->get_resources()->menu_light,
363                         top_level->get_resources()->menu_up,
364                         top_level->get_resources()->menu_shadow,
365                         BLACK);
366         }
367
368         for(int i = 0; i < menu_items.total; i++)
369         {
370                 menu_items.values[i]->draw();
371         }
372         popup->flash();
373
374
375         return 0;
376 }
377
378 int BC_MenuPopup::get_dimensions()
379 {
380         int widest_text = 10, widest_key = 10;
381         int text_w, key_w;
382         int i = 0;
383
384 // pad for border
385         h = 2;
386 // Set up parameters in each item and get total h.
387         for(i = 0; i < menu_items.total; i++)
388         {
389                 text_w = 10 + top_level->get_text_width(MEDIUMFONT, menu_items.values[i]->text);
390                 if(menu_items.values[i]->checked) text_w += check->get_w() + 1;
391
392                 key_w = 10 + top_level->get_text_width(MEDIUMFONT, menu_items.values[i]->hotkey_text);
393                 if(text_w > widest_text) widest_text = text_w;
394                 if(key_w > widest_key) widest_key = key_w;
395
396                 if(!strcmp(menu_items.values[i]->text, "-"))
397                         menu_items.values[i]->h = 5;
398                 else
399                 {
400                         menu_items.values[i]->h = item_bg[0] ? item_bg[0]->get_h() :
401                                 top_level->get_text_height(MEDIUMFONT) + 4;
402                 }
403
404                 menu_items.values[i]->y = h;
405                 menu_items.values[i]->highlighted = 0;
406                 menu_items.values[i]->down = 0;
407                 h += menu_items.values[i]->h;
408         }
409         w = widest_text + widest_key + 10;
410
411         w = MAX(w, top_level->get_resources()->min_menu_w);
412 // pad for division
413         key_x = widest_text + 5;
414 // pad for border
415         h += 2;
416         return 0;
417 }
418
419 int BC_MenuPopup::get_key_x()
420 {
421         return key_x;
422 }
423
424 BC_Popup* BC_MenuPopup::get_popup()
425 {
426         return popup;
427 }
428
429 int BC_MenuPopup::cursor_inside()
430 {
431         if( !popup ) return 0;
432         if( popup->cursor_above() ) return 1;
433         for( int i=0; i<menu_items.size(); ++i ) {
434                 if( !menu_items[i]->submenu ) continue;
435                 if( menu_items[i]->submenu->cursor_inside() ) return 1;
436         }
437         return 0;
438 }
439
440 int BC_MenuPopup::get_w()
441 {
442         return w;
443 }
444
445
446
447
448
449 // ================================= Sub Menu ==================================
450
451 BC_SubMenu::BC_SubMenu() : BC_MenuPopup()
452 {
453 }
454
455 BC_SubMenu::~BC_SubMenu()
456 {
457 }
458
459 int BC_SubMenu::add_submenuitem(BC_MenuItem *item)
460 {
461         add_item(item);
462         return 0;
463 }
464