Add support for toggle buttons to wxRibbonButtonBar.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@66055 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Peter Cawley
2010-11-07 13:44:22 +00:00
parent 258593354e
commit 955bad410e
7 changed files with 112 additions and 4 deletions

View File

@@ -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

View File

@@ -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;

View File

@@ -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
};
/**

View File

@@ -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.

View File

@@ -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))
{

View File

@@ -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;
}

View File

@@ -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;