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"
25 #include "wx/string.h"
29 #include "wx/dcscreen.h"
31 #include "wx/window.h"
32 #include "wx/dialog.h"
35 #include "wx/settings.h"
38 #include "wx/splitter.h"
42 DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED
)
43 DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING
)
44 DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPLITTER_DOUBLECLICKED
)
45 DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPLITTER_UNSPLIT
)
47 IMPLEMENT_DYNAMIC_CLASS(wxSplitterWindow
, wxWindow
)
48 IMPLEMENT_DYNAMIC_CLASS(wxSplitterEvent
, wxNotifyEvent
)
50 BEGIN_EVENT_TABLE(wxSplitterWindow
, wxWindow
)
51 EVT_PAINT(wxSplitterWindow::OnPaint
)
52 EVT_SIZE(wxSplitterWindow::OnSize
)
53 EVT_IDLE(wxSplitterWindow::OnIdle
)
54 EVT_MOUSE_EVENTS(wxSplitterWindow::OnMouseEvent
)
56 #if defined( __WXMSW__ ) || defined( __WXMAC__)
57 EVT_SET_CURSOR(wxSplitterWindow::OnSetCursor
)
60 WX_EVENT_TABLE_CONTROL_CONTAINER(wxSplitterWindow
)
63 WX_DELEGATE_TO_CONTROL_CONTAINER(wxSplitterWindow
);
65 bool wxSplitterWindow::Create(wxWindow
*parent
, wxWindowID id
,
71 // allow TABbing from one window to the other
72 style
|= wxTAB_TRAVERSAL
;
74 if (!wxWindow::Create(parent
, id
, pos
, size
, style
, name
))
77 m_permitUnsplitAlways
= (style
& wxSP_PERMIT_UNSPLIT
) != 0;
79 if ( style
& wxSP_3DSASH
)
84 if ( style
& wxSP_3DBORDER
)
86 else if ( style
& wxSP_BORDER
)
93 wxGetOsVersion( &major
, &minor
);
95 m_windowStyle
|= wxSP_SASH_AQUA
;
101 void wxSplitterWindow::Init()
103 m_container
.SetContainerWindow(this);
105 m_splitMode
= wxSPLIT_VERTICAL
;
106 m_permitUnsplitAlways
= TRUE
;
107 m_windowOne
= (wxWindow
*) NULL
;
108 m_windowTwo
= (wxWindow
*) NULL
;
109 m_dragMode
= wxSPLIT_DRAG_NONE
;
116 m_sashPosition
= m_requestedSashPosition
= 0;
117 m_minimumPaneSize
= 0;
118 m_sashCursorWE
= wxCursor(wxCURSOR_SIZEWE
);
119 m_sashCursorNS
= wxCursor(wxCURSOR_SIZENS
);
120 m_sashTrackerPen
= new wxPen(*wxBLACK
, 2, wxSOLID
);
121 m_lightShadowPen
= (wxPen
*) NULL
;
122 m_mediumShadowPen
= (wxPen
*) NULL
;
123 m_darkShadowPen
= (wxPen
*) NULL
;
124 m_faceBrush
= (wxBrush
*) NULL
;
125 m_facePen
= (wxPen
*) NULL
;
126 m_hilightPen
= (wxPen
*) NULL
;
130 m_needUpdating
= FALSE
;
133 wxSplitterWindow::~wxSplitterWindow()
135 delete m_sashTrackerPen
;
136 delete m_lightShadowPen
;
137 delete m_darkShadowPen
;
138 delete m_mediumShadowPen
;
144 void wxSplitterWindow::SetResizeCursor()
146 SetCursor(m_splitMode
== wxSPLIT_VERTICAL
? m_sashCursorWE
150 void wxSplitterWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
154 if ( m_borderSize
> 0 )
159 void wxSplitterWindow::OnIdle(wxIdleEvent
& event
)
167 void wxSplitterWindow::OnMouseEvent(wxMouseEvent
& event
)
169 int x
= (int)event
.GetX(),
170 y
= (int)event
.GetY();
172 #if defined(__WXMSW__)
173 // SetCursor(wxCursor()); // Is this required?
176 if (GetWindowStyle() & wxSP_NOSASH
)
179 // with wxSP_LIVE_UPDATE style the splitter windows are always resized
180 // following the mouse movement while it drags the sash, without it we only
181 // draw the sash at the new position but only resize the windows when the
182 // dragging is finished
183 bool isLive
= (GetWindowStyleFlag() & wxSP_LIVE_UPDATE
) != 0;
185 if (event
.LeftDown())
187 if ( SashHitTest(x
, y
) )
189 // Start the drag now
190 m_dragMode
= wxSPLIT_DRAG_DRAGGING
;
192 // Capture mouse and set the cursor
198 // remember the initial sash position and draw the initial
200 m_sashPositionCurrent
= m_sashPosition
;
202 DrawSashTracker(x
, y
);
212 else if (event
.LeftUp() && m_dragMode
== wxSPLIT_DRAG_DRAGGING
)
214 // We can stop dragging now and see what we've got.
215 m_dragMode
= wxSPLIT_DRAG_NONE
;
217 // Release mouse and unset the cursor
219 SetCursor(* wxSTANDARD_CURSOR
);
221 // exit if unsplit after doubleclick
230 DrawSashTracker(m_oldX
, m_oldY
);
233 // the position of the click doesn't exactly correspond to
234 // m_sashPosition, rather it changes it by the distance by which the
236 int diff
= m_splitMode
== wxSPLIT_VERTICAL
? x
- m_oldX
: y
- m_oldY
;
238 int posSashOld
= isLive
? m_sashPosition
: m_sashPositionCurrent
;
239 int posSashNew
= OnSashPositionChanging(posSashOld
+ diff
);
240 if ( posSashNew
== -1 )
242 // change not allowed
246 if ( m_permitUnsplitAlways
|| m_minimumPaneSize
== 0 )
248 // Deal with possible unsplit scenarios
249 if ( posSashNew
== 0 )
251 // We remove the first window from the view
252 wxWindow
*removedWindow
= m_windowOne
;
253 m_windowOne
= m_windowTwo
;
254 m_windowTwo
= (wxWindow
*) NULL
;
255 OnUnsplit(removedWindow
);
256 SetSashPositionAndNotify(0);
258 else if ( posSashNew
== GetWindowSize() )
260 // We remove the second window from the view
261 wxWindow
*removedWindow
= m_windowTwo
;
262 m_windowTwo
= (wxWindow
*) NULL
;
263 OnUnsplit(removedWindow
);
264 SetSashPositionAndNotify(0);
268 SetSashPositionAndNotify(posSashNew
);
273 SetSashPositionAndNotify(posSashNew
);
277 } // left up && dragging
278 else if (event
.Moving() && !event
.Dragging())
280 // Just change the cursor as required
281 if ( SashHitTest(x
, y
) )
284 SetCursor(* wxSTANDARD_CURSOR
);
286 else if (event
.Dragging() && (m_dragMode
== wxSPLIT_DRAG_DRAGGING
))
288 int diff
= m_splitMode
== wxSPLIT_VERTICAL
? x
- m_oldX
: y
- m_oldY
;
291 // nothing to do, mouse didn't really move far enough
295 int posSashOld
= isLive
? m_sashPosition
: m_sashPositionCurrent
;
296 int posSashNew
= OnSashPositionChanging(posSashOld
+ diff
);
297 if ( posSashNew
== -1 )
299 // change not allowed
303 if ( posSashNew
== m_sashPosition
)
309 DrawSashTracker(m_oldX
, m_oldY
);
312 if (m_splitMode
== wxSPLIT_VERTICAL
)
317 // Remember old positions
322 // As we captured the mouse, we may get the mouse events from outside
323 // our window - for example, negative values in x, y. This has a weird
324 // consequence under MSW where we use unsigned values sometimes and
325 // signed ones other times: the coordinates turn as big positive
326 // numbers and so the sash is drawn on the *right* side of the window
327 // instead of the left (or bottom instead of top). Correct this.
328 if ( (short)m_oldX
< 0 )
330 if ( (short)m_oldY
< 0 )
337 m_sashPositionCurrent
= posSashNew
;
339 DrawSashTracker(m_oldX
, m_oldY
);
343 SetSashPositionAndNotify(posSashNew
);
344 m_needUpdating
= TRUE
;
347 else if ( event
.LeftDClick() && m_windowTwo
)
349 OnDoubleClickSash(x
, y
);
353 void wxSplitterWindow::OnSize(wxSizeEvent
& event
)
355 // only process this message if we're not iconized - otherwise iconizing
356 // and restoring a window containing the splitter has a funny side effect
357 // of changing the splitter position!
358 wxWindow
*parent
= GetParent();
359 while ( parent
&& !parent
->IsTopLevel() )
361 parent
= parent
->GetParent();
364 bool iconized
= FALSE
;
366 // wxMotif doesn't yet have a wxTopLevelWindow implementation
368 wxFrame
*winTop
= wxDynamicCast(parent
, wxFrame
);
370 wxTopLevelWindow
*winTop
= wxDynamicCast(parent
, wxTopLevelWindow
);
374 iconized
= winTop
->IsIconized();
379 wxFAIL_MSG(wxT("should have a top level parent!"));
393 GetClientSize( &cw
, &ch
);
396 if ( m_splitMode
== wxSPLIT_VERTICAL
)
398 if ( m_sashPosition
>= (cw
- 5) )
399 SetSashPositionAndNotify(wxMax(10, cw
- 40));
401 else // m_splitMode == wxSPLIT_HORIZONTAL
403 if ( m_sashPosition
>= (ch
- 5) )
404 SetSashPositionAndNotify(wxMax(10, ch
- 40));
411 bool wxSplitterWindow::SashHitTest(int x
, int y
, int tolerance
)
413 if ( m_windowTwo
== NULL
|| m_sashPosition
== 0)
414 return FALSE
; // No sash
416 if ( m_splitMode
== wxSPLIT_VERTICAL
)
418 if ( (x
>= m_sashPosition
- tolerance
) && (x
<= m_sashPosition
+ m_sashSize
+ tolerance
) )
425 if ( (y
>= (m_sashPosition
- tolerance
)) && (y
<= (m_sashPosition
+ m_sashSize
+ tolerance
)) )
432 // Draw 3D effect borders
433 void wxSplitterWindow::DrawBorders(wxDC
& dc
)
436 GetClientSize(&w
, &h
);
438 if ( GetWindowStyleFlag() & wxSP_3DBORDER
)
441 dc
.SetPen(*m_facePen
);
442 dc
.SetBrush(*m_faceBrush
);
443 dc
.DrawRectangle(1, 1 , w
-1, m_borderSize
-2 ); //high
444 dc
.DrawRectangle(1, m_borderSize
-2 , m_borderSize
-2, h
-1 ); // left
445 dc
.DrawRectangle(w
-m_borderSize
+2, m_borderSize
-2 , w
-1, h
-1 ); // right
446 dc
.DrawRectangle(m_borderSize
-2, h
-m_borderSize
+2 , w
-m_borderSize
+2, h
-1 ); //bottom
448 dc
.SetPen(*m_mediumShadowPen
);
449 dc
.DrawLine(m_borderSize
-2, m_borderSize
-2, w
-m_borderSize
+1, m_borderSize
-2);
450 dc
.DrawLine(m_borderSize
-2, m_borderSize
-2, m_borderSize
-2, h
-m_borderSize
+1);
452 dc
.SetPen(*m_darkShadowPen
);
453 dc
.DrawLine(m_borderSize
-1, m_borderSize
-1, w
-m_borderSize
, m_borderSize
-1);
454 dc
.DrawLine(m_borderSize
-1, m_borderSize
-1, m_borderSize
-1, h
-m_borderSize
);
456 dc
.SetPen(*m_hilightPen
);
457 dc
.DrawLine(m_borderSize
- 2, h
-m_borderSize
+1, w
-m_borderSize
+1, h
-m_borderSize
+1);
458 dc
.DrawLine(w
-m_borderSize
+1, m_borderSize
- 2, w
-m_borderSize
+1, h
-m_borderSize
+2); // Surely the maximum y pos. should be h - 1.
459 /// Anyway, h is required for MSW.
461 dc
.SetPen(*m_lightShadowPen
);
462 dc
.DrawLine(w
-m_borderSize
, m_borderSize
-1, w
-m_borderSize
, h
-m_borderSize
); // Right hand side
463 dc
.DrawLine(m_borderSize
-1, h
-m_borderSize
, w
-m_borderSize
+1, h
-m_borderSize
); // Bottom
465 else if ( GetWindowStyleFlag() & wxSP_BORDER
)
467 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
468 dc
.SetPen(*wxBLACK_PEN
);
469 dc
.DrawRectangle(0, 0, w
-1, h
-1);
472 dc
.SetPen(wxNullPen
);
473 dc
.SetBrush(wxNullBrush
);
477 void wxSplitterWindow::DrawSash(wxDC
& dc
)
479 if ( m_sashPosition
== 0 || !m_windowTwo
)
481 if (GetWindowStyle() & wxSP_NOSASH
)
485 GetClientSize(&w
, &h
);
487 if ( GetWindowStyleFlag() & wxSP_3DSASH
)
489 if ( m_splitMode
== wxSPLIT_VERTICAL
)
491 dc
.SetPen(*m_facePen
);
493 if (HasFlag( wxSP_SASH_AQUA
))
494 dc
.SetBrush(*wxWHITE_BRUSH
);
496 dc
.SetBrush(*m_faceBrush
);
497 dc
.DrawRectangle(m_sashPosition
+ 2, 0 , m_sashSize
- 4, h
);
499 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
501 dc
.SetPen(*m_lightShadowPen
);
502 int xShadow
= m_borderSize
? m_borderSize
- 1 : 0 ;
503 dc
.DrawLine(m_sashPosition
, xShadow
, m_sashPosition
, h
-m_borderSize
);
505 dc
.SetPen(*m_hilightPen
);
506 dc
.DrawLine(m_sashPosition
+1, m_borderSize
- 2, m_sashPosition
+1, h
- m_borderSize
+2);
508 if (!HasFlag( wxSP_SASH_AQUA
))
509 dc
.SetPen(*m_mediumShadowPen
);
511 int yMedium
= m_borderSize
? h
-m_borderSize
+1 : h
;
512 dc
.DrawLine(m_sashPosition
+m_sashSize
-2, xShadow
, m_sashPosition
+m_sashSize
-2, yMedium
);
514 if (HasFlag( wxSP_SASH_AQUA
))
515 dc
.SetPen(*m_lightShadowPen
);
517 dc
.SetPen(*m_darkShadowPen
);
518 dc
.DrawLine(m_sashPosition
+m_sashSize
-1, m_borderSize
, m_sashPosition
+m_sashSize
-1, h
-m_borderSize
);
520 // Draw the top and bottom edges of the sash, if requested
521 if (GetWindowStyle() & wxSP_FULLSASH
)
524 dc
.SetPen(*m_hilightPen
);
525 dc
.DrawLine(m_sashPosition
+1, m_borderSize
, m_sashPosition
+m_sashSize
-1, m_borderSize
);
528 dc
.SetPen(*m_darkShadowPen
);
529 dc
.DrawLine(m_sashPosition
+1, h
-m_borderSize
-1, m_sashPosition
+m_sashSize
-1, h
-m_borderSize
-1);
532 else // wxSPLIT_HORIZONTAL
534 dc
.SetPen(*m_facePen
);
535 if (HasFlag( wxSP_SASH_AQUA
))
536 dc
.SetBrush(*wxWHITE_BRUSH
);
538 dc
.SetBrush(*m_faceBrush
);
539 dc
.DrawRectangle( m_borderSize
-2, m_sashPosition
+ 2, w
-m_borderSize
+2, m_sashSize
- 4);
541 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
543 dc
.SetPen(*m_lightShadowPen
);
544 dc
.DrawLine(m_borderSize
-1, m_sashPosition
, w
-m_borderSize
, m_sashPosition
);
546 dc
.SetPen(*m_hilightPen
);
547 dc
.DrawLine(m_borderSize
-2, m_sashPosition
+1, w
-m_borderSize
+1, m_sashPosition
+1);
549 if (!HasFlag( wxSP_SASH_AQUA
))
550 dc
.SetPen(*m_mediumShadowPen
);
551 dc
.DrawLine(m_borderSize
-1, m_sashPosition
+m_sashSize
-2, w
-m_borderSize
+1, m_sashPosition
+m_sashSize
-2);
553 if (HasFlag( wxSP_SASH_AQUA
))
554 dc
.SetPen(*m_lightShadowPen
);
556 dc
.SetPen(*m_darkShadowPen
);
557 dc
.DrawLine(m_borderSize
, m_sashPosition
+m_sashSize
-1, w
-m_borderSize
, m_sashPosition
+m_sashSize
-1);
559 // Draw the left and right edges of the sash, if requested
560 if (GetWindowStyle() & wxSP_FULLSASH
)
563 dc
.SetPen(*m_hilightPen
);
564 dc
.DrawLine(m_borderSize
, m_sashPosition
, m_borderSize
, m_sashPosition
+m_sashSize
);
567 dc
.SetPen(*m_darkShadowPen
);
568 dc
.DrawLine(w
-m_borderSize
-1, m_sashPosition
+1, w
-m_borderSize
-1, m_sashPosition
+m_sashSize
-1);
574 dc
.SetPen(*wxTRANSPARENT_PEN
);
575 dc
.SetBrush(*m_faceBrush
);
576 if ( m_splitMode
== wxSPLIT_VERTICAL
)
580 if ( (GetWindowStyleFlag() & wxSP_BORDER
) != wxSP_BORDER
&& (GetWindowStyleFlag() & wxSP_3DBORDER
) != wxSP_3DBORDER
)
581 h1
+= 1; // Not sure why this is necessary...
582 if ( (GetWindowStyleFlag() & wxSP_3DBORDER
) == wxSP_3DBORDER
)
586 dc
.DrawRectangle(m_sashPosition
, y1
, m_sashSize
, h1
);
588 else // wxSPLIT_HORIZONTAL
592 if ( (GetWindowStyleFlag() & wxSP_BORDER
) != wxSP_BORDER
&& (GetWindowStyleFlag() & wxSP_3DBORDER
) != wxSP_3DBORDER
)
594 if ( (GetWindowStyleFlag() & wxSP_3DBORDER
) == wxSP_3DBORDER
)
598 dc
.DrawRectangle(x1
, m_sashPosition
, w1
, m_sashSize
);
602 dc
.SetPen(wxNullPen
);
603 dc
.SetBrush(wxNullBrush
);
606 // Draw the sash tracker (for whilst moving the sash)
607 void wxSplitterWindow::DrawSashTracker(int x
, int y
)
610 GetClientSize(&w
, &h
);
616 if ( m_splitMode
== wxSPLIT_VERTICAL
)
647 ClientToScreen(&x1
, &y1
);
648 ClientToScreen(&x2
, &y2
);
650 screenDC
.SetLogicalFunction(wxINVERT
);
651 screenDC
.SetPen(*m_sashTrackerPen
);
652 screenDC
.SetBrush(*wxTRANSPARENT_BRUSH
);
654 screenDC
.DrawLine(x1
, y1
, x2
, y2
);
656 screenDC
.SetLogicalFunction(wxCOPY
);
658 screenDC
.SetPen(wxNullPen
);
659 screenDC
.SetBrush(wxNullBrush
);
662 int wxSplitterWindow::GetWindowSize() const
664 wxSize size
= GetClientSize();
666 return m_splitMode
== wxSPLIT_VERTICAL
? size
.x
: size
.y
;
669 int wxSplitterWindow::AdjustSashPosition(int sashPos
) const
671 int window_size
= GetWindowSize();
678 // the window shouldn't be smaller than its own minimal size nor
679 // smaller than the minimual pane size specified for this splitter
680 int minSize
= m_splitMode
== wxSPLIT_VERTICAL
? win
->GetMinWidth()
681 : win
->GetMinHeight();
683 if ( minSize
== -1 || m_minimumPaneSize
> minSize
)
684 minSize
= m_minimumPaneSize
;
686 minSize
+= GetBorderSize();
688 if ( sashPos
< minSize
)
695 int minSize
= m_splitMode
== wxSPLIT_VERTICAL
? win
->GetMinWidth()
696 : win
->GetMinHeight();
698 if ( minSize
== -1 || m_minimumPaneSize
> minSize
)
699 minSize
= m_minimumPaneSize
;
701 int maxSize
= window_size
- minSize
- GetBorderSize();
702 if ( sashPos
> maxSize
)
709 bool wxSplitterWindow::DoSetSashPosition(int sashPos
)
711 int newSashPosition
= AdjustSashPosition(sashPos
);
713 if ( newSashPosition
== m_sashPosition
)
716 m_sashPosition
= newSashPosition
;
721 void wxSplitterWindow::SetSashPositionAndNotify(int sashPos
)
723 if ( DoSetSashPosition(sashPos
) )
725 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED
, this);
726 event
.m_data
.pos
= m_sashPosition
;
728 (void)DoSendEvent(event
);
732 // Position and size subwindows.
733 // Note that the border size applies to each subwindow, not
734 // including the edges next to the sash.
735 void wxSplitterWindow::SizeWindows()
737 // check if we have delayed setting the real sash position
738 if ( m_requestedSashPosition
!= INT_MAX
)
740 int newSashPosition
= ConvertSashPosition(m_requestedSashPosition
);
741 if ( newSashPosition
!= m_sashPosition
)
743 DoSetSashPosition(newSashPosition
);
746 if ( newSashPosition
<= m_sashPosition
747 && newSashPosition
>= m_sashPosition
- GetBorderSize() )
749 // don't update it any more
750 m_requestedSashPosition
= INT_MAX
;
755 GetClientSize(&w
, &h
);
757 if ( GetWindow1() && !GetWindow2() )
759 GetWindow1()->SetSize(GetBorderSize(), GetBorderSize(),
760 w
- 2*GetBorderSize(), h
- 2*GetBorderSize());
762 else if ( GetWindow1() && GetWindow2() )
764 if (GetSplitMode() == wxSPLIT_VERTICAL
)
766 int x1
= GetBorderSize();
767 int y1
= GetBorderSize();
768 int w1
= GetSashPosition() - GetBorderSize();
769 int h1
= h
- 2*GetBorderSize();
771 int x2
= GetSashPosition() + GetSashSize();
772 int y2
= GetBorderSize();
773 int w2
= w
- 2*GetBorderSize() - GetSashSize() - w1
;
774 int h2
= h
- 2*GetBorderSize();
776 GetWindow1()->SetSize(x1
, y1
, w1
, h1
);
777 GetWindow2()->SetSize(x2
, y2
, w2
, h2
);
781 GetWindow1()->SetSize(GetBorderSize(), GetBorderSize(),
782 w
- 2*GetBorderSize(), GetSashPosition() - GetBorderSize());
783 GetWindow2()->SetSize(GetBorderSize(), GetSashPosition() + GetSashSize(),
784 w
- 2*GetBorderSize(), h
- 2*GetBorderSize() - GetSashSize() - (GetSashPosition() - GetBorderSize()));
788 if ( GetBorderSize() > 0 )
792 SetNeedUpdating(FALSE
);
795 // Set pane for unsplit window
796 void wxSplitterWindow::Initialize(wxWindow
*window
)
798 wxASSERT_MSG( window
&& window
->GetParent() == this,
799 _T("windows in the splitter should have it as parent!") );
801 m_windowOne
= window
;
802 m_windowTwo
= (wxWindow
*) NULL
;
803 DoSetSashPosition(0);
806 // Associates the given window with window 2, drawing the appropriate sash
807 // and changing the split mode.
808 // Does nothing and returns FALSE if the window is already split.
809 bool wxSplitterWindow::DoSplit(wxSplitMode mode
,
810 wxWindow
*window1
, wxWindow
*window2
,
816 wxCHECK_MSG( window1
&& window2
, FALSE
,
817 _T("can not split with NULL window(s)") );
819 wxCHECK_MSG( window1
->GetParent() == this && window2
->GetParent() == this, FALSE
,
820 _T("windows in the splitter should have it as parent!") );
823 m_windowOne
= window1
;
824 m_windowTwo
= window2
;
826 // remember the sash position we want to set for later if we can't set it
827 // right now (e.g. because the window is too small)
828 m_requestedSashPosition
= sashPosition
;
830 DoSetSashPosition(ConvertSashPosition(sashPosition
));
837 int wxSplitterWindow::ConvertSashPosition(int sashPosition
) const
839 if ( sashPosition
> 0 )
843 else if ( sashPosition
< 0 )
845 // It's negative so adding is subtracting
846 return GetWindowSize() + sashPosition
;
848 else // sashPosition == 0
850 // default, put it in the centre
851 return GetWindowSize() / 2;
855 // Remove the specified (or second) window from the view
856 // Doesn't actually delete the window.
857 bool wxSplitterWindow::Unsplit(wxWindow
*toRemove
)
862 wxWindow
*win
= NULL
;
863 if ( toRemove
== NULL
|| toRemove
== m_windowTwo
)
866 m_windowTwo
= (wxWindow
*) NULL
;
868 else if ( toRemove
== m_windowOne
)
871 m_windowOne
= m_windowTwo
;
872 m_windowTwo
= (wxWindow
*) NULL
;
876 wxFAIL_MSG(wxT("splitter: attempt to remove a non-existent window"));
882 DoSetSashPosition(0);
888 // Replace a window with another one
889 bool wxSplitterWindow::ReplaceWindow(wxWindow
*winOld
, wxWindow
*winNew
)
891 wxCHECK_MSG( winOld
, FALSE
, wxT("use one of Split() functions instead") );
892 wxCHECK_MSG( winNew
, FALSE
, wxT("use Unsplit() functions instead") );
894 if ( winOld
== m_windowTwo
)
896 m_windowTwo
= winNew
;
898 else if ( winOld
== m_windowOne
)
900 m_windowOne
= winNew
;
904 wxFAIL_MSG(wxT("splitter: attempt to replace a non-existent window"));
914 void wxSplitterWindow::SetMinimumPaneSize(int min
)
916 m_minimumPaneSize
= min
;
917 SetSashPosition(m_sashPosition
); // re-check limits
920 void wxSplitterWindow::SetSashPosition(int position
, bool redraw
)
922 DoSetSashPosition(position
);
930 // Initialize colours
931 void wxSplitterWindow::InitColours()
933 wxDELETE( m_facePen
);
934 wxDELETE( m_faceBrush
);
935 wxDELETE( m_mediumShadowPen
);
936 wxDELETE( m_darkShadowPen
);
937 wxDELETE( m_lightShadowPen
);
938 wxDELETE( m_hilightPen
);
942 wxColour
faceColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
943 m_facePen
= new wxPen(faceColour
, 1, wxSOLID
);
944 m_faceBrush
= new wxBrush(faceColour
, wxSOLID
);
946 wxColour
mediumShadowColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
));
947 m_mediumShadowPen
= new wxPen(mediumShadowColour
, 1, wxSOLID
);
949 wxColour
darkShadowColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
));
950 m_darkShadowPen
= new wxPen(darkShadowColour
, 1, wxSOLID
);
952 wxColour
lightShadowColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
));
953 m_lightShadowPen
= new wxPen(lightShadowColour
, 1, wxSOLID
);
955 wxColour
hilightColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DHILIGHT
));
956 m_hilightPen
= new wxPen(hilightColour
, 1, wxSOLID
);
958 m_facePen
= new wxPen("LIGHT GREY", 1, wxSOLID
);
959 m_faceBrush
= new wxBrush("LIGHT GREY", wxSOLID
);
960 m_mediumShadowPen
= new wxPen("GREY", 1, wxSOLID
);
961 m_darkShadowPen
= new wxPen("BLACK", 1, wxSOLID
);
962 m_lightShadowPen
= new wxPen("LIGHT GREY", 1, wxSOLID
);
963 m_hilightPen
= new wxPen("WHITE", 1, wxSOLID
);
967 bool wxSplitterWindow::DoSendEvent(wxSplitterEvent
& event
)
969 return !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed();
972 // ---------------------------------------------------------------------------
973 // wxSplitterWindow virtual functions: they now just generate the events
974 // ---------------------------------------------------------------------------
976 bool wxSplitterWindow::OnSashPositionChange(int WXUNUSED(newSashPosition
))
978 // always allow by default
982 int wxSplitterWindow::OnSashPositionChanging(int newSashPosition
)
984 // If within UNSPLIT_THRESHOLD from edge, set to edge to cause closure.
985 const int UNSPLIT_THRESHOLD
= 4;
987 // first of all, check if OnSashPositionChange() doesn't forbid this change
988 if ( !OnSashPositionChange(newSashPosition
) )
994 // Obtain relevant window dimension for bottom / right threshold check
995 int window_size
= GetWindowSize();
997 bool unsplit_scenario
= FALSE
;
998 if ( m_permitUnsplitAlways
|| m_minimumPaneSize
== 0 )
1000 // Do edge detection if unsplit premitted
1001 if ( newSashPosition
<= UNSPLIT_THRESHOLD
)
1003 // threshold top / left check
1004 newSashPosition
= 0;
1005 unsplit_scenario
= TRUE
;
1007 if ( newSashPosition
>= window_size
- UNSPLIT_THRESHOLD
)
1009 // threshold bottom/right check
1010 newSashPosition
= window_size
;
1011 unsplit_scenario
= TRUE
;
1015 if ( !unsplit_scenario
)
1017 // If resultant pane would be too small, enlarge it
1018 newSashPosition
= AdjustSashPosition(newSashPosition
);
1021 // If the result is out of bounds it means minimum size is too big,
1022 // so split window in half as best compromise.
1023 if ( newSashPosition
< 0 || newSashPosition
> window_size
)
1024 newSashPosition
= window_size
/ 2;
1026 // now let the event handler have it
1028 // FIXME: shouldn't we do it before the adjustments above so as to ensure
1029 // that the sash position is always reasonable?
1030 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING
, this);
1031 event
.m_data
.pos
= newSashPosition
;
1033 if ( !DoSendEvent(event
) )
1035 // the event handler vetoed the change
1036 newSashPosition
= -1;
1040 // it could have been changed by it
1041 newSashPosition
= event
.GetSashPosition();
1044 return newSashPosition
;
1047 // Called when the sash is double-clicked. The default behaviour is to remove
1048 // the sash if the minimum pane size is zero.
1049 void wxSplitterWindow::OnDoubleClickSash(int x
, int y
)
1051 wxCHECK_RET(m_windowTwo
, wxT("splitter: no window to remove"));
1053 // new code should handle events instead of using the virtual functions
1054 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_DOUBLECLICKED
, this);
1055 event
.m_data
.pt
.x
= x
;
1056 event
.m_data
.pt
.y
= y
;
1057 if ( DoSendEvent(event
) )
1059 if ( GetMinimumPaneSize() == 0 || m_permitUnsplitAlways
)
1061 wxWindow
* win
= m_windowTwo
;
1066 //else: blocked by user
1069 void wxSplitterWindow::OnUnsplit(wxWindow
*winRemoved
)
1071 // do it before calling the event handler which may delete the window
1072 winRemoved
->Show(FALSE
);
1074 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_UNSPLIT
, this);
1075 event
.m_data
.win
= winRemoved
;
1077 (void)DoSendEvent(event
);
1080 #if defined( __WXMSW__ ) || defined( __WXMAC__)
1082 // this is currently called (and needed) under MSW only...
1083 void wxSplitterWindow::OnSetCursor(wxSetCursorEvent
& event
)
1085 // if we don't do it, the resizing cursor might be set for child window:
1086 // and like this we explicitly say that our cursor should not be used for
1087 // children windows which overlap us
1089 if ( SashHitTest(event
.GetX(), event
.GetY()) )
1091 // default processing is ok
1094 //else: do nothing, in particular, don't call Skip()
1097 #endif // wxUSE_SPLITTER