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 #if defined( __WXMAC__ ) && TARGET_API_MAC_OSX == 1
206 bool isLive
= (GetWindowStyleFlag() & wxSP_LIVE_UPDATE
) != 0;
208 if (event
.LeftDown())
210 if ( SashHitTest(x
, y
) )
212 // Start the drag now
213 m_dragMode
= wxSPLIT_DRAG_DRAGGING
;
215 // Capture mouse and set the cursor
221 // remember the initial sash position and draw the initial
223 m_sashPositionCurrent
= m_sashPosition
;
225 DrawSashTracker(x
, y
);
235 else if (event
.LeftUp() && m_dragMode
== wxSPLIT_DRAG_DRAGGING
)
237 // We can stop dragging now and see what we've got.
238 m_dragMode
= wxSPLIT_DRAG_NONE
;
240 // Release mouse and unset the cursor
242 SetCursor(* wxSTANDARD_CURSOR
);
244 // exit if unsplit after doubleclick
253 DrawSashTracker(m_oldX
, m_oldY
);
256 // the position of the click doesn't exactly correspond to
257 // m_sashPosition, rather it changes it by the distance by which the
259 int diff
= m_splitMode
== wxSPLIT_VERTICAL
? x
- m_oldX
: y
- m_oldY
;
261 int posSashOld
= isLive
? m_sashPosition
: m_sashPositionCurrent
;
262 int posSashNew
= OnSashPositionChanging(posSashOld
+ diff
);
263 if ( posSashNew
== -1 )
265 // change not allowed
269 if ( m_permitUnsplitAlways
|| m_minimumPaneSize
== 0 )
271 // Deal with possible unsplit scenarios
272 if ( posSashNew
== 0 )
274 // We remove the first window from the view
275 wxWindow
*removedWindow
= m_windowOne
;
276 m_windowOne
= m_windowTwo
;
277 m_windowTwo
= (wxWindow
*) NULL
;
278 OnUnsplit(removedWindow
);
279 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_UNSPLIT
, this);
280 event
.m_data
.win
= removedWindow
;
281 (void)DoSendEvent(event
);
282 SetSashPositionAndNotify(0);
284 else if ( posSashNew
== GetWindowSize() )
286 // We remove the second window from the view
287 wxWindow
*removedWindow
= m_windowTwo
;
288 m_windowTwo
= (wxWindow
*) NULL
;
289 OnUnsplit(removedWindow
);
290 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_UNSPLIT
, this);
291 event
.m_data
.win
= removedWindow
;
292 (void)DoSendEvent(event
);
293 SetSashPositionAndNotify(0);
297 SetSashPositionAndNotify(posSashNew
);
302 SetSashPositionAndNotify(posSashNew
);
306 } // left up && dragging
307 else if ((event
.Moving() || event
.Leaving() || event
.Entering()) && (m_dragMode
== wxSPLIT_DRAG_NONE
))
309 if ( event
.Leaving() || !SashHitTest(x
, y
) )
314 else if (event
.Dragging() && (m_dragMode
== wxSPLIT_DRAG_DRAGGING
))
316 int diff
= m_splitMode
== wxSPLIT_VERTICAL
? x
- m_oldX
: y
- m_oldY
;
319 // nothing to do, mouse didn't really move far enough
323 int posSashOld
= isLive
? m_sashPosition
: m_sashPositionCurrent
;
324 int posSashNew
= OnSashPositionChanging(posSashOld
+ diff
);
325 if ( posSashNew
== -1 )
327 // change not allowed
331 if ( posSashNew
== m_sashPosition
)
337 DrawSashTracker(m_oldX
, m_oldY
);
340 if (m_splitMode
== wxSPLIT_VERTICAL
)
345 // Remember old positions
350 // As we captured the mouse, we may get the mouse events from outside
351 // our window - for example, negative values in x, y. This has a weird
352 // consequence under MSW where we use unsigned values sometimes and
353 // signed ones other times: the coordinates turn as big positive
354 // numbers and so the sash is drawn on the *right* side of the window
355 // instead of the left (or bottom instead of top). Correct this.
356 if ( (short)m_oldX
< 0 )
358 if ( (short)m_oldY
< 0 )
365 m_sashPositionCurrent
= posSashNew
;
367 DrawSashTracker(m_oldX
, m_oldY
);
371 SetSashPositionAndNotify(posSashNew
);
372 m_needUpdating
= true;
375 else if ( event
.LeftDClick() && m_windowTwo
)
377 OnDoubleClickSash(x
, y
);
381 void wxSplitterWindow::OnSize(wxSizeEvent
& event
)
383 // only process this message if we're not iconized - otherwise iconizing
384 // and restoring a window containing the splitter has a funny side effect
385 // of changing the splitter position!
386 wxWindow
*parent
= wxGetTopLevelParent(this);
389 wxTopLevelWindow
*winTop
= wxDynamicCast(parent
, wxTopLevelWindow
);
392 iconized
= winTop
->IsIconized();
396 wxFAIL_MSG(wxT("should have a top level parent!"));
411 GetClientSize(&w
, &h
);
413 int size
= m_splitMode
== wxSPLIT_VERTICAL
? w
: h
;
414 if ( m_sashPosition
>= size
- 5 )
415 SetSashPositionAndNotify(wxMax(10, size
- 40));
421 bool wxSplitterWindow::SashHitTest(int x
, int y
, int tolerance
)
423 if ( m_windowTwo
== NULL
|| m_sashPosition
== 0)
424 return false; // No sash
426 int z
= m_splitMode
== wxSPLIT_VERTICAL
? x
: y
;
427 int hitMin
= m_sashPosition
- tolerance
;
428 int hitMax
= m_sashPosition
+ GetSashSize() + tolerance
;
430 return z
>= hitMin
&& z
<= hitMax
;
433 int wxSplitterWindow::GetSashSize() const
435 return wxRendererNative::Get().GetSplitterParams(this).widthSash
;
438 int wxSplitterWindow::GetBorderSize() const
440 return wxRendererNative::Get().GetSplitterParams(this).border
;
444 void wxSplitterWindow::DrawSash(wxDC
& dc
)
446 if (HasFlag(wxSP_3DBORDER
))
447 wxRendererNative::Get().DrawSplitterBorder
454 // don't draw sash if we're not split
455 if ( m_sashPosition
== 0 || !m_windowTwo
)
458 // nor if we're configured to not show it
459 if ( HasFlag(wxSP_NOSASH
) )
462 wxRendererNative::Get().DrawSplitterSash
468 m_splitMode
== wxSPLIT_VERTICAL
? wxVERTICAL
470 m_isHot
? wxCONTROL_CURRENT
: 0
474 // Draw the sash tracker (for whilst moving the sash)
475 void wxSplitterWindow::DrawSashTracker(int x
, int y
)
478 GetClientSize(&w
, &h
);
484 if ( m_splitMode
== wxSPLIT_VERTICAL
)
515 ClientToScreen(&x1
, &y1
);
516 ClientToScreen(&x2
, &y2
);
518 screenDC
.SetLogicalFunction(wxINVERT
);
519 screenDC
.SetPen(*m_sashTrackerPen
);
520 screenDC
.SetBrush(*wxTRANSPARENT_BRUSH
);
522 screenDC
.DrawLine(x1
, y1
, x2
, y2
);
524 screenDC
.SetLogicalFunction(wxCOPY
);
527 int wxSplitterWindow::GetWindowSize() const
529 wxSize size
= GetClientSize();
531 return m_splitMode
== wxSPLIT_VERTICAL
? size
.x
: size
.y
;
534 int wxSplitterWindow::AdjustSashPosition(int sashPos
) const
536 int window_size
= GetWindowSize();
543 // the window shouldn't be smaller than its own minimal size nor
544 // smaller than the minimual pane size specified for this splitter
545 int minSize
= m_splitMode
== wxSPLIT_VERTICAL
? win
->GetMinWidth()
546 : win
->GetMinHeight();
548 if ( minSize
== -1 || m_minimumPaneSize
> minSize
)
549 minSize
= m_minimumPaneSize
;
551 minSize
+= GetBorderSize();
553 if ( sashPos
< minSize
)
560 int minSize
= m_splitMode
== wxSPLIT_VERTICAL
? win
->GetMinWidth()
561 : win
->GetMinHeight();
563 if ( minSize
== -1 || m_minimumPaneSize
> minSize
)
564 minSize
= m_minimumPaneSize
;
566 int maxSize
= window_size
- minSize
- GetBorderSize();
567 if ( sashPos
> maxSize
)
574 bool wxSplitterWindow::DoSetSashPosition(int sashPos
)
576 int newSashPosition
= AdjustSashPosition(sashPos
);
578 if ( newSashPosition
== m_sashPosition
)
581 m_sashPosition
= newSashPosition
;
586 void wxSplitterWindow::SetSashPositionAndNotify(int sashPos
)
588 // we must reset the request here, otherwise the sash would be stuck at
589 // old position if the user attempted to move the sash after invalid
590 // (e.g. smaller than minsize) sash position was requested using
591 // SetSashPosition():
592 m_requestedSashPosition
= INT_MAX
;
594 if ( DoSetSashPosition(sashPos
) )
596 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED
, this);
597 event
.m_data
.pos
= m_sashPosition
;
599 (void)DoSendEvent(event
);
603 // Position and size subwindows.
604 // Note that the border size applies to each subwindow, not
605 // including the edges next to the sash.
606 void wxSplitterWindow::SizeWindows()
608 // check if we have delayed setting the real sash position
609 if ( m_checkRequestedSashPosition
&& m_requestedSashPosition
!= INT_MAX
)
611 int newSashPosition
= ConvertSashPosition(m_requestedSashPosition
);
612 if ( newSashPosition
!= m_sashPosition
)
614 DoSetSashPosition(newSashPosition
);
617 if ( newSashPosition
<= m_sashPosition
618 && newSashPosition
>= m_sashPosition
- GetBorderSize() )
620 // don't update it any more
621 m_requestedSashPosition
= INT_MAX
;
626 GetClientSize(&w
, &h
);
628 if ( GetWindow1() && !GetWindow2() )
630 GetWindow1()->SetSize(GetBorderSize(), GetBorderSize(),
631 w
- 2*GetBorderSize(), h
- 2*GetBorderSize());
633 else if ( GetWindow1() && GetWindow2() )
635 const int border
= GetBorderSize(),
636 sash
= GetSashSize();
638 int size1
= GetSashPosition() - border
,
639 size2
= GetSashPosition() + sash
;
641 int x2
, y2
, w1
, h1
, w2
, h2
;
642 if ( GetSplitMode() == wxSPLIT_VERTICAL
)
645 w2
= w
- 2*border
- sash
- w1
;
651 else // horz splitter
656 h2
= h
- 2*border
- sash
- h1
;
661 GetWindow1()->SetSize(border
, border
, w1
, h1
);
662 GetWindow2()->SetSize(x2
, y2
, w2
, h2
);
668 SetNeedUpdating(false);
671 // Set pane for unsplit window
672 void wxSplitterWindow::Initialize(wxWindow
*window
)
674 wxASSERT_MSG( (!window
|| (window
&& window
->GetParent() == this)),
675 _T("windows in the splitter should have it as parent!") );
677 m_windowOne
= window
;
678 m_windowTwo
= (wxWindow
*) NULL
;
679 DoSetSashPosition(0);
682 // Associates the given window with window 2, drawing the appropriate sash
683 // and changing the split mode.
684 // Does nothing and returns false if the window is already split.
685 bool wxSplitterWindow::DoSplit(wxSplitMode mode
,
686 wxWindow
*window1
, wxWindow
*window2
,
692 wxCHECK_MSG( window1
&& window2
, false,
693 _T("can not split with NULL window(s)") );
695 wxCHECK_MSG( window1
->GetParent() == this && window2
->GetParent() == this, false,
696 _T("windows in the splitter should have it as parent!") );
699 m_windowOne
= window1
;
700 m_windowTwo
= window2
;
702 // remember the sash position we want to set for later if we can't set it
703 // right now (e.g. because the window is too small)
704 m_requestedSashPosition
= sashPosition
;
705 m_checkRequestedSashPosition
= false;
707 DoSetSashPosition(ConvertSashPosition(sashPosition
));
714 int wxSplitterWindow::ConvertSashPosition(int sashPosition
) const
716 if ( sashPosition
> 0 )
720 else if ( sashPosition
< 0 )
722 // It's negative so adding is subtracting
723 return GetWindowSize() + sashPosition
;
725 else // sashPosition == 0
727 // default, put it in the centre
728 return GetWindowSize() / 2;
732 // Remove the specified (or second) window from the view
733 // Doesn't actually delete the window.
734 bool wxSplitterWindow::Unsplit(wxWindow
*toRemove
)
740 if ( toRemove
== NULL
|| toRemove
== m_windowTwo
)
743 m_windowTwo
= (wxWindow
*) NULL
;
745 else if ( toRemove
== m_windowOne
)
748 m_windowOne
= m_windowTwo
;
749 m_windowTwo
= (wxWindow
*) NULL
;
753 wxFAIL_MSG(wxT("splitter: attempt to remove a non-existent window"));
759 DoSetSashPosition(0);
765 // Replace a window with another one
766 bool wxSplitterWindow::ReplaceWindow(wxWindow
*winOld
, wxWindow
*winNew
)
768 wxCHECK_MSG( winOld
, false, wxT("use one of Split() functions instead") );
769 wxCHECK_MSG( winNew
, false, wxT("use Unsplit() functions instead") );
771 if ( winOld
== m_windowTwo
)
773 m_windowTwo
= winNew
;
775 else if ( winOld
== m_windowOne
)
777 m_windowOne
= winNew
;
781 wxFAIL_MSG(wxT("splitter: attempt to replace a non-existent window"));
791 void wxSplitterWindow::SetMinimumPaneSize(int min
)
793 m_minimumPaneSize
= min
;
794 int pos
= m_requestedSashPosition
!= INT_MAX
? m_requestedSashPosition
: m_sashPosition
;
795 SetSashPosition(pos
); // re-check limits
798 void wxSplitterWindow::SetSashPosition(int position
, bool redraw
)
800 // remember the sash position we want to set for later if we can't set it
801 // right now (e.g. because the window is too small)
802 m_requestedSashPosition
= position
;
803 m_checkRequestedSashPosition
= false;
805 DoSetSashPosition(ConvertSashPosition(position
));
813 // Make sure the child window sizes are updated. This is useful
814 // for reducing flicker by updating the sizes before a
815 // window is shown, if you know the overall size is correct.
816 void wxSplitterWindow::UpdateSize()
818 m_checkRequestedSashPosition
= true;
820 m_checkRequestedSashPosition
= false;
823 bool wxSplitterWindow::DoSendEvent(wxSplitterEvent
& event
)
825 return !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed();
828 // ---------------------------------------------------------------------------
829 // wxSplitterWindow virtual functions: they now just generate the events
830 // ---------------------------------------------------------------------------
832 bool wxSplitterWindow::OnSashPositionChange(int WXUNUSED(newSashPosition
))
834 // always allow by default
838 int wxSplitterWindow::OnSashPositionChanging(int newSashPosition
)
840 // If within UNSPLIT_THRESHOLD from edge, set to edge to cause closure.
841 const int UNSPLIT_THRESHOLD
= 4;
843 // first of all, check if OnSashPositionChange() doesn't forbid this change
844 if ( !OnSashPositionChange(newSashPosition
) )
850 // Obtain relevant window dimension for bottom / right threshold check
851 int window_size
= GetWindowSize();
853 bool unsplit_scenario
= false;
854 if ( m_permitUnsplitAlways
|| m_minimumPaneSize
== 0 )
856 // Do edge detection if unsplit premitted
857 if ( newSashPosition
<= UNSPLIT_THRESHOLD
)
859 // threshold top / left check
861 unsplit_scenario
= true;
863 if ( newSashPosition
>= window_size
- UNSPLIT_THRESHOLD
)
865 // threshold bottom/right check
866 newSashPosition
= window_size
;
867 unsplit_scenario
= true;
871 if ( !unsplit_scenario
)
873 // If resultant pane would be too small, enlarge it
874 newSashPosition
= AdjustSashPosition(newSashPosition
);
877 // If the result is out of bounds it means minimum size is too big,
878 // so split window in half as best compromise.
879 if ( newSashPosition
< 0 || newSashPosition
> window_size
)
880 newSashPosition
= window_size
/ 2;
882 // now let the event handler have it
884 // FIXME: shouldn't we do it before the adjustments above so as to ensure
885 // that the sash position is always reasonable?
886 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING
, this);
887 event
.m_data
.pos
= newSashPosition
;
889 if ( !DoSendEvent(event
) )
891 // the event handler vetoed the change
892 newSashPosition
= -1;
896 // it could have been changed by it
897 newSashPosition
= event
.GetSashPosition();
900 return newSashPosition
;
903 // Called when the sash is double-clicked. The default behaviour is to remove
904 // the sash if the minimum pane size is zero.
905 void wxSplitterWindow::OnDoubleClickSash(int x
, int y
)
907 wxCHECK_RET(m_windowTwo
, wxT("splitter: no window to remove"));
909 // new code should handle events instead of using the virtual functions
910 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_DOUBLECLICKED
, this);
911 event
.m_data
.pt
.x
= x
;
912 event
.m_data
.pt
.y
= y
;
913 if ( DoSendEvent(event
) )
915 if ( GetMinimumPaneSize() == 0 || m_permitUnsplitAlways
)
917 wxWindow
* win
= m_windowTwo
;
920 wxSplitterEvent
unsplitEvent(wxEVT_COMMAND_SPLITTER_UNSPLIT
, this);
921 unsplitEvent
.m_data
.win
= win
;
922 (void)DoSendEvent(unsplitEvent
);
926 //else: blocked by user
929 void wxSplitterWindow::OnUnsplit(wxWindow
*winRemoved
)
931 // call this before calling the event handler which may delete the window
932 winRemoved
->Show(false);
935 #if defined( __WXMSW__ ) || defined( __WXMAC__)
937 // this is currently called (and needed) under MSW only...
938 void wxSplitterWindow::OnSetCursor(wxSetCursorEvent
& event
)
940 // if we don't do it, the resizing cursor might be set for child window:
941 // and like this we explicitly say that our cursor should not be used for
942 // children windows which overlap us
944 if ( SashHitTest(event
.GetX(), event
.GetY(), 0) )
946 // default processing is ok
949 //else: do nothing, in particular, don't call Skip()
952 #endif // wxMSW || wxMac
954 #endif // wxUSE_SPLITTER