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"
32 #include "wx/string.h"
33 #include "wx/dcscreen.h"
34 #include "wx/sashwin.h"
35 #include "wx/laywin.h"
37 #if !USE_SHARED_LIBRARY
38 IMPLEMENT_DYNAMIC_CLASS(wxSashWindow
, wxWindow
)
39 IMPLEMENT_DYNAMIC_CLASS(wxSashEvent
, wxCommandEvent
)
41 BEGIN_EVENT_TABLE(wxSashWindow
, wxWindow
)
42 EVT_PAINT(wxSashWindow::OnPaint
)
43 EVT_SIZE(wxSashWindow::OnSize
)
44 EVT_MOUSE_EVENTS(wxSashWindow::OnMouseEvent
)
48 wxSashWindow::wxSashWindow()
50 m_draggingEdge
= wxSASH_NONE
;
51 m_dragMode
= wxSASH_DRAG_NONE
;
57 m_extraBorderSize
= 0;
58 m_sashCursorWE
= NULL
;
59 m_sashCursorNS
= NULL
;
61 m_minimumPaneSizeX
= 0;
62 m_minimumPaneSizeY
= 0;
63 m_maximumPaneSizeX
= 10000;
64 m_maximumPaneSizeY
= 10000;
67 wxSashWindow::wxSashWindow(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
,
68 const wxSize
& size
, long style
, const wxString
& name
)
69 :wxWindow(parent
, id
, pos
, size
, style
, name
)
71 m_draggingEdge
= wxSASH_NONE
;
72 m_dragMode
= wxSASH_DRAG_NONE
;
78 m_extraBorderSize
= 0;
79 m_minimumPaneSizeX
= 0;
80 m_minimumPaneSizeY
= 0;
81 m_maximumPaneSizeX
= 10000;
82 m_maximumPaneSizeY
= 10000;
83 m_sashCursorWE
= new wxCursor(wxCURSOR_SIZEWE
);
84 m_sashCursorNS
= new wxCursor(wxCURSOR_SIZENS
);
86 // Eventually, we'll respond to colour change messages
90 wxSashWindow::~wxSashWindow()
92 delete m_sashCursorWE
;
93 delete m_sashCursorNS
;
96 void wxSashWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
100 // if ( m_borderSize > 0 )
106 void wxSashWindow::OnMouseEvent(wxMouseEvent
& event
)
109 event
.Position(&x
, &y
);
111 wxSashEdgePosition sashHit
= SashHitTest(x
, y
);
114 SetCursor(wxCursor());
116 if (event
.LeftDown())
118 if ( sashHit
!= wxSASH_NONE
)
122 // Required for X to specify that
123 // that we wish to draw on top of all windows
124 // - and we optimise by specifying the area
125 // for creating the overlap window.
126 // Find the first frame or dialog and use this to specify
127 // the area to draw on.
128 wxWindow
* parent
= this;
130 while (parent
&& !parent
->IsKindOf(CLASSINFO(wxDialog
)) &&
131 !parent
->IsKindOf(CLASSINFO(wxFrame
)))
132 parent
= parent
->GetParent();
134 wxScreenDC::StartDrawingOnTop(parent
);
136 // We don't say we're dragging yet; we leave that
137 // decision for the Dragging() branch, to ensure
138 // the user has dragged a little bit.
139 m_dragMode
= wxSASH_DRAG_LEFT_DOWN
;
140 m_draggingEdge
= sashHit
;
145 else if ( event
.LeftUp() && m_dragMode
== wxSASH_DRAG_LEFT_DOWN
)
147 // Wasn't a proper drag
149 wxScreenDC::EndDrawingOnTop();
150 m_dragMode
= wxSASH_DRAG_NONE
;
151 m_draggingEdge
= wxSASH_NONE
;
153 else if (event
.LeftUp() && m_dragMode
== wxSASH_DRAG_DRAGGING
)
155 // We can stop dragging now and see what we've got.
156 m_dragMode
= wxSASH_DRAG_NONE
;
159 DrawSashTracker(m_draggingEdge
, m_oldX
, m_oldY
);
161 // End drawing on top (frees the window used for drawing
163 wxScreenDC::EndDrawingOnTop();
168 GetPosition(&xp
, &yp
);
170 wxSashEdgePosition edge
= m_draggingEdge
;
171 m_draggingEdge
= wxSASH_NONE
;
174 wxSashDragStatus status
= wxSASH_STATUS_OK
;
180 status
= wxSASH_STATUS_OUT_OF_RANGE
;
181 int newHeight
= (h
- y
);
182 newHeight
=wxMax(newHeight
,m_minimumPaneSizeY
);
183 newHeight
=wxMin(newHeight
,m_maximumPaneSizeY
);
184 dragRect
= wxRect(xp
, (yp
+ h
) - newHeight
, w
, newHeight
);
190 status
= wxSASH_STATUS_OUT_OF_RANGE
;
192 newHeight
=wxMax(newHeight
,m_minimumPaneSizeY
);
193 newHeight
=wxMin(newHeight
,m_maximumPaneSizeY
);
194 dragRect
= wxRect(xp
, yp
, w
, newHeight
);
200 status
= wxSASH_STATUS_OUT_OF_RANGE
;
201 int newWidth
= (w
- x
);
202 newWidth
=wxMax(newWidth
,m_minimumPaneSizeX
);
203 newWidth
=wxMin(newWidth
,m_maximumPaneSizeX
);
204 dragRect
= wxRect((xp
+ w
) - newWidth
, yp
, newWidth
, h
);
210 status
= wxSASH_STATUS_OUT_OF_RANGE
;
212 newWidth
=wxMax(newWidth
,m_minimumPaneSizeX
);
213 newWidth
=wxMin(newWidth
,m_maximumPaneSizeX
);
214 dragRect
= wxRect(xp
, yp
, newWidth
, h
);
223 wxSashEvent
event(GetId(), edge
);
224 event
.SetEventObject(this);
225 event
.SetDragStatus(status
);
226 event
.SetDragRect(dragRect
);
227 GetEventHandler()->ProcessEvent(event
);
229 else if (event
.Moving() && !event
.Dragging())
231 // Just change the cursor if required
232 if ( sashHit
!= wxSASH_NONE
)
234 if ( (sashHit
== wxSASH_LEFT
) || (sashHit
== wxSASH_RIGHT
) )
236 SetCursor(*m_sashCursorWE
);
240 SetCursor(*m_sashCursorNS
);
244 else if ( event
.Dragging() &&
245 ((m_dragMode
== wxSASH_DRAG_DRAGGING
) || (m_dragMode
== wxSASH_DRAG_LEFT_DOWN
))
248 if ( (m_draggingEdge
== wxSASH_LEFT
) || (m_draggingEdge
== wxSASH_RIGHT
) )
250 SetCursor(*m_sashCursorWE
);
254 SetCursor(*m_sashCursorNS
);
257 if (m_dragMode
== wxSASH_DRAG_LEFT_DOWN
)
259 m_dragMode
= wxSASH_DRAG_DRAGGING
;
260 DrawSashTracker(m_draggingEdge
, x
, y
);
264 if ( m_dragMode
== wxSASH_DRAG_DRAGGING
)
267 DrawSashTracker(m_draggingEdge
, m_oldX
, m_oldY
);
270 DrawSashTracker(m_draggingEdge
, x
, y
);
276 else if ( event
.LeftDClick() )
285 void wxSashWindow::OnSize(wxSizeEvent
& WXUNUSED(event
))
290 wxSashEdgePosition
wxSashWindow::SashHitTest(int x
, int y
, int WXUNUSED(tolerance
))
293 GetClientSize(& cx
, & cy
);
296 for (i
= 0; i
< 4; i
++)
298 wxSashEdge
& edge
= m_sashes
[i
];
299 wxSashEdgePosition position
= (wxSashEdgePosition
) i
;
307 if (y
>= 0 && y
<= GetEdgeMargin(position
))
313 if ((x
>= cx
- GetEdgeMargin(position
)) && (x
<= cx
))
319 if ((y
>= cy
- GetEdgeMargin(position
)) && (y
<= cy
))
320 return wxSASH_BOTTOM
;
325 if ((x
<= GetEdgeMargin(position
)) && (x
>= 0))
339 // Draw 3D effect borders
340 void wxSashWindow::DrawBorders(wxDC
& dc
)
343 GetClientSize(&w
, &h
);
345 wxPen
mediumShadowPen(m_mediumShadowColour
, 1, wxSOLID
);
346 wxPen
darkShadowPen(m_darkShadowColour
, 1, wxSOLID
);
347 wxPen
lightShadowPen(m_lightShadowColour
, 1, wxSOLID
);
348 wxPen
hilightPen(m_hilightColour
, 1, wxSOLID
);
350 if ( GetWindowStyleFlag() & wxSP_3D
)
352 dc
.SetPen(mediumShadowPen
);
353 dc
.DrawLine(0, 0, w
-1, 0);
354 dc
.DrawLine(0, 0, 0, h
- 1);
356 dc
.SetPen(darkShadowPen
);
357 dc
.DrawLine(1, 1, w
-2, 1);
358 dc
.DrawLine(1, 1, 1, h
-2);
360 dc
.SetPen(hilightPen
);
361 dc
.DrawLine(0, h
-1, w
-1, h
-1);
362 dc
.DrawLine(w
-1, 0, w
-1, h
); // Surely the maximum y pos. should be h - 1.
363 /// Anyway, h is required for MSW.
365 dc
.SetPen(lightShadowPen
);
366 dc
.DrawLine(w
-2, 1, w
-2, h
-2); // Right hand side
367 dc
.DrawLine(1, h
-2, w
-1, h
-2); // Bottom
369 else if ( GetWindowStyleFlag() & wxSP_BORDER
)
371 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
372 dc
.SetPen(*wxBLACK_PEN
);
373 dc
.DrawRectangle(0, 0, w
-1, h
-1);
376 dc
.SetPen(wxNullPen
);
377 dc
.SetBrush(wxNullBrush
);
380 void wxSashWindow::DrawSashes(wxDC
& dc
)
383 for (i
= 0; i
< 4; i
++)
384 if (m_sashes
[i
].m_show
)
385 DrawSash((wxSashEdgePosition
) i
, dc
);
389 void wxSashWindow::DrawSash(wxSashEdgePosition edge
, wxDC
& dc
)
392 GetClientSize(&w
, &h
);
394 wxPen
facePen(m_faceColour
, 1, wxSOLID
);
395 wxBrush
faceBrush(m_faceColour
, wxSOLID
);
396 wxPen
mediumShadowPen(m_mediumShadowColour
, 1, wxSOLID
);
397 wxPen
darkShadowPen(m_darkShadowColour
, 1, wxSOLID
);
398 wxPen
lightShadowPen(m_lightShadowColour
, 1, wxSOLID
);
399 wxPen
hilightPen(m_hilightColour
, 1, wxSOLID
);
400 wxPen
blackPen(wxColour(0, 0, 0), 1, wxSOLID
);
401 wxPen
whitePen(wxColour(255, 255, 255), 1, wxSOLID
);
403 if ( edge
== wxSASH_LEFT
|| edge
== wxSASH_RIGHT
)
405 int sashPosition
= 0;
406 if (edge
== wxSASH_LEFT
)
409 sashPosition
= w
- GetEdgeMargin(edge
);
412 dc
.SetBrush(faceBrush
);
413 dc
.DrawRectangle(sashPosition
, 0, GetEdgeMargin(edge
), h
);
415 if (GetWindowStyleFlag() & wxSW_3D
)
417 if (edge
== wxSASH_LEFT
)
419 // Draw a black line on the left to indicate that the
422 dc
.DrawLine(GetEdgeMargin(edge
), 0, GetEdgeMargin(edge
), h
);
426 // Draw a white line on the right to indicate that the
429 dc
.DrawLine(w
- GetEdgeMargin(edge
), 0, w
- GetEdgeMargin(edge
), h
);
433 else // top or bottom
435 int sashPosition
= 0;
436 if (edge
== wxSASH_TOP
)
439 sashPosition
= h
- GetEdgeMargin(edge
);
442 dc
.SetBrush(faceBrush
);
443 dc
.DrawRectangle(0, sashPosition
, w
, GetEdgeMargin(edge
));
445 if (GetWindowStyleFlag() & wxSW_3D
)
447 if (edge
== wxSASH_BOTTOM
)
449 // Draw a black line on the bottom to indicate that the
452 dc
.DrawLine(0, h
- GetEdgeMargin(edge
), w
, h
- GetEdgeMargin(edge
));
456 // Draw a white line on the top to indicate that the
459 dc
.DrawLine(0, GetEdgeMargin(edge
), w
, GetEdgeMargin(edge
));
464 dc
.SetPen(wxNullPen
);
465 dc
.SetBrush(wxNullBrush
);
468 // Draw the sash tracker (for whilst moving the sash)
469 void wxSashWindow::DrawSashTracker(wxSashEdgePosition edge
, int x
, int y
)
472 GetClientSize(&w
, &h
);
478 if ( edge
== wxSASH_LEFT
|| edge
== wxSASH_RIGHT
)
483 if ( (edge
== wxSASH_LEFT
) && (x1
> w
) )
487 else if ( (edge
== wxSASH_RIGHT
) && (x1
< 0) )
497 if ( (edge
== wxSASH_TOP
) && (y1
> h
) )
502 else if ( (edge
== wxSASH_BOTTOM
) && (y1
< 0) )
509 ClientToScreen(&x1
, &y1
);
510 ClientToScreen(&x2
, &y2
);
512 wxPen
sashTrackerPen(*wxBLACK
, 2, wxSOLID
);
514 screenDC
.SetLogicalFunction(wxXOR
);
515 screenDC
.SetPen(sashTrackerPen
);
516 screenDC
.SetBrush(*wxTRANSPARENT_BRUSH
);
518 screenDC
.DrawLine(x1
, y1
, x2
, y2
);
520 screenDC
.SetLogicalFunction(wxCOPY
);
522 screenDC
.SetPen(wxNullPen
);
523 screenDC
.SetBrush(wxNullBrush
);
526 // Position and size subwindows.
527 // Note that the border size applies to each subwindow, not
528 // including the edges next to the sash.
529 void wxSashWindow::SizeWindows()
532 GetClientSize(&cw
, &ch
);
534 if (GetChildren().Number() == 1)
536 wxWindow
* child
= (wxWindow
*) (GetChildren().First()->Data());
544 if (m_sashes
[0].m_show
)
547 height
-= m_borderSize
;
549 y
+= m_extraBorderSize
;
552 if (m_sashes
[3].m_show
)
555 width
-= m_borderSize
;
557 x
+= m_extraBorderSize
;
560 if (m_sashes
[1].m_show
)
562 width
-= m_borderSize
;
564 width
-= 2*m_extraBorderSize
;
567 if (m_sashes
[2].m_show
)
569 height
-= m_borderSize
;
571 height
-= 2*m_extraBorderSize
;
573 child
->SetSize(x
, y
, width
, height
);
575 else if (GetChildren().Number() > 1)
577 // Perhaps multiple children are themselves sash windows.
578 // TODO: this doesn't really work because the subwindows sizes/positions
579 // must be set to leave a gap for the parent's sash (hit-test and decorations).
580 // Perhaps we can allow for this within LayoutWindow, testing whether the parent
581 // is a sash window, and if so, allowing some space for the edges.
582 wxLayoutAlgorithm layout
;
583 layout
.LayoutWindow(this);
591 // Initialize colours
592 void wxSashWindow::InitColours()
595 #if defined(__WIN95__)
596 m_faceColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
);
597 m_mediumShadowColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DSHADOW
);
598 m_darkShadowColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DDKSHADOW
);
599 m_lightShadowColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT
);
600 m_hilightColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DHILIGHT
);
602 m_faceColour
= *(wxTheColourDatabase
->FindColour("LIGHT GREY"));
603 m_mediumShadowColour
= *(wxTheColourDatabase
->FindColour("GREY"));
604 m_darkShadowColour
= *(wxTheColourDatabase
->FindColour("BLACK"));
605 m_lightShadowColour
= *(wxTheColourDatabase
->FindColour("LIGHT GREY"));
606 m_hilightColour
= *(wxTheColourDatabase
->FindColour("WHITE"));
610 void wxSashWindow::SetSashVisible(wxSashEdgePosition edge
, bool sash
)
612 m_sashes
[edge
].m_show
= sash
;
614 m_sashes
[edge
].m_margin
= m_borderSize
;
616 m_sashes
[edge
].m_margin
= 0;