]>
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
);
152 SetCursor(wxCursor());
155 if (event
.LeftDown())
157 if ( SashHitTest(x
, y
) )
161 m_dragMode
= wxSPLIT_DRAG_DRAGGING
;
163 DrawSashTracker(x
, y
);
169 else if (event
.LeftUp() && m_dragMode
== wxSPLIT_DRAG_DRAGGING
)
171 // We can stop dragging now and see what we've got.
172 m_dragMode
= wxSPLIT_DRAG_NONE
;
176 DrawSashTracker(m_oldX
, m_oldY
);
179 GetClientSize(&w
, &h
);
180 if ( m_splitMode
== wxSPLIT_VERTICAL
)
182 if ( !OnSashPositionChange(x
) )
187 // We remove the first window from the view
188 wxWindow
*removedWindow
= m_windowOne
;
189 m_windowOne
= m_windowTwo
;
190 m_windowTwo
= (wxWindow
*) NULL
;
192 OnUnsplit(removedWindow
);
195 else if ( x
>= (w
- 4) )
197 // We remove the second window from the view
198 wxWindow
*removedWindow
= m_windowTwo
;
199 m_windowTwo
= (wxWindow
*) NULL
;
200 OnUnsplit(removedWindow
);
208 else // m_splitMode == wxSPLIT_VERTICAL
210 if ( !OnSashPositionChange(y
) )
215 // We remove the first window from the view
216 wxWindow
*removedWindow
= m_windowOne
;
217 m_windowOne
= m_windowTwo
;
218 m_windowTwo
= (wxWindow
*) NULL
;
220 OnUnsplit(removedWindow
);
223 else if ( y
>= (h
- 4) )
225 // We remove the second window from the view
226 wxWindow
*removedWindow
= m_windowTwo
;
227 m_windowTwo
= (wxWindow
*) NULL
;
228 OnUnsplit(removedWindow
);
235 } // m_splitMode == wxSPLIT_VERTICAL
237 } // left up && dragging
238 else if (event
.Moving() && !event
.Dragging())
240 // Just change the cursor if required
241 if ( SashHitTest(x
, y
) )
243 if ( m_splitMode
== wxSPLIT_VERTICAL
)
245 SetCursor(*m_sashCursorWE
);
249 SetCursor(*m_sashCursorNS
);
253 else if (event
.Dragging() && (m_dragMode
== wxSPLIT_DRAG_DRAGGING
))
256 DrawSashTracker(m_oldX
, m_oldY
);
259 DrawSashTracker(x
, y
);
264 else if ( event
.LeftDClick() )
266 OnDoubleClickSash(x
, y
);
270 void wxSplitterWindow::OnSize(wxSizeEvent
& WXUNUSED(event
))
273 GetClientSize( &cw
, &ch
);
276 if ( m_splitMode
== wxSPLIT_VERTICAL
)
278 if ( m_sashPosition
>= (cw
- 5) )
279 m_sashPosition
= wxMax(10, cw
- 40);
281 if ( m_splitMode
== wxSPLIT_HORIZONTAL
)
283 if ( m_sashPosition
>= (ch
- 5) )
284 m_sashPosition
= wxMax(10, ch
- 40);
290 bool wxSplitterWindow::SashHitTest(int x
, int y
, int tolerance
)
292 if ( m_windowTwo
== NULL
|| m_sashPosition
== 0)
293 return FALSE
; // No sash
295 if ( m_splitMode
== wxSPLIT_VERTICAL
)
297 if ( (x
>= m_sashPosition
- tolerance
) && (x
<= m_sashPosition
+ m_sashSize
+ tolerance
) )
304 if ( (y
>= (m_sashPosition
- tolerance
)) && (y
<= (m_sashPosition
+ m_sashSize
+ tolerance
)) )
313 // Draw 3D effect borders
314 void wxSplitterWindow::DrawBorders(wxDC
& dc
)
317 GetClientSize(&w
, &h
);
319 if ( GetWindowStyleFlag() & wxSP_3D
)
321 dc
.SetPen(*m_mediumShadowPen
);
322 dc
.DrawLine(0, 0, w
-1, 0);
323 dc
.DrawLine(0, 0, 0, h
- 1);
325 dc
.SetPen(*m_darkShadowPen
);
326 dc
.DrawLine(1, 1, w
-2, 1);
327 dc
.DrawLine(1, 1, 1, h
-2);
329 dc
.SetPen(*m_hilightPen
);
330 dc
.DrawLine(0, h
-1, w
-1, h
-1);
331 dc
.DrawLine(w
-1, 0, w
-1, h
); // Surely the maximum y pos. should be h - 1.
332 /// Anyway, h is required for MSW.
334 dc
.SetPen(*m_lightShadowPen
);
335 dc
.DrawLine(w
-2, 1, w
-2, h
-2); // Right hand side
336 dc
.DrawLine(1, h
-2, w
-1, h
-2); // Bottom
338 else if ( GetWindowStyleFlag() & wxSP_BORDER
)
340 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
341 dc
.SetPen(*wxBLACK_PEN
);
342 dc
.DrawRectangle(0, 0, w
-1, h
-1);
345 dc
.SetPen(wxNullPen
);
346 dc
.SetBrush(wxNullBrush
);
350 void wxSplitterWindow::DrawSash(wxDC
& dc
)
352 if ( m_sashPosition
== 0 || !m_windowTwo
)
356 GetClientSize(&w
, &h
);
358 if ( GetWindowStyleFlag() & wxSP_3D
)
360 if ( m_splitMode
== wxSPLIT_VERTICAL
)
362 dc
.SetPen(*m_facePen
);
363 dc
.SetBrush(*m_faceBrush
);
364 dc
.DrawRectangle(m_sashPosition
+ 2, 0, m_sashSize
- 4, h
);
366 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
368 dc
.SetPen(*m_lightShadowPen
);
369 dc
.DrawLine(m_sashPosition
, 1, m_sashPosition
, h
-2);
371 dc
.SetPen(*m_hilightPen
);
372 dc
.DrawLine(m_sashPosition
+1, 0, m_sashPosition
+1, h
);
374 dc
.SetPen(*m_mediumShadowPen
);
375 dc
.DrawLine(m_sashPosition
+m_sashSize
-2, 1, m_sashPosition
+m_sashSize
-2, h
-1);
377 dc
.SetPen(*m_darkShadowPen
);
378 dc
.DrawLine(m_sashPosition
+m_sashSize
-1, 2, m_sashPosition
+m_sashSize
-1, h
-2);
382 dc
.SetPen(*m_facePen
);
383 dc
.SetBrush(*m_faceBrush
);
384 dc
.DrawRectangle(0, m_sashPosition
+ 2, w
, m_sashSize
- 4);
386 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
388 dc
.SetPen(*m_lightShadowPen
);
389 dc
.DrawLine(1, m_sashPosition
, w
-2, m_sashPosition
);
391 dc
.SetPen(*m_hilightPen
);
392 dc
.DrawLine(0, m_sashPosition
+1, w
, m_sashPosition
+1);
394 dc
.SetPen(*m_mediumShadowPen
);
395 dc
.DrawLine(1, m_sashPosition
+m_sashSize
-2, w
-1, m_sashPosition
+m_sashSize
-2);
397 dc
.SetPen(*m_darkShadowPen
);
398 dc
.DrawLine(2, m_sashPosition
+m_sashSize
-1, w
-2, m_sashPosition
+m_sashSize
-1);
403 if ( m_splitMode
== wxSPLIT_VERTICAL
)
405 dc
.SetPen(*wxBLACK_PEN
);
406 dc
.SetBrush(*wxBLACK_BRUSH
);
408 if ( (GetWindowStyleFlag() & wxSP_BORDER
) != wxSP_BORDER
)
409 h1
+= 1; // Not sure why this is necessary...
410 dc
.DrawRectangle(m_sashPosition
, 0, m_sashSize
, h1
);
414 dc
.SetPen(*wxBLACK_PEN
);
415 dc
.SetBrush(*wxBLACK_BRUSH
);
417 if ( (GetWindowStyleFlag() & wxSP_BORDER
) != wxSP_BORDER
)
420 dc
.DrawRectangle(0, m_sashPosition
, w1
, m_sashSize
);
425 dc
.SetPen(wxNullPen
);
426 dc
.SetBrush(wxNullBrush
);
429 // Draw the sash tracker (for whilst moving the sash)
430 void wxSplitterWindow::DrawSashTracker(int x
, int y
)
433 GetClientSize(&w
, &h
);
439 if ( m_splitMode
== wxSPLIT_VERTICAL
)
470 ClientToScreen(&x1
, &y1
);
471 ClientToScreen(&x2
, &y2
);
473 screenDC
.SetLogicalFunction(wxXOR
);
474 screenDC
.SetPen(*m_sashTrackerPen
);
475 screenDC
.SetBrush(*wxTRANSPARENT_BRUSH
);
477 screenDC
.DrawLine(x1
, y1
, x2
, y2
);
479 screenDC
.SetLogicalFunction(wxCOPY
);
481 screenDC
.SetPen(wxNullPen
);
482 screenDC
.SetBrush(wxNullBrush
);
485 // Position and size subwindows.
486 // Note that the border size applies to each subwindow, not
487 // including the edges next to the sash.
488 void wxSplitterWindow::SizeWindows()
491 GetClientSize(&w
, &h
);
493 if ( m_windowOne
&& !m_windowTwo
)
495 m_windowOne
->SetSize(m_borderSize
, m_borderSize
, w
- 2*m_borderSize
, h
- 2*m_borderSize
);
497 else if ( m_windowOne
&& m_windowTwo
)
499 if (m_splitMode
== wxSPLIT_VERTICAL
)
501 int x1
= m_borderSize
;
502 int y1
= m_borderSize
;
503 int w1
= m_sashPosition
- m_borderSize
;
504 int h1
= h
- 2*m_borderSize
;
506 int x2
= m_sashPosition
+ m_sashSize
;
507 int y2
= m_borderSize
;
508 int w2
= w
- 2*m_borderSize
- m_sashSize
- w1
;
509 int h2
= h
- 2*m_borderSize
;
511 m_windowOne
->SetSize(x1
, y1
, w1
, h1
);
512 m_windowTwo
->SetSize(x2
, y2
, w2
, h2
);
516 m_windowOne
->SetSize(m_borderSize
, m_borderSize
,
517 w
- 2*m_borderSize
, m_sashPosition
- m_borderSize
);
518 m_windowTwo
->SetSize(m_borderSize
, m_sashPosition
+ m_sashSize
,
519 w
- 2*m_borderSize
, h
- 2*m_borderSize
- m_sashSize
- (m_sashPosition
- m_borderSize
));
527 // Set pane for unsplit window
528 void wxSplitterWindow::Initialize(wxWindow
*window
)
530 m_windowOne
= window
;
531 m_windowTwo
= (wxWindow
*) NULL
;
535 // Associates the given window with window 2, drawing the appropriate sash
536 // and changing the split mode.
537 // Does nothing and returns FALSE if the window is already split.
538 bool wxSplitterWindow::SplitVertically(wxWindow
*window1
, wxWindow
*window2
, int sashPosition
)
544 GetClientSize(&w
, &h
);
546 m_splitMode
= wxSPLIT_VERTICAL
;
547 m_windowOne
= window1
;
548 m_windowTwo
= window2
;
549 if ( sashPosition
> 0 )
550 m_sashPosition
= sashPosition
;
551 else if ( sashPosition
< 0 )
552 m_sashPosition
= w
- sashPosition
;
554 m_sashPosition
= w
/2;
561 bool wxSplitterWindow::SplitHorizontally(wxWindow
*window1
, wxWindow
*window2
, int sashPosition
)
567 GetClientSize(&w
, &h
);
569 m_splitMode
= wxSPLIT_HORIZONTAL
;
570 m_windowOne
= window1
;
571 m_windowTwo
= window2
;
572 if ( sashPosition
> 0 )
573 m_sashPosition
= sashPosition
;
574 else if ( sashPosition
< 0 )
575 m_sashPosition
= h
- sashPosition
;
577 m_sashPosition
= h
/2;
585 // Remove the specified (or second) window from the view
586 // Doesn't actually delete the window.
587 bool wxSplitterWindow::Unsplit(wxWindow
*toRemove
)
592 wxWindow
*win
= NULL
;
593 if ( toRemove
== NULL
|| toRemove
== m_windowTwo
)
596 m_windowTwo
= (wxWindow
*) NULL
;
598 else if ( toRemove
== m_windowOne
)
601 m_windowOne
= m_windowTwo
;
602 m_windowTwo
= (wxWindow
*) NULL
;
606 wxFAIL_MSG("splitter: attempt to remove a non-existent window");
618 // Replace a window with another one
619 bool wxSplitterWindow::ReplaceWindow(wxWindow
*winOld
, wxWindow
*winNew
)
621 wxCHECK_MSG( winOld
, FALSE
, "use one of Split() functions instead" );
622 wxCHECK_MSG( winNew
, FALSE
, "use Unsplit() functions instead" );
624 if ( winOld
== m_windowTwo
)
626 m_windowTwo
= winNew
;
628 else if ( winOld
== m_windowOne
)
630 m_windowOne
= winNew
;
634 wxFAIL_MSG("splitter: attempt to replace a non-existent window");
644 void wxSplitterWindow::SetSashPosition(int position
, bool redraw
)
646 m_sashPosition
= position
;
654 bool wxSplitterWindow::OnSashPositionChange(int newSashPosition
)
656 // is the left/upper pane too small?
657 if ( newSashPosition
< m_minimumPaneSize
)
660 // is the right/lower pane too small?
662 GetClientSize(&w
, &h
);
664 if ( m_splitMode
== wxSPLIT_VERTICAL
)
666 if ( w
- newSashPosition
< m_minimumPaneSize
)
669 else // m_splitMode = wxSPLIT_HORIZONTAL
671 if ( h
- newSashPosition
< m_minimumPaneSize
)
675 // it's ok to move sash
679 // Called when the sash is double-clicked.
680 // The default behaviour is to remove the sash if the
681 // minimum pane size is zero.
682 void wxSplitterWindow::OnDoubleClickSash(int WXUNUSED(x
), int WXUNUSED(y
) )
684 if ( GetMinimumPaneSize() == 0 )
690 // Initialize colours
691 void wxSplitterWindow::InitColours()
693 wxDELETE( m_facePen
);
694 wxDELETE( m_faceBrush
);
695 wxDELETE( m_mediumShadowPen
);
696 wxDELETE( m_darkShadowPen
);
697 wxDELETE( m_lightShadowPen
);
698 wxDELETE( m_hilightPen
);
701 #if defined(__WIN95__)
702 wxColour
faceColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
703 m_facePen
= new wxPen(faceColour
, 1, wxSOLID
);
704 m_faceBrush
= new wxBrush(faceColour
, wxSOLID
);
706 wxColour
mediumShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DSHADOW
));
707 m_mediumShadowPen
= new wxPen(mediumShadowColour
, 1, wxSOLID
);
709 wxColour
darkShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DDKSHADOW
));
710 m_darkShadowPen
= new wxPen(darkShadowColour
, 1, wxSOLID
);
712 wxColour
lightShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT
));
713 m_lightShadowPen
= new wxPen(lightShadowColour
, 1, wxSOLID
);
715 wxColour
hilightColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DHILIGHT
));
716 m_hilightPen
= new wxPen(hilightColour
, 1, wxSOLID
);
718 m_facePen
= new wxPen("LIGHT GREY", 1, wxSOLID
);
719 m_faceBrush
= new wxBrush("LIGHT GREY", wxSOLID
);
720 m_mediumShadowPen
= new wxPen("GREY", 1, wxSOLID
);
721 m_darkShadowPen
= new wxPen("BLACK", 1, wxSOLID
);
722 m_lightShadowPen
= new wxPen("LIGHT GREY", 1, wxSOLID
);
723 m_hilightPen
= new wxPen("WHITE", 1, wxSOLID
);
724 #endif // Win32/!Win32