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 /////////////////////////////////////////////////////////////////////////////
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
)
51 IMPLEMENT_DYNAMIC_CLASS(wxSplitterEvent
, wxNotifyEvent
)
53 BEGIN_EVENT_TABLE(wxSplitterWindow
, wxWindow
)
54 EVT_PAINT(wxSplitterWindow::OnPaint
)
55 EVT_SIZE(wxSplitterWindow::OnSize
)
56 EVT_MOUSE_EVENTS(wxSplitterWindow::OnMouseEvent
)
58 #if defined( __WXMSW__ ) || defined( __WXMAC__)
59 EVT_SET_CURSOR(wxSplitterWindow::OnSetCursor
)
62 WX_EVENT_TABLE_CONTROL_CONTAINER(wxSplitterWindow
)
65 WX_DELEGATE_TO_CONTROL_CONTAINER(wxSplitterWindow
);
67 bool wxSplitterWindow::Create(wxWindow
*parent
, wxWindowID id
,
73 // allow TABbing from one window to the other
74 style
|= wxTAB_TRAVERSAL
;
76 // we draw our border ourselves to blend the sash with it
77 style
&= ~wxBORDER_MASK
;
78 style
|= wxBORDER_NONE
;
80 // we don't need to be completely repainted after resize and doing it
81 // results in horrible flicker
82 style
|= wxNO_FULL_REPAINT_ON_RESIZE
;
84 if ( !wxWindow::Create(parent
, id
, pos
, size
, style
, name
) )
87 m_permitUnsplitAlways
= (style
& wxSP_PERMIT_UNSPLIT
) != 0;
92 void wxSplitterWindow::Init()
94 m_container
.SetContainerWindow(this);
96 m_splitMode
= wxSPLIT_VERTICAL
;
97 m_permitUnsplitAlways
= TRUE
;
98 m_windowOne
= (wxWindow
*) NULL
;
99 m_windowTwo
= (wxWindow
*) NULL
;
100 m_dragMode
= wxSPLIT_DRAG_NONE
;
105 m_sashPosition
= m_requestedSashPosition
= 0;
106 m_minimumPaneSize
= 0;
107 m_sashCursorWE
= wxCursor(wxCURSOR_SIZEWE
);
108 m_sashCursorNS
= wxCursor(wxCURSOR_SIZENS
);
109 m_sashTrackerPen
= new wxPen(*wxBLACK
, 2, wxSOLID
);
111 m_needUpdating
= FALSE
;
115 wxSplitterWindow::~wxSplitterWindow()
117 delete m_sashTrackerPen
;
120 // ----------------------------------------------------------------------------
121 // entering/leaving sash
122 // ----------------------------------------------------------------------------
124 void wxSplitterWindow::RedrawIfHotSensitive(bool isHot
)
126 if ( wxRendererNative::Get().GetSplitterParams(this).isHotSensitive
)
133 //else: we don't change our appearance, don't redraw to avoid flicker
136 void wxSplitterWindow::OnEnterSash()
140 RedrawIfHotSensitive(true);
143 void wxSplitterWindow::OnLeaveSash()
145 SetCursor(*wxSTANDARD_CURSOR
);
147 RedrawIfHotSensitive(false);
150 void wxSplitterWindow::SetResizeCursor()
152 SetCursor(m_splitMode
== wxSPLIT_VERTICAL
? m_sashCursorWE
156 // ----------------------------------------------------------------------------
157 // other event handlers
158 // ----------------------------------------------------------------------------
160 void wxSplitterWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
167 void wxSplitterWindow::OnInternalIdle()
169 wxWindow::OnInternalIdle();
175 void wxSplitterWindow::OnMouseEvent(wxMouseEvent
& event
)
177 int x
= (int)event
.GetX(),
178 y
= (int)event
.GetY();
180 if (GetWindowStyle() & wxSP_NOSASH
)
183 // with wxSP_LIVE_UPDATE style the splitter windows are always resized
184 // following the mouse movement while it drags the sash, without it we only
185 // draw the sash at the new position but only resize the windows when the
186 // dragging is finished
187 bool isLive
= (GetWindowStyleFlag() & wxSP_LIVE_UPDATE
) != 0;
189 if (event
.LeftDown())
191 if ( SashHitTest(x
, y
) )
193 // Start the drag now
194 m_dragMode
= wxSPLIT_DRAG_DRAGGING
;
196 // Capture mouse and set the cursor
202 // remember the initial sash position and draw the initial
204 m_sashPositionCurrent
= m_sashPosition
;
206 DrawSashTracker(x
, y
);
216 else if (event
.LeftUp() && m_dragMode
== wxSPLIT_DRAG_DRAGGING
)
218 // We can stop dragging now and see what we've got.
219 m_dragMode
= wxSPLIT_DRAG_NONE
;
221 // Release mouse and unset the cursor
223 SetCursor(* wxSTANDARD_CURSOR
);
225 // exit if unsplit after doubleclick
234 DrawSashTracker(m_oldX
, m_oldY
);
237 // the position of the click doesn't exactly correspond to
238 // m_sashPosition, rather it changes it by the distance by which the
240 int diff
= m_splitMode
== wxSPLIT_VERTICAL
? x
- m_oldX
: y
- m_oldY
;
242 int posSashOld
= isLive
? m_sashPosition
: m_sashPositionCurrent
;
243 int posSashNew
= OnSashPositionChanging(posSashOld
+ diff
);
244 if ( posSashNew
== -1 )
246 // change not allowed
250 if ( m_permitUnsplitAlways
|| m_minimumPaneSize
== 0 )
252 // Deal with possible unsplit scenarios
253 if ( posSashNew
== 0 )
255 // We remove the first window from the view
256 wxWindow
*removedWindow
= m_windowOne
;
257 m_windowOne
= m_windowTwo
;
258 m_windowTwo
= (wxWindow
*) NULL
;
259 OnUnsplit(removedWindow
);
260 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_UNSPLIT
, this);
261 event
.m_data
.win
= removedWindow
;
262 (void)DoSendEvent(event
);
263 SetSashPositionAndNotify(0);
265 else if ( posSashNew
== GetWindowSize() )
267 // We remove the second window from the view
268 wxWindow
*removedWindow
= m_windowTwo
;
269 m_windowTwo
= (wxWindow
*) NULL
;
270 OnUnsplit(removedWindow
);
271 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_UNSPLIT
, this);
272 event
.m_data
.win
= removedWindow
;
273 (void)DoSendEvent(event
);
274 SetSashPositionAndNotify(0);
278 SetSashPositionAndNotify(posSashNew
);
283 SetSashPositionAndNotify(posSashNew
);
287 } // left up && dragging
288 else if ((event
.Moving() || event
.Leaving() || event
.Entering()) && (m_dragMode
== wxSPLIT_DRAG_NONE
))
290 if ( event
.Leaving() || !SashHitTest(x
, y
) )
295 else if (event
.Dragging() && (m_dragMode
== wxSPLIT_DRAG_DRAGGING
))
297 int diff
= m_splitMode
== wxSPLIT_VERTICAL
? x
- m_oldX
: y
- m_oldY
;
300 // nothing to do, mouse didn't really move far enough
304 int posSashOld
= isLive
? m_sashPosition
: m_sashPositionCurrent
;
305 int posSashNew
= OnSashPositionChanging(posSashOld
+ diff
);
306 if ( posSashNew
== -1 )
308 // change not allowed
312 if ( posSashNew
== m_sashPosition
)
318 DrawSashTracker(m_oldX
, m_oldY
);
321 if (m_splitMode
== wxSPLIT_VERTICAL
)
326 // Remember old positions
331 // As we captured the mouse, we may get the mouse events from outside
332 // our window - for example, negative values in x, y. This has a weird
333 // consequence under MSW where we use unsigned values sometimes and
334 // signed ones other times: the coordinates turn as big positive
335 // numbers and so the sash is drawn on the *right* side of the window
336 // instead of the left (or bottom instead of top). Correct this.
337 if ( (short)m_oldX
< 0 )
339 if ( (short)m_oldY
< 0 )
346 m_sashPositionCurrent
= posSashNew
;
348 DrawSashTracker(m_oldX
, m_oldY
);
352 SetSashPositionAndNotify(posSashNew
);
353 m_needUpdating
= TRUE
;
356 else if ( event
.LeftDClick() && m_windowTwo
)
358 OnDoubleClickSash(x
, y
);
362 void wxSplitterWindow::OnSize(wxSizeEvent
& event
)
364 // only process this message if we're not iconized - otherwise iconizing
365 // and restoring a window containing the splitter has a funny side effect
366 // of changing the splitter position!
367 wxWindow
*parent
= GetParent();
368 while ( parent
&& !parent
->IsTopLevel() )
370 parent
= parent
->GetParent();
373 bool iconized
= FALSE
;
375 wxTopLevelWindow
*winTop
= wxDynamicCast(parent
, wxTopLevelWindow
);
378 iconized
= winTop
->IsIconized();
382 wxFAIL_MSG(wxT("should have a top level parent!"));
397 GetClientSize(&w
, &h
);
399 int size
= m_splitMode
== wxSPLIT_VERTICAL
? w
: h
;
400 if ( m_sashPosition
>= size
- 5 )
401 SetSashPositionAndNotify(wxMax(10, size
- 40));
407 bool wxSplitterWindow::SashHitTest(int x
, int y
, int tolerance
)
409 if ( m_windowTwo
== NULL
|| m_sashPosition
== 0)
410 return FALSE
; // No sash
412 int z
= m_splitMode
== wxSPLIT_VERTICAL
? x
: y
;
414 return z
>= m_sashPosition
- tolerance
&&
415 z
<= m_sashPosition
+ GetSashSize() + tolerance
;
418 int wxSplitterWindow::GetSashSize() const
420 return wxRendererNative::Get().GetSplitterParams(this).widthSash
;
423 int wxSplitterWindow::GetBorderSize() const
425 return wxRendererNative::Get().GetSplitterParams(this).border
;
429 void wxSplitterWindow::DrawSash(wxDC
& dc
)
431 if (HasFlag(wxSP_3DBORDER
))
432 wxRendererNative::Get().DrawSplitterBorder
439 // don't draw sash if we're not split
440 if ( m_sashPosition
== 0 || !m_windowTwo
)
443 // nor if we're configured to not show it
444 if ( HasFlag(wxSP_NOSASH
) )
447 wxRendererNative::Get().DrawSplitterSash
453 m_splitMode
== wxSPLIT_VERTICAL
? wxVERTICAL
455 m_isHot
? wxCONTROL_CURRENT
: 0
459 // Draw the sash tracker (for whilst moving the sash)
460 void wxSplitterWindow::DrawSashTracker(int x
, int y
)
463 GetClientSize(&w
, &h
);
469 if ( m_splitMode
== wxSPLIT_VERTICAL
)
500 ClientToScreen(&x1
, &y1
);
501 ClientToScreen(&x2
, &y2
);
503 screenDC
.SetLogicalFunction(wxINVERT
);
504 screenDC
.SetPen(*m_sashTrackerPen
);
505 screenDC
.SetBrush(*wxTRANSPARENT_BRUSH
);
507 screenDC
.DrawLine(x1
, y1
, x2
, y2
);
509 screenDC
.SetLogicalFunction(wxCOPY
);
512 int wxSplitterWindow::GetWindowSize() const
514 wxSize size
= GetClientSize();
516 return m_splitMode
== wxSPLIT_VERTICAL
? size
.x
: size
.y
;
519 int wxSplitterWindow::AdjustSashPosition(int sashPos
) const
521 int window_size
= GetWindowSize();
528 // the window shouldn't be smaller than its own minimal size nor
529 // smaller than the minimual pane size specified for this splitter
530 int minSize
= m_splitMode
== wxSPLIT_VERTICAL
? win
->GetMinWidth()
531 : win
->GetMinHeight();
533 if ( minSize
== -1 || m_minimumPaneSize
> minSize
)
534 minSize
= m_minimumPaneSize
;
536 minSize
+= GetBorderSize();
538 if ( sashPos
< minSize
)
545 int minSize
= m_splitMode
== wxSPLIT_VERTICAL
? win
->GetMinWidth()
546 : win
->GetMinHeight();
548 if ( minSize
== -1 || m_minimumPaneSize
> minSize
)
549 minSize
= m_minimumPaneSize
;
551 int maxSize
= window_size
- minSize
- GetBorderSize();
552 if ( sashPos
> maxSize
)
559 bool wxSplitterWindow::DoSetSashPosition(int sashPos
)
561 int newSashPosition
= AdjustSashPosition(sashPos
);
563 if ( newSashPosition
== m_sashPosition
)
566 m_sashPosition
= newSashPosition
;
571 void wxSplitterWindow::SetSashPositionAndNotify(int sashPos
)
573 if ( DoSetSashPosition(sashPos
) )
575 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED
, this);
576 event
.m_data
.pos
= m_sashPosition
;
578 (void)DoSendEvent(event
);
582 // Position and size subwindows.
583 // Note that the border size applies to each subwindow, not
584 // including the edges next to the sash.
585 void wxSplitterWindow::SizeWindows()
587 // check if we have delayed setting the real sash position
588 if ( m_requestedSashPosition
!= INT_MAX
)
590 int newSashPosition
= ConvertSashPosition(m_requestedSashPosition
);
591 if ( newSashPosition
!= m_sashPosition
)
593 DoSetSashPosition(newSashPosition
);
596 if ( newSashPosition
<= m_sashPosition
597 && newSashPosition
>= m_sashPosition
- GetBorderSize() )
599 // don't update it any more
600 m_requestedSashPosition
= INT_MAX
;
605 GetClientSize(&w
, &h
);
607 if ( GetWindow1() && !GetWindow2() )
609 GetWindow1()->SetSize(GetBorderSize(), GetBorderSize(),
610 w
- 2*GetBorderSize(), h
- 2*GetBorderSize());
612 else if ( GetWindow1() && GetWindow2() )
614 const int border
= GetBorderSize(),
615 sash
= GetSashSize();
617 int size1
= GetSashPosition() - border
,
618 size2
= GetSashPosition() + sash
;
620 int x2
, y2
, w1
, h1
, w2
, h2
;
621 if ( GetSplitMode() == wxSPLIT_VERTICAL
)
624 w2
= w
- 2*border
- sash
- w1
;
630 else // horz splitter
635 h2
= h
- 2*border
- sash
- h1
;
640 GetWindow1()->SetSize(border
, border
, w1
, h1
);
641 GetWindow2()->SetSize(x2
, y2
, w2
, h2
);
647 SetNeedUpdating(FALSE
);
650 // Set pane for unsplit window
651 void wxSplitterWindow::Initialize(wxWindow
*window
)
653 wxASSERT_MSG( window
&& window
->GetParent() == this,
654 _T("windows in the splitter should have it as parent!") );
656 m_windowOne
= window
;
657 m_windowTwo
= (wxWindow
*) NULL
;
658 DoSetSashPosition(0);
661 // Associates the given window with window 2, drawing the appropriate sash
662 // and changing the split mode.
663 // Does nothing and returns FALSE if the window is already split.
664 bool wxSplitterWindow::DoSplit(wxSplitMode mode
,
665 wxWindow
*window1
, wxWindow
*window2
,
671 wxCHECK_MSG( window1
&& window2
, FALSE
,
672 _T("can not split with NULL window(s)") );
674 wxCHECK_MSG( window1
->GetParent() == this && window2
->GetParent() == this, FALSE
,
675 _T("windows in the splitter should have it as parent!") );
678 m_windowOne
= window1
;
679 m_windowTwo
= window2
;
681 // remember the sash position we want to set for later if we can't set it
682 // right now (e.g. because the window is too small)
683 m_requestedSashPosition
= sashPosition
;
685 DoSetSashPosition(ConvertSashPosition(sashPosition
));
692 int wxSplitterWindow::ConvertSashPosition(int sashPosition
) const
694 if ( sashPosition
> 0 )
698 else if ( sashPosition
< 0 )
700 // It's negative so adding is subtracting
701 return GetWindowSize() + sashPosition
;
703 else // sashPosition == 0
705 // default, put it in the centre
706 return GetWindowSize() / 2;
710 // Remove the specified (or second) window from the view
711 // Doesn't actually delete the window.
712 bool wxSplitterWindow::Unsplit(wxWindow
*toRemove
)
717 wxWindow
*win
= NULL
;
718 if ( toRemove
== NULL
|| toRemove
== m_windowTwo
)
721 m_windowTwo
= (wxWindow
*) NULL
;
723 else if ( toRemove
== m_windowOne
)
726 m_windowOne
= m_windowTwo
;
727 m_windowTwo
= (wxWindow
*) NULL
;
731 wxFAIL_MSG(wxT("splitter: attempt to remove a non-existent window"));
737 DoSetSashPosition(0);
743 // Replace a window with another one
744 bool wxSplitterWindow::ReplaceWindow(wxWindow
*winOld
, wxWindow
*winNew
)
746 wxCHECK_MSG( winOld
, FALSE
, wxT("use one of Split() functions instead") );
747 wxCHECK_MSG( winNew
, FALSE
, wxT("use Unsplit() functions instead") );
749 if ( winOld
== m_windowTwo
)
751 m_windowTwo
= winNew
;
753 else if ( winOld
== m_windowOne
)
755 m_windowOne
= winNew
;
759 wxFAIL_MSG(wxT("splitter: attempt to replace a non-existent window"));
769 void wxSplitterWindow::SetMinimumPaneSize(int min
)
771 m_minimumPaneSize
= min
;
772 SetSashPosition(m_sashPosition
); // re-check limits
775 void wxSplitterWindow::SetSashPosition(int position
, bool redraw
)
777 DoSetSashPosition(position
);
785 bool wxSplitterWindow::DoSendEvent(wxSplitterEvent
& event
)
787 return !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed();
790 // ---------------------------------------------------------------------------
791 // wxSplitterWindow virtual functions: they now just generate the events
792 // ---------------------------------------------------------------------------
794 bool wxSplitterWindow::OnSashPositionChange(int WXUNUSED(newSashPosition
))
796 // always allow by default
800 int wxSplitterWindow::OnSashPositionChanging(int newSashPosition
)
802 // If within UNSPLIT_THRESHOLD from edge, set to edge to cause closure.
803 const int UNSPLIT_THRESHOLD
= 4;
805 // first of all, check if OnSashPositionChange() doesn't forbid this change
806 if ( !OnSashPositionChange(newSashPosition
) )
812 // Obtain relevant window dimension for bottom / right threshold check
813 int window_size
= GetWindowSize();
815 bool unsplit_scenario
= FALSE
;
816 if ( m_permitUnsplitAlways
|| m_minimumPaneSize
== 0 )
818 // Do edge detection if unsplit premitted
819 if ( newSashPosition
<= UNSPLIT_THRESHOLD
)
821 // threshold top / left check
823 unsplit_scenario
= TRUE
;
825 if ( newSashPosition
>= window_size
- UNSPLIT_THRESHOLD
)
827 // threshold bottom/right check
828 newSashPosition
= window_size
;
829 unsplit_scenario
= TRUE
;
833 if ( !unsplit_scenario
)
835 // If resultant pane would be too small, enlarge it
836 newSashPosition
= AdjustSashPosition(newSashPosition
);
839 // If the result is out of bounds it means minimum size is too big,
840 // so split window in half as best compromise.
841 if ( newSashPosition
< 0 || newSashPosition
> window_size
)
842 newSashPosition
= window_size
/ 2;
844 // now let the event handler have it
846 // FIXME: shouldn't we do it before the adjustments above so as to ensure
847 // that the sash position is always reasonable?
848 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING
, this);
849 event
.m_data
.pos
= newSashPosition
;
851 if ( !DoSendEvent(event
) )
853 // the event handler vetoed the change
854 newSashPosition
= -1;
858 // it could have been changed by it
859 newSashPosition
= event
.GetSashPosition();
862 return newSashPosition
;
865 // Called when the sash is double-clicked. The default behaviour is to remove
866 // the sash if the minimum pane size is zero.
867 void wxSplitterWindow::OnDoubleClickSash(int x
, int y
)
869 wxCHECK_RET(m_windowTwo
, wxT("splitter: no window to remove"));
871 // new code should handle events instead of using the virtual functions
872 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_DOUBLECLICKED
, this);
873 event
.m_data
.pt
.x
= x
;
874 event
.m_data
.pt
.y
= y
;
875 if ( DoSendEvent(event
) )
877 if ( GetMinimumPaneSize() == 0 || m_permitUnsplitAlways
)
879 wxWindow
* win
= m_windowTwo
;
882 wxSplitterEvent
unsplitEvent(wxEVT_COMMAND_SPLITTER_UNSPLIT
, this);
883 unsplitEvent
.m_data
.win
= win
;
884 (void)DoSendEvent(unsplitEvent
);
888 //else: blocked by user
891 void wxSplitterWindow::OnUnsplit(wxWindow
*winRemoved
)
893 // call this before calling the event handler which may delete the window
894 winRemoved
->Show(FALSE
);
897 #if defined( __WXMSW__ ) || defined( __WXMAC__)
899 // this is currently called (and needed) under MSW only...
900 void wxSplitterWindow::OnSetCursor(wxSetCursorEvent
& event
)
902 // if we don't do it, the resizing cursor might be set for child window:
903 // and like this we explicitly say that our cursor should not be used for
904 // children windows which overlap us
906 if ( SashHitTest(event
.GetX(), event
.GetY()) )
908 // default processing is ok
911 //else: do nothing, in particular, don't call Skip()
914 #endif // wxMSW || wxMac
916 #endif // wxUSE_SPLITTER