1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/splitter.cpp
3 // Purpose: wxSplitterWindow implementation
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "splitter.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
26 #include "wx/string.h"
30 #include "wx/dcscreen.h"
32 #include "wx/window.h"
33 #include "wx/dialog.h"
36 #include "wx/settings.h"
39 #include "wx/renderer.h"
41 #include "wx/splitter.h"
45 DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED
)
46 DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING
)
47 DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPLITTER_DOUBLECLICKED
)
48 DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPLITTER_UNSPLIT
)
50 IMPLEMENT_DYNAMIC_CLASS(wxSplitterWindow
, wxWindow
)
61 IMPLEMENT_DYNAMIC_CLASS(wxSplitterEvent
, wxNotifyEvent
)
63 BEGIN_EVENT_TABLE(wxSplitterWindow
, wxWindow
)
64 EVT_PAINT(wxSplitterWindow::OnPaint
)
65 EVT_SIZE(wxSplitterWindow::OnSize
)
66 EVT_MOUSE_EVENTS(wxSplitterWindow::OnMouseEvent
)
68 #if defined( __WXMSW__ ) || defined( __WXMAC__)
69 EVT_SET_CURSOR(wxSplitterWindow::OnSetCursor
)
72 WX_EVENT_TABLE_CONTROL_CONTAINER(wxSplitterWindow
)
75 WX_DELEGATE_TO_CONTROL_CONTAINER(wxSplitterWindow
);
77 bool wxSplitterWindow::Create(wxWindow
*parent
, wxWindowID id
,
83 // allow TABbing from one window to the other
84 style
|= wxTAB_TRAVERSAL
;
86 // we draw our border ourselves to blend the sash with it
87 style
&= ~wxBORDER_MASK
;
88 style
|= wxBORDER_NONE
;
90 // we don't need to be completely repainted after resize and doing it
91 // results in horrible flicker
92 style
|= wxNO_FULL_REPAINT_ON_RESIZE
;
94 if ( !wxWindow::Create(parent
, id
, pos
, size
, style
, name
) )
97 m_permitUnsplitAlways
= (style
& wxSP_PERMIT_UNSPLIT
) != 0;
102 void wxSplitterWindow::Init()
104 m_container
.SetContainerWindow(this);
106 m_splitMode
= wxSPLIT_VERTICAL
;
107 m_permitUnsplitAlways
= TRUE
;
108 m_windowOne
= (wxWindow
*) NULL
;
109 m_windowTwo
= (wxWindow
*) NULL
;
110 m_dragMode
= wxSPLIT_DRAG_NONE
;
115 m_sashPosition
= m_requestedSashPosition
= 0;
116 m_minimumPaneSize
= 0;
117 m_sashCursorWE
= wxCursor(wxCURSOR_SIZEWE
);
118 m_sashCursorNS
= wxCursor(wxCURSOR_SIZENS
);
119 m_sashTrackerPen
= new wxPen(*wxBLACK
, 2, wxSOLID
);
121 m_needUpdating
= FALSE
;
125 wxSplitterWindow::~wxSplitterWindow()
127 delete m_sashTrackerPen
;
130 // ----------------------------------------------------------------------------
131 // entering/leaving sash
132 // ----------------------------------------------------------------------------
134 void wxSplitterWindow::RedrawIfHotSensitive(bool isHot
)
136 if ( wxRendererNative::Get().GetSplitterParams(this).isHotSensitive
)
143 //else: we don't change our appearance, don't redraw to avoid flicker
146 void wxSplitterWindow::OnEnterSash()
150 RedrawIfHotSensitive(true);
153 void wxSplitterWindow::OnLeaveSash()
155 SetCursor(*wxSTANDARD_CURSOR
);
157 RedrawIfHotSensitive(false);
160 void wxSplitterWindow::SetResizeCursor()
162 SetCursor(m_splitMode
== wxSPLIT_VERTICAL
? m_sashCursorWE
166 // ----------------------------------------------------------------------------
167 // other event handlers
168 // ----------------------------------------------------------------------------
170 void wxSplitterWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
177 void wxSplitterWindow::OnInternalIdle()
179 wxWindow::OnInternalIdle();
185 void wxSplitterWindow::OnMouseEvent(wxMouseEvent
& event
)
187 int x
= (int)event
.GetX(),
188 y
= (int)event
.GetY();
190 if (GetWindowStyle() & wxSP_NOSASH
)
193 // with wxSP_LIVE_UPDATE style the splitter windows are always resized
194 // following the mouse movement while it drags the sash, without it we only
195 // draw the sash at the new position but only resize the windows when the
196 // dragging is finished
197 bool isLive
= (GetWindowStyleFlag() & wxSP_LIVE_UPDATE
) != 0;
199 if (event
.LeftDown())
201 if ( SashHitTest(x
, y
) )
203 // Start the drag now
204 m_dragMode
= wxSPLIT_DRAG_DRAGGING
;
206 // Capture mouse and set the cursor
212 // remember the initial sash position and draw the initial
214 m_sashPositionCurrent
= m_sashPosition
;
216 DrawSashTracker(x
, y
);
226 else if (event
.LeftUp() && m_dragMode
== wxSPLIT_DRAG_DRAGGING
)
228 // We can stop dragging now and see what we've got.
229 m_dragMode
= wxSPLIT_DRAG_NONE
;
231 // Release mouse and unset the cursor
233 SetCursor(* wxSTANDARD_CURSOR
);
235 // exit if unsplit after doubleclick
244 DrawSashTracker(m_oldX
, m_oldY
);
247 // the position of the click doesn't exactly correspond to
248 // m_sashPosition, rather it changes it by the distance by which the
250 int diff
= m_splitMode
== wxSPLIT_VERTICAL
? x
- m_oldX
: y
- m_oldY
;
252 int posSashOld
= isLive
? m_sashPosition
: m_sashPositionCurrent
;
253 int posSashNew
= OnSashPositionChanging(posSashOld
+ diff
);
254 if ( posSashNew
== -1 )
256 // change not allowed
260 if ( m_permitUnsplitAlways
|| m_minimumPaneSize
== 0 )
262 // Deal with possible unsplit scenarios
263 if ( posSashNew
== 0 )
265 // We remove the first window from the view
266 wxWindow
*removedWindow
= m_windowOne
;
267 m_windowOne
= m_windowTwo
;
268 m_windowTwo
= (wxWindow
*) NULL
;
269 OnUnsplit(removedWindow
);
270 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_UNSPLIT
, this);
271 event
.m_data
.win
= removedWindow
;
272 (void)DoSendEvent(event
);
273 SetSashPositionAndNotify(0);
275 else if ( posSashNew
== GetWindowSize() )
277 // We remove the second window from the view
278 wxWindow
*removedWindow
= m_windowTwo
;
279 m_windowTwo
= (wxWindow
*) NULL
;
280 OnUnsplit(removedWindow
);
281 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_UNSPLIT
, this);
282 event
.m_data
.win
= removedWindow
;
283 (void)DoSendEvent(event
);
284 SetSashPositionAndNotify(0);
288 SetSashPositionAndNotify(posSashNew
);
293 SetSashPositionAndNotify(posSashNew
);
297 } // left up && dragging
298 else if ((event
.Moving() || event
.Leaving() || event
.Entering()) && (m_dragMode
== wxSPLIT_DRAG_NONE
))
300 if ( event
.Leaving() || !SashHitTest(x
, y
) )
305 else if (event
.Dragging() && (m_dragMode
== wxSPLIT_DRAG_DRAGGING
))
307 int diff
= m_splitMode
== wxSPLIT_VERTICAL
? x
- m_oldX
: y
- m_oldY
;
310 // nothing to do, mouse didn't really move far enough
314 int posSashOld
= isLive
? m_sashPosition
: m_sashPositionCurrent
;
315 int posSashNew
= OnSashPositionChanging(posSashOld
+ diff
);
316 if ( posSashNew
== -1 )
318 // change not allowed
322 if ( posSashNew
== m_sashPosition
)
328 DrawSashTracker(m_oldX
, m_oldY
);
331 if (m_splitMode
== wxSPLIT_VERTICAL
)
336 // Remember old positions
341 // As we captured the mouse, we may get the mouse events from outside
342 // our window - for example, negative values in x, y. This has a weird
343 // consequence under MSW where we use unsigned values sometimes and
344 // signed ones other times: the coordinates turn as big positive
345 // numbers and so the sash is drawn on the *right* side of the window
346 // instead of the left (or bottom instead of top). Correct this.
347 if ( (short)m_oldX
< 0 )
349 if ( (short)m_oldY
< 0 )
356 m_sashPositionCurrent
= posSashNew
;
358 DrawSashTracker(m_oldX
, m_oldY
);
362 SetSashPositionAndNotify(posSashNew
);
363 m_needUpdating
= TRUE
;
366 else if ( event
.LeftDClick() && m_windowTwo
)
368 OnDoubleClickSash(x
, y
);
372 void wxSplitterWindow::OnSize(wxSizeEvent
& event
)
374 // only process this message if we're not iconized - otherwise iconizing
375 // and restoring a window containing the splitter has a funny side effect
376 // of changing the splitter position!
377 wxWindow
*parent
= GetParent();
378 while ( parent
&& !parent
->IsTopLevel() )
380 parent
= parent
->GetParent();
383 bool iconized
= FALSE
;
385 wxTopLevelWindow
*winTop
= wxDynamicCast(parent
, wxTopLevelWindow
);
388 iconized
= winTop
->IsIconized();
392 wxFAIL_MSG(wxT("should have a top level parent!"));
407 GetClientSize(&w
, &h
);
409 int size
= m_splitMode
== wxSPLIT_VERTICAL
? w
: h
;
410 if ( m_sashPosition
>= size
- 5 )
411 SetSashPositionAndNotify(wxMax(10, size
- 40));
417 bool wxSplitterWindow::SashHitTest(int x
, int y
, int tolerance
)
419 if ( m_windowTwo
== NULL
|| m_sashPosition
== 0)
420 return FALSE
; // No sash
422 int z
= m_splitMode
== wxSPLIT_VERTICAL
? x
: y
;
424 return z
>= m_sashPosition
- tolerance
&&
425 z
<= m_sashPosition
+ GetSashSize() + tolerance
;
428 int wxSplitterWindow::GetSashSize() const
430 return wxRendererNative::Get().GetSplitterParams(this).widthSash
;
433 int wxSplitterWindow::GetBorderSize() const
435 return wxRendererNative::Get().GetSplitterParams(this).border
;
439 void wxSplitterWindow::DrawSash(wxDC
& dc
)
441 if (HasFlag(wxSP_3DBORDER
))
442 wxRendererNative::Get().DrawSplitterBorder
449 // don't draw sash if we're not split
450 if ( m_sashPosition
== 0 || !m_windowTwo
)
453 // nor if we're configured to not show it
454 if ( HasFlag(wxSP_NOSASH
) )
457 wxRendererNative::Get().DrawSplitterSash
463 m_splitMode
== wxSPLIT_VERTICAL
? wxVERTICAL
465 m_isHot
? wxCONTROL_CURRENT
: 0
469 // Draw the sash tracker (for whilst moving the sash)
470 void wxSplitterWindow::DrawSashTracker(int x
, int y
)
473 GetClientSize(&w
, &h
);
479 if ( m_splitMode
== wxSPLIT_VERTICAL
)
510 ClientToScreen(&x1
, &y1
);
511 ClientToScreen(&x2
, &y2
);
513 screenDC
.SetLogicalFunction(wxINVERT
);
514 screenDC
.SetPen(*m_sashTrackerPen
);
515 screenDC
.SetBrush(*wxTRANSPARENT_BRUSH
);
517 screenDC
.DrawLine(x1
, y1
, x2
, y2
);
519 screenDC
.SetLogicalFunction(wxCOPY
);
522 int wxSplitterWindow::GetWindowSize() const
524 wxSize size
= GetClientSize();
526 return m_splitMode
== wxSPLIT_VERTICAL
? size
.x
: size
.y
;
529 int wxSplitterWindow::AdjustSashPosition(int sashPos
) const
531 int window_size
= GetWindowSize();
538 // the window shouldn't be smaller than its own minimal size nor
539 // smaller than the minimual pane size specified for this splitter
540 int minSize
= m_splitMode
== wxSPLIT_VERTICAL
? win
->GetMinWidth()
541 : win
->GetMinHeight();
543 if ( minSize
== -1 || m_minimumPaneSize
> minSize
)
544 minSize
= m_minimumPaneSize
;
546 minSize
+= GetBorderSize();
548 if ( sashPos
< minSize
)
555 int minSize
= m_splitMode
== wxSPLIT_VERTICAL
? win
->GetMinWidth()
556 : win
->GetMinHeight();
558 if ( minSize
== -1 || m_minimumPaneSize
> minSize
)
559 minSize
= m_minimumPaneSize
;
561 int maxSize
= window_size
- minSize
- GetBorderSize();
562 if ( sashPos
> maxSize
)
569 bool wxSplitterWindow::DoSetSashPosition(int sashPos
)
571 int newSashPosition
= AdjustSashPosition(sashPos
);
573 if ( newSashPosition
== m_sashPosition
)
576 m_sashPosition
= newSashPosition
;
581 void wxSplitterWindow::SetSashPositionAndNotify(int sashPos
)
583 if ( DoSetSashPosition(sashPos
) )
585 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED
, this);
586 event
.m_data
.pos
= m_sashPosition
;
588 (void)DoSendEvent(event
);
592 // Position and size subwindows.
593 // Note that the border size applies to each subwindow, not
594 // including the edges next to the sash.
595 void wxSplitterWindow::SizeWindows()
597 // check if we have delayed setting the real sash position
598 if ( m_requestedSashPosition
!= INT_MAX
)
600 int newSashPosition
= ConvertSashPosition(m_requestedSashPosition
);
601 if ( newSashPosition
!= m_sashPosition
)
603 DoSetSashPosition(newSashPosition
);
606 if ( newSashPosition
<= m_sashPosition
607 && newSashPosition
>= m_sashPosition
- GetBorderSize() )
609 // don't update it any more
610 m_requestedSashPosition
= INT_MAX
;
615 GetClientSize(&w
, &h
);
617 if ( GetWindow1() && !GetWindow2() )
619 GetWindow1()->SetSize(GetBorderSize(), GetBorderSize(),
620 w
- 2*GetBorderSize(), h
- 2*GetBorderSize());
622 else if ( GetWindow1() && GetWindow2() )
624 const int border
= GetBorderSize(),
625 sash
= GetSashSize();
627 int size1
= GetSashPosition() - border
,
628 size2
= GetSashPosition() + sash
;
630 int x2
, y2
, w1
, h1
, w2
, h2
;
631 if ( GetSplitMode() == wxSPLIT_VERTICAL
)
634 w2
= w
- 2*border
- sash
- w1
;
640 else // horz splitter
645 h2
= h
- 2*border
- sash
- h1
;
650 GetWindow1()->SetSize(border
, border
, w1
, h1
);
651 GetWindow2()->SetSize(x2
, y2
, w2
, h2
);
657 SetNeedUpdating(FALSE
);
660 // Set pane for unsplit window
661 void wxSplitterWindow::Initialize(wxWindow
*window
)
663 wxASSERT_MSG( window
&& window
->GetParent() == this,
664 _T("windows in the splitter should have it as parent!") );
666 m_windowOne
= window
;
667 m_windowTwo
= (wxWindow
*) NULL
;
668 DoSetSashPosition(0);
671 // Associates the given window with window 2, drawing the appropriate sash
672 // and changing the split mode.
673 // Does nothing and returns FALSE if the window is already split.
674 bool wxSplitterWindow::DoSplit(wxSplitMode mode
,
675 wxWindow
*window1
, wxWindow
*window2
,
681 wxCHECK_MSG( window1
&& window2
, FALSE
,
682 _T("can not split with NULL window(s)") );
684 wxCHECK_MSG( window1
->GetParent() == this && window2
->GetParent() == this, FALSE
,
685 _T("windows in the splitter should have it as parent!") );
688 m_windowOne
= window1
;
689 m_windowTwo
= window2
;
691 // remember the sash position we want to set for later if we can't set it
692 // right now (e.g. because the window is too small)
693 m_requestedSashPosition
= sashPosition
;
695 DoSetSashPosition(ConvertSashPosition(sashPosition
));
702 int wxSplitterWindow::ConvertSashPosition(int sashPosition
) const
704 if ( sashPosition
> 0 )
708 else if ( sashPosition
< 0 )
710 // It's negative so adding is subtracting
711 return GetWindowSize() + sashPosition
;
713 else // sashPosition == 0
715 // default, put it in the centre
716 return GetWindowSize() / 2;
720 // Remove the specified (or second) window from the view
721 // Doesn't actually delete the window.
722 bool wxSplitterWindow::Unsplit(wxWindow
*toRemove
)
727 wxWindow
*win
= NULL
;
728 if ( toRemove
== NULL
|| toRemove
== m_windowTwo
)
731 m_windowTwo
= (wxWindow
*) NULL
;
733 else if ( toRemove
== m_windowOne
)
736 m_windowOne
= m_windowTwo
;
737 m_windowTwo
= (wxWindow
*) NULL
;
741 wxFAIL_MSG(wxT("splitter: attempt to remove a non-existent window"));
747 DoSetSashPosition(0);
753 // Replace a window with another one
754 bool wxSplitterWindow::ReplaceWindow(wxWindow
*winOld
, wxWindow
*winNew
)
756 wxCHECK_MSG( winOld
, FALSE
, wxT("use one of Split() functions instead") );
757 wxCHECK_MSG( winNew
, FALSE
, wxT("use Unsplit() functions instead") );
759 if ( winOld
== m_windowTwo
)
761 m_windowTwo
= winNew
;
763 else if ( winOld
== m_windowOne
)
765 m_windowOne
= winNew
;
769 wxFAIL_MSG(wxT("splitter: attempt to replace a non-existent window"));
779 void wxSplitterWindow::SetMinimumPaneSize(int min
)
781 m_minimumPaneSize
= min
;
782 SetSashPosition(m_sashPosition
); // re-check limits
785 void wxSplitterWindow::SetSashPosition(int position
, bool redraw
)
787 DoSetSashPosition(position
);
795 bool wxSplitterWindow::DoSendEvent(wxSplitterEvent
& event
)
797 return !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed();
800 // ---------------------------------------------------------------------------
801 // wxSplitterWindow virtual functions: they now just generate the events
802 // ---------------------------------------------------------------------------
804 bool wxSplitterWindow::OnSashPositionChange(int WXUNUSED(newSashPosition
))
806 // always allow by default
810 int wxSplitterWindow::OnSashPositionChanging(int newSashPosition
)
812 // If within UNSPLIT_THRESHOLD from edge, set to edge to cause closure.
813 const int UNSPLIT_THRESHOLD
= 4;
815 // first of all, check if OnSashPositionChange() doesn't forbid this change
816 if ( !OnSashPositionChange(newSashPosition
) )
822 // Obtain relevant window dimension for bottom / right threshold check
823 int window_size
= GetWindowSize();
825 bool unsplit_scenario
= FALSE
;
826 if ( m_permitUnsplitAlways
|| m_minimumPaneSize
== 0 )
828 // Do edge detection if unsplit premitted
829 if ( newSashPosition
<= UNSPLIT_THRESHOLD
)
831 // threshold top / left check
833 unsplit_scenario
= TRUE
;
835 if ( newSashPosition
>= window_size
- UNSPLIT_THRESHOLD
)
837 // threshold bottom/right check
838 newSashPosition
= window_size
;
839 unsplit_scenario
= TRUE
;
843 if ( !unsplit_scenario
)
845 // If resultant pane would be too small, enlarge it
846 newSashPosition
= AdjustSashPosition(newSashPosition
);
849 // If the result is out of bounds it means minimum size is too big,
850 // so split window in half as best compromise.
851 if ( newSashPosition
< 0 || newSashPosition
> window_size
)
852 newSashPosition
= window_size
/ 2;
854 // now let the event handler have it
856 // FIXME: shouldn't we do it before the adjustments above so as to ensure
857 // that the sash position is always reasonable?
858 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING
, this);
859 event
.m_data
.pos
= newSashPosition
;
861 if ( !DoSendEvent(event
) )
863 // the event handler vetoed the change
864 newSashPosition
= -1;
868 // it could have been changed by it
869 newSashPosition
= event
.GetSashPosition();
872 return newSashPosition
;
875 // Called when the sash is double-clicked. The default behaviour is to remove
876 // the sash if the minimum pane size is zero.
877 void wxSplitterWindow::OnDoubleClickSash(int x
, int y
)
879 wxCHECK_RET(m_windowTwo
, wxT("splitter: no window to remove"));
881 // new code should handle events instead of using the virtual functions
882 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_DOUBLECLICKED
, this);
883 event
.m_data
.pt
.x
= x
;
884 event
.m_data
.pt
.y
= y
;
885 if ( DoSendEvent(event
) )
887 if ( GetMinimumPaneSize() == 0 || m_permitUnsplitAlways
)
889 wxWindow
* win
= m_windowTwo
;
892 wxSplitterEvent
unsplitEvent(wxEVT_COMMAND_SPLITTER_UNSPLIT
, this);
893 unsplitEvent
.m_data
.win
= win
;
894 (void)DoSendEvent(unsplitEvent
);
898 //else: blocked by user
901 void wxSplitterWindow::OnUnsplit(wxWindow
*winRemoved
)
903 // call this before calling the event handler which may delete the window
904 winRemoved
->Show(FALSE
);
907 #if defined( __WXMSW__ ) || defined( __WXMAC__)
909 // this is currently called (and needed) under MSW only...
910 void wxSplitterWindow::OnSetCursor(wxSetCursorEvent
& event
)
912 // if we don't do it, the resizing cursor might be set for child window:
913 // and like this we explicitly say that our cursor should not be used for
914 // children windows which overlap us
916 if ( SashHitTest(event
.GetX(), event
.GetY()) )
918 // default processing is ok
921 //else: do nothing, in particular, don't call Skip()
924 #endif // wxMSW || wxMac
926 #endif // wxUSE_SPLITTER