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 license
12 /////////////////////////////////////////////////////////////////////////////
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"
36 #include "wx/dcscreen.h"
37 #include "wx/sashwin.h"
38 #include "wx/laywin.h"
40 DEFINE_EVENT_TYPE(wxEVT_SASH_DRAGGED
)
42 IMPLEMENT_DYNAMIC_CLASS(wxSashWindow
, wxWindow
)
43 IMPLEMENT_DYNAMIC_CLASS(wxSashEvent
, wxCommandEvent
)
45 BEGIN_EVENT_TABLE(wxSashWindow
, wxWindow
)
46 EVT_PAINT(wxSashWindow::OnPaint
)
47 EVT_SIZE(wxSashWindow::OnSize
)
48 EVT_MOUSE_EVENTS(wxSashWindow::OnMouseEvent
)
51 bool wxSashWindow::Create(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
,
52 const wxSize
& size
, long style
, const wxString
& name
)
54 return wxWindow::Create(parent
, id
, pos
, size
, style
, name
);
57 wxSashWindow::~wxSashWindow()
59 delete m_sashCursorWE
;
60 delete m_sashCursorNS
;
63 void wxSashWindow::Init()
65 m_draggingEdge
= wxSASH_NONE
;
66 m_dragMode
= wxSASH_DRAG_NONE
;
72 m_extraBorderSize
= 0;
73 m_minimumPaneSizeX
= 0;
74 m_minimumPaneSizeY
= 0;
75 m_maximumPaneSizeX
= 10000;
76 m_maximumPaneSizeY
= 10000;
77 m_sashCursorWE
= new wxCursor(wxCURSOR_SIZEWE
);
78 m_sashCursorNS
= new wxCursor(wxCURSOR_SIZENS
);
79 m_mouseCaptured
= FALSE
;
81 // Eventually, we'll respond to colour change messages
85 void wxSashWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
89 // if ( m_borderSize > 0 )
95 void wxSashWindow::OnMouseEvent(wxMouseEvent
& event
)
98 event
.GetPosition(&x
, &y
);
100 wxSashEdgePosition sashHit
= SashHitTest(x
, y
);
103 #if defined(__WXMOTIF__) || defined(__WXGTK__)
104 // Not necessary and in fact inhibits proper cursor setting (JACS 8/2000)
105 //SetCursor(* wxSTANDARD_CURSOR);
108 SetCursor(wxNullCursor
);
111 if (event
.LeftDown())
114 m_mouseCaptured
= TRUE
;
116 if ( sashHit
!= wxSASH_NONE
)
118 // Required for X to specify that
119 // that we wish to draw on top of all windows
120 // - and we optimise by specifying the area
121 // for creating the overlap window.
122 // Find the first frame or dialog and use this to specify
123 // the area to draw on.
124 wxWindow
* parent
= this;
126 while (parent
&& !parent
->IsKindOf(CLASSINFO(wxDialog
)) &&
127 !parent
->IsKindOf(CLASSINFO(wxFrame
)))
128 parent
= parent
->GetParent();
130 wxScreenDC::StartDrawingOnTop(parent
);
132 // We don't say we're dragging yet; we leave that
133 // decision for the Dragging() branch, to ensure
134 // the user has dragged a little bit.
135 m_dragMode
= wxSASH_DRAG_LEFT_DOWN
;
136 m_draggingEdge
= sashHit
;
140 if ( (sashHit
== wxSASH_LEFT
) || (sashHit
== wxSASH_RIGHT
) )
142 SetCursor(*m_sashCursorWE
);
146 SetCursor(*m_sashCursorNS
);
150 else if ( event
.LeftUp() && m_dragMode
== wxSASH_DRAG_LEFT_DOWN
)
152 // Wasn't a proper drag
155 m_mouseCaptured
= FALSE
;
157 wxScreenDC::EndDrawingOnTop();
158 m_dragMode
= wxSASH_DRAG_NONE
;
159 m_draggingEdge
= wxSASH_NONE
;
161 else if (event
.LeftUp() && m_dragMode
== wxSASH_DRAG_DRAGGING
)
163 // We can stop dragging now and see what we've got.
164 m_dragMode
= wxSASH_DRAG_NONE
;
167 m_mouseCaptured
= FALSE
;
170 DrawSashTracker(m_draggingEdge
, m_oldX
, m_oldY
);
172 // End drawing on top (frees the window used for drawing
174 wxScreenDC::EndDrawingOnTop();
179 GetPosition(&xp
, &yp
);
181 wxSashEdgePosition edge
= m_draggingEdge
;
182 m_draggingEdge
= wxSASH_NONE
;
185 wxSashDragStatus status
= wxSASH_STATUS_OK
;
187 // the new height and width of the window - if -1, it didn't change
191 // NB: x and y may be negative and they're relative to the sash window
192 // upper left corner, while xp and yp are expressed in the parent
193 // window system of coordinates, so adjust them! After this
194 // adjustment, all coordinates are relative to the parent window.
203 // top sash shouldn't get below the bottom one
204 status
= wxSASH_STATUS_OUT_OF_RANGE
;
208 newHeight
= h
- (y
- yp
);
215 // bottom sash shouldn't get above the top one
216 status
= wxSASH_STATUS_OUT_OF_RANGE
;
227 // left sash shouldn't get beyond the right one
228 status
= wxSASH_STATUS_OUT_OF_RANGE
;
232 newWidth
= w
- (x
- xp
);
239 // and the right sash, finally, shouldn't be beyond the
241 status
= wxSASH_STATUS_OUT_OF_RANGE
;
250 // can this happen at all?
254 if ( newHeight
== -1 )
261 // make sure it's in m_minimumPaneSizeY..m_maximumPaneSizeY range
262 newHeight
= wxMax(newHeight
, m_minimumPaneSizeY
);
263 newHeight
= wxMin(newHeight
, m_maximumPaneSizeY
);
266 if ( newWidth
== -1 )
273 // make sure it's in m_minimumPaneSizeY..m_maximumPaneSizeY range
274 newWidth
= wxMax(newWidth
, m_minimumPaneSizeX
);
275 newWidth
= wxMin(newWidth
, m_maximumPaneSizeX
);
278 dragRect
= wxRect(x
, y
, newWidth
, newHeight
);
280 wxSashEvent
event(GetId(), edge
);
281 event
.SetEventObject(this);
282 event
.SetDragStatus(status
);
283 event
.SetDragRect(dragRect
);
284 GetEventHandler()->ProcessEvent(event
);
286 else if ( event
.LeftUp() )
290 m_mouseCaptured
= FALSE
;
292 else if (event
.Moving() && !event
.Dragging())
294 // Just change the cursor if required
295 if ( sashHit
!= wxSASH_NONE
)
297 if ( (sashHit
== wxSASH_LEFT
) || (sashHit
== wxSASH_RIGHT
) )
299 SetCursor(*m_sashCursorWE
);
303 SetCursor(*m_sashCursorNS
);
308 SetCursor(wxNullCursor
);
311 else if ( event
.Dragging() &&
312 ((m_dragMode
== wxSASH_DRAG_DRAGGING
) ||
313 (m_dragMode
== wxSASH_DRAG_LEFT_DOWN
)) )
315 if ( (m_draggingEdge
== wxSASH_LEFT
) || (m_draggingEdge
== wxSASH_RIGHT
) )
317 SetCursor(*m_sashCursorWE
);
321 SetCursor(*m_sashCursorNS
);
324 if (m_dragMode
== wxSASH_DRAG_LEFT_DOWN
)
326 m_dragMode
= wxSASH_DRAG_DRAGGING
;
327 DrawSashTracker(m_draggingEdge
, x
, y
);
331 if ( m_dragMode
== wxSASH_DRAG_DRAGGING
)
334 DrawSashTracker(m_draggingEdge
, m_oldX
, m_oldY
);
337 DrawSashTracker(m_draggingEdge
, x
, y
);
343 else if ( event
.LeftDClick() )
352 void wxSashWindow::OnSize(wxSizeEvent
& WXUNUSED(event
))
357 wxSashEdgePosition
wxSashWindow::SashHitTest(int x
, int y
, int WXUNUSED(tolerance
))
360 GetClientSize(& cx
, & cy
);
363 for (i
= 0; i
< 4; i
++)
365 wxSashEdge
& edge
= m_sashes
[i
];
366 wxSashEdgePosition position
= (wxSashEdgePosition
) i
;
374 if (y
>= 0 && y
<= GetEdgeMargin(position
))
380 if ((x
>= cx
- GetEdgeMargin(position
)) && (x
<= cx
))
386 if ((y
>= cy
- GetEdgeMargin(position
)) && (y
<= cy
))
387 return wxSASH_BOTTOM
;
392 if ((x
<= GetEdgeMargin(position
)) && (x
>= 0))
406 // Draw 3D effect borders
407 void wxSashWindow::DrawBorders(wxDC
& dc
)
410 GetClientSize(&w
, &h
);
412 wxPen
mediumShadowPen(m_mediumShadowColour
, 1, wxSOLID
);
413 wxPen
darkShadowPen(m_darkShadowColour
, 1, wxSOLID
);
414 wxPen
lightShadowPen(m_lightShadowColour
, 1, wxSOLID
);
415 wxPen
hilightPen(m_hilightColour
, 1, wxSOLID
);
417 if ( GetWindowStyleFlag() & wxSW_3DBORDER
)
419 dc
.SetPen(mediumShadowPen
);
420 dc
.DrawLine(0, 0, w
-1, 0);
421 dc
.DrawLine(0, 0, 0, h
- 1);
423 dc
.SetPen(darkShadowPen
);
424 dc
.DrawLine(1, 1, w
-2, 1);
425 dc
.DrawLine(1, 1, 1, h
-2);
427 dc
.SetPen(hilightPen
);
428 dc
.DrawLine(0, h
-1, w
-1, h
-1);
429 dc
.DrawLine(w
-1, 0, w
-1, h
); // Surely the maximum y pos. should be h - 1.
430 /// Anyway, h is required for MSW.
432 dc
.SetPen(lightShadowPen
);
433 dc
.DrawLine(w
-2, 1, w
-2, h
-2); // Right hand side
434 dc
.DrawLine(1, h
-2, w
-1, h
-2); // Bottom
436 else if ( GetWindowStyleFlag() & wxSW_BORDER
)
438 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
439 dc
.SetPen(*wxBLACK_PEN
);
440 dc
.DrawRectangle(0, 0, w
-1, h
-1);
443 dc
.SetPen(wxNullPen
);
444 dc
.SetBrush(wxNullBrush
);
447 void wxSashWindow::DrawSashes(wxDC
& dc
)
450 for (i
= 0; i
< 4; i
++)
451 if (m_sashes
[i
].m_show
)
452 DrawSash((wxSashEdgePosition
) i
, dc
);
456 void wxSashWindow::DrawSash(wxSashEdgePosition edge
, wxDC
& dc
)
459 GetClientSize(&w
, &h
);
461 wxPen
facePen(m_faceColour
, 1, wxSOLID
);
462 wxBrush
faceBrush(m_faceColour
, wxSOLID
);
463 wxPen
mediumShadowPen(m_mediumShadowColour
, 1, wxSOLID
);
464 wxPen
darkShadowPen(m_darkShadowColour
, 1, wxSOLID
);
465 wxPen
lightShadowPen(m_lightShadowColour
, 1, wxSOLID
);
466 wxPen
hilightPen(m_hilightColour
, 1, wxSOLID
);
467 wxPen
blackPen(wxColour(0, 0, 0), 1, wxSOLID
);
468 wxPen
whitePen(wxColour(255, 255, 255), 1, wxSOLID
);
470 if ( edge
== wxSASH_LEFT
|| edge
== wxSASH_RIGHT
)
472 int sashPosition
= 0;
473 if (edge
== wxSASH_LEFT
)
476 sashPosition
= w
- GetEdgeMargin(edge
);
479 dc
.SetBrush(faceBrush
);
480 dc
.DrawRectangle(sashPosition
, 0, GetEdgeMargin(edge
), h
);
482 if (GetWindowStyleFlag() & wxSW_3DSASH
)
484 if (edge
== wxSASH_LEFT
)
486 // Draw a dark grey line on the left to indicate that the
488 dc
.SetPen(mediumShadowPen
);
489 dc
.DrawLine(GetEdgeMargin(edge
), 0, GetEdgeMargin(edge
), h
);
493 // Draw a highlight line on the right to indicate that the
495 dc
.SetPen(hilightPen
);
496 dc
.DrawLine(w
- GetEdgeMargin(edge
), 0, w
- GetEdgeMargin(edge
), h
);
500 else // top or bottom
502 int sashPosition
= 0;
503 if (edge
== wxSASH_TOP
)
506 sashPosition
= h
- GetEdgeMargin(edge
);
509 dc
.SetBrush(faceBrush
);
510 dc
.DrawRectangle(0, sashPosition
, w
, GetEdgeMargin(edge
));
512 if (GetWindowStyleFlag() & wxSW_3DSASH
)
514 if (edge
== wxSASH_BOTTOM
)
516 // Draw a highlight line on the bottom to indicate that the
518 dc
.SetPen(hilightPen
);
519 dc
.DrawLine(0, h
- GetEdgeMargin(edge
), w
, h
- GetEdgeMargin(edge
));
523 // Draw a drak grey line on the top to indicate that the
525 dc
.SetPen(mediumShadowPen
);
526 dc
.DrawLine(1, GetEdgeMargin(edge
), w
-1, GetEdgeMargin(edge
));
531 dc
.SetPen(wxNullPen
);
532 dc
.SetBrush(wxNullBrush
);
535 // Draw the sash tracker (for whilst moving the sash)
536 void wxSashWindow::DrawSashTracker(wxSashEdgePosition edge
, int x
, int y
)
539 GetClientSize(&w
, &h
);
545 if ( edge
== wxSASH_LEFT
|| edge
== wxSASH_RIGHT
)
550 if ( (edge
== wxSASH_LEFT
) && (x1
> w
) )
554 else if ( (edge
== wxSASH_RIGHT
) && (x1
< 0) )
564 if ( (edge
== wxSASH_TOP
) && (y1
> h
) )
569 else if ( (edge
== wxSASH_BOTTOM
) && (y1
< 0) )
576 ClientToScreen(&x1
, &y1
);
577 ClientToScreen(&x2
, &y2
);
579 wxPen
sashTrackerPen(*wxBLACK
, 2, wxSOLID
);
581 screenDC
.SetLogicalFunction(wxINVERT
);
582 screenDC
.SetPen(sashTrackerPen
);
583 screenDC
.SetBrush(*wxTRANSPARENT_BRUSH
);
585 screenDC
.DrawLine(x1
, y1
, x2
, y2
);
587 screenDC
.SetLogicalFunction(wxCOPY
);
589 screenDC
.SetPen(wxNullPen
);
590 screenDC
.SetBrush(wxNullBrush
);
593 // Position and size subwindows.
594 // Note that the border size applies to each subwindow, not
595 // including the edges next to the sash.
596 void wxSashWindow::SizeWindows()
599 GetClientSize(&cw
, &ch
);
601 if (GetChildren().Number() == 1)
603 wxWindow
* child
= (wxWindow
*) (GetChildren().First()->Data());
611 if (m_sashes
[0].m_show
)
614 height
-= m_borderSize
;
616 y
+= m_extraBorderSize
;
619 if (m_sashes
[3].m_show
)
622 width
-= m_borderSize
;
624 x
+= m_extraBorderSize
;
627 if (m_sashes
[1].m_show
)
629 width
-= m_borderSize
;
631 width
-= 2*m_extraBorderSize
;
634 if (m_sashes
[2].m_show
)
636 height
-= m_borderSize
;
638 height
-= 2*m_extraBorderSize
;
640 child
->SetSize(x
, y
, width
, height
);
642 else if (GetChildren().Number() > 1)
644 // Perhaps multiple children are themselves sash windows.
645 // TODO: this doesn't really work because the subwindows sizes/positions
646 // must be set to leave a gap for the parent's sash (hit-test and decorations).
647 // Perhaps we can allow for this within LayoutWindow, testing whether the parent
648 // is a sash window, and if so, allowing some space for the edges.
649 wxLayoutAlgorithm layout
;
650 layout
.LayoutWindow(this);
658 // Initialize colours
659 void wxSashWindow::InitColours()
663 m_faceColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
);
664 m_mediumShadowColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DSHADOW
);
665 m_darkShadowColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DDKSHADOW
);
666 m_lightShadowColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT
);
667 m_hilightColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DHILIGHT
);
669 m_faceColour
= *(wxTheColourDatabase
->FindColour("LIGHT GREY"));
670 m_mediumShadowColour
= *(wxTheColourDatabase
->FindColour("GREY"));
671 m_darkShadowColour
= *(wxTheColourDatabase
->FindColour("BLACK"));
672 m_lightShadowColour
= *(wxTheColourDatabase
->FindColour("LIGHT GREY"));
673 m_hilightColour
= *(wxTheColourDatabase
->FindColour("WHITE"));
677 void wxSashWindow::SetSashVisible(wxSashEdgePosition edge
, bool sash
)
679 m_sashes
[edge
].m_show
= sash
;
681 m_sashes
[edge
].m_margin
= m_borderSize
;
683 m_sashes
[edge
].m_margin
= 0;