From 955bad410e165e32aa3f8b438d3c4cace59774e9 Mon Sep 17 00:00:00 2001 From: Peter Cawley Date: Sun, 7 Nov 2010 13:44:22 +0000 Subject: [PATCH 1/1] Add support for toggle buttons to wxRibbonButtonBar. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@66055 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/ribbon/art.h | 4 ++- include/wx/ribbon/buttonbar.h | 7 +++++ interface/wx/ribbon/art.h | 6 +++++ interface/wx/ribbon/buttonbar.h | 33 +++++++++++++++++++++-- src/ribbon/art_aui.cpp | 7 +++++ src/ribbon/art_msw.cpp | 12 ++++++++- src/ribbon/buttonbar.cpp | 47 +++++++++++++++++++++++++++++++++ 7 files changed, 112 insertions(+), 4 deletions(-) diff --git a/include/wx/ribbon/art.h b/include/wx/ribbon/art.h index 9f1a74f890..e629b38778 100644 --- a/include/wx/ribbon/art.h +++ b/include/wx/ribbon/art.h @@ -154,6 +154,7 @@ enum wxRibbonButtonKind wxRIBBON_BUTTON_NORMAL = 1 << 0, wxRIBBON_BUTTON_DROPDOWN = 1 << 1, wxRIBBON_BUTTON_HYBRID = wxRIBBON_BUTTON_NORMAL | wxRIBBON_BUTTON_DROPDOWN, + wxRIBBON_BUTTON_TOGGLE = 1 << 2 }; enum wxRibbonButtonBarButtonState @@ -170,7 +171,8 @@ enum wxRibbonButtonBarButtonState wxRIBBON_BUTTONBAR_BUTTON_DROPDOWN_ACTIVE = 1 << 6, wxRIBBON_BUTTONBAR_BUTTON_ACTIVE_MASK = wxRIBBON_BUTTONBAR_BUTTON_NORMAL_ACTIVE | wxRIBBON_BUTTONBAR_BUTTON_DROPDOWN_ACTIVE, wxRIBBON_BUTTONBAR_BUTTON_DISABLED = 1 << 7, - wxRIBBON_BUTTONBAR_BUTTON_STATE_MASK = 0xF8, + wxRIBBON_BUTTONBAR_BUTTON_TOGGLED = 1 << 8, + wxRIBBON_BUTTONBAR_BUTTON_STATE_MASK = 0x1F8, }; enum wxRibbonGalleryButtonState diff --git a/include/wx/ribbon/buttonbar.h b/include/wx/ribbon/buttonbar.h index 7063326b67..9d7da82f3f 100644 --- a/include/wx/ribbon/buttonbar.h +++ b/include/wx/ribbon/buttonbar.h @@ -67,6 +67,12 @@ public: const wxBitmap& bitmap, const wxString& help_string = wxEmptyString); + virtual wxRibbonButtonBarButtonBase* AddToggleButton( + int button_id, + const wxString& label, + const wxBitmap& bitmap, + const wxString& help_string = wxEmptyString); + virtual wxRibbonButtonBarButtonBase* AddButton( int button_id, const wxString& label, @@ -82,6 +88,7 @@ public: virtual void ClearButtons(); virtual bool DeleteButton(int button_id); virtual void EnableButton(int button_id, bool enable = true); + virtual void ToggleButton(int button_id, bool checked); virtual void SetArtProvider(wxRibbonArtProvider* art); virtual bool IsSizingContinuous() const; diff --git a/interface/wx/ribbon/art.h b/interface/wx/ribbon/art.h index c08e5b3cc4..23836be78d 100644 --- a/interface/wx/ribbon/art.h +++ b/interface/wx/ribbon/art.h @@ -174,6 +174,12 @@ enum wxRibbonButtonKind menu, and one which causes a generic action. */ wxRIBBON_BUTTON_HYBRID = wxRIBBON_BUTTON_NORMAL | wxRIBBON_BUTTON_DROPDOWN, + + /** + Normal button or tool with a clickable area which toggles the button + between a pressed and unpressed state. + */ + wxRIBBON_BUTTON_TOGGLE = 1 << 2 }; /** diff --git a/interface/wx/ribbon/buttonbar.h b/interface/wx/ribbon/buttonbar.h index c209cf7146..dd36d7c1b4 100644 --- a/interface/wx/ribbon/buttonbar.h +++ b/interface/wx/ribbon/buttonbar.h @@ -78,10 +78,15 @@ enum wxRibbonButtonBarButtonState */ wxRIBBON_BUTTONBAR_BUTTON_DISABLED = 1 << 7, + /** + The button is a toggle button which is currently in the toggled state. + */ + wxRIBBON_BUTTONBAR_BUTTON_TOGGLED = 1 << 8, + /** A mask to extract button state from a combination of flags. */ - wxRIBBON_BUTTONBAR_BUTTON_STATE_MASK = 0xF8, + wxRIBBON_BUTTONBAR_BUTTON_STATE_MASK = 0x1F8, }; /** @@ -190,6 +195,17 @@ public: const wxBitmap& bitmap, const wxString& help_string = wxEmptyString); + /** + Add a toggle button to the button bar (simple version). + + @see AddButton() + */ + virtual wxRibbonButtonBarButtonBase* AddToggleButton( + int button_id, + const wxString& label, + const wxBitmap& bitmap, + const wxString& help_string = wxEmptyString); + /** Add a button to the button bar. @@ -223,6 +239,7 @@ public: @see AddDropdownButton() @see AddHybridButton() + @see AddToggleButton() */ virtual wxRibbonButtonBarButtonBase* AddButton( int button_id, @@ -267,13 +284,25 @@ public: @true to enable the button, @false to disable it. */ virtual void EnableButton(int button_id, bool enable = true); + + /** + Set a toggle button to the checked or unchecked state. + + @param button_id + ID of the toggle button to manipulate. + @param checked + @true to set the button to the toggled/pressed/checked state, + @false to set it to the untoggled/unpressed/unchecked state. + */ + virtual void ToggleButton(int button_id, bool checked); }; /** @class wxRibbonButtonBarEvent Event used to indicate various actions relating to a button on a - wxRibbonButtonBar. + wxRibbonButtonBar. For toggle buttons, IsChecked() can be used to test + the state of the button. See wxRibbonButtonBar for available event types. diff --git a/src/ribbon/art_aui.cpp b/src/ribbon/art_aui.cpp index 2c7d56de42..99ae7001be 100644 --- a/src/ribbon/art_aui.cpp +++ b/src/ribbon/art_aui.cpp @@ -959,6 +959,13 @@ void wxRibbonAUIArtProvider::DrawButtonBarButton( const wxBitmap& bitmap_large, const wxBitmap& bitmap_small) { + if(kind == wxRIBBON_BUTTON_TOGGLE) + { + kind = wxRIBBON_BUTTON_NORMAL; + if(state & wxRIBBON_BUTTONBAR_BUTTON_TOGGLED) + state ^= wxRIBBON_BUTTONBAR_BUTTON_ACTIVE_MASK; + } + if(state & (wxRIBBON_BUTTONBAR_BUTTON_HOVER_MASK | wxRIBBON_BUTTONBAR_BUTTON_ACTIVE_MASK)) { diff --git a/src/ribbon/art_msw.cpp b/src/ribbon/art_msw.cpp index af5673864a..f10d562083 100644 --- a/src/ribbon/art_msw.cpp +++ b/src/ribbon/art_msw.cpp @@ -2021,6 +2021,13 @@ void wxRibbonMSWArtProvider::DrawButtonBarButton( const wxBitmap& bitmap_large, const wxBitmap& bitmap_small) { + if(kind == wxRIBBON_BUTTON_TOGGLE) + { + kind = wxRIBBON_BUTTON_NORMAL; + if(state & wxRIBBON_BUTTONBAR_BUTTON_TOGGLED) + state ^= wxRIBBON_BUTTONBAR_BUTTON_ACTIVE_MASK; + } + if(state & (wxRIBBON_BUTTONBAR_BUTTON_HOVER_MASK | wxRIBBON_BUTTONBAR_BUTTON_ACTIVE_MASK)) { @@ -2627,6 +2634,7 @@ bool wxRibbonMSWArtProvider::GetButtonBarButtonSize( switch(kind) { case wxRIBBON_BUTTON_NORMAL: + case wxRIBBON_BUTTON_TOGGLE: *normal_region = wxRect(*button_size); *dropdown_region = wxRect(0, 0, 0, 0); break; @@ -2660,6 +2668,7 @@ bool wxRibbonMSWArtProvider::GetButtonBarButtonSize( dropdown_region->SetX(dropdown_region->GetX() + text_size); // no break case wxRIBBON_BUTTON_NORMAL: + case wxRIBBON_BUTTON_TOGGLE: normal_region->SetWidth(normal_region->GetWidth() + text_size); break; } @@ -2674,7 +2683,7 @@ bool wxRibbonMSWArtProvider::GetButtonBarButtonSize( wxCoord best_width; dc.GetTextExtent(label, &best_width, &label_height); int last_line_extra_width = 0; - if(kind != wxRIBBON_BUTTON_NORMAL) + if(kind != wxRIBBON_BUTTON_NORMAL && kind != wxRIBBON_BUTTON_TOGGLE) { last_line_extra_width += 8; } @@ -2711,6 +2720,7 @@ bool wxRibbonMSWArtProvider::GetButtonBarButtonSize( dropdown_region->height = icon_size.GetHeight() - normal_region->height; break; case wxRIBBON_BUTTON_NORMAL: + case wxRIBBON_BUTTON_TOGGLE: *normal_region = wxRect(icon_size); break; } diff --git a/src/ribbon/buttonbar.cpp b/src/ribbon/buttonbar.cpp index 543e8385c9..a1404abbb8 100644 --- a/src/ribbon/buttonbar.cpp +++ b/src/ribbon/buttonbar.cpp @@ -249,6 +249,16 @@ wxRibbonButtonBarButtonBase* wxRibbonButtonBar::AddDropdownButton( wxRIBBON_BUTTON_DROPDOWN); } +wxRibbonButtonBarButtonBase* wxRibbonButtonBar::AddToggleButton( + int button_id, + const wxString& label, + const wxBitmap& bitmap, + const wxString& help_string) +{ + return AddButton(button_id, label, bitmap, help_string, + wxRIBBON_BUTTON_TOGGLE); +} + wxRibbonButtonBarButtonBase* wxRibbonButtonBar::AddHybridButton( int button_id, const wxString& label, @@ -445,6 +455,36 @@ void wxRibbonButtonBar::EnableButton(int button_id, bool enable) } } +void wxRibbonButtonBar::ToggleButton(int button_id, bool checked) +{ + size_t count = m_buttons.GetCount(); + size_t i; + for(i = 0; i < count; ++i) + { + wxRibbonButtonBarButtonBase* button = m_buttons.Item(i); + if(button->id == button_id) + { + if(checked) + { + if((button->state & wxRIBBON_BUTTONBAR_BUTTON_TOGGLED) == 0) + { + button->state |= wxRIBBON_BUTTONBAR_BUTTON_TOGGLED; + Refresh(); + } + } + else + { + if(button->state & wxRIBBON_BUTTONBAR_BUTTON_TOGGLED) + { + button->state &= ~wxRIBBON_BUTTONBAR_BUTTON_TOGGLED; + Refresh(); + } + } + return; + } + } +} + void wxRibbonButtonBar::SetArtProvider(wxRibbonArtProvider* art) { if(art == m_art) @@ -936,6 +976,13 @@ void wxRibbonButtonBar::OnMouseUp(wxMouseEvent& evt) else break; wxRibbonButtonBarEvent notification(event_type, id); + if(m_active_button->base->kind == wxRIBBON_BUTTON_TOGGLE) + { + m_active_button->base->state ^= + wxRIBBON_BUTTONBAR_BUTTON_TOGGLED; + notification.SetInt(m_active_button->base->state & + wxRIBBON_BUTTONBAR_BUTTON_TOGGLED); + } notification.SetEventObject(this); notification.SetBar(this); m_lock_active_state = true; -- 2.45.2