2 #include "bcmenuitem.h"
3 #include "bcmenupopup.h"
6 #include "bcpopupmenu.h"
7 #include "bcresources.h"
8 #include "bcwindowbase.h"
19 #define MENUITEM_MARGIN 2
21 // ================================ Menu Item ==================================
23 BC_MenuItem::BC_MenuItem(char *text, char *hotkey_text, int hotkey)
27 if(text) set_text(text);
28 if(hotkey_text) set_hotkey_text(hotkey_text);
30 this->hotkey = hotkey;
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()
51 hotkey_text = new char[1];
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;
65 int BC_MenuItem::set_checked(int value)
67 this->checked = value;
71 int BC_MenuItem::get_checked()
76 void BC_MenuItem::set_icon(BC_Pixmap *icon)
81 BC_Pixmap* BC_MenuItem::get_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)
104 submenu->deactivate_submenus(exclude);
105 submenu->deactivate_menu();
111 int BC_MenuItem::activate_submenu()
114 if(menu_popup->popup && submenu && !submenu->popup)
117 XTranslateCoordinates(top_level->display,
118 menu_popup->get_popup()->win,
125 submenu->activate_menu(new_x + 5, new_y, menu_popup->w - 10, h, 0, 0);
132 int BC_MenuItem::dispatch_button_press()
138 result = submenu->dispatch_button_press();
141 if(!result && top_level->event_win == menu_popup->get_popup()->win)
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)
163 int BC_MenuItem::dispatch_button_release(int &redraw)
166 int cursor_x, cursor_y;
169 if(!strcmp(text, "-")) return 0;
173 result = submenu->dispatch_button_release();
178 XTranslateCoordinates(top_level->display,
179 top_level->event_win,
180 menu_popup->get_popup()->win,
187 if(cursor_x >= 0 && cursor_x < menu_popup->get_w() &&
188 cursor_y >= y && cursor_y < y + h)
191 menu_bar->deactivate();
193 menu_popup->popup_menu->deactivate();
195 if(!handle_event() && menu_popup && menu_popup->popup_menu)
197 menu_popup->popup_menu->set_text(text);
198 menu_popup->popup_menu->handle_event();
206 int BC_MenuItem::dispatch_motion_event(int &redraw)
209 int cursor_x, cursor_y;
214 result = submenu->dispatch_motion_event();
217 top_level->translate_coordinates(top_level->event_win,
218 menu_popup->get_popup()->win,
224 if(cursor_x >= 0 && cursor_x < menu_popup->get_w() &&
225 cursor_y >= y && cursor_y < y + h)
227 // Highlight the item
230 // Deactivate submenus in the parent menu excluding this one.
231 menu_popup->deactivate_submenus(submenu);
233 if(submenu) activate_submenu();
248 int BC_MenuItem::dispatch_translation_event()
251 submenu->dispatch_translation_event();
255 int BC_MenuItem::dispatch_cursor_leave()
260 result = submenu->dispatch_cursor_leave();
263 if(!result && highlighted && top_level->event_win == menu_popup->get_popup()->win)
271 int BC_MenuItem::dispatch_key_press()
276 result = submenu->dispatch_key_press();
282 if(top_level->get_keypress() == hotkey &&
283 shift_hotkey == top_level->shift_down() &&
284 alt_hotkey == top_level->alt_down())
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, "-"))
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);
312 int w = menu_popup->get_w() - 4;
316 if(top_level->get_button_down() && !submenu)
318 if(menu_popup->item_bg[MENUITEM_DN])
320 menu_popup->get_popup()->draw_9segment(MENUITEM_MARGIN,
322 menu_popup->get_w() - MENUITEM_MARGIN * 2,
324 menu_popup->item_bg[MENUITEM_DN]);
328 menu_popup->get_popup()->draw_3d_box(MENUITEM_MARGIN,
330 menu_popup->get_w() - MENUITEM_MARGIN * 2,
332 resources->menu_shadow,
334 resources->menu_down,
335 resources->menu_down,
336 resources->menu_light);
343 if(menu_popup->item_bg[MENUITEM_HI])
345 menu_popup->get_popup()->draw_9segment(MENUITEM_MARGIN,
347 menu_popup->get_w() - MENUITEM_MARGIN * 2,
349 menu_popup->item_bg[MENUITEM_HI]);
353 menu_popup->get_popup()->set_color(resources->menu_highlighted);
354 menu_popup->get_popup()->draw_box(MENUITEM_MARGIN,
356 menu_popup->get_w() - MENUITEM_MARGIN * 2,
360 menu_popup->get_popup()->set_color(resources->menu_highlighted_fontcolor);
364 menu_popup->get_popup()->set_color(resources->menu_item_text);
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);
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);
384 int BC_MenuItem::add_submenu(BC_SubMenu *submenu)
386 this->submenu = submenu;
387 submenu->initialize(top_level, menu_bar, 0, this, 0);
391 char* BC_MenuItem::get_text()
396 BC_WindowBase* BC_MenuItem::get_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;
412 int BC_MenuItem::set_alt(int value)