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
);
115 SetCursor(* wxSTANDARD_CURSOR
);
118 SetCursor(wxCursor());
121 if (event
.LeftDown())
123 if ( sashHit
!= wxSASH_NONE
)
127 // Required for X to specify that
128 // that we wish to draw on top of all windows
129 // - and we optimise by specifying the area
130 // for creating the overlap window.
131 // Find the first frame or dialog and use this to specify
132 // the area to draw on.
133 wxWindow
* parent
= this;
135 while (parent
&& !parent
->IsKindOf(CLASSINFO(wxDialog
)) &&
136 !parent
->IsKindOf(CLASSINFO(wxFrame
)))
137 parent
= parent
->GetParent();
139 wxScreenDC::StartDrawingOnTop(parent
);
141 // We don't say we're dragging yet; we leave that
142 // decision for the Dragging() branch, to ensure
143 // the user has dragged a little bit.
144 m_dragMode
= wxSASH_DRAG_LEFT_DOWN
;
145 m_draggingEdge
= sashHit
;
150 else if ( event
.LeftUp() && m_dragMode
== wxSASH_DRAG_LEFT_DOWN
)
152 // Wasn't a proper drag
154 wxScreenDC::EndDrawingOnTop();
155 m_dragMode
= wxSASH_DRAG_NONE
;
156 m_draggingEdge
= wxSASH_NONE
;
158 else if (event
.LeftUp() && m_dragMode
== wxSASH_DRAG_DRAGGING
)
160 // We can stop dragging now and see what we've got.
161 m_dragMode
= wxSASH_DRAG_NONE
;
164 DrawSashTracker(m_draggingEdge
, m_oldX
, m_oldY
);
166 // End drawing on top (frees the window used for drawing
168 wxScreenDC::EndDrawingOnTop();
173 GetPosition(&xp
, &yp
);
175 wxSashEdgePosition edge
= m_draggingEdge
;
176 m_draggingEdge
= wxSASH_NONE
;
179 wxSashDragStatus status
= wxSASH_STATUS_OK
;
185 status
= wxSASH_STATUS_OUT_OF_RANGE
;
186 int newHeight
= (h
- y
);
187 newHeight
=wxMax(newHeight
,m_minimumPaneSizeY
);
188 newHeight
=wxMin(newHeight
,m_maximumPaneSizeY
);
189 dragRect
= wxRect(xp
, (yp
+ h
) - newHeight
, w
, newHeight
);
195 status
= wxSASH_STATUS_OUT_OF_RANGE
;
197 newHeight
=wxMax(newHeight
,m_minimumPaneSizeY
);
198 newHeight
=wxMin(newHeight
,m_maximumPaneSizeY
);
199 dragRect
= wxRect(xp
, yp
, w
, newHeight
);
205 status
= wxSASH_STATUS_OUT_OF_RANGE
;
206 int newWidth
= (w
- x
);
207 newWidth
=wxMax(newWidth
,m_minimumPaneSizeX
);
208 newWidth
=wxMin(newWidth
,m_maximumPaneSizeX
);
209 dragRect
= wxRect((xp
+ w
) - newWidth
, yp
, newWidth
, h
);
215 status
= wxSASH_STATUS_OUT_OF_RANGE
;
217 newWidth
=wxMax(newWidth
,m_minimumPaneSizeX
);
218 newWidth
=wxMin(newWidth
,m_maximumPaneSizeX
);
219 dragRect
= wxRect(xp
, yp
, newWidth
, h
);
228 wxSashEvent
event(GetId(), edge
);
229 event
.SetEventObject(this);
230 event
.SetDragStatus(status
);
231 event
.SetDragRect(dragRect
);
232 GetEventHandler()->ProcessEvent(event
);
234 else if (event
.Moving() && !event
.Dragging())
236 // Just change the cursor if required
237 if ( sashHit
!= wxSASH_NONE
)
239 if ( (sashHit
== wxSASH_LEFT
) || (sashHit
== wxSASH_RIGHT
) )
241 SetCursor(*m_sashCursorWE
);
245 SetCursor(*m_sashCursorNS
);
249 else if ( event
.Dragging() &&
250 ((m_dragMode
== wxSASH_DRAG_DRAGGING
) || (m_dragMode
== wxSASH_DRAG_LEFT_DOWN
))
253 if ( (m_draggingEdge
== wxSASH_LEFT
) || (m_draggingEdge
== wxSASH_RIGHT
) )
255 SetCursor(*m_sashCursorWE
);
259 SetCursor(*m_sashCursorNS
);
262 if (m_dragMode
== wxSASH_DRAG_LEFT_DOWN
)
264 m_dragMode
= wxSASH_DRAG_DRAGGING
;
265 DrawSashTracker(m_draggingEdge
, x
, y
);
269 if ( m_dragMode
== wxSASH_DRAG_DRAGGING
)
272 DrawSashTracker(m_draggingEdge
, m_oldX
, m_oldY
);
275 DrawSashTracker(m_draggingEdge
, x
, y
);
281 else if ( event
.LeftDClick() )
290 void wxSashWindow::OnSize(wxSizeEvent
& WXUNUSED(event
))
295 wxSashEdgePosition
wxSashWindow::SashHitTest(int x
, int y
, int WXUNUSED(tolerance
))
298 GetClientSize(& cx
, & cy
);
301 for (i
= 0; i
< 4; i
++)
303 wxSashEdge
& edge
= m_sashes
[i
];
304 wxSashEdgePosition position
= (wxSashEdgePosition
) i
;
312 if (y
>= 0 && y
<= GetEdgeMargin(position
))
318 if ((x
>= cx
- GetEdgeMargin(position
)) && (x
<= cx
))
324 if ((y
>= cy
- GetEdgeMargin(position
)) && (y
<= cy
))
325 return wxSASH_BOTTOM
;
330 if ((x
<= GetEdgeMargin(position
)) && (x
>= 0))
344 // Draw 3D effect borders
345 void wxSashWindow::DrawBorders(wxDC
& dc
)
348 GetClientSize(&w
, &h
);
350 wxPen
mediumShadowPen(m_mediumShadowColour
, 1, wxSOLID
);
351 wxPen
darkShadowPen(m_darkShadowColour
, 1, wxSOLID
);
352 wxPen
lightShadowPen(m_lightShadowColour
, 1, wxSOLID
);
353 wxPen
hilightPen(m_hilightColour
, 1, wxSOLID
);
355 if ( GetWindowStyleFlag() & wxSP_3D
)
357 dc
.SetPen(mediumShadowPen
);
358 dc
.DrawLine(0, 0, w
-1, 0);
359 dc
.DrawLine(0, 0, 0, h
- 1);
361 dc
.SetPen(darkShadowPen
);
362 dc
.DrawLine(1, 1, w
-2, 1);
363 dc
.DrawLine(1, 1, 1, h
-2);
365 dc
.SetPen(hilightPen
);
366 dc
.DrawLine(0, h
-1, w
-1, h
-1);
367 dc
.DrawLine(w
-1, 0, w
-1, h
); // Surely the maximum y pos. should be h - 1.
368 /// Anyway, h is required for MSW.
370 dc
.SetPen(lightShadowPen
);
371 dc
.DrawLine(w
-2, 1, w
-2, h
-2); // Right hand side
372 dc
.DrawLine(1, h
-2, w
-1, h
-2); // Bottom
374 else if ( GetWindowStyleFlag() & wxSP_BORDER
)
376 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
377 dc
.SetPen(*wxBLACK_PEN
);
378 dc
.DrawRectangle(0, 0, w
-1, h
-1);
381 dc
.SetPen(wxNullPen
);
382 dc
.SetBrush(wxNullBrush
);
385 void wxSashWindow::DrawSashes(wxDC
& dc
)
388 for (i
= 0; i
< 4; i
++)
389 if (m_sashes
[i
].m_show
)
390 DrawSash((wxSashEdgePosition
) i
, dc
);
394 void wxSashWindow::DrawSash(wxSashEdgePosition edge
, wxDC
& dc
)
397 GetClientSize(&w
, &h
);
399 wxPen
facePen(m_faceColour
, 1, wxSOLID
);
400 wxBrush
faceBrush(m_faceColour
, wxSOLID
);
401 wxPen
mediumShadowPen(m_mediumShadowColour
, 1, wxSOLID
);
402 wxPen
darkShadowPen(m_darkShadowColour
, 1, wxSOLID
);
403 wxPen
lightShadowPen(m_lightShadowColour
, 1, wxSOLID
);
404 wxPen
hilightPen(m_hilightColour
, 1, wxSOLID
);
405 wxPen
blackPen(wxColour(0, 0, 0), 1, wxSOLID
);
406 wxPen
whitePen(wxColour(255, 255, 255), 1, wxSOLID
);
408 if ( edge
== wxSASH_LEFT
|| edge
== wxSASH_RIGHT
)
410 int sashPosition
= 0;
411 if (edge
== wxSASH_LEFT
)
414 sashPosition
= w
- GetEdgeMargin(edge
);
417 dc
.SetBrush(faceBrush
);
418 dc
.DrawRectangle(sashPosition
, 0, GetEdgeMargin(edge
), h
);
420 if (GetWindowStyleFlag() & wxSW_3D
)
422 if (edge
== wxSASH_LEFT
)
424 // Draw a black line on the left to indicate that the
427 dc
.DrawLine(GetEdgeMargin(edge
), 0, GetEdgeMargin(edge
), h
);
431 // Draw a white line on the right to indicate that the
434 dc
.DrawLine(w
- GetEdgeMargin(edge
), 0, w
- GetEdgeMargin(edge
), h
);
438 else // top or bottom
440 int sashPosition
= 0;
441 if (edge
== wxSASH_TOP
)
444 sashPosition
= h
- GetEdgeMargin(edge
);
447 dc
.SetBrush(faceBrush
);
448 dc
.DrawRectangle(0, sashPosition
, w
, GetEdgeMargin(edge
));
450 if (GetWindowStyleFlag() & wxSW_3D
)
452 if (edge
== wxSASH_BOTTOM
)
454 // Draw a black line on the bottom to indicate that the
457 dc
.DrawLine(0, h
- GetEdgeMargin(edge
), w
, h
- GetEdgeMargin(edge
));
461 // Draw a white line on the top to indicate that the
464 dc
.DrawLine(0, GetEdgeMargin(edge
), w
, GetEdgeMargin(edge
));
469 dc
.SetPen(wxNullPen
);
470 dc
.SetBrush(wxNullBrush
);
473 // Draw the sash tracker (for whilst moving the sash)
474 void wxSashWindow::DrawSashTracker(wxSashEdgePosition edge
, int x
, int y
)
477 GetClientSize(&w
, &h
);
483 if ( edge
== wxSASH_LEFT
|| edge
== wxSASH_RIGHT
)
488 if ( (edge
== wxSASH_LEFT
) && (x1
> w
) )
492 else if ( (edge
== wxSASH_RIGHT
) && (x1
< 0) )
502 if ( (edge
== wxSASH_TOP
) && (y1
> h
) )
507 else if ( (edge
== wxSASH_BOTTOM
) && (y1
< 0) )
514 ClientToScreen(&x1
, &y1
);
515 ClientToScreen(&x2
, &y2
);
517 wxPen
sashTrackerPen(*wxBLACK
, 2, wxSOLID
);
519 screenDC
.SetLogicalFunction(wxXOR
);
520 screenDC
.SetPen(sashTrackerPen
);
521 screenDC
.SetBrush(*wxTRANSPARENT_BRUSH
);
523 screenDC
.DrawLine(x1
, y1
, x2
, y2
);
525 screenDC
.SetLogicalFunction(wxCOPY
);
527 screenDC
.SetPen(wxNullPen
);
528 screenDC
.SetBrush(wxNullBrush
);
531 // Position and size subwindows.
532 // Note that the border size applies to each subwindow, not
533 // including the edges next to the sash.
534 void wxSashWindow::SizeWindows()
537 GetClientSize(&cw
, &ch
);
539 if (GetChildren().Number() == 1)
541 wxWindow
* child
= (wxWindow
*) (GetChildren().First()->Data());
549 if (m_sashes
[0].m_show
)
552 height
-= m_borderSize
;
554 y
+= m_extraBorderSize
;
557 if (m_sashes
[3].m_show
)
560 width
-= m_borderSize
;
562 x
+= m_extraBorderSize
;
565 if (m_sashes
[1].m_show
)
567 width
-= m_borderSize
;
569 width
-= 2*m_extraBorderSize
;
572 if (m_sashes
[2].m_show
)
574 height
-= m_borderSize
;
576 height
-= 2*m_extraBorderSize
;
578 child
->SetSize(x
, y
, width
, height
);
580 else if (GetChildren().Number() > 1)
582 // Perhaps multiple children are themselves sash windows.
583 // TODO: this doesn't really work because the subwindows sizes/positions
584 // must be set to leave a gap for the parent's sash (hit-test and decorations).
585 // Perhaps we can allow for this within LayoutWindow, testing whether the parent
586 // is a sash window, and if so, allowing some space for the edges.
587 wxLayoutAlgorithm layout
;
588 layout
.LayoutWindow(this);
596 // Initialize colours
597 void wxSashWindow::InitColours()
600 #if defined(__WIN95__)
601 m_faceColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
);
602 m_mediumShadowColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DSHADOW
);
603 m_darkShadowColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DDKSHADOW
);
604 m_lightShadowColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT
);
605 m_hilightColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DHILIGHT
);
607 m_faceColour
= *(wxTheColourDatabase
->FindColour("LIGHT GREY"));
608 m_mediumShadowColour
= *(wxTheColourDatabase
->FindColour("GREY"));
609 m_darkShadowColour
= *(wxTheColourDatabase
->FindColour("BLACK"));
610 m_lightShadowColour
= *(wxTheColourDatabase
->FindColour("LIGHT GREY"));
611 m_hilightColour
= *(wxTheColourDatabase
->FindColour("WHITE"));
615 void wxSashWindow::SetSashVisible(wxSashEdgePosition edge
, bool sash
)
617 m_sashes
[edge
].m_show
= sash
;
619 m_sashes
[edge
].m_margin
= m_borderSize
;
621 m_sashes
[edge
].m_margin
= 0;