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"
24 #include "wx/string.h"
28 #include "wx/dcscreen.h"
30 #include "wx/window.h"
31 #include "wx/dialog.h"
34 #include "wx/settings.h"
37 #include "wx/splitter.h"
41 DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED
)
42 DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING
)
43 DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPLITTER_DOUBLECLICKED
)
44 DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPLITTER_UNSPLIT
)
46 IMPLEMENT_DYNAMIC_CLASS(wxSplitterWindow
, wxWindow
)
47 IMPLEMENT_DYNAMIC_CLASS(wxSplitterEvent
, wxNotifyEvent
)
49 BEGIN_EVENT_TABLE(wxSplitterWindow
, wxWindow
)
50 EVT_PAINT(wxSplitterWindow::OnPaint
)
51 EVT_SIZE(wxSplitterWindow::OnSize
)
52 EVT_IDLE(wxSplitterWindow::OnIdle
)
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::OnIdle(wxIdleEvent
& event
)
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 SetSashPositionAndNotify(0);
257 else if ( posSashNew
== GetWindowSize() )
259 // We remove the second window from the view
260 wxWindow
*removedWindow
= m_windowTwo
;
261 m_windowTwo
= (wxWindow
*) NULL
;
262 OnUnsplit(removedWindow
);
263 SetSashPositionAndNotify(0);
267 SetSashPositionAndNotify(posSashNew
);
272 SetSashPositionAndNotify(posSashNew
);
276 } // left up && dragging
277 else if (event
.Moving() && !event
.Dragging())
279 // Just change the cursor as required
280 if ( SashHitTest(x
, y
) )
283 SetCursor(* wxSTANDARD_CURSOR
);
285 else if (event
.Dragging() && (m_dragMode
== wxSPLIT_DRAG_DRAGGING
))
287 int diff
= m_splitMode
== wxSPLIT_VERTICAL
? x
- m_oldX
: y
- m_oldY
;
290 // nothing to do, mouse didn't really move far enough
294 int posSashOld
= isLive
? m_sashPosition
: m_sashPositionCurrent
;
295 int posSashNew
= OnSashPositionChanging(posSashOld
+ diff
);
296 if ( posSashNew
== -1 )
298 // change not allowed
302 if ( posSashNew
== m_sashPosition
)
308 DrawSashTracker(m_oldX
, m_oldY
);
311 if (m_splitMode
== wxSPLIT_VERTICAL
)
316 // Remember old positions
321 // As we captured the mouse, we may get the mouse events from outside
322 // our window - for example, negative values in x, y. This has a weird
323 // consequence under MSW where we use unsigned values sometimes and
324 // signed ones other times: the coordinates turn as big positive
325 // numbers and so the sash is drawn on the *right* side of the window
326 // instead of the left (or bottom instead of top). Correct this.
327 if ( (short)m_oldX
< 0 )
329 if ( (short)m_oldY
< 0 )
336 m_sashPositionCurrent
= posSashNew
;
338 DrawSashTracker(m_oldX
, m_oldY
);
342 SetSashPositionAndNotify(posSashNew
);
343 m_needUpdating
= TRUE
;
346 else if ( event
.LeftDClick() && m_windowTwo
)
348 OnDoubleClickSash(x
, y
);
352 void wxSplitterWindow::OnSize(wxSizeEvent
& event
)
354 // only process this message if we're not iconized - otherwise iconizing
355 // and restoring a window containing the splitter has a funny side effect
356 // of changing the splitter position!
357 wxWindow
*parent
= GetParent();
358 while ( parent
&& !parent
->IsTopLevel() )
360 parent
= parent
->GetParent();
363 bool iconized
= FALSE
;
365 // wxMotif doesn't yet have a wxTopLevelWindow implementation
367 wxFrame
*winTop
= wxDynamicCast(parent
, wxFrame
);
369 wxTopLevelWindow
*winTop
= wxDynamicCast(parent
, wxTopLevelWindow
);
373 iconized
= winTop
->IsIconized();
378 wxFAIL_MSG(wxT("should have a top level parent!"));
392 GetClientSize( &cw
, &ch
);
395 if ( m_splitMode
== wxSPLIT_VERTICAL
)
397 if ( m_sashPosition
>= (cw
- 5) )
398 SetSashPositionAndNotify(wxMax(10, cw
- 40));
400 else // m_splitMode == wxSPLIT_HORIZONTAL
402 if ( m_sashPosition
>= (ch
- 5) )
403 SetSashPositionAndNotify(wxMax(10, ch
- 40));
410 bool wxSplitterWindow::SashHitTest(int x
, int y
, int tolerance
)
412 if ( m_windowTwo
== NULL
|| m_sashPosition
== 0)
413 return FALSE
; // No sash
415 if ( m_splitMode
== wxSPLIT_VERTICAL
)
417 if ( (x
>= m_sashPosition
- tolerance
) && (x
<= m_sashPosition
+ m_sashSize
+ tolerance
) )
424 if ( (y
>= (m_sashPosition
- tolerance
)) && (y
<= (m_sashPosition
+ m_sashSize
+ tolerance
)) )
431 // Draw 3D effect borders
432 void wxSplitterWindow::DrawBorders(wxDC
& dc
)
435 GetClientSize(&w
, &h
);
437 if ( GetWindowStyleFlag() & wxSP_3DBORDER
)
440 dc
.SetPen(*m_facePen
);
441 dc
.SetBrush(*m_faceBrush
);
442 dc
.DrawRectangle(1, 1 , w
-1, m_borderSize
-2 ); //high
443 dc
.DrawRectangle(1, m_borderSize
-2 , m_borderSize
-2, h
-1 ); // left
444 dc
.DrawRectangle(w
-m_borderSize
+2, m_borderSize
-2 , w
-1, h
-1 ); // right
445 dc
.DrawRectangle(m_borderSize
-2, h
-m_borderSize
+2 , w
-m_borderSize
+2, h
-1 ); //bottom
447 dc
.SetPen(*m_mediumShadowPen
);
448 dc
.DrawLine(m_borderSize
-2, m_borderSize
-2, w
-m_borderSize
+1, m_borderSize
-2);
449 dc
.DrawLine(m_borderSize
-2, m_borderSize
-2, m_borderSize
-2, h
-m_borderSize
+1);
451 dc
.SetPen(*m_darkShadowPen
);
452 dc
.DrawLine(m_borderSize
-1, m_borderSize
-1, w
-m_borderSize
, m_borderSize
-1);
453 dc
.DrawLine(m_borderSize
-1, m_borderSize
-1, m_borderSize
-1, h
-m_borderSize
);
455 dc
.SetPen(*m_hilightPen
);
456 dc
.DrawLine(m_borderSize
- 2, h
-m_borderSize
+1, w
-m_borderSize
+1, h
-m_borderSize
+1);
457 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.
458 /// Anyway, h is required for MSW.
460 dc
.SetPen(*m_lightShadowPen
);
461 dc
.DrawLine(w
-m_borderSize
, m_borderSize
-1, w
-m_borderSize
, h
-m_borderSize
); // Right hand side
462 dc
.DrawLine(m_borderSize
-1, h
-m_borderSize
, w
-m_borderSize
+1, h
-m_borderSize
); // Bottom
464 else if ( GetWindowStyleFlag() & wxSP_BORDER
)
466 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
467 dc
.SetPen(*wxBLACK_PEN
);
468 dc
.DrawRectangle(0, 0, w
-1, h
-1);
471 dc
.SetPen(wxNullPen
);
472 dc
.SetBrush(wxNullBrush
);
476 void wxSplitterWindow::DrawSash(wxDC
& dc
)
478 if ( m_sashPosition
== 0 || !m_windowTwo
)
480 if (GetWindowStyle() & wxSP_NOSASH
)
484 GetClientSize(&w
, &h
);
486 if ( GetWindowStyleFlag() & wxSP_3DSASH
)
488 if ( m_splitMode
== wxSPLIT_VERTICAL
)
490 dc
.SetPen(*m_facePen
);
492 if (HasFlag( wxSP_SASH_AQUA
))
493 dc
.SetBrush(*wxWHITE_BRUSH
);
495 dc
.SetBrush(*m_faceBrush
);
496 dc
.DrawRectangle(m_sashPosition
+ 2, 0 , m_sashSize
- 4, h
);
498 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
500 dc
.SetPen(*m_lightShadowPen
);
501 int xShadow
= m_borderSize
? m_borderSize
- 1 : 0 ;
502 dc
.DrawLine(m_sashPosition
, xShadow
, m_sashPosition
, h
-m_borderSize
);
504 dc
.SetPen(*m_hilightPen
);
505 dc
.DrawLine(m_sashPosition
+1, m_borderSize
- 2, m_sashPosition
+1, h
- m_borderSize
+2);
507 if (!HasFlag( wxSP_SASH_AQUA
))
508 dc
.SetPen(*m_mediumShadowPen
);
510 int yMedium
= m_borderSize
? h
-m_borderSize
+1 : h
;
511 dc
.DrawLine(m_sashPosition
+m_sashSize
-2, xShadow
, m_sashPosition
+m_sashSize
-2, yMedium
);
513 if (HasFlag( wxSP_SASH_AQUA
))
514 dc
.SetPen(*m_lightShadowPen
);
516 dc
.SetPen(*m_darkShadowPen
);
517 dc
.DrawLine(m_sashPosition
+m_sashSize
-1, m_borderSize
, m_sashPosition
+m_sashSize
-1, h
-m_borderSize
);
519 // Draw the top and bottom edges of the sash, if requested
520 if (GetWindowStyle() & wxSP_FULLSASH
)
523 dc
.SetPen(*m_hilightPen
);
524 dc
.DrawLine(m_sashPosition
+1, m_borderSize
, m_sashPosition
+m_sashSize
-1, m_borderSize
);
527 dc
.SetPen(*m_darkShadowPen
);
528 dc
.DrawLine(m_sashPosition
+1, h
-m_borderSize
-1, m_sashPosition
+m_sashSize
-1, h
-m_borderSize
-1);
531 else // wxSPLIT_HORIZONTAL
533 dc
.SetPen(*m_facePen
);
534 if (HasFlag( wxSP_SASH_AQUA
))
535 dc
.SetBrush(*wxWHITE_BRUSH
);
537 dc
.SetBrush(*m_faceBrush
);
538 dc
.DrawRectangle( m_borderSize
-2, m_sashPosition
+ 2, w
-m_borderSize
+2, m_sashSize
- 4);
540 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
542 dc
.SetPen(*m_lightShadowPen
);
543 dc
.DrawLine(m_borderSize
-1, m_sashPosition
, w
-m_borderSize
, m_sashPosition
);
545 dc
.SetPen(*m_hilightPen
);
546 dc
.DrawLine(m_borderSize
-2, m_sashPosition
+1, w
-m_borderSize
+1, m_sashPosition
+1);
548 if (!HasFlag( wxSP_SASH_AQUA
))
549 dc
.SetPen(*m_mediumShadowPen
);
550 dc
.DrawLine(m_borderSize
-1, m_sashPosition
+m_sashSize
-2, w
-m_borderSize
+1, m_sashPosition
+m_sashSize
-2);
552 if (HasFlag( wxSP_SASH_AQUA
))
553 dc
.SetPen(*m_lightShadowPen
);
555 dc
.SetPen(*m_darkShadowPen
);
556 dc
.DrawLine(m_borderSize
, m_sashPosition
+m_sashSize
-1, w
-m_borderSize
, m_sashPosition
+m_sashSize
-1);
558 // Draw the left and right edges of the sash, if requested
559 if (GetWindowStyle() & wxSP_FULLSASH
)
562 dc
.SetPen(*m_hilightPen
);
563 dc
.DrawLine(m_borderSize
, m_sashPosition
, m_borderSize
, m_sashPosition
+m_sashSize
);
566 dc
.SetPen(*m_darkShadowPen
);
567 dc
.DrawLine(w
-m_borderSize
-1, m_sashPosition
+1, w
-m_borderSize
-1, m_sashPosition
+m_sashSize
-1);
573 dc
.SetPen(*wxTRANSPARENT_PEN
);
574 dc
.SetBrush(*m_faceBrush
);
575 if ( m_splitMode
== wxSPLIT_VERTICAL
)
579 if ( (GetWindowStyleFlag() & wxSP_BORDER
) != wxSP_BORDER
&& (GetWindowStyleFlag() & wxSP_3DBORDER
) != wxSP_3DBORDER
)
580 h1
+= 1; // Not sure why this is necessary...
581 if ( (GetWindowStyleFlag() & wxSP_3DBORDER
) == wxSP_3DBORDER
)
585 dc
.DrawRectangle(m_sashPosition
, y1
, m_sashSize
, h1
);
587 else // wxSPLIT_HORIZONTAL
591 if ( (GetWindowStyleFlag() & wxSP_BORDER
) != wxSP_BORDER
&& (GetWindowStyleFlag() & wxSP_3DBORDER
) != wxSP_3DBORDER
)
593 if ( (GetWindowStyleFlag() & wxSP_3DBORDER
) == wxSP_3DBORDER
)
597 dc
.DrawRectangle(x1
, m_sashPosition
, w1
, m_sashSize
);
601 dc
.SetPen(wxNullPen
);
602 dc
.SetBrush(wxNullBrush
);
605 // Draw the sash tracker (for whilst moving the sash)
606 void wxSplitterWindow::DrawSashTracker(int x
, int y
)
609 GetClientSize(&w
, &h
);
615 if ( m_splitMode
== wxSPLIT_VERTICAL
)
646 ClientToScreen(&x1
, &y1
);
647 ClientToScreen(&x2
, &y2
);
649 screenDC
.SetLogicalFunction(wxINVERT
);
650 screenDC
.SetPen(*m_sashTrackerPen
);
651 screenDC
.SetBrush(*wxTRANSPARENT_BRUSH
);
653 screenDC
.DrawLine(x1
, y1
, x2
, y2
);
655 screenDC
.SetLogicalFunction(wxCOPY
);
657 screenDC
.SetPen(wxNullPen
);
658 screenDC
.SetBrush(wxNullBrush
);
661 int wxSplitterWindow::GetWindowSize() const
663 wxSize size
= GetClientSize();
665 return m_splitMode
== wxSPLIT_VERTICAL
? size
.x
: size
.y
;
668 int wxSplitterWindow::AdjustSashPosition(int sashPos
) const
670 int window_size
= GetWindowSize();
677 // the window shouldn't be smaller than its own minimal size nor
678 // smaller than the minimual pane size specified for this splitter
679 int minSize
= m_splitMode
== wxSPLIT_VERTICAL
? win
->GetMinWidth()
680 : win
->GetMinHeight();
682 if ( minSize
== -1 || m_minimumPaneSize
> minSize
)
683 minSize
= m_minimumPaneSize
;
685 minSize
+= GetBorderSize();
687 if ( sashPos
< minSize
)
694 int minSize
= m_splitMode
== wxSPLIT_VERTICAL
? win
->GetMinWidth()
695 : win
->GetMinHeight();
697 if ( minSize
== -1 || m_minimumPaneSize
> minSize
)
698 minSize
= m_minimumPaneSize
;
700 int maxSize
= window_size
- minSize
- GetBorderSize();
701 if ( sashPos
> maxSize
)
708 bool wxSplitterWindow::DoSetSashPosition(int sashPos
)
710 int newSashPosition
= AdjustSashPosition(sashPos
);
712 if ( newSashPosition
== m_sashPosition
)
715 m_sashPosition
= newSashPosition
;
720 void wxSplitterWindow::SetSashPositionAndNotify(int sashPos
)
722 if ( DoSetSashPosition(sashPos
) )
724 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED
, this);
725 event
.m_data
.pos
= m_sashPosition
;
727 (void)DoSendEvent(event
);
731 // Position and size subwindows.
732 // Note that the border size applies to each subwindow, not
733 // including the edges next to the sash.
734 void wxSplitterWindow::SizeWindows()
736 // check if we have delayed setting the real sash position
737 if ( m_requestedSashPosition
!= INT_MAX
)
739 int newSashPosition
= ConvertSashPosition(m_requestedSashPosition
);
740 if ( newSashPosition
!= m_sashPosition
)
742 DoSetSashPosition(newSashPosition
);
745 if ( newSashPosition
<= m_sashPosition
746 && newSashPosition
>= m_sashPosition
- GetBorderSize() )
748 // don't update it any more
749 m_requestedSashPosition
= INT_MAX
;
754 GetClientSize(&w
, &h
);
756 if ( GetWindow1() && !GetWindow2() )
758 GetWindow1()->SetSize(GetBorderSize(), GetBorderSize(),
759 w
- 2*GetBorderSize(), h
- 2*GetBorderSize());
761 else if ( GetWindow1() && GetWindow2() )
763 if (GetSplitMode() == wxSPLIT_VERTICAL
)
765 int x1
= GetBorderSize();
766 int y1
= GetBorderSize();
767 int w1
= GetSashPosition() - GetBorderSize();
768 int h1
= h
- 2*GetBorderSize();
770 int x2
= GetSashPosition() + GetSashSize();
771 int y2
= GetBorderSize();
772 int w2
= w
- 2*GetBorderSize() - GetSashSize() - w1
;
773 int h2
= h
- 2*GetBorderSize();
775 GetWindow1()->SetSize(x1
, y1
, w1
, h1
);
776 GetWindow2()->SetSize(x2
, y2
, w2
, h2
);
780 GetWindow1()->SetSize(GetBorderSize(), GetBorderSize(),
781 w
- 2*GetBorderSize(), GetSashPosition() - GetBorderSize());
782 GetWindow2()->SetSize(GetBorderSize(), GetSashPosition() + GetSashSize(),
783 w
- 2*GetBorderSize(), h
- 2*GetBorderSize() - GetSashSize() - (GetSashPosition() - GetBorderSize()));
787 if ( GetBorderSize() > 0 )
791 SetNeedUpdating(FALSE
);
794 // Set pane for unsplit window
795 void wxSplitterWindow::Initialize(wxWindow
*window
)
797 wxASSERT_MSG( window
&& window
->GetParent() == this,
798 _T("windows in the splitter should have it as parent!") );
800 m_windowOne
= window
;
801 m_windowTwo
= (wxWindow
*) NULL
;
802 DoSetSashPosition(0);
805 // Associates the given window with window 2, drawing the appropriate sash
806 // and changing the split mode.
807 // Does nothing and returns FALSE if the window is already split.
808 bool wxSplitterWindow::DoSplit(wxSplitMode mode
,
809 wxWindow
*window1
, wxWindow
*window2
,
815 wxCHECK_MSG( window1
&& window2
, FALSE
,
816 _T("can not split with NULL window(s)") );
818 wxCHECK_MSG( window1
->GetParent() == this && window2
->GetParent() == this, FALSE
,
819 _T("windows in the splitter should have it as parent!") );
822 m_windowOne
= window1
;
823 m_windowTwo
= window2
;
825 // remember the sash position we want to set for later if we can't set it
826 // right now (e.g. because the window is too small)
827 m_requestedSashPosition
= sashPosition
;
829 DoSetSashPosition(ConvertSashPosition(sashPosition
));
836 int wxSplitterWindow::ConvertSashPosition(int sashPosition
) const
838 if ( sashPosition
> 0 )
842 else if ( sashPosition
< 0 )
844 // It's negative so adding is subtracting
845 return GetWindowSize() + sashPosition
;
847 else // sashPosition == 0
849 // default, put it in the centre
850 return GetWindowSize() / 2;
854 // Remove the specified (or second) window from the view
855 // Doesn't actually delete the window.
856 bool wxSplitterWindow::Unsplit(wxWindow
*toRemove
)
861 wxWindow
*win
= NULL
;
862 if ( toRemove
== NULL
|| toRemove
== m_windowTwo
)
865 m_windowTwo
= (wxWindow
*) NULL
;
867 else if ( toRemove
== m_windowOne
)
870 m_windowOne
= m_windowTwo
;
871 m_windowTwo
= (wxWindow
*) NULL
;
875 wxFAIL_MSG(wxT("splitter: attempt to remove a non-existent window"));
881 DoSetSashPosition(0);
887 // Replace a window with another one
888 bool wxSplitterWindow::ReplaceWindow(wxWindow
*winOld
, wxWindow
*winNew
)
890 wxCHECK_MSG( winOld
, FALSE
, wxT("use one of Split() functions instead") );
891 wxCHECK_MSG( winNew
, FALSE
, wxT("use Unsplit() functions instead") );
893 if ( winOld
== m_windowTwo
)
895 m_windowTwo
= winNew
;
897 else if ( winOld
== m_windowOne
)
899 m_windowOne
= winNew
;
903 wxFAIL_MSG(wxT("splitter: attempt to replace a non-existent window"));
913 void wxSplitterWindow::SetMinimumPaneSize(int min
)
915 m_minimumPaneSize
= min
;
916 SetSashPosition(m_sashPosition
); // re-check limits
919 void wxSplitterWindow::SetSashPosition(int position
, bool redraw
)
921 DoSetSashPosition(position
);
929 // Initialize colours
930 void wxSplitterWindow::InitColours()
932 wxDELETE( m_facePen
);
933 wxDELETE( m_faceBrush
);
934 wxDELETE( m_mediumShadowPen
);
935 wxDELETE( m_darkShadowPen
);
936 wxDELETE( m_lightShadowPen
);
937 wxDELETE( m_hilightPen
);
941 wxColour
faceColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
942 m_facePen
= new wxPen(faceColour
, 1, wxSOLID
);
943 m_faceBrush
= new wxBrush(faceColour
, wxSOLID
);
945 wxColour
mediumShadowColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
));
946 m_mediumShadowPen
= new wxPen(mediumShadowColour
, 1, wxSOLID
);
948 wxColour
darkShadowColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
));
949 m_darkShadowPen
= new wxPen(darkShadowColour
, 1, wxSOLID
);
951 wxColour
lightShadowColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
));
952 m_lightShadowPen
= new wxPen(lightShadowColour
, 1, wxSOLID
);
954 wxColour
hilightColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DHILIGHT
));
955 m_hilightPen
= new wxPen(hilightColour
, 1, wxSOLID
);
957 m_facePen
= new wxPen("LIGHT GREY", 1, wxSOLID
);
958 m_faceBrush
= new wxBrush("LIGHT GREY", wxSOLID
);
959 m_mediumShadowPen
= new wxPen("GREY", 1, wxSOLID
);
960 m_darkShadowPen
= new wxPen("BLACK", 1, wxSOLID
);
961 m_lightShadowPen
= new wxPen("LIGHT GREY", 1, wxSOLID
);
962 m_hilightPen
= new wxPen("WHITE", 1, wxSOLID
);
966 bool wxSplitterWindow::DoSendEvent(wxSplitterEvent
& event
)
968 return !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed();
971 // ---------------------------------------------------------------------------
972 // wxSplitterWindow virtual functions: they now just generate the events
973 // ---------------------------------------------------------------------------
975 bool wxSplitterWindow::OnSashPositionChange(int WXUNUSED(newSashPosition
))
977 // always allow by default
981 int wxSplitterWindow::OnSashPositionChanging(int newSashPosition
)
983 // If within UNSPLIT_THRESHOLD from edge, set to edge to cause closure.
984 const int UNSPLIT_THRESHOLD
= 4;
986 // first of all, check if OnSashPositionChange() doesn't forbid this change
987 if ( !OnSashPositionChange(newSashPosition
) )
993 // Obtain relevant window dimension for bottom / right threshold check
994 int window_size
= GetWindowSize();
996 bool unsplit_scenario
= FALSE
;
997 if ( m_permitUnsplitAlways
|| m_minimumPaneSize
== 0 )
999 // Do edge detection if unsplit premitted
1000 if ( newSashPosition
<= UNSPLIT_THRESHOLD
)
1002 // threshold top / left check
1003 newSashPosition
= 0;
1004 unsplit_scenario
= TRUE
;
1006 if ( newSashPosition
>= window_size
- UNSPLIT_THRESHOLD
)
1008 // threshold bottom/right check
1009 newSashPosition
= window_size
;
1010 unsplit_scenario
= TRUE
;
1014 if ( !unsplit_scenario
)
1016 // If resultant pane would be too small, enlarge it
1017 newSashPosition
= AdjustSashPosition(newSashPosition
);
1020 // If the result is out of bounds it means minimum size is too big,
1021 // so split window in half as best compromise.
1022 if ( newSashPosition
< 0 || newSashPosition
> window_size
)
1023 newSashPosition
= window_size
/ 2;
1025 // now let the event handler have it
1027 // FIXME: shouldn't we do it before the adjustments above so as to ensure
1028 // that the sash position is always reasonable?
1029 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING
, this);
1030 event
.m_data
.pos
= newSashPosition
;
1032 if ( !DoSendEvent(event
) )
1034 // the event handler vetoed the change
1035 newSashPosition
= -1;
1039 // it could have been changed by it
1040 newSashPosition
= event
.GetSashPosition();
1043 return newSashPosition
;
1046 // Called when the sash is double-clicked. The default behaviour is to remove
1047 // the sash if the minimum pane size is zero.
1048 void wxSplitterWindow::OnDoubleClickSash(int x
, int y
)
1050 wxCHECK_RET(m_windowTwo
, wxT("splitter: no window to remove"));
1052 // new code should handle events instead of using the virtual functions
1053 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_DOUBLECLICKED
, this);
1054 event
.m_data
.pt
.x
= x
;
1055 event
.m_data
.pt
.y
= y
;
1056 if ( DoSendEvent(event
) )
1058 if ( GetMinimumPaneSize() == 0 || m_permitUnsplitAlways
)
1060 wxWindow
* win
= m_windowTwo
;
1065 //else: blocked by user
1068 void wxSplitterWindow::OnUnsplit(wxWindow
*winRemoved
)
1070 // do it before calling the event handler which may delete the window
1071 winRemoved
->Show(FALSE
);
1073 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_UNSPLIT
, this);
1074 event
.m_data
.win
= winRemoved
;
1076 (void)DoSendEvent(event
);
1079 #if defined( __WXMSW__ ) || defined( __WXMAC__)
1081 // this is currently called (and needed) under MSW only...
1082 void wxSplitterWindow::OnSetCursor(wxSetCursorEvent
& event
)
1084 // if we don't do it, the resizing cursor might be set for child window:
1085 // and like this we explicitly say that our cursor should not be used for
1086 // children windows which overlap us
1088 if ( SashHitTest(event
.GetX(), event
.GetY()) )
1090 // default processing is ok
1093 //else: do nothing, in particular, don't call Skip()