1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/ribbon/bar.cpp
3 // Purpose: Top-level component of the ribbon-bar-style interface
4 // Author: Peter Cawley
8 // Copyright: (C) Peter Cawley
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
20 #include "wx/ribbon/bar.h"
21 #include "wx/ribbon/art.h"
22 #include "wx/dcbuffer.h"
29 #include "wx/msw/private.h"
32 #include "wx/arrimpl.cpp"
34 WX_DEFINE_USER_EXPORTED_OBJARRAY(wxRibbonPageTabInfoArray
)
36 wxDEFINE_EVENT(wxEVT_COMMAND_RIBBONBAR_PAGE_CHANGED
, wxRibbonBarEvent
);
37 wxDEFINE_EVENT(wxEVT_COMMAND_RIBBONBAR_PAGE_CHANGING
, wxRibbonBarEvent
);
38 wxDEFINE_EVENT(wxEVT_COMMAND_RIBBONBAR_TAB_MIDDLE_DOWN
, wxRibbonBarEvent
);
39 wxDEFINE_EVENT(wxEVT_COMMAND_RIBBONBAR_TAB_MIDDLE_UP
, wxRibbonBarEvent
);
40 wxDEFINE_EVENT(wxEVT_COMMAND_RIBBONBAR_TAB_RIGHT_DOWN
, wxRibbonBarEvent
);
41 wxDEFINE_EVENT(wxEVT_COMMAND_RIBBONBAR_TAB_RIGHT_UP
, wxRibbonBarEvent
);
42 wxDEFINE_EVENT(wxEVT_COMMAND_RIBBONBAR_TAB_LEFT_DCLICK
, wxRibbonBarEvent
);
43 wxDEFINE_EVENT(wxEVT_COMMAND_RIBBONBAR_TOGGLED
, wxRibbonBarEvent
);
45 IMPLEMENT_CLASS(wxRibbonBar
, wxRibbonControl
)
46 IMPLEMENT_DYNAMIC_CLASS(wxRibbonBarEvent
, wxNotifyEvent
)
48 BEGIN_EVENT_TABLE(wxRibbonBar
, wxRibbonControl
)
49 EVT_ERASE_BACKGROUND(wxRibbonBar::OnEraseBackground
)
50 EVT_LEAVE_WINDOW(wxRibbonBar::OnMouseLeave
)
51 EVT_LEFT_DOWN(wxRibbonBar::OnMouseLeftDown
)
52 EVT_LEFT_UP(wxRibbonBar::OnMouseLeftUp
)
53 EVT_MIDDLE_DOWN(wxRibbonBar::OnMouseMiddleDown
)
54 EVT_MIDDLE_UP(wxRibbonBar::OnMouseMiddleUp
)
55 EVT_MOTION(wxRibbonBar::OnMouseMove
)
56 EVT_PAINT(wxRibbonBar::OnPaint
)
57 EVT_RIGHT_DOWN(wxRibbonBar::OnMouseRightDown
)
58 EVT_RIGHT_UP(wxRibbonBar::OnMouseRightUp
)
59 EVT_LEFT_DCLICK(wxRibbonBar::OnMouseDoubleClick
)
60 EVT_SIZE(wxRibbonBar::OnSize
)
63 void wxRibbonBar::AddPage(wxRibbonPage
*page
)
65 wxRibbonPageTabInfo info
;
71 // info.rect not set (intentional)
73 wxClientDC
dcTemp(this);
74 wxString label
= wxEmptyString
;
75 if(m_flags
& wxRIBBON_BAR_SHOW_PAGE_LABELS
)
76 label
= page
->GetLabel();
77 wxBitmap icon
= wxNullBitmap
;
78 if(m_flags
& wxRIBBON_BAR_SHOW_PAGE_ICONS
)
79 icon
= page
->GetIcon();
80 m_art
->GetBarTabWidth(dcTemp
, this, label
, icon
,
82 &info
.small_begin_need_separator_width
,
83 &info
.small_must_have_separator_width
,
88 m_tabs_total_width_ideal
= info
.ideal_width
;
89 m_tabs_total_width_minimum
= info
.minimum_width
;
93 int sep
= m_art
->GetMetric(wxRIBBON_ART_TAB_SEPARATION_SIZE
);
94 m_tabs_total_width_ideal
+= sep
+ info
.ideal_width
;
95 m_tabs_total_width_minimum
+= sep
+ info
.minimum_width
;
99 page
->Hide(); // Most likely case is that this new page is not the active tab
100 page
->SetArtProvider(m_art
);
102 if(m_pages
.GetCount() == 1)
104 SetActivePage((size_t)0);
108 bool wxRibbonBar::DismissExpandedPanel()
110 if(m_current_page
== -1)
112 return m_pages
.Item(m_current_page
).page
->DismissExpandedPanel();
115 void wxRibbonBar::ShowPanels(bool show
)
117 m_arePanelsShown
= show
;
118 SetMinSize(wxSize(GetSize().GetWidth(), DoGetBestSize().GetHeight()));
120 GetParent()->Layout();
123 void wxRibbonBar::SetWindowStyleFlag(long style
)
127 m_art
->SetFlags(style
);
130 long wxRibbonBar::GetWindowStyleFlag() const
135 bool wxRibbonBar::Realize()
139 wxClientDC
dcTemp(this);
140 int sep
= m_art
->GetMetric(wxRIBBON_ART_TAB_SEPARATION_SIZE
);
141 size_t numtabs
= m_pages
.GetCount();
143 for(i
= 0; i
< numtabs
; ++i
)
145 wxRibbonPageTabInfo
& info
= m_pages
.Item(i
);
148 RepositionPage(info
.page
);
149 if(!info
.page
->Realize())
153 wxString label
= wxEmptyString
;
154 if(m_flags
& wxRIBBON_BAR_SHOW_PAGE_LABELS
)
155 label
= info
.page
->GetLabel();
156 wxBitmap icon
= wxNullBitmap
;
157 if(m_flags
& wxRIBBON_BAR_SHOW_PAGE_ICONS
)
158 icon
= info
.page
->GetIcon();
159 m_art
->GetBarTabWidth(dcTemp
, this, label
, icon
,
161 &info
.small_begin_need_separator_width
,
162 &info
.small_must_have_separator_width
,
163 &info
.minimum_width
);
167 m_tabs_total_width_ideal
= info
.ideal_width
;
168 m_tabs_total_width_minimum
= info
.minimum_width
;
172 m_tabs_total_width_ideal
+= sep
+ info
.ideal_width
;
173 m_tabs_total_width_minimum
+= sep
+ info
.minimum_width
;
176 m_tab_height
= m_art
->GetTabCtrlHeight(dcTemp
, this, m_pages
);
178 RecalculateMinSize();
179 RecalculateTabSizes();
185 void wxRibbonBar::OnMouseMove(wxMouseEvent
& evt
)
189 int hovered_page
= -1;
190 bool refresh_tabs
= false;
193 // It is quite likely that the mouse moved a small amount and is still over the same tab
194 if(m_current_hovered_page
!= -1 && m_pages
.Item((size_t)m_current_hovered_page
).rect
.Contains(x
, y
))
196 hovered_page
= m_current_hovered_page
;
197 // But be careful, if tabs can be scrolled, then parts of the tab rect may not be valid
198 if(m_tab_scroll_buttons_shown
)
200 if(x
>= m_tab_scroll_right_button_rect
.GetX() || x
< m_tab_scroll_left_button_rect
.GetRight())
208 HitTestTabs(evt
.GetPosition(), &hovered_page
);
211 if(hovered_page
!= m_current_hovered_page
)
213 if(m_current_hovered_page
!= -1)
215 m_pages
.Item((int)m_current_hovered_page
).hovered
= false;
217 m_current_hovered_page
= hovered_page
;
218 if(m_current_hovered_page
!= -1)
220 m_pages
.Item((int)m_current_hovered_page
).hovered
= true;
224 if(m_tab_scroll_buttons_shown
)
226 #define SET_FLAG(variable, flag) \
227 { if(((variable) & (flag)) != (flag)) { variable |= (flag); refresh_tabs = true; }}
228 #define UNSET_FLAG(variable, flag) \
229 { if((variable) & (flag)) { variable &= ~(flag); refresh_tabs = true; }}
231 if(m_tab_scroll_left_button_rect
.Contains(x
, y
))
232 SET_FLAG(m_tab_scroll_left_button_state
, wxRIBBON_SCROLL_BTN_HOVERED
)
234 UNSET_FLAG(m_tab_scroll_left_button_state
, wxRIBBON_SCROLL_BTN_HOVERED
)
236 if(m_tab_scroll_right_button_rect
.Contains(x
, y
))
237 SET_FLAG(m_tab_scroll_right_button_state
, wxRIBBON_SCROLL_BTN_HOVERED
)
239 UNSET_FLAG(m_tab_scroll_right_button_state
, wxRIBBON_SCROLL_BTN_HOVERED
)
247 HitTestToggleButton(evt
.GetPosition());
250 void wxRibbonBar::OnMouseLeave(wxMouseEvent
& WXUNUSED(evt
))
252 // The ribbon bar is (usually) at the top of a window, and at least on MSW, the mouse
253 // can leave the window quickly and leave a tab in the hovered state.
254 bool refresh_tabs
= false;
255 if(m_current_hovered_page
!= -1)
257 m_pages
.Item((int)m_current_hovered_page
).hovered
= false;
258 m_current_hovered_page
= -1;
261 if(m_tab_scroll_left_button_state
& wxRIBBON_SCROLL_BTN_HOVERED
)
263 m_tab_scroll_left_button_state
&= ~wxRIBBON_SCROLL_BTN_HOVERED
;
266 if(m_tab_scroll_right_button_state
& wxRIBBON_SCROLL_BTN_HOVERED
)
268 m_tab_scroll_right_button_state
&= ~wxRIBBON_SCROLL_BTN_HOVERED
;
275 if(m_toggle_button_hovered
)
277 m_bar_hovered
= false;
278 m_toggle_button_hovered
= false;
283 wxRibbonPage
* wxRibbonBar::GetPage(int n
)
285 if(n
< 0 || (size_t)n
>= m_pages
.GetCount())
287 return m_pages
.Item(n
).page
;
290 size_t wxRibbonBar::GetPageCount() const
292 return m_pages
.GetCount();
295 bool wxRibbonBar::IsPageShown(size_t page
) const
297 if (page
>= m_pages
.GetCount())
299 return m_pages
.Item(page
).shown
;
302 void wxRibbonBar::ShowPage(size_t page
, bool show
)
304 if(page
>= m_pages
.GetCount())
306 m_pages
.Item(page
).shown
= show
;
309 void wxRibbonBar::DeletePage(size_t n
)
311 if(n
< m_pages
.GetCount())
313 wxRibbonPage
*page
= m_pages
.Item(n
).page
;
315 // Schedule page object for destruction and not destroying directly
316 // as this function can be called in an event handler and page functions
317 // can be called afeter removing.
318 // Like in wxRibbonButtonBar::OnMouseUp
319 if(!wxTheApp
->IsScheduledForDestruction(page
))
321 wxTheApp
->ScheduleForDestruction(page
);
326 if(m_current_page
== static_cast<int>(n
))
330 if(m_pages
.GetCount() > 0)
332 if(n
>= m_pages
.GetCount())
334 SetActivePage(m_pages
.GetCount() - 1);
338 SetActivePage(n
- 1);
342 else if(m_current_page
> static_cast<int>(n
))
349 void wxRibbonBar::ClearPages()
352 for(i
=0; i
<m_pages
.GetCount(); i
++)
354 wxRibbonPage
*page
= m_pages
.Item(i
).page
;
355 // Schedule page object for destruction and not destroying directly
356 // as this function can be called in an event handler and page functions
357 // can be called afeter removing.
358 // Like in wxRibbonButtonBar::OnMouseUp
359 if(!wxTheApp
->IsScheduledForDestruction(page
))
361 wxTheApp
->ScheduleForDestruction(page
);
370 bool wxRibbonBar::SetActivePage(size_t page
)
372 if(m_current_page
== (int)page
)
377 if(page
>= m_pages
.GetCount())
382 if(m_current_page
!= -1)
384 m_pages
.Item((size_t)m_current_page
).active
= false;
385 m_pages
.Item((size_t)m_current_page
).page
->Hide();
387 m_current_page
= (int)page
;
388 m_pages
.Item(page
).active
= true;
389 m_pages
.Item(page
).shown
= true;
391 wxRibbonPage
* wnd
= m_pages
.Item(page
).page
;
401 bool wxRibbonBar::SetActivePage(wxRibbonPage
* page
)
403 size_t numpages
= m_pages
.GetCount();
405 for(i
= 0; i
< numpages
; ++i
)
407 if(m_pages
.Item(i
).page
== page
)
409 return SetActivePage(i
);
415 int wxRibbonBar::GetPageNumber(wxRibbonPage
* page
) const
417 size_t numpages
= m_pages
.GetCount();
418 for(size_t i
= 0; i
< numpages
; ++i
)
420 if(m_pages
.Item(i
).page
== page
)
429 int wxRibbonBar::GetActivePage() const
431 return m_current_page
;
434 void wxRibbonBar::SetTabCtrlMargins(int left
, int right
)
436 m_tab_margin_left
= left
;
437 m_tab_margin_right
= right
;
439 RecalculateTabSizes();
442 static int OrderPageTabInfoBySmallWidthAsc(wxRibbonPageTabInfo
**first
, wxRibbonPageTabInfo
**second
)
444 return (**first
).small_must_have_separator_width
- (**second
).small_must_have_separator_width
;
447 void wxRibbonBar::RecalculateTabSizes()
449 size_t numtabs
= m_pages
.GetCount();
454 int width
= GetSize().GetWidth() - m_tab_margin_left
- m_tab_margin_right
;
455 int tabsep
= m_art
->GetMetric(wxRIBBON_ART_TAB_SEPARATION_SIZE
);
456 int x
= m_tab_margin_left
;
459 if(width
>= m_tabs_total_width_ideal
)
461 // Simple case: everything at ideal width
463 for(i
= 0; i
< numtabs
; ++i
)
465 wxRibbonPageTabInfo
& info
= m_pages
.Item(i
);
470 info
.rect
.width
= info
.ideal_width
;
471 info
.rect
.height
= m_tab_height
;
472 x
+= info
.rect
.width
+ tabsep
;
474 m_tab_scroll_buttons_shown
= false;
475 m_tab_scroll_left_button_rect
.SetWidth(0);
476 m_tab_scroll_right_button_rect
.SetWidth(0);
478 else if(width
< m_tabs_total_width_minimum
)
480 // Simple case: everything minimum with scrollbar
482 for(i
= 0; i
< numtabs
; ++i
)
484 wxRibbonPageTabInfo
& info
= m_pages
.Item(i
);
489 info
.rect
.width
= info
.minimum_width
;
490 info
.rect
.height
= m_tab_height
;
491 x
+= info
.rect
.width
+ tabsep
;
493 if(!m_tab_scroll_buttons_shown
)
495 m_tab_scroll_left_button_state
= wxRIBBON_SCROLL_BTN_NORMAL
;
496 m_tab_scroll_right_button_state
= wxRIBBON_SCROLL_BTN_NORMAL
;
497 m_tab_scroll_buttons_shown
= true;
500 wxClientDC
temp_dc(this);
501 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());
502 m_tab_scroll_left_button_rect
.SetHeight(m_tab_height
);
503 m_tab_scroll_left_button_rect
.SetX(m_tab_margin_left
);
504 m_tab_scroll_left_button_rect
.SetY(0);
505 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());
506 m_tab_scroll_right_button_rect
.SetHeight(m_tab_height
);
507 m_tab_scroll_right_button_rect
.SetX(GetClientSize().GetWidth() - m_tab_margin_right
- m_tab_scroll_right_button_rect
.GetWidth());
508 m_tab_scroll_right_button_rect
.SetY(0);
510 if(m_tab_scroll_amount
== 0)
512 m_tab_scroll_left_button_rect
.SetWidth(0);
514 else if(m_tab_scroll_amount
+ width
>= m_tabs_total_width_minimum
)
516 m_tab_scroll_amount
= m_tabs_total_width_minimum
- width
;
517 m_tab_scroll_right_button_rect
.SetX(m_tab_scroll_right_button_rect
.GetX() + m_tab_scroll_right_button_rect
.GetWidth());
518 m_tab_scroll_right_button_rect
.SetWidth(0);
520 for(i
= 0; i
< numtabs
; ++i
)
522 wxRibbonPageTabInfo
& info
= m_pages
.Item(i
);
525 info
.rect
.x
-= m_tab_scroll_amount
;
530 m_tab_scroll_buttons_shown
= false;
531 m_tab_scroll_left_button_rect
.SetWidth(0);
532 m_tab_scroll_right_button_rect
.SetWidth(0);
533 // Complex case: everything sized such that: minimum <= width < ideal
536 1) Uniformly reduce all tab widths from ideal to small_must_have_separator_width
537 2) Reduce the largest tab by 1 pixel, repeating until all tabs are same width (or at minimum)
538 3) Uniformly reduce all tabs down to their minimum width
540 int smallest_tab_width
= INT_MAX
;
541 int total_small_width
= tabsep
* (numtabs
- 1);
543 for(i
= 0; i
< numtabs
; ++i
)
545 wxRibbonPageTabInfo
& info
= m_pages
.Item(i
);
548 if(info
.small_must_have_separator_width
< smallest_tab_width
)
550 smallest_tab_width
= info
.small_must_have_separator_width
;
552 total_small_width
+= info
.small_must_have_separator_width
;
554 if(width
>= total_small_width
)
557 int total_delta
= m_tabs_total_width_ideal
- total_small_width
;
558 total_small_width
-= tabsep
* (numtabs
- 1);
559 width
-= tabsep
* (numtabs
- 1);
560 for(i
= 0; i
< numtabs
; ++i
)
562 wxRibbonPageTabInfo
& info
= m_pages
.Item(i
);
565 int delta
= info
.ideal_width
- info
.small_must_have_separator_width
;
568 info
.rect
.width
= info
.small_must_have_separator_width
+ delta
* (width
- total_small_width
) / total_delta
;
569 info
.rect
.height
= m_tab_height
;
571 x
+= info
.rect
.width
+ tabsep
;
572 total_delta
-= delta
;
573 total_small_width
-= info
.small_must_have_separator_width
;
574 width
-= info
.rect
.width
;
579 total_small_width
= tabsep
* (numtabs
- 1);
580 for(i
= 0; i
< numtabs
; ++i
)
582 wxRibbonPageTabInfo
& info
= m_pages
.Item(i
);
585 if(info
.minimum_width
< smallest_tab_width
)
587 total_small_width
+= smallest_tab_width
;
591 total_small_width
+= info
.minimum_width
;
594 if(width
>= total_small_width
)
597 wxRibbonPageTabInfoArray sorted_pages
;
598 for(i
= 0; i
< numtabs
; ++i
)
600 // Sneaky obj array trickery to not copy the tab descriptors
601 if (!m_pages
.Item(i
).shown
)
603 sorted_pages
.Add(&m_pages
.Item(i
));
605 sorted_pages
.Sort(OrderPageTabInfoBySmallWidthAsc
);
606 width
-= tabsep
* (numtabs
- 1);
607 for(i
= 0; i
< numtabs
; ++i
)
609 wxRibbonPageTabInfo
& info
= sorted_pages
.Item(i
);
612 if(info
.small_must_have_separator_width
* (int)(numtabs
- i
) <= width
)
614 info
.rect
.width
= info
.small_must_have_separator_width
;;
618 info
.rect
.width
= width
/ (numtabs
- i
);
620 width
-= info
.rect
.width
;
622 for(i
= 0; i
< numtabs
; ++i
)
624 wxRibbonPageTabInfo
& info
= m_pages
.Item(i
);
629 info
.rect
.height
= m_tab_height
;
630 x
+= info
.rect
.width
+ tabsep
;
631 sorted_pages
.Detach(numtabs
- (i
+ 1));
637 total_small_width
= (smallest_tab_width
+ tabsep
) * numtabs
- tabsep
;
638 int total_delta
= total_small_width
- m_tabs_total_width_minimum
;
639 total_small_width
= m_tabs_total_width_minimum
- tabsep
* (numtabs
- 1);
640 width
-= tabsep
* (numtabs
- 1);
641 for(i
= 0; i
< numtabs
; ++i
)
643 wxRibbonPageTabInfo
& info
= m_pages
.Item(i
);
646 int delta
= smallest_tab_width
- info
.minimum_width
;
649 info
.rect
.width
= info
.minimum_width
+ delta
* (width
- total_small_width
) / total_delta
;
650 info
.rect
.height
= m_tab_height
;
652 x
+= info
.rect
.width
+ tabsep
;
653 total_delta
-= delta
;
654 total_small_width
-= info
.minimum_width
;
655 width
-= info
.rect
.width
;
662 wxRibbonBar::wxRibbonBar()
665 m_tabs_total_width_ideal
= 0;
666 m_tabs_total_width_minimum
= 0;
667 m_tab_margin_left
= 0;
668 m_tab_margin_right
= 0;
670 m_tab_scroll_amount
= 0;
672 m_current_hovered_page
= -1;
673 m_tab_scroll_left_button_state
= wxRIBBON_SCROLL_BTN_NORMAL
;
674 m_tab_scroll_right_button_state
= wxRIBBON_SCROLL_BTN_NORMAL
;
675 m_tab_scroll_buttons_shown
= false;
676 m_arePanelsShown
= true;
679 wxRibbonBar::wxRibbonBar(wxWindow
* parent
,
684 : wxRibbonControl(parent
, id
, pos
, size
, wxBORDER_NONE
)
689 wxRibbonBar::~wxRibbonBar()
691 SetArtProvider(NULL
);
694 bool wxRibbonBar::Create(wxWindow
* parent
,
700 if(!wxRibbonControl::Create(parent
, id
, pos
, size
, wxBORDER_NONE
))
708 void wxRibbonBar::CommonInit(long style
)
710 SetName(wxT("wxRibbonBar"));
713 m_tabs_total_width_ideal
= 0;
714 m_tabs_total_width_minimum
= 0;
715 m_tab_margin_left
= 50;
716 m_tab_margin_right
= 20;
717 m_tab_height
= 20; // initial guess
718 m_tab_scroll_amount
= 0;
720 m_current_hovered_page
= -1;
721 m_tab_scroll_left_button_state
= wxRIBBON_SCROLL_BTN_NORMAL
;
722 m_tab_scroll_right_button_state
= wxRIBBON_SCROLL_BTN_NORMAL
;
723 m_tab_scroll_buttons_shown
= false;
724 m_arePanelsShown
= true;
728 SetArtProvider(new wxRibbonDefaultArtProvider
);
730 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
732 m_toggle_button_hovered
= false;
733 m_bar_hovered
= false;
736 void wxRibbonBar::SetArtProvider(wxRibbonArtProvider
* art
)
738 wxRibbonArtProvider
*old
= m_art
;
743 art
->SetFlags(m_flags
);
745 size_t numpages
= m_pages
.GetCount();
747 for(i
= 0; i
< numpages
; ++i
)
749 wxRibbonPage
*page
= m_pages
.Item(i
).page
;
750 if(page
->GetArtProvider() != art
)
752 page
->SetArtProvider(art
);
759 void wxRibbonBar::OnPaint(wxPaintEvent
& WXUNUSED(evt
))
761 wxAutoBufferedPaintDC
dc(this);
763 if(GetUpdateRegion().Contains(0, 0, GetClientSize().GetWidth(), m_tab_height
) == wxOutRegion
)
765 // Nothing to do in the tab area, and the page area is handled by the active page
769 DoEraseBackground(dc
);
771 m_toggle_button_rect
= m_art
->GetBarToggleButtonArea(dc
, this, GetSize());
773 size_t numtabs
= m_pages
.GetCount();
774 double sep_visibility
= 0.0;
775 bool draw_sep
= false;
776 wxRect
tabs_rect(m_tab_margin_left
, 0, GetClientSize().GetWidth() - m_tab_margin_left
- m_tab_margin_right
, m_tab_height
);
777 if(m_tab_scroll_buttons_shown
)
779 tabs_rect
.x
+= m_tab_scroll_left_button_rect
.GetWidth();
780 tabs_rect
.width
-= m_tab_scroll_left_button_rect
.GetWidth() + m_tab_scroll_right_button_rect
.GetWidth();
783 for(i
= 0; i
< numtabs
; ++i
)
785 wxRibbonPageTabInfo
& info
= m_pages
.Item(i
);
789 dc
.DestroyClippingRegion();
790 if(m_tab_scroll_buttons_shown
)
792 if(!tabs_rect
.Intersects(info
.rect
))
794 dc
.SetClippingRegion(tabs_rect
);
796 dc
.SetClippingRegion(info
.rect
);
797 m_art
->DrawTab(dc
, this, info
);
799 if(info
.rect
.width
< info
.small_begin_need_separator_width
)
802 if(info
.rect
.width
< info
.small_must_have_separator_width
)
804 sep_visibility
+= 1.0;
808 sep_visibility
+= (double)(info
.small_begin_need_separator_width
- info
.rect
.width
) / (double)(info
.small_begin_need_separator_width
- info
.small_must_have_separator_width
);
814 wxRect rect
= m_pages
.Item(0).rect
;
815 rect
.width
= m_art
->GetMetric(wxRIBBON_ART_TAB_SEPARATION_SIZE
);
816 sep_visibility
/= (double)numtabs
;
817 for(i
= 0; i
< numtabs
- 1; ++i
)
819 wxRibbonPageTabInfo
& info
= m_pages
.Item(i
);
822 rect
.x
= info
.rect
.x
+ info
.rect
.width
;
824 if(m_tab_scroll_buttons_shown
&& !tabs_rect
.Intersects(rect
))
829 dc
.DestroyClippingRegion();
830 dc
.SetClippingRegion(rect
);
831 m_art
->DrawTabSeparator(dc
, this, rect
, sep_visibility
);
834 if(m_tab_scroll_buttons_shown
)
836 dc
.DestroyClippingRegion();
837 if(m_tab_scroll_left_button_rect
.GetWidth() != 0)
839 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
);
841 if(m_tab_scroll_right_button_rect
.GetWidth() != 0)
843 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
);
846 wxRect
rect(GetClientSize().GetWidth() - 30, 6, 12, 12);
847 if ( m_flags
& wxRIBBON_BAR_SHOW_TOGGLE_BUTTON
)
848 m_art
->DrawToggleButton(dc
, this, rect
, ArePanelsShown());
851 void wxRibbonBar::OnEraseBackground(wxEraseEvent
& WXUNUSED(evt
))
853 // Background painting done in main paint handler to reduce screen flicker
856 void wxRibbonBar::DoEraseBackground(wxDC
& dc
)
858 wxRect
tabs(GetSize());
859 tabs
.height
= m_tab_height
;
860 m_art
->DrawTabCtrlBackground(dc
, this, tabs
);
863 void wxRibbonBar::OnSize(wxSizeEvent
& evt
)
865 RecalculateTabSizes();
866 if(m_current_page
!= -1)
868 RepositionPage(m_pages
.Item(m_current_page
).page
);
875 void wxRibbonBar::RepositionPage(wxRibbonPage
*page
)
879 page
->SetSizeWithScrollButtonAdjustment(0, m_tab_height
, w
, h
- m_tab_height
);
882 wxRibbonPageTabInfo
* wxRibbonBar::HitTestTabs(wxPoint position
, int* index
)
884 wxRect
tabs_rect(m_tab_margin_left
, 0, GetClientSize().GetWidth() - m_tab_margin_left
- m_tab_margin_right
, m_tab_height
);
885 if(m_tab_scroll_buttons_shown
)
887 tabs_rect
.SetX(tabs_rect
.GetX() + m_tab_scroll_left_button_rect
.GetWidth());
888 tabs_rect
.SetWidth(tabs_rect
.GetWidth() - m_tab_scroll_left_button_rect
.GetWidth() - m_tab_scroll_right_button_rect
.GetWidth());
890 if(tabs_rect
.Contains(position
))
892 size_t numtabs
= m_pages
.GetCount();
894 for(i
= 0; i
< numtabs
; ++i
)
896 wxRibbonPageTabInfo
& info
= m_pages
.Item(i
);
899 if(info
.rect
.Contains(position
))
916 void wxRibbonBar::OnMouseLeftDown(wxMouseEvent
& evt
)
918 wxRibbonPageTabInfo
*tab
= HitTestTabs(evt
.GetPosition());
919 if(tab
&& tab
!= &m_pages
.Item(m_current_page
))
921 wxRibbonBarEvent
query(wxEVT_COMMAND_RIBBONBAR_PAGE_CHANGING
, GetId(), tab
->page
);
922 query
.SetEventObject(this);
923 ProcessWindowEvent(query
);
924 if(query
.IsAllowed())
926 SetActivePage(query
.GetPage());
928 wxRibbonBarEvent
notification(wxEVT_COMMAND_RIBBONBAR_PAGE_CHANGED
, GetId(), m_pages
.Item(m_current_page
).page
);
929 notification
.SetEventObject(this);
930 ProcessWindowEvent(notification
);
935 if(m_tab_scroll_left_button_rect
.Contains(evt
.GetPosition()))
937 m_tab_scroll_left_button_state
|= wxRIBBON_SCROLL_BTN_ACTIVE
| wxRIBBON_SCROLL_BTN_HOVERED
;
940 else if(m_tab_scroll_right_button_rect
.Contains(evt
.GetPosition()))
942 m_tab_scroll_right_button_state
|= wxRIBBON_SCROLL_BTN_ACTIVE
| wxRIBBON_SCROLL_BTN_HOVERED
;
947 wxPoint position
= evt
.GetPosition();
949 if(position
.x
>= 0 && position
.y
>= 0)
951 wxSize size
= GetSize();
952 if(position
.x
< size
.GetWidth() && position
.y
< size
.GetHeight())
954 if(m_toggle_button_rect
.Contains(position
))
956 ShowPanels(!ArePanelsShown());
957 wxRibbonBarEvent
event(wxEVT_COMMAND_RIBBONBAR_TOGGLED
, GetId());
958 event
.SetEventObject(this);
959 ProcessWindowEvent(event
);
965 void wxRibbonBar::OnMouseLeftUp(wxMouseEvent
& WXUNUSED(evt
))
967 if(!m_tab_scroll_buttons_shown
)
973 if(m_tab_scroll_left_button_state
& wxRIBBON_SCROLL_BTN_ACTIVE
)
977 else if(m_tab_scroll_right_button_state
& wxRIBBON_SCROLL_BTN_ACTIVE
)
983 m_tab_scroll_left_button_state
&= ~wxRIBBON_SCROLL_BTN_ACTIVE
;
984 m_tab_scroll_right_button_state
&= ~wxRIBBON_SCROLL_BTN_ACTIVE
;
985 ScrollTabBar(amount
* 8);
989 void wxRibbonBar::ScrollTabBar(int amount
)
991 bool show_left
= true;
992 bool show_right
= true;
993 if(m_tab_scroll_amount
+ amount
<= 0)
995 amount
= -m_tab_scroll_amount
;
998 else if(m_tab_scroll_amount
+ amount
+ (GetClientSize().GetWidth() - m_tab_margin_left
- m_tab_margin_right
) >= m_tabs_total_width_minimum
)
1000 amount
= m_tabs_total_width_minimum
- m_tab_scroll_amount
- (GetClientSize().GetWidth() - m_tab_margin_left
- m_tab_margin_right
);
1007 m_tab_scroll_amount
+= amount
;
1008 size_t numtabs
= m_pages
.GetCount();
1010 for(i
= 0; i
< numtabs
; ++i
)
1012 wxRibbonPageTabInfo
& info
= m_pages
.Item(i
);
1015 info
.rect
.SetX(info
.rect
.GetX() - amount
);
1017 if(show_right
!= (m_tab_scroll_right_button_rect
.GetWidth() != 0) ||
1018 show_left
!= (m_tab_scroll_left_button_rect
.GetWidth() != 0))
1020 wxClientDC
temp_dc(this);
1023 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());
1027 m_tab_scroll_left_button_rect
.SetWidth(0);
1032 if(m_tab_scroll_right_button_rect
.GetWidth() == 0)
1034 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());
1035 m_tab_scroll_right_button_rect
.SetX(m_tab_scroll_right_button_rect
.GetX() - m_tab_scroll_right_button_rect
.GetWidth());
1040 if(m_tab_scroll_right_button_rect
.GetWidth() != 0)
1042 m_tab_scroll_right_button_rect
.SetX(m_tab_scroll_right_button_rect
.GetX() + m_tab_scroll_right_button_rect
.GetWidth());
1043 m_tab_scroll_right_button_rect
.SetWidth(0);
1051 void wxRibbonBar::RefreshTabBar()
1053 wxRect
tab_rect(0, 0, GetClientSize().GetWidth(), m_tab_height
);
1054 Refresh(false, &tab_rect
);
1057 void wxRibbonBar::OnMouseMiddleDown(wxMouseEvent
& evt
)
1059 DoMouseButtonCommon(evt
, wxEVT_COMMAND_RIBBONBAR_TAB_MIDDLE_DOWN
);
1062 void wxRibbonBar::OnMouseMiddleUp(wxMouseEvent
& evt
)
1064 DoMouseButtonCommon(evt
, wxEVT_COMMAND_RIBBONBAR_TAB_MIDDLE_UP
);
1067 void wxRibbonBar::OnMouseRightDown(wxMouseEvent
& evt
)
1069 DoMouseButtonCommon(evt
, wxEVT_COMMAND_RIBBONBAR_TAB_RIGHT_DOWN
);
1072 void wxRibbonBar::OnMouseRightUp(wxMouseEvent
& evt
)
1074 DoMouseButtonCommon(evt
, wxEVT_COMMAND_RIBBONBAR_TAB_RIGHT_UP
);
1077 void wxRibbonBar::OnMouseDoubleClick(wxMouseEvent
& evt
)
1079 DoMouseButtonCommon(evt
, wxEVT_COMMAND_RIBBONBAR_TAB_LEFT_DCLICK
);
1082 void wxRibbonBar::DoMouseButtonCommon(wxMouseEvent
& evt
, wxEventType tab_event_type
)
1084 wxRibbonPageTabInfo
*tab
= HitTestTabs(evt
.GetPosition());
1087 wxRibbonBarEvent
notification(tab_event_type
, GetId(), tab
->page
);
1088 notification
.SetEventObject(this);
1089 ProcessWindowEvent(notification
);
1093 void wxRibbonBar::RecalculateMinSize()
1095 wxSize
min_size(wxDefaultCoord
, wxDefaultCoord
);
1096 size_t numtabs
= m_pages
.GetCount();
1099 min_size
= m_pages
.Item(0).page
->GetMinSize();
1102 for(i
= 1; i
< numtabs
; ++i
)
1104 wxRibbonPageTabInfo
& info
= m_pages
.Item(i
);
1107 wxSize page_min
= info
.page
->GetMinSize();
1109 min_size
.x
= wxMax(min_size
.x
, page_min
.x
);
1110 min_size
.y
= wxMax(min_size
.y
, page_min
.y
);
1113 if(min_size
.y
!= wxDefaultCoord
)
1115 // TODO: Decide on best course of action when min height is unspecified
1116 // - should we specify it to the tab minimum, or leave it unspecified?
1117 min_size
.IncBy(0, m_tab_height
);
1120 m_minWidth
= min_size
.GetWidth();
1121 m_minHeight
= m_arePanelsShown
? min_size
.GetHeight() : m_tab_height
;
1124 wxSize
wxRibbonBar::DoGetBestSize() const
1127 if(m_current_page
!= -1)
1129 best
= m_pages
.Item(m_current_page
).page
->GetBestSize();
1131 if(best
.GetHeight() == wxDefaultCoord
)
1133 best
.SetHeight(m_tab_height
);
1137 best
.IncBy(0, m_tab_height
);
1139 if(!m_arePanelsShown
)
1141 best
.SetHeight(m_tab_height
);
1146 void wxRibbonBar::HitTestToggleButton(wxPoint position
)
1148 bool hovered
= false, toggle_button_hovered
= false;
1149 if(position
.x
>= 0 && position
.y
>= 0)
1151 wxSize size
= GetSize();
1152 if(position
.x
< size
.GetWidth() && position
.y
< size
.GetHeight())
1159 toggle_button_hovered
= (m_flags
& wxRIBBON_BAR_SHOW_TOGGLE_BUTTON
) &&
1160 m_toggle_button_rect
.Contains(position
);
1162 if(hovered
!= m_bar_hovered
|| toggle_button_hovered
!= m_toggle_button_hovered
)
1164 m_bar_hovered
= hovered
;
1165 m_toggle_button_hovered
= toggle_button_hovered
;
1171 #endif // wxUSE_RIBBON