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"
36 #if !USE_SHARED_LIBRARY
37 IMPLEMENT_DYNAMIC_CLASS(wxSashWindow
, wxWindow
)
38 IMPLEMENT_DYNAMIC_CLASS(wxSashEvent
, wxCommandEvent
)
40 BEGIN_EVENT_TABLE(wxSashWindow
, wxWindow
)
41 EVT_PAINT(wxSashWindow::OnPaint
)
42 EVT_SIZE(wxSashWindow::OnSize
)
43 EVT_MOUSE_EVENTS(wxSashWindow::OnMouseEvent
)
47 wxSashWindow::wxSashWindow()
49 m_draggingEdge
= wxSASH_NONE
;
50 m_dragMode
= wxSASH_DRAG_NONE
;
56 m_extraBorderSize
= 0;
57 m_sashCursorWE
= NULL
;
58 m_sashCursorNS
= NULL
;
60 m_minimumPaneSizeX
= 0;
61 m_minimumPaneSizeY
= 0;
62 m_maximumPaneSizeX
= 10000;
63 m_maximumPaneSizeY
= 10000;
66 wxSashWindow::wxSashWindow(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
,
67 const wxSize
& size
, long style
, const wxString
& name
)
68 :wxWindow(parent
, id
, pos
, size
, style
, name
)
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
);
85 // Eventually, we'll respond to colour change messages
89 wxSashWindow::~wxSashWindow()
91 delete m_sashCursorWE
;
92 delete m_sashCursorNS
;
95 void wxSashWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
100 if ( m_borderSize
> 0 )
107 void wxSashWindow::OnMouseEvent(wxMouseEvent
& event
)
110 event
.Position(&x
, &y
);
112 wxSashEdgePosition sashHit
= SashHitTest(x
, y
);
114 if (event
.LeftDown())
116 if ( sashHit
!= wxSASH_NONE
)
120 // Required for X to specify that
121 // that we wish to draw on top of all windows
122 // - and we optimise by specifying the area
123 // for creating the overlap window.
124 // Find the first frame or dialog and use this to specify
125 // the area to draw on.
126 wxWindow
* parent
= this;
128 while (parent
&& !parent
->IsKindOf(CLASSINFO(wxDialog
)) &&
129 !parent
->IsKindOf(CLASSINFO(wxFrame
)))
130 parent
= parent
->GetParent();
132 wxScreenDC::StartDrawingOnTop(parent
);
134 // We don't say we're dragging yet; we leave that
135 // decision for the Dragging() branch, to ensure
136 // the user has dragged a little bit.
137 m_dragMode
= wxSASH_DRAG_LEFT_DOWN
;
138 m_draggingEdge
= sashHit
;
143 else if ( event
.LeftUp() && m_dragMode
== wxSASH_DRAG_LEFT_DOWN
)
145 // Wasn't a proper drag
147 wxScreenDC::EndDrawingOnTop();
148 m_dragMode
= wxSASH_DRAG_NONE
;
149 m_draggingEdge
= wxSASH_NONE
;
151 SetCursor(*wxSTANDARD_CURSOR
);
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
);
245 SetCursor(*wxSTANDARD_CURSOR
);
248 else if ( event
.Dragging() &&
249 ((m_dragMode
== wxSASH_DRAG_DRAGGING
) || (m_dragMode
== wxSASH_DRAG_LEFT_DOWN
))
252 if ( (m_draggingEdge
== wxSASH_LEFT
) || (m_draggingEdge
== wxSASH_RIGHT
) )
254 SetCursor(*m_sashCursorWE
);
258 SetCursor(*m_sashCursorNS
);
261 if (m_dragMode
== wxSASH_DRAG_LEFT_DOWN
)
263 m_dragMode
= wxSASH_DRAG_DRAGGING
;
264 DrawSashTracker(m_draggingEdge
, x
, y
);
268 if ( m_dragMode
== wxSASH_DRAG_DRAGGING
)
271 DrawSashTracker(m_draggingEdge
, m_oldX
, m_oldY
);
274 DrawSashTracker(m_draggingEdge
, x
, y
);
280 else if ( event
.LeftDClick() )
289 void wxSashWindow::OnSize(wxSizeEvent
& WXUNUSED(event
))
294 wxSashEdgePosition
wxSashWindow::SashHitTest(int x
, int y
, int WXUNUSED(tolerance
))
297 GetClientSize(& cx
, & cy
);
300 for (i
= 0; i
< 4; i
++)
302 wxSashEdge
& edge
= m_sashes
[i
];
303 wxSashEdgePosition position
= (wxSashEdgePosition
) i
;
311 if (y
>= 0 && y
<= GetEdgeMargin(position
))
317 if ((x
>= cx
- GetEdgeMargin(position
)) && (x
<= cx
))
323 if ((y
>= cy
- GetEdgeMargin(position
)) && (y
<= cy
))
324 return wxSASH_BOTTOM
;
329 if ((x
>= GetEdgeMargin(position
)) && (x
>= 0))
343 // Draw 3D effect borders
344 void wxSashWindow::DrawBorders(wxDC
& dc
)
347 GetClientSize(&w
, &h
);
349 wxPen
mediumShadowPen(m_mediumShadowColour
, 1, wxSOLID
);
350 wxPen
darkShadowPen(m_darkShadowColour
, 1, wxSOLID
);
351 wxPen
lightShadowPen(m_lightShadowColour
, 1, wxSOLID
);
352 wxPen
hilightPen(m_hilightColour
, 1, wxSOLID
);
354 if ( GetWindowStyleFlag() & wxSP_3D
)
356 dc
.SetPen(mediumShadowPen
);
357 dc
.DrawLine(0, 0, w
-1, 0);
358 dc
.DrawLine(0, 0, 0, h
- 1);
360 dc
.SetPen(darkShadowPen
);
361 dc
.DrawLine(1, 1, w
-2, 1);
362 dc
.DrawLine(1, 1, 1, h
-2);
364 dc
.SetPen(hilightPen
);
365 dc
.DrawLine(0, h
-1, w
-1, h
-1);
366 dc
.DrawLine(w
-1, 0, w
-1, h
); // Surely the maximum y pos. should be h - 1.
367 /// Anyway, h is required for MSW.
369 dc
.SetPen(lightShadowPen
);
370 dc
.DrawLine(w
-2, 1, w
-2, h
-2); // Right hand side
371 dc
.DrawLine(1, h
-2, w
-1, h
-2); // Bottom
373 else if ( GetWindowStyleFlag() & wxSP_BORDER
)
375 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
376 dc
.SetPen(*wxBLACK_PEN
);
377 dc
.DrawRectangle(0, 0, w
-1, h
-1);
380 dc
.SetPen(wxNullPen
);
381 dc
.SetBrush(wxNullBrush
);
384 void wxSashWindow::DrawSashes(wxDC
& dc
)
387 for (i
= 0; i
< 4; i
++)
388 if (m_sashes
[i
].m_show
)
389 DrawSash((wxSashEdgePosition
) i
, dc
);
393 void wxSashWindow::DrawSash(wxSashEdgePosition edge
, wxDC
& dc
)
396 GetClientSize(&w
, &h
);
398 wxPen
facePen(m_faceColour
, 1, wxSOLID
);
399 wxBrush
faceBrush(m_faceColour
, wxSOLID
);
400 wxPen
mediumShadowPen(m_mediumShadowColour
, 1, wxSOLID
);
401 wxPen
darkShadowPen(m_darkShadowColour
, 1, wxSOLID
);
402 wxPen
lightShadowPen(m_lightShadowColour
, 1, wxSOLID
);
403 wxPen
hilightPen(m_hilightColour
, 1, wxSOLID
);
404 wxPen
blackPen(wxColour(0, 0, 0), 1, wxSOLID
);
405 wxPen
whitePen(wxColour(255, 255, 255), 1, wxSOLID
);
407 if ( edge
== wxSASH_LEFT
|| edge
== wxSASH_RIGHT
)
409 int sashPosition
= 0;
410 if (edge
== wxSASH_LEFT
)
413 sashPosition
= w
- GetEdgeMargin(edge
);
416 dc
.SetBrush(faceBrush
);
417 dc
.DrawRectangle(sashPosition
, 0, GetEdgeMargin(edge
), h
);
419 if (GetWindowStyleFlag() & wxSW_3D
)
421 if (edge
== wxSASH_LEFT
)
423 // Draw a black line on the left to indicate that the
426 dc
.DrawLine(GetEdgeMargin(edge
), 0, GetEdgeMargin(edge
), h
);
430 // Draw a white line on the right to indicate that the
433 dc
.DrawLine(w
- GetEdgeMargin(edge
), 0, w
- GetEdgeMargin(edge
), h
);
437 else // top or bottom
439 int sashPosition
= 0;
440 if (edge
== wxSASH_TOP
)
443 sashPosition
= h
- GetEdgeMargin(edge
);
446 dc
.SetBrush(faceBrush
);
447 dc
.DrawRectangle(0, sashPosition
, w
, GetEdgeMargin(edge
));
449 if (GetWindowStyleFlag() & wxSW_3D
)
451 if (edge
== wxSASH_BOTTOM
)
453 // Draw a black line on the bottom to indicate that the
456 dc
.DrawLine(0, h
- GetEdgeMargin(edge
), w
, h
- GetEdgeMargin(edge
));
460 // Draw a white line on the top to indicate that the
463 dc
.DrawLine(0, GetEdgeMargin(edge
), w
, GetEdgeMargin(edge
));
468 dc
.SetPen(wxNullPen
);
469 dc
.SetBrush(wxNullBrush
);
472 // Draw the sash tracker (for whilst moving the sash)
473 void wxSashWindow::DrawSashTracker(wxSashEdgePosition edge
, int x
, int y
)
476 GetClientSize(&w
, &h
);
482 if ( edge
== wxSASH_LEFT
|| edge
== wxSASH_RIGHT
)
487 if ( (edge
== wxSASH_LEFT
) && (x1
> w
) )
491 else if ( (edge
== wxSASH_RIGHT
) && (x1
< 0) )
501 if ( (edge
== wxSASH_TOP
) && (y1
> h
) )
506 else if ( (edge
== wxSASH_BOTTOM
) && (y1
< 0) )
513 ClientToScreen(&x1
, &y1
);
514 ClientToScreen(&x2
, &y2
);
516 wxPen
sashTrackerPen(*wxBLACK
, 2, wxSOLID
);
518 screenDC
.SetLogicalFunction(wxXOR
);
519 screenDC
.SetPen(sashTrackerPen
);
520 screenDC
.SetBrush(*wxTRANSPARENT_BRUSH
);
522 screenDC
.DrawLine(x1
, y1
, x2
, y2
);
524 screenDC
.SetLogicalFunction(wxCOPY
);
526 screenDC
.SetPen(wxNullPen
);
527 screenDC
.SetBrush(wxNullBrush
);
530 // Position and size subwindows.
531 // Note that the border size applies to each subwindow, not
532 // including the edges next to the sash.
533 void wxSashWindow::SizeWindows()
536 GetClientSize(&cw
, &ch
);
538 if (GetChildren().Number() > 0)
540 wxWindow
* child
= (wxWindow
*) (GetChildren().First()->Data());
548 if (m_sashes
[0].m_show
)
551 height
-= m_borderSize
;
553 y
+= m_extraBorderSize
;
556 if (m_sashes
[3].m_show
)
559 width
-= m_borderSize
;
561 x
+= m_extraBorderSize
;
564 if (m_sashes
[1].m_show
)
566 width
-= m_borderSize
;
568 width
-= 2*m_extraBorderSize
;
571 if (m_sashes
[2].m_show
)
573 height
-= m_borderSize
;
575 height
-= 2*m_extraBorderSize
;
577 child
->SetSize(x
, y
, width
, height
);
585 // Initialize colours
586 void wxSashWindow::InitColours()
589 #if defined(__WIN95__)
590 m_faceColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
);
591 m_mediumShadowColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DSHADOW
);
592 m_darkShadowColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DDKSHADOW
);
593 m_lightShadowColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT
);
594 m_hilightColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DHILIGHT
);
596 m_faceColour
= *(wxTheColourDatabase
->FindColour("LIGHT GREY"));
597 m_mediumShadowColour
= *(wxTheColourDatabase
->FindColour("GREY"));
598 m_darkShadowColour
= *(wxTheColourDatabase
->FindColour("BLACK"));
599 m_lightShadowColour
= *(wxTheColourDatabase
->FindColour("LIGHT GREY"));
600 m_hilightColour
= *(wxTheColourDatabase
->FindColour("WHITE"));
604 void wxSashWindow::SetSashVisible(wxSashEdgePosition edge
, bool sash
)
606 m_sashes
[edge
].m_show
= sash
;
608 m_sashes
[edge
].m_margin
= m_borderSize
;
610 m_sashes
[edge
].m_margin
= 0;