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
))
99 // if ( m_borderSize > 0 )
105 void wxSashWindow::OnMouseEvent(wxMouseEvent
& event
)
108 event
.Position(&x
, &y
);
110 wxSashEdgePosition sashHit
= SashHitTest(x
, y
);
112 if (event
.LeftDown())
114 if ( sashHit
!= wxSASH_NONE
)
118 // Required for X to specify that
119 // that we wish to draw on top of all windows
120 // - and we optimise by specifying the area
121 // for creating the overlap window.
122 // Find the first frame or dialog and use this to specify
123 // the area to draw on.
124 wxWindow
* parent
= this;
126 while (parent
&& !parent
->IsKindOf(CLASSINFO(wxDialog
)) &&
127 !parent
->IsKindOf(CLASSINFO(wxFrame
)))
128 parent
= parent
->GetParent();
130 wxScreenDC::StartDrawingOnTop(parent
);
132 // We don't say we're dragging yet; we leave that
133 // decision for the Dragging() branch, to ensure
134 // the user has dragged a little bit.
135 m_dragMode
= wxSASH_DRAG_LEFT_DOWN
;
136 m_draggingEdge
= sashHit
;
141 else if ( event
.LeftUp() && m_dragMode
== wxSASH_DRAG_LEFT_DOWN
)
143 // Wasn't a proper drag
145 wxScreenDC::EndDrawingOnTop();
146 m_dragMode
= wxSASH_DRAG_NONE
;
147 m_draggingEdge
= wxSASH_NONE
;
149 SetCursor(*wxSTANDARD_CURSOR
);
151 else if (event
.LeftUp() && m_dragMode
== wxSASH_DRAG_DRAGGING
)
153 // We can stop dragging now and see what we've got.
154 m_dragMode
= wxSASH_DRAG_NONE
;
157 DrawSashTracker(m_draggingEdge
, m_oldX
, m_oldY
);
159 // End drawing on top (frees the window used for drawing
161 wxScreenDC::EndDrawingOnTop();
166 GetPosition(&xp
, &yp
);
168 wxSashEdgePosition edge
= m_draggingEdge
;
169 m_draggingEdge
= wxSASH_NONE
;
172 wxSashDragStatus status
= wxSASH_STATUS_OK
;
178 status
= wxSASH_STATUS_OUT_OF_RANGE
;
179 int newHeight
= (h
- y
);
180 newHeight
=wxMax(newHeight
,m_minimumPaneSizeY
);
181 newHeight
=wxMin(newHeight
,m_maximumPaneSizeY
);
182 dragRect
= wxRect(xp
, (yp
+ h
) - newHeight
, w
, newHeight
);
188 status
= wxSASH_STATUS_OUT_OF_RANGE
;
190 newHeight
=wxMax(newHeight
,m_minimumPaneSizeY
);
191 newHeight
=wxMin(newHeight
,m_maximumPaneSizeY
);
192 dragRect
= wxRect(xp
, yp
, w
, newHeight
);
198 status
= wxSASH_STATUS_OUT_OF_RANGE
;
199 int newWidth
= (w
- x
);
200 newWidth
=wxMax(newWidth
,m_minimumPaneSizeX
);
201 newWidth
=wxMin(newWidth
,m_maximumPaneSizeX
);
202 dragRect
= wxRect((xp
+ w
) - newWidth
, yp
, newWidth
, h
);
208 status
= wxSASH_STATUS_OUT_OF_RANGE
;
210 newWidth
=wxMax(newWidth
,m_minimumPaneSizeX
);
211 newWidth
=wxMin(newWidth
,m_maximumPaneSizeX
);
212 dragRect
= wxRect(xp
, yp
, newWidth
, h
);
221 wxSashEvent
event(GetId(), edge
);
222 event
.SetEventObject(this);
223 event
.SetDragStatus(status
);
224 event
.SetDragRect(dragRect
);
225 GetEventHandler()->ProcessEvent(event
);
227 else if (event
.Moving() && !event
.Dragging())
229 // Just change the cursor if required
230 if ( sashHit
!= wxSASH_NONE
)
232 if ( (sashHit
== wxSASH_LEFT
) || (sashHit
== wxSASH_RIGHT
) )
234 SetCursor(*m_sashCursorWE
);
238 SetCursor(*m_sashCursorNS
);
243 SetCursor(*wxSTANDARD_CURSOR
);
246 else if ( event
.Dragging() &&
247 ((m_dragMode
== wxSASH_DRAG_DRAGGING
) || (m_dragMode
== wxSASH_DRAG_LEFT_DOWN
))
250 if ( (m_draggingEdge
== wxSASH_LEFT
) || (m_draggingEdge
== wxSASH_RIGHT
) )
252 SetCursor(*m_sashCursorWE
);
256 SetCursor(*m_sashCursorNS
);
259 if (m_dragMode
== wxSASH_DRAG_LEFT_DOWN
)
261 m_dragMode
= wxSASH_DRAG_DRAGGING
;
262 DrawSashTracker(m_draggingEdge
, x
, y
);
266 if ( m_dragMode
== wxSASH_DRAG_DRAGGING
)
269 DrawSashTracker(m_draggingEdge
, m_oldX
, m_oldY
);
272 DrawSashTracker(m_draggingEdge
, x
, y
);
278 else if ( event
.LeftDClick() )
287 void wxSashWindow::OnSize(wxSizeEvent
& WXUNUSED(event
))
292 wxSashEdgePosition
wxSashWindow::SashHitTest(int x
, int y
, int WXUNUSED(tolerance
))
295 GetClientSize(& cx
, & cy
);
298 for (i
= 0; i
< 4; i
++)
300 wxSashEdge
& edge
= m_sashes
[i
];
301 wxSashEdgePosition position
= (wxSashEdgePosition
) i
;
309 if (y
>= 0 && y
<= GetEdgeMargin(position
))
315 if ((x
>= cx
- GetEdgeMargin(position
)) && (x
<= cx
))
321 if ((y
>= cy
- GetEdgeMargin(position
)) && (y
<= cy
))
322 return wxSASH_BOTTOM
;
327 if ((x
>= GetEdgeMargin(position
)) && (x
>= 0))
341 // Draw 3D effect borders
342 void wxSashWindow::DrawBorders(wxDC
& dc
)
345 GetClientSize(&w
, &h
);
347 wxPen
mediumShadowPen(m_mediumShadowColour
, 1, wxSOLID
);
348 wxPen
darkShadowPen(m_darkShadowColour
, 1, wxSOLID
);
349 wxPen
lightShadowPen(m_lightShadowColour
, 1, wxSOLID
);
350 wxPen
hilightPen(m_hilightColour
, 1, wxSOLID
);
352 if ( GetWindowStyleFlag() & wxSP_3D
)
354 dc
.SetPen(mediumShadowPen
);
355 dc
.DrawLine(0, 0, w
-1, 0);
356 dc
.DrawLine(0, 0, 0, h
- 1);
358 dc
.SetPen(darkShadowPen
);
359 dc
.DrawLine(1, 1, w
-2, 1);
360 dc
.DrawLine(1, 1, 1, h
-2);
362 dc
.SetPen(hilightPen
);
363 dc
.DrawLine(0, h
-1, w
-1, h
-1);
364 dc
.DrawLine(w
-1, 0, w
-1, h
); // Surely the maximum y pos. should be h - 1.
365 /// Anyway, h is required for MSW.
367 dc
.SetPen(lightShadowPen
);
368 dc
.DrawLine(w
-2, 1, w
-2, h
-2); // Right hand side
369 dc
.DrawLine(1, h
-2, w
-1, h
-2); // Bottom
371 else if ( GetWindowStyleFlag() & wxSP_BORDER
)
373 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
374 dc
.SetPen(*wxBLACK_PEN
);
375 dc
.DrawRectangle(0, 0, w
-1, h
-1);
378 dc
.SetPen(wxNullPen
);
379 dc
.SetBrush(wxNullBrush
);
382 void wxSashWindow::DrawSashes(wxDC
& dc
)
385 for (i
= 0; i
< 4; i
++)
386 if (m_sashes
[i
].m_show
)
387 DrawSash((wxSashEdgePosition
) i
, dc
);
391 void wxSashWindow::DrawSash(wxSashEdgePosition edge
, wxDC
& dc
)
394 GetClientSize(&w
, &h
);
396 wxPen
facePen(m_faceColour
, 1, wxSOLID
);
397 wxBrush
faceBrush(m_faceColour
, wxSOLID
);
398 wxPen
mediumShadowPen(m_mediumShadowColour
, 1, wxSOLID
);
399 wxPen
darkShadowPen(m_darkShadowColour
, 1, wxSOLID
);
400 wxPen
lightShadowPen(m_lightShadowColour
, 1, wxSOLID
);
401 wxPen
hilightPen(m_hilightColour
, 1, wxSOLID
);
402 wxPen
blackPen(wxColour(0, 0, 0), 1, wxSOLID
);
403 wxPen
whitePen(wxColour(255, 255, 255), 1, wxSOLID
);
405 if ( edge
== wxSASH_LEFT
|| edge
== wxSASH_RIGHT
)
407 int sashPosition
= 0;
408 if (edge
== wxSASH_LEFT
)
411 sashPosition
= w
- GetEdgeMargin(edge
);
414 dc
.SetBrush(faceBrush
);
415 dc
.DrawRectangle(sashPosition
, 0, GetEdgeMargin(edge
), h
);
417 if (GetWindowStyleFlag() & wxSW_3D
)
419 if (edge
== wxSASH_LEFT
)
421 // Draw a black line on the left to indicate that the
424 dc
.DrawLine(GetEdgeMargin(edge
), 0, GetEdgeMargin(edge
), h
);
428 // Draw a white line on the right to indicate that the
431 dc
.DrawLine(w
- GetEdgeMargin(edge
), 0, w
- GetEdgeMargin(edge
), h
);
435 else // top or bottom
437 int sashPosition
= 0;
438 if (edge
== wxSASH_TOP
)
441 sashPosition
= h
- GetEdgeMargin(edge
);
444 dc
.SetBrush(faceBrush
);
445 dc
.DrawRectangle(0, sashPosition
, w
, GetEdgeMargin(edge
));
447 if (GetWindowStyleFlag() & wxSW_3D
)
449 if (edge
== wxSASH_BOTTOM
)
451 // Draw a black line on the bottom to indicate that the
454 dc
.DrawLine(0, h
- GetEdgeMargin(edge
), w
, h
- GetEdgeMargin(edge
));
458 // Draw a white line on the top to indicate that the
461 dc
.DrawLine(0, GetEdgeMargin(edge
), w
, GetEdgeMargin(edge
));
466 dc
.SetPen(wxNullPen
);
467 dc
.SetBrush(wxNullBrush
);
470 // Draw the sash tracker (for whilst moving the sash)
471 void wxSashWindow::DrawSashTracker(wxSashEdgePosition edge
, int x
, int y
)
474 GetClientSize(&w
, &h
);
480 if ( edge
== wxSASH_LEFT
|| edge
== wxSASH_RIGHT
)
485 if ( (edge
== wxSASH_LEFT
) && (x1
> w
) )
489 else if ( (edge
== wxSASH_RIGHT
) && (x1
< 0) )
499 if ( (edge
== wxSASH_TOP
) && (y1
> h
) )
504 else if ( (edge
== wxSASH_BOTTOM
) && (y1
< 0) )
511 ClientToScreen(&x1
, &y1
);
512 ClientToScreen(&x2
, &y2
);
514 wxPen
sashTrackerPen(*wxBLACK
, 2, wxSOLID
);
516 screenDC
.SetLogicalFunction(wxXOR
);
517 screenDC
.SetPen(sashTrackerPen
);
518 screenDC
.SetBrush(*wxTRANSPARENT_BRUSH
);
520 screenDC
.DrawLine(x1
, y1
, x2
, y2
);
522 screenDC
.SetLogicalFunction(wxCOPY
);
524 screenDC
.SetPen(wxNullPen
);
525 screenDC
.SetBrush(wxNullBrush
);
528 // Position and size subwindows.
529 // Note that the border size applies to each subwindow, not
530 // including the edges next to the sash.
531 void wxSashWindow::SizeWindows()
534 GetClientSize(&cw
, &ch
);
536 if (GetChildren().Number() > 0)
538 wxWindow
* child
= (wxWindow
*) (GetChildren().First()->Data());
546 if (m_sashes
[0].m_show
)
549 height
-= m_borderSize
;
551 y
+= m_extraBorderSize
;
554 if (m_sashes
[3].m_show
)
557 width
-= m_borderSize
;
559 x
+= m_extraBorderSize
;
562 if (m_sashes
[1].m_show
)
564 width
-= m_borderSize
;
566 width
-= 2*m_extraBorderSize
;
569 if (m_sashes
[2].m_show
)
571 height
-= m_borderSize
;
573 height
-= 2*m_extraBorderSize
;
575 child
->SetSize(x
, y
, width
, height
);
583 // Initialize colours
584 void wxSashWindow::InitColours()
587 #if defined(__WIN95__)
588 m_faceColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
);
589 m_mediumShadowColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DSHADOW
);
590 m_darkShadowColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DDKSHADOW
);
591 m_lightShadowColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT
);
592 m_hilightColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DHILIGHT
);
594 m_faceColour
= *(wxTheColourDatabase
->FindColour("LIGHT GREY"));
595 m_mediumShadowColour
= *(wxTheColourDatabase
->FindColour("GREY"));
596 m_darkShadowColour
= *(wxTheColourDatabase
->FindColour("BLACK"));
597 m_lightShadowColour
= *(wxTheColourDatabase
->FindColour("LIGHT GREY"));
598 m_hilightColour
= *(wxTheColourDatabase
->FindColour("WHITE"));
602 void wxSashWindow::SetSashVisible(wxSashEdgePosition edge
, bool sash
)
604 m_sashes
[edge
].m_show
= sash
;
606 m_sashes
[edge
].m_margin
= m_borderSize
;
608 m_sashes
[edge
].m_margin
= 0;