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

bcmenuitem.C

Go to the documentation of this file.
00001 #include "bcmenubar.h"
00002 #include "bcmenuitem.h"
00003 #include "bcmenupopup.h"
00004 #include "bcpixmap.h"
00005 #include "bcpopup.h"
00006 #include "bcpopupmenu.h"
00007 #include "bcresources.h"
00008 #include "bcwindowbase.h"
00009 #include "colors.h"
00010 
00011 #include <string.h>
00012 
00013 
00014 #define MENUITEM_UP 0
00015 #define MENUITEM_HI 1
00016 #define MENUITEM_DN 2
00017 
00018 
00019 #define MENUITEM_MARGIN 2
00020 
00021 // ================================ Menu Item ==================================
00022 
00023 BC_MenuItem::BC_MenuItem(char *text, char *hotkey_text, int hotkey)
00024 {
00025         reset();
00026 
00027         if(text) set_text(text);
00028         if(hotkey_text) set_hotkey_text(hotkey_text);
00029 
00030         this->hotkey = hotkey;
00031         checked = 0;
00032         highlighted = 0;
00033         down = 0;
00034         submenu = 0;
00035         shift_hotkey = 0;
00036         alt_hotkey = 0;
00037         menu_popup = 0;
00038 }
00039 
00040 BC_MenuItem::~BC_MenuItem()
00041 {
00042         if(text) delete [] text;
00043         if(hotkey_text) delete [] hotkey_text;
00044         if(submenu) delete submenu;
00045         if(menu_popup) menu_popup->remove_item(this);
00046 }
00047 
00048 void BC_MenuItem::reset()
00049 {
00050         text = new char[1];
00051         hotkey_text = new char[1];
00052         text[0] = 0;
00053         hotkey_text[0] = 0;
00054         icon = 0;
00055 }
00056 
00057 int BC_MenuItem::initialize(BC_WindowBase *top_level, BC_MenuBar *menu_bar, BC_MenuPopup *menu_popup)
00058 {
00059         this->top_level = top_level;
00060         this->menu_popup = menu_popup;
00061         this->menu_bar = menu_bar;
00062         return 0;
00063 }
00064 
00065 int BC_MenuItem::set_checked(int value)
00066 {
00067         this->checked = value;
00068         return 0;
00069 }
00070 
00071 int BC_MenuItem::get_checked()
00072 {
00073         return checked;
00074 }
00075 
00076 void BC_MenuItem::set_icon(BC_Pixmap *icon)
00077 {
00078         this->icon = icon;
00079 }
00080 
00081 BC_Pixmap* BC_MenuItem::get_icon()
00082 {
00083         return icon;
00084 }
00085 
00086 void BC_MenuItem::set_text(char *text)
00087 {
00088         if(this->text) delete [] this->text;
00089         this->text = new char[strlen(text) + 1];
00090         strcpy(this->text, text);
00091 }
00092 
00093 void BC_MenuItem::set_hotkey_text(char *text)
00094 {
00095         if(this->hotkey_text) delete [] this->hotkey_text;
00096         this->hotkey_text = new char[strlen(text) + 1];
00097         strcpy(this->hotkey_text, text);
00098 }
00099 
00100 int BC_MenuItem::deactivate_submenus(BC_MenuPopup *exclude)
00101 {
00102         if(submenu && submenu != exclude)
00103         {
00104                 submenu->deactivate_submenus(exclude);
00105                 submenu->deactivate_menu();
00106                 highlighted = 0;
00107         }
00108         return 0;
00109 }
00110 
00111 int BC_MenuItem::activate_submenu()
00112 {
00113         int new_x, new_y;
00114         if(menu_popup->popup && submenu && !submenu->popup)
00115         {
00116                 Window tempwin;
00117                 XTranslateCoordinates(top_level->display, 
00118                         menu_popup->get_popup()->win, 
00119                         top_level->rootwin, 
00120                         0, 
00121                         y, 
00122                         &new_x, 
00123                         &new_y, 
00124                         &tempwin);
00125                 submenu->activate_menu(new_x + 5, new_y, menu_popup->w - 10, h, 0, 0);
00126                 highlighted = 1;
00127         }
00128         return 0;
00129 }
00130 
00131 
00132 int BC_MenuItem::dispatch_button_press()
00133 {
00134         int result = 0;
00135 
00136         if(submenu)
00137         {
00138                 result = submenu->dispatch_button_press();
00139         }
00140 
00141         if(!result && top_level->event_win == menu_popup->get_popup()->win)
00142         {
00143                 if(top_level->cursor_x >= 0 && top_level->cursor_x < menu_popup->get_w() &&
00144                         top_level->cursor_y >= y && top_level->cursor_y < y + h)
00145                 {
00146                         if(!highlighted)
00147                         {
00148                                 highlighted = 1;
00149                         }
00150                         result = 1;
00151                 }
00152                 else
00153                 if(highlighted)
00154                 {
00155                         highlighted = 0;
00156                         result = 1;
00157                 }
00158         }
00159 
00160         return result;
00161 }
00162 
00163 int BC_MenuItem::dispatch_button_release(int &redraw)
00164 {
00165         int result = 0;
00166         int cursor_x, cursor_y;
00167         Window tempwin;
00168 
00169         if(!strcmp(text, "-")) return 0;
00170 
00171         if(submenu)
00172         {
00173                 result = submenu->dispatch_button_release();
00174         }
00175 
00176         if(!result)
00177         {
00178                 XTranslateCoordinates(top_level->display, 
00179                         top_level->event_win, 
00180                         menu_popup->get_popup()->win, 
00181                         top_level->cursor_x, 
00182                         top_level->cursor_y, 
00183                         &cursor_x, 
00184                         &cursor_y, 
00185                         &tempwin);
00186 
00187                 if(cursor_x >= 0 && cursor_x < menu_popup->get_w() &&
00188                         cursor_y >= y && cursor_y < y + h)
00189                 {
00190                         if(menu_bar)
00191                                 menu_bar->deactivate();
00192                         else
00193                                 menu_popup->popup_menu->deactivate();
00194 
00195                         if(!handle_event() && menu_popup && menu_popup->popup_menu)
00196                         {
00197                                 menu_popup->popup_menu->set_text(text);
00198                                 menu_popup->popup_menu->handle_event();
00199                         }
00200                         return 1;
00201                 }
00202         }
00203         return 0;
00204 }
00205 
00206 int BC_MenuItem::dispatch_motion_event(int &redraw)
00207 {
00208         int result = 0;
00209         int cursor_x, cursor_y;
00210         Window tempwin;
00211 
00212         if(submenu)
00213         {
00214                 result = submenu->dispatch_motion_event();
00215         }
00216 
00217         top_level->translate_coordinates(top_level->event_win, 
00218                 menu_popup->get_popup()->win,
00219                 top_level->cursor_x,
00220                 top_level->cursor_y,
00221                 &cursor_x,
00222                 &cursor_y);
00223 
00224         if(cursor_x >= 0 && cursor_x < menu_popup->get_w() &&
00225                 cursor_y >= y && cursor_y < y + h)
00226         {
00227 // Highlight the item
00228                 if(!highlighted)
00229                 {
00230 // Deactivate submenus in the parent menu excluding this one.
00231                         menu_popup->deactivate_submenus(submenu);
00232                         highlighted = 1;
00233                         if(submenu) activate_submenu();
00234                         redraw = 1;
00235                 }
00236                 result = 1;
00237         }
00238         else
00239         if(highlighted)
00240         {
00241                 highlighted = 0;
00242                 result = 1;
00243                 redraw = 1;
00244         }
00245         return result;
00246 }
00247 
00248 int BC_MenuItem::dispatch_translation_event()
00249 {
00250         if(submenu)
00251                 submenu->dispatch_translation_event();
00252         return 0;
00253 }
00254 
00255 int BC_MenuItem::dispatch_cursor_leave()
00256 {
00257         int result = 0;
00258         if(submenu)
00259         {
00260                 result = submenu->dispatch_cursor_leave();
00261         }
00262 
00263         if(!result && highlighted && top_level->event_win == menu_popup->get_popup()->win)
00264         {
00265                 highlighted = 0;
00266                 return 1;
00267         }
00268         return 0;
00269 }
00270 
00271 int BC_MenuItem::dispatch_key_press()
00272 {
00273         int result = 0;
00274         if(submenu)
00275         {
00276                 result = submenu->dispatch_key_press();
00277         }
00278         
00279         if(!result)
00280         {
00281 
00282                 if(top_level->get_keypress() == hotkey && 
00283                         shift_hotkey == top_level->shift_down() &&
00284                         alt_hotkey == top_level->alt_down())
00285                 {
00286                         result = 1;
00287                         handle_event();
00288                 }
00289         }
00290         return result;
00291 }
00292 
00293 
00294 int BC_MenuItem::draw()
00295 {
00296         int text_line = top_level->get_text_descent(MEDIUMFONT);
00297         BC_Resources *resources = top_level->get_resources();
00298 
00299         if(!strcmp(text, "-"))
00300         {
00301                 menu_popup->get_popup()->set_color(DKGREY);
00302                 menu_popup->get_popup()->draw_line(5, y + h / 2, menu_popup->get_w() - 5, y + h / 2);
00303                 menu_popup->get_popup()->set_color(LTGREY);
00304                 menu_popup->get_popup()->draw_line(5, y + h / 2 + 1, menu_popup->get_w() - 5, y + h / 2 + 1);
00305         }
00306         else
00307         {
00308                 int offset = 0;
00309                 if(highlighted)
00310                 {
00311                         int y = this->y;
00312                         int w = menu_popup->get_w() - 4;
00313                         int h = this->h;
00314 
00315 // Button down
00316                         if(top_level->get_button_down() && !submenu)
00317                         {
00318                                 if(menu_popup->item_bg[MENUITEM_DN])
00319                                 {
00320                                         menu_popup->get_popup()->draw_9segment(MENUITEM_MARGIN,
00321                                                 y,
00322                                                 menu_popup->get_w() - MENUITEM_MARGIN * 2,
00323                                                 h,
00324                                                 menu_popup->item_bg[MENUITEM_DN]);
00325                                 }
00326                                 else
00327                                 {
00328                                         menu_popup->get_popup()->draw_3d_box(MENUITEM_MARGIN, 
00329                                                 y, 
00330                                                 menu_popup->get_w() - MENUITEM_MARGIN * 2, 
00331                                                 h, 
00332                                                 resources->menu_shadow,
00333                                                 BLACK,
00334                                                 resources->menu_down,
00335                                                 resources->menu_down,
00336                                                 resources->menu_light);
00337                                 }
00338                                 offset = 1;
00339                         }
00340                         else
00341 // Highlighted
00342                         {
00343                                 if(menu_popup->item_bg[MENUITEM_HI])
00344                                 {
00345                                         menu_popup->get_popup()->draw_9segment(MENUITEM_MARGIN,
00346                                                 y,
00347                                                 menu_popup->get_w() - MENUITEM_MARGIN * 2,
00348                                                 h,
00349                                                 menu_popup->item_bg[MENUITEM_HI]);
00350                                 }
00351                                 else
00352                                 {
00353                                         menu_popup->get_popup()->set_color(resources->menu_highlighted);
00354                                         menu_popup->get_popup()->draw_box(MENUITEM_MARGIN, 
00355                                                 y, 
00356                                                 menu_popup->get_w() - MENUITEM_MARGIN * 2, 
00357                                                 h);
00358                                 }
00359                         }
00360                         menu_popup->get_popup()->set_color(resources->menu_highlighted_fontcolor);
00361                 }
00362                 else
00363                   {
00364                     menu_popup->get_popup()->set_color(resources->menu_item_text);
00365                   }
00366                 if(checked)
00367                 {
00368                         menu_popup->get_popup()->draw_check(10 + offset, y + 2 + offset);
00369                         menu_popup->get_popup()->set_font(MEDIUMFONT);
00370                         menu_popup->get_popup()->draw_text(30 + offset, y + h - text_line - 2 + offset, text);
00371                         menu_popup->get_popup()->draw_text(menu_popup->get_key_x() + offset, y + h - text_line - 2 + offset, hotkey_text);
00372                 }
00373                 else
00374                 {
00375                         menu_popup->get_popup()->set_font(MEDIUMFONT);
00376                         menu_popup->get_popup()->draw_text(10 + offset, y + h - text_line - 2 + offset, text);
00377                         menu_popup->get_popup()->draw_text(menu_popup->get_key_x() + offset, y + h - text_line - 2 + offset, hotkey_text);
00378                 }
00379         }
00380         return 0;
00381 }
00382 
00383 
00384 int BC_MenuItem::add_submenu(BC_SubMenu *submenu)
00385 {
00386         this->submenu = submenu;
00387         submenu->initialize(top_level, menu_bar, 0, this, 0);
00388         return 0;
00389 }
00390 
00391 char* BC_MenuItem::get_text()
00392 {
00393         return text;
00394 }
00395 
00396 BC_WindowBase* BC_MenuItem::get_top_level()
00397 {
00398         return top_level;
00399 }
00400 
00401 BC_PopupMenu* BC_MenuItem::get_popup_menu()
00402 {
00403         return menu_popup->popup_menu;
00404 }
00405 
00406 int BC_MenuItem::set_shift(int value)
00407 {
00408         shift_hotkey = value;
00409         return 0;
00410 }
00411 
00412 int BC_MenuItem::set_alt(int value)
00413 {
00414         alt_hotkey = value;
00415         return 0;
00416 }

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