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"
28 #include "wx/msw/private.h"
31 #include "wx/arrimpl.cpp"
33 WX_DEFINE_USER_EXPORTED_OBJARRAY(wxRibbonPageTabInfoArray
)
35 wxDEFINE_EVENT(wxEVT_COMMAND_RIBBONBAR_PAGE_CHANGED
, wxRibbonBarEvent
);
36 wxDEFINE_EVENT(wxEVT_COMMAND_RIBBONBAR_PAGE_CHANGING
, wxRibbonBarEvent
);
37 wxDEFINE_EVENT(wxEVT_COMMAND_RIBBONBAR_TAB_MIDDLE_DOWN
, wxRibbonBarEvent
);
38 wxDEFINE_EVENT(wxEVT_COMMAND_RIBBONBAR_TAB_MIDDLE_UP
, wxRibbonBarEvent
);
39 wxDEFINE_EVENT(wxEVT_COMMAND_RIBBONBAR_TAB_RIGHT_DOWN
, wxRibbonBarEvent
);
40 wxDEFINE_EVENT(wxEVT_COMMAND_RIBBONBAR_TAB_RIGHT_UP
, wxRibbonBarEvent
);
41 wxDEFINE_EVENT(wxEVT_COMMAND_RIBBONBAR_TAB_LEFT_DCLICK
, wxRibbonBarEvent
);
43 IMPLEMENT_CLASS(wxRibbonBar
, wxRibbonControl
)
44 IMPLEMENT_DYNAMIC_CLASS(wxRibbonBarEvent
, wxNotifyEvent
)
46 BEGIN_EVENT_TABLE(wxRibbonBar
, wxRibbonControl
)
47 EVT_ERASE_BACKGROUND(wxRibbonBar::OnEraseBackground
)
48 EVT_LEAVE_WINDOW(wxRibbonBar::OnMouseLeave
)
49 EVT_LEFT_DOWN(wxRibbonBar::OnMouseLeftDown
)
50 EVT_LEFT_UP(wxRibbonBar::OnMouseLeftUp
)
51 EVT_MIDDLE_DOWN(wxRibbonBar::OnMouseMiddleDown
)
52 EVT_MIDDLE_UP(wxRibbonBar::OnMouseMiddleUp
)
53 EVT_MOTION(wxRibbonBar::OnMouseMove
)
54 EVT_PAINT(wxRibbonBar::OnPaint
)
55 EVT_RIGHT_DOWN(wxRibbonBar::OnMouseRightDown
)
56 EVT_RIGHT_UP(wxRibbonBar::OnMouseRightUp
)
57 EVT_LEFT_DCLICK(wxRibbonBar::OnMouseDoubleClick
)
58 EVT_SIZE(wxRibbonBar::OnSize
)
61 void wxRibbonBar::AddPage(wxRibbonPage
*page
)
63 wxRibbonPageTabInfo info
;
68 // info.rect not set (intentional)
70 wxClientDC
dcTemp(this);
71 wxString label
= wxEmptyString
;
72 if(m_flags
& wxRIBBON_BAR_SHOW_PAGE_LABELS
)
73 label
= page
->GetLabel();
74 wxBitmap icon
= wxNullBitmap
;
75 if(m_flags
& wxRIBBON_BAR_SHOW_PAGE_ICONS
)
76 icon
= page
->GetIcon();
77 m_art
->GetBarTabWidth(dcTemp
, this, label
, icon
,
79 &info
.small_begin_need_separator_width
,
80 &info
.small_must_have_separator_width
,
85 m_tabs_total_width_ideal
= info
.ideal_width
;
86 m_tabs_total_width_minimum
= info
.minimum_width
;
90 int sep
= m_art
->GetMetric(wxRIBBON_ART_TAB_SEPARATION_SIZE
);
91 m_tabs_total_width_ideal
+= sep
+ info
.ideal_width
;
92 m_tabs_total_width_minimum
+= sep
+ info
.minimum_width
;
96 page
->Hide(); // Most likely case is that this new page is not the active tab
97 page
->SetArtProvider(m_art
);
99 if(m_pages
.GetCount() == 1)
101 SetActivePage((size_t)0);
105 bool wxRibbonBar::DismissExpandedPanel()
107 if(m_current_page
== -1)
109 return m_pages
.Item(m_current_page
).page
->DismissExpandedPanel();
112 void wxRibbonBar::SetWindowStyleFlag(long style
)
116 m_art
->SetFlags(style
);
119 long wxRibbonBar::GetWindowStyleFlag() const
124 bool wxRibbonBar::Realize()
128 wxClientDC
dcTemp(this);
129 int sep
= m_art
->GetMetric(wxRIBBON_ART_TAB_SEPARATION_SIZE
);
130 size_t numtabs
= m_pages
.GetCount();
132 for(i
= 0; i
< numtabs
; ++i
)
134 wxRibbonPageTabInfo
& info
= m_pages
.Item(i
);
135 RepositionPage(info
.page
);
136 if(!info
.page
->Realize())
140 wxString label
= wxEmptyString
;
141 if(m_flags
& wxRIBBON_BAR_SHOW_PAGE_LABELS
)
142 label
= info
.page
->GetLabel();
143 wxBitmap icon
= wxNullBitmap
;
144 if(m_flags
& wxRIBBON_BAR_SHOW_PAGE_ICONS
)
145 icon
= info
.page
->GetIcon();
146 m_art
->GetBarTabWidth(dcTemp
, this, label
, icon
,
148 &info
.small_begin_need_separator_width
,
149 &info
.small_must_have_separator_width
,
150 &info
.minimum_width
);
154 m_tabs_total_width_ideal
= info
.ideal_width
;
155 m_tabs_total_width_minimum
= info
.minimum_width
;
159 m_tabs_total_width_ideal
+= sep
+ info
.ideal_width
;
160 m_tabs_total_width_minimum
+= sep
+ info
.minimum_width
;
163 m_tab_height
= m_art
->GetTabCtrlHeight(dcTemp
, this, m_pages
);
165 RecalculateMinSize();
166 RecalculateTabSizes();
172 void wxRibbonBar::OnMouseMove(wxMouseEvent
& evt
)
176 int hovered_page
= -1;
177 bool refresh_tabs
= false;
180 // It is quite likely that the mouse moved a small amount and is still over the same tab
181 if(m_current_hovered_page
!= -1 && m_pages
.Item((size_t)m_current_hovered_page
).rect
.Contains(x
, y
))
183 hovered_page
= m_current_hovered_page
;
184 // But be careful, if tabs can be scrolled, then parts of the tab rect may not be valid
185 if(m_tab_scroll_buttons_shown
)
187 if(x
>= m_tab_scroll_right_button_rect
.GetX() || x
< m_tab_scroll_left_button_rect
.GetRight())
195 HitTestTabs(evt
.GetPosition(), &hovered_page
);
198 if(hovered_page
!= m_current_hovered_page
)
200 if(m_current_hovered_page
!= -1)
202 m_pages
.Item((int)m_current_hovered_page
).hovered
= false;
204 m_current_hovered_page
= hovered_page
;
205 if(m_current_hovered_page
!= -1)
207 m_pages
.Item((int)m_current_hovered_page
).hovered
= true;
211 if(m_tab_scroll_buttons_shown
)
213 #define SET_FLAG(variable, flag) \
214 { if(((variable) & (flag)) != (flag)) { variable |= (flag); refresh_tabs = true; }}
215 #define UNSET_FLAG(variable, flag) \
216 { if((variable) & (flag)) { variable &= ~(flag); refresh_tabs = true; }}
218 if(m_tab_scroll_left_button_rect
.Contains(x
, y
))
219 SET_FLAG(m_tab_scroll_left_button_state
, wxRIBBON_SCROLL_BTN_HOVERED
)
221 UNSET_FLAG(m_tab_scroll_left_button_state
, wxRIBBON_SCROLL_BTN_HOVERED
)
223 if(m_tab_scroll_right_button_rect
.Contains(x
, y
))
224 SET_FLAG(m_tab_scroll_right_button_state
, wxRIBBON_SCROLL_BTN_HOVERED
)
226 UNSET_FLAG(m_tab_scroll_right_button_state
, wxRIBBON_SCROLL_BTN_HOVERED
)
236 void wxRibbonBar::OnMouseLeave(wxMouseEvent
& WXUNUSED(evt
))
238 // The ribbon bar is (usually) at the top of a window, and at least on MSW, the mouse
239 // can leave the window quickly and leave a tab in the hovered state.
240 bool refresh_tabs
= false;
241 if(m_current_hovered_page
!= -1)
243 m_pages
.Item((int)m_current_hovered_page
).hovered
= false;
244 m_current_hovered_page
= -1;
247 if(m_tab_scroll_left_button_state
& wxRIBBON_SCROLL_BTN_HOVERED
)
249 m_tab_scroll_left_button_state
&= ~wxRIBBON_SCROLL_BTN_HOVERED
;
252 if(m_tab_scroll_right_button_state
& wxRIBBON_SCROLL_BTN_HOVERED
)
254 m_tab_scroll_right_button_state
&= ~wxRIBBON_SCROLL_BTN_HOVERED
;
263 wxRibbonPage
* wxRibbonBar::GetPage(int n
)
265 if(n
< 0 || (size_t)n
>= m_pages
.GetCount())
267 return m_pages
.Item(n
).page
;
270 bool wxRibbonBar::SetActivePage(size_t page
)
272 if(m_current_page
== (int)page
)
277 if(page
>= m_pages
.GetCount())
282 if(m_current_page
!= -1)
284 m_pages
.Item((size_t)m_current_page
).active
= false;
285 m_pages
.Item((size_t)m_current_page
).page
->Hide();
287 m_current_page
= (int)page
;
288 m_pages
.Item(page
).active
= true;
290 wxRibbonPage
* wnd
= m_pages
.Item(page
).page
;
300 bool wxRibbonBar::SetActivePage(wxRibbonPage
* page
)
302 size_t numpages
= m_pages
.GetCount();
304 for(i
= 0; i
< numpages
; ++i
)
306 if(m_pages
.Item(i
).page
== page
)
308 return SetActivePage(i
);
314 int wxRibbonBar::GetActivePage() const
316 return m_current_page
;
319 void wxRibbonBar::SetTabCtrlMargins(int left
, int right
)
321 m_tab_margin_left
= left
;
322 m_tab_margin_right
= right
;
324 RecalculateTabSizes();
327 static int OrderPageTabInfoBySmallWidthAsc(wxRibbonPageTabInfo
**first
, wxRibbonPageTabInfo
**second
)
329 return (**first
).small_must_have_separator_width
- (**second
).small_must_have_separator_width
;
332 void wxRibbonBar::RecalculateTabSizes()
334 size_t numtabs
= m_pages
.GetCount();
339 int width
= GetSize().GetWidth() - m_tab_margin_left
- m_tab_margin_right
;
340 int tabsep
= m_art
->GetMetric(wxRIBBON_ART_TAB_SEPARATION_SIZE
);
341 int x
= m_tab_margin_left
;
344 if(width
>= m_tabs_total_width_ideal
)
346 // Simple case: everything at ideal width
348 for(i
= 0; i
< numtabs
; ++i
)
350 wxRibbonPageTabInfo
& info
= m_pages
.Item(i
);
353 info
.rect
.width
= info
.ideal_width
;
354 info
.rect
.height
= m_tab_height
;
355 x
+= info
.rect
.width
+ tabsep
;
357 m_tab_scroll_buttons_shown
= false;
358 m_tab_scroll_left_button_rect
.SetWidth(0);
359 m_tab_scroll_right_button_rect
.SetWidth(0);
361 else if(width
< m_tabs_total_width_minimum
)
363 // Simple case: everything minimum with scrollbar
365 for(i
= 0; i
< numtabs
; ++i
)
367 wxRibbonPageTabInfo
& info
= m_pages
.Item(i
);
370 info
.rect
.width
= info
.minimum_width
;
371 info
.rect
.height
= m_tab_height
;
372 x
+= info
.rect
.width
+ tabsep
;
374 if(!m_tab_scroll_buttons_shown
)
376 m_tab_scroll_left_button_state
= wxRIBBON_SCROLL_BTN_NORMAL
;
377 m_tab_scroll_right_button_state
= wxRIBBON_SCROLL_BTN_NORMAL
;
378 m_tab_scroll_buttons_shown
= true;
381 wxClientDC
temp_dc(this);
382 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());
383 m_tab_scroll_left_button_rect
.SetHeight(m_tab_height
);
384 m_tab_scroll_left_button_rect
.SetX(m_tab_margin_left
);
385 m_tab_scroll_left_button_rect
.SetY(0);
386 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());
387 m_tab_scroll_right_button_rect
.SetHeight(m_tab_height
);
388 m_tab_scroll_right_button_rect
.SetX(GetClientSize().GetWidth() - m_tab_margin_right
- m_tab_scroll_right_button_rect
.GetWidth());
389 m_tab_scroll_right_button_rect
.SetY(0);
391 if(m_tab_scroll_amount
== 0)
393 m_tab_scroll_left_button_rect
.SetWidth(0);
395 else if(m_tab_scroll_amount
+ width
>= m_tabs_total_width_minimum
)
397 m_tab_scroll_amount
= m_tabs_total_width_minimum
- width
;
398 m_tab_scroll_right_button_rect
.SetX(m_tab_scroll_right_button_rect
.GetX() + m_tab_scroll_right_button_rect
.GetWidth());
399 m_tab_scroll_right_button_rect
.SetWidth(0);
401 for(i
= 0; i
< numtabs
; ++i
)
403 wxRibbonPageTabInfo
& info
= m_pages
.Item(i
);
404 info
.rect
.x
-= m_tab_scroll_amount
;
409 m_tab_scroll_buttons_shown
= false;
410 m_tab_scroll_left_button_rect
.SetWidth(0);
411 m_tab_scroll_right_button_rect
.SetWidth(0);
412 // Complex case: everything sized such that: minimum <= width < ideal
415 1) Uniformly reduce all tab widths from ideal to small_must_have_separator_width
416 2) Reduce the largest tab by 1 pixel, repeating until all tabs are same width (or at minimum)
417 3) Uniformly reduce all tabs down to their minimum width
419 int smallest_tab_width
= INT_MAX
;
420 int total_small_width
= tabsep
* (numtabs
- 1);
422 for(i
= 0; i
< numtabs
; ++i
)
424 wxRibbonPageTabInfo
& info
= m_pages
.Item(i
);
425 if(info
.small_must_have_separator_width
< smallest_tab_width
)
427 smallest_tab_width
= info
.small_must_have_separator_width
;
429 total_small_width
+= info
.small_must_have_separator_width
;
431 if(width
>= total_small_width
)
434 int total_delta
= m_tabs_total_width_ideal
- total_small_width
;
435 total_small_width
-= tabsep
* (numtabs
- 1);
436 width
-= tabsep
* (numtabs
- 1);
437 for(i
= 0; i
< numtabs
; ++i
)
439 wxRibbonPageTabInfo
& info
= m_pages
.Item(i
);
440 int delta
= info
.ideal_width
- info
.small_must_have_separator_width
;
443 info
.rect
.width
= info
.small_must_have_separator_width
+ delta
* (width
- total_small_width
) / total_delta
;
444 info
.rect
.height
= m_tab_height
;
446 x
+= info
.rect
.width
+ tabsep
;
447 total_delta
-= delta
;
448 total_small_width
-= info
.small_must_have_separator_width
;
449 width
-= info
.rect
.width
;
454 total_small_width
= tabsep
* (numtabs
- 1);
455 for(i
= 0; i
< numtabs
; ++i
)
457 wxRibbonPageTabInfo
& info
= m_pages
.Item(i
);
458 if(info
.minimum_width
< smallest_tab_width
)
460 total_small_width
+= smallest_tab_width
;
464 total_small_width
+= info
.minimum_width
;
467 if(width
>= total_small_width
)
470 wxRibbonPageTabInfoArray sorted_pages
;
471 for(i
= 0; i
< numtabs
; ++i
)
473 // Sneaky obj array trickery to not copy the tab descriptors
474 sorted_pages
.Add(&m_pages
.Item(i
));
476 sorted_pages
.Sort(OrderPageTabInfoBySmallWidthAsc
);
477 width
-= tabsep
* (numtabs
- 1);
478 for(i
= 0; i
< numtabs
; ++i
)
480 wxRibbonPageTabInfo
& info
= sorted_pages
.Item(i
);
481 if(info
.small_must_have_separator_width
* (int)(numtabs
- i
) <= width
)
483 info
.rect
.width
= info
.small_must_have_separator_width
;;
487 info
.rect
.width
= width
/ (numtabs
- i
);
489 width
-= info
.rect
.width
;
491 for(i
= 0; i
< numtabs
; ++i
)
493 wxRibbonPageTabInfo
& info
= m_pages
.Item(i
);
496 info
.rect
.height
= m_tab_height
;
497 x
+= info
.rect
.width
+ tabsep
;
498 sorted_pages
.Detach(numtabs
- (i
+ 1));
504 total_small_width
= (smallest_tab_width
+ tabsep
) * numtabs
- tabsep
;
505 int total_delta
= total_small_width
- m_tabs_total_width_minimum
;
506 total_small_width
= m_tabs_total_width_minimum
- tabsep
* (numtabs
- 1);
507 width
-= tabsep
* (numtabs
- 1);
508 for(i
= 0; i
< numtabs
; ++i
)
510 wxRibbonPageTabInfo
& info
= m_pages
.Item(i
);
511 int delta
= smallest_tab_width
- info
.minimum_width
;
514 info
.rect
.width
= info
.minimum_width
+ delta
* (width
- total_small_width
) / total_delta
;
515 info
.rect
.height
= m_tab_height
;
517 x
+= info
.rect
.width
+ tabsep
;
518 total_delta
-= delta
;
519 total_small_width
-= info
.minimum_width
;
520 width
-= info
.rect
.width
;
527 wxRibbonBar::wxRibbonBar()
530 m_tabs_total_width_ideal
= 0;
531 m_tabs_total_width_minimum
= 0;
532 m_tab_margin_left
= 0;
533 m_tab_margin_right
= 0;
535 m_tab_scroll_amount
= 0;
537 m_current_hovered_page
= -1;
538 m_tab_scroll_left_button_state
= wxRIBBON_SCROLL_BTN_NORMAL
;
539 m_tab_scroll_right_button_state
= wxRIBBON_SCROLL_BTN_NORMAL
;
540 m_tab_scroll_buttons_shown
= false;
543 wxRibbonBar::wxRibbonBar(wxWindow
* parent
,
548 : wxRibbonControl(parent
, id
, pos
, size
, wxBORDER_NONE
)
553 wxRibbonBar::~wxRibbonBar()
555 SetArtProvider(NULL
);
558 bool wxRibbonBar::Create(wxWindow
* parent
,
564 if(!wxRibbonControl::Create(parent
, id
, pos
, size
, wxBORDER_NONE
))
572 void wxRibbonBar::CommonInit(long style
)
574 SetName(wxT("wxRibbonBar"));
577 m_tabs_total_width_ideal
= 0;
578 m_tabs_total_width_minimum
= 0;
579 m_tab_margin_left
= 50;
580 m_tab_margin_right
= 20;
581 m_tab_height
= 20; // initial guess
582 m_tab_scroll_amount
= 0;
584 m_current_hovered_page
= -1;
585 m_tab_scroll_left_button_state
= wxRIBBON_SCROLL_BTN_NORMAL
;
586 m_tab_scroll_right_button_state
= wxRIBBON_SCROLL_BTN_NORMAL
;
587 m_tab_scroll_buttons_shown
= false;
591 SetArtProvider(new wxRibbonDefaultArtProvider
);
593 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
596 void wxRibbonBar::SetArtProvider(wxRibbonArtProvider
* art
)
598 wxRibbonArtProvider
*old
= m_art
;
603 art
->SetFlags(m_flags
);
605 size_t numpages
= m_pages
.GetCount();
607 for(i
= 0; i
< numpages
; ++i
)
609 wxRibbonPage
*page
= m_pages
.Item(i
).page
;
610 if(page
->GetArtProvider() != art
)
612 page
->SetArtProvider(art
);
619 void wxRibbonBar::OnPaint(wxPaintEvent
& WXUNUSED(evt
))
621 wxAutoBufferedPaintDC
dc(this);
623 if(GetUpdateRegion().Contains(0, 0, GetClientSize().GetWidth(), m_tab_height
) == wxOutRegion
)
625 // Nothing to do in the tab area, and the page area is handled by the active page
629 DoEraseBackground(dc
);
631 size_t numtabs
= m_pages
.GetCount();
632 double sep_visibility
= 0.0;
633 bool draw_sep
= false;
634 wxRect
tabs_rect(m_tab_margin_left
, 0, GetClientSize().GetWidth() - m_tab_margin_left
- m_tab_margin_right
, m_tab_height
);
635 if(m_tab_scroll_buttons_shown
)
637 tabs_rect
.x
+= m_tab_scroll_left_button_rect
.GetWidth();
638 tabs_rect
.width
-= m_tab_scroll_left_button_rect
.GetWidth() + m_tab_scroll_right_button_rect
.GetWidth();
641 for(i
= 0; i
< numtabs
; ++i
)
643 wxRibbonPageTabInfo
& info
= m_pages
.Item(i
);
645 dc
.DestroyClippingRegion();
646 if(m_tab_scroll_buttons_shown
)
648 if(!tabs_rect
.Intersects(info
.rect
))
650 dc
.SetClippingRegion(tabs_rect
);
652 dc
.SetClippingRegion(info
.rect
);
653 m_art
->DrawTab(dc
, this, info
);
655 if(info
.rect
.width
< info
.small_begin_need_separator_width
)
658 if(info
.rect
.width
< info
.small_must_have_separator_width
)
660 sep_visibility
+= 1.0;
664 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
);
670 wxRect rect
= m_pages
.Item(0).rect
;
671 rect
.width
= m_art
->GetMetric(wxRIBBON_ART_TAB_SEPARATION_SIZE
);
672 sep_visibility
/= (double)numtabs
;
673 for(i
= 0; i
< numtabs
- 1; ++i
)
675 wxRibbonPageTabInfo
& info
= m_pages
.Item(i
);
676 rect
.x
= info
.rect
.x
+ info
.rect
.width
;
678 if(m_tab_scroll_buttons_shown
&& !tabs_rect
.Intersects(rect
))
683 dc
.DestroyClippingRegion();
684 dc
.SetClippingRegion(rect
);
685 m_art
->DrawTabSeparator(dc
, this, rect
, sep_visibility
);
688 if(m_tab_scroll_buttons_shown
)
690 dc
.DestroyClippingRegion();
691 if(m_tab_scroll_left_button_rect
.GetWidth() != 0)
693 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
);
695 if(m_tab_scroll_right_button_rect
.GetWidth() != 0)
697 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
);
702 void wxRibbonBar::OnEraseBackground(wxEraseEvent
& WXUNUSED(evt
))
704 // Background painting done in main paint handler to reduce screen flicker
707 void wxRibbonBar::DoEraseBackground(wxDC
& dc
)
709 wxRect
tabs(GetSize());
710 tabs
.height
= m_tab_height
;
711 m_art
->DrawTabCtrlBackground(dc
, this, tabs
);
714 void wxRibbonBar::OnSize(wxSizeEvent
& evt
)
716 RecalculateTabSizes();
717 if(m_current_page
!= -1)
719 RepositionPage(m_pages
.Item(m_current_page
).page
);
726 void wxRibbonBar::RepositionPage(wxRibbonPage
*page
)
730 page
->SetSizeWithScrollButtonAdjustment(0, m_tab_height
, w
, h
- m_tab_height
);
733 wxRibbonPageTabInfo
* wxRibbonBar::HitTestTabs(wxPoint position
, int* index
)
735 wxRect
tabs_rect(m_tab_margin_left
, 0, GetClientSize().GetWidth() - m_tab_margin_left
- m_tab_margin_right
, m_tab_height
);
736 if(m_tab_scroll_buttons_shown
)
738 tabs_rect
.SetX(tabs_rect
.GetX() + m_tab_scroll_left_button_rect
.GetWidth());
739 tabs_rect
.SetWidth(tabs_rect
.GetWidth() - m_tab_scroll_left_button_rect
.GetWidth() - m_tab_scroll_right_button_rect
.GetWidth());
741 if(tabs_rect
.Contains(position
))
743 size_t numtabs
= m_pages
.GetCount();
745 for(i
= 0; i
< numtabs
; ++i
)
747 wxRibbonPageTabInfo
& info
= m_pages
.Item(i
);
748 if(info
.rect
.Contains(position
))
765 void wxRibbonBar::OnMouseLeftDown(wxMouseEvent
& evt
)
767 wxRibbonPageTabInfo
*tab
= HitTestTabs(evt
.GetPosition());
768 if(tab
&& tab
!= &m_pages
.Item(m_current_page
))
770 wxRibbonBarEvent
query(wxEVT_COMMAND_RIBBONBAR_PAGE_CHANGING
, GetId(), tab
->page
);
771 query
.SetEventObject(this);
772 ProcessWindowEvent(query
);
773 if(query
.IsAllowed())
775 SetActivePage(query
.GetPage());
777 wxRibbonBarEvent
notification(wxEVT_COMMAND_RIBBONBAR_PAGE_CHANGED
, GetId(), m_pages
.Item(m_current_page
).page
);
778 notification
.SetEventObject(this);
779 ProcessWindowEvent(notification
);
784 if(m_tab_scroll_left_button_rect
.Contains(evt
.GetPosition()))
786 m_tab_scroll_left_button_state
|= wxRIBBON_SCROLL_BTN_ACTIVE
| wxRIBBON_SCROLL_BTN_HOVERED
;
789 else if(m_tab_scroll_right_button_rect
.Contains(evt
.GetPosition()))
791 m_tab_scroll_right_button_state
|= wxRIBBON_SCROLL_BTN_ACTIVE
| wxRIBBON_SCROLL_BTN_HOVERED
;
797 void wxRibbonBar::OnMouseLeftUp(wxMouseEvent
& WXUNUSED(evt
))
799 if(!m_tab_scroll_buttons_shown
)
805 if(m_tab_scroll_left_button_state
& wxRIBBON_SCROLL_BTN_ACTIVE
)
809 else if(m_tab_scroll_right_button_state
& wxRIBBON_SCROLL_BTN_ACTIVE
)
815 m_tab_scroll_left_button_state
&= ~wxRIBBON_SCROLL_BTN_ACTIVE
;
816 m_tab_scroll_right_button_state
&= ~wxRIBBON_SCROLL_BTN_ACTIVE
;
817 ScrollTabBar(amount
* 8);
821 void wxRibbonBar::ScrollTabBar(int amount
)
823 bool show_left
= true;
824 bool show_right
= true;
825 if(m_tab_scroll_amount
+ amount
<= 0)
827 amount
= -m_tab_scroll_amount
;
830 else if(m_tab_scroll_amount
+ amount
+ (GetClientSize().GetWidth() - m_tab_margin_left
- m_tab_margin_right
) >= m_tabs_total_width_minimum
)
832 amount
= m_tabs_total_width_minimum
- m_tab_scroll_amount
- (GetClientSize().GetWidth() - m_tab_margin_left
- m_tab_margin_right
);
839 m_tab_scroll_amount
+= amount
;
840 size_t numtabs
= m_pages
.GetCount();
842 for(i
= 0; i
< numtabs
; ++i
)
844 wxRibbonPageTabInfo
& info
= m_pages
.Item(i
);
845 info
.rect
.SetX(info
.rect
.GetX() - amount
);
847 if(show_right
!= (m_tab_scroll_right_button_rect
.GetWidth() != 0) ||
848 show_left
!= (m_tab_scroll_left_button_rect
.GetWidth() != 0))
850 wxClientDC
temp_dc(this);
853 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());
857 m_tab_scroll_left_button_rect
.SetWidth(0);
862 if(m_tab_scroll_right_button_rect
.GetWidth() == 0)
864 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());
865 m_tab_scroll_right_button_rect
.SetX(m_tab_scroll_right_button_rect
.GetX() - m_tab_scroll_right_button_rect
.GetWidth());
870 if(m_tab_scroll_right_button_rect
.GetWidth() != 0)
872 m_tab_scroll_right_button_rect
.SetX(m_tab_scroll_right_button_rect
.GetX() + m_tab_scroll_right_button_rect
.GetWidth());
873 m_tab_scroll_right_button_rect
.SetWidth(0);
881 void wxRibbonBar::RefreshTabBar()
883 wxRect
tab_rect(0, 0, GetClientSize().GetWidth(), m_tab_height
);
884 Refresh(false, &tab_rect
);
887 void wxRibbonBar::OnMouseMiddleDown(wxMouseEvent
& evt
)
889 DoMouseButtonCommon(evt
, wxEVT_COMMAND_RIBBONBAR_TAB_MIDDLE_DOWN
);
892 void wxRibbonBar::OnMouseMiddleUp(wxMouseEvent
& evt
)
894 DoMouseButtonCommon(evt
, wxEVT_COMMAND_RIBBONBAR_TAB_MIDDLE_UP
);
897 void wxRibbonBar::OnMouseRightDown(wxMouseEvent
& evt
)
899 DoMouseButtonCommon(evt
, wxEVT_COMMAND_RIBBONBAR_TAB_RIGHT_DOWN
);
902 void wxRibbonBar::OnMouseRightUp(wxMouseEvent
& evt
)
904 DoMouseButtonCommon(evt
, wxEVT_COMMAND_RIBBONBAR_TAB_RIGHT_UP
);
907 void wxRibbonBar::OnMouseDoubleClick(wxMouseEvent
& evt
)
909 DoMouseButtonCommon(evt
, wxEVT_COMMAND_RIBBONBAR_TAB_LEFT_DCLICK
);
912 void wxRibbonBar::DoMouseButtonCommon(wxMouseEvent
& evt
, wxEventType tab_event_type
)
914 wxRibbonPageTabInfo
*tab
= HitTestTabs(evt
.GetPosition());
917 wxRibbonBarEvent
notification(tab_event_type
, GetId(), tab
->page
);
918 notification
.SetEventObject(this);
919 ProcessWindowEvent(notification
);
923 void wxRibbonBar::RecalculateMinSize()
925 wxSize
min_size(wxDefaultCoord
, wxDefaultCoord
);
926 size_t numtabs
= m_pages
.GetCount();
929 min_size
= m_pages
.Item(0).page
->GetMinSize();
932 for(i
= 1; i
< numtabs
; ++i
)
934 wxRibbonPageTabInfo
& info
= m_pages
.Item(i
);
935 wxSize page_min
= info
.page
->GetMinSize();
937 min_size
.x
= wxMax(min_size
.x
, page_min
.x
);
938 min_size
.y
= wxMax(min_size
.y
, page_min
.y
);
941 if(min_size
.y
!= wxDefaultCoord
)
943 // TODO: Decide on best course of action when min height is unspecified
944 // - should we specify it to the tab minimum, or leave it unspecified?
945 min_size
.IncBy(0, m_tab_height
);
948 m_minWidth
= min_size
.GetWidth();
949 m_minHeight
= min_size
.GetHeight();
952 wxSize
wxRibbonBar::DoGetBestSize() const
955 if(m_current_page
!= -1)
957 best
= m_pages
.Item(m_current_page
).page
->GetBestSize();
959 if(best
.GetHeight() == wxDefaultCoord
)
961 best
.SetHeight(m_tab_height
);
965 best
.IncBy(0, m_tab_height
);
970 #endif // wxUSE_RIBBON