Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

bcpopupmenu.C

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

Generated on Sun Jan 8 13:26:32 2006 for Guicast-svn by  doxygen 1.4.4