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/dcmirror.h"
40 #include "wx/renderer.h"
42 #include "wx/splitter.h"
46 DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED
)
47 DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING
)
48 DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPLITTER_DOUBLECLICKED
)
49 DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPLITTER_UNSPLIT
)
51 IMPLEMENT_DYNAMIC_CLASS(wxSplitterWindow
, wxWindow
)
52 IMPLEMENT_DYNAMIC_CLASS(wxSplitterEvent
, wxNotifyEvent
)
54 BEGIN_EVENT_TABLE(wxSplitterWindow
, wxWindow
)
55 EVT_PAINT(wxSplitterWindow::OnPaint
)
56 EVT_SIZE(wxSplitterWindow::OnSize
)
57 EVT_MOUSE_EVENTS(wxSplitterWindow::OnMouseEvent
)
59 #if defined( __WXMSW__ ) || defined( __WXMAC__)
60 EVT_SET_CURSOR(wxSplitterWindow::OnSetCursor
)
63 WX_EVENT_TABLE_CONTROL_CONTAINER(wxSplitterWindow
)
66 WX_DELEGATE_TO_CONTROL_CONTAINER(wxSplitterWindow
);
68 bool wxSplitterWindow::Create(wxWindow
*parent
, wxWindowID id
,
74 // allow TABbing from one window to the other
75 style
|= wxTAB_TRAVERSAL
;
77 // we draw our border ourselves to blend the sash with it
78 style
&= ~wxBORDER_MASK
;
79 style
|= wxBORDER_NONE
;
81 // we don't need to be completely repainted after resize and doing it
82 // results in horrible flicker
83 style
|= wxNO_FULL_REPAINT_ON_RESIZE
;
85 if ( !wxWindow::Create(parent
, id
, pos
, size
, style
, name
) )
88 m_permitUnsplitAlways
= (style
& wxSP_PERMIT_UNSPLIT
) != 0;
93 void wxSplitterWindow::Init()
95 m_container
.SetContainerWindow(this);
97 m_splitMode
= wxSPLIT_VERTICAL
;
98 m_permitUnsplitAlways
= TRUE
;
99 m_windowOne
= (wxWindow
*) NULL
;
100 m_windowTwo
= (wxWindow
*) NULL
;
101 m_dragMode
= wxSPLIT_DRAG_NONE
;
106 m_sashPosition
= m_requestedSashPosition
= 0;
107 m_minimumPaneSize
= 0;
108 m_sashCursorWE
= wxCursor(wxCURSOR_SIZEWE
);
109 m_sashCursorNS
= wxCursor(wxCURSOR_SIZENS
);
110 m_sashTrackerPen
= new wxPen(*wxBLACK
, 2, wxSOLID
);
112 m_needUpdating
= FALSE
;
115 wxSplitterWindow::~wxSplitterWindow()
117 delete m_sashTrackerPen
;
120 void wxSplitterWindow::SetResizeCursor()
122 SetCursor(m_splitMode
== wxSPLIT_VERTICAL
? m_sashCursorWE
126 void wxSplitterWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
133 void wxSplitterWindow::OnInternalIdle()
135 wxWindow::OnInternalIdle();
141 void wxSplitterWindow::OnMouseEvent(wxMouseEvent
& event
)
143 int x
= (int)event
.GetX(),
144 y
= (int)event
.GetY();
146 if (GetWindowStyle() & wxSP_NOSASH
)
149 // with wxSP_LIVE_UPDATE style the splitter windows are always resized
150 // following the mouse movement while it drags the sash, without it we only
151 // draw the sash at the new position but only resize the windows when the
152 // dragging is finished
153 bool isLive
= (GetWindowStyleFlag() & wxSP_LIVE_UPDATE
) != 0;
155 if (event
.LeftDown())
157 if ( SashHitTest(x
, y
) )
159 // Start the drag now
160 m_dragMode
= wxSPLIT_DRAG_DRAGGING
;
162 // Capture mouse and set the cursor
168 // remember the initial sash position and draw the initial
170 m_sashPositionCurrent
= m_sashPosition
;
172 DrawSashTracker(x
, y
);
182 else if (event
.LeftUp() && m_dragMode
== wxSPLIT_DRAG_DRAGGING
)
184 // We can stop dragging now and see what we've got.
185 m_dragMode
= wxSPLIT_DRAG_NONE
;
187 // Release mouse and unset the cursor
189 SetCursor(* wxSTANDARD_CURSOR
);
191 // exit if unsplit after doubleclick
200 DrawSashTracker(m_oldX
, m_oldY
);
203 // the position of the click doesn't exactly correspond to
204 // m_sashPosition, rather it changes it by the distance by which the
206 int diff
= m_splitMode
== wxSPLIT_VERTICAL
? x
- m_oldX
: y
- m_oldY
;
208 int posSashOld
= isLive
? m_sashPosition
: m_sashPositionCurrent
;
209 int posSashNew
= OnSashPositionChanging(posSashOld
+ diff
);
210 if ( posSashNew
== -1 )
212 // change not allowed
216 if ( m_permitUnsplitAlways
|| m_minimumPaneSize
== 0 )
218 // Deal with possible unsplit scenarios
219 if ( posSashNew
== 0 )
221 // We remove the first window from the view
222 wxWindow
*removedWindow
= m_windowOne
;
223 m_windowOne
= m_windowTwo
;
224 m_windowTwo
= (wxWindow
*) NULL
;
225 OnUnsplit(removedWindow
);
226 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_UNSPLIT
, this);
227 event
.m_data
.win
= removedWindow
;
228 (void)DoSendEvent(event
);
229 SetSashPositionAndNotify(0);
231 else if ( posSashNew
== GetWindowSize() )
233 // We remove the second window from the view
234 wxWindow
*removedWindow
= m_windowTwo
;
235 m_windowTwo
= (wxWindow
*) NULL
;
236 OnUnsplit(removedWindow
);
237 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_UNSPLIT
, this);
238 event
.m_data
.win
= removedWindow
;
239 (void)DoSendEvent(event
);
240 SetSashPositionAndNotify(0);
244 SetSashPositionAndNotify(posSashNew
);
249 SetSashPositionAndNotify(posSashNew
);
253 } // left up && dragging
254 else if ((event
.Moving() || event
.Leaving() || event
.Entering()) && (m_dragMode
== wxSPLIT_DRAG_NONE
))
256 // Just change the cursor as required
257 if ( !event
.Leaving() && SashHitTest(x
, y
) )
263 SetCursor(* wxSTANDARD_CURSOR
);
266 else if (event
.Dragging() && (m_dragMode
== wxSPLIT_DRAG_DRAGGING
))
268 int diff
= m_splitMode
== wxSPLIT_VERTICAL
? x
- m_oldX
: y
- m_oldY
;
271 // nothing to do, mouse didn't really move far enough
275 int posSashOld
= isLive
? m_sashPosition
: m_sashPositionCurrent
;
276 int posSashNew
= OnSashPositionChanging(posSashOld
+ diff
);
277 if ( posSashNew
== -1 )
279 // change not allowed
283 if ( posSashNew
== m_sashPosition
)
289 DrawSashTracker(m_oldX
, m_oldY
);
292 if (m_splitMode
== wxSPLIT_VERTICAL
)
297 // Remember old positions
302 // As we captured the mouse, we may get the mouse events from outside
303 // our window - for example, negative values in x, y. This has a weird
304 // consequence under MSW where we use unsigned values sometimes and
305 // signed ones other times: the coordinates turn as big positive
306 // numbers and so the sash is drawn on the *right* side of the window
307 // instead of the left (or bottom instead of top). Correct this.
308 if ( (short)m_oldX
< 0 )
310 if ( (short)m_oldY
< 0 )
317 m_sashPositionCurrent
= posSashNew
;
319 DrawSashTracker(m_oldX
, m_oldY
);
323 SetSashPositionAndNotify(posSashNew
);
324 m_needUpdating
= TRUE
;
327 else if ( event
.LeftDClick() && m_windowTwo
)
329 OnDoubleClickSash(x
, y
);
333 void wxSplitterWindow::OnSize(wxSizeEvent
& event
)
335 // only process this message if we're not iconized - otherwise iconizing
336 // and restoring a window containing the splitter has a funny side effect
337 // of changing the splitter position!
338 wxWindow
*parent
= GetParent();
339 while ( parent
&& !parent
->IsTopLevel() )
341 parent
= parent
->GetParent();
344 bool iconized
= FALSE
;
346 wxTopLevelWindow
*winTop
= wxDynamicCast(parent
, wxTopLevelWindow
);
349 iconized
= winTop
->IsIconized();
353 wxFAIL_MSG(wxT("should have a top level parent!"));
368 GetClientSize(&w
, &h
);
370 int size
= m_splitMode
== wxSPLIT_VERTICAL
? w
: h
;
371 if ( m_sashPosition
>= size
- 5 )
372 SetSashPositionAndNotify(wxMax(10, size
- 40));
378 bool wxSplitterWindow::SashHitTest(int x
, int y
, int tolerance
)
380 if ( m_windowTwo
== NULL
|| m_sashPosition
== 0)
381 return FALSE
; // No sash
383 int z
= m_splitMode
== wxSPLIT_VERTICAL
? x
: y
;
385 return z
>= m_sashPosition
- tolerance
&& z
<= m_sashPosition
+ 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 wxMirrorDC
dcMirror(dc
, m_splitMode
!= wxSPLIT_VERTICAL
);
417 wxRendererNative::Get().DrawSplitterSash
421 dcMirror
.Reflect(GetClientSize()),
426 // Draw the sash tracker (for whilst moving the sash)
427 void wxSplitterWindow::DrawSashTracker(int x
, int y
)
430 GetClientSize(&w
, &h
);
436 if ( m_splitMode
== wxSPLIT_VERTICAL
)
467 ClientToScreen(&x1
, &y1
);
468 ClientToScreen(&x2
, &y2
);
470 screenDC
.SetLogicalFunction(wxINVERT
);
471 screenDC
.SetPen(*m_sashTrackerPen
);
472 screenDC
.SetBrush(*wxTRANSPARENT_BRUSH
);
474 screenDC
.DrawLine(x1
, y1
, x2
, y2
);
476 screenDC
.SetLogicalFunction(wxCOPY
);
479 int wxSplitterWindow::GetWindowSize() const
481 wxSize size
= GetClientSize();
483 return m_splitMode
== wxSPLIT_VERTICAL
? size
.x
: size
.y
;
486 int wxSplitterWindow::AdjustSashPosition(int sashPos
) const
488 int window_size
= GetWindowSize();
495 // the window shouldn't be smaller than its own minimal size nor
496 // smaller than the minimual pane size specified for this splitter
497 int minSize
= m_splitMode
== wxSPLIT_VERTICAL
? win
->GetMinWidth()
498 : win
->GetMinHeight();
500 if ( minSize
== -1 || m_minimumPaneSize
> minSize
)
501 minSize
= m_minimumPaneSize
;
503 minSize
+= GetBorderSize();
505 if ( sashPos
< minSize
)
512 int minSize
= m_splitMode
== wxSPLIT_VERTICAL
? win
->GetMinWidth()
513 : win
->GetMinHeight();
515 if ( minSize
== -1 || m_minimumPaneSize
> minSize
)
516 minSize
= m_minimumPaneSize
;
518 int maxSize
= window_size
- minSize
- GetBorderSize();
519 if ( sashPos
> maxSize
)
526 bool wxSplitterWindow::DoSetSashPosition(int sashPos
)
528 int newSashPosition
= AdjustSashPosition(sashPos
);
530 if ( newSashPosition
== m_sashPosition
)
533 m_sashPosition
= newSashPosition
;
538 void wxSplitterWindow::SetSashPositionAndNotify(int sashPos
)
540 if ( DoSetSashPosition(sashPos
) )
542 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED
, this);
543 event
.m_data
.pos
= m_sashPosition
;
545 (void)DoSendEvent(event
);
549 // Position and size subwindows.
550 // Note that the border size applies to each subwindow, not
551 // including the edges next to the sash.
552 void wxSplitterWindow::SizeWindows()
554 // check if we have delayed setting the real sash position
555 if ( m_requestedSashPosition
!= INT_MAX
)
557 int newSashPosition
= ConvertSashPosition(m_requestedSashPosition
);
558 if ( newSashPosition
!= m_sashPosition
)
560 DoSetSashPosition(newSashPosition
);
563 if ( newSashPosition
<= m_sashPosition
564 && newSashPosition
>= m_sashPosition
- GetBorderSize() )
566 // don't update it any more
567 m_requestedSashPosition
= INT_MAX
;
572 GetClientSize(&w
, &h
);
574 if ( GetWindow1() && !GetWindow2() )
576 GetWindow1()->SetSize(GetBorderSize(), GetBorderSize(),
577 w
- 2*GetBorderSize(), h
- 2*GetBorderSize());
579 else if ( GetWindow1() && GetWindow2() )
581 const int border
= GetBorderSize(),
582 sash
= GetSashSize();
584 int size1
= GetSashPosition() - border
,
585 size2
= GetSashPosition() + sash
;
587 int x2
, y2
, w1
, h1
, w2
, h2
;
588 if ( GetSplitMode() == wxSPLIT_VERTICAL
)
591 w2
= w
- 2*border
- sash
- w1
;
597 else // horz splitter
602 h2
= h
- 2*border
- sash
- h1
;
607 GetWindow1()->SetSize(border
, border
, w1
, h1
);
608 GetWindow2()->SetSize(x2
, y2
, w2
, h2
);
614 SetNeedUpdating(FALSE
);
617 // Set pane for unsplit window
618 void wxSplitterWindow::Initialize(wxWindow
*window
)
620 wxASSERT_MSG( window
&& window
->GetParent() == this,
621 _T("windows in the splitter should have it as parent!") );
623 m_windowOne
= window
;
624 m_windowTwo
= (wxWindow
*) NULL
;
625 DoSetSashPosition(0);
628 // Associates the given window with window 2, drawing the appropriate sash
629 // and changing the split mode.
630 // Does nothing and returns FALSE if the window is already split.
631 bool wxSplitterWindow::DoSplit(wxSplitMode mode
,
632 wxWindow
*window1
, wxWindow
*window2
,
638 wxCHECK_MSG( window1
&& window2
, FALSE
,
639 _T("can not split with NULL window(s)") );
641 wxCHECK_MSG( window1
->GetParent() == this && window2
->GetParent() == this, FALSE
,
642 _T("windows in the splitter should have it as parent!") );
645 m_windowOne
= window1
;
646 m_windowTwo
= window2
;
648 // remember the sash position we want to set for later if we can't set it
649 // right now (e.g. because the window is too small)
650 m_requestedSashPosition
= sashPosition
;
652 DoSetSashPosition(ConvertSashPosition(sashPosition
));
659 int wxSplitterWindow::ConvertSashPosition(int sashPosition
) const
661 if ( sashPosition
> 0 )
665 else if ( sashPosition
< 0 )
667 // It's negative so adding is subtracting
668 return GetWindowSize() + sashPosition
;
670 else // sashPosition == 0
672 // default, put it in the centre
673 return GetWindowSize() / 2;
677 // Remove the specified (or second) window from the view
678 // Doesn't actually delete the window.
679 bool wxSplitterWindow::Unsplit(wxWindow
*toRemove
)
684 wxWindow
*win
= NULL
;
685 if ( toRemove
== NULL
|| toRemove
== m_windowTwo
)
688 m_windowTwo
= (wxWindow
*) NULL
;
690 else if ( toRemove
== m_windowOne
)
693 m_windowOne
= m_windowTwo
;
694 m_windowTwo
= (wxWindow
*) NULL
;
698 wxFAIL_MSG(wxT("splitter: attempt to remove a non-existent window"));
704 DoSetSashPosition(0);
710 // Replace a window with another one
711 bool wxSplitterWindow::ReplaceWindow(wxWindow
*winOld
, wxWindow
*winNew
)
713 wxCHECK_MSG( winOld
, FALSE
, wxT("use one of Split() functions instead") );
714 wxCHECK_MSG( winNew
, FALSE
, wxT("use Unsplit() functions instead") );
716 if ( winOld
== m_windowTwo
)
718 m_windowTwo
= winNew
;
720 else if ( winOld
== m_windowOne
)
722 m_windowOne
= winNew
;
726 wxFAIL_MSG(wxT("splitter: attempt to replace a non-existent window"));
736 void wxSplitterWindow::SetMinimumPaneSize(int min
)
738 m_minimumPaneSize
= min
;
739 SetSashPosition(m_sashPosition
); // re-check limits
742 void wxSplitterWindow::SetSashPosition(int position
, bool redraw
)
744 DoSetSashPosition(position
);
752 bool wxSplitterWindow::DoSendEvent(wxSplitterEvent
& event
)
754 return !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed();
757 // ---------------------------------------------------------------------------
758 // wxSplitterWindow virtual functions: they now just generate the events
759 // ---------------------------------------------------------------------------
761 bool wxSplitterWindow::OnSashPositionChange(int WXUNUSED(newSashPosition
))
763 // always allow by default
767 int wxSplitterWindow::OnSashPositionChanging(int newSashPosition
)
769 // If within UNSPLIT_THRESHOLD from edge, set to edge to cause closure.
770 const int UNSPLIT_THRESHOLD
= 4;
772 // first of all, check if OnSashPositionChange() doesn't forbid this change
773 if ( !OnSashPositionChange(newSashPosition
) )
779 // Obtain relevant window dimension for bottom / right threshold check
780 int window_size
= GetWindowSize();
782 bool unsplit_scenario
= FALSE
;
783 if ( m_permitUnsplitAlways
|| m_minimumPaneSize
== 0 )
785 // Do edge detection if unsplit premitted
786 if ( newSashPosition
<= UNSPLIT_THRESHOLD
)
788 // threshold top / left check
790 unsplit_scenario
= TRUE
;
792 if ( newSashPosition
>= window_size
- UNSPLIT_THRESHOLD
)
794 // threshold bottom/right check
795 newSashPosition
= window_size
;
796 unsplit_scenario
= TRUE
;
800 if ( !unsplit_scenario
)
802 // If resultant pane would be too small, enlarge it
803 newSashPosition
= AdjustSashPosition(newSashPosition
);
806 // If the result is out of bounds it means minimum size is too big,
807 // so split window in half as best compromise.
808 if ( newSashPosition
< 0 || newSashPosition
> window_size
)
809 newSashPosition
= window_size
/ 2;
811 // now let the event handler have it
813 // FIXME: shouldn't we do it before the adjustments above so as to ensure
814 // that the sash position is always reasonable?
815 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING
, this);
816 event
.m_data
.pos
= newSashPosition
;
818 if ( !DoSendEvent(event
) )
820 // the event handler vetoed the change
821 newSashPosition
= -1;
825 // it could have been changed by it
826 newSashPosition
= event
.GetSashPosition();
829 return newSashPosition
;
832 // Called when the sash is double-clicked. The default behaviour is to remove
833 // the sash if the minimum pane size is zero.
834 void wxSplitterWindow::OnDoubleClickSash(int x
, int y
)
836 wxCHECK_RET(m_windowTwo
, wxT("splitter: no window to remove"));
838 // new code should handle events instead of using the virtual functions
839 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_DOUBLECLICKED
, this);
840 event
.m_data
.pt
.x
= x
;
841 event
.m_data
.pt
.y
= y
;
842 if ( DoSendEvent(event
) )
844 if ( GetMinimumPaneSize() == 0 || m_permitUnsplitAlways
)
846 wxWindow
* win
= m_windowTwo
;
849 wxSplitterEvent
unsplitEvent(wxEVT_COMMAND_SPLITTER_UNSPLIT
, this);
850 unsplitEvent
.m_data
.win
= win
;
851 (void)DoSendEvent(unsplitEvent
);
855 //else: blocked by user
858 void wxSplitterWindow::OnUnsplit(wxWindow
*winRemoved
)
860 // call this before calling the event handler which may delete the window
861 winRemoved
->Show(FALSE
);
864 #if defined( __WXMSW__ ) || defined( __WXMAC__)
866 // this is currently called (and needed) under MSW only...
867 void wxSplitterWindow::OnSetCursor(wxSetCursorEvent
& event
)
869 // if we don't do it, the resizing cursor might be set for child window:
870 // and like this we explicitly say that our cursor should not be used for
871 // children windows which overlap us
873 if ( SashHitTest(event
.GetX(), event
.GetY()) )
875 // default processing is ok
878 //else: do nothing, in particular, don't call Skip()
881 #endif // wxMSW || wxMac
883 #endif // wxUSE_SPLITTER