]> git.saurik.com Git - wxWidgets.git/blob - src/ribbon/bar.cpp
Add a few missing appearance screenshots for the manual.
[wxWidgets.git] / src / ribbon / bar.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/ribbon/bar.cpp
3 // Purpose: Top-level component of the ribbon-bar-style interface
4 // Author: Peter Cawley
5 // Modified by:
6 // Created: 2009-05-23
7 // Copyright: (C) Peter Cawley
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 #include "wx/wxprec.h"
12
13 #ifdef __BORLANDC__
14 #pragma hdrstop
15 #endif
16
17 #if wxUSE_RIBBON
18
19 #include "wx/ribbon/bar.h"
20 #include "wx/ribbon/art.h"
21 #include "wx/dcbuffer.h"
22 #include "wx/app.h"
23 #include "wx/vector.h"
24
25 #ifndef WX_PRECOMP
26 #endif
27
28 #ifdef __WXMSW__
29 #include "wx/msw/private.h"
30 #endif
31
32 #include "wx/arrimpl.cpp"
33
34 WX_DEFINE_USER_EXPORTED_OBJARRAY(wxRibbonPageTabInfoArray)
35
36 wxDEFINE_EVENT(wxEVT_RIBBONBAR_PAGE_CHANGED, wxRibbonBarEvent);
37 wxDEFINE_EVENT(wxEVT_RIBBONBAR_PAGE_CHANGING, wxRibbonBarEvent);
38 wxDEFINE_EVENT(wxEVT_RIBBONBAR_TAB_MIDDLE_DOWN, wxRibbonBarEvent);
39 wxDEFINE_EVENT(wxEVT_RIBBONBAR_TAB_MIDDLE_UP, wxRibbonBarEvent);
40 wxDEFINE_EVENT(wxEVT_RIBBONBAR_TAB_RIGHT_DOWN, wxRibbonBarEvent);
41 wxDEFINE_EVENT(wxEVT_RIBBONBAR_TAB_RIGHT_UP, wxRibbonBarEvent);
42 wxDEFINE_EVENT(wxEVT_RIBBONBAR_TAB_LEFT_DCLICK, wxRibbonBarEvent);
43 wxDEFINE_EVENT(wxEVT_RIBBONBAR_TOGGLED, wxRibbonBarEvent);
44 wxDEFINE_EVENT(wxEVT_RIBBONBAR_HELP_CLICK, wxRibbonBarEvent);
45
46 IMPLEMENT_CLASS(wxRibbonBar, wxRibbonControl)
47 IMPLEMENT_DYNAMIC_CLASS(wxRibbonBarEvent, wxNotifyEvent)
48
49 BEGIN_EVENT_TABLE(wxRibbonBar, wxRibbonControl)
50 EVT_ERASE_BACKGROUND(wxRibbonBar::OnEraseBackground)
51 EVT_LEAVE_WINDOW(wxRibbonBar::OnMouseLeave)
52 EVT_LEFT_DOWN(wxRibbonBar::OnMouseLeftDown)
53 EVT_LEFT_UP(wxRibbonBar::OnMouseLeftUp)
54 EVT_MIDDLE_DOWN(wxRibbonBar::OnMouseMiddleDown)
55 EVT_MIDDLE_UP(wxRibbonBar::OnMouseMiddleUp)
56 EVT_MOTION(wxRibbonBar::OnMouseMove)
57 EVT_PAINT(wxRibbonBar::OnPaint)
58 EVT_RIGHT_DOWN(wxRibbonBar::OnMouseRightDown)
59 EVT_RIGHT_UP(wxRibbonBar::OnMouseRightUp)
60 EVT_LEFT_DCLICK(wxRibbonBar::OnMouseDoubleClick)
61 EVT_SIZE(wxRibbonBar::OnSize)
62 EVT_KILL_FOCUS(wxRibbonBar::OnKillFocus)
63 END_EVENT_TABLE()
64
65 void wxRibbonBar::AddPage(wxRibbonPage *page)
66 {
67 wxRibbonPageTabInfo info;
68
69 info.page = page;
70 info.active = false;
71 info.hovered = false;
72 info.highlight = false;
73 info.shown = true;
74 // info.rect not set (intentional)
75
76 wxClientDC dcTemp(this);
77 wxString label = wxEmptyString;
78 if(m_flags & wxRIBBON_BAR_SHOW_PAGE_LABELS)
79 label = page->GetLabel();
80 wxBitmap icon = wxNullBitmap;
81 if(m_flags & wxRIBBON_BAR_SHOW_PAGE_ICONS)
82 icon = page->GetIcon();
83 m_art->GetBarTabWidth(dcTemp, this, label, icon,
84 &info.ideal_width,
85 &info.small_begin_need_separator_width,
86 &info.small_must_have_separator_width,
87 &info.minimum_width);
88
89 if(m_pages.IsEmpty())
90 {
91 m_tabs_total_width_ideal = info.ideal_width;
92 m_tabs_total_width_minimum = info.minimum_width;
93 }
94 else
95 {
96 int sep = m_art->GetMetric(wxRIBBON_ART_TAB_SEPARATION_SIZE);
97 m_tabs_total_width_ideal += sep + info.ideal_width;
98 m_tabs_total_width_minimum += sep + info.minimum_width;
99 }
100 m_pages.Add(info);
101
102 page->Hide(); // Most likely case is that this new page is not the active tab
103 page->SetArtProvider(m_art);
104
105 if(m_pages.GetCount() == 1)
106 {
107 SetActivePage((size_t)0);
108 }
109 }
110
111 bool wxRibbonBar::DismissExpandedPanel()
112 {
113 if(m_current_page == -1)
114 return false;
115 return m_pages.Item(m_current_page).page->DismissExpandedPanel();
116 }
117
118 void wxRibbonBar::ShowPanels(bool show)
119 {
120 m_arePanelsShown = show;
121 SetMinSize(wxSize(GetSize().GetWidth(), DoGetBestSize().GetHeight()));
122 Realise();
123 GetParent()->Layout();
124 }
125
126 void wxRibbonBar::SetWindowStyleFlag(long style)
127 {
128 m_flags = style;
129 if(m_art)
130 m_art->SetFlags(style);
131 }
132
133 long wxRibbonBar::GetWindowStyleFlag() const
134 {
135 return m_flags;
136 }
137
138 bool wxRibbonBar::Realize()
139 {
140 bool status = true;
141
142 wxClientDC dcTemp(this);
143 int sep = m_art->GetMetric(wxRIBBON_ART_TAB_SEPARATION_SIZE);
144 size_t numtabs = m_pages.GetCount();
145 size_t i;
146 for(i = 0; i < numtabs; ++i)
147 {
148 wxRibbonPageTabInfo& info = m_pages.Item(i);
149 if (!info.shown)
150 continue;
151 RepositionPage(info.page);
152 if(!info.page->Realize())
153 {
154 status = false;
155 }
156 wxString label = wxEmptyString;
157 if(m_flags & wxRIBBON_BAR_SHOW_PAGE_LABELS)
158 label = info.page->GetLabel();
159 wxBitmap icon = wxNullBitmap;
160 if(m_flags & wxRIBBON_BAR_SHOW_PAGE_ICONS)
161 icon = info.page->GetIcon();
162 m_art->GetBarTabWidth(dcTemp, this, label, icon,
163 &info.ideal_width,
164 &info.small_begin_need_separator_width,
165 &info.small_must_have_separator_width,
166 &info.minimum_width);
167
168 if(i == 0)
169 {
170 m_tabs_total_width_ideal = info.ideal_width;
171 m_tabs_total_width_minimum = info.minimum_width;
172 }
173 else
174 {
175 m_tabs_total_width_ideal += sep + info.ideal_width;
176 m_tabs_total_width_minimum += sep + info.minimum_width;
177 }
178 }
179 m_tab_height = m_art->GetTabCtrlHeight(dcTemp, this, m_pages);
180
181 RecalculateMinSize();
182 RecalculateTabSizes();
183 Refresh();
184
185 return status;
186 }
187
188 void wxRibbonBar::OnMouseMove(wxMouseEvent& evt)
189 {
190 int x = evt.GetX();
191 int y = evt.GetY();
192 int hovered_page = -1;
193 bool refresh_tabs = false;
194 if(y < m_tab_height)
195 {
196 // It is quite likely that the mouse moved a small amount and is still over the same tab
197 if(m_current_hovered_page != -1 && m_pages.Item((size_t)m_current_hovered_page).rect.Contains(x, y))
198 {
199 hovered_page = m_current_hovered_page;
200 // But be careful, if tabs can be scrolled, then parts of the tab rect may not be valid
201 if(m_tab_scroll_buttons_shown)
202 {
203 if(x >= m_tab_scroll_right_button_rect.GetX() || x < m_tab_scroll_left_button_rect.GetRight())
204 {
205 hovered_page = -1;
206 }
207 }
208 }
209 else
210 {
211 HitTestTabs(evt.GetPosition(), &hovered_page);
212 }
213 }
214 if(hovered_page != m_current_hovered_page)
215 {
216 if(m_current_hovered_page != -1)
217 {
218 m_pages.Item((int)m_current_hovered_page).hovered = false;
219 }
220 m_current_hovered_page = hovered_page;
221 if(m_current_hovered_page != -1)
222 {
223 m_pages.Item((int)m_current_hovered_page).hovered = true;
224 }
225 refresh_tabs = true;
226 }
227 if(m_tab_scroll_buttons_shown)
228 {
229 #define SET_FLAG(variable, flag) \
230 { if(((variable) & (flag)) != (flag)) { variable |= (flag); refresh_tabs = true; }}
231 #define UNSET_FLAG(variable, flag) \
232 { if((variable) & (flag)) { variable &= ~(flag); refresh_tabs = true; }}
233
234 if(m_tab_scroll_left_button_rect.Contains(x, y))
235 SET_FLAG(m_tab_scroll_left_button_state, wxRIBBON_SCROLL_BTN_HOVERED)
236 else
237 UNSET_FLAG(m_tab_scroll_left_button_state, wxRIBBON_SCROLL_BTN_HOVERED)
238
239 if(m_tab_scroll_right_button_rect.Contains(x, y))
240 SET_FLAG(m_tab_scroll_right_button_state, wxRIBBON_SCROLL_BTN_HOVERED)
241 else
242 UNSET_FLAG(m_tab_scroll_right_button_state, wxRIBBON_SCROLL_BTN_HOVERED)
243 #undef SET_FLAG
244 #undef UNSET_FLAG
245 }
246 if(refresh_tabs)
247 {
248 RefreshTabBar();
249 }
250 if ( m_flags & wxRIBBON_BAR_SHOW_TOGGLE_BUTTON )
251 HitTestRibbonButton(m_toggle_button_rect, evt.GetPosition(), m_toggle_button_hovered);
252 if ( m_flags & wxRIBBON_BAR_SHOW_HELP_BUTTON )
253 HitTestRibbonButton(m_help_button_rect, evt.GetPosition(), m_help_button_hovered);
254 }
255
256 void wxRibbonBar::OnMouseLeave(wxMouseEvent& WXUNUSED(evt))
257 {
258 // The ribbon bar is (usually) at the top of a window, and at least on MSW, the mouse
259 // can leave the window quickly and leave a tab in the hovered state.
260 bool refresh_tabs = false;
261 if(m_current_hovered_page != -1)
262 {
263 m_pages.Item((int)m_current_hovered_page).hovered = false;
264 m_current_hovered_page = -1;
265 refresh_tabs = true;
266 }
267 if(m_tab_scroll_left_button_state & wxRIBBON_SCROLL_BTN_HOVERED)
268 {
269 m_tab_scroll_left_button_state &= ~wxRIBBON_SCROLL_BTN_HOVERED;
270 refresh_tabs = true;
271 }
272 if(m_tab_scroll_right_button_state & wxRIBBON_SCROLL_BTN_HOVERED)
273 {
274 m_tab_scroll_right_button_state &= ~wxRIBBON_SCROLL_BTN_HOVERED;
275 refresh_tabs = true;
276 }
277 if(refresh_tabs)
278 {
279 RefreshTabBar();
280 }
281 if(m_toggle_button_hovered)
282 {
283 m_bar_hovered = false;
284 m_toggle_button_hovered = false;
285 Refresh(false);
286 }
287 if ( m_help_button_hovered )
288 {
289 m_help_button_hovered = false;
290 m_bar_hovered = false;
291 Refresh(false);
292 }
293 }
294
295 wxRibbonPage* wxRibbonBar::GetPage(int n)
296 {
297 if(n < 0 || (size_t)n >= m_pages.GetCount())
298 return 0;
299 return m_pages.Item(n).page;
300 }
301
302 size_t wxRibbonBar::GetPageCount() const
303 {
304 return m_pages.GetCount();
305 }
306
307 bool wxRibbonBar::IsPageShown(size_t page) const
308 {
309 if (page >= m_pages.GetCount())
310 return false;
311 return m_pages.Item(page).shown;
312 }
313
314 void wxRibbonBar::ShowPage(size_t page, bool show)
315 {
316 if(page >= m_pages.GetCount())
317 return;
318 m_pages.Item(page).shown = show;
319 }
320
321 bool wxRibbonBar::IsPageHighlighted(size_t page) const
322 {
323 if (page >= m_pages.GetCount())
324 return false;
325 return m_pages.Item(page).highlight;
326 }
327
328 void wxRibbonBar::AddPageHighlight(size_t page, bool highlight)
329 {
330 if(page >= m_pages.GetCount())
331 return;
332 m_pages.Item(page).highlight = highlight;
333 }
334
335 void wxRibbonBar::DeletePage(size_t n)
336 {
337 if(n < m_pages.GetCount())
338 {
339 wxRibbonPage *page = m_pages.Item(n).page;
340
341 // Schedule page object for destruction and not destroying directly
342 // as this function can be called in an event handler and page functions
343 // can be called afeter removing.
344 // Like in wxRibbonButtonBar::OnMouseUp
345 if(!wxTheApp->IsScheduledForDestruction(page))
346 {
347 wxTheApp->ScheduleForDestruction(page);
348 }
349
350 m_pages.RemoveAt(n);
351
352 if(m_current_page == static_cast<int>(n))
353 {
354 m_current_page = -1;
355
356 if(m_pages.GetCount() > 0)
357 {
358 if(n >= m_pages.GetCount())
359 {
360 SetActivePage(m_pages.GetCount() - 1);
361 }
362 else
363 {
364 SetActivePage(n - 1);
365 }
366 }
367 }
368 else if(m_current_page > static_cast<int>(n))
369 {
370 m_current_page--;
371 }
372 }
373 }
374
375 void wxRibbonBar::ClearPages()
376 {
377 size_t i;
378 for(i=0; i<m_pages.GetCount(); i++)
379 {
380 wxRibbonPage *page = m_pages.Item(i).page;
381 // Schedule page object for destruction and not destroying directly
382 // as this function can be called in an event handler and page functions
383 // can be called afeter removing.
384 // Like in wxRibbonButtonBar::OnMouseUp
385 if(!wxTheApp->IsScheduledForDestruction(page))
386 {
387 wxTheApp->ScheduleForDestruction(page);
388 }
389 }
390 m_pages.Empty();
391 Realize();
392 m_current_page = -1;
393 Refresh();
394 }
395
396 bool wxRibbonBar::SetActivePage(size_t page)
397 {
398 if(m_current_page == (int)page)
399 {
400 return true;
401 }
402
403 if(page >= m_pages.GetCount())
404 {
405 return false;
406 }
407
408 if(m_current_page != -1)
409 {
410 m_pages.Item((size_t)m_current_page).active = false;
411 m_pages.Item((size_t)m_current_page).page->Hide();
412 }
413 m_current_page = (int)page;
414 m_pages.Item(page).active = true;
415 m_pages.Item(page).shown = true;
416 {
417 wxRibbonPage* wnd = m_pages.Item(page).page;
418 RepositionPage(wnd);
419 wnd->Layout();
420 wnd->Show();
421 }
422 Refresh();
423
424 return true;
425 }
426
427 bool wxRibbonBar::SetActivePage(wxRibbonPage* page)
428 {
429 size_t numpages = m_pages.GetCount();
430 size_t i;
431 for(i = 0; i < numpages; ++i)
432 {
433 if(m_pages.Item(i).page == page)
434 {
435 return SetActivePage(i);
436 }
437 }
438 return false;
439 }
440
441 int wxRibbonBar::GetPageNumber(wxRibbonPage* page) const
442 {
443 size_t numpages = m_pages.GetCount();
444 for(size_t i = 0; i < numpages; ++i)
445 {
446 if(m_pages.Item(i).page == page)
447 {
448 return i;
449 }
450 }
451 return wxNOT_FOUND;
452 }
453
454
455 int wxRibbonBar::GetActivePage() const
456 {
457 return m_current_page;
458 }
459
460 void wxRibbonBar::SetTabCtrlMargins(int left, int right)
461 {
462 m_tab_margin_left = left;
463 m_tab_margin_right = right;
464
465 RecalculateTabSizes();
466 }
467
468 struct PageComparedBySmallWidthAsc
469 {
470 wxEXPLICIT PageComparedBySmallWidthAsc(wxRibbonPageTabInfo* page)
471 : m_page(page)
472 {
473 }
474
475 bool operator<(const PageComparedBySmallWidthAsc& other) const
476 {
477 return m_page->small_must_have_separator_width
478 < other.m_page->small_must_have_separator_width;
479 }
480
481 wxRibbonPageTabInfo *m_page;
482 };
483
484 void wxRibbonBar::RecalculateTabSizes()
485 {
486 size_t numtabs = m_pages.GetCount();
487
488 if(numtabs == 0)
489 return;
490
491 int width = GetSize().GetWidth() - m_tab_margin_left - m_tab_margin_right;
492 int tabsep = m_art->GetMetric(wxRIBBON_ART_TAB_SEPARATION_SIZE);
493 int x = m_tab_margin_left;
494 const int y = 0;
495
496 if(width >= m_tabs_total_width_ideal)
497 {
498 // Simple case: everything at ideal width
499 size_t i;
500 for(i = 0; i < numtabs; ++i)
501 {
502 wxRibbonPageTabInfo& info = m_pages.Item(i);
503 if (!info.shown)
504 continue;
505 info.rect.x = x;
506 info.rect.y = y;
507 info.rect.width = info.ideal_width;
508 info.rect.height = m_tab_height;
509 x += info.rect.width + tabsep;
510 }
511 m_tab_scroll_buttons_shown = false;
512 m_tab_scroll_left_button_rect.SetWidth(0);
513 m_tab_scroll_right_button_rect.SetWidth(0);
514 }
515 else if(width < m_tabs_total_width_minimum)
516 {
517 // Simple case: everything minimum with scrollbar
518 size_t i;
519 for(i = 0; i < numtabs; ++i)
520 {
521 wxRibbonPageTabInfo& info = m_pages.Item(i);
522 if (!info.shown)
523 continue;
524 info.rect.x = x;
525 info.rect.y = y;
526 info.rect.width = info.minimum_width;
527 info.rect.height = m_tab_height;
528 x += info.rect.width + tabsep;
529 }
530 if(!m_tab_scroll_buttons_shown)
531 {
532 m_tab_scroll_left_button_state = wxRIBBON_SCROLL_BTN_NORMAL;
533 m_tab_scroll_right_button_state = wxRIBBON_SCROLL_BTN_NORMAL;
534 m_tab_scroll_buttons_shown = true;
535 }
536 {
537 wxClientDC temp_dc(this);
538 int right_button_pos = GetClientSize().GetWidth() - m_tab_margin_right - m_tab_scroll_right_button_rect.GetWidth();
539 if ( right_button_pos < m_tab_margin_left )
540 right_button_pos = m_tab_margin_left;
541
542 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());
543 m_tab_scroll_left_button_rect.SetHeight(m_tab_height);
544 m_tab_scroll_left_button_rect.SetX(m_tab_margin_left);
545 m_tab_scroll_left_button_rect.SetY(0);
546 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());
547 m_tab_scroll_right_button_rect.SetHeight(m_tab_height);
548 m_tab_scroll_right_button_rect.SetX(right_button_pos);
549 m_tab_scroll_right_button_rect.SetY(0);
550 }
551 if(m_tab_scroll_amount == 0)
552 {
553 m_tab_scroll_left_button_rect.SetWidth(0);
554 }
555 else if(m_tab_scroll_amount + width >= m_tabs_total_width_minimum)
556 {
557 m_tab_scroll_amount = m_tabs_total_width_minimum - width;
558 m_tab_scroll_right_button_rect.SetX(m_tab_scroll_right_button_rect.GetX() + m_tab_scroll_right_button_rect.GetWidth());
559 m_tab_scroll_right_button_rect.SetWidth(0);
560 }
561 for(i = 0; i < numtabs; ++i)
562 {
563 wxRibbonPageTabInfo& info = m_pages.Item(i);
564 if (!info.shown)
565 continue;
566 info.rect.x -= m_tab_scroll_amount;
567 }
568 }
569 else
570 {
571 m_tab_scroll_buttons_shown = false;
572 m_tab_scroll_left_button_rect.SetWidth(0);
573 m_tab_scroll_right_button_rect.SetWidth(0);
574 // Complex case: everything sized such that: minimum <= width < ideal
575 /*
576 Strategy:
577 1) Uniformly reduce all tab widths from ideal to small_must_have_separator_width
578 2) Reduce the largest tab by 1 pixel, repeating until all tabs are same width (or at minimum)
579 3) Uniformly reduce all tabs down to their minimum width
580 */
581 int smallest_tab_width = INT_MAX;
582 int total_small_width = tabsep * (numtabs - 1);
583 size_t i;
584 for(i = 0; i < numtabs; ++i)
585 {
586 wxRibbonPageTabInfo& info = m_pages.Item(i);
587 if (!info.shown)
588 continue;
589 if(info.small_must_have_separator_width < smallest_tab_width)
590 {
591 smallest_tab_width = info.small_must_have_separator_width;
592 }
593 total_small_width += info.small_must_have_separator_width;
594 }
595 if(width >= total_small_width)
596 {
597 // Do (1)
598 int total_delta = m_tabs_total_width_ideal - total_small_width;
599 total_small_width -= tabsep * (numtabs - 1);
600 width -= tabsep * (numtabs - 1);
601 for(i = 0; i < numtabs; ++i)
602 {
603 wxRibbonPageTabInfo& info = m_pages.Item(i);
604 if (!info.shown)
605 continue;
606 int delta = info.ideal_width - info.small_must_have_separator_width;
607 info.rect.x = x;
608 info.rect.y = y;
609 info.rect.width = info.small_must_have_separator_width + delta * (width - total_small_width) / total_delta;
610 info.rect.height = m_tab_height;
611
612 x += info.rect.width + tabsep;
613 total_delta -= delta;
614 total_small_width -= info.small_must_have_separator_width;
615 width -= info.rect.width;
616 }
617 }
618 else
619 {
620 total_small_width = tabsep * (numtabs - 1);
621 for(i = 0; i < numtabs; ++i)
622 {
623 wxRibbonPageTabInfo& info = m_pages.Item(i);
624 if (!info.shown)
625 continue;
626 if(info.minimum_width < smallest_tab_width)
627 {
628 total_small_width += smallest_tab_width;
629 }
630 else
631 {
632 total_small_width += info.minimum_width;
633 }
634 }
635 if(width >= total_small_width)
636 {
637 // Do (2)
638 wxVector<PageComparedBySmallWidthAsc> sorted_pages;
639 sorted_pages.reserve(numtabs);
640 for ( i = 0; i < numtabs; ++i )
641 sorted_pages.push_back(PageComparedBySmallWidthAsc(&m_pages.Item(i)));
642
643 wxVectorSort(sorted_pages);
644 width -= tabsep * (numtabs - 1);
645 for(i = 0; i < numtabs; ++i)
646 {
647 wxRibbonPageTabInfo* info = sorted_pages[i].m_page;
648 if (!info->shown)
649 continue;
650 if(info->small_must_have_separator_width * (int)(numtabs - i) <= width)
651 {
652 info->rect.width = info->small_must_have_separator_width;;
653 }
654 else
655 {
656 info->rect.width = width / (numtabs - i);
657 }
658 width -= info->rect.width;
659 }
660 for(i = 0; i < numtabs; ++i)
661 {
662 wxRibbonPageTabInfo& info = m_pages.Item(i);
663 if (!info.shown)
664 continue;
665 info.rect.x = x;
666 info.rect.y = y;
667 info.rect.height = m_tab_height;
668 x += info.rect.width + tabsep;
669 }
670 }
671 else
672 {
673 // Do (3)
674 total_small_width = (smallest_tab_width + tabsep) * numtabs - tabsep;
675 int total_delta = total_small_width - m_tabs_total_width_minimum;
676 total_small_width = m_tabs_total_width_minimum - tabsep * (numtabs - 1);
677 width -= tabsep * (numtabs - 1);
678 for(i = 0; i < numtabs; ++i)
679 {
680 wxRibbonPageTabInfo& info = m_pages.Item(i);
681 if (!info.shown)
682 continue;
683 int delta = smallest_tab_width - info.minimum_width;
684 info.rect.x = x;
685 info.rect.y = y;
686 info.rect.width = info.minimum_width + delta * (width - total_small_width) / total_delta;
687 info.rect.height = m_tab_height;
688
689 x += info.rect.width + tabsep;
690 total_delta -= delta;
691 total_small_width -= info.minimum_width;
692 width -= info.rect.width;
693 }
694 }
695 }
696 }
697 }
698
699 wxRibbonBar::wxRibbonBar()
700 {
701 m_flags = 0;
702 m_tabs_total_width_ideal = 0;
703 m_tabs_total_width_minimum = 0;
704 m_tab_margin_left = 0;
705 m_tab_margin_right = 0;
706 m_tab_height = 0;
707 m_tab_scroll_amount = 0;
708 m_current_page = -1;
709 m_current_hovered_page = -1;
710 m_tab_scroll_left_button_state = wxRIBBON_SCROLL_BTN_NORMAL;
711 m_tab_scroll_right_button_state = wxRIBBON_SCROLL_BTN_NORMAL;
712 m_tab_scroll_buttons_shown = false;
713 m_arePanelsShown = true;
714 m_help_button_hovered = false;
715 }
716
717 wxRibbonBar::wxRibbonBar(wxWindow* parent,
718 wxWindowID id,
719 const wxPoint& pos,
720 const wxSize& size,
721 long style)
722 : wxRibbonControl(parent, id, pos, size, wxBORDER_NONE)
723 {
724 CommonInit(style);
725 }
726
727 wxRibbonBar::~wxRibbonBar()
728 {
729 SetArtProvider(NULL);
730 }
731
732 bool wxRibbonBar::Create(wxWindow* parent,
733 wxWindowID id,
734 const wxPoint& pos,
735 const wxSize& size,
736 long style)
737 {
738 if(!wxRibbonControl::Create(parent, id, pos, size, wxBORDER_NONE))
739 return false;
740
741 CommonInit(style);
742
743 return true;
744 }
745
746 void wxRibbonBar::CommonInit(long style)
747 {
748 SetName(wxT("wxRibbonBar"));
749
750 m_flags = style;
751 m_tabs_total_width_ideal = 0;
752 m_tabs_total_width_minimum = 0;
753 m_tab_margin_left = 50;
754 m_tab_margin_right = 20;
755 if ( m_flags & wxRIBBON_BAR_SHOW_TOGGLE_BUTTON )
756 m_tab_margin_right += 20;
757 if ( m_flags & wxRIBBON_BAR_SHOW_HELP_BUTTON )
758 m_tab_margin_right += 20;
759 m_tab_height = 20; // initial guess
760 m_tab_scroll_amount = 0;
761 m_current_page = -1;
762 m_current_hovered_page = -1;
763 m_tab_scroll_left_button_state = wxRIBBON_SCROLL_BTN_NORMAL;
764 m_tab_scroll_right_button_state = wxRIBBON_SCROLL_BTN_NORMAL;
765 m_tab_scroll_buttons_shown = false;
766 m_arePanelsShown = true;
767
768 if(m_art == NULL)
769 {
770 SetArtProvider(new wxRibbonDefaultArtProvider);
771 }
772 SetBackgroundStyle(wxBG_STYLE_CUSTOM);
773
774 m_toggle_button_hovered = false;
775 m_bar_hovered = false;
776
777 m_ribbon_state = wxRIBBON_BAR_PINNED;
778 }
779
780 void wxRibbonBar::SetArtProvider(wxRibbonArtProvider* art)
781 {
782 wxRibbonArtProvider *old = m_art;
783 m_art = art;
784
785 if(art)
786 {
787 art->SetFlags(m_flags);
788 }
789 size_t numpages = m_pages.GetCount();
790 size_t i;
791 for(i = 0; i < numpages; ++i)
792 {
793 wxRibbonPage *page = m_pages.Item(i).page;
794 if(page->GetArtProvider() != art)
795 {
796 page->SetArtProvider(art);
797 }
798 }
799
800 delete old;
801 }
802
803 void wxRibbonBar::OnPaint(wxPaintEvent& WXUNUSED(evt))
804 {
805 wxAutoBufferedPaintDC dc(this);
806
807 if(GetUpdateRegion().Contains(0, 0, GetClientSize().GetWidth(), m_tab_height) == wxOutRegion)
808 {
809 // Nothing to do in the tab area, and the page area is handled by the active page
810 return;
811 }
812
813 DoEraseBackground(dc);
814
815 if ( m_flags & wxRIBBON_BAR_SHOW_HELP_BUTTON )
816 m_help_button_rect = m_art->GetRibbonHelpButtonArea(GetSize());
817 if ( m_flags & wxRIBBON_BAR_SHOW_TOGGLE_BUTTON )
818 m_toggle_button_rect = m_art->GetBarToggleButtonArea(GetSize());
819
820 size_t numtabs = m_pages.GetCount();
821 double sep_visibility = 0.0;
822 bool draw_sep = false;
823 wxRect tabs_rect(m_tab_margin_left, 0, GetClientSize().GetWidth() - m_tab_margin_left - m_tab_margin_right, m_tab_height);
824 if(m_tab_scroll_buttons_shown)
825 {
826 tabs_rect.x += m_tab_scroll_left_button_rect.GetWidth();
827 tabs_rect.width -= m_tab_scroll_left_button_rect.GetWidth() + m_tab_scroll_right_button_rect.GetWidth();
828 }
829 size_t i;
830 for(i = 0; i < numtabs; ++i)
831 {
832 wxRibbonPageTabInfo& info = m_pages.Item(i);
833 if (!info.shown)
834 continue;
835
836 dc.DestroyClippingRegion();
837 if(m_tab_scroll_buttons_shown)
838 {
839 if(!tabs_rect.Intersects(info.rect))
840 continue;
841 dc.SetClippingRegion(tabs_rect);
842 }
843 dc.SetClippingRegion(info.rect);
844 m_art->DrawTab(dc, this, info);
845
846 if(info.rect.width < info.small_begin_need_separator_width)
847 {
848 draw_sep = true;
849 if(info.rect.width < info.small_must_have_separator_width)
850 {
851 sep_visibility += 1.0;
852 }
853 else
854 {
855 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);
856 }
857 }
858 }
859 if(draw_sep)
860 {
861 wxRect rect = m_pages.Item(0).rect;
862 rect.width = m_art->GetMetric(wxRIBBON_ART_TAB_SEPARATION_SIZE);
863 sep_visibility /= (double)numtabs;
864 for(i = 0; i < numtabs - 1; ++i)
865 {
866 wxRibbonPageTabInfo& info = m_pages.Item(i);
867 if (!info.shown)
868 continue;
869 rect.x = info.rect.x + info.rect.width;
870
871 if(m_tab_scroll_buttons_shown && !tabs_rect.Intersects(rect))
872 {
873 continue;
874 }
875
876 dc.DestroyClippingRegion();
877 dc.SetClippingRegion(rect);
878 m_art->DrawTabSeparator(dc, this, rect, sep_visibility);
879 }
880 }
881 if(m_tab_scroll_buttons_shown)
882 {
883 if(m_tab_scroll_left_button_rect.GetWidth() != 0)
884 {
885 dc.DestroyClippingRegion();
886 dc.SetClippingRegion(m_tab_scroll_left_button_rect);
887 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);
888 }
889 if(m_tab_scroll_right_button_rect.GetWidth() != 0)
890 {
891 dc.DestroyClippingRegion();
892 dc.SetClippingRegion(m_tab_scroll_right_button_rect);
893 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);
894 }
895 }
896
897 if ( m_flags & wxRIBBON_BAR_SHOW_HELP_BUTTON )
898 m_art->DrawHelpButton(dc, this, m_help_button_rect);
899 if ( m_flags & wxRIBBON_BAR_SHOW_TOGGLE_BUTTON )
900 m_art->DrawToggleButton(dc, this, m_toggle_button_rect, m_ribbon_state);
901
902 }
903
904 void wxRibbonBar::OnEraseBackground(wxEraseEvent& WXUNUSED(evt))
905 {
906 // Background painting done in main paint handler to reduce screen flicker
907 }
908
909 void wxRibbonBar::DoEraseBackground(wxDC& dc)
910 {
911 wxRect tabs(GetSize());
912 tabs.height = m_tab_height;
913 m_art->DrawTabCtrlBackground(dc, this, tabs);
914 }
915
916 void wxRibbonBar::OnSize(wxSizeEvent& evt)
917 {
918 RecalculateTabSizes();
919 if(m_current_page != -1)
920 {
921 RepositionPage(m_pages.Item(m_current_page).page);
922 }
923 RefreshTabBar();
924
925 evt.Skip();
926 }
927
928 void wxRibbonBar::RepositionPage(wxRibbonPage *page)
929 {
930 int w, h;
931 GetSize(&w, &h);
932 page->SetSizeWithScrollButtonAdjustment(0, m_tab_height, w, h - m_tab_height);
933 }
934
935 wxRibbonPageTabInfo* wxRibbonBar::HitTestTabs(wxPoint position, int* index)
936 {
937 wxRect tabs_rect(m_tab_margin_left, 0, GetClientSize().GetWidth() - m_tab_margin_left - m_tab_margin_right, m_tab_height);
938 if(m_tab_scroll_buttons_shown)
939 {
940 tabs_rect.SetX(tabs_rect.GetX() + m_tab_scroll_left_button_rect.GetWidth());
941 tabs_rect.SetWidth(tabs_rect.GetWidth() - m_tab_scroll_left_button_rect.GetWidth() - m_tab_scroll_right_button_rect.GetWidth());
942 }
943 if(tabs_rect.Contains(position))
944 {
945 size_t numtabs = m_pages.GetCount();
946 size_t i;
947 for(i = 0; i < numtabs; ++i)
948 {
949 wxRibbonPageTabInfo& info = m_pages.Item(i);
950 if (!info.shown)
951 continue;
952 if(info.rect.Contains(position))
953 {
954 if(index != NULL)
955 {
956 *index = (int)i;
957 }
958 return &info;
959 }
960 }
961 }
962 if(index != NULL)
963 {
964 *index = -1;
965 }
966 return NULL;
967 }
968
969 void wxRibbonBar::OnMouseLeftDown(wxMouseEvent& evt)
970 {
971 wxRibbonPageTabInfo *tab = HitTestTabs(evt.GetPosition());
972 SetFocus();
973 if ( tab )
974 {
975 if ( m_ribbon_state == wxRIBBON_BAR_MINIMIZED )
976 {
977 ShowPanels();
978 m_ribbon_state = wxRIBBON_BAR_EXPANDED;
979 }
980 else if ( (tab == &m_pages.Item(m_current_page)) && (m_ribbon_state == wxRIBBON_BAR_EXPANDED) )
981 {
982 HidePanels();
983 m_ribbon_state = wxRIBBON_BAR_MINIMIZED;
984 }
985 }
986 else
987 {
988 if ( m_ribbon_state == wxRIBBON_BAR_EXPANDED )
989 {
990 HidePanels();
991 m_ribbon_state = wxRIBBON_BAR_MINIMIZED;
992 }
993 }
994 if(tab && tab != &m_pages.Item(m_current_page))
995 {
996 wxRibbonBarEvent query(wxEVT_RIBBONBAR_PAGE_CHANGING, GetId(), tab->page);
997 query.SetEventObject(this);
998 ProcessWindowEvent(query);
999 if(query.IsAllowed())
1000 {
1001 SetActivePage(query.GetPage());
1002
1003 wxRibbonBarEvent notification(wxEVT_RIBBONBAR_PAGE_CHANGED, GetId(), m_pages.Item(m_current_page).page);
1004 notification.SetEventObject(this);
1005 ProcessWindowEvent(notification);
1006 }
1007 }
1008 else if(tab == NULL)
1009 {
1010 if(m_tab_scroll_left_button_rect.Contains(evt.GetPosition()))
1011 {
1012 m_tab_scroll_left_button_state |= wxRIBBON_SCROLL_BTN_ACTIVE | wxRIBBON_SCROLL_BTN_HOVERED;
1013 RefreshTabBar();
1014 }
1015 else if(m_tab_scroll_right_button_rect.Contains(evt.GetPosition()))
1016 {
1017 m_tab_scroll_right_button_state |= wxRIBBON_SCROLL_BTN_ACTIVE | wxRIBBON_SCROLL_BTN_HOVERED;
1018 RefreshTabBar();
1019 }
1020 }
1021
1022 wxPoint position = evt.GetPosition();
1023
1024 if(position.x >= 0 && position.y >= 0)
1025 {
1026 wxSize size = GetSize();
1027 if(position.x < size.GetWidth() && position.y < size.GetHeight())
1028 {
1029 if(m_toggle_button_rect.Contains(position))
1030 {
1031 bool pshown = ArePanelsShown();
1032 ShowPanels(!pshown);
1033 if ( pshown )
1034 m_ribbon_state = wxRIBBON_BAR_MINIMIZED;
1035 else
1036 m_ribbon_state = wxRIBBON_BAR_PINNED;
1037 wxRibbonBarEvent event(wxEVT_RIBBONBAR_TOGGLED, GetId());
1038 event.SetEventObject(this);
1039 ProcessWindowEvent(event);
1040 }
1041 if ( m_help_button_rect.Contains(position) )
1042 {
1043 wxRibbonBarEvent event(wxEVT_RIBBONBAR_HELP_CLICK, GetId());
1044 event.SetEventObject(this);
1045 ProcessWindowEvent(event);
1046 }
1047 }
1048 }
1049 }
1050
1051 void wxRibbonBar::OnMouseLeftUp(wxMouseEvent& WXUNUSED(evt))
1052 {
1053 if(!m_tab_scroll_buttons_shown)
1054 {
1055 return;
1056 }
1057
1058 int amount = 0;
1059 if(m_tab_scroll_left_button_state & wxRIBBON_SCROLL_BTN_ACTIVE)
1060 {
1061 amount = -1;
1062 }
1063 else if(m_tab_scroll_right_button_state & wxRIBBON_SCROLL_BTN_ACTIVE)
1064 {
1065 amount = 1;
1066 }
1067 if(amount != 0)
1068 {
1069 m_tab_scroll_left_button_state &= ~wxRIBBON_SCROLL_BTN_ACTIVE;
1070 m_tab_scroll_right_button_state &= ~wxRIBBON_SCROLL_BTN_ACTIVE;
1071 ScrollTabBar(amount * 8);
1072 }
1073 }
1074
1075 void wxRibbonBar::ScrollTabBar(int amount)
1076 {
1077 bool show_left = true;
1078 bool show_right = true;
1079 if(m_tab_scroll_amount + amount <= 0)
1080 {
1081 amount = -m_tab_scroll_amount;
1082 show_left = false;
1083 }
1084 else if(m_tab_scroll_amount + amount + (GetClientSize().GetWidth() - m_tab_margin_left - m_tab_margin_right) >= m_tabs_total_width_minimum)
1085 {
1086 amount = m_tabs_total_width_minimum - m_tab_scroll_amount - (GetClientSize().GetWidth() - m_tab_margin_left - m_tab_margin_right);
1087 show_right = false;
1088 }
1089 if(amount == 0)
1090 {
1091 return;
1092 }
1093 m_tab_scroll_amount += amount;
1094 size_t numtabs = m_pages.GetCount();
1095 size_t i;
1096 for(i = 0; i < numtabs; ++i)
1097 {
1098 wxRibbonPageTabInfo& info = m_pages.Item(i);
1099 if (!info.shown)
1100 continue;
1101 info.rect.SetX(info.rect.GetX() - amount);
1102 }
1103 if(show_right != (m_tab_scroll_right_button_rect.GetWidth() != 0) ||
1104 show_left != (m_tab_scroll_left_button_rect.GetWidth() != 0))
1105 {
1106 wxClientDC temp_dc(this);
1107 if(show_left)
1108 {
1109 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());
1110 }
1111 else
1112 {
1113 m_tab_scroll_left_button_rect.SetWidth(0);
1114 }
1115
1116 if(show_right)
1117 {
1118 if(m_tab_scroll_right_button_rect.GetWidth() == 0)
1119 {
1120 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());
1121 m_tab_scroll_right_button_rect.SetX(m_tab_scroll_right_button_rect.GetX() - m_tab_scroll_right_button_rect.GetWidth());
1122 }
1123 }
1124 else
1125 {
1126 if(m_tab_scroll_right_button_rect.GetWidth() != 0)
1127 {
1128 m_tab_scroll_right_button_rect.SetX(m_tab_scroll_right_button_rect.GetX() + m_tab_scroll_right_button_rect.GetWidth());
1129 m_tab_scroll_right_button_rect.SetWidth(0);
1130 }
1131 }
1132 }
1133
1134 RefreshTabBar();
1135 }
1136
1137 void wxRibbonBar::RefreshTabBar()
1138 {
1139 wxRect tab_rect(0, 0, GetClientSize().GetWidth(), m_tab_height);
1140 Refresh(false, &tab_rect);
1141 }
1142
1143 void wxRibbonBar::OnMouseMiddleDown(wxMouseEvent& evt)
1144 {
1145 DoMouseButtonCommon(evt, wxEVT_RIBBONBAR_TAB_MIDDLE_DOWN);
1146 }
1147
1148 void wxRibbonBar::OnMouseMiddleUp(wxMouseEvent& evt)
1149 {
1150 DoMouseButtonCommon(evt, wxEVT_RIBBONBAR_TAB_MIDDLE_UP);
1151 }
1152
1153 void wxRibbonBar::OnMouseRightDown(wxMouseEvent& evt)
1154 {
1155 DoMouseButtonCommon(evt, wxEVT_RIBBONBAR_TAB_RIGHT_DOWN);
1156 }
1157
1158 void wxRibbonBar::OnMouseRightUp(wxMouseEvent& evt)
1159 {
1160 DoMouseButtonCommon(evt, wxEVT_RIBBONBAR_TAB_RIGHT_UP);
1161 }
1162
1163 void wxRibbonBar::OnMouseDoubleClick(wxMouseEvent& evt)
1164 {
1165 wxRibbonPageTabInfo *tab = HitTestTabs(evt.GetPosition());
1166 SetFocus();
1167 if ( tab && tab == &m_pages.Item(m_current_page) )
1168 {
1169 if ( m_ribbon_state == wxRIBBON_BAR_PINNED )
1170 {
1171 m_ribbon_state = wxRIBBON_BAR_MINIMIZED;
1172 HidePanels();
1173 }
1174 else
1175 {
1176 m_ribbon_state = wxRIBBON_BAR_PINNED;
1177 ShowPanels();
1178 }
1179 }
1180 }
1181
1182 void wxRibbonBar::DoMouseButtonCommon(wxMouseEvent& evt, wxEventType tab_event_type)
1183 {
1184 wxRibbonPageTabInfo *tab = HitTestTabs(evt.GetPosition());
1185 if(tab)
1186 {
1187 wxRibbonBarEvent notification(tab_event_type, GetId(), tab->page);
1188 notification.SetEventObject(this);
1189 ProcessWindowEvent(notification);
1190 }
1191 }
1192
1193 void wxRibbonBar::RecalculateMinSize()
1194 {
1195 wxSize min_size(wxDefaultCoord, wxDefaultCoord);
1196 size_t numtabs = m_pages.GetCount();
1197 if(numtabs != 0)
1198 {
1199 min_size = m_pages.Item(0).page->GetMinSize();
1200
1201 size_t i;
1202 for(i = 1; i < numtabs; ++i)
1203 {
1204 wxRibbonPageTabInfo& info = m_pages.Item(i);
1205 if (!info.shown)
1206 continue;
1207 wxSize page_min = info.page->GetMinSize();
1208
1209 min_size.x = wxMax(min_size.x, page_min.x);
1210 min_size.y = wxMax(min_size.y, page_min.y);
1211 }
1212 }
1213 if(min_size.y != wxDefaultCoord)
1214 {
1215 // TODO: Decide on best course of action when min height is unspecified
1216 // - should we specify it to the tab minimum, or leave it unspecified?
1217 min_size.IncBy(0, m_tab_height);
1218 }
1219
1220 m_minWidth = min_size.GetWidth();
1221 m_minHeight = m_arePanelsShown ? min_size.GetHeight() : m_tab_height;
1222 }
1223
1224 wxSize wxRibbonBar::DoGetBestSize() const
1225 {
1226 wxSize best(0, 0);
1227 if(m_current_page != -1)
1228 {
1229 best = m_pages.Item(m_current_page).page->GetBestSize();
1230 }
1231 if(best.GetHeight() == wxDefaultCoord)
1232 {
1233 best.SetHeight(m_tab_height);
1234 }
1235 else
1236 {
1237 best.IncBy(0, m_tab_height);
1238 }
1239 if(!m_arePanelsShown)
1240 {
1241 best.SetHeight(m_tab_height);
1242 }
1243 return best;
1244 }
1245
1246 void wxRibbonBar::HitTestRibbonButton(const wxRect& rect, const wxPoint& position, bool &hover_flag)
1247 {
1248 bool hovered = false, toggle_button_hovered = false;
1249 if(position.x >= 0 && position.y >= 0)
1250 {
1251 wxSize size = GetSize();
1252 if(position.x < size.GetWidth() && position.y < size.GetHeight())
1253 {
1254 hovered = true;
1255 }
1256 }
1257 if(hovered)
1258 {
1259 toggle_button_hovered = rect.Contains(position);
1260
1261 if ( hovered != m_bar_hovered || toggle_button_hovered != hover_flag )
1262 {
1263 m_bar_hovered = hovered;
1264 hover_flag = toggle_button_hovered;
1265 Refresh(false);
1266 }
1267 }
1268 }
1269
1270 void wxRibbonBar::HideIfExpanded()
1271 {
1272 if ( m_ribbon_state == wxRIBBON_BAR_EXPANDED )
1273 {
1274 HidePanels();
1275 m_ribbon_state = wxRIBBON_BAR_MINIMIZED;
1276 }
1277 else
1278 {
1279 ShowPanels();
1280 m_ribbon_state = wxRIBBON_BAR_PINNED;
1281 }
1282 }
1283
1284 void wxRibbonBar::OnKillFocus(wxFocusEvent& WXUNUSED(evt))
1285 {
1286 HideIfExpanded();
1287 }
1288
1289 #endif // wxUSE_RIBBON