2 #include "bcmenuitem.h"
3 #include "bcmenupopup.h"
6 #include "bcresources.h"
7 #include "bcwindowbase.h"
14 // ==================================== Menu Popup =============================
16 // Types of menu popups
17 #define MENUPOPUP_MENUBAR 0
18 #define MENUPOPUP_SUBMENU 1
19 #define MENUPOPUP_POPUP 2
21 BC_MenuPopup::BC_MenuPopup()
29 BC_MenuPopup::~BC_MenuPopup()
31 while(menu_items.total)
33 // Each menuitem recursively removes itself from the arraylist
34 delete menu_items.values[0];
42 int BC_MenuPopup::initialize(BC_WindowBase *top_level,
45 BC_MenuItem *menu_item,
46 BC_PopupMenu *popup_menu)
51 this->menu_bar = menu_bar;
52 this->menu_item = menu_item;
53 this->popup_menu = popup_menu;
54 this->top_level = top_level;
56 if(menu_item) this->type = MENUPOPUP_SUBMENU;
58 if(menu) this->type = MENUPOPUP_MENUBAR;
60 if(popup_menu) this->type = MENUPOPUP_POPUP;
62 BC_Resources *resources = top_level->get_resources();
63 if(resources->menu_popup_bg)
65 window_bg = new BC_Pixmap(top_level, resources->menu_popup_bg);
67 if(resources->menu_item_bg)
69 item_bg[0] = new BC_Pixmap(top_level, resources->menu_item_bg[0], PIXMAP_ALPHA);
70 item_bg[1] = new BC_Pixmap(top_level, resources->menu_item_bg[1], PIXMAP_ALPHA);
71 item_bg[2] = new BC_Pixmap(top_level, resources->menu_item_bg[2], PIXMAP_ALPHA);
77 int BC_MenuPopup::add_item(BC_MenuItem *item)
79 menu_items.append(item);
80 item->initialize(top_level, menu_bar, this);
84 int BC_MenuPopup::remove_item(BC_MenuItem *item)
88 item = menu_items.values[menu_items.total - 1];
91 if(item) menu_items.remove(item);
95 int BC_MenuPopup::total_menuitems()
97 return menu_items.total;
100 int BC_MenuPopup::dispatch_button_press()
105 for(int i = 0; i < menu_items.total && !result; i++)
107 result = menu_items.values[i]->dispatch_button_press();
109 if(result) draw_items();
114 int BC_MenuPopup::dispatch_button_release()
116 int result = 0, redraw = 0;
119 for(int i = 0; i < menu_items.total && !result; i++)
121 result = menu_items.values[i]->dispatch_button_release(redraw);
123 if(redraw) draw_items();
128 int BC_MenuPopup::dispatch_key_press()
131 for(int i = 0; i < menu_items.total && !result; i++)
133 result = menu_items.values[i]->dispatch_key_press();
138 int BC_MenuPopup::dispatch_motion_event()
140 int i, result = 0, redraw = 0;
145 // Try submenus and items
146 for(i = 0; i < menu_items.total; i++)
148 result |= menu_items.values[i]->dispatch_motion_event(redraw);
151 if(redraw) draw_items();
157 int BC_MenuPopup::dispatch_translation_event()
162 (top_level->last_translate_x -
164 top_level->get_resources()->get_left_border());
166 (top_level->last_translate_y -
168 top_level->get_resources()->get_top_border());
170 // printf("BC_MenuPopup::dispatch_translation_event %d %d %d %d\n",
171 // top_level->prev_x,
172 // top_level->last_translate_x,
173 // top_level->prev_y,
174 // top_level->last_translate_y);
175 popup->reposition_window(new_x, new_y, popup->get_w(), popup->get_h());
180 for(int i = 0; i < menu_items.total; i++)
182 menu_items.values[i]->dispatch_translation_event();
189 int BC_MenuPopup::dispatch_cursor_leave()
195 for(int i = 0; i < menu_items.total; i++)
197 result |= menu_items.values[i]->dispatch_cursor_leave();
199 if(result) draw_items();
204 int BC_MenuPopup::activate_menu(int x,
208 int top_window_coords,
209 int vertical_justify)
212 int new_x, new_y, top_w, top_h;
213 top_w = top_level->get_root_w(1, 0);
214 top_h = top_level->get_root_h(0);
218 // Coords are relative to the main window
219 if(top_window_coords)
220 XTranslateCoordinates(top_level->display,
229 // Coords are absolute
235 // All coords are now relative to root window.
240 if(this->x + this->w > top_w) this->x -= this->x + this->w - top_w; // Right justify
241 if(this->y + this->h > top_h) this->y -= this->h + h; // Bottom justify
247 if(this->x + this->w > top_w) this->x = new_x - this->w;
248 if(this->y + this->h > top_h) this->y = new_y + h - this->h;
254 popup = new BC_Popup(menu_bar,
259 top_level->get_resources()->menu_up,
261 menu_bar->bg_pixmap);
265 popup = new BC_Popup(top_level,
270 top_level->get_resources()->menu_up,
273 // popup->set_background(top_level->get_resources()->menu_bg);
276 popup->show_window();
280 int BC_MenuPopup::deactivate_submenus(BC_MenuPopup *exclude)
282 for(int i = 0; i < menu_items.total; i++)
284 menu_items.values[i]->deactivate_submenus(exclude);
289 int BC_MenuPopup::deactivate_menu()
291 deactivate_submenus(0);
293 if(popup) delete popup;
300 int BC_MenuPopup::draw_items()
303 popup->draw_top_tiles(menu_bar, 0, 0, w, h);
305 popup->draw_top_tiles(popup, 0, 0, w, h);
309 popup->draw_9segment(0,
317 popup->draw_3d_border(0, 0, w, h,
318 top_level->get_resources()->menu_light,
319 top_level->get_resources()->menu_up,
320 top_level->get_resources()->menu_shadow,
324 for(int i = 0; i < menu_items.total; i++)
326 menu_items.values[i]->draw();
334 int BC_MenuPopup::get_dimensions()
336 int widest_text = 10, widest_key = 10;
342 // Set up parameters in each item and get total h.
343 for(i = 0; i < menu_items.total; i++)
345 text_w = 10 + top_level->get_text_width(MEDIUMFONT, menu_items.values[i]->text);
346 if(menu_items.values[i]->checked) text_w += 20;
348 key_w = 10 + top_level->get_text_width(MEDIUMFONT, menu_items.values[i]->hotkey_text);
349 if(text_w > widest_text) widest_text = text_w;
350 if(key_w > widest_key) widest_key = key_w;
352 if(!strcmp(menu_items.values[i]->text, "-"))
353 menu_items.values[i]->h = 5;
355 menu_items.values[i]->h = top_level->get_text_height(MEDIUMFONT) + 4;
357 menu_items.values[i]->y = h;
358 menu_items.values[i]->highlighted = 0;
359 menu_items.values[i]->down = 0;
360 h += menu_items.values[i]->h;
362 w = widest_text + widest_key + 10;
364 w = MAX(w, top_level->get_resources()->min_menu_w);
366 key_x = widest_text + 5;
372 int BC_MenuPopup::get_key_x()
377 BC_Popup* BC_MenuPopup::get_popup()
382 int BC_MenuPopup::get_w()
391 // ================================= Sub Menu ==================================
393 BC_SubMenu::BC_SubMenu() : BC_MenuPopup()
397 BC_SubMenu::~BC_SubMenu()
401 int BC_SubMenu::add_submenuitem(BC_MenuItem *item)
410 // c-file-style: "linux"