From f03ec224dcde69ee0b30f5baecc4b5e5466b02c1 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 4 Dec 1999 13:43:59 +0000 Subject: [PATCH] wxMenuBar::Insert() seems to work! git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@4817 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/gtk/menu.h | 3 ++ include/wx/gtk1/menu.h | 3 ++ samples/menu/menu.cpp | 35 ++++++++++++++------ src/gtk/menu.cpp | 72 +++++++++++++++++++++++++++++++++++++----- src/gtk1/menu.cpp | 72 +++++++++++++++++++++++++++++++++++++----- 5 files changed, 160 insertions(+), 25 deletions(-) diff --git a/include/wx/gtk/menu.h b/include/wx/gtk/menu.h index c9d198017e..4f670803ec 100644 --- a/include/wx/gtk/menu.h +++ b/include/wx/gtk/menu.h @@ -45,6 +45,9 @@ public: void SetInvokingWindow( wxWindow *win ); void UnsetInvokingWindow( wxWindow *win ); + // common part of Append and Insert + bool GtkAppend(wxMenu *menu, const wxString& title); + GtkAccelGroup *m_accel; GtkItemFactory *m_factory; GtkWidget *m_menubar; diff --git a/include/wx/gtk1/menu.h b/include/wx/gtk1/menu.h index c9d198017e..4f670803ec 100644 --- a/include/wx/gtk1/menu.h +++ b/include/wx/gtk1/menu.h @@ -45,6 +45,9 @@ public: void SetInvokingWindow( wxWindow *win ); void UnsetInvokingWindow( wxWindow *win ); + // common part of Append and Insert + bool GtkAppend(wxMenu *menu, const wxString& title); + GtkAccelGroup *m_accel; GtkItemFactory *m_factory; GtkWidget *m_menubar; diff --git a/samples/menu/menu.cpp b/samples/menu/menu.cpp index b4fa149155..26cb1baeac 100644 --- a/samples/menu/menu.cpp +++ b/samples/menu/menu.cpp @@ -65,6 +65,7 @@ public: void OnGetMenuItemInfo(wxCommandEvent& event); void OnAppendMenu(wxCommandEvent& event); + void OnInsertMenu(wxCommandEvent& event); void OnDeleteMenu(wxCommandEvent& event); void OnToggleMenu(wxCommandEvent& event); void OnEnableMenu(wxCommandEvent& event); @@ -76,12 +77,14 @@ public: void OnUpdateCheckMenuItemUI(wxUpdateUIEvent& event); private: - wxMenu *CreateDummyMenu(); + wxMenu *CreateDummyMenu(wxString *title); wxMenuItem *GetLastMenuItem() const; wxMenu *m_menu; + size_t m_countDummy; + DECLARE_EVENT_TABLE() }; @@ -95,6 +98,7 @@ enum Menu_MenuBar_Toggle = 200, Menu_MenuBar_Append, + Menu_MenuBar_Insert, Menu_MenuBar_Delete, Menu_MenuBar_Enable, Menu_MenuBar_GetLabel, @@ -137,6 +141,7 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_MENU(Menu_MenuBar_Toggle, MyFrame::OnToggleMenu) EVT_MENU(Menu_MenuBar_Append, MyFrame::OnAppendMenu) + EVT_MENU(Menu_MenuBar_Insert, MyFrame::OnInsertMenu) EVT_MENU(Menu_MenuBar_Delete, MyFrame::OnDeleteMenu) EVT_MENU(Menu_MenuBar_Enable, MyFrame::OnEnableMenu) EVT_MENU(Menu_MenuBar_GetLabel, MyFrame::OnGetLabelMenu) @@ -195,6 +200,7 @@ MyFrame::MyFrame() wxDefaultPosition, wxSize(300, 200)) { m_menu = NULL; + m_countDummy = 0; CreateStatusBar(); @@ -205,6 +211,8 @@ MyFrame::MyFrame() wxMenu *menubarMenu = new wxMenu; menubarMenu->Append(Menu_MenuBar_Append, "&Append menu\tCtrl-A", "Append a menu to the menubar"); + menubarMenu->Append(Menu_MenuBar_Insert, "&Insert menu\tCtrl-I", + "Insert a menu into the menubar"); menubarMenu->Append(Menu_MenuBar_Delete, "&Delete menu\tCtrl-D", "Delete the last menu from the menubar"); menubarMenu->Append(Menu_MenuBar_Toggle, "&Toggle menu\tCtrl-T", @@ -261,13 +269,18 @@ MyFrame::MyFrame() SetMenuBar(menuBar); } -wxMenu *MyFrame::CreateDummyMenu() +wxMenu *MyFrame::CreateDummyMenu(wxString *title) { wxMenu *menu = new wxMenu; menu->Append(Menu_Dummy_First, "First item\tCtrl-F1"); menu->AppendSeparator(); menu->Append(Menu_Dummy_Second, "Second item\tCtrl-F2", "", TRUE); + if ( title ) + { + title->Printf("Dummy menu &%d", ++m_countDummy); + } + return menu; } @@ -317,14 +330,18 @@ void MyFrame::OnDeleteMenu(wxCommandEvent& WXUNUSED(event)) } } -void MyFrame::OnAppendMenu(wxCommandEvent& WXUNUSED(event)) +void MyFrame::OnInsertMenu(wxCommandEvent& WXUNUSED(event)) { - static int s_count = 0; - wxString title; - title.Printf("Dummy menu &%d", ++s_count); + wxMenu *menu = CreateDummyMenu(&title); + GetMenuBar()->Insert(0, menu, title); +} - GetMenuBar()->Append(CreateDummyMenu(), title); +void MyFrame::OnAppendMenu(wxCommandEvent& WXUNUSED(event)) +{ + wxString title; + wxMenu *menu = CreateDummyMenu(&title); + GetMenuBar()->Append(menu, title); } void MyFrame::OnToggleMenu(wxCommandEvent& WXUNUSED(event)) @@ -395,7 +412,7 @@ void MyFrame::OnAppendSubMenu(wxCommandEvent& WXUNUSED(event)) wxMenu *menu = menubar->GetMenu(menubar->GetMenuCount() - 1); menu->Append(Menu_Dummy_Last, "Dummy sub menu\tCtrl-F2", - CreateDummyMenu()); + CreateDummyMenu(NULL)); } void MyFrame::OnDeleteMenuItem(wxCommandEvent& WXUNUSED(event)) @@ -549,7 +566,7 @@ void MyFrame::OnRightDown(wxMouseEvent &event ) wxMenu menu("Test popup"); menu.Append(Menu_Help_About, "&About"); - menu.Append(Menu_Popup_Submenu, "Submenu", CreateDummyMenu()); + menu.Append(Menu_Popup_Submenu, "Submenu", CreateDummyMenu(NULL)); menu.Append(Menu_Popup_ToBeDeleted, "To be deleted"); menu.Append(Menu_Popup_ToBeChecked, "To be checked", "", TRUE); menu.Append(Menu_Popup_ToBeGreyed, "To be greyed"); diff --git a/src/gtk/menu.cpp b/src/gtk/menu.cpp index b645a865b9..b7f1643856 100644 --- a/src/gtk/menu.cpp +++ b/src/gtk/menu.cpp @@ -205,8 +205,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,9 +302,27 @@ 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) @@ -306,17 +330,48 @@ wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title) if ( !wxMenuBarBase::Replace(pos, menu, title) ) return FALSE; - wxFAIL_MSG(wxT("TODO")); + // remove the old item and insert a new one + wxMenu *menuOld = Remove(pos); + if ( menuOld && !Insert(pos, menu, title) ) + { + return 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) ) + wxMenu *menu = wxMenuBarBase::Remove(pos); + if ( !menu ) return FALSE; - wxFAIL_MSG(wxT("TODO")); +#ifdef __WXGTK12__ + // gtk_item_factory_delete_entry() is buggy as of GTK+ 1.2.6, so don't use + // it but delete the widget manually instead + wxString path = _T("
/"), + title = menu->GetTitle(); + for ( const wxChar *p = title.c_str(); *p; p++ ) + { + if ( *p != _T('_') ) + path += *p; + } + + GtkWidget *widget = gtk_item_factory_get_item(m_factory, path.mb_str()); + if ( widget ) + { + gtk_widget_destroy(widget); + + return menu; + } + + // shouldn't happen (FIXME but does now) + wxFAIL_MSG( _T("gtk_item_factory_get_item() failed") ); +#else // GTK < 1.2 + // this should be very simple to implement + wxFAIL_MSG( wxT("TODO") ); +#endif // GTK 1.2/1.0 return NULL; } @@ -705,7 +760,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(); @@ -758,7 +814,7 @@ void wxMenu::Init() wxMenu::~wxMenu() { m_items.Clear(); - + gtk_widget_destroy( m_menu ); gtk_object_unref( GTK_OBJECT(m_factory) ); diff --git a/src/gtk1/menu.cpp b/src/gtk1/menu.cpp index b645a865b9..b7f1643856 100644 --- a/src/gtk1/menu.cpp +++ b/src/gtk1/menu.cpp @@ -205,8 +205,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,9 +302,27 @@ 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) @@ -306,17 +330,48 @@ wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title) if ( !wxMenuBarBase::Replace(pos, menu, title) ) return FALSE; - wxFAIL_MSG(wxT("TODO")); + // remove the old item and insert a new one + wxMenu *menuOld = Remove(pos); + if ( menuOld && !Insert(pos, menu, title) ) + { + return 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) ) + wxMenu *menu = wxMenuBarBase::Remove(pos); + if ( !menu ) return FALSE; - wxFAIL_MSG(wxT("TODO")); +#ifdef __WXGTK12__ + // gtk_item_factory_delete_entry() is buggy as of GTK+ 1.2.6, so don't use + // it but delete the widget manually instead + wxString path = _T("
/"), + title = menu->GetTitle(); + for ( const wxChar *p = title.c_str(); *p; p++ ) + { + if ( *p != _T('_') ) + path += *p; + } + + GtkWidget *widget = gtk_item_factory_get_item(m_factory, path.mb_str()); + if ( widget ) + { + gtk_widget_destroy(widget); + + return menu; + } + + // shouldn't happen (FIXME but does now) + wxFAIL_MSG( _T("gtk_item_factory_get_item() failed") ); +#else // GTK < 1.2 + // this should be very simple to implement + wxFAIL_MSG( wxT("TODO") ); +#endif // GTK 1.2/1.0 return NULL; } @@ -705,7 +760,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(); @@ -758,7 +814,7 @@ void wxMenu::Init() wxMenu::~wxMenu() { m_items.Clear(); - + gtk_widget_destroy( m_menu ); gtk_object_unref( GTK_OBJECT(m_factory) ); -- 2.45.2