]> git.saurik.com Git - wxWidgets.git/blobdiff - src/ribbon/bar.cpp
No changes, just silence some MSVC 11 static analyzer warnings.
[wxWidgets.git] / src / ribbon / bar.cpp
index 1e2acc9e2de3d14c44abfa3c5b204f40b15e9075..0970d3b43b08b7ffcdd9a2e7852f3efd8e2aaa32 100644 (file)
@@ -40,6 +40,8 @@ wxDEFINE_EVENT(wxEVT_COMMAND_RIBBONBAR_TAB_MIDDLE_UP, wxRibbonBarEvent);
 wxDEFINE_EVENT(wxEVT_COMMAND_RIBBONBAR_TAB_RIGHT_DOWN, wxRibbonBarEvent);
 wxDEFINE_EVENT(wxEVT_COMMAND_RIBBONBAR_TAB_RIGHT_UP, wxRibbonBarEvent);
 wxDEFINE_EVENT(wxEVT_COMMAND_RIBBONBAR_TAB_LEFT_DCLICK, wxRibbonBarEvent);
+wxDEFINE_EVENT(wxEVT_COMMAND_RIBBONBAR_TOGGLED, wxRibbonBarEvent);
+wxDEFINE_EVENT(wxEVT_COMMAND_RIBBONBAR_HELP_CLICKED, wxRibbonBarEvent);
 
 IMPLEMENT_CLASS(wxRibbonBar, wxRibbonControl)
 IMPLEMENT_DYNAMIC_CLASS(wxRibbonBarEvent, wxNotifyEvent)
@@ -57,6 +59,7 @@ BEGIN_EVENT_TABLE(wxRibbonBar, wxRibbonControl)
   EVT_RIGHT_UP(wxRibbonBar::OnMouseRightUp)
   EVT_LEFT_DCLICK(wxRibbonBar::OnMouseDoubleClick)
   EVT_SIZE(wxRibbonBar::OnSize)
+  EVT_KILL_FOCUS(wxRibbonBar::OnKillFocus)
 END_EVENT_TABLE()
 
 void wxRibbonBar::AddPage(wxRibbonPage *page)
@@ -66,6 +69,7 @@ void wxRibbonBar::AddPage(wxRibbonPage *page)
     info.page = page;
     info.active = false;
     info.hovered = false;
+    info.highlight = false;
     info.shown = true;
     // info.rect not set (intentional)
 
@@ -243,6 +247,10 @@ void wxRibbonBar::OnMouseMove(wxMouseEvent& evt)
     {
         RefreshTabBar();
     }
+    if ( m_flags & wxRIBBON_BAR_SHOW_TOGGLE_BUTTON )
+        HitTestRibbonButton(m_toggle_button_rect, evt.GetPosition(), m_toggle_button_hovered);
+    if ( m_flags & wxRIBBON_BAR_SHOW_HELP_BUTTON )
+        HitTestRibbonButton(m_help_button_rect, evt.GetPosition(), m_help_button_hovered);
 }
 
 void wxRibbonBar::OnMouseLeave(wxMouseEvent& WXUNUSED(evt))
@@ -270,6 +278,18 @@ void wxRibbonBar::OnMouseLeave(wxMouseEvent& WXUNUSED(evt))
     {
         RefreshTabBar();
     }
+    if(m_toggle_button_hovered)
+    {
+        m_bar_hovered = false;
+        m_toggle_button_hovered = false;
+        Refresh(false);
+    }
+    if ( m_help_button_hovered )
+    {
+        m_help_button_hovered = false;
+        m_bar_hovered = false;
+        Refresh(false);
+    }
 }
 
 wxRibbonPage* wxRibbonBar::GetPage(int n)
@@ -298,6 +318,20 @@ void wxRibbonBar::ShowPage(size_t page, bool show)
     m_pages.Item(page).shown = show;
 }
 
+bool wxRibbonBar::IsPageHighlighted(size_t page) const
+{
+    if (page >= m_pages.GetCount())
+        return false;
+    return m_pages.Item(page).highlight;
+}
+
+void wxRibbonBar::AddPageHighlight(size_t page, bool highlight)
+{
+    if(page >= m_pages.GetCount())
+        return;
+    m_pages.Item(page).highlight = highlight;
+}
+
 void wxRibbonBar::DeletePage(size_t n)
 {
     if(n < m_pages.GetCount())
@@ -490,13 +524,17 @@ void wxRibbonBar::RecalculateTabSizes()
         }
         {
             wxClientDC temp_dc(this);
+            int right_button_pos = GetClientSize().GetWidth() - m_tab_margin_right - m_tab_scroll_right_button_rect.GetWidth();
+            if ( right_button_pos < m_tab_margin_left )
+                right_button_pos = m_tab_margin_left;
+
             m_tab_scroll_left_button_rect.SetWidth(m_art->GetScrollButtonMinimumSize(temp_dc, this, wxRIBBON_SCROLL_BTN_LEFT | wxRIBBON_SCROLL_BTN_NORMAL | wxRIBBON_SCROLL_BTN_FOR_TABS).GetWidth());
             m_tab_scroll_left_button_rect.SetHeight(m_tab_height);
             m_tab_scroll_left_button_rect.SetX(m_tab_margin_left);
             m_tab_scroll_left_button_rect.SetY(0);
             m_tab_scroll_right_button_rect.SetWidth(m_art->GetScrollButtonMinimumSize(temp_dc, this, wxRIBBON_SCROLL_BTN_RIGHT | wxRIBBON_SCROLL_BTN_NORMAL | wxRIBBON_SCROLL_BTN_FOR_TABS).GetWidth());
             m_tab_scroll_right_button_rect.SetHeight(m_tab_height);
-            m_tab_scroll_right_button_rect.SetX(GetClientSize().GetWidth() - m_tab_margin_right - m_tab_scroll_right_button_rect.GetWidth());
+            m_tab_scroll_right_button_rect.SetX(right_button_pos);
             m_tab_scroll_right_button_rect.SetY(0);
         }
         if(m_tab_scroll_amount == 0)
@@ -706,6 +744,10 @@ void wxRibbonBar::CommonInit(long style)
     m_tabs_total_width_minimum = 0;
     m_tab_margin_left = 50;
     m_tab_margin_right = 20;
+    if ( m_flags & wxRIBBON_BAR_SHOW_TOGGLE_BUTTON )
+        m_tab_margin_right += 20;
+    if ( m_flags & wxRIBBON_BAR_SHOW_HELP_BUTTON )
+        m_tab_margin_right += 20;
     m_tab_height = 20; // initial guess
     m_tab_scroll_amount = 0;
     m_current_page = -1;
@@ -720,6 +762,11 @@ void wxRibbonBar::CommonInit(long style)
         SetArtProvider(new wxRibbonDefaultArtProvider);
     }
     SetBackgroundStyle(wxBG_STYLE_CUSTOM);
+
+    m_toggle_button_hovered = false;
+    m_bar_hovered = false;
+
+    m_ribbon_state = wxRIBBON_BAR_PINNED;
 }
 
 void wxRibbonBar::SetArtProvider(wxRibbonArtProvider* art)
@@ -757,6 +804,11 @@ void wxRibbonBar::OnPaint(wxPaintEvent& WXUNUSED(evt))
 
     DoEraseBackground(dc);
 
+    if ( m_flags & wxRIBBON_BAR_SHOW_HELP_BUTTON  )
+        m_help_button_rect = m_art->GetRibbonHelpButtonArea(GetSize());
+    if ( m_flags & wxRIBBON_BAR_SHOW_TOGGLE_BUTTON  )
+        m_toggle_button_rect = m_art->GetBarToggleButtonArea(GetSize());
+
     size_t numtabs = m_pages.GetCount();
     double sep_visibility = 0.0;
     bool draw_sep = false;
@@ -820,16 +872,25 @@ void wxRibbonBar::OnPaint(wxPaintEvent& WXUNUSED(evt))
     }
     if(m_tab_scroll_buttons_shown)
     {
-        dc.DestroyClippingRegion();
         if(m_tab_scroll_left_button_rect.GetWidth() != 0)
         {
+            dc.DestroyClippingRegion();
+            dc.SetClippingRegion(m_tab_scroll_left_button_rect);
             m_art->DrawScrollButton(dc, this, m_tab_scroll_left_button_rect, wxRIBBON_SCROLL_BTN_LEFT | m_tab_scroll_left_button_state | wxRIBBON_SCROLL_BTN_FOR_TABS);
         }
         if(m_tab_scroll_right_button_rect.GetWidth() != 0)
         {
+            dc.DestroyClippingRegion();
+            dc.SetClippingRegion(m_tab_scroll_right_button_rect);
             m_art->DrawScrollButton(dc, this, m_tab_scroll_right_button_rect, wxRIBBON_SCROLL_BTN_RIGHT | m_tab_scroll_right_button_state | wxRIBBON_SCROLL_BTN_FOR_TABS);
         }
     }
+
+    if ( m_flags & wxRIBBON_BAR_SHOW_HELP_BUTTON  )
+        m_art->DrawHelpButton(dc, this, m_help_button_rect);
+    if ( m_flags & wxRIBBON_BAR_SHOW_TOGGLE_BUTTON  )
+        m_art->DrawToggleButton(dc, this, m_toggle_button_rect, m_ribbon_state);
+
 }
 
 void wxRibbonBar::OnEraseBackground(wxEraseEvent& WXUNUSED(evt))
@@ -900,6 +961,28 @@ wxRibbonPageTabInfo* wxRibbonBar::HitTestTabs(wxPoint position, int* index)
 void wxRibbonBar::OnMouseLeftDown(wxMouseEvent& evt)
 {
     wxRibbonPageTabInfo *tab = HitTestTabs(evt.GetPosition());
+    SetFocus();
+    if ( tab )
+    {
+        if ( m_ribbon_state == wxRIBBON_BAR_MINIMIZED )
+        {
+            ShowPanels();
+            m_ribbon_state = wxRIBBON_BAR_EXPANDED;
+        }
+        else if ( (tab == &m_pages.Item(m_current_page)) && (m_ribbon_state == wxRIBBON_BAR_EXPANDED) )
+        {
+            HidePanels();
+            m_ribbon_state = wxRIBBON_BAR_MINIMIZED;
+        }
+    }
+    else
+    {
+        if ( m_ribbon_state == wxRIBBON_BAR_EXPANDED )
+        {
+            HidePanels();
+            m_ribbon_state = wxRIBBON_BAR_MINIMIZED;
+        }
+    }
     if(tab && tab != &m_pages.Item(m_current_page))
     {
         wxRibbonBarEvent query(wxEVT_COMMAND_RIBBONBAR_PAGE_CHANGING, GetId(), tab->page);
@@ -927,6 +1010,34 @@ void wxRibbonBar::OnMouseLeftDown(wxMouseEvent& evt)
             RefreshTabBar();
         }
     }
+
+    wxPoint position = evt.GetPosition();
+
+    if(position.x >= 0 && position.y >= 0)
+    {
+        wxSize size = GetSize();
+        if(position.x < size.GetWidth() && position.y < size.GetHeight())
+        {
+            if(m_toggle_button_rect.Contains(position))
+            {
+                bool pshown = ArePanelsShown();
+                ShowPanels(!pshown);
+                if ( pshown )
+                    m_ribbon_state = wxRIBBON_BAR_MINIMIZED;
+                else
+                    m_ribbon_state = wxRIBBON_BAR_PINNED;
+                wxRibbonBarEvent event(wxEVT_COMMAND_RIBBONBAR_TOGGLED, GetId());
+                event.SetEventObject(this);
+                ProcessWindowEvent(event);
+            }
+            if ( m_help_button_rect.Contains(position) )
+            {
+                wxRibbonBarEvent event(wxEVT_COMMAND_RIBBONBAR_HELP_CLICKED, GetId());
+                event.SetEventObject(this);
+                ProcessWindowEvent(event);
+            }
+        }
+    }
 }
 
 void wxRibbonBar::OnMouseLeftUp(wxMouseEvent& WXUNUSED(evt))
@@ -1043,7 +1154,21 @@ void wxRibbonBar::OnMouseRightUp(wxMouseEvent& evt)
 
 void wxRibbonBar::OnMouseDoubleClick(wxMouseEvent& evt)
 {
-    DoMouseButtonCommon(evt, wxEVT_COMMAND_RIBBONBAR_TAB_LEFT_DCLICK);
+    wxRibbonPageTabInfo *tab = HitTestTabs(evt.GetPosition());
+    SetFocus();
+    if ( tab && tab == &m_pages.Item(m_current_page) )
+    {
+        if ( m_ribbon_state == wxRIBBON_BAR_PINNED )
+        {
+            m_ribbon_state = wxRIBBON_BAR_MINIMIZED;
+            HidePanels();
+        }
+        else
+        {
+            m_ribbon_state = wxRIBBON_BAR_PINNED;
+            ShowPanels();
+        }
+    }
 }
 
 void wxRibbonBar::DoMouseButtonCommon(wxMouseEvent& evt, wxEventType tab_event_type)
@@ -1110,4 +1235,47 @@ wxSize wxRibbonBar::DoGetBestSize() const
     return best;
 }
 
+void wxRibbonBar::HitTestRibbonButton(const wxRect& rect, const wxPoint& position, bool &hover_flag)
+{
+    bool hovered = false, toggle_button_hovered = false;
+    if(position.x >= 0 && position.y >= 0)
+    {
+        wxSize size = GetSize();
+        if(position.x < size.GetWidth() && position.y < size.GetHeight())
+        {
+            hovered = true;
+        }
+    }
+    if(hovered)
+    {
+        toggle_button_hovered = rect.Contains(position);
+
+        if ( hovered != m_bar_hovered || toggle_button_hovered != hover_flag )
+        {
+            m_bar_hovered = hovered;
+            hover_flag = toggle_button_hovered;
+            Refresh(false);
+        }
+    }
+}
+
+void wxRibbonBar::HideIfExpanded()
+{
+    if ( m_ribbon_state == wxRIBBON_BAR_EXPANDED )
+    {
+        HidePanels();
+        m_ribbon_state = wxRIBBON_BAR_MINIMIZED;
+    }
+    else
+    {
+        ShowPanels();
+        m_ribbon_state = wxRIBBON_BAR_PINNED;
+    }
+}
+
+void wxRibbonBar::OnKillFocus(wxFocusEvent& WXUNUSED(evt))
+{
+    HideIfExpanded();
+}
+
 #endif // wxUSE_RIBBON