| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: menu.cpp |
| 3 | // Purpose: |
| 4 | // Author: Robert Roebling |
| 5 | // Id: $Id$ |
| 6 | // Copyright: (c) 1998 Robert Roebling |
| 7 | // Licence: wxWindows licence |
| 8 | ///////////////////////////////////////////////////////////////////////////// |
| 9 | |
| 10 | #ifdef __GNUG__ |
| 11 | #pragma implementation "menu.h" |
| 12 | #pragma implementation "menuitem.h" |
| 13 | #endif |
| 14 | |
| 15 | #include "wx/log.h" |
| 16 | #include "wx/intl.h" |
| 17 | #include "wx/app.h" |
| 18 | #include "wx/menu.h" |
| 19 | |
| 20 | #if wxUSE_ACCEL |
| 21 | #include "wx/accel.h" |
| 22 | #endif // wxUSE_ACCEL |
| 23 | |
| 24 | #include <gdk/gdk.h> |
| 25 | #include <gtk/gtk.h> |
| 26 | |
| 27 | //----------------------------------------------------------------------------- |
| 28 | // idle system |
| 29 | //----------------------------------------------------------------------------- |
| 30 | |
| 31 | extern void wxapp_install_idle_handler(); |
| 32 | extern bool g_isIdle; |
| 33 | |
| 34 | #if (GTK_MINOR_VERSION > 0) && wxUSE_ACCEL |
| 35 | static wxString GetHotKey( const wxMenuItem& item ); |
| 36 | #endif |
| 37 | |
| 38 | //----------------------------------------------------------------------------- |
| 39 | // wxMenuBar |
| 40 | //----------------------------------------------------------------------------- |
| 41 | |
| 42 | IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow) |
| 43 | |
| 44 | wxMenuBar::wxMenuBar( long style ) |
| 45 | { |
| 46 | /* the parent window is known after wxFrame::SetMenu() */ |
| 47 | m_needParent = FALSE; |
| 48 | m_style = style; |
| 49 | m_invokingWindow = (wxWindow*) NULL; |
| 50 | |
| 51 | if (!PreCreation( (wxWindow*) NULL, wxDefaultPosition, wxDefaultSize ) || |
| 52 | !CreateBase( (wxWindow*) NULL, -1, wxDefaultPosition, wxDefaultSize, style, wxDefaultValidator, wxT("menubar") )) |
| 53 | { |
| 54 | wxFAIL_MSG( wxT("wxMenuBar creation failed") ); |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | m_menus.DeleteContents( TRUE ); |
| 59 | |
| 60 | /* GTK 1.2.0 doesn't have gtk_item_factory_get_item(), but GTK 1.2.1 has. */ |
| 61 | #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0) |
| 62 | m_accel = gtk_accel_group_new(); |
| 63 | m_factory = gtk_item_factory_new( GTK_TYPE_MENU_BAR, "<main>", m_accel ); |
| 64 | m_menubar = gtk_item_factory_get_widget( m_factory, "<main>" ); |
| 65 | #else |
| 66 | m_menubar = gtk_menu_bar_new(); |
| 67 | #endif |
| 68 | |
| 69 | if (style & wxMB_DOCKABLE) |
| 70 | { |
| 71 | m_widget = gtk_handle_box_new(); |
| 72 | gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_menubar) ); |
| 73 | gtk_widget_show( GTK_WIDGET(m_menubar) ); |
| 74 | } |
| 75 | else |
| 76 | { |
| 77 | m_widget = GTK_WIDGET(m_menubar); |
| 78 | } |
| 79 | |
| 80 | PostCreation(); |
| 81 | |
| 82 | ApplyWidgetStyle(); |
| 83 | } |
| 84 | |
| 85 | wxMenuBar::wxMenuBar() |
| 86 | { |
| 87 | /* the parent window is known after wxFrame::SetMenu() */ |
| 88 | m_needParent = FALSE; |
| 89 | m_style = 0; |
| 90 | m_invokingWindow = (wxWindow*) NULL; |
| 91 | |
| 92 | if (!PreCreation( (wxWindow*) NULL, wxDefaultPosition, wxDefaultSize ) || |
| 93 | !CreateBase( (wxWindow*) NULL, -1, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, wxT("menubar") )) |
| 94 | { |
| 95 | wxFAIL_MSG( wxT("wxMenuBar creation failed") ); |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | m_menus.DeleteContents( TRUE ); |
| 100 | |
| 101 | /* GTK 1.2.0 doesn't have gtk_item_factory_get_item(), but GTK 1.2.1 has. */ |
| 102 | #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0) |
| 103 | m_accel = gtk_accel_group_new(); |
| 104 | m_factory = gtk_item_factory_new( GTK_TYPE_MENU_BAR, "<main>", m_accel ); |
| 105 | m_menubar = gtk_item_factory_get_widget( m_factory, "<main>" ); |
| 106 | #else |
| 107 | m_menubar = gtk_menu_bar_new(); |
| 108 | #endif |
| 109 | |
| 110 | m_widget = GTK_WIDGET(m_menubar); |
| 111 | |
| 112 | PostCreation(); |
| 113 | |
| 114 | ApplyWidgetStyle(); |
| 115 | } |
| 116 | |
| 117 | wxMenuBar::~wxMenuBar() |
| 118 | { |
| 119 | // gtk_object_unref( GTK_OBJECT(m_factory) ); why not ? |
| 120 | } |
| 121 | |
| 122 | static void wxMenubarUnsetInvokingWindow( wxMenu *menu, wxWindow *win ) |
| 123 | { |
| 124 | menu->SetInvokingWindow( (wxWindow*) NULL ); |
| 125 | |
| 126 | #if (GTK_MINOR_VERSION > 0) |
| 127 | wxWindow *top_frame = win; |
| 128 | while (top_frame->GetParent() && !(top_frame->IsTopLevel())) |
| 129 | top_frame = top_frame->GetParent(); |
| 130 | |
| 131 | /* support for native hot keys */ |
| 132 | gtk_accel_group_detach( menu->m_accel, GTK_OBJECT(top_frame->m_widget) ); |
| 133 | #endif |
| 134 | |
| 135 | wxMenuItemList::Node *node = menu->GetMenuItems().GetFirst(); |
| 136 | while (node) |
| 137 | { |
| 138 | wxMenuItem *menuitem = node->GetData(); |
| 139 | if (menuitem->IsSubMenu()) |
| 140 | wxMenubarUnsetInvokingWindow( menuitem->GetSubMenu(), win ); |
| 141 | node = node->GetNext(); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | static void wxMenubarSetInvokingWindow( wxMenu *menu, wxWindow *win ) |
| 146 | { |
| 147 | menu->SetInvokingWindow( win ); |
| 148 | |
| 149 | #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0) |
| 150 | wxWindow *top_frame = win; |
| 151 | while (top_frame->GetParent() && !(top_frame->IsTopLevel())) |
| 152 | top_frame = top_frame->GetParent(); |
| 153 | |
| 154 | /* support for native hot keys */ |
| 155 | GtkObject *obj = GTK_OBJECT(top_frame->m_widget); |
| 156 | if ( !g_slist_find( menu->m_accel->attach_objects, obj ) ) |
| 157 | gtk_accel_group_attach( menu->m_accel, obj ); |
| 158 | #endif |
| 159 | |
| 160 | wxMenuItemList::Node *node = menu->GetMenuItems().GetFirst(); |
| 161 | while (node) |
| 162 | { |
| 163 | wxMenuItem *menuitem = node->GetData(); |
| 164 | if (menuitem->IsSubMenu()) |
| 165 | wxMenubarSetInvokingWindow( menuitem->GetSubMenu(), win ); |
| 166 | node = node->GetNext(); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | void wxMenuBar::SetInvokingWindow( wxWindow *win ) |
| 171 | { |
| 172 | m_invokingWindow = win; |
| 173 | #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0) |
| 174 | wxWindow *top_frame = win; |
| 175 | while (top_frame->GetParent() && !(top_frame->IsTopLevel())) |
| 176 | top_frame = top_frame->GetParent(); |
| 177 | |
| 178 | /* support for native key accelerators indicated by underscroes */ |
| 179 | GtkObject *obj = GTK_OBJECT(top_frame->m_widget); |
| 180 | if ( !g_slist_find( m_accel->attach_objects, obj ) ) |
| 181 | gtk_accel_group_attach( m_accel, obj ); |
| 182 | #endif |
| 183 | |
| 184 | wxMenuList::Node *node = m_menus.GetFirst(); |
| 185 | while (node) |
| 186 | { |
| 187 | wxMenu *menu = node->GetData(); |
| 188 | wxMenubarSetInvokingWindow( menu, win ); |
| 189 | node = node->GetNext(); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | void wxMenuBar::UnsetInvokingWindow( wxWindow *win ) |
| 194 | { |
| 195 | m_invokingWindow = (wxWindow*) NULL; |
| 196 | #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0) |
| 197 | wxWindow *top_frame = win; |
| 198 | while (top_frame->GetParent() && !(top_frame->IsTopLevel())) |
| 199 | top_frame = top_frame->GetParent(); |
| 200 | |
| 201 | /* support for native key accelerators indicated by underscroes */ |
| 202 | gtk_accel_group_detach( m_accel, GTK_OBJECT(top_frame->m_widget) ); |
| 203 | #endif |
| 204 | |
| 205 | wxMenuList::Node *node = m_menus.GetFirst(); |
| 206 | while (node) |
| 207 | { |
| 208 | wxMenu *menu = node->GetData(); |
| 209 | wxMenubarUnsetInvokingWindow( menu, win ); |
| 210 | node = node->GetNext(); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | bool wxMenuBar::Append( wxMenu *menu, const wxString &title ) |
| 215 | { |
| 216 | if ( !wxMenuBarBase::Append( menu, title ) ) |
| 217 | return FALSE; |
| 218 | |
| 219 | return GtkAppend(menu, title); |
| 220 | } |
| 221 | |
| 222 | bool wxMenuBar::GtkAppend(wxMenu *menu, const wxString& title) |
| 223 | { |
| 224 | const wxChar *pc; |
| 225 | |
| 226 | /* GTK 1.2 wants to have "_" instead of "&" for accelerators */ |
| 227 | wxString str; |
| 228 | for ( pc = title; *pc != wxT('\0'); pc++ ) |
| 229 | { |
| 230 | if (*pc == wxT('&')) |
| 231 | { |
| 232 | #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0) |
| 233 | str << wxT('_'); |
| 234 | } |
| 235 | else if (*pc == wxT('/')) |
| 236 | { |
| 237 | str << wxT('\\'); |
| 238 | #endif |
| 239 | } |
| 240 | else |
| 241 | { |
| 242 | #if __WXGTK12__ |
| 243 | if ( *pc == wxT('_') ) |
| 244 | { |
| 245 | // underscores must be doubled to prevent them from being |
| 246 | // interpreted as accelerator character prefix by GTK |
| 247 | str << *pc; |
| 248 | } |
| 249 | #endif // GTK+ 1.2 |
| 250 | |
| 251 | str << *pc; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | /* this doesn't have much effect right now */ |
| 256 | menu->SetTitle( str ); |
| 257 | |
| 258 | /* GTK 1.2.0 doesn't have gtk_item_factory_get_item(), but GTK 1.2.1 has. */ |
| 259 | #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0) |
| 260 | |
| 261 | /* local buffer in multibyte form */ |
| 262 | wxString buf; |
| 263 | buf << wxT('/') << str.c_str(); |
| 264 | |
| 265 | char *cbuf = new char[buf.Length()+1]; |
| 266 | strcpy(cbuf, buf.mbc_str()); |
| 267 | |
| 268 | GtkItemFactoryEntry entry; |
| 269 | entry.path = (gchar *)cbuf; // const_cast |
| 270 | entry.accelerator = (gchar*) NULL; |
| 271 | entry.callback = (GtkItemFactoryCallback) NULL; |
| 272 | entry.callback_action = 0; |
| 273 | entry.item_type = "<Branch>"; |
| 274 | |
| 275 | gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */ |
| 276 | /* in order to get the pointer to the item we need the item text _without_ underscores */ |
| 277 | wxString tmp = wxT("<main>/"); |
| 278 | for ( pc = str; *pc != wxT('\0'); pc++ ) |
| 279 | { |
| 280 | // contrary to the common sense, we must throw out _all_ underscores, |
| 281 | // (i.e. "Hello__World" => "HelloWorld" and not "Hello_World" as we |
| 282 | // might naively think). IMHO it's a bug in GTK+ (VZ) |
| 283 | while (*pc == wxT('_')) |
| 284 | pc++; |
| 285 | tmp << *pc; |
| 286 | } |
| 287 | menu->m_owner = gtk_item_factory_get_item( m_factory, tmp.mb_str() ); |
| 288 | gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu->m_owner), menu->m_menu ); |
| 289 | delete [] cbuf; |
| 290 | #else |
| 291 | |
| 292 | menu->m_owner = gtk_menu_item_new_with_label( str.mb_str() ); |
| 293 | gtk_widget_show( menu->m_owner ); |
| 294 | gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu->m_owner), menu->m_menu ); |
| 295 | |
| 296 | gtk_menu_bar_append( GTK_MENU_BAR(m_menubar), menu->m_owner ); |
| 297 | |
| 298 | #endif |
| 299 | |
| 300 | // m_invokingWindow is set after wxFrame::SetMenuBar(). This call enables |
| 301 | // adding menu later on. |
| 302 | if (m_invokingWindow) |
| 303 | wxMenubarSetInvokingWindow( menu, m_invokingWindow ); |
| 304 | |
| 305 | return TRUE; |
| 306 | } |
| 307 | |
| 308 | bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title) |
| 309 | { |
| 310 | if ( !wxMenuBarBase::Insert(pos, menu, title) ) |
| 311 | return FALSE; |
| 312 | |
| 313 | #if __WXGTK12__ |
| 314 | // GTK+ doesn't have a function to insert a menu using GtkItemFactory (as |
| 315 | // of version 1.2.6), so we first append the item and then change its |
| 316 | // index |
| 317 | if ( !GtkAppend(menu, title) ) |
| 318 | return FALSE; |
| 319 | |
| 320 | if (pos+1 >= m_menus.GetCount()) |
| 321 | return TRUE; |
| 322 | |
| 323 | GtkMenuShell *menu_shell = GTK_MENU_SHELL(m_factory->widget); |
| 324 | gpointer data = g_list_last(menu_shell->children)->data; |
| 325 | menu_shell->children = g_list_remove(menu_shell->children, data); |
| 326 | menu_shell->children = g_list_insert(menu_shell->children, data, pos); |
| 327 | |
| 328 | return TRUE; |
| 329 | #else // GTK < 1.2 |
| 330 | // this should be easy to do with GTK 1.0 - can use standard functions for |
| 331 | // this and don't need any hacks like above, but as I don't have GTK 1.0 |
| 332 | // any more I can't do it |
| 333 | wxFAIL_MSG( wxT("TODO") ); |
| 334 | |
| 335 | return FALSE; |
| 336 | #endif // GTK 1.2/1.0 |
| 337 | } |
| 338 | |
| 339 | wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title) |
| 340 | { |
| 341 | // remove the old item and insert a new one |
| 342 | wxMenu *menuOld = Remove(pos); |
| 343 | if ( menuOld && !Insert(pos, menu, title) ) |
| 344 | { |
| 345 | return (wxMenu*) NULL; |
| 346 | } |
| 347 | |
| 348 | // either Insert() succeeded or Remove() failed and menuOld is NULL |
| 349 | return menuOld; |
| 350 | } |
| 351 | |
| 352 | wxMenu *wxMenuBar::Remove(size_t pos) |
| 353 | { |
| 354 | wxMenu *menu = wxMenuBarBase::Remove(pos); |
| 355 | if ( !menu ) |
| 356 | return (wxMenu*) NULL; |
| 357 | |
| 358 | /* |
| 359 | GtkMenuShell *menu_shell = GTK_MENU_SHELL(m_factory->widget); |
| 360 | |
| 361 | printf( "factory entries before %d\n", (int)g_slist_length(m_factory->items) ); |
| 362 | printf( "menu shell entries before %d\n", (int)g_list_length( menu_shell->children ) ); |
| 363 | */ |
| 364 | |
| 365 | // unparent calls unref() and that would delete the widget so we raise |
| 366 | // the ref count to 2 artificially before invoking unparent. |
| 367 | gtk_widget_ref( menu->m_menu ); |
| 368 | gtk_widget_unparent( menu->m_menu ); |
| 369 | |
| 370 | gtk_widget_destroy( menu->m_owner ); |
| 371 | |
| 372 | /* |
| 373 | printf( "factory entries after %d\n", (int)g_slist_length(m_factory->items) ); |
| 374 | printf( "menu shell entries after %d\n", (int)g_list_length( menu_shell->children ) ); |
| 375 | */ |
| 376 | |
| 377 | return menu; |
| 378 | } |
| 379 | |
| 380 | static int FindMenuItemRecursive( const wxMenu *menu, const wxString &menuString, const wxString &itemString ) |
| 381 | { |
| 382 | if (menu->GetTitle() == menuString) |
| 383 | { |
| 384 | int res = menu->FindItem( itemString ); |
| 385 | if (res != wxNOT_FOUND) |
| 386 | return res; |
| 387 | } |
| 388 | |
| 389 | wxMenuItemList::Node *node = menu->GetMenuItems().GetFirst(); |
| 390 | while (node) |
| 391 | { |
| 392 | wxMenuItem *item = node->GetData(); |
| 393 | if (item->IsSubMenu()) |
| 394 | return FindMenuItemRecursive(item->GetSubMenu(), menuString, itemString); |
| 395 | |
| 396 | node = node->GetNext(); |
| 397 | } |
| 398 | |
| 399 | return wxNOT_FOUND; |
| 400 | } |
| 401 | |
| 402 | int wxMenuBar::FindMenuItem( const wxString &menuString, const wxString &itemString ) const |
| 403 | { |
| 404 | wxMenuList::Node *node = m_menus.GetFirst(); |
| 405 | while (node) |
| 406 | { |
| 407 | wxMenu *menu = node->GetData(); |
| 408 | int res = FindMenuItemRecursive( menu, menuString, itemString); |
| 409 | if (res != -1) |
| 410 | return res; |
| 411 | node = node->GetNext(); |
| 412 | } |
| 413 | |
| 414 | return wxNOT_FOUND; |
| 415 | } |
| 416 | |
| 417 | // Find a wxMenuItem using its id. Recurses down into sub-menus |
| 418 | static wxMenuItem* FindMenuItemByIdRecursive(const wxMenu* menu, int id) |
| 419 | { |
| 420 | wxMenuItem* result = menu->FindChildItem(id); |
| 421 | |
| 422 | wxMenuItemList::Node *node = menu->GetMenuItems().GetFirst(); |
| 423 | while ( node && result == NULL ) |
| 424 | { |
| 425 | wxMenuItem *item = node->GetData(); |
| 426 | if (item->IsSubMenu()) |
| 427 | { |
| 428 | result = FindMenuItemByIdRecursive( item->GetSubMenu(), id ); |
| 429 | } |
| 430 | node = node->GetNext(); |
| 431 | } |
| 432 | |
| 433 | return result; |
| 434 | } |
| 435 | |
| 436 | wxMenuItem* wxMenuBar::FindItem( int id, wxMenu **menuForItem ) const |
| 437 | { |
| 438 | wxMenuItem* result = 0; |
| 439 | wxMenuList::Node *node = m_menus.GetFirst(); |
| 440 | while (node && result == 0) |
| 441 | { |
| 442 | wxMenu *menu = node->GetData(); |
| 443 | result = FindMenuItemByIdRecursive( menu, id ); |
| 444 | node = node->GetNext(); |
| 445 | } |
| 446 | |
| 447 | if ( menuForItem ) |
| 448 | { |
| 449 | *menuForItem = result ? result->GetMenu() : (wxMenu *)NULL; |
| 450 | } |
| 451 | |
| 452 | return result; |
| 453 | } |
| 454 | |
| 455 | void wxMenuBar::EnableTop( size_t pos, bool flag ) |
| 456 | { |
| 457 | wxMenuList::Node *node = m_menus.Item( pos ); |
| 458 | |
| 459 | wxCHECK_RET( node, wxT("menu not found") ); |
| 460 | |
| 461 | wxMenu* menu = node->GetData(); |
| 462 | |
| 463 | if (menu->m_owner) |
| 464 | gtk_widget_set_sensitive( menu->m_owner, flag ); |
| 465 | } |
| 466 | |
| 467 | wxString wxMenuBar::GetLabelTop( size_t pos ) const |
| 468 | { |
| 469 | wxMenuList::Node *node = m_menus.Item( pos ); |
| 470 | |
| 471 | wxCHECK_MSG( node, wxT("invalid"), wxT("menu not found") ); |
| 472 | |
| 473 | wxMenu* menu = node->GetData(); |
| 474 | |
| 475 | return menu->GetTitle(); |
| 476 | } |
| 477 | |
| 478 | void wxMenuBar::SetLabelTop( size_t pos, const wxString& label ) |
| 479 | { |
| 480 | wxMenuList::Node *node = m_menus.Item( pos ); |
| 481 | |
| 482 | wxCHECK_RET( node, wxT("menu not found") ); |
| 483 | |
| 484 | wxMenu* menu = node->GetData(); |
| 485 | |
| 486 | menu->SetTitle( label ); |
| 487 | } |
| 488 | |
| 489 | //----------------------------------------------------------------------------- |
| 490 | // "activate" |
| 491 | //----------------------------------------------------------------------------- |
| 492 | |
| 493 | static void gtk_menu_clicked_callback( GtkWidget *widget, wxMenu *menu ) |
| 494 | { |
| 495 | if (g_isIdle) wxapp_install_idle_handler(); |
| 496 | |
| 497 | int id = menu->FindMenuIdByMenuItem(widget); |
| 498 | |
| 499 | /* should find it for normal (not popup) menu */ |
| 500 | wxASSERT( (id != -1) || (menu->GetInvokingWindow() != NULL) ); |
| 501 | |
| 502 | if (!menu->IsEnabled(id)) |
| 503 | return; |
| 504 | |
| 505 | wxMenuItem* item = menu->FindChildItem( id ); |
| 506 | wxCHECK_RET( item, wxT("error in menu item callback") ); |
| 507 | |
| 508 | if (item->IsCheckable()) |
| 509 | { |
| 510 | bool isReallyChecked = item->IsChecked(); |
| 511 | if ( item->wxMenuItemBase::IsChecked() == isReallyChecked ) |
| 512 | { |
| 513 | /* the menu item has been checked by calling wxMenuItem->Check() */ |
| 514 | return; |
| 515 | } |
| 516 | else |
| 517 | { |
| 518 | /* the user pressed on the menu item -> report and make consistent |
| 519 | * again */ |
| 520 | item->wxMenuItemBase::Check(isReallyChecked); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | wxCommandEvent event( wxEVT_COMMAND_MENU_SELECTED, id ); |
| 525 | event.SetEventObject( menu ); |
| 526 | event.SetInt(id ); |
| 527 | |
| 528 | #if wxUSE_MENU_CALLBACK |
| 529 | if (menu->GetCallback()) |
| 530 | { |
| 531 | (void) (*(menu->GetCallback())) (*menu, event); |
| 532 | return; |
| 533 | } |
| 534 | #endif // wxUSE_MENU_CALLBACK |
| 535 | |
| 536 | if (menu->GetEventHandler()->ProcessEvent(event)) |
| 537 | return; |
| 538 | |
| 539 | wxWindow *win = menu->GetInvokingWindow(); |
| 540 | if (win) |
| 541 | win->GetEventHandler()->ProcessEvent( event ); |
| 542 | } |
| 543 | |
| 544 | //----------------------------------------------------------------------------- |
| 545 | // "select" |
| 546 | //----------------------------------------------------------------------------- |
| 547 | |
| 548 | static void gtk_menu_hilight_callback( GtkWidget *widget, wxMenu *menu ) |
| 549 | { |
| 550 | if (g_isIdle) wxapp_install_idle_handler(); |
| 551 | |
| 552 | int id = menu->FindMenuIdByMenuItem(widget); |
| 553 | |
| 554 | wxASSERT( id != -1 ); // should find it! |
| 555 | |
| 556 | if (!menu->IsEnabled(id)) |
| 557 | return; |
| 558 | |
| 559 | wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, id ); |
| 560 | event.SetEventObject( menu ); |
| 561 | |
| 562 | if (menu->GetEventHandler()->ProcessEvent(event)) |
| 563 | return; |
| 564 | |
| 565 | wxWindow *win = menu->GetInvokingWindow(); |
| 566 | if (win) win->GetEventHandler()->ProcessEvent( event ); |
| 567 | } |
| 568 | |
| 569 | //----------------------------------------------------------------------------- |
| 570 | // "deselect" |
| 571 | //----------------------------------------------------------------------------- |
| 572 | |
| 573 | static void gtk_menu_nolight_callback( GtkWidget *widget, wxMenu *menu ) |
| 574 | { |
| 575 | if (g_isIdle) wxapp_install_idle_handler(); |
| 576 | |
| 577 | int id = menu->FindMenuIdByMenuItem(widget); |
| 578 | |
| 579 | wxASSERT( id != -1 ); // should find it! |
| 580 | |
| 581 | if (!menu->IsEnabled(id)) |
| 582 | return; |
| 583 | |
| 584 | wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, -1 ); |
| 585 | event.SetEventObject( menu ); |
| 586 | |
| 587 | if (menu->GetEventHandler()->ProcessEvent(event)) |
| 588 | return; |
| 589 | |
| 590 | wxWindow *win = menu->GetInvokingWindow(); |
| 591 | if (win) |
| 592 | win->GetEventHandler()->ProcessEvent( event ); |
| 593 | } |
| 594 | |
| 595 | //----------------------------------------------------------------------------- |
| 596 | // wxMenuItem |
| 597 | //----------------------------------------------------------------------------- |
| 598 | |
| 599 | IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxMenuItemBase) |
| 600 | |
| 601 | wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu, |
| 602 | int id, |
| 603 | const wxString& name, |
| 604 | const wxString& help, |
| 605 | bool isCheckable, |
| 606 | wxMenu *subMenu) |
| 607 | { |
| 608 | return new wxMenuItem(parentMenu, id, name, help, isCheckable, subMenu); |
| 609 | } |
| 610 | |
| 611 | wxMenuItem::wxMenuItem(wxMenu *parentMenu, |
| 612 | int id, |
| 613 | const wxString& text, |
| 614 | const wxString& help, |
| 615 | bool isCheckable, |
| 616 | wxMenu *subMenu) |
| 617 | { |
| 618 | m_id = id; |
| 619 | m_isCheckable = isCheckable; |
| 620 | m_isChecked = FALSE; |
| 621 | m_isEnabled = TRUE; |
| 622 | m_subMenu = subMenu; |
| 623 | m_parentMenu = parentMenu; |
| 624 | m_help = help; |
| 625 | |
| 626 | m_menuItem = (GtkWidget *) NULL; |
| 627 | |
| 628 | DoSetText(text); |
| 629 | } |
| 630 | |
| 631 | wxMenuItem::~wxMenuItem() |
| 632 | { |
| 633 | // don't delete menu items, the menus take care of that |
| 634 | } |
| 635 | |
| 636 | // return the menu item text without any menu accels |
| 637 | /* static */ |
| 638 | wxString wxMenuItemBase::GetLabelFromText(const wxString& text) |
| 639 | { |
| 640 | wxString label; |
| 641 | #if (GTK_MINOR_VERSION > 0) |
| 642 | for ( const wxChar *pc = text.c_str(); *pc; pc++ ) |
| 643 | { |
| 644 | if ( *pc == wxT('_') || *pc == wxT('&') ) |
| 645 | { |
| 646 | // '_' is the escape character for GTK+ and '&' is the one for |
| 647 | // wxWindows - skip both of them |
| 648 | continue; |
| 649 | } |
| 650 | |
| 651 | label += *pc; |
| 652 | } |
| 653 | #else // GTK+ 1.0 |
| 654 | label = text; |
| 655 | #endif // GTK+ 1.2/1.0 |
| 656 | |
| 657 | return label; |
| 658 | } |
| 659 | |
| 660 | void wxMenuItem::SetText( const wxString& str ) |
| 661 | { |
| 662 | DoSetText(str); |
| 663 | |
| 664 | if (m_menuItem) |
| 665 | { |
| 666 | GtkLabel *label = GTK_LABEL( GTK_BIN(m_menuItem)->child ); |
| 667 | |
| 668 | /* set new text */ |
| 669 | gtk_label_set( label, m_text.mb_str()); |
| 670 | |
| 671 | /* reparse key accel */ |
| 672 | (void)gtk_label_parse_uline (GTK_LABEL(label), m_text.mb_str() ); |
| 673 | gtk_accel_label_refetch( GTK_ACCEL_LABEL(label) ); |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | // it's valid for this function to be called even if m_menuItem == NULL |
| 678 | void wxMenuItem::DoSetText( const wxString& str ) |
| 679 | { |
| 680 | /* '\t' is the deliminator indicating a hot key */ |
| 681 | m_text.Empty(); |
| 682 | const wxChar *pc = str; |
| 683 | for (; (*pc != wxT('\0')) && (*pc != wxT('\t')); pc++ ) |
| 684 | { |
| 685 | if (*pc == wxT('&')) |
| 686 | { |
| 687 | #if (GTK_MINOR_VERSION > 0) |
| 688 | m_text << wxT('_'); |
| 689 | } |
| 690 | else if ( *pc == wxT('_') ) // escape underscores |
| 691 | { |
| 692 | m_text << wxT("__"); |
| 693 | } |
| 694 | else if (*pc == wxT('/')) /* we have to filter out slashes ... */ |
| 695 | { |
| 696 | m_text << wxT('\\'); /* ... and replace them with back slashes */ |
| 697 | #endif |
| 698 | } |
| 699 | else |
| 700 | m_text << *pc; |
| 701 | } |
| 702 | |
| 703 | /* only GTK 1.2 knows about hot keys */ |
| 704 | m_hotKey = wxT(""); |
| 705 | #if (GTK_MINOR_VERSION > 0) |
| 706 | if(*pc == wxT('\t')) |
| 707 | { |
| 708 | pc++; |
| 709 | m_hotKey = pc; |
| 710 | } |
| 711 | #endif |
| 712 | } |
| 713 | |
| 714 | #if wxUSE_ACCEL |
| 715 | |
| 716 | wxAcceleratorEntry *wxMenuItem::GetAccel() const |
| 717 | { |
| 718 | if ( !GetHotKey() ) |
| 719 | { |
| 720 | // nothing |
| 721 | return (wxAcceleratorEntry *)NULL; |
| 722 | } |
| 723 | |
| 724 | // as wxGetAccelFromString() looks for TAB, insert a dummy one here |
| 725 | wxString label; |
| 726 | label << wxT('\t') << GetHotKey(); |
| 727 | |
| 728 | return wxGetAccelFromString(label); |
| 729 | } |
| 730 | |
| 731 | #endif // wxUSE_ACCEL |
| 732 | |
| 733 | void wxMenuItem::Check( bool check ) |
| 734 | { |
| 735 | wxCHECK_RET( m_menuItem, wxT("invalid menu item") ); |
| 736 | |
| 737 | wxCHECK_RET( IsCheckable(), wxT("Can't check uncheckable item!") ) |
| 738 | |
| 739 | if (check == m_isChecked) |
| 740 | return; |
| 741 | |
| 742 | wxMenuItemBase::Check( check ); |
| 743 | gtk_check_menu_item_set_state( (GtkCheckMenuItem*)m_menuItem, (gint)check ); |
| 744 | } |
| 745 | |
| 746 | void wxMenuItem::Enable( bool enable ) |
| 747 | { |
| 748 | wxCHECK_RET( m_menuItem, wxT("invalid menu item") ); |
| 749 | |
| 750 | gtk_widget_set_sensitive( m_menuItem, enable ); |
| 751 | wxMenuItemBase::Enable( enable ); |
| 752 | } |
| 753 | |
| 754 | bool wxMenuItem::IsChecked() const |
| 755 | { |
| 756 | wxCHECK_MSG( m_menuItem, FALSE, wxT("invalid menu item") ); |
| 757 | |
| 758 | wxCHECK_MSG( IsCheckable(), FALSE, |
| 759 | wxT("can't get state of uncheckable item!") ); |
| 760 | |
| 761 | return ((GtkCheckMenuItem*)m_menuItem)->active != 0; |
| 762 | } |
| 763 | |
| 764 | wxString wxMenuItem::GetFactoryPath() const |
| 765 | { |
| 766 | /* in order to get the pointer to the item we need the item text _without_ |
| 767 | underscores */ |
| 768 | wxString path( wxT("<main>/") ); |
| 769 | path += GetLabel(); |
| 770 | |
| 771 | return path; |
| 772 | } |
| 773 | |
| 774 | //----------------------------------------------------------------------------- |
| 775 | // wxMenu |
| 776 | //----------------------------------------------------------------------------- |
| 777 | |
| 778 | IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler) |
| 779 | |
| 780 | void wxMenu::Init() |
| 781 | { |
| 782 | #if (GTK_MINOR_VERSION > 0) |
| 783 | m_accel = gtk_accel_group_new(); |
| 784 | m_factory = gtk_item_factory_new( GTK_TYPE_MENU, "<main>", m_accel ); |
| 785 | m_menu = gtk_item_factory_get_widget( m_factory, "<main>" ); |
| 786 | #else |
| 787 | m_menu = gtk_menu_new(); // Do not show! |
| 788 | #endif |
| 789 | |
| 790 | m_owner = (GtkWidget*) NULL; |
| 791 | |
| 792 | #if (GTK_MINOR_VERSION > 0) |
| 793 | /* Tearoffs are entries, just like separators. So if we want this |
| 794 | menu to be a tear-off one, we just append a tearoff entry |
| 795 | immediately. */ |
| 796 | if(m_style & wxMENU_TEAROFF) |
| 797 | { |
| 798 | GtkItemFactoryEntry entry; |
| 799 | entry.path = "/tearoff"; |
| 800 | entry.callback = (GtkItemFactoryCallback) NULL; |
| 801 | entry.callback_action = 0; |
| 802 | entry.item_type = "<Tearoff>"; |
| 803 | entry.accelerator = (gchar*) NULL; |
| 804 | gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */ |
| 805 | //GtkWidget *menuItem = gtk_item_factory_get_widget( m_factory, "<main>/tearoff" ); |
| 806 | } |
| 807 | #endif |
| 808 | |
| 809 | // append the title as the very first entry if we have it |
| 810 | if ( !!m_title ) |
| 811 | { |
| 812 | Append(-2, m_title); |
| 813 | AppendSeparator(); |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | wxMenu::~wxMenu() |
| 818 | { |
| 819 | m_items.Clear(); |
| 820 | |
| 821 | gtk_widget_destroy( m_menu ); |
| 822 | |
| 823 | gtk_object_unref( GTK_OBJECT(m_factory) ); |
| 824 | } |
| 825 | |
| 826 | bool wxMenu::GtkAppend(wxMenuItem *mitem) |
| 827 | { |
| 828 | GtkWidget *menuItem; |
| 829 | |
| 830 | if ( mitem->IsSeparator() ) |
| 831 | { |
| 832 | #if (GTK_MINOR_VERSION > 0) |
| 833 | GtkItemFactoryEntry entry; |
| 834 | entry.path = "/sep"; |
| 835 | entry.callback = (GtkItemFactoryCallback) NULL; |
| 836 | entry.callback_action = 0; |
| 837 | entry.item_type = "<Separator>"; |
| 838 | entry.accelerator = (gchar*) NULL; |
| 839 | |
| 840 | gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */ |
| 841 | |
| 842 | /* this will be wrong for more than one separator. do we care? */ |
| 843 | menuItem = gtk_item_factory_get_widget( m_factory, "<main>/sep" ); |
| 844 | #else // GTK+ 1.0 |
| 845 | menuItem = gtk_menu_item_new(); |
| 846 | #endif // GTK 1.2/1.0 |
| 847 | } |
| 848 | else if ( mitem->IsSubMenu() ) |
| 849 | { |
| 850 | #if (GTK_MINOR_VERSION > 0) |
| 851 | /* text has "_" instead of "&" after mitem->SetText() */ |
| 852 | wxString text( mitem->GetText() ); |
| 853 | |
| 854 | /* local buffer in multibyte form */ |
| 855 | char buf[200]; |
| 856 | strcpy( buf, "/" ); |
| 857 | strcat( buf, text.mb_str() ); |
| 858 | |
| 859 | GtkItemFactoryEntry entry; |
| 860 | entry.path = buf; |
| 861 | entry.callback = (GtkItemFactoryCallback) 0; |
| 862 | entry.callback_action = 0; |
| 863 | entry.item_type = "<Branch>"; |
| 864 | entry.accelerator = (gchar*) NULL; |
| 865 | |
| 866 | gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */ |
| 867 | |
| 868 | wxString path( mitem->GetFactoryPath() ); |
| 869 | menuItem = gtk_item_factory_get_item( m_factory, path.mb_str() ); |
| 870 | #else // GTK+ 1.0 |
| 871 | menuItem = gtk_menu_item_new_with_label(mitem->GetText().mbc_str()); |
| 872 | #endif // GTK 1.2/1.0 |
| 873 | |
| 874 | gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), mitem->GetSubMenu()->m_menu ); |
| 875 | } |
| 876 | else // a normal item |
| 877 | { |
| 878 | #if (GTK_MINOR_VERSION > 0) |
| 879 | /* text has "_" instead of "&" after mitem->SetText() */ |
| 880 | wxString text( mitem->GetText() ); |
| 881 | |
| 882 | /* local buffer in multibyte form */ |
| 883 | char buf[200]; |
| 884 | strcpy( buf, "/" ); |
| 885 | strcat( buf, text.mb_str() ); |
| 886 | |
| 887 | GtkItemFactoryEntry entry; |
| 888 | entry.path = buf; |
| 889 | entry.callback = (GtkItemFactoryCallback) gtk_menu_clicked_callback; |
| 890 | entry.callback_action = 0; |
| 891 | if ( mitem->IsCheckable() ) |
| 892 | entry.item_type = "<CheckItem>"; |
| 893 | else |
| 894 | entry.item_type = "<Item>"; |
| 895 | entry.accelerator = (gchar*) NULL; |
| 896 | |
| 897 | #if wxUSE_ACCEL |
| 898 | // due to an apparent bug in GTK+, we have to use a static buffer here - |
| 899 | // otherwise GTK+ 1.2.2 manages to override the memory we pass to it |
| 900 | // somehow! (VZ) |
| 901 | static char s_accel[32]; // must be big enough for <control><alt><shift>F12 |
| 902 | strncpy(s_accel, GetHotKey(*mitem).mb_str(), WXSIZEOF(s_accel)); |
| 903 | entry.accelerator = s_accel; |
| 904 | #else // !wxUSE_ACCEL |
| 905 | entry.accelerator = (char*) NULL; |
| 906 | #endif // wxUSE_ACCEL/!wxUSE_ACCEL |
| 907 | |
| 908 | gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */ |
| 909 | |
| 910 | wxString path( mitem->GetFactoryPath() ); |
| 911 | menuItem = gtk_item_factory_get_widget( m_factory, path.mb_str() ); |
| 912 | #else // GTK+ 1.0 |
| 913 | menuItem = checkable ? gtk_check_menu_item_new_with_label( mitem->GetText().mb_str() ) |
| 914 | : gtk_menu_item_new_with_label( mitem->GetText().mb_str() ); |
| 915 | |
| 916 | gtk_signal_connect( GTK_OBJECT(menuItem), "activate", |
| 917 | GTK_SIGNAL_FUNC(gtk_menu_clicked_callback), |
| 918 | (gpointer)this ); |
| 919 | #endif // GTK+ 1.2/1.0 |
| 920 | } |
| 921 | |
| 922 | if ( !mitem->IsSeparator() ) |
| 923 | { |
| 924 | gtk_signal_connect( GTK_OBJECT(menuItem), "select", |
| 925 | GTK_SIGNAL_FUNC(gtk_menu_hilight_callback), |
| 926 | (gpointer)this ); |
| 927 | |
| 928 | gtk_signal_connect( GTK_OBJECT(menuItem), "deselect", |
| 929 | GTK_SIGNAL_FUNC(gtk_menu_nolight_callback), |
| 930 | (gpointer)this ); |
| 931 | } |
| 932 | |
| 933 | #if GTK_MINOR_VERSION == 0 |
| 934 | gtk_menu_append( GTK_MENU(m_menu), menuItem ); |
| 935 | gtk_widget_show( menuItem ); |
| 936 | #endif // GTK+ 1.0 |
| 937 | |
| 938 | mitem->SetMenuItem(menuItem); |
| 939 | |
| 940 | return TRUE; |
| 941 | } |
| 942 | |
| 943 | bool wxMenu::DoAppend(wxMenuItem *mitem) |
| 944 | { |
| 945 | return GtkAppend(mitem) && wxMenuBase::DoAppend(mitem); |
| 946 | } |
| 947 | |
| 948 | bool wxMenu::DoInsert(size_t pos, wxMenuItem *item) |
| 949 | { |
| 950 | if ( !wxMenuBase::DoInsert(pos, item) ) |
| 951 | return FALSE; |
| 952 | |
| 953 | #ifdef __WXGTK12__ |
| 954 | // GTK+ doesn't have a function to insert a menu using GtkItemFactory (as |
| 955 | // of version 1.2.6), so we first append the item and then change its |
| 956 | // index |
| 957 | if ( !GtkAppend(item) ) |
| 958 | return FALSE; |
| 959 | |
| 960 | GtkMenuShell *menu_shell = GTK_MENU_SHELL(m_factory->widget); |
| 961 | gpointer data = g_list_last(menu_shell->children)->data; |
| 962 | menu_shell->children = g_list_remove(menu_shell->children, data); |
| 963 | menu_shell->children = g_list_insert(menu_shell->children, data, pos); |
| 964 | |
| 965 | return TRUE; |
| 966 | #else // GTK < 1.2 |
| 967 | // this should be easy to do... |
| 968 | wxFAIL_MSG( wxT("not implemented") ); |
| 969 | |
| 970 | return FALSE; |
| 971 | #endif // GTK 1.2/1.0 |
| 972 | } |
| 973 | |
| 974 | wxMenuItem *wxMenu::DoRemove(wxMenuItem *item) |
| 975 | { |
| 976 | if ( !wxMenuBase::DoRemove(item) ) |
| 977 | return (wxMenuItem *)NULL; |
| 978 | |
| 979 | // TODO: this code doesn't delete the item factory item and this seems |
| 980 | // impossible as of GTK 1.2.6. |
| 981 | gtk_widget_destroy( item->GetMenuItem() ); |
| 982 | |
| 983 | return item; |
| 984 | } |
| 985 | |
| 986 | int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const |
| 987 | { |
| 988 | wxNode *node = m_items.First(); |
| 989 | while (node) |
| 990 | { |
| 991 | wxMenuItem *item = (wxMenuItem*)node->Data(); |
| 992 | if (item->GetMenuItem() == menuItem) |
| 993 | return item->GetId(); |
| 994 | node = node->Next(); |
| 995 | } |
| 996 | |
| 997 | return wxNOT_FOUND; |
| 998 | } |
| 999 | |
| 1000 | // ---------------------------------------------------------------------------- |
| 1001 | // helpers |
| 1002 | // ---------------------------------------------------------------------------- |
| 1003 | |
| 1004 | #if (GTK_MINOR_VERSION > 0) && wxUSE_ACCEL |
| 1005 | static wxString GetHotKey( const wxMenuItem& item ) |
| 1006 | { |
| 1007 | wxString hotkey; |
| 1008 | |
| 1009 | wxAcceleratorEntry *accel = item.GetAccel(); |
| 1010 | if ( accel ) |
| 1011 | { |
| 1012 | int flags = accel->GetFlags(); |
| 1013 | if ( flags & wxACCEL_ALT ) |
| 1014 | hotkey += wxT("<alt>"); |
| 1015 | if ( flags & wxACCEL_CTRL ) |
| 1016 | hotkey += wxT("<control>"); |
| 1017 | if ( flags & wxACCEL_SHIFT ) |
| 1018 | hotkey += wxT("<shift>"); |
| 1019 | |
| 1020 | int code = accel->GetKeyCode(); |
| 1021 | switch ( code ) |
| 1022 | { |
| 1023 | case WXK_F1: |
| 1024 | case WXK_F2: |
| 1025 | case WXK_F3: |
| 1026 | case WXK_F4: |
| 1027 | case WXK_F5: |
| 1028 | case WXK_F6: |
| 1029 | case WXK_F7: |
| 1030 | case WXK_F8: |
| 1031 | case WXK_F9: |
| 1032 | case WXK_F10: |
| 1033 | case WXK_F11: |
| 1034 | case WXK_F12: |
| 1035 | hotkey << wxT('F') << code - WXK_F1 + 1; |
| 1036 | break; |
| 1037 | |
| 1038 | // if there are any other keys wxGetAccelFromString() may return, |
| 1039 | // we should process them here |
| 1040 | |
| 1041 | default: |
| 1042 | if ( wxIsalnum(code) ) |
| 1043 | { |
| 1044 | hotkey << (wxChar)code; |
| 1045 | |
| 1046 | break; |
| 1047 | } |
| 1048 | |
| 1049 | wxFAIL_MSG( wxT("unknown keyboard accel") ); |
| 1050 | } |
| 1051 | |
| 1052 | delete accel; |
| 1053 | } |
| 1054 | |
| 1055 | return hotkey; |
| 1056 | } |
| 1057 | #endif // wxUSE_ACCEL |
| 1058 | |