1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxSashWindow implementation. A sash window has an optional
4 // sash on each edge, allowing it to be dragged. An event
5 // is generated when the sash is released.
6 // Author: Julian Smart
10 // Copyright: (c) Julian Smart
11 // Licence: wxWindows licence
12 /////////////////////////////////////////////////////////////////////////////
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
24 #include "wx/dialog.h"
26 #include "wx/settings.h"
33 #include "wx/dcscreen.h"
34 #include "wx/sashwin.h"
35 #include "wx/laywin.h"
37 DEFINE_EVENT_TYPE(wxEVT_SASH_DRAGGED
)
39 IMPLEMENT_DYNAMIC_CLASS(wxSashWindow
, wxWindow
)
40 IMPLEMENT_DYNAMIC_CLASS(wxSashEvent
, wxCommandEvent
)
42 BEGIN_EVENT_TABLE(wxSashWindow
, wxWindow
)
43 EVT_PAINT(wxSashWindow::OnPaint
)
44 EVT_SIZE(wxSashWindow::OnSize
)
45 EVT_MOUSE_EVENTS(wxSashWindow::OnMouseEvent
)
46 #if defined( __WXMSW__ ) || defined( __WXMAC__)
47 EVT_SET_CURSOR(wxSashWindow::OnSetCursor
)
52 bool wxSashWindow::Create(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
,
53 const wxSize
& size
, long style
, const wxString
& name
)
55 return wxWindow::Create(parent
, id
, pos
, size
, style
, name
);
58 wxSashWindow::~wxSashWindow()
60 delete m_sashCursorWE
;
61 delete m_sashCursorNS
;
64 void wxSashWindow::Init()
66 m_draggingEdge
= wxSASH_NONE
;
67 m_dragMode
= wxSASH_DRAG_NONE
;
73 m_extraBorderSize
= 0;
74 m_minimumPaneSizeX
= 0;
75 m_minimumPaneSizeY
= 0;
76 m_maximumPaneSizeX
= 10000;
77 m_maximumPaneSizeY
= 10000;
78 m_sashCursorWE
= new wxCursor(wxCURSOR_SIZEWE
);
79 m_sashCursorNS
= new wxCursor(wxCURSOR_SIZENS
);
80 m_mouseCaptured
= false;
81 m_currentCursor
= NULL
;
83 // Eventually, we'll respond to colour change messages
87 void wxSashWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
95 void wxSashWindow::OnMouseEvent(wxMouseEvent
& event
)
98 event
.GetPosition(&x
, &y
);
100 wxSashEdgePosition sashHit
= SashHitTest(x
, y
);
102 if (event
.LeftDown())
105 m_mouseCaptured
= true;
107 if ( sashHit
!= wxSASH_NONE
)
109 // Required for X to specify that
110 // that we wish to draw on top of all windows
111 // - and we optimise by specifying the area
112 // for creating the overlap window.
113 // Find the first frame or dialog and use this to specify
114 // the area to draw on.
115 wxWindow
* parent
= this;
117 while (parent
&& !parent
->IsKindOf(CLASSINFO(wxDialog
)) &&
118 !parent
->IsKindOf(CLASSINFO(wxFrame
)))
119 parent
= parent
->GetParent();
121 wxScreenDC::StartDrawingOnTop(parent
);
123 // We don't say we're dragging yet; we leave that
124 // decision for the Dragging() branch, to ensure
125 // the user has dragged a little bit.
126 m_dragMode
= wxSASH_DRAG_LEFT_DOWN
;
127 m_draggingEdge
= sashHit
;
131 if ( (sashHit
== wxSASH_LEFT
) || (sashHit
== wxSASH_RIGHT
) )
133 if (m_currentCursor
!= m_sashCursorWE
)
135 SetCursor(*m_sashCursorWE
);
137 m_currentCursor
= m_sashCursorWE
;
141 if (m_currentCursor
!= m_sashCursorNS
)
143 SetCursor(*m_sashCursorNS
);
145 m_currentCursor
= m_sashCursorNS
;
149 else if ( event
.LeftUp() && m_dragMode
== wxSASH_DRAG_LEFT_DOWN
)
151 // Wasn't a proper drag
154 m_mouseCaptured
= false;
156 wxScreenDC::EndDrawingOnTop();
157 m_dragMode
= wxSASH_DRAG_NONE
;
158 m_draggingEdge
= wxSASH_NONE
;
160 else if (event
.LeftUp() && m_dragMode
== wxSASH_DRAG_DRAGGING
)
162 // We can stop dragging now and see what we've got.
163 m_dragMode
= wxSASH_DRAG_NONE
;
166 m_mouseCaptured
= false;
169 DrawSashTracker(m_draggingEdge
, m_oldX
, m_oldY
);
171 // End drawing on top (frees the window used for drawing
173 wxScreenDC::EndDrawingOnTop();
178 GetPosition(&xp
, &yp
);
180 wxSashEdgePosition edge
= m_draggingEdge
;
181 m_draggingEdge
= wxSASH_NONE
;
184 wxSashDragStatus status
= wxSASH_STATUS_OK
;
186 // the new height and width of the window - if -1, it didn't change
187 int newHeight
= wxDefaultCoord
,
188 newWidth
= wxDefaultCoord
;
190 // NB: x and y may be negative and they're relative to the sash window
191 // upper left corner, while xp and yp are expressed in the parent
192 // window system of coordinates, so adjust them! After this
193 // adjustment, all coordinates are relative to the parent window.
202 // top sash shouldn't get below the bottom one
203 status
= wxSASH_STATUS_OUT_OF_RANGE
;
207 newHeight
= h
- (y
- yp
);
214 // bottom sash shouldn't get above the top one
215 status
= wxSASH_STATUS_OUT_OF_RANGE
;
226 // left sash shouldn't get beyond the right one
227 status
= wxSASH_STATUS_OUT_OF_RANGE
;
231 newWidth
= w
- (x
- xp
);
238 // and the right sash, finally, shouldn't be beyond the
240 status
= wxSASH_STATUS_OUT_OF_RANGE
;
249 // can this happen at all?
253 if ( newHeight
== wxDefaultCoord
)
260 // make sure it's in m_minimumPaneSizeY..m_maximumPaneSizeY range
261 newHeight
= wxMax(newHeight
, m_minimumPaneSizeY
);
262 newHeight
= wxMin(newHeight
, m_maximumPaneSizeY
);
265 if ( newWidth
== wxDefaultCoord
)
272 // make sure it's in m_minimumPaneSizeY..m_maximumPaneSizeY range
273 newWidth
= wxMax(newWidth
, m_minimumPaneSizeX
);
274 newWidth
= wxMin(newWidth
, m_maximumPaneSizeX
);
277 dragRect
= wxRect(x
, y
, newWidth
, newHeight
);
279 wxSashEvent
event(GetId(), edge
);
280 event
.SetEventObject(this);
281 event
.SetDragStatus(status
);
282 event
.SetDragRect(dragRect
);
283 GetEventHandler()->ProcessEvent(event
);
285 else if ( event
.LeftUp() )
289 m_mouseCaptured
= false;
291 else if (event
.Moving() && !event
.Dragging())
293 // Just change the cursor if required
294 if ( sashHit
!= wxSASH_NONE
)
296 if ( (sashHit
== wxSASH_LEFT
) || (sashHit
== wxSASH_RIGHT
) )
298 if (m_currentCursor
!= m_sashCursorWE
)
300 SetCursor(*m_sashCursorWE
);
302 m_currentCursor
= m_sashCursorWE
;
306 if (m_currentCursor
!= m_sashCursorNS
)
308 SetCursor(*m_sashCursorNS
);
310 m_currentCursor
= m_sashCursorNS
;
315 SetCursor(wxNullCursor
);
316 m_currentCursor
= NULL
;
319 else if ( event
.Dragging() &&
320 ((m_dragMode
== wxSASH_DRAG_DRAGGING
) ||
321 (m_dragMode
== wxSASH_DRAG_LEFT_DOWN
)) )
323 if ( (m_draggingEdge
== wxSASH_LEFT
) || (m_draggingEdge
== wxSASH_RIGHT
) )
325 if (m_currentCursor
!= m_sashCursorWE
)
327 SetCursor(*m_sashCursorWE
);
329 m_currentCursor
= m_sashCursorWE
;
333 if (m_currentCursor
!= m_sashCursorNS
)
335 SetCursor(*m_sashCursorNS
);
337 m_currentCursor
= m_sashCursorNS
;
340 if (m_dragMode
== wxSASH_DRAG_LEFT_DOWN
)
342 m_dragMode
= wxSASH_DRAG_DRAGGING
;
343 DrawSashTracker(m_draggingEdge
, x
, y
);
347 if ( m_dragMode
== wxSASH_DRAG_DRAGGING
)
350 DrawSashTracker(m_draggingEdge
, m_oldX
, m_oldY
);
353 DrawSashTracker(m_draggingEdge
, x
, y
);
359 else if ( event
.LeftDClick() )
368 void wxSashWindow::OnSize(wxSizeEvent
& WXUNUSED(event
))
373 wxSashEdgePosition
wxSashWindow::SashHitTest(int x
, int y
, int WXUNUSED(tolerance
))
376 GetClientSize(& cx
, & cy
);
379 for (i
= 0; i
< 4; i
++)
381 wxSashEdge
& edge
= m_sashes
[i
];
382 wxSashEdgePosition position
= (wxSashEdgePosition
) i
;
390 if (y
>= 0 && y
<= GetEdgeMargin(position
))
396 if ((x
>= cx
- GetEdgeMargin(position
)) && (x
<= cx
))
402 if ((y
>= cy
- GetEdgeMargin(position
)) && (y
<= cy
))
403 return wxSASH_BOTTOM
;
408 if ((x
<= GetEdgeMargin(position
)) && (x
>= 0))
422 // Draw 3D effect borders
423 void wxSashWindow::DrawBorders(wxDC
& dc
)
426 GetClientSize(&w
, &h
);
428 wxPen
mediumShadowPen(m_mediumShadowColour
, 1, wxSOLID
);
429 wxPen
darkShadowPen(m_darkShadowColour
, 1, wxSOLID
);
430 wxPen
lightShadowPen(m_lightShadowColour
, 1, wxSOLID
);
431 wxPen
hilightPen(m_hilightColour
, 1, wxSOLID
);
433 if ( GetWindowStyleFlag() & wxSW_3DBORDER
)
435 dc
.SetPen(mediumShadowPen
);
436 dc
.DrawLine(0, 0, w
-1, 0);
437 dc
.DrawLine(0, 0, 0, h
- 1);
439 dc
.SetPen(darkShadowPen
);
440 dc
.DrawLine(1, 1, w
-2, 1);
441 dc
.DrawLine(1, 1, 1, h
-2);
443 dc
.SetPen(hilightPen
);
444 dc
.DrawLine(0, h
-1, w
-1, h
-1);
445 dc
.DrawLine(w
-1, 0, w
-1, h
); // Surely the maximum y pos. should be h - 1.
446 /// Anyway, h is required for MSW.
448 dc
.SetPen(lightShadowPen
);
449 dc
.DrawLine(w
-2, 1, w
-2, h
-2); // Right hand side
450 dc
.DrawLine(1, h
-2, w
-1, h
-2); // Bottom
452 else if ( GetWindowStyleFlag() & wxSW_BORDER
)
454 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
455 dc
.SetPen(*wxBLACK_PEN
);
456 dc
.DrawRectangle(0, 0, w
-1, h
-1);
459 dc
.SetPen(wxNullPen
);
460 dc
.SetBrush(wxNullBrush
);
463 void wxSashWindow::DrawSashes(wxDC
& dc
)
466 for (i
= 0; i
< 4; i
++)
467 if (m_sashes
[i
].m_show
)
468 DrawSash((wxSashEdgePosition
) i
, dc
);
472 void wxSashWindow::DrawSash(wxSashEdgePosition edge
, wxDC
& dc
)
475 GetClientSize(&w
, &h
);
477 wxPen
facePen(m_faceColour
, 1, wxSOLID
);
478 wxBrush
faceBrush(m_faceColour
, wxSOLID
);
479 wxPen
mediumShadowPen(m_mediumShadowColour
, 1, wxSOLID
);
480 wxPen
darkShadowPen(m_darkShadowColour
, 1, wxSOLID
);
481 wxPen
lightShadowPen(m_lightShadowColour
, 1, wxSOLID
);
482 wxPen
hilightPen(m_hilightColour
, 1, wxSOLID
);
483 wxColour
blackClr(0, 0, 0);
484 wxColour
whiteClr(255, 255, 255);
485 wxPen
blackPen(blackClr
, 1, wxSOLID
);
486 wxPen
whitePen(whiteClr
, 1, wxSOLID
);
488 if ( edge
== wxSASH_LEFT
|| edge
== wxSASH_RIGHT
)
490 int sashPosition
= (edge
== wxSASH_LEFT
) ? 0 : ( w
- GetEdgeMargin(edge
) );
493 dc
.SetBrush(faceBrush
);
494 dc
.DrawRectangle(sashPosition
, 0, GetEdgeMargin(edge
), h
);
496 if (GetWindowStyleFlag() & wxSW_3DSASH
)
498 if (edge
== wxSASH_LEFT
)
500 // Draw a dark grey line on the left to indicate that the
502 dc
.SetPen(mediumShadowPen
);
503 dc
.DrawLine(GetEdgeMargin(edge
), 0, GetEdgeMargin(edge
), h
);
507 // Draw a highlight line on the right to indicate that the
509 dc
.SetPen(hilightPen
);
510 dc
.DrawLine(w
- GetEdgeMargin(edge
), 0, w
- GetEdgeMargin(edge
), h
);
514 else // top or bottom
516 int sashPosition
= (edge
== wxSASH_TOP
) ? 0 : ( h
- GetEdgeMargin(edge
) );
519 dc
.SetBrush(faceBrush
);
520 dc
.DrawRectangle(0, sashPosition
, w
, GetEdgeMargin(edge
));
522 if (GetWindowStyleFlag() & wxSW_3DSASH
)
524 if (edge
== wxSASH_BOTTOM
)
526 // Draw a highlight line on the bottom to indicate that the
528 dc
.SetPen(hilightPen
);
529 dc
.DrawLine(0, h
- GetEdgeMargin(edge
), w
, h
- GetEdgeMargin(edge
));
533 // Draw a drak grey line on the top to indicate that the
535 dc
.SetPen(mediumShadowPen
);
536 dc
.DrawLine(1, GetEdgeMargin(edge
), w
-1, GetEdgeMargin(edge
));
541 dc
.SetPen(wxNullPen
);
542 dc
.SetBrush(wxNullBrush
);
545 // Draw the sash tracker (for whilst moving the sash)
546 void wxSashWindow::DrawSashTracker(wxSashEdgePosition edge
, int x
, int y
)
549 GetClientSize(&w
, &h
);
555 if ( edge
== wxSASH_LEFT
|| edge
== wxSASH_RIGHT
)
560 if ( (edge
== wxSASH_LEFT
) && (x1
> w
) )
564 else if ( (edge
== wxSASH_RIGHT
) && (x1
< 0) )
574 if ( (edge
== wxSASH_TOP
) && (y1
> h
) )
579 else if ( (edge
== wxSASH_BOTTOM
) && (y1
< 0) )
586 ClientToScreen(&x1
, &y1
);
587 ClientToScreen(&x2
, &y2
);
589 wxPen
sashTrackerPen(*wxBLACK
, 2, wxSOLID
);
591 screenDC
.SetLogicalFunction(wxINVERT
);
592 screenDC
.SetPen(sashTrackerPen
);
593 screenDC
.SetBrush(*wxTRANSPARENT_BRUSH
);
595 screenDC
.DrawLine(x1
, y1
, x2
, y2
);
597 screenDC
.SetLogicalFunction(wxCOPY
);
599 screenDC
.SetPen(wxNullPen
);
600 screenDC
.SetBrush(wxNullBrush
);
603 // Position and size subwindows.
604 // Note that the border size applies to each subwindow, not
605 // including the edges next to the sash.
606 void wxSashWindow::SizeWindows()
609 GetClientSize(&cw
, &ch
);
611 if (GetChildren().GetCount() == 1)
613 wxWindow
* child
= GetChildren().GetFirst()->GetData();
621 if (m_sashes
[0].m_show
)
624 height
-= m_borderSize
;
626 y
+= m_extraBorderSize
;
629 if (m_sashes
[3].m_show
)
632 width
-= m_borderSize
;
634 x
+= m_extraBorderSize
;
637 if (m_sashes
[1].m_show
)
639 width
-= m_borderSize
;
641 width
-= 2*m_extraBorderSize
;
644 if (m_sashes
[2].m_show
)
646 height
-= m_borderSize
;
648 height
-= 2*m_extraBorderSize
;
650 child
->SetSize(x
, y
, width
, height
);
652 else if (GetChildren().GetCount() > 1)
654 // Perhaps multiple children are themselves sash windows.
655 // TODO: this doesn't really work because the subwindows sizes/positions
656 // must be set to leave a gap for the parent's sash (hit-test and decorations).
657 // Perhaps we can allow for this within LayoutWindow, testing whether the parent
658 // is a sash window, and if so, allowing some space for the edges.
659 wxLayoutAlgorithm layout
;
660 layout
.LayoutWindow(this);
668 // Initialize colours
669 void wxSashWindow::InitColours()
672 m_faceColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
673 m_mediumShadowColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
);
674 m_darkShadowColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
);
675 m_lightShadowColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
676 m_hilightColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DHILIGHT
);
679 void wxSashWindow::SetSashVisible(wxSashEdgePosition edge
, bool sash
)
681 m_sashes
[edge
].m_show
= sash
;
683 m_sashes
[edge
].m_margin
= m_borderSize
;
685 m_sashes
[edge
].m_margin
= 0;
688 #if defined( __WXMSW__ ) || defined( __WXMAC__)
690 // this is currently called (and needed) under MSW only...
691 void wxSashWindow::OnSetCursor(wxSetCursorEvent
& event
)
693 // if we don't do it, the resizing cursor might be set for child window:
694 // and like this we explicitly say that our cursor should not be used for
695 // children windows which overlap us
697 if ( SashHitTest(event
.GetX(), event
.GetY()) != wxSASH_NONE
)
699 // default processing is ok
702 //else: do nothing, in particular, don't call Skip()