2 #include "bcmenuitem.h"
3 #include "bcmenupopup.h"
5 #include "bcresources.h"
6 #include "bcwindowbase.h"
13 // ==================================== Menu Popup =============================
15 // Types of menu popups
16 #define MENUPOPUP_MENUBAR 0
17 #define MENUPOPUP_SUBMENU 1
18 #define MENUPOPUP_POPUP 2
20 BC_MenuPopup::BC_MenuPopup()
24 BC_MenuPopup::~BC_MenuPopup()
26 while(menu_items.total)
28 // Each menuitem recursively removes itself from the arraylist
29 delete menu_items.values[0];
33 int BC_MenuPopup::initialize(BC_WindowBase *top_level,
36 BC_MenuItem *menu_item,
37 BC_PopupMenu *popup_menu)
42 this->menu_bar = menu_bar;
43 this->menu_item = menu_item;
44 this->popup_menu = popup_menu;
45 this->top_level = top_level;
47 if(menu_item) this->type = MENUPOPUP_SUBMENU;
49 if(menu) this->type = MENUPOPUP_MENUBAR;
51 if(popup_menu) this->type = MENUPOPUP_POPUP;
56 int BC_MenuPopup::add_item(BC_MenuItem *item)
58 menu_items.append(item);
59 item->initialize(top_level, menu_bar, this);
63 int BC_MenuPopup::remove_item(BC_MenuItem *item)
67 item = menu_items.values[menu_items.total - 1];
70 if(item) menu_items.remove(item);
74 int BC_MenuPopup::total_menuitems()
76 return menu_items.total;
79 int BC_MenuPopup::dispatch_button_press()
84 for(int i = 0; i < menu_items.total && !result; i++)
86 result = menu_items.values[i]->dispatch_button_press();
88 if(result) draw_items();
93 int BC_MenuPopup::dispatch_button_release()
95 int result = 0, redraw = 0;
98 for(int i = 0; i < menu_items.total && !result; i++)
100 result = menu_items.values[i]->dispatch_button_release(redraw);
102 if(redraw) draw_items();
107 int BC_MenuPopup::dispatch_key_press()
110 for(int i = 0; i < menu_items.total && !result; i++)
112 result = menu_items.values[i]->dispatch_key_press();
117 int BC_MenuPopup::dispatch_motion_event()
119 int i, result = 0, redraw = 0;
124 // Try submenus and items
125 for(i = 0; i < menu_items.total; i++)
127 result |= menu_items.values[i]->dispatch_motion_event(redraw);
130 if(redraw) draw_items();
136 int BC_MenuPopup::dispatch_translation_event()
141 (top_level->last_translate_x -
143 top_level->get_resources()->get_left_border());
145 (top_level->last_translate_y -
147 top_level->get_resources()->get_top_border());
149 // printf("BC_MenuPopup::dispatch_translation_event %d %d %d %d\n",
150 // top_level->prev_x,
151 // top_level->last_translate_x,
152 // top_level->prev_y,
153 // top_level->last_translate_y);
154 popup->reposition_window(new_x, new_y, popup->get_w(), popup->get_h());
159 for(int i = 0; i < menu_items.total; i++)
161 menu_items.values[i]->dispatch_translation_event();
168 int BC_MenuPopup::dispatch_cursor_leave()
174 for(int i = 0; i < menu_items.total; i++)
176 result |= menu_items.values[i]->dispatch_cursor_leave();
178 if(result) draw_items();
183 int BC_MenuPopup::activate_menu(int x,
187 int top_window_coords,
188 int vertical_justify)
191 int new_x, new_y, top_w, top_h;
192 top_w = top_level->get_root_w(1, 0);
193 top_h = top_level->get_root_h(0);
197 // Coords are relative to the main window
198 if(top_window_coords)
199 XTranslateCoordinates(top_level->display,
208 // Coords are absolute
214 // All coords are now relative to root window.
219 if(this->x + this->w > top_w) this->x -= this->x + this->w - top_w; // Right justify
220 if(this->y + this->h > top_h) this->y -= this->h + h; // Bottom justify
226 if(this->x + this->w > top_w) this->x = new_x - this->w;
227 if(this->y + this->h > top_h) this->y = new_y + h - this->h;
233 popup = new BC_Popup(menu_bar,
238 top_level->get_resources()->menu_up,
240 menu_bar->bg_pixmap);
244 popup = new BC_Popup(top_level,
249 top_level->get_resources()->menu_up,
252 // popup->set_background(top_level->get_resources()->menu_bg);
255 popup->show_window();
259 int BC_MenuPopup::deactivate_submenus(BC_MenuPopup *exclude)
261 for(int i = 0; i < menu_items.total; i++)
263 menu_items.values[i]->deactivate_submenus(exclude);
268 int BC_MenuPopup::deactivate_menu()
270 deactivate_submenus(0);
272 if(popup) delete popup;
279 int BC_MenuPopup::draw_items()
282 popup->draw_top_tiles(menu_bar, 0, 0, w, h);
284 popup->draw_top_tiles(popup, 0, 0, w, h);
286 popup->draw_3d_border(0, 0, w, h,
287 top_level->get_resources()->menu_light,
288 top_level->get_resources()->menu_up,
289 top_level->get_resources()->menu_shadow,
292 for(int i = 0; i < menu_items.total; i++)
294 menu_items.values[i]->draw();
297 //printf("BC_MenuPopup::draw_items 1\n");
301 int BC_MenuPopup::get_dimensions()
303 int widest_text = 10, widest_key = 10;
309 // Set up parameters in each item and get total h.
310 for(i = 0; i < menu_items.total; i++)
312 text_w = 10 + top_level->get_text_width(MEDIUMFONT, menu_items.values[i]->text);
313 if(menu_items.values[i]->checked) text_w += 20;
315 key_w = 10 + top_level->get_text_width(MEDIUMFONT, menu_items.values[i]->hotkey_text);
316 if(text_w > widest_text) widest_text = text_w;
317 if(key_w > widest_key) widest_key = key_w;
319 if(!strcmp(menu_items.values[i]->text, "-"))
320 menu_items.values[i]->h = 5;
322 menu_items.values[i]->h = top_level->get_text_height(MEDIUMFONT) + 4;
324 menu_items.values[i]->y = h;
325 menu_items.values[i]->highlighted = 0;
326 menu_items.values[i]->down = 0;
327 h += menu_items.values[i]->h;
329 w = widest_text + widest_key + 10;
331 key_x = widest_text + 5;
337 int BC_MenuPopup::get_key_x()
342 BC_Popup* BC_MenuPopup::get_popup()
347 int BC_MenuPopup::get_w()
356 // ================================= Sub Menu ==================================
358 BC_SubMenu::BC_SubMenu() : BC_MenuPopup()
362 BC_SubMenu::~BC_SubMenu()
366 int BC_SubMenu::add_submenuitem(BC_MenuItem *item)