]>
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()
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
,
75 : wxWindow(parent
, id
, pos
, size
, style
, name
)
77 m_splitMode
= wxSPLIT_VERTICAL
;
78 m_windowOne
= (wxWindow
*) NULL
;
79 m_windowTwo
= (wxWindow
*) NULL
;
80 m_dragMode
= wxSPLIT_DRAG_NONE
;
88 m_minimumPaneSize
= 0;
89 m_sashCursorWE
= new wxCursor(wxCURSOR_SIZEWE
);
90 m_sashCursorNS
= new wxCursor(wxCURSOR_SIZENS
);
91 m_sashTrackerPen
= new wxPen(*wxBLACK
, 2, wxSOLID
);
92 m_lightShadowPen
= (wxPen
*) NULL
;
93 m_mediumShadowPen
= (wxPen
*) NULL
;
94 m_darkShadowPen
= (wxPen
*) NULL
;
95 m_faceBrush
= (wxBrush
*) NULL
;
96 m_facePen
= (wxPen
*) NULL
;
97 m_hilightPen
= (wxPen
*) NULL
;
99 if ( style
& wxSP_3D
)
104 else if ( style
& wxSP_BORDER
)
115 // Eventually, we'll respond to colour change messages
118 // For debugging purposes, to see the background.
119 // SetBackground(wxBLUE_BRUSH);
122 wxSplitterWindow::~wxSplitterWindow()
124 delete m_sashCursorWE
;
125 delete m_sashCursorNS
;
126 delete m_sashTrackerPen
;
127 delete m_lightShadowPen
;
128 delete m_darkShadowPen
;
129 delete m_mediumShadowPen
;
135 void wxSplitterWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
139 if ( m_borderSize
> 0 )
144 void wxSplitterWindow::OnMouseEvent(wxMouseEvent
& event
)
147 event
.Position(&x
, &y
);
149 if (event
.LeftDown())
151 if ( SashHitTest(x
, y
) )
155 // Required for X to specify that
156 // that we wish to draw on top of all windows
157 // - and we optimise by specifying the area
158 // for creating the overlap window.
159 wxScreenDC::StartDrawingOnTop(this);
161 // We don't say we're dragging yet; we leave that
162 // decision for the Dragging() branch, to ensure
163 // the user has dragged a little bit.
164 m_dragMode
= wxSPLIT_DRAG_LEFT_DOWN
;
169 else if ( event
.LeftUp() && m_dragMode
== wxSPLIT_DRAG_LEFT_DOWN
)
171 // Wasn't a proper drag
173 wxScreenDC::EndDrawingOnTop();
174 m_dragMode
= wxSPLIT_DRAG_NONE
;
176 SetCursor(*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 if ( !OnSashPositionChange(x
) )
199 // We remove the first window from the view
200 wxWindow
*removedWindow
= m_windowOne
;
201 m_windowOne
= m_windowTwo
;
202 m_windowTwo
= (wxWindow
*) NULL
;
204 OnUnsplit(removedWindow
);
207 else if ( x
>= (w
- 4) )
209 // We remove the second window from the view
210 wxWindow
*removedWindow
= m_windowTwo
;
211 m_windowTwo
= (wxWindow
*) NULL
;
212 OnUnsplit(removedWindow
);
222 if ( !OnSashPositionChange(y
) )
227 // We remove the first window from the view
228 wxWindow
*removedWindow
= m_windowOne
;
229 m_windowOne
= m_windowTwo
;
230 m_windowTwo
= (wxWindow
*) NULL
;
232 OnUnsplit(removedWindow
);
235 else if ( y
>= (h
- 4) )
237 // We remove the second window from the view
238 wxWindow
*removedWindow
= m_windowTwo
;
239 m_windowTwo
= (wxWindow
*) NULL
;
240 OnUnsplit(removedWindow
);
250 else if (event
.Moving() && !event
.Dragging())
252 // Just change the cursor if required
253 if ( SashHitTest(x
, y
) )
255 if ( m_splitMode
== wxSPLIT_VERTICAL
)
257 SetCursor(*m_sashCursorWE
);
261 SetCursor(*m_sashCursorNS
);
266 SetCursor(*wxSTANDARD_CURSOR
);
269 else if ( (event
.Dragging() && (m_dragMode
== wxSPLIT_DRAG_DRAGGING
)) ||
270 (event
.Dragging() && SashHitTest(x
, y
, 4)) )
272 if ( m_splitMode
== wxSPLIT_VERTICAL
)
274 SetCursor(*m_sashCursorWE
);
278 SetCursor(*m_sashCursorNS
);
281 // Detect that this is really a drag: we've moved more than 1 pixel either way
282 if ((m_dragMode
== wxSPLIT_DRAG_LEFT_DOWN
) &&
283 // (abs((int)x - m_firstX) > 1 || abs((int)y - m_firstY) > 1) )
284 (abs((int)x
- m_firstX
) > 0 || abs((int)y
- m_firstY
) > 1) )
286 m_dragMode
= wxSPLIT_DRAG_DRAGGING
;
287 DrawSashTracker(x
, y
);
291 if ( m_dragMode
== wxSPLIT_DRAG_DRAGGING
)
294 DrawSashTracker(m_oldX
, m_oldY
);
297 DrawSashTracker(x
, y
);
303 else if ( event
.LeftDClick() )
305 OnDoubleClickSash(x
, y
);
312 void wxSplitterWindow::OnSize(wxSizeEvent
& WXUNUSED(event
))
315 GetClientSize( &cw
, &ch
);
318 if ( m_splitMode
== wxSPLIT_VERTICAL
)
320 if ( m_sashPosition
>= (cw
- 5) )
321 m_sashPosition
= wxMax(10, cw
- 40);
323 if ( m_splitMode
== wxSPLIT_HORIZONTAL
)
325 if ( m_sashPosition
>= (ch
- 5) )
326 m_sashPosition
= wxMax(10, ch
- 40);
332 bool wxSplitterWindow::SashHitTest(int x
, int y
, int tolerance
)
334 if ( m_windowTwo
== NULL
|| m_sashPosition
== 0)
335 return FALSE
; // No sash
337 if ( m_splitMode
== wxSPLIT_VERTICAL
)
339 if ( (x
>= m_sashPosition
- tolerance
) && (x
<= m_sashPosition
+ m_sashSize
+ tolerance
) )
346 if ( (y
>= (m_sashPosition
- tolerance
)) && (y
<= (m_sashPosition
+ m_sashSize
+ tolerance
)) )
355 // Draw 3D effect borders
356 void wxSplitterWindow::DrawBorders(wxDC
& dc
)
359 GetClientSize(&w
, &h
);
361 if ( GetWindowStyleFlag() & wxSP_3D
)
363 dc
.SetPen(*m_mediumShadowPen
);
364 dc
.DrawLine(0, 0, w
-1, 0);
365 dc
.DrawLine(0, 0, 0, h
- 1);
367 dc
.SetPen(*m_darkShadowPen
);
368 dc
.DrawLine(1, 1, w
-2, 1);
369 dc
.DrawLine(1, 1, 1, h
-2);
371 dc
.SetPen(*m_hilightPen
);
372 dc
.DrawLine(0, h
-1, w
-1, h
-1);
373 dc
.DrawLine(w
-1, 0, w
-1, h
); // Surely the maximum y pos. should be h - 1.
374 /// Anyway, h is required for MSW.
376 dc
.SetPen(*m_lightShadowPen
);
377 dc
.DrawLine(w
-2, 1, w
-2, h
-2); // Right hand side
378 dc
.DrawLine(1, h
-2, w
-1, h
-2); // Bottom
380 else if ( GetWindowStyleFlag() & wxSP_BORDER
)
382 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
383 dc
.SetPen(*wxBLACK_PEN
);
384 dc
.DrawRectangle(0, 0, w
-1, h
-1);
387 dc
.SetPen(wxNullPen
);
388 dc
.SetBrush(wxNullBrush
);
392 void wxSplitterWindow::DrawSash(wxDC
& dc
)
394 if ( m_sashPosition
== 0 || !m_windowTwo
)
398 GetClientSize(&w
, &h
);
400 if ( GetWindowStyleFlag() & wxSP_3D
)
402 if ( m_splitMode
== wxSPLIT_VERTICAL
)
404 dc
.SetPen(*m_facePen
);
405 dc
.SetBrush(*m_faceBrush
);
406 dc
.DrawRectangle(m_sashPosition
+ 2, 0, m_sashSize
- 4, h
);
408 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
410 dc
.SetPen(*m_lightShadowPen
);
411 dc
.DrawLine(m_sashPosition
, 1, m_sashPosition
, h
-2);
413 dc
.SetPen(*m_hilightPen
);
414 dc
.DrawLine(m_sashPosition
+1, 0, m_sashPosition
+1, h
);
416 dc
.SetPen(*m_mediumShadowPen
);
417 dc
.DrawLine(m_sashPosition
+m_sashSize
-2, 1, m_sashPosition
+m_sashSize
-2, h
-1);
419 dc
.SetPen(*m_darkShadowPen
);
420 dc
.DrawLine(m_sashPosition
+m_sashSize
-1, 2, m_sashPosition
+m_sashSize
-1, h
-2);
424 dc
.SetPen(*m_facePen
);
425 dc
.SetBrush(*m_faceBrush
);
426 dc
.DrawRectangle(0, m_sashPosition
+ 2, w
, m_sashSize
- 4);
428 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
430 dc
.SetPen(*m_lightShadowPen
);
431 dc
.DrawLine(1, m_sashPosition
, w
-2, m_sashPosition
);
433 dc
.SetPen(*m_hilightPen
);
434 dc
.DrawLine(0, m_sashPosition
+1, w
, m_sashPosition
+1);
436 dc
.SetPen(*m_mediumShadowPen
);
437 dc
.DrawLine(1, m_sashPosition
+m_sashSize
-2, w
-1, m_sashPosition
+m_sashSize
-2);
439 dc
.SetPen(*m_darkShadowPen
);
440 dc
.DrawLine(2, m_sashPosition
+m_sashSize
-1, w
-2, m_sashPosition
+m_sashSize
-1);
445 if ( m_splitMode
== wxSPLIT_VERTICAL
)
447 dc
.SetPen(*wxBLACK_PEN
);
448 dc
.SetBrush(*wxBLACK_BRUSH
);
450 if ( (GetWindowStyleFlag() & wxSP_BORDER
) != wxSP_BORDER
)
451 h1
+= 1; // Not sure why this is necessary...
452 dc
.DrawRectangle(m_sashPosition
, 0, m_sashSize
, h1
);
456 dc
.SetPen(*wxBLACK_PEN
);
457 dc
.SetBrush(*wxBLACK_BRUSH
);
459 if ( (GetWindowStyleFlag() & wxSP_BORDER
) != wxSP_BORDER
)
462 dc
.DrawRectangle(0, m_sashPosition
, w1
, m_sashSize
);
467 dc
.SetPen(wxNullPen
);
468 dc
.SetBrush(wxNullBrush
);
471 // Draw the sash tracker (for whilst moving the sash)
472 void wxSplitterWindow::DrawSashTracker(int x
, int y
)
475 GetClientSize(&w
, &h
);
481 if ( m_splitMode
== wxSPLIT_VERTICAL
)
512 ClientToScreen(&x1
, &y1
);
513 ClientToScreen(&x2
, &y2
);
515 screenDC
.SetLogicalFunction(wxXOR
);
516 screenDC
.SetPen(*m_sashTrackerPen
);
517 screenDC
.SetBrush(*wxTRANSPARENT_BRUSH
);
519 screenDC
.DrawLine(x1
, y1
, x2
, y2
);
521 screenDC
.SetLogicalFunction(wxCOPY
);
523 screenDC
.SetPen(wxNullPen
);
524 screenDC
.SetBrush(wxNullBrush
);
527 // Position and size subwindows.
528 // Note that the border size applies to each subwindow, not
529 // including the edges next to the sash.
530 void wxSplitterWindow::SizeWindows()
533 GetClientSize(&w
, &h
);
535 if ( m_windowOne
&& !m_windowTwo
)
537 m_windowOne
->SetSize(m_borderSize
, m_borderSize
, w
- 2*m_borderSize
, h
- 2*m_borderSize
);
539 else if ( m_windowOne
&& m_windowTwo
)
541 if (m_splitMode
== wxSPLIT_VERTICAL
)
543 int x1
= m_borderSize
;
544 int y1
= m_borderSize
;
545 int w1
= m_sashPosition
- m_borderSize
;
546 int h1
= h
- 2*m_borderSize
;
548 int x2
= m_sashPosition
+ m_sashSize
;
549 int y2
= m_borderSize
;
550 int w2
= w
- 2*m_borderSize
- m_sashSize
- w1
;
551 int h2
= h
- 2*m_borderSize
;
553 m_windowOne
->SetSize(x1
, y1
, w1
, h1
);
554 m_windowTwo
->SetSize(x2
, y2
, w2
, h2
);
558 m_windowOne
->SetSize(m_borderSize
, m_borderSize
,
559 w
- 2*m_borderSize
, m_sashPosition
- m_borderSize
);
560 m_windowTwo
->SetSize(m_borderSize
, m_sashPosition
+ m_sashSize
,
561 w
- 2*m_borderSize
, h
- 2*m_borderSize
- m_sashSize
- (m_sashPosition
- m_borderSize
));
569 // Set pane for unsplit window
570 void wxSplitterWindow::Initialize(wxWindow
*window
)
572 m_windowOne
= window
;
573 m_windowTwo
= (wxWindow
*) NULL
;
577 // Associates the given window with window 2, drawing the appropriate sash
578 // and changing the split mode.
579 // Does nothing and returns FALSE if the window is already split.
580 bool wxSplitterWindow::SplitVertically(wxWindow
*window1
, wxWindow
*window2
, int sashPosition
)
586 GetClientSize(&w
, &h
);
588 m_splitMode
= wxSPLIT_VERTICAL
;
589 m_windowOne
= window1
;
590 m_windowTwo
= window2
;
591 if ( sashPosition
> 0 )
592 m_sashPosition
= sashPosition
;
593 else if ( sashPosition
< 0 )
594 m_sashPosition
= w
- sashPosition
;
596 m_sashPosition
= w
/2;
603 bool wxSplitterWindow::SplitHorizontally(wxWindow
*window1
, wxWindow
*window2
, int sashPosition
)
609 GetClientSize(&w
, &h
);
611 m_splitMode
= wxSPLIT_HORIZONTAL
;
612 m_windowOne
= window1
;
613 m_windowTwo
= window2
;
614 if ( sashPosition
> 0 )
615 m_sashPosition
= sashPosition
;
616 else if ( sashPosition
< 0 )
617 m_sashPosition
= h
- sashPosition
;
619 m_sashPosition
= h
/2;
627 // Remove the specified (or second) window from the view
628 // Doesn't actually delete the window.
629 bool wxSplitterWindow::Unsplit(wxWindow
*toRemove
)
634 if ( toRemove
== NULL
|| toRemove
== m_windowTwo
)
636 wxWindow
*win
= m_windowTwo
;
637 m_windowTwo
= (wxWindow
*) NULL
;
642 else if ( toRemove
== m_windowOne
)
644 wxWindow
*win
= m_windowOne
;
645 m_windowOne
= m_windowTwo
;
646 m_windowTwo
= (wxWindow
*) NULL
;
657 void wxSplitterWindow::SetSashPosition(int position
, bool redraw
)
659 m_sashPosition
= position
;
667 bool wxSplitterWindow::OnSashPositionChange(int newSashPosition
)
669 // is the left/upper pane too small?
670 if ( newSashPosition
< m_minimumPaneSize
)
673 // is the right/lower pane too small?
675 GetClientSize(&w
, &h
);
677 if ( m_splitMode
== wxSPLIT_VERTICAL
)
679 if ( w
- newSashPosition
< m_minimumPaneSize
)
682 else // m_splitMode = wxSPLIT_HORIZONTAL
684 if ( h
- newSashPosition
< m_minimumPaneSize
)
688 // it's ok to move sash
692 // Called when the sash is double-clicked.
693 // The default behaviour is to remove the sash if the
694 // minimum pane size is zero.
695 void wxSplitterWindow::OnDoubleClickSash(int WXUNUSED(x
), int WXUNUSED(y
) )
697 if ( GetMinimumPaneSize() == 0 )
703 // Initialize colours
704 void wxSplitterWindow::InitColours()
706 wxDELETE( m_facePen
);
707 wxDELETE( m_faceBrush
);
708 wxDELETE( m_mediumShadowPen
);
709 wxDELETE( m_darkShadowPen
);
710 wxDELETE( m_lightShadowPen
);
711 wxDELETE( m_hilightPen
);
714 #if defined(__WIN95__)
715 wxColour
faceColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
716 m_facePen
= new wxPen(faceColour
, 1, wxSOLID
);
717 m_faceBrush
= new wxBrush(faceColour
, wxSOLID
);
719 wxColour
mediumShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DSHADOW
));
720 m_mediumShadowPen
= new wxPen(mediumShadowColour
, 1, wxSOLID
);
722 wxColour
darkShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DDKSHADOW
));
723 m_darkShadowPen
= new wxPen(darkShadowColour
, 1, wxSOLID
);
725 wxColour
lightShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT
));
726 m_lightShadowPen
= new wxPen(lightShadowColour
, 1, wxSOLID
);
728 wxColour
hilightColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DHILIGHT
));
729 m_hilightPen
= new wxPen(hilightColour
, 1, wxSOLID
);
731 m_facePen
= new wxPen("LIGHT GREY", 1, wxSOLID
);
732 m_faceBrush
= new wxBrush("LIGHT GREY", wxSOLID
);
733 m_mediumShadowPen
= new wxPen("GREY", 1, wxSOLID
);
734 m_darkShadowPen
= new wxPen("BLACK", 1, wxSOLID
);
735 m_lightShadowPen
= new wxPen("LIGHT GREY", 1, wxSOLID
);
736 m_hilightPen
= new wxPen("WHITE", 1, wxSOLID
);
737 #endif // Win32/!Win32