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_MOUSE_EVENTS(wxSplitterWindow::OnMouseEvent
)
55 #if defined( __WXMSW__ ) || defined( __WXMAC__)
56 EVT_SET_CURSOR(wxSplitterWindow::OnSetCursor
)
59 WX_EVENT_TABLE_CONTROL_CONTAINER(wxSplitterWindow
)
62 WX_DELEGATE_TO_CONTROL_CONTAINER(wxSplitterWindow
);
64 bool wxSplitterWindow::Create(wxWindow
*parent
, wxWindowID id
,
70 // allow TABbing from one window to the other
71 style
|= wxTAB_TRAVERSAL
;
73 if (!wxWindow::Create(parent
, id
, pos
, size
, style
, name
))
76 m_permitUnsplitAlways
= (style
& wxSP_PERMIT_UNSPLIT
) != 0;
78 if ( style
& wxSP_3DSASH
)
83 if ( style
& wxSP_3DBORDER
)
85 else if ( style
& wxSP_BORDER
)
92 wxGetOsVersion( &major
, &minor
);
94 m_windowStyle
|= wxSP_SASH_AQUA
;
100 void wxSplitterWindow::Init()
102 m_container
.SetContainerWindow(this);
104 m_splitMode
= wxSPLIT_VERTICAL
;
105 m_permitUnsplitAlways
= TRUE
;
106 m_windowOne
= (wxWindow
*) NULL
;
107 m_windowTwo
= (wxWindow
*) NULL
;
108 m_dragMode
= wxSPLIT_DRAG_NONE
;
115 m_sashPosition
= m_requestedSashPosition
= 0;
116 m_minimumPaneSize
= 0;
117 m_sashCursorWE
= wxCursor(wxCURSOR_SIZEWE
);
118 m_sashCursorNS
= wxCursor(wxCURSOR_SIZENS
);
119 m_sashTrackerPen
= new wxPen(*wxBLACK
, 2, wxSOLID
);
120 m_lightShadowPen
= (wxPen
*) NULL
;
121 m_mediumShadowPen
= (wxPen
*) NULL
;
122 m_darkShadowPen
= (wxPen
*) NULL
;
123 m_faceBrush
= (wxBrush
*) NULL
;
124 m_facePen
= (wxPen
*) NULL
;
125 m_hilightPen
= (wxPen
*) NULL
;
129 m_needUpdating
= FALSE
;
132 wxSplitterWindow::~wxSplitterWindow()
134 delete m_sashTrackerPen
;
135 delete m_lightShadowPen
;
136 delete m_darkShadowPen
;
137 delete m_mediumShadowPen
;
143 void wxSplitterWindow::SetResizeCursor()
145 SetCursor(m_splitMode
== wxSPLIT_VERTICAL
? m_sashCursorWE
149 void wxSplitterWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
153 if ( m_borderSize
> 0 )
158 void wxSplitterWindow::OnInternalIdle()
160 wxWindow::OnInternalIdle();
166 void wxSplitterWindow::OnMouseEvent(wxMouseEvent
& event
)
168 int x
= (int)event
.GetX(),
169 y
= (int)event
.GetY();
171 #if defined(__WXMSW__)
172 // SetCursor(wxCursor()); // Is this required?
175 if (GetWindowStyle() & wxSP_NOSASH
)
178 // with wxSP_LIVE_UPDATE style the splitter windows are always resized
179 // following the mouse movement while it drags the sash, without it we only
180 // draw the sash at the new position but only resize the windows when the
181 // dragging is finished
182 bool isLive
= (GetWindowStyleFlag() & wxSP_LIVE_UPDATE
) != 0;
184 if (event
.LeftDown())
186 if ( SashHitTest(x
, y
) )
188 // Start the drag now
189 m_dragMode
= wxSPLIT_DRAG_DRAGGING
;
191 // Capture mouse and set the cursor
197 // remember the initial sash position and draw the initial
199 m_sashPositionCurrent
= m_sashPosition
;
201 DrawSashTracker(x
, y
);
211 else if (event
.LeftUp() && m_dragMode
== wxSPLIT_DRAG_DRAGGING
)
213 // We can stop dragging now and see what we've got.
214 m_dragMode
= wxSPLIT_DRAG_NONE
;
216 // Release mouse and unset the cursor
218 SetCursor(* wxSTANDARD_CURSOR
);
220 // exit if unsplit after doubleclick
229 DrawSashTracker(m_oldX
, m_oldY
);
232 // the position of the click doesn't exactly correspond to
233 // m_sashPosition, rather it changes it by the distance by which the
235 int diff
= m_splitMode
== wxSPLIT_VERTICAL
? x
- m_oldX
: y
- m_oldY
;
237 int posSashOld
= isLive
? m_sashPosition
: m_sashPositionCurrent
;
238 int posSashNew
= OnSashPositionChanging(posSashOld
+ diff
);
239 if ( posSashNew
== -1 )
241 // change not allowed
245 if ( m_permitUnsplitAlways
|| m_minimumPaneSize
== 0 )
247 // Deal with possible unsplit scenarios
248 if ( posSashNew
== 0 )
250 // We remove the first window from the view
251 wxWindow
*removedWindow
= m_windowOne
;
252 m_windowOne
= m_windowTwo
;
253 m_windowTwo
= (wxWindow
*) NULL
;
254 OnUnsplit(removedWindow
);
255 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_UNSPLIT
, this);
256 event
.m_data
.win
= removedWindow
;
257 (void)DoSendEvent(event
);
258 SetSashPositionAndNotify(0);
260 else if ( posSashNew
== GetWindowSize() )
262 // We remove the second window from the view
263 wxWindow
*removedWindow
= 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);
273 SetSashPositionAndNotify(posSashNew
);
278 SetSashPositionAndNotify(posSashNew
);
282 } // left up && dragging
283 else if ((event
.Moving() || event
.Leaving() || event
.Entering()) && (m_dragMode
== wxSPLIT_DRAG_NONE
))
285 // Just change the cursor as required
286 if ( !event
.Leaving() && SashHitTest(x
, y
) )
292 SetCursor(* wxSTANDARD_CURSOR
);
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 // wxMotif doesn't yet have a wxTopLevelWindow implementation
377 wxFrame
*winTop
= wxDynamicCast(parent
, wxFrame
);
379 wxTopLevelWindow
*winTop
= wxDynamicCast(parent
, wxTopLevelWindow
);
383 iconized
= winTop
->IsIconized();
388 wxFAIL_MSG(wxT("should have a top level parent!"));
402 GetClientSize( &cw
, &ch
);
405 if ( m_splitMode
== wxSPLIT_VERTICAL
)
407 if ( m_sashPosition
>= (cw
- 5) )
408 SetSashPositionAndNotify(wxMax(10, cw
- 40));
410 else // m_splitMode == wxSPLIT_HORIZONTAL
412 if ( m_sashPosition
>= (ch
- 5) )
413 SetSashPositionAndNotify(wxMax(10, ch
- 40));
420 bool wxSplitterWindow::SashHitTest(int x
, int y
, int tolerance
)
422 if ( m_windowTwo
== NULL
|| m_sashPosition
== 0)
423 return FALSE
; // No sash
425 if ( m_splitMode
== wxSPLIT_VERTICAL
)
427 if ( (x
>= m_sashPosition
- tolerance
) && (x
<= m_sashPosition
+ m_sashSize
+ tolerance
) )
434 if ( (y
>= (m_sashPosition
- tolerance
)) && (y
<= (m_sashPosition
+ m_sashSize
+ tolerance
)) )
441 // Draw 3D effect borders
442 void wxSplitterWindow::DrawBorders(wxDC
& dc
)
445 GetClientSize(&w
, &h
);
447 if ( GetWindowStyleFlag() & wxSP_3DBORDER
)
450 dc
.SetPen(*m_facePen
);
451 dc
.SetBrush(*m_faceBrush
);
452 dc
.DrawRectangle(1, 1 , w
-1, m_borderSize
-2 ); //high
453 dc
.DrawRectangle(1, m_borderSize
-2 , m_borderSize
-2, h
-1 ); // left
454 dc
.DrawRectangle(w
-m_borderSize
+2, m_borderSize
-2 , w
-1, h
-1 ); // right
455 dc
.DrawRectangle(m_borderSize
-2, h
-m_borderSize
+2 , w
-m_borderSize
+2, h
-1 ); //bottom
457 dc
.SetPen(*m_mediumShadowPen
);
458 dc
.DrawLine(m_borderSize
-2, m_borderSize
-2, w
-m_borderSize
+1, m_borderSize
-2);
459 dc
.DrawLine(m_borderSize
-2, m_borderSize
-2, m_borderSize
-2, h
-m_borderSize
+1);
461 dc
.SetPen(*m_darkShadowPen
);
462 dc
.DrawLine(m_borderSize
-1, m_borderSize
-1, w
-m_borderSize
, m_borderSize
-1);
463 dc
.DrawLine(m_borderSize
-1, m_borderSize
-1, m_borderSize
-1, h
-m_borderSize
);
465 dc
.SetPen(*m_hilightPen
);
466 dc
.DrawLine(m_borderSize
- 2, h
-m_borderSize
+1, w
-m_borderSize
+1, h
-m_borderSize
+1);
467 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.
468 /// Anyway, h is required for MSW.
470 dc
.SetPen(*m_lightShadowPen
);
471 dc
.DrawLine(w
-m_borderSize
, m_borderSize
-1, w
-m_borderSize
, h
-m_borderSize
); // Right hand side
472 dc
.DrawLine(m_borderSize
-1, h
-m_borderSize
, w
-m_borderSize
+1, h
-m_borderSize
); // Bottom
474 else if ( GetWindowStyleFlag() & wxSP_BORDER
)
476 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
477 dc
.SetPen(*wxBLACK_PEN
);
478 dc
.DrawRectangle(0, 0, w
-1, h
-1);
481 dc
.SetPen(wxNullPen
);
482 dc
.SetBrush(wxNullBrush
);
486 void wxSplitterWindow::DrawSash(wxDC
& dc
)
488 if ( m_sashPosition
== 0 || !m_windowTwo
)
490 if (GetWindowStyle() & wxSP_NOSASH
)
494 GetClientSize(&w
, &h
);
496 if ( GetWindowStyleFlag() & wxSP_3DSASH
)
498 if ( m_splitMode
== wxSPLIT_VERTICAL
)
500 dc
.SetPen(*m_facePen
);
502 if (HasFlag( wxSP_SASH_AQUA
))
503 dc
.SetBrush(*wxWHITE_BRUSH
);
505 dc
.SetBrush(*m_faceBrush
);
506 dc
.DrawRectangle(m_sashPosition
+ 2, 0 , m_sashSize
- 4, h
);
508 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
510 dc
.SetPen(*m_lightShadowPen
);
511 int xShadow
= m_borderSize
? m_borderSize
- 1 : 0 ;
512 dc
.DrawLine(m_sashPosition
, xShadow
, m_sashPosition
, h
-m_borderSize
);
514 dc
.SetPen(*m_hilightPen
);
515 dc
.DrawLine(m_sashPosition
+1, m_borderSize
- 2, m_sashPosition
+1, h
- m_borderSize
+2);
517 if (!HasFlag( wxSP_SASH_AQUA
))
518 dc
.SetPen(*m_mediumShadowPen
);
520 int yMedium
= m_borderSize
? h
-m_borderSize
+1 : h
;
521 dc
.DrawLine(m_sashPosition
+m_sashSize
-2, xShadow
, m_sashPosition
+m_sashSize
-2, yMedium
);
523 if (HasFlag( wxSP_SASH_AQUA
))
524 dc
.SetPen(*m_lightShadowPen
);
526 dc
.SetPen(*m_darkShadowPen
);
527 dc
.DrawLine(m_sashPosition
+m_sashSize
-1, m_borderSize
, m_sashPosition
+m_sashSize
-1, h
-m_borderSize
);
529 // Draw the top and bottom edges of the sash, if requested
530 if (GetWindowStyle() & wxSP_FULLSASH
)
533 dc
.SetPen(*m_hilightPen
);
534 dc
.DrawLine(m_sashPosition
+1, m_borderSize
, m_sashPosition
+m_sashSize
-1, m_borderSize
);
537 dc
.SetPen(*m_darkShadowPen
);
538 dc
.DrawLine(m_sashPosition
+1, h
-m_borderSize
-1, m_sashPosition
+m_sashSize
-1, h
-m_borderSize
-1);
541 else // wxSPLIT_HORIZONTAL
543 dc
.SetPen(*m_facePen
);
544 if (HasFlag( wxSP_SASH_AQUA
))
545 dc
.SetBrush(*wxWHITE_BRUSH
);
547 dc
.SetBrush(*m_faceBrush
);
548 dc
.DrawRectangle( m_borderSize
-2, m_sashPosition
+ 2, w
-m_borderSize
+2, m_sashSize
- 4);
550 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
552 dc
.SetPen(*m_lightShadowPen
);
553 dc
.DrawLine(m_borderSize
-1, m_sashPosition
, w
-m_borderSize
, m_sashPosition
);
555 dc
.SetPen(*m_hilightPen
);
556 dc
.DrawLine(m_borderSize
-2, m_sashPosition
+1, w
-m_borderSize
+1, m_sashPosition
+1);
558 if (!HasFlag( wxSP_SASH_AQUA
))
559 dc
.SetPen(*m_mediumShadowPen
);
560 dc
.DrawLine(m_borderSize
-1, m_sashPosition
+m_sashSize
-2, w
-m_borderSize
+1, m_sashPosition
+m_sashSize
-2);
562 if (HasFlag( wxSP_SASH_AQUA
))
563 dc
.SetPen(*m_lightShadowPen
);
565 dc
.SetPen(*m_darkShadowPen
);
566 dc
.DrawLine(m_borderSize
, m_sashPosition
+m_sashSize
-1, w
-m_borderSize
, m_sashPosition
+m_sashSize
-1);
568 // Draw the left and right edges of the sash, if requested
569 if (GetWindowStyle() & wxSP_FULLSASH
)
572 dc
.SetPen(*m_hilightPen
);
573 dc
.DrawLine(m_borderSize
, m_sashPosition
, m_borderSize
, m_sashPosition
+m_sashSize
);
576 dc
.SetPen(*m_darkShadowPen
);
577 dc
.DrawLine(w
-m_borderSize
-1, m_sashPosition
+1, w
-m_borderSize
-1, m_sashPosition
+m_sashSize
-1);
583 dc
.SetPen(*wxTRANSPARENT_PEN
);
584 dc
.SetBrush(*m_faceBrush
);
585 if ( m_splitMode
== wxSPLIT_VERTICAL
)
589 if ( (GetWindowStyleFlag() & wxSP_BORDER
) != wxSP_BORDER
&& (GetWindowStyleFlag() & wxSP_3DBORDER
) != wxSP_3DBORDER
)
590 h1
+= 1; // Not sure why this is necessary...
591 if ( (GetWindowStyleFlag() & wxSP_3DBORDER
) == wxSP_3DBORDER
)
595 dc
.DrawRectangle(m_sashPosition
, y1
, m_sashSize
, h1
);
597 else // wxSPLIT_HORIZONTAL
601 if ( (GetWindowStyleFlag() & wxSP_BORDER
) != wxSP_BORDER
&& (GetWindowStyleFlag() & wxSP_3DBORDER
) != wxSP_3DBORDER
)
603 if ( (GetWindowStyleFlag() & wxSP_3DBORDER
) == wxSP_3DBORDER
)
607 dc
.DrawRectangle(x1
, m_sashPosition
, w1
, m_sashSize
);
611 dc
.SetPen(wxNullPen
);
612 dc
.SetBrush(wxNullBrush
);
615 // Draw the sash tracker (for whilst moving the sash)
616 void wxSplitterWindow::DrawSashTracker(int x
, int y
)
619 GetClientSize(&w
, &h
);
625 if ( m_splitMode
== wxSPLIT_VERTICAL
)
656 ClientToScreen(&x1
, &y1
);
657 ClientToScreen(&x2
, &y2
);
659 screenDC
.SetLogicalFunction(wxINVERT
);
660 screenDC
.SetPen(*m_sashTrackerPen
);
661 screenDC
.SetBrush(*wxTRANSPARENT_BRUSH
);
663 screenDC
.DrawLine(x1
, y1
, x2
, y2
);
665 screenDC
.SetLogicalFunction(wxCOPY
);
667 screenDC
.SetPen(wxNullPen
);
668 screenDC
.SetBrush(wxNullBrush
);
671 int wxSplitterWindow::GetWindowSize() const
673 wxSize size
= GetClientSize();
675 return m_splitMode
== wxSPLIT_VERTICAL
? size
.x
: size
.y
;
678 int wxSplitterWindow::AdjustSashPosition(int sashPos
) const
680 int window_size
= GetWindowSize();
687 // the window shouldn't be smaller than its own minimal size nor
688 // smaller than the minimual pane size specified for this splitter
689 int minSize
= m_splitMode
== wxSPLIT_VERTICAL
? win
->GetMinWidth()
690 : win
->GetMinHeight();
692 if ( minSize
== -1 || m_minimumPaneSize
> minSize
)
693 minSize
= m_minimumPaneSize
;
695 minSize
+= GetBorderSize();
697 if ( sashPos
< minSize
)
704 int minSize
= m_splitMode
== wxSPLIT_VERTICAL
? win
->GetMinWidth()
705 : win
->GetMinHeight();
707 if ( minSize
== -1 || m_minimumPaneSize
> minSize
)
708 minSize
= m_minimumPaneSize
;
710 int maxSize
= window_size
- minSize
- GetBorderSize();
711 if ( sashPos
> maxSize
)
718 bool wxSplitterWindow::DoSetSashPosition(int sashPos
)
720 int newSashPosition
= AdjustSashPosition(sashPos
);
722 if ( newSashPosition
== m_sashPosition
)
725 m_sashPosition
= newSashPosition
;
730 void wxSplitterWindow::SetSashPositionAndNotify(int sashPos
)
732 if ( DoSetSashPosition(sashPos
) )
734 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED
, this);
735 event
.m_data
.pos
= m_sashPosition
;
737 (void)DoSendEvent(event
);
741 // Position and size subwindows.
742 // Note that the border size applies to each subwindow, not
743 // including the edges next to the sash.
744 void wxSplitterWindow::SizeWindows()
746 // check if we have delayed setting the real sash position
747 if ( m_requestedSashPosition
!= INT_MAX
)
749 int newSashPosition
= ConvertSashPosition(m_requestedSashPosition
);
750 if ( newSashPosition
!= m_sashPosition
)
752 DoSetSashPosition(newSashPosition
);
755 if ( newSashPosition
<= m_sashPosition
756 && newSashPosition
>= m_sashPosition
- GetBorderSize() )
758 // don't update it any more
759 m_requestedSashPosition
= INT_MAX
;
764 GetClientSize(&w
, &h
);
766 if ( GetWindow1() && !GetWindow2() )
768 GetWindow1()->SetSize(GetBorderSize(), GetBorderSize(),
769 w
- 2*GetBorderSize(), h
- 2*GetBorderSize());
771 else if ( GetWindow1() && GetWindow2() )
773 if (GetSplitMode() == wxSPLIT_VERTICAL
)
775 int x1
= GetBorderSize();
776 int y1
= GetBorderSize();
777 int w1
= GetSashPosition() - GetBorderSize();
778 int h1
= h
- 2*GetBorderSize();
780 int x2
= GetSashPosition() + GetSashSize();
781 int y2
= GetBorderSize();
782 int w2
= w
- 2*GetBorderSize() - GetSashSize() - w1
;
783 int h2
= h
- 2*GetBorderSize();
785 GetWindow1()->SetSize(x1
, y1
, w1
, h1
);
786 GetWindow2()->SetSize(x2
, y2
, w2
, h2
);
790 GetWindow1()->SetSize(GetBorderSize(), GetBorderSize(),
791 w
- 2*GetBorderSize(), GetSashPosition() - GetBorderSize());
792 GetWindow2()->SetSize(GetBorderSize(), GetSashPosition() + GetSashSize(),
793 w
- 2*GetBorderSize(), h
- 2*GetBorderSize() - GetSashSize() - (GetSashPosition() - GetBorderSize()));
797 if ( GetBorderSize() > 0 )
801 SetNeedUpdating(FALSE
);
804 // Set pane for unsplit window
805 void wxSplitterWindow::Initialize(wxWindow
*window
)
807 wxASSERT_MSG( window
&& window
->GetParent() == this,
808 _T("windows in the splitter should have it as parent!") );
810 m_windowOne
= window
;
811 m_windowTwo
= (wxWindow
*) NULL
;
812 DoSetSashPosition(0);
815 // Associates the given window with window 2, drawing the appropriate sash
816 // and changing the split mode.
817 // Does nothing and returns FALSE if the window is already split.
818 bool wxSplitterWindow::DoSplit(wxSplitMode mode
,
819 wxWindow
*window1
, wxWindow
*window2
,
825 wxCHECK_MSG( window1
&& window2
, FALSE
,
826 _T("can not split with NULL window(s)") );
828 wxCHECK_MSG( window1
->GetParent() == this && window2
->GetParent() == this, FALSE
,
829 _T("windows in the splitter should have it as parent!") );
832 m_windowOne
= window1
;
833 m_windowTwo
= window2
;
835 // remember the sash position we want to set for later if we can't set it
836 // right now (e.g. because the window is too small)
837 m_requestedSashPosition
= sashPosition
;
839 DoSetSashPosition(ConvertSashPosition(sashPosition
));
846 int wxSplitterWindow::ConvertSashPosition(int sashPosition
) const
848 if ( sashPosition
> 0 )
852 else if ( sashPosition
< 0 )
854 // It's negative so adding is subtracting
855 return GetWindowSize() + sashPosition
;
857 else // sashPosition == 0
859 // default, put it in the centre
860 return GetWindowSize() / 2;
864 // Remove the specified (or second) window from the view
865 // Doesn't actually delete the window.
866 bool wxSplitterWindow::Unsplit(wxWindow
*toRemove
)
871 wxWindow
*win
= NULL
;
872 if ( toRemove
== NULL
|| toRemove
== m_windowTwo
)
875 m_windowTwo
= (wxWindow
*) NULL
;
877 else if ( toRemove
== m_windowOne
)
880 m_windowOne
= m_windowTwo
;
881 m_windowTwo
= (wxWindow
*) NULL
;
885 wxFAIL_MSG(wxT("splitter: attempt to remove a non-existent window"));
891 DoSetSashPosition(0);
897 // Replace a window with another one
898 bool wxSplitterWindow::ReplaceWindow(wxWindow
*winOld
, wxWindow
*winNew
)
900 wxCHECK_MSG( winOld
, FALSE
, wxT("use one of Split() functions instead") );
901 wxCHECK_MSG( winNew
, FALSE
, wxT("use Unsplit() functions instead") );
903 if ( winOld
== m_windowTwo
)
905 m_windowTwo
= winNew
;
907 else if ( winOld
== m_windowOne
)
909 m_windowOne
= winNew
;
913 wxFAIL_MSG(wxT("splitter: attempt to replace a non-existent window"));
923 void wxSplitterWindow::SetMinimumPaneSize(int min
)
925 m_minimumPaneSize
= min
;
926 SetSashPosition(m_sashPosition
); // re-check limits
929 void wxSplitterWindow::SetSashPosition(int position
, bool redraw
)
931 DoSetSashPosition(position
);
939 // Initialize colours
940 void wxSplitterWindow::InitColours()
942 wxDELETE( m_facePen
);
943 wxDELETE( m_faceBrush
);
944 wxDELETE( m_mediumShadowPen
);
945 wxDELETE( m_darkShadowPen
);
946 wxDELETE( m_lightShadowPen
);
947 wxDELETE( m_hilightPen
);
951 wxColour
faceColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
952 m_facePen
= new wxPen(faceColour
, 1, wxSOLID
);
953 m_faceBrush
= new wxBrush(faceColour
, wxSOLID
);
955 wxColour
mediumShadowColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
));
956 m_mediumShadowPen
= new wxPen(mediumShadowColour
, 1, wxSOLID
);
958 wxColour
darkShadowColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
));
959 m_darkShadowPen
= new wxPen(darkShadowColour
, 1, wxSOLID
);
961 wxColour
lightShadowColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
));
962 m_lightShadowPen
= new wxPen(lightShadowColour
, 1, wxSOLID
);
964 wxColour
hilightColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DHILIGHT
));
965 m_hilightPen
= new wxPen(hilightColour
, 1, wxSOLID
);
967 m_facePen
= new wxPen("LIGHT GREY", 1, wxSOLID
);
968 m_faceBrush
= new wxBrush("LIGHT GREY", wxSOLID
);
969 m_mediumShadowPen
= new wxPen("GREY", 1, wxSOLID
);
970 m_darkShadowPen
= new wxPen("BLACK", 1, wxSOLID
);
971 m_lightShadowPen
= new wxPen("LIGHT GREY", 1, wxSOLID
);
972 m_hilightPen
= new wxPen("WHITE", 1, wxSOLID
);
976 bool wxSplitterWindow::DoSendEvent(wxSplitterEvent
& event
)
978 return !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed();
981 // ---------------------------------------------------------------------------
982 // wxSplitterWindow virtual functions: they now just generate the events
983 // ---------------------------------------------------------------------------
985 bool wxSplitterWindow::OnSashPositionChange(int WXUNUSED(newSashPosition
))
987 // always allow by default
991 int wxSplitterWindow::OnSashPositionChanging(int newSashPosition
)
993 // If within UNSPLIT_THRESHOLD from edge, set to edge to cause closure.
994 const int UNSPLIT_THRESHOLD
= 4;
996 // first of all, check if OnSashPositionChange() doesn't forbid this change
997 if ( !OnSashPositionChange(newSashPosition
) )
1003 // Obtain relevant window dimension for bottom / right threshold check
1004 int window_size
= GetWindowSize();
1006 bool unsplit_scenario
= FALSE
;
1007 if ( m_permitUnsplitAlways
|| m_minimumPaneSize
== 0 )
1009 // Do edge detection if unsplit premitted
1010 if ( newSashPosition
<= UNSPLIT_THRESHOLD
)
1012 // threshold top / left check
1013 newSashPosition
= 0;
1014 unsplit_scenario
= TRUE
;
1016 if ( newSashPosition
>= window_size
- UNSPLIT_THRESHOLD
)
1018 // threshold bottom/right check
1019 newSashPosition
= window_size
;
1020 unsplit_scenario
= TRUE
;
1024 if ( !unsplit_scenario
)
1026 // If resultant pane would be too small, enlarge it
1027 newSashPosition
= AdjustSashPosition(newSashPosition
);
1030 // If the result is out of bounds it means minimum size is too big,
1031 // so split window in half as best compromise.
1032 if ( newSashPosition
< 0 || newSashPosition
> window_size
)
1033 newSashPosition
= window_size
/ 2;
1035 // now let the event handler have it
1037 // FIXME: shouldn't we do it before the adjustments above so as to ensure
1038 // that the sash position is always reasonable?
1039 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING
, this);
1040 event
.m_data
.pos
= newSashPosition
;
1042 if ( !DoSendEvent(event
) )
1044 // the event handler vetoed the change
1045 newSashPosition
= -1;
1049 // it could have been changed by it
1050 newSashPosition
= event
.GetSashPosition();
1053 return newSashPosition
;
1056 // Called when the sash is double-clicked. The default behaviour is to remove
1057 // the sash if the minimum pane size is zero.
1058 void wxSplitterWindow::OnDoubleClickSash(int x
, int y
)
1060 wxCHECK_RET(m_windowTwo
, wxT("splitter: no window to remove"));
1062 // new code should handle events instead of using the virtual functions
1063 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_DOUBLECLICKED
, this);
1064 event
.m_data
.pt
.x
= x
;
1065 event
.m_data
.pt
.y
= y
;
1066 if ( DoSendEvent(event
) )
1068 if ( GetMinimumPaneSize() == 0 || m_permitUnsplitAlways
)
1070 wxWindow
* win
= m_windowTwo
;
1073 wxSplitterEvent
unsplitEvent(wxEVT_COMMAND_SPLITTER_UNSPLIT
, this);
1074 unsplitEvent
.m_data
.win
= win
;
1075 (void)DoSendEvent(unsplitEvent
);
1079 //else: blocked by user
1082 void wxSplitterWindow::OnUnsplit(wxWindow
*winRemoved
)
1084 // call this before calling the event handler which may delete the window
1085 winRemoved
->Show(FALSE
);
1088 #if defined( __WXMSW__ ) || defined( __WXMAC__)
1090 // this is currently called (and needed) under MSW only...
1091 void wxSplitterWindow::OnSetCursor(wxSetCursorEvent
& event
)
1093 // if we don't do it, the resizing cursor might be set for child window:
1094 // and like this we explicitly say that our cursor should not be used for
1095 // children windows which overlap us
1097 if ( SashHitTest(event
.GetX(), event
.GetY()) )
1099 // default processing is ok
1102 //else: do nothing, in particular, don't call Skip()
1105 #endif // wxUSE_SPLITTER