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"
41 #define MENUITEM_MARGIN 2
43 // ================================ Menu Item ==================================
45 BC_MenuItem::BC_MenuItem(const char *text, const char *hotkey_text, int hotkey)
49 if(text) set_text(text);
50 if(hotkey_text) set_hotkey_text(hotkey_text);
52 this->hotkey = hotkey;
63 BC_MenuItem::~BC_MenuItem()
65 if(text) delete [] text;
67 if(hotkey_text) delete [] hotkey_text;
69 if(submenu) delete submenu;
72 menu_popup->remove_item(this);
75 void BC_MenuItem::reset()
78 hotkey_text = new char[1];
84 int BC_MenuItem::initialize(BC_WindowBase *top_level, BC_MenuBar *menu_bar, BC_MenuPopup *menu_popup)
86 this->top_level = top_level;
87 this->menu_popup = menu_popup;
88 this->menu_bar = menu_bar;
92 int BC_MenuItem::set_checked(int value)
94 this->checked = value;
98 int BC_MenuItem::get_checked()
103 void BC_MenuItem::set_icon(BC_Pixmap *icon)
108 BC_Pixmap* BC_MenuItem::get_icon()
113 void BC_MenuItem::set_text(const char *text)
115 if(this->text) delete [] this->text;
116 this->text = new char[strlen(text) + 1];
117 strcpy(this->text, text);
120 void BC_MenuItem::set_hotkey_text(const char *text)
122 if(this->hotkey_text) delete [] this->hotkey_text;
123 this->hotkey_text = new char[strlen(text) + 1];
124 strcpy(this->hotkey_text, text);
127 int BC_MenuItem::deactivate_submenus(BC_MenuPopup *exclude)
129 if(submenu && submenu != exclude)
131 submenu->deactivate_submenus(exclude);
132 submenu->deactivate_menu();
133 submenu->popup_menu = 0;
139 int BC_MenuItem::activate_submenu()
142 if(menu_popup->popup && submenu && !submenu->popup)
145 XTranslateCoordinates(top_level->display,
146 menu_popup->get_popup()->win,
153 submenu->popup_menu = menu_popup->popup_menu;
154 submenu->activate_menu(new_x + 5, new_y, menu_popup->w - 10, h, 0, 0);
161 int BC_MenuItem::dispatch_button_press()
167 result = submenu->dispatch_button_press();
170 if(!result && top_level->event_win == menu_popup->get_popup()->win)
172 if(top_level->cursor_x >= 0 && top_level->cursor_x < menu_popup->get_w() &&
173 top_level->cursor_y >= y && top_level->cursor_y < y + h)
192 int BC_MenuItem::dispatch_button_release(int &redraw)
195 int cursor_x, cursor_y;
198 if(!strcmp(text, "-")) return 0;
202 result = submenu->dispatch_button_release();
207 XTranslateCoordinates(top_level->display,
208 top_level->event_win,
209 menu_popup->get_popup()->win,
216 if(cursor_x >= 0 && cursor_x < menu_popup->get_w() &&
217 cursor_y >= y && cursor_y < y + h)
220 menu_bar->deactivate();
222 menu_popup->popup_menu->deactivate();
224 if(!handle_event() && menu_popup && menu_popup->popup_menu)
226 menu_popup->popup_menu->set_text(text);
227 menu_popup->popup_menu->handle_event();
235 int BC_MenuItem::dispatch_motion_event(int &redraw)
238 int cursor_x, cursor_y;
242 result = submenu->dispatch_motion_event();
245 top_level->translate_coordinates(top_level->event_win,
246 menu_popup->get_popup()->win,
252 if(cursor_x >= 0 && cursor_x < menu_popup->get_w() &&
253 cursor_y >= y && cursor_y < y + h)
255 // Highlight the item
258 // Deactivate submenus in the parent menu excluding this one.
259 menu_popup->deactivate_submenus(submenu);
261 if(submenu) activate_submenu();
276 int BC_MenuItem::dispatch_translation_event()
279 submenu->dispatch_translation_event();
283 int BC_MenuItem::dispatch_cursor_leave()
288 result = submenu->dispatch_cursor_leave();
291 if(!result && highlighted && top_level->event_win == menu_popup->get_popup()->win)
299 int BC_MenuItem::dispatch_key_press()
304 result = submenu->dispatch_key_press();
310 if(top_level->get_keypress() == hotkey &&
311 shift_hotkey == top_level->shift_down() &&
312 alt_hotkey == top_level->alt_down() &&
313 ctrl_hotkey == top_level->ctrl_down())
323 int BC_MenuItem::draw()
325 int text_line = top_level->get_text_descent(MEDIUMFONT);
326 BC_Resources *resources = top_level->get_resources();
328 if(!strcmp(text, "-"))
330 menu_popup->get_popup()->set_color(DKGREY);
331 menu_popup->get_popup()->draw_line(5, y + h / 2, menu_popup->get_w() - 5, y + h / 2);
332 menu_popup->get_popup()->set_color(LTGREY);
333 menu_popup->get_popup()->draw_line(5, y + h / 2 + 1, menu_popup->get_w() - 5, y + h / 2 + 1);
341 //int w = menu_popup->get_w() - 4;
345 if(top_level->get_button_down() && !submenu)
347 if(menu_popup->item_bg[MENUITEM_DN])
349 // menu_popup->get_popup()->draw_9segment(MENUITEM_MARGIN,
351 // menu_popup->get_w() - MENUITEM_MARGIN * 2,
353 // menu_popup->item_bg[MENUITEM_DN]);
354 menu_popup->get_popup()->draw_3segmenth(MENUITEM_MARGIN,
356 menu_popup->get_w() - MENUITEM_MARGIN * 2,
357 menu_popup->item_bg[MENUITEM_DN]);
361 menu_popup->get_popup()->draw_3d_box(MENUITEM_MARGIN,
363 menu_popup->get_w() - MENUITEM_MARGIN * 2,
365 resources->menu_shadow,
367 resources->menu_down,
368 resources->menu_down,
369 resources->menu_light);
376 if(menu_popup->item_bg[MENUITEM_HI])
378 // menu_popup->get_popup()->draw_9segment(MENUITEM_MARGIN,
380 // menu_popup->get_w() - MENUITEM_MARGIN * 2,
382 // menu_popup->item_bg[MENUITEM_HI]);
383 menu_popup->get_popup()->draw_3segmenth(MENUITEM_MARGIN,
385 menu_popup->get_w() - MENUITEM_MARGIN * 2,
386 menu_popup->item_bg[MENUITEM_HI]);
390 menu_popup->get_popup()->set_color(resources->menu_highlighted);
391 menu_popup->get_popup()->draw_box(MENUITEM_MARGIN,
393 menu_popup->get_w() - MENUITEM_MARGIN * 2,
397 menu_popup->get_popup()->set_color(resources->menu_highlighted_fontcolor);
401 menu_popup->get_popup()->set_color(resources->menu_item_text);
405 // menu_popup->get_popup()->draw_check(10 + offset, y + 2 + offset);
406 menu_popup->get_popup()->draw_pixmap(menu_popup->check,
408 y + (this->h - menu_popup->check->get_h()) / 2 + offset);
409 menu_popup->get_popup()->set_font(MEDIUMFONT);
410 menu_popup->get_popup()->draw_text(menu_popup->check->get_w() + offset,
411 y + h - text_line - 2 + offset,
413 menu_popup->get_popup()->draw_text(menu_popup->get_key_x() + offset,
414 y + h - text_line - 2 + offset,
419 menu_popup->get_popup()->set_font(MEDIUMFONT);
420 menu_popup->get_popup()->draw_text(10 + offset, y + h - text_line - 2 + offset, text);
421 menu_popup->get_popup()->draw_text(menu_popup->get_key_x() + offset, y + h - text_line - 2 + offset, hotkey_text);
428 int BC_MenuItem::add_submenu(BC_SubMenu *submenu)
430 this->submenu = submenu;
431 submenu->initialize(top_level, menu_bar, 0, this, 0);
435 BC_SubMenu* BC_MenuItem::get_submenu()
440 char* BC_MenuItem::get_text()
445 BC_WindowBase* BC_MenuItem::get_top_level()
450 BC_PopupMenu* BC_MenuItem::get_popup_menu()
452 return menu_popup->popup_menu;
455 int BC_MenuItem::set_shift(int value)
457 shift_hotkey = value;
461 int BC_MenuItem::set_alt(int value)
467 void BC_MenuItem::set_ctrl(int value)