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 ///////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
17 #include "wx/ribbon/art.h"
18 #include "wx/ribbon/bar.h"
19 #include "wx/ribbon/panel.h"
20 #include "wx/dcbuffer.h"
21 #include "wx/display.h"
29 #include "wx/msw/private.h"
32 IMPLEMENT_CLASS(wxRibbonPanel
, wxRibbonControl
)
34 BEGIN_EVENT_TABLE(wxRibbonPanel
, wxRibbonControl
)
35 EVT_ENTER_WINDOW(wxRibbonPanel::OnMouseEnter
)
36 EVT_ERASE_BACKGROUND(wxRibbonPanel::OnEraseBackground
)
37 EVT_KILL_FOCUS(wxRibbonPanel::OnKillFocus
)
38 EVT_LEAVE_WINDOW(wxRibbonPanel::OnMouseLeave
)
39 EVT_LEFT_DOWN(wxRibbonPanel::OnMouseClick
)
40 EVT_PAINT(wxRibbonPanel::OnPaint
)
41 EVT_SIZE(wxRibbonPanel::OnSize
)
44 wxRibbonPanel::wxRibbonPanel()
48 wxRibbonPanel::wxRibbonPanel(wxWindow
* parent
,
50 const wxString
& label
,
51 const wxBitmap
& minimised_icon
,
55 : wxRibbonControl(parent
, id
, pos
, size
, wxBORDER_NONE
)
57 CommonInit(label
, minimised_icon
, style
);
60 wxRibbonPanel::~wxRibbonPanel()
64 m_expanded_panel
->m_expanded_dummy
= NULL
;
65 m_expanded_panel
->GetParent()->Destroy();
69 bool wxRibbonPanel::Create(wxWindow
* parent
,
71 const wxString
& label
,
77 if(!wxRibbonControl::Create(parent
, id
, pos
, size
, wxBORDER_NONE
))
82 CommonInit(label
, icon
, style
);
87 void wxRibbonPanel::SetArtProvider(wxRibbonArtProvider
* art
)
90 for ( wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
92 node
= node
->GetNext() )
94 wxWindow
* child
= node
->GetData();
95 wxRibbonControl
* ribbon_child
= wxDynamicCast(child
, wxRibbonControl
);
98 ribbon_child
->SetArtProvider(art
);
102 m_expanded_panel
->SetArtProvider(art
);
105 void wxRibbonPanel::CommonInit(const wxString
& label
, const wxBitmap
& icon
, long style
)
110 m_minimised_size
= wxDefaultSize
; // Unknown / none
111 m_smallest_unminimised_size
= wxSize(INT_MAX
, INT_MAX
); // Unknown / none
112 m_preferred_expand_direction
= wxSOUTH
;
113 m_expanded_dummy
= NULL
;
114 m_expanded_panel
= NULL
;
116 m_minimised_icon
= icon
;
122 wxRibbonControl
* parent
= wxDynamicCast(GetParent(), wxRibbonControl
);
125 m_art
= parent
->GetArtProvider();
130 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
131 SetMinSize(wxSize(20, 20));
134 bool wxRibbonPanel::IsMinimised() const
139 bool wxRibbonPanel::IsHovered() const
144 void wxRibbonPanel::OnMouseEnter(wxMouseEvent
& evt
)
146 TestPositionForHover(evt
.GetPosition());
149 void wxRibbonPanel::OnMouseEnterChild(wxMouseEvent
& evt
)
151 wxPoint pos
= evt
.GetPosition();
152 wxWindow
*child
= wxDynamicCast(evt
.GetEventObject(), wxWindow
);
155 pos
+= child
->GetPosition();
156 TestPositionForHover(pos
);
161 void wxRibbonPanel::OnMouseLeave(wxMouseEvent
& evt
)
163 TestPositionForHover(evt
.GetPosition());
166 void wxRibbonPanel::OnMouseLeaveChild(wxMouseEvent
& evt
)
168 wxPoint pos
= evt
.GetPosition();
169 wxWindow
*child
= wxDynamicCast(evt
.GetEventObject(), wxWindow
);
172 pos
+= child
->GetPosition();
173 TestPositionForHover(pos
);
178 void wxRibbonPanel::TestPositionForHover(const wxPoint
& pos
)
180 bool hovered
= false;
181 if(pos
.x
>= 0 && pos
.y
>= 0)
183 wxSize size
= GetSize();
184 if(pos
.x
< size
.GetWidth() && pos
.y
< size
.GetHeight())
189 if(hovered
!= m_hovered
)
196 void wxRibbonPanel::AddChild(wxWindowBase
*child
)
198 wxRibbonControl::AddChild(child
);
200 // Window enter / leave events count for only the window in question, not
201 // for children of the window. The panel wants to be in the hovered state
202 // whenever the mouse cursor is within its boundary, so the events need to
203 // be attached to children too.
204 child
->Connect(wxEVT_ENTER_WINDOW
, (wxObjectEventFunction
)&wxRibbonPanel::OnMouseEnterChild
, NULL
, this);
205 child
->Connect(wxEVT_LEAVE_WINDOW
, (wxObjectEventFunction
)&wxRibbonPanel::OnMouseLeaveChild
, NULL
, this);
208 void wxRibbonPanel::RemoveChild(wxWindowBase
*child
)
210 child
->Disconnect(wxEVT_ENTER_WINDOW
, (wxObjectEventFunction
)&wxRibbonPanel::OnMouseEnterChild
, NULL
, this);
211 child
->Disconnect(wxEVT_LEAVE_WINDOW
, (wxObjectEventFunction
)&wxRibbonPanel::OnMouseLeaveChild
, NULL
, this);
213 wxRibbonControl::RemoveChild(child
);
216 void wxRibbonPanel::OnSize(wxSizeEvent
& evt
)
224 void wxRibbonPanel::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
226 // At least on MSW, changing the size of a window will cause GetSize() to
227 // report the new size, but a size event may not be handled immediately.
228 // If this minimised check was performed in the OnSize handler, then
229 // GetSize() could return a size much larger than the minimised size while
230 // IsMinimised() returns true. This would then affect layout, as the panel
231 // will refuse to grow any larger while in limbo between minimised and non.
233 bool minimised
= (m_flags
& wxRIBBON_PANEL_NO_AUTO_MINIMISE
) == 0 &&
234 IsMinimised(wxSize(width
, height
));
235 if(minimised
!= m_minimised
)
237 m_minimised
= minimised
;
239 for (wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
241 node
= node
->GetNext())
243 node
->GetData()->Show(!minimised
);
249 wxRibbonControl::DoSetSize(x
, y
, width
, height
, sizeFlags
);
252 bool wxRibbonPanel::IsMinimised(wxSize at_size
) const
254 if(!m_minimised_size
.IsFullySpecified())
257 return (at_size
.GetX() <= m_minimised_size
.GetX() &&
258 at_size
.GetY() <= m_minimised_size
.GetY()) ||
259 at_size
.GetX() < m_smallest_unminimised_size
.GetX() ||
260 at_size
.GetY() < m_smallest_unminimised_size
.GetY();
263 void wxRibbonPanel::OnEraseBackground(wxEraseEvent
& WXUNUSED(evt
))
265 // All painting done in main paint handler to minimise flicker
268 void wxRibbonPanel::OnPaint(wxPaintEvent
& WXUNUSED(evt
))
270 wxAutoBufferedPaintDC
dc(this);
276 m_art
->DrawMinimisedPanel(dc
, this, GetSize(), m_minimised_icon_resized
);
280 m_art
->DrawPanelBackground(dc
, this, GetSize());
285 bool wxRibbonPanel::IsSizingContinuous() const
287 // A panel never sizes continuously, even if all of its children can,
288 // as it would appear out of place along side non-continuous panels.
292 wxSize
wxRibbonPanel::DoGetNextSmallerSize(wxOrientation direction
,
293 wxSize relative_to
) const
295 if(m_expanded_panel
!= NULL
)
297 // Next size depends upon children, who are currently in the
299 return m_expanded_panel
->DoGetNextSmallerSize(direction
, relative_to
);
302 // TODO: Check for, and delegate to, a sizer
304 // Simple (and common) case of single ribbon child
305 if(GetChildren().GetCount() == 1)
307 wxWindow
* child
= GetChildren().Item(0)->GetData();
308 wxRibbonControl
* ribbon_child
= wxDynamicCast(child
, wxRibbonControl
);
309 if(m_art
!= NULL
&& ribbon_child
!= NULL
)
312 wxSize child_relative
= m_art
->GetPanelClientSize(dc
, this, relative_to
, NULL
);
313 wxSize smaller
= ribbon_child
->GetNextSmallerSize(direction
, child_relative
);
314 if(smaller
== child_relative
)
316 if(CanAutoMinimise())
318 wxSize minimised
= m_minimised_size
;
322 minimised
.SetHeight(relative_to
.GetHeight());
325 minimised
.SetWidth(relative_to
.GetWidth());
339 return m_art
->GetPanelSize(dc
, this, smaller
, NULL
);
344 // Fallback: Decrease by 20% (or minimum size, whichever larger)
345 wxSize
current(relative_to
);
346 wxSize
minimum(GetMinSize());
347 if(direction
& wxHORIZONTAL
)
349 current
.x
= (current
.x
* 4) / 5;
350 if(current
.x
< minimum
.x
)
352 current
.x
= minimum
.x
;
355 if(direction
& wxVERTICAL
)
357 current
.y
= (current
.y
* 4) / 5;
358 if(current
.y
< minimum
.y
)
360 current
.y
= minimum
.y
;
366 wxSize
wxRibbonPanel::DoGetNextLargerSize(wxOrientation direction
,
367 wxSize relative_to
) const
369 if(m_expanded_panel
!= NULL
)
371 // Next size depends upon children, who are currently in the
373 return m_expanded_panel
->DoGetNextLargerSize(direction
, relative_to
);
376 if(IsMinimised(relative_to
))
378 wxSize current
= relative_to
;
379 wxSize min_size
= GetMinNotMinimisedSize();
383 if(min_size
.x
> current
.x
&& min_size
.y
== current
.y
)
387 if(min_size
.x
== current
.x
&& min_size
.y
> current
.y
)
391 if(min_size
.x
> current
.x
&& min_size
.y
> current
.y
)
399 // TODO: Check for, and delegate to, a sizer
401 // Simple (and common) case of single ribbon child
402 if(GetChildren().GetCount() == 1)
404 wxWindow
* child
= GetChildren().Item(0)->GetData();
405 wxRibbonControl
* ribbon_child
= wxDynamicCast(child
, wxRibbonControl
);
406 if(ribbon_child
!= NULL
)
409 wxSize child_relative
= m_art
->GetPanelClientSize(dc
, this, relative_to
, NULL
);
410 wxSize larger
= ribbon_child
->GetNextLargerSize(direction
, child_relative
);
411 if(larger
== child_relative
)
418 return m_art
->GetPanelSize(dc
, this, larger
, NULL
);
423 // Fallback: Increase by 25% (equal to a prior or subsequent 20% decrease)
424 // Note that due to rounding errors, this increase may not exactly equal a
425 // matching decrease - an ideal solution would not have these errors, but
426 // avoiding them is non-trivial unless an increase is by 100% rather than
427 // a fractional amount. This would then be non-ideal as the resizes happen
428 // at very large intervals.
429 wxSize
current(relative_to
);
430 if(direction
& wxHORIZONTAL
)
432 current
.x
= (current
.x
* 5 + 3) / 4;
434 if(direction
& wxVERTICAL
)
436 current
.y
= (current
.y
* 5 + 3) / 4;
441 bool wxRibbonPanel::CanAutoMinimise() const
443 return (m_flags
& wxRIBBON_PANEL_NO_AUTO_MINIMISE
) == 0
444 && m_minimised_size
.IsFullySpecified();
447 wxSize
wxRibbonPanel::GetMinSize() const
449 if(m_expanded_panel
!= NULL
)
451 // Minimum size depends upon children, who are currently in the
453 return m_expanded_panel
->GetMinSize();
456 if(CanAutoMinimise())
458 return m_minimised_size
;
462 return GetMinNotMinimisedSize();
466 wxSize
wxRibbonPanel::GetMinNotMinimisedSize() const
470 // Common case of no sizer and single child taking up the entire panel
471 if(GetChildren().GetCount() == 1)
473 wxWindow
* child
= GetChildren().Item(0)->GetData();
475 return m_art
->GetPanelSize(dc
, this, child
->GetMinSize(), NULL
);
478 return wxRibbonControl::GetMinSize();
481 wxSize
wxRibbonPanel::DoGetBestSize() const
485 // Common case of no sizer and single child taking up the entire panel
486 if(GetChildren().GetCount() == 1)
488 wxWindow
* child
= GetChildren().Item(0)->GetData();
490 return m_art
->GetPanelSize(dc
, this, child
->GetBestSize(), NULL
);
493 return wxRibbonControl::DoGetBestSize();
496 bool wxRibbonPanel::Realize()
500 for (wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
502 node
= node
->GetNext())
504 wxRibbonControl
* child
= wxDynamicCast(node
->GetData(), wxRibbonControl
);
509 if(!child
->Realize())
515 wxSize
minimum_children_size(0, 0);
516 // TODO: Ask sizer if there is one
517 if(GetChildren().GetCount() == 1)
519 minimum_children_size
= GetChildren().GetFirst()->GetData()->GetMinSize();
526 m_smallest_unminimised_size
=
527 m_art
->GetPanelSize(temp_dc
, this, minimum_children_size
, NULL
);
530 wxSize panel_min_size
= GetMinNotMinimisedSize();
531 m_minimised_size
= m_art
->GetMinimisedPanelMinimumSize(temp_dc
, this,
532 &bitmap_size
, &m_preferred_expand_direction
);
533 if(m_minimised_icon
.IsOk() && m_minimised_icon
.GetSize() != bitmap_size
)
535 wxImage
img(m_minimised_icon
.ConvertToImage());
536 img
.Rescale(bitmap_size
.GetWidth(), bitmap_size
.GetHeight(), wxIMAGE_QUALITY_HIGH
);
537 m_minimised_icon_resized
= wxBitmap(img
);
541 m_minimised_icon_resized
= m_minimised_icon
;
543 if(m_minimised_size
.x
> panel_min_size
.x
&&
544 m_minimised_size
.y
> panel_min_size
.y
)
546 // No point in having a minimised size which is larger than the
547 // minimum size which the children can go to.
548 m_minimised_size
= wxSize(-1, -1);
552 if(m_art
->GetFlags() & wxRIBBON_BAR_FLOW_VERTICAL
)
554 m_minimised_size
.x
= panel_min_size
.x
;
558 m_minimised_size
.y
= panel_min_size
.y
;
564 m_minimised_size
= wxSize(-1, -1);
567 return Layout() && status
;
570 bool wxRibbonPanel::Layout()
574 // Children are all invisible when minimised
578 // TODO: Delegate to a sizer
580 // Common case of no sizer and single child taking up the entire panel
581 if(GetChildren().GetCount() == 1)
583 wxWindow
* child
= GetChildren().Item(0)->GetData();
586 wxSize size
= m_art
->GetPanelClientSize(dc
, this, GetSize(), &position
);
587 child
->SetSize(position
.x
, position
.y
, size
.GetWidth(), size
.GetHeight());
592 void wxRibbonPanel::OnMouseClick(wxMouseEvent
& WXUNUSED(evt
))
596 if(m_expanded_panel
!= NULL
)
607 wxRibbonPanel
* wxRibbonPanel::GetExpandedDummy()
609 return m_expanded_dummy
;
612 wxRibbonPanel
* wxRibbonPanel::GetExpandedPanel()
614 return m_expanded_panel
;
617 bool wxRibbonPanel::ShowExpanded()
623 if(m_expanded_dummy
!= NULL
|| m_expanded_panel
!= NULL
)
628 wxSize size
= GetBestSize();
629 wxPoint pos
= GetExpandedPosition(wxRect(GetScreenPosition(), GetSize()),
630 size
, m_preferred_expand_direction
).GetTopLeft();
632 // Need a top-level frame to contain the expanded panel
633 wxFrame
*container
= new wxFrame(NULL
, wxID_ANY
, GetLabel(),
634 pos
, size
, wxFRAME_NO_TASKBAR
| wxBORDER_NONE
);
636 m_expanded_panel
= new wxRibbonPanel(container
, wxID_ANY
,
637 GetLabel(), m_minimised_icon
, wxPoint(0, 0), size
, m_flags
);
639 m_expanded_panel
->SetArtProvider(m_art
);
640 m_expanded_panel
->m_expanded_dummy
= this;
642 // Move all children to the new panel.
643 // Conceptually it might be simpler to reparent this entire panel to the
644 // container and create a new panel to sit in its place while expanded.
645 // This approach has a problem though - when the panel is reinserted into
646 // its original parent, it'll be at a different position in the child list
647 // and thus assume a new position.
648 // NB: Children iterators not used as behaviour is not well defined
649 // when iterating over a container which is being emptied
650 while(!GetChildren().IsEmpty())
652 wxWindow
*child
= GetChildren().GetFirst()->GetData();
653 child
->Reparent(m_expanded_panel
);
657 // TODO: Move sizer to new panel
659 m_expanded_panel
->Realize();
662 m_expanded_panel
->SetFocus();
667 bool wxRibbonPanel::ShouldSendEventToDummy(wxEvent
& evt
)
669 // For an expanded panel, filter events between being sent up to the
670 // floating top level window or to the dummy panel sitting in the ribbon
673 // Child focus events should not be redirected, as the child would not be a
674 // child of the window the event is redirected to. All other command events
675 // seem to be suitable for redirecting.
676 return evt
.IsCommandEvent() && evt
.GetEventType() != wxEVT_CHILD_FOCUS
;
679 bool wxRibbonPanel::TryAfter(wxEvent
& evt
)
681 if(m_expanded_dummy
&& ShouldSendEventToDummy(evt
))
683 wxPropagateOnce
propagateOnce(evt
);
684 return m_expanded_dummy
->GetEventHandler()->ProcessEvent(evt
);
688 return wxRibbonControl::TryAfter(evt
);
692 static bool IsAncestorOf(wxWindow
*ancestor
, wxWindow
*window
)
694 while(window
!= NULL
)
696 wxWindow
*parent
= window
->GetParent();
697 if(parent
== ancestor
)
705 void wxRibbonPanel::OnKillFocus(wxFocusEvent
& evt
)
709 wxWindow
*receiver
= evt
.GetWindow();
710 if(IsAncestorOf(this, receiver
))
712 m_child_with_focus
= receiver
;
713 receiver
->Connect(wxEVT_KILL_FOCUS
,
714 wxFocusEventHandler(wxRibbonPanel::OnChildKillFocus
),
717 else if(receiver
== NULL
|| receiver
!= m_expanded_dummy
)
724 void wxRibbonPanel::OnChildKillFocus(wxFocusEvent
& evt
)
726 if(m_child_with_focus
== NULL
)
727 return; // Should never happen, but a check can't hurt
729 m_child_with_focus
->Disconnect(wxEVT_KILL_FOCUS
,
730 wxFocusEventHandler(wxRibbonPanel::OnChildKillFocus
), NULL
, this);
731 m_child_with_focus
= NULL
;
733 wxWindow
*receiver
= evt
.GetWindow();
734 if(receiver
== this || IsAncestorOf(this, receiver
))
736 m_child_with_focus
= receiver
;
737 receiver
->Connect(wxEVT_KILL_FOCUS
,
738 wxFocusEventHandler(wxRibbonPanel::OnChildKillFocus
), NULL
, this);
741 else if(receiver
== NULL
|| receiver
!= m_expanded_dummy
)
744 // Do not skip event, as the panel has been de-expanded, causing the
745 // child with focus to be reparented (and hidden). If the event
746 // continues propogation then bad things happen.
754 bool wxRibbonPanel::HideExpanded()
756 if(m_expanded_dummy
== NULL
)
760 return m_expanded_panel
->HideExpanded();
768 // Move children back to original panel
769 // NB: Children iterators not used as behaviour is not well defined
770 // when iterating over a container which is being emptied
771 while(!GetChildren().IsEmpty())
773 wxWindow
*child
= GetChildren().GetFirst()->GetData();
774 child
->Reparent(m_expanded_dummy
);
778 // TODO: Move sizer back
780 m_expanded_dummy
->m_expanded_panel
= NULL
;
781 m_expanded_dummy
->Realize();
782 m_expanded_dummy
->Refresh();
783 wxWindow
*parent
= GetParent();
790 wxRect
wxRibbonPanel::GetExpandedPosition(wxRect panel
,
791 wxSize expanded_size
,
792 wxDirection direction
)
795 // 1) Determine primary position based on requested direction
796 // 2) Move the position so that it sits entirely within a display
797 // (for single monitor systems, this moves it into the display region,
798 // but for multiple monitors, it does so without splitting it over
799 // more than one display)
800 // 2.1) Move in the primary axis
801 // 2.2) Move in the secondary axis
804 bool primary_x
= false;
810 pos
.x
= panel
.GetX() + (panel
.GetWidth() - expanded_size
.GetWidth()) / 2;
811 pos
.y
= panel
.GetY() - expanded_size
.GetHeight();
816 pos
.x
= panel
.GetRight();
817 pos
.y
= panel
.GetY() + (panel
.GetHeight() - expanded_size
.GetHeight()) / 2;
821 pos
.x
= panel
.GetX() + (panel
.GetWidth() - expanded_size
.GetWidth()) / 2;
822 pos
.y
= panel
.GetBottom();
828 pos
.x
= panel
.GetX() - expanded_size
.GetWidth();
829 pos
.y
= panel
.GetY() + (panel
.GetHeight() - expanded_size
.GetHeight()) / 2;
833 wxRect
expanded(pos
, expanded_size
);
835 wxRect
best(expanded
);
836 int best_distance
= INT_MAX
;
838 const unsigned display_n
= wxDisplay::GetCount();
840 for(display_i
= 0; display_i
< display_n
; ++display_i
)
842 wxRect display
= wxDisplay(display_i
).GetGeometry();
844 if(display
.Contains(expanded
))
848 else if(display
.Intersects(expanded
))
850 wxRect
new_rect(expanded
);
855 if(expanded
.GetRight() > display
.GetRight())
857 distance
= expanded
.GetRight() - display
.GetRight();
858 new_rect
.x
-= distance
;
860 else if(expanded
.GetLeft() < display
.GetLeft())
862 distance
= display
.GetLeft() - expanded
.GetLeft();
863 new_rect
.x
+= distance
;
868 if(expanded
.GetBottom() > display
.GetBottom())
870 distance
= expanded
.GetBottom() - display
.GetBottom();
871 new_rect
.y
-= distance
;
873 else if(expanded
.GetTop() < display
.GetTop())
875 distance
= display
.GetTop() - expanded
.GetTop();
876 new_rect
.y
+= distance
;
879 if(!display
.Contains(new_rect
))
881 // Tried moving in primary axis, but failed.
882 // Hence try moving in the secondary axis.
883 int dx
= secondary_x
* (panel
.GetWidth() + expanded_size
.GetWidth());
884 int dy
= secondary_y
* (panel
.GetHeight() + expanded_size
.GetHeight());
888 // Squaring makes secondary moves more expensive (and also
889 // prevents a negative cost)
890 distance
+= dx
* dx
+ dy
* dy
;
892 if(display
.Contains(new_rect
) && distance
< best_distance
)
895 best_distance
= distance
;
903 #endif // wxUSE_RIBBON