Fix creating wxMenuItem with nullptr parent under wxQt
This is documented and is already the case for the other ports.
This commit is contained in:
parent
03ad7039d6
commit
90d56e52a3
3 changed files with 53 additions and 28 deletions
|
|
@ -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 );
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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<wxQtAction *>(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<wxQtAction *>(m_qtAction);
|
||||
if ( m_qtAction )
|
||||
{
|
||||
auto qtAction = static_cast<wxQtAction *>(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<wxQtAction *>(m_qtAction)->UpdateShortcuts( accel.ToRawString() );
|
||||
if ( m_qtAction )
|
||||
static_cast<wxQtAction *>(m_qtAction)->UpdateShortcuts( accel.ToRawString() );
|
||||
}
|
||||
|
||||
void wxMenuItem::ClearExtraAccels()
|
||||
{
|
||||
wxMenuItemBase::ClearExtraAccels();
|
||||
|
||||
static_cast<wxQtAction *>(m_qtAction)->UpdateShortcuts( wxString() );
|
||||
if ( m_qtAction )
|
||||
static_cast<wxQtAction *>(m_qtAction)->UpdateShortcuts( wxString() );
|
||||
}
|
||||
#endif // wxUSE_ACCEL
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue