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
;
114 wxSplitterWindow::~wxSplitterWindow()
116 delete m_sashTrackerPen
;
119 void wxSplitterWindow::SetResizeCursor()
121 SetCursor(m_splitMode
== wxSPLIT_VERTICAL
? m_sashCursorWE
125 void wxSplitterWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
132 void wxSplitterWindow::OnInternalIdle()
134 wxWindow::OnInternalIdle();
140 void wxSplitterWindow::OnMouseEvent(wxMouseEvent
& event
)
142 int x
= (int)event
.GetX(),
143 y
= (int)event
.GetY();
145 if (GetWindowStyle() & wxSP_NOSASH
)
148 // with wxSP_LIVE_UPDATE style the splitter windows are always resized
149 // following the mouse movement while it drags the sash, without it we only
150 // draw the sash at the new position but only resize the windows when the
151 // dragging is finished
152 bool isLive
= (GetWindowStyleFlag() & wxSP_LIVE_UPDATE
) != 0;
154 if (event
.LeftDown())
156 if ( SashHitTest(x
, y
) )
158 // Start the drag now
159 m_dragMode
= wxSPLIT_DRAG_DRAGGING
;
161 // Capture mouse and set the cursor
167 // remember the initial sash position and draw the initial
169 m_sashPositionCurrent
= m_sashPosition
;
171 DrawSashTracker(x
, y
);
181 else if (event
.LeftUp() && m_dragMode
== wxSPLIT_DRAG_DRAGGING
)
183 // We can stop dragging now and see what we've got.
184 m_dragMode
= wxSPLIT_DRAG_NONE
;
186 // Release mouse and unset the cursor
188 SetCursor(* wxSTANDARD_CURSOR
);
190 // exit if unsplit after doubleclick
199 DrawSashTracker(m_oldX
, m_oldY
);
202 // the position of the click doesn't exactly correspond to
203 // m_sashPosition, rather it changes it by the distance by which the
205 int diff
= m_splitMode
== wxSPLIT_VERTICAL
? x
- m_oldX
: y
- m_oldY
;
207 int posSashOld
= isLive
? m_sashPosition
: m_sashPositionCurrent
;
208 int posSashNew
= OnSashPositionChanging(posSashOld
+ diff
);
209 if ( posSashNew
== -1 )
211 // change not allowed
215 if ( m_permitUnsplitAlways
|| m_minimumPaneSize
== 0 )
217 // Deal with possible unsplit scenarios
218 if ( posSashNew
== 0 )
220 // We remove the first window from the view
221 wxWindow
*removedWindow
= m_windowOne
;
222 m_windowOne
= m_windowTwo
;
223 m_windowTwo
= (wxWindow
*) NULL
;
224 OnUnsplit(removedWindow
);
225 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_UNSPLIT
, this);
226 event
.m_data
.win
= removedWindow
;
227 (void)DoSendEvent(event
);
228 SetSashPositionAndNotify(0);
230 else if ( posSashNew
== GetWindowSize() )
232 // We remove the second window from the view
233 wxWindow
*removedWindow
= m_windowTwo
;
234 m_windowTwo
= (wxWindow
*) NULL
;
235 OnUnsplit(removedWindow
);
236 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_UNSPLIT
, this);
237 event
.m_data
.win
= removedWindow
;
238 (void)DoSendEvent(event
);
239 SetSashPositionAndNotify(0);
243 SetSashPositionAndNotify(posSashNew
);
248 SetSashPositionAndNotify(posSashNew
);
252 } // left up && dragging
253 else if ((event
.Moving() || event
.Leaving() || event
.Entering()) && (m_dragMode
== wxSPLIT_DRAG_NONE
))
255 // Just change the cursor as required
256 if ( !event
.Leaving() && SashHitTest(x
, y
) )
262 SetCursor(* wxSTANDARD_CURSOR
);
265 else if (event
.Dragging() && (m_dragMode
== wxSPLIT_DRAG_DRAGGING
))
267 int diff
= m_splitMode
== wxSPLIT_VERTICAL
? x
- m_oldX
: y
- m_oldY
;
270 // nothing to do, mouse didn't really move far enough
274 int posSashOld
= isLive
? m_sashPosition
: m_sashPositionCurrent
;
275 int posSashNew
= OnSashPositionChanging(posSashOld
+ diff
);
276 if ( posSashNew
== -1 )
278 // change not allowed
282 if ( posSashNew
== m_sashPosition
)
288 DrawSashTracker(m_oldX
, m_oldY
);
291 if (m_splitMode
== wxSPLIT_VERTICAL
)
296 // Remember old positions
301 // As we captured the mouse, we may get the mouse events from outside
302 // our window - for example, negative values in x, y. This has a weird
303 // consequence under MSW where we use unsigned values sometimes and
304 // signed ones other times: the coordinates turn as big positive
305 // numbers and so the sash is drawn on the *right* side of the window
306 // instead of the left (or bottom instead of top). Correct this.
307 if ( (short)m_oldX
< 0 )
309 if ( (short)m_oldY
< 0 )
316 m_sashPositionCurrent
= posSashNew
;
318 DrawSashTracker(m_oldX
, m_oldY
);
322 SetSashPositionAndNotify(posSashNew
);
323 m_needUpdating
= TRUE
;
326 else if ( event
.LeftDClick() && m_windowTwo
)
328 OnDoubleClickSash(x
, y
);
332 void wxSplitterWindow::OnSize(wxSizeEvent
& event
)
334 // only process this message if we're not iconized - otherwise iconizing
335 // and restoring a window containing the splitter has a funny side effect
336 // of changing the splitter position!
337 wxWindow
*parent
= GetParent();
338 while ( parent
&& !parent
->IsTopLevel() )
340 parent
= parent
->GetParent();
343 bool iconized
= FALSE
;
345 wxTopLevelWindow
*winTop
= wxDynamicCast(parent
, wxTopLevelWindow
);
348 iconized
= winTop
->IsIconized();
352 wxFAIL_MSG(wxT("should have a top level parent!"));
367 GetClientSize(&w
, &h
);
369 int size
= m_splitMode
== wxSPLIT_VERTICAL
? w
: h
;
370 if ( m_sashPosition
>= size
- 5 )
371 SetSashPositionAndNotify(wxMax(10, size
- 40));
377 bool wxSplitterWindow::SashHitTest(int x
, int y
, int tolerance
)
379 if ( m_windowTwo
== NULL
|| m_sashPosition
== 0)
380 return FALSE
; // No sash
382 int z
= m_splitMode
== wxSPLIT_VERTICAL
? x
: y
;
384 return z
>= m_sashPosition
- tolerance
&&
385 z
<= m_sashPosition
+ GetSashSize() + tolerance
;
388 int wxSplitterWindow::GetSashSize() const
390 return wxRendererNative::Get().GetSplitterSashAndBorder(this).x
;
393 int wxSplitterWindow::GetBorderSize() const
395 return wxRendererNative::Get().GetSplitterSashAndBorder(this).y
;
399 void wxSplitterWindow::DrawSash(wxDC
& dc
)
401 wxRendererNative::Get().DrawSplitterBorder
408 // don't draw sash if we're not split
409 if ( m_sashPosition
== 0 || !m_windowTwo
)
412 // nor if we're configured to not show it
413 if ( HasFlag(wxSP_NOSASH
) )
416 wxRendererNative::Get().DrawSplitterSash
422 m_splitMode
== wxSPLIT_VERTICAL
428 // Draw the sash tracker (for whilst moving the sash)
429 void wxSplitterWindow::DrawSashTracker(int x
, int y
)
432 GetClientSize(&w
, &h
);
438 if ( m_splitMode
== wxSPLIT_VERTICAL
)
469 ClientToScreen(&x1
, &y1
);
470 ClientToScreen(&x2
, &y2
);
472 screenDC
.SetLogicalFunction(wxINVERT
);
473 screenDC
.SetPen(*m_sashTrackerPen
);
474 screenDC
.SetBrush(*wxTRANSPARENT_BRUSH
);
476 screenDC
.DrawLine(x1
, y1
, x2
, y2
);
478 screenDC
.SetLogicalFunction(wxCOPY
);
481 int wxSplitterWindow::GetWindowSize() const
483 wxSize size
= GetClientSize();
485 return m_splitMode
== wxSPLIT_VERTICAL
? size
.x
: size
.y
;
488 int wxSplitterWindow::AdjustSashPosition(int sashPos
) const
490 int window_size
= GetWindowSize();
497 // the window shouldn't be smaller than its own minimal size nor
498 // smaller than the minimual pane size specified for this splitter
499 int minSize
= m_splitMode
== wxSPLIT_VERTICAL
? win
->GetMinWidth()
500 : win
->GetMinHeight();
502 if ( minSize
== -1 || m_minimumPaneSize
> minSize
)
503 minSize
= m_minimumPaneSize
;
505 minSize
+= GetBorderSize();
507 if ( sashPos
< minSize
)
514 int minSize
= m_splitMode
== wxSPLIT_VERTICAL
? win
->GetMinWidth()
515 : win
->GetMinHeight();
517 if ( minSize
== -1 || m_minimumPaneSize
> minSize
)
518 minSize
= m_minimumPaneSize
;
520 int maxSize
= window_size
- minSize
- GetBorderSize();
521 if ( sashPos
> maxSize
)
528 bool wxSplitterWindow::DoSetSashPosition(int sashPos
)
530 int newSashPosition
= AdjustSashPosition(sashPos
);
532 if ( newSashPosition
== m_sashPosition
)
535 m_sashPosition
= newSashPosition
;
540 void wxSplitterWindow::SetSashPositionAndNotify(int sashPos
)
542 if ( DoSetSashPosition(sashPos
) )
544 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED
, this);
545 event
.m_data
.pos
= m_sashPosition
;
547 (void)DoSendEvent(event
);
551 // Position and size subwindows.
552 // Note that the border size applies to each subwindow, not
553 // including the edges next to the sash.
554 void wxSplitterWindow::SizeWindows()
556 // check if we have delayed setting the real sash position
557 if ( m_requestedSashPosition
!= INT_MAX
)
559 int newSashPosition
= ConvertSashPosition(m_requestedSashPosition
);
560 if ( newSashPosition
!= m_sashPosition
)
562 DoSetSashPosition(newSashPosition
);
565 if ( newSashPosition
<= m_sashPosition
566 && newSashPosition
>= m_sashPosition
- GetBorderSize() )
568 // don't update it any more
569 m_requestedSashPosition
= INT_MAX
;
574 GetClientSize(&w
, &h
);
576 if ( GetWindow1() && !GetWindow2() )
578 GetWindow1()->SetSize(GetBorderSize(), GetBorderSize(),
579 w
- 2*GetBorderSize(), h
- 2*GetBorderSize());
581 else if ( GetWindow1() && GetWindow2() )
583 const int border
= GetBorderSize(),
584 sash
= GetSashSize();
586 int size1
= GetSashPosition() - border
,
587 size2
= GetSashPosition() + sash
;
589 int x2
, y2
, w1
, h1
, w2
, h2
;
590 if ( GetSplitMode() == wxSPLIT_VERTICAL
)
593 w2
= w
- 2*border
- sash
- w1
;
599 else // horz splitter
604 h2
= h
- 2*border
- sash
- h1
;
609 GetWindow1()->SetSize(border
, border
, w1
, h1
);
610 GetWindow2()->SetSize(x2
, y2
, w2
, h2
);
616 SetNeedUpdating(FALSE
);
619 // Set pane for unsplit window
620 void wxSplitterWindow::Initialize(wxWindow
*window
)
622 wxASSERT_MSG( window
&& window
->GetParent() == this,
623 _T("windows in the splitter should have it as parent!") );
625 m_windowOne
= window
;
626 m_windowTwo
= (wxWindow
*) NULL
;
627 DoSetSashPosition(0);
630 // Associates the given window with window 2, drawing the appropriate sash
631 // and changing the split mode.
632 // Does nothing and returns FALSE if the window is already split.
633 bool wxSplitterWindow::DoSplit(wxSplitMode mode
,
634 wxWindow
*window1
, wxWindow
*window2
,
640 wxCHECK_MSG( window1
&& window2
, FALSE
,
641 _T("can not split with NULL window(s)") );
643 wxCHECK_MSG( window1
->GetParent() == this && window2
->GetParent() == this, FALSE
,
644 _T("windows in the splitter should have it as parent!") );
647 m_windowOne
= window1
;
648 m_windowTwo
= window2
;
650 // remember the sash position we want to set for later if we can't set it
651 // right now (e.g. because the window is too small)
652 m_requestedSashPosition
= sashPosition
;
654 DoSetSashPosition(ConvertSashPosition(sashPosition
));
661 int wxSplitterWindow::ConvertSashPosition(int sashPosition
) const
663 if ( sashPosition
> 0 )
667 else if ( sashPosition
< 0 )
669 // It's negative so adding is subtracting
670 return GetWindowSize() + sashPosition
;
672 else // sashPosition == 0
674 // default, put it in the centre
675 return GetWindowSize() / 2;
679 // Remove the specified (or second) window from the view
680 // Doesn't actually delete the window.
681 bool wxSplitterWindow::Unsplit(wxWindow
*toRemove
)
686 wxWindow
*win
= NULL
;
687 if ( toRemove
== NULL
|| toRemove
== m_windowTwo
)
690 m_windowTwo
= (wxWindow
*) NULL
;
692 else if ( toRemove
== m_windowOne
)
695 m_windowOne
= m_windowTwo
;
696 m_windowTwo
= (wxWindow
*) NULL
;
700 wxFAIL_MSG(wxT("splitter: attempt to remove a non-existent window"));
706 DoSetSashPosition(0);
712 // Replace a window with another one
713 bool wxSplitterWindow::ReplaceWindow(wxWindow
*winOld
, wxWindow
*winNew
)
715 wxCHECK_MSG( winOld
, FALSE
, wxT("use one of Split() functions instead") );
716 wxCHECK_MSG( winNew
, FALSE
, wxT("use Unsplit() functions instead") );
718 if ( winOld
== m_windowTwo
)
720 m_windowTwo
= winNew
;
722 else if ( winOld
== m_windowOne
)
724 m_windowOne
= winNew
;
728 wxFAIL_MSG(wxT("splitter: attempt to replace a non-existent window"));
738 void wxSplitterWindow::SetMinimumPaneSize(int min
)
740 m_minimumPaneSize
= min
;
741 SetSashPosition(m_sashPosition
); // re-check limits
744 void wxSplitterWindow::SetSashPosition(int position
, bool redraw
)
746 DoSetSashPosition(position
);
754 bool wxSplitterWindow::DoSendEvent(wxSplitterEvent
& event
)
756 return !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed();
759 // ---------------------------------------------------------------------------
760 // wxSplitterWindow virtual functions: they now just generate the events
761 // ---------------------------------------------------------------------------
763 bool wxSplitterWindow::OnSashPositionChange(int WXUNUSED(newSashPosition
))
765 // always allow by default
769 int wxSplitterWindow::OnSashPositionChanging(int newSashPosition
)
771 // If within UNSPLIT_THRESHOLD from edge, set to edge to cause closure.
772 const int UNSPLIT_THRESHOLD
= 4;
774 // first of all, check if OnSashPositionChange() doesn't forbid this change
775 if ( !OnSashPositionChange(newSashPosition
) )
781 // Obtain relevant window dimension for bottom / right threshold check
782 int window_size
= GetWindowSize();
784 bool unsplit_scenario
= FALSE
;
785 if ( m_permitUnsplitAlways
|| m_minimumPaneSize
== 0 )
787 // Do edge detection if unsplit premitted
788 if ( newSashPosition
<= UNSPLIT_THRESHOLD
)
790 // threshold top / left check
792 unsplit_scenario
= TRUE
;
794 if ( newSashPosition
>= window_size
- UNSPLIT_THRESHOLD
)
796 // threshold bottom/right check
797 newSashPosition
= window_size
;
798 unsplit_scenario
= TRUE
;
802 if ( !unsplit_scenario
)
804 // If resultant pane would be too small, enlarge it
805 newSashPosition
= AdjustSashPosition(newSashPosition
);
808 // If the result is out of bounds it means minimum size is too big,
809 // so split window in half as best compromise.
810 if ( newSashPosition
< 0 || newSashPosition
> window_size
)
811 newSashPosition
= window_size
/ 2;
813 // now let the event handler have it
815 // FIXME: shouldn't we do it before the adjustments above so as to ensure
816 // that the sash position is always reasonable?
817 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING
, this);
818 event
.m_data
.pos
= newSashPosition
;
820 if ( !DoSendEvent(event
) )
822 // the event handler vetoed the change
823 newSashPosition
= -1;
827 // it could have been changed by it
828 newSashPosition
= event
.GetSashPosition();
831 return newSashPosition
;
834 // Called when the sash is double-clicked. The default behaviour is to remove
835 // the sash if the minimum pane size is zero.
836 void wxSplitterWindow::OnDoubleClickSash(int x
, int y
)
838 wxCHECK_RET(m_windowTwo
, wxT("splitter: no window to remove"));
840 // new code should handle events instead of using the virtual functions
841 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_DOUBLECLICKED
, this);
842 event
.m_data
.pt
.x
= x
;
843 event
.m_data
.pt
.y
= y
;
844 if ( DoSendEvent(event
) )
846 if ( GetMinimumPaneSize() == 0 || m_permitUnsplitAlways
)
848 wxWindow
* win
= m_windowTwo
;
851 wxSplitterEvent
unsplitEvent(wxEVT_COMMAND_SPLITTER_UNSPLIT
, this);
852 unsplitEvent
.m_data
.win
= win
;
853 (void)DoSendEvent(unsplitEvent
);
857 //else: blocked by user
860 void wxSplitterWindow::OnUnsplit(wxWindow
*winRemoved
)
862 // call this before calling the event handler which may delete the window
863 winRemoved
->Show(FALSE
);
866 #if defined( __WXMSW__ ) || defined( __WXMAC__)
868 // this is currently called (and needed) under MSW only...
869 void wxSplitterWindow::OnSetCursor(wxSetCursorEvent
& event
)
871 // if we don't do it, the resizing cursor might be set for child window:
872 // and like this we explicitly say that our cursor should not be used for
873 // children windows which overlap us
875 if ( SashHitTest(event
.GetX(), event
.GetY()) )
877 // default processing is ok
880 //else: do nothing, in particular, don't call Skip()
883 #endif // wxMSW || wxMac
885 #endif // wxUSE_SPLITTER