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"
30 #include "wx/msw/private.h"
33 IMPLEMENT_CLASS(wxRibbonPanel
, wxRibbonControl
)
35 BEGIN_EVENT_TABLE(wxRibbonPanel
, wxRibbonControl
)
36 EVT_ENTER_WINDOW(wxRibbonPanel::OnMouseEnter
)
37 EVT_ERASE_BACKGROUND(wxRibbonPanel::OnEraseBackground
)
38 EVT_KILL_FOCUS(wxRibbonPanel::OnKillFocus
)
39 EVT_LEAVE_WINDOW(wxRibbonPanel::OnMouseLeave
)
40 EVT_LEFT_DOWN(wxRibbonPanel::OnMouseClick
)
41 EVT_PAINT(wxRibbonPanel::OnPaint
)
42 EVT_SIZE(wxRibbonPanel::OnSize
)
45 wxRibbonPanel::wxRibbonPanel()
49 wxRibbonPanel::wxRibbonPanel(wxWindow
* parent
,
51 const wxString
& label
,
52 const wxBitmap
& minimised_icon
,
56 : wxRibbonControl(parent
, id
, pos
, size
, wxBORDER_NONE
)
58 CommonInit(label
, minimised_icon
, style
);
61 wxRibbonPanel::~wxRibbonPanel()
65 m_expanded_panel
->m_expanded_dummy
= NULL
;
66 m_expanded_panel
->GetParent()->Destroy();
70 bool wxRibbonPanel::Create(wxWindow
* parent
,
72 const wxString
& label
,
78 if(!wxRibbonControl::Create(parent
, id
, pos
, size
, wxBORDER_NONE
))
83 CommonInit(label
, icon
, style
);
88 void wxRibbonPanel::SetArtProvider(wxRibbonArtProvider
* art
)
91 for ( wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
93 node
= node
->GetNext() )
95 wxWindow
* child
= node
->GetData();
96 wxRibbonControl
* ribbon_child
= wxDynamicCast(child
, wxRibbonControl
);
99 ribbon_child
->SetArtProvider(art
);
103 m_expanded_panel
->SetArtProvider(art
);
106 void wxRibbonPanel::CommonInit(const wxString
& label
, const wxBitmap
& icon
, long style
)
111 m_minimised_size
= wxDefaultSize
; // Unknown / none
112 m_smallest_unminimised_size
= wxSize(INT_MAX
, INT_MAX
); // Unknown / none
113 m_preferred_expand_direction
= wxSOUTH
;
114 m_expanded_dummy
= NULL
;
115 m_expanded_panel
= NULL
;
117 m_minimised_icon
= icon
;
123 wxRibbonControl
* parent
= wxDynamicCast(GetParent(), wxRibbonControl
);
126 m_art
= parent
->GetArtProvider();
131 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
132 SetMinSize(wxSize(20, 20));
135 bool wxRibbonPanel::IsMinimised() const
140 bool wxRibbonPanel::IsHovered() const
145 void wxRibbonPanel::OnMouseEnter(wxMouseEvent
& evt
)
147 TestPositionForHover(evt
.GetPosition());
150 void wxRibbonPanel::OnMouseEnterChild(wxMouseEvent
& evt
)
152 wxPoint pos
= evt
.GetPosition();
153 wxWindow
*child
= wxDynamicCast(evt
.GetEventObject(), wxWindow
);
156 pos
+= child
->GetPosition();
157 TestPositionForHover(pos
);
162 void wxRibbonPanel::OnMouseLeave(wxMouseEvent
& evt
)
164 TestPositionForHover(evt
.GetPosition());
167 void wxRibbonPanel::OnMouseLeaveChild(wxMouseEvent
& evt
)
169 wxPoint pos
= evt
.GetPosition();
170 wxWindow
*child
= wxDynamicCast(evt
.GetEventObject(), wxWindow
);
173 pos
+= child
->GetPosition();
174 TestPositionForHover(pos
);
179 void wxRibbonPanel::TestPositionForHover(const wxPoint
& pos
)
181 bool hovered
= false;
182 if(pos
.x
>= 0 && pos
.y
>= 0)
184 wxSize size
= GetSize();
185 if(pos
.x
< size
.GetWidth() && pos
.y
< size
.GetHeight())
190 if(hovered
!= m_hovered
)
197 void wxRibbonPanel::AddChild(wxWindowBase
*child
)
199 wxRibbonControl::AddChild(child
);
201 // Window enter / leave events count for only the window in question, not
202 // for children of the window. The panel wants to be in the hovered state
203 // whenever the mouse cursor is within its boundary, so the events need to
204 // be attached to children too.
205 child
->Connect(wxEVT_ENTER_WINDOW
, (wxObjectEventFunction
)&wxRibbonPanel::OnMouseEnterChild
, NULL
, this);
206 child
->Connect(wxEVT_LEAVE_WINDOW
, (wxObjectEventFunction
)&wxRibbonPanel::OnMouseLeaveChild
, NULL
, this);
209 void wxRibbonPanel::RemoveChild(wxWindowBase
*child
)
211 child
->Disconnect(wxEVT_ENTER_WINDOW
, (wxObjectEventFunction
)&wxRibbonPanel::OnMouseEnterChild
, NULL
, this);
212 child
->Disconnect(wxEVT_LEAVE_WINDOW
, (wxObjectEventFunction
)&wxRibbonPanel::OnMouseLeaveChild
, NULL
, this);
214 wxRibbonControl::RemoveChild(child
);
217 void wxRibbonPanel::OnSize(wxSizeEvent
& evt
)
225 void wxRibbonPanel::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
227 // At least on MSW, changing the size of a window will cause GetSize() to
228 // report the new size, but a size event may not be handled immediately.
229 // If this minimised check was performed in the OnSize handler, then
230 // GetSize() could return a size much larger than the minimised size while
231 // IsMinimised() returns true. This would then affect layout, as the panel
232 // will refuse to grow any larger while in limbo between minimised and non.
234 bool minimised
= (m_flags
& wxRIBBON_PANEL_NO_AUTO_MINIMISE
) == 0 &&
235 IsMinimised(wxSize(width
, height
));
236 if(minimised
!= m_minimised
)
238 m_minimised
= minimised
;
240 for (wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
242 node
= node
->GetNext())
244 node
->GetData()->Show(!minimised
);
250 wxRibbonControl::DoSetSize(x
, y
, width
, height
, sizeFlags
);
253 bool wxRibbonPanel::IsMinimised(wxSize at_size
) const
255 if(!m_minimised_size
.IsFullySpecified())
258 return (at_size
.GetX() <= m_minimised_size
.GetX() &&
259 at_size
.GetY() <= m_minimised_size
.GetY()) ||
260 at_size
.GetX() < m_smallest_unminimised_size
.GetX() ||
261 at_size
.GetY() < m_smallest_unminimised_size
.GetY();
264 void wxRibbonPanel::OnEraseBackground(wxEraseEvent
& WXUNUSED(evt
))
266 // All painting done in main paint handler to minimise flicker
269 void wxRibbonPanel::OnPaint(wxPaintEvent
& WXUNUSED(evt
))
271 wxAutoBufferedPaintDC
dc(this);
277 m_art
->DrawMinimisedPanel(dc
, this, GetSize(), m_minimised_icon_resized
);
281 m_art
->DrawPanelBackground(dc
, this, GetSize());
286 bool wxRibbonPanel::IsSizingContinuous() const
288 // A panel never sizes continuously, even if all of its children can,
289 // as it would appear out of place along side non-continuous panels.
293 wxSize
wxRibbonPanel::DoGetNextSmallerSize(wxOrientation direction
,
294 wxSize relative_to
) const
296 if(m_expanded_panel
!= NULL
)
298 // Next size depends upon children, who are currently in the
300 return m_expanded_panel
->DoGetNextSmallerSize(direction
, relative_to
);
303 // TODO: Check for, and delegate to, a sizer
305 // Simple (and common) case of single ribbon child
306 if(GetChildren().GetCount() == 1)
308 wxWindow
* child
= GetChildren().Item(0)->GetData();
309 wxRibbonControl
* ribbon_child
= wxDynamicCast(child
, wxRibbonControl
);
310 if(m_art
!= NULL
&& ribbon_child
!= NULL
)
312 wxClientDC
dc((wxRibbonPanel
*) this);
313 wxSize child_relative
= m_art
->GetPanelClientSize(dc
, this, relative_to
, NULL
);
314 wxSize smaller
= ribbon_child
->GetNextSmallerSize(direction
, child_relative
);
315 if(smaller
== child_relative
)
317 if(CanAutoMinimise())
319 wxSize minimised
= m_minimised_size
;
323 minimised
.SetHeight(relative_to
.GetHeight());
326 minimised
.SetWidth(relative_to
.GetWidth());
340 return m_art
->GetPanelSize(dc
, this, smaller
, NULL
);
345 // Fallback: Decrease by 20% (or minimum size, whichever larger)
346 wxSize
current(relative_to
);
347 wxSize
minimum(GetMinSize());
348 if(direction
& wxHORIZONTAL
)
350 current
.x
= (current
.x
* 4) / 5;
351 if(current
.x
< minimum
.x
)
353 current
.x
= minimum
.x
;
356 if(direction
& wxVERTICAL
)
358 current
.y
= (current
.y
* 4) / 5;
359 if(current
.y
< minimum
.y
)
361 current
.y
= minimum
.y
;
367 wxSize
wxRibbonPanel::DoGetNextLargerSize(wxOrientation direction
,
368 wxSize relative_to
) const
370 if(m_expanded_panel
!= NULL
)
372 // Next size depends upon children, who are currently in the
374 return m_expanded_panel
->DoGetNextLargerSize(direction
, relative_to
);
377 if(IsMinimised(relative_to
))
379 wxSize current
= relative_to
;
380 wxSize min_size
= GetMinNotMinimisedSize();
384 if(min_size
.x
> current
.x
&& min_size
.y
== current
.y
)
388 if(min_size
.x
== current
.x
&& min_size
.y
> current
.y
)
392 if(min_size
.x
> current
.x
&& min_size
.y
> current
.y
)
400 // TODO: Check for, and delegate to, a sizer
402 // Simple (and common) case of single ribbon child
403 if(GetChildren().GetCount() == 1)
405 wxWindow
* child
= GetChildren().Item(0)->GetData();
406 wxRibbonControl
* ribbon_child
= wxDynamicCast(child
, wxRibbonControl
);
407 if(ribbon_child
!= NULL
)
409 wxClientDC
dc((wxRibbonPanel
*) this);
410 wxSize child_relative
= m_art
->GetPanelClientSize(dc
, this, relative_to
, NULL
);
411 wxSize larger
= ribbon_child
->GetNextLargerSize(direction
, child_relative
);
412 if(larger
== child_relative
)
418 wxClientDC
dc((wxRibbonPanel
*) this);
419 return m_art
->GetPanelSize(dc
, this, larger
, NULL
);
424 // Fallback: Increase by 25% (equal to a prior or subsequent 20% decrease)
425 // Note that due to rounding errors, this increase may not exactly equal a
426 // matching decrease - an ideal solution would not have these errors, but
427 // avoiding them is non-trivial unless an increase is by 100% rather than
428 // a fractional amount. This would then be non-ideal as the resizes happen
429 // at very large intervals.
430 wxSize
current(relative_to
);
431 if(direction
& wxHORIZONTAL
)
433 current
.x
= (current
.x
* 5 + 3) / 4;
435 if(direction
& wxVERTICAL
)
437 current
.y
= (current
.y
* 5 + 3) / 4;
442 bool wxRibbonPanel::CanAutoMinimise() const
444 return (m_flags
& wxRIBBON_PANEL_NO_AUTO_MINIMISE
) == 0
445 && m_minimised_size
.IsFullySpecified();
448 wxSize
wxRibbonPanel::GetMinSize() const
450 if(m_expanded_panel
!= NULL
)
452 // Minimum size depends upon children, who are currently in the
454 return m_expanded_panel
->GetMinSize();
457 if(CanAutoMinimise())
459 return m_minimised_size
;
463 return GetMinNotMinimisedSize();
467 wxSize
wxRibbonPanel::GetMinNotMinimisedSize() const
471 // Common case of no sizer and single child taking up the entire panel
472 if(GetChildren().GetCount() == 1)
474 wxWindow
* child
= GetChildren().Item(0)->GetData();
475 wxClientDC
dc((wxRibbonPanel
*) this);
476 return m_art
->GetPanelSize(dc
, this, child
->GetMinSize(), NULL
);
479 return wxRibbonControl::GetMinSize();
482 wxSize
wxRibbonPanel::DoGetBestSize() const
486 // Common case of no sizer and single child taking up the entire panel
487 if(GetChildren().GetCount() == 1)
489 wxWindow
* child
= GetChildren().Item(0)->GetData();
490 wxClientDC
dc((wxRibbonPanel
*) this);
491 return m_art
->GetPanelSize(dc
, this, child
->GetBestSize(), NULL
);
494 return wxRibbonControl::DoGetBestSize();
497 bool wxRibbonPanel::Realize()
501 for (wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
503 node
= node
->GetNext())
505 wxRibbonControl
* child
= wxDynamicCast(node
->GetData(), wxRibbonControl
);
510 if(!child
->Realize())
516 wxSize
minimum_children_size(0, 0);
517 // TODO: Ask sizer if there is one
518 if(GetChildren().GetCount() == 1)
520 minimum_children_size
= GetChildren().GetFirst()->GetData()->GetMinSize();
525 wxClientDC
temp_dc(this);
527 m_smallest_unminimised_size
=
528 m_art
->GetPanelSize(temp_dc
, this, minimum_children_size
, NULL
);
531 wxSize panel_min_size
= GetMinNotMinimisedSize();
532 m_minimised_size
= m_art
->GetMinimisedPanelMinimumSize(temp_dc
, this,
533 &bitmap_size
, &m_preferred_expand_direction
);
534 if(m_minimised_icon
.IsOk() && m_minimised_icon
.GetSize() != bitmap_size
)
536 wxImage
img(m_minimised_icon
.ConvertToImage());
537 img
.Rescale(bitmap_size
.GetWidth(), bitmap_size
.GetHeight(), wxIMAGE_QUALITY_HIGH
);
538 m_minimised_icon_resized
= wxBitmap(img
);
542 m_minimised_icon_resized
= m_minimised_icon
;
544 if(m_minimised_size
.x
> panel_min_size
.x
&&
545 m_minimised_size
.y
> panel_min_size
.y
)
547 // No point in having a minimised size which is larger than the
548 // minimum size which the children can go to.
549 m_minimised_size
= wxSize(-1, -1);
553 if(m_art
->GetFlags() & wxRIBBON_BAR_FLOW_VERTICAL
)
555 m_minimised_size
.x
= panel_min_size
.x
;
559 m_minimised_size
.y
= panel_min_size
.y
;
565 m_minimised_size
= wxSize(-1, -1);
568 return Layout() && status
;
571 bool wxRibbonPanel::Layout()
575 // Children are all invisible when minimised
579 // TODO: Delegate to a sizer
581 // Common case of no sizer and single child taking up the entire panel
582 if(GetChildren().GetCount() == 1)
584 wxWindow
* child
= GetChildren().Item(0)->GetData();
587 wxSize size
= m_art
->GetPanelClientSize(dc
, this, GetSize(), &position
);
588 child
->SetSize(position
.x
, position
.y
, size
.GetWidth(), size
.GetHeight());
593 void wxRibbonPanel::OnMouseClick(wxMouseEvent
& WXUNUSED(evt
))
597 if(m_expanded_panel
!= NULL
)
608 wxRibbonPanel
* wxRibbonPanel::GetExpandedDummy()
610 return m_expanded_dummy
;
613 wxRibbonPanel
* wxRibbonPanel::GetExpandedPanel()
615 return m_expanded_panel
;
618 bool wxRibbonPanel::ShowExpanded()
624 if(m_expanded_dummy
!= NULL
|| m_expanded_panel
!= NULL
)
629 wxSize size
= GetBestSize();
630 wxPoint pos
= GetExpandedPosition(wxRect(GetScreenPosition(), GetSize()),
631 size
, m_preferred_expand_direction
).GetTopLeft();
633 // Need a top-level frame to contain the expanded panel
634 wxFrame
*container
= new wxFrame(NULL
, wxID_ANY
, GetLabel(),
635 pos
, size
, wxFRAME_NO_TASKBAR
| wxBORDER_NONE
);
637 m_expanded_panel
= new wxRibbonPanel(container
, wxID_ANY
,
638 GetLabel(), m_minimised_icon
, wxPoint(0, 0), size
, m_flags
);
640 m_expanded_panel
->SetArtProvider(m_art
);
641 m_expanded_panel
->m_expanded_dummy
= this;
643 // Move all children to the new panel.
644 // Conceptually it might be simpler to reparent this entire panel to the
645 // container and create a new panel to sit in its place while expanded.
646 // This approach has a problem though - when the panel is reinserted into
647 // its original parent, it'll be at a different position in the child list
648 // and thus assume a new position.
649 // NB: Children iterators not used as behaviour is not well defined
650 // when iterating over a container which is being emptied
651 while(!GetChildren().IsEmpty())
653 wxWindow
*child
= GetChildren().GetFirst()->GetData();
654 child
->Reparent(m_expanded_panel
);
658 // TODO: Move sizer to new panel
660 m_expanded_panel
->Realize();
663 m_expanded_panel
->SetFocus();
668 bool wxRibbonPanel::ShouldSendEventToDummy(wxEvent
& evt
)
670 // For an expanded panel, filter events between being sent up to the
671 // floating top level window or to the dummy panel sitting in the ribbon
674 // Child focus events should not be redirected, as the child would not be a
675 // child of the window the event is redirected to. All other command events
676 // seem to be suitable for redirecting.
677 return evt
.IsCommandEvent() && evt
.GetEventType() != wxEVT_CHILD_FOCUS
;
680 bool wxRibbonPanel::TryAfter(wxEvent
& evt
)
682 if(m_expanded_dummy
&& ShouldSendEventToDummy(evt
))
684 wxPropagateOnce
propagateOnce(evt
);
685 return m_expanded_dummy
->GetEventHandler()->ProcessEvent(evt
);
689 return wxRibbonControl::TryAfter(evt
);
693 static bool IsAncestorOf(wxWindow
*ancestor
, wxWindow
*window
)
695 while(window
!= NULL
)
697 wxWindow
*parent
= window
->GetParent();
698 if(parent
== ancestor
)
706 void wxRibbonPanel::OnKillFocus(wxFocusEvent
& evt
)
710 wxWindow
*receiver
= evt
.GetWindow();
711 if(IsAncestorOf(this, receiver
))
713 m_child_with_focus
= receiver
;
714 receiver
->Connect(wxEVT_KILL_FOCUS
,
715 wxFocusEventHandler(wxRibbonPanel::OnChildKillFocus
),
718 else if(receiver
== NULL
|| receiver
!= m_expanded_dummy
)
725 void wxRibbonPanel::OnChildKillFocus(wxFocusEvent
& evt
)
727 if(m_child_with_focus
== NULL
)
728 return; // Should never happen, but a check can't hurt
730 m_child_with_focus
->Disconnect(wxEVT_KILL_FOCUS
,
731 wxFocusEventHandler(wxRibbonPanel::OnChildKillFocus
), NULL
, this);
732 m_child_with_focus
= NULL
;
734 wxWindow
*receiver
= evt
.GetWindow();
735 if(receiver
== this || IsAncestorOf(this, receiver
))
737 m_child_with_focus
= receiver
;
738 receiver
->Connect(wxEVT_KILL_FOCUS
,
739 wxFocusEventHandler(wxRibbonPanel::OnChildKillFocus
), NULL
, this);
742 else if(receiver
== NULL
|| receiver
!= m_expanded_dummy
)
745 // Do not skip event, as the panel has been de-expanded, causing the
746 // child with focus to be reparented (and hidden). If the event
747 // continues propogation then bad things happen.
755 bool wxRibbonPanel::HideExpanded()
757 if(m_expanded_dummy
== NULL
)
761 return m_expanded_panel
->HideExpanded();
769 // Move children back to original panel
770 // NB: Children iterators not used as behaviour is not well defined
771 // when iterating over a container which is being emptied
772 while(!GetChildren().IsEmpty())
774 wxWindow
*child
= GetChildren().GetFirst()->GetData();
775 child
->Reparent(m_expanded_dummy
);
779 // TODO: Move sizer back
781 m_expanded_dummy
->m_expanded_panel
= NULL
;
782 m_expanded_dummy
->Realize();
783 m_expanded_dummy
->Refresh();
784 wxWindow
*parent
= GetParent();
791 wxRect
wxRibbonPanel::GetExpandedPosition(wxRect panel
,
792 wxSize expanded_size
,
793 wxDirection direction
)
796 // 1) Determine primary position based on requested direction
797 // 2) Move the position so that it sits entirely within a display
798 // (for single monitor systems, this moves it into the display region,
799 // but for multiple monitors, it does so without splitting it over
800 // more than one display)
801 // 2.1) Move in the primary axis
802 // 2.2) Move in the secondary axis
805 bool primary_x
= false;
811 pos
.x
= panel
.GetX() + (panel
.GetWidth() - expanded_size
.GetWidth()) / 2;
812 pos
.y
= panel
.GetY() - expanded_size
.GetHeight();
817 pos
.x
= panel
.GetRight();
818 pos
.y
= panel
.GetY() + (panel
.GetHeight() - expanded_size
.GetHeight()) / 2;
822 pos
.x
= panel
.GetX() + (panel
.GetWidth() - expanded_size
.GetWidth()) / 2;
823 pos
.y
= panel
.GetBottom();
829 pos
.x
= panel
.GetX() - expanded_size
.GetWidth();
830 pos
.y
= panel
.GetY() + (panel
.GetHeight() - expanded_size
.GetHeight()) / 2;
834 wxRect
expanded(pos
, expanded_size
);
836 wxRect
best(expanded
);
837 int best_distance
= INT_MAX
;
839 const unsigned display_n
= wxDisplay::GetCount();
841 for(display_i
= 0; display_i
< display_n
; ++display_i
)
843 wxRect display
= wxDisplay(display_i
).GetGeometry();
845 if(display
.Contains(expanded
))
849 else if(display
.Intersects(expanded
))
851 wxRect
new_rect(expanded
);
856 if(expanded
.GetRight() > display
.GetRight())
858 distance
= expanded
.GetRight() - display
.GetRight();
859 new_rect
.x
-= distance
;
861 else if(expanded
.GetLeft() < display
.GetLeft())
863 distance
= display
.GetLeft() - expanded
.GetLeft();
864 new_rect
.x
+= distance
;
869 if(expanded
.GetBottom() > display
.GetBottom())
871 distance
= expanded
.GetBottom() - display
.GetBottom();
872 new_rect
.y
-= distance
;
874 else if(expanded
.GetTop() < display
.GetTop())
876 distance
= display
.GetTop() - expanded
.GetTop();
877 new_rect
.y
+= distance
;
880 if(!display
.Contains(new_rect
))
882 // Tried moving in primary axis, but failed.
883 // Hence try moving in the secondary axis.
884 int dx
= secondary_x
* (panel
.GetWidth() + expanded_size
.GetWidth());
885 int dy
= secondary_y
* (panel
.GetHeight() + expanded_size
.GetHeight());
889 // Squaring makes secondary moves more expensive (and also
890 // prevents a negative cost)
891 distance
+= dx
* dx
+ dy
* dy
;
893 if(display
.Contains(new_rect
) && distance
< best_distance
)
896 best_distance
= distance
;
904 #endif // wxUSE_RIBBON