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
= 0;
63 m_maximumPaneSizeY
= 0;
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
= 0;
81 m_maximumPaneSizeY
= 0;
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 dragRect
= wxRect(xp
, (yp
+ h
) - newHeight
, w
, newHeight
);
180 status
= wxSASH_STATUS_OUT_OF_RANGE
;
182 dragRect
= wxRect(xp
, yp
, w
, newHeight
);
188 status
= wxSASH_STATUS_OUT_OF_RANGE
;
189 int newWidth
= (w
- x
);
190 dragRect
= wxRect((xp
+ w
) - newWidth
, yp
, newWidth
, h
);
196 status
= wxSASH_STATUS_OUT_OF_RANGE
;
198 dragRect
= wxRect(xp
, yp
, newWidth
, h
);
207 wxSashEvent
event(GetId(), edge
);
208 event
.SetEventObject(this);
209 event
.SetDragStatus(status
);
210 event
.SetDragRect(dragRect
);
211 GetEventHandler()->ProcessEvent(event
);
213 else if (event
.Moving() && !event
.Dragging())
215 // Just change the cursor if required
216 if ( sashHit
!= wxSASH_NONE
)
218 if ( (sashHit
== wxSASH_LEFT
) || (sashHit
== wxSASH_RIGHT
) )
220 SetCursor(*m_sashCursorWE
);
224 SetCursor(*m_sashCursorNS
);
229 SetCursor(*wxSTANDARD_CURSOR
);
232 else if ( event
.Dragging() &&
233 ((m_dragMode
== wxSASH_DRAG_DRAGGING
) || (m_dragMode
== wxSASH_DRAG_LEFT_DOWN
))
236 if ( (m_draggingEdge
== wxSASH_LEFT
) || (m_draggingEdge
== wxSASH_RIGHT
) )
238 SetCursor(*m_sashCursorWE
);
242 SetCursor(*m_sashCursorNS
);
245 if (m_dragMode
== wxSASH_DRAG_LEFT_DOWN
)
247 m_dragMode
= wxSASH_DRAG_DRAGGING
;
248 DrawSashTracker(m_draggingEdge
, x
, y
);
252 if ( m_dragMode
== wxSASH_DRAG_DRAGGING
)
255 DrawSashTracker(m_draggingEdge
, m_oldX
, m_oldY
);
258 DrawSashTracker(m_draggingEdge
, x
, y
);
264 else if ( event
.LeftDClick() )
273 void wxSashWindow::OnSize(wxSizeEvent
& WXUNUSED(event
))
278 wxSashEdgePosition
wxSashWindow::SashHitTest(int x
, int y
, int WXUNUSED(tolerance
))
281 GetClientSize(& cx
, & cy
);
284 for (i
= 0; i
< 4; i
++)
286 wxSashEdge
& edge
= m_sashes
[i
];
287 wxSashEdgePosition position
= (wxSashEdgePosition
) i
;
295 if (y
>= 0 && y
<= GetEdgeMargin(position
))
301 if ((x
>= cx
- GetEdgeMargin(position
)) && (x
<= cx
))
307 if ((y
>= cy
- GetEdgeMargin(position
)) && (y
<= cy
))
308 return wxSASH_BOTTOM
;
313 if ((x
>= GetEdgeMargin(position
)) && (x
>= 0))
327 // Draw 3D effect borders
328 void wxSashWindow::DrawBorders(wxDC
& dc
)
331 GetClientSize(&w
, &h
);
333 wxPen
mediumShadowPen(m_mediumShadowColour
, 1, wxSOLID
);
334 wxPen
darkShadowPen(m_darkShadowColour
, 1, wxSOLID
);
335 wxPen
lightShadowPen(m_lightShadowColour
, 1, wxSOLID
);
336 wxPen
hilightPen(m_hilightColour
, 1, wxSOLID
);
338 if ( GetWindowStyleFlag() & wxSP_3D
)
340 dc
.SetPen(mediumShadowPen
);
341 dc
.DrawLine(0, 0, w
-1, 0);
342 dc
.DrawLine(0, 0, 0, h
- 1);
344 dc
.SetPen(darkShadowPen
);
345 dc
.DrawLine(1, 1, w
-2, 1);
346 dc
.DrawLine(1, 1, 1, h
-2);
348 dc
.SetPen(hilightPen
);
349 dc
.DrawLine(0, h
-1, w
-1, h
-1);
350 dc
.DrawLine(w
-1, 0, w
-1, h
); // Surely the maximum y pos. should be h - 1.
351 /// Anyway, h is required for MSW.
353 dc
.SetPen(lightShadowPen
);
354 dc
.DrawLine(w
-2, 1, w
-2, h
-2); // Right hand side
355 dc
.DrawLine(1, h
-2, w
-1, h
-2); // Bottom
357 else if ( GetWindowStyleFlag() & wxSP_BORDER
)
359 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
360 dc
.SetPen(*wxBLACK_PEN
);
361 dc
.DrawRectangle(0, 0, w
-1, h
-1);
364 dc
.SetPen(wxNullPen
);
365 dc
.SetBrush(wxNullBrush
);
368 void wxSashWindow::DrawSashes(wxDC
& dc
)
371 for (i
= 0; i
< 4; i
++)
372 if (m_sashes
[i
].m_show
)
373 DrawSash((wxSashEdgePosition
) i
, dc
);
377 void wxSashWindow::DrawSash(wxSashEdgePosition edge
, wxDC
& dc
)
380 GetClientSize(&w
, &h
);
382 wxPen
facePen(m_faceColour
, 1, wxSOLID
);
383 wxBrush
faceBrush(m_faceColour
, wxSOLID
);
384 wxPen
mediumShadowPen(m_mediumShadowColour
, 1, wxSOLID
);
385 wxPen
darkShadowPen(m_darkShadowColour
, 1, wxSOLID
);
386 wxPen
lightShadowPen(m_lightShadowColour
, 1, wxSOLID
);
387 wxPen
hilightPen(m_hilightColour
, 1, wxSOLID
);
388 wxPen
blackPen(wxColour(0, 0, 0), 1, wxSOLID
);
389 wxPen
whitePen(wxColour(255, 255, 255), 1, wxSOLID
);
391 if ( edge
== wxSASH_LEFT
|| edge
== wxSASH_RIGHT
)
393 int sashPosition
= 0;
394 if (edge
== wxSASH_LEFT
)
397 sashPosition
= w
- GetEdgeMargin(edge
);
400 dc
.SetBrush(faceBrush
);
401 dc
.DrawRectangle(sashPosition
, 0, GetEdgeMargin(edge
), h
);
403 if (GetWindowStyleFlag() & wxSW_3D
)
405 if (edge
== wxSASH_LEFT
)
407 // Draw a black line on the left to indicate that the
410 dc
.DrawLine(GetEdgeMargin(edge
), 0, GetEdgeMargin(edge
), h
);
414 // Draw a white line on the right to indicate that the
417 dc
.DrawLine(w
- GetEdgeMargin(edge
), 0, w
- GetEdgeMargin(edge
), h
);
421 else // top or bottom
423 int sashPosition
= 0;
424 if (edge
== wxSASH_TOP
)
427 sashPosition
= h
- GetEdgeMargin(edge
);
430 dc
.SetBrush(faceBrush
);
431 dc
.DrawRectangle(0, sashPosition
, w
, GetEdgeMargin(edge
));
433 if (GetWindowStyleFlag() & wxSW_3D
)
435 if (edge
== wxSASH_BOTTOM
)
437 // Draw a black line on the bottom to indicate that the
440 dc
.DrawLine(0, h
- GetEdgeMargin(edge
), w
, h
- GetEdgeMargin(edge
));
444 // Draw a white line on the top to indicate that the
447 dc
.DrawLine(0, GetEdgeMargin(edge
), w
, GetEdgeMargin(edge
));
452 dc
.SetPen(wxNullPen
);
453 dc
.SetBrush(wxNullBrush
);
456 // Draw the sash tracker (for whilst moving the sash)
457 void wxSashWindow::DrawSashTracker(wxSashEdgePosition edge
, int x
, int y
)
460 GetClientSize(&w
, &h
);
466 if ( edge
== wxSASH_LEFT
|| edge
== wxSASH_RIGHT
)
471 if ( (edge
== wxSASH_LEFT
) && (x1
> w
) )
475 else if ( (edge
== wxSASH_RIGHT
) && (x1
< 0) )
485 if ( (edge
== wxSASH_TOP
) && (y1
> h
) )
490 else if ( (edge
== wxSASH_BOTTOM
) && (y1
< 0) )
497 ClientToScreen(&x1
, &y1
);
498 ClientToScreen(&x2
, &y2
);
500 wxPen
sashTrackerPen(*wxBLACK
, 2, wxSOLID
);
502 screenDC
.SetLogicalFunction(wxXOR
);
503 screenDC
.SetPen(sashTrackerPen
);
504 screenDC
.SetBrush(*wxTRANSPARENT_BRUSH
);
506 screenDC
.DrawLine(x1
, y1
, x2
, y2
);
508 screenDC
.SetLogicalFunction(wxCOPY
);
510 screenDC
.SetPen(wxNullPen
);
511 screenDC
.SetBrush(wxNullBrush
);
514 // Position and size subwindows.
515 // Note that the border size applies to each subwindow, not
516 // including the edges next to the sash.
517 void wxSashWindow::SizeWindows()
520 GetClientSize(&cw
, &ch
);
522 if (GetChildren()->Number() > 0)
524 wxWindow
* child
= (wxWindow
*) (GetChildren()->First()->Data());
532 if (m_sashes
[0].m_show
)
535 height
-= m_borderSize
;
537 y
+= m_extraBorderSize
;
540 if (m_sashes
[3].m_show
)
543 width
-= m_borderSize
;
545 x
+= m_extraBorderSize
;
548 if (m_sashes
[1].m_show
)
550 width
-= m_borderSize
;
552 width
-= 2*m_extraBorderSize
;
555 if (m_sashes
[2].m_show
)
557 height
-= m_borderSize
;
559 height
-= 2*m_extraBorderSize
;
561 child
->SetSize(x
, y
, width
, height
);
569 // Initialize colours
570 void wxSashWindow::InitColours()
573 #if defined(__WIN95__)
574 m_faceColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
);
575 m_mediumShadowColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DSHADOW
);
576 m_darkShadowColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DDKSHADOW
);
577 m_lightShadowColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT
);
578 m_hilightColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DHILIGHT
);
580 m_faceColour
= *(wxTheColourDatabase
->FindColour("LIGHT GREY"));
581 m_mediumShadowColour
= *(wxTheColourDatabase
->FindColour("GREY"));
582 m_darkShadowColour
= *(wxTheColourDatabase
->FindColour("BLACK"));
583 m_lightShadowColour
= *(wxTheColourDatabase
->FindColour("LIGHT GREY"));
584 m_hilightColour
= *(wxTheColourDatabase
->FindColour("WHITE"));
588 void wxSashWindow::SetSashVisible(wxSashEdgePosition edge
, bool sash
)
590 m_sashes
[edge
].m_show
= sash
;
592 m_sashes
[edge
].m_margin
= m_borderSize
;
594 m_sashes
[edge
].m_margin
= 0;