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 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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 if ( !wxWindow::Create(parent
, id
, pos
, size
, style
, name
) )
93 m_permitUnsplitAlways
= (style
& wxSP_PERMIT_UNSPLIT
) != 0;
98 void wxSplitterWindow::Init()
100 m_container
.SetContainerWindow(this);
102 m_splitMode
= wxSPLIT_VERTICAL
;
103 m_permitUnsplitAlways
= true;
104 m_windowOne
= (wxWindow
*) NULL
;
105 m_windowTwo
= (wxWindow
*) NULL
;
106 m_dragMode
= wxSPLIT_DRAG_NONE
;
111 m_sashPosition
= m_requestedSashPosition
= 0;
112 m_checkRequestedSashPosition
= false;
113 m_minimumPaneSize
= 0;
114 m_sashCursorWE
= wxCursor(wxCURSOR_SIZEWE
);
115 m_sashCursorNS
= wxCursor(wxCURSOR_SIZENS
);
116 m_sashTrackerPen
= new wxPen(*wxBLACK
, 2, wxSOLID
);
118 m_needUpdating
= false;
122 wxSplitterWindow::~wxSplitterWindow()
124 delete m_sashTrackerPen
;
127 // ----------------------------------------------------------------------------
128 // entering/leaving sash
129 // ----------------------------------------------------------------------------
131 void wxSplitterWindow::RedrawIfHotSensitive(bool isHot
)
133 if ( wxRendererNative::Get().GetSplitterParams(this).isHotSensitive
)
140 //else: we don't change our appearance, don't redraw to avoid flicker
143 void wxSplitterWindow::OnEnterSash()
147 RedrawIfHotSensitive(true);
150 void wxSplitterWindow::OnLeaveSash()
152 SetCursor(*wxSTANDARD_CURSOR
);
154 RedrawIfHotSensitive(false);
157 void wxSplitterWindow::SetResizeCursor()
159 SetCursor(m_splitMode
== wxSPLIT_VERTICAL
? m_sashCursorWE
163 // ----------------------------------------------------------------------------
164 // other event handlers
165 // ----------------------------------------------------------------------------
167 void wxSplitterWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
174 void wxSplitterWindow::OnInternalIdle()
176 wxWindow::OnInternalIdle();
178 // if this is the first idle time after a sash position has potentially
179 // been set, allow SizeWindows to check for a requested size.
180 if (!m_checkRequestedSashPosition
)
182 m_checkRequestedSashPosition
= true;
184 return; // it won't needUpdating in this case
191 void wxSplitterWindow::OnMouseEvent(wxMouseEvent
& event
)
193 int x
= (int)event
.GetX(),
194 y
= (int)event
.GetY();
196 if (GetWindowStyle() & wxSP_NOSASH
)
199 // with wxSP_LIVE_UPDATE style the splitter windows are always resized
200 // following the mouse movement while it drags the sash, without it we only
201 // draw the sash at the new position but only resize the windows when the
202 // dragging is finished
203 bool isLive
= (GetWindowStyleFlag() & wxSP_LIVE_UPDATE
) != 0;
205 if (event
.LeftDown())
207 if ( SashHitTest(x
, y
) )
209 // Start the drag now
210 m_dragMode
= wxSPLIT_DRAG_DRAGGING
;
212 // Capture mouse and set the cursor
218 // remember the initial sash position and draw the initial
220 m_sashPositionCurrent
= m_sashPosition
;
222 DrawSashTracker(x
, y
);
232 else if (event
.LeftUp() && m_dragMode
== wxSPLIT_DRAG_DRAGGING
)
234 // We can stop dragging now and see what we've got.
235 m_dragMode
= wxSPLIT_DRAG_NONE
;
237 // Release mouse and unset the cursor
239 SetCursor(* wxSTANDARD_CURSOR
);
241 // exit if unsplit after doubleclick
250 DrawSashTracker(m_oldX
, m_oldY
);
253 // the position of the click doesn't exactly correspond to
254 // m_sashPosition, rather it changes it by the distance by which the
256 int diff
= m_splitMode
== wxSPLIT_VERTICAL
? x
- m_oldX
: y
- m_oldY
;
258 int posSashOld
= isLive
? m_sashPosition
: m_sashPositionCurrent
;
259 int posSashNew
= OnSashPositionChanging(posSashOld
+ diff
);
260 if ( posSashNew
== -1 )
262 // change not allowed
266 if ( m_permitUnsplitAlways
|| m_minimumPaneSize
== 0 )
268 // Deal with possible unsplit scenarios
269 if ( posSashNew
== 0 )
271 // We remove the first window from the view
272 wxWindow
*removedWindow
= m_windowOne
;
273 m_windowOne
= m_windowTwo
;
274 m_windowTwo
= (wxWindow
*) NULL
;
275 OnUnsplit(removedWindow
);
276 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_UNSPLIT
, this);
277 event
.m_data
.win
= removedWindow
;
278 (void)DoSendEvent(event
);
279 SetSashPositionAndNotify(0);
281 else if ( posSashNew
== GetWindowSize() )
283 // We remove the second window from the view
284 wxWindow
*removedWindow
= m_windowTwo
;
285 m_windowTwo
= (wxWindow
*) NULL
;
286 OnUnsplit(removedWindow
);
287 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_UNSPLIT
, this);
288 event
.m_data
.win
= removedWindow
;
289 (void)DoSendEvent(event
);
290 SetSashPositionAndNotify(0);
294 SetSashPositionAndNotify(posSashNew
);
299 SetSashPositionAndNotify(posSashNew
);
303 } // left up && dragging
304 else if ((event
.Moving() || event
.Leaving() || event
.Entering()) && (m_dragMode
== wxSPLIT_DRAG_NONE
))
306 if ( event
.Leaving() || !SashHitTest(x
, y
) )
311 else if (event
.Dragging() && (m_dragMode
== wxSPLIT_DRAG_DRAGGING
))
313 int diff
= m_splitMode
== wxSPLIT_VERTICAL
? x
- m_oldX
: y
- m_oldY
;
316 // nothing to do, mouse didn't really move far enough
320 int posSashOld
= isLive
? m_sashPosition
: m_sashPositionCurrent
;
321 int posSashNew
= OnSashPositionChanging(posSashOld
+ diff
);
322 if ( posSashNew
== -1 )
324 // change not allowed
328 if ( posSashNew
== m_sashPosition
)
334 DrawSashTracker(m_oldX
, m_oldY
);
337 if (m_splitMode
== wxSPLIT_VERTICAL
)
342 // Remember old positions
347 // As we captured the mouse, we may get the mouse events from outside
348 // our window - for example, negative values in x, y. This has a weird
349 // consequence under MSW where we use unsigned values sometimes and
350 // signed ones other times: the coordinates turn as big positive
351 // numbers and so the sash is drawn on the *right* side of the window
352 // instead of the left (or bottom instead of top). Correct this.
353 if ( (short)m_oldX
< 0 )
355 if ( (short)m_oldY
< 0 )
362 m_sashPositionCurrent
= posSashNew
;
364 DrawSashTracker(m_oldX
, m_oldY
);
368 SetSashPositionAndNotify(posSashNew
);
369 m_needUpdating
= true;
372 else if ( event
.LeftDClick() && m_windowTwo
)
374 OnDoubleClickSash(x
, y
);
378 void wxSplitterWindow::OnSize(wxSizeEvent
& event
)
380 // only process this message if we're not iconized - otherwise iconizing
381 // and restoring a window containing the splitter has a funny side effect
382 // of changing the splitter position!
383 wxWindow
*parent
= wxGetTopLevelParent(this);
386 wxTopLevelWindow
*winTop
= wxDynamicCast(parent
, wxTopLevelWindow
);
389 iconized
= winTop
->IsIconized();
393 wxFAIL_MSG(wxT("should have a top level parent!"));
408 GetClientSize(&w
, &h
);
410 int size
= m_splitMode
== wxSPLIT_VERTICAL
? w
: h
;
411 if ( m_sashPosition
>= size
- 5 )
412 SetSashPositionAndNotify(wxMax(10, size
- 40));
418 bool wxSplitterWindow::SashHitTest(int x
, int y
, int tolerance
)
420 if ( m_windowTwo
== NULL
|| m_sashPosition
== 0)
421 return false; // No sash
423 int z
= m_splitMode
== wxSPLIT_VERTICAL
? x
: y
;
424 int hitMin
= m_sashPosition
- tolerance
;
425 int hitMax
= m_sashPosition
+ GetSashSize() + tolerance
;
427 return z
>= hitMin
&& z
<= hitMax
;
430 int wxSplitterWindow::GetSashSize() const
432 return wxRendererNative::Get().GetSplitterParams(this).widthSash
;
435 int wxSplitterWindow::GetBorderSize() const
437 return wxRendererNative::Get().GetSplitterParams(this).border
;
441 void wxSplitterWindow::DrawSash(wxDC
& dc
)
443 if (HasFlag(wxSP_3DBORDER
))
444 wxRendererNative::Get().DrawSplitterBorder
451 // don't draw sash if we're not split
452 if ( m_sashPosition
== 0 || !m_windowTwo
)
455 // nor if we're configured to not show it
456 if ( HasFlag(wxSP_NOSASH
) )
459 wxRendererNative::Get().DrawSplitterSash
465 m_splitMode
== wxSPLIT_VERTICAL
? wxVERTICAL
467 m_isHot
? wxCONTROL_CURRENT
: 0
471 // Draw the sash tracker (for whilst moving the sash)
472 void wxSplitterWindow::DrawSashTracker(int x
, int y
)
475 GetClientSize(&w
, &h
);
481 if ( m_splitMode
== wxSPLIT_VERTICAL
)
512 ClientToScreen(&x1
, &y1
);
513 ClientToScreen(&x2
, &y2
);
515 screenDC
.SetLogicalFunction(wxINVERT
);
516 screenDC
.SetPen(*m_sashTrackerPen
);
517 screenDC
.SetBrush(*wxTRANSPARENT_BRUSH
);
519 screenDC
.DrawLine(x1
, y1
, x2
, y2
);
521 screenDC
.SetLogicalFunction(wxCOPY
);
524 int wxSplitterWindow::GetWindowSize() const
526 wxSize size
= GetClientSize();
528 return m_splitMode
== wxSPLIT_VERTICAL
? size
.x
: size
.y
;
531 int wxSplitterWindow::AdjustSashPosition(int sashPos
) const
533 int window_size
= GetWindowSize();
540 // the window shouldn't be smaller than its own minimal size nor
541 // smaller than the minimual pane size specified for this splitter
542 int minSize
= m_splitMode
== wxSPLIT_VERTICAL
? win
->GetMinWidth()
543 : win
->GetMinHeight();
545 if ( minSize
== -1 || m_minimumPaneSize
> minSize
)
546 minSize
= m_minimumPaneSize
;
548 minSize
+= GetBorderSize();
550 if ( sashPos
< minSize
)
557 int minSize
= m_splitMode
== wxSPLIT_VERTICAL
? win
->GetMinWidth()
558 : win
->GetMinHeight();
560 if ( minSize
== -1 || m_minimumPaneSize
> minSize
)
561 minSize
= m_minimumPaneSize
;
563 int maxSize
= window_size
- minSize
- GetBorderSize();
564 if ( sashPos
> maxSize
)
571 bool wxSplitterWindow::DoSetSashPosition(int sashPos
)
573 int newSashPosition
= AdjustSashPosition(sashPos
);
575 if ( newSashPosition
== m_sashPosition
)
578 m_sashPosition
= newSashPosition
;
583 void wxSplitterWindow::SetSashPositionAndNotify(int sashPos
)
585 if ( DoSetSashPosition(sashPos
) )
587 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED
, this);
588 event
.m_data
.pos
= m_sashPosition
;
590 (void)DoSendEvent(event
);
594 // Position and size subwindows.
595 // Note that the border size applies to each subwindow, not
596 // including the edges next to the sash.
597 void wxSplitterWindow::SizeWindows()
599 // check if we have delayed setting the real sash position
600 if ( m_checkRequestedSashPosition
&& m_requestedSashPosition
!= INT_MAX
)
602 int newSashPosition
= ConvertSashPosition(m_requestedSashPosition
);
603 if ( newSashPosition
!= m_sashPosition
)
605 DoSetSashPosition(newSashPosition
);
608 if ( newSashPosition
<= m_sashPosition
609 && newSashPosition
>= m_sashPosition
- GetBorderSize() )
611 // don't update it any more
612 m_requestedSashPosition
= INT_MAX
;
617 GetClientSize(&w
, &h
);
619 if ( GetWindow1() && !GetWindow2() )
621 GetWindow1()->SetSize(GetBorderSize(), GetBorderSize(),
622 w
- 2*GetBorderSize(), h
- 2*GetBorderSize());
624 else if ( GetWindow1() && GetWindow2() )
626 const int border
= GetBorderSize(),
627 sash
= GetSashSize();
629 int size1
= GetSashPosition() - border
,
630 size2
= GetSashPosition() + sash
;
632 int x2
, y2
, w1
, h1
, w2
, h2
;
633 if ( GetSplitMode() == wxSPLIT_VERTICAL
)
636 w2
= w
- 2*border
- sash
- w1
;
642 else // horz splitter
647 h2
= h
- 2*border
- sash
- h1
;
652 GetWindow1()->SetSize(border
, border
, w1
, h1
);
653 GetWindow2()->SetSize(x2
, y2
, w2
, h2
);
659 SetNeedUpdating(false);
662 // Set pane for unsplit window
663 void wxSplitterWindow::Initialize(wxWindow
*window
)
665 wxASSERT_MSG( window
&& window
->GetParent() == this,
666 _T("windows in the splitter should have it as parent!") );
668 m_windowOne
= window
;
669 m_windowTwo
= (wxWindow
*) NULL
;
670 DoSetSashPosition(0);
673 // Associates the given window with window 2, drawing the appropriate sash
674 // and changing the split mode.
675 // Does nothing and returns false if the window is already split.
676 bool wxSplitterWindow::DoSplit(wxSplitMode mode
,
677 wxWindow
*window1
, wxWindow
*window2
,
683 wxCHECK_MSG( window1
&& window2
, false,
684 _T("can not split with NULL window(s)") );
686 wxCHECK_MSG( window1
->GetParent() == this && window2
->GetParent() == this, false,
687 _T("windows in the splitter should have it as parent!") );
690 m_windowOne
= window1
;
691 m_windowTwo
= window2
;
693 // remember the sash position we want to set for later if we can't set it
694 // right now (e.g. because the window is too small)
695 m_requestedSashPosition
= sashPosition
;
696 m_checkRequestedSashPosition
= false;
698 DoSetSashPosition(ConvertSashPosition(sashPosition
));
705 int wxSplitterWindow::ConvertSashPosition(int sashPosition
) const
707 if ( sashPosition
> 0 )
711 else if ( sashPosition
< 0 )
713 // It's negative so adding is subtracting
714 return GetWindowSize() + sashPosition
;
716 else // sashPosition == 0
718 // default, put it in the centre
719 return GetWindowSize() / 2;
723 // Remove the specified (or second) window from the view
724 // Doesn't actually delete the window.
725 bool wxSplitterWindow::Unsplit(wxWindow
*toRemove
)
731 if ( toRemove
== NULL
|| toRemove
== m_windowTwo
)
734 m_windowTwo
= (wxWindow
*) NULL
;
736 else if ( toRemove
== m_windowOne
)
739 m_windowOne
= m_windowTwo
;
740 m_windowTwo
= (wxWindow
*) NULL
;
744 wxFAIL_MSG(wxT("splitter: attempt to remove a non-existent window"));
750 DoSetSashPosition(0);
756 // Replace a window with another one
757 bool wxSplitterWindow::ReplaceWindow(wxWindow
*winOld
, wxWindow
*winNew
)
759 wxCHECK_MSG( winOld
, false, wxT("use one of Split() functions instead") );
760 wxCHECK_MSG( winNew
, false, wxT("use Unsplit() functions instead") );
762 if ( winOld
== m_windowTwo
)
764 m_windowTwo
= winNew
;
766 else if ( winOld
== m_windowOne
)
768 m_windowOne
= winNew
;
772 wxFAIL_MSG(wxT("splitter: attempt to replace a non-existent window"));
782 void wxSplitterWindow::SetMinimumPaneSize(int min
)
784 m_minimumPaneSize
= min
;
785 int pos
= m_requestedSashPosition
!= INT_MAX
? m_requestedSashPosition
: m_sashPosition
;
786 SetSashPosition(pos
); // re-check limits
789 void wxSplitterWindow::SetSashPosition(int position
, bool redraw
)
791 // remember the sash position we want to set for later if we can't set it
792 // right now (e.g. because the window is too small)
793 m_requestedSashPosition
= position
;
794 m_checkRequestedSashPosition
= false;
796 DoSetSashPosition(ConvertSashPosition(position
));
804 // Make sure the child window sizes are updated. This is useful
805 // for reducing flicker by updating the sizes before a
806 // window is shown, if you know the overall size is correct.
807 void wxSplitterWindow::UpdateSize()
809 m_checkRequestedSashPosition
= true;
811 m_checkRequestedSashPosition
= false;
814 bool wxSplitterWindow::DoSendEvent(wxSplitterEvent
& event
)
816 return !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed();
819 // ---------------------------------------------------------------------------
820 // wxSplitterWindow virtual functions: they now just generate the events
821 // ---------------------------------------------------------------------------
823 bool wxSplitterWindow::OnSashPositionChange(int WXUNUSED(newSashPosition
))
825 // always allow by default
829 int wxSplitterWindow::OnSashPositionChanging(int newSashPosition
)
831 // If within UNSPLIT_THRESHOLD from edge, set to edge to cause closure.
832 const int UNSPLIT_THRESHOLD
= 4;
834 // first of all, check if OnSashPositionChange() doesn't forbid this change
835 if ( !OnSashPositionChange(newSashPosition
) )
841 // Obtain relevant window dimension for bottom / right threshold check
842 int window_size
= GetWindowSize();
844 bool unsplit_scenario
= false;
845 if ( m_permitUnsplitAlways
|| m_minimumPaneSize
== 0 )
847 // Do edge detection if unsplit premitted
848 if ( newSashPosition
<= UNSPLIT_THRESHOLD
)
850 // threshold top / left check
852 unsplit_scenario
= true;
854 if ( newSashPosition
>= window_size
- UNSPLIT_THRESHOLD
)
856 // threshold bottom/right check
857 newSashPosition
= window_size
;
858 unsplit_scenario
= true;
862 if ( !unsplit_scenario
)
864 // If resultant pane would be too small, enlarge it
865 newSashPosition
= AdjustSashPosition(newSashPosition
);
868 // If the result is out of bounds it means minimum size is too big,
869 // so split window in half as best compromise.
870 if ( newSashPosition
< 0 || newSashPosition
> window_size
)
871 newSashPosition
= window_size
/ 2;
873 // now let the event handler have it
875 // FIXME: shouldn't we do it before the adjustments above so as to ensure
876 // that the sash position is always reasonable?
877 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING
, this);
878 event
.m_data
.pos
= newSashPosition
;
880 if ( !DoSendEvent(event
) )
882 // the event handler vetoed the change
883 newSashPosition
= -1;
887 // it could have been changed by it
888 newSashPosition
= event
.GetSashPosition();
891 return newSashPosition
;
894 // Called when the sash is double-clicked. The default behaviour is to remove
895 // the sash if the minimum pane size is zero.
896 void wxSplitterWindow::OnDoubleClickSash(int x
, int y
)
898 wxCHECK_RET(m_windowTwo
, wxT("splitter: no window to remove"));
900 // new code should handle events instead of using the virtual functions
901 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_DOUBLECLICKED
, this);
902 event
.m_data
.pt
.x
= x
;
903 event
.m_data
.pt
.y
= y
;
904 if ( DoSendEvent(event
) )
906 if ( GetMinimumPaneSize() == 0 || m_permitUnsplitAlways
)
908 wxWindow
* win
= m_windowTwo
;
911 wxSplitterEvent
unsplitEvent(wxEVT_COMMAND_SPLITTER_UNSPLIT
, this);
912 unsplitEvent
.m_data
.win
= win
;
913 (void)DoSendEvent(unsplitEvent
);
917 //else: blocked by user
920 void wxSplitterWindow::OnUnsplit(wxWindow
*winRemoved
)
922 // call this before calling the event handler which may delete the window
923 winRemoved
->Show(false);
926 #if defined( __WXMSW__ ) || defined( __WXMAC__)
928 // this is currently called (and needed) under MSW only...
929 void wxSplitterWindow::OnSetCursor(wxSetCursorEvent
& event
)
931 // if we don't do it, the resizing cursor might be set for child window:
932 // and like this we explicitly say that our cursor should not be used for
933 // children windows which overlap us
935 if ( SashHitTest(event
.GetX(), event
.GetY(), 0) )
937 // default processing is ok
940 //else: do nothing, in particular, don't call Skip()
943 #endif // wxMSW || wxMac
945 #endif // wxUSE_SPLITTER