From d1b15f03b8e614c83541ca3487f847f61122b514 Mon Sep 17 00:00:00 2001 From: Robert Roebling Date: Mon, 11 Oct 1999 19:32:58 +0000 Subject: [PATCH] Added wxMenu::Delete() and fixed some menu deleted memory leaks. Some. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@3930 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/gtk/menu.h | 9 ++++++--- include/wx/gtk/menuitem.h | 1 + include/wx/gtk1/menu.h | 9 ++++++--- include/wx/gtk1/menuitem.h | 1 + samples/dnd/dnd.cpp | 17 ++++++++++++++++- src/gtk/menu.cpp | 37 ++++++++++++++++++++++++++++++++++--- src/gtk1/menu.cpp | 37 ++++++++++++++++++++++++++++++++++--- 7 files changed, 98 insertions(+), 13 deletions(-) diff --git a/include/wx/gtk/menu.h b/include/wx/gtk/menu.h index 8d6f68db7e..dfa0569718 100644 --- a/include/wx/gtk/menu.h +++ b/include/wx/gtk/menu.h @@ -128,11 +128,11 @@ public: ~wxMenu(); - // operations - // title + // title void SetTitle(const wxString& label); const wxString GetTitle() const; - // menu creation + + // menu creation void AppendSeparator(); void Append(int id, const wxString &item, const wxString &helpStr = "", bool checkable = FALSE); @@ -141,6 +141,9 @@ public: void Append(wxMenuItem *pItem); void Break() { } + // delete item. don't delete the wxMenu if it's a submenu + void Delete( int id ); + // find item by name/id int FindItem( const wxString itemString ) const; wxMenuItem *FindItem( int id ) const; diff --git a/include/wx/gtk/menuitem.h b/include/wx/gtk/menuitem.h index 0575a81dab..500e235d68 100644 --- a/include/wx/gtk/menuitem.h +++ b/include/wx/gtk/menuitem.h @@ -40,6 +40,7 @@ DECLARE_DYNAMIC_CLASS(wxMenuItem) public: wxMenuItem(); + ~wxMenuItem(); // accessors // id diff --git a/include/wx/gtk1/menu.h b/include/wx/gtk1/menu.h index 8d6f68db7e..dfa0569718 100644 --- a/include/wx/gtk1/menu.h +++ b/include/wx/gtk1/menu.h @@ -128,11 +128,11 @@ public: ~wxMenu(); - // operations - // title + // title void SetTitle(const wxString& label); const wxString GetTitle() const; - // menu creation + + // menu creation void AppendSeparator(); void Append(int id, const wxString &item, const wxString &helpStr = "", bool checkable = FALSE); @@ -141,6 +141,9 @@ public: void Append(wxMenuItem *pItem); void Break() { } + // delete item. don't delete the wxMenu if it's a submenu + void Delete( int id ); + // find item by name/id int FindItem( const wxString itemString ) const; wxMenuItem *FindItem( int id ) const; diff --git a/include/wx/gtk1/menuitem.h b/include/wx/gtk1/menuitem.h index 0575a81dab..500e235d68 100644 --- a/include/wx/gtk1/menuitem.h +++ b/include/wx/gtk1/menuitem.h @@ -40,6 +40,7 @@ DECLARE_DYNAMIC_CLASS(wxMenuItem) public: wxMenuItem(); + ~wxMenuItem(); // accessors // id diff --git a/samples/dnd/dnd.cpp b/samples/dnd/dnd.cpp index c5159b9905..e78b54aa2b 100644 --- a/samples/dnd/dnd.cpp +++ b/samples/dnd/dnd.cpp @@ -133,7 +133,9 @@ enum Menu_CopyBitmap, Menu_PasteBitmap, Menu_HasText, - Menu_HasBitmap + Menu_HasBitmap, + Menu_ToBeGreyed, /* for testing */ + Menu_ToBeDeleted /* for testing */ }; BEGIN_EVENT_TABLE(DnDFrame, wxFrame) @@ -184,11 +186,19 @@ DnDFrame::DnDFrame(wxFrame *frame, char *title, int x, int y, int w, int h) CreateStatusBar(); + // construct sub menu for testing + wxMenu *sub_menu = new wxMenu; + sub_menu->Append(Menu_Quit, "E&xit"); + sub_menu->Append(Menu_Quit, "E&xit"); + sub_menu->Append(Menu_Quit, "E&xit"); + // construct menu wxMenu *file_menu = new wxMenu; file_menu->Append(Menu_Drag, "&Test drag..."); file_menu->AppendSeparator(); file_menu->Append(Menu_Quit, "E&xit"); + file_menu->AppendSeparator(); + file_menu->Append( 0, "More exit menus", sub_menu); wxMenu *log_menu = new wxMenu; log_menu->Append(Menu_Clear, "Clear"); @@ -417,6 +427,11 @@ void DnDFrame::OnRightDown(wxMouseEvent &event ) menu->Append(Menu_Drag, "&Test drag..."); menu->Append(Menu_About, "&About"); menu->Append(Menu_Quit, "E&xit"); + menu->Append(Menu_ToBeDeleted, "To be deleted"); + menu->Append(Menu_ToBeGreyed, "To be greyed"); + + menu->Delete( Menu_ToBeDeleted ); + menu->Enable( Menu_ToBeGreyed, FALSE ); PopupMenu( menu, event.GetX(), event.GetY() ); } diff --git a/src/gtk/menu.cpp b/src/gtk/menu.cpp index c56045a78d..8827eb6e8b 100644 --- a/src/gtk/menu.cpp +++ b/src/gtk/menu.cpp @@ -104,7 +104,7 @@ wxMenuBar::wxMenuBar() wxMenuBar::~wxMenuBar() { - // how to destroy a GtkItemFactory ? +// gtk_object_unref( GTK_OBJECT(m_factory) ); why not ? } static void wxMenubarUnsetInvokingWindow( wxMenu *menu, wxWindow *win ) @@ -584,6 +584,11 @@ wxMenuItem::wxMenuItem() m_menuItem = (GtkWidget *) NULL; } +wxMenuItem::~wxMenuItem() +{ + // don't delete menu items, the menus take care of that +} + // it's valid for this function to be called even if m_menuItem == NULL void wxMenuItem::SetName( const wxString& str ) { @@ -717,9 +722,19 @@ wxMenu::Init( const wxString& title, wxMenu::~wxMenu() { - /* how do we delete an item-factory ? */ - gtk_widget_destroy( m_menu ); + wxNode *node = m_items.First(); + while (node) + { + wxMenuItem *item = (wxMenuItem*)node->Data(); + wxMenu *submenu = item->GetSubMenu(); + if (submenu) + delete submenu; + node = node->Next(); + } + gtk_widget_destroy( m_menu ); + + gtk_object_unref( GTK_OBJECT(m_factory) ); } void wxMenu::SetTitle( const wxString& title ) @@ -964,6 +979,22 @@ void wxMenu::Append( wxMenuItem *item ) item->SetMenuItem(menuItem); } +void wxMenu::Delete( int id ) +{ + wxNode *node = m_items.First(); + while (node) + { + wxMenuItem *item = (wxMenuItem*)node->Data(); + if (item->GetId() == id) + { + gtk_widget_destroy( item->GetMenuItem() ); + m_items.DeleteNode( node ); + return; + } + node = node->Next(); + } +} + int wxMenu::FindItem( const wxString itemString ) const { wxString s = wxT(""); diff --git a/src/gtk1/menu.cpp b/src/gtk1/menu.cpp index c56045a78d..8827eb6e8b 100644 --- a/src/gtk1/menu.cpp +++ b/src/gtk1/menu.cpp @@ -104,7 +104,7 @@ wxMenuBar::wxMenuBar() wxMenuBar::~wxMenuBar() { - // how to destroy a GtkItemFactory ? +// gtk_object_unref( GTK_OBJECT(m_factory) ); why not ? } static void wxMenubarUnsetInvokingWindow( wxMenu *menu, wxWindow *win ) @@ -584,6 +584,11 @@ wxMenuItem::wxMenuItem() m_menuItem = (GtkWidget *) NULL; } +wxMenuItem::~wxMenuItem() +{ + // don't delete menu items, the menus take care of that +} + // it's valid for this function to be called even if m_menuItem == NULL void wxMenuItem::SetName( const wxString& str ) { @@ -717,9 +722,19 @@ wxMenu::Init( const wxString& title, wxMenu::~wxMenu() { - /* how do we delete an item-factory ? */ - gtk_widget_destroy( m_menu ); + wxNode *node = m_items.First(); + while (node) + { + wxMenuItem *item = (wxMenuItem*)node->Data(); + wxMenu *submenu = item->GetSubMenu(); + if (submenu) + delete submenu; + node = node->Next(); + } + gtk_widget_destroy( m_menu ); + + gtk_object_unref( GTK_OBJECT(m_factory) ); } void wxMenu::SetTitle( const wxString& title ) @@ -964,6 +979,22 @@ void wxMenu::Append( wxMenuItem *item ) item->SetMenuItem(menuItem); } +void wxMenu::Delete( int id ) +{ + wxNode *node = m_items.First(); + while (node) + { + wxMenuItem *item = (wxMenuItem*)node->Data(); + if (item->GetId() == id) + { + gtk_widget_destroy( item->GetMenuItem() ); + m_items.DeleteNode( node ); + return; + } + node = node->Next(); + } +} + int wxMenu::FindItem( const wxString itemString ) const { wxString s = wxT(""); -- 2.45.2