X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/1987af7e9f5685198b0c19e2326eebf7e56b7834..8535af00e277ee89c877e9cb3ac357791119f497:/src/gtk1/menu.cpp diff --git a/src/gtk1/menu.cpp b/src/gtk1/menu.cpp index 5ebd301230..daff7c92be 100644 --- a/src/gtk1/menu.cpp +++ b/src/gtk1/menu.cpp @@ -21,8 +21,8 @@ #include "wx/accel.h" #endif // wxUSE_ACCEL -#include "gdk/gdk.h" -#include "gtk/gtk.h" +#include +#include //----------------------------------------------------------------------------- // idle system @@ -78,6 +78,8 @@ wxMenuBar::wxMenuBar( long style ) } PostCreation(); + + ApplyWidgetStyle(); } wxMenuBar::wxMenuBar() @@ -108,6 +110,8 @@ wxMenuBar::wxMenuBar() m_widget = GTK_WIDGET(m_menubar); PostCreation(); + + ApplyWidgetStyle(); } wxMenuBar::~wxMenuBar() @@ -121,7 +125,7 @@ static void wxMenubarUnsetInvokingWindow( wxMenu *menu, wxWindow *win ) #if (GTK_MINOR_VERSION > 0) wxWindow *top_frame = win; - while (top_frame->GetParent() && !(top_frame->GetParent()->m_isFrame)) + while (top_frame->GetParent() && !(top_frame->IsTopLevel())) top_frame = top_frame->GetParent(); /* support for native hot keys */ @@ -144,7 +148,7 @@ static void wxMenubarSetInvokingWindow( wxMenu *menu, wxWindow *win ) #if (GTK_MINOR_VERSION > 0) wxWindow *top_frame = win; - while (top_frame->GetParent() && !(top_frame->GetParent()->m_isFrame)) + while (top_frame->GetParent() && !(top_frame->IsTopLevel())) top_frame = top_frame->GetParent(); /* support for native hot keys */ @@ -166,7 +170,7 @@ void wxMenuBar::SetInvokingWindow( wxWindow *win ) m_invokingWindow = win; #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0) wxWindow *top_frame = win; - while (top_frame->GetParent() && !(top_frame->GetParent()->m_isFrame)) + while (top_frame->GetParent() && !(top_frame->IsTopLevel())) top_frame = top_frame->GetParent(); /* support for native key accelerators indicated by underscroes */ @@ -187,7 +191,7 @@ void wxMenuBar::UnsetInvokingWindow( wxWindow *win ) m_invokingWindow = (wxWindow*) NULL; #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0) wxWindow *top_frame = win; - while (top_frame->GetParent() && !(top_frame->GetParent()->m_isFrame)) + while (top_frame->GetParent() && !(top_frame->IsTopLevel())) top_frame = top_frame->GetParent(); /* support for native key accelerators indicated by underscroes */ @@ -205,8 +209,14 @@ void wxMenuBar::UnsetInvokingWindow( wxWindow *win ) bool wxMenuBar::Append( wxMenu *menu, const wxString &title ) { - m_menus.Append( menu ); + if ( !wxMenuBarBase::Append( menu, title ) ) + return FALSE; + + return GtkAppend(menu, title); +} +bool wxMenuBar::GtkAppend(wxMenu *menu, const wxString& title) +{ const wxChar *pc; /* GTK 1.2 wants to have "_" instead of "&" for accelerators */ @@ -296,29 +306,66 @@ bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title) if ( !wxMenuBarBase::Insert(pos, menu, title) ) return FALSE; - wxFAIL_MSG(wxT("TODO")); +#if __WXGTK12__ + // GTK+ doesn't have a function to insert a menu using GtkItemFactory (as + // of version 1.2.6), so we first append the item and then change its + // index + if ( !GtkAppend(menu, title) ) + return FALSE; + + 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); + menu_shell->children = g_list_insert(menu_shell->children, data, pos); + + return TRUE; +#else // GTK < 1.2 + // this should be easy to do with GTK 1.0 - can use standard functions for + // this and don't need any hacks like above, but as I don't have GTK 1.0 + // any more I can't do it + wxFAIL_MSG( wxT("TODO") ); return FALSE; +#endif // GTK 1.2/1.0 } wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title) { if ( !wxMenuBarBase::Replace(pos, menu, title) ) - return FALSE; + return (wxMenu*) NULL; - wxFAIL_MSG(wxT("TODO")); + // remove the old item and insert a new one + wxMenu *menuOld = Remove(pos); + if ( menuOld && !Insert(pos, menu, title) ) + { + return (wxMenu*) NULL; + } - return NULL; + // either Insert() succeeded or Remove() failed and menuOld is NULL + return menuOld; } wxMenu *wxMenuBar::Remove(size_t pos) { - if ( !wxMenuBarBase::Remove(pos) ) - return FALSE; - - wxFAIL_MSG(wxT("TODO")); - - return NULL; + wxMenu *menu = wxMenuBarBase::Remove(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 ) @@ -437,7 +484,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 */ @@ -469,13 +516,13 @@ static void gtk_menu_clicked_callback( GtkWidget *widget, wxMenu *menu ) event.SetEventObject( menu ); event.SetInt(id ); -#if WXWIN_COMPATIBILITY +#if wxUSE_MENU_CALLBACK if (menu->GetCallback()) { (void) (*(menu->GetCallback())) (*menu, event); return; } -#endif // WXWIN_COMPATIBILITY +#endif // wxUSE_MENU_CALLBACK if (menu->GetEventHandler()->ProcessEvent(event)) return; @@ -578,11 +625,12 @@ 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('_') ) { @@ -593,7 +641,7 @@ wxString wxMenuItem::GetLabel() const label += *pc; } #else // GTK+ 1.0 - label = m_text; + label = text; #endif // GTK+ 1.2/1.0 return label; @@ -705,7 +753,8 @@ bool wxMenuItem::IsChecked() const wxString wxMenuItem::GetFactoryPath() const { - /* in order to get the pointer to the item we need the item text _without_ underscores */ + /* in order to get the pointer to the item we need the item text _without_ + underscores */ wxString path( wxT("
/") ); path += GetLabel(); @@ -757,14 +806,14 @@ void wxMenu::Init() wxMenu::~wxMenu() { + m_items.Clear(); + gtk_widget_destroy( m_menu ); gtk_object_unref( GTK_OBJECT(m_factory) ); - - // the menu items are deleted by the base class dtor } -bool wxMenu::DoAppend(wxMenuItem *mitem) +bool wxMenu::GtkAppend(wxMenuItem *mitem) { GtkWidget *menuItem; @@ -802,6 +851,7 @@ bool wxMenu::DoAppend(wxMenuItem *mitem) entry.callback = (GtkItemFactoryCallback) 0; entry.callback_action = 0; entry.item_type = ""; + entry.accelerator = (gchar*) NULL; gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */ @@ -832,6 +882,7 @@ bool wxMenu::DoAppend(wxMenuItem *mitem) entry.item_type = ""; else entry.item_type = ""; + entry.accelerator = (gchar*) NULL; #if wxUSE_ACCEL // due to an apparent bug in GTK+, we have to use a static buffer here - @@ -876,58 +927,38 @@ bool wxMenu::DoAppend(wxMenuItem *mitem) mitem->SetMenuItem(menuItem); - return wxMenuBase::DoAppend(mitem); + return TRUE; } -// VZ: this seems to be GTK+ 1.0 only code, I don't understand why there were -// both specialized versions of Append() and this one before my changes, -// but it seems that the others are better... -#if 0 -void wxMenu::Append( wxMenuItem *item ) +bool wxMenu::DoAppend(wxMenuItem *mitem) { - GtkWidget *menuItem = (GtkWidget*) NULL; - - if (item->IsSeparator()) - menuItem = gtk_menu_item_new(); - else if (item->IsSubMenu()) - menuItem = gtk_menu_item_new_with_label(item->GetText().mbc_str()); - else - menuItem = item->IsCheckable() ? gtk_check_menu_item_new_with_label(item->GetText().mbc_str()) - : gtk_menu_item_new_with_label(item->GetText().mbc_str()); - - if (!item->IsSeparator()) - { - gtk_signal_connect( GTK_OBJECT(menuItem), "select", - GTK_SIGNAL_FUNC(gtk_menu_hilight_callback), - (gpointer*)this ); - - gtk_signal_connect( GTK_OBJECT(menuItem), "deselect", - GTK_SIGNAL_FUNC(gtk_menu_nolight_callback), - (gpointer*)this ); - - if (!item->IsSubMenu()) - { - gtk_signal_connect( GTK_OBJECT(menuItem), "activate", - GTK_SIGNAL_FUNC(gtk_menu_clicked_callback), - (gpointer*)this ); - } - } - - gtk_menu_append( GTK_MENU(m_menu), menuItem ); - gtk_widget_show( menuItem ); - - item->SetMenuItem(menuItem); + return GtkAppend(mitem) && wxMenuBase::DoAppend(mitem); } -#endif // 0 bool wxMenu::DoInsert(size_t pos, wxMenuItem *item) { if ( !wxMenuBase::DoInsert(pos, item) ) return FALSE; - wxFAIL_MSG(wxT("not implemented")); +#ifdef __WXGTK12__ + // GTK+ doesn't have a function to insert a menu using GtkItemFactory (as + // of version 1.2.6), so we first append the item and then change its + // index + if ( !GtkAppend(item) ) + return FALSE; + + 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); + menu_shell->children = g_list_insert(menu_shell->children, data, pos); + + return TRUE; +#else // GTK < 1.2 + // this should be easy to do... + wxFAIL_MSG( wxT("not implemented") ); return FALSE; +#endif // GTK 1.2/1.0 } wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)