r1009: Move the dependencies to newer package names
[cinelerra_cv/mob.git] / guicast / bcmenu.C
blobf9e11134989bc256d444df6d0e37997da699153c
1 #include "bcmenu.h"
2 #include "bcmenubar.h"
3 #include "bcmenuitem.h"
4 #include "bcmenupopup.h"
5 #include "bcpixmap.h"
6 #include "bcresources.h"
7 #include "bcsignals.h"
8 #include <string.h>
12 // ==================================== Menu ===================================
14 BC_Menu::BC_Menu(char *text)
16         strcpy(this->text, text);
17         menu_bar = 0;
18         active = 0;
19         highlighted = 0;
22 BC_Menu::~BC_Menu()
24         delete menu_popup;
27 int BC_Menu::initialize(BC_WindowBase *top_level, 
28                 BC_MenuBar *menu_bar, 
29                 int x, 
30                 int y, 
31                 int w, 
32                 int h)
34         this->x = x; 
35         this->y = y; 
36         this->w = w; 
37         this->h = h;
38         this->menu_bar = menu_bar;
39         this->top_level = top_level;
40         menu_popup = new BC_MenuPopup;
41         menu_popup->initialize(top_level, menu_bar, this, 0, 0);
42         draw_title();
43         return 0;
46 int BC_Menu::add_item(BC_MenuItem* menuitem)
48         menu_popup->add_item(menuitem);
49         return 0;
52 int BC_Menu::remove_item(BC_MenuItem *item)
54         menu_popup->remove_item(item);
55         return 0;
58 int BC_Menu::total_menuitems()
60         return menu_popup->total_menuitems();
63 int BC_Menu::dispatch_button_press()
65         int result = 0;
67 // Menu is down so dispatch to popup
68         if(active)
69         {
70                 result = menu_popup->dispatch_button_press();
71         }
73 // Try title.
74         if(!result)
75         {
76                 if(top_level->event_win == menu_bar->win &&
77                         top_level->cursor_x >= x && top_level->cursor_x < x + w &&
78                         top_level->cursor_y >= y && top_level->cursor_y < y + h)
79                 {
80                         if(!active)
81                         {
82                                 menu_bar->deactivate();
83                                 menu_bar->unhighlight();
84                                 menu_bar->button_releases = 0;
85                                 menu_bar->activate();
86                                 activate_menu();
87                         }
88                         result = 1;
89                 }
90         }
91         return result;
94 int BC_Menu::dispatch_button_release()
96 // try the title
97         int result = 0;
98         if(top_level->event_win == menu_bar->win &&
99                 top_level->cursor_x >= x && top_level->cursor_y < x + w &&
100                 top_level->cursor_y >= y && top_level->cursor_y < y + h)
101         {
102                 if(menu_bar->button_releases >= 2)
103                 {
104                         highlighted = 1;
105                         menu_bar->deactivate();
106                 }
107                 result = 1;
108         }
109         else
110 // try the popup
111                 result = menu_popup->dispatch_button_release();
112         return result;
115 int BC_Menu::dispatch_keypress()
117         return menu_popup->dispatch_key_press();
120 int BC_Menu::dispatch_motion_event()
122         int result = 0;
123         int cursor_x, cursor_y;
124         Window tempwin;
126 // try the popup
127         if(active)
128         {
129                 result = menu_popup->dispatch_motion_event();
130         }
132         if(!result)
133         {
134                 top_level->translate_coordinates(top_level->event_win, 
135                         menu_bar->win,
136                         top_level->cursor_x,
137                         top_level->cursor_y,
138                         &cursor_x,
139                         &cursor_y);
141 // change focus from other menu
142                 if(menu_bar->active && !active &&
143                         cursor_x >= x && cursor_x < x + w &&
144                         cursor_y >= y && cursor_y < y + h)
145                 {
146                         menu_bar->activate();
147                         activate_menu();
148                         result = 1;
149                 }
150                 else
151 // control highlighting
152                 if(highlighted)
153                 {
154                         if(cursor_x < x || cursor_x >= x + w ||
155                                 cursor_y < y || cursor_y >= y + h)
156                         {
157                                 highlighted = 0;
158                                 draw_title();
159                         }
160                 }
161                 else
162                 {
163                         if(cursor_x >= x && cursor_x < x + w &&
164                                 cursor_y >= y && cursor_y < y + h)
165                         {
166                                 menu_bar->unhighlight();
167                                 highlighted = 1;
168                                 draw_title();
169                                 result = 1;
170                         }
171                 }
172         }
173         return result;
176 int BC_Menu::dispatch_cursor_leave()
178         if(active)
179         {
180                 menu_popup->dispatch_cursor_leave();
181         }
182         unhighlight();
183         return 0;
186 int BC_Menu::dispatch_translation_event()
188         if(active)
189         {
190                 menu_popup->dispatch_translation_event();
191         }
192         return 0;
195 int BC_Menu::activate_menu()
197         Window tempwin;
198         int new_x, new_y, top_w, top_h;
199         if(menu_bar)
200         {
201                 XTranslateCoordinates(top_level->display, 
202                         menu_bar->win, 
203                         top_level->rootwin, 
204                         x, 
205                         y, 
206                         &new_x, 
207                         &new_y, 
208                         &tempwin);
209                 menu_popup->activate_menu(new_x, new_y, w, h, 0, 1);
210         }
211         else
212                 menu_popup->activate_menu(x, y, w, h, 1, 1);
214         active = 1;
215         draw_title();
216         return 0;
219 void BC_Menu::draw_items()
221         if(active) menu_popup->draw_items();
224 int BC_Menu::set_text(char *text)
226         strcpy(this->text, text);
227         draw_title();
228         return 0;
231 int BC_Menu::draw_title()
233         BC_Resources *resources = top_level->get_resources();
234         int text_offset = 0;
236         if(active && menu_popup)
237         {
238 // Menu is pulled down and title is recessed.
240                 if(menu_bar->menu_title_bg[0])
241                 {
243                         menu_bar->draw_9segment(x, 0, w, menu_bar->get_h(), menu_bar->menu_title_bg[2]);
244                 }
245                 else
246                 {
247                         menu_bar->draw_3d_box(x, y, w, h, 
248                                 resources->menu_shadow, 
249                                 BLACK, 
250                                 resources->menu_down,
251                                 resources->menu_down,
252                                 resources->menu_light);
253                 }
254                 text_offset = 1;
255         }
256         else
257 // Menu is not pulled down.
258         {
259                 if(highlighted)
260                 {
262                         if(menu_bar->menu_title_bg[0])
263                         {
265                                 menu_bar->draw_9segment(x, 0, w, menu_bar->get_h(), menu_bar->menu_title_bg[1]);
266                         }
267                         else
268                         {
269                                 menu_bar->set_color(resources->menu_highlighted);
270                                 menu_bar->draw_box(x, y, w, h);
271                         }
272                 }
273                 else
274                 {
276                         if(menu_bar->menu_title_bg[0])
277                         {
279                                 menu_bar->draw_9segment(x, 0, w, menu_bar->get_h(), menu_bar->menu_title_bg[0]);
280                         }
281                         else
282                         {
283                                 menu_bar->draw_background(x, y, w, h);
284                         }
285                 }
286         }
288         menu_bar->set_color(resources->menu_title_text);
289         menu_bar->set_font(MEDIUMFONT);
290         menu_bar->draw_text(x + 10 + text_offset, 
291                 h - menu_bar->get_text_descent(MEDIUMFONT) + text_offset, 
292                 text);
293         menu_bar->flash();
295         return 0;
298 int BC_Menu::deactivate_menu()
300         if(active)
301         {
302                 menu_popup->deactivate_menu();
303                 active = 0;
304                 draw_title();
305         }
306         return 0;
309 int BC_Menu::unhighlight()
311         if(highlighted)
312         {
313                 highlighted = 0;
314                 draw_title();
315         }
316         return 0;