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,
51 : BC_SubWindow(x, y, 0, 0, -1)
53 highlighted = popup_down = 0;
57 this->margin = margin;
59 this->margin = BC_WindowBase::get_resources()->popupmenu_margin;
61 this->use_title = use_title;
62 strcpy(this->text, text);
63 for(int i = 0; i < TOTAL_IMAGES; i++)
73 BC_PopupMenu::BC_PopupMenu(int x,
78 : BC_SubWindow(x, y, 0, -1, -1)
80 highlighted = popup_down = 0;
83 this->use_title = use_title;
84 strcpy(this->text, text);
85 for(int i = 0; i < TOTAL_IMAGES; i++)
90 this->w_argument = -1;
95 BC_PopupMenu::~BC_PopupMenu()
100 for(int i = 0; i < TOTAL_IMAGES; i++)
102 if(images[i]) delete images[i];
106 char* BC_PopupMenu::get_text()
111 void BC_PopupMenu::set_text(const char *text)
115 strcpy(this->text, text);
120 void BC_PopupMenu::set_icon(BC_Pixmap *icon)
125 if(menu_popup) draw_title(1);
129 int BC_PopupMenu::initialize()
136 if(BC_WindowBase::get_resources()->popupmenu_images)
137 set_images(BC_WindowBase::get_resources()->popupmenu_images);
139 set_images(BC_WindowBase::get_resources()->generic_button_images);
142 // Move outside window if no title
150 BC_SubWindow::initialize();
152 menu_popup = new BC_MenuPopup;
153 menu_popup->initialize(top_level,
159 if(use_title) draw_title(0);
164 int BC_PopupMenu::set_images(VFrame **data)
166 BC_Resources *resources = get_resources();
167 for(int i = 0; i < 3; i++)
169 if(images[i]) delete images[i];
170 images[i] = new BC_Pixmap(parent_window, data[i], PIXMAP_ALPHA);
176 resources->popupmenu_triangle_margin;
178 w = get_text_width(MEDIUMFONT, text) +
180 resources->popupmenu_triangle_margin;
182 h = images[BUTTON_UP]->get_h();
186 int BC_PopupMenu::calculate_w(int w_argument)
189 BC_WindowBase::get_resources()->popupmenu_margin +
190 BC_WindowBase::get_resources()->popupmenu_triangle_margin;
193 int BC_PopupMenu::calculate_h(VFrame **data)
198 if(BC_WindowBase::get_resources()->popupmenu_images)
199 data = BC_WindowBase::get_resources()->popupmenu_images;
201 data = BC_WindowBase::get_resources()->generic_button_images;
204 return data[BUTTON_UP]->get_h();
207 int BC_PopupMenu::add_item(BC_MenuItem *item)
209 menu_popup->add_item(item);
213 int BC_PopupMenu::remove_item(BC_MenuItem *item)
215 menu_popup->remove_item(item);
219 int BC_PopupMenu::del_item(BC_MenuItem *item)
221 menu_popup->del_item(item);
225 int BC_PopupMenu::total_items()
227 return menu_popup->total_items();
230 BC_MenuItem* BC_PopupMenu::get_item(int i)
232 return menu_popup->menu_items.values[i];
235 int BC_PopupMenu::draw_title(int flush)
237 if(!use_title) return 0;
238 BC_Resources *resources = get_resources();
241 draw_top_background(parent_window, 0, 0, w, h);
242 draw_3segmenth(0, 0, w, images[status]);
245 set_color(get_resources()->popup_title_text);
247 if(status == BUTTON_DN)
251 set_font(MEDIUMFONT);
252 char truncated[BCTEXTLEN];
253 int available_w = get_w() - margin * 2 - resources->popupmenu_triangle_margin;
254 truncate_text(truncated, text, available_w);
256 BC_WindowBase::draw_center_text(
257 available_w / 2 + margin + offset,
258 (int)((float)get_h() / 2 + get_text_ascent(MEDIUMFONT) / 2 - 2) + offset,
265 (get_w() - margin * 2 - resources->popupmenu_triangle_margin) / 2 + margin + offset - icon->get_w() / 2 ,
266 get_h() / 2 - icon->get_h() / 2 + offset);
270 draw_triangle_down_flat(get_w() - margin - resources->popupmenu_triangle_margin,
271 get_h() / 2 - TRIANGLE_H / 2, TRIANGLE_W, TRIANGLE_H);
277 int BC_PopupMenu::deactivate()
281 top_level->active_popup_menu = 0;
283 menu_popup->deactivate_menu();
285 if(use_title) draw_title(1); // draw the title
290 int BC_PopupMenu::activate_menu()
292 if( !get_button_down() || !BC_WindowBase::get_resources()->popupmenu_btnup )
293 return menu_activate();
298 int BC_PopupMenu::menu_activate()
306 top_level->deactivate();
307 top_level->active_popup_menu = this;
310 x = top_level->get_abs_cursor_x(0) - get_w();
311 y = top_level->get_abs_cursor_y(0) - get_h();
312 button_press_x = top_level->cursor_x;
313 button_press_y = top_level->cursor_y;
320 XTranslateCoordinates(top_level->display,
321 win, top_level->rootwin,
322 0, 0, &new_x, &new_y, &tempwin);
323 menu_popup->activate_menu(new_x, new_y,
327 menu_popup->activate_menu(x+3, y+3, w, h, 0, 1);
329 if(use_title) draw_title(1);
334 int BC_PopupMenu::deactivate_menu()
341 int BC_PopupMenu::reposition_window(int x, int y)
343 BC_WindowBase::reposition_window(x, y);
348 int BC_PopupMenu::focus_out_event()
350 if( popup_down && !get_button_down() &&
351 !cursor_inside() && !menu_popup->cursor_inside() )
357 int BC_PopupMenu::repeat_event(int64_t duration)
359 if( status == BUTTON_HI &&
360 tooltip_text && tooltip_text[0] != 0 &&
361 duration == top_level->get_resources()->tooltip_delay )
369 int BC_PopupMenu::button_press_event()
371 if(get_buttonpress() == 1 &&
375 top_level->hide_tooltip();
376 if(status == BUTTON_HI || status == BUTTON_UP) status = BUTTON_DN;
384 && (get_buttonpress() == 4 || get_buttonpress() == 5)
385 && menu_popup->total_items() > 1 )
388 for (int i = 0; i < menu_popup->total_items(); i++) {
389 if (!strcmp(menu_popup->menu_items.values[i]->get_text(),get_text())) {
395 if (theval == -1) theval=0;
396 else if (get_buttonpress() == 4) theval--;
397 else if (get_buttonpress() == 5) theval++;
401 if (theval >= menu_popup->total_items())
402 theval = menu_popup->total_items() - 1;
404 BC_MenuItem *tmp = menu_popup->menu_items.values[theval];
405 set_text(tmp->get_text());
406 if (!tmp->handle_event())
407 this->handle_event();
411 // Menu is down so dispatch to popup.
412 menu_popup->dispatch_button_press();
419 int BC_PopupMenu::button_release_event()
424 if(is_event_win() && use_title)
427 if(status == BUTTON_DN)
435 return menu_activate();
437 if( !use_title && status == BUTTON_DN ) {
440 else if( popup_down && menu_popup->cursor_inside() ) {
441 // Menu is down so dispatch to popup.
442 result = menu_popup->dispatch_button_release();
444 // released outside popup
445 if( get_resources()->popupmenu_btnup && !result && popup_down ) {
454 int BC_PopupMenu::translation_event()
456 //printf("BC_PopupMenu::translation_event 1\n");
457 if(popup_down) menu_popup->dispatch_translation_event();
461 int BC_PopupMenu::cursor_leave_event()
464 if(status == BUTTON_HI && use_title)
473 if( !get_button_down() && !menu_popup->cursor_inside() ) {
477 menu_popup->dispatch_cursor_leave();
484 int BC_PopupMenu::cursor_enter_event()
486 if(is_event_win() && use_title)
488 if(top_level->button_down)
493 if(status == BUTTON_UP)
501 int BC_PopupMenu::cursor_motion_event()
505 // This menu is down.
507 result = menu_popup->dispatch_motion_event();
510 if(!result && use_title && is_event_win()) {
512 if(!cursor_inside()) {
518 if(cursor_inside()) {
529 int BC_PopupMenu::drag_start_event()
531 //printf("BC_PopupMenu::drag_start_event %d\n", popup_down);
532 if(popup_down) return 1;
536 int BC_PopupMenu::drag_stop_event()
538 if(popup_down) return 1;
542 int BC_PopupMenu::drag_motion_event()
544 if(popup_down) return 1;