4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
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.
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.
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
22 #include "bcmenubar.h"
23 #include "bcmenuitem.h"
24 #include "bcmenupopup.h"
27 #include "bcpopupmenu.h"
28 #include "bcresources.h"
29 #include "bcsignals.h"
30 #include "bcwindowbase.h"
42 #define MENUITEM_MARGIN 2
44 // ================================ Menu Item ==================================
46 BC_MenuItem::BC_MenuItem(const char *text, const char *hotkey_text, int hotkey)
49 this->hotkey_text = 0;
52 if(text) set_text(text);
53 if(hotkey_text) set_hotkey_text(hotkey_text);
55 this->hotkey = hotkey;
66 BC_MenuItem::~BC_MenuItem()
68 if(text) delete [] text;
70 if(hotkey_text) delete [] hotkey_text;
72 if(submenu) delete submenu;
75 menu_popup->remove_item(this);
78 void BC_MenuItem::reset()
85 int BC_MenuItem::initialize(BC_WindowBase *top_level, BC_MenuBar *menu_bar, BC_MenuPopup *menu_popup)
87 this->top_level = top_level;
88 this->menu_popup = menu_popup;
89 this->menu_bar = menu_bar;
93 int BC_MenuItem::set_checked(int value)
95 this->checked = value;
99 int BC_MenuItem::get_checked()
104 void BC_MenuItem::set_icon(BC_Pixmap *icon)
109 BC_Pixmap* BC_MenuItem::get_icon()
114 void BC_MenuItem::set_text(const char *text)
116 delete [] this->text;
117 this->text = cstrdup(text);
120 void BC_MenuItem::set_hotkey_text(const char *text)
122 delete [] this->hotkey_text;
123 this->hotkey_text = cstrdup(text);
126 int BC_MenuItem::deactivate_submenus(BC_MenuPopup *exclude)
128 if(submenu && submenu != exclude)
130 submenu->deactivate_submenus(exclude);
131 submenu->deactivate_menu();
132 submenu->popup_menu = 0;
138 int BC_MenuItem::activate_submenu()
141 if(menu_popup->popup && submenu && !submenu->popup)
144 XTranslateCoordinates(top_level->display,
145 menu_popup->get_popup()->win,
152 submenu->popup_menu = menu_popup->popup_menu;
153 submenu->activate_menu(new_x + 5, new_y, menu_popup->w - 10, h, 0, 0);
160 int BC_MenuItem::dispatch_button_press()
166 result = submenu->dispatch_button_press();
169 if(!result && menu_popup->get_popup()->is_event_win())
171 if(top_level->cursor_x >= 0 && top_level->cursor_x < menu_popup->get_w() &&
172 top_level->cursor_y >= y && top_level->cursor_y < y + h)
191 int BC_MenuItem::dispatch_button_release(int &redraw)
195 if(!strcmp(text, "-")) return 0;
199 result = submenu->dispatch_button_release();
202 if( !result && menu_popup->cursor_inside() ) {
203 int cursor_x, cursor_y;
204 menu_popup->get_popup()->get_relative_cursor(cursor_x, cursor_y);
205 if( cursor_x >= 0 && cursor_x < menu_popup->get_w() &&
206 cursor_y >= y && cursor_y < y + h ) {
208 menu_bar->deactivate();
210 menu_popup->popup_menu->deactivate();
212 if(!handle_event() && menu_popup && menu_popup->popup_menu)
214 menu_popup->popup_menu->set_text(text);
215 menu_popup->popup_menu->handle_event();
223 int BC_MenuItem::dispatch_motion_event(int &redraw)
229 result = submenu->dispatch_motion_event();
232 if( !result && menu_popup->cursor_inside() ) {
233 int cursor_x, cursor_y;
234 menu_popup->get_popup()->get_relative_cursor(cursor_x, cursor_y);
235 if( cursor_x >= 0 && cursor_x < menu_popup->get_w() &&
236 cursor_y >= y && cursor_y < y + h) {
237 // Highlight the item
240 // Deactivate submenus in the parent menu excluding this one.
241 menu_popup->deactivate_submenus(submenu);
243 if(submenu) activate_submenu();
259 int BC_MenuItem::dispatch_translation_event()
262 submenu->dispatch_translation_event();
266 int BC_MenuItem::dispatch_cursor_leave()
271 result = submenu->dispatch_cursor_leave();
274 if(!result && highlighted && menu_popup->get_popup()->is_event_win())
282 int BC_MenuItem::dispatch_key_press()
287 result = submenu->dispatch_key_press();
293 if(top_level->get_keypress() == hotkey &&
294 shift_hotkey == top_level->shift_down() &&
295 alt_hotkey == top_level->alt_down() &&
296 ctrl_hotkey == top_level->ctrl_down())
306 int BC_MenuItem::draw()
308 int text_line = top_level->get_text_descent(MEDIUMFONT);
309 BC_Resources *resources = top_level->get_resources();
311 if(!strcmp(text, "-"))
313 menu_popup->get_popup()->set_color(DKGREY);
314 menu_popup->get_popup()->draw_line(5, y + h / 2, menu_popup->get_w() - 5, y + h / 2);
315 menu_popup->get_popup()->set_color(LTGREY);
316 menu_popup->get_popup()->draw_line(5, y + h / 2 + 1, menu_popup->get_w() - 5, y + h / 2 + 1);
324 //int w = menu_popup->get_w() - 4;
328 if(top_level->get_button_down() && !submenu)
330 if(menu_popup->item_bg[MENUITEM_DN])
332 // menu_popup->get_popup()->draw_9segment(MENUITEM_MARGIN,
334 // menu_popup->get_w() - MENUITEM_MARGIN * 2,
336 // menu_popup->item_bg[MENUITEM_DN]);
337 menu_popup->get_popup()->draw_3segmenth(MENUITEM_MARGIN,
339 menu_popup->get_w() - MENUITEM_MARGIN * 2,
340 menu_popup->item_bg[MENUITEM_DN]);
344 menu_popup->get_popup()->draw_3d_box(MENUITEM_MARGIN,
346 menu_popup->get_w() - MENUITEM_MARGIN * 2,
348 resources->menu_shadow,
350 resources->menu_down,
351 resources->menu_down,
352 resources->menu_light);
359 if(menu_popup->item_bg[MENUITEM_HI])
361 // menu_popup->get_popup()->draw_9segment(MENUITEM_MARGIN,
363 // menu_popup->get_w() - MENUITEM_MARGIN * 2,
365 // menu_popup->item_bg[MENUITEM_HI]);
366 menu_popup->get_popup()->draw_3segmenth(MENUITEM_MARGIN,
368 menu_popup->get_w() - MENUITEM_MARGIN * 2,
369 menu_popup->item_bg[MENUITEM_HI]);
373 menu_popup->get_popup()->set_color(resources->menu_highlighted);
374 menu_popup->get_popup()->draw_box(MENUITEM_MARGIN,
376 menu_popup->get_w() - MENUITEM_MARGIN * 2,
380 menu_popup->get_popup()->set_color(resources->menu_highlighted_fontcolor);
384 menu_popup->get_popup()->set_color(resources->menu_item_text);
388 // menu_popup->get_popup()->draw_check(10 + offset, y + 2 + offset);
389 menu_popup->get_popup()->draw_pixmap(menu_popup->check,
391 y + (this->h - menu_popup->check->get_h()) / 2 + offset);
392 menu_popup->get_popup()->set_font(MEDIUMFONT);
393 menu_popup->get_popup()->draw_text(menu_popup->check->get_w() + offset,
394 y + h - text_line - 2 + offset,
396 menu_popup->get_popup()->draw_text(menu_popup->get_key_x() + offset,
397 y + h - text_line - 2 + offset,
402 menu_popup->get_popup()->set_font(MEDIUMFONT);
403 menu_popup->get_popup()->draw_text(10 + offset, y + h - text_line - 2 + offset, text);
404 menu_popup->get_popup()->draw_text(menu_popup->get_key_x() + offset, y + h - text_line - 2 + offset, hotkey_text);
411 int BC_MenuItem::add_submenu(BC_SubMenu *submenu)
413 this->submenu = submenu;
414 submenu->initialize(top_level, menu_bar, 0, this, 0);
418 BC_SubMenu* BC_MenuItem::get_submenu()
423 char* BC_MenuItem::get_text()
428 BC_WindowBase* BC_MenuItem::get_top_level()
433 BC_PopupMenu* BC_MenuItem::get_popup_menu()
435 return menu_popup->popup_menu;
438 int BC_MenuItem::set_shift(int value)
440 shift_hotkey = value;
444 int BC_MenuItem::set_alt(int value)
450 void BC_MenuItem::set_ctrl(int value)