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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
15 #pragma implementation "sashwin.h"
18 // For compilers that support precompilation, includes "wx.h".
19 #include "wx/wxprec.h"
28 #include "wx/dialog.h"
30 #include "wx/settings.h"
37 #include "wx/dcscreen.h"
38 #include "wx/sashwin.h"
39 #include "wx/laywin.h"
41 DEFINE_EVENT_TYPE(wxEVT_SASH_DRAGGED
)
43 IMPLEMENT_DYNAMIC_CLASS(wxSashWindow
, wxWindow
)
44 IMPLEMENT_DYNAMIC_CLASS(wxSashEvent
, wxCommandEvent
)
46 BEGIN_EVENT_TABLE(wxSashWindow
, wxWindow
)
47 EVT_PAINT(wxSashWindow::OnPaint
)
48 EVT_SIZE(wxSashWindow::OnSize
)
49 EVT_MOUSE_EVENTS(wxSashWindow::OnMouseEvent
)
50 #if defined( __WXMSW__ ) || defined( __WXMAC__)
51 EVT_SET_CURSOR(wxSashWindow::OnSetCursor
)
56 bool wxSashWindow::Create(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
,
57 const wxSize
& size
, long style
, const wxString
& name
)
59 return wxWindow::Create(parent
, id
, pos
, size
, style
, name
);
62 wxSashWindow::~wxSashWindow()
64 delete m_sashCursorWE
;
65 delete m_sashCursorNS
;
68 void wxSashWindow::Init()
70 m_draggingEdge
= wxSASH_NONE
;
71 m_dragMode
= wxSASH_DRAG_NONE
;
77 m_extraBorderSize
= 0;
78 m_minimumPaneSizeX
= 0;
79 m_minimumPaneSizeY
= 0;
80 m_maximumPaneSizeX
= 10000;
81 m_maximumPaneSizeY
= 10000;
82 m_sashCursorWE
= new wxCursor(wxCURSOR_SIZEWE
);
83 m_sashCursorNS
= new wxCursor(wxCURSOR_SIZENS
);
84 m_mouseCaptured
= false;
85 m_currentCursor
= NULL
;
87 // Eventually, we'll respond to colour change messages
91 void wxSashWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
99 void wxSashWindow::OnMouseEvent(wxMouseEvent
& event
)
102 event
.GetPosition(&x
, &y
);
104 wxSashEdgePosition sashHit
= SashHitTest(x
, y
);
106 if (event
.LeftDown())
109 m_mouseCaptured
= true;
111 if ( sashHit
!= wxSASH_NONE
)
113 // Required for X to specify that
114 // that we wish to draw on top of all windows
115 // - and we optimise by specifying the area
116 // for creating the overlap window.
117 // Find the first frame or dialog and use this to specify
118 // the area to draw on.
119 wxWindow
* parent
= this;
121 while (parent
&& !parent
->IsKindOf(CLASSINFO(wxDialog
)) &&
122 !parent
->IsKindOf(CLASSINFO(wxFrame
)))
123 parent
= parent
->GetParent();
125 wxScreenDC::StartDrawingOnTop(parent
);
127 // We don't say we're dragging yet; we leave that
128 // decision for the Dragging() branch, to ensure
129 // the user has dragged a little bit.
130 m_dragMode
= wxSASH_DRAG_LEFT_DOWN
;
131 m_draggingEdge
= sashHit
;
135 if ( (sashHit
== wxSASH_LEFT
) || (sashHit
== wxSASH_RIGHT
) )
137 if (m_currentCursor
!= m_sashCursorWE
)
139 SetCursor(*m_sashCursorWE
);
141 m_currentCursor
= m_sashCursorWE
;
145 if (m_currentCursor
!= m_sashCursorNS
)
147 SetCursor(*m_sashCursorNS
);
149 m_currentCursor
= m_sashCursorNS
;
153 else if ( event
.LeftUp() && m_dragMode
== wxSASH_DRAG_LEFT_DOWN
)
155 // Wasn't a proper drag
158 m_mouseCaptured
= false;
160 wxScreenDC::EndDrawingOnTop();
161 m_dragMode
= wxSASH_DRAG_NONE
;
162 m_draggingEdge
= wxSASH_NONE
;
164 else if (event
.LeftUp() && m_dragMode
== wxSASH_DRAG_DRAGGING
)
166 // We can stop dragging now and see what we've got.
167 m_dragMode
= wxSASH_DRAG_NONE
;
170 m_mouseCaptured
= false;
173 DrawSashTracker(m_draggingEdge
, m_oldX
, m_oldY
);
175 // End drawing on top (frees the window used for drawing
177 wxScreenDC::EndDrawingOnTop();
182 GetPosition(&xp
, &yp
);
184 wxSashEdgePosition edge
= m_draggingEdge
;
185 m_draggingEdge
= wxSASH_NONE
;
188 wxSashDragStatus status
= wxSASH_STATUS_OK
;
190 // the new height and width of the window - if -1, it didn't change
191 int newHeight
= wxDefaultCoord
,
192 newWidth
= wxDefaultCoord
;
194 // NB: x and y may be negative and they're relative to the sash window
195 // upper left corner, while xp and yp are expressed in the parent
196 // window system of coordinates, so adjust them! After this
197 // adjustment, all coordinates are relative to the parent window.
206 // top sash shouldn't get below the bottom one
207 status
= wxSASH_STATUS_OUT_OF_RANGE
;
211 newHeight
= h
- (y
- yp
);
218 // bottom sash shouldn't get above the top one
219 status
= wxSASH_STATUS_OUT_OF_RANGE
;
230 // left sash shouldn't get beyond the right one
231 status
= wxSASH_STATUS_OUT_OF_RANGE
;
235 newWidth
= w
- (x
- xp
);
242 // and the right sash, finally, shouldn't be beyond the
244 status
= wxSASH_STATUS_OUT_OF_RANGE
;
253 // can this happen at all?
257 if ( newHeight
== wxDefaultCoord
)
264 // make sure it's in m_minimumPaneSizeY..m_maximumPaneSizeY range
265 newHeight
= wxMax(newHeight
, m_minimumPaneSizeY
);
266 newHeight
= wxMin(newHeight
, m_maximumPaneSizeY
);
269 if ( newWidth
== wxDefaultCoord
)
276 // make sure it's in m_minimumPaneSizeY..m_maximumPaneSizeY range
277 newWidth
= wxMax(newWidth
, m_minimumPaneSizeX
);
278 newWidth
= wxMin(newWidth
, m_maximumPaneSizeX
);
281 dragRect
= wxRect(x
, y
, newWidth
, newHeight
);
283 wxSashEvent
event(GetId(), edge
);
284 event
.SetEventObject(this);
285 event
.SetDragStatus(status
);
286 event
.SetDragRect(dragRect
);
287 GetEventHandler()->ProcessEvent(event
);
289 else if ( event
.LeftUp() )
293 m_mouseCaptured
= false;
295 else if (event
.Moving() && !event
.Dragging())
297 // Just change the cursor if required
298 if ( sashHit
!= wxSASH_NONE
)
300 if ( (sashHit
== wxSASH_LEFT
) || (sashHit
== wxSASH_RIGHT
) )
302 if (m_currentCursor
!= m_sashCursorWE
)
304 SetCursor(*m_sashCursorWE
);
306 m_currentCursor
= m_sashCursorWE
;
310 if (m_currentCursor
!= m_sashCursorNS
)
312 SetCursor(*m_sashCursorNS
);
314 m_currentCursor
= m_sashCursorNS
;
319 SetCursor(wxNullCursor
);
320 m_currentCursor
= NULL
;
323 else if ( event
.Dragging() &&
324 ((m_dragMode
== wxSASH_DRAG_DRAGGING
) ||
325 (m_dragMode
== wxSASH_DRAG_LEFT_DOWN
)) )
327 if ( (m_draggingEdge
== wxSASH_LEFT
) || (m_draggingEdge
== wxSASH_RIGHT
) )
329 if (m_currentCursor
!= m_sashCursorWE
)
331 SetCursor(*m_sashCursorWE
);
333 m_currentCursor
= m_sashCursorWE
;
337 if (m_currentCursor
!= m_sashCursorNS
)
339 SetCursor(*m_sashCursorNS
);
341 m_currentCursor
= m_sashCursorNS
;
344 if (m_dragMode
== wxSASH_DRAG_LEFT_DOWN
)
346 m_dragMode
= wxSASH_DRAG_DRAGGING
;
347 DrawSashTracker(m_draggingEdge
, x
, y
);
351 if ( m_dragMode
== wxSASH_DRAG_DRAGGING
)
354 DrawSashTracker(m_draggingEdge
, m_oldX
, m_oldY
);
357 DrawSashTracker(m_draggingEdge
, x
, y
);
363 else if ( event
.LeftDClick() )
372 void wxSashWindow::OnSize(wxSizeEvent
& WXUNUSED(event
))
377 wxSashEdgePosition
wxSashWindow::SashHitTest(int x
, int y
, int WXUNUSED(tolerance
))
380 GetClientSize(& cx
, & cy
);
383 for (i
= 0; i
< 4; i
++)
385 wxSashEdge
& edge
= m_sashes
[i
];
386 wxSashEdgePosition position
= (wxSashEdgePosition
) i
;
394 if (y
>= 0 && y
<= GetEdgeMargin(position
))
400 if ((x
>= cx
- GetEdgeMargin(position
)) && (x
<= cx
))
406 if ((y
>= cy
- GetEdgeMargin(position
)) && (y
<= cy
))
407 return wxSASH_BOTTOM
;
412 if ((x
<= GetEdgeMargin(position
)) && (x
>= 0))
426 // Draw 3D effect borders
427 void wxSashWindow::DrawBorders(wxDC
& dc
)
430 GetClientSize(&w
, &h
);
432 wxPen
mediumShadowPen(m_mediumShadowColour
, 1, wxSOLID
);
433 wxPen
darkShadowPen(m_darkShadowColour
, 1, wxSOLID
);
434 wxPen
lightShadowPen(m_lightShadowColour
, 1, wxSOLID
);
435 wxPen
hilightPen(m_hilightColour
, 1, wxSOLID
);
437 if ( GetWindowStyleFlag() & wxSW_3DBORDER
)
439 dc
.SetPen(mediumShadowPen
);
440 dc
.DrawLine(0, 0, w
-1, 0);
441 dc
.DrawLine(0, 0, 0, h
- 1);
443 dc
.SetPen(darkShadowPen
);
444 dc
.DrawLine(1, 1, w
-2, 1);
445 dc
.DrawLine(1, 1, 1, h
-2);
447 dc
.SetPen(hilightPen
);
448 dc
.DrawLine(0, h
-1, w
-1, h
-1);
449 dc
.DrawLine(w
-1, 0, w
-1, h
); // Surely the maximum y pos. should be h - 1.
450 /// Anyway, h is required for MSW.
452 dc
.SetPen(lightShadowPen
);
453 dc
.DrawLine(w
-2, 1, w
-2, h
-2); // Right hand side
454 dc
.DrawLine(1, h
-2, w
-1, h
-2); // Bottom
456 else if ( GetWindowStyleFlag() & wxSW_BORDER
)
458 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
459 dc
.SetPen(*wxBLACK_PEN
);
460 dc
.DrawRectangle(0, 0, w
-1, h
-1);
463 dc
.SetPen(wxNullPen
);
464 dc
.SetBrush(wxNullBrush
);
467 void wxSashWindow::DrawSashes(wxDC
& dc
)
470 for (i
= 0; i
< 4; i
++)
471 if (m_sashes
[i
].m_show
)
472 DrawSash((wxSashEdgePosition
) i
, dc
);
476 void wxSashWindow::DrawSash(wxSashEdgePosition edge
, wxDC
& dc
)
479 GetClientSize(&w
, &h
);
481 wxPen
facePen(m_faceColour
, 1, wxSOLID
);
482 wxBrush
faceBrush(m_faceColour
, wxSOLID
);
483 wxPen
mediumShadowPen(m_mediumShadowColour
, 1, wxSOLID
);
484 wxPen
darkShadowPen(m_darkShadowColour
, 1, wxSOLID
);
485 wxPen
lightShadowPen(m_lightShadowColour
, 1, wxSOLID
);
486 wxPen
hilightPen(m_hilightColour
, 1, wxSOLID
);
487 wxColour
blackClr(0, 0, 0);
488 wxColour
whiteClr(255, 255, 255);
489 wxPen
blackPen(blackClr
, 1, wxSOLID
);
490 wxPen
whitePen(whiteClr
, 1, wxSOLID
);
492 if ( edge
== wxSASH_LEFT
|| edge
== wxSASH_RIGHT
)
494 int sashPosition
= (edge
== wxSASH_LEFT
) ? 0 : ( w
- GetEdgeMargin(edge
) );
497 dc
.SetBrush(faceBrush
);
498 dc
.DrawRectangle(sashPosition
, 0, GetEdgeMargin(edge
), h
);
500 if (GetWindowStyleFlag() & wxSW_3DSASH
)
502 if (edge
== wxSASH_LEFT
)
504 // Draw a dark grey line on the left to indicate that the
506 dc
.SetPen(mediumShadowPen
);
507 dc
.DrawLine(GetEdgeMargin(edge
), 0, GetEdgeMargin(edge
), h
);
511 // Draw a highlight line on the right to indicate that the
513 dc
.SetPen(hilightPen
);
514 dc
.DrawLine(w
- GetEdgeMargin(edge
), 0, w
- GetEdgeMargin(edge
), h
);
518 else // top or bottom
520 int sashPosition
= (edge
== wxSASH_TOP
) ? 0 : ( h
- GetEdgeMargin(edge
) );
523 dc
.SetBrush(faceBrush
);
524 dc
.DrawRectangle(0, sashPosition
, w
, GetEdgeMargin(edge
));
526 if (GetWindowStyleFlag() & wxSW_3DSASH
)
528 if (edge
== wxSASH_BOTTOM
)
530 // Draw a highlight line on the bottom to indicate that the
532 dc
.SetPen(hilightPen
);
533 dc
.DrawLine(0, h
- GetEdgeMargin(edge
), w
, h
- GetEdgeMargin(edge
));
537 // Draw a drak grey line on the top to indicate that the
539 dc
.SetPen(mediumShadowPen
);
540 dc
.DrawLine(1, GetEdgeMargin(edge
), w
-1, GetEdgeMargin(edge
));
545 dc
.SetPen(wxNullPen
);
546 dc
.SetBrush(wxNullBrush
);
549 // Draw the sash tracker (for whilst moving the sash)
550 void wxSashWindow::DrawSashTracker(wxSashEdgePosition edge
, int x
, int y
)
553 GetClientSize(&w
, &h
);
559 if ( edge
== wxSASH_LEFT
|| edge
== wxSASH_RIGHT
)
564 if ( (edge
== wxSASH_LEFT
) && (x1
> w
) )
568 else if ( (edge
== wxSASH_RIGHT
) && (x1
< 0) )
578 if ( (edge
== wxSASH_TOP
) && (y1
> h
) )
583 else if ( (edge
== wxSASH_BOTTOM
) && (y1
< 0) )
590 ClientToScreen(&x1
, &y1
);
591 ClientToScreen(&x2
, &y2
);
593 wxPen
sashTrackerPen(*wxBLACK
, 2, wxSOLID
);
595 screenDC
.SetLogicalFunction(wxINVERT
);
596 screenDC
.SetPen(sashTrackerPen
);
597 screenDC
.SetBrush(*wxTRANSPARENT_BRUSH
);
599 screenDC
.DrawLine(x1
, y1
, x2
, y2
);
601 screenDC
.SetLogicalFunction(wxCOPY
);
603 screenDC
.SetPen(wxNullPen
);
604 screenDC
.SetBrush(wxNullBrush
);
607 // Position and size subwindows.
608 // Note that the border size applies to each subwindow, not
609 // including the edges next to the sash.
610 void wxSashWindow::SizeWindows()
613 GetClientSize(&cw
, &ch
);
615 if (GetChildren().GetCount() == 1)
617 wxWindow
* child
= GetChildren().GetFirst()->GetData();
625 if (m_sashes
[0].m_show
)
628 height
-= m_borderSize
;
630 y
+= m_extraBorderSize
;
633 if (m_sashes
[3].m_show
)
636 width
-= m_borderSize
;
638 x
+= m_extraBorderSize
;
641 if (m_sashes
[1].m_show
)
643 width
-= m_borderSize
;
645 width
-= 2*m_extraBorderSize
;
648 if (m_sashes
[2].m_show
)
650 height
-= m_borderSize
;
652 height
-= 2*m_extraBorderSize
;
654 child
->SetSize(x
, y
, width
, height
);
656 else if (GetChildren().GetCount() > 1)
658 // Perhaps multiple children are themselves sash windows.
659 // TODO: this doesn't really work because the subwindows sizes/positions
660 // must be set to leave a gap for the parent's sash (hit-test and decorations).
661 // Perhaps we can allow for this within LayoutWindow, testing whether the parent
662 // is a sash window, and if so, allowing some space for the edges.
663 wxLayoutAlgorithm layout
;
664 layout
.LayoutWindow(this);
672 // Initialize colours
673 void wxSashWindow::InitColours()
676 m_faceColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
677 m_mediumShadowColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
);
678 m_darkShadowColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
);
679 m_lightShadowColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
680 m_hilightColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DHILIGHT
);
683 void wxSashWindow::SetSashVisible(wxSashEdgePosition edge
, bool sash
)
685 m_sashes
[edge
].m_show
= sash
;
687 m_sashes
[edge
].m_margin
= m_borderSize
;
689 m_sashes
[edge
].m_margin
= 0;
692 #if defined( __WXMSW__ ) || defined( __WXMAC__)
694 // this is currently called (and needed) under MSW only...
695 void wxSashWindow::OnSetCursor(wxSetCursorEvent
& event
)
697 // if we don't do it, the resizing cursor might be set for child window:
698 // and like this we explicitly say that our cursor should not be used for
699 // children windows which overlap us
701 if ( SashHitTest(event
.GetX(), event
.GetY()) != wxSASH_NONE
)
703 // default processing is ok
706 //else: do nothing, in particular, don't call Skip()