From 3ec0697c04bf023c12716a770bd0ff74de305f70 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 20 Mar 2022 02:38:35 +0100 Subject: [PATCH] Don't crash in toolbar sample if there is no toolbar Using many menu commands after removing the toolbar could result in a crash because they used the toolbar pointer without checking for its validity. Add wxEVT_UPDATE_UI handler disabling these commands to prevent this from happening. This required rearranging the menu item IDs to allow using a single range for all of them. --- samples/toolbar/toolbar.cpp | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/samples/toolbar/toolbar.cpp b/samples/toolbar/toolbar.cpp index 357d39b082..0aebe56c2a 100644 --- a/samples/toolbar/toolbar.cpp +++ b/samples/toolbar/toolbar.cpp @@ -151,6 +151,8 @@ public: void OnUpdateCopyAndCut(wxUpdateUIEvent& event); void OnUpdateToggleHorzText(wxUpdateUIEvent& event); + void OnUpdateNeedsToolbar(wxUpdateUIEvent& event) + { event.Enable( GetToolBar() != NULL ); } void OnUpdateToggleRadioBtn(wxUpdateUIEvent& event) { event.Enable( m_tbar != NULL ); } @@ -206,13 +208,8 @@ enum IDM_TOOLBAR_TOGGLE_HORIZONTAL_TEXT, IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR, IDM_TOOLBAR_TOGGLETOOLBARSIZE, - IDM_TOOLBAR_TOGGLETOOLBARROWS, IDM_TOOLBAR_TOGGLETOOLTIPS, IDM_TOOLBAR_TOGGLECUSTOMDISABLED, - IDM_TOOLBAR_SHOW_TEXT, - IDM_TOOLBAR_SHOW_ICONS, - IDM_TOOLBAR_SHOW_BOTH, - IDM_TOOLBAR_BG_COL, IDM_TOOLBAR_CUSTOM_PATH, IDM_TOOLBAR_TOP_ORIENTATION, IDM_TOOLBAR_LEFT_ORIENTATION, @@ -223,19 +220,31 @@ enum IDM_TOOLBAR_OTHER_3, IDM_TOOLBAR_OTHER_4, + // items starting from this one must be disabled when there is no toolbar + IDM_FIRST_NEEDS_TOOLBAR, + + IDM_TOOLBAR_TOGGLETOOLBARROWS, + IDM_TOOLBAR_SHOW_TEXT, + IDM_TOOLBAR_SHOW_ICONS, + IDM_TOOLBAR_SHOW_BOTH, + IDM_TOOLBAR_BG_COL, + // tools menu items IDM_TOOLBAR_ENABLEPRINT, IDM_TOOLBAR_DELETEPRINT, IDM_TOOLBAR_INSERTPRINT, IDM_TOOLBAR_TOGGLEHELP, IDM_TOOLBAR_TOGGLESEARCH, - IDM_TOOLBAR_TOGGLERADIOBTN1, - IDM_TOOLBAR_TOGGLERADIOBTN2, - IDM_TOOLBAR_TOGGLERADIOBTN3, IDM_TOOLBAR_CHANGE_TOOLTIP, IDM_TOOLBAR_INC_TOOL_SPACING, IDM_TOOLBAR_DEC_TOOL_SPACING, + IDM_LAST_NEEDS_TOOLBAR, + + IDM_TOOLBAR_TOGGLERADIOBTN1, + IDM_TOOLBAR_TOGGLERADIOBTN2, + IDM_TOOLBAR_TOGGLERADIOBTN3, + ID_COMBO = 1000 }; @@ -290,6 +299,9 @@ wxBEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_UPDATE_UI(wxID_COPY, MyFrame::OnUpdateCopyAndCut) EVT_UPDATE_UI(wxID_CUT, MyFrame::OnUpdateCopyAndCut) + EVT_UPDATE_UI_RANGE(IDM_FIRST_NEEDS_TOOLBAR, + IDM_LAST_NEEDS_TOOLBAR, + MyFrame::OnUpdateNeedsToolbar) EVT_UPDATE_UI_RANGE(IDM_TOOLBAR_TOGGLERADIOBTN1, IDM_TOOLBAR_TOGGLERADIOBTN3, MyFrame::OnUpdateToggleRadioBtn)