r1010: Save mask point coordinates using %g instead of %e to save space.
[cinelerra_cv/ct.git] / guicast / bcmenuitem.C
blob3ebd494fb505b26876f2d2e686d8055bccc84f58
1 #include "bcmenubar.h"
2 #include "bcmenuitem.h"
3 #include "bcmenupopup.h"
4 #include "bcpixmap.h"
5 #include "bcpopup.h"
6 #include "bcpopupmenu.h"
7 #include "bcresources.h"
8 #include "bcwindowbase.h"
9 #include "colors.h"
11 #include <string.h>
14 #define MENUITEM_UP 0
15 #define MENUITEM_HI 1
16 #define MENUITEM_DN 2
19 #define MENUITEM_MARGIN 2
21 // ================================ Menu Item ==================================
23 BC_MenuItem::BC_MenuItem(char *text, char *hotkey_text, int hotkey)
25         reset();
27         if(text) set_text(text);
28         if(hotkey_text) set_hotkey_text(hotkey_text);
30         this->hotkey = hotkey;
31         checked = 0;
32         highlighted = 0;
33         down = 0;
34         submenu = 0;
35         shift_hotkey = 0;
36         alt_hotkey = 0;
37         menu_popup = 0;
40 BC_MenuItem::~BC_MenuItem()
42         if(text) delete [] text;
43         if(hotkey_text) delete [] hotkey_text;
44         if(submenu) delete submenu;
45         if(menu_popup) menu_popup->remove_item(this);
48 void BC_MenuItem::reset()
50         text = new char[1];
51         hotkey_text = new char[1];
52         text[0] = 0;
53         hotkey_text[0] = 0;
54         icon = 0;
57 int BC_MenuItem::initialize(BC_WindowBase *top_level, BC_MenuBar *menu_bar, BC_MenuPopup *menu_popup)
59         this->top_level = top_level;
60         this->menu_popup = menu_popup;
61         this->menu_bar = menu_bar;
62         return 0;
65 int BC_MenuItem::set_checked(int value)
67         this->checked = value;
68         return 0;
71 int BC_MenuItem::get_checked()
73         return checked;
76 void BC_MenuItem::set_icon(BC_Pixmap *icon)
78         this->icon = icon;
81 BC_Pixmap* BC_MenuItem::get_icon()
83         return icon;
86 void BC_MenuItem::set_text(char *text)
88         if(this->text) delete [] this->text;
89         this->text = new char[strlen(text) + 1];
90         strcpy(this->text, text);
93 void BC_MenuItem::set_hotkey_text(char *text)
95         if(this->hotkey_text) delete [] this->hotkey_text;
96         this->hotkey_text = new char[strlen(text) + 1];
97         strcpy(this->hotkey_text, text);
100 int BC_MenuItem::deactivate_submenus(BC_MenuPopup *exclude)
102         if(submenu && submenu != exclude)
103         {
104                 submenu->deactivate_submenus(exclude);
105                 submenu->deactivate_menu();
106                 highlighted = 0;
107         }
108         return 0;
111 int BC_MenuItem::activate_submenu()
113         int new_x, new_y;
114         if(menu_popup->popup && submenu && !submenu->popup)
115         {
116                 Window tempwin;
117                 XTranslateCoordinates(top_level->display, 
118                         menu_popup->get_popup()->win, 
119                         top_level->rootwin, 
120                         0, 
121                         y, 
122                         &new_x, 
123                         &new_y, 
124                         &tempwin);
125                 submenu->activate_menu(new_x + 5, new_y, menu_popup->w - 10, h, 0, 0);
126                 highlighted = 1;
127         }
128         return 0;
132 int BC_MenuItem::dispatch_button_press()
134         int result = 0;
136         if(submenu)
137         {
138                 result = submenu->dispatch_button_press();
139         }
141         if(!result && top_level->event_win == menu_popup->get_popup()->win)
142         {
143                 if(top_level->cursor_x >= 0 && top_level->cursor_x < menu_popup->get_w() &&
144                         top_level->cursor_y >= y && top_level->cursor_y < y + h)
145                 {
146                         if(!highlighted)
147                         {
148                                 highlighted = 1;
149                         }
150                         result = 1;
151                 }
152                 else
153                 if(highlighted)
154                 {
155                         highlighted = 0;
156                         result = 1;
157                 }
158         }
160         return result;
163 int BC_MenuItem::dispatch_button_release(int &redraw)
165         int result = 0;
166         int cursor_x, cursor_y;
167         Window tempwin;
169         if(!strcmp(text, "-")) return 0;
171         if(submenu)
172         {
173                 result = submenu->dispatch_button_release();
174         }
176         if(!result)
177         {
178                 XTranslateCoordinates(top_level->display, 
179                         top_level->event_win, 
180                         menu_popup->get_popup()->win, 
181                         top_level->cursor_x, 
182                         top_level->cursor_y, 
183                         &cursor_x, 
184                         &cursor_y, 
185                         &tempwin);
187                 if(cursor_x >= 0 && cursor_x < menu_popup->get_w() &&
188                         cursor_y >= y && cursor_y < y + h)
189                 {
190                         if(menu_bar)
191                                 menu_bar->deactivate();
192                         else
193                                 menu_popup->popup_menu->deactivate();
195                         if(!handle_event() && menu_popup && menu_popup->popup_menu)
196                         {
197                                 menu_popup->popup_menu->set_text(text);
198                                 menu_popup->popup_menu->handle_event();
199                         }
200                         return 1;
201                 }
202         }
203         return 0;
206 int BC_MenuItem::dispatch_motion_event(int &redraw)
208         int result = 0;
209         int cursor_x, cursor_y;
210         Window tempwin;
212         if(submenu)
213         {
214                 result = submenu->dispatch_motion_event();
215         }
217         top_level->translate_coordinates(top_level->event_win, 
218                 menu_popup->get_popup()->win,
219                 top_level->cursor_x,
220                 top_level->cursor_y,
221                 &cursor_x,
222                 &cursor_y);
224         if(cursor_x >= 0 && cursor_x < menu_popup->get_w() &&
225                 cursor_y >= y && cursor_y < y + h)
226         {
227 // Highlight the item
228                 if(!highlighted)
229                 {
230 // Deactivate submenus in the parent menu excluding this one.
231                         menu_popup->deactivate_submenus(submenu);
232                         highlighted = 1;
233                         if(submenu) activate_submenu();
234                         redraw = 1;
235                 }
236                 result = 1;
237         }
238         else
239         if(highlighted)
240         {
241                 highlighted = 0;
242                 result = 1;
243                 redraw = 1;
244         }
245         return result;
248 int BC_MenuItem::dispatch_translation_event()
250         if(submenu)
251                 submenu->dispatch_translation_event();
252         return 0;
255 int BC_MenuItem::dispatch_cursor_leave()
257         int result = 0;
258         if(submenu)
259         {
260                 result = submenu->dispatch_cursor_leave();
261         }
263         if(!result && highlighted && top_level->event_win == menu_popup->get_popup()->win)
264         {
265                 highlighted = 0;
266                 return 1;
267         }
268         return 0;
271 int BC_MenuItem::dispatch_key_press()
273         int result = 0;
274         if(submenu)
275         {
276                 result = submenu->dispatch_key_press();
277         }
278         
279         if(!result)
280         {
282                 if(top_level->get_keypress() == hotkey && 
283                         shift_hotkey == top_level->shift_down() &&
284                         alt_hotkey == top_level->alt_down())
285                 {
286                         result = 1;
287                         handle_event();
288                 }
289         }
290         return result;
294 int BC_MenuItem::draw()
296         int text_line = top_level->get_text_descent(MEDIUMFONT);
297         BC_Resources *resources = top_level->get_resources();
299         if(!strcmp(text, "-"))
300         {
301                 menu_popup->get_popup()->set_color(DKGREY);
302                 menu_popup->get_popup()->draw_line(5, y + h / 2, menu_popup->get_w() - 5, y + h / 2);
303                 menu_popup->get_popup()->set_color(LTGREY);
304                 menu_popup->get_popup()->draw_line(5, y + h / 2 + 1, menu_popup->get_w() - 5, y + h / 2 + 1);
305         }
306         else
307         {
308                 int offset = 0;
309                 if(highlighted)
310                 {
311                         int y = this->y;
312                         int w = menu_popup->get_w() - 4;
313                         int h = this->h;
315 // Button down
316                         if(top_level->get_button_down() && !submenu)
317                         {
318                                 if(menu_popup->item_bg[MENUITEM_DN])
319                                 {
320                                         menu_popup->get_popup()->draw_9segment(MENUITEM_MARGIN,
321                                                 y,
322                                                 menu_popup->get_w() - MENUITEM_MARGIN * 2,
323                                                 h,
324                                                 menu_popup->item_bg[MENUITEM_DN]);
325                                 }
326                                 else
327                                 {
328                                         menu_popup->get_popup()->draw_3d_box(MENUITEM_MARGIN, 
329                                                 y, 
330                                                 menu_popup->get_w() - MENUITEM_MARGIN * 2, 
331                                                 h, 
332                                                 resources->menu_shadow,
333                                                 BLACK,
334                                                 resources->menu_down,
335                                                 resources->menu_down,
336                                                 resources->menu_light);
337                                 }
338                                 offset = 1;
339                         }
340                         else
341 // Highlighted
342                         {
343                                 if(menu_popup->item_bg[MENUITEM_HI])
344                                 {
345                                         menu_popup->get_popup()->draw_9segment(MENUITEM_MARGIN,
346                                                 y,
347                                                 menu_popup->get_w() - MENUITEM_MARGIN * 2,
348                                                 h,
349                                                 menu_popup->item_bg[MENUITEM_HI]);
350                                 }
351                                 else
352                                 {
353                                         menu_popup->get_popup()->set_color(resources->menu_highlighted);
354                                         menu_popup->get_popup()->draw_box(MENUITEM_MARGIN, 
355                                                 y, 
356                                                 menu_popup->get_w() - MENUITEM_MARGIN * 2, 
357                                                 h);
358                                 }
359                         }
360                         menu_popup->get_popup()->set_color(resources->menu_highlighted_fontcolor);
361                 }
362                 else
363                   {
364                     menu_popup->get_popup()->set_color(resources->menu_item_text);
365                   }
366                 if(checked)
367                 {
368                         menu_popup->get_popup()->draw_check(10 + offset, y + 2 + offset);
369                         menu_popup->get_popup()->set_font(MEDIUMFONT);
370                         menu_popup->get_popup()->draw_text(30 + offset, y + h - text_line - 2 + offset, text);
371                         menu_popup->get_popup()->draw_text(menu_popup->get_key_x() + offset, y + h - text_line - 2 + offset, hotkey_text);
372                 }
373                 else
374                 {
375                         menu_popup->get_popup()->set_font(MEDIUMFONT);
376                         menu_popup->get_popup()->draw_text(10 + offset, y + h - text_line - 2 + offset, text);
377                         menu_popup->get_popup()->draw_text(menu_popup->get_key_x() + offset, y + h - text_line - 2 + offset, hotkey_text);
378                 }
379         }
380         return 0;
384 int BC_MenuItem::add_submenu(BC_SubMenu *submenu)
386         this->submenu = submenu;
387         submenu->initialize(top_level, menu_bar, 0, this, 0);
388         return 0;
391 char* BC_MenuItem::get_text()
393         return text;
396 BC_WindowBase* BC_MenuItem::get_top_level()
398         return top_level;
401 BC_PopupMenu* BC_MenuItem::get_popup_menu()
403         return menu_popup->popup_menu;
406 int BC_MenuItem::set_shift(int value)
408         shift_hotkey = value;
409         return 0;
412 int BC_MenuItem::set_alt(int value)
414         alt_hotkey = value;
415         return 0;