r499: This commit was manufactured by cvs2svn to create tag 'r1_2_1-last'.
[cinelerra_cv.git] / hvirtual / guicast / bcmenu.C
blob043c07f967624eefed19e0fc7277a3198e4c8919
1 #include "bcmenu.h"
2 #include "bcmenubar.h"
3 #include "bcmenuitem.h"
4 #include "bcmenupopup.h"
5 #include "bcresources.h"
7 #include <string.h>
11 // ==================================== Menu ===================================
13 BC_Menu::BC_Menu(char *text)
15         strcpy(this->text, text);
16         menu_bar = 0;
17         active = 0;
18         highlighted = 0;
21 BC_Menu::~BC_Menu()
23         delete menu_popup;
26 int BC_Menu::initialize(BC_WindowBase *top_level, 
27                 BC_MenuBar *menu_bar, 
28                 int x, 
29                 int y, 
30                 int w, 
31                 int h)
33         this->x = x; 
34         this->y = y; 
35         this->w = w; 
36         this->h = h;
37         this->menu_bar = menu_bar;
38         this->top_level = top_level;
39         menu_popup = new BC_MenuPopup;
40         menu_popup->initialize(top_level, menu_bar, this, 0, 0);
41         draw_title();
42         return 0;
45 int BC_Menu::add_item(BC_MenuItem* menuitem)
47         menu_popup->add_item(menuitem);
48         return 0;
51 int BC_Menu::remove_item(BC_MenuItem *item)
53         menu_popup->remove_item(item);
54         return 0;
57 int BC_Menu::total_menuitems()
59         return menu_popup->total_menuitems();
62 int BC_Menu::dispatch_button_press()
64         int result = 0;
66 // Menu is down so dispatch to popup
67         if(active)
68         {
69                 result = menu_popup->dispatch_button_press();
70         }
72 // Try title.
73         if(!result)
74         {
75                 if(top_level->event_win == menu_bar->win &&
76                         top_level->cursor_x >= x && top_level->cursor_x < x + w &&
77                         top_level->cursor_y >= y && top_level->cursor_y < y + h)
78                 {
79                         if(!active)
80                         {
81                                 menu_bar->deactivate();
82                                 menu_bar->unhighlight();
83                                 menu_bar->button_releases = 0;
84                                 menu_bar->activate();
85                                 activate_menu();
86                         }
87                         result = 1;
88                 }
89         }
90         return result;
93 int BC_Menu::dispatch_button_release()
95 // try the title
96         int result = 0;
97         if(top_level->event_win == menu_bar->win &&
98                 top_level->cursor_x >= x && top_level->cursor_y < x + w &&
99                 top_level->cursor_y >= y && top_level->cursor_y < y + h)
100         {
101                 if(menu_bar->button_releases >= 2)
102                 {
103                         highlighted = 1;
104                         menu_bar->deactivate();
105                 }
106                 result = 1;
107         }
108         else
109 // try the popup
110                 result = menu_popup->dispatch_button_release();
111         return result;
114 int BC_Menu::dispatch_keypress()
116         return menu_popup->dispatch_key_press();
119 int BC_Menu::dispatch_motion_event()
121         int result = 0;
122         int cursor_x, cursor_y;
123         Window tempwin;
125 // try the popup
126         if(active)
127         {
128                 result = menu_popup->dispatch_motion_event();
129         }
131         if(!result)
132         {
133                 top_level->translate_coordinates(top_level->event_win, 
134                         menu_bar->win,
135                         top_level->cursor_x,
136                         top_level->cursor_y,
137                         &cursor_x,
138                         &cursor_y);
140 // change focus from other menu
141                 if(menu_bar->active && !active &&
142                         cursor_x >= x && cursor_x < x + w &&
143                         cursor_y >= y && cursor_y < y + h)
144                 {
145                         menu_bar->activate();
146                         activate_menu();
147                         result = 1;
148                 }
149                 else
150 // control highlighting
151                 if(highlighted)
152                 {
153                         if(cursor_x < x || cursor_x >= x + w ||
154                                 cursor_y < y || cursor_y >= y + h)
155                         {
156                                 highlighted = 0;
157                                 draw_title();
158                         }
159                 }
160                 else
161                 {
162                         if(cursor_x >= x && cursor_x < x + w &&
163                                 cursor_y >= y && cursor_y < y + h)
164                         {
165                                 menu_bar->unhighlight();
166                                 highlighted = 1;
167                                 draw_title();
168                                 result = 1;
169                         }
170                 }
171         }
172         return result;
175 int BC_Menu::dispatch_cursor_leave()
177         if(active)
178         {
179                 menu_popup->dispatch_cursor_leave();
180         }
181         unhighlight();
182         return 0;
185 int BC_Menu::dispatch_translation_event()
187         if(active)
188         {
189                 menu_popup->dispatch_translation_event();
190         }
191         return 0;
194 int BC_Menu::activate_menu()
196         Window tempwin;
197         int new_x, new_y, top_w, top_h;
198         if(menu_bar)
199         {
200                 XTranslateCoordinates(top_level->display, 
201                         menu_bar->win, 
202                         top_level->rootwin, 
203                         x, 
204                         y, 
205                         &new_x, 
206                         &new_y, 
207                         &tempwin);
208                 menu_popup->activate_menu(new_x, new_y, w, h, 0, 1);
209         }
210         else
211                 menu_popup->activate_menu(x, y, w, h, 1, 1);
213         active = 1;
214         draw_title();
215         return 0;
218 void BC_Menu::draw_items()
220         if(active) menu_popup->draw_items();
223 int BC_Menu::set_text(char *text)
225         strcpy(this->text, text);
226         draw_title();
227         return 0;
230 int BC_Menu::draw_title()
232         if(active && menu_popup)
233         {
234 // Menu is pulled down and title is recessed.
235                 menu_bar->draw_3d_box(x, y, w, h, 
236                         top_level->get_resources()->menu_shadow, 
237                         BLACK, 
238                         top_level->get_resources()->menu_down,
239                         top_level->get_resources()->menu_down,
240                         top_level->get_resources()->menu_light);
241                 menu_bar->set_color(top_level->get_resources()->menu_item_text);
242         }
243         else
244 // Menu is not pulled down.
245         {
246                 if(highlighted)
247                 {
248                   menu_bar->set_color(top_level->get_resources()->menu_highlighted);
249                   menu_bar->draw_box(x, y, w, h);
250                   menu_bar->set_color(top_level->get_resources()->menu_highlighted_fontcolor);                  
251                 }
252                 else
253                 {
254                   menu_bar->draw_background(x, y, w, h);
255                   menu_bar->set_color(top_level->get_resources()->menu_item_text);
256                 }
257         }
258         menu_bar->set_font(MEDIUMFONT);
259         menu_bar->draw_text(x + 10, h - menu_bar->get_text_descent(MEDIUMFONT), text);
260         menu_bar->flash();
261         menu_bar->flush();
262         return 0;
265 int BC_Menu::deactivate_menu()
267         if(active)
268         {
269                 menu_popup->deactivate_menu();
270                 active = 0;
271                 draw_title();
272         }
273         return 0;
276 int BC_Menu::unhighlight()
278         if(highlighted)
279         {
280                 highlighted = 0;
281                 draw_title();
282         }
283         return 0;