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 "bcdisplayinfo.h"
23 #include "bcmenubar.h"
24 #include "bcmenuitem.h"
25 #include "bcmenupopup.h"
28 #include "bcresources.h"
29 #include "bcwindowbase.h"
36 // ==================================== Menu Popup =============================
38 // Types of menu popups
39 #define MENUPOPUP_MENUBAR 0
40 #define MENUPOPUP_SUBMENU 1
41 #define MENUPOPUP_POPUP 2
43 BC_MenuPopup::BC_MenuPopup()
52 BC_MenuPopup::~BC_MenuPopup()
54 while(menu_items.size())
56 // Each menuitem recursively removes itself from the arraylist
57 delete menu_items.get(0);
67 int BC_MenuPopup::initialize(BC_WindowBase *top_level,
70 BC_MenuItem *menu_item,
71 BC_PopupMenu *popup_menu)
76 this->menu_bar = menu_bar;
77 this->menu_item = menu_item;
78 this->popup_menu = popup_menu;
79 this->top_level = top_level;
81 if(menu_item) this->type = MENUPOPUP_SUBMENU;
83 if(menu) this->type = MENUPOPUP_MENUBAR;
85 if(popup_menu) this->type = MENUPOPUP_POPUP;
87 BC_Resources *resources = top_level->get_resources();
88 if(resources->menu_popup_bg)
90 window_bg = new BC_Pixmap(top_level, resources->menu_popup_bg);
93 if(resources->menu_item_bg)
95 item_bg[0] = new BC_Pixmap(top_level, resources->menu_item_bg[0], PIXMAP_ALPHA);
96 item_bg[1] = new BC_Pixmap(top_level, resources->menu_item_bg[1], PIXMAP_ALPHA);
97 item_bg[2] = new BC_Pixmap(top_level, resources->menu_item_bg[2], PIXMAP_ALPHA);
102 check = new BC_Pixmap(top_level, resources->check, PIXMAP_ALPHA);
108 int BC_MenuPopup::add_item(BC_MenuItem *item)
110 menu_items.append(item);
111 item->initialize(top_level, menu_bar, this);
115 int BC_MenuPopup::remove_item(BC_MenuItem *item)
117 menu_items.remove(item);
121 int BC_MenuPopup::del_item(BC_MenuItem *item)
123 if(!item && menu_items.size() > 0)
125 item = menu_items.get(menu_items.size() - 1);
131 item->menu_popup = 0;
137 BC_MenuItem *BC_MenuPopup::get_item(int i)
139 return menu_items[i];
142 int BC_MenuPopup::total_items()
144 return menu_items.size();
147 int BC_MenuPopup::dispatch_button_press()
152 for(int i = 0; i < menu_items.total && !result && popup; i++)
154 result = menu_items.values[i]->dispatch_button_press();
156 if(result) draw_items();
161 int BC_MenuPopup::dispatch_button_release()
163 int result = 0, redraw = 0;
166 for(int i = 0; i < menu_items.total && !result && popup; i++)
168 result = menu_items.values[i]->dispatch_button_release(redraw);
170 if(redraw) draw_items();
175 int BC_MenuPopup::dispatch_key_press()
178 for(int i = 0; i < menu_items.total && !result; i++)
180 result = menu_items.values[i]->dispatch_key_press();
185 int BC_MenuPopup::dispatch_motion_event()
187 int i, result = 0, redraw = 0;
191 // Try submenus and items
192 for(i = 0; i < menu_items.total; i++)
194 result |= menu_items.values[i]->dispatch_motion_event(redraw);
197 if(redraw) draw_items();
203 int BC_MenuPopup::dispatch_translation_event()
208 (top_level->last_translate_x - top_level->prev_x -
209 BC_DisplayInfo::get_left_border());
211 (top_level->last_translate_y - top_level->prev_y -
212 BC_DisplayInfo::get_top_border());
214 // printf("BC_MenuPopup::dispatch_translation_event %d %d %d %d\n",
215 // top_level->prev_x,
216 // top_level->last_translate_x,
217 // top_level->prev_y,
218 // top_level->last_translate_y);
219 popup->reposition_window(new_x, new_y, popup->get_w(), popup->get_h());
224 for(int i = 0; i < menu_items.total; i++)
226 menu_items.values[i]->dispatch_translation_event();
233 int BC_MenuPopup::dispatch_cursor_leave()
239 for(int i = 0; i < menu_items.total; i++)
241 result |= menu_items.values[i]->dispatch_cursor_leave();
243 if(result) draw_items();
248 int BC_MenuPopup::activate_menu(int x,
252 int top_window_coords,
253 int vertical_justify)
257 int top_x0 = top_level->get_screen_x(0, -1);
258 int top_y0 = top_level->get_screen_y(0, -1);
259 int top_x1 = top_x0 + top_level->get_screen_w(0, -1);
260 int top_y1 = top_y0 + top_level->get_screen_h(0, -1);
264 // Coords are relative to the main window
265 if(top_window_coords)
266 XTranslateCoordinates(top_level->display,
275 // Coords are absolute
281 // All coords are now relative to root window.
286 if( this->x < top_x0 ) this->x = top_x0;
287 if( this->y < top_y0 ) this->y = top_y0;
288 if(this->x + this->w > top_x1) this->x -= this->x + this->w - top_x1; // Right justify
289 if(this->y + this->h > top_y1) this->y -= this->h + h; // Bottom justify
290 // Avoid top of menu going out of screen
298 if( this->x < top_x0 ) this->x = top_x0;
299 if( this->y < top_y0 ) this->y = top_y0;
300 if(this->x + this->w > top_x1) this->x = new_x - this->w;
301 if(this->y + this->h > top_y1) this->y = new_y + h - this->h;
303 top_x0 += xS(2); top_y0 += yS(2);
304 if( this->x < top_x0 ) this->x = top_x0;
305 if( this->y < top_y0 ) this->y = top_y0;
310 popup = new BC_Popup(menu_bar, this->x, this->y, this->w, this->h,
311 top_level->get_resources()->menu_up, 1, menu_bar->bg_pixmap);
315 popup = new BC_Popup(top_level, this->x, this->y, this->w, this->h,
316 top_level->get_resources()->menu_up, 1, 0);
317 // popup->set_background(top_level->get_resources()->menu_bg);
320 popup->show_window();
324 int BC_MenuPopup::deactivate_submenus(BC_MenuPopup *exclude)
326 for(int i = 0; i < menu_items.total; i++)
328 menu_items.values[i]->deactivate_submenus(exclude);
333 int BC_MenuPopup::deactivate_menu()
335 deactivate_submenus(0);
337 if(popup) delete popup;
344 int BC_MenuPopup::draw_items()
347 popup->draw_top_tiles(menu_bar, 0, 0, w, h);
349 popup->draw_top_tiles(popup, 0, 0, w, h);
353 popup->draw_9segment(0, 0, w, h, window_bg);
357 popup->draw_3d_border(0, 0, w, h,
358 top_level->get_resources()->menu_light,
359 top_level->get_resources()->menu_up,
360 top_level->get_resources()->menu_shadow,
364 for(int i = 0; i < menu_items.total; i++)
366 menu_items.values[i]->draw();
374 int BC_MenuPopup::get_dimensions()
376 int xs10 = xS(10), xs20 = xS(20);
377 int ys4 = yS(4), ys5 = yS(10);
378 int widest_text = xs10, widest_key = xs10;
384 // Set up parameters in each item and get total h.
385 for(i = 0; i < menu_items.total; i++)
387 text_w = xs10 + top_level->get_text_width(MEDIUMFONT, menu_items.values[i]->text);
388 if(menu_items.values[i]->checked) text_w += check->get_w() + xS(1);
390 key_w = xs10 + top_level->get_text_width(MEDIUMFONT, menu_items.values[i]->hotkey_text);
391 if(text_w > widest_text) widest_text = text_w;
392 if(key_w > widest_key) widest_key = key_w;
394 if(!strcmp(menu_items.values[i]->text, "-"))
395 menu_items.values[i]->h = ys5;
398 menu_items.values[i]->h = item_bg[0] ? item_bg[0]->get_h() :
399 top_level->get_text_height(MEDIUMFONT) + ys4;
402 menu_items.values[i]->y = h;
403 menu_items.values[i]->highlighted = 0;
404 menu_items.values[i]->down = 0;
405 h += menu_items.values[i]->h;
407 w = widest_text + widest_key + xs20;
409 w = MAX(w, top_level->get_resources()->min_menu_w);
411 key_x = widest_text + xS(16);
417 int BC_MenuPopup::get_key_x()
422 BC_Popup* BC_MenuPopup::get_popup()
427 int BC_MenuPopup::cursor_inside()
429 if( !popup ) return 0;
430 if( popup->cursor_above() ) return 1;
431 for( int i=0; i<menu_items.size(); ++i ) {
432 if( !menu_items[i]->submenu ) continue;
433 if( menu_items[i]->submenu->cursor_inside() ) return 1;
438 int BC_MenuPopup::get_w()
447 // ================================= Sub Menu ==================================
449 BC_SubMenu::BC_SubMenu() : BC_MenuPopup()
453 BC_SubMenu::~BC_SubMenu()
457 int BC_SubMenu::add_submenuitem(BC_MenuItem *item)