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 wxScreenDC::StartDrawingOnTop(this);
126 // We don't say we're dragging yet; we leave that
127 // decision for the Dragging() branch, to ensure
128 // the user has dragged a little bit.
129 m_dragMode
= wxSASH_DRAG_LEFT_DOWN
;
130 m_draggingEdge
= sashHit
;
135 else if ( event
.LeftUp() && m_dragMode
== wxSASH_DRAG_LEFT_DOWN
)
137 // Wasn't a proper drag
139 wxScreenDC::EndDrawingOnTop();
140 m_dragMode
= wxSASH_DRAG_NONE
;
141 m_draggingEdge
= wxSASH_NONE
;
143 SetCursor(*wxSTANDARD_CURSOR
);
145 else if (event
.LeftUp() && m_dragMode
== wxSASH_DRAG_DRAGGING
)
147 // We can stop dragging now and see what we've got.
148 m_dragMode
= wxSASH_DRAG_NONE
;
151 DrawSashTracker(m_draggingEdge
, m_oldX
, m_oldY
);
153 // End drawing on top (frees the window used for drawing
155 wxScreenDC::EndDrawingOnTop();
160 GetPosition(&xp
, &yp
);
162 wxSashEdgePosition edge
= m_draggingEdge
;
163 m_draggingEdge
= wxSASH_NONE
;
166 wxSashDragStatus status
= wxSASH_STATUS_OK
;
172 status
= wxSASH_STATUS_OUT_OF_RANGE
;
173 int newHeight
= (h
- y
);
174 newHeight
=wxMax(newHeight
,m_minimumPaneSizeY
);
175 newHeight
=wxMin(newHeight
,m_maximumPaneSizeY
);
176 dragRect
= wxRect(xp
, (yp
+ h
) - newHeight
, w
, newHeight
);
182 status
= wxSASH_STATUS_OUT_OF_RANGE
;
184 newHeight
=wxMax(newHeight
,m_minimumPaneSizeY
);
185 newHeight
=wxMin(newHeight
,m_maximumPaneSizeY
);
186 dragRect
= wxRect(xp
, yp
, w
, newHeight
);
192 status
= wxSASH_STATUS_OUT_OF_RANGE
;
193 int newWidth
= (w
- x
);
194 newWidth
=wxMax(newWidth
,m_minimumPaneSizeX
);
195 newWidth
=wxMin(newWidth
,m_maximumPaneSizeX
);
196 dragRect
= wxRect((xp
+ w
) - newWidth
, yp
, newWidth
, h
);
202 status
= wxSASH_STATUS_OUT_OF_RANGE
;
204 newWidth
=wxMax(newWidth
,m_minimumPaneSizeX
);
205 newWidth
=wxMin(newWidth
,m_maximumPaneSizeX
);
206 dragRect
= wxRect(xp
, yp
, newWidth
, h
);
215 wxSashEvent
event(GetId(), edge
);
216 event
.SetEventObject(this);
217 event
.SetDragStatus(status
);
218 event
.SetDragRect(dragRect
);
219 GetEventHandler()->ProcessEvent(event
);
221 else if (event
.Moving() && !event
.Dragging())
223 // Just change the cursor if required
224 if ( sashHit
!= wxSASH_NONE
)
226 if ( (sashHit
== wxSASH_LEFT
) || (sashHit
== wxSASH_RIGHT
) )
228 SetCursor(*m_sashCursorWE
);
232 SetCursor(*m_sashCursorNS
);
237 SetCursor(*wxSTANDARD_CURSOR
);
240 else if ( event
.Dragging() &&
241 ((m_dragMode
== wxSASH_DRAG_DRAGGING
) || (m_dragMode
== wxSASH_DRAG_LEFT_DOWN
))
244 if ( (m_draggingEdge
== wxSASH_LEFT
) || (m_draggingEdge
== wxSASH_RIGHT
) )
246 SetCursor(*m_sashCursorWE
);
250 SetCursor(*m_sashCursorNS
);
253 if (m_dragMode
== wxSASH_DRAG_LEFT_DOWN
)
255 m_dragMode
= wxSASH_DRAG_DRAGGING
;
256 DrawSashTracker(m_draggingEdge
, x
, y
);
260 if ( m_dragMode
== wxSASH_DRAG_DRAGGING
)
263 DrawSashTracker(m_draggingEdge
, m_oldX
, m_oldY
);
266 DrawSashTracker(m_draggingEdge
, x
, y
);
272 else if ( event
.LeftDClick() )
281 void wxSashWindow::OnSize(wxSizeEvent
& WXUNUSED(event
))
286 wxSashEdgePosition
wxSashWindow::SashHitTest(int x
, int y
, int WXUNUSED(tolerance
))
289 GetClientSize(& cx
, & cy
);
292 for (i
= 0; i
< 4; i
++)
294 wxSashEdge
& edge
= m_sashes
[i
];
295 wxSashEdgePosition position
= (wxSashEdgePosition
) i
;
303 if (y
>= 0 && y
<= GetEdgeMargin(position
))
309 if ((x
>= cx
- GetEdgeMargin(position
)) && (x
<= cx
))
315 if ((y
>= cy
- GetEdgeMargin(position
)) && (y
<= cy
))
316 return wxSASH_BOTTOM
;
321 if ((x
>= GetEdgeMargin(position
)) && (x
>= 0))
335 // Draw 3D effect borders
336 void wxSashWindow::DrawBorders(wxDC
& dc
)
339 GetClientSize(&w
, &h
);
341 wxPen
mediumShadowPen(m_mediumShadowColour
, 1, wxSOLID
);
342 wxPen
darkShadowPen(m_darkShadowColour
, 1, wxSOLID
);
343 wxPen
lightShadowPen(m_lightShadowColour
, 1, wxSOLID
);
344 wxPen
hilightPen(m_hilightColour
, 1, wxSOLID
);
346 if ( GetWindowStyleFlag() & wxSP_3D
)
348 dc
.SetPen(mediumShadowPen
);
349 dc
.DrawLine(0, 0, w
-1, 0);
350 dc
.DrawLine(0, 0, 0, h
- 1);
352 dc
.SetPen(darkShadowPen
);
353 dc
.DrawLine(1, 1, w
-2, 1);
354 dc
.DrawLine(1, 1, 1, h
-2);
356 dc
.SetPen(hilightPen
);
357 dc
.DrawLine(0, h
-1, w
-1, h
-1);
358 dc
.DrawLine(w
-1, 0, w
-1, h
); // Surely the maximum y pos. should be h - 1.
359 /// Anyway, h is required for MSW.
361 dc
.SetPen(lightShadowPen
);
362 dc
.DrawLine(w
-2, 1, w
-2, h
-2); // Right hand side
363 dc
.DrawLine(1, h
-2, w
-1, h
-2); // Bottom
365 else if ( GetWindowStyleFlag() & wxSP_BORDER
)
367 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
368 dc
.SetPen(*wxBLACK_PEN
);
369 dc
.DrawRectangle(0, 0, w
-1, h
-1);
372 dc
.SetPen(wxNullPen
);
373 dc
.SetBrush(wxNullBrush
);
376 void wxSashWindow::DrawSashes(wxDC
& dc
)
379 for (i
= 0; i
< 4; i
++)
380 if (m_sashes
[i
].m_show
)
381 DrawSash((wxSashEdgePosition
) i
, dc
);
385 void wxSashWindow::DrawSash(wxSashEdgePosition edge
, wxDC
& dc
)
388 GetClientSize(&w
, &h
);
390 wxPen
facePen(m_faceColour
, 1, wxSOLID
);
391 wxBrush
faceBrush(m_faceColour
, wxSOLID
);
392 wxPen
mediumShadowPen(m_mediumShadowColour
, 1, wxSOLID
);
393 wxPen
darkShadowPen(m_darkShadowColour
, 1, wxSOLID
);
394 wxPen
lightShadowPen(m_lightShadowColour
, 1, wxSOLID
);
395 wxPen
hilightPen(m_hilightColour
, 1, wxSOLID
);
396 wxPen
blackPen(wxColour(0, 0, 0), 1, wxSOLID
);
397 wxPen
whitePen(wxColour(255, 255, 255), 1, wxSOLID
);
399 if ( edge
== wxSASH_LEFT
|| edge
== wxSASH_RIGHT
)
401 int sashPosition
= 0;
402 if (edge
== wxSASH_LEFT
)
405 sashPosition
= w
- GetEdgeMargin(edge
);
408 dc
.SetBrush(faceBrush
);
409 dc
.DrawRectangle(sashPosition
, 0, GetEdgeMargin(edge
), h
);
411 if (GetWindowStyleFlag() & wxSW_3D
)
413 if (edge
== wxSASH_LEFT
)
415 // Draw a black line on the left to indicate that the
418 dc
.DrawLine(GetEdgeMargin(edge
), 0, GetEdgeMargin(edge
), h
);
422 // Draw a white line on the right to indicate that the
425 dc
.DrawLine(w
- GetEdgeMargin(edge
), 0, w
- GetEdgeMargin(edge
), h
);
429 else // top or bottom
431 int sashPosition
= 0;
432 if (edge
== wxSASH_TOP
)
435 sashPosition
= h
- GetEdgeMargin(edge
);
438 dc
.SetBrush(faceBrush
);
439 dc
.DrawRectangle(0, sashPosition
, w
, GetEdgeMargin(edge
));
441 if (GetWindowStyleFlag() & wxSW_3D
)
443 if (edge
== wxSASH_BOTTOM
)
445 // Draw a black line on the bottom to indicate that the
448 dc
.DrawLine(0, h
- GetEdgeMargin(edge
), w
, h
- GetEdgeMargin(edge
));
452 // Draw a white line on the top to indicate that the
455 dc
.DrawLine(0, GetEdgeMargin(edge
), w
, GetEdgeMargin(edge
));
460 dc
.SetPen(wxNullPen
);
461 dc
.SetBrush(wxNullBrush
);
464 // Draw the sash tracker (for whilst moving the sash)
465 void wxSashWindow::DrawSashTracker(wxSashEdgePosition edge
, int x
, int y
)
468 GetClientSize(&w
, &h
);
474 if ( edge
== wxSASH_LEFT
|| edge
== wxSASH_RIGHT
)
479 if ( (edge
== wxSASH_LEFT
) && (x1
> w
) )
483 else if ( (edge
== wxSASH_RIGHT
) && (x1
< 0) )
493 if ( (edge
== wxSASH_TOP
) && (y1
> h
) )
498 else if ( (edge
== wxSASH_BOTTOM
) && (y1
< 0) )
505 ClientToScreen(&x1
, &y1
);
506 ClientToScreen(&x2
, &y2
);
508 wxPen
sashTrackerPen(*wxBLACK
, 2, wxSOLID
);
510 screenDC
.SetLogicalFunction(wxXOR
);
511 screenDC
.SetPen(sashTrackerPen
);
512 screenDC
.SetBrush(*wxTRANSPARENT_BRUSH
);
514 screenDC
.DrawLine(x1
, y1
, x2
, y2
);
516 screenDC
.SetLogicalFunction(wxCOPY
);
518 screenDC
.SetPen(wxNullPen
);
519 screenDC
.SetBrush(wxNullBrush
);
522 // Position and size subwindows.
523 // Note that the border size applies to each subwindow, not
524 // including the edges next to the sash.
525 void wxSashWindow::SizeWindows()
528 GetClientSize(&cw
, &ch
);
530 if (GetChildren().Number() > 0)
532 wxWindow
* child
= (wxWindow
*) (GetChildren().First()->Data());
540 if (m_sashes
[0].m_show
)
543 height
-= m_borderSize
;
545 y
+= m_extraBorderSize
;
548 if (m_sashes
[3].m_show
)
551 width
-= m_borderSize
;
553 x
+= m_extraBorderSize
;
556 if (m_sashes
[1].m_show
)
558 width
-= m_borderSize
;
560 width
-= 2*m_extraBorderSize
;
563 if (m_sashes
[2].m_show
)
565 height
-= m_borderSize
;
567 height
-= 2*m_extraBorderSize
;
569 child
->SetSize(x
, y
, width
, height
);
577 // Initialize colours
578 void wxSashWindow::InitColours()
581 #if defined(__WIN95__)
582 m_faceColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
);
583 m_mediumShadowColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DSHADOW
);
584 m_darkShadowColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DDKSHADOW
);
585 m_lightShadowColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT
);
586 m_hilightColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DHILIGHT
);
588 m_faceColour
= *(wxTheColourDatabase
->FindColour("LIGHT GREY"));
589 m_mediumShadowColour
= *(wxTheColourDatabase
->FindColour("GREY"));
590 m_darkShadowColour
= *(wxTheColourDatabase
->FindColour("BLACK"));
591 m_lightShadowColour
= *(wxTheColourDatabase
->FindColour("LIGHT GREY"));
592 m_hilightColour
= *(wxTheColourDatabase
->FindColour("WHITE"));
596 void wxSashWindow::SetSashVisible(wxSashEdgePosition edge
, bool sash
)
598 m_sashes
[edge
].m_show
= sash
;
600 m_sashes
[edge
].m_margin
= m_borderSize
;
602 m_sashes
[edge
].m_margin
= 0;