| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/gtk1/menu.cpp |
| 3 | // Purpose: |
| 4 | // Author: Robert Roebling |
| 5 | // Id: $Id$ |
| 6 | // Copyright: (c) 1998 Robert Roebling |
| 7 | // Licence: wxWindows licence |
| 8 | ///////////////////////////////////////////////////////////////////////////// |
| 9 | |
| 10 | // For compilers that support precompilation, includes "wx.h". |
| 11 | #include "wx/wxprec.h" |
| 12 | |
| 13 | #include "wx/menu.h" |
| 14 | #include "wx/log.h" |
| 15 | #include "wx/intl.h" |
| 16 | #include "wx/app.h" |
| 17 | #include "wx/bitmap.h" |
| 18 | |
| 19 | #if wxUSE_ACCEL |
| 20 | #include "wx/accel.h" |
| 21 | #endif // wxUSE_ACCEL |
| 22 | |
| 23 | #include "wx/gtk1/private.h" |
| 24 | |
| 25 | #include <gdk/gdkkeysyms.h> |
| 26 | |
| 27 | #define ACCEL_OBJECT GtkObject |
| 28 | #define ACCEL_OBJECTS(a) (a)->attach_objects |
| 29 | #define ACCEL_OBJ_CAST(obj) GTK_OBJECT(obj) |
| 30 | |
| 31 | // we use normal item but with a special id for the menu title |
| 32 | static const int wxGTK_TITLE_ID = -3; |
| 33 | |
| 34 | // defined in window.cpp |
| 35 | extern guint32 wxGtkTimeLastClick; |
| 36 | |
| 37 | //----------------------------------------------------------------------------- |
| 38 | // idle system |
| 39 | //----------------------------------------------------------------------------- |
| 40 | |
| 41 | extern void wxapp_install_idle_handler(); |
| 42 | extern bool g_isIdle; |
| 43 | |
| 44 | #if wxUSE_ACCEL |
| 45 | static wxString GetGtkHotKey( const wxMenuItem& item ); |
| 46 | #endif |
| 47 | |
| 48 | //----------------------------------------------------------------------------- |
| 49 | // idle system |
| 50 | //----------------------------------------------------------------------------- |
| 51 | |
| 52 | static wxString wxReplaceUnderscore( const wxString& title ) |
| 53 | { |
| 54 | const wxChar *pc; |
| 55 | |
| 56 | // GTK 1.2 wants to have "_" instead of "&" for accelerators |
| 57 | wxString str; |
| 58 | pc = title; |
| 59 | while (*pc != wxT('\0')) |
| 60 | { |
| 61 | if ((*pc == wxT('&')) && (*(pc+1) == wxT('&'))) |
| 62 | { |
| 63 | // "&" is doubled to indicate "&" instead of accelerator |
| 64 | ++pc; |
| 65 | str << wxT('&'); |
| 66 | } |
| 67 | else if (*pc == wxT('&')) |
| 68 | { |
| 69 | str << wxT('_'); |
| 70 | } |
| 71 | else |
| 72 | { |
| 73 | if ( *pc == wxT('_') ) |
| 74 | { |
| 75 | // underscores must be doubled to prevent them from being |
| 76 | // interpreted as accelerator character prefix by GTK |
| 77 | str << *pc; |
| 78 | } |
| 79 | |
| 80 | str << *pc; |
| 81 | } |
| 82 | ++pc; |
| 83 | } |
| 84 | |
| 85 | // wxPrintf( wxT("before %s after %s\n"), title.c_str(), str.c_str() ); |
| 86 | |
| 87 | return str; |
| 88 | } |
| 89 | |
| 90 | //----------------------------------------------------------------------------- |
| 91 | // activate message from GTK |
| 92 | //----------------------------------------------------------------------------- |
| 93 | |
| 94 | static void DoCommonMenuCallbackCode(wxMenu *menu, wxMenuEvent& event) |
| 95 | { |
| 96 | if (g_isIdle) |
| 97 | wxapp_install_idle_handler(); |
| 98 | |
| 99 | event.SetEventObject( menu ); |
| 100 | |
| 101 | wxEvtHandler* handler = menu->GetEventHandler(); |
| 102 | if (handler && handler->ProcessEvent(event)) |
| 103 | return; |
| 104 | |
| 105 | wxWindow *win = menu->GetInvokingWindow(); |
| 106 | if (win) |
| 107 | win->GetEventHandler()->ProcessEvent( event ); |
| 108 | } |
| 109 | |
| 110 | extern "C" { |
| 111 | |
| 112 | static void gtk_menu_open_callback( GtkWidget *widget, wxMenu *menu ) |
| 113 | { |
| 114 | wxMenuEvent event(wxEVT_MENU_OPEN, -1, menu); |
| 115 | |
| 116 | DoCommonMenuCallbackCode(menu, event); |
| 117 | } |
| 118 | |
| 119 | static void gtk_menu_close_callback( GtkWidget *widget, wxMenuBar *menubar ) |
| 120 | { |
| 121 | if ( !menubar->GetMenuCount() ) |
| 122 | { |
| 123 | // if menubar is empty we can't call GetMenu(0) below |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | wxMenuEvent event( wxEVT_MENU_CLOSE, -1, NULL ); |
| 128 | |
| 129 | DoCommonMenuCallbackCode(menubar->GetMenu(0), event); |
| 130 | } |
| 131 | |
| 132 | } |
| 133 | |
| 134 | //----------------------------------------------------------------------------- |
| 135 | // wxMenuBar |
| 136 | //----------------------------------------------------------------------------- |
| 137 | |
| 138 | IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow) |
| 139 | |
| 140 | void wxMenuBar::Init(size_t n, wxMenu *menus[], const wxString titles[], long style) |
| 141 | { |
| 142 | // the parent window is known after wxFrame::SetMenu() |
| 143 | m_needParent = false; |
| 144 | m_style = style; |
| 145 | m_invokingWindow = (wxWindow*) NULL; |
| 146 | |
| 147 | if (!PreCreation( (wxWindow*) NULL, wxDefaultPosition, wxDefaultSize ) || |
| 148 | !CreateBase( (wxWindow*) NULL, -1, wxDefaultPosition, wxDefaultSize, style, wxDefaultValidator, wxT("menubar") )) |
| 149 | { |
| 150 | wxFAIL_MSG( wxT("wxMenuBar creation failed") ); |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | m_menubar = gtk_menu_bar_new(); |
| 155 | m_accel = gtk_accel_group_new(); |
| 156 | |
| 157 | if (style & wxMB_DOCKABLE) |
| 158 | { |
| 159 | m_widget = gtk_handle_box_new(); |
| 160 | gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_menubar) ); |
| 161 | gtk_widget_show( GTK_WIDGET(m_menubar) ); |
| 162 | } |
| 163 | else |
| 164 | { |
| 165 | m_widget = GTK_WIDGET(m_menubar); |
| 166 | } |
| 167 | |
| 168 | PostCreation(); |
| 169 | |
| 170 | ApplyWidgetStyle(); |
| 171 | |
| 172 | for (size_t i = 0; i < n; ++i ) |
| 173 | Append(menus[i], titles[i]); |
| 174 | |
| 175 | // VZ: for some reason connecting to menus "deactivate" doesn't work (we |
| 176 | // don't get it when the menu is dismissed by clicking outside the |
| 177 | // toolbar) so we connect to the global one, even if it means that we |
| 178 | // can't pass the menu which was closed in wxMenuEvent object |
| 179 | gtk_signal_connect( GTK_OBJECT(GTK_MENU_SHELL(m_menubar)), |
| 180 | "deactivate", |
| 181 | GTK_SIGNAL_FUNC(gtk_menu_close_callback), |
| 182 | (gpointer)this ); |
| 183 | |
| 184 | } |
| 185 | |
| 186 | wxMenuBar::wxMenuBar(size_t n, wxMenu *menus[], const wxString titles[], long style) |
| 187 | { |
| 188 | Init(n, menus, titles, style); |
| 189 | } |
| 190 | |
| 191 | wxMenuBar::wxMenuBar(long style) |
| 192 | { |
| 193 | Init(0, NULL, NULL, style); |
| 194 | } |
| 195 | |
| 196 | wxMenuBar::wxMenuBar() |
| 197 | { |
| 198 | Init(0, NULL, NULL, 0); |
| 199 | } |
| 200 | |
| 201 | wxMenuBar::~wxMenuBar() |
| 202 | { |
| 203 | } |
| 204 | |
| 205 | static void wxMenubarUnsetInvokingWindow( wxMenu *menu, wxWindow *win ) |
| 206 | { |
| 207 | menu->SetInvokingWindow( (wxWindow*) NULL ); |
| 208 | |
| 209 | wxWindow *top_frame = win; |
| 210 | while (top_frame->GetParent() && !(top_frame->IsTopLevel())) |
| 211 | top_frame = top_frame->GetParent(); |
| 212 | |
| 213 | // support for native hot keys |
| 214 | gtk_accel_group_detach( menu->m_accel, ACCEL_OBJ_CAST(top_frame->m_widget) ); |
| 215 | |
| 216 | wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst(); |
| 217 | while (node) |
| 218 | { |
| 219 | wxMenuItem *menuitem = node->GetData(); |
| 220 | if (menuitem->IsSubMenu()) |
| 221 | wxMenubarUnsetInvokingWindow( menuitem->GetSubMenu(), win ); |
| 222 | node = node->GetNext(); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | static void wxMenubarSetInvokingWindow( wxMenu *menu, wxWindow *win ) |
| 227 | { |
| 228 | menu->SetInvokingWindow( win ); |
| 229 | |
| 230 | wxWindow *top_frame = win; |
| 231 | while (top_frame->GetParent() && !(top_frame->IsTopLevel())) |
| 232 | top_frame = top_frame->GetParent(); |
| 233 | |
| 234 | // support for native hot keys |
| 235 | ACCEL_OBJECT *obj = ACCEL_OBJ_CAST(top_frame->m_widget); |
| 236 | if ( !g_slist_find( ACCEL_OBJECTS(menu->m_accel), obj ) ) |
| 237 | gtk_accel_group_attach( menu->m_accel, obj ); |
| 238 | |
| 239 | wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst(); |
| 240 | while (node) |
| 241 | { |
| 242 | wxMenuItem *menuitem = node->GetData(); |
| 243 | if (menuitem->IsSubMenu()) |
| 244 | wxMenubarSetInvokingWindow( menuitem->GetSubMenu(), win ); |
| 245 | node = node->GetNext(); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | void wxMenuBar::SetInvokingWindow( wxWindow *win ) |
| 250 | { |
| 251 | m_invokingWindow = win; |
| 252 | wxWindow *top_frame = win; |
| 253 | while (top_frame->GetParent() && !(top_frame->IsTopLevel())) |
| 254 | top_frame = top_frame->GetParent(); |
| 255 | |
| 256 | // support for native key accelerators indicated by underscroes |
| 257 | ACCEL_OBJECT *obj = ACCEL_OBJ_CAST(top_frame->m_widget); |
| 258 | if ( !g_slist_find( ACCEL_OBJECTS(m_accel), obj ) ) |
| 259 | gtk_accel_group_attach( m_accel, obj ); |
| 260 | |
| 261 | wxMenuList::compatibility_iterator node = m_menus.GetFirst(); |
| 262 | while (node) |
| 263 | { |
| 264 | wxMenu *menu = node->GetData(); |
| 265 | wxMenubarSetInvokingWindow( menu, win ); |
| 266 | node = node->GetNext(); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | void wxMenuBar::UnsetInvokingWindow( wxWindow *win ) |
| 271 | { |
| 272 | m_invokingWindow = (wxWindow*) NULL; |
| 273 | wxWindow *top_frame = win; |
| 274 | while (top_frame->GetParent() && !(top_frame->IsTopLevel())) |
| 275 | top_frame = top_frame->GetParent(); |
| 276 | |
| 277 | // support for native key accelerators indicated by underscroes |
| 278 | gtk_accel_group_detach( m_accel, ACCEL_OBJ_CAST(top_frame->m_widget) ); |
| 279 | |
| 280 | wxMenuList::compatibility_iterator node = m_menus.GetFirst(); |
| 281 | while (node) |
| 282 | { |
| 283 | wxMenu *menu = node->GetData(); |
| 284 | wxMenubarUnsetInvokingWindow( menu, win ); |
| 285 | node = node->GetNext(); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | bool wxMenuBar::Append( wxMenu *menu, const wxString &title ) |
| 290 | { |
| 291 | if ( !wxMenuBarBase::Append( menu, title ) ) |
| 292 | return false; |
| 293 | |
| 294 | return GtkAppend(menu, title); |
| 295 | } |
| 296 | |
| 297 | bool wxMenuBar::GtkAppend(wxMenu *menu, const wxString& title, int pos) |
| 298 | { |
| 299 | wxString str( wxReplaceUnderscore( title ) ); |
| 300 | |
| 301 | // This doesn't have much effect right now. |
| 302 | menu->SetTitle( str ); |
| 303 | |
| 304 | // The "m_owner" is the "menu item" |
| 305 | menu->m_owner = gtk_menu_item_new_with_label( wxGTK_CONV( str ) ); |
| 306 | GtkLabel *label = GTK_LABEL( GTK_BIN(menu->m_owner)->child ); |
| 307 | // set new text |
| 308 | gtk_label_set_text( label, wxGTK_CONV( str ) ); |
| 309 | // reparse key accel |
| 310 | guint accel_key = gtk_label_parse_uline (GTK_LABEL(label), wxGTK_CONV( str ) ); |
| 311 | if (accel_key != GDK_VoidSymbol) |
| 312 | { |
| 313 | gtk_widget_add_accelerator (menu->m_owner, |
| 314 | "activate_item", |
| 315 | m_accel, //gtk_menu_ensure_uline_accel_group(GTK_MENU(m_menubar)), |
| 316 | accel_key, |
| 317 | GDK_MOD1_MASK, |
| 318 | GTK_ACCEL_LOCKED); |
| 319 | } |
| 320 | |
| 321 | gtk_widget_show( menu->m_owner ); |
| 322 | |
| 323 | gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu->m_owner), menu->m_menu ); |
| 324 | |
| 325 | if (pos == -1) |
| 326 | gtk_menu_shell_append( GTK_MENU_SHELL(m_menubar), menu->m_owner ); |
| 327 | else |
| 328 | gtk_menu_shell_insert( GTK_MENU_SHELL(m_menubar), menu->m_owner, pos ); |
| 329 | |
| 330 | gtk_signal_connect( GTK_OBJECT(menu->m_owner), "activate", |
| 331 | GTK_SIGNAL_FUNC(gtk_menu_open_callback), |
| 332 | (gpointer)menu ); |
| 333 | |
| 334 | // m_invokingWindow is set after wxFrame::SetMenuBar(). This call enables |
| 335 | // addings menu later on. |
| 336 | if (m_invokingWindow) |
| 337 | { |
| 338 | wxMenubarSetInvokingWindow( menu, m_invokingWindow ); |
| 339 | |
| 340 | // OPTIMISE ME: we should probably cache this, or pass it |
| 341 | // directly, but for now this is a minimal |
| 342 | // change to validate the new dynamic sizing. |
| 343 | // see (and refactor :) similar code in Remove |
| 344 | // below. |
| 345 | |
| 346 | wxFrame *frame = wxDynamicCast( m_invokingWindow, wxFrame ); |
| 347 | |
| 348 | if( frame ) |
| 349 | frame->UpdateMenuBarSize(); |
| 350 | } |
| 351 | |
| 352 | return true; |
| 353 | } |
| 354 | |
| 355 | bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title) |
| 356 | { |
| 357 | if ( !wxMenuBarBase::Insert(pos, menu, title) ) |
| 358 | return false; |
| 359 | |
| 360 | // TODO |
| 361 | |
| 362 | if ( !GtkAppend(menu, title, (int)pos) ) |
| 363 | return false; |
| 364 | |
| 365 | return true; |
| 366 | } |
| 367 | |
| 368 | wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title) |
| 369 | { |
| 370 | // remove the old item and insert a new one |
| 371 | wxMenu *menuOld = Remove(pos); |
| 372 | if ( menuOld && !Insert(pos, menu, title) ) |
| 373 | { |
| 374 | return (wxMenu*) NULL; |
| 375 | } |
| 376 | |
| 377 | // either Insert() succeeded or Remove() failed and menuOld is NULL |
| 378 | return menuOld; |
| 379 | } |
| 380 | |
| 381 | wxMenu *wxMenuBar::Remove(size_t pos) |
| 382 | { |
| 383 | wxMenu *menu = wxMenuBarBase::Remove(pos); |
| 384 | if ( !menu ) |
| 385 | return (wxMenu*) NULL; |
| 386 | |
| 387 | gtk_menu_item_remove_submenu( GTK_MENU_ITEM(menu->m_owner) ); |
| 388 | gtk_container_remove(GTK_CONTAINER(m_menubar), menu->m_owner); |
| 389 | |
| 390 | gtk_widget_destroy( menu->m_owner ); |
| 391 | menu->m_owner = NULL; |
| 392 | |
| 393 | if (m_invokingWindow) |
| 394 | { |
| 395 | // OPTIMISE ME: see comment in GtkAppend |
| 396 | wxFrame *frame = wxDynamicCast( m_invokingWindow, wxFrame ); |
| 397 | |
| 398 | if( frame ) |
| 399 | frame->UpdateMenuBarSize(); |
| 400 | } |
| 401 | |
| 402 | return menu; |
| 403 | } |
| 404 | |
| 405 | static int FindMenuItemRecursive( const wxMenu *menu, const wxString &menuString, const wxString &itemString ) |
| 406 | { |
| 407 | if (wxMenuItem::GetLabelFromText(menu->GetTitle()) == wxMenuItem::GetLabelFromText(menuString)) |
| 408 | { |
| 409 | int res = menu->FindItem( itemString ); |
| 410 | if (res != wxNOT_FOUND) |
| 411 | return res; |
| 412 | } |
| 413 | |
| 414 | wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst(); |
| 415 | while (node) |
| 416 | { |
| 417 | wxMenuItem *item = node->GetData(); |
| 418 | if (item->IsSubMenu()) |
| 419 | return FindMenuItemRecursive(item->GetSubMenu(), menuString, itemString); |
| 420 | |
| 421 | node = node->GetNext(); |
| 422 | } |
| 423 | |
| 424 | return wxNOT_FOUND; |
| 425 | } |
| 426 | |
| 427 | int wxMenuBar::FindMenuItem( const wxString &menuString, const wxString &itemString ) const |
| 428 | { |
| 429 | wxMenuList::compatibility_iterator node = m_menus.GetFirst(); |
| 430 | while (node) |
| 431 | { |
| 432 | wxMenu *menu = node->GetData(); |
| 433 | int res = FindMenuItemRecursive( menu, menuString, itemString); |
| 434 | if (res != -1) |
| 435 | return res; |
| 436 | node = node->GetNext(); |
| 437 | } |
| 438 | |
| 439 | return wxNOT_FOUND; |
| 440 | } |
| 441 | |
| 442 | // Find a wxMenuItem using its id. Recurses down into sub-menus |
| 443 | static wxMenuItem* FindMenuItemByIdRecursive(const wxMenu* menu, int id) |
| 444 | { |
| 445 | wxMenuItem* result = menu->FindChildItem(id); |
| 446 | |
| 447 | wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst(); |
| 448 | while ( node && result == NULL ) |
| 449 | { |
| 450 | wxMenuItem *item = node->GetData(); |
| 451 | if (item->IsSubMenu()) |
| 452 | { |
| 453 | result = FindMenuItemByIdRecursive( item->GetSubMenu(), id ); |
| 454 | } |
| 455 | node = node->GetNext(); |
| 456 | } |
| 457 | |
| 458 | return result; |
| 459 | } |
| 460 | |
| 461 | wxMenuItem* wxMenuBar::FindItem( int id, wxMenu **menuForItem ) const |
| 462 | { |
| 463 | wxMenuItem* result = 0; |
| 464 | wxMenuList::compatibility_iterator node = m_menus.GetFirst(); |
| 465 | while (node && result == 0) |
| 466 | { |
| 467 | wxMenu *menu = node->GetData(); |
| 468 | result = FindMenuItemByIdRecursive( menu, id ); |
| 469 | node = node->GetNext(); |
| 470 | } |
| 471 | |
| 472 | if ( menuForItem ) |
| 473 | { |
| 474 | *menuForItem = result ? result->GetMenu() : (wxMenu *)NULL; |
| 475 | } |
| 476 | |
| 477 | return result; |
| 478 | } |
| 479 | |
| 480 | void wxMenuBar::EnableTop( size_t pos, bool flag ) |
| 481 | { |
| 482 | wxMenuList::compatibility_iterator node = m_menus.Item( pos ); |
| 483 | |
| 484 | wxCHECK_RET( node, wxT("menu not found") ); |
| 485 | |
| 486 | wxMenu* menu = node->GetData(); |
| 487 | |
| 488 | if (menu->m_owner) |
| 489 | gtk_widget_set_sensitive( menu->m_owner, flag ); |
| 490 | } |
| 491 | |
| 492 | wxString wxMenuBar::GetLabelTop( size_t pos ) const |
| 493 | { |
| 494 | wxMenuList::compatibility_iterator node = m_menus.Item( pos ); |
| 495 | |
| 496 | wxCHECK_MSG( node, wxT("invalid"), wxT("menu not found") ); |
| 497 | |
| 498 | wxMenu* menu = node->GetData(); |
| 499 | |
| 500 | wxString label; |
| 501 | wxString text( menu->GetTitle() ); |
| 502 | for ( const wxChar *pc = text.c_str(); *pc; pc++ ) |
| 503 | { |
| 504 | if ( *pc == wxT('_') ) |
| 505 | { |
| 506 | // '_' is the escape character for GTK+ |
| 507 | continue; |
| 508 | } |
| 509 | |
| 510 | // don't remove ampersands '&' since if we have them in the menu title |
| 511 | // it means that they were doubled to indicate "&" instead of accelerator |
| 512 | |
| 513 | label += *pc; |
| 514 | } |
| 515 | |
| 516 | return label; |
| 517 | } |
| 518 | |
| 519 | void wxMenuBar::SetLabelTop( size_t pos, const wxString& label ) |
| 520 | { |
| 521 | wxMenuList::compatibility_iterator node = m_menus.Item( pos ); |
| 522 | |
| 523 | wxCHECK_RET( node, wxT("menu not found") ); |
| 524 | |
| 525 | wxMenu* menu = node->GetData(); |
| 526 | |
| 527 | const wxString str( wxReplaceUnderscore( label ) ); |
| 528 | |
| 529 | menu->SetTitle( str ); |
| 530 | |
| 531 | if (menu->m_owner) |
| 532 | { |
| 533 | GtkLabel *glabel = GTK_LABEL( GTK_BIN(menu->m_owner)->child ); |
| 534 | |
| 535 | /* set new text */ |
| 536 | gtk_label_set( glabel, wxGTK_CONV( str ) ); |
| 537 | |
| 538 | /* reparse key accel */ |
| 539 | (void)gtk_label_parse_uline (GTK_LABEL(glabel), wxGTK_CONV( str ) ); |
| 540 | gtk_accel_label_refetch( GTK_ACCEL_LABEL(glabel) ); |
| 541 | } |
| 542 | |
| 543 | } |
| 544 | |
| 545 | //----------------------------------------------------------------------------- |
| 546 | // "activate" |
| 547 | //----------------------------------------------------------------------------- |
| 548 | |
| 549 | extern "C" { |
| 550 | static void gtk_menu_clicked_callback( GtkWidget *widget, wxMenu *menu ) |
| 551 | { |
| 552 | if (g_isIdle) |
| 553 | wxapp_install_idle_handler(); |
| 554 | |
| 555 | int id = menu->FindMenuIdByMenuItem(widget); |
| 556 | |
| 557 | /* should find it for normal (not popup) menu */ |
| 558 | wxASSERT_MSG( (id != -1) || (menu->GetInvokingWindow() != NULL), |
| 559 | _T("menu item not found in gtk_menu_clicked_callback") ); |
| 560 | |
| 561 | if (!menu->IsEnabled(id)) |
| 562 | return; |
| 563 | |
| 564 | wxMenuItem* item = menu->FindChildItem( id ); |
| 565 | wxCHECK_RET( item, wxT("error in menu item callback") ); |
| 566 | |
| 567 | if ( item->GetId() == wxGTK_TITLE_ID ) |
| 568 | { |
| 569 | // ignore events from the menu title |
| 570 | return; |
| 571 | } |
| 572 | |
| 573 | if (item->IsCheckable()) |
| 574 | { |
| 575 | bool isReallyChecked = item->IsChecked(), |
| 576 | isInternallyChecked = item->wxMenuItemBase::IsChecked(); |
| 577 | |
| 578 | // ensure that the internal state is always consistent with what is |
| 579 | // shown on the screen |
| 580 | item->wxMenuItemBase::Check(isReallyChecked); |
| 581 | |
| 582 | // we must not report the events for the radio button going up nor the |
| 583 | // events resulting from the calls to wxMenuItem::Check() |
| 584 | if ( (item->GetKind() == wxITEM_RADIO && !isReallyChecked) || |
| 585 | (isInternallyChecked == isReallyChecked) ) |
| 586 | { |
| 587 | return; |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | |
| 592 | // Is this menu on a menubar? (possibly nested) |
| 593 | wxFrame* frame = NULL; |
| 594 | if(menu->IsAttached()) |
| 595 | frame = menu->GetMenuBar()->GetFrame(); |
| 596 | |
| 597 | // FIXME: why do we have to call wxFrame::GetEventHandler() directly here? |
| 598 | // normally wxMenu::SendEvent() should be enough, if it doesn't work |
| 599 | // in wxGTK then we have a bug in wxMenu::GetInvokingWindow() which |
| 600 | // should be fixed instead of working around it here... |
| 601 | if (frame) |
| 602 | { |
| 603 | // If it is attached then let the frame send the event. |
| 604 | // Don't call frame->ProcessCommand(id) because it toggles |
| 605 | // checkable items and we've already done that above. |
| 606 | wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, id); |
| 607 | commandEvent.SetEventObject(frame); |
| 608 | if (item->IsCheckable()) |
| 609 | commandEvent.SetInt(item->IsChecked()); |
| 610 | commandEvent.SetEventObject(menu); |
| 611 | |
| 612 | frame->GetEventHandler()->ProcessEvent(commandEvent); |
| 613 | } |
| 614 | else |
| 615 | { |
| 616 | // otherwise let the menu have it |
| 617 | menu->SendEvent(id, item->IsCheckable() ? item->IsChecked() : -1); |
| 618 | } |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | //----------------------------------------------------------------------------- |
| 623 | // "select" |
| 624 | //----------------------------------------------------------------------------- |
| 625 | |
| 626 | extern "C" { |
| 627 | static void gtk_menu_hilight_callback( GtkWidget *widget, wxMenu *menu ) |
| 628 | { |
| 629 | if (g_isIdle) wxapp_install_idle_handler(); |
| 630 | |
| 631 | int id = menu->FindMenuIdByMenuItem(widget); |
| 632 | |
| 633 | wxASSERT( id != -1 ); // should find it! |
| 634 | |
| 635 | if (!menu->IsEnabled(id)) |
| 636 | return; |
| 637 | |
| 638 | wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, id ); |
| 639 | event.SetEventObject( menu ); |
| 640 | |
| 641 | wxEvtHandler* handler = menu->GetEventHandler(); |
| 642 | if (handler && handler->ProcessEvent(event)) |
| 643 | return; |
| 644 | |
| 645 | wxWindow *win = menu->GetInvokingWindow(); |
| 646 | if (win) win->GetEventHandler()->ProcessEvent( event ); |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | //----------------------------------------------------------------------------- |
| 651 | // "deselect" |
| 652 | //----------------------------------------------------------------------------- |
| 653 | |
| 654 | extern "C" { |
| 655 | static void gtk_menu_nolight_callback( GtkWidget *widget, wxMenu *menu ) |
| 656 | { |
| 657 | if (g_isIdle) wxapp_install_idle_handler(); |
| 658 | |
| 659 | int id = menu->FindMenuIdByMenuItem(widget); |
| 660 | |
| 661 | wxASSERT( id != -1 ); // should find it! |
| 662 | |
| 663 | if (!menu->IsEnabled(id)) |
| 664 | return; |
| 665 | |
| 666 | wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, -1 ); |
| 667 | event.SetEventObject( menu ); |
| 668 | |
| 669 | wxEvtHandler* handler = menu->GetEventHandler(); |
| 670 | if (handler && handler->ProcessEvent(event)) |
| 671 | return; |
| 672 | |
| 673 | wxWindow *win = menu->GetInvokingWindow(); |
| 674 | if (win) |
| 675 | win->GetEventHandler()->ProcessEvent( event ); |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | //----------------------------------------------------------------------------- |
| 680 | // wxMenuItem |
| 681 | //----------------------------------------------------------------------------- |
| 682 | |
| 683 | IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject) |
| 684 | |
| 685 | wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu, |
| 686 | int id, |
| 687 | const wxString& name, |
| 688 | const wxString& help, |
| 689 | wxItemKind kind, |
| 690 | wxMenu *subMenu) |
| 691 | { |
| 692 | return new wxMenuItem(parentMenu, id, name, help, kind, subMenu); |
| 693 | } |
| 694 | |
| 695 | wxMenuItem::wxMenuItem(wxMenu *parentMenu, |
| 696 | int id, |
| 697 | const wxString& text, |
| 698 | const wxString& help, |
| 699 | wxItemKind kind, |
| 700 | wxMenu *subMenu) |
| 701 | : wxMenuItemBase(parentMenu, id, text, help, kind, subMenu) |
| 702 | { |
| 703 | Init(text); |
| 704 | } |
| 705 | |
| 706 | wxMenuItem::wxMenuItem(wxMenu *parentMenu, |
| 707 | int id, |
| 708 | const wxString& text, |
| 709 | const wxString& help, |
| 710 | bool isCheckable, |
| 711 | wxMenu *subMenu) |
| 712 | : wxMenuItemBase(parentMenu, id, text, help, |
| 713 | isCheckable ? wxITEM_CHECK : wxITEM_NORMAL, subMenu) |
| 714 | { |
| 715 | Init(text); |
| 716 | } |
| 717 | |
| 718 | void wxMenuItem::Init(const wxString& text) |
| 719 | { |
| 720 | m_labelWidget = (GtkWidget *) NULL; |
| 721 | m_menuItem = (GtkWidget *) NULL; |
| 722 | |
| 723 | DoSetText(text); |
| 724 | } |
| 725 | |
| 726 | wxMenuItem::~wxMenuItem() |
| 727 | { |
| 728 | // don't delete menu items, the menus take care of that |
| 729 | } |
| 730 | |
| 731 | // return the menu item text without any menu accels |
| 732 | /* static */ |
| 733 | wxString wxMenuItemBase::GetLabelFromText(const wxString& text) |
| 734 | { |
| 735 | wxString label; |
| 736 | |
| 737 | for ( const wxChar *pc = text.c_str(); *pc; pc++ ) |
| 738 | { |
| 739 | if ( *pc == wxT('\t')) |
| 740 | break; |
| 741 | |
| 742 | if ( *pc == wxT('_') ) |
| 743 | { |
| 744 | // GTK 1.2 escapes "xxx_xxx" to "xxx__xxx" |
| 745 | pc++; |
| 746 | label += *pc; |
| 747 | continue; |
| 748 | } |
| 749 | |
| 750 | if ( (*pc == wxT('&')) && (*(pc+1) != wxT('&')) ) |
| 751 | { |
| 752 | // wxMSW escapes "&" |
| 753 | // "&" is doubled to indicate "&" instead of accelerator |
| 754 | continue; |
| 755 | } |
| 756 | |
| 757 | label += *pc; |
| 758 | } |
| 759 | |
| 760 | // wxPrintf( wxT("GetLabelFromText(): text %s label %s\n"), text.c_str(), label.c_str() ); |
| 761 | |
| 762 | return label; |
| 763 | } |
| 764 | |
| 765 | void wxMenuItem::SetText( const wxString& str ) |
| 766 | { |
| 767 | // Some optimization to avoid flicker |
| 768 | wxString oldLabel = m_text; |
| 769 | oldLabel = wxStripMenuCodes(oldLabel); |
| 770 | oldLabel.Replace(wxT("_"), wxEmptyString); |
| 771 | wxString label1 = wxStripMenuCodes(str); |
| 772 | wxString oldhotkey = GetHotKey(); // Store the old hotkey in Ctrl-foo format |
| 773 | wxCharBuffer oldbuf = wxGTK_CONV( GetGtkHotKey(*this) ); // and as <control>foo |
| 774 | |
| 775 | DoSetText(str); |
| 776 | |
| 777 | if (oldLabel == label1 && |
| 778 | oldhotkey == GetHotKey()) // Make sure we can change a hotkey even if the label is unaltered |
| 779 | return; |
| 780 | |
| 781 | if (m_menuItem) |
| 782 | { |
| 783 | GtkLabel *label; |
| 784 | if (m_labelWidget) |
| 785 | label = (GtkLabel*) m_labelWidget; |
| 786 | else |
| 787 | label = GTK_LABEL( GTK_BIN(m_menuItem)->child ); |
| 788 | |
| 789 | // set new text |
| 790 | gtk_label_set( label, wxGTK_CONV( m_text ) ); |
| 791 | |
| 792 | // reparse key accel |
| 793 | (void)gtk_label_parse_uline (GTK_LABEL(label), wxGTK_CONV(m_text) ); |
| 794 | gtk_accel_label_refetch( GTK_ACCEL_LABEL(label) ); |
| 795 | } |
| 796 | |
| 797 | guint accel_key; |
| 798 | GdkModifierType accel_mods; |
| 799 | gtk_accelerator_parse( (const char*) oldbuf, &accel_key, &accel_mods); |
| 800 | if (accel_key != 0) |
| 801 | { |
| 802 | gtk_widget_remove_accelerator( GTK_WIDGET(m_menuItem), |
| 803 | m_parentMenu->m_accel, |
| 804 | accel_key, |
| 805 | accel_mods ); |
| 806 | } |
| 807 | |
| 808 | wxCharBuffer buf = wxGTK_CONV( GetGtkHotKey(*this) ); |
| 809 | gtk_accelerator_parse( (const char*) buf, &accel_key, &accel_mods); |
| 810 | if (accel_key != 0) |
| 811 | { |
| 812 | gtk_widget_add_accelerator( GTK_WIDGET(m_menuItem), |
| 813 | "activate", |
| 814 | m_parentMenu->m_accel, |
| 815 | accel_key, |
| 816 | accel_mods, |
| 817 | GTK_ACCEL_VISIBLE); |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | // it's valid for this function to be called even if m_menuItem == NULL |
| 822 | void wxMenuItem::DoSetText( const wxString& str ) |
| 823 | { |
| 824 | // '\t' is the deliminator indicating a hot key |
| 825 | m_text.Empty(); |
| 826 | const wxChar *pc = str; |
| 827 | while ( (*pc != wxT('\0')) && (*pc != wxT('\t')) ) |
| 828 | { |
| 829 | if ((*pc == wxT('&')) && (*(pc+1) == wxT('&'))) |
| 830 | { |
| 831 | // "&" is doubled to indicate "&" instead of accelerator |
| 832 | ++pc; |
| 833 | m_text << wxT('&'); |
| 834 | } |
| 835 | else if (*pc == wxT('&')) |
| 836 | { |
| 837 | m_text << wxT('_'); |
| 838 | } |
| 839 | else if ( *pc == wxT('_') ) // escape underscores |
| 840 | { |
| 841 | m_text << wxT("__"); |
| 842 | } |
| 843 | else |
| 844 | { |
| 845 | m_text << *pc; |
| 846 | } |
| 847 | ++pc; |
| 848 | } |
| 849 | |
| 850 | m_hotKey = wxEmptyString; |
| 851 | |
| 852 | if(*pc == wxT('\t')) |
| 853 | { |
| 854 | pc++; |
| 855 | m_hotKey = pc; |
| 856 | } |
| 857 | |
| 858 | // wxPrintf( wxT("DoSetText(): str %s m_text %s hotkey %s\n"), str.c_str(), m_text.c_str(), m_hotKey.c_str() ); |
| 859 | } |
| 860 | |
| 861 | #if wxUSE_ACCEL |
| 862 | |
| 863 | wxAcceleratorEntry *wxMenuItem::GetAccel() const |
| 864 | { |
| 865 | if ( !GetHotKey() ) |
| 866 | { |
| 867 | // nothing |
| 868 | return (wxAcceleratorEntry *)NULL; |
| 869 | } |
| 870 | |
| 871 | // as wxGetAccelFromString() looks for TAB, insert a dummy one here |
| 872 | wxString label; |
| 873 | label << wxT('\t') << GetHotKey(); |
| 874 | |
| 875 | return wxGetAccelFromString(label); |
| 876 | } |
| 877 | |
| 878 | #endif // wxUSE_ACCEL |
| 879 | |
| 880 | void wxMenuItem::Check( bool check ) |
| 881 | { |
| 882 | wxCHECK_RET( m_menuItem, wxT("invalid menu item") ); |
| 883 | |
| 884 | if (check == m_isChecked) |
| 885 | return; |
| 886 | |
| 887 | wxMenuItemBase::Check( check ); |
| 888 | |
| 889 | switch ( GetKind() ) |
| 890 | { |
| 891 | case wxITEM_CHECK: |
| 892 | case wxITEM_RADIO: |
| 893 | gtk_check_menu_item_set_state( (GtkCheckMenuItem*)m_menuItem, (gint)check ); |
| 894 | break; |
| 895 | |
| 896 | default: |
| 897 | wxFAIL_MSG( _T("can't check this item") ); |
| 898 | } |
| 899 | } |
| 900 | |
| 901 | void wxMenuItem::Enable( bool enable ) |
| 902 | { |
| 903 | wxCHECK_RET( m_menuItem, wxT("invalid menu item") ); |
| 904 | |
| 905 | gtk_widget_set_sensitive( m_menuItem, enable ); |
| 906 | wxMenuItemBase::Enable( enable ); |
| 907 | } |
| 908 | |
| 909 | bool wxMenuItem::IsChecked() const |
| 910 | { |
| 911 | wxCHECK_MSG( m_menuItem, false, wxT("invalid menu item") ); |
| 912 | |
| 913 | wxCHECK_MSG( IsCheckable(), false, |
| 914 | wxT("can't get state of uncheckable item!") ); |
| 915 | |
| 916 | return ((GtkCheckMenuItem*)m_menuItem)->active != 0; |
| 917 | } |
| 918 | |
| 919 | //----------------------------------------------------------------------------- |
| 920 | // wxMenu |
| 921 | //----------------------------------------------------------------------------- |
| 922 | |
| 923 | IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler) |
| 924 | |
| 925 | void wxMenu::Init() |
| 926 | { |
| 927 | m_accel = gtk_accel_group_new(); |
| 928 | m_menu = gtk_menu_new(); |
| 929 | // NB: keep reference to the menu so that it is not destroyed behind |
| 930 | // our back by GTK+ e.g. when it is removed from menubar: |
| 931 | gtk_widget_ref(m_menu); |
| 932 | |
| 933 | m_owner = (GtkWidget*) NULL; |
| 934 | |
| 935 | // Tearoffs are entries, just like separators. So if we want this |
| 936 | // menu to be a tear-off one, we just append a tearoff entry |
| 937 | // immediately. |
| 938 | if ( m_style & wxMENU_TEAROFF ) |
| 939 | { |
| 940 | GtkWidget *tearoff = gtk_tearoff_menu_item_new(); |
| 941 | |
| 942 | gtk_menu_append(GTK_MENU(m_menu), tearoff); |
| 943 | } |
| 944 | |
| 945 | m_prevRadio = NULL; |
| 946 | |
| 947 | // append the title as the very first entry if we have it |
| 948 | if ( !m_title.empty() ) |
| 949 | { |
| 950 | Append(wxGTK_TITLE_ID, m_title); |
| 951 | AppendSeparator(); |
| 952 | } |
| 953 | } |
| 954 | |
| 955 | wxMenu::~wxMenu() |
| 956 | { |
| 957 | WX_CLEAR_LIST(wxMenuItemList, m_items); |
| 958 | |
| 959 | if ( GTK_IS_WIDGET( m_menu )) |
| 960 | { |
| 961 | // see wxMenu::Init |
| 962 | gtk_widget_unref( m_menu ); |
| 963 | // if the menu is inserted in another menu at this time, there was |
| 964 | // one more reference to it: |
| 965 | if ( m_owner ) |
| 966 | gtk_widget_destroy( m_menu ); |
| 967 | } |
| 968 | } |
| 969 | |
| 970 | bool wxMenu::GtkAppend(wxMenuItem *mitem, int pos) |
| 971 | { |
| 972 | GtkWidget *menuItem; |
| 973 | |
| 974 | wxString text; |
| 975 | GtkLabel* label = NULL; |
| 976 | |
| 977 | if ( mitem->IsSeparator() ) |
| 978 | { |
| 979 | // TODO |
| 980 | menuItem = gtk_menu_item_new(); |
| 981 | } |
| 982 | else if (mitem->GetBitmap().Ok()) |
| 983 | { |
| 984 | text = mitem->GetText(); |
| 985 | const wxBitmap *bitmap = &mitem->GetBitmap(); |
| 986 | |
| 987 | // TODO |
| 988 | wxUnusedVar(bitmap); |
| 989 | menuItem = gtk_menu_item_new_with_label( wxGTK_CONV( text ) ); |
| 990 | label = GTK_LABEL( GTK_BIN(menuItem)->child ); |
| 991 | |
| 992 | m_prevRadio = NULL; |
| 993 | } |
| 994 | else // a normal item |
| 995 | { |
| 996 | // text has "_" instead of "&" after mitem->SetText() so don't use it |
| 997 | text = mitem->GetText() ; |
| 998 | |
| 999 | switch ( mitem->GetKind() ) |
| 1000 | { |
| 1001 | case wxITEM_CHECK: |
| 1002 | { |
| 1003 | menuItem = gtk_check_menu_item_new_with_label( wxGTK_CONV( text ) ); |
| 1004 | label = GTK_LABEL( GTK_BIN(menuItem)->child ); |
| 1005 | // set new text |
| 1006 | gtk_label_set_text( label, wxGTK_CONV( text ) ); |
| 1007 | m_prevRadio = NULL; |
| 1008 | break; |
| 1009 | } |
| 1010 | |
| 1011 | case wxITEM_RADIO: |
| 1012 | { |
| 1013 | GSList *group = NULL; |
| 1014 | if ( m_prevRadio == NULL ) |
| 1015 | { |
| 1016 | // start of a new radio group |
| 1017 | m_prevRadio = menuItem = gtk_radio_menu_item_new_with_label( group, wxGTK_CONV( text ) ); |
| 1018 | label = GTK_LABEL( GTK_BIN(menuItem)->child ); |
| 1019 | // set new text |
| 1020 | gtk_label_set_text( label, wxGTK_CONV( text ) ); |
| 1021 | } |
| 1022 | else // continue the radio group |
| 1023 | { |
| 1024 | group = gtk_radio_menu_item_group (GTK_RADIO_MENU_ITEM (m_prevRadio)); |
| 1025 | m_prevRadio = menuItem = gtk_radio_menu_item_new_with_label( group, wxGTK_CONV( text ) ); |
| 1026 | label = GTK_LABEL( GTK_BIN(menuItem)->child ); |
| 1027 | } |
| 1028 | break; |
| 1029 | } |
| 1030 | |
| 1031 | default: |
| 1032 | wxFAIL_MSG( _T("unexpected menu item kind") ); |
| 1033 | // fall through |
| 1034 | |
| 1035 | case wxITEM_NORMAL: |
| 1036 | { |
| 1037 | menuItem = gtk_menu_item_new_with_label( wxGTK_CONV( text ) ); |
| 1038 | label = GTK_LABEL( GTK_BIN(menuItem)->child ); |
| 1039 | m_prevRadio = NULL; |
| 1040 | break; |
| 1041 | } |
| 1042 | } |
| 1043 | |
| 1044 | } |
| 1045 | |
| 1046 | guint accel_key; |
| 1047 | GdkModifierType accel_mods; |
| 1048 | wxCharBuffer buf = wxGTK_CONV( GetGtkHotKey(*mitem) ); |
| 1049 | |
| 1050 | // wxPrintf( wxT("item: %s hotkey %s\n"), mitem->GetText().c_str(), GetGtkHotKey(*mitem).c_str() ); |
| 1051 | gtk_accelerator_parse( (const char*) buf, &accel_key, &accel_mods); |
| 1052 | if (accel_key != 0) |
| 1053 | { |
| 1054 | gtk_widget_add_accelerator (GTK_WIDGET(menuItem), |
| 1055 | "activate", |
| 1056 | m_accel, |
| 1057 | accel_key, |
| 1058 | accel_mods, |
| 1059 | GTK_ACCEL_VISIBLE); |
| 1060 | } |
| 1061 | |
| 1062 | if (pos == -1) |
| 1063 | gtk_menu_shell_append(GTK_MENU_SHELL(m_menu), menuItem); |
| 1064 | else |
| 1065 | gtk_menu_shell_insert(GTK_MENU_SHELL(m_menu), menuItem, pos); |
| 1066 | |
| 1067 | gtk_widget_show( menuItem ); |
| 1068 | |
| 1069 | if ( !mitem->IsSeparator() ) |
| 1070 | { |
| 1071 | wxASSERT_MSG( menuItem, wxT("invalid menuitem") ); |
| 1072 | |
| 1073 | gtk_signal_connect( GTK_OBJECT(menuItem), "select", |
| 1074 | GTK_SIGNAL_FUNC(gtk_menu_hilight_callback), |
| 1075 | (gpointer)this ); |
| 1076 | |
| 1077 | gtk_signal_connect( GTK_OBJECT(menuItem), "deselect", |
| 1078 | GTK_SIGNAL_FUNC(gtk_menu_nolight_callback), |
| 1079 | (gpointer)this ); |
| 1080 | |
| 1081 | if ( mitem->IsSubMenu() && mitem->GetKind() != wxITEM_RADIO && mitem->GetKind() != wxITEM_CHECK ) |
| 1082 | { |
| 1083 | gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), mitem->GetSubMenu()->m_menu ); |
| 1084 | |
| 1085 | gtk_widget_show( mitem->GetSubMenu()->m_menu ); |
| 1086 | |
| 1087 | // if adding a submenu to a menu already existing in the menu bar, we |
| 1088 | // must set invoking window to allow processing events from this |
| 1089 | // submenu |
| 1090 | if ( m_invokingWindow ) |
| 1091 | wxMenubarSetInvokingWindow(mitem->GetSubMenu(), m_invokingWindow); |
| 1092 | } |
| 1093 | else |
| 1094 | { |
| 1095 | gtk_signal_connect( GTK_OBJECT(menuItem), "activate", |
| 1096 | GTK_SIGNAL_FUNC(gtk_menu_clicked_callback), |
| 1097 | (gpointer)this ); |
| 1098 | } |
| 1099 | |
| 1100 | guint accel_key = gtk_label_parse_uline (GTK_LABEL(label), wxGTK_CONV( text ) ); |
| 1101 | if (accel_key != GDK_VoidSymbol) |
| 1102 | { |
| 1103 | gtk_widget_add_accelerator (menuItem, |
| 1104 | "activate_item", |
| 1105 | gtk_menu_ensure_uline_accel_group(GTK_MENU(m_menu)), |
| 1106 | accel_key, |
| 1107 | GDK_MOD1_MASK, |
| 1108 | GTK_ACCEL_LOCKED); |
| 1109 | } |
| 1110 | } |
| 1111 | |
| 1112 | mitem->SetMenuItem(menuItem); |
| 1113 | |
| 1114 | if (ms_locked) |
| 1115 | { |
| 1116 | // This doesn't even exist! |
| 1117 | // gtk_widget_lock_accelerators(mitem->GetMenuItem()); |
| 1118 | } |
| 1119 | |
| 1120 | return true; |
| 1121 | } |
| 1122 | |
| 1123 | wxMenuItem* wxMenu::DoAppend(wxMenuItem *mitem) |
| 1124 | { |
| 1125 | if (!GtkAppend(mitem)) |
| 1126 | return NULL; |
| 1127 | |
| 1128 | return wxMenuBase::DoAppend(mitem); |
| 1129 | } |
| 1130 | |
| 1131 | wxMenuItem* wxMenu::DoInsert(size_t pos, wxMenuItem *item) |
| 1132 | { |
| 1133 | if ( !wxMenuBase::DoInsert(pos, item) ) |
| 1134 | return NULL; |
| 1135 | |
| 1136 | // TODO |
| 1137 | if ( !GtkAppend(item, (int)pos) ) |
| 1138 | return NULL; |
| 1139 | |
| 1140 | return item; |
| 1141 | } |
| 1142 | |
| 1143 | wxMenuItem *wxMenu::DoRemove(wxMenuItem *item) |
| 1144 | { |
| 1145 | if ( !wxMenuBase::DoRemove(item) ) |
| 1146 | return (wxMenuItem *)NULL; |
| 1147 | |
| 1148 | // TODO: this code doesn't delete the item factory item and this seems |
| 1149 | // impossible as of GTK 1.2.6. |
| 1150 | gtk_widget_destroy( item->GetMenuItem() ); |
| 1151 | |
| 1152 | return item; |
| 1153 | } |
| 1154 | |
| 1155 | int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const |
| 1156 | { |
| 1157 | wxMenuItemList::compatibility_iterator node = m_items.GetFirst(); |
| 1158 | while (node) |
| 1159 | { |
| 1160 | wxMenuItem *item = node->GetData(); |
| 1161 | if (item->GetMenuItem() == menuItem) |
| 1162 | return item->GetId(); |
| 1163 | node = node->GetNext(); |
| 1164 | } |
| 1165 | |
| 1166 | return wxNOT_FOUND; |
| 1167 | } |
| 1168 | |
| 1169 | // ---------------------------------------------------------------------------- |
| 1170 | // helpers |
| 1171 | // ---------------------------------------------------------------------------- |
| 1172 | |
| 1173 | #if wxUSE_ACCEL |
| 1174 | |
| 1175 | static wxString GetGtkHotKey( const wxMenuItem& item ) |
| 1176 | { |
| 1177 | wxString hotkey; |
| 1178 | |
| 1179 | wxAcceleratorEntry *accel = item.GetAccel(); |
| 1180 | if ( accel ) |
| 1181 | { |
| 1182 | int flags = accel->GetFlags(); |
| 1183 | if ( flags & wxACCEL_ALT ) |
| 1184 | hotkey += wxT("<alt>"); |
| 1185 | if ( flags & wxACCEL_CTRL ) |
| 1186 | hotkey += wxT("<control>"); |
| 1187 | if ( flags & wxACCEL_SHIFT ) |
| 1188 | hotkey += wxT("<shift>"); |
| 1189 | |
| 1190 | int code = accel->GetKeyCode(); |
| 1191 | switch ( code ) |
| 1192 | { |
| 1193 | case WXK_F1: |
| 1194 | case WXK_F2: |
| 1195 | case WXK_F3: |
| 1196 | case WXK_F4: |
| 1197 | case WXK_F5: |
| 1198 | case WXK_F6: |
| 1199 | case WXK_F7: |
| 1200 | case WXK_F8: |
| 1201 | case WXK_F9: |
| 1202 | case WXK_F10: |
| 1203 | case WXK_F11: |
| 1204 | case WXK_F12: |
| 1205 | case WXK_F13: |
| 1206 | case WXK_F14: |
| 1207 | case WXK_F15: |
| 1208 | case WXK_F16: |
| 1209 | case WXK_F17: |
| 1210 | case WXK_F18: |
| 1211 | case WXK_F19: |
| 1212 | case WXK_F20: |
| 1213 | case WXK_F21: |
| 1214 | case WXK_F22: |
| 1215 | case WXK_F23: |
| 1216 | case WXK_F24: |
| 1217 | hotkey += wxString::Format(wxT("F%d"), code - WXK_F1 + 1); |
| 1218 | break; |
| 1219 | |
| 1220 | // TODO: we should use gdk_keyval_name() (a.k.a. |
| 1221 | // XKeysymToString) here as well as hardcoding the keysym |
| 1222 | // names this might be not portable |
| 1223 | case WXK_INSERT: |
| 1224 | hotkey << wxT("Insert" ); |
| 1225 | break; |
| 1226 | case WXK_DELETE: |
| 1227 | hotkey << wxT("Delete" ); |
| 1228 | break; |
| 1229 | case WXK_UP: |
| 1230 | hotkey << wxT("Up" ); |
| 1231 | break; |
| 1232 | case WXK_DOWN: |
| 1233 | hotkey << wxT("Down" ); |
| 1234 | break; |
| 1235 | case WXK_PAGEUP: |
| 1236 | hotkey << wxT("PgUp" ); |
| 1237 | break; |
| 1238 | case WXK_PAGEDOWN: |
| 1239 | hotkey << wxT("PgDn" ); |
| 1240 | break; |
| 1241 | case WXK_LEFT: |
| 1242 | hotkey << wxT("Left" ); |
| 1243 | break; |
| 1244 | case WXK_RIGHT: |
| 1245 | hotkey << wxT("Right" ); |
| 1246 | break; |
| 1247 | case WXK_HOME: |
| 1248 | hotkey << wxT("Home" ); |
| 1249 | break; |
| 1250 | case WXK_END: |
| 1251 | hotkey << wxT("End" ); |
| 1252 | break; |
| 1253 | case WXK_RETURN: |
| 1254 | hotkey << wxT("Return" ); |
| 1255 | break; |
| 1256 | case WXK_BACK: |
| 1257 | hotkey << wxT("BackSpace" ); |
| 1258 | break; |
| 1259 | case WXK_TAB: |
| 1260 | hotkey << wxT("Tab" ); |
| 1261 | break; |
| 1262 | case WXK_ESCAPE: |
| 1263 | hotkey << wxT("Esc" ); |
| 1264 | break; |
| 1265 | case WXK_SPACE: |
| 1266 | hotkey << wxT("space" ); |
| 1267 | break; |
| 1268 | case WXK_MULTIPLY: |
| 1269 | hotkey << wxT("Multiply" ); |
| 1270 | break; |
| 1271 | case WXK_ADD: |
| 1272 | hotkey << wxT("Add" ); |
| 1273 | break; |
| 1274 | case WXK_SEPARATOR: |
| 1275 | hotkey << wxT("Separator" ); |
| 1276 | break; |
| 1277 | case WXK_SUBTRACT: |
| 1278 | hotkey << wxT("Subtract" ); |
| 1279 | break; |
| 1280 | case WXK_DECIMAL: |
| 1281 | hotkey << wxT("Decimal" ); |
| 1282 | break; |
| 1283 | case WXK_DIVIDE: |
| 1284 | hotkey << wxT("Divide" ); |
| 1285 | break; |
| 1286 | case WXK_CANCEL: |
| 1287 | hotkey << wxT("Cancel" ); |
| 1288 | break; |
| 1289 | case WXK_CLEAR: |
| 1290 | hotkey << wxT("Clear" ); |
| 1291 | break; |
| 1292 | case WXK_MENU: |
| 1293 | hotkey << wxT("Menu" ); |
| 1294 | break; |
| 1295 | case WXK_PAUSE: |
| 1296 | hotkey << wxT("Pause" ); |
| 1297 | break; |
| 1298 | case WXK_CAPITAL: |
| 1299 | hotkey << wxT("Capital" ); |
| 1300 | break; |
| 1301 | case WXK_SELECT: |
| 1302 | hotkey << wxT("Select" ); |
| 1303 | break; |
| 1304 | case WXK_PRINT: |
| 1305 | hotkey << wxT("Print" ); |
| 1306 | break; |
| 1307 | case WXK_EXECUTE: |
| 1308 | hotkey << wxT("Execute" ); |
| 1309 | break; |
| 1310 | case WXK_SNAPSHOT: |
| 1311 | hotkey << wxT("Snapshot" ); |
| 1312 | break; |
| 1313 | case WXK_HELP: |
| 1314 | hotkey << wxT("Help" ); |
| 1315 | break; |
| 1316 | case WXK_NUMLOCK: |
| 1317 | hotkey << wxT("Num_Lock" ); |
| 1318 | break; |
| 1319 | case WXK_SCROLL: |
| 1320 | hotkey << wxT("Scroll_Lock" ); |
| 1321 | break; |
| 1322 | case WXK_NUMPAD_INSERT: |
| 1323 | hotkey << wxT("KP_Insert" ); |
| 1324 | break; |
| 1325 | case WXK_NUMPAD_DELETE: |
| 1326 | hotkey << wxT("KP_Delete" ); |
| 1327 | break; |
| 1328 | case WXK_NUMPAD_SPACE: |
| 1329 | hotkey << wxT("KP_Space" ); |
| 1330 | break; |
| 1331 | case WXK_NUMPAD_TAB: |
| 1332 | hotkey << wxT("KP_Tab" ); |
| 1333 | break; |
| 1334 | case WXK_NUMPAD_ENTER: |
| 1335 | hotkey << wxT("KP_Enter" ); |
| 1336 | break; |
| 1337 | case WXK_NUMPAD_F1: case WXK_NUMPAD_F2: case WXK_NUMPAD_F3: |
| 1338 | case WXK_NUMPAD_F4: |
| 1339 | hotkey += wxString::Format(wxT("KP_F%d"), code - WXK_NUMPAD_F1 + 1); |
| 1340 | break; |
| 1341 | case WXK_NUMPAD_HOME: |
| 1342 | hotkey << wxT("KP_Home" ); |
| 1343 | break; |
| 1344 | case WXK_NUMPAD_LEFT: |
| 1345 | hotkey << wxT("KP_Left" ); |
| 1346 | break; |
| 1347 | case WXK_NUMPAD_UP: |
| 1348 | hotkey << wxT("KP_Up" ); |
| 1349 | break; |
| 1350 | case WXK_NUMPAD_RIGHT: |
| 1351 | hotkey << wxT("KP_Right" ); |
| 1352 | break; |
| 1353 | case WXK_NUMPAD_DOWN: |
| 1354 | hotkey << wxT("KP_Down" ); |
| 1355 | break; |
| 1356 | case WXK_NUMPAD_PAGEUP: |
| 1357 | hotkey << wxT("KP_PgUp" ); |
| 1358 | break; |
| 1359 | case WXK_NUMPAD_PAGEDOWN: |
| 1360 | hotkey << wxT("KP_PgDn" ); |
| 1361 | break; |
| 1362 | case WXK_NUMPAD_END: |
| 1363 | hotkey << wxT("KP_End" ); |
| 1364 | break; |
| 1365 | case WXK_NUMPAD_BEGIN: |
| 1366 | hotkey << wxT("KP_Begin" ); |
| 1367 | break; |
| 1368 | case WXK_NUMPAD_EQUAL: |
| 1369 | hotkey << wxT("KP_Equal" ); |
| 1370 | break; |
| 1371 | case WXK_NUMPAD_MULTIPLY: |
| 1372 | hotkey << wxT("KP_Multiply" ); |
| 1373 | break; |
| 1374 | case WXK_NUMPAD_ADD: |
| 1375 | hotkey << wxT("KP_Add" ); |
| 1376 | break; |
| 1377 | case WXK_NUMPAD_SEPARATOR: |
| 1378 | hotkey << wxT("KP_Separator" ); |
| 1379 | break; |
| 1380 | case WXK_NUMPAD_SUBTRACT: |
| 1381 | hotkey << wxT("KP_Subtract" ); |
| 1382 | break; |
| 1383 | case WXK_NUMPAD_DECIMAL: |
| 1384 | hotkey << wxT("KP_Decimal" ); |
| 1385 | break; |
| 1386 | case WXK_NUMPAD_DIVIDE: |
| 1387 | hotkey << wxT("KP_Divide" ); |
| 1388 | break; |
| 1389 | case WXK_NUMPAD0: case WXK_NUMPAD1: case WXK_NUMPAD2: |
| 1390 | case WXK_NUMPAD3: case WXK_NUMPAD4: case WXK_NUMPAD5: |
| 1391 | case WXK_NUMPAD6: case WXK_NUMPAD7: case WXK_NUMPAD8: case WXK_NUMPAD9: |
| 1392 | hotkey += wxString::Format(wxT("KP_%d"), code - WXK_NUMPAD0); |
| 1393 | break; |
| 1394 | case WXK_WINDOWS_LEFT: |
| 1395 | hotkey << wxT("Super_L" ); |
| 1396 | break; |
| 1397 | case WXK_WINDOWS_RIGHT: |
| 1398 | hotkey << wxT("Super_R" ); |
| 1399 | break; |
| 1400 | case WXK_WINDOWS_MENU: |
| 1401 | hotkey << wxT("Menu" ); |
| 1402 | break; |
| 1403 | case WXK_COMMAND: |
| 1404 | hotkey << wxT("Command" ); |
| 1405 | break; |
| 1406 | /* These probably wouldn't work as there is no SpecialX in gdk/keynames.txt |
| 1407 | case WXK_SPECIAL1: case WXK_SPECIAL2: case WXK_SPECIAL3: case WXK_SPECIAL4: |
| 1408 | case WXK_SPECIAL5: case WXK_SPECIAL6: case WXK_SPECIAL7: case WXK_SPECIAL8: |
| 1409 | case WXK_SPECIAL9: case WXK_SPECIAL10: case WXK_SPECIAL11: case WXK_SPECIAL12: |
| 1410 | case WXK_SPECIAL13: case WXK_SPECIAL14: case WXK_SPECIAL15: case WXK_SPECIAL16: |
| 1411 | case WXK_SPECIAL17: case WXK_SPECIAL18: case WXK_SPECIAL19: case WXK_SPECIAL20: |
| 1412 | hotkey += wxString::Format(wxT("Special%d"), code - WXK_SPECIAL1 + 1); |
| 1413 | break; |
| 1414 | */ |
| 1415 | // if there are any other keys wxGetAccelFromString() may |
| 1416 | // return, we should process them here |
| 1417 | |
| 1418 | default: |
| 1419 | if ( code < 127 ) |
| 1420 | { |
| 1421 | wxString name = wxGTK_CONV_BACK( gdk_keyval_name((guint)code) ); |
| 1422 | if ( name ) |
| 1423 | { |
| 1424 | hotkey << name; |
| 1425 | break; |
| 1426 | } |
| 1427 | } |
| 1428 | |
| 1429 | wxFAIL_MSG( wxT("unknown keyboard accel") ); |
| 1430 | } |
| 1431 | |
| 1432 | delete accel; |
| 1433 | } |
| 1434 | |
| 1435 | return hotkey; |
| 1436 | } |
| 1437 | |
| 1438 | #endif // wxUSE_ACCEL |
| 1439 | |
| 1440 | // ---------------------------------------------------------------------------- |
| 1441 | // Pop-up menu stuff |
| 1442 | // ---------------------------------------------------------------------------- |
| 1443 | |
| 1444 | #if wxUSE_MENUS_NATIVE |
| 1445 | |
| 1446 | extern "C" WXDLLIMPEXP_CORE |
| 1447 | void gtk_pop_hide_callback( GtkWidget *WXUNUSED(widget), bool* is_waiting ) |
| 1448 | { |
| 1449 | *is_waiting = FALSE; |
| 1450 | } |
| 1451 | |
| 1452 | WXDLLIMPEXP_CORE void SetInvokingWindow( wxMenu *menu, wxWindow* win ) |
| 1453 | { |
| 1454 | menu->SetInvokingWindow( win ); |
| 1455 | |
| 1456 | wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst(); |
| 1457 | while (node) |
| 1458 | { |
| 1459 | wxMenuItem *menuitem = node->GetData(); |
| 1460 | if (menuitem->IsSubMenu()) |
| 1461 | { |
| 1462 | SetInvokingWindow( menuitem->GetSubMenu(), win ); |
| 1463 | } |
| 1464 | |
| 1465 | node = node->GetNext(); |
| 1466 | } |
| 1467 | } |
| 1468 | |
| 1469 | extern "C" WXDLLIMPEXP_CORE |
| 1470 | void wxPopupMenuPositionCallback( GtkMenu *menu, |
| 1471 | gint *x, gint *y, |
| 1472 | gpointer user_data ) |
| 1473 | { |
| 1474 | // ensure that the menu appears entirely on screen |
| 1475 | GtkRequisition req; |
| 1476 | gtk_widget_get_child_requisition(GTK_WIDGET(menu), &req); |
| 1477 | |
| 1478 | wxSize sizeScreen = wxGetDisplaySize(); |
| 1479 | wxPoint *pos = (wxPoint*)user_data; |
| 1480 | |
| 1481 | gint xmax = sizeScreen.x - req.width, |
| 1482 | ymax = sizeScreen.y - req.height; |
| 1483 | |
| 1484 | *x = pos->x < xmax ? pos->x : xmax; |
| 1485 | *y = pos->y < ymax ? pos->y : ymax; |
| 1486 | } |
| 1487 | |
| 1488 | bool wxWindowGTK::DoPopupMenu( wxMenu *menu, int x, int y ) |
| 1489 | { |
| 1490 | wxCHECK_MSG( m_widget != NULL, false, wxT("invalid window") ); |
| 1491 | |
| 1492 | wxCHECK_MSG( menu != NULL, false, wxT("invalid popup-menu") ); |
| 1493 | |
| 1494 | // NOTE: if you change this code, you need to update |
| 1495 | // the same code in taskbar.cpp as well. This |
| 1496 | // is ugly code duplication, I know. |
| 1497 | |
| 1498 | SetInvokingWindow( menu, this ); |
| 1499 | |
| 1500 | menu->UpdateUI(); |
| 1501 | |
| 1502 | bool is_waiting = true; |
| 1503 | |
| 1504 | gulong handler = gtk_signal_connect( GTK_OBJECT(menu->m_menu), |
| 1505 | "hide", |
| 1506 | GTK_SIGNAL_FUNC(gtk_pop_hide_callback), |
| 1507 | (gpointer)&is_waiting ); |
| 1508 | |
| 1509 | wxPoint pos; |
| 1510 | gpointer userdata; |
| 1511 | GtkMenuPositionFunc posfunc; |
| 1512 | if ( x == -1 && y == -1 ) |
| 1513 | { |
| 1514 | // use GTK's default positioning algorithm |
| 1515 | userdata = NULL; |
| 1516 | posfunc = NULL; |
| 1517 | } |
| 1518 | else |
| 1519 | { |
| 1520 | pos = ClientToScreen(wxPoint(x, y)); |
| 1521 | userdata = &pos; |
| 1522 | posfunc = wxPopupMenuPositionCallback; |
| 1523 | } |
| 1524 | |
| 1525 | wxMenuEvent eventOpen(wxEVT_MENU_OPEN, -1, menu); |
| 1526 | DoCommonMenuCallbackCode(menu, eventOpen); |
| 1527 | |
| 1528 | gtk_menu_popup( |
| 1529 | GTK_MENU(menu->m_menu), |
| 1530 | (GtkWidget *) NULL, // parent menu shell |
| 1531 | (GtkWidget *) NULL, // parent menu item |
| 1532 | posfunc, // function to position it |
| 1533 | userdata, // client data |
| 1534 | 0, // button used to activate it |
| 1535 | wxGtkTimeLastClick // the time of activation |
| 1536 | ); |
| 1537 | |
| 1538 | while (is_waiting) |
| 1539 | { |
| 1540 | gtk_main_iteration(); |
| 1541 | } |
| 1542 | |
| 1543 | gtk_signal_disconnect(GTK_OBJECT(menu->m_menu), handler); |
| 1544 | |
| 1545 | wxMenuEvent eventClose(wxEVT_MENU_CLOSE, -1, menu); |
| 1546 | DoCommonMenuCallbackCode(menu, eventClose); |
| 1547 | |
| 1548 | return true; |
| 1549 | } |
| 1550 | |
| 1551 | #endif // wxUSE_MENUS_NATIVE |