]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/splitter.cpp
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"
30 #include "wx/string.h"
31 #include "wx/splitter.h"
32 #include "wx/dcscreen.h"
34 #if !USE_SHARED_LIBRARY
35 IMPLEMENT_DYNAMIC_CLASS(wxSplitterWindow
, wxWindow
)
37 BEGIN_EVENT_TABLE(wxSplitterWindow
, wxWindow
)
38 EVT_PAINT(wxSplitterWindow::OnPaint
)
39 EVT_SIZE(wxSplitterWindow::OnSize
)
40 EVT_MOUSE_EVENTS(wxSplitterWindow::OnMouseEvent
)
44 wxSplitterWindow::wxSplitterWindow()
46 m_splitMode
= wxSPLIT_VERTICAL
;
47 m_windowOne
= (wxWindow
*) NULL
;
48 m_windowTwo
= (wxWindow
*) NULL
;
49 m_dragMode
= wxSPLIT_DRAG_NONE
;
57 m_sashCursorWE
= (wxCursor
*) NULL
;
58 m_sashCursorNS
= (wxCursor
*) NULL
;
59 m_sashTrackerPen
= (wxPen
*) NULL
;
60 m_lightShadowPen
= (wxPen
*) NULL
;
61 m_mediumShadowPen
= (wxPen
*) NULL
;
62 m_darkShadowPen
= (wxPen
*) NULL
;
63 m_faceBrush
= (wxBrush
*) NULL
;
64 m_facePen
= (wxPen
*) NULL
;
65 m_hilightPen
= (wxPen
*) NULL
;
66 m_minimumPaneSize
= 0;
69 wxSplitterWindow::wxSplitterWindow(wxWindow
*parent
, wxWindowID id
,
74 : wxWindow(parent
, id
, pos
, size
, style
, name
)
76 m_splitMode
= wxSPLIT_VERTICAL
;
77 m_windowOne
= (wxWindow
*) NULL
;
78 m_windowTwo
= (wxWindow
*) NULL
;
79 m_dragMode
= wxSPLIT_DRAG_NONE
;
87 m_minimumPaneSize
= 0;
88 m_sashCursorWE
= new wxCursor(wxCURSOR_SIZEWE
);
89 m_sashCursorNS
= new wxCursor(wxCURSOR_SIZENS
);
90 m_sashTrackerPen
= new wxPen(*wxBLACK
, 2, wxSOLID
);
91 m_lightShadowPen
= (wxPen
*) NULL
;
92 m_mediumShadowPen
= (wxPen
*) NULL
;
93 m_darkShadowPen
= (wxPen
*) NULL
;
94 m_faceBrush
= (wxBrush
*) NULL
;
95 m_facePen
= (wxPen
*) NULL
;
96 m_hilightPen
= (wxPen
*) NULL
;
98 if ( style
& wxSP_3D
)
103 else if ( style
& wxSP_BORDER
)
114 // Eventually, we'll respond to colour change messages
117 // For debugging purposes, to see the background.
118 // SetBackground(wxBLUE_BRUSH);
121 wxSplitterWindow::~wxSplitterWindow()
123 delete m_sashCursorWE
;
124 delete m_sashCursorNS
;
125 delete m_sashTrackerPen
;
126 delete m_lightShadowPen
;
127 delete m_darkShadowPen
;
128 delete m_mediumShadowPen
;
134 void wxSplitterWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
138 if ( m_borderSize
> 0 )
143 void wxSplitterWindow::OnMouseEvent(wxMouseEvent
& event
)
146 event
.Position(&x
, &y
);
150 SetCursor(* wxSTANDARD_CURSOR
);
153 SetCursor(wxCursor());
156 // under wxGTK the method above causes the mouse
157 // to flicker so we set the standard cursor only
158 // when leaving the window and when moving over
159 // non-sash parts of the window. this should work
160 // on the other platforms as well, but who knows.
163 SetCursor(* wxSTANDARD_CURSOR
);
166 if (event
.LeftDown())
168 if ( SashHitTest(x
, y
) )
172 m_dragMode
= wxSPLIT_DRAG_DRAGGING
;
174 DrawSashTracker(x
, y
);
180 else if (event
.LeftUp() && m_dragMode
== wxSPLIT_DRAG_DRAGGING
)
182 // We can stop dragging now and see what we've got.
183 m_dragMode
= wxSPLIT_DRAG_NONE
;
187 DrawSashTracker(m_oldX
, m_oldY
);
190 GetClientSize(&w
, &h
);
191 if ( m_splitMode
== wxSPLIT_VERTICAL
)
193 if ( !OnSashPositionChange(x
) )
198 // We remove the first window from the view
199 wxWindow
*removedWindow
= m_windowOne
;
200 m_windowOne
= m_windowTwo
;
201 m_windowTwo
= (wxWindow
*) NULL
;
203 OnUnsplit(removedWindow
);
206 else if ( x
>= (w
- 4) )
208 // We remove the second window from the view
209 wxWindow
*removedWindow
= m_windowTwo
;
210 m_windowTwo
= (wxWindow
*) NULL
;
211 OnUnsplit(removedWindow
);
219 else // m_splitMode == wxSPLIT_VERTICAL
221 if ( !OnSashPositionChange(y
) )
226 // We remove the first window from the view
227 wxWindow
*removedWindow
= m_windowOne
;
228 m_windowOne
= m_windowTwo
;
229 m_windowTwo
= (wxWindow
*) NULL
;
231 OnUnsplit(removedWindow
);
234 else if ( y
>= (h
- 4) )
236 // We remove the second window from the view
237 wxWindow
*removedWindow
= m_windowTwo
;
238 m_windowTwo
= (wxWindow
*) NULL
;
239 OnUnsplit(removedWindow
);
246 } // m_splitMode == wxSPLIT_VERTICAL
248 } // left up && dragging
249 else if (event
.Moving() && !event
.Dragging())
251 // Just change the cursor if required
252 if ( SashHitTest(x
, y
) )
254 if ( m_splitMode
== wxSPLIT_VERTICAL
)
256 SetCursor(*m_sashCursorWE
);
260 SetCursor(*m_sashCursorNS
);
266 // where else do we unset the cursor?
267 SetCursor(* wxSTANDARD_CURSOR
);
271 else if (event
.Dragging() && (m_dragMode
== wxSPLIT_DRAG_DRAGGING
))
274 DrawSashTracker(m_oldX
, m_oldY
);
277 DrawSashTracker(x
, y
);
282 else if ( event
.LeftDClick() )
284 OnDoubleClickSash(x
, y
);
288 void wxSplitterWindow::OnSize(wxSizeEvent
& WXUNUSED(event
))
291 GetClientSize( &cw
, &ch
);
294 if ( m_splitMode
== wxSPLIT_VERTICAL
)
296 if ( m_sashPosition
>= (cw
- 5) )
297 m_sashPosition
= wxMax(10, cw
- 40);
299 if ( m_splitMode
== wxSPLIT_HORIZONTAL
)
301 if ( m_sashPosition
>= (ch
- 5) )
302 m_sashPosition
= wxMax(10, ch
- 40);
308 bool wxSplitterWindow::SashHitTest(int x
, int y
, int tolerance
)
310 if ( m_windowTwo
== NULL
|| m_sashPosition
== 0)
311 return FALSE
; // No sash
313 if ( m_splitMode
== wxSPLIT_VERTICAL
)
315 if ( (x
>= m_sashPosition
- tolerance
) && (x
<= m_sashPosition
+ m_sashSize
+ tolerance
) )
322 if ( (y
>= (m_sashPosition
- tolerance
)) && (y
<= (m_sashPosition
+ m_sashSize
+ tolerance
)) )
331 // Draw 3D effect borders
332 void wxSplitterWindow::DrawBorders(wxDC
& dc
)
335 GetClientSize(&w
, &h
);
337 if ( GetWindowStyleFlag() & wxSP_3D
)
339 dc
.SetPen(*m_mediumShadowPen
);
340 dc
.DrawLine(0, 0, w
-1, 0);
341 dc
.DrawLine(0, 0, 0, h
- 1);
343 dc
.SetPen(*m_darkShadowPen
);
344 dc
.DrawLine(1, 1, w
-2, 1);
345 dc
.DrawLine(1, 1, 1, h
-2);
347 dc
.SetPen(*m_hilightPen
);
348 dc
.DrawLine(0, h
-1, w
-1, h
-1);
349 dc
.DrawLine(w
-1, 0, w
-1, h
); // Surely the maximum y pos. should be h - 1.
350 /// Anyway, h is required for MSW.
352 dc
.SetPen(*m_lightShadowPen
);
353 dc
.DrawLine(w
-2, 1, w
-2, h
-2); // Right hand side
354 dc
.DrawLine(1, h
-2, w
-1, h
-2); // Bottom
356 else if ( GetWindowStyleFlag() & wxSP_BORDER
)
358 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
359 dc
.SetPen(*wxBLACK_PEN
);
360 dc
.DrawRectangle(0, 0, w
-1, h
-1);
363 dc
.SetPen(wxNullPen
);
364 dc
.SetBrush(wxNullBrush
);
368 void wxSplitterWindow::DrawSash(wxDC
& dc
)
370 if ( m_sashPosition
== 0 || !m_windowTwo
)
374 GetClientSize(&w
, &h
);
376 if ( GetWindowStyleFlag() & wxSP_3D
)
378 if ( m_splitMode
== wxSPLIT_VERTICAL
)
380 dc
.SetPen(*m_facePen
);
381 dc
.SetBrush(*m_faceBrush
);
382 dc
.DrawRectangle(m_sashPosition
+ 2, 0, m_sashSize
- 4, h
);
384 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
386 dc
.SetPen(*m_lightShadowPen
);
387 dc
.DrawLine(m_sashPosition
, 1, m_sashPosition
, h
-2);
389 dc
.SetPen(*m_hilightPen
);
390 dc
.DrawLine(m_sashPosition
+1, 0, m_sashPosition
+1, h
);
392 dc
.SetPen(*m_mediumShadowPen
);
393 dc
.DrawLine(m_sashPosition
+m_sashSize
-2, 1, m_sashPosition
+m_sashSize
-2, h
-1);
395 dc
.SetPen(*m_darkShadowPen
);
396 dc
.DrawLine(m_sashPosition
+m_sashSize
-1, 2, m_sashPosition
+m_sashSize
-1, h
-2);
400 dc
.SetPen(*m_facePen
);
401 dc
.SetBrush(*m_faceBrush
);
402 dc
.DrawRectangle(0, m_sashPosition
+ 2, w
, m_sashSize
- 4);
404 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
406 dc
.SetPen(*m_lightShadowPen
);
407 dc
.DrawLine(1, m_sashPosition
, w
-2, m_sashPosition
);
409 dc
.SetPen(*m_hilightPen
);
410 dc
.DrawLine(0, m_sashPosition
+1, w
, m_sashPosition
+1);
412 dc
.SetPen(*m_mediumShadowPen
);
413 dc
.DrawLine(1, m_sashPosition
+m_sashSize
-2, w
-1, m_sashPosition
+m_sashSize
-2);
415 dc
.SetPen(*m_darkShadowPen
);
416 dc
.DrawLine(2, m_sashPosition
+m_sashSize
-1, w
-2, m_sashPosition
+m_sashSize
-1);
421 if ( m_splitMode
== wxSPLIT_VERTICAL
)
423 dc
.SetPen(*wxBLACK_PEN
);
424 dc
.SetBrush(*wxBLACK_BRUSH
);
426 if ( (GetWindowStyleFlag() & wxSP_BORDER
) != wxSP_BORDER
)
427 h1
+= 1; // Not sure why this is necessary...
428 dc
.DrawRectangle(m_sashPosition
, 0, m_sashSize
, h1
);
432 dc
.SetPen(*wxBLACK_PEN
);
433 dc
.SetBrush(*wxBLACK_BRUSH
);
435 if ( (GetWindowStyleFlag() & wxSP_BORDER
) != wxSP_BORDER
)
438 dc
.DrawRectangle(0, m_sashPosition
, w1
, m_sashSize
);
443 dc
.SetPen(wxNullPen
);
444 dc
.SetBrush(wxNullBrush
);
447 // Draw the sash tracker (for whilst moving the sash)
448 void wxSplitterWindow::DrawSashTracker(int x
, int y
)
451 GetClientSize(&w
, &h
);
457 if ( m_splitMode
== wxSPLIT_VERTICAL
)
488 ClientToScreen(&x1
, &y1
);
489 ClientToScreen(&x2
, &y2
);
491 screenDC
.SetLogicalFunction(wxXOR
);
492 screenDC
.SetPen(*m_sashTrackerPen
);
493 screenDC
.SetBrush(*wxTRANSPARENT_BRUSH
);
495 screenDC
.DrawLine(x1
, y1
, x2
, y2
);
497 screenDC
.SetLogicalFunction(wxCOPY
);
499 screenDC
.SetPen(wxNullPen
);
500 screenDC
.SetBrush(wxNullBrush
);
503 // Position and size subwindows.
504 // Note that the border size applies to each subwindow, not
505 // including the edges next to the sash.
506 void wxSplitterWindow::SizeWindows()
509 GetClientSize(&w
, &h
);
511 if ( m_windowOne
&& !m_windowTwo
)
513 m_windowOne
->SetSize(m_borderSize
, m_borderSize
, w
- 2*m_borderSize
, h
- 2*m_borderSize
);
515 else if ( m_windowOne
&& m_windowTwo
)
517 if (m_splitMode
== wxSPLIT_VERTICAL
)
519 int x1
= m_borderSize
;
520 int y1
= m_borderSize
;
521 int w1
= m_sashPosition
- m_borderSize
;
522 int h1
= h
- 2*m_borderSize
;
524 int x2
= m_sashPosition
+ m_sashSize
;
525 int y2
= m_borderSize
;
526 int w2
= w
- 2*m_borderSize
- m_sashSize
- w1
;
527 int h2
= h
- 2*m_borderSize
;
529 m_windowOne
->SetSize(x1
, y1
, w1
, h1
);
530 m_windowTwo
->SetSize(x2
, y2
, w2
, h2
);
534 m_windowOne
->SetSize(m_borderSize
, m_borderSize
,
535 w
- 2*m_borderSize
, m_sashPosition
- m_borderSize
);
536 m_windowTwo
->SetSize(m_borderSize
, m_sashPosition
+ m_sashSize
,
537 w
- 2*m_borderSize
, h
- 2*m_borderSize
- m_sashSize
- (m_sashPosition
- m_borderSize
));
545 // Set pane for unsplit window
546 void wxSplitterWindow::Initialize(wxWindow
*window
)
548 m_windowOne
= window
;
549 m_windowTwo
= (wxWindow
*) NULL
;
553 // Associates the given window with window 2, drawing the appropriate sash
554 // and changing the split mode.
555 // Does nothing and returns FALSE if the window is already split.
556 bool wxSplitterWindow::SplitVertically(wxWindow
*window1
, wxWindow
*window2
, int sashPosition
)
562 GetClientSize(&w
, &h
);
564 m_splitMode
= wxSPLIT_VERTICAL
;
565 m_windowOne
= window1
;
566 m_windowTwo
= window2
;
567 if ( sashPosition
> 0 )
568 m_sashPosition
= sashPosition
;
569 else if ( sashPosition
< 0 )
570 m_sashPosition
= w
- sashPosition
;
572 m_sashPosition
= w
/2;
579 bool wxSplitterWindow::SplitHorizontally(wxWindow
*window1
, wxWindow
*window2
, int sashPosition
)
585 GetClientSize(&w
, &h
);
587 m_splitMode
= wxSPLIT_HORIZONTAL
;
588 m_windowOne
= window1
;
589 m_windowTwo
= window2
;
590 if ( sashPosition
> 0 )
591 m_sashPosition
= sashPosition
;
592 else if ( sashPosition
< 0 )
593 m_sashPosition
= h
- sashPosition
;
595 m_sashPosition
= h
/2;
603 // Remove the specified (or second) window from the view
604 // Doesn't actually delete the window.
605 bool wxSplitterWindow::Unsplit(wxWindow
*toRemove
)
610 wxWindow
*win
= NULL
;
611 if ( toRemove
== NULL
|| toRemove
== m_windowTwo
)
614 m_windowTwo
= (wxWindow
*) NULL
;
616 else if ( toRemove
== m_windowOne
)
619 m_windowOne
= m_windowTwo
;
620 m_windowTwo
= (wxWindow
*) NULL
;
624 wxFAIL_MSG(_T("splitter: attempt to remove a non-existent window"));
636 // Replace a window with another one
637 bool wxSplitterWindow::ReplaceWindow(wxWindow
*winOld
, wxWindow
*winNew
)
639 wxCHECK_MSG( winOld
, FALSE
, _T("use one of Split() functions instead") );
640 wxCHECK_MSG( winNew
, FALSE
, _T("use Unsplit() functions instead") );
642 if ( winOld
== m_windowTwo
)
644 m_windowTwo
= winNew
;
646 else if ( winOld
== m_windowOne
)
648 m_windowOne
= winNew
;
652 wxFAIL_MSG(_T("splitter: attempt to replace a non-existent window"));
662 void wxSplitterWindow::SetSashPosition(int position
, bool redraw
)
664 m_sashPosition
= position
;
672 bool wxSplitterWindow::OnSashPositionChange(int newSashPosition
)
674 // is the left/upper pane too small?
675 if ( newSashPosition
< m_minimumPaneSize
)
678 // is the right/lower pane too small?
680 GetClientSize(&w
, &h
);
682 if ( m_splitMode
== wxSPLIT_VERTICAL
)
684 if ( w
- newSashPosition
< m_minimumPaneSize
)
687 else // m_splitMode = wxSPLIT_HORIZONTAL
689 if ( h
- newSashPosition
< m_minimumPaneSize
)
693 // it's ok to move sash
697 // Called when the sash is double-clicked.
698 // The default behaviour is to remove the sash if the
699 // minimum pane size is zero.
700 void wxSplitterWindow::OnDoubleClickSash(int WXUNUSED(x
), int WXUNUSED(y
) )
702 if ( GetMinimumPaneSize() == 0 )
708 // Initialize colours
709 void wxSplitterWindow::InitColours()
711 wxDELETE( m_facePen
);
712 wxDELETE( m_faceBrush
);
713 wxDELETE( m_mediumShadowPen
);
714 wxDELETE( m_darkShadowPen
);
715 wxDELETE( m_lightShadowPen
);
716 wxDELETE( m_hilightPen
);
719 #if defined(__WIN95__)
720 wxColour
faceColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
721 m_facePen
= new wxPen(faceColour
, 1, wxSOLID
);
722 m_faceBrush
= new wxBrush(faceColour
, wxSOLID
);
724 wxColour
mediumShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DSHADOW
));
725 m_mediumShadowPen
= new wxPen(mediumShadowColour
, 1, wxSOLID
);
727 wxColour
darkShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DDKSHADOW
));
728 m_darkShadowPen
= new wxPen(darkShadowColour
, 1, wxSOLID
);
730 wxColour
lightShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT
));
731 m_lightShadowPen
= new wxPen(lightShadowColour
, 1, wxSOLID
);
733 wxColour
hilightColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DHILIGHT
));
734 m_hilightPen
= new wxPen(hilightColour
, 1, wxSOLID
);
736 m_facePen
= new wxPen("LIGHT GREY", 1, wxSOLID
);
737 m_faceBrush
= new wxBrush("LIGHT GREY", wxSOLID
);
738 m_mediumShadowPen
= new wxPen("GREY", 1, wxSOLID
);
739 m_darkShadowPen
= new wxPen("BLACK", 1, wxSOLID
);
740 m_lightShadowPen
= new wxPen("LIGHT GREY", 1, wxSOLID
);
741 m_hilightPen
= new wxPen("WHITE", 1, wxSOLID
);
742 #endif // Win32/!Win32