1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/ribbon/panel.cpp
3 // Purpose: Ribbon-style container for a group of related tools / controls
4 // Author: Peter Cawley
8 // Copyright: (C) Peter Cawley
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
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"
32 #include "wx/msw/private.h"
35 IMPLEMENT_CLASS(wxRibbonPanel
, wxRibbonControl
)
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
)
47 wxRibbonPanel::wxRibbonPanel() : m_expanded_dummy(NULL
), m_expanded_panel(NULL
)
51 wxRibbonPanel::wxRibbonPanel(wxWindow
* parent
,
53 const wxString
& label
,
54 const wxBitmap
& minimised_icon
,
58 : wxRibbonControl(parent
, id
, pos
, size
, wxBORDER_NONE
)
60 CommonInit(label
, minimised_icon
, style
);
63 wxRibbonPanel::~wxRibbonPanel()
67 m_expanded_panel
->m_expanded_dummy
= NULL
;
68 m_expanded_panel
->GetParent()->Destroy();
72 bool wxRibbonPanel::Create(wxWindow
* parent
,
74 const wxString
& label
,
80 if(!wxRibbonControl::Create(parent
, id
, pos
, size
, wxBORDER_NONE
))
85 CommonInit(label
, icon
, style
);
90 void wxRibbonPanel::SetArtProvider(wxRibbonArtProvider
* art
)
93 for ( wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
95 node
= node
->GetNext() )
97 wxWindow
* child
= node
->GetData();
98 wxRibbonControl
* ribbon_child
= wxDynamicCast(child
, wxRibbonControl
);
101 ribbon_child
->SetArtProvider(art
);
105 m_expanded_panel
->SetArtProvider(art
);
108 void wxRibbonPanel::CommonInit(const wxString
& label
, const wxBitmap
& icon
, long style
)
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
;
119 m_minimised_icon
= icon
;
125 wxRibbonControl
* parent
= wxDynamicCast(GetParent(), wxRibbonControl
);
128 m_art
= parent
->GetArtProvider();
133 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
134 SetMinSize(wxSize(20, 20));
137 bool wxRibbonPanel::IsMinimised() const
142 bool wxRibbonPanel::IsHovered() const
147 void wxRibbonPanel::OnMouseEnter(wxMouseEvent
& evt
)
149 TestPositionForHover(evt
.GetPosition());
152 void wxRibbonPanel::OnMouseEnterChild(wxMouseEvent
& evt
)
154 wxPoint pos
= evt
.GetPosition();
155 wxWindow
*child
= wxDynamicCast(evt
.GetEventObject(), wxWindow
);
158 pos
+= child
->GetPosition();
159 TestPositionForHover(pos
);
164 void wxRibbonPanel::OnMouseLeave(wxMouseEvent
& evt
)
166 TestPositionForHover(evt
.GetPosition());
169 void wxRibbonPanel::OnMouseLeaveChild(wxMouseEvent
& evt
)
171 wxPoint pos
= evt
.GetPosition();
172 wxWindow
*child
= wxDynamicCast(evt
.GetEventObject(), wxWindow
);
175 pos
+= child
->GetPosition();
176 TestPositionForHover(pos
);
181 void wxRibbonPanel::TestPositionForHover(const wxPoint
& pos
)
183 bool hovered
= false;
184 if(pos
.x
>= 0 && pos
.y
>= 0)
186 wxSize size
= GetSize();
187 if(pos
.x
< size
.GetWidth() && pos
.y
< size
.GetHeight())
192 if(hovered
!= m_hovered
)
199 void wxRibbonPanel::AddChild(wxWindowBase
*child
)
201 wxRibbonControl::AddChild(child
);
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);
211 void wxRibbonPanel::RemoveChild(wxWindowBase
*child
)
213 child
->Disconnect(wxEVT_ENTER_WINDOW
, (wxObjectEventFunction
)&wxRibbonPanel::OnMouseEnterChild
, NULL
, this);
214 child
->Disconnect(wxEVT_LEAVE_WINDOW
, (wxObjectEventFunction
)&wxRibbonPanel::OnMouseLeaveChild
, NULL
, this);
216 wxRibbonControl::RemoveChild(child
);
219 void wxRibbonPanel::OnSize(wxSizeEvent
& evt
)
227 void wxRibbonPanel::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
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.
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
)
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();
246 node
= node
->GetNext())
248 node
->GetData()->Show(!minimised
);
254 wxRibbonControl::DoSetSize(x
, y
, width
, height
, sizeFlags
);
257 // Checks if panel would be minimised at (client size) at_size
258 bool wxRibbonPanel::IsMinimised(wxSize at_size
) const
262 // we have no information on size change direction
264 wxSize size
= GetMinNotMinimisedSize();
265 if(size
.x
> at_size
.x
|| size
.y
> at_size
.y
)
271 if(!m_minimised_size
.IsFullySpecified())
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();
280 void wxRibbonPanel::OnEraseBackground(wxEraseEvent
& WXUNUSED(evt
))
282 // All painting done in main paint handler to minimise flicker
285 void wxRibbonPanel::OnPaint(wxPaintEvent
& WXUNUSED(evt
))
287 wxAutoBufferedPaintDC
dc(this);
293 m_art
->DrawMinimisedPanel(dc
, this, GetSize(), m_minimised_icon_resized
);
297 m_art
->DrawPanelBackground(dc
, this, GetSize());
302 bool wxRibbonPanel::IsSizingContinuous() const
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.
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;
314 // Finds the best width and height given the parent's width and height
315 wxSize
wxRibbonPanel::GetBestSizeForParentSize(const wxSize
& parentSize
) const
317 if (GetChildren().GetCount() == 1)
319 wxWindow
* win
= GetChildren().GetFirst()->GetData();
320 wxRibbonControl
* control
= wxDynamicCast(win
, wxRibbonControl
);
323 wxClientDC
temp_dc((wxRibbonPanel
*) this);
324 wxSize childSize
= control
->GetBestSizeForParentSize(parentSize
);
325 wxSize overallSize
= m_art
->GetPanelSize(temp_dc
, this, childSize
, NULL
);
332 wxSize
wxRibbonPanel::DoGetNextSmallerSize(wxOrientation direction
,
333 wxSize relative_to
) const
335 if(m_expanded_panel
!= NULL
)
337 // Next size depends upon children, who are currently in the
339 return m_expanded_panel
->DoGetNextSmallerSize(direction
, relative_to
);
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;
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
)
356 minimise
= (child_relative
.y
<= smaller
.y
);
357 if(smaller
.x
< child_relative
.x
)
358 smaller
.x
= child_relative
.x
;
362 minimise
= (child_relative
.x
<= smaller
.x
);
363 if(smaller
.y
< child_relative
.y
)
364 smaller
.y
= child_relative
.y
;
367 else if(GetChildren().GetCount() == 1)
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
)
374 smaller
= ribbon_child
->GetNextSmallerSize(direction
, child_relative
);
375 minimise
= (smaller
== child_relative
);
381 if(CanAutoMinimise())
383 wxSize minimised
= m_minimised_size
;
387 minimised
.SetHeight(relative_to
.GetHeight());
390 minimised
.SetWidth(relative_to
.GetWidth());
402 else if(smaller
.IsFullySpecified()) // Use fallback if !(sizer/child = 1)
404 return m_art
->GetPanelSize(dc
, this, smaller
, NULL
);
408 // Fallback: Decrease by 20% (or minimum size, whichever larger)
409 wxSize
current(relative_to
);
410 wxSize
minimum(GetMinSize());
411 if(direction
& wxHORIZONTAL
)
413 current
.x
= (current
.x
* 4) / 5;
414 if(current
.x
< minimum
.x
)
416 current
.x
= minimum
.x
;
419 if(direction
& wxVERTICAL
)
421 current
.y
= (current
.y
* 4) / 5;
422 if(current
.y
< minimum
.y
)
424 current
.y
= minimum
.y
;
430 wxSize
wxRibbonPanel::DoGetNextLargerSize(wxOrientation direction
,
431 wxSize relative_to
) const
433 if(m_expanded_panel
!= NULL
)
435 // Next size depends upon children, who are currently in the
437 return m_expanded_panel
->DoGetNextLargerSize(direction
, relative_to
);
440 if(IsMinimised(relative_to
))
442 wxSize current
= relative_to
;
443 wxSize min_size
= GetMinNotMinimisedSize();
447 if(min_size
.x
> current
.x
&& min_size
.y
== current
.y
)
451 if(min_size
.x
== current
.x
&& min_size
.y
> current
.y
)
455 if(min_size
.x
> current
.x
&& min_size
.y
> current
.y
)
465 wxClientDC
dc((wxRibbonPanel
*) this);
466 wxSize child_relative
= m_art
->GetPanelClientSize(dc
, this, relative_to
, NULL
);
467 wxSize
larger(-1, -1);
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
)
477 if(larger
.x
!= child_relative
.x
)
478 larger
.x
= child_relative
.x
;
480 else if(larger
.y
!= child_relative
.y
)
482 larger
.y
= child_relative
.y
;
485 else if(GetChildren().GetCount() == 1)
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
)
492 larger
= ribbon_child
->GetNextLargerSize(direction
, child_relative
);
496 if(larger
.IsFullySpecified()) // Use fallback if !(sizer/child = 1)
498 if(larger
== child_relative
)
504 return m_art
->GetPanelSize(dc
, this, larger
, NULL
);
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
)
518 current
.x
= (current
.x
* 5 + 3) / 4;
520 if(direction
& wxVERTICAL
)
522 current
.y
= (current
.y
* 5 + 3) / 4;
527 bool wxRibbonPanel::CanAutoMinimise() const
529 return (m_flags
& wxRIBBON_PANEL_NO_AUTO_MINIMISE
) == 0
530 && m_minimised_size
.IsFullySpecified();
533 wxSize
wxRibbonPanel::GetMinSize() const
535 if(m_expanded_panel
!= NULL
)
537 // Minimum size depends upon children, who are currently in the
539 return m_expanded_panel
->GetMinSize();
542 if(CanAutoMinimise())
544 return m_minimised_size
;
548 return GetMinNotMinimisedSize();
552 wxSize
wxRibbonPanel::GetMinNotMinimisedSize() const
554 // Ask sizer if present
557 wxClientDC
dc((wxRibbonPanel
*) this);
558 return m_art
->GetPanelSize(dc
, this, GetPanelSizerMinSize(), NULL
);
560 else if(GetChildren().GetCount() == 1)
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
);
568 return wxRibbonControl::GetMinSize();
571 wxSize
wxRibbonPanel::GetPanelSizerMinSize() const
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.
579 // Check if is visible and not previously calculated
580 if(IsShown() && !m_smallest_unminimised_size
.IsFullySpecified())
582 return GetSizer()->CalcMin();
584 // else use previously calculated m_smallest_unminimised_size
585 wxClientDC
dc((wxRibbonPanel
*) this);
586 return m_art
->GetPanelClientSize(dc
,
588 m_smallest_unminimised_size
,
592 wxSize
wxRibbonPanel::GetPanelSizerBestSize() const
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)
601 wxSize
wxRibbonPanel::DoGetBestSize() const
603 // Ask sizer if present
606 wxClientDC
dc((wxRibbonPanel
*) this);
607 return m_art
->GetPanelSize(dc
, this, GetPanelSizerBestSize(), NULL
);
609 else if(GetChildren().GetCount() == 1)
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
);
617 return wxRibbonControl::DoGetBestSize();
620 bool wxRibbonPanel::Realize()
624 for (wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
626 node
= node
->GetNext())
628 wxRibbonControl
* child
= wxDynamicCast(node
->GetData(), wxRibbonControl
);
633 if(!child
->Realize())
639 wxSize
minimum_children_size(0, 0);
641 // Ask sizer if there is one present
644 minimum_children_size
= GetPanelSizerMinSize();
646 else if(GetChildren().GetCount() == 1)
648 minimum_children_size
= GetChildren().GetFirst()->GetData()->GetMinSize();
653 wxClientDC
temp_dc(this);
655 m_smallest_unminimised_size
=
656 m_art
->GetPanelSize(temp_dc
, this, minimum_children_size
, NULL
);
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
)
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
);
670 m_minimised_icon_resized
= m_minimised_icon
;
672 if(m_minimised_size
.x
> panel_min_size
.x
&&
673 m_minimised_size
.y
> panel_min_size
.y
)
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);
681 if(m_art
->GetFlags() & wxRIBBON_BAR_FLOW_VERTICAL
)
683 m_minimised_size
.x
= panel_min_size
.x
;
687 m_minimised_size
.y
= panel_min_size
.y
;
693 m_minimised_size
= wxSize(-1, -1);
696 return Layout() && status
;
699 bool wxRibbonPanel::Layout()
703 // Children are all invisible when minimised
707 // Get wxRibbonPanel client size
710 wxSize size
= m_art
->GetPanelClientSize(dc
, this, GetSize(), &position
);
712 // If there is a sizer, use it
715 GetSizer()->SetDimension(position
, size
); // SetSize and Layout()
717 else if(GetChildren().GetCount() == 1)
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());
726 void wxRibbonPanel::OnMouseClick(wxMouseEvent
& WXUNUSED(evt
))
730 if(m_expanded_panel
!= NULL
)
741 wxRibbonPanel
* wxRibbonPanel::GetExpandedDummy()
743 return m_expanded_dummy
;
746 wxRibbonPanel
* wxRibbonPanel::GetExpandedPanel()
748 return m_expanded_panel
;
751 bool wxRibbonPanel::ShowExpanded()
757 if(m_expanded_dummy
!= NULL
|| m_expanded_panel
!= NULL
)
762 wxSize size
= GetBestSize();
764 // Special case for flexible panel layout, where GetBestSize doesn't work
765 if (GetFlags() & wxRIBBON_PANEL_FLEXIBLE
)
767 size
= GetBestSizeForParentSize(wxSize(400, 1000));
770 wxPoint pos
= GetExpandedPosition(wxRect(GetScreenPosition(), GetSize()),
771 size
, m_preferred_expand_direction
).GetTopLeft();
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
);
777 m_expanded_panel
= new wxRibbonPanel(container
, wxID_ANY
,
778 GetLabel(), m_minimised_icon
, wxPoint(0, 0), size
, (m_flags
/* & ~wxRIBBON_PANEL_FLEXIBLE */));
780 m_expanded_panel
->SetArtProvider(m_art
);
781 m_expanded_panel
->m_expanded_dummy
= this;
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())
793 wxWindow
*child
= GetChildren().GetFirst()->GetData();
794 child
->Reparent(m_expanded_panel
);
798 // Move sizer to new panel
801 wxSizer
* sizer
= GetSizer();
802 SetSizer(NULL
, false);
803 m_expanded_panel
->SetSizer(sizer
);
806 m_expanded_panel
->Realize();
808 container
->SetMinClientSize(size
);
810 m_expanded_panel
->SetFocus();
815 bool wxRibbonPanel::ShouldSendEventToDummy(wxEvent
& evt
)
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
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
;
827 bool wxRibbonPanel::TryAfter(wxEvent
& evt
)
829 if(m_expanded_dummy
&& ShouldSendEventToDummy(evt
))
831 wxPropagateOnce
propagateOnce(evt
);
832 return m_expanded_dummy
->GetEventHandler()->ProcessEvent(evt
);
836 return wxRibbonControl::TryAfter(evt
);
840 static bool IsAncestorOf(wxWindow
*ancestor
, wxWindow
*window
)
842 while(window
!= NULL
)
844 wxWindow
*parent
= window
->GetParent();
845 if(parent
== ancestor
)
853 void wxRibbonPanel::OnKillFocus(wxFocusEvent
& evt
)
857 wxWindow
*receiver
= evt
.GetWindow();
858 if(IsAncestorOf(this, receiver
))
860 m_child_with_focus
= receiver
;
861 receiver
->Connect(wxEVT_KILL_FOCUS
,
862 wxFocusEventHandler(wxRibbonPanel::OnChildKillFocus
),
865 else if(receiver
== NULL
|| receiver
!= m_expanded_dummy
)
872 void wxRibbonPanel::OnChildKillFocus(wxFocusEvent
& evt
)
874 if(m_child_with_focus
== NULL
)
875 return; // Should never happen, but a check can't hurt
877 m_child_with_focus
->Disconnect(wxEVT_KILL_FOCUS
,
878 wxFocusEventHandler(wxRibbonPanel::OnChildKillFocus
), NULL
, this);
879 m_child_with_focus
= NULL
;
881 wxWindow
*receiver
= evt
.GetWindow();
882 if(receiver
== this || IsAncestorOf(this, receiver
))
884 m_child_with_focus
= receiver
;
885 receiver
->Connect(wxEVT_KILL_FOCUS
,
886 wxFocusEventHandler(wxRibbonPanel::OnChildKillFocus
), NULL
, this);
889 else if(receiver
== NULL
|| receiver
!= m_expanded_dummy
)
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.
902 bool wxRibbonPanel::HideExpanded()
904 if(m_expanded_dummy
== NULL
)
908 return m_expanded_panel
->HideExpanded();
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())
921 wxWindow
*child
= GetChildren().GetFirst()->GetData();
922 child
->Reparent(m_expanded_dummy
);
929 wxSizer
* sizer
= GetSizer();
930 SetSizer(NULL
, false);
931 m_expanded_dummy
->SetSizer(sizer
);
934 m_expanded_dummy
->m_expanded_panel
= NULL
;
935 m_expanded_dummy
->Realize();
936 m_expanded_dummy
->Refresh();
937 wxWindow
*parent
= GetParent();
944 wxRect
wxRibbonPanel::GetExpandedPosition(wxRect panel
,
945 wxSize expanded_size
,
946 wxDirection direction
)
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
958 bool primary_x
= false;
964 pos
.x
= panel
.GetX() + (panel
.GetWidth() - expanded_size
.GetWidth()) / 2;
965 pos
.y
= panel
.GetY() - expanded_size
.GetHeight();
970 pos
.x
= panel
.GetRight();
971 pos
.y
= panel
.GetY() + (panel
.GetHeight() - expanded_size
.GetHeight()) / 2;
975 pos
.x
= panel
.GetX() + (panel
.GetWidth() - expanded_size
.GetWidth()) / 2;
976 pos
.y
= panel
.GetBottom();
982 pos
.x
= panel
.GetX() - expanded_size
.GetWidth();
983 pos
.y
= panel
.GetY() + (panel
.GetHeight() - expanded_size
.GetHeight()) / 2;
987 wxRect
expanded(pos
, expanded_size
);
989 wxRect
best(expanded
);
990 int best_distance
= INT_MAX
;
992 const unsigned display_n
= wxDisplay::GetCount();
994 for(display_i
= 0; display_i
< display_n
; ++display_i
)
996 wxRect display
= wxDisplay(display_i
).GetGeometry();
998 if(display
.Contains(expanded
))
1002 else if(display
.Intersects(expanded
))
1004 wxRect
new_rect(expanded
);
1009 if(expanded
.GetRight() > display
.GetRight())
1011 distance
= expanded
.GetRight() - display
.GetRight();
1012 new_rect
.x
-= distance
;
1014 else if(expanded
.GetLeft() < display
.GetLeft())
1016 distance
= display
.GetLeft() - expanded
.GetLeft();
1017 new_rect
.x
+= distance
;
1022 if(expanded
.GetBottom() > display
.GetBottom())
1024 distance
= expanded
.GetBottom() - display
.GetBottom();
1025 new_rect
.y
-= distance
;
1027 else if(expanded
.GetTop() < display
.GetTop())
1029 distance
= display
.GetTop() - expanded
.GetTop();
1030 new_rect
.y
+= distance
;
1033 if(!display
.Contains(new_rect
))
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());
1042 // Squaring makes secondary moves more expensive (and also
1043 // prevents a negative cost)
1044 distance
+= dx
* dx
+ dy
* dy
;
1046 if(display
.Contains(new_rect
) && distance
< best_distance
)
1049 best_distance
= distance
;
1057 #endif // wxUSE_RIBBON