]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/splitter.cpp
44243d300b4a9e46f17ab5aa4ac4c6586bed9fc7
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
);
149 SetCursor(wxCursor());
151 if (event
.LeftDown())
153 if ( SashHitTest(x
, y
) )
157 m_dragMode
= wxSPLIT_DRAG_DRAGGING
;
159 DrawSashTracker(x
, y
);
165 else if (event
.LeftUp() && m_dragMode
== wxSPLIT_DRAG_DRAGGING
)
167 // We can stop dragging now and see what we've got.
168 m_dragMode
= wxSPLIT_DRAG_NONE
;
172 DrawSashTracker(m_oldX
, m_oldY
);
175 GetClientSize(&w
, &h
);
176 if ( m_splitMode
== wxSPLIT_VERTICAL
)
178 if ( !OnSashPositionChange(x
) )
183 // We remove the first window from the view
184 wxWindow
*removedWindow
= m_windowOne
;
185 m_windowOne
= m_windowTwo
;
186 m_windowTwo
= (wxWindow
*) NULL
;
188 OnUnsplit(removedWindow
);
191 else if ( x
>= (w
- 4) )
193 // We remove the second window from the view
194 wxWindow
*removedWindow
= m_windowTwo
;
195 m_windowTwo
= (wxWindow
*) NULL
;
196 OnUnsplit(removedWindow
);
204 else // m_splitMode == wxSPLIT_VERTICAL
206 if ( !OnSashPositionChange(y
) )
211 // We remove the first window from the view
212 wxWindow
*removedWindow
= m_windowOne
;
213 m_windowOne
= m_windowTwo
;
214 m_windowTwo
= (wxWindow
*) NULL
;
216 OnUnsplit(removedWindow
);
219 else if ( y
>= (h
- 4) )
221 // We remove the second window from the view
222 wxWindow
*removedWindow
= m_windowTwo
;
223 m_windowTwo
= (wxWindow
*) NULL
;
224 OnUnsplit(removedWindow
);
231 } // m_splitMode == wxSPLIT_VERTICAL
233 } // left up && dragging
234 else if (event
.Moving() && !event
.Dragging())
236 // Just change the cursor if required
237 if ( SashHitTest(x
, y
) )
239 if ( m_splitMode
== wxSPLIT_VERTICAL
)
241 SetCursor(*m_sashCursorWE
);
245 SetCursor(*m_sashCursorNS
);
249 else if (event
.Dragging() && (m_dragMode
== wxSPLIT_DRAG_DRAGGING
))
252 DrawSashTracker(m_oldX
, m_oldY
);
255 DrawSashTracker(x
, y
);
260 else if ( event
.LeftDClick() )
262 OnDoubleClickSash(x
, y
);
266 void wxSplitterWindow::OnSize(wxSizeEvent
& WXUNUSED(event
))
269 GetClientSize( &cw
, &ch
);
272 if ( m_splitMode
== wxSPLIT_VERTICAL
)
274 if ( m_sashPosition
>= (cw
- 5) )
275 m_sashPosition
= wxMax(10, cw
- 40);
277 if ( m_splitMode
== wxSPLIT_HORIZONTAL
)
279 if ( m_sashPosition
>= (ch
- 5) )
280 m_sashPosition
= wxMax(10, ch
- 40);
286 bool wxSplitterWindow::SashHitTest(int x
, int y
, int tolerance
)
288 if ( m_windowTwo
== NULL
|| m_sashPosition
== 0)
289 return FALSE
; // No sash
291 if ( m_splitMode
== wxSPLIT_VERTICAL
)
293 if ( (x
>= m_sashPosition
- tolerance
) && (x
<= m_sashPosition
+ m_sashSize
+ tolerance
) )
300 if ( (y
>= (m_sashPosition
- tolerance
)) && (y
<= (m_sashPosition
+ m_sashSize
+ tolerance
)) )
309 // Draw 3D effect borders
310 void wxSplitterWindow::DrawBorders(wxDC
& dc
)
313 GetClientSize(&w
, &h
);
315 if ( GetWindowStyleFlag() & wxSP_3D
)
317 dc
.SetPen(*m_mediumShadowPen
);
318 dc
.DrawLine(0, 0, w
-1, 0);
319 dc
.DrawLine(0, 0, 0, h
- 1);
321 dc
.SetPen(*m_darkShadowPen
);
322 dc
.DrawLine(1, 1, w
-2, 1);
323 dc
.DrawLine(1, 1, 1, h
-2);
325 dc
.SetPen(*m_hilightPen
);
326 dc
.DrawLine(0, h
-1, w
-1, h
-1);
327 dc
.DrawLine(w
-1, 0, w
-1, h
); // Surely the maximum y pos. should be h - 1.
328 /// Anyway, h is required for MSW.
330 dc
.SetPen(*m_lightShadowPen
);
331 dc
.DrawLine(w
-2, 1, w
-2, h
-2); // Right hand side
332 dc
.DrawLine(1, h
-2, w
-1, h
-2); // Bottom
334 else if ( GetWindowStyleFlag() & wxSP_BORDER
)
336 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
337 dc
.SetPen(*wxBLACK_PEN
);
338 dc
.DrawRectangle(0, 0, w
-1, h
-1);
341 dc
.SetPen(wxNullPen
);
342 dc
.SetBrush(wxNullBrush
);
346 void wxSplitterWindow::DrawSash(wxDC
& dc
)
348 if ( m_sashPosition
== 0 || !m_windowTwo
)
352 GetClientSize(&w
, &h
);
354 if ( GetWindowStyleFlag() & wxSP_3D
)
356 if ( m_splitMode
== wxSPLIT_VERTICAL
)
358 dc
.SetPen(*m_facePen
);
359 dc
.SetBrush(*m_faceBrush
);
360 dc
.DrawRectangle(m_sashPosition
+ 2, 0, m_sashSize
- 4, h
);
362 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
364 dc
.SetPen(*m_lightShadowPen
);
365 dc
.DrawLine(m_sashPosition
, 1, m_sashPosition
, h
-2);
367 dc
.SetPen(*m_hilightPen
);
368 dc
.DrawLine(m_sashPosition
+1, 0, m_sashPosition
+1, h
);
370 dc
.SetPen(*m_mediumShadowPen
);
371 dc
.DrawLine(m_sashPosition
+m_sashSize
-2, 1, m_sashPosition
+m_sashSize
-2, h
-1);
373 dc
.SetPen(*m_darkShadowPen
);
374 dc
.DrawLine(m_sashPosition
+m_sashSize
-1, 2, m_sashPosition
+m_sashSize
-1, h
-2);
378 dc
.SetPen(*m_facePen
);
379 dc
.SetBrush(*m_faceBrush
);
380 dc
.DrawRectangle(0, m_sashPosition
+ 2, w
, m_sashSize
- 4);
382 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
384 dc
.SetPen(*m_lightShadowPen
);
385 dc
.DrawLine(1, m_sashPosition
, w
-2, m_sashPosition
);
387 dc
.SetPen(*m_hilightPen
);
388 dc
.DrawLine(0, m_sashPosition
+1, w
, m_sashPosition
+1);
390 dc
.SetPen(*m_mediumShadowPen
);
391 dc
.DrawLine(1, m_sashPosition
+m_sashSize
-2, w
-1, m_sashPosition
+m_sashSize
-2);
393 dc
.SetPen(*m_darkShadowPen
);
394 dc
.DrawLine(2, m_sashPosition
+m_sashSize
-1, w
-2, m_sashPosition
+m_sashSize
-1);
399 if ( m_splitMode
== wxSPLIT_VERTICAL
)
401 dc
.SetPen(*wxBLACK_PEN
);
402 dc
.SetBrush(*wxBLACK_BRUSH
);
404 if ( (GetWindowStyleFlag() & wxSP_BORDER
) != wxSP_BORDER
)
405 h1
+= 1; // Not sure why this is necessary...
406 dc
.DrawRectangle(m_sashPosition
, 0, m_sashSize
, h1
);
410 dc
.SetPen(*wxBLACK_PEN
);
411 dc
.SetBrush(*wxBLACK_BRUSH
);
413 if ( (GetWindowStyleFlag() & wxSP_BORDER
) != wxSP_BORDER
)
416 dc
.DrawRectangle(0, m_sashPosition
, w1
, m_sashSize
);
421 dc
.SetPen(wxNullPen
);
422 dc
.SetBrush(wxNullBrush
);
425 // Draw the sash tracker (for whilst moving the sash)
426 void wxSplitterWindow::DrawSashTracker(int x
, int y
)
429 GetClientSize(&w
, &h
);
435 if ( m_splitMode
== wxSPLIT_VERTICAL
)
466 ClientToScreen(&x1
, &y1
);
467 ClientToScreen(&x2
, &y2
);
469 screenDC
.SetLogicalFunction(wxXOR
);
470 screenDC
.SetPen(*m_sashTrackerPen
);
471 screenDC
.SetBrush(*wxTRANSPARENT_BRUSH
);
473 screenDC
.DrawLine(x1
, y1
, x2
, y2
);
475 screenDC
.SetLogicalFunction(wxCOPY
);
477 screenDC
.SetPen(wxNullPen
);
478 screenDC
.SetBrush(wxNullBrush
);
481 // Position and size subwindows.
482 // Note that the border size applies to each subwindow, not
483 // including the edges next to the sash.
484 void wxSplitterWindow::SizeWindows()
487 GetClientSize(&w
, &h
);
489 if ( m_windowOne
&& !m_windowTwo
)
491 m_windowOne
->SetSize(m_borderSize
, m_borderSize
, w
- 2*m_borderSize
, h
- 2*m_borderSize
);
493 else if ( m_windowOne
&& m_windowTwo
)
495 if (m_splitMode
== wxSPLIT_VERTICAL
)
497 int x1
= m_borderSize
;
498 int y1
= m_borderSize
;
499 int w1
= m_sashPosition
- m_borderSize
;
500 int h1
= h
- 2*m_borderSize
;
502 int x2
= m_sashPosition
+ m_sashSize
;
503 int y2
= m_borderSize
;
504 int w2
= w
- 2*m_borderSize
- m_sashSize
- w1
;
505 int h2
= h
- 2*m_borderSize
;
507 m_windowOne
->SetSize(x1
, y1
, w1
, h1
);
508 m_windowTwo
->SetSize(x2
, y2
, w2
, h2
);
512 m_windowOne
->SetSize(m_borderSize
, m_borderSize
,
513 w
- 2*m_borderSize
, m_sashPosition
- m_borderSize
);
514 m_windowTwo
->SetSize(m_borderSize
, m_sashPosition
+ m_sashSize
,
515 w
- 2*m_borderSize
, h
- 2*m_borderSize
- m_sashSize
- (m_sashPosition
- m_borderSize
));
523 // Set pane for unsplit window
524 void wxSplitterWindow::Initialize(wxWindow
*window
)
526 m_windowOne
= window
;
527 m_windowTwo
= (wxWindow
*) NULL
;
531 // Associates the given window with window 2, drawing the appropriate sash
532 // and changing the split mode.
533 // Does nothing and returns FALSE if the window is already split.
534 bool wxSplitterWindow::SplitVertically(wxWindow
*window1
, wxWindow
*window2
, int sashPosition
)
540 GetClientSize(&w
, &h
);
542 m_splitMode
= wxSPLIT_VERTICAL
;
543 m_windowOne
= window1
;
544 m_windowTwo
= window2
;
545 if ( sashPosition
> 0 )
546 m_sashPosition
= sashPosition
;
547 else if ( sashPosition
< 0 )
548 m_sashPosition
= w
- sashPosition
;
550 m_sashPosition
= w
/2;
557 bool wxSplitterWindow::SplitHorizontally(wxWindow
*window1
, wxWindow
*window2
, int sashPosition
)
563 GetClientSize(&w
, &h
);
565 m_splitMode
= wxSPLIT_HORIZONTAL
;
566 m_windowOne
= window1
;
567 m_windowTwo
= window2
;
568 if ( sashPosition
> 0 )
569 m_sashPosition
= sashPosition
;
570 else if ( sashPosition
< 0 )
571 m_sashPosition
= h
- sashPosition
;
573 m_sashPosition
= h
/2;
581 // Remove the specified (or second) window from the view
582 // Doesn't actually delete the window.
583 bool wxSplitterWindow::Unsplit(wxWindow
*toRemove
)
588 wxWindow
*win
= NULL
;
589 if ( toRemove
== NULL
|| toRemove
== m_windowTwo
)
592 m_windowTwo
= (wxWindow
*) NULL
;
594 else if ( toRemove
== m_windowOne
)
597 m_windowOne
= m_windowTwo
;
598 m_windowTwo
= (wxWindow
*) NULL
;
602 wxFAIL_MSG("splitter: attempt to remove a non-existent window");
614 // Replace a window with another one
615 bool wxSplitterWindow::ReplaceWindow(wxWindow
*winOld
, wxWindow
*winNew
)
617 wxCHECK_MSG( winOld
, FALSE
, "use one of Split() functions instead" );
618 wxCHECK_MSG( winNew
, FALSE
, "use Unsplit() functions instead" );
620 if ( winOld
== m_windowTwo
)
622 m_windowTwo
= winNew
;
624 else if ( winOld
== m_windowOne
)
626 m_windowOne
= winNew
;
630 wxFAIL_MSG("splitter: attempt to replace a non-existent window");
640 void wxSplitterWindow::SetSashPosition(int position
, bool redraw
)
642 m_sashPosition
= position
;
650 bool wxSplitterWindow::OnSashPositionChange(int newSashPosition
)
652 // is the left/upper pane too small?
653 if ( newSashPosition
< m_minimumPaneSize
)
656 // is the right/lower pane too small?
658 GetClientSize(&w
, &h
);
660 if ( m_splitMode
== wxSPLIT_VERTICAL
)
662 if ( w
- newSashPosition
< m_minimumPaneSize
)
665 else // m_splitMode = wxSPLIT_HORIZONTAL
667 if ( h
- newSashPosition
< m_minimumPaneSize
)
671 // it's ok to move sash
675 // Called when the sash is double-clicked.
676 // The default behaviour is to remove the sash if the
677 // minimum pane size is zero.
678 void wxSplitterWindow::OnDoubleClickSash(int WXUNUSED(x
), int WXUNUSED(y
) )
680 if ( GetMinimumPaneSize() == 0 )
686 // Initialize colours
687 void wxSplitterWindow::InitColours()
689 wxDELETE( m_facePen
);
690 wxDELETE( m_faceBrush
);
691 wxDELETE( m_mediumShadowPen
);
692 wxDELETE( m_darkShadowPen
);
693 wxDELETE( m_lightShadowPen
);
694 wxDELETE( m_hilightPen
);
697 #if defined(__WIN95__)
698 wxColour
faceColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
699 m_facePen
= new wxPen(faceColour
, 1, wxSOLID
);
700 m_faceBrush
= new wxBrush(faceColour
, wxSOLID
);
702 wxColour
mediumShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DSHADOW
));
703 m_mediumShadowPen
= new wxPen(mediumShadowColour
, 1, wxSOLID
);
705 wxColour
darkShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DDKSHADOW
));
706 m_darkShadowPen
= new wxPen(darkShadowColour
, 1, wxSOLID
);
708 wxColour
lightShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT
));
709 m_lightShadowPen
= new wxPen(lightShadowColour
, 1, wxSOLID
);
711 wxColour
hilightColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DHILIGHT
));
712 m_hilightPen
= new wxPen(hilightColour
, 1, wxSOLID
);
714 m_facePen
= new wxPen("LIGHT GREY", 1, wxSOLID
);
715 m_faceBrush
= new wxBrush("LIGHT GREY", wxSOLID
);
716 m_mediumShadowPen
= new wxPen("GREY", 1, wxSOLID
);
717 m_darkShadowPen
= new wxPen("BLACK", 1, wxSOLID
);
718 m_lightShadowPen
= new wxPen("LIGHT GREY", 1, wxSOLID
);
719 m_hilightPen
= new wxPen("WHITE", 1, wxSOLID
);
720 #endif // Win32/!Win32