0d418cb8c4f442b3657d55eea63fb4a87cb42b38
[goodguy/history.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, int recursive)
116 {
117         if(!item && menu_items.size() > 0)
118         {
119                 item = menu_items.get(menu_items.size() - 1);
120         }
121
122         if(item)
123         {
124                 menu_items.remove(item);
125                 item->menu_popup = 0;
126                 if(!recursive) delete item;
127         }
128         return 0;
129 }
130
131 int BC_MenuPopup::total_menuitems()
132 {
133         return menu_items.total;
134 }
135
136 int BC_MenuPopup::dispatch_button_press()
137 {
138         int result = 0;
139         if(popup)
140         {
141                 for(int i = 0; i < menu_items.total && !result; i++)
142                 {
143                         result = menu_items.values[i]->dispatch_button_press();
144                 }
145                 if(result) draw_items();
146         }
147         return 0;
148 }
149
150 int BC_MenuPopup::dispatch_button_release()
151 {
152         int result = 0, redraw = 0;
153         if(popup)
154         {
155                 for(int i = 0; i < menu_items.total && !result; i++)
156                 {
157                         result = menu_items.values[i]->dispatch_button_release(redraw);
158                 }
159                 if(redraw) draw_items();
160         }
161         return result;
162 }
163
164 int BC_MenuPopup::dispatch_key_press()
165 {
166         int result = 0;
167         for(int i = 0; i < menu_items.total && !result; i++)
168         {
169                 result = menu_items.values[i]->dispatch_key_press();
170         }
171         return result;
172 }
173
174 int BC_MenuPopup::dispatch_motion_event()
175 {
176         int i, result = 0, redraw = 0;
177
178         if(popup)
179         {
180 // Try submenus and items
181                 for(i = 0; i < menu_items.total; i++)
182                 {
183                         result |= menu_items.values[i]->dispatch_motion_event(redraw);
184                 }
185
186                 if(redraw) draw_items();
187         }
188
189         return result;
190 }
191
192 int BC_MenuPopup::dispatch_translation_event()
193 {
194         if(popup)
195         {
196                 int new_x = x +
197                         (top_level->last_translate_x - top_level->prev_x -
198                         BC_DisplayInfo::get_left_border());
199                 int new_y = y +
200                         (top_level->last_translate_y - top_level->prev_y -
201                         BC_DisplayInfo::get_top_border());
202
203 // printf("BC_MenuPopup::dispatch_translation_event %d %d %d %d\n",
204 // top_level->prev_x,
205 // top_level->last_translate_x,
206 // top_level->prev_y,
207 // top_level->last_translate_y);
208                 popup->reposition_window(new_x, new_y, popup->get_w(), popup->get_h());
209                 top_level->flush();
210                 this->x = new_x;
211                 this->y = new_y;
212
213                 for(int i = 0; i < menu_items.total; i++)
214                 {
215                         menu_items.values[i]->dispatch_translation_event();
216                 }
217         }
218         return 0;
219 }
220
221
222 int BC_MenuPopup::dispatch_cursor_leave()
223 {
224         int result = 0;
225
226         if(popup)
227         {
228                 for(int i = 0; i < menu_items.total; i++)
229                 {
230                         result |= menu_items.values[i]->dispatch_cursor_leave();
231                 }
232                 if(result) draw_items();
233         }
234         return 0;
235 }
236
237 int BC_MenuPopup::activate_menu(int x,
238         int y,
239         int w,
240         int h,
241         int top_window_coords,
242         int vertical_justify)
243 {
244         Window tempwin;
245         int new_x, new_y;
246         int top_x0 = top_level->get_screen_x(0, -1);
247         int top_y0 = top_level->get_screen_y(0, -1);
248         int top_x1 = top_x0 + top_level->get_screen_w(0, -1);
249         int top_y1 = top_y0 + top_level->get_screen_h(0, -1);
250
251         get_dimensions();
252
253 // Coords are relative to the main window
254         if(top_window_coords)
255                 XTranslateCoordinates(top_level->display,
256                         top_level->win,
257                         top_level->rootwin,
258                         x,
259                         y,
260                         &new_x,
261                         &new_y,
262                         &tempwin);
263         else
264 // Coords are absolute
265         {
266                 new_x = x;
267                 new_y = y;
268         }
269
270 // All coords are now relative to root window.
271         if(vertical_justify)
272         {
273                 this->x = new_x;
274                 this->y = new_y + h;
275                 if( this->x < top_x0 ) this->x = top_x0;
276                 if( this->y < top_y0 ) this->y = top_y0;
277                 if(this->x + this->w > top_x1) this->x -= this->x + this->w - top_x1; // Right justify
278                 if(this->y + this->h > top_y1) this->y -= this->h + h; // Bottom justify
279 // Avoid top of menu going out of screen
280                 if(this->y < 0)
281                         this->y = 2;
282         }
283         else
284         {
285                 this->x = new_x + w;
286                 this->y = new_y;
287                 if( this->x < top_x0 ) this->x = top_x0;
288                 if( this->y < top_y0 ) this->y = top_y0;
289                 if(this->x + this->w > top_x1) this->x = new_x - this->w;
290                 if(this->y + this->h > top_y1) this->y = new_y + h - this->h;
291         }
292         top_x0 += 2;  top_y0 += 2;
293         if( this->x < top_x0 ) this->x = top_x0;
294         if( this->y < top_y0 ) this->y = top_y0;
295
296         active = 1;
297         if(menu_bar)
298         {
299                 popup = new BC_Popup(menu_bar, this->x, this->y, this->w, this->h,
300                                         top_level->get_resources()->menu_up,
301                                         1,
302                                         menu_bar->bg_pixmap);
303         }
304         else
305         {
306                 popup = new BC_Popup(top_level, this->x, this->y, this->w, this->h,
307                                         top_level->get_resources()->menu_up,
308                                         1,
309                                         0);
310 //              popup->set_background(top_level->get_resources()->menu_bg);
311         }
312         draw_items();
313         popup->show_window();
314         return 0;
315 }
316
317 int BC_MenuPopup::deactivate_submenus(BC_MenuPopup *exclude)
318 {
319         for(int i = 0; i < menu_items.total; i++)
320         {
321                 menu_items.values[i]->deactivate_submenus(exclude);
322         }
323         return 0;
324 }
325
326 int BC_MenuPopup::deactivate_menu()
327 {
328         deactivate_submenus(0);
329
330         if(popup) delete popup;
331         popup = 0;
332         active = 0;
333
334         return 0;
335 }
336
337 int BC_MenuPopup::draw_items()
338 {
339         if(menu_bar)
340                 popup->draw_top_tiles(menu_bar, 0, 0, w, h);
341         else
342                 popup->draw_top_tiles(popup, 0, 0, w, h);
343
344         if(window_bg)
345         {
346                 popup->draw_9segment(0, 0, w, h, window_bg);
347         }
348         else
349         {
350                 popup->draw_3d_border(0, 0, w, h,
351                         top_level->get_resources()->menu_light,
352                         top_level->get_resources()->menu_up,
353                         top_level->get_resources()->menu_shadow,
354                         BLACK);
355         }
356
357         for(int i = 0; i < menu_items.total; i++)
358         {
359                 menu_items.values[i]->draw();
360         }
361         popup->flash();
362
363
364         return 0;
365 }
366
367 int BC_MenuPopup::get_dimensions()
368 {
369         int widest_text = 10, widest_key = 10;
370         int text_w, key_w;
371         int i = 0;
372
373 // pad for border
374         h = 2;
375 // Set up parameters in each item and get total h.
376         for(i = 0; i < menu_items.total; i++)
377         {
378                 text_w = 10 + top_level->get_text_width(MEDIUMFONT, menu_items.values[i]->text);
379                 if(menu_items.values[i]->checked) text_w += check->get_w() + 1;
380
381                 key_w = 10 + top_level->get_text_width(MEDIUMFONT, menu_items.values[i]->hotkey_text);
382                 if(text_w > widest_text) widest_text = text_w;
383                 if(key_w > widest_key) widest_key = key_w;
384
385                 if(!strcmp(menu_items.values[i]->text, "-"))
386                         menu_items.values[i]->h = 5;
387                 else
388                 {
389                         menu_items.values[i]->h = item_bg[0] ? item_bg[0]->get_h() :
390                                 top_level->get_text_height(MEDIUMFONT) + 4;
391                 }
392
393                 menu_items.values[i]->y = h;
394                 menu_items.values[i]->highlighted = 0;
395                 menu_items.values[i]->down = 0;
396                 h += menu_items.values[i]->h;
397         }
398         w = widest_text + widest_key + 10;
399
400         w = MAX(w, top_level->get_resources()->min_menu_w);
401 // pad for division
402         key_x = widest_text + 5;
403 // pad for border
404         h += 2;
405         return 0;
406 }
407
408 int BC_MenuPopup::get_key_x()
409 {
410         return key_x;
411 }
412
413 BC_Popup* BC_MenuPopup::get_popup()
414 {
415         return popup;
416 }
417
418 int BC_MenuPopup::cursor_inside()
419 {
420         if( !popup ) return 0;
421         int x = popup->get_relative_cursor_x();
422         if( x < 0 || x > popup->get_w() ) return 0;
423         int y = popup->get_relative_cursor_y();
424         if( y < 0 || y > popup->get_h() ) return 0;
425         return 1;
426 }
427
428 int BC_MenuPopup::get_w()
429 {
430         return w;
431 }
432
433
434
435
436
437 // ================================= Sub Menu ==================================
438
439 BC_SubMenu::BC_SubMenu() : BC_MenuPopup()
440 {
441 }
442
443 BC_SubMenu::~BC_SubMenu()
444 {
445 }
446
447 int BC_SubMenu::add_submenuitem(BC_MenuItem *item)
448 {
449         add_item(item);
450         return 0;
451 }
452