]>
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"
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 )
144 void wxSplitterWindow::OnMouseEvent(wxMouseEvent
& event
)
146 long x
= event
.GetX();
147 long y
= event
.GetY();
151 SetCursor(* wxSTANDARD_CURSOR
);
154 SetCursor(wxCursor());
157 // under wxGTK the method above causes the mouse
158 // to flicker so we set the standard cursor only
159 // when leaving the window and when moving over
160 // non-sash parts of the window. this should work
161 // on the other platforms as well, but who knows.
164 SetCursor(* wxSTANDARD_CURSOR
);
167 if (event
.LeftDown())
169 if ( SashHitTest(x
, y
) )
173 m_dragMode
= wxSPLIT_DRAG_DRAGGING
;
175 DrawSashTracker(x
, y
);
181 else if (event
.LeftUp() && m_dragMode
== wxSPLIT_DRAG_DRAGGING
)
183 // We can stop dragging now and see what we've got.
184 m_dragMode
= wxSPLIT_DRAG_NONE
;
188 DrawSashTracker(m_oldX
, m_oldY
);
190 // Obtain window size. We are only interested in the dimension the sash
193 GetClientSize(&w
, &h
);
194 int window_size
= (m_splitMode
== wxSPLIT_VERTICAL
? w
: h
);
195 int new_sash_position
=
196 (int) ( m_splitMode
== wxSPLIT_VERTICAL
? x
: y
);
198 if ( !OnSashPositionChange(new_sash_position
) )
201 if ( new_sash_position
== 0 )
203 // We remove the first window from the view
204 wxWindow
*removedWindow
= m_windowOne
;
205 m_windowOne
= m_windowTwo
;
206 m_windowTwo
= (wxWindow
*) NULL
;
207 OnUnsplit(removedWindow
);
210 else if ( new_sash_position
== window_size
)
212 // We remove the second window from the view
213 wxWindow
*removedWindow
= m_windowTwo
;
214 m_windowTwo
= (wxWindow
*) NULL
;
215 OnUnsplit(removedWindow
);
220 m_sashPosition
= new_sash_position
;
223 } // left up && dragging
224 else if (event
.Moving() && !event
.Dragging())
226 // Just change the cursor if required
227 if ( SashHitTest(x
, y
) )
229 if ( m_splitMode
== wxSPLIT_VERTICAL
)
231 SetCursor(*m_sashCursorWE
);
235 SetCursor(*m_sashCursorNS
);
241 // where else do we unset the cursor?
242 SetCursor(* wxSTANDARD_CURSOR
);
246 else if (event
.Dragging() && (m_dragMode
== wxSPLIT_DRAG_DRAGGING
))
249 DrawSashTracker(m_oldX
, m_oldY
);
252 DrawSashTracker(x
, y
);
257 else if ( event
.LeftDClick() )
259 OnDoubleClickSash(x
, y
);
263 void wxSplitterWindow::OnSize(wxSizeEvent
& WXUNUSED(event
))
266 GetClientSize( &cw
, &ch
);
269 if ( m_splitMode
== wxSPLIT_VERTICAL
)
271 if ( m_sashPosition
>= (cw
- 5) )
272 m_sashPosition
= wxMax(10, cw
- 40);
274 if ( m_splitMode
== wxSPLIT_HORIZONTAL
)
276 if ( m_sashPosition
>= (ch
- 5) )
277 m_sashPosition
= wxMax(10, ch
- 40);
283 bool wxSplitterWindow::SashHitTest(int x
, int y
, int tolerance
)
285 if ( m_windowTwo
== NULL
|| m_sashPosition
== 0)
286 return FALSE
; // No sash
288 if ( m_splitMode
== wxSPLIT_VERTICAL
)
290 if ( (x
>= m_sashPosition
- tolerance
) && (x
<= m_sashPosition
+ m_sashSize
+ tolerance
) )
297 if ( (y
>= (m_sashPosition
- tolerance
)) && (y
<= (m_sashPosition
+ m_sashSize
+ tolerance
)) )
306 // Draw 3D effect borders
307 void wxSplitterWindow::DrawBorders(wxDC
& dc
)
310 GetClientSize(&w
, &h
);
312 if ( GetWindowStyleFlag() & wxSP_3D
)
314 dc
.SetPen(*m_mediumShadowPen
);
315 dc
.DrawLine(0, 0, w
-1, 0);
316 dc
.DrawLine(0, 0, 0, h
- 1);
318 dc
.SetPen(*m_darkShadowPen
);
319 dc
.DrawLine(1, 1, w
-2, 1);
320 dc
.DrawLine(1, 1, 1, h
-2);
322 dc
.SetPen(*m_hilightPen
);
323 dc
.DrawLine(0, h
-1, w
-1, h
-1);
324 dc
.DrawLine(w
-1, 0, w
-1, h
); // Surely the maximum y pos. should be h - 1.
325 /// Anyway, h is required for MSW.
327 dc
.SetPen(*m_lightShadowPen
);
328 dc
.DrawLine(w
-2, 1, w
-2, h
-2); // Right hand side
329 dc
.DrawLine(1, h
-2, w
-1, h
-2); // Bottom
331 else if ( GetWindowStyleFlag() & wxSP_BORDER
)
333 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
334 dc
.SetPen(*wxBLACK_PEN
);
335 dc
.DrawRectangle(0, 0, w
-1, h
-1);
338 dc
.SetPen(wxNullPen
);
339 dc
.SetBrush(wxNullBrush
);
343 void wxSplitterWindow::DrawSash(wxDC
& dc
)
345 if ( m_sashPosition
== 0 || !m_windowTwo
)
349 GetClientSize(&w
, &h
);
351 if ( GetWindowStyleFlag() & wxSP_3D
)
353 if ( m_splitMode
== wxSPLIT_VERTICAL
)
355 dc
.SetPen(*m_facePen
);
356 dc
.SetBrush(*m_faceBrush
);
357 dc
.DrawRectangle(m_sashPosition
+ 2, 0, m_sashSize
- 4, h
);
359 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
361 dc
.SetPen(*m_lightShadowPen
);
362 dc
.DrawLine(m_sashPosition
, 1, m_sashPosition
, h
-2);
364 dc
.SetPen(*m_hilightPen
);
365 dc
.DrawLine(m_sashPosition
+1, 0, m_sashPosition
+1, h
);
367 dc
.SetPen(*m_mediumShadowPen
);
368 dc
.DrawLine(m_sashPosition
+m_sashSize
-2, 1, m_sashPosition
+m_sashSize
-2, h
-1);
370 dc
.SetPen(*m_darkShadowPen
);
371 dc
.DrawLine(m_sashPosition
+m_sashSize
-1, 2, m_sashPosition
+m_sashSize
-1, h
-2);
375 dc
.SetPen(*m_facePen
);
376 dc
.SetBrush(*m_faceBrush
);
377 dc
.DrawRectangle(0, m_sashPosition
+ 2, w
, m_sashSize
- 4);
379 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
381 dc
.SetPen(*m_lightShadowPen
);
382 dc
.DrawLine(1, m_sashPosition
, w
-2, m_sashPosition
);
384 dc
.SetPen(*m_hilightPen
);
385 dc
.DrawLine(0, m_sashPosition
+1, w
, m_sashPosition
+1);
387 dc
.SetPen(*m_mediumShadowPen
);
388 dc
.DrawLine(1, m_sashPosition
+m_sashSize
-2, w
-1, m_sashPosition
+m_sashSize
-2);
390 dc
.SetPen(*m_darkShadowPen
);
391 dc
.DrawLine(2, m_sashPosition
+m_sashSize
-1, w
-2, m_sashPosition
+m_sashSize
-1);
396 if ( m_splitMode
== wxSPLIT_VERTICAL
)
398 dc
.SetPen(*wxBLACK_PEN
);
399 dc
.SetBrush(*wxBLACK_BRUSH
);
401 if ( (GetWindowStyleFlag() & wxSP_BORDER
) != wxSP_BORDER
)
402 h1
+= 1; // Not sure why this is necessary...
403 dc
.DrawRectangle(m_sashPosition
, 0, m_sashSize
, h1
);
407 dc
.SetPen(*wxBLACK_PEN
);
408 dc
.SetBrush(*wxBLACK_BRUSH
);
410 if ( (GetWindowStyleFlag() & wxSP_BORDER
) != wxSP_BORDER
)
413 dc
.DrawRectangle(0, m_sashPosition
, w1
, m_sashSize
);
418 dc
.SetPen(wxNullPen
);
419 dc
.SetBrush(wxNullBrush
);
422 // Draw the sash tracker (for whilst moving the sash)
423 void wxSplitterWindow::DrawSashTracker(int x
, int y
)
426 GetClientSize(&w
, &h
);
432 if ( m_splitMode
== wxSPLIT_VERTICAL
)
463 ClientToScreen(&x1
, &y1
);
464 ClientToScreen(&x2
, &y2
);
466 screenDC
.SetLogicalFunction(wxXOR
);
467 screenDC
.SetPen(*m_sashTrackerPen
);
468 screenDC
.SetBrush(*wxTRANSPARENT_BRUSH
);
470 screenDC
.DrawLine(x1
, y1
, x2
, y2
);
472 screenDC
.SetLogicalFunction(wxCOPY
);
474 screenDC
.SetPen(wxNullPen
);
475 screenDC
.SetBrush(wxNullBrush
);
478 // Position and size subwindows.
479 // Note that the border size applies to each subwindow, not
480 // including the edges next to the sash.
481 void wxSplitterWindow::SizeWindows()
484 GetClientSize(&w
, &h
);
486 if ( m_windowOne
&& !m_windowTwo
)
488 m_windowOne
->SetSize(m_borderSize
, m_borderSize
, w
- 2*m_borderSize
, h
- 2*m_borderSize
);
490 else if ( m_windowOne
&& m_windowTwo
)
492 if (m_splitMode
== wxSPLIT_VERTICAL
)
494 int x1
= m_borderSize
;
495 int y1
= m_borderSize
;
496 int w1
= m_sashPosition
- m_borderSize
;
497 int h1
= h
- 2*m_borderSize
;
499 int x2
= m_sashPosition
+ m_sashSize
;
500 int y2
= m_borderSize
;
501 int w2
= w
- 2*m_borderSize
- m_sashSize
- w1
;
502 int h2
= h
- 2*m_borderSize
;
504 m_windowOne
->SetSize(x1
, y1
, w1
, h1
);
505 m_windowTwo
->SetSize(x2
, y2
, w2
, h2
);
509 m_windowOne
->SetSize(m_borderSize
, m_borderSize
,
510 w
- 2*m_borderSize
, m_sashPosition
- m_borderSize
);
511 m_windowTwo
->SetSize(m_borderSize
, m_sashPosition
+ m_sashSize
,
512 w
- 2*m_borderSize
, h
- 2*m_borderSize
- m_sashSize
- (m_sashPosition
- m_borderSize
));
520 // Set pane for unsplit window
521 void wxSplitterWindow::Initialize(wxWindow
*window
)
523 m_windowOne
= window
;
524 m_windowTwo
= (wxWindow
*) NULL
;
528 // Associates the given window with window 2, drawing the appropriate sash
529 // and changing the split mode.
530 // Does nothing and returns FALSE if the window is already split.
531 bool wxSplitterWindow::SplitVertically(wxWindow
*window1
, wxWindow
*window2
, int sashPosition
)
537 GetClientSize(&w
, &h
);
539 m_splitMode
= wxSPLIT_VERTICAL
;
540 m_windowOne
= window1
;
541 m_windowTwo
= window2
;
542 if ( sashPosition
> 0 )
543 m_sashPosition
= sashPosition
;
544 else if ( sashPosition
< 0 )
545 m_sashPosition
= w
- sashPosition
;
547 m_sashPosition
= w
/2;
554 bool wxSplitterWindow::SplitHorizontally(wxWindow
*window1
, wxWindow
*window2
, int sashPosition
)
560 GetClientSize(&w
, &h
);
562 m_splitMode
= wxSPLIT_HORIZONTAL
;
563 m_windowOne
= window1
;
564 m_windowTwo
= window2
;
565 if ( sashPosition
> 0 )
566 m_sashPosition
= sashPosition
;
567 else if ( sashPosition
< 0 )
568 m_sashPosition
= h
- sashPosition
;
570 m_sashPosition
= h
/2;
578 // Remove the specified (or second) window from the view
579 // Doesn't actually delete the window.
580 bool wxSplitterWindow::Unsplit(wxWindow
*toRemove
)
585 wxWindow
*win
= NULL
;
586 if ( toRemove
== NULL
|| toRemove
== m_windowTwo
)
589 m_windowTwo
= (wxWindow
*) NULL
;
591 else if ( toRemove
== m_windowOne
)
594 m_windowOne
= m_windowTwo
;
595 m_windowTwo
= (wxWindow
*) NULL
;
599 wxFAIL_MSG(_T("splitter: attempt to remove a non-existent window"));
611 // Replace a window with another one
612 bool wxSplitterWindow::ReplaceWindow(wxWindow
*winOld
, wxWindow
*winNew
)
614 wxCHECK_MSG( winOld
, FALSE
, _T("use one of Split() functions instead") );
615 wxCHECK_MSG( winNew
, FALSE
, _T("use Unsplit() functions instead") );
617 if ( winOld
== m_windowTwo
)
619 m_windowTwo
= winNew
;
621 else if ( winOld
== m_windowOne
)
623 m_windowOne
= winNew
;
627 wxFAIL_MSG(_T("splitter: attempt to replace a non-existent window"));
637 void wxSplitterWindow::SetSashPosition(int position
, bool redraw
)
639 m_sashPosition
= position
;
647 bool wxSplitterWindow::OnSashPositionChange(int& newSashPosition
)
649 // If within UNSPLIT_THRESHOLD from edge, set to edge to cause closure.
650 const int UNSPLIT_THRESHOLD
= 4;
652 if (newSashPosition
<= UNSPLIT_THRESHOLD
) // threshold top / left check
658 // Obtain relevant window dimension for bottom / right threshold check
660 GetClientSize(&w
, &h
);
661 int window_size
= (m_splitMode
== wxSPLIT_VERTICAL
) ? w
: h
;
663 if ( newSashPosition
>= window_size
- UNSPLIT_THRESHOLD
)
665 newSashPosition
= window_size
;
669 // If resultant pane would be too small, enlarge it.
671 // Check upper / left pane
672 if ( newSashPosition
< m_minimumPaneSize
)
673 newSashPosition
= m_minimumPaneSize
; // NB: don't return just yet
675 // Check lower / right pane (check even if sash was just adjusted)
676 if ( newSashPosition
> window_size
- m_minimumPaneSize
)
677 newSashPosition
= window_size
- m_minimumPaneSize
;
679 // If the result is out of bounds it means minimum size is too big, so
680 // split window in half as best compromise.
681 if (newSashPosition
< 0 || newSashPosition
> window_size
)
682 newSashPosition
= window_size
/ 2;
686 // Called when the sash is double-clicked.
687 // The default behaviour is to remove the sash if the
688 // minimum pane size is zero.
689 void wxSplitterWindow::OnDoubleClickSash(int WXUNUSED(x
), int WXUNUSED(y
) )
691 if ( GetMinimumPaneSize() == 0 )
697 // Initialize colours
698 void wxSplitterWindow::InitColours()
700 wxDELETE( m_facePen
);
701 wxDELETE( m_faceBrush
);
702 wxDELETE( m_mediumShadowPen
);
703 wxDELETE( m_darkShadowPen
);
704 wxDELETE( m_lightShadowPen
);
705 wxDELETE( m_hilightPen
);
708 #if defined(__WIN95__)
709 wxColour
faceColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
710 m_facePen
= new wxPen(faceColour
, 1, wxSOLID
);
711 m_faceBrush
= new wxBrush(faceColour
, wxSOLID
);
713 wxColour
mediumShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DSHADOW
));
714 m_mediumShadowPen
= new wxPen(mediumShadowColour
, 1, wxSOLID
);
716 wxColour
darkShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DDKSHADOW
));
717 m_darkShadowPen
= new wxPen(darkShadowColour
, 1, wxSOLID
);
719 wxColour
lightShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT
));
720 m_lightShadowPen
= new wxPen(lightShadowColour
, 1, wxSOLID
);
722 wxColour
hilightColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DHILIGHT
));
723 m_hilightPen
= new wxPen(hilightColour
, 1, wxSOLID
);
725 m_facePen
= new wxPen("LIGHT GREY", 1, wxSOLID
);
726 m_faceBrush
= new wxBrush("LIGHT GREY", wxSOLID
);
727 m_mediumShadowPen
= new wxPen("GREY", 1, wxSOLID
);
728 m_darkShadowPen
= new wxPen("BLACK", 1, wxSOLID
);
729 m_lightShadowPen
= new wxPen("LIGHT GREY", 1, wxSOLID
);
730 m_hilightPen
= new wxPen("WHITE", 1, wxSOLID
);
731 #endif // Win32/!Win32