]>
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
;
48 m_windowOne
= (wxWindow
*) NULL
;
49 m_windowTwo
= (wxWindow
*) NULL
;
50 m_dragMode
= wxSPLIT_DRAG_NONE
;
58 m_sashCursorWE
= (wxCursor
*) NULL
;
59 m_sashCursorNS
= (wxCursor
*) NULL
;
60 m_sashTrackerPen
= (wxPen
*) NULL
;
61 m_lightShadowPen
= (wxPen
*) NULL
;
62 m_mediumShadowPen
= (wxPen
*) NULL
;
63 m_darkShadowPen
= (wxPen
*) NULL
;
64 m_faceBrush
= (wxBrush
*) NULL
;
65 m_facePen
= (wxPen
*) NULL
;
66 m_hilightPen
= (wxPen
*) 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
;
75 m_windowOne
= (wxWindow
*) NULL
;
76 m_windowTwo
= (wxWindow
*) NULL
;
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
= (wxPen
*) NULL
;
90 m_mediumShadowPen
= (wxPen
*) NULL
;
91 m_darkShadowPen
= (wxPen
*) NULL
;
92 m_faceBrush
= (wxBrush
*) NULL
;
93 m_facePen
= (wxPen
*) NULL
;
94 m_hilightPen
= (wxPen
*) NULL
;
96 if ( style
& wxSP_3D
)
101 else if ( style
& wxSP_BORDER
)
112 // Eventually, we'll respond to colour change messages
115 // For debugging purposes, to see the background.
116 // SetBackground(wxBLUE_BRUSH);
119 wxSplitterWindow::~wxSplitterWindow(void)
121 delete m_sashCursorWE
;
122 delete m_sashCursorNS
;
123 delete m_sashTrackerPen
;
124 delete m_lightShadowPen
;
125 delete m_darkShadowPen
;
126 delete m_mediumShadowPen
;
132 void wxSplitterWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
136 if ( m_borderSize
> 0 )
141 void wxSplitterWindow::OnMouseEvent(wxMouseEvent
& event
)
144 event
.Position(&x
, &y
);
146 if (event
.LeftDown())
148 if ( SashHitTest(x
, y
) )
152 // Required for X to specify that
153 // that we wish to draw on top of all windows
154 // - and we optimise by specifying the area
155 // for creating the overlap window.
156 wxScreenDC::StartDrawingOnTop(this);
158 // We don't say we're dragging yet; we leave that
159 // decision for the Dragging() branch, to ensure
160 // the user has dragged a little bit.
161 m_dragMode
= wxSPLIT_DRAG_LEFT_DOWN
;
166 else if ( event
.LeftUp() && m_dragMode
== wxSPLIT_DRAG_LEFT_DOWN
)
168 // Wasn't a proper drag
170 wxScreenDC::EndDrawingOnTop();
171 m_dragMode
= wxSPLIT_DRAG_NONE
;
173 SetCursor(*wxSTANDARD_CURSOR
);
175 else if (event
.LeftUp() && m_dragMode
== wxSPLIT_DRAG_DRAGGING
)
177 // We can stop dragging now and see what we've got.
178 m_dragMode
= wxSPLIT_DRAG_NONE
;
181 DrawSashTracker(m_oldX
, m_oldY
);
183 // End drawing on top (frees the window used for drawing
185 wxScreenDC::EndDrawingOnTop();
188 GetClientSize(&w
, &h
);
189 if ( m_splitMode
== wxSPLIT_VERTICAL
)
191 // First check if we should veto this resize because
192 // the pane size is too small
193 if ( wxMax(x
, 0) < m_minimumPaneSize
|| wxMax((w
- x
), 0) < m_minimumPaneSize
)
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
);
221 // First check if we should veto this resize because
222 // the pane size is too small
223 if ( wxMax(y
, 0) < m_minimumPaneSize
|| wxMax((h
- y
), 0) < m_minimumPaneSize
)
228 // We remove the first window from the view
229 wxWindow
*removedWindow
= m_windowOne
;
230 m_windowOne
= m_windowTwo
;
231 m_windowTwo
= (wxWindow
*) NULL
;
233 OnUnsplit(removedWindow
);
236 else if ( y
>= (h
- 4) )
238 // We remove the second window from the view
239 wxWindow
*removedWindow
= m_windowTwo
;
240 m_windowTwo
= (wxWindow
*) NULL
;
241 OnUnsplit(removedWindow
);
251 else if (event
.Moving() && !event
.Dragging())
253 // Just change the cursor if required
254 if ( SashHitTest(x
, y
) )
256 if ( m_splitMode
== wxSPLIT_VERTICAL
)
258 SetCursor(*m_sashCursorWE
);
262 SetCursor(*m_sashCursorNS
);
267 SetCursor(*wxSTANDARD_CURSOR
);
270 else if ( (event
.Dragging() && (m_dragMode
== wxSPLIT_DRAG_DRAGGING
)) ||
271 (event
.Dragging() && SashHitTest(x
, y
, 4)) )
273 if ( m_splitMode
== wxSPLIT_VERTICAL
)
275 SetCursor(*m_sashCursorWE
);
279 SetCursor(*m_sashCursorNS
);
282 // Detect that this is really a drag: we've moved more than 1 pixel either way
283 if ((m_dragMode
== wxSPLIT_DRAG_LEFT_DOWN
) &&
284 // (abs((int)x - m_firstX) > 1 || abs((int)y - m_firstY) > 1) )
285 (abs((int)x
- m_firstX
) > 0 || abs((int)y
- m_firstY
) > 1) )
287 m_dragMode
= wxSPLIT_DRAG_DRAGGING
;
288 DrawSashTracker(x
, y
);
292 if ( m_dragMode
== wxSPLIT_DRAG_DRAGGING
)
295 DrawSashTracker(m_oldX
, m_oldY
);
298 DrawSashTracker(x
, y
);
304 else if ( event
.LeftDClick() )
306 OnDoubleClickSash(x
, y
);
313 void wxSplitterWindow::OnSize(wxSizeEvent
& WXUNUSED(event
))
316 GetClientSize( &cw
, &ch
);
319 if ( m_splitMode
== wxSPLIT_VERTICAL
)
321 if ( m_sashPosition
>= (cw
- 5) )
322 m_sashPosition
= wxMax(10, cw
- 40);
324 if ( m_splitMode
== wxSPLIT_HORIZONTAL
)
326 if ( m_sashPosition
>= (ch
- 5) )
327 m_sashPosition
= wxMax(10, ch
- 40);
333 bool wxSplitterWindow::SashHitTest(int x
, int y
, int tolerance
)
335 if ( m_windowTwo
== NULL
|| m_sashPosition
== 0)
336 return FALSE
; // No sash
338 if ( m_splitMode
== wxSPLIT_VERTICAL
)
340 if ( (x
>= m_sashPosition
- tolerance
) && (x
<= m_sashPosition
+ m_sashSize
+ tolerance
) )
347 if ( (y
>= (m_sashPosition
- tolerance
)) && (y
<= (m_sashPosition
+ m_sashSize
+ tolerance
)) )
356 // Draw 3D effect borders
357 void wxSplitterWindow::DrawBorders(wxDC
& dc
)
360 GetClientSize(&w
, &h
);
362 if ( GetWindowStyleFlag() & wxSP_3D
)
364 dc
.SetPen(*m_mediumShadowPen
);
365 dc
.DrawLine(0, 0, w
-1, 0);
366 dc
.DrawLine(0, 0, 0, h
- 1);
368 dc
.SetPen(*m_darkShadowPen
);
369 dc
.DrawLine(1, 1, w
-2, 1);
370 dc
.DrawLine(1, 1, 1, h
-2);
372 dc
.SetPen(*m_hilightPen
);
373 dc
.DrawLine(0, h
-1, w
-1, h
-1);
374 dc
.DrawLine(w
-1, 0, w
-1, h
); // Surely the maximum y pos. should be h - 1.
375 /// Anyway, h is required for MSW.
377 dc
.SetPen(*m_lightShadowPen
);
378 dc
.DrawLine(w
-2, 1, w
-2, h
-2); // Right hand side
379 dc
.DrawLine(1, h
-2, w
-1, h
-2); // Bottom
381 else if ( GetWindowStyleFlag() & wxSP_BORDER
)
383 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
384 dc
.SetPen(*wxBLACK_PEN
);
385 dc
.DrawRectangle(0, 0, w
-1, h
-1);
388 dc
.SetPen(wxNullPen
);
389 dc
.SetBrush(wxNullBrush
);
393 void wxSplitterWindow::DrawSash(wxDC
& dc
)
395 if ( m_sashPosition
== 0 || !m_windowTwo
)
399 GetClientSize(&w
, &h
);
401 if ( GetWindowStyleFlag() & wxSP_3D
)
403 if ( m_splitMode
== wxSPLIT_VERTICAL
)
405 dc
.SetPen(*m_facePen
);
406 dc
.SetBrush(*m_faceBrush
);
407 dc
.DrawRectangle(m_sashPosition
+ 2, 0, m_sashSize
- 4, h
);
409 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
411 dc
.SetPen(*m_lightShadowPen
);
412 dc
.DrawLine(m_sashPosition
, 1, m_sashPosition
, h
-2);
414 dc
.SetPen(*m_hilightPen
);
415 dc
.DrawLine(m_sashPosition
+1, 0, m_sashPosition
+1, h
);
417 dc
.SetPen(*m_mediumShadowPen
);
418 dc
.DrawLine(m_sashPosition
+m_sashSize
-2, 1, m_sashPosition
+m_sashSize
-2, h
-1);
420 dc
.SetPen(*m_darkShadowPen
);
421 dc
.DrawLine(m_sashPosition
+m_sashSize
-1, 2, m_sashPosition
+m_sashSize
-1, h
-2);
425 dc
.SetPen(*m_facePen
);
426 dc
.SetBrush(*m_faceBrush
);
427 dc
.DrawRectangle(0, m_sashPosition
+ 2, w
, m_sashSize
- 4);
429 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
431 dc
.SetPen(*m_lightShadowPen
);
432 dc
.DrawLine(1, m_sashPosition
, w
-2, m_sashPosition
);
434 dc
.SetPen(*m_hilightPen
);
435 dc
.DrawLine(0, m_sashPosition
+1, w
, m_sashPosition
+1);
437 dc
.SetPen(*m_mediumShadowPen
);
438 dc
.DrawLine(1, m_sashPosition
+m_sashSize
-2, w
-1, m_sashPosition
+m_sashSize
-2);
440 dc
.SetPen(*m_darkShadowPen
);
441 dc
.DrawLine(2, m_sashPosition
+m_sashSize
-1, w
-2, m_sashPosition
+m_sashSize
-1);
446 if ( m_splitMode
== wxSPLIT_VERTICAL
)
448 dc
.SetPen(*wxBLACK_PEN
);
449 dc
.SetBrush(*wxBLACK_BRUSH
);
451 if ( (GetWindowStyleFlag() & wxSP_BORDER
) != wxSP_BORDER
)
452 h1
+= 1; // Not sure why this is necessary...
453 dc
.DrawRectangle(m_sashPosition
, 0, m_sashSize
, h1
);
457 dc
.SetPen(*wxBLACK_PEN
);
458 dc
.SetBrush(*wxBLACK_BRUSH
);
460 if ( (GetWindowStyleFlag() & wxSP_BORDER
) != wxSP_BORDER
)
463 dc
.DrawRectangle(0, m_sashPosition
, w1
, m_sashSize
);
468 dc
.SetPen(wxNullPen
);
469 dc
.SetBrush(wxNullBrush
);
472 // Draw the sash tracker (for whilst moving the sash)
473 void wxSplitterWindow::DrawSashTracker(int x
, int y
)
476 GetClientSize(&w
, &h
);
482 if ( m_splitMode
== wxSPLIT_VERTICAL
)
513 ClientToScreen(&x1
, &y1
);
514 ClientToScreen(&x2
, &y2
);
516 screenDC
.SetLogicalFunction(wxXOR
);
517 screenDC
.SetPen(*m_sashTrackerPen
);
518 screenDC
.SetBrush(*wxTRANSPARENT_BRUSH
);
520 screenDC
.DrawLine(x1
, y1
, x2
, y2
);
522 screenDC
.SetLogicalFunction(wxCOPY
);
524 screenDC
.SetPen(wxNullPen
);
525 screenDC
.SetBrush(wxNullBrush
);
528 // Position and size subwindows.
529 // Note that the border size applies to each subwindow, not
530 // including the edges next to the sash.
531 void wxSplitterWindow::SizeWindows(void)
534 GetClientSize(&w
, &h
);
536 if ( m_windowOne
&& !m_windowTwo
)
538 m_windowOne
->SetSize(m_borderSize
, m_borderSize
, w
- 2*m_borderSize
, h
- 2*m_borderSize
);
540 else if ( m_windowOne
&& m_windowTwo
)
542 if (m_splitMode
== wxSPLIT_VERTICAL
)
544 int x1
= m_borderSize
;
545 int y1
= m_borderSize
;
546 int w1
= m_sashPosition
- m_borderSize
;
547 int h1
= h
- 2*m_borderSize
;
549 int x2
= m_sashPosition
+ m_sashSize
;
550 int y2
= m_borderSize
;
551 int w2
= w
- 2*m_borderSize
- m_sashSize
- w1
;
552 int h2
= h
- 2*m_borderSize
;
554 m_windowOne
->SetSize(x1
, y1
,
556 m_windowTwo
->SetSize(x2
, y2
,
561 m_windowOne
->SetSize(m_borderSize
, m_borderSize
,
562 w
- 2*m_borderSize
, m_sashPosition
- m_borderSize
);
563 m_windowTwo
->SetSize(m_borderSize
, m_sashPosition
+ m_sashSize
,
564 w
- 2*m_borderSize
, h
- 2*m_borderSize
- m_sashSize
- (m_sashPosition
- m_borderSize
));
572 // Set pane for unsplit window
573 void wxSplitterWindow::Initialize(wxWindow
*window
)
575 m_windowOne
= window
;
576 m_windowTwo
= (wxWindow
*) NULL
;
580 // Associates the given window with window 2, drawing the appropriate sash
581 // and changing the split mode.
582 // Does nothing and returns FALSE if the window is already split.
583 bool wxSplitterWindow::SplitVertically(wxWindow
*window1
, wxWindow
*window2
, int sashPosition
)
588 m_splitMode
= wxSPLIT_VERTICAL
;
589 m_windowOne
= window1
;
590 m_windowTwo
= window2
;
591 if ( sashPosition
== -1 )
592 m_sashPosition
= 100;
594 m_sashPosition
= sashPosition
;
601 bool wxSplitterWindow::SplitHorizontally(wxWindow
*window1
, wxWindow
*window2
, int sashPosition
)
606 m_splitMode
= wxSPLIT_HORIZONTAL
;
607 m_windowOne
= window1
;
608 m_windowTwo
= window2
;
609 if ( sashPosition
== -1 )
610 m_sashPosition
= 100;
612 m_sashPosition
= sashPosition
;
620 // Remove the specified (or second) window from the view
621 // Doesn't actually delete the window.
622 bool wxSplitterWindow::Unsplit(wxWindow
*toRemove
)
627 if ( toRemove
== NULL
|| toRemove
== m_windowTwo
)
629 wxWindow
*win
= m_windowTwo
;
630 m_windowTwo
= (wxWindow
*) NULL
;
635 else if ( toRemove
== m_windowOne
)
637 wxWindow
*win
= m_windowOne
;
638 m_windowOne
= m_windowTwo
;
639 m_windowTwo
= (wxWindow
*) NULL
;
650 void wxSplitterWindow::SetSashPosition(int position
, bool redraw
)
652 m_sashPosition
= position
;
660 // Called when the sash is double-clicked.
661 // The default behaviour is to remove the sash if the
662 // minimum pane size is zero.
663 void wxSplitterWindow::OnDoubleClickSash(int WXUNUSED(x
), int WXUNUSED(y
) )
665 if ( GetMinimumPaneSize() == 0 )
671 // Initialize colours
672 void wxSplitterWindow::InitColours(void)
678 if ( m_mediumShadowPen
)
679 delete m_mediumShadowPen
;
680 if ( m_darkShadowPen
)
681 delete m_darkShadowPen
;
682 if ( m_lightShadowPen
)
683 delete m_lightShadowPen
;
688 #if defined(__WIN95__)
689 // COLORREF ref = ::GetSysColor(COLOR_3DFACE); // Normally light grey
690 wxColour
faceColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
691 m_facePen
= new wxPen(faceColour
, 1, wxSOLID
);
692 m_faceBrush
= new wxBrush(faceColour
, wxSOLID
);
694 // ref = ::GetSysColor(COLOR_3DSHADOW); // Normally dark grey
695 wxColour
mediumShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DSHADOW
));
696 m_mediumShadowPen
= new wxPen(mediumShadowColour
, 1, wxSOLID
);
698 // ref = ::GetSysColor(COLOR_3DDKSHADOW); // Normally black
699 wxColour
darkShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DDKSHADOW
));
700 m_darkShadowPen
= new wxPen(darkShadowColour
, 1, wxSOLID
);
702 // ref = ::GetSysColor(COLOR_3DLIGHT); // Normally light grey
703 wxColour
lightShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT
));
704 m_lightShadowPen
= new wxPen(lightShadowColour
, 1, wxSOLID
);
706 // ref = ::GetSysColor(COLOR_3DHILIGHT); // Normally white
707 wxColour
hilightColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DHILIGHT
));
708 m_hilightPen
= new wxPen(hilightColour
, 1, wxSOLID
);
710 m_facePen
= new wxPen("LIGHT GREY", 1, wxSOLID
);
711 m_faceBrush
= new wxBrush("LIGHT GREY", wxSOLID
);
712 m_mediumShadowPen
= new wxPen("GREY", 1, wxSOLID
);
713 m_darkShadowPen
= new wxPen("BLACK", 1, wxSOLID
);
714 m_lightShadowPen
= new wxPen("LIGHT GREY", 1, wxSOLID
);
715 m_hilightPen
= new wxPen("WHITE", 1, wxSOLID
);