Added wxRIBBON_PANEL_FLEXIBLE flag to allow toolbars to wrap, taking up the optimum...
[wxWidgets.git] / src / ribbon / panel.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/ribbon/panel.cpp
3 // Purpose: Ribbon-style container for a group of related tools / controls
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
12 #include "wx/wxprec.h"
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 #if wxUSE_RIBBON
19
20 #include "wx/ribbon/panel.h"
21 #include "wx/ribbon/art.h"
22 #include "wx/ribbon/bar.h"
23 #include "wx/dcbuffer.h"
24 #include "wx/display.h"
25 #include "wx/sizer.h"
26
27 #ifndef WX_PRECOMP
28 #include "wx/frame.h"
29 #endif
30
31 #ifdef __WXMSW__
32 #include "wx/msw/private.h"
33 #endif
34
35 IMPLEMENT_CLASS(wxRibbonPanel, wxRibbonControl)
36
37 BEGIN_EVENT_TABLE(wxRibbonPanel, wxRibbonControl)
38 EVT_ENTER_WINDOW(wxRibbonPanel::OnMouseEnter)
39 EVT_ERASE_BACKGROUND(wxRibbonPanel::OnEraseBackground)
40 EVT_KILL_FOCUS(wxRibbonPanel::OnKillFocus)
41 EVT_LEAVE_WINDOW(wxRibbonPanel::OnMouseLeave)
42 EVT_LEFT_DOWN(wxRibbonPanel::OnMouseClick)
43 EVT_PAINT(wxRibbonPanel::OnPaint)
44 EVT_SIZE(wxRibbonPanel::OnSize)
45 END_EVENT_TABLE()
46
47 wxRibbonPanel::wxRibbonPanel() : m_expanded_dummy(NULL), m_expanded_panel(NULL)
48 {
49 }
50
51 wxRibbonPanel::wxRibbonPanel(wxWindow* parent,
52 wxWindowID id,
53 const wxString& label,
54 const wxBitmap& minimised_icon,
55 const wxPoint& pos,
56 const wxSize& size,
57 long style)
58 : wxRibbonControl(parent, id, pos, size, wxBORDER_NONE)
59 {
60 CommonInit(label, minimised_icon, style);
61 }
62
63 wxRibbonPanel::~wxRibbonPanel()
64 {
65 if(m_expanded_panel)
66 {
67 m_expanded_panel->m_expanded_dummy = NULL;
68 m_expanded_panel->GetParent()->Destroy();
69 }
70 }
71
72 bool wxRibbonPanel::Create(wxWindow* parent,
73 wxWindowID id,
74 const wxString& label,
75 const wxBitmap& icon,
76 const wxPoint& pos,
77 const wxSize& size,
78 long style)
79 {
80 if(!wxRibbonControl::Create(parent, id, pos, size, wxBORDER_NONE))
81 {
82 return false;
83 }
84
85 CommonInit(label, icon, style);
86
87 return true;
88 }
89
90 void wxRibbonPanel::SetArtProvider(wxRibbonArtProvider* art)
91 {
92 m_art = art;
93 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
94 node;
95 node = node->GetNext() )
96 {
97 wxWindow* child = node->GetData();
98 wxRibbonControl* ribbon_child = wxDynamicCast(child, wxRibbonControl);
99 if(ribbon_child)
100 {
101 ribbon_child->SetArtProvider(art);
102 }
103 }
104 if(m_expanded_panel)
105 m_expanded_panel->SetArtProvider(art);
106 }
107
108 void wxRibbonPanel::CommonInit(const wxString& label, const wxBitmap& icon, long style)
109 {
110 SetName(label);
111 SetLabel(label);
112
113 m_minimised_size = wxDefaultSize; // Unknown / none
114 m_smallest_unminimised_size = wxDefaultSize;// Unknown / none for IsFullySpecified()
115 m_preferred_expand_direction = wxSOUTH;
116 m_expanded_dummy = NULL;
117 m_expanded_panel = NULL;
118 m_flags = style;
119 m_minimised_icon = icon;
120 m_minimised = false;
121 m_hovered = false;
122
123 if(m_art == NULL)
124 {
125 wxRibbonControl* parent = wxDynamicCast(GetParent(), wxRibbonControl);
126 if(parent != NULL)
127 {
128 m_art = parent->GetArtProvider();
129 }
130 }
131
132 SetAutoLayout(true);
133 SetBackgroundStyle(wxBG_STYLE_CUSTOM);
134 SetMinSize(wxSize(20, 20));
135 }
136
137 bool wxRibbonPanel::IsMinimised() const
138 {
139 return m_minimised;
140 }
141
142 bool wxRibbonPanel::IsHovered() const
143 {
144 return m_hovered;
145 }
146
147 void wxRibbonPanel::OnMouseEnter(wxMouseEvent& evt)
148 {
149 TestPositionForHover(evt.GetPosition());
150 }
151
152 void wxRibbonPanel::OnMouseEnterChild(wxMouseEvent& evt)
153 {
154 wxPoint pos = evt.GetPosition();
155 wxWindow *child = wxDynamicCast(evt.GetEventObject(), wxWindow);
156 if(child)
157 {
158 pos += child->GetPosition();
159 TestPositionForHover(pos);
160 }
161 evt.Skip();
162 }
163
164 void wxRibbonPanel::OnMouseLeave(wxMouseEvent& evt)
165 {
166 TestPositionForHover(evt.GetPosition());
167 }
168
169 void wxRibbonPanel::OnMouseLeaveChild(wxMouseEvent& evt)
170 {
171 wxPoint pos = evt.GetPosition();
172 wxWindow *child = wxDynamicCast(evt.GetEventObject(), wxWindow);
173 if(child)
174 {
175 pos += child->GetPosition();
176 TestPositionForHover(pos);
177 }
178 evt.Skip();
179 }
180
181 void wxRibbonPanel::TestPositionForHover(const wxPoint& pos)
182 {
183 bool hovered = false;
184 if(pos.x >= 0 && pos.y >= 0)
185 {
186 wxSize size = GetSize();
187 if(pos.x < size.GetWidth() && pos.y < size.GetHeight())
188 {
189 hovered = true;
190 }
191 }
192 if(hovered != m_hovered)
193 {
194 m_hovered = hovered;
195 Refresh(false);
196 }
197 }
198
199 void wxRibbonPanel::AddChild(wxWindowBase *child)
200 {
201 wxRibbonControl::AddChild(child);
202
203 // Window enter / leave events count for only the window in question, not
204 // for children of the window. The panel wants to be in the hovered state
205 // whenever the mouse cursor is within its boundary, so the events need to
206 // be attached to children too.
207 child->Connect(wxEVT_ENTER_WINDOW, (wxObjectEventFunction)&wxRibbonPanel::OnMouseEnterChild, NULL, this);
208 child->Connect(wxEVT_LEAVE_WINDOW, (wxObjectEventFunction)&wxRibbonPanel::OnMouseLeaveChild, NULL, this);
209 }
210
211 void wxRibbonPanel::RemoveChild(wxWindowBase *child)
212 {
213 child->Disconnect(wxEVT_ENTER_WINDOW, (wxObjectEventFunction)&wxRibbonPanel::OnMouseEnterChild, NULL, this);
214 child->Disconnect(wxEVT_LEAVE_WINDOW, (wxObjectEventFunction)&wxRibbonPanel::OnMouseLeaveChild, NULL, this);
215
216 wxRibbonControl::RemoveChild(child);
217 }
218
219 void wxRibbonPanel::OnSize(wxSizeEvent& evt)
220 {
221 if(GetAutoLayout())
222 Layout();
223
224 evt.Skip();
225 }
226
227 void wxRibbonPanel::DoSetSize(int x, int y, int width, int height, int sizeFlags)
228 {
229 // At least on MSW, changing the size of a window will cause GetSize() to
230 // report the new size, but a size event may not be handled immediately.
231 // If this minimised check was performed in the OnSize handler, then
232 // GetSize() could return a size much larger than the minimised size while
233 // IsMinimised() returns true. This would then affect layout, as the panel
234 // will refuse to grow any larger while in limbo between minimised and non.
235
236 bool minimised = (m_flags & wxRIBBON_PANEL_NO_AUTO_MINIMISE) == 0 &&
237 IsMinimised(wxSize(width, height)); // check if would be at this size
238 if(minimised != m_minimised)
239 {
240 m_minimised = minimised;
241 // Note that for sizers, this routine disallows the use of mixed shown
242 // and hidden controls
243 // TODO ? use some list of user set invisible children to restore status.
244 for (wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
245 node;
246 node = node->GetNext())
247 {
248 node->GetData()->Show(!minimised);
249 }
250
251 Refresh();
252 }
253
254 wxRibbonControl::DoSetSize(x, y, width, height, sizeFlags);
255 }
256
257 // Checks if panel would be minimised at (client size) at_size
258 bool wxRibbonPanel::IsMinimised(wxSize at_size) const
259 {
260 if(GetSizer())
261 {
262 // we have no information on size change direction
263 // so check both
264 wxSize size = GetMinNotMinimisedSize();
265 if(size.x > at_size.x || size.y > at_size.y)
266 return true;
267
268 return false;
269 }
270
271 if(!m_minimised_size.IsFullySpecified())
272 return false;
273
274 return (at_size.GetX() <= m_minimised_size.GetX() &&
275 at_size.GetY() <= m_minimised_size.GetY()) ||
276 at_size.GetX() < m_smallest_unminimised_size.GetX() ||
277 at_size.GetY() < m_smallest_unminimised_size.GetY();
278 }
279
280 void wxRibbonPanel::OnEraseBackground(wxEraseEvent& WXUNUSED(evt))
281 {
282 // All painting done in main paint handler to minimise flicker
283 }
284
285 void wxRibbonPanel::OnPaint(wxPaintEvent& WXUNUSED(evt))
286 {
287 wxAutoBufferedPaintDC dc(this);
288
289 if(m_art != NULL)
290 {
291 if(IsMinimised())
292 {
293 m_art->DrawMinimisedPanel(dc, this, GetSize(), m_minimised_icon_resized);
294 }
295 else
296 {
297 m_art->DrawPanelBackground(dc, this, GetSize());
298 }
299 }
300 }
301
302 bool wxRibbonPanel::IsSizingContinuous() const
303 {
304 // A panel never sizes continuously, even if all of its children can,
305 // as it would appear out of place along side non-continuous panels.
306
307 // JS 2012-03-09: introducing wxRIBBON_PANEL_STRETCH to allow
308 // the panel to fill its parent page. For example we might have
309 // a list of styles in one of the pages, which should stretch to
310 // fill available space.
311 return (m_flags & wxRIBBON_PANEL_STRETCH) != 0;
312 }
313
314 // Finds the best width and height given the parent's width and height
315 wxSize wxRibbonPanel::GetBestSizeForParentSize(const wxSize& parentSize) const
316 {
317 if (GetChildren().GetCount() == 1)
318 {
319 wxWindow* win = GetChildren().GetFirst()->GetData();
320 wxRibbonControl* control = wxDynamicCast(win, wxRibbonControl);
321 if (control)
322 {
323 wxClientDC temp_dc((wxRibbonPanel*) this);
324 wxSize childSize = control->GetBestSizeForParentSize(parentSize);
325 wxSize overallSize = m_art->GetPanelSize(temp_dc, this, childSize, NULL);
326 return overallSize;
327 }
328 }
329 return GetSize();
330 }
331
332 wxSize wxRibbonPanel::DoGetNextSmallerSize(wxOrientation direction,
333 wxSize relative_to) const
334 {
335 if(m_expanded_panel != NULL)
336 {
337 // Next size depends upon children, who are currently in the
338 // expanded panel
339 return m_expanded_panel->DoGetNextSmallerSize(direction, relative_to);
340 }
341
342 if(m_art != NULL)
343 {
344 wxClientDC dc((wxRibbonPanel*) this);
345 wxSize child_relative = m_art->GetPanelClientSize(dc, this, relative_to, NULL);
346 wxSize smaller(-1, -1);
347 bool minimise = false;
348
349 if(GetSizer())
350 {
351 // Get smallest non minimised size
352 smaller = GetMinSize();
353 // and adjust to child_relative for parent page
354 if(m_art->GetFlags() & wxRIBBON_BAR_FLOW_VERTICAL)
355 {
356 minimise = (child_relative.y <= smaller.y);
357 if(smaller.x < child_relative.x)
358 smaller.x = child_relative.x;
359 }
360 else
361 {
362 minimise = (child_relative.x <= smaller.x);
363 if(smaller.y < child_relative.y)
364 smaller.y = child_relative.y;
365 }
366 }
367 else if(GetChildren().GetCount() == 1)
368 {
369 // Simple (and common) case of single ribbon child or Sizer
370 wxWindow* child = GetChildren().Item(0)->GetData();
371 wxRibbonControl* ribbon_child = wxDynamicCast(child, wxRibbonControl);
372 if(ribbon_child != NULL)
373 {
374 smaller = ribbon_child->GetNextSmallerSize(direction, child_relative);
375 minimise = (smaller == child_relative);
376 }
377 }
378
379 if(minimise)
380 {
381 if(CanAutoMinimise())
382 {
383 wxSize minimised = m_minimised_size;
384 switch(direction)
385 {
386 case wxHORIZONTAL:
387 minimised.SetHeight(relative_to.GetHeight());
388 break;
389 case wxVERTICAL:
390 minimised.SetWidth(relative_to.GetWidth());
391 break;
392 default:
393 break;
394 }
395 return minimised;
396 }
397 else
398 {
399 return relative_to;
400 }
401 }
402 else if(smaller.IsFullySpecified()) // Use fallback if !(sizer/child = 1)
403 {
404 return m_art->GetPanelSize(dc, this, smaller, NULL);
405 }
406 }
407
408 // Fallback: Decrease by 20% (or minimum size, whichever larger)
409 wxSize current(relative_to);
410 wxSize minimum(GetMinSize());
411 if(direction & wxHORIZONTAL)
412 {
413 current.x = (current.x * 4) / 5;
414 if(current.x < minimum.x)
415 {
416 current.x = minimum.x;
417 }
418 }
419 if(direction & wxVERTICAL)
420 {
421 current.y = (current.y * 4) / 5;
422 if(current.y < minimum.y)
423 {
424 current.y = minimum.y;
425 }
426 }
427 return current;
428 }
429
430 wxSize wxRibbonPanel::DoGetNextLargerSize(wxOrientation direction,
431 wxSize relative_to) const
432 {
433 if(m_expanded_panel != NULL)
434 {
435 // Next size depends upon children, who are currently in the
436 // expanded panel
437 return m_expanded_panel->DoGetNextLargerSize(direction, relative_to);
438 }
439
440 if(IsMinimised(relative_to))
441 {
442 wxSize current = relative_to;
443 wxSize min_size = GetMinNotMinimisedSize();
444 switch(direction)
445 {
446 case wxHORIZONTAL:
447 if(min_size.x > current.x && min_size.y == current.y)
448 return min_size;
449 break;
450 case wxVERTICAL:
451 if(min_size.x == current.x && min_size.y > current.y)
452 return min_size;
453 break;
454 case wxBOTH:
455 if(min_size.x > current.x && min_size.y > current.y)
456 return min_size;
457 break;
458 default:
459 break;
460 }
461 }
462
463 if(m_art != NULL)
464 {
465 wxClientDC dc((wxRibbonPanel*) this);
466 wxSize child_relative = m_art->GetPanelClientSize(dc, this, relative_to, NULL);
467 wxSize larger(-1, -1);
468
469 if(GetSizer())
470 {
471 // We could just let the sizer expand in flow direction but see comment
472 // in IsSizingContinuous()
473 larger = GetPanelSizerBestSize();
474 // and adjust for page in non flow direction
475 if(m_art->GetFlags() & wxRIBBON_BAR_FLOW_VERTICAL)
476 {
477 if(larger.x != child_relative.x)
478 larger.x = child_relative.x;
479 }
480 else if(larger.y != child_relative.y)
481 {
482 larger.y = child_relative.y;
483 }
484 }
485 else if(GetChildren().GetCount() == 1)
486 {
487 // Simple (and common) case of single ribbon child
488 wxWindow* child = GetChildren().Item(0)->GetData();
489 wxRibbonControl* ribbon_child = wxDynamicCast(child, wxRibbonControl);
490 if(ribbon_child != NULL)
491 {
492 larger = ribbon_child->GetNextLargerSize(direction, child_relative);
493 }
494 }
495
496 if(larger.IsFullySpecified()) // Use fallback if !(sizer/child = 1)
497 {
498 if(larger == child_relative)
499 {
500 return relative_to;
501 }
502 else
503 {
504 return m_art->GetPanelSize(dc, this, larger, NULL);
505 }
506 }
507 }
508
509 // Fallback: Increase by 25% (equal to a prior or subsequent 20% decrease)
510 // Note that due to rounding errors, this increase may not exactly equal a
511 // matching decrease - an ideal solution would not have these errors, but
512 // avoiding them is non-trivial unless an increase is by 100% rather than
513 // a fractional amount. This would then be non-ideal as the resizes happen
514 // at very large intervals.
515 wxSize current(relative_to);
516 if(direction & wxHORIZONTAL)
517 {
518 current.x = (current.x * 5 + 3) / 4;
519 }
520 if(direction & wxVERTICAL)
521 {
522 current.y = (current.y * 5 + 3) / 4;
523 }
524 return current;
525 }
526
527 bool wxRibbonPanel::CanAutoMinimise() const
528 {
529 return (m_flags & wxRIBBON_PANEL_NO_AUTO_MINIMISE) == 0
530 && m_minimised_size.IsFullySpecified();
531 }
532
533 wxSize wxRibbonPanel::GetMinSize() const
534 {
535 if(m_expanded_panel != NULL)
536 {
537 // Minimum size depends upon children, who are currently in the
538 // expanded panel
539 return m_expanded_panel->GetMinSize();
540 }
541
542 if(CanAutoMinimise())
543 {
544 return m_minimised_size;
545 }
546 else
547 {
548 return GetMinNotMinimisedSize();
549 }
550 }
551
552 wxSize wxRibbonPanel::GetMinNotMinimisedSize() const
553 {
554 // Ask sizer if present
555 if(GetSizer())
556 {
557 wxClientDC dc((wxRibbonPanel*) this);
558 return m_art->GetPanelSize(dc, this, GetPanelSizerMinSize(), NULL);
559 }
560 else if(GetChildren().GetCount() == 1)
561 {
562 // Common case of single child taking up the entire panel
563 wxWindow* child = GetChildren().Item(0)->GetData();
564 wxClientDC dc((wxRibbonPanel*) this);
565 return m_art->GetPanelSize(dc, this, child->GetMinSize(), NULL);
566 }
567
568 return wxRibbonControl::GetMinSize();
569 }
570
571 wxSize wxRibbonPanel::GetPanelSizerMinSize() const
572 {
573 // Called from Realize() to set m_smallest_unminimised_size and from other
574 // functions to get the minimum size.
575 // The panel will be invisible when minimised and sizer calcs will be 0
576 // Uses m_smallest_unminimised_size in preference to GetSizer()->CalcMin()
577 // to eliminate flicker.
578
579 // Check if is visible and not previously calculated
580 if(IsShown() && !m_smallest_unminimised_size.IsFullySpecified())
581 {
582 return GetSizer()->CalcMin();
583 }
584 // else use previously calculated m_smallest_unminimised_size
585 wxClientDC dc((wxRibbonPanel*) this);
586 return m_art->GetPanelClientSize(dc,
587 this,
588 m_smallest_unminimised_size,
589 NULL);
590 }
591
592 wxSize wxRibbonPanel::GetPanelSizerBestSize() const
593 {
594 wxSize size = GetPanelSizerMinSize();
595 // TODO allow panel to increase its size beyond minimum size
596 // by steps similarly to ribbon control panels (preferred for aesthetics)
597 // or continuously.
598 return size;
599 }
600
601 wxSize wxRibbonPanel::DoGetBestSize() const
602 {
603 // Ask sizer if present
604 if( GetSizer())
605 {
606 wxClientDC dc((wxRibbonPanel*) this);
607 return m_art->GetPanelSize(dc, this, GetPanelSizerBestSize(), NULL);
608 }
609 else if(GetChildren().GetCount() == 1)
610 {
611 // Common case of no sizer and single child taking up the entire panel
612 wxWindow* child = GetChildren().Item(0)->GetData();
613 wxClientDC dc((wxRibbonPanel*) this);
614 return m_art->GetPanelSize(dc, this, child->GetBestSize(), NULL);
615 }
616
617 return wxRibbonControl::DoGetBestSize();
618 }
619
620 bool wxRibbonPanel::Realize()
621 {
622 bool status = true;
623
624 for (wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
625 node;
626 node = node->GetNext())
627 {
628 wxRibbonControl* child = wxDynamicCast(node->GetData(), wxRibbonControl);
629 if(child == NULL)
630 {
631 continue;
632 }
633 if(!child->Realize())
634 {
635 status = false;
636 }
637 }
638
639 wxSize minimum_children_size(0, 0);
640
641 // Ask sizer if there is one present
642 if(GetSizer())
643 {
644 minimum_children_size = GetPanelSizerMinSize();
645 }
646 else if(GetChildren().GetCount() == 1)
647 {
648 minimum_children_size = GetChildren().GetFirst()->GetData()->GetMinSize();
649 }
650
651 if(m_art != NULL)
652 {
653 wxClientDC temp_dc(this);
654
655 m_smallest_unminimised_size =
656 m_art->GetPanelSize(temp_dc, this, minimum_children_size, NULL);
657
658 wxSize bitmap_size;
659 wxSize panel_min_size = GetMinNotMinimisedSize();
660 m_minimised_size = m_art->GetMinimisedPanelMinimumSize(temp_dc, this,
661 &bitmap_size, &m_preferred_expand_direction);
662 if(m_minimised_icon.IsOk() && m_minimised_icon.GetSize() != bitmap_size)
663 {
664 wxImage img(m_minimised_icon.ConvertToImage());
665 img.Rescale(bitmap_size.GetWidth(), bitmap_size.GetHeight(), wxIMAGE_QUALITY_HIGH);
666 m_minimised_icon_resized = wxBitmap(img);
667 }
668 else
669 {
670 m_minimised_icon_resized = m_minimised_icon;
671 }
672 if(m_minimised_size.x > panel_min_size.x &&
673 m_minimised_size.y > panel_min_size.y)
674 {
675 // No point in having a minimised size which is larger than the
676 // minimum size which the children can go to.
677 m_minimised_size = wxSize(-1, -1);
678 }
679 else
680 {
681 if(m_art->GetFlags() & wxRIBBON_BAR_FLOW_VERTICAL)
682 {
683 m_minimised_size.x = panel_min_size.x;
684 }
685 else
686 {
687 m_minimised_size.y = panel_min_size.y;
688 }
689 }
690 }
691 else
692 {
693 m_minimised_size = wxSize(-1, -1);
694 }
695
696 return Layout() && status;
697 }
698
699 bool wxRibbonPanel::Layout()
700 {
701 if(IsMinimised())
702 {
703 // Children are all invisible when minimised
704 return true;
705 }
706
707 // Get wxRibbonPanel client size
708 wxPoint position;
709 wxClientDC dc(this);
710 wxSize size = m_art->GetPanelClientSize(dc, this, GetSize(), &position);
711
712 // If there is a sizer, use it
713 if(GetSizer())
714 {
715 GetSizer()->SetDimension(position, size); // SetSize and Layout()
716 }
717 else if(GetChildren().GetCount() == 1)
718 {
719 // Common case of no sizer and single child taking up the entire panel
720 wxWindow* child = GetChildren().Item(0)->GetData();
721 child->SetSize(position.x, position.y, size.GetWidth(), size.GetHeight());
722 }
723 return true;
724 }
725
726 void wxRibbonPanel::OnMouseClick(wxMouseEvent& WXUNUSED(evt))
727 {
728 if(IsMinimised())
729 {
730 if(m_expanded_panel != NULL)
731 {
732 HideExpanded();
733 }
734 else
735 {
736 ShowExpanded();
737 }
738 }
739 }
740
741 wxRibbonPanel* wxRibbonPanel::GetExpandedDummy()
742 {
743 return m_expanded_dummy;
744 }
745
746 wxRibbonPanel* wxRibbonPanel::GetExpandedPanel()
747 {
748 return m_expanded_panel;
749 }
750
751 bool wxRibbonPanel::ShowExpanded()
752 {
753 if(!IsMinimised())
754 {
755 return false;
756 }
757 if(m_expanded_dummy != NULL || m_expanded_panel != NULL)
758 {
759 return false;
760 }
761
762 wxSize size = GetBestSize();
763
764 // Special case for flexible panel layout, where GetBestSize doesn't work
765 if (GetFlags() & wxRIBBON_PANEL_FLEXIBLE)
766 {
767 size = GetBestSizeForParentSize(wxSize(400, 1000));
768 }
769
770 wxPoint pos = GetExpandedPosition(wxRect(GetScreenPosition(), GetSize()),
771 size, m_preferred_expand_direction).GetTopLeft();
772
773 // Need a top-level frame to contain the expanded panel
774 wxFrame *container = new wxFrame(NULL, wxID_ANY, GetLabel(),
775 pos, size, wxFRAME_NO_TASKBAR | wxBORDER_NONE);
776
777 m_expanded_panel = new wxRibbonPanel(container, wxID_ANY,
778 GetLabel(), m_minimised_icon, wxPoint(0, 0), size, (m_flags /* & ~wxRIBBON_PANEL_FLEXIBLE */));
779
780 m_expanded_panel->SetArtProvider(m_art);
781 m_expanded_panel->m_expanded_dummy = this;
782
783 // Move all children to the new panel.
784 // Conceptually it might be simpler to reparent this entire panel to the
785 // container and create a new panel to sit in its place while expanded.
786 // This approach has a problem though - when the panel is reinserted into
787 // its original parent, it'll be at a different position in the child list
788 // and thus assume a new position.
789 // NB: Children iterators not used as behaviour is not well defined
790 // when iterating over a container which is being emptied
791 while(!GetChildren().IsEmpty())
792 {
793 wxWindow *child = GetChildren().GetFirst()->GetData();
794 child->Reparent(m_expanded_panel);
795 child->Show();
796 }
797
798 // Move sizer to new panel
799 if(GetSizer())
800 {
801 wxSizer* sizer = GetSizer();
802 SetSizer(NULL, false);
803 m_expanded_panel->SetSizer(sizer);
804 }
805
806 m_expanded_panel->Realize();
807 Refresh();
808 container->SetMinClientSize(size);
809 container->Show();
810 m_expanded_panel->SetFocus();
811
812 return true;
813 }
814
815 bool wxRibbonPanel::ShouldSendEventToDummy(wxEvent& evt)
816 {
817 // For an expanded panel, filter events between being sent up to the
818 // floating top level window or to the dummy panel sitting in the ribbon
819 // bar.
820
821 // Child focus events should not be redirected, as the child would not be a
822 // child of the window the event is redirected to. All other command events
823 // seem to be suitable for redirecting.
824 return evt.IsCommandEvent() && evt.GetEventType() != wxEVT_CHILD_FOCUS;
825 }
826
827 bool wxRibbonPanel::TryAfter(wxEvent& evt)
828 {
829 if(m_expanded_dummy && ShouldSendEventToDummy(evt))
830 {
831 wxPropagateOnce propagateOnce(evt);
832 return m_expanded_dummy->GetEventHandler()->ProcessEvent(evt);
833 }
834 else
835 {
836 return wxRibbonControl::TryAfter(evt);
837 }
838 }
839
840 static bool IsAncestorOf(wxWindow *ancestor, wxWindow *window)
841 {
842 while(window != NULL)
843 {
844 wxWindow *parent = window->GetParent();
845 if(parent == ancestor)
846 return true;
847 else
848 window = parent;
849 }
850 return false;
851 }
852
853 void wxRibbonPanel::OnKillFocus(wxFocusEvent& evt)
854 {
855 if(m_expanded_dummy)
856 {
857 wxWindow *receiver = evt.GetWindow();
858 if(IsAncestorOf(this, receiver))
859 {
860 m_child_with_focus = receiver;
861 receiver->Connect(wxEVT_KILL_FOCUS,
862 wxFocusEventHandler(wxRibbonPanel::OnChildKillFocus),
863 NULL, this);
864 }
865 else if(receiver == NULL || receiver != m_expanded_dummy)
866 {
867 HideExpanded();
868 }
869 }
870 }
871
872 void wxRibbonPanel::OnChildKillFocus(wxFocusEvent& evt)
873 {
874 if(m_child_with_focus == NULL)
875 return; // Should never happen, but a check can't hurt
876
877 m_child_with_focus->Disconnect(wxEVT_KILL_FOCUS,
878 wxFocusEventHandler(wxRibbonPanel::OnChildKillFocus), NULL, this);
879 m_child_with_focus = NULL;
880
881 wxWindow *receiver = evt.GetWindow();
882 if(receiver == this || IsAncestorOf(this, receiver))
883 {
884 m_child_with_focus = receiver;
885 receiver->Connect(wxEVT_KILL_FOCUS,
886 wxFocusEventHandler(wxRibbonPanel::OnChildKillFocus), NULL, this);
887 evt.Skip();
888 }
889 else if(receiver == NULL || receiver != m_expanded_dummy)
890 {
891 HideExpanded();
892 // Do not skip event, as the panel has been de-expanded, causing the
893 // child with focus to be reparented (and hidden). If the event
894 // continues propogation then bad things happen.
895 }
896 else
897 {
898 evt.Skip();
899 }
900 }
901
902 bool wxRibbonPanel::HideExpanded()
903 {
904 if(m_expanded_dummy == NULL)
905 {
906 if(m_expanded_panel)
907 {
908 return m_expanded_panel->HideExpanded();
909 }
910 else
911 {
912 return false;
913 }
914 }
915
916 // Move children back to original panel
917 // NB: Children iterators not used as behaviour is not well defined
918 // when iterating over a container which is being emptied
919 while(!GetChildren().IsEmpty())
920 {
921 wxWindow *child = GetChildren().GetFirst()->GetData();
922 child->Reparent(m_expanded_dummy);
923 child->Hide();
924 }
925
926 // Move sizer back
927 if(GetSizer())
928 {
929 wxSizer* sizer = GetSizer();
930 SetSizer(NULL, false);
931 m_expanded_dummy->SetSizer(sizer);
932 }
933
934 m_expanded_dummy->m_expanded_panel = NULL;
935 m_expanded_dummy->Realize();
936 m_expanded_dummy->Refresh();
937 wxWindow *parent = GetParent();
938 Destroy();
939 parent->Destroy();
940
941 return true;
942 }
943
944 wxRect wxRibbonPanel::GetExpandedPosition(wxRect panel,
945 wxSize expanded_size,
946 wxDirection direction)
947 {
948 // Strategy:
949 // 1) Determine primary position based on requested direction
950 // 2) Move the position so that it sits entirely within a display
951 // (for single monitor systems, this moves it into the display region,
952 // but for multiple monitors, it does so without splitting it over
953 // more than one display)
954 // 2.1) Move in the primary axis
955 // 2.2) Move in the secondary axis
956
957 wxPoint pos;
958 bool primary_x = false;
959 int secondary_x = 0;
960 int secondary_y = 0;
961 switch(direction)
962 {
963 case wxNORTH:
964 pos.x = panel.GetX() + (panel.GetWidth() - expanded_size.GetWidth()) / 2;
965 pos.y = panel.GetY() - expanded_size.GetHeight();
966 primary_x = true;
967 secondary_y = 1;
968 break;
969 case wxEAST:
970 pos.x = panel.GetRight();
971 pos.y = panel.GetY() + (panel.GetHeight() - expanded_size.GetHeight()) / 2;
972 secondary_x = -1;
973 break;
974 case wxSOUTH:
975 pos.x = panel.GetX() + (panel.GetWidth() - expanded_size.GetWidth()) / 2;
976 pos.y = panel.GetBottom();
977 primary_x = true;
978 secondary_y = -1;
979 break;
980 case wxWEST:
981 default:
982 pos.x = panel.GetX() - expanded_size.GetWidth();
983 pos.y = panel.GetY() + (panel.GetHeight() - expanded_size.GetHeight()) / 2;
984 secondary_x = 1;
985 break;
986 }
987 wxRect expanded(pos, expanded_size);
988
989 wxRect best(expanded);
990 int best_distance = INT_MAX;
991
992 const unsigned display_n = wxDisplay::GetCount();
993 unsigned display_i;
994 for(display_i = 0; display_i < display_n; ++display_i)
995 {
996 wxRect display = wxDisplay(display_i).GetGeometry();
997
998 if(display.Contains(expanded))
999 {
1000 return expanded;
1001 }
1002 else if(display.Intersects(expanded))
1003 {
1004 wxRect new_rect(expanded);
1005 int distance = 0;
1006
1007 if(primary_x)
1008 {
1009 if(expanded.GetRight() > display.GetRight())
1010 {
1011 distance = expanded.GetRight() - display.GetRight();
1012 new_rect.x -= distance;
1013 }
1014 else if(expanded.GetLeft() < display.GetLeft())
1015 {
1016 distance = display.GetLeft() - expanded.GetLeft();
1017 new_rect.x += distance;
1018 }
1019 }
1020 else
1021 {
1022 if(expanded.GetBottom() > display.GetBottom())
1023 {
1024 distance = expanded.GetBottom() - display.GetBottom();
1025 new_rect.y -= distance;
1026 }
1027 else if(expanded.GetTop() < display.GetTop())
1028 {
1029 distance = display.GetTop() - expanded.GetTop();
1030 new_rect.y += distance;
1031 }
1032 }
1033 if(!display.Contains(new_rect))
1034 {
1035 // Tried moving in primary axis, but failed.
1036 // Hence try moving in the secondary axis.
1037 int dx = secondary_x * (panel.GetWidth() + expanded_size.GetWidth());
1038 int dy = secondary_y * (panel.GetHeight() + expanded_size.GetHeight());
1039 new_rect.x += dx;
1040 new_rect.y += dy;
1041
1042 // Squaring makes secondary moves more expensive (and also
1043 // prevents a negative cost)
1044 distance += dx * dx + dy * dy;
1045 }
1046 if(display.Contains(new_rect) && distance < best_distance)
1047 {
1048 best = new_rect;
1049 best_distance = distance;
1050 }
1051 }
1052 }
1053
1054 return best;
1055 }
1056
1057 #endif // wxUSE_RIBBON