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;
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;
void OnGetMenuItemInfo(wxCommandEvent& event);
void OnAppendMenu(wxCommandEvent& event);
+ void OnInsertMenu(wxCommandEvent& event);
void OnDeleteMenu(wxCommandEvent& event);
void OnToggleMenu(wxCommandEvent& event);
void OnEnableMenu(wxCommandEvent& event);
void OnUpdateCheckMenuItemUI(wxUpdateUIEvent& event);
private:
- wxMenu *CreateDummyMenu();
+ wxMenu *CreateDummyMenu(wxString *title);
wxMenuItem *GetLastMenuItem() const;
wxMenu *m_menu;
+ size_t m_countDummy;
+
DECLARE_EVENT_TABLE()
};
Menu_MenuBar_Toggle = 200,
Menu_MenuBar_Append,
+ Menu_MenuBar_Insert,
Menu_MenuBar_Delete,
Menu_MenuBar_Enable,
Menu_MenuBar_GetLabel,
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)
wxDefaultPosition, wxSize(300, 200))
{
m_menu = NULL;
+ m_countDummy = 0;
CreateStatusBar();
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",
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;
}
}
}
-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))
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))
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");
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 */
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;
- 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("<main>/"),
+ 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;
}
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("<main>/") );
path += GetLabel();
wxMenu::~wxMenu()
{
m_items.Clear();
-
+
gtk_widget_destroy( m_menu );
gtk_object_unref( GTK_OBJECT(m_factory) );
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 */
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;
- 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("<main>/"),
+ 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;
}
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("<main>/") );
path += GetLabel();
wxMenu::~wxMenu()
{
m_items.Clear();
-
+
gtk_widget_destroy( m_menu );
gtk_object_unref( GTK_OBJECT(m_factory) );