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 // we must reset the request here, otherwise the sash would be stuck at
586 // old position if the user attempted to move the sash after invalid
587 // (e.g. smaller than minsize) sash position was requested using
588 // SetSashPosition():
589 m_requestedSashPosition
= INT_MAX
;
591 if ( DoSetSashPosition(sashPos
) )
593 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED
, this);
594 event
.m_data
.pos
= m_sashPosition
;
596 (void)DoSendEvent(event
);
600 // Position and size subwindows.
601 // Note that the border size applies to each subwindow, not
602 // including the edges next to the sash.
603 void wxSplitterWindow::SizeWindows()
605 // check if we have delayed setting the real sash position
606 if ( m_checkRequestedSashPosition
&& m_requestedSashPosition
!= INT_MAX
)
608 int newSashPosition
= ConvertSashPosition(m_requestedSashPosition
);
609 if ( newSashPosition
!= m_sashPosition
)
611 DoSetSashPosition(newSashPosition
);
614 if ( newSashPosition
<= m_sashPosition
615 && newSashPosition
>= m_sashPosition
- GetBorderSize() )
617 // don't update it any more
618 m_requestedSashPosition
= INT_MAX
;
623 GetClientSize(&w
, &h
);
625 if ( GetWindow1() && !GetWindow2() )
627 GetWindow1()->SetSize(GetBorderSize(), GetBorderSize(),
628 w
- 2*GetBorderSize(), h
- 2*GetBorderSize());
630 else if ( GetWindow1() && GetWindow2() )
632 const int border
= GetBorderSize(),
633 sash
= GetSashSize();
635 int size1
= GetSashPosition() - border
,
636 size2
= GetSashPosition() + sash
;
638 int x2
, y2
, w1
, h1
, w2
, h2
;
639 if ( GetSplitMode() == wxSPLIT_VERTICAL
)
642 w2
= w
- 2*border
- sash
- w1
;
648 else // horz splitter
653 h2
= h
- 2*border
- sash
- h1
;
658 GetWindow1()->SetSize(border
, border
, w1
, h1
);
659 GetWindow2()->SetSize(x2
, y2
, w2
, h2
);
665 SetNeedUpdating(false);
668 // Set pane for unsplit window
669 void wxSplitterWindow::Initialize(wxWindow
*window
)
671 wxASSERT_MSG( (!window
|| (window
&& window
->GetParent() == this)),
672 _T("windows in the splitter should have it as parent!") );
674 m_windowOne
= window
;
675 m_windowTwo
= (wxWindow
*) NULL
;
676 DoSetSashPosition(0);
679 // Associates the given window with window 2, drawing the appropriate sash
680 // and changing the split mode.
681 // Does nothing and returns false if the window is already split.
682 bool wxSplitterWindow::DoSplit(wxSplitMode mode
,
683 wxWindow
*window1
, wxWindow
*window2
,
689 wxCHECK_MSG( window1
&& window2
, false,
690 _T("can not split with NULL window(s)") );
692 wxCHECK_MSG( window1
->GetParent() == this && window2
->GetParent() == this, false,
693 _T("windows in the splitter should have it as parent!") );
696 m_windowOne
= window1
;
697 m_windowTwo
= window2
;
699 // remember the sash position we want to set for later if we can't set it
700 // right now (e.g. because the window is too small)
701 m_requestedSashPosition
= sashPosition
;
702 m_checkRequestedSashPosition
= false;
704 DoSetSashPosition(ConvertSashPosition(sashPosition
));
711 int wxSplitterWindow::ConvertSashPosition(int sashPosition
) const
713 if ( sashPosition
> 0 )
717 else if ( sashPosition
< 0 )
719 // It's negative so adding is subtracting
720 return GetWindowSize() + sashPosition
;
722 else // sashPosition == 0
724 // default, put it in the centre
725 return GetWindowSize() / 2;
729 // Remove the specified (or second) window from the view
730 // Doesn't actually delete the window.
731 bool wxSplitterWindow::Unsplit(wxWindow
*toRemove
)
737 if ( toRemove
== NULL
|| toRemove
== m_windowTwo
)
740 m_windowTwo
= (wxWindow
*) NULL
;
742 else if ( toRemove
== m_windowOne
)
745 m_windowOne
= m_windowTwo
;
746 m_windowTwo
= (wxWindow
*) NULL
;
750 wxFAIL_MSG(wxT("splitter: attempt to remove a non-existent window"));
756 DoSetSashPosition(0);
762 // Replace a window with another one
763 bool wxSplitterWindow::ReplaceWindow(wxWindow
*winOld
, wxWindow
*winNew
)
765 wxCHECK_MSG( winOld
, false, wxT("use one of Split() functions instead") );
766 wxCHECK_MSG( winNew
, false, wxT("use Unsplit() functions instead") );
768 if ( winOld
== m_windowTwo
)
770 m_windowTwo
= winNew
;
772 else if ( winOld
== m_windowOne
)
774 m_windowOne
= winNew
;
778 wxFAIL_MSG(wxT("splitter: attempt to replace a non-existent window"));
788 void wxSplitterWindow::SetMinimumPaneSize(int min
)
790 m_minimumPaneSize
= min
;
791 int pos
= m_requestedSashPosition
!= INT_MAX
? m_requestedSashPosition
: m_sashPosition
;
792 SetSashPosition(pos
); // re-check limits
795 void wxSplitterWindow::SetSashPosition(int position
, bool redraw
)
797 // remember the sash position we want to set for later if we can't set it
798 // right now (e.g. because the window is too small)
799 m_requestedSashPosition
= position
;
800 m_checkRequestedSashPosition
= false;
802 DoSetSashPosition(ConvertSashPosition(position
));
810 // Make sure the child window sizes are updated. This is useful
811 // for reducing flicker by updating the sizes before a
812 // window is shown, if you know the overall size is correct.
813 void wxSplitterWindow::UpdateSize()
815 m_checkRequestedSashPosition
= true;
817 m_checkRequestedSashPosition
= false;
820 bool wxSplitterWindow::DoSendEvent(wxSplitterEvent
& event
)
822 return !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed();
825 // ---------------------------------------------------------------------------
826 // wxSplitterWindow virtual functions: they now just generate the events
827 // ---------------------------------------------------------------------------
829 bool wxSplitterWindow::OnSashPositionChange(int WXUNUSED(newSashPosition
))
831 // always allow by default
835 int wxSplitterWindow::OnSashPositionChanging(int newSashPosition
)
837 // If within UNSPLIT_THRESHOLD from edge, set to edge to cause closure.
838 const int UNSPLIT_THRESHOLD
= 4;
840 // first of all, check if OnSashPositionChange() doesn't forbid this change
841 if ( !OnSashPositionChange(newSashPosition
) )
847 // Obtain relevant window dimension for bottom / right threshold check
848 int window_size
= GetWindowSize();
850 bool unsplit_scenario
= false;
851 if ( m_permitUnsplitAlways
|| m_minimumPaneSize
== 0 )
853 // Do edge detection if unsplit premitted
854 if ( newSashPosition
<= UNSPLIT_THRESHOLD
)
856 // threshold top / left check
858 unsplit_scenario
= true;
860 if ( newSashPosition
>= window_size
- UNSPLIT_THRESHOLD
)
862 // threshold bottom/right check
863 newSashPosition
= window_size
;
864 unsplit_scenario
= true;
868 if ( !unsplit_scenario
)
870 // If resultant pane would be too small, enlarge it
871 newSashPosition
= AdjustSashPosition(newSashPosition
);
874 // If the result is out of bounds it means minimum size is too big,
875 // so split window in half as best compromise.
876 if ( newSashPosition
< 0 || newSashPosition
> window_size
)
877 newSashPosition
= window_size
/ 2;
879 // now let the event handler have it
881 // FIXME: shouldn't we do it before the adjustments above so as to ensure
882 // that the sash position is always reasonable?
883 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING
, this);
884 event
.m_data
.pos
= newSashPosition
;
886 if ( !DoSendEvent(event
) )
888 // the event handler vetoed the change
889 newSashPosition
= -1;
893 // it could have been changed by it
894 newSashPosition
= event
.GetSashPosition();
897 return newSashPosition
;
900 // Called when the sash is double-clicked. The default behaviour is to remove
901 // the sash if the minimum pane size is zero.
902 void wxSplitterWindow::OnDoubleClickSash(int x
, int y
)
904 wxCHECK_RET(m_windowTwo
, wxT("splitter: no window to remove"));
906 // new code should handle events instead of using the virtual functions
907 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_DOUBLECLICKED
, this);
908 event
.m_data
.pt
.x
= x
;
909 event
.m_data
.pt
.y
= y
;
910 if ( DoSendEvent(event
) )
912 if ( GetMinimumPaneSize() == 0 || m_permitUnsplitAlways
)
914 wxWindow
* win
= m_windowTwo
;
917 wxSplitterEvent
unsplitEvent(wxEVT_COMMAND_SPLITTER_UNSPLIT
, this);
918 unsplitEvent
.m_data
.win
= win
;
919 (void)DoSendEvent(unsplitEvent
);
923 //else: blocked by user
926 void wxSplitterWindow::OnUnsplit(wxWindow
*winRemoved
)
928 // call this before calling the event handler which may delete the window
929 winRemoved
->Show(false);
932 #if defined( __WXMSW__ ) || defined( __WXMAC__)
934 // this is currently called (and needed) under MSW only...
935 void wxSplitterWindow::OnSetCursor(wxSetCursorEvent
& event
)
937 // if we don't do it, the resizing cursor might be set for child window:
938 // and like this we explicitly say that our cursor should not be used for
939 // children windows which overlap us
941 if ( SashHitTest(event
.GetX(), event
.GetY(), 0) )
943 // default processing is ok
946 //else: do nothing, in particular, don't call Skip()
949 #endif // wxMSW || wxMac
951 #endif // wxUSE_SPLITTER