]>
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 )
143 void wxSplitterWindow::OnMouseEvent(wxMouseEvent
& event
)
146 event
.Position(&x
, &y
);
150 SetCursor(* wxSTANDARD_CURSOR
);
153 SetCursor(wxCursor());
156 if (event
.LeftDown())
158 if ( SashHitTest(x
, y
) )
162 m_dragMode
= wxSPLIT_DRAG_DRAGGING
;
164 DrawSashTracker(x
, y
);
170 else if (event
.LeftUp() && m_dragMode
== wxSPLIT_DRAG_DRAGGING
)
172 // We can stop dragging now and see what we've got.
173 m_dragMode
= wxSPLIT_DRAG_NONE
;
177 DrawSashTracker(m_oldX
, m_oldY
);
180 GetClientSize(&w
, &h
);
181 if ( m_splitMode
== wxSPLIT_VERTICAL
)
183 if ( !OnSashPositionChange(x
) )
188 // We remove the first window from the view
189 wxWindow
*removedWindow
= m_windowOne
;
190 m_windowOne
= m_windowTwo
;
191 m_windowTwo
= (wxWindow
*) NULL
;
193 OnUnsplit(removedWindow
);
196 else if ( x
>= (w
- 4) )
198 // We remove the second window from the view
199 wxWindow
*removedWindow
= m_windowTwo
;
200 m_windowTwo
= (wxWindow
*) NULL
;
201 OnUnsplit(removedWindow
);
209 else // m_splitMode == wxSPLIT_VERTICAL
211 if ( !OnSashPositionChange(y
) )
216 // We remove the first window from the view
217 wxWindow
*removedWindow
= m_windowOne
;
218 m_windowOne
= m_windowTwo
;
219 m_windowTwo
= (wxWindow
*) NULL
;
221 OnUnsplit(removedWindow
);
224 else if ( y
>= (h
- 4) )
226 // We remove the second window from the view
227 wxWindow
*removedWindow
= m_windowTwo
;
228 m_windowTwo
= (wxWindow
*) NULL
;
229 OnUnsplit(removedWindow
);
236 } // m_splitMode == wxSPLIT_VERTICAL
238 } // left up && dragging
239 else if (event
.Moving() && !event
.Dragging())
241 // Just change the cursor if required
242 if ( SashHitTest(x
, y
) )
244 if ( m_splitMode
== wxSPLIT_VERTICAL
)
246 SetCursor(*m_sashCursorWE
);
250 SetCursor(*m_sashCursorNS
);
254 else if (event
.Dragging() && (m_dragMode
== wxSPLIT_DRAG_DRAGGING
))
257 DrawSashTracker(m_oldX
, m_oldY
);
260 DrawSashTracker(x
, y
);
265 else if ( event
.LeftDClick() )
267 OnDoubleClickSash(x
, y
);
271 void wxSplitterWindow::OnSize(wxSizeEvent
& WXUNUSED(event
))
274 GetClientSize( &cw
, &ch
);
277 if ( m_splitMode
== wxSPLIT_VERTICAL
)
279 if ( m_sashPosition
>= (cw
- 5) )
280 m_sashPosition
= wxMax(10, cw
- 40);
282 if ( m_splitMode
== wxSPLIT_HORIZONTAL
)
284 if ( m_sashPosition
>= (ch
- 5) )
285 m_sashPosition
= wxMax(10, ch
- 40);
291 bool wxSplitterWindow::SashHitTest(int x
, int y
, int tolerance
)
293 if ( m_windowTwo
== NULL
|| m_sashPosition
== 0)
294 return FALSE
; // No sash
296 if ( m_splitMode
== wxSPLIT_VERTICAL
)
298 if ( (x
>= m_sashPosition
- tolerance
) && (x
<= m_sashPosition
+ m_sashSize
+ tolerance
) )
305 if ( (y
>= (m_sashPosition
- tolerance
)) && (y
<= (m_sashPosition
+ m_sashSize
+ tolerance
)) )
314 // Draw 3D effect borders
315 void wxSplitterWindow::DrawBorders(wxDC
& dc
)
318 GetClientSize(&w
, &h
);
320 if ( GetWindowStyleFlag() & wxSP_3D
)
322 dc
.SetPen(*m_mediumShadowPen
);
323 dc
.DrawLine(0, 0, w
-1, 0);
324 dc
.DrawLine(0, 0, 0, h
- 1);
326 dc
.SetPen(*m_darkShadowPen
);
327 dc
.DrawLine(1, 1, w
-2, 1);
328 dc
.DrawLine(1, 1, 1, h
-2);
330 dc
.SetPen(*m_hilightPen
);
331 dc
.DrawLine(0, h
-1, w
-1, h
-1);
332 dc
.DrawLine(w
-1, 0, w
-1, h
); // Surely the maximum y pos. should be h - 1.
333 /// Anyway, h is required for MSW.
335 dc
.SetPen(*m_lightShadowPen
);
336 dc
.DrawLine(w
-2, 1, w
-2, h
-2); // Right hand side
337 dc
.DrawLine(1, h
-2, w
-1, h
-2); // Bottom
339 else if ( GetWindowStyleFlag() & wxSP_BORDER
)
341 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
342 dc
.SetPen(*wxBLACK_PEN
);
343 dc
.DrawRectangle(0, 0, w
-1, h
-1);
346 dc
.SetPen(wxNullPen
);
347 dc
.SetBrush(wxNullBrush
);
351 void wxSplitterWindow::DrawSash(wxDC
& dc
)
353 if ( m_sashPosition
== 0 || !m_windowTwo
)
357 GetClientSize(&w
, &h
);
359 if ( GetWindowStyleFlag() & wxSP_3D
)
361 if ( m_splitMode
== wxSPLIT_VERTICAL
)
363 dc
.SetPen(*m_facePen
);
364 dc
.SetBrush(*m_faceBrush
);
365 dc
.DrawRectangle(m_sashPosition
+ 2, 0, m_sashSize
- 4, h
);
367 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
369 dc
.SetPen(*m_lightShadowPen
);
370 dc
.DrawLine(m_sashPosition
, 1, m_sashPosition
, h
-2);
372 dc
.SetPen(*m_hilightPen
);
373 dc
.DrawLine(m_sashPosition
+1, 0, m_sashPosition
+1, h
);
375 dc
.SetPen(*m_mediumShadowPen
);
376 dc
.DrawLine(m_sashPosition
+m_sashSize
-2, 1, m_sashPosition
+m_sashSize
-2, h
-1);
378 dc
.SetPen(*m_darkShadowPen
);
379 dc
.DrawLine(m_sashPosition
+m_sashSize
-1, 2, m_sashPosition
+m_sashSize
-1, h
-2);
383 dc
.SetPen(*m_facePen
);
384 dc
.SetBrush(*m_faceBrush
);
385 dc
.DrawRectangle(0, m_sashPosition
+ 2, w
, m_sashSize
- 4);
387 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
389 dc
.SetPen(*m_lightShadowPen
);
390 dc
.DrawLine(1, m_sashPosition
, w
-2, m_sashPosition
);
392 dc
.SetPen(*m_hilightPen
);
393 dc
.DrawLine(0, m_sashPosition
+1, w
, m_sashPosition
+1);
395 dc
.SetPen(*m_mediumShadowPen
);
396 dc
.DrawLine(1, m_sashPosition
+m_sashSize
-2, w
-1, m_sashPosition
+m_sashSize
-2);
398 dc
.SetPen(*m_darkShadowPen
);
399 dc
.DrawLine(2, m_sashPosition
+m_sashSize
-1, w
-2, m_sashPosition
+m_sashSize
-1);
404 if ( m_splitMode
== wxSPLIT_VERTICAL
)
406 dc
.SetPen(*wxBLACK_PEN
);
407 dc
.SetBrush(*wxBLACK_BRUSH
);
409 if ( (GetWindowStyleFlag() & wxSP_BORDER
) != wxSP_BORDER
)
410 h1
+= 1; // Not sure why this is necessary...
411 dc
.DrawRectangle(m_sashPosition
, 0, m_sashSize
, h1
);
415 dc
.SetPen(*wxBLACK_PEN
);
416 dc
.SetBrush(*wxBLACK_BRUSH
);
418 if ( (GetWindowStyleFlag() & wxSP_BORDER
) != wxSP_BORDER
)
421 dc
.DrawRectangle(0, m_sashPosition
, w1
, m_sashSize
);
426 dc
.SetPen(wxNullPen
);
427 dc
.SetBrush(wxNullBrush
);
430 // Draw the sash tracker (for whilst moving the sash)
431 void wxSplitterWindow::DrawSashTracker(int x
, int y
)
434 GetClientSize(&w
, &h
);
440 if ( m_splitMode
== wxSPLIT_VERTICAL
)
471 ClientToScreen(&x1
, &y1
);
472 ClientToScreen(&x2
, &y2
);
474 screenDC
.SetLogicalFunction(wxXOR
);
475 screenDC
.SetPen(*m_sashTrackerPen
);
476 screenDC
.SetBrush(*wxTRANSPARENT_BRUSH
);
478 screenDC
.DrawLine(x1
, y1
, x2
, y2
);
480 screenDC
.SetLogicalFunction(wxCOPY
);
482 screenDC
.SetPen(wxNullPen
);
483 screenDC
.SetBrush(wxNullBrush
);
486 // Position and size subwindows.
487 // Note that the border size applies to each subwindow, not
488 // including the edges next to the sash.
489 void wxSplitterWindow::SizeWindows()
492 GetClientSize(&w
, &h
);
494 if ( m_windowOne
&& !m_windowTwo
)
496 m_windowOne
->SetSize(m_borderSize
, m_borderSize
, w
- 2*m_borderSize
, h
- 2*m_borderSize
);
498 else if ( m_windowOne
&& m_windowTwo
)
500 if (m_splitMode
== wxSPLIT_VERTICAL
)
502 int x1
= m_borderSize
;
503 int y1
= m_borderSize
;
504 int w1
= m_sashPosition
- m_borderSize
;
505 int h1
= h
- 2*m_borderSize
;
507 int x2
= m_sashPosition
+ m_sashSize
;
508 int y2
= m_borderSize
;
509 int w2
= w
- 2*m_borderSize
- m_sashSize
- w1
;
510 int h2
= h
- 2*m_borderSize
;
512 m_windowOne
->SetSize(x1
, y1
, w1
, h1
);
513 m_windowTwo
->SetSize(x2
, y2
, w2
, h2
);
517 m_windowOne
->SetSize(m_borderSize
, m_borderSize
,
518 w
- 2*m_borderSize
, m_sashPosition
- m_borderSize
);
519 m_windowTwo
->SetSize(m_borderSize
, m_sashPosition
+ m_sashSize
,
520 w
- 2*m_borderSize
, h
- 2*m_borderSize
- m_sashSize
- (m_sashPosition
- m_borderSize
));
528 // Set pane for unsplit window
529 void wxSplitterWindow::Initialize(wxWindow
*window
)
531 m_windowOne
= window
;
532 m_windowTwo
= (wxWindow
*) NULL
;
536 // Associates the given window with window 2, drawing the appropriate sash
537 // and changing the split mode.
538 // Does nothing and returns FALSE if the window is already split.
539 bool wxSplitterWindow::SplitVertically(wxWindow
*window1
, wxWindow
*window2
, int sashPosition
)
545 GetClientSize(&w
, &h
);
547 m_splitMode
= wxSPLIT_VERTICAL
;
548 m_windowOne
= window1
;
549 m_windowTwo
= window2
;
550 if ( sashPosition
> 0 )
551 m_sashPosition
= sashPosition
;
552 else if ( sashPosition
< 0 )
553 m_sashPosition
= w
- sashPosition
;
555 m_sashPosition
= w
/2;
562 bool wxSplitterWindow::SplitHorizontally(wxWindow
*window1
, wxWindow
*window2
, int sashPosition
)
568 GetClientSize(&w
, &h
);
570 m_splitMode
= wxSPLIT_HORIZONTAL
;
571 m_windowOne
= window1
;
572 m_windowTwo
= window2
;
573 if ( sashPosition
> 0 )
574 m_sashPosition
= sashPosition
;
575 else if ( sashPosition
< 0 )
576 m_sashPosition
= h
- sashPosition
;
578 m_sashPosition
= h
/2;
586 // Remove the specified (or second) window from the view
587 // Doesn't actually delete the window.
588 bool wxSplitterWindow::Unsplit(wxWindow
*toRemove
)
593 wxWindow
*win
= NULL
;
594 if ( toRemove
== NULL
|| toRemove
== m_windowTwo
)
597 m_windowTwo
= (wxWindow
*) NULL
;
599 else if ( toRemove
== m_windowOne
)
602 m_windowOne
= m_windowTwo
;
603 m_windowTwo
= (wxWindow
*) NULL
;
607 wxFAIL_MSG("splitter: attempt to remove a non-existent window");
619 // Replace a window with another one
620 bool wxSplitterWindow::ReplaceWindow(wxWindow
*winOld
, wxWindow
*winNew
)
622 wxCHECK_MSG( winOld
, FALSE
, "use one of Split() functions instead" );
623 wxCHECK_MSG( winNew
, FALSE
, "use Unsplit() functions instead" );
625 if ( winOld
== m_windowTwo
)
627 m_windowTwo
= winNew
;
629 else if ( winOld
== m_windowOne
)
631 m_windowOne
= winNew
;
635 wxFAIL_MSG("splitter: attempt to replace a non-existent window");
645 void wxSplitterWindow::SetSashPosition(int position
, bool redraw
)
647 m_sashPosition
= position
;
655 bool wxSplitterWindow::OnSashPositionChange(int newSashPosition
)
657 // is the left/upper pane too small?
658 if ( newSashPosition
< m_minimumPaneSize
)
661 // is the right/lower pane too small?
663 GetClientSize(&w
, &h
);
665 if ( m_splitMode
== wxSPLIT_VERTICAL
)
667 if ( w
- newSashPosition
< m_minimumPaneSize
)
670 else // m_splitMode = wxSPLIT_HORIZONTAL
672 if ( h
- newSashPosition
< m_minimumPaneSize
)
676 // it's ok to move sash
680 // Called when the sash is double-clicked.
681 // The default behaviour is to remove the sash if the
682 // minimum pane size is zero.
683 void wxSplitterWindow::OnDoubleClickSash(int WXUNUSED(x
), int WXUNUSED(y
) )
685 if ( GetMinimumPaneSize() == 0 )
691 // Initialize colours
692 void wxSplitterWindow::InitColours()
694 wxDELETE( m_facePen
);
695 wxDELETE( m_faceBrush
);
696 wxDELETE( m_mediumShadowPen
);
697 wxDELETE( m_darkShadowPen
);
698 wxDELETE( m_lightShadowPen
);
699 wxDELETE( m_hilightPen
);
702 #if defined(__WIN95__)
703 wxColour
faceColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
704 m_facePen
= new wxPen(faceColour
, 1, wxSOLID
);
705 m_faceBrush
= new wxBrush(faceColour
, wxSOLID
);
707 wxColour
mediumShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DSHADOW
));
708 m_mediumShadowPen
= new wxPen(mediumShadowColour
, 1, wxSOLID
);
710 wxColour
darkShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DDKSHADOW
));
711 m_darkShadowPen
= new wxPen(darkShadowColour
, 1, wxSOLID
);
713 wxColour
lightShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT
));
714 m_lightShadowPen
= new wxPen(lightShadowColour
, 1, wxSOLID
);
716 wxColour
hilightColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DHILIGHT
));
717 m_hilightPen
= new wxPen(hilightColour
, 1, wxSOLID
);
719 m_facePen
= new wxPen("LIGHT GREY", 1, wxSOLID
);
720 m_faceBrush
= new wxBrush("LIGHT GREY", wxSOLID
);
721 m_mediumShadowPen
= new wxPen("GREY", 1, wxSOLID
);
722 m_darkShadowPen
= new wxPen("BLACK", 1, wxSOLID
);
723 m_lightShadowPen
= new wxPen("LIGHT GREY", 1, wxSOLID
);
724 m_hilightPen
= new wxPen("WHITE", 1, wxSOLID
);
725 #endif // Win32/!Win32