X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/db434467a143a2c847c4bcfe987f0202c2d02bf2..40d432c482326ccbc609b212415f3c62ddd4e70d:/src/gtk/menu.cpp diff --git a/src/gtk/menu.cpp b/src/gtk/menu.cpp index 6758d6b4ba..b3b73c34f5 100644 --- a/src/gtk/menu.cpp +++ b/src/gtk/menu.cpp @@ -35,6 +35,45 @@ extern bool g_isIdle; static wxString GetHotKey( const wxMenuItem& item ); #endif +//----------------------------------------------------------------------------- +// idle system +//----------------------------------------------------------------------------- + +static wxString wxReplaceUnderscore( const wxString& title ) +{ + const wxChar *pc; + + /* GTK 1.2 wants to have "_" instead of "&" for accelerators */ + wxString str; + for ( pc = title; *pc != wxT('\0'); pc++ ) + { + if (*pc == wxT('&')) + { +#if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0) + str << wxT('_'); + } + else if (*pc == wxT('/')) + { + str << wxT('\\'); +#endif + } + else + { +#if __WXGTK12__ + if ( *pc == wxT('_') ) + { + // underscores must be doubled to prevent them from being + // interpreted as accelerator character prefix by GTK + str << *pc; + } +#endif // GTK+ 1.2 + + str << *pc; + } + } + return str; +} + //----------------------------------------------------------------------------- // wxMenuBar //----------------------------------------------------------------------------- @@ -78,7 +117,7 @@ wxMenuBar::wxMenuBar( long style ) } PostCreation(); - + ApplyWidgetStyle(); } @@ -110,7 +149,7 @@ wxMenuBar::wxMenuBar() m_widget = GTK_WIDGET(m_menubar); PostCreation(); - + ApplyWidgetStyle(); } @@ -146,13 +185,15 @@ static void wxMenubarSetInvokingWindow( wxMenu *menu, wxWindow *win ) { menu->SetInvokingWindow( win ); -#if (GTK_MINOR_VERSION > 0) +#if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0) wxWindow *top_frame = win; while (top_frame->GetParent() && !(top_frame->IsTopLevel())) top_frame = top_frame->GetParent(); /* support for native hot keys */ - gtk_accel_group_attach( menu->m_accel, GTK_OBJECT(top_frame->m_widget) ); + GtkObject *obj = GTK_OBJECT(top_frame->m_widget); + if ( !g_slist_find( menu->m_accel->attach_objects, obj ) ) + gtk_accel_group_attach( menu->m_accel, obj ); #endif wxMenuItemList::Node *node = menu->GetMenuItems().GetFirst(); @@ -174,7 +215,9 @@ void wxMenuBar::SetInvokingWindow( wxWindow *win ) top_frame = top_frame->GetParent(); /* support for native key accelerators indicated by underscroes */ - gtk_accel_group_attach( m_accel, GTK_OBJECT(top_frame->m_widget) ); + GtkObject *obj = GTK_OBJECT(top_frame->m_widget); + if ( !g_slist_find( m_accel->attach_objects, obj ) ) + gtk_accel_group_attach( m_accel, obj ); #endif wxMenuList::Node *node = m_menus.GetFirst(); @@ -217,36 +260,7 @@ bool wxMenuBar::Append( wxMenu *menu, const wxString &title ) bool wxMenuBar::GtkAppend(wxMenu *menu, const wxString& title) { - const wxChar *pc; - - /* GTK 1.2 wants to have "_" instead of "&" for accelerators */ - wxString str; - for ( pc = title; *pc != wxT('\0'); pc++ ) - { - if (*pc == wxT('&')) - { -#if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0) - str << wxT('_'); - } - else if (*pc == wxT('/')) - { - str << wxT('\\'); -#endif - } - else - { -#if __WXGTK12__ - if ( *pc == wxT('_') ) - { - // underscores must be doubled to prevent them from being - // interpreted as accelerator character prefix by GTK - str << *pc; - } -#endif // GTK+ 1.2 - - str << *pc; - } - } + wxString str( wxReplaceUnderscore( title ) ); /* this doesn't have much effect right now */ menu->SetTitle( str ); @@ -271,6 +285,7 @@ bool wxMenuBar::GtkAppend(wxMenu *menu, const wxString& title) gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */ /* in order to get the pointer to the item we need the item text _without_ underscores */ wxString tmp = wxT("
/"); + const wxChar *pc; for ( pc = str; *pc != wxT('\0'); pc++ ) { // contrary to the common sense, we must throw out _all_ underscores, @@ -313,6 +328,9 @@ bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title) if ( !GtkAppend(menu, title) ) return FALSE; + if (pos+1 >= m_menus.GetCount()) + return TRUE; + GtkMenuShell *menu_shell = GTK_MENU_SHELL(m_factory->widget); gpointer data = g_list_last(menu_shell->children)->data; menu_shell->children = g_list_remove(menu_shell->children, data); @@ -331,9 +349,6 @@ bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title) wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title) { - if ( !wxMenuBarBase::Replace(pos, menu, title) ) - return (wxMenu*) NULL; - // remove the old item and insert a new one wxMenu *menuOld = Remove(pos); if ( menuOld && !Insert(pos, menu, title) ) @@ -351,26 +366,31 @@ wxMenu *wxMenuBar::Remove(size_t pos) if ( !menu ) return (wxMenu*) NULL; +/* GtkMenuShell *menu_shell = GTK_MENU_SHELL(m_factory->widget); + printf( "factory entries before %d\n", (int)g_slist_length(m_factory->items) ); printf( "menu shell entries before %d\n", (int)g_list_length( menu_shell->children ) ); - +*/ + // unparent calls unref() and that would delete the widget so we raise // the ref count to 2 artificially before invoking unparent. gtk_widget_ref( menu->m_menu ); gtk_widget_unparent( menu->m_menu ); - + gtk_widget_destroy( menu->m_owner ); - + +/* printf( "factory entries after %d\n", (int)g_slist_length(m_factory->items) ); printf( "menu shell entries after %d\n", (int)g_list_length( menu_shell->children ) ); - +*/ + return menu; } static int FindMenuItemRecursive( const wxMenu *menu, const wxString &menuString, const wxString &itemString ) { - if (menu->GetTitle() == menuString) + if (wxMenuItem::GetLabelFromText(menu->GetTitle()) == wxMenuItem::GetLabelFromText(menuString)) { int res = menu->FindItem( itemString ); if (res != wxNOT_FOUND) @@ -463,7 +483,25 @@ wxString wxMenuBar::GetLabelTop( size_t pos ) const wxMenu* menu = node->GetData(); - return menu->GetTitle(); + wxString label; + wxString text( menu->GetTitle() ); +#if (GTK_MINOR_VERSION > 0) + for ( const wxChar *pc = text.c_str(); *pc; pc++ ) + { + if ( *pc == wxT('_') || *pc == wxT('&') ) + { + // '_' is the escape character for GTK+ and '&' is the one for + // wxWindows - skip both of them + continue; + } + + label += *pc; + } +#else // GTK+ 1.0 + label = text; +#endif // GTK+ 1.2/1.0 + + return label; } void wxMenuBar::SetLabelTop( size_t pos, const wxString& label ) @@ -474,7 +512,22 @@ void wxMenuBar::SetLabelTop( size_t pos, const wxString& label ) wxMenu* menu = node->GetData(); - menu->SetTitle( label ); + wxString str( wxReplaceUnderscore( label ) ); + + menu->SetTitle( str ); + + if (menu->m_owner) + { + GtkLabel *label = GTK_LABEL( GTK_BIN(menu->m_owner)->child ); + + /* set new text */ + gtk_label_set( label, str.mb_str()); + + /* reparse key accel */ + (void)gtk_label_parse_uline (GTK_LABEL(label), str.mb_str() ); + gtk_accel_label_refetch( GTK_ACCEL_LABEL(label) ); + } + } //----------------------------------------------------------------------------- @@ -484,7 +537,7 @@ void wxMenuBar::SetLabelTop( size_t pos, const wxString& label ) static void gtk_menu_clicked_callback( GtkWidget *widget, wxMenu *menu ) { if (g_isIdle) wxapp_install_idle_handler(); - + int id = menu->FindMenuIdByMenuItem(widget); /* should find it for normal (not popup) menu */ @@ -514,7 +567,8 @@ static void gtk_menu_clicked_callback( GtkWidget *widget, wxMenu *menu ) wxCommandEvent event( wxEVT_COMMAND_MENU_SELECTED, id ); event.SetEventObject( menu ); - event.SetInt(id ); + if (item->IsCheckable()) + event.SetInt( item->IsChecked() ); #if wxUSE_MENU_CALLBACK if (menu->GetCallback()) @@ -625,22 +679,24 @@ wxMenuItem::~wxMenuItem() } // return the menu item text without any menu accels -wxString wxMenuItem::GetLabel() const +/* static */ +wxString wxMenuItemBase::GetLabelFromText(const wxString& text) { wxString label; #if (GTK_MINOR_VERSION > 0) - for ( const wxChar *pc = m_text.c_str(); *pc; pc++ ) + for ( const wxChar *pc = text.c_str(); *pc; pc++ ) { - if ( *pc == wxT('_') ) + if ( *pc == wxT('_') || *pc == wxT('&') ) { - // this is the escape character for GTK+ - skip it + // '_' is the escape character for GTK+ and '&' is the one for + // wxWindows - skip both of them continue; } label += *pc; } #else // GTK+ 1.0 - label = m_text; + label = text; #endif // GTK+ 1.2/1.0 return label; @@ -861,6 +917,12 @@ bool wxMenu::GtkAppend(wxMenuItem *mitem) #endif // GTK 1.2/1.0 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), mitem->GetSubMenu()->m_menu ); + + // if adding a submenu to a menu already existing in the menu bar, we + // must set invoking window to allow processing events from this + // submenu + if ( m_invokingWindow ) + wxMenubarSetInvokingWindow(mitem->GetSubMenu(), m_invokingWindow); } else // a normal item { @@ -887,8 +949,9 @@ bool wxMenu::GtkAppend(wxMenuItem *mitem) // due to an apparent bug in GTK+, we have to use a static buffer here - // otherwise GTK+ 1.2.2 manages to override the memory we pass to it // somehow! (VZ) - static char s_accel[32]; // must be big enough for F12 - strncpy(s_accel, GetHotKey(*mitem).mb_str(), WXSIZEOF(s_accel)); + static char s_accel[50]; // must be big enougg + wxString tmp( GetHotKey(*mitem) ); + strncpy(s_accel, tmp.mb_str(), WXSIZEOF(s_accel)); entry.accelerator = s_accel; #else // !wxUSE_ACCEL entry.accelerator = (char*) NULL; @@ -946,6 +1009,12 @@ bool wxMenu::DoInsert(size_t pos, wxMenuItem *item) if ( !GtkAppend(item) ) return FALSE; + if ( m_style & wxMENU_TEAROFF ) + { + // change the position as the first item is the tear-off marker + pos++; + } + GtkMenuShell *menu_shell = GTK_MENU_SHELL(m_factory->widget); gpointer data = g_list_last(menu_shell->children)->data; menu_shell->children = g_list_remove(menu_shell->children, data); @@ -955,9 +1024,9 @@ bool wxMenu::DoInsert(size_t pos, wxMenuItem *item) #else // GTK < 1.2 // this should be easy to do... wxFAIL_MSG( wxT("not implemented") ); -#endif // GTK 1.2/1.0 return FALSE; +#endif // GTK 1.2/1.0 } wxMenuItem *wxMenu::DoRemove(wxMenuItem *item) @@ -1023,6 +1092,20 @@ static wxString GetHotKey( const wxMenuItem& item ) case WXK_F12: hotkey << wxT('F') << code - WXK_F1 + 1; break; + + // GTK seems to use XStringToKeySym here + case WXK_NUMPAD_INSERT: + hotkey << wxT("KP_Insert" ); + break; + case WXK_NUMPAD_DELETE: + hotkey << wxT("KP_Delete" ); + break; + case WXK_INSERT: + hotkey << wxT("Insert" ); + break; + case WXK_DELETE: + hotkey << wxT("Delete" ); + break; // if there are any other keys wxGetAccelFromString() may return, // we should process them here