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