]> git.saurik.com Git - wxWidgets.git/commitdiff
Add support for toggle buttons to wxRibbonButtonBar.
authorPeter Cawley <corsix@corsix.org>
Sun, 7 Nov 2010 13:44:22 +0000 (13:44 +0000)
committerPeter Cawley <corsix@corsix.org>
Sun, 7 Nov 2010 13:44:22 +0000 (13:44 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@66055 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/ribbon/art.h
include/wx/ribbon/buttonbar.h
interface/wx/ribbon/art.h
interface/wx/ribbon/buttonbar.h
src/ribbon/art_aui.cpp
src/ribbon/art_msw.cpp
src/ribbon/buttonbar.cpp

index 9f1a74f8900ffb476917afcf2e8dd62fcbe8f37a..e629b3877835193e6b22f9c3e2837bdf42c4e948 100644 (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
index 7063326b67cb47f40f2ba31196ed235074db20ab..9d7da82f3f90507d48a87a9399d0fb48a4fc935e 100644 (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;
index c08e5b3cc4a6f73fcb655b5456a8424eda61d72f..23836be78d47401db1d54914c5280459d5253fe8 100644 (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
 };
 
 /**
index c209cf7146c392456e1d6648053077a3543ccaae..dd36d7c1b4e34a4f7293161b4c3d80ee4347b496 100644 (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.
 
index 2c7d56de4211f287223086b350a27cdc9d1c5bd9..99ae7001be165cd77ba07d560b45c022b5cde4c7 100644 (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))
     {
index af5673864a4d2dcb7f5cbf00d8913376851272cb..f10d562083f2167f83fcd9e10f3252919bc2e7f3 100644 (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;
             }
index 543e8385c9c4590c45125b43b5daf19beba7e868..a1404abbb8d2450e98966d27ca66be7b46ebaa49 100644 (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;