From be9c18cbd6b72242f56d2c90ae485367f0ada9bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C4=83t=C4=83lin=20R=C4=83ceanu?= Date: Wed, 13 Feb 2019 01:23:20 +0200 Subject: [PATCH] Reuse Qt implementation of normal button for toggle buttons --- include/wx/qt/anybutton.h | 1 + include/wx/qt/button.h | 3 +++ include/wx/qt/tglbtn.h | 2 ++ src/qt/anybutton.cpp | 8 +++++-- src/qt/tglbtn.cpp | 50 +++++++++++++-------------------------- 5 files changed, 29 insertions(+), 35 deletions(-) diff --git a/include/wx/qt/anybutton.h b/include/wx/qt/anybutton.h index 57ca06bead..eb3951aca7 100644 --- a/include/wx/qt/anybutton.h +++ b/include/wx/qt/anybutton.h @@ -29,6 +29,7 @@ public: // implementation only void QtUpdateState(); + virtual int GetEventType() const = 0; protected: virtual wxBitmap DoGetBitmap(State state) const wxOVERRIDE; diff --git a/include/wx/qt/button.h b/include/wx/qt/button.h index 0bbce110a4..d1b11c1f51 100644 --- a/include/wx/qt/button.h +++ b/include/wx/qt/button.h @@ -31,6 +31,9 @@ public: virtual wxWindow *SetDefault(); + // implementation only + virtual int GetEventType() const wxOVERRIDE { return wxEVT_BUTTON; } + private: wxDECLARE_DYNAMIC_CLASS_NO_COPY(wxButton); }; diff --git a/include/wx/qt/tglbtn.h b/include/wx/qt/tglbtn.h index a2c9373974..f2b361e579 100644 --- a/include/wx/qt/tglbtn.h +++ b/include/wx/qt/tglbtn.h @@ -35,6 +35,8 @@ public: virtual void SetValue(bool state); virtual bool GetValue() const; + // implementation only + virtual int GetEventType() const wxOVERRIDE { return wxEVT_TOGGLEBUTTON; } private: wxDECLARE_DYNAMIC_CLASS(wxToggleButton); diff --git a/src/qt/anybutton.cpp b/src/qt/anybutton.cpp index b0392263c4..acb5f4c6fd 100644 --- a/src/qt/anybutton.cpp +++ b/src/qt/anybutton.cpp @@ -40,12 +40,16 @@ wxQtPushButton::wxQtPushButton(wxWindow *parent, wxAnyButton *handler) connect(this, &QPushButton::released, this, &wxQtPushButton::action); } -void wxQtPushButton::clicked( bool WXUNUSED(checked) ) +void wxQtPushButton::clicked(bool checked) { wxAnyButton *handler = GetHandler(); if ( handler ) { - wxCommandEvent event( wxEVT_BUTTON, handler->GetId() ); + wxCommandEvent event( handler->GetEventType(), handler->GetId() ); + if ( isCheckable() ) // toggle buttons + { + event.SetInt(checked); + } EmitEvent( event ); } } diff --git a/src/qt/tglbtn.cpp b/src/qt/tglbtn.cpp index a03d5a7cfa..5244627091 100644 --- a/src/qt/tglbtn.cpp +++ b/src/qt/tglbtn.cpp @@ -23,35 +23,6 @@ #include -class wxQtToggleButton : public wxQtEventSignalHandler< QPushButton, wxAnyButton > -{ - -public: - wxQtToggleButton( wxWindow *parent, wxAnyButton *handler); - -private: - void clicked( bool checked ); -}; - -wxQtToggleButton::wxQtToggleButton(wxWindow *parent, wxAnyButton *handler) - : wxQtEventSignalHandler< QPushButton, wxAnyButton >( parent, handler ) -{ - setCheckable( true ); - connect(this, &QPushButton::clicked, this, &wxQtToggleButton::clicked); -} - -void wxQtToggleButton::clicked( bool checked ) -{ - wxAnyButton *handler = GetHandler(); - if ( handler ) - { - // for toggle buttons, send the checked state in the wx event: - wxCommandEvent event( wxEVT_TOGGLEBUTTON, handler->GetId() ); - event.SetInt( checked ); - EmitEvent( event ); - } -} - wxDEFINE_EVENT( wxEVT_TOGGLEBUTTON, wxCommandEvent ); wxIMPLEMENT_DYNAMIC_CLASS(wxBitmapToggleButton, wxToggleButton); @@ -80,10 +51,22 @@ bool wxBitmapToggleButton::Create(wxWindow *parent, const wxValidator& validator, const wxString& name) { - // this button is toggleable and has a bitmap label: - QtSetBitmap( label ); + if ( !wxToggleButton::Create(parent, id, wxString(), pos, size, style, validator, name) ) + { + return false; + } - return QtCreateControl( parent, id, pos, size, style, validator, name ); + // this button is toggleable and has a bitmap label: + if ( label.IsOk() ) + { + SetBitmapLabel(label); + + // we need to adjust the size after setting the bitmap as it may be too + // big for the default button size + SetInitialSize(size); + } + + return true; } //############################################################################## @@ -115,7 +98,8 @@ bool wxToggleButton::Create(wxWindow *parent, const wxString& name) { // create a checkable push button - m_qtPushButton = new wxQtToggleButton( parent, this ); + QtCreate(parent); + m_qtPushButton->setCheckable(true); // this button is toggleable and has a text label SetLabel( wxIsStockID( id ) ? wxGetStockLabel( id ) : label );