1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxSplitterWindow implementation
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "splitter.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
29 #include "wx/string.h"
30 #include "wx/splitter.h"
31 #include "wx/dcscreen.h"
33 #if !USE_SHARED_LIBRARY
34 IMPLEMENT_DYNAMIC_CLASS(wxSplitterWindow
, wxWindow
)
35 IMPLEMENT_DYNAMIC_CLASS(wxSplitterEvent
, wxCommandEvent
)
37 BEGIN_EVENT_TABLE(wxSplitterWindow
, wxWindow
)
38 EVT_PAINT(wxSplitterWindow::OnPaint
)
39 EVT_SIZE(wxSplitterWindow::OnSize
)
40 EVT_MOUSE_EVENTS(wxSplitterWindow::OnMouseEvent
)
42 EVT_SPLITTER_SASH_POS_CHANGED(-1, wxSplitterWindow::OnSashPosChanged
)
43 EVT_SPLITTER_DCLICK(-1, wxSplitterWindow::OnDoubleClick
)
44 EVT_SPLITTER_UNSPLIT(-1, wxSplitterWindow::OnUnsplitEvent
)
48 wxSplitterWindow::wxSplitterWindow()
50 m_splitMode
= wxSPLIT_VERTICAL
;
51 m_windowOne
= (wxWindow
*) NULL
;
52 m_windowTwo
= (wxWindow
*) NULL
;
53 m_dragMode
= wxSPLIT_DRAG_NONE
;
61 m_sashCursorWE
= (wxCursor
*) NULL
;
62 m_sashCursorNS
= (wxCursor
*) NULL
;
63 m_sashTrackerPen
= (wxPen
*) NULL
;
64 m_lightShadowPen
= (wxPen
*) NULL
;
65 m_mediumShadowPen
= (wxPen
*) NULL
;
66 m_darkShadowPen
= (wxPen
*) NULL
;
67 m_faceBrush
= (wxBrush
*) NULL
;
68 m_facePen
= (wxPen
*) NULL
;
69 m_hilightPen
= (wxPen
*) NULL
;
70 m_minimumPaneSize
= 0;
73 wxSplitterWindow::wxSplitterWindow(wxWindow
*parent
, wxWindowID id
,
78 : wxWindow(parent
, id
, pos
, size
, style
, name
)
80 m_splitMode
= wxSPLIT_VERTICAL
;
81 m_windowOne
= (wxWindow
*) NULL
;
82 m_windowTwo
= (wxWindow
*) NULL
;
83 m_dragMode
= wxSPLIT_DRAG_NONE
;
91 m_minimumPaneSize
= 0;
92 m_sashCursorWE
= new wxCursor(wxCURSOR_SIZEWE
);
93 m_sashCursorNS
= new wxCursor(wxCURSOR_SIZENS
);
94 m_sashTrackerPen
= new wxPen(*wxBLACK
, 2, wxSOLID
);
95 m_lightShadowPen
= (wxPen
*) NULL
;
96 m_mediumShadowPen
= (wxPen
*) NULL
;
97 m_darkShadowPen
= (wxPen
*) NULL
;
98 m_faceBrush
= (wxBrush
*) NULL
;
99 m_facePen
= (wxPen
*) NULL
;
100 m_hilightPen
= (wxPen
*) NULL
;
102 if ( style
& wxSP_3D
)
107 else if ( style
& wxSP_BORDER
)
118 // Eventually, we'll respond to colour change messages
121 // For debugging purposes, to see the background.
122 // SetBackground(wxBLUE_BRUSH);
125 wxSplitterWindow::~wxSplitterWindow()
127 delete m_sashCursorWE
;
128 delete m_sashCursorNS
;
129 delete m_sashTrackerPen
;
130 delete m_lightShadowPen
;
131 delete m_darkShadowPen
;
132 delete m_mediumShadowPen
;
138 void wxSplitterWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
142 if ( m_borderSize
> 0 )
148 void wxSplitterWindow::OnMouseEvent(wxMouseEvent
& event
)
150 long x
= event
.GetX();
151 long y
= event
.GetY();
155 SetCursor(* wxSTANDARD_CURSOR
);
158 SetCursor(wxCursor());
161 // under wxGTK the method above causes the mouse
162 // to flicker so we set the standard cursor only
163 // when leaving the window and when moving over
164 // non-sash parts of the window. this should work
165 // on the other platforms as well, but who knows.
168 SetCursor(* wxSTANDARD_CURSOR
);
171 if (event
.LeftDown())
173 if ( SashHitTest(x
, y
) )
177 m_dragMode
= wxSPLIT_DRAG_DRAGGING
;
179 DrawSashTracker(x
, y
);
185 else if (event
.LeftUp() && m_dragMode
== wxSPLIT_DRAG_DRAGGING
)
187 // We can stop dragging now and see what we've got.
188 m_dragMode
= wxSPLIT_DRAG_NONE
;
192 DrawSashTracker(m_oldX
, m_oldY
);
194 // Obtain window size. We are only interested in the dimension the sash
197 GetClientSize(&w
, &h
);
198 int window_size
= (m_splitMode
== wxSPLIT_VERTICAL
? w
: h
);
199 int new_sash_position
=
200 (int) ( m_splitMode
== wxSPLIT_VERTICAL
? x
: y
);
202 wxSplitterEvent
eventSplitter(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED
,
204 eventSplitter
.m_data
.pos
= new_sash_position
;
205 if ( GetEventHandler()->ProcessEvent(eventSplitter
) )
207 new_sash_position
= eventSplitter
.GetSashPosition();
208 if ( new_sash_position
== -1 )
210 // change not allowed
215 if ( new_sash_position
== 0 )
217 // We remove the first window from the view
218 wxWindow
*removedWindow
= m_windowOne
;
219 m_windowOne
= m_windowTwo
;
220 m_windowTwo
= (wxWindow
*) NULL
;
221 SendUnsplitEvent(removedWindow
);
224 else if ( new_sash_position
== window_size
)
226 // We remove the second window from the view
227 wxWindow
*removedWindow
= m_windowTwo
;
228 m_windowTwo
= (wxWindow
*) NULL
;
229 SendUnsplitEvent(removedWindow
);
234 m_sashPosition
= new_sash_position
;
238 } // left up && dragging
239 else if (event
.Moving() && !event
.Dragging())
241 // Just change the cursor if required
242 if ( SashHitTest(x
, y
) )
244 if ( m_splitMode
== wxSPLIT_VERTICAL
)
246 SetCursor(*m_sashCursorWE
);
250 SetCursor(*m_sashCursorNS
);
256 // where else do we unset the cursor?
257 SetCursor(* wxSTANDARD_CURSOR
);
261 else if (event
.Dragging() && (m_dragMode
== wxSPLIT_DRAG_DRAGGING
))
264 DrawSashTracker(m_oldX
, m_oldY
);
267 DrawSashTracker(x
, y
);
272 else if ( event
.LeftDClick() )
274 wxSplitterEvent
eventSplitter(wxEVT_COMMAND_SPLITTER_DOUBLECLICKED
,
276 eventSplitter
.m_data
.pt
.x
= x
;
277 eventSplitter
.m_data
.pt
.y
= y
;
279 (void)GetEventHandler()->ProcessEvent(eventSplitter
);
283 void wxSplitterWindow::OnSize(wxSizeEvent
& WXUNUSED(event
))
286 GetClientSize( &cw
, &ch
);
289 if ( m_splitMode
== wxSPLIT_VERTICAL
)
291 if ( m_sashPosition
>= (cw
- 5) )
292 m_sashPosition
= wxMax(10, cw
- 40);
294 if ( m_splitMode
== wxSPLIT_HORIZONTAL
)
296 if ( m_sashPosition
>= (ch
- 5) )
297 m_sashPosition
= wxMax(10, ch
- 40);
303 bool wxSplitterWindow::SashHitTest(int x
, int y
, int tolerance
)
305 if ( m_windowTwo
== NULL
|| m_sashPosition
== 0)
306 return FALSE
; // No sash
308 if ( m_splitMode
== wxSPLIT_VERTICAL
)
310 if ( (x
>= m_sashPosition
- tolerance
) && (x
<= m_sashPosition
+ m_sashSize
+ tolerance
) )
317 if ( (y
>= (m_sashPosition
- tolerance
)) && (y
<= (m_sashPosition
+ m_sashSize
+ tolerance
)) )
326 // Draw 3D effect borders
327 void wxSplitterWindow::DrawBorders(wxDC
& dc
)
330 GetClientSize(&w
, &h
);
332 if ( GetWindowStyleFlag() & wxSP_3D
)
334 dc
.SetPen(*m_mediumShadowPen
);
335 dc
.DrawLine(0, 0, w
-1, 0);
336 dc
.DrawLine(0, 0, 0, h
- 1);
338 dc
.SetPen(*m_darkShadowPen
);
339 dc
.DrawLine(1, 1, w
-2, 1);
340 dc
.DrawLine(1, 1, 1, h
-2);
342 dc
.SetPen(*m_hilightPen
);
343 dc
.DrawLine(0, h
-1, w
-1, h
-1);
344 dc
.DrawLine(w
-1, 0, w
-1, h
); // Surely the maximum y pos. should be h - 1.
345 /// Anyway, h is required for MSW.
347 dc
.SetPen(*m_lightShadowPen
);
348 dc
.DrawLine(w
-2, 1, w
-2, h
-2); // Right hand side
349 dc
.DrawLine(1, h
-2, w
-1, h
-2); // Bottom
351 else if ( GetWindowStyleFlag() & wxSP_BORDER
)
353 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
354 dc
.SetPen(*wxBLACK_PEN
);
355 dc
.DrawRectangle(0, 0, w
-1, h
-1);
358 dc
.SetPen(wxNullPen
);
359 dc
.SetBrush(wxNullBrush
);
363 void wxSplitterWindow::DrawSash(wxDC
& dc
)
365 if ( m_sashPosition
== 0 || !m_windowTwo
)
369 GetClientSize(&w
, &h
);
371 if ( GetWindowStyleFlag() & wxSP_3D
)
373 if ( m_splitMode
== wxSPLIT_VERTICAL
)
375 dc
.SetPen(*m_facePen
);
376 dc
.SetBrush(*m_faceBrush
);
377 dc
.DrawRectangle(m_sashPosition
+ 2, 0, m_sashSize
- 4, h
);
379 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
381 dc
.SetPen(*m_lightShadowPen
);
382 dc
.DrawLine(m_sashPosition
, 1, m_sashPosition
, h
-2);
384 dc
.SetPen(*m_hilightPen
);
385 dc
.DrawLine(m_sashPosition
+1, 0, m_sashPosition
+1, h
);
387 dc
.SetPen(*m_mediumShadowPen
);
388 dc
.DrawLine(m_sashPosition
+m_sashSize
-2, 1, m_sashPosition
+m_sashSize
-2, h
-1);
390 dc
.SetPen(*m_darkShadowPen
);
391 dc
.DrawLine(m_sashPosition
+m_sashSize
-1, 2, m_sashPosition
+m_sashSize
-1, h
-2);
395 dc
.SetPen(*m_facePen
);
396 dc
.SetBrush(*m_faceBrush
);
397 dc
.DrawRectangle(0, m_sashPosition
+ 2, w
, m_sashSize
- 4);
399 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
401 dc
.SetPen(*m_lightShadowPen
);
402 dc
.DrawLine(1, m_sashPosition
, w
-2, m_sashPosition
);
404 dc
.SetPen(*m_hilightPen
);
405 dc
.DrawLine(0, m_sashPosition
+1, w
, m_sashPosition
+1);
407 dc
.SetPen(*m_mediumShadowPen
);
408 dc
.DrawLine(1, m_sashPosition
+m_sashSize
-2, w
-1, m_sashPosition
+m_sashSize
-2);
410 dc
.SetPen(*m_darkShadowPen
);
411 dc
.DrawLine(2, m_sashPosition
+m_sashSize
-1, w
-2, m_sashPosition
+m_sashSize
-1);
416 if ( m_splitMode
== wxSPLIT_VERTICAL
)
418 dc
.SetPen(*wxBLACK_PEN
);
419 dc
.SetBrush(*wxBLACK_BRUSH
);
421 if ( (GetWindowStyleFlag() & wxSP_BORDER
) != wxSP_BORDER
)
422 h1
+= 1; // Not sure why this is necessary...
423 dc
.DrawRectangle(m_sashPosition
, 0, m_sashSize
, h1
);
427 dc
.SetPen(*wxBLACK_PEN
);
428 dc
.SetBrush(*wxBLACK_BRUSH
);
430 if ( (GetWindowStyleFlag() & wxSP_BORDER
) != wxSP_BORDER
)
433 dc
.DrawRectangle(0, m_sashPosition
, w1
, m_sashSize
);
438 dc
.SetPen(wxNullPen
);
439 dc
.SetBrush(wxNullBrush
);
442 // Draw the sash tracker (for whilst moving the sash)
443 void wxSplitterWindow::DrawSashTracker(int x
, int y
)
446 GetClientSize(&w
, &h
);
452 if ( m_splitMode
== wxSPLIT_VERTICAL
)
483 ClientToScreen(&x1
, &y1
);
484 ClientToScreen(&x2
, &y2
);
486 screenDC
.SetLogicalFunction(wxXOR
);
487 screenDC
.SetPen(*m_sashTrackerPen
);
488 screenDC
.SetBrush(*wxTRANSPARENT_BRUSH
);
490 screenDC
.DrawLine(x1
, y1
, x2
, y2
);
492 screenDC
.SetLogicalFunction(wxCOPY
);
494 screenDC
.SetPen(wxNullPen
);
495 screenDC
.SetBrush(wxNullBrush
);
498 // Position and size subwindows.
499 // Note that the border size applies to each subwindow, not
500 // including the edges next to the sash.
501 void wxSplitterWindow::SizeWindows()
504 GetClientSize(&w
, &h
);
506 if ( m_windowOne
&& !m_windowTwo
)
508 m_windowOne
->SetSize(m_borderSize
, m_borderSize
, w
- 2*m_borderSize
, h
- 2*m_borderSize
);
510 if (m_windowOne
->GetAutoLayout())
511 m_windowOne
->Layout();
513 else if ( m_windowOne
&& m_windowTwo
)
515 if (m_splitMode
== wxSPLIT_VERTICAL
)
517 int x1
= m_borderSize
;
518 int y1
= m_borderSize
;
519 int w1
= m_sashPosition
- m_borderSize
;
520 int h1
= h
- 2*m_borderSize
;
522 int x2
= m_sashPosition
+ m_sashSize
;
523 int y2
= m_borderSize
;
524 int w2
= w
- 2*m_borderSize
- m_sashSize
- w1
;
525 int h2
= h
- 2*m_borderSize
;
527 m_windowOne
->SetSize(x1
, y1
, w1
, h1
);
528 m_windowTwo
->SetSize(x2
, y2
, w2
, h2
);
530 if (m_windowOne
->GetAutoLayout())
531 m_windowOne
->Layout();
532 if (m_windowTwo
->GetAutoLayout())
533 m_windowTwo
->Layout();
537 m_windowOne
->SetSize(m_borderSize
, m_borderSize
,
538 w
- 2*m_borderSize
, m_sashPosition
- m_borderSize
);
539 m_windowTwo
->SetSize(m_borderSize
, m_sashPosition
+ m_sashSize
,
540 w
- 2*m_borderSize
, h
- 2*m_borderSize
- m_sashSize
- (m_sashPosition
- m_borderSize
));
542 if (m_windowOne
->GetAutoLayout())
543 m_windowOne
->Layout();
544 if (m_windowTwo
->GetAutoLayout())
545 m_windowTwo
->Layout();
553 // Set pane for unsplit window
554 void wxSplitterWindow::Initialize(wxWindow
*window
)
556 m_windowOne
= window
;
557 m_windowTwo
= (wxWindow
*) NULL
;
561 // Associates the given window with window 2, drawing the appropriate sash
562 // and changing the split mode.
563 // Does nothing and returns FALSE if the window is already split.
564 bool wxSplitterWindow::SplitVertically(wxWindow
*window1
, wxWindow
*window2
, int sashPosition
)
570 GetClientSize(&w
, &h
);
572 m_splitMode
= wxSPLIT_VERTICAL
;
573 m_windowOne
= window1
;
574 m_windowTwo
= window2
;
575 if ( sashPosition
> 0 )
576 m_sashPosition
= sashPosition
;
577 else if ( sashPosition
< 0 )
578 m_sashPosition
= w
- sashPosition
;
580 m_sashPosition
= w
/2;
587 bool wxSplitterWindow::SplitHorizontally(wxWindow
*window1
, wxWindow
*window2
, int sashPosition
)
593 GetClientSize(&w
, &h
);
595 m_splitMode
= wxSPLIT_HORIZONTAL
;
596 m_windowOne
= window1
;
597 m_windowTwo
= window2
;
598 if ( sashPosition
> 0 )
599 m_sashPosition
= sashPosition
;
600 else if ( sashPosition
< 0 )
601 m_sashPosition
= h
- sashPosition
;
603 m_sashPosition
= h
/2;
611 // Remove the specified (or second) window from the view
612 // Doesn't actually delete the window.
613 bool wxSplitterWindow::Unsplit(wxWindow
*toRemove
)
618 wxWindow
*win
= NULL
;
619 if ( toRemove
== NULL
|| toRemove
== m_windowTwo
)
622 m_windowTwo
= (wxWindow
*) NULL
;
624 else if ( toRemove
== m_windowOne
)
627 m_windowOne
= m_windowTwo
;
628 m_windowTwo
= (wxWindow
*) NULL
;
632 wxFAIL_MSG(_T("splitter: attempt to remove a non-existent window"));
637 SendUnsplitEvent(win
);
644 // Replace a window with another one
645 bool wxSplitterWindow::ReplaceWindow(wxWindow
*winOld
, wxWindow
*winNew
)
647 wxCHECK_MSG( winOld
, FALSE
, _T("use one of Split() functions instead") );
648 wxCHECK_MSG( winNew
, FALSE
, _T("use Unsplit() functions instead") );
650 if ( winOld
== m_windowTwo
)
652 m_windowTwo
= winNew
;
654 else if ( winOld
== m_windowOne
)
656 m_windowOne
= winNew
;
660 wxFAIL_MSG(_T("splitter: attempt to replace a non-existent window"));
670 void wxSplitterWindow::SetSashPosition(int position
, bool redraw
)
672 m_sashPosition
= position
;
680 // Initialize colours
681 void wxSplitterWindow::InitColours()
683 wxDELETE( m_facePen
);
684 wxDELETE( m_faceBrush
);
685 wxDELETE( m_mediumShadowPen
);
686 wxDELETE( m_darkShadowPen
);
687 wxDELETE( m_lightShadowPen
);
688 wxDELETE( m_hilightPen
);
691 #if defined(__WIN95__)
692 wxColour
faceColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
693 m_facePen
= new wxPen(faceColour
, 1, wxSOLID
);
694 m_faceBrush
= new wxBrush(faceColour
, wxSOLID
);
696 wxColour
mediumShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DSHADOW
));
697 m_mediumShadowPen
= new wxPen(mediumShadowColour
, 1, wxSOLID
);
699 wxColour
darkShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DDKSHADOW
));
700 m_darkShadowPen
= new wxPen(darkShadowColour
, 1, wxSOLID
);
702 wxColour
lightShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT
));
703 m_lightShadowPen
= new wxPen(lightShadowColour
, 1, wxSOLID
);
705 wxColour
hilightColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DHILIGHT
));
706 m_hilightPen
= new wxPen(hilightColour
, 1, wxSOLID
);
708 m_facePen
= new wxPen("LIGHT GREY", 1, wxSOLID
);
709 m_faceBrush
= new wxBrush("LIGHT GREY", wxSOLID
);
710 m_mediumShadowPen
= new wxPen("GREY", 1, wxSOLID
);
711 m_darkShadowPen
= new wxPen("BLACK", 1, wxSOLID
);
712 m_lightShadowPen
= new wxPen("LIGHT GREY", 1, wxSOLID
);
713 m_hilightPen
= new wxPen("WHITE", 1, wxSOLID
);
714 #endif // Win32/!Win32
717 void wxSplitterWindow::SendUnsplitEvent(wxWindow
*winRemoved
)
719 wxSplitterEvent
event(wxEVT_COMMAND_SPLITTER_UNSPLIT
, this);
720 event
.m_data
.win
= winRemoved
;
722 (void)GetEventHandler()->ProcessEvent(event
);
725 // ---------------------------------------------------------------------------
726 // splitter event handlers
727 // ---------------------------------------------------------------------------
729 void wxSplitterWindow::OnSashPosChanged(wxSplitterEvent
& event
)
731 // If within UNSPLIT_THRESHOLD from edge, set to edge to cause closure.
732 const int UNSPLIT_THRESHOLD
= 4;
734 int newSashPosition
= event
.GetSashPosition();
736 // Obtain relevant window dimension for bottom / right threshold check
738 GetClientSize(&w
, &h
);
739 int window_size
= (m_splitMode
== wxSPLIT_VERTICAL
) ? w
: h
;
741 if ( newSashPosition
<= UNSPLIT_THRESHOLD
)
743 // threshold top / left check
746 else if ( newSashPosition
>= window_size
- UNSPLIT_THRESHOLD
)
748 // threshold bottom/right check
749 newSashPosition
= window_size
;
752 // If resultant pane would be too small, enlarge it.
754 // Check upper / left pane
755 if ( newSashPosition
< m_minimumPaneSize
)
756 newSashPosition
= m_minimumPaneSize
;
758 // Check lower / right pane (check even if sash was just adjusted)
759 if ( newSashPosition
> window_size
- m_minimumPaneSize
)
760 newSashPosition
= window_size
- m_minimumPaneSize
;
762 // If the result is out of bounds it means minimum size is too big,
763 // so split window in half as best compromise.
764 if ( newSashPosition
< 0 || newSashPosition
> window_size
)
765 newSashPosition
= window_size
/ 2;
767 // for compatibility, call the virtual function
768 if ( !OnSashPositionChange(newSashPosition
) )
770 newSashPosition
= -1;
773 event
.SetSashPosition(newSashPosition
);
776 // Called when the sash is double-clicked. The default behaviour is to remove
777 // the sash if the minimum pane size is zero.
778 void wxSplitterWindow::OnDoubleClick(wxSplitterEvent
& event
)
780 // for compatibility, call the virtual function
781 OnDoubleClickSash(event
.GetX(), event
.GetY());
783 if ( GetMinimumPaneSize() == 0 )
789 void wxSplitterWindow::OnUnsplitEvent(wxSplitterEvent
& event
)
791 wxWindow
*win
= event
.GetWindowBeingRemoved();
793 // for compatibility, call the virtual function