00001 #include "bcmenubar.h"
00002 #include "bcmenuitem.h"
00003 #include "bcmenupopup.h"
00004 #include "bcpixmap.h"
00005 #include "bcpopupmenu.h"
00006 #include "bcresources.h"
00007 #include "bcsignals.h"
00008 #include "colors.h"
00009 #include "fonts.h"
00010 #include <string.h>
00011 #include "vframe.h"
00012
00013 #define BUTTON_UP 0
00014 #define BUTTON_HI 1
00015 #define BUTTON_DN 2
00016 #define TOTAL_IMAGES 3
00017
00018
00019 #define TRIANGLE_W 10
00020 #define TRIANGLE_H 10
00021
00022
00023 BC_PopupMenu::BC_PopupMenu(int x,
00024 int y,
00025 int w,
00026 char *text,
00027 int use_title,
00028 VFrame **data,
00029 int margin)
00030 : BC_SubWindow(x, y, 0, 0, -1)
00031 {
00032 highlighted = popup_down = 0;
00033 menu_popup = 0;
00034 icon = 0;
00035 if(margin >= 0)
00036 this->margin = margin;
00037 else
00038 this->margin = BC_WindowBase::get_resources()->popupmenu_margin;
00039
00040 this->use_title = use_title;
00041 strcpy(this->text, text);
00042 for(int i = 0; i < TOTAL_IMAGES; i++)
00043 {
00044 images[i] = 0;
00045 }
00046 this->data = data;
00047 this->w_argument = w;
00048 status = BUTTON_UP;
00049 }
00050
00051 BC_PopupMenu::BC_PopupMenu(int x,
00052 int y,
00053 char *text,
00054 int use_title,
00055 VFrame **data)
00056 : BC_SubWindow(x, y, w, -1, -1)
00057 {
00058 highlighted = popup_down = 0;
00059 menu_popup = 0;
00060 icon = 0;
00061 this->use_title = use_title;
00062 strcpy(this->text, text);
00063 for(int i = 0; i < TOTAL_IMAGES; i++)
00064 {
00065 images[i] = 0;
00066 }
00067 this->data = data;
00068 this->w_argument = 0;
00069 status = BUTTON_UP;
00070 }
00071
00072 BC_PopupMenu::~BC_PopupMenu()
00073 {
00074 if(menu_popup) delete menu_popup;
00075 for(int i = 0; i < TOTAL_IMAGES; i++)
00076 {
00077 if(images[i]) delete images[i];
00078 }
00079 }
00080
00081 char* BC_PopupMenu::get_text()
00082 {
00083 return text;
00084 }
00085
00086 void BC_PopupMenu::set_text(char *text)
00087 {
00088 if(use_title)
00089 {
00090 strcpy(this->text, text);
00091 draw_title();
00092 }
00093 }
00094
00095 void BC_PopupMenu::set_icon(BC_Pixmap *icon)
00096 {
00097 if(use_title)
00098 {
00099 this->icon = icon;
00100 if(menu_popup) draw_title();
00101 }
00102 }
00103
00104 int BC_PopupMenu::initialize()
00105 {
00106 if(use_title)
00107 {
00108 if(data)
00109 set_images(data);
00110 else
00111 if(BC_WindowBase::get_resources()->popupmenu_images)
00112 set_images(BC_WindowBase::get_resources()->popupmenu_images);
00113 else
00114 set_images(BC_WindowBase::get_resources()->generic_button_images);
00115 }
00116 else
00117
00118 {
00119 x = -10;
00120 y = -10;
00121 w = 10;
00122 h = 10;
00123 }
00124
00125 BC_SubWindow::initialize();
00126
00127 menu_popup = new BC_MenuPopup;
00128 menu_popup->initialize(top_level,
00129 0,
00130 0,
00131 0,
00132 this);
00133
00134 if(use_title) draw_title();
00135
00136 return 0;
00137 }
00138
00139 int BC_PopupMenu::set_images(VFrame **data)
00140 {
00141 BC_Resources *resources = get_resources();
00142 for(int i = 0; i < 3; i++)
00143 {
00144 if(images[i]) delete images[i];
00145 images[i] = new BC_Pixmap(parent_window, data[i], PIXMAP_ALPHA);
00146 }
00147
00148 if(w_argument > 0)
00149 w = w_argument +
00150 margin +
00151 resources->popupmenu_triangle_margin;
00152 else
00153 w = get_text_width(MEDIUMFONT, text) +
00154 margin +
00155 resources->popupmenu_triangle_margin;
00156
00157 h = images[BUTTON_UP]->get_h();
00158 return 0;
00159 }
00160
00161 int BC_PopupMenu::calculate_h(VFrame **data)
00162 {
00163 if(data)
00164 ;
00165 else
00166 if(BC_WindowBase::get_resources()->popupmenu_images)
00167 data = BC_WindowBase::get_resources()->popupmenu_images;
00168 else
00169 data = BC_WindowBase::get_resources()->generic_button_images;
00170
00171
00172 return data[BUTTON_UP]->get_h();
00173 }
00174
00175 int BC_PopupMenu::add_item(BC_MenuItem *item)
00176 {
00177 menu_popup->add_item(item);
00178 return 0;
00179 }
00180
00181 int BC_PopupMenu::remove_item(BC_MenuItem *item)
00182 {
00183 menu_popup->remove_item(item);
00184 return 0;
00185 }
00186
00187 int BC_PopupMenu::total_items()
00188 {
00189 return menu_popup->total_menuitems();
00190 return 0;
00191 }
00192
00193 BC_MenuItem* BC_PopupMenu::get_item(int i)
00194 {
00195 return menu_popup->menu_items.values[i];
00196 }
00197
00198 int BC_PopupMenu::draw_title()
00199 {
00200 if(!use_title) return 0;
00201 BC_Resources *resources = get_resources();
00202
00203
00204 draw_top_background(parent_window, 0, 0, w, h);
00205 draw_3segmenth(0, 0, w, images[status]);
00206
00207
00208 set_color(get_resources()->popup_title_text);
00209 int offset = 0;
00210 if(status == BUTTON_DN)
00211 offset = 1;
00212 if(!icon)
00213 {
00214 set_font(MEDIUMFONT);
00215 BC_WindowBase::draw_center_text(
00216 (get_w() - margin * 2 - resources->popupmenu_triangle_margin) / 2 + margin + offset,
00217 (int)((float)get_h() / 2 + get_text_ascent(MEDIUMFONT) / 2 - 2) + offset,
00218 text);
00219 }
00220
00221 if(icon)
00222 {
00223 draw_pixmap(icon,
00224 (get_w() - margin * 2 - resources->popupmenu_triangle_margin) / 2 + margin + offset - icon->get_w() / 2 ,
00225 get_h() / 2 - icon->get_h() / 2 + offset);
00226 }
00227
00228 draw_triangle_down_flat(get_w() - margin - resources->popupmenu_triangle_margin,
00229 get_h() / 2 - TRIANGLE_H / 2,
00230 TRIANGLE_W, TRIANGLE_H);
00231
00232 flash();
00233 return 0;
00234 }
00235
00236 int BC_PopupMenu::deactivate()
00237 {
00238 if(popup_down)
00239 {
00240 top_level->active_popup_menu = 0;
00241 popup_down = 0;
00242 menu_popup->deactivate_menu();
00243
00244 if(use_title) draw_title();
00245 }
00246 return 0;
00247 }
00248
00249 int BC_PopupMenu::activate_menu()
00250 {
00251 if(!popup_down)
00252 {
00253 int x = this->x;
00254 int y = this->y;
00255
00256 top_level->deactivate();
00257 top_level->active_popup_menu = this;
00258 if(!use_title)
00259 {
00260 x = top_level->get_abs_cursor_x(0) - get_w();
00261 y = top_level->get_abs_cursor_y(0) - get_h();
00262 button_press_x = top_level->cursor_x;
00263 button_press_y = top_level->cursor_y;
00264 }
00265
00266 button_releases = 0;
00267 if(use_title)
00268 {
00269 Window tempwin;
00270 int new_x, new_y, top_w, top_h;
00271 XTranslateCoordinates(top_level->display,
00272 win,
00273 top_level->rootwin,
00274 0,
00275 0,
00276 &new_x,
00277 &new_y,
00278 &tempwin);
00279 menu_popup->activate_menu(new_x,
00280 new_y,
00281 w,
00282 h,
00283 0,
00284 1);
00285 }
00286 else
00287 menu_popup->activate_menu(x, y, w, h, 0, 1);
00288 popup_down = 1;
00289 if(use_title) draw_title();
00290 }
00291 return 0;
00292 }
00293
00294 int BC_PopupMenu::deactivate_menu()
00295 {
00296 deactivate();
00297 return 0;
00298 }
00299
00300
00301 int BC_PopupMenu::reposition_window(int x, int y)
00302 {
00303 BC_WindowBase::reposition_window(x, y);
00304 draw_title();
00305 return 0;
00306 }
00307
00308 int BC_PopupMenu::focus_out_event()
00309 {
00310 deactivate();
00311 return 0;
00312 }
00313
00314
00315 int BC_PopupMenu::repeat_event(int64_t duration)
00316 {
00317 if(duration == top_level->get_resources()->tooltip_delay &&
00318 tooltip_text[0] != 0 &&
00319 status == BUTTON_HI &&
00320 !tooltip_done)
00321 {
00322 show_tooltip();
00323 tooltip_done = 1;
00324 return 1;
00325 }
00326 return 0;
00327 }
00328
00329 int BC_PopupMenu::button_press_event()
00330 {
00331 int result = 0;
00332
00333 if(get_buttonpress() == 1 &&
00334 is_event_win() &&
00335 use_title)
00336 {
00337 top_level->hide_tooltip();
00338 if(status == BUTTON_HI || status == BUTTON_UP) status = BUTTON_DN;
00339 activate_menu();
00340 draw_title();
00341 return 1;
00342 }
00343
00344
00345 if (is_event_win()
00346 && (get_buttonpress() == 4 || get_buttonpress() == 5)
00347 && menu_popup->total_menuitems() > 1
00348 )
00349 {
00350 int theval = -1;
00351 for (int i = 0; i < menu_popup->total_menuitems(); i++) {
00352 if (!strcmp(menu_popup->menu_items.values[i]->get_text(),get_text())) {
00353 theval=i;
00354 break;
00355 }
00356 }
00357
00358 if (theval == -1) theval=0;
00359 else if (get_buttonpress() == 4) theval--;
00360 else if (get_buttonpress() == 5) theval++;
00361
00362 if (theval < 0)
00363 theval=0;
00364 if (theval >= menu_popup->total_menuitems())
00365 theval = menu_popup->total_menuitems() - 1;
00366
00367 BC_MenuItem *tmp = menu_popup->menu_items.values[theval];
00368 set_text(tmp->get_text());
00369 if (!tmp->handle_event())
00370 this->handle_event();
00371 }
00372
00373
00374 if(popup_down)
00375 {
00376
00377 menu_popup->dispatch_button_press();
00378 return 1;
00379 }
00380
00381 return 0;
00382 }
00383
00384 int BC_PopupMenu::button_release_event()
00385 {
00386
00387 int result = 0;
00388
00389 button_releases++;
00390
00391 if(is_event_win() && use_title)
00392 {
00393 hide_tooltip();
00394 if(status == BUTTON_DN)
00395 {
00396 status = BUTTON_HI;
00397 draw_title();
00398 }
00399 }
00400
00401 if(popup_down)
00402 {
00403
00404 result = menu_popup->dispatch_button_release();
00405 }
00406
00407 if(popup_down && button_releases >= 2)
00408 {
00409 deactivate();
00410 }
00411
00412 if(!result && use_title && cursor_inside() && is_event_win())
00413 {
00414 hide_tooltip();
00415 result = 1;
00416 }
00417 else
00418 if(!result && !use_title && popup_down && button_releases < 2)
00419 {
00420 result = 1;
00421 }
00422
00423
00424 if(!result && popup_down)
00425 {
00426
00427 deactivate();
00428 result = 1;
00429 }
00430
00431 return result;
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442 if(popup_down)
00443 {
00444
00445 result = menu_popup->dispatch_button_release();
00446 }
00447
00448 if(!result && use_title && cursor_inside() && top_level->event_win == win)
00449 {
00450
00451 if(button_releases >= 2)
00452 {
00453 highlighted = 1;
00454 deactivate();
00455 }
00456 result = 1;
00457 }
00458 else
00459 if(!result && !use_title && button_releases < 2)
00460 {
00461
00462
00463
00464
00465
00466
00467 deactivate();
00468 result = 1;
00469 }
00470
00471 return result;
00472 }
00473
00474 int BC_PopupMenu::translation_event()
00475 {
00476
00477 if(popup_down) menu_popup->dispatch_translation_event();
00478 return 0;
00479 }
00480
00481 int BC_PopupMenu::cursor_leave_event()
00482 {
00483
00484 if(status == BUTTON_HI && use_title)
00485 {
00486 status = BUTTON_UP;
00487 draw_title();
00488 hide_tooltip();
00489 }
00490
00491
00492 if(popup_down)
00493 {
00494 menu_popup->dispatch_cursor_leave();
00495 }
00496
00497 return 0;
00498 }
00499
00500
00501 int BC_PopupMenu::cursor_enter_event()
00502 {
00503 if(is_event_win() && use_title)
00504 {
00505 tooltip_done = 0;
00506 if(top_level->button_down)
00507 {
00508 status = BUTTON_DN;
00509 }
00510 else
00511 if(status == BUTTON_UP)
00512 status = BUTTON_HI;
00513 draw_title();
00514 }
00515
00516 return 0;
00517 }
00518
00519 int BC_PopupMenu::cursor_motion_event()
00520 {
00521 int result = 0;
00522
00523
00524 if(popup_down)
00525 {
00526 result = menu_popup->dispatch_motion_event();
00527 }
00528
00529 if(!result && use_title && top_level->event_win == win)
00530 {
00531 if(highlighted)
00532 {
00533 if(cursor_inside())
00534 {
00535 highlighted = 0;
00536 draw_title();
00537 }
00538 }
00539 else
00540 {
00541 if(cursor_inside())
00542 {
00543 highlighted = 1;
00544 draw_title();
00545 result = 1;
00546 }
00547 }
00548 }
00549
00550 return result;
00551 }
00552
00553 int BC_PopupMenu::drag_start_event()
00554 {
00555
00556 if(popup_down) return 1;
00557 return 0;
00558 }
00559
00560 int BC_PopupMenu::drag_stop_event()
00561 {
00562 if(popup_down) return 1;
00563 return 0;
00564 }
00565
00566 int BC_PopupMenu::drag_motion_event()
00567 {
00568 if(popup_down) return 1;
00569 return 0;
00570 }
00571
00572
00573
00574
00575
00576
00577