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"
40 #include "wx/mac/private.h"
43 #include "wx/renderer.h"
45 #include "wx/splitter.h"
49 DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED
)
50 DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING
)
51 DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPLITTER_DOUBLECLICKED
)
52 DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPLITTER_UNSPLIT
)
54 IMPLEMENT_DYNAMIC_CLASS(wxSplitterWindow
, wxWindow
)
65 IMPLEMENT_DYNAMIC_CLASS(wxSplitterEvent
, wxNotifyEvent
)
67 BEGIN_EVENT_TABLE(wxSplitterWindow
, wxWindow
)
68 EVT_PAINT(wxSplitterWindow::OnPaint
)
69 EVT_SIZE(wxSplitterWindow::OnSize
)
70 EVT_MOUSE_EVENTS(wxSplitterWindow::OnMouseEvent
)
72 #if defined( __WXMSW__ ) || defined( __WXMAC__)
73 EVT_SET_CURSOR(wxSplitterWindow::OnSetCursor
)
76 WX_EVENT_TABLE_CONTROL_CONTAINER(wxSplitterWindow
)
79 WX_DELEGATE_TO_CONTROL_CONTAINER(wxSplitterWindow
);
81 bool wxSplitterWindow::Create(wxWindow
*parent
, wxWindowID id
,
87 // allow TABbing from one window to the other
88 style
|= wxTAB_TRAVERSAL
;
90 // we draw our border ourselves to blend the sash with it
91 style
&= ~wxBORDER_MASK
;
92 style
|= wxBORDER_NONE
;
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_checkRequestedSashPosition
= false;
117 m_minimumPaneSize
= 0;
118 m_sashCursorWE
= wxCursor(wxCURSOR_SIZEWE
);
119 m_sashCursorNS
= wxCursor(wxCURSOR_SIZENS
);
120 m_sashTrackerPen
= new wxPen(*wxBLACK
, 2, wxSOLID
);
122 m_needUpdating
= false;
126 wxSplitterWindow::~wxSplitterWindow()
128 delete m_sashTrackerPen
;
131 // ----------------------------------------------------------------------------
132 // entering/leaving sash
133 // ----------------------------------------------------------------------------
135 void wxSplitterWindow::RedrawIfHotSensitive(bool isHot
)
137 if ( wxRendererNative::Get().GetSplitterParams(this).isHotSensitive
)
144 //else: we don't change our appearance, don't redraw to avoid flicker
147 void wxSplitterWindow::OnEnterSash()
151 RedrawIfHotSensitive(true);
154 void wxSplitterWindow::OnLeaveSash()
156 SetCursor(*wxSTANDARD_CURSOR
);
158 RedrawIfHotSensitive(false);
161 void wxSplitterWindow::SetResizeCursor()
163 SetCursor(m_splitMode
== wxSPLIT_VERTICAL
? m_sashCursorWE
167 // ----------------------------------------------------------------------------
168 // other event handlers
169 // ----------------------------------------------------------------------------
171 void wxSplitterWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
178 void wxSplitterWindow::OnInternalIdle()
180 wxWindow::OnInternalIdle();
182 // if this is the first idle time after a sash position has potentially
183 // been set, allow SizeWindows to check for a requested size.
184 if (!m_checkRequestedSashPosition
)
186 m_checkRequestedSashPosition
= true;
188 return; // it won't needUpdating in this case
195 void wxSplitterWindow::OnMouseEvent(wxMouseEvent
& event
)
197 int x
= (int)event
.GetX(),
198 y
= (int)event
.GetY();
200 if (GetWindowStyle() & wxSP_NOSASH
)
203 // with wxSP_LIVE_UPDATE style the splitter windows are always resized
204 // following the mouse movement while it drags the sash, without it we only
205 // draw the sash at the new position but only resize the windows when the
206 // dragging is finished
207 #if defined( __WXMAC__ ) && TARGET_API_MAC_OSX == 1
210 bool isLive
= (GetWindowStyleFlag() & wxSP_LIVE_UPDATE
) != 0;
212 if (event
.LeftDown())
214 if ( SashHitTest(x
, y
) )
216 // Start the drag now
217 m_dragMode
= wxSPLIT_DRAG_DRAGGING
;
219 // Capture mouse and set the cursor
225 // remember the initial sash position and draw the initial
227 m_sashPositionCurrent
= m_sashPosition
;
229 DrawSashTracker(x
, y
);
239 else if (event
.LeftUp() && m_dragMode
== wxSPLIT_DRAG_DRAGGING
)
241 // We can stop dragging now and see what we've got.
242 m_dragMode
= wxSPLIT_DRAG_NONE
;
244 // Release mouse and unset the cursor
246 SetCursor(* wxSTANDARD_CURSOR
);
248 // exit if unsplit after doubleclick
257 DrawSashTracker(m_oldX
, m_oldY
);
260 // the position of the click doesn't exactly correspond to
261 // m_sashPosition, rather it changes it by the distance by which the
263 int diff
= m_splitMode
== wxSPLIT_VERTICAL
? x
- m_oldX
: y
- m_oldY
;
265 int posSashOld
= isLive
? m_sashPosition
: m_sashPositionCurrent
;
266 int posSashNew
= OnSashPositionChanging(posSashOld
+ diff
);
267 if ( posSashNew
== -1 )
269 // change not allowed
273 if ( m_permitUnsplitAlways
|| m_minimumPaneSize
== 0 )
275 // Deal with possible unsplit scenarios
276 if ( posSashNew
== 0 )
278 // We remove the first window from the view
279 wxWindow
*removedWindow
= m_windowOne
;
280 m_windowOne
= m_windowTwo
;
281 m_windowTwo
= (wxWindow
*) NULL
;
282 OnUnsplit(removedWindow
);
283 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_UNSPLIT
, this);
284 event
.m_data
.win
= removedWindow
;
285 (void)DoSendEvent(event
);
286 SetSashPositionAndNotify(0);
288 else if ( posSashNew
== GetWindowSize() )
290 // We remove the second window from the view
291 wxWindow
*removedWindow
= m_windowTwo
;
292 m_windowTwo
= (wxWindow
*) NULL
;
293 OnUnsplit(removedWindow
);
294 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_UNSPLIT
, this);
295 event
.m_data
.win
= removedWindow
;
296 (void)DoSendEvent(event
);
297 SetSashPositionAndNotify(0);
301 SetSashPositionAndNotify(posSashNew
);
306 SetSashPositionAndNotify(posSashNew
);
310 } // left up && dragging
311 else if ((event
.Moving() || event
.Leaving() || event
.Entering()) && (m_dragMode
== wxSPLIT_DRAG_NONE
))
313 if ( event
.Leaving() || !SashHitTest(x
, y
) )
318 else if (event
.Dragging() && (m_dragMode
== wxSPLIT_DRAG_DRAGGING
))
320 int diff
= m_splitMode
== wxSPLIT_VERTICAL
? x
- m_oldX
: y
- m_oldY
;
323 // nothing to do, mouse didn't really move far enough
327 int posSashOld
= isLive
? m_sashPosition
: m_sashPositionCurrent
;
328 int posSashNew
= OnSashPositionChanging(posSashOld
+ diff
);
329 if ( posSashNew
== -1 )
331 // change not allowed
335 if ( posSashNew
== m_sashPosition
)
341 DrawSashTracker(m_oldX
, m_oldY
);
344 if (m_splitMode
== wxSPLIT_VERTICAL
)
349 // Remember old positions
354 // As we captured the mouse, we may get the mouse events from outside
355 // our window - for example, negative values in x, y. This has a weird
356 // consequence under MSW where we use unsigned values sometimes and
357 // signed ones other times: the coordinates turn as big positive
358 // numbers and so the sash is drawn on the *right* side of the window
359 // instead of the left (or bottom instead of top). Correct this.
360 if ( (short)m_oldX
< 0 )
362 if ( (short)m_oldY
< 0 )
369 m_sashPositionCurrent
= posSashNew
;
371 DrawSashTracker(m_oldX
, m_oldY
);
375 SetSashPositionAndNotify(posSashNew
);
376 m_needUpdating
= true;
379 else if ( event
.LeftDClick() && m_windowTwo
)
381 OnDoubleClickSash(x
, y
);
385 void wxSplitterWindow::OnSize(wxSizeEvent
& event
)
387 // only process this message if we're not iconized - otherwise iconizing
388 // and restoring a window containing the splitter has a funny side effect
389 // of changing the splitter position!
390 wxWindow
*parent
= wxGetTopLevelParent(this);
393 wxTopLevelWindow
*winTop
= wxDynamicCast(parent
, wxTopLevelWindow
);
396 iconized
= winTop
->IsIconized();
400 wxFAIL_MSG(wxT("should have a top level parent!"));
415 GetClientSize(&w
, &h
);
417 int size
= m_splitMode
== wxSPLIT_VERTICAL
? w
: h
;
418 if ( m_sashPosition
>= size
- 5 )
419 SetSashPositionAndNotify(wxMax(10, size
- 40));
425 bool wxSplitterWindow::SashHitTest(int x
, int y
, int tolerance
)
427 if ( m_windowTwo
== NULL
|| m_sashPosition
== 0)
428 return false; // No sash
430 int z
= m_splitMode
== wxSPLIT_VERTICAL
? x
: y
;
431 int hitMin
= m_sashPosition
- tolerance
;
432 int hitMax
= m_sashPosition
+ GetSashSize() + tolerance
;
434 return z
>= hitMin
&& z
<= hitMax
;
437 int wxSplitterWindow::GetSashSize() const
439 return wxRendererNative::Get().GetSplitterParams(this).widthSash
;
442 int wxSplitterWindow::GetBorderSize() const
444 return wxRendererNative::Get().GetSplitterParams(this).border
;
448 void wxSplitterWindow::DrawSash(wxDC
& dc
)
450 if (HasFlag(wxSP_3DBORDER
))
451 wxRendererNative::Get().DrawSplitterBorder
458 // don't draw sash if we're not split
459 if ( m_sashPosition
== 0 || !m_windowTwo
)
462 // nor if we're configured to not show it
463 if ( HasFlag(wxSP_NOSASH
) )
466 wxRendererNative::Get().DrawSplitterSash
472 m_splitMode
== wxSPLIT_VERTICAL
? wxVERTICAL
474 m_isHot
? wxCONTROL_CURRENT
: 0
478 // Draw the sash tracker (for whilst moving the sash)
479 void wxSplitterWindow::DrawSashTracker(int x
, int y
)
482 GetClientSize(&w
, &h
);
488 if ( m_splitMode
== wxSPLIT_VERTICAL
)
519 ClientToScreen(&x1
, &y1
);
520 ClientToScreen(&x2
, &y2
);
522 screenDC
.SetLogicalFunction(wxINVERT
);
523 screenDC
.SetPen(*m_sashTrackerPen
);
524 screenDC
.SetBrush(*wxTRANSPARENT_BRUSH
);
526 screenDC
.DrawLine(x1
, y1
, x2
, y2
);
528 screenDC
.SetLogicalFunction(wxCOPY
);
531 int wxSplitterWindow::GetWindowSize() const
533 wxSize size
= GetClientSize();
535 return m_splitMode
== wxSPLIT_VERTICAL
? size
.x
: size
.y
;
538 int wxSplitterWindow::AdjustSashPosition(int sashPos
) const
540 int window_size
= GetWindowSize();
547 // the window shouldn't be smaller than its own minimal size nor
548 // smaller than the minimual pane size specified for this splitter
549 int minSize
= m_splitMode
== wxSPLIT_VERTICAL
? win
->GetMinWidth()
550 : win
->GetMinHeight();
552 if ( minSize
== -1 || m_minimumPaneSize
> minSize
)
553 minSize
= m_minimumPaneSize
;
555 minSize
+= GetBorderSize();
557 if ( sashPos
< minSize
)
564 int minSize
= m_splitMode
== wxSPLIT_VERTICAL
? win
->GetMinWidth()
565 : win
->GetMinHeight();
567 if ( minSize
== -1 || m_minimumPaneSize
> minSize
)
568 minSize
= m_minimumPaneSize
;
570 int maxSize
= window_size
- minSize
- GetBorderSize();
571 if ( sashPos
> maxSize
)
578 bool wxSplitterWindow::DoSetSashPosition(int sashPos
)
580 int newSashPosition
= AdjustSashPosition(sashPos
);
582 if ( newSashPosition
== m_sashPosition
)
585 m_sashPosition
= newSashPosition
;
590 void wxSplitterWindow::SetSashPositionAndNotify(int sashPos
)
592 // we must reset the request here, otherwise the sash would be stuck at
593 // old position if the user attempted to move the sash after invalid
594 // (e.g. smaller than minsize) sash position was requested using
595 // SetSashPosition():
596 m_requestedSashPosition
= INT_MAX
;
598 if ( DoSetSashPosition(sashPos
) )
600 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED
, this);
601 event
.m_data
.pos
= m_sashPosition
;
603 (void)DoSendEvent(event
);
607 // Position and size subwindows.
608 // Note that the border size applies to each subwindow, not
609 // including the edges next to the sash.
610 void wxSplitterWindow::SizeWindows()
612 // check if we have delayed setting the real sash position
613 if ( m_checkRequestedSashPosition
&& m_requestedSashPosition
!= INT_MAX
)
615 int newSashPosition
= ConvertSashPosition(m_requestedSashPosition
);
616 if ( newSashPosition
!= m_sashPosition
)
618 DoSetSashPosition(newSashPosition
);
621 if ( newSashPosition
<= m_sashPosition
622 && newSashPosition
>= m_sashPosition
- GetBorderSize() )
624 // don't update it any more
625 m_requestedSashPosition
= INT_MAX
;
630 GetClientSize(&w
, &h
);
632 if ( GetWindow1() && !GetWindow2() )
634 GetWindow1()->SetSize(GetBorderSize(), GetBorderSize(),
635 w
- 2*GetBorderSize(), h
- 2*GetBorderSize());
637 else if ( GetWindow1() && GetWindow2() )
639 const int border
= GetBorderSize(),
640 sash
= GetSashSize();
642 int size1
= GetSashPosition() - border
,
643 size2
= GetSashPosition() + sash
;
645 int x2
, y2
, w1
, h1
, w2
, h2
;
646 if ( GetSplitMode() == wxSPLIT_VERTICAL
)
649 w2
= w
- 2*border
- sash
- w1
;
655 else // horz splitter
660 h2
= h
- 2*border
- sash
- h1
;
665 GetWindow1()->SetSize(border
, border
, w1
, h1
);
666 GetWindow2()->SetSize(x2
, y2
, w2
, h2
);
672 SetNeedUpdating(false);
675 // Set pane for unsplit window
676 void wxSplitterWindow::Initialize(wxWindow
*window
)
678 wxASSERT_MSG( (!window
|| (window
&& window
->GetParent() == this)),
679 _T("windows in the splitter should have it as parent!") );
681 m_windowOne
= window
;
682 m_windowTwo
= (wxWindow
*) NULL
;
683 DoSetSashPosition(0);
686 // Associates the given window with window 2, drawing the appropriate sash
687 // and changing the split mode.
688 // Does nothing and returns false if the window is already split.
689 bool wxSplitterWindow::DoSplit(wxSplitMode mode
,
690 wxWindow
*window1
, wxWindow
*window2
,
696 wxCHECK_MSG( window1
&& window2
, false,
697 _T("can not split with NULL window(s)") );
699 wxCHECK_MSG( window1
->GetParent() == this && window2
->GetParent() == this, false,
700 _T("windows in the splitter should have it as parent!") );
703 m_windowOne
= window1
;
704 m_windowTwo
= window2
;
706 // remember the sash position we want to set for later if we can't set it
707 // right now (e.g. because the window is too small)
708 m_requestedSashPosition
= sashPosition
;
709 m_checkRequestedSashPosition
= false;
711 DoSetSashPosition(ConvertSashPosition(sashPosition
));
718 int wxSplitterWindow::ConvertSashPosition(int sashPosition
) const
720 if ( sashPosition
> 0 )
724 else if ( sashPosition
< 0 )
726 // It's negative so adding is subtracting
727 return GetWindowSize() + sashPosition
;
729 else // sashPosition == 0
731 // default, put it in the centre
732 return GetWindowSize() / 2;
736 // Remove the specified (or second) window from the view
737 // Doesn't actually delete the window.
738 bool wxSplitterWindow::Unsplit(wxWindow
*toRemove
)
744 if ( toRemove
== NULL
|| toRemove
== m_windowTwo
)
747 m_windowTwo
= (wxWindow
*) NULL
;
749 else if ( toRemove
== m_windowOne
)
752 m_windowOne
= m_windowTwo
;
753 m_windowTwo
= (wxWindow
*) NULL
;
757 wxFAIL_MSG(wxT("splitter: attempt to remove a non-existent window"));
763 DoSetSashPosition(0);
769 // Replace a window with another one
770 bool wxSplitterWindow::ReplaceWindow(wxWindow
*winOld
, wxWindow
*winNew
)
772 wxCHECK_MSG( winOld
, false, wxT("use one of Split() functions instead") );
773 wxCHECK_MSG( winNew
, false, wxT("use Unsplit() functions instead") );
775 if ( winOld
== m_windowTwo
)
777 m_windowTwo
= winNew
;
779 else if ( winOld
== m_windowOne
)
781 m_windowOne
= winNew
;
785 wxFAIL_MSG(wxT("splitter: attempt to replace a non-existent window"));
795 void wxSplitterWindow::SetMinimumPaneSize(int min
)
797 m_minimumPaneSize
= min
;
798 int pos
= m_requestedSashPosition
!= INT_MAX
? m_requestedSashPosition
: m_sashPosition
;
799 SetSashPosition(pos
); // re-check limits
802 void wxSplitterWindow::SetSashPosition(int position
, bool redraw
)
804 // remember the sash position we want to set for later if we can't set it
805 // right now (e.g. because the window is too small)
806 m_requestedSashPosition
= position
;
807 m_checkRequestedSashPosition
= false;
809 DoSetSashPosition(ConvertSashPosition(position
));
817 // Make sure the child window sizes are updated. This is useful
818 // for reducing flicker by updating the sizes before a
819 // window is shown, if you know the overall size is correct.
820 void wxSplitterWindow::UpdateSize()
822 m_checkRequestedSashPosition
= true;
824 m_checkRequestedSashPosition
= false;
827 bool wxSplitterWindow::DoSendEvent(wxSplitterEvent
& event
)
829 return !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed();
832 // ---------------------------------------------------------------------------
833 // wxSplitterWindow virtual functions: they now just generate the events
834 // ---------------------------------------------------------------------------
836 bool wxSplitterWindow::OnSashPositionChange(int WXUNUSED(newSashPosition
))
838 // always allow by default
842 int wxSplitterWindow::OnSashPositionChanging(int newSashPosition
)
844 // If within UNSPLIT_THRESHOLD from edge, set to edge to cause closure.
845 const int UNSPLIT_THRESHOLD
= 4;
847 // first of all, check if OnSashPositionChange() doesn't forbid this change
848 if ( !OnSashPositionChange(newSashPosition
) )
854 // Obtain relevant window dimension for bottom / right threshold check
855 int window_size
= GetWindowSize();
857 bool unsplit_scenario
= false;
858 if ( m_permitUnsplitAlways
|| m_minimumPaneSize
== 0 )
860 // Do edge detection if unsplit premitted
861 if ( newSashPosition
<= UNSPLIT_THRESHOLD
)
863 // threshold top / left check
865 unsplit_scenario
= true;
867 if ( newSashPosition
>= window_size
- UNSPLIT_THRESHOLD
)
869 // threshold bottom/right check
870 newSashPosition
= window_size
;
871 unsplit_scenario
= true;
875 if ( !unsplit_scenario
)
877 // If resultant pane would be too small, enlarge it
878 newSashPosition
= AdjustSashPosition(newSashPosition
);
881 // If the result is out of bounds it means minimum size is too big,
882 // so split window in half as best compromise.
883 if ( newSashPosition
< 0 || newSashPosition
> window_size
)
884 newSashPosition
= window_size
/ 2;
886 // now let the event handler have it
888 // FIXME: shouldn't we do it before the adjustments above so as to ensure
889 // that the sash position is always reasonable?
890 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING
, this);
891 event
.m_data
.pos
= newSashPosition
;
893 if ( !DoSendEvent(event
) )
895 // the event handler vetoed the change
896 newSashPosition
= -1;
900 // it could have been changed by it
901 newSashPosition
= event
.GetSashPosition();
904 return newSashPosition
;
907 // Called when the sash is double-clicked. The default behaviour is to remove
908 // the sash if the minimum pane size is zero.
909 void wxSplitterWindow::OnDoubleClickSash(int x
, int y
)
911 wxCHECK_RET(m_windowTwo
, wxT("splitter: no window to remove"));
913 // new code should handle events instead of using the virtual functions
914 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_DOUBLECLICKED
, this);
915 event
.m_data
.pt
.x
= x
;
916 event
.m_data
.pt
.y
= y
;
917 if ( DoSendEvent(event
) )
919 if ( GetMinimumPaneSize() == 0 || m_permitUnsplitAlways
)
921 wxWindow
* win
= m_windowTwo
;
924 wxSplitterEvent
unsplitEvent(wxEVT_COMMAND_SPLITTER_UNSPLIT
, this);
925 unsplitEvent
.m_data
.win
= win
;
926 (void)DoSendEvent(unsplitEvent
);
930 //else: blocked by user
933 void wxSplitterWindow::OnUnsplit(wxWindow
*winRemoved
)
935 // call this before calling the event handler which may delete the window
936 winRemoved
->Show(false);
939 #if defined( __WXMSW__ ) || defined( __WXMAC__)
941 // this is currently called (and needed) under MSW only...
942 void wxSplitterWindow::OnSetCursor(wxSetCursorEvent
& event
)
944 // if we don't do it, the resizing cursor might be set for child window:
945 // and like this we explicitly say that our cursor should not be used for
946 // children windows which overlap us
948 if ( SashHitTest(event
.GetX(), event
.GetY(), 0) )
950 // default processing is ok
953 //else: do nothing, in particular, don't call Skip()
956 #endif // wxMSW || wxMac
958 #endif // wxUSE_SPLITTER