]>
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
, wxWindowID id
, const wxPoint
& pos
,
71 const wxSize
& size
, 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
);
177 else if (event
.LeftUp() && m_dragMode
== wxSPLIT_DRAG_DRAGGING
)
179 // We can stop dragging now and see what we've got.
180 m_dragMode
= wxSPLIT_DRAG_NONE
;
183 DrawSashTracker(m_oldX
, m_oldY
);
185 // End drawing on top (frees the window used for drawing
187 wxScreenDC::EndDrawingOnTop();
190 GetClientSize(&w
, &h
);
191 if ( m_splitMode
== wxSPLIT_VERTICAL
)
193 // First check if we should veto this resize because
194 // the pane size is too small
195 if ( wxMax(x
, 0) < m_minimumPaneSize
|| wxMax((w
- x
), 0) < m_minimumPaneSize
)
200 // We remove the first window from the view
201 wxWindow
*removedWindow
= m_windowOne
;
202 m_windowOne
= m_windowTwo
;
205 OnUnsplit(removedWindow
);
208 else if ( x
>= (w
- 4) )
210 // We remove the second window from the view
211 wxWindow
*removedWindow
= m_windowTwo
;
213 OnUnsplit(removedWindow
);
223 // First check if we should veto this resize because
224 // the pane size is too small
225 if ( wxMax(y
, 0) < m_minimumPaneSize
|| wxMax((h
- y
), 0) < m_minimumPaneSize
)
230 // We remove the first window from the view
231 wxWindow
*removedWindow
= m_windowOne
;
232 m_windowOne
= m_windowTwo
;
235 OnUnsplit(removedWindow
);
238 else if ( y
>= (h
- 4) )
240 // We remove the second window from the view
241 wxWindow
*removedWindow
= m_windowTwo
;
243 OnUnsplit(removedWindow
);
253 else if (event
.Moving() && !event
.Dragging())
255 // Just change the cursor if required
256 if ( SashHitTest(x
, y
) )
258 if ( m_splitMode
== wxSPLIT_VERTICAL
)
260 SetCursor(*m_sashCursorWE
);
264 SetCursor(*m_sashCursorNS
);
269 SetCursor(*wxSTANDARD_CURSOR
);
272 else if ( (event
.Dragging() && (m_dragMode
== wxSPLIT_DRAG_DRAGGING
)) ||
273 (event
.Dragging() && SashHitTest(x
, y
, 4)) )
275 if ( m_splitMode
== wxSPLIT_VERTICAL
)
277 SetCursor(*m_sashCursorWE
);
281 SetCursor(*m_sashCursorNS
);
284 // Detect that this is really a drag: we've moved more than 1 pixel either way
285 if ((m_dragMode
== wxSPLIT_DRAG_LEFT_DOWN
) &&
286 // (abs((int)x - m_firstX) > 1 || abs((int)y - m_firstY) > 1) )
287 (abs((int)x
- m_firstX
) > 0 || abs((int)y
- m_firstY
) > 1) )
289 m_dragMode
= wxSPLIT_DRAG_DRAGGING
;
290 DrawSashTracker(x
, y
);
294 if ( m_dragMode
== wxSPLIT_DRAG_DRAGGING
)
297 DrawSashTracker(m_oldX
, m_oldY
);
300 DrawSashTracker(x
, y
);
306 else if ( event
.LeftDClick() )
308 OnDoubleClickSash(x
, y
);
315 void wxSplitterWindow::OnSize(wxSizeEvent
& WXUNUSED(event
))
318 GetClientSize( &cw
, &ch
);
321 if ( m_splitMode
== wxSPLIT_VERTICAL
)
323 if ( m_sashPosition
>= (cw
- 5) )
324 m_sashPosition
= wxMax(10, cw
- 40);
326 if ( m_splitMode
== wxSPLIT_HORIZONTAL
)
328 if ( m_sashPosition
>= (ch
- 5) )
329 m_sashPosition
= wxMax(10, ch
- 40);
335 bool wxSplitterWindow::SashHitTest(int x
, int y
, int tolerance
)
337 if ( m_windowTwo
== NULL
|| m_sashPosition
== 0)
338 return FALSE
; // No sash
340 if ( m_splitMode
== wxSPLIT_VERTICAL
)
342 if ( (x
>= m_sashPosition
- tolerance
) && (x
<= m_sashPosition
+ m_sashSize
+ tolerance
) )
349 if ( (y
>= (m_sashPosition
- tolerance
)) && (y
<= (m_sashPosition
+ m_sashSize
+ tolerance
)) )
358 // Draw 3D effect borders
359 void wxSplitterWindow::DrawBorders(wxDC
& dc
)
362 GetClientSize(&w
, &h
);
364 if ( GetWindowStyleFlag() & wxSP_3D
)
366 dc
.SetPen(*m_mediumShadowPen
);
367 dc
.DrawLine(0, 0, w
-1, 0);
368 dc
.DrawLine(0, 0, 0, h
- 1);
370 dc
.SetPen(*m_darkShadowPen
);
371 dc
.DrawLine(1, 1, w
-2, 1);
372 dc
.DrawLine(1, 1, 1, h
-2);
374 dc
.SetPen(*m_hilightPen
);
375 dc
.DrawLine(0, h
-1, w
-1, h
-1);
376 dc
.DrawLine(w
-1, 0, w
-1, h
); // Surely the maximum y pos. should be h - 1.
377 /// Anyway, h is required for MSW.
379 dc
.SetPen(*m_lightShadowPen
);
380 dc
.DrawLine(w
-2, 1, w
-2, h
-2); // Right hand side
381 dc
.DrawLine(1, h
-2, w
-1, h
-2); // Bottom
383 else if ( GetWindowStyleFlag() & wxSP_BORDER
)
385 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
386 dc
.SetPen(*wxBLACK_PEN
);
387 dc
.DrawRectangle(0, 0, w
-1, h
-1);
390 dc
.SetPen(wxNullPen
);
391 dc
.SetBrush(wxNullBrush
);
395 void wxSplitterWindow::DrawSash(wxDC
& dc
)
397 if ( m_sashPosition
== 0 || !m_windowTwo
)
401 GetClientSize(&w
, &h
);
403 if ( GetWindowStyleFlag() & wxSP_3D
)
405 if ( m_splitMode
== wxSPLIT_VERTICAL
)
407 dc
.SetPen(*m_facePen
);
408 dc
.SetBrush(*m_faceBrush
);
409 dc
.DrawRectangle(m_sashPosition
+ 2, 0, m_sashSize
- 4, h
);
411 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
413 dc
.SetPen(*m_lightShadowPen
);
414 dc
.DrawLine(m_sashPosition
, 1, m_sashPosition
, h
-2);
416 dc
.SetPen(*m_hilightPen
);
417 dc
.DrawLine(m_sashPosition
+1, 0, m_sashPosition
+1, h
);
419 dc
.SetPen(*m_mediumShadowPen
);
420 dc
.DrawLine(m_sashPosition
+m_sashSize
-2, 1, m_sashPosition
+m_sashSize
-2, h
-1);
422 dc
.SetPen(*m_darkShadowPen
);
423 dc
.DrawLine(m_sashPosition
+m_sashSize
-1, 2, m_sashPosition
+m_sashSize
-1, h
-2);
427 dc
.SetPen(*m_facePen
);
428 dc
.SetBrush(*m_faceBrush
);
429 dc
.DrawRectangle(0, m_sashPosition
+ 2, w
, m_sashSize
- 4);
431 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
433 dc
.SetPen(*m_lightShadowPen
);
434 dc
.DrawLine(1, m_sashPosition
, w
-2, m_sashPosition
);
436 dc
.SetPen(*m_hilightPen
);
437 dc
.DrawLine(0, m_sashPosition
+1, w
, m_sashPosition
+1);
439 dc
.SetPen(*m_mediumShadowPen
);
440 dc
.DrawLine(1, m_sashPosition
+m_sashSize
-2, w
-1, m_sashPosition
+m_sashSize
-2);
442 dc
.SetPen(*m_darkShadowPen
);
443 dc
.DrawLine(2, m_sashPosition
+m_sashSize
-1, w
-2, m_sashPosition
+m_sashSize
-1);
448 if ( m_splitMode
== wxSPLIT_VERTICAL
)
450 dc
.SetPen(*wxBLACK_PEN
);
451 dc
.SetBrush(*wxBLACK_BRUSH
);
453 if ( (GetWindowStyleFlag() & wxSP_BORDER
) != wxSP_BORDER
)
454 h1
+= 1; // Not sure why this is necessary...
455 dc
.DrawRectangle(m_sashPosition
, 0, m_sashSize
, h1
);
459 dc
.SetPen(*wxBLACK_PEN
);
460 dc
.SetBrush(*wxBLACK_BRUSH
);
462 if ( (GetWindowStyleFlag() & wxSP_BORDER
) != wxSP_BORDER
)
465 dc
.DrawRectangle(0, m_sashPosition
, w1
, m_sashSize
);
470 dc
.SetPen(wxNullPen
);
471 dc
.SetBrush(wxNullBrush
);
474 // Draw the sash tracker (for whilst moving the sash)
475 void wxSplitterWindow::DrawSashTracker(int x
, int y
)
478 GetClientSize(&w
, &h
);
484 if ( m_splitMode
== wxSPLIT_VERTICAL
)
515 ClientToScreen(&x1
, &y1
);
516 ClientToScreen(&x2
, &y2
);
518 screenDC
.SetLogicalFunction(wxXOR
);
519 screenDC
.SetPen(*m_sashTrackerPen
);
520 screenDC
.SetBrush(*wxTRANSPARENT_BRUSH
);
522 screenDC
.DrawLine(x1
, y1
, x2
, y2
);
524 screenDC
.SetLogicalFunction(wxCOPY
);
526 screenDC
.SetPen(wxNullPen
);
527 screenDC
.SetBrush(wxNullBrush
);
530 // Position and size subwindows.
531 // Note that the border size applies to each subwindow, not
532 // including the edges next to the sash.
533 void wxSplitterWindow::SizeWindows(void)
536 GetClientSize(&w
, &h
);
538 if ( m_windowOne
&& !m_windowTwo
)
540 m_windowOne
->SetSize(m_borderSize
, m_borderSize
, w
- 2*m_borderSize
, h
- 2*m_borderSize
);
542 else if ( m_windowOne
&& m_windowTwo
)
544 if (m_splitMode
== wxSPLIT_VERTICAL
)
546 int x1
= m_borderSize
;
547 int y1
= m_borderSize
;
548 int w1
= m_sashPosition
- m_borderSize
;
549 int h1
= h
- 2*m_borderSize
;
551 int x2
= m_sashPosition
+ m_sashSize
;
552 int y2
= m_borderSize
;
553 int w2
= w
- 2*m_borderSize
- m_sashSize
- w1
;
554 int h2
= h
- 2*m_borderSize
;
556 m_windowOne
->SetSize(x1
, y1
,
558 m_windowTwo
->SetSize(x2
, y2
,
563 m_windowOne
->SetSize(m_borderSize
, m_borderSize
,
564 w
- 2*m_borderSize
, m_sashPosition
- m_borderSize
);
565 m_windowTwo
->SetSize(m_borderSize
, m_sashPosition
+ m_sashSize
,
566 w
- 2*m_borderSize
, h
- 2*m_borderSize
- m_sashSize
- (m_sashPosition
- m_borderSize
));
574 // Set pane for unsplit window
575 void wxSplitterWindow::Initialize(wxWindow
*window
)
577 m_windowOne
= window
;
582 // Associates the given window with window 2, drawing the appropriate sash
583 // and changing the split mode.
584 // Does nothing and returns FALSE if the window is already split.
585 bool wxSplitterWindow::SplitVertically(wxWindow
*window1
, wxWindow
*window2
, int sashPosition
)
590 m_splitMode
= wxSPLIT_VERTICAL
;
591 m_windowOne
= window1
;
592 m_windowTwo
= window2
;
593 if ( sashPosition
== -1 )
594 m_sashPosition
= 100;
596 m_sashPosition
= sashPosition
;
603 bool wxSplitterWindow::SplitHorizontally(wxWindow
*window1
, wxWindow
*window2
, int sashPosition
)
608 m_splitMode
= wxSPLIT_HORIZONTAL
;
609 m_windowOne
= window1
;
610 m_windowTwo
= window2
;
611 if ( sashPosition
== -1 )
612 m_sashPosition
= 100;
614 m_sashPosition
= sashPosition
;
622 // Remove the specified (or second) window from the view
623 // Doesn't actually delete the window.
624 bool wxSplitterWindow::Unsplit(wxWindow
*toRemove
)
629 if ( toRemove
== NULL
|| toRemove
== m_windowTwo
)
631 wxWindow
*win
= m_windowTwo
;
637 else if ( toRemove
== m_windowOne
)
639 wxWindow
*win
= m_windowOne
;
640 m_windowOne
= m_windowTwo
;
652 void wxSplitterWindow::SetSashPosition(int position
, bool redraw
)
654 m_sashPosition
= position
;
662 // Called when the sash is double-clicked.
663 // The default behaviour is to remove the sash if the
664 // minimum pane size is zero.
665 void wxSplitterWindow::OnDoubleClickSash(int WXUNUSED(x
), int WXUNUSED(y
) )
667 if ( GetMinimumPaneSize() == 0 )
673 // Initialize colours
674 void wxSplitterWindow::InitColours(void)
680 if ( m_mediumShadowPen
)
681 delete m_mediumShadowPen
;
682 if ( m_darkShadowPen
)
683 delete m_darkShadowPen
;
684 if ( m_lightShadowPen
)
685 delete m_lightShadowPen
;
690 #if defined(__WIN95__)
691 // COLORREF ref = ::GetSysColor(COLOR_3DFACE); // Normally light grey
692 wxColour
faceColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
693 m_facePen
= new wxPen(faceColour
, 1, wxSOLID
);
694 m_faceBrush
= new wxBrush(faceColour
, wxSOLID
);
696 // ref = ::GetSysColor(COLOR_3DSHADOW); // Normally dark grey
697 wxColour
mediumShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DSHADOW
));
698 m_mediumShadowPen
= new wxPen(mediumShadowColour
, 1, wxSOLID
);
700 // ref = ::GetSysColor(COLOR_3DDKSHADOW); // Normally black
701 wxColour
darkShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DDKSHADOW
));
702 m_darkShadowPen
= new wxPen(darkShadowColour
, 1, wxSOLID
);
704 // ref = ::GetSysColor(COLOR_3DLIGHT); // Normally light grey
705 wxColour
lightShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT
));
706 m_lightShadowPen
= new wxPen(lightShadowColour
, 1, wxSOLID
);
708 // ref = ::GetSysColor(COLOR_3DHILIGHT); // Normally white
709 wxColour
hilightColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DHILIGHT
));
710 m_hilightPen
= new wxPen(hilightColour
, 1, wxSOLID
);
712 m_facePen
= new wxPen("LIGHT GREY", 1, wxSOLID
);
713 m_faceBrush
= new wxBrush("LIGHT GREY", wxSOLID
);
714 m_mediumShadowPen
= new wxPen("GREY", 1, wxSOLID
);
715 m_darkShadowPen
= new wxPen("BLACK", 1, wxSOLID
);
716 m_lightShadowPen
= new wxPen("LIGHT GREY", 1, wxSOLID
);
717 m_hilightPen
= new wxPen("WHITE", 1, wxSOLID
);