]>
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 m_dragMode
= wxSPLIT_DRAG_DRAGGING
;
157 DrawSashTracker(x
, y
);
163 else if (event
.LeftUp() && m_dragMode
== wxSPLIT_DRAG_DRAGGING
)
165 // We can stop dragging now and see what we've got.
166 m_dragMode
= wxSPLIT_DRAG_NONE
;
170 DrawSashTracker(m_oldX
, m_oldY
);
173 GetClientSize(&w
, &h
);
174 if ( m_splitMode
== wxSPLIT_VERTICAL
)
176 if ( !OnSashPositionChange(x
) )
181 // We remove the first window from the view
182 wxWindow
*removedWindow
= m_windowOne
;
183 m_windowOne
= m_windowTwo
;
184 m_windowTwo
= (wxWindow
*) NULL
;
186 OnUnsplit(removedWindow
);
189 else if ( x
>= (w
- 4) )
191 // We remove the second window from the view
192 wxWindow
*removedWindow
= m_windowTwo
;
193 m_windowTwo
= (wxWindow
*) NULL
;
194 OnUnsplit(removedWindow
);
202 else // m_splitMode == wxSPLIT_VERTICAL
204 if ( !OnSashPositionChange(y
) )
209 // We remove the first window from the view
210 wxWindow
*removedWindow
= m_windowOne
;
211 m_windowOne
= m_windowTwo
;
212 m_windowTwo
= (wxWindow
*) NULL
;
214 OnUnsplit(removedWindow
);
217 else if ( y
>= (h
- 4) )
219 // We remove the second window from the view
220 wxWindow
*removedWindow
= m_windowTwo
;
221 m_windowTwo
= (wxWindow
*) NULL
;
222 OnUnsplit(removedWindow
);
229 } // m_splitMode == wxSPLIT_VERTICAL
231 } // left up && dragging
232 else if (event
.Moving() && !event
.Dragging())
234 // Just change the cursor if required
235 if ( SashHitTest(x
, y
) )
237 if ( m_splitMode
== wxSPLIT_VERTICAL
)
239 SetCursor(*m_sashCursorWE
);
243 SetCursor(*m_sashCursorNS
);
248 SetCursor(*wxSTANDARD_CURSOR
);
251 else if (event
.Dragging() && (m_dragMode
== wxSPLIT_DRAG_DRAGGING
))
254 DrawSashTracker(m_oldX
, m_oldY
);
257 DrawSashTracker(x
, y
);
262 else if ( event
.LeftDClick() )
264 OnDoubleClickSash(x
, y
);
268 void wxSplitterWindow::OnSize(wxSizeEvent
& WXUNUSED(event
))
271 GetClientSize( &cw
, &ch
);
274 if ( m_splitMode
== wxSPLIT_VERTICAL
)
276 if ( m_sashPosition
>= (cw
- 5) )
277 m_sashPosition
= wxMax(10, cw
- 40);
279 if ( m_splitMode
== wxSPLIT_HORIZONTAL
)
281 if ( m_sashPosition
>= (ch
- 5) )
282 m_sashPosition
= wxMax(10, ch
- 40);
288 bool wxSplitterWindow::SashHitTest(int x
, int y
, int tolerance
)
290 if ( m_windowTwo
== NULL
|| m_sashPosition
== 0)
291 return FALSE
; // No sash
293 if ( m_splitMode
== wxSPLIT_VERTICAL
)
295 if ( (x
>= m_sashPosition
- tolerance
) && (x
<= m_sashPosition
+ m_sashSize
+ tolerance
) )
302 if ( (y
>= (m_sashPosition
- tolerance
)) && (y
<= (m_sashPosition
+ m_sashSize
+ tolerance
)) )
311 // Draw 3D effect borders
312 void wxSplitterWindow::DrawBorders(wxDC
& dc
)
315 GetClientSize(&w
, &h
);
317 if ( GetWindowStyleFlag() & wxSP_3D
)
319 dc
.SetPen(*m_mediumShadowPen
);
320 dc
.DrawLine(0, 0, w
-1, 0);
321 dc
.DrawLine(0, 0, 0, h
- 1);
323 dc
.SetPen(*m_darkShadowPen
);
324 dc
.DrawLine(1, 1, w
-2, 1);
325 dc
.DrawLine(1, 1, 1, h
-2);
327 dc
.SetPen(*m_hilightPen
);
328 dc
.DrawLine(0, h
-1, w
-1, h
-1);
329 dc
.DrawLine(w
-1, 0, w
-1, h
); // Surely the maximum y pos. should be h - 1.
330 /// Anyway, h is required for MSW.
332 dc
.SetPen(*m_lightShadowPen
);
333 dc
.DrawLine(w
-2, 1, w
-2, h
-2); // Right hand side
334 dc
.DrawLine(1, h
-2, w
-1, h
-2); // Bottom
336 else if ( GetWindowStyleFlag() & wxSP_BORDER
)
338 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
339 dc
.SetPen(*wxBLACK_PEN
);
340 dc
.DrawRectangle(0, 0, w
-1, h
-1);
343 dc
.SetPen(wxNullPen
);
344 dc
.SetBrush(wxNullBrush
);
348 void wxSplitterWindow::DrawSash(wxDC
& dc
)
350 if ( m_sashPosition
== 0 || !m_windowTwo
)
354 GetClientSize(&w
, &h
);
356 if ( GetWindowStyleFlag() & wxSP_3D
)
358 if ( m_splitMode
== wxSPLIT_VERTICAL
)
360 dc
.SetPen(*m_facePen
);
361 dc
.SetBrush(*m_faceBrush
);
362 dc
.DrawRectangle(m_sashPosition
+ 2, 0, m_sashSize
- 4, h
);
364 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
366 dc
.SetPen(*m_lightShadowPen
);
367 dc
.DrawLine(m_sashPosition
, 1, m_sashPosition
, h
-2);
369 dc
.SetPen(*m_hilightPen
);
370 dc
.DrawLine(m_sashPosition
+1, 0, m_sashPosition
+1, h
);
372 dc
.SetPen(*m_mediumShadowPen
);
373 dc
.DrawLine(m_sashPosition
+m_sashSize
-2, 1, m_sashPosition
+m_sashSize
-2, h
-1);
375 dc
.SetPen(*m_darkShadowPen
);
376 dc
.DrawLine(m_sashPosition
+m_sashSize
-1, 2, m_sashPosition
+m_sashSize
-1, h
-2);
380 dc
.SetPen(*m_facePen
);
381 dc
.SetBrush(*m_faceBrush
);
382 dc
.DrawRectangle(0, m_sashPosition
+ 2, w
, m_sashSize
- 4);
384 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
386 dc
.SetPen(*m_lightShadowPen
);
387 dc
.DrawLine(1, m_sashPosition
, w
-2, m_sashPosition
);
389 dc
.SetPen(*m_hilightPen
);
390 dc
.DrawLine(0, m_sashPosition
+1, w
, m_sashPosition
+1);
392 dc
.SetPen(*m_mediumShadowPen
);
393 dc
.DrawLine(1, m_sashPosition
+m_sashSize
-2, w
-1, m_sashPosition
+m_sashSize
-2);
395 dc
.SetPen(*m_darkShadowPen
);
396 dc
.DrawLine(2, m_sashPosition
+m_sashSize
-1, w
-2, m_sashPosition
+m_sashSize
-1);
401 if ( m_splitMode
== wxSPLIT_VERTICAL
)
403 dc
.SetPen(*wxBLACK_PEN
);
404 dc
.SetBrush(*wxBLACK_BRUSH
);
406 if ( (GetWindowStyleFlag() & wxSP_BORDER
) != wxSP_BORDER
)
407 h1
+= 1; // Not sure why this is necessary...
408 dc
.DrawRectangle(m_sashPosition
, 0, m_sashSize
, h1
);
412 dc
.SetPen(*wxBLACK_PEN
);
413 dc
.SetBrush(*wxBLACK_BRUSH
);
415 if ( (GetWindowStyleFlag() & wxSP_BORDER
) != wxSP_BORDER
)
418 dc
.DrawRectangle(0, m_sashPosition
, w1
, m_sashSize
);
423 dc
.SetPen(wxNullPen
);
424 dc
.SetBrush(wxNullBrush
);
427 // Draw the sash tracker (for whilst moving the sash)
428 void wxSplitterWindow::DrawSashTracker(int x
, int y
)
431 GetClientSize(&w
, &h
);
437 if ( m_splitMode
== wxSPLIT_VERTICAL
)
468 ClientToScreen(&x1
, &y1
);
469 ClientToScreen(&x2
, &y2
);
471 screenDC
.SetLogicalFunction(wxXOR
);
472 screenDC
.SetPen(*m_sashTrackerPen
);
473 screenDC
.SetBrush(*wxTRANSPARENT_BRUSH
);
475 screenDC
.DrawLine(x1
, y1
, x2
, y2
);
477 screenDC
.SetLogicalFunction(wxCOPY
);
479 screenDC
.SetPen(wxNullPen
);
480 screenDC
.SetBrush(wxNullBrush
);
483 // Position and size subwindows.
484 // Note that the border size applies to each subwindow, not
485 // including the edges next to the sash.
486 void wxSplitterWindow::SizeWindows()
489 GetClientSize(&w
, &h
);
491 if ( m_windowOne
&& !m_windowTwo
)
493 m_windowOne
->SetSize(m_borderSize
, m_borderSize
, w
- 2*m_borderSize
, h
- 2*m_borderSize
);
495 else if ( m_windowOne
&& m_windowTwo
)
497 if (m_splitMode
== wxSPLIT_VERTICAL
)
499 int x1
= m_borderSize
;
500 int y1
= m_borderSize
;
501 int w1
= m_sashPosition
- m_borderSize
;
502 int h1
= h
- 2*m_borderSize
;
504 int x2
= m_sashPosition
+ m_sashSize
;
505 int y2
= m_borderSize
;
506 int w2
= w
- 2*m_borderSize
- m_sashSize
- w1
;
507 int h2
= h
- 2*m_borderSize
;
509 m_windowOne
->SetSize(x1
, y1
, w1
, h1
);
510 m_windowTwo
->SetSize(x2
, y2
, w2
, h2
);
514 m_windowOne
->SetSize(m_borderSize
, m_borderSize
,
515 w
- 2*m_borderSize
, m_sashPosition
- m_borderSize
);
516 m_windowTwo
->SetSize(m_borderSize
, m_sashPosition
+ m_sashSize
,
517 w
- 2*m_borderSize
, h
- 2*m_borderSize
- m_sashSize
- (m_sashPosition
- m_borderSize
));
525 // Set pane for unsplit window
526 void wxSplitterWindow::Initialize(wxWindow
*window
)
528 m_windowOne
= window
;
529 m_windowTwo
= (wxWindow
*) NULL
;
533 // Associates the given window with window 2, drawing the appropriate sash
534 // and changing the split mode.
535 // Does nothing and returns FALSE if the window is already split.
536 bool wxSplitterWindow::SplitVertically(wxWindow
*window1
, wxWindow
*window2
, int sashPosition
)
542 GetClientSize(&w
, &h
);
544 m_splitMode
= wxSPLIT_VERTICAL
;
545 m_windowOne
= window1
;
546 m_windowTwo
= window2
;
547 if ( sashPosition
> 0 )
548 m_sashPosition
= sashPosition
;
549 else if ( sashPosition
< 0 )
550 m_sashPosition
= w
- sashPosition
;
552 m_sashPosition
= w
/2;
559 bool wxSplitterWindow::SplitHorizontally(wxWindow
*window1
, wxWindow
*window2
, int sashPosition
)
565 GetClientSize(&w
, &h
);
567 m_splitMode
= wxSPLIT_HORIZONTAL
;
568 m_windowOne
= window1
;
569 m_windowTwo
= window2
;
570 if ( sashPosition
> 0 )
571 m_sashPosition
= sashPosition
;
572 else if ( sashPosition
< 0 )
573 m_sashPosition
= h
- sashPosition
;
575 m_sashPosition
= h
/2;
583 // Remove the specified (or second) window from the view
584 // Doesn't actually delete the window.
585 bool wxSplitterWindow::Unsplit(wxWindow
*toRemove
)
590 if ( toRemove
== NULL
|| toRemove
== m_windowTwo
)
592 wxWindow
*win
= m_windowTwo
;
593 m_windowTwo
= (wxWindow
*) NULL
;
598 else if ( toRemove
== m_windowOne
)
600 wxWindow
*win
= m_windowOne
;
601 m_windowOne
= m_windowTwo
;
602 m_windowTwo
= (wxWindow
*) NULL
;
609 wxFAIL_MSG("splitter: attempt to remove a non-existent window");
617 void wxSplitterWindow::SetSashPosition(int position
, bool redraw
)
619 m_sashPosition
= position
;
627 bool wxSplitterWindow::OnSashPositionChange(int newSashPosition
)
629 // is the left/upper pane too small?
630 if ( newSashPosition
< m_minimumPaneSize
)
633 // is the right/lower pane too small?
635 GetClientSize(&w
, &h
);
637 if ( m_splitMode
== wxSPLIT_VERTICAL
)
639 if ( w
- newSashPosition
< m_minimumPaneSize
)
642 else // m_splitMode = wxSPLIT_HORIZONTAL
644 if ( h
- newSashPosition
< m_minimumPaneSize
)
648 // it's ok to move sash
652 // Called when the sash is double-clicked.
653 // The default behaviour is to remove the sash if the
654 // minimum pane size is zero.
655 void wxSplitterWindow::OnDoubleClickSash(int WXUNUSED(x
), int WXUNUSED(y
) )
657 if ( GetMinimumPaneSize() == 0 )
663 // Initialize colours
664 void wxSplitterWindow::InitColours()
666 wxDELETE( m_facePen
);
667 wxDELETE( m_faceBrush
);
668 wxDELETE( m_mediumShadowPen
);
669 wxDELETE( m_darkShadowPen
);
670 wxDELETE( m_lightShadowPen
);
671 wxDELETE( m_hilightPen
);
674 #if defined(__WIN95__)
675 wxColour
faceColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
676 m_facePen
= new wxPen(faceColour
, 1, wxSOLID
);
677 m_faceBrush
= new wxBrush(faceColour
, wxSOLID
);
679 wxColour
mediumShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DSHADOW
));
680 m_mediumShadowPen
= new wxPen(mediumShadowColour
, 1, wxSOLID
);
682 wxColour
darkShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DDKSHADOW
));
683 m_darkShadowPen
= new wxPen(darkShadowColour
, 1, wxSOLID
);
685 wxColour
lightShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT
));
686 m_lightShadowPen
= new wxPen(lightShadowColour
, 1, wxSOLID
);
688 wxColour
hilightColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DHILIGHT
));
689 m_hilightPen
= new wxPen(hilightColour
, 1, wxSOLID
);
691 m_facePen
= new wxPen("LIGHT GREY", 1, wxSOLID
);
692 m_faceBrush
= new wxBrush("LIGHT GREY", wxSOLID
);
693 m_mediumShadowPen
= new wxPen("GREY", 1, wxSOLID
);
694 m_darkShadowPen
= new wxPen("BLACK", 1, wxSOLID
);
695 m_lightShadowPen
= new wxPen("LIGHT GREY", 1, wxSOLID
);
696 m_hilightPen
= new wxPen("WHITE", 1, wxSOLID
);
697 #endif // Win32/!Win32