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_minimumPaneSize
= 0;
113 m_sashCursorWE
= wxCursor(wxCURSOR_SIZEWE
);
114 m_sashCursorNS
= wxCursor(wxCURSOR_SIZENS
);
115 m_sashTrackerPen
= new wxPen(*wxBLACK
, 2, wxSOLID
);
117 m_needUpdating
= FALSE
;
121 wxSplitterWindow::~wxSplitterWindow()
123 delete m_sashTrackerPen
;
126 // ----------------------------------------------------------------------------
127 // entering/leaving sash
128 // ----------------------------------------------------------------------------
130 void wxSplitterWindow::RedrawIfHotSensitive(bool isHot
)
132 if ( wxRendererNative::Get().GetSplitterParams(this).isHotSensitive
)
139 //else: we don't change our appearance, don't redraw to avoid flicker
142 void wxSplitterWindow::OnEnterSash()
146 RedrawIfHotSensitive(true);
149 void wxSplitterWindow::OnLeaveSash()
151 SetCursor(*wxSTANDARD_CURSOR
);
153 RedrawIfHotSensitive(false);
156 void wxSplitterWindow::SetResizeCursor()
158 SetCursor(m_splitMode
== wxSPLIT_VERTICAL
? m_sashCursorWE
162 // ----------------------------------------------------------------------------
163 // other event handlers
164 // ----------------------------------------------------------------------------
166 void wxSplitterWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
173 void wxSplitterWindow::OnInternalIdle()
175 wxWindow::OnInternalIdle();
181 void wxSplitterWindow::OnMouseEvent(wxMouseEvent
& event
)
183 int x
= (int)event
.GetX(),
184 y
= (int)event
.GetY();
186 if (GetWindowStyle() & wxSP_NOSASH
)
189 // with wxSP_LIVE_UPDATE style the splitter windows are always resized
190 // following the mouse movement while it drags the sash, without it we only
191 // draw the sash at the new position but only resize the windows when the
192 // dragging is finished
193 bool isLive
= (GetWindowStyleFlag() & wxSP_LIVE_UPDATE
) != 0;
195 if (event
.LeftDown())
197 if ( SashHitTest(x
, y
) )
199 // Start the drag now
200 m_dragMode
= wxSPLIT_DRAG_DRAGGING
;
202 // Capture mouse and set the cursor
208 // remember the initial sash position and draw the initial
210 m_sashPositionCurrent
= m_sashPosition
;
212 DrawSashTracker(x
, y
);
222 else if (event
.LeftUp() && m_dragMode
== wxSPLIT_DRAG_DRAGGING
)
224 // We can stop dragging now and see what we've got.
225 m_dragMode
= wxSPLIT_DRAG_NONE
;
227 // Release mouse and unset the cursor
229 SetCursor(* wxSTANDARD_CURSOR
);
231 // exit if unsplit after doubleclick
240 DrawSashTracker(m_oldX
, m_oldY
);
243 // the position of the click doesn't exactly correspond to
244 // m_sashPosition, rather it changes it by the distance by which the
246 int diff
= m_splitMode
== wxSPLIT_VERTICAL
? x
- m_oldX
: y
- m_oldY
;
248 int posSashOld
= isLive
? m_sashPosition
: m_sashPositionCurrent
;
249 int posSashNew
= OnSashPositionChanging(posSashOld
+ diff
);
250 if ( posSashNew
== -1 )
252 // change not allowed
256 if ( m_permitUnsplitAlways
|| m_minimumPaneSize
== 0 )
258 // Deal with possible unsplit scenarios
259 if ( posSashNew
== 0 )
261 // We remove the first window from the view
262 wxWindow
*removedWindow
= m_windowOne
;
263 m_windowOne
= m_windowTwo
;
264 m_windowTwo
= (wxWindow
*) NULL
;
265 OnUnsplit(removedWindow
);
266 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_UNSPLIT
, this);
267 event
.m_data
.win
= removedWindow
;
268 (void)DoSendEvent(event
);
269 SetSashPositionAndNotify(0);
271 else if ( posSashNew
== GetWindowSize() )
273 // We remove the second window from the view
274 wxWindow
*removedWindow
= m_windowTwo
;
275 m_windowTwo
= (wxWindow
*) NULL
;
276 OnUnsplit(removedWindow
);
277 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_UNSPLIT
, this);
278 event
.m_data
.win
= removedWindow
;
279 (void)DoSendEvent(event
);
280 SetSashPositionAndNotify(0);
284 SetSashPositionAndNotify(posSashNew
);
289 SetSashPositionAndNotify(posSashNew
);
293 } // left up && dragging
294 else if ((event
.Moving() || event
.Leaving() || event
.Entering()) && (m_dragMode
== wxSPLIT_DRAG_NONE
))
296 if ( event
.Leaving() || !SashHitTest(x
, y
) )
301 else if (event
.Dragging() && (m_dragMode
== wxSPLIT_DRAG_DRAGGING
))
303 int diff
= m_splitMode
== wxSPLIT_VERTICAL
? x
- m_oldX
: y
- m_oldY
;
306 // nothing to do, mouse didn't really move far enough
310 int posSashOld
= isLive
? m_sashPosition
: m_sashPositionCurrent
;
311 int posSashNew
= OnSashPositionChanging(posSashOld
+ diff
);
312 if ( posSashNew
== -1 )
314 // change not allowed
318 if ( posSashNew
== m_sashPosition
)
324 DrawSashTracker(m_oldX
, m_oldY
);
327 if (m_splitMode
== wxSPLIT_VERTICAL
)
332 // Remember old positions
337 // As we captured the mouse, we may get the mouse events from outside
338 // our window - for example, negative values in x, y. This has a weird
339 // consequence under MSW where we use unsigned values sometimes and
340 // signed ones other times: the coordinates turn as big positive
341 // numbers and so the sash is drawn on the *right* side of the window
342 // instead of the left (or bottom instead of top). Correct this.
343 if ( (short)m_oldX
< 0 )
345 if ( (short)m_oldY
< 0 )
352 m_sashPositionCurrent
= posSashNew
;
354 DrawSashTracker(m_oldX
, m_oldY
);
358 SetSashPositionAndNotify(posSashNew
);
359 m_needUpdating
= TRUE
;
362 else if ( event
.LeftDClick() && m_windowTwo
)
364 OnDoubleClickSash(x
, y
);
368 void wxSplitterWindow::OnSize(wxSizeEvent
& event
)
370 // only process this message if we're not iconized - otherwise iconizing
371 // and restoring a window containing the splitter has a funny side effect
372 // of changing the splitter position!
373 wxWindow
*parent
= GetParent();
374 while ( parent
&& !parent
->IsTopLevel() )
376 parent
= parent
->GetParent();
379 bool iconized
= FALSE
;
381 wxTopLevelWindow
*winTop
= wxDynamicCast(parent
, wxTopLevelWindow
);
384 iconized
= winTop
->IsIconized();
388 wxFAIL_MSG(wxT("should have a top level parent!"));
403 GetClientSize(&w
, &h
);
405 int size
= m_splitMode
== wxSPLIT_VERTICAL
? w
: h
;
406 if ( m_sashPosition
>= size
- 5 )
407 SetSashPositionAndNotify(wxMax(10, size
- 40));
413 bool wxSplitterWindow::SashHitTest(int x
, int y
, int tolerance
)
415 if ( m_windowTwo
== NULL
|| m_sashPosition
== 0)
416 return FALSE
; // No sash
418 int z
= m_splitMode
== wxSPLIT_VERTICAL
? x
: y
;
420 return z
>= m_sashPosition
- tolerance
&&
421 z
<= m_sashPosition
+ GetSashSize() + tolerance
;
424 int wxSplitterWindow::GetSashSize() const
426 return wxRendererNative::Get().GetSplitterParams(this).widthSash
;
429 int wxSplitterWindow::GetBorderSize() const
431 return wxRendererNative::Get().GetSplitterParams(this).border
;
435 void wxSplitterWindow::DrawSash(wxDC
& dc
)
437 if (HasFlag(wxSP_3DBORDER
))
438 wxRendererNative::Get().DrawSplitterBorder
445 // don't draw sash if we're not split
446 if ( m_sashPosition
== 0 || !m_windowTwo
)
449 // nor if we're configured to not show it
450 if ( HasFlag(wxSP_NOSASH
) )
453 wxRendererNative::Get().DrawSplitterSash
459 m_splitMode
== wxSPLIT_VERTICAL
? wxVERTICAL
461 m_isHot
? wxCONTROL_CURRENT
: 0
465 // Draw the sash tracker (for whilst moving the sash)
466 void wxSplitterWindow::DrawSashTracker(int x
, int y
)
469 GetClientSize(&w
, &h
);
475 if ( m_splitMode
== wxSPLIT_VERTICAL
)
506 ClientToScreen(&x1
, &y1
);
507 ClientToScreen(&x2
, &y2
);
509 screenDC
.SetLogicalFunction(wxINVERT
);
510 screenDC
.SetPen(*m_sashTrackerPen
);
511 screenDC
.SetBrush(*wxTRANSPARENT_BRUSH
);
513 screenDC
.DrawLine(x1
, y1
, x2
, y2
);
515 screenDC
.SetLogicalFunction(wxCOPY
);
518 int wxSplitterWindow::GetWindowSize() const
520 wxSize size
= GetClientSize();
522 return m_splitMode
== wxSPLIT_VERTICAL
? size
.x
: size
.y
;
525 int wxSplitterWindow::AdjustSashPosition(int sashPos
) const
527 int window_size
= GetWindowSize();
534 // the window shouldn't be smaller than its own minimal size nor
535 // smaller than the minimual pane size specified for this splitter
536 int minSize
= m_splitMode
== wxSPLIT_VERTICAL
? win
->GetMinWidth()
537 : win
->GetMinHeight();
539 if ( minSize
== -1 || m_minimumPaneSize
> minSize
)
540 minSize
= m_minimumPaneSize
;
542 minSize
+= GetBorderSize();
544 if ( sashPos
< minSize
)
551 int minSize
= m_splitMode
== wxSPLIT_VERTICAL
? win
->GetMinWidth()
552 : win
->GetMinHeight();
554 if ( minSize
== -1 || m_minimumPaneSize
> minSize
)
555 minSize
= m_minimumPaneSize
;
557 int maxSize
= window_size
- minSize
- GetBorderSize();
558 if ( sashPos
> maxSize
)
565 bool wxSplitterWindow::DoSetSashPosition(int sashPos
)
567 int newSashPosition
= AdjustSashPosition(sashPos
);
569 if ( newSashPosition
== m_sashPosition
)
572 m_sashPosition
= newSashPosition
;
577 void wxSplitterWindow::SetSashPositionAndNotify(int sashPos
)
579 if ( DoSetSashPosition(sashPos
) )
581 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED
, this);
582 event
.m_data
.pos
= m_sashPosition
;
584 (void)DoSendEvent(event
);
588 // Position and size subwindows.
589 // Note that the border size applies to each subwindow, not
590 // including the edges next to the sash.
591 void wxSplitterWindow::SizeWindows()
593 // check if we have delayed setting the real sash position
594 if ( m_requestedSashPosition
!= INT_MAX
)
596 int newSashPosition
= ConvertSashPosition(m_requestedSashPosition
);
597 if ( newSashPosition
!= m_sashPosition
)
599 DoSetSashPosition(newSashPosition
);
602 if ( newSashPosition
<= m_sashPosition
603 && newSashPosition
>= m_sashPosition
- GetBorderSize() )
605 // don't update it any more
606 m_requestedSashPosition
= INT_MAX
;
611 GetClientSize(&w
, &h
);
613 if ( GetWindow1() && !GetWindow2() )
615 GetWindow1()->SetSize(GetBorderSize(), GetBorderSize(),
616 w
- 2*GetBorderSize(), h
- 2*GetBorderSize());
618 else if ( GetWindow1() && GetWindow2() )
620 const int border
= GetBorderSize(),
621 sash
= GetSashSize();
623 int size1
= GetSashPosition() - border
,
624 size2
= GetSashPosition() + sash
;
626 int x2
, y2
, w1
, h1
, w2
, h2
;
627 if ( GetSplitMode() == wxSPLIT_VERTICAL
)
630 w2
= w
- 2*border
- sash
- w1
;
636 else // horz splitter
641 h2
= h
- 2*border
- sash
- h1
;
646 GetWindow1()->SetSize(border
, border
, w1
, h1
);
647 GetWindow2()->SetSize(x2
, y2
, w2
, h2
);
653 SetNeedUpdating(FALSE
);
656 // Set pane for unsplit window
657 void wxSplitterWindow::Initialize(wxWindow
*window
)
659 wxASSERT_MSG( window
&& window
->GetParent() == this,
660 _T("windows in the splitter should have it as parent!") );
662 m_windowOne
= window
;
663 m_windowTwo
= (wxWindow
*) NULL
;
664 DoSetSashPosition(0);
667 // Associates the given window with window 2, drawing the appropriate sash
668 // and changing the split mode.
669 // Does nothing and returns FALSE if the window is already split.
670 bool wxSplitterWindow::DoSplit(wxSplitMode mode
,
671 wxWindow
*window1
, wxWindow
*window2
,
677 wxCHECK_MSG( window1
&& window2
, FALSE
,
678 _T("can not split with NULL window(s)") );
680 wxCHECK_MSG( window1
->GetParent() == this && window2
->GetParent() == this, FALSE
,
681 _T("windows in the splitter should have it as parent!") );
684 m_windowOne
= window1
;
685 m_windowTwo
= window2
;
687 // remember the sash position we want to set for later if we can't set it
688 // right now (e.g. because the window is too small)
689 m_requestedSashPosition
= sashPosition
;
691 DoSetSashPosition(ConvertSashPosition(sashPosition
));
698 int wxSplitterWindow::ConvertSashPosition(int sashPosition
) const
700 if ( sashPosition
> 0 )
704 else if ( sashPosition
< 0 )
706 // It's negative so adding is subtracting
707 return GetWindowSize() + sashPosition
;
709 else // sashPosition == 0
711 // default, put it in the centre
712 return GetWindowSize() / 2;
716 // Remove the specified (or second) window from the view
717 // Doesn't actually delete the window.
718 bool wxSplitterWindow::Unsplit(wxWindow
*toRemove
)
723 wxWindow
*win
= NULL
;
724 if ( toRemove
== NULL
|| toRemove
== m_windowTwo
)
727 m_windowTwo
= (wxWindow
*) NULL
;
729 else if ( toRemove
== m_windowOne
)
732 m_windowOne
= m_windowTwo
;
733 m_windowTwo
= (wxWindow
*) NULL
;
737 wxFAIL_MSG(wxT("splitter: attempt to remove a non-existent window"));
743 DoSetSashPosition(0);
749 // Replace a window with another one
750 bool wxSplitterWindow::ReplaceWindow(wxWindow
*winOld
, wxWindow
*winNew
)
752 wxCHECK_MSG( winOld
, FALSE
, wxT("use one of Split() functions instead") );
753 wxCHECK_MSG( winNew
, FALSE
, wxT("use Unsplit() functions instead") );
755 if ( winOld
== m_windowTwo
)
757 m_windowTwo
= winNew
;
759 else if ( winOld
== m_windowOne
)
761 m_windowOne
= winNew
;
765 wxFAIL_MSG(wxT("splitter: attempt to replace a non-existent window"));
775 void wxSplitterWindow::SetMinimumPaneSize(int min
)
777 m_minimumPaneSize
= min
;
778 SetSashPosition(m_sashPosition
); // re-check limits
781 void wxSplitterWindow::SetSashPosition(int position
, bool redraw
)
783 DoSetSashPosition(position
);
791 bool wxSplitterWindow::DoSendEvent(wxSplitterEvent
& event
)
793 return !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed();
796 // ---------------------------------------------------------------------------
797 // wxSplitterWindow virtual functions: they now just generate the events
798 // ---------------------------------------------------------------------------
800 bool wxSplitterWindow::OnSashPositionChange(int WXUNUSED(newSashPosition
))
802 // always allow by default
806 int wxSplitterWindow::OnSashPositionChanging(int newSashPosition
)
808 // If within UNSPLIT_THRESHOLD from edge, set to edge to cause closure.
809 const int UNSPLIT_THRESHOLD
= 4;
811 // first of all, check if OnSashPositionChange() doesn't forbid this change
812 if ( !OnSashPositionChange(newSashPosition
) )
818 // Obtain relevant window dimension for bottom / right threshold check
819 int window_size
= GetWindowSize();
821 bool unsplit_scenario
= FALSE
;
822 if ( m_permitUnsplitAlways
|| m_minimumPaneSize
== 0 )
824 // Do edge detection if unsplit premitted
825 if ( newSashPosition
<= UNSPLIT_THRESHOLD
)
827 // threshold top / left check
829 unsplit_scenario
= TRUE
;
831 if ( newSashPosition
>= window_size
- UNSPLIT_THRESHOLD
)
833 // threshold bottom/right check
834 newSashPosition
= window_size
;
835 unsplit_scenario
= TRUE
;
839 if ( !unsplit_scenario
)
841 // If resultant pane would be too small, enlarge it
842 newSashPosition
= AdjustSashPosition(newSashPosition
);
845 // If the result is out of bounds it means minimum size is too big,
846 // so split window in half as best compromise.
847 if ( newSashPosition
< 0 || newSashPosition
> window_size
)
848 newSashPosition
= window_size
/ 2;
850 // now let the event handler have it
852 // FIXME: shouldn't we do it before the adjustments above so as to ensure
853 // that the sash position is always reasonable?
854 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING
, this);
855 event
.m_data
.pos
= newSashPosition
;
857 if ( !DoSendEvent(event
) )
859 // the event handler vetoed the change
860 newSashPosition
= -1;
864 // it could have been changed by it
865 newSashPosition
= event
.GetSashPosition();
868 return newSashPosition
;
871 // Called when the sash is double-clicked. The default behaviour is to remove
872 // the sash if the minimum pane size is zero.
873 void wxSplitterWindow::OnDoubleClickSash(int x
, int y
)
875 wxCHECK_RET(m_windowTwo
, wxT("splitter: no window to remove"));
877 // new code should handle events instead of using the virtual functions
878 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_DOUBLECLICKED
, this);
879 event
.m_data
.pt
.x
= x
;
880 event
.m_data
.pt
.y
= y
;
881 if ( DoSendEvent(event
) )
883 if ( GetMinimumPaneSize() == 0 || m_permitUnsplitAlways
)
885 wxWindow
* win
= m_windowTwo
;
888 wxSplitterEvent
unsplitEvent(wxEVT_COMMAND_SPLITTER_UNSPLIT
, this);
889 unsplitEvent
.m_data
.win
= win
;
890 (void)DoSendEvent(unsplitEvent
);
894 //else: blocked by user
897 void wxSplitterWindow::OnUnsplit(wxWindow
*winRemoved
)
899 // call this before calling the event handler which may delete the window
900 winRemoved
->Show(FALSE
);
903 #if defined( __WXMSW__ ) || defined( __WXMAC__)
905 // this is currently called (and needed) under MSW only...
906 void wxSplitterWindow::OnSetCursor(wxSetCursorEvent
& event
)
908 // if we don't do it, the resizing cursor might be set for child window:
909 // and like this we explicitly say that our cursor should not be used for
910 // children windows which overlap us
912 if ( SashHitTest(event
.GetX(), event
.GetY()) )
914 // default processing is ok
917 //else: do nothing, in particular, don't call Skip()
920 #endif // wxMSW || wxMac
922 #endif // wxUSE_SPLITTER