]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/splitter.cpp
c52c7e75862de163505530cc8be3e05cf0740572
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 if (event
.LeftDown())
158 if ( SashHitTest(x
, y
) )
162 m_dragMode
= wxSPLIT_DRAG_DRAGGING
;
164 DrawSashTracker(x
, y
);
170 else if (event
.LeftUp() && m_dragMode
== wxSPLIT_DRAG_DRAGGING
)
172 // We can stop dragging now and see what we've got.
173 m_dragMode
= wxSPLIT_DRAG_NONE
;
177 SetCursor(* wxSTANDARD_CURSOR
);
180 DrawSashTracker(m_oldX
, m_oldY
);
183 GetClientSize(&w
, &h
);
184 if ( m_splitMode
== wxSPLIT_VERTICAL
)
186 if ( !OnSashPositionChange(x
) )
191 // We remove the first window from the view
192 wxWindow
*removedWindow
= m_windowOne
;
193 m_windowOne
= m_windowTwo
;
194 m_windowTwo
= (wxWindow
*) NULL
;
196 OnUnsplit(removedWindow
);
199 else if ( x
>= (w
- 4) )
201 // We remove the second window from the view
202 wxWindow
*removedWindow
= m_windowTwo
;
203 m_windowTwo
= (wxWindow
*) NULL
;
204 OnUnsplit(removedWindow
);
212 else // m_splitMode == wxSPLIT_VERTICAL
214 if ( !OnSashPositionChange(y
) )
219 // We remove the first window from the view
220 wxWindow
*removedWindow
= m_windowOne
;
221 m_windowOne
= m_windowTwo
;
222 m_windowTwo
= (wxWindow
*) NULL
;
224 OnUnsplit(removedWindow
);
227 else if ( y
>= (h
- 4) )
229 // We remove the second window from the view
230 wxWindow
*removedWindow
= m_windowTwo
;
231 m_windowTwo
= (wxWindow
*) NULL
;
232 OnUnsplit(removedWindow
);
239 } // m_splitMode == wxSPLIT_VERTICAL
241 } // left up && dragging
242 else if (event
.Moving() && !event
.Dragging())
244 // Just change the cursor if required
245 if ( SashHitTest(x
, y
) )
247 if ( m_splitMode
== wxSPLIT_VERTICAL
)
249 SetCursor(*m_sashCursorWE
);
253 SetCursor(*m_sashCursorNS
);
257 else if (event
.Dragging() && (m_dragMode
== wxSPLIT_DRAG_DRAGGING
))
260 DrawSashTracker(m_oldX
, m_oldY
);
263 DrawSashTracker(x
, y
);
268 else if ( event
.LeftDClick() )
270 OnDoubleClickSash(x
, y
);
274 void wxSplitterWindow::OnSize(wxSizeEvent
& WXUNUSED(event
))
277 GetClientSize( &cw
, &ch
);
280 if ( m_splitMode
== wxSPLIT_VERTICAL
)
282 if ( m_sashPosition
>= (cw
- 5) )
283 m_sashPosition
= wxMax(10, cw
- 40);
285 if ( m_splitMode
== wxSPLIT_HORIZONTAL
)
287 if ( m_sashPosition
>= (ch
- 5) )
288 m_sashPosition
= wxMax(10, ch
- 40);
294 bool wxSplitterWindow::SashHitTest(int x
, int y
, int tolerance
)
296 if ( m_windowTwo
== NULL
|| m_sashPosition
== 0)
297 return FALSE
; // No sash
299 if ( m_splitMode
== wxSPLIT_VERTICAL
)
301 if ( (x
>= m_sashPosition
- tolerance
) && (x
<= m_sashPosition
+ m_sashSize
+ tolerance
) )
308 if ( (y
>= (m_sashPosition
- tolerance
)) && (y
<= (m_sashPosition
+ m_sashSize
+ tolerance
)) )
317 // Draw 3D effect borders
318 void wxSplitterWindow::DrawBorders(wxDC
& dc
)
321 GetClientSize(&w
, &h
);
323 if ( GetWindowStyleFlag() & wxSP_3D
)
325 dc
.SetPen(*m_mediumShadowPen
);
326 dc
.DrawLine(0, 0, w
-1, 0);
327 dc
.DrawLine(0, 0, 0, h
- 1);
329 dc
.SetPen(*m_darkShadowPen
);
330 dc
.DrawLine(1, 1, w
-2, 1);
331 dc
.DrawLine(1, 1, 1, h
-2);
333 dc
.SetPen(*m_hilightPen
);
334 dc
.DrawLine(0, h
-1, w
-1, h
-1);
335 dc
.DrawLine(w
-1, 0, w
-1, h
); // Surely the maximum y pos. should be h - 1.
336 /// Anyway, h is required for MSW.
338 dc
.SetPen(*m_lightShadowPen
);
339 dc
.DrawLine(w
-2, 1, w
-2, h
-2); // Right hand side
340 dc
.DrawLine(1, h
-2, w
-1, h
-2); // Bottom
342 else if ( GetWindowStyleFlag() & wxSP_BORDER
)
344 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
345 dc
.SetPen(*wxBLACK_PEN
);
346 dc
.DrawRectangle(0, 0, w
-1, h
-1);
349 dc
.SetPen(wxNullPen
);
350 dc
.SetBrush(wxNullBrush
);
354 void wxSplitterWindow::DrawSash(wxDC
& dc
)
356 if ( m_sashPosition
== 0 || !m_windowTwo
)
360 GetClientSize(&w
, &h
);
362 if ( GetWindowStyleFlag() & wxSP_3D
)
364 if ( m_splitMode
== wxSPLIT_VERTICAL
)
366 dc
.SetPen(*m_facePen
);
367 dc
.SetBrush(*m_faceBrush
);
368 dc
.DrawRectangle(m_sashPosition
+ 2, 0, m_sashSize
- 4, h
);
370 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
372 dc
.SetPen(*m_lightShadowPen
);
373 dc
.DrawLine(m_sashPosition
, 1, m_sashPosition
, h
-2);
375 dc
.SetPen(*m_hilightPen
);
376 dc
.DrawLine(m_sashPosition
+1, 0, m_sashPosition
+1, h
);
378 dc
.SetPen(*m_mediumShadowPen
);
379 dc
.DrawLine(m_sashPosition
+m_sashSize
-2, 1, m_sashPosition
+m_sashSize
-2, h
-1);
381 dc
.SetPen(*m_darkShadowPen
);
382 dc
.DrawLine(m_sashPosition
+m_sashSize
-1, 2, m_sashPosition
+m_sashSize
-1, h
-2);
386 dc
.SetPen(*m_facePen
);
387 dc
.SetBrush(*m_faceBrush
);
388 dc
.DrawRectangle(0, m_sashPosition
+ 2, w
, m_sashSize
- 4);
390 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
392 dc
.SetPen(*m_lightShadowPen
);
393 dc
.DrawLine(1, m_sashPosition
, w
-2, m_sashPosition
);
395 dc
.SetPen(*m_hilightPen
);
396 dc
.DrawLine(0, m_sashPosition
+1, w
, m_sashPosition
+1);
398 dc
.SetPen(*m_mediumShadowPen
);
399 dc
.DrawLine(1, m_sashPosition
+m_sashSize
-2, w
-1, m_sashPosition
+m_sashSize
-2);
401 dc
.SetPen(*m_darkShadowPen
);
402 dc
.DrawLine(2, m_sashPosition
+m_sashSize
-1, w
-2, m_sashPosition
+m_sashSize
-1);
407 if ( m_splitMode
== wxSPLIT_VERTICAL
)
409 dc
.SetPen(*wxBLACK_PEN
);
410 dc
.SetBrush(*wxBLACK_BRUSH
);
412 if ( (GetWindowStyleFlag() & wxSP_BORDER
) != wxSP_BORDER
)
413 h1
+= 1; // Not sure why this is necessary...
414 dc
.DrawRectangle(m_sashPosition
, 0, m_sashSize
, h1
);
418 dc
.SetPen(*wxBLACK_PEN
);
419 dc
.SetBrush(*wxBLACK_BRUSH
);
421 if ( (GetWindowStyleFlag() & wxSP_BORDER
) != wxSP_BORDER
)
424 dc
.DrawRectangle(0, m_sashPosition
, w1
, m_sashSize
);
429 dc
.SetPen(wxNullPen
);
430 dc
.SetBrush(wxNullBrush
);
433 // Draw the sash tracker (for whilst moving the sash)
434 void wxSplitterWindow::DrawSashTracker(int x
, int y
)
437 GetClientSize(&w
, &h
);
443 if ( m_splitMode
== wxSPLIT_VERTICAL
)
474 ClientToScreen(&x1
, &y1
);
475 ClientToScreen(&x2
, &y2
);
477 screenDC
.SetLogicalFunction(wxXOR
);
478 screenDC
.SetPen(*m_sashTrackerPen
);
479 screenDC
.SetBrush(*wxTRANSPARENT_BRUSH
);
481 screenDC
.DrawLine(x1
, y1
, x2
, y2
);
483 screenDC
.SetLogicalFunction(wxCOPY
);
485 screenDC
.SetPen(wxNullPen
);
486 screenDC
.SetBrush(wxNullBrush
);
489 // Position and size subwindows.
490 // Note that the border size applies to each subwindow, not
491 // including the edges next to the sash.
492 void wxSplitterWindow::SizeWindows()
495 GetClientSize(&w
, &h
);
497 if ( m_windowOne
&& !m_windowTwo
)
499 m_windowOne
->SetSize(m_borderSize
, m_borderSize
, w
- 2*m_borderSize
, h
- 2*m_borderSize
);
501 else if ( m_windowOne
&& m_windowTwo
)
503 if (m_splitMode
== wxSPLIT_VERTICAL
)
505 int x1
= m_borderSize
;
506 int y1
= m_borderSize
;
507 int w1
= m_sashPosition
- m_borderSize
;
508 int h1
= h
- 2*m_borderSize
;
510 int x2
= m_sashPosition
+ m_sashSize
;
511 int y2
= m_borderSize
;
512 int w2
= w
- 2*m_borderSize
- m_sashSize
- w1
;
513 int h2
= h
- 2*m_borderSize
;
515 m_windowOne
->SetSize(x1
, y1
, w1
, h1
);
516 m_windowTwo
->SetSize(x2
, y2
, w2
, h2
);
520 m_windowOne
->SetSize(m_borderSize
, m_borderSize
,
521 w
- 2*m_borderSize
, m_sashPosition
- m_borderSize
);
522 m_windowTwo
->SetSize(m_borderSize
, m_sashPosition
+ m_sashSize
,
523 w
- 2*m_borderSize
, h
- 2*m_borderSize
- m_sashSize
- (m_sashPosition
- m_borderSize
));
531 // Set pane for unsplit window
532 void wxSplitterWindow::Initialize(wxWindow
*window
)
534 m_windowOne
= window
;
535 m_windowTwo
= (wxWindow
*) NULL
;
539 // Associates the given window with window 2, drawing the appropriate sash
540 // and changing the split mode.
541 // Does nothing and returns FALSE if the window is already split.
542 bool wxSplitterWindow::SplitVertically(wxWindow
*window1
, wxWindow
*window2
, int sashPosition
)
548 GetClientSize(&w
, &h
);
550 m_splitMode
= wxSPLIT_VERTICAL
;
551 m_windowOne
= window1
;
552 m_windowTwo
= window2
;
553 if ( sashPosition
> 0 )
554 m_sashPosition
= sashPosition
;
555 else if ( sashPosition
< 0 )
556 m_sashPosition
= w
- sashPosition
;
558 m_sashPosition
= w
/2;
565 bool wxSplitterWindow::SplitHorizontally(wxWindow
*window1
, wxWindow
*window2
, int sashPosition
)
571 GetClientSize(&w
, &h
);
573 m_splitMode
= wxSPLIT_HORIZONTAL
;
574 m_windowOne
= window1
;
575 m_windowTwo
= window2
;
576 if ( sashPosition
> 0 )
577 m_sashPosition
= sashPosition
;
578 else if ( sashPosition
< 0 )
579 m_sashPosition
= h
- sashPosition
;
581 m_sashPosition
= h
/2;
589 // Remove the specified (or second) window from the view
590 // Doesn't actually delete the window.
591 bool wxSplitterWindow::Unsplit(wxWindow
*toRemove
)
596 wxWindow
*win
= NULL
;
597 if ( toRemove
== NULL
|| toRemove
== m_windowTwo
)
600 m_windowTwo
= (wxWindow
*) NULL
;
602 else if ( toRemove
== m_windowOne
)
605 m_windowOne
= m_windowTwo
;
606 m_windowTwo
= (wxWindow
*) NULL
;
610 wxFAIL_MSG(_T("splitter: attempt to remove a non-existent window"));
622 // Replace a window with another one
623 bool wxSplitterWindow::ReplaceWindow(wxWindow
*winOld
, wxWindow
*winNew
)
625 wxCHECK_MSG( winOld
, FALSE
, _T("use one of Split() functions instead") );
626 wxCHECK_MSG( winNew
, FALSE
, _T("use Unsplit() functions instead") );
628 if ( winOld
== m_windowTwo
)
630 m_windowTwo
= winNew
;
632 else if ( winOld
== m_windowOne
)
634 m_windowOne
= winNew
;
638 wxFAIL_MSG(_T("splitter: attempt to replace a non-existent window"));
648 void wxSplitterWindow::SetSashPosition(int position
, bool redraw
)
650 m_sashPosition
= position
;
658 bool wxSplitterWindow::OnSashPositionChange(int newSashPosition
)
660 // is the left/upper pane too small?
661 if ( newSashPosition
< m_minimumPaneSize
)
664 // is the right/lower pane too small?
666 GetClientSize(&w
, &h
);
668 if ( m_splitMode
== wxSPLIT_VERTICAL
)
670 if ( w
- newSashPosition
< m_minimumPaneSize
)
673 else // m_splitMode = wxSPLIT_HORIZONTAL
675 if ( h
- newSashPosition
< m_minimumPaneSize
)
679 // it's ok to move sash
683 // Called when the sash is double-clicked.
684 // The default behaviour is to remove the sash if the
685 // minimum pane size is zero.
686 void wxSplitterWindow::OnDoubleClickSash(int WXUNUSED(x
), int WXUNUSED(y
) )
688 if ( GetMinimumPaneSize() == 0 )
694 // Initialize colours
695 void wxSplitterWindow::InitColours()
697 wxDELETE( m_facePen
);
698 wxDELETE( m_faceBrush
);
699 wxDELETE( m_mediumShadowPen
);
700 wxDELETE( m_darkShadowPen
);
701 wxDELETE( m_lightShadowPen
);
702 wxDELETE( m_hilightPen
);
705 #if defined(__WIN95__)
706 wxColour
faceColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
707 m_facePen
= new wxPen(faceColour
, 1, wxSOLID
);
708 m_faceBrush
= new wxBrush(faceColour
, wxSOLID
);
710 wxColour
mediumShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DSHADOW
));
711 m_mediumShadowPen
= new wxPen(mediumShadowColour
, 1, wxSOLID
);
713 wxColour
darkShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DDKSHADOW
));
714 m_darkShadowPen
= new wxPen(darkShadowColour
, 1, wxSOLID
);
716 wxColour
lightShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT
));
717 m_lightShadowPen
= new wxPen(lightShadowColour
, 1, wxSOLID
);
719 wxColour
hilightColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DHILIGHT
));
720 m_hilightPen
= new wxPen(hilightColour
, 1, wxSOLID
);
722 m_facePen
= new wxPen("LIGHT GREY", 1, wxSOLID
);
723 m_faceBrush
= new wxBrush("LIGHT GREY", wxSOLID
);
724 m_mediumShadowPen
= new wxPen("GREY", 1, wxSOLID
);
725 m_darkShadowPen
= new wxPen("BLACK", 1, wxSOLID
);
726 m_lightShadowPen
= new wxPen("LIGHT GREY", 1, wxSOLID
);
727 m_hilightPen
= new wxPen("WHITE", 1, wxSOLID
);
728 #endif // Win32/!Win32