Added wxRIBBON_PANEL_STRETCH to allow a single panel to stretch to fill the parent...
[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 wxSize wxRibbonPanel::DoGetNextSmallerSize(wxOrientation direction,
315 wxSize relative_to) const
316 {
317 if(m_expanded_panel != NULL)
318 {
319 // Next size depends upon children, who are currently in the
320 // expanded panel
321 return m_expanded_panel->DoGetNextSmallerSize(direction, relative_to);
322 }
323
324 if(m_art != NULL)
325 {
326 wxClientDC dc((wxRibbonPanel*) this);
327 wxSize child_relative = m_art->GetPanelClientSize(dc, this, relative_to, NULL);
328 wxSize smaller(-1, -1);
329 bool minimise = false;
330
331 if(GetSizer())
332 {
333 // Get smallest non minimised size
334 smaller = GetMinSize();
335 // and adjust to child_relative for parent page
336 if(m_art->GetFlags() & wxRIBBON_BAR_FLOW_VERTICAL)
337 {
338 minimise = (child_relative.y <= smaller.y);
339 if(smaller.x < child_relative.x)
340 smaller.x = child_relative.x;
341 }
342 else
343 {
344 minimise = (child_relative.x <= smaller.x);
345 if(smaller.y < child_relative.y)
346 smaller.y = child_relative.y;
347 }
348 }
349 else if(GetChildren().GetCount() == 1)
350 {
351 // Simple (and common) case of single ribbon child or Sizer
352 wxWindow* child = GetChildren().Item(0)->GetData();
353 wxRibbonControl* ribbon_child = wxDynamicCast(child, wxRibbonControl);
354 if(ribbon_child != NULL)
355 {
356 smaller = ribbon_child->GetNextSmallerSize(direction, child_relative);
357 minimise = (smaller == child_relative);
358 }
359 }
360
361 if(minimise)
362 {
363 if(CanAutoMinimise())
364 {
365 wxSize minimised = m_minimised_size;
366 switch(direction)
367 {
368 case wxHORIZONTAL:
369 minimised.SetHeight(relative_to.GetHeight());
370 break;
371 case wxVERTICAL:
372 minimised.SetWidth(relative_to.GetWidth());
373 break;
374 default:
375 break;
376 }
377 return minimised;
378 }
379 else
380 {
381 return relative_to;
382 }
383 }
384 else if(smaller.IsFullySpecified()) // Use fallback if !(sizer/child = 1)
385 {
386 return m_art->GetPanelSize(dc, this, smaller, NULL);
387 }
388 }
389
390 // Fallback: Decrease by 20% (or minimum size, whichever larger)
391 wxSize current(relative_to);
392 wxSize minimum(GetMinSize());
393 if(direction & wxHORIZONTAL)
394 {
395 current.x = (current.x * 4) / 5;
396 if(current.x < minimum.x)
397 {
398 current.x = minimum.x;
399 }
400 }
401 if(direction & wxVERTICAL)
402 {
403 current.y = (current.y * 4) / 5;
404 if(current.y < minimum.y)
405 {
406 current.y = minimum.y;
407 }
408 }
409 return current;
410 }
411
412 wxSize wxRibbonPanel::DoGetNextLargerSize(wxOrientation direction,
413 wxSize relative_to) const
414 {
415 if(m_expanded_panel != NULL)
416 {
417 // Next size depends upon children, who are currently in the
418 // expanded panel
419 return m_expanded_panel->DoGetNextLargerSize(direction, relative_to);
420 }
421
422 if(IsMinimised(relative_to))
423 {
424 wxSize current = relative_to;
425 wxSize min_size = GetMinNotMinimisedSize();
426 switch(direction)
427 {
428 case wxHORIZONTAL:
429 if(min_size.x > current.x && min_size.y == current.y)
430 return min_size;
431 break;
432 case wxVERTICAL:
433 if(min_size.x == current.x && min_size.y > current.y)
434 return min_size;
435 break;
436 case wxBOTH:
437 if(min_size.x > current.x && min_size.y > current.y)
438 return min_size;
439 break;
440 default:
441 break;
442 }
443 }
444
445 if(m_art != NULL)
446 {
447 wxClientDC dc((wxRibbonPanel*) this);
448 wxSize child_relative = m_art->GetPanelClientSize(dc, this, relative_to, NULL);
449 wxSize larger(-1, -1);
450
451 if(GetSizer())
452 {
453 // We could just let the sizer expand in flow direction but see comment
454 // in IsSizingContinuous()
455 larger = GetPanelSizerBestSize();
456 // and adjust for page in non flow direction
457 if(m_art->GetFlags() & wxRIBBON_BAR_FLOW_VERTICAL)
458 {
459 if(larger.x != child_relative.x)
460 larger.x = child_relative.x;
461 }
462 else if(larger.y != child_relative.y)
463 {
464 larger.y = child_relative.y;
465 }
466 }
467 else if(GetChildren().GetCount() == 1)
468 {
469 // Simple (and common) case of single ribbon child
470 wxWindow* child = GetChildren().Item(0)->GetData();
471 wxRibbonControl* ribbon_child = wxDynamicCast(child, wxRibbonControl);
472 if(ribbon_child != NULL)
473 {
474 larger = ribbon_child->GetNextLargerSize(direction, child_relative);
475 }
476 }
477
478 if(larger.IsFullySpecified()) // Use fallback if !(sizer/child = 1)
479 {
480 if(larger == child_relative)
481 {
482 return relative_to;
483 }
484 else
485 {
486 return m_art->GetPanelSize(dc, this, larger, NULL);
487 }
488 }
489 }
490
491 // Fallback: Increase by 25% (equal to a prior or subsequent 20% decrease)
492 // Note that due to rounding errors, this increase may not exactly equal a
493 // matching decrease - an ideal solution would not have these errors, but
494 // avoiding them is non-trivial unless an increase is by 100% rather than
495 // a fractional amount. This would then be non-ideal as the resizes happen
496 // at very large intervals.
497 wxSize current(relative_to);
498 if(direction & wxHORIZONTAL)
499 {
500 current.x = (current.x * 5 + 3) / 4;
501 }
502 if(direction & wxVERTICAL)
503 {
504 current.y = (current.y * 5 + 3) / 4;
505 }
506 return current;
507 }
508
509 bool wxRibbonPanel::CanAutoMinimise() const
510 {
511 return (m_flags & wxRIBBON_PANEL_NO_AUTO_MINIMISE) == 0
512 && m_minimised_size.IsFullySpecified();
513 }
514
515 wxSize wxRibbonPanel::GetMinSize() const
516 {
517 if(m_expanded_panel != NULL)
518 {
519 // Minimum size depends upon children, who are currently in the
520 // expanded panel
521 return m_expanded_panel->GetMinSize();
522 }
523
524 if(CanAutoMinimise())
525 {
526 return m_minimised_size;
527 }
528 else
529 {
530 return GetMinNotMinimisedSize();
531 }
532 }
533
534 wxSize wxRibbonPanel::GetMinNotMinimisedSize() const
535 {
536 // Ask sizer if present
537 if(GetSizer())
538 {
539 wxClientDC dc((wxRibbonPanel*) this);
540 return m_art->GetPanelSize(dc, this, GetPanelSizerMinSize(), NULL);
541 }
542 else if(GetChildren().GetCount() == 1)
543 {
544 // Common case of single child taking up the entire panel
545 wxWindow* child = GetChildren().Item(0)->GetData();
546 wxClientDC dc((wxRibbonPanel*) this);
547 return m_art->GetPanelSize(dc, this, child->GetMinSize(), NULL);
548 }
549
550 return wxRibbonControl::GetMinSize();
551 }
552
553 wxSize wxRibbonPanel::GetPanelSizerMinSize() const
554 {
555 // Called from Realize() to set m_smallest_unminimised_size and from other
556 // functions to get the minimum size.
557 // The panel will be invisible when minimised and sizer calcs will be 0
558 // Uses m_smallest_unminimised_size in preference to GetSizer()->CalcMin()
559 // to eliminate flicker.
560
561 // Check if is visible and not previously calculated
562 if(IsShown() && !m_smallest_unminimised_size.IsFullySpecified())
563 {
564 return GetSizer()->CalcMin();
565 }
566 // else use previously calculated m_smallest_unminimised_size
567 wxClientDC dc((wxRibbonPanel*) this);
568 return m_art->GetPanelClientSize(dc,
569 this,
570 m_smallest_unminimised_size,
571 NULL);
572 }
573
574 wxSize wxRibbonPanel::GetPanelSizerBestSize() const
575 {
576 wxSize size = GetPanelSizerMinSize();
577 // TODO allow panel to increase its size beyond minimum size
578 // by steps similarly to ribbon control panels (preferred for aesthetics)
579 // or continuously.
580 return size;
581 }
582
583 wxSize wxRibbonPanel::DoGetBestSize() const
584 {
585 // Ask sizer if present
586 if( GetSizer())
587 {
588 wxClientDC dc((wxRibbonPanel*) this);
589 return m_art->GetPanelSize(dc, this, GetPanelSizerBestSize(), NULL);
590 }
591 else if(GetChildren().GetCount() == 1)
592 {
593 // Common case of no sizer and single child taking up the entire panel
594 wxWindow* child = GetChildren().Item(0)->GetData();
595 wxClientDC dc((wxRibbonPanel*) this);
596 return m_art->GetPanelSize(dc, this, child->GetBestSize(), NULL);
597 }
598
599 return wxRibbonControl::DoGetBestSize();
600 }
601
602 bool wxRibbonPanel::Realize()
603 {
604 bool status = true;
605
606 for (wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
607 node;
608 node = node->GetNext())
609 {
610 wxRibbonControl* child = wxDynamicCast(node->GetData(), wxRibbonControl);
611 if(child == NULL)
612 {
613 continue;
614 }
615 if(!child->Realize())
616 {
617 status = false;
618 }
619 }
620
621 wxSize minimum_children_size(0, 0);
622
623 // Ask sizer if there is one present
624 if(GetSizer())
625 {
626 minimum_children_size = GetPanelSizerMinSize();
627 }
628 else if(GetChildren().GetCount() == 1)
629 {
630 minimum_children_size = GetChildren().GetFirst()->GetData()->GetMinSize();
631 }
632
633 if(m_art != NULL)
634 {
635 wxClientDC temp_dc(this);
636
637 m_smallest_unminimised_size =
638 m_art->GetPanelSize(temp_dc, this, minimum_children_size, NULL);
639
640 wxSize bitmap_size;
641 wxSize panel_min_size = GetMinNotMinimisedSize();
642 m_minimised_size = m_art->GetMinimisedPanelMinimumSize(temp_dc, this,
643 &bitmap_size, &m_preferred_expand_direction);
644 if(m_minimised_icon.IsOk() && m_minimised_icon.GetSize() != bitmap_size)
645 {
646 wxImage img(m_minimised_icon.ConvertToImage());
647 img.Rescale(bitmap_size.GetWidth(), bitmap_size.GetHeight(), wxIMAGE_QUALITY_HIGH);
648 m_minimised_icon_resized = wxBitmap(img);
649 }
650 else
651 {
652 m_minimised_icon_resized = m_minimised_icon;
653 }
654 if(m_minimised_size.x > panel_min_size.x &&
655 m_minimised_size.y > panel_min_size.y)
656 {
657 // No point in having a minimised size which is larger than the
658 // minimum size which the children can go to.
659 m_minimised_size = wxSize(-1, -1);
660 }
661 else
662 {
663 if(m_art->GetFlags() & wxRIBBON_BAR_FLOW_VERTICAL)
664 {
665 m_minimised_size.x = panel_min_size.x;
666 }
667 else
668 {
669 m_minimised_size.y = panel_min_size.y;
670 }
671 }
672 }
673 else
674 {
675 m_minimised_size = wxSize(-1, -1);
676 }
677
678 return Layout() && status;
679 }
680
681 bool wxRibbonPanel::Layout()
682 {
683 if(IsMinimised())
684 {
685 // Children are all invisible when minimised
686 return true;
687 }
688
689 // Get wxRibbonPanel client size
690 wxPoint position;
691 wxClientDC dc(this);
692 wxSize size = m_art->GetPanelClientSize(dc, this, GetSize(), &position);
693
694 // If there is a sizer, use it
695 if(GetSizer())
696 {
697 GetSizer()->SetDimension(position, size); // SetSize and Layout()
698 }
699 else if(GetChildren().GetCount() == 1)
700 {
701 // Common case of no sizer and single child taking up the entire panel
702 wxWindow* child = GetChildren().Item(0)->GetData();
703 child->SetSize(position.x, position.y, size.GetWidth(), size.GetHeight());
704 }
705 return true;
706 }
707
708 void wxRibbonPanel::OnMouseClick(wxMouseEvent& WXUNUSED(evt))
709 {
710 if(IsMinimised())
711 {
712 if(m_expanded_panel != NULL)
713 {
714 HideExpanded();
715 }
716 else
717 {
718 ShowExpanded();
719 }
720 }
721 }
722
723 wxRibbonPanel* wxRibbonPanel::GetExpandedDummy()
724 {
725 return m_expanded_dummy;
726 }
727
728 wxRibbonPanel* wxRibbonPanel::GetExpandedPanel()
729 {
730 return m_expanded_panel;
731 }
732
733 bool wxRibbonPanel::ShowExpanded()
734 {
735 if(!IsMinimised())
736 {
737 return false;
738 }
739 if(m_expanded_dummy != NULL || m_expanded_panel != NULL)
740 {
741 return false;
742 }
743
744 wxSize size = GetBestSize();
745 wxPoint pos = GetExpandedPosition(wxRect(GetScreenPosition(), GetSize()),
746 size, m_preferred_expand_direction).GetTopLeft();
747
748 // Need a top-level frame to contain the expanded panel
749 wxFrame *container = new wxFrame(NULL, wxID_ANY, GetLabel(),
750 pos, size, wxFRAME_NO_TASKBAR | wxBORDER_NONE);
751
752 m_expanded_panel = new wxRibbonPanel(container, wxID_ANY,
753 GetLabel(), m_minimised_icon, wxPoint(0, 0), size, m_flags);
754
755 m_expanded_panel->SetArtProvider(m_art);
756 m_expanded_panel->m_expanded_dummy = this;
757
758 // Move all children to the new panel.
759 // Conceptually it might be simpler to reparent this entire panel to the
760 // container and create a new panel to sit in its place while expanded.
761 // This approach has a problem though - when the panel is reinserted into
762 // its original parent, it'll be at a different position in the child list
763 // and thus assume a new position.
764 // NB: Children iterators not used as behaviour is not well defined
765 // when iterating over a container which is being emptied
766 while(!GetChildren().IsEmpty())
767 {
768 wxWindow *child = GetChildren().GetFirst()->GetData();
769 child->Reparent(m_expanded_panel);
770 child->Show();
771 }
772
773 // Move sizer to new panel
774 if(GetSizer())
775 {
776 wxSizer* sizer = GetSizer();
777 SetSizer(NULL, false);
778 m_expanded_panel->SetSizer(sizer);
779 }
780
781 m_expanded_panel->Realize();
782 Refresh();
783 container->SetMinClientSize(size);
784 container->Show();
785 m_expanded_panel->SetFocus();
786
787 return true;
788 }
789
790 bool wxRibbonPanel::ShouldSendEventToDummy(wxEvent& evt)
791 {
792 // For an expanded panel, filter events between being sent up to the
793 // floating top level window or to the dummy panel sitting in the ribbon
794 // bar.
795
796 // Child focus events should not be redirected, as the child would not be a
797 // child of the window the event is redirected to. All other command events
798 // seem to be suitable for redirecting.
799 return evt.IsCommandEvent() && evt.GetEventType() != wxEVT_CHILD_FOCUS;
800 }
801
802 bool wxRibbonPanel::TryAfter(wxEvent& evt)
803 {
804 if(m_expanded_dummy && ShouldSendEventToDummy(evt))
805 {
806 wxPropagateOnce propagateOnce(evt);
807 return m_expanded_dummy->GetEventHandler()->ProcessEvent(evt);
808 }
809 else
810 {
811 return wxRibbonControl::TryAfter(evt);
812 }
813 }
814
815 static bool IsAncestorOf(wxWindow *ancestor, wxWindow *window)
816 {
817 while(window != NULL)
818 {
819 wxWindow *parent = window->GetParent();
820 if(parent == ancestor)
821 return true;
822 else
823 window = parent;
824 }
825 return false;
826 }
827
828 void wxRibbonPanel::OnKillFocus(wxFocusEvent& evt)
829 {
830 if(m_expanded_dummy)
831 {
832 wxWindow *receiver = evt.GetWindow();
833 if(IsAncestorOf(this, receiver))
834 {
835 m_child_with_focus = receiver;
836 receiver->Connect(wxEVT_KILL_FOCUS,
837 wxFocusEventHandler(wxRibbonPanel::OnChildKillFocus),
838 NULL, this);
839 }
840 else if(receiver == NULL || receiver != m_expanded_dummy)
841 {
842 HideExpanded();
843 }
844 }
845 }
846
847 void wxRibbonPanel::OnChildKillFocus(wxFocusEvent& evt)
848 {
849 if(m_child_with_focus == NULL)
850 return; // Should never happen, but a check can't hurt
851
852 m_child_with_focus->Disconnect(wxEVT_KILL_FOCUS,
853 wxFocusEventHandler(wxRibbonPanel::OnChildKillFocus), NULL, this);
854 m_child_with_focus = NULL;
855
856 wxWindow *receiver = evt.GetWindow();
857 if(receiver == this || IsAncestorOf(this, receiver))
858 {
859 m_child_with_focus = receiver;
860 receiver->Connect(wxEVT_KILL_FOCUS,
861 wxFocusEventHandler(wxRibbonPanel::OnChildKillFocus), NULL, this);
862 evt.Skip();
863 }
864 else if(receiver == NULL || receiver != m_expanded_dummy)
865 {
866 HideExpanded();
867 // Do not skip event, as the panel has been de-expanded, causing the
868 // child with focus to be reparented (and hidden). If the event
869 // continues propogation then bad things happen.
870 }
871 else
872 {
873 evt.Skip();
874 }
875 }
876
877 bool wxRibbonPanel::HideExpanded()
878 {
879 if(m_expanded_dummy == NULL)
880 {
881 if(m_expanded_panel)
882 {
883 return m_expanded_panel->HideExpanded();
884 }
885 else
886 {
887 return false;
888 }
889 }
890
891 // Move children back to original panel
892 // NB: Children iterators not used as behaviour is not well defined
893 // when iterating over a container which is being emptied
894 while(!GetChildren().IsEmpty())
895 {
896 wxWindow *child = GetChildren().GetFirst()->GetData();
897 child->Reparent(m_expanded_dummy);
898 child->Hide();
899 }
900
901 // Move sizer back
902 if(GetSizer())
903 {
904 wxSizer* sizer = GetSizer();
905 SetSizer(NULL, false);
906 m_expanded_dummy->SetSizer(sizer);
907 }
908
909 m_expanded_dummy->m_expanded_panel = NULL;
910 m_expanded_dummy->Realize();
911 m_expanded_dummy->Refresh();
912 wxWindow *parent = GetParent();
913 Destroy();
914 parent->Destroy();
915
916 return true;
917 }
918
919 wxRect wxRibbonPanel::GetExpandedPosition(wxRect panel,
920 wxSize expanded_size,
921 wxDirection direction)
922 {
923 // Strategy:
924 // 1) Determine primary position based on requested direction
925 // 2) Move the position so that it sits entirely within a display
926 // (for single monitor systems, this moves it into the display region,
927 // but for multiple monitors, it does so without splitting it over
928 // more than one display)
929 // 2.1) Move in the primary axis
930 // 2.2) Move in the secondary axis
931
932 wxPoint pos;
933 bool primary_x = false;
934 int secondary_x = 0;
935 int secondary_y = 0;
936 switch(direction)
937 {
938 case wxNORTH:
939 pos.x = panel.GetX() + (panel.GetWidth() - expanded_size.GetWidth()) / 2;
940 pos.y = panel.GetY() - expanded_size.GetHeight();
941 primary_x = true;
942 secondary_y = 1;
943 break;
944 case wxEAST:
945 pos.x = panel.GetRight();
946 pos.y = panel.GetY() + (panel.GetHeight() - expanded_size.GetHeight()) / 2;
947 secondary_x = -1;
948 break;
949 case wxSOUTH:
950 pos.x = panel.GetX() + (panel.GetWidth() - expanded_size.GetWidth()) / 2;
951 pos.y = panel.GetBottom();
952 primary_x = true;
953 secondary_y = -1;
954 break;
955 case wxWEST:
956 default:
957 pos.x = panel.GetX() - expanded_size.GetWidth();
958 pos.y = panel.GetY() + (panel.GetHeight() - expanded_size.GetHeight()) / 2;
959 secondary_x = 1;
960 break;
961 }
962 wxRect expanded(pos, expanded_size);
963
964 wxRect best(expanded);
965 int best_distance = INT_MAX;
966
967 const unsigned display_n = wxDisplay::GetCount();
968 unsigned display_i;
969 for(display_i = 0; display_i < display_n; ++display_i)
970 {
971 wxRect display = wxDisplay(display_i).GetGeometry();
972
973 if(display.Contains(expanded))
974 {
975 return expanded;
976 }
977 else if(display.Intersects(expanded))
978 {
979 wxRect new_rect(expanded);
980 int distance = 0;
981
982 if(primary_x)
983 {
984 if(expanded.GetRight() > display.GetRight())
985 {
986 distance = expanded.GetRight() - display.GetRight();
987 new_rect.x -= distance;
988 }
989 else if(expanded.GetLeft() < display.GetLeft())
990 {
991 distance = display.GetLeft() - expanded.GetLeft();
992 new_rect.x += distance;
993 }
994 }
995 else
996 {
997 if(expanded.GetBottom() > display.GetBottom())
998 {
999 distance = expanded.GetBottom() - display.GetBottom();
1000 new_rect.y -= distance;
1001 }
1002 else if(expanded.GetTop() < display.GetTop())
1003 {
1004 distance = display.GetTop() - expanded.GetTop();
1005 new_rect.y += distance;
1006 }
1007 }
1008 if(!display.Contains(new_rect))
1009 {
1010 // Tried moving in primary axis, but failed.
1011 // Hence try moving in the secondary axis.
1012 int dx = secondary_x * (panel.GetWidth() + expanded_size.GetWidth());
1013 int dy = secondary_y * (panel.GetHeight() + expanded_size.GetHeight());
1014 new_rect.x += dx;
1015 new_rect.y += dy;
1016
1017 // Squaring makes secondary moves more expensive (and also
1018 // prevents a negative cost)
1019 distance += dx * dx + dy * dy;
1020 }
1021 if(display.Contains(new_rect) && distance < best_distance)
1022 {
1023 best = new_rect;
1024 best_distance = distance;
1025 }
1026 }
1027 }
1028
1029 return best;
1030 }
1031
1032 #endif // wxUSE_RIBBON