initial commit
[goodguy/history.git] / cinelerra-5.0 / guicast / bcmenu.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 "bcmenu.h"
23 #include "bcmenubar.h"
24 #include "bcmenuitem.h"
25 #include "bcmenupopup.h"
26 #include "bcpixmap.h"
27 #include "bcresources.h"
28 #include "bcsignals.h"
29 #include <string.h>
30
31
32
33 // ==================================== Menu ===================================
34
35 BC_Menu::BC_Menu(const char *text)
36 {
37         strcpy(this->text, text);
38         menu_bar = 0;
39         active = 0;
40         highlighted = 0;
41 }
42
43 BC_Menu::~BC_Menu()
44 {
45         delete menu_popup;
46 }
47
48 int BC_Menu::initialize(BC_WindowBase *top_level, 
49                 BC_MenuBar *menu_bar, 
50                 int x, 
51                 int y, 
52                 int w, 
53                 int h)
54 {
55         this->x = x; 
56         this->y = y; 
57         this->w = w; 
58         this->h = h;
59         this->menu_bar = menu_bar;
60         this->top_level = top_level;
61         menu_popup = new BC_MenuPopup;
62         menu_popup->initialize(top_level, menu_bar, this, 0, 0);
63         draw_title(1, 0);
64         return 0;
65 }
66
67 int BC_Menu::add_item(BC_MenuItem* menuitem)
68 {
69         menu_popup->add_item(menuitem);
70         return 0;
71 }
72
73 int BC_Menu::remove_item(BC_MenuItem *item)
74 {
75         menu_popup->remove_item(item, 0);
76         return 0;
77 }
78
79 int BC_Menu::total_menuitems()
80 {
81         return menu_popup->total_menuitems();
82 }
83
84 int BC_Menu::dispatch_button_press()
85 {
86         int result = 0;
87
88 // Menu is down so dispatch to popup
89         if(active)
90         {
91                 result = menu_popup->dispatch_button_press();
92         }
93
94 // Try title.
95         if(!result)
96         {
97                 if(top_level->event_win == menu_bar->win &&
98                         top_level->cursor_x >= x && top_level->cursor_x < x + w &&
99                         top_level->cursor_y >= y && top_level->cursor_y < y + h)
100                 {
101                         if(!active)
102                         {
103                                 menu_bar->deactivate();
104                                 menu_bar->unhighlight();
105                                 menu_bar->button_releases = 0;
106                                 menu_bar->activate();
107                                 activate_menu();
108                         }
109                         result = 1;
110                 }
111         }
112         return result;
113 }
114
115 int BC_Menu::dispatch_button_release()
116 {
117 // try the title
118         int result = 0;
119         if(top_level->event_win == menu_bar->win &&
120                 top_level->cursor_x >= x && top_level->cursor_y < x + w &&
121                 top_level->cursor_y >= y && top_level->cursor_y < y + h)
122         {
123                 if(menu_bar->button_releases >= 2)
124                 {
125                         highlighted = 1;
126                         menu_bar->deactivate();
127                 }
128                 result = 1;
129         }
130         else
131 // try the popup
132                 result = menu_popup->dispatch_button_release();
133         return result;
134 }
135
136 int BC_Menu::dispatch_keypress()
137 {
138         return menu_popup->dispatch_key_press();
139 }
140
141 int BC_Menu::dispatch_motion_event()
142 {
143         int result = 0;
144         int cursor_x = 0, cursor_y = 0;
145
146 // try the popup
147         if(active)
148         {
149                 result = menu_popup->dispatch_motion_event();
150         }
151
152         if(!result && top_level->match_window(top_level->event_win))
153         {
154                 top_level->translate_coordinates(top_level->event_win, 
155                         menu_bar->win,
156                         top_level->cursor_x,
157                         top_level->cursor_y,
158                         &cursor_x,
159                         &cursor_y);
160
161 // change focus from other menu
162                 if(menu_bar->active && !active &&
163                         cursor_x >= x && cursor_x < x + w &&
164                         cursor_y >= y && cursor_y < y + h)
165                 {
166                         menu_bar->activate();
167                         activate_menu();
168                         result = 1;
169                 }
170                 else
171 // control highlighting
172                 if(highlighted)
173                 {
174                         if(cursor_x < x || cursor_x >= x + w ||
175                                 cursor_y < y || cursor_y >= y + h)
176                         {
177                                 highlighted = 0;
178                                 draw_title(1, 1);
179                         }
180                 }
181                 else
182                 {
183                         if(cursor_x >= x && cursor_x < x + w &&
184                                 cursor_y >= y && cursor_y < y + h)
185                         {
186                                 menu_bar->unhighlight();
187                                 highlighted = 1;
188                                 draw_title(1, 1);
189                                 result = 1;
190                         }
191                 }
192         }
193         return result;
194 }
195
196 int BC_Menu::dispatch_cursor_leave()
197 {
198         if(active)
199         {
200                 menu_popup->dispatch_cursor_leave();
201         }
202         unhighlight();
203         return 0;
204 }
205
206 int BC_Menu::dispatch_translation_event()
207 {
208         if(active)
209         {
210                 menu_popup->dispatch_translation_event();
211         }
212         return 0;
213 }
214
215 int BC_Menu::activate_menu()
216 {
217         Window tempwin;
218         int new_x, new_y;
219         if(menu_bar)
220         {
221                 XTranslateCoordinates(top_level->display, 
222                         menu_bar->win, 
223                         top_level->rootwin, 
224                         x, 
225                         y, 
226                         &new_x, 
227                         &new_y, 
228                         &tempwin);
229                 menu_popup->activate_menu(new_x, new_y, w, h, 0, 1);
230         }
231         else
232                 menu_popup->activate_menu(x, y, w, h, 1, 1);
233
234         active = 1;
235         draw_title(1, 1);
236         return 0;
237 }
238
239 void BC_Menu::draw_items()
240 {
241         if(active) menu_popup->draw_items();
242 }
243
244 int BC_Menu::set_text(char *text)
245 {
246         strcpy(this->text, text);
247         draw_title(1, 1);
248         return 0;
249 }
250
251 int BC_Menu::draw_title(int flash, int flush)
252 {
253         BC_Resources *resources = top_level->get_resources();
254         int text_offset = 0;
255
256         if(active && menu_popup)
257         {
258 // Menu is pulled down and title is recessed.
259
260                 if(menu_bar->menu_title_bg[0])
261                 {
262
263                         menu_bar->draw_9segment(x, 0, w, menu_bar->get_h(), menu_bar->menu_title_bg[2]);
264                 }
265                 else
266                 {
267                         menu_bar->draw_3d_box(x, y, w, h, 
268                                 resources->menu_shadow, 
269                                 BLACK, 
270                                 resources->menu_down,
271                                 resources->menu_down,
272                                 resources->menu_light);
273                 }
274                 text_offset = 1;
275         }
276         else
277 // Menu is not pulled down.
278         {
279                 if(highlighted)
280                 {
281
282                         if(menu_bar->menu_title_bg[0])
283                         {
284
285                                 menu_bar->draw_9segment(x, 0, w, menu_bar->get_h(), menu_bar->menu_title_bg[1]);
286                         }
287                         else
288                         {
289                                 menu_bar->set_color(resources->menu_highlighted);
290                                 menu_bar->draw_box(x, y, w, h);
291                         }
292                 }
293                 else
294                 {
295
296                         if(menu_bar->menu_title_bg[0])
297                         {
298
299                                 menu_bar->draw_9segment(x, 0, w, menu_bar->get_h(), menu_bar->menu_title_bg[0]);
300                         }
301                         else
302                         {
303                                 menu_bar->draw_background(x, y, w, h);
304                         }
305                 }
306         }
307
308         menu_bar->set_color(resources->menu_title_text);
309         menu_bar->set_font(MEDIUMFONT);
310         menu_bar->draw_text(x + 10 + text_offset, 
311                 h - menu_bar->get_text_descent(MEDIUMFONT) + text_offset, 
312                 text);
313         if(flash) menu_bar->flash(flush);
314
315         return 0;
316 }
317
318 int BC_Menu::deactivate_menu()
319 {
320         if(active)
321         {
322                 menu_popup->deactivate_menu();
323                 active = 0;
324                 draw_title(1, 1);
325         }
326         return 0;
327 }
328
329 int BC_Menu::unhighlight()
330 {
331         if(highlighted)
332         {
333                 highlighted = 0;
334                 draw_title(1, 1);
335         }
336         return 0;
337 }