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"
18 #include "wx/ribbon/bar.h"
22 #include "wx/ribbon/art.h"
23 #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
);
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_SIZE(wxRibbonBar::OnSize
)
60 void wxRibbonBar::AddPage(wxRibbonPage
*page
)
62 wxRibbonPageTabInfo info
;
67 // info.rect not set (intentional)
69 wxClientDC
dcTemp(this);
70 wxString label
= wxEmptyString
;
71 if(m_flags
& wxRIBBON_BAR_SHOW_PAGE_LABELS
)
72 label
= page
->GetLabel();
73 wxBitmap icon
= wxNullBitmap
;
74 if(m_flags
& wxRIBBON_BAR_SHOW_PAGE_ICONS
)
75 icon
= page
->GetIcon();
76 m_art
->GetBarTabWidth(dcTemp
, this, label
, icon
,
78 &info
.small_begin_need_separator_width
,
79 &info
.small_must_have_separator_width
,
84 m_tabs_total_width_ideal
= info
.ideal_width
;
85 m_tabs_total_width_minimum
= info
.minimum_width
;
89 int sep
= m_art
->GetMetric(wxRIBBON_ART_TAB_SEPARATION_SIZE
);
90 m_tabs_total_width_ideal
+= sep
+ info
.ideal_width
;
91 m_tabs_total_width_minimum
+= sep
+ info
.minimum_width
;
95 page
->Hide(); // Most likely case is that this new page is not the active tab
96 page
->SetArtProvider(m_art
);
98 if(m_pages
.GetCount() == 1)
100 SetActivePage((size_t)0);
104 bool wxRibbonBar::DismissExpandedPanel()
106 if(m_current_page
== -1)
108 return m_pages
.Item(m_current_page
).page
->DismissExpandedPanel();
111 void wxRibbonBar::SetWindowStyleFlag(long style
)
115 m_art
->SetFlags(style
);
118 long wxRibbonBar::GetWindowStyleFlag() const
123 bool wxRibbonBar::Realize()
127 wxClientDC
dcTemp(this);
128 int sep
= m_art
->GetMetric(wxRIBBON_ART_TAB_SEPARATION_SIZE
);
129 size_t numtabs
= m_pages
.GetCount();
131 for(i
= 0; i
< numtabs
; ++i
)
133 wxRibbonPageTabInfo
& info
= m_pages
.Item(i
);
134 RepositionPage(info
.page
);
135 if(!info
.page
->Realize())
139 wxString label
= wxEmptyString
;
140 if(m_flags
& wxRIBBON_BAR_SHOW_PAGE_LABELS
)
141 label
= info
.page
->GetLabel();
142 wxBitmap icon
= wxNullBitmap
;
143 if(m_flags
& wxRIBBON_BAR_SHOW_PAGE_ICONS
)
144 icon
= info
.page
->GetIcon();
145 m_art
->GetBarTabWidth(dcTemp
, this, label
, icon
,
147 &info
.small_begin_need_separator_width
,
148 &info
.small_must_have_separator_width
,
149 &info
.minimum_width
);
153 m_tabs_total_width_ideal
= info
.ideal_width
;
154 m_tabs_total_width_minimum
= info
.minimum_width
;
158 m_tabs_total_width_ideal
+= sep
+ info
.ideal_width
;
159 m_tabs_total_width_minimum
+= sep
+ info
.minimum_width
;
162 m_tab_height
= m_art
->GetTabCtrlHeight(dcTemp
, this, m_pages
);
164 RecalculateMinSize();
165 RecalculateTabSizes();
171 void wxRibbonBar::OnMouseMove(wxMouseEvent
& evt
)
175 int hovered_page
= -1;
176 bool refresh_tabs
= false;
179 // It is quite likely that the mouse moved a small amount and is still over the same tab
180 if(m_current_hovered_page
!= -1 && m_pages
.Item((size_t)m_current_hovered_page
).rect
.Contains(x
, y
))
182 hovered_page
= m_current_hovered_page
;
183 // But be careful, if tabs can be scrolled, then parts of the tab rect may not be valid
184 if(m_tab_scroll_buttons_shown
)
186 if(x
>= m_tab_scroll_right_button_rect
.GetX() || x
< m_tab_scroll_left_button_rect
.GetRight())
194 HitTestTabs(evt
.GetPosition(), &hovered_page
);
197 if(hovered_page
!= m_current_hovered_page
)
199 if(m_current_hovered_page
!= -1)
201 m_pages
.Item((int)m_current_hovered_page
).hovered
= false;
203 m_current_hovered_page
= hovered_page
;
204 if(m_current_hovered_page
!= -1)
206 m_pages
.Item((int)m_current_hovered_page
).hovered
= true;
210 if(m_tab_scroll_buttons_shown
)
212 #define SET_FLAG(variable, flag) \
213 { if(((variable) & (flag)) != (flag)) variable |= (flag), refresh_tabs = true; }
214 #define UNSET_FLAG(variable, flag) \
215 { if((variable) & (flag)) variable &= ~(flag), refresh_tabs = true; }
217 if(m_tab_scroll_left_button_rect
.Contains(x
, y
))
218 SET_FLAG(m_tab_scroll_left_button_state
, wxRIBBON_SCROLL_BTN_HOVERED
)
220 UNSET_FLAG(m_tab_scroll_left_button_state
, wxRIBBON_SCROLL_BTN_HOVERED
)
222 if(m_tab_scroll_right_button_rect
.Contains(x
, y
))
223 SET_FLAG(m_tab_scroll_right_button_state
, wxRIBBON_SCROLL_BTN_HOVERED
)
225 UNSET_FLAG(m_tab_scroll_right_button_state
, wxRIBBON_SCROLL_BTN_HOVERED
)
235 void wxRibbonBar::OnMouseLeave(wxMouseEvent
& WXUNUSED(evt
))
237 // The ribbon bar is (usually) at the top of a window, and at least on MSW, the mouse
238 // can leave the window quickly and leave a tab in the hovered state.
239 bool refresh_tabs
= false;
240 if(m_current_hovered_page
!= -1)
242 m_pages
.Item((int)m_current_hovered_page
).hovered
= false;
243 m_current_hovered_page
= -1;
246 if(m_tab_scroll_left_button_state
& wxRIBBON_SCROLL_BTN_HOVERED
)
248 m_tab_scroll_left_button_state
&= ~wxRIBBON_SCROLL_BTN_HOVERED
;
251 if(m_tab_scroll_right_button_state
& wxRIBBON_SCROLL_BTN_HOVERED
)
253 m_tab_scroll_right_button_state
&= ~wxRIBBON_SCROLL_BTN_HOVERED
;
262 wxRibbonPage
* wxRibbonBar::GetPage(int n
)
264 if(n
< 0 || (size_t)n
>= m_pages
.GetCount())
266 return m_pages
.Item(n
).page
;
269 bool wxRibbonBar::SetActivePage(size_t page
)
271 if(m_current_page
== (int)page
)
276 if(page
>= m_pages
.GetCount())
281 if(m_current_page
!= -1)
283 m_pages
.Item((size_t)m_current_page
).active
= false;
284 m_pages
.Item((size_t)m_current_page
).page
->Hide();
286 m_current_page
= (int)page
;
287 m_pages
.Item(page
).active
= true;
289 wxRibbonPage
* wnd
= m_pages
.Item(page
).page
;
299 bool wxRibbonBar::SetActivePage(wxRibbonPage
* page
)
301 size_t numpages
= m_pages
.GetCount();
303 for(i
= 0; i
< numpages
; ++i
)
305 if(m_pages
.Item(i
).page
== page
)
307 return SetActivePage(i
);
313 int wxRibbonBar::GetActivePage() const
315 return m_current_page
;
318 void wxRibbonBar::SetTabCtrlMargins(int left
, int right
)
320 m_tab_margin_left
= left
;
321 m_tab_margin_right
= right
;
323 RecalculateTabSizes();
326 static int OrderPageTabInfoBySmallWidthAsc(wxRibbonPageTabInfo
**first
, wxRibbonPageTabInfo
**second
)
328 return (**first
).small_must_have_separator_width
- (**second
).small_must_have_separator_width
;
331 void wxRibbonBar::RecalculateTabSizes()
333 size_t numtabs
= m_pages
.GetCount();
338 int width
= GetSize().GetWidth() - m_tab_margin_left
- m_tab_margin_right
;
339 int tabsep
= m_art
->GetMetric(wxRIBBON_ART_TAB_SEPARATION_SIZE
);
340 int x
= m_tab_margin_left
;
343 if(width
>= m_tabs_total_width_ideal
)
345 // Simple case: everything at ideal width
347 for(i
= 0; i
< numtabs
; ++i
)
349 wxRibbonPageTabInfo
& info
= m_pages
.Item(i
);
352 info
.rect
.width
= info
.ideal_width
;
353 info
.rect
.height
= m_tab_height
;
354 x
+= info
.rect
.width
+ tabsep
;
356 m_tab_scroll_buttons_shown
= false;
357 m_tab_scroll_left_button_rect
.SetWidth(0);
358 m_tab_scroll_right_button_rect
.SetWidth(0);
360 else if(width
< m_tabs_total_width_minimum
)
362 // Simple case: everything minimum with scrollbar
364 for(i
= 0; i
< numtabs
; ++i
)
366 wxRibbonPageTabInfo
& info
= m_pages
.Item(i
);
369 info
.rect
.width
= info
.minimum_width
;
370 info
.rect
.height
= m_tab_height
;
371 x
+= info
.rect
.width
+ tabsep
;
373 if(!m_tab_scroll_buttons_shown
)
375 m_tab_scroll_left_button_state
= wxRIBBON_SCROLL_BTN_NORMAL
;
376 m_tab_scroll_right_button_state
= wxRIBBON_SCROLL_BTN_NORMAL
;
377 m_tab_scroll_buttons_shown
= true;
380 wxClientDC
temp_dc(this);
381 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());
382 m_tab_scroll_left_button_rect
.SetHeight(m_tab_height
);
383 m_tab_scroll_left_button_rect
.SetX(m_tab_margin_left
);
384 m_tab_scroll_left_button_rect
.SetY(0);
385 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());
386 m_tab_scroll_right_button_rect
.SetHeight(m_tab_height
);
387 m_tab_scroll_right_button_rect
.SetX(GetClientSize().GetWidth() - m_tab_margin_right
- m_tab_scroll_right_button_rect
.GetWidth());
388 m_tab_scroll_right_button_rect
.SetY(0);
390 if(m_tab_scroll_amount
== 0)
392 m_tab_scroll_left_button_rect
.SetWidth(0);
394 else if(m_tab_scroll_amount
+ width
>= m_tabs_total_width_minimum
)
396 m_tab_scroll_amount
= m_tabs_total_width_minimum
- width
;
397 m_tab_scroll_right_button_rect
.SetX(m_tab_scroll_right_button_rect
.GetX() + m_tab_scroll_right_button_rect
.GetWidth());
398 m_tab_scroll_right_button_rect
.SetWidth(0);
400 for(i
= 0; i
< numtabs
; ++i
)
402 wxRibbonPageTabInfo
& info
= m_pages
.Item(i
);
403 info
.rect
.x
-= m_tab_scroll_amount
;
408 m_tab_scroll_buttons_shown
= false;
409 m_tab_scroll_left_button_rect
.SetWidth(0);
410 m_tab_scroll_right_button_rect
.SetWidth(0);
411 // Complex case: everything sized such that: minimum <= width < ideal
414 1) Uniformly reduce all tab widths from ideal to small_must_have_separator_width
415 2) Reduce the largest tab by 1 pixel, repeating until all tabs are same width (or at minimum)
416 3) Uniformly reduce all tabs down to their minimum width
418 int smallest_tab_width
= INT_MAX
;
419 int total_small_width
= tabsep
* (numtabs
- 1);
421 for(i
= 0; i
< numtabs
; ++i
)
423 wxRibbonPageTabInfo
& info
= m_pages
.Item(i
);
424 if(info
.small_must_have_separator_width
< smallest_tab_width
)
426 smallest_tab_width
= info
.small_must_have_separator_width
;
428 total_small_width
+= info
.small_must_have_separator_width
;
430 if(width
>= total_small_width
)
433 int total_delta
= m_tabs_total_width_ideal
- total_small_width
;
434 total_small_width
-= tabsep
* (numtabs
- 1);
435 width
-= tabsep
* (numtabs
- 1);
436 for(i
= 0; i
< numtabs
; ++i
)
438 wxRibbonPageTabInfo
& info
= m_pages
.Item(i
);
439 int delta
= info
.ideal_width
- info
.small_must_have_separator_width
;
442 info
.rect
.width
= info
.small_must_have_separator_width
+ delta
* (width
- total_small_width
) / total_delta
;
443 info
.rect
.height
= m_tab_height
;
445 x
+= info
.rect
.width
+ tabsep
;
446 total_delta
-= delta
;
447 total_small_width
-= info
.small_must_have_separator_width
;
448 width
-= info
.rect
.width
;
453 total_small_width
= tabsep
* (numtabs
- 1);
454 for(i
= 0; i
< numtabs
; ++i
)
456 wxRibbonPageTabInfo
& info
= m_pages
.Item(i
);
457 if(info
.minimum_width
< smallest_tab_width
)
459 total_small_width
+= smallest_tab_width
;
463 total_small_width
+= info
.minimum_width
;
466 if(width
>= total_small_width
)
469 wxRibbonPageTabInfoArray sorted_pages
;
470 for(i
= 0; i
< numtabs
; ++i
)
472 // Sneaky obj array trickery to not copy the tab descriptors
473 sorted_pages
.Add(&m_pages
.Item(i
));
475 sorted_pages
.Sort(OrderPageTabInfoBySmallWidthAsc
);
476 width
-= tabsep
* (numtabs
- 1);
477 for(i
= 0; i
< numtabs
; ++i
)
479 wxRibbonPageTabInfo
& info
= sorted_pages
.Item(i
);
480 if(info
.small_must_have_separator_width
* (int)(numtabs
- i
) <= width
)
482 info
.rect
.width
= info
.small_must_have_separator_width
;;
486 info
.rect
.width
= width
/ (numtabs
- i
);
488 width
-= info
.rect
.width
;
490 for(i
= 0; i
< numtabs
; ++i
)
492 wxRibbonPageTabInfo
& info
= m_pages
.Item(i
);
495 info
.rect
.height
= m_tab_height
;
496 x
+= info
.rect
.width
+ tabsep
;
497 sorted_pages
.Detach(numtabs
- (i
+ 1));
503 total_small_width
= (smallest_tab_width
+ tabsep
) * numtabs
- tabsep
;
504 int total_delta
= total_small_width
- m_tabs_total_width_minimum
;
505 total_small_width
= m_tabs_total_width_minimum
- tabsep
* (numtabs
- 1);
506 width
-= tabsep
* (numtabs
- 1);
507 for(i
= 0; i
< numtabs
; ++i
)
509 wxRibbonPageTabInfo
& info
= m_pages
.Item(i
);
510 int delta
= smallest_tab_width
- info
.minimum_width
;
513 info
.rect
.width
= info
.minimum_width
+ delta
* (width
- total_small_width
) / total_delta
;
514 info
.rect
.height
= m_tab_height
;
516 x
+= info
.rect
.width
+ tabsep
;
517 total_delta
-= delta
;
518 total_small_width
-= info
.minimum_width
;
519 width
-= info
.rect
.width
;
526 wxRibbonBar::wxRibbonBar()
529 m_tabs_total_width_ideal
= 0;
530 m_tabs_total_width_minimum
= 0;
531 m_tab_margin_left
= 0;
532 m_tab_margin_right
= 0;
534 m_tab_scroll_amount
= 0;
536 m_current_hovered_page
= -1;
537 m_tab_scroll_left_button_state
= wxRIBBON_SCROLL_BTN_NORMAL
;
538 m_tab_scroll_right_button_state
= wxRIBBON_SCROLL_BTN_NORMAL
;
539 m_tab_scroll_buttons_shown
= false;
542 wxRibbonBar::wxRibbonBar(wxWindow
* parent
,
547 : wxRibbonControl(parent
, id
, pos
, size
, wxBORDER_NONE
)
552 wxRibbonBar::~wxRibbonBar()
554 SetArtProvider(NULL
);
557 bool wxRibbonBar::Create(wxWindow
* parent
,
563 if(!wxRibbonControl::Create(parent
, id
, pos
, size
, wxBORDER_NONE
))
571 void wxRibbonBar::CommonInit(long style
)
573 SetName(wxT("wxRibbonBar"));
576 m_tabs_total_width_ideal
= 0;
577 m_tabs_total_width_minimum
= 0;
578 m_tab_margin_left
= 50;
579 m_tab_margin_right
= 20;
580 m_tab_height
= 20; // initial guess
581 m_tab_scroll_amount
= 0;
583 m_current_hovered_page
= -1;
584 m_tab_scroll_left_button_state
= wxRIBBON_SCROLL_BTN_NORMAL
;
585 m_tab_scroll_right_button_state
= wxRIBBON_SCROLL_BTN_NORMAL
;
586 m_tab_scroll_buttons_shown
= false;
590 SetArtProvider(new wxRibbonDefaultArtProvider
);
592 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
595 void wxRibbonBar::SetArtProvider(wxRibbonArtProvider
* art
)
597 wxRibbonArtProvider
*old
= m_art
;
602 art
->SetFlags(m_flags
);
604 size_t numpages
= m_pages
.GetCount();
606 for(i
= 0; i
< numpages
; ++i
)
608 wxRibbonPage
*page
= m_pages
.Item(i
).page
;
609 if(page
->GetArtProvider() != art
)
611 page
->SetArtProvider(art
);
618 void wxRibbonBar::OnPaint(wxPaintEvent
& WXUNUSED(evt
))
620 wxAutoBufferedPaintDC
dc(this);
622 if(GetUpdateRegion().Contains(0, 0, GetClientSize().GetWidth(), m_tab_height
) == wxOutRegion
)
624 // Nothing to do in the tab area, and the page area is handled by the active page
628 DoEraseBackground(dc
);
630 size_t numtabs
= m_pages
.GetCount();
631 double sep_visibility
= 0.0;
632 bool draw_sep
= false;
633 wxRect
tabs_rect(m_tab_margin_left
, 0, GetClientSize().GetWidth() - m_tab_margin_left
- m_tab_margin_right
, m_tab_height
);
634 if(m_tab_scroll_buttons_shown
)
636 tabs_rect
.x
+= m_tab_scroll_left_button_rect
.GetWidth();
637 tabs_rect
.width
-= m_tab_scroll_left_button_rect
.GetWidth() + m_tab_scroll_right_button_rect
.GetWidth();
640 for(i
= 0; i
< numtabs
; ++i
)
642 wxRibbonPageTabInfo
& info
= m_pages
.Item(i
);
644 dc
.DestroyClippingRegion();
645 if(m_tab_scroll_buttons_shown
)
647 if(!tabs_rect
.Intersects(info
.rect
))
649 dc
.SetClippingRegion(tabs_rect
);
651 dc
.SetClippingRegion(info
.rect
);
652 m_art
->DrawTab(dc
, this, info
);
654 if(info
.rect
.width
< info
.small_begin_need_separator_width
)
657 if(info
.rect
.width
< info
.small_must_have_separator_width
)
659 sep_visibility
+= 1.0;
663 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
);
669 wxRect rect
= m_pages
.Item(0).rect
;
670 rect
.width
= m_art
->GetMetric(wxRIBBON_ART_TAB_SEPARATION_SIZE
);
671 sep_visibility
/= (double)numtabs
;
672 for(i
= 0; i
< numtabs
- 1; ++i
)
674 wxRibbonPageTabInfo
& info
= m_pages
.Item(i
);
675 rect
.x
= info
.rect
.x
+ info
.rect
.width
;
677 if(m_tab_scroll_buttons_shown
&& !tabs_rect
.Intersects(rect
))
682 dc
.DestroyClippingRegion();
683 dc
.SetClippingRegion(rect
);
684 m_art
->DrawTabSeparator(dc
, this, rect
, sep_visibility
);
687 if(m_tab_scroll_buttons_shown
)
689 dc
.DestroyClippingRegion();
690 if(m_tab_scroll_left_button_rect
.GetWidth() != 0)
692 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
);
694 if(m_tab_scroll_right_button_rect
.GetWidth() != 0)
696 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
);
701 void wxRibbonBar::OnEraseBackground(wxEraseEvent
& WXUNUSED(evt
))
703 // Background painting done in main paint handler to reduce screen flicker
706 void wxRibbonBar::DoEraseBackground(wxDC
& dc
)
708 wxRect
tabs(GetSize());
709 tabs
.height
= m_tab_height
;
710 m_art
->DrawTabCtrlBackground(dc
, this, tabs
);
713 void wxRibbonBar::OnSize(wxSizeEvent
& evt
)
715 RecalculateTabSizes();
716 if(m_current_page
!= -1)
718 RepositionPage(m_pages
.Item(m_current_page
).page
);
725 void wxRibbonBar::RepositionPage(wxRibbonPage
*page
)
729 page
->SetSizeWithScrollButtonAdjustment(0, m_tab_height
, w
, h
- m_tab_height
);
732 wxRibbonPageTabInfo
* wxRibbonBar::HitTestTabs(wxPoint position
, int* index
)
734 wxRect
tabs_rect(m_tab_margin_left
, 0, GetClientSize().GetWidth() - m_tab_margin_left
- m_tab_margin_right
, m_tab_height
);
735 if(m_tab_scroll_buttons_shown
)
737 tabs_rect
.SetX(tabs_rect
.GetX() + m_tab_scroll_left_button_rect
.GetWidth());
738 tabs_rect
.SetWidth(tabs_rect
.GetWidth() - m_tab_scroll_left_button_rect
.GetWidth() - m_tab_scroll_right_button_rect
.GetWidth());
740 if(tabs_rect
.Contains(position
))
742 size_t numtabs
= m_pages
.GetCount();
744 for(i
= 0; i
< numtabs
; ++i
)
746 wxRibbonPageTabInfo
& info
= m_pages
.Item(i
);
747 if(info
.rect
.Contains(position
))
764 void wxRibbonBar::OnMouseLeftDown(wxMouseEvent
& evt
)
766 wxRibbonPageTabInfo
*tab
= HitTestTabs(evt
.GetPosition());
767 if(tab
&& tab
!= &m_pages
.Item(m_current_page
))
769 wxRibbonBarEvent
query(wxEVT_COMMAND_RIBBONBAR_PAGE_CHANGING
, GetId(), tab
->page
);
770 query
.SetEventObject(this);
771 ProcessWindowEvent(query
);
772 if(query
.IsAllowed())
774 SetActivePage(query
.GetPage());
776 wxRibbonBarEvent
notification(wxEVT_COMMAND_RIBBONBAR_PAGE_CHANGED
, GetId(), m_pages
.Item(m_current_page
).page
);
777 notification
.SetEventObject(this);
778 ProcessWindowEvent(notification
);
783 if(m_tab_scroll_left_button_rect
.Contains(evt
.GetPosition()))
785 m_tab_scroll_left_button_state
|= wxRIBBON_SCROLL_BTN_ACTIVE
| wxRIBBON_SCROLL_BTN_HOVERED
;
788 else if(m_tab_scroll_right_button_rect
.Contains(evt
.GetPosition()))
790 m_tab_scroll_right_button_state
|= wxRIBBON_SCROLL_BTN_ACTIVE
| wxRIBBON_SCROLL_BTN_HOVERED
;
796 void wxRibbonBar::OnMouseLeftUp(wxMouseEvent
& WXUNUSED(evt
))
798 if(!m_tab_scroll_buttons_shown
)
804 if(m_tab_scroll_left_button_state
& wxRIBBON_SCROLL_BTN_ACTIVE
)
808 else if(m_tab_scroll_right_button_state
& wxRIBBON_SCROLL_BTN_ACTIVE
)
814 m_tab_scroll_left_button_state
&= ~wxRIBBON_SCROLL_BTN_ACTIVE
;
815 m_tab_scroll_right_button_state
&= ~wxRIBBON_SCROLL_BTN_ACTIVE
;
816 ScrollTabBar(amount
* 8);
820 void wxRibbonBar::ScrollTabBar(int amount
)
822 bool show_left
= true;
823 bool show_right
= true;
824 if(m_tab_scroll_amount
+ amount
<= 0)
826 amount
= -m_tab_scroll_amount
;
829 else if(m_tab_scroll_amount
+ amount
+ (GetClientSize().GetWidth() - m_tab_margin_left
- m_tab_margin_right
) >= m_tabs_total_width_minimum
)
831 amount
= m_tabs_total_width_minimum
- m_tab_scroll_amount
- (GetClientSize().GetWidth() - m_tab_margin_left
- m_tab_margin_right
);
838 m_tab_scroll_amount
+= amount
;
839 size_t numtabs
= m_pages
.GetCount();
841 for(i
= 0; i
< numtabs
; ++i
)
843 wxRibbonPageTabInfo
& info
= m_pages
.Item(i
);
844 info
.rect
.SetX(info
.rect
.GetX() - amount
);
846 if(show_right
!= (m_tab_scroll_right_button_rect
.GetWidth() != 0) ||
847 show_left
!= (m_tab_scroll_left_button_rect
.GetWidth() != 0))
849 wxClientDC
temp_dc(this);
852 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());
856 m_tab_scroll_left_button_rect
.SetWidth(0);
861 if(m_tab_scroll_right_button_rect
.GetWidth() == 0)
863 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());
864 m_tab_scroll_right_button_rect
.SetX(m_tab_scroll_right_button_rect
.GetX() - m_tab_scroll_right_button_rect
.GetWidth());
869 if(m_tab_scroll_right_button_rect
.GetWidth() != 0)
871 m_tab_scroll_right_button_rect
.SetX(m_tab_scroll_right_button_rect
.GetX() + m_tab_scroll_right_button_rect
.GetWidth());
872 m_tab_scroll_right_button_rect
.SetWidth(0);
880 void wxRibbonBar::RefreshTabBar()
882 wxRect
tab_rect(0, 0, GetClientSize().GetWidth(), m_tab_height
);
883 Refresh(false, &tab_rect
);
886 void wxRibbonBar::OnMouseMiddleDown(wxMouseEvent
& evt
)
888 DoMouseButtonCommon(evt
, wxEVT_COMMAND_RIBBONBAR_TAB_MIDDLE_DOWN
);
891 void wxRibbonBar::OnMouseMiddleUp(wxMouseEvent
& evt
)
893 DoMouseButtonCommon(evt
, wxEVT_COMMAND_RIBBONBAR_TAB_MIDDLE_UP
);
896 void wxRibbonBar::OnMouseRightDown(wxMouseEvent
& evt
)
898 DoMouseButtonCommon(evt
, wxEVT_COMMAND_RIBBONBAR_TAB_RIGHT_DOWN
);
901 void wxRibbonBar::OnMouseRightUp(wxMouseEvent
& evt
)
903 DoMouseButtonCommon(evt
, wxEVT_COMMAND_RIBBONBAR_TAB_RIGHT_UP
);
906 void wxRibbonBar::DoMouseButtonCommon(wxMouseEvent
& evt
, wxEventType tab_event_type
)
908 wxRibbonPageTabInfo
*tab
= HitTestTabs(evt
.GetPosition());
911 wxRibbonBarEvent
notification(tab_event_type
, GetId(), tab
->page
);
912 notification
.SetEventObject(this);
913 ProcessWindowEvent(notification
);
917 void wxRibbonBar::RecalculateMinSize()
919 wxSize
min_size(wxDefaultCoord
, wxDefaultCoord
);
920 size_t numtabs
= m_pages
.GetCount();
923 min_size
= m_pages
.Item(0).page
->GetMinSize();
926 for(i
= 1; i
< numtabs
; ++i
)
928 wxRibbonPageTabInfo
& info
= m_pages
.Item(i
);
929 wxSize page_min
= info
.page
->GetMinSize();
931 min_size
.x
= wxMax(min_size
.x
, page_min
.x
);
932 min_size
.y
= wxMax(min_size
.y
, page_min
.y
);
935 if(min_size
.y
!= wxDefaultCoord
)
937 // TODO: Decide on best course of action when min height is unspecified
938 // - should we specify it to the tab minimum, or leave it unspecified?
939 min_size
.IncBy(0, m_tab_height
);
942 m_minWidth
= min_size
.GetWidth();
943 m_minHeight
= min_size
.GetHeight();
946 wxSize
wxRibbonBar::DoGetBestSize() const
949 if(m_current_page
!= -1)
951 best
= m_pages
.Item(m_current_page
).page
->GetBestSize();
953 if(best
.GetHeight() == wxDefaultCoord
)
955 best
.SetHeight(m_tab_height
);
959 best
.IncBy(0, m_tab_height
);
964 #endif // wxUSE_RIBBON