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"
26 #include "bcpopupmenu.h"
27 #include "bcresources.h"
28 #include "bcsignals.h"
37 #define TOTAL_IMAGES 3
44 BC_PopupMenu::BC_PopupMenu(int x, int y, int w, const char *text,
45 int use_title, VFrame **data, int margin)
46 : BC_SubWindow(x, y, 0, 0, -1)
48 highlighted = popup_down = 0;
51 this->margin = margin >= 0 ? margin :
52 BC_WindowBase::get_resources()->popupmenu_margin;
53 this->use_title = use_title;
54 strcpy(this->text, text);
55 for( int i=0; i<TOTAL_IMAGES; ++i ) images[i] = 0;
62 BC_PopupMenu::BC_PopupMenu(int x, int y, const char *text,
63 int use_title, VFrame **data)
64 : BC_SubWindow(x, y, 0, -1, -1)
66 highlighted = popup_down = 0;
69 this->margin = BC_WindowBase::get_resources()->popupmenu_margin;
70 this->use_title = use_title;
71 strcpy(this->text, text);
72 for( int i=0; i<TOTAL_IMAGES; ++i ) images[i] = 0;
74 this->w_argument = -1;
79 BC_PopupMenu::~BC_PopupMenu()
84 for( int i=0; i<TOTAL_IMAGES; ++i ) delete images[i];
87 char* BC_PopupMenu::get_text()
92 void BC_PopupMenu::set_text(const char *text)
95 strcpy(this->text, text);
100 void BC_PopupMenu::set_icon(BC_Pixmap *icon)
104 if( menu_popup ) draw_title(1);
108 int BC_PopupMenu::initialize()
114 if( BC_WindowBase::get_resources()->popupmenu_images )
115 set_images(BC_WindowBase::get_resources()->popupmenu_images);
117 set_images(BC_WindowBase::get_resources()->generic_button_images);
120 // Move outside window if no title
125 BC_SubWindow::initialize();
127 menu_popup = new BC_MenuPopup;
128 menu_popup->initialize(top_level, 0, 0, 0, this);
130 if( use_title ) draw_title(0);
135 int BC_PopupMenu::set_images(VFrame **data)
137 for( int i=0; i<3; ++i ) {
139 images[i] = new BC_Pixmap(parent_window, data[i], PIXMAP_ALPHA);
142 w = w_argument > 0 ? w_argument :
143 calculate_w(margin, get_text_width(MEDIUMFONT, text), use_title);
144 h = images[BUTTON_UP]->get_h();
148 int BC_PopupMenu::calculate_w(int margin, int text_width, int use_title)
150 BC_Resources *resources = get_resources();
151 int l = margin >= 0 ? margin : resources->popupmenu_margin;
152 int r = use_title < 0 ? l : resources->popupmenu_triangle_margin;
153 return l + text_width + r;
156 int BC_PopupMenu::calculate_w(int text_width)
158 return calculate_w(-1, text_width, 0);
161 int BC_PopupMenu::calculate_h(VFrame **data)
163 if( !data ) data = BC_WindowBase::get_resources()->popupmenu_images ?
164 BC_WindowBase::get_resources()->popupmenu_images :
165 BC_WindowBase::get_resources()->generic_button_images ;
167 return data[BUTTON_UP]->get_h();
170 int BC_PopupMenu::add_item(BC_MenuItem *item)
172 menu_popup->add_item(item);
176 int BC_PopupMenu::remove_item(BC_MenuItem *item)
178 menu_popup->remove_item(item);
182 int BC_PopupMenu::del_item(BC_MenuItem *item)
184 menu_popup->del_item(item);
188 int BC_PopupMenu::total_items()
190 return menu_popup->total_items();
193 BC_MenuItem* BC_PopupMenu::get_item(int i)
195 return menu_popup->menu_items.values[i];
198 int BC_PopupMenu::get_margin()
203 int BC_PopupMenu::draw_face(int dx, int color)
205 if( !use_title ) return 0;
208 draw_top_background(parent_window, 0, 0, w, h);
209 draw_3segmenth(0, 0, w, images[status]);
212 if( color < 0 ) color = get_resources()->popup_title_text;
215 int offset = status == BUTTON_DN ? 1 : 0;
216 int available_w = get_w() - calculate_w(margin, 0, use_title);
219 char truncated[BCTEXTLEN];
220 truncate_text(truncated, text, available_w);
221 set_font(MEDIUMFONT);
222 BC_WindowBase::draw_center_text(
223 dx + available_w/2 + margin + offset,
224 (int)((float)get_h()/2 + get_text_ascent(MEDIUMFONT)/2 - 2) + offset,
230 available_w/ 2 + margin + offset - icon->get_w()/2 ,
231 get_h()/2 - icon->get_h()/2 + offset);
235 draw_triangle_down_flat(available_w + margin,
236 get_h()/2 - TRIANGLE_H/2, TRIANGLE_W, TRIANGLE_H);
240 int BC_PopupMenu::draw_title(int flush)
247 int BC_PopupMenu::deactivate()
250 top_level->active_popup_menu = 0;
252 menu_popup->deactivate_menu();
254 if( use_title ) draw_title(1); // draw the title
259 int BC_PopupMenu::activate_menu()
261 if( !get_button_down() || !BC_WindowBase::get_resources()->popupmenu_btnup )
262 return menu_activate();
263 top_level->active_popup_menu = this;
268 int BC_PopupMenu::menu_activate()
275 top_level->deactivate();
276 top_level->active_popup_menu = this;
278 x = top_level->get_abs_cursor_x(0) - get_w();
279 y = top_level->get_abs_cursor_y(0) - get_h();
280 button_press_x = top_level->cursor_x;
281 button_press_y = top_level->cursor_y;
287 XTranslateCoordinates(top_level->display,
288 win, top_level->rootwin,
289 0, 0, &new_x, &new_y, &tempwin);
290 menu_popup->activate_menu(new_x, new_y,
294 menu_popup->activate_menu(x+3, y+3, w, h, 0, 1);
296 if( use_title ) draw_title(1);
301 int BC_PopupMenu::deactivate_menu()
308 int BC_PopupMenu::reposition_window(int x, int y)
310 BC_WindowBase::reposition_window(x, y);
315 int BC_PopupMenu::focus_out_event()
317 if( popup_down && !get_button_down() &&
318 !cursor_inside() && !menu_popup->cursor_inside() )
324 int BC_PopupMenu::repeat_event(int64_t duration)
326 if( status == BUTTON_HI &&
327 tooltip_text && tooltip_text[0] != 0 &&
328 duration == top_level->get_resources()->tooltip_delay ) {
335 int BC_PopupMenu::button_press_event()
338 if( get_buttonpress() == 1 &&
341 top_level->hide_tooltip();
342 if( status == BUTTON_HI || status == BUTTON_UP ) status = BUTTON_DN;
350 && (get_buttonpress() == 4 || get_buttonpress() == 5)
351 && menu_popup->total_items() > 1 ) {
353 for( int i=0; i<menu_popup->total_items(); ++i ) {
354 if( !strcmp(menu_popup->menu_items.values[i]->get_text(),get_text()) ) {
360 if( theval == -1 ) theval = 0;
361 else if( get_buttonpress() == 4 ) --theval;
362 else if( get_buttonpress() == 5 ) ++theval;
366 else if( theval >= menu_popup->total_items() )
367 theval = menu_popup->total_items() - 1;
369 BC_MenuItem *tmp = menu_popup->menu_items.values[theval];
370 set_text(tmp->get_text());
371 result = tmp->handle_event();
373 result = this->handle_event();
376 // Menu is down so dispatch to popup.
377 menu_popup->dispatch_button_press();
384 int BC_PopupMenu::button_release_event()
389 if( is_event_win() && use_title ) {
391 if( status == BUTTON_DN ) {
398 return menu_activate();
400 if( !use_title && status == BUTTON_DN ) {
403 else if( popup_down && menu_popup->cursor_inside() ) {
404 // Menu is down so dispatch to popup.
405 result = menu_popup->dispatch_button_release();
407 // released outside popup
408 if( get_resources()->popupmenu_btnup && !result && popup_down ) {
417 int BC_PopupMenu::translation_event()
419 //printf("BC_PopupMenu::translation_event 1\n");
420 if( popup_down ) menu_popup->dispatch_translation_event();
424 int BC_PopupMenu::cursor_leave_event()
427 if( status == BUTTON_HI && use_title ) {
435 if( !get_button_down() && !menu_popup->cursor_inside() ) {
437 // deactivate_menu();
439 menu_popup->dispatch_cursor_leave();
446 int BC_PopupMenu::cursor_enter_event()
448 if( is_event_win() && use_title ) {
449 if( top_level->button_down ) {
453 if( status == BUTTON_UP )
461 int BC_PopupMenu::cursor_motion_event()
465 // This menu is down.
467 result = menu_popup->dispatch_motion_event();
470 if( !result && use_title && is_event_win() ) {
472 if( !cursor_inside() ) {
478 if( cursor_inside() ) {
489 int BC_PopupMenu::drag_start_event()
491 //printf("BC_PopupMenu::drag_start_event %d\n", popup_down);
492 if( popup_down ) return 1;
496 int BC_PopupMenu::drag_stop_event()
498 if( popup_down ) return 1;
502 int BC_PopupMenu::drag_motion_event()
504 if( popup_down ) return 1;