]>
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"
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
31 #include "wx/string.h"
32 #include "wx/splitter.h"
33 #include "wx/dcscreen.h"
35 #if !USE_SHARED_LIBRARY
36 IMPLEMENT_DYNAMIC_CLASS(wxSplitterWindow
, wxWindow
)
38 BEGIN_EVENT_TABLE(wxSplitterWindow
, wxWindow
)
39 EVT_PAINT(wxSplitterWindow::OnPaint
)
40 EVT_SIZE(wxSplitterWindow::OnSize
)
41 EVT_MOUSE_EVENTS(wxSplitterWindow::OnMouseEvent
)
45 wxSplitterWindow::wxSplitterWindow(void)
47 m_splitMode
= wxSPLIT_VERTICAL
;
50 m_dragMode
= wxSPLIT_DRAG_NONE
;
58 m_sashCursorWE
= NULL
;
59 m_sashCursorNS
= NULL
;
60 m_sashTrackerPen
= NULL
;
61 m_lightShadowPen
= NULL
;
62 m_mediumShadowPen
= NULL
;
63 m_darkShadowPen
= NULL
;
67 m_minimumPaneSize
= 0;
70 wxSplitterWindow::wxSplitterWindow(wxWindow
*parent
, const wxWindowID id
, const wxPoint
& pos
,
71 const wxSize
& size
, const long style
, const wxString
& name
)
72 :wxWindow(parent
, id
, pos
, size
, style
, name
)
74 m_splitMode
= wxSPLIT_VERTICAL
;
77 m_dragMode
= wxSPLIT_DRAG_NONE
;
85 m_minimumPaneSize
= 0;
86 m_sashCursorWE
= new wxCursor(wxCURSOR_SIZEWE
);
87 m_sashCursorNS
= new wxCursor(wxCURSOR_SIZENS
);
88 m_sashTrackerPen
= new wxPen(*wxBLACK
, 2, wxSOLID
);
89 m_lightShadowPen
= NULL
;
90 m_mediumShadowPen
= NULL
;
91 m_darkShadowPen
= NULL
;
96 if ( style
& wxSP_3D
)
101 else if ( style
& wxSP_BORDER
)
112 // Eventually, we'll respond to colour change messages
115 SetDoubleClick(TRUE
);
117 // For debugging purposes, to see the background.
118 // SetBackground(wxBLUE_BRUSH);
121 wxSplitterWindow::~wxSplitterWindow(void)
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
);
148 if (event
.LeftDown())
150 if ( SashHitTest(x
, y
) )
154 // Required for X to specify that
155 // that we wish to draw on top of all windows
156 // - and we optimise by specifying the area
157 // for creating the overlap window.
158 wxScreenDC::StartDrawingOnTop(this);
160 // We don't say we're dragging yet; we leave that
161 // decision for the Dragging() branch, to ensure
162 // the user has dragged a little bit.
163 m_dragMode
= wxSPLIT_DRAG_LEFT_DOWN
;
168 else if ( event
.LeftUp() && m_dragMode
== wxSPLIT_DRAG_LEFT_DOWN
)
170 // Wasn't a proper drag
172 wxScreenDC::EndDrawingOnTop();
173 m_dragMode
= wxSPLIT_DRAG_NONE
;
175 SetCursor(*wxSTANDARD_CURSOR
);
176 wxSetCursor(*wxSTANDARD_CURSOR
);
178 else if (event
.LeftUp() && m_dragMode
== wxSPLIT_DRAG_DRAGGING
)
180 // We can stop dragging now and see what we've got.
181 m_dragMode
= wxSPLIT_DRAG_NONE
;
184 DrawSashTracker(m_oldX
, m_oldY
);
186 // End drawing on top (frees the window used for drawing
188 wxScreenDC::EndDrawingOnTop();
191 GetClientSize(&w
, &h
);
192 if ( m_splitMode
== wxSPLIT_VERTICAL
)
194 // First check if we should veto this resize because
195 // the pane size is too small
196 if ( wxMax(x
, 0) < m_minimumPaneSize
|| wxMax((w
- x
), 0) < m_minimumPaneSize
)
201 // We remove the first window from the view
202 wxWindow
*removedWindow
= m_windowOne
;
203 m_windowOne
= m_windowTwo
;
206 OnUnsplit(removedWindow
);
209 else if ( x
>= (w
- 4) )
211 // We remove the second window from the view
212 wxWindow
*removedWindow
= m_windowTwo
;
214 OnUnsplit(removedWindow
);
224 // First check if we should veto this resize because
225 // the pane size is too small
226 if ( wxMax(y
, 0) < m_minimumPaneSize
|| wxMax((h
- y
), 0) < m_minimumPaneSize
)
231 // We remove the first window from the view
232 wxWindow
*removedWindow
= m_windowOne
;
233 m_windowOne
= m_windowTwo
;
236 OnUnsplit(removedWindow
);
239 else if ( y
>= (h
- 4) )
241 // We remove the second window from the view
242 wxWindow
*removedWindow
= m_windowTwo
;
244 OnUnsplit(removedWindow
);
254 else if (event
.Moving() && !event
.Dragging())
256 // Just change the cursor if required
257 if ( SashHitTest(x
, y
) )
259 if ( m_splitMode
== wxSPLIT_VERTICAL
)
261 SetCursor(*m_sashCursorWE
);
262 // Windows needs the following
263 wxSetCursor(*m_sashCursorWE
);
267 SetCursor(*m_sashCursorNS
);
268 wxSetCursor(*m_sashCursorNS
);
273 SetCursor(*wxSTANDARD_CURSOR
);
274 wxSetCursor(*wxSTANDARD_CURSOR
);
277 else if ( (event
.Dragging() && (m_dragMode
== wxSPLIT_DRAG_DRAGGING
)) ||
278 (event
.Dragging() && SashHitTest(x
, y
, 4)) )
280 if ( m_splitMode
== wxSPLIT_VERTICAL
)
282 SetCursor(*m_sashCursorWE
);
283 wxSetCursor(*m_sashCursorWE
);
287 SetCursor(*m_sashCursorNS
);
288 wxSetCursor(*m_sashCursorNS
);
291 // Detect that this is really a drag: we've moved more than 1 pixel either way
292 if ((m_dragMode
== wxSPLIT_DRAG_LEFT_DOWN
) &&
293 // (abs((int)x - m_firstX) > 1 || abs((int)y - m_firstY) > 1) )
294 (abs((int)x
- m_firstX
) > 0 || abs((int)y
- m_firstY
) > 1) )
296 m_dragMode
= wxSPLIT_DRAG_DRAGGING
;
297 DrawSashTracker(x
, y
);
301 if ( m_dragMode
== wxSPLIT_DRAG_DRAGGING
)
304 DrawSashTracker(m_oldX
, m_oldY
);
307 DrawSashTracker(x
, y
);
313 else if ( event
.LeftDClick() )
315 OnDoubleClickSash(x
, y
);
319 SetCursor(*wxSTANDARD_CURSOR
);
320 wxSetCursor(*wxSTANDARD_CURSOR
);
324 void wxSplitterWindow::OnSize(wxSizeEvent
& WXUNUSED(event
))
327 GetClientSize( &cw
, &ch
);
330 if ( m_splitMode
== wxSPLIT_VERTICAL
)
332 if ( m_sashPosition
>= (cw
- 5) )
333 m_sashPosition
= wxMax(10, cw
- 40);
335 if ( m_splitMode
== wxSPLIT_HORIZONTAL
)
337 if ( m_sashPosition
>= (ch
- 5) )
338 m_sashPosition
= wxMax(10, ch
- 40);
344 bool wxSplitterWindow::SashHitTest(const int x
, const int y
, const int tolerance
)
346 if ( m_windowTwo
== NULL
|| m_sashPosition
== 0)
347 return FALSE
; // No sash
349 if ( m_splitMode
== wxSPLIT_VERTICAL
)
351 if ( (x
>= m_sashPosition
- tolerance
) && (x
<= m_sashPosition
+ m_sashSize
+ tolerance
) )
358 if ( (y
>= (m_sashPosition
- tolerance
)) && (y
<= (m_sashPosition
+ m_sashSize
+ tolerance
)) )
367 // Draw 3D effect borders
368 void wxSplitterWindow::DrawBorders(wxDC
& dc
)
371 GetClientSize(&w
, &h
);
373 if ( GetWindowStyleFlag() & wxSP_3D
)
375 dc
.SetPen(*m_mediumShadowPen
);
376 dc
.DrawLine(0, 0, w
-1, 0);
377 dc
.DrawLine(0, 0, 0, h
- 1);
379 dc
.SetPen(*m_darkShadowPen
);
380 dc
.DrawLine(1, 1, w
-2, 1);
381 dc
.DrawLine(1, 1, 1, h
-2);
383 dc
.SetPen(*m_hilightPen
);
384 dc
.DrawLine(0, h
-1, w
-1, h
-1);
385 dc
.DrawLine(w
-1, 0, w
-1, h
); // Surely the maximum y pos. should be h - 1.
386 /// Anyway, h is required for MSW.
388 dc
.SetPen(*m_lightShadowPen
);
389 dc
.DrawLine(w
-2, 1, w
-2, h
-2); // Right hand side
390 dc
.DrawLine(1, h
-2, w
-1, h
-2); // Bottom
392 else if ( GetWindowStyleFlag() & wxSP_BORDER
)
394 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
395 dc
.SetPen(*wxBLACK_PEN
);
396 dc
.DrawRectangle(0, 0, w
-1, h
-1);
399 dc
.SetPen(wxNullPen
);
400 dc
.SetBrush(wxNullBrush
);
404 void wxSplitterWindow::DrawSash(wxDC
& dc
)
406 if ( m_sashPosition
== 0 || !m_windowTwo
)
410 GetClientSize(&w
, &h
);
412 if ( GetWindowStyleFlag() & wxSP_3D
)
414 if ( m_splitMode
== wxSPLIT_VERTICAL
)
416 dc
.SetPen(*m_facePen
);
417 dc
.SetBrush(*m_faceBrush
);
418 dc
.DrawRectangle(m_sashPosition
+ 2, 0, m_sashSize
- 4, h
);
420 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
422 dc
.SetPen(*m_lightShadowPen
);
423 dc
.DrawLine(m_sashPosition
, 1, m_sashPosition
, h
-2);
425 dc
.SetPen(*m_hilightPen
);
426 dc
.DrawLine(m_sashPosition
+1, 0, m_sashPosition
+1, h
);
428 dc
.SetPen(*m_mediumShadowPen
);
429 dc
.DrawLine(m_sashPosition
+m_sashSize
-2, 1, m_sashPosition
+m_sashSize
-2, h
-1);
431 dc
.SetPen(*m_darkShadowPen
);
432 dc
.DrawLine(m_sashPosition
+m_sashSize
-1, 2, m_sashPosition
+m_sashSize
-1, h
-2);
436 dc
.SetPen(*m_facePen
);
437 dc
.SetBrush(*m_faceBrush
);
438 dc
.DrawRectangle(0, m_sashPosition
+ 2, w
, m_sashSize
- 4);
440 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
442 dc
.SetPen(*m_lightShadowPen
);
443 dc
.DrawLine(1, m_sashPosition
, w
-2, m_sashPosition
);
445 dc
.SetPen(*m_hilightPen
);
446 dc
.DrawLine(0, m_sashPosition
+1, w
, m_sashPosition
+1);
448 dc
.SetPen(*m_mediumShadowPen
);
449 dc
.DrawLine(1, m_sashPosition
+m_sashSize
-2, w
-1, m_sashPosition
+m_sashSize
-2);
451 dc
.SetPen(*m_darkShadowPen
);
452 dc
.DrawLine(2, m_sashPosition
+m_sashSize
-1, w
-2, m_sashPosition
+m_sashSize
-1);
457 if ( m_splitMode
== wxSPLIT_VERTICAL
)
459 dc
.SetPen(*wxBLACK_PEN
);
460 dc
.SetBrush(*wxBLACK_BRUSH
);
462 if ( (GetWindowStyleFlag() & wxSP_BORDER
) != wxSP_BORDER
)
463 h1
+= 1; // Not sure why this is necessary...
464 dc
.DrawRectangle(m_sashPosition
, 0, m_sashSize
, h1
);
468 dc
.SetPen(*wxBLACK_PEN
);
469 dc
.SetBrush(*wxBLACK_BRUSH
);
471 if ( (GetWindowStyleFlag() & wxSP_BORDER
) != wxSP_BORDER
)
474 dc
.DrawRectangle(0, m_sashPosition
, w1
, m_sashSize
);
479 dc
.SetPen(wxNullPen
);
480 dc
.SetBrush(wxNullBrush
);
483 // Draw the sash tracker (for whilst moving the sash)
484 void wxSplitterWindow::DrawSashTracker(const int x
, const int y
)
487 GetClientSize(&w
, &h
);
493 if ( m_splitMode
== wxSPLIT_VERTICAL
)
524 ClientToScreen(&x1
, &y1
);
525 ClientToScreen(&x2
, &y2
);
527 screenDC
.SetLogicalFunction(wxXOR
);
528 screenDC
.SetPen(*m_sashTrackerPen
);
529 screenDC
.SetBrush(*wxTRANSPARENT_BRUSH
);
531 screenDC
.DrawLine(x1
, y1
, x2
, y2
);
533 screenDC
.SetLogicalFunction(wxCOPY
);
535 screenDC
.SetPen(wxNullPen
);
536 screenDC
.SetBrush(wxNullBrush
);
539 // Position and size subwindows.
540 // Note that the border size applies to each subwindow, not
541 // including the edges next to the sash.
542 void wxSplitterWindow::SizeWindows(void)
545 GetClientSize(&w
, &h
);
547 if ( m_windowOne
&& !m_windowTwo
)
549 m_windowOne
->SetSize(m_borderSize
, m_borderSize
, w
- 2*m_borderSize
, h
- 2*m_borderSize
);
551 else if ( m_windowOne
&& m_windowTwo
)
553 if (m_splitMode
== wxSPLIT_VERTICAL
)
555 int x1
= m_borderSize
;
556 int y1
= m_borderSize
;
557 int w1
= m_sashPosition
- m_borderSize
;
558 int h1
= h
- 2*m_borderSize
;
560 int x2
= m_sashPosition
+ m_sashSize
;
561 int y2
= m_borderSize
;
562 int w2
= w
- 2*m_borderSize
- m_sashSize
- w1
;
563 int h2
= h
- 2*m_borderSize
;
565 m_windowOne
->SetSize(x1
, y1
,
567 m_windowTwo
->SetSize(x2
, y2
,
572 m_windowOne
->SetSize(m_borderSize
, m_borderSize
,
573 w
- 2*m_borderSize
, m_sashPosition
- m_borderSize
);
574 m_windowTwo
->SetSize(m_borderSize
, m_sashPosition
+ m_sashSize
,
575 w
- 2*m_borderSize
, h
- 2*m_borderSize
- m_sashSize
- (m_sashPosition
- m_borderSize
));
583 // Set pane for unsplit window
584 void wxSplitterWindow::Initialize(wxWindow
*window
)
586 m_windowOne
= window
;
591 // Associates the given window with window 2, drawing the appropriate sash
592 // and changing the split mode.
593 // Does nothing and returns FALSE if the window is already split.
594 bool wxSplitterWindow::SplitVertically(wxWindow
*window1
, wxWindow
*window2
, const int sashPosition
)
599 m_splitMode
= wxSPLIT_VERTICAL
;
600 m_windowOne
= window1
;
601 m_windowTwo
= window2
;
602 if ( sashPosition
== -1 )
603 m_sashPosition
= 100;
605 m_sashPosition
= sashPosition
;
612 bool wxSplitterWindow::SplitHorizontally(wxWindow
*window1
, wxWindow
*window2
, const int sashPosition
)
617 m_splitMode
= wxSPLIT_HORIZONTAL
;
618 m_windowOne
= window1
;
619 m_windowTwo
= window2
;
620 if ( sashPosition
== -1 )
621 m_sashPosition
= 100;
623 m_sashPosition
= sashPosition
;
631 // Remove the specified (or second) window from the view
632 // Doesn't actually delete the window.
633 bool wxSplitterWindow::Unsplit(wxWindow
*toRemove
)
638 if ( toRemove
== NULL
|| toRemove
== m_windowTwo
)
640 wxWindow
*win
= m_windowTwo
;
646 else if ( toRemove
== m_windowOne
)
648 wxWindow
*win
= m_windowOne
;
649 m_windowOne
= m_windowTwo
;
661 void wxSplitterWindow::SetSashPosition(const int position
, const bool redraw
)
663 m_sashPosition
= position
;
671 // Called when the sash is double-clicked.
672 // The default behaviour is to remove the sash if the
673 // minimum pane size is zero.
674 void wxSplitterWindow::OnDoubleClickSash(int WXUNUSED(x
), int WXUNUSED(y
) )
676 if ( GetMinimumPaneSize() == 0 )
682 // Initialize colours
683 void wxSplitterWindow::InitColours(void)
689 if ( m_mediumShadowPen
)
690 delete m_mediumShadowPen
;
691 if ( m_darkShadowPen
)
692 delete m_darkShadowPen
;
693 if ( m_lightShadowPen
)
694 delete m_lightShadowPen
;
699 #if defined(__WIN95__)
700 // COLORREF ref = ::GetSysColor(COLOR_3DFACE); // Normally light grey
701 wxColour
faceColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
702 m_facePen
= new wxPen(faceColour
, 1, wxSOLID
);
703 m_faceBrush
= new wxBrush(faceColour
, wxSOLID
);
705 // ref = ::GetSysColor(COLOR_3DSHADOW); // Normally dark grey
706 wxColour
mediumShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DSHADOW
));
707 m_mediumShadowPen
= new wxPen(mediumShadowColour
, 1, wxSOLID
);
709 // ref = ::GetSysColor(COLOR_3DDKSHADOW); // Normally black
710 wxColour
darkShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DDKSHADOW
));
711 m_darkShadowPen
= new wxPen(darkShadowColour
, 1, wxSOLID
);
713 // ref = ::GetSysColor(COLOR_3DLIGHT); // Normally light grey
714 wxColour
lightShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT
));
715 m_lightShadowPen
= new wxPen(lightShadowColour
, 1, wxSOLID
);
717 // ref = ::GetSysColor(COLOR_3DHILIGHT); // Normally white
718 wxColour
hilightColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DHILIGHT
));
719 m_hilightPen
= new wxPen(hilightColour
, 1, wxSOLID
);
721 m_facePen
= new wxPen("LIGHT GREY", 1, wxSOLID
);
722 m_faceBrush
= new wxBrush("LIGHT GREY", wxSOLID
);
723 m_mediumShadowPen
= new wxPen("GREY", 1, wxSOLID
);
724 m_darkShadowPen
= new wxPen("BLACK", 1, wxSOLID
);
725 m_lightShadowPen
= new wxPen("LIGHT GREY", 1, wxSOLID
);
726 m_hilightPen
= new wxPen("WHITE", 1, wxSOLID
);