From 90d56e52a36f11a31e79c5539dedfa9483226683 Mon Sep 17 00:00:00 2001 From: ali kettab Date: Thu, 11 Jan 2024 22:27:22 +0100 Subject: [PATCH] Fix creating wxMenuItem with nullptr parent under wxQt This is documented and is already the case for the other ports. --- include/wx/qt/menuitem.h | 7 ++-- src/qt/menu.cpp | 4 +++ src/qt/menuitem.cpp | 70 ++++++++++++++++++++++++++-------------- 3 files changed, 53 insertions(+), 28 deletions(-) diff --git a/include/wx/qt/menuitem.h b/include/wx/qt/menuitem.h index d39e860d33..673eb93bf5 100644 --- a/include/wx/qt/menuitem.h +++ b/include/wx/qt/menuitem.h @@ -34,8 +34,6 @@ public: virtual void Check(bool check = true) override; virtual bool IsChecked() const override; - virtual void SetBitmap(const wxBitmapBundle& bitmap) override; - virtual QAction *GetHandle() const; virtual void SetFont(const wxFont& font); @@ -45,9 +43,12 @@ public: virtual void ClearExtraAccels() override; #endif // wxUSE_ACCEL + // implementation + void QtCreateAction(wxMenu* parentMenu); + private: // Qt is using an action instead of a menu item. - QAction *m_qtAction; + QAction *m_qtAction = nullptr; wxDECLARE_DYNAMIC_CLASS( wxMenuItem ); }; diff --git a/src/qt/menu.cpp b/src/qt/menu.cpp index 8a3791fbbd..f5d646f2e8 100644 --- a/src/qt/menu.cpp +++ b/src/qt/menu.cpp @@ -176,6 +176,8 @@ wxMenuItem *wxMenu::DoAppend(wxMenuItem *item) if ( wxMenuBase::DoAppend( item ) == nullptr ) return nullptr; + item->QtCreateAction( this ); + InsertMenuItemAction( this, previousItem, item, successiveItem ); return item; @@ -193,6 +195,8 @@ wxMenuItem *wxMenu::DoInsert(size_t insertPosition, wxMenuItem *item) if ( wxMenuBase::DoInsert( insertPosition, item ) == nullptr ) return nullptr; + item->QtCreateAction( this ); + InsertMenuItemAction( this, previousItem, item, successiveItem ); return item; diff --git a/src/qt/menuitem.cpp b/src/qt/menuitem.cpp index e6f8cd44bf..4860623358 100644 --- a/src/qt/menuitem.cpp +++ b/src/qt/menuitem.cpp @@ -49,7 +49,7 @@ private: void onActionTriggered( bool checked ) { - m_handler->SendEvent(m_mitemId, m_isCheckable ? checked : -1 ); + m_handler->SendEvent(m_mitemId, m_isCheckable ? checked : -1); } wxMenu* m_handler; @@ -70,11 +70,27 @@ wxMenuItem::wxMenuItem(wxMenu *parentMenu, int id, const wxString& text, const wxString& help, wxItemKind kind, wxMenu *subMenu) : wxMenuItemBase( parentMenu, id, text, help, kind, subMenu ) { - m_qtAction = new wxQtAction( parentMenu, id, - wxQtAction::Normalize( text ), - help, kind, subMenu, this ); } +void wxMenuItem::QtCreateAction(wxMenu* parentMenu) +{ + wxASSERT_MSG( parentMenu, "invalid parent" ); + + m_qtAction = new wxQtAction( parentMenu, GetId(), + wxQtAction::Normalize( GetItemLabel() ), + GetHelp(), GetKind(), GetSubMenu(), this ); + + if ( m_bitmap.IsOk() && m_kind == wxITEM_NORMAL ) + { + m_qtAction->setIcon( QIcon(*GetBitmapFromBundle(m_bitmap).GetHandle()) ); + } + +#if wxUSE_ACCEL + auto qtAction = static_cast(m_qtAction); + for ( const auto accel : m_extraAccels ) + qtAction->UpdateShortcuts( accel.ToRawString() ); +#endif +} void wxMenuItem::SetItemLabel( const wxString &label ) @@ -83,16 +99,21 @@ void wxMenuItem::SetItemLabel( const wxString &label ) wxMenuItemBase::SetItemLabel( qtlabel ); - auto qtAction = static_cast(m_qtAction); + if ( m_qtAction ) + { + auto qtAction = static_cast(m_qtAction); - qtAction->UpdateShortcutsFromLabel( qtlabel ); - qtAction->setText( wxQtConvertString( qtlabel )); + qtAction->UpdateShortcutsFromLabel( qtlabel ); + qtAction->setText( wxQtConvertString( qtlabel )); + } } void wxMenuItem::SetCheckable( bool checkable ) { + wxCHECK_RET( m_qtAction, "invalid menu item" ); + wxMenuItemBase::SetCheckable( checkable ); m_qtAction->setCheckable( checkable ); @@ -102,6 +123,8 @@ void wxMenuItem::SetCheckable( bool checkable ) void wxMenuItem::Enable( bool enable ) { + wxCHECK_RET( m_qtAction, "invalid menu item" ); + wxMenuItemBase::Enable( enable ); m_qtAction->setEnabled( enable ); @@ -111,6 +134,8 @@ void wxMenuItem::Enable( bool enable ) bool wxMenuItem::IsEnabled() const { + wxCHECK_MSG( m_qtAction, false, "invalid menu item" ); + bool isEnabled = m_qtAction->isEnabled(); // Make sure the enabled stati are in synch: @@ -123,6 +148,8 @@ bool wxMenuItem::IsEnabled() const void wxMenuItem::Check( bool checked ) { + wxCHECK_RET( m_qtAction, "invalid menu item" ); + wxMenuItemBase::Check( checked ); m_qtAction->setChecked( checked ); @@ -132,6 +159,11 @@ void wxMenuItem::Check( bool checked ) bool wxMenuItem::IsChecked() const { + wxCHECK_MSG( m_qtAction, false, "invalid menu item" ); + + wxCHECK_MSG( IsCheckable(), false, + "can't get state of uncheckable item!" ); + bool isChecked = m_qtAction->isChecked(); // Make sure the checked stati are in synch: @@ -141,24 +173,10 @@ bool wxMenuItem::IsChecked() const } -void wxMenuItem::SetBitmap(const wxBitmapBundle& bitmap) -{ - if ( m_kind == wxITEM_NORMAL ) - { - m_bitmap = bitmap; - if ( m_bitmap.IsOk() ) - { - m_qtAction->setIcon( QIcon(*GetBitmapFromBundle(m_bitmap).GetHandle()) ); - } - } - else - { - wxFAIL_MSG("only normal menu items can have bitmaps"); - } -} - void wxMenuItem::SetFont(const wxFont& font) { + wxCHECK_RET( m_qtAction, "invalid menu item" ); + m_qtAction->setFont(font.GetHandle()); } @@ -172,14 +190,16 @@ void wxMenuItem::AddExtraAccel(const wxAcceleratorEntry& accel) { wxMenuItemBase::AddExtraAccel(accel); - static_cast(m_qtAction)->UpdateShortcuts( accel.ToRawString() ); + if ( m_qtAction ) + static_cast(m_qtAction)->UpdateShortcuts( accel.ToRawString() ); } void wxMenuItem::ClearExtraAccels() { wxMenuItemBase::ClearExtraAccels(); - static_cast(m_qtAction)->UpdateShortcuts( wxString() ); + if ( m_qtAction ) + static_cast(m_qtAction)->UpdateShortcuts( wxString() ); } #endif // wxUSE_ACCEL