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;
99 // don't erase the splitter background, it's pointless as we overwrite it
101 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
106 void wxSplitterWindow::Init()
108 m_container
.SetContainerWindow(this);
110 m_splitMode
= wxSPLIT_VERTICAL
;
111 m_permitUnsplitAlways
= true;
112 m_windowOne
= (wxWindow
*) NULL
;
113 m_windowTwo
= (wxWindow
*) NULL
;
114 m_dragMode
= wxSPLIT_DRAG_NONE
;
119 m_sashPosition
= m_requestedSashPosition
= 0;
120 m_checkRequestedSashPosition
= false;
121 m_minimumPaneSize
= 0;
122 m_sashCursorWE
= wxCursor(wxCURSOR_SIZEWE
);
123 m_sashCursorNS
= wxCursor(wxCURSOR_SIZENS
);
124 m_sashTrackerPen
= new wxPen(*wxBLACK
, 2, wxSOLID
);
126 m_needUpdating
= false;
130 wxSplitterWindow::~wxSplitterWindow()
132 delete m_sashTrackerPen
;
135 // ----------------------------------------------------------------------------
136 // entering/leaving sash
137 // ----------------------------------------------------------------------------
139 void wxSplitterWindow::RedrawIfHotSensitive(bool isHot
)
141 if ( wxRendererNative::Get().GetSplitterParams(this).isHotSensitive
)
148 //else: we don't change our appearance, don't redraw to avoid flicker
151 void wxSplitterWindow::OnEnterSash()
155 RedrawIfHotSensitive(true);
158 void wxSplitterWindow::OnLeaveSash()
160 SetCursor(*wxSTANDARD_CURSOR
);
162 RedrawIfHotSensitive(false);
165 void wxSplitterWindow::SetResizeCursor()
167 SetCursor(m_splitMode
== wxSPLIT_VERTICAL
? m_sashCursorWE
171 // ----------------------------------------------------------------------------
172 // other event handlers
173 // ----------------------------------------------------------------------------
175 void wxSplitterWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
182 void wxSplitterWindow::OnInternalIdle()
184 wxWindow::OnInternalIdle();
186 // if this is the first idle time after a sash position has potentially
187 // been set, allow SizeWindows to check for a requested size.
188 if (!m_checkRequestedSashPosition
)
190 m_checkRequestedSashPosition
= true;
192 return; // it won't needUpdating in this case
199 void wxSplitterWindow::OnMouseEvent(wxMouseEvent
& event
)
201 int x
= (int)event
.GetX(),
202 y
= (int)event
.GetY();
204 if (GetWindowStyle() & wxSP_NOSASH
)
207 // with wxSP_LIVE_UPDATE style the splitter windows are always resized
208 // following the mouse movement while it drags the sash, without it we only
209 // draw the sash at the new position but only resize the windows when the
210 // dragging is finished
211 #if defined( __WXMAC__ ) && TARGET_API_MAC_OSX == 1
214 bool isLive
= (GetWindowStyleFlag() & wxSP_LIVE_UPDATE
) != 0;
216 if (event
.LeftDown())
218 if ( SashHitTest(x
, y
) )
220 // Start the drag now
221 m_dragMode
= wxSPLIT_DRAG_DRAGGING
;
223 // Capture mouse and set the cursor
229 // remember the initial sash position and draw the initial
231 m_sashPositionCurrent
= m_sashPosition
;
233 DrawSashTracker(x
, y
);
243 else if (event
.LeftUp() && m_dragMode
== wxSPLIT_DRAG_DRAGGING
)
245 // We can stop dragging now and see what we've got.
246 m_dragMode
= wxSPLIT_DRAG_NONE
;
248 // Release mouse and unset the cursor
250 SetCursor(* wxSTANDARD_CURSOR
);
252 // exit if unsplit after doubleclick
261 DrawSashTracker(m_oldX
, m_oldY
);
264 // the position of the click doesn't exactly correspond to
265 // m_sashPosition, rather it changes it by the distance by which the
267 int diff
= m_splitMode
== wxSPLIT_VERTICAL
? x
- m_oldX
: y
- m_oldY
;
269 int posSashOld
= isLive
? m_sashPosition
: m_sashPositionCurrent
;
270 int posSashNew
= OnSashPositionChanging(posSashOld
+ diff
);
271 if ( posSashNew
== -1 )
273 // change not allowed
277 if ( m_permitUnsplitAlways
|| m_minimumPaneSize
== 0 )
279 // Deal with possible unsplit scenarios
280 if ( posSashNew
== 0 )
282 // We remove the first window from the view
283 wxWindow
*removedWindow
= m_windowOne
;
284 m_windowOne
= 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);
292 else if ( posSashNew
== GetWindowSize() )
294 // We remove the second window from the view
295 wxWindow
*removedWindow
= m_windowTwo
;
296 m_windowTwo
= (wxWindow
*) NULL
;
297 OnUnsplit(removedWindow
);
298 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_UNSPLIT
, this);
299 event
.m_data
.win
= removedWindow
;
300 (void)DoSendEvent(event
);
301 SetSashPositionAndNotify(0);
305 SetSashPositionAndNotify(posSashNew
);
310 SetSashPositionAndNotify(posSashNew
);
314 } // left up && dragging
315 else if ((event
.Moving() || event
.Leaving() || event
.Entering()) && (m_dragMode
== wxSPLIT_DRAG_NONE
))
317 if ( event
.Leaving() || !SashHitTest(x
, y
) )
322 else if (event
.Dragging() && (m_dragMode
== wxSPLIT_DRAG_DRAGGING
))
324 int diff
= m_splitMode
== wxSPLIT_VERTICAL
? x
- m_oldX
: y
- m_oldY
;
327 // nothing to do, mouse didn't really move far enough
331 int posSashOld
= isLive
? m_sashPosition
: m_sashPositionCurrent
;
332 int posSashNew
= OnSashPositionChanging(posSashOld
+ diff
);
333 if ( posSashNew
== -1 )
335 // change not allowed
339 if ( posSashNew
== m_sashPosition
)
345 DrawSashTracker(m_oldX
, m_oldY
);
348 if (m_splitMode
== wxSPLIT_VERTICAL
)
353 // Remember old positions
358 // As we captured the mouse, we may get the mouse events from outside
359 // our window - for example, negative values in x, y. This has a weird
360 // consequence under MSW where we use unsigned values sometimes and
361 // signed ones other times: the coordinates turn as big positive
362 // numbers and so the sash is drawn on the *right* side of the window
363 // instead of the left (or bottom instead of top). Correct this.
364 if ( (short)m_oldX
< 0 )
366 if ( (short)m_oldY
< 0 )
373 m_sashPositionCurrent
= posSashNew
;
375 DrawSashTracker(m_oldX
, m_oldY
);
379 SetSashPositionAndNotify(posSashNew
);
380 m_needUpdating
= true;
383 else if ( event
.LeftDClick() && m_windowTwo
)
385 OnDoubleClickSash(x
, y
);
389 void wxSplitterWindow::OnSize(wxSizeEvent
& event
)
391 // only process this message if we're not iconized - otherwise iconizing
392 // and restoring a window containing the splitter has a funny side effect
393 // of changing the splitter position!
394 wxWindow
*parent
= wxGetTopLevelParent(this);
397 wxTopLevelWindow
*winTop
= wxDynamicCast(parent
, wxTopLevelWindow
);
400 iconized
= winTop
->IsIconized();
404 wxFAIL_MSG(wxT("should have a top level parent!"));
419 GetClientSize(&w
, &h
);
421 int size
= m_splitMode
== wxSPLIT_VERTICAL
? w
: h
;
422 if ( m_sashPosition
>= size
- 5 )
423 SetSashPositionAndNotify(wxMax(10, size
- 40));
429 bool wxSplitterWindow::SashHitTest(int x
, int y
, int tolerance
)
431 if ( m_windowTwo
== NULL
|| m_sashPosition
== 0)
432 return false; // No sash
434 int z
= m_splitMode
== wxSPLIT_VERTICAL
? x
: y
;
435 int hitMin
= m_sashPosition
- tolerance
;
436 int hitMax
= m_sashPosition
+ GetSashSize() + tolerance
;
438 return z
>= hitMin
&& z
<= hitMax
;
441 int wxSplitterWindow::GetSashSize() const
443 return wxRendererNative::Get().GetSplitterParams(this).widthSash
;
446 int wxSplitterWindow::GetBorderSize() const
448 return wxRendererNative::Get().GetSplitterParams(this).border
;
452 void wxSplitterWindow::DrawSash(wxDC
& dc
)
454 if (HasFlag(wxSP_3DBORDER
))
455 wxRendererNative::Get().DrawSplitterBorder
462 // don't draw sash if we're not split
463 if ( m_sashPosition
== 0 || !m_windowTwo
)
466 // nor if we're configured to not show it
467 if ( HasFlag(wxSP_NOSASH
) )
470 wxRendererNative::Get().DrawSplitterSash
476 m_splitMode
== wxSPLIT_VERTICAL
? wxVERTICAL
478 m_isHot
? wxCONTROL_CURRENT
: 0
482 // Draw the sash tracker (for whilst moving the sash)
483 void wxSplitterWindow::DrawSashTracker(int x
, int y
)
486 GetClientSize(&w
, &h
);
492 if ( m_splitMode
== wxSPLIT_VERTICAL
)
523 ClientToScreen(&x1
, &y1
);
524 ClientToScreen(&x2
, &y2
);
526 screenDC
.SetLogicalFunction(wxINVERT
);
527 screenDC
.SetPen(*m_sashTrackerPen
);
528 screenDC
.SetBrush(*wxTRANSPARENT_BRUSH
);
530 screenDC
.DrawLine(x1
, y1
, x2
, y2
);
532 screenDC
.SetLogicalFunction(wxCOPY
);
535 int wxSplitterWindow::GetWindowSize() const
537 wxSize size
= GetClientSize();
539 return m_splitMode
== wxSPLIT_VERTICAL
? size
.x
: size
.y
;
542 int wxSplitterWindow::AdjustSashPosition(int sashPos
) const
544 int window_size
= GetWindowSize();
551 // the window shouldn't be smaller than its own minimal size nor
552 // smaller than the minimual pane size specified for this splitter
553 int minSize
= m_splitMode
== wxSPLIT_VERTICAL
? win
->GetMinWidth()
554 : win
->GetMinHeight();
556 if ( minSize
== -1 || m_minimumPaneSize
> minSize
)
557 minSize
= m_minimumPaneSize
;
559 minSize
+= GetBorderSize();
561 if ( sashPos
< minSize
)
568 int minSize
= m_splitMode
== wxSPLIT_VERTICAL
? win
->GetMinWidth()
569 : win
->GetMinHeight();
571 if ( minSize
== -1 || m_minimumPaneSize
> minSize
)
572 minSize
= m_minimumPaneSize
;
574 int maxSize
= window_size
- minSize
- GetBorderSize() - GetSashSize();
575 if ( sashPos
> maxSize
)
582 bool wxSplitterWindow::DoSetSashPosition(int sashPos
)
584 int newSashPosition
= AdjustSashPosition(sashPos
);
586 if ( newSashPosition
== m_sashPosition
)
589 m_sashPosition
= newSashPosition
;
594 void wxSplitterWindow::SetSashPositionAndNotify(int sashPos
)
596 // we must reset the request here, otherwise the sash would be stuck at
597 // old position if the user attempted to move the sash after invalid
598 // (e.g. smaller than minsize) sash position was requested using
599 // SetSashPosition():
600 m_requestedSashPosition
= INT_MAX
;
602 if ( DoSetSashPosition(sashPos
) )
604 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED
, this);
605 event
.m_data
.pos
= m_sashPosition
;
607 (void)DoSendEvent(event
);
611 // Position and size subwindows.
612 // Note that the border size applies to each subwindow, not
613 // including the edges next to the sash.
614 void wxSplitterWindow::SizeWindows()
616 // check if we have delayed setting the real sash position
617 if ( m_checkRequestedSashPosition
&& m_requestedSashPosition
!= INT_MAX
)
619 int newSashPosition
= ConvertSashPosition(m_requestedSashPosition
);
620 if ( newSashPosition
!= m_sashPosition
)
622 DoSetSashPosition(newSashPosition
);
625 if ( newSashPosition
<= m_sashPosition
626 && newSashPosition
>= m_sashPosition
- GetBorderSize() )
628 // don't update it any more
629 m_requestedSashPosition
= INT_MAX
;
634 GetClientSize(&w
, &h
);
636 if ( GetWindow1() && !GetWindow2() )
638 GetWindow1()->SetSize(GetBorderSize(), GetBorderSize(),
639 w
- 2*GetBorderSize(), h
- 2*GetBorderSize());
641 else if ( GetWindow1() && GetWindow2() )
643 const int border
= GetBorderSize(),
644 sash
= GetSashSize();
646 int size1
= GetSashPosition() - border
,
647 size2
= GetSashPosition() + sash
;
649 int x2
, y2
, w1
, h1
, w2
, h2
;
650 if ( GetSplitMode() == wxSPLIT_VERTICAL
)
653 w2
= w
- 2*border
- sash
- w1
;
659 else // horz splitter
664 h2
= h
- 2*border
- sash
- h1
;
669 GetWindow1()->SetSize(border
, border
, w1
, h1
);
670 GetWindow2()->SetSize(x2
, y2
, w2
, h2
);
676 SetNeedUpdating(false);
679 // Set pane for unsplit window
680 void wxSplitterWindow::Initialize(wxWindow
*window
)
682 wxASSERT_MSG( (!window
|| (window
&& window
->GetParent() == this)),
683 _T("windows in the splitter should have it as parent!") );
685 m_windowOne
= window
;
686 m_windowTwo
= (wxWindow
*) NULL
;
687 DoSetSashPosition(0);
690 // Associates the given window with window 2, drawing the appropriate sash
691 // and changing the split mode.
692 // Does nothing and returns false if the window is already split.
693 bool wxSplitterWindow::DoSplit(wxSplitMode mode
,
694 wxWindow
*window1
, wxWindow
*window2
,
700 wxCHECK_MSG( window1
&& window2
, false,
701 _T("can not split with NULL window(s)") );
703 wxCHECK_MSG( window1
->GetParent() == this && window2
->GetParent() == this, false,
704 _T("windows in the splitter should have it as parent!") );
707 m_windowOne
= window1
;
708 m_windowTwo
= window2
;
710 // remember the sash position we want to set for later if we can't set it
711 // right now (e.g. because the window is too small)
712 m_requestedSashPosition
= sashPosition
;
713 m_checkRequestedSashPosition
= false;
715 DoSetSashPosition(ConvertSashPosition(sashPosition
));
722 int wxSplitterWindow::ConvertSashPosition(int sashPosition
) const
724 if ( sashPosition
> 0 )
728 else if ( sashPosition
< 0 )
730 // It's negative so adding is subtracting
731 return GetWindowSize() + sashPosition
;
733 else // sashPosition == 0
735 // default, put it in the centre
736 return GetWindowSize() / 2;
740 // Remove the specified (or second) window from the view
741 // Doesn't actually delete the window.
742 bool wxSplitterWindow::Unsplit(wxWindow
*toRemove
)
748 if ( toRemove
== NULL
|| toRemove
== m_windowTwo
)
751 m_windowTwo
= (wxWindow
*) NULL
;
753 else if ( toRemove
== m_windowOne
)
756 m_windowOne
= m_windowTwo
;
757 m_windowTwo
= (wxWindow
*) NULL
;
761 wxFAIL_MSG(wxT("splitter: attempt to remove a non-existent window"));
767 DoSetSashPosition(0);
773 // Replace a window with another one
774 bool wxSplitterWindow::ReplaceWindow(wxWindow
*winOld
, wxWindow
*winNew
)
776 wxCHECK_MSG( winOld
, false, wxT("use one of Split() functions instead") );
777 wxCHECK_MSG( winNew
, false, wxT("use Unsplit() functions instead") );
779 if ( winOld
== m_windowTwo
)
781 m_windowTwo
= winNew
;
783 else if ( winOld
== m_windowOne
)
785 m_windowOne
= winNew
;
789 wxFAIL_MSG(wxT("splitter: attempt to replace a non-existent window"));
799 void wxSplitterWindow::SetMinimumPaneSize(int min
)
801 m_minimumPaneSize
= min
;
802 int pos
= m_requestedSashPosition
!= INT_MAX
? m_requestedSashPosition
: m_sashPosition
;
803 SetSashPosition(pos
); // re-check limits
806 void wxSplitterWindow::SetSashPosition(int position
, bool redraw
)
808 // remember the sash position we want to set for later if we can't set it
809 // right now (e.g. because the window is too small)
810 m_requestedSashPosition
= position
;
811 m_checkRequestedSashPosition
= false;
813 DoSetSashPosition(ConvertSashPosition(position
));
821 // Make sure the child window sizes are updated. This is useful
822 // for reducing flicker by updating the sizes before a
823 // window is shown, if you know the overall size is correct.
824 void wxSplitterWindow::UpdateSize()
826 m_checkRequestedSashPosition
= true;
828 m_checkRequestedSashPosition
= false;
831 bool wxSplitterWindow::DoSendEvent(wxSplitterEvent
& event
)
833 return !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed();
836 // ---------------------------------------------------------------------------
837 // wxSplitterWindow virtual functions: they now just generate the events
838 // ---------------------------------------------------------------------------
840 bool wxSplitterWindow::OnSashPositionChange(int WXUNUSED(newSashPosition
))
842 // always allow by default
846 int wxSplitterWindow::OnSashPositionChanging(int newSashPosition
)
848 // If within UNSPLIT_THRESHOLD from edge, set to edge to cause closure.
849 const int UNSPLIT_THRESHOLD
= 4;
851 // first of all, check if OnSashPositionChange() doesn't forbid this change
852 if ( !OnSashPositionChange(newSashPosition
) )
858 // Obtain relevant window dimension for bottom / right threshold check
859 int window_size
= GetWindowSize();
861 bool unsplit_scenario
= false;
862 if ( m_permitUnsplitAlways
|| m_minimumPaneSize
== 0 )
864 // Do edge detection if unsplit premitted
865 if ( newSashPosition
<= UNSPLIT_THRESHOLD
)
867 // threshold top / left check
869 unsplit_scenario
= true;
871 if ( newSashPosition
>= window_size
- UNSPLIT_THRESHOLD
)
873 // threshold bottom/right check
874 newSashPosition
= window_size
;
875 unsplit_scenario
= true;
879 if ( !unsplit_scenario
)
881 // If resultant pane would be too small, enlarge it
882 newSashPosition
= AdjustSashPosition(newSashPosition
);
885 // If the result is out of bounds it means minimum size is too big,
886 // so split window in half as best compromise.
887 if ( newSashPosition
< 0 || newSashPosition
> window_size
)
888 newSashPosition
= window_size
/ 2;
890 // now let the event handler have it
892 // FIXME: shouldn't we do it before the adjustments above so as to ensure
893 // that the sash position is always reasonable?
894 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING
, this);
895 event
.m_data
.pos
= newSashPosition
;
897 if ( !DoSendEvent(event
) )
899 // the event handler vetoed the change
900 newSashPosition
= -1;
904 // it could have been changed by it
905 newSashPosition
= event
.GetSashPosition();
908 return newSashPosition
;
911 // Called when the sash is double-clicked. The default behaviour is to remove
912 // the sash if the minimum pane size is zero.
913 void wxSplitterWindow::OnDoubleClickSash(int x
, int y
)
915 wxCHECK_RET(m_windowTwo
, wxT("splitter: no window to remove"));
917 // new code should handle events instead of using the virtual functions
918 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_DOUBLECLICKED
, this);
919 event
.m_data
.pt
.x
= x
;
920 event
.m_data
.pt
.y
= y
;
921 if ( DoSendEvent(event
) )
923 if ( GetMinimumPaneSize() == 0 || m_permitUnsplitAlways
)
925 wxWindow
* win
= m_windowTwo
;
928 wxSplitterEvent
unsplitEvent(wxEVT_COMMAND_SPLITTER_UNSPLIT
, this);
929 unsplitEvent
.m_data
.win
= win
;
930 (void)DoSendEvent(unsplitEvent
);
934 //else: blocked by user
937 void wxSplitterWindow::OnUnsplit(wxWindow
*winRemoved
)
939 // call this before calling the event handler which may delete the window
940 winRemoved
->Show(false);
943 #if defined( __WXMSW__ ) || defined( __WXMAC__)
945 // this is currently called (and needed) under MSW only...
946 void wxSplitterWindow::OnSetCursor(wxSetCursorEvent
& event
)
948 // if we don't do it, the resizing cursor might be set for child window:
949 // and like this we explicitly say that our cursor should not be used for
950 // children windows which overlap us
952 if ( SashHitTest(event
.GetX(), event
.GetY(), 0) )
954 // default processing is ok
957 //else: do nothing, in particular, don't call Skip()
960 #endif // wxMSW || wxMac
962 #endif // wxUSE_SPLITTER