move everything inside wxUSE_RIBBON wrapper
[wxWidgets.git] / src / ribbon / page.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/ribbon/page.cpp
3 // Purpose: Container for ribbon-bar-style interface panels
4 // Author: Peter Cawley
5 // Modified by:
6 // Created: 2009-05-25
7 // RCS-ID: $Id$
8 // Copyright: (C) Peter Cawley
9 // Licence: wxWindows licence
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/page.h"
20 #include "wx/ribbon/art.h"
21 #include "wx/ribbon/bar.h"
22 #include "wx/dcbuffer.h"
23
24 #ifndef WX_PRECOMP
25 #endif
26
27 #ifdef __WXMSW__
28 #include "wx/msw/private.h"
29 #endif
30
31 static int GetSizeInOrientation(wxSize size, wxOrientation orientation);
32
33 // As scroll buttons need to be rendered on top of a page's child windows, the
34 // buttons themselves have to be proper child windows (rather than just painted
35 // onto the page). In order to get proper clipping of a page's children (with
36 // regard to the scroll button), the scroll buttons are created as children of
37 // the ribbon bar rather than children of the page. This could not have been
38 // achieved by creating buttons as children of the page and then doing some Z-order
39 // manipulation, as this causes problems on win32 due to ribbon panels having the
40 // transparent flag set.
41 class wxRibbonPageScrollButton : public wxRibbonControl
42 {
43 public:
44 wxRibbonPageScrollButton(wxRibbonPage* sibling,
45 wxWindowID id = wxID_ANY,
46 const wxPoint& pos = wxDefaultPosition,
47 const wxSize& size = wxDefaultSize,
48 long style = 0);
49
50 virtual ~wxRibbonPageScrollButton();
51
52 protected:
53 virtual wxBorder GetDefaultBorder() const { return wxBORDER_NONE; }
54
55 void OnEraseBackground(wxEraseEvent& evt);
56 void OnPaint(wxPaintEvent& evt);
57 void OnMouseEnter(wxMouseEvent& evt);
58 void OnMouseLeave(wxMouseEvent& evt);
59 void OnMouseDown(wxMouseEvent& evt);
60 void OnMouseUp(wxMouseEvent& evt);
61
62 wxRibbonPage* m_sibling;
63 long m_flags;
64
65 DECLARE_CLASS(wxRibbonPageScrollButton)
66 DECLARE_EVENT_TABLE()
67 };
68
69 IMPLEMENT_CLASS(wxRibbonPageScrollButton, wxRibbonControl)
70
71 BEGIN_EVENT_TABLE(wxRibbonPageScrollButton, wxRibbonControl)
72 EVT_ENTER_WINDOW(wxRibbonPageScrollButton::OnMouseEnter)
73 EVT_ERASE_BACKGROUND(wxRibbonPageScrollButton::OnEraseBackground)
74 EVT_LEAVE_WINDOW(wxRibbonPageScrollButton::OnMouseLeave)
75 EVT_LEFT_DOWN(wxRibbonPageScrollButton::OnMouseDown)
76 EVT_LEFT_UP(wxRibbonPageScrollButton::OnMouseUp)
77 EVT_PAINT(wxRibbonPageScrollButton::OnPaint)
78 END_EVENT_TABLE()
79
80 wxRibbonPageScrollButton::wxRibbonPageScrollButton(wxRibbonPage* sibling,
81 wxWindowID id,
82 const wxPoint& pos,
83 const wxSize& size,
84 long style) : wxRibbonControl(sibling->GetParent(), id, pos, size, wxBORDER_NONE)
85 {
86 SetBackgroundStyle(wxBG_STYLE_CUSTOM);
87 m_sibling = sibling;
88 m_flags = (style & wxRIBBON_SCROLL_BTN_DIRECTION_MASK) | wxRIBBON_SCROLL_BTN_FOR_PAGE;
89 }
90
91 wxRibbonPageScrollButton::~wxRibbonPageScrollButton()
92 {
93 }
94
95 void wxRibbonPageScrollButton::OnEraseBackground(wxEraseEvent& WXUNUSED(evt))
96 {
97 // Do nothing - all painting done in main paint handler
98 }
99
100 void wxRibbonPageScrollButton::OnPaint(wxPaintEvent& WXUNUSED(evt))
101 {
102 wxAutoBufferedPaintDC dc(this);
103 if(m_art)
104 {
105 m_art->DrawScrollButton(dc, this, GetSize(), m_flags);
106 }
107 }
108
109 void wxRibbonPageScrollButton::OnMouseEnter(wxMouseEvent& WXUNUSED(evt))
110 {
111 m_flags |= wxRIBBON_SCROLL_BTN_HOVERED;
112 Refresh(false);
113 }
114
115 void wxRibbonPageScrollButton::OnMouseLeave(wxMouseEvent& WXUNUSED(evt))
116 {
117 m_flags &= ~wxRIBBON_SCROLL_BTN_HOVERED;
118 m_flags &= ~wxRIBBON_SCROLL_BTN_ACTIVE;
119 Refresh(false);
120 }
121
122 void wxRibbonPageScrollButton::OnMouseDown(wxMouseEvent& WXUNUSED(evt))
123 {
124 m_flags |= wxRIBBON_SCROLL_BTN_ACTIVE;
125 Refresh(false);
126 }
127
128 void wxRibbonPageScrollButton::OnMouseUp(wxMouseEvent& WXUNUSED(evt))
129 {
130 if(m_flags & wxRIBBON_SCROLL_BTN_ACTIVE)
131 {
132 m_flags &= ~wxRIBBON_SCROLL_BTN_ACTIVE;
133 Refresh(false);
134 switch(m_flags & wxRIBBON_SCROLL_BTN_DIRECTION_MASK)
135 {
136 case wxRIBBON_SCROLL_BTN_DOWN:
137 case wxRIBBON_SCROLL_BTN_RIGHT:
138 m_sibling->ScrollLines(1);
139 break;
140 case wxRIBBON_SCROLL_BTN_UP:
141 case wxRIBBON_SCROLL_BTN_LEFT:
142 m_sibling->ScrollLines(-1);
143 break;
144 default:
145 break;
146 }
147 }
148 }
149
150 IMPLEMENT_CLASS(wxRibbonPage, wxRibbonControl)
151
152 BEGIN_EVENT_TABLE(wxRibbonPage, wxRibbonControl)
153 EVT_ERASE_BACKGROUND(wxRibbonPage::OnEraseBackground)
154 EVT_PAINT(wxRibbonPage::OnPaint)
155 EVT_SIZE(wxRibbonPage::OnSize)
156 END_EVENT_TABLE()
157
158 wxRibbonPage::wxRibbonPage()
159 {
160 m_scroll_left_btn = NULL;
161 m_scroll_right_btn = NULL;
162 m_scroll_amount = 0;
163 m_scroll_buttons_visible = false;
164 }
165
166 wxRibbonPage::wxRibbonPage(wxRibbonBar* parent,
167 wxWindowID id,
168 const wxString& label,
169 const wxBitmap& icon,
170 long WXUNUSED(style))
171 : wxRibbonControl(parent, id, wxDefaultPosition, wxDefaultSize, wxBORDER_NONE)
172 {
173 CommonInit(label, icon);
174 }
175
176 wxRibbonPage::~wxRibbonPage()
177 {
178 delete[] m_size_calc_array;
179 }
180
181 bool wxRibbonPage::Create(wxRibbonBar* parent,
182 wxWindowID id,
183 const wxString& label,
184 const wxBitmap& icon,
185 long WXUNUSED(style))
186 {
187 if(!wxRibbonControl::Create(parent, id, wxDefaultPosition, wxDefaultSize, wxBORDER_NONE))
188 return false;
189
190 CommonInit(label, icon);
191
192 return true;
193 }
194
195 void wxRibbonPage::CommonInit(const wxString& label, const wxBitmap& icon)
196 {
197 SetName(label);
198
199 SetLabel(label);
200 m_old_size = wxSize(0, 0);
201 m_icon = icon;
202 m_scroll_left_btn = NULL;
203 m_scroll_right_btn = NULL;
204 m_size_calc_array = NULL;
205 m_size_calc_array_size = 0;
206 m_scroll_amount = 0;
207 m_scroll_buttons_visible = false;
208
209 SetBackgroundStyle(wxBG_STYLE_CUSTOM);
210
211 wxDynamicCast(GetParent(), wxRibbonBar)->AddPage(this);
212 }
213
214 void wxRibbonPage::SetArtProvider(wxRibbonArtProvider* art)
215 {
216 m_art = art;
217 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
218 node;
219 node = node->GetNext() )
220 {
221 wxWindow* child = node->GetData();
222 wxRibbonControl* ribbon_child = wxDynamicCast(child, wxRibbonControl);
223 if(ribbon_child)
224 {
225 ribbon_child->SetArtProvider(art);
226 }
227 }
228 }
229
230 void wxRibbonPage::AdjustRectToIncludeScrollButtons(wxRect* rect) const
231 {
232 if(m_scroll_buttons_visible)
233 {
234 if(GetMajorAxis() == wxVERTICAL)
235 {
236 if(m_scroll_left_btn)
237 {
238 rect->SetY(rect->GetY() -
239 m_scroll_left_btn->GetSize().GetHeight());
240 rect->SetHeight(rect->GetHeight() +
241 m_scroll_left_btn->GetSize().GetHeight());
242 }
243 if(m_scroll_right_btn)
244 {
245 rect->SetHeight(rect->GetHeight() +
246 m_scroll_right_btn->GetSize().GetHeight());
247 }
248 }
249 else
250 {
251 if(m_scroll_left_btn)
252 {
253 rect->SetX(rect->GetX() -
254 m_scroll_left_btn->GetSize().GetWidth());
255 rect->SetWidth(rect->GetWidth() +
256 m_scroll_left_btn->GetSize().GetWidth());
257 }
258 if(m_scroll_right_btn)
259 {
260 rect->SetWidth(rect->GetWidth() +
261 m_scroll_right_btn->GetSize().GetWidth());
262 }
263 }
264 }
265 }
266
267 void wxRibbonPage::OnEraseBackground(wxEraseEvent& WXUNUSED(evt))
268 {
269 // All painting done in main paint handler to minimise flicker
270 }
271
272 void wxRibbonPage::OnPaint(wxPaintEvent& WXUNUSED(evt))
273 {
274 // No foreground painting done by the page itself, but a paint DC
275 // must be created anyway.
276 wxAutoBufferedPaintDC dc(this);
277 wxRect rect(GetSize());
278 AdjustRectToIncludeScrollButtons(&rect);
279 m_art->DrawPageBackground(dc, this, rect);
280 }
281
282 wxOrientation wxRibbonPage::GetMajorAxis() const
283 {
284 if(m_art && (m_art->GetFlags() & wxRIBBON_BAR_FLOW_VERTICAL))
285 {
286 return wxVERTICAL;
287 }
288 else
289 {
290 return wxHORIZONTAL;
291 }
292 }
293
294 bool wxRibbonPage::ScrollLines(int lines)
295 {
296 return ScrollPixels(lines * 8);
297 }
298
299 bool wxRibbonPage::ScrollPixels(int pixels)
300 {
301 if(pixels < 0)
302 {
303 if(m_scroll_amount == 0)
304 return false;
305 if(m_scroll_amount < -pixels)
306 pixels = -m_scroll_amount;
307 }
308 else if(pixels > 0)
309 {
310 if(m_scroll_amount == m_scroll_amount_limit)
311 return false;
312 if(m_scroll_amount + pixels > m_scroll_amount_limit)
313 pixels = m_scroll_amount_limit - m_scroll_amount;
314 }
315 else
316 return false;
317
318 m_scroll_amount += pixels;
319
320 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
321 node;
322 node = node->GetNext() )
323 {
324 wxWindow* child = node->GetData();
325 int x, y;
326 child->GetPosition(&x, &y);
327 if(GetMajorAxis() == wxHORIZONTAL)
328 x -= pixels;
329 else
330 y -= pixels;
331 child->SetPosition(wxPoint(x, y));
332 }
333
334 ShowScrollButtons();
335 Refresh();
336 return true;
337 }
338
339 void wxRibbonPage::SetSizeWithScrollButtonAdjustment(int x, int y, int width, int height)
340 {
341 if(m_scroll_buttons_visible)
342 {
343 if(GetMajorAxis() == wxHORIZONTAL)
344 {
345 if(m_scroll_left_btn)
346 {
347 int w = m_scroll_left_btn->GetSize().GetWidth();
348 m_scroll_left_btn->SetPosition(wxPoint(x, y));
349 x += w;
350 width -= w;
351 }
352 if(m_scroll_right_btn)
353 {
354 int w = m_scroll_right_btn->GetSize().GetWidth();
355 width -= w;
356 m_scroll_right_btn->SetPosition(wxPoint(x + width, y));
357 }
358 }
359 else
360 {
361 if(m_scroll_left_btn)
362 {
363 int h = m_scroll_left_btn->GetSize().GetHeight();
364 m_scroll_left_btn->SetPosition(wxPoint(x, y));
365 y += h;
366 height -= h;
367 }
368 if(m_scroll_right_btn)
369 {
370 int h = m_scroll_right_btn->GetSize().GetHeight();
371 height -= h;
372 m_scroll_right_btn->SetPosition(wxPoint(x, y + height));
373 }
374 }
375 }
376 SetSize(x, y, width, height);
377 }
378
379 void wxRibbonPage::DoSetSize(int x, int y, int width, int height, int sizeFlags)
380 {
381 // When a resize triggers the scroll buttons to become visible, the page is resized.
382 // This resize from within a resize event can cause (MSW) wxWidgets some confusion,
383 // and report the 1st size to the 2nd size event. Hence the most recent size is
384 // remembered internally and used in Layout() where appropiate.
385
386 if(GetMajorAxis() == wxHORIZONTAL)
387 {
388 m_size_in_major_axis_for_children = width;
389 if(m_scroll_buttons_visible)
390 {
391 if(m_scroll_left_btn)
392 m_size_in_major_axis_for_children += m_scroll_left_btn->GetSize().GetWidth();
393 if(m_scroll_right_btn)
394 m_size_in_major_axis_for_children += m_scroll_right_btn->GetSize().GetWidth();
395 }
396 }
397 else
398 {
399 m_size_in_major_axis_for_children = height;
400 if(m_scroll_buttons_visible)
401 {
402 if(m_scroll_left_btn)
403 m_size_in_major_axis_for_children += m_scroll_left_btn->GetSize().GetHeight();
404 if(m_scroll_right_btn)
405 m_size_in_major_axis_for_children += m_scroll_right_btn->GetSize().GetHeight();
406 }
407 }
408
409 wxRibbonControl::DoSetSize(x, y, width, height, sizeFlags);
410 }
411
412 void wxRibbonPage::OnSize(wxSizeEvent& evt)
413 {
414 wxSize new_size = evt.GetSize();
415
416 wxMemoryDC temp_dc;
417 wxRect invalid_rect = m_art->GetPageBackgroundRedrawArea(temp_dc, this, m_old_size, new_size);
418 Refresh(true, &invalid_rect);
419
420 m_old_size = new_size;
421
422 if(new_size.GetX() > 0 && new_size.GetY() > 0)
423 {
424 Layout();
425 }
426 else
427 {
428 // Simplify other calculations by pretending new size is zero in both
429 // X and Y
430 new_size.Set(0, 0);
431 // When size == 0, no point in doing any layout
432 }
433
434 evt.Skip();
435 }
436
437 void wxRibbonPage::RemoveChild(wxWindowBase *child)
438 {
439 // Remove all references to the child from the collapse stack
440 size_t count = m_collapse_stack.GetCount();
441 size_t src, dst;
442 for(src = 0, dst = 0; src < count; ++src, ++dst)
443 {
444 wxRibbonControl *item = m_collapse_stack.Item(src);
445 if(item == child)
446 {
447 ++src;
448 if(src == count)
449 {
450 break;
451 }
452 }
453 if(src != dst)
454 {
455 m_collapse_stack.Item(dst) = item;
456 }
457 }
458 if(src > dst)
459 {
460 m_collapse_stack.RemoveAt(dst, src - dst);
461 }
462
463 // ... and then proceed as normal
464 wxRibbonControl::RemoveChild(child);
465 }
466
467 bool wxRibbonPage::Realize()
468 {
469 bool status = true;
470
471 m_collapse_stack.Clear();
472 for (wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
473 node;
474 node = node->GetNext())
475 {
476 wxRibbonControl* child = wxDynamicCast(node->GetData(), wxRibbonControl);
477 if(child == NULL)
478 {
479 continue;
480 }
481 if(!child->Realize())
482 {
483 status = false;
484 }
485 }
486 PopulateSizeCalcArray(&wxWindow::GetMinSize);
487
488 return DoActualLayout() && status;
489 }
490
491 void wxRibbonPage::PopulateSizeCalcArray(wxSize (wxWindow::*get_size)(void) const)
492 {
493 if(m_size_calc_array_size != GetChildren().GetCount())
494 {
495 delete[] m_size_calc_array;
496 m_size_calc_array_size = GetChildren().GetCount();
497 m_size_calc_array = new wxSize[m_size_calc_array_size];
498 }
499 wxSize* node_size = m_size_calc_array;
500 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
501 node;
502 node = node->GetNext(), ++node_size )
503 {
504 wxWindow* child = node->GetData();
505 *node_size = (child->*get_size)();
506 }
507 }
508
509 bool wxRibbonPage::Layout()
510 {
511 if(GetChildren().GetCount() == 0)
512 {
513 return true;
514 }
515 else
516 {
517 PopulateSizeCalcArray(&wxWindow::GetSize);
518 return DoActualLayout();
519 }
520 }
521
522 bool wxRibbonPage::DoActualLayout()
523 {
524 wxPoint origin(m_art->GetMetric(wxRIBBON_ART_PAGE_BORDER_LEFT_SIZE), m_art->GetMetric(wxRIBBON_ART_PAGE_BORDER_TOP_SIZE));
525 wxOrientation major_axis = GetMajorAxis();
526 int gap;
527 int minor_axis_size;
528 int available_space;
529 if(major_axis == wxHORIZONTAL)
530 {
531 gap = m_art->GetMetric(wxRIBBON_ART_PANEL_X_SEPARATION_SIZE);
532 minor_axis_size = GetSize().GetHeight() - origin.y - m_art->GetMetric(wxRIBBON_ART_PAGE_BORDER_BOTTOM_SIZE);
533 available_space = m_size_in_major_axis_for_children - m_art->GetMetric(wxRIBBON_ART_PAGE_BORDER_RIGHT_SIZE) - origin.x;
534 }
535 else
536 {
537 gap = m_art->GetMetric(wxRIBBON_ART_PANEL_Y_SEPARATION_SIZE);
538 minor_axis_size = GetSize().GetWidth() - origin.x - m_art->GetMetric(wxRIBBON_ART_PAGE_BORDER_RIGHT_SIZE);
539 available_space = m_size_in_major_axis_for_children - m_art->GetMetric(wxRIBBON_ART_PAGE_BORDER_BOTTOM_SIZE) - origin.y;
540 }
541 if (minor_axis_size < 0) minor_axis_size = 0;
542 size_t size_index;
543 for(size_index = 0; size_index < m_size_calc_array_size; ++size_index)
544 {
545 if(major_axis == wxHORIZONTAL)
546 {
547 available_space -= m_size_calc_array[size_index].GetWidth();
548 m_size_calc_array[size_index].SetHeight(minor_axis_size);
549 }
550 else
551 {
552 available_space -= m_size_calc_array[size_index].GetHeight();
553 m_size_calc_array[size_index].SetWidth(minor_axis_size);
554 }
555 if(size_index != 0)
556 available_space -= gap;
557 }
558 bool todo_hide_scroll_buttons = false;
559 bool todo_show_scroll_buttons = false;
560 if(available_space >= 0)
561 {
562 if(m_scroll_buttons_visible)
563 todo_hide_scroll_buttons = true;
564 if(available_space > 0)
565 ExpandPanels(major_axis, available_space);
566 }
567 else
568 {
569 if(m_scroll_buttons_visible)
570 {
571 // Scroll buttons already visible - not going to be able to downsize any more
572 m_scroll_amount_limit = -available_space;
573 if(m_scroll_amount > m_scroll_amount_limit)
574 {
575 m_scroll_amount = m_scroll_amount_limit;
576 todo_show_scroll_buttons = true;
577 }
578 }
579 else
580 {
581 if(!CollapsePanels(major_axis, -available_space))
582 {
583 m_scroll_amount = 0;
584 m_scroll_amount_limit = -available_space;
585 todo_show_scroll_buttons = true;
586 }
587 }
588 }
589 if(m_scroll_buttons_visible)
590 {
591 if(major_axis == wxHORIZONTAL)
592 {
593 origin.x -= m_scroll_amount;
594 if(m_scroll_left_btn)
595 origin.x -= m_scroll_left_btn->GetSize().GetWidth();
596 }
597 else
598 {
599 origin.y -= m_scroll_amount;
600 if(m_scroll_left_btn)
601 origin.y -= m_scroll_left_btn->GetSize().GetHeight();
602 }
603 }
604 size_index = 0;
605 for(wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
606 node;
607 node = node->GetNext(), ++size_index )
608 {
609 wxWindow* child = node->GetData();
610 int w = m_size_calc_array[size_index].GetWidth();
611 int h = m_size_calc_array[size_index].GetHeight();
612 child->SetSize(origin.x, origin.y, w, h);
613 if(major_axis == wxHORIZONTAL)
614 {
615 origin.x += w + gap;
616 }
617 else
618 {
619 origin.y += h + gap;
620 }
621 }
622
623 if(todo_show_scroll_buttons)
624 ShowScrollButtons();
625 else if(todo_hide_scroll_buttons)
626 HideScrollButtons();
627
628 Refresh();
629 return true;
630 }
631
632 bool wxRibbonPage::Show(bool show)
633 {
634 if(m_scroll_left_btn)
635 m_scroll_left_btn->Show(show);
636 if(m_scroll_right_btn)
637 m_scroll_right_btn->Show(show);
638 return wxRibbonControl::Show(show);
639 }
640
641 void wxRibbonPage::HideScrollButtons()
642 {
643 m_scroll_amount = 0;
644 m_scroll_amount_limit = 0;
645 ShowScrollButtons();
646 }
647
648 void wxRibbonPage::ShowScrollButtons()
649 {
650 bool show_left = true;
651 bool show_right = true;
652 bool reposition = false;
653 if(m_scroll_amount == 0)
654 {
655 show_left = false;
656 }
657 if(m_scroll_amount >= m_scroll_amount_limit)
658 {
659 show_right = false;
660 m_scroll_amount = m_scroll_amount_limit;
661 }
662 m_scroll_buttons_visible = show_left || show_right;
663
664 if(show_left)
665 {
666 if(m_scroll_left_btn == NULL)
667 {
668 wxMemoryDC temp_dc;
669 wxSize size;
670 long direction;
671 if(GetMajorAxis() == wxHORIZONTAL)
672 {
673 direction = wxRIBBON_SCROLL_BTN_LEFT;
674 size = m_art->GetScrollButtonMinimumSize(temp_dc, GetParent(), direction);
675 size.SetHeight(GetSize().GetHeight());
676 }
677 else
678 {
679 direction = wxRIBBON_SCROLL_BTN_UP;
680 size = m_art->GetScrollButtonMinimumSize(temp_dc, GetParent(), direction);
681 size.SetWidth(GetSize().GetWidth());
682 }
683 m_scroll_left_btn = new wxRibbonPageScrollButton(this, wxID_ANY, GetPosition(), size, direction);
684 if(!IsShown())
685 {
686 m_scroll_left_btn->Hide();
687 }
688 reposition = true;
689 }
690 }
691 else
692 {
693 if(m_scroll_left_btn != NULL)
694 {
695 m_scroll_left_btn->Destroy();
696 m_scroll_left_btn = NULL;
697 reposition = true;
698 }
699 }
700
701 if(show_right)
702 {
703 if(m_scroll_right_btn == NULL)
704 {
705 wxMemoryDC temp_dc;
706 wxSize size;
707 long direction;
708 if(GetMajorAxis() == wxHORIZONTAL)
709 {
710 direction = wxRIBBON_SCROLL_BTN_RIGHT;
711 size = m_art->GetScrollButtonMinimumSize(temp_dc, GetParent(), direction);
712 size.SetHeight(GetSize().GetHeight());
713 }
714 else
715 {
716 direction = wxRIBBON_SCROLL_BTN_DOWN;
717 size = m_art->GetScrollButtonMinimumSize(temp_dc, GetParent(), direction);
718 size.SetWidth(GetSize().GetWidth());
719 }
720 wxPoint initial_pos = GetPosition() + GetSize() - size;
721 m_scroll_right_btn = new wxRibbonPageScrollButton(this, wxID_ANY, initial_pos, size, direction);
722 if(!IsShown())
723 {
724 m_scroll_right_btn->Hide();
725 }
726 reposition = true;
727 }
728 }
729 else
730 {
731 if(m_scroll_right_btn != NULL)
732 {
733 m_scroll_right_btn->Destroy();
734 m_scroll_right_btn = NULL;
735 reposition = true;
736 }
737 }
738
739 if(reposition)
740 {
741 wxDynamicCast(GetParent(), wxRibbonBar)->RepositionPage(this);
742 }
743 }
744
745 static int GetSizeInOrientation(wxSize size, wxOrientation orientation)
746 {
747 switch(orientation)
748 {
749 case wxHORIZONTAL: return size.GetWidth();
750 case wxVERTICAL: return size.GetHeight();
751 case wxBOTH: return size.GetWidth() * size.GetHeight();
752 default: return 0;
753 }
754 }
755
756 bool wxRibbonPage::ExpandPanels(wxOrientation direction, int maximum_amount)
757 {
758 bool expanded_something = false;
759 while(maximum_amount > 0)
760 {
761 int smallest_size = INT_MAX;
762 wxRibbonPanel* smallest_panel = NULL;
763 wxSize* smallest_panel_size = NULL;
764 wxSize* panel_size = m_size_calc_array;
765 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
766 node;
767 node = node->GetNext(), ++panel_size )
768 {
769 wxRibbonPanel* panel = wxDynamicCast(node->GetData(), wxRibbonPanel);
770 if(panel == NULL)
771 {
772 continue;
773 }
774 if(panel->IsSizingContinuous())
775 {
776 int size = GetSizeInOrientation(*panel_size, direction);
777 if(size < smallest_size)
778 {
779 smallest_size = size;
780 smallest_panel = panel;
781 smallest_panel_size = panel_size;
782 }
783 }
784 else
785 {
786 int size = GetSizeInOrientation(*panel_size, direction);
787 if(size < smallest_size)
788 {
789 wxSize larger = panel->GetNextLargerSize(direction, *panel_size);
790 if(larger != (*panel_size) && GetSizeInOrientation(larger, direction) > size)
791 {
792 smallest_size = size;
793 smallest_panel = panel;
794 smallest_panel_size = panel_size;
795 }
796 }
797 }
798 }
799 if(smallest_panel != NULL)
800 {
801 if(smallest_panel->IsSizingContinuous())
802 {
803 int amount = maximum_amount;
804 if(amount > 32)
805 {
806 // For "large" growth, grow this panel a bit, and then re-allocate
807 // the remainder (which may come to this panel again anyway)
808 amount = 32;
809 }
810 if(direction & wxHORIZONTAL)
811 {
812 smallest_panel_size->x += amount;
813 }
814 if(direction & wxVERTICAL)
815 {
816 smallest_panel_size->y += amount;
817 }
818 maximum_amount -= amount;
819 m_collapse_stack.Add(smallest_panel);
820 expanded_something = true;
821 }
822 else
823 {
824 wxSize larger = smallest_panel->GetNextLargerSize(direction, *smallest_panel_size);
825 wxSize delta = larger - (*smallest_panel_size);
826 if(GetSizeInOrientation(delta, direction) <= maximum_amount)
827 {
828 *smallest_panel_size = larger;
829 maximum_amount -= GetSizeInOrientation(delta, direction);
830 m_collapse_stack.Add(smallest_panel);
831 expanded_something = true;
832 }
833 else
834 {
835 break;
836 }
837 }
838 }
839 else
840 {
841 break;
842 }
843 }
844 return expanded_something;
845 }
846
847 bool wxRibbonPage::CollapsePanels(wxOrientation direction, int minimum_amount)
848 {
849 bool collapsed_something = false;
850 while(minimum_amount > 0)
851 {
852 int largest_size = 0;
853 wxRibbonPanel* largest_panel = NULL;
854 wxSize* largest_panel_size = NULL;
855 wxSize* panel_size = m_size_calc_array;
856 if(!m_collapse_stack.IsEmpty())
857 {
858 // For a more consistent panel layout, try to collapse panels which
859 // were recently expanded.
860 largest_panel = wxDynamicCast(m_collapse_stack.Last(), wxRibbonPanel);
861 m_collapse_stack.RemoveAt(m_collapse_stack.GetCount() - 1);
862 for(wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
863 node;
864 node = node->GetNext(), ++panel_size )
865 {
866 wxRibbonPanel* panel = wxDynamicCast(node->GetData(), wxRibbonPanel);
867 if(panel == largest_panel)
868 {
869 largest_panel_size = panel_size;
870 break;
871 }
872 }
873 }
874 else
875 {
876 for(wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
877 node;
878 node = node->GetNext(), ++panel_size )
879 {
880 wxRibbonPanel* panel = wxDynamicCast(node->GetData(), wxRibbonPanel);
881 if(panel == NULL)
882 {
883 continue;
884 }
885 if(panel->IsSizingContinuous())
886 {
887 int size = GetSizeInOrientation(*panel_size, direction);
888 if(size > largest_size)
889 {
890 largest_size = size;
891 largest_panel = panel;
892 largest_panel_size = panel_size;
893 }
894 }
895 else
896 {
897 int size = GetSizeInOrientation(*panel_size, direction);
898 if(size > largest_size)
899 {
900 wxSize smaller = panel->GetNextSmallerSize(direction, *panel_size);
901 if(smaller != (*panel_size) &&
902 GetSizeInOrientation(smaller, direction) < size)
903 {
904 largest_size = size;
905 largest_panel = panel;
906 largest_panel_size = panel_size;
907 }
908 }
909 }
910 }
911 }
912 if(largest_panel != NULL)
913 {
914 if(largest_panel->IsSizingContinuous())
915 {
916 int amount = minimum_amount;
917 if(amount > 32)
918 {
919 // For "large" contraction, reduce this panel a bit, and
920 // then re-allocate the remainder of the quota (which may
921 // come to this panel again anyway)
922 amount = 32;
923 }
924 if(direction & wxHORIZONTAL)
925 {
926 largest_panel_size->x -= amount;
927 }
928 if(direction & wxVERTICAL)
929 {
930 largest_panel_size->y -= amount;
931 }
932 minimum_amount -= amount;
933 collapsed_something = true;
934 }
935 else
936 {
937 wxSize smaller = largest_panel->GetNextSmallerSize(direction, *largest_panel_size);
938 wxSize delta = (*largest_panel_size) - smaller;
939 *largest_panel_size = smaller;
940 minimum_amount -= GetSizeInOrientation(delta, direction);
941 collapsed_something = true;
942 }
943 }
944 else
945 {
946 break;
947 }
948 }
949 return collapsed_something;
950 }
951
952 bool wxRibbonPage::DismissExpandedPanel()
953 {
954 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
955 node;
956 node = node->GetNext() )
957 {
958 wxRibbonPanel* panel = wxDynamicCast(node->GetData(), wxRibbonPanel);
959 if(panel == NULL)
960 {
961 continue;
962 }
963 if(panel->GetExpandedPanel() != NULL)
964 {
965 return panel->HideExpanded();
966 }
967 }
968 return false;
969 }
970
971 wxSize wxRibbonPage::GetMinSize() const
972 {
973 wxSize min(wxDefaultCoord, wxDefaultCoord);
974
975 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
976 node;
977 node = node->GetNext() )
978 {
979 wxWindow* child = node->GetData();
980 wxSize child_min(child->GetMinSize());
981
982 min.x = wxMax(min.x, child_min.x);
983 min.y = wxMax(min.y, child_min.y);
984 }
985
986 if(GetMajorAxis() == wxHORIZONTAL)
987 {
988 min.x = wxDefaultCoord;
989 if(min.y != wxDefaultCoord)
990 {
991 min.y += m_art->GetMetric(wxRIBBON_ART_PAGE_BORDER_TOP_SIZE) + m_art->GetMetric(wxRIBBON_ART_PAGE_BORDER_BOTTOM_SIZE);
992 }
993 }
994 else
995 {
996 if(min.x != wxDefaultCoord)
997 {
998 min.x += m_art->GetMetric(wxRIBBON_ART_PAGE_BORDER_LEFT_SIZE) + m_art->GetMetric(wxRIBBON_ART_PAGE_BORDER_RIGHT_SIZE);
999 }
1000 min.y = wxDefaultCoord;
1001 }
1002
1003 return min;
1004 }
1005
1006 wxSize wxRibbonPage::DoGetBestSize() const
1007 {
1008 wxSize best(0, 0);
1009 size_t count = 0;
1010
1011 if(GetMajorAxis() == wxHORIZONTAL)
1012 {
1013 best.y = wxDefaultCoord;
1014
1015 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
1016 node;
1017 node = node->GetNext() )
1018 {
1019 wxWindow* child = node->GetData();
1020 wxSize child_best(child->GetBestSize());
1021
1022 if(child_best.x != wxDefaultCoord)
1023 {
1024 best.IncBy(child_best.x, 0);
1025 }
1026 best.y = wxMax(best.y, child_best.y);
1027
1028 ++count;
1029 }
1030
1031 if(count > 1)
1032 {
1033 best.IncBy((count - 1) * m_art->GetMetric(wxRIBBON_ART_PANEL_X_SEPARATION_SIZE), 0);
1034 }
1035 }
1036 else
1037 {
1038 best.x = wxDefaultCoord;
1039
1040 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
1041 node;
1042 node = node->GetNext() )
1043 {
1044 wxWindow* child = node->GetData();
1045 wxSize child_best(child->GetBestSize());
1046
1047 best.x = wxMax(best.x, child_best.x);
1048 if(child_best.y != wxDefaultCoord)
1049 {
1050 best.IncBy(0, child_best.y);
1051 }
1052
1053 ++count;
1054 }
1055
1056 if(count > 1)
1057 {
1058 best.IncBy(0, (count - 1) * m_art->GetMetric(wxRIBBON_ART_PANEL_Y_SEPARATION_SIZE));
1059 }
1060 }
1061
1062 if(best.x != wxDefaultCoord)
1063 {
1064 best.x += m_art->GetMetric(wxRIBBON_ART_PAGE_BORDER_LEFT_SIZE) + m_art->GetMetric(wxRIBBON_ART_PAGE_BORDER_RIGHT_SIZE);
1065 }
1066 if(best.y != wxDefaultCoord)
1067 {
1068 best.y += m_art->GetMetric(wxRIBBON_ART_PAGE_BORDER_TOP_SIZE) + m_art->GetMetric(wxRIBBON_ART_PAGE_BORDER_BOTTOM_SIZE);
1069 }
1070 return best;
1071 }
1072
1073 #endif // wxUSE_RIBBON