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"
34 #include "wx/string.h"
35 #include "wx/dcscreen.h"
36 #include "wx/sashwin.h"
37 #include "wx/laywin.h"
39 IMPLEMENT_DYNAMIC_CLASS(wxSashWindow
, wxWindow
)
40 IMPLEMENT_DYNAMIC_CLASS(wxSashEvent
, wxCommandEvent
)
42 BEGIN_EVENT_TABLE(wxSashWindow
, wxWindow
)
43 EVT_PAINT(wxSashWindow::OnPaint
)
44 EVT_SIZE(wxSashWindow::OnSize
)
45 EVT_MOUSE_EVENTS(wxSashWindow::OnMouseEvent
)
48 bool wxSashWindow::Create(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
,
49 const wxSize
& size
, long style
, const wxString
& name
)
51 return wxWindow::Create(parent
, id
, pos
, size
, style
, name
);
54 wxSashWindow::~wxSashWindow()
56 delete m_sashCursorWE
;
57 delete m_sashCursorNS
;
60 void wxSashWindow::Init()
62 m_draggingEdge
= wxSASH_NONE
;
63 m_dragMode
= wxSASH_DRAG_NONE
;
69 m_extraBorderSize
= 0;
70 m_minimumPaneSizeX
= 0;
71 m_minimumPaneSizeY
= 0;
72 m_maximumPaneSizeX
= 10000;
73 m_maximumPaneSizeY
= 10000;
74 m_sashCursorWE
= new wxCursor(wxCURSOR_SIZEWE
);
75 m_sashCursorNS
= new wxCursor(wxCURSOR_SIZENS
);
76 m_mouseCaptured
= FALSE
;
78 // Eventually, we'll respond to colour change messages
82 void wxSashWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
86 // if ( m_borderSize > 0 )
92 void wxSashWindow::OnMouseEvent(wxMouseEvent
& event
)
95 event
.GetPosition(&x
, &y
);
97 wxSashEdgePosition sashHit
= SashHitTest(x
, y
);
100 #if defined(__WXMOTIF__) || defined(__WXGTK__)
101 // Not necessary and in fact inhibits proper cursor setting (JACS 8/2000)
102 //SetCursor(* wxSTANDARD_CURSOR);
105 SetCursor(wxNullCursor
);
108 if (event
.LeftDown())
111 m_mouseCaptured
= TRUE
;
113 if ( sashHit
!= wxSASH_NONE
)
115 // Required for X to specify that
116 // that we wish to draw on top of all windows
117 // - and we optimise by specifying the area
118 // for creating the overlap window.
119 // Find the first frame or dialog and use this to specify
120 // the area to draw on.
121 wxWindow
* parent
= this;
123 while (parent
&& !parent
->IsKindOf(CLASSINFO(wxDialog
)) &&
124 !parent
->IsKindOf(CLASSINFO(wxFrame
)))
125 parent
= parent
->GetParent();
127 wxScreenDC::StartDrawingOnTop(parent
);
129 // We don't say we're dragging yet; we leave that
130 // decision for the Dragging() branch, to ensure
131 // the user has dragged a little bit.
132 m_dragMode
= wxSASH_DRAG_LEFT_DOWN
;
133 m_draggingEdge
= sashHit
;
137 if ( (sashHit
== wxSASH_LEFT
) || (sashHit
== wxSASH_RIGHT
) )
139 SetCursor(*m_sashCursorWE
);
143 SetCursor(*m_sashCursorNS
);
147 else if ( event
.LeftUp() && m_dragMode
== wxSASH_DRAG_LEFT_DOWN
)
149 // Wasn't a proper drag
152 m_mouseCaptured
= FALSE
;
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 m_mouseCaptured
= FALSE
;
167 DrawSashTracker(m_draggingEdge
, m_oldX
, m_oldY
);
169 // End drawing on top (frees the window used for drawing
171 wxScreenDC::EndDrawingOnTop();
176 GetPosition(&xp
, &yp
);
178 wxSashEdgePosition edge
= m_draggingEdge
;
179 m_draggingEdge
= wxSASH_NONE
;
182 wxSashDragStatus status
= wxSASH_STATUS_OK
;
184 // the new height and width of the window - if -1, it didn't change
188 // NB: x and y may be negative and they're relative to the sash window
189 // upper left corner, while xp and yp are expressed in the parent
190 // window system of coordinates, so adjust them! After this
191 // adjustment, all coordinates are relative to the parent window.
200 // top sash shouldn't get below the bottom one
201 status
= wxSASH_STATUS_OUT_OF_RANGE
;
205 newHeight
= h
- (y
- yp
);
212 // bottom sash shouldn't get above the top one
213 status
= wxSASH_STATUS_OUT_OF_RANGE
;
224 // left sash shouldn't get beyond the right one
225 status
= wxSASH_STATUS_OUT_OF_RANGE
;
229 newWidth
= w
- (x
- xp
);
236 // and the right sash, finally, shouldn't be beyond the
238 status
= wxSASH_STATUS_OUT_OF_RANGE
;
247 // can this happen at all?
251 if ( newHeight
== -1 )
258 // make sure it's in m_minimumPaneSizeY..m_maximumPaneSizeY range
259 newHeight
= wxMax(newHeight
, m_minimumPaneSizeY
);
260 newHeight
= wxMin(newHeight
, m_maximumPaneSizeY
);
263 if ( newWidth
== -1 )
270 // make sure it's in m_minimumPaneSizeY..m_maximumPaneSizeY range
271 newWidth
= wxMax(newWidth
, m_minimumPaneSizeX
);
272 newWidth
= wxMin(newWidth
, m_maximumPaneSizeX
);
275 dragRect
= wxRect(x
, y
, newWidth
, newHeight
);
277 wxSashEvent
event(GetId(), edge
);
278 event
.SetEventObject(this);
279 event
.SetDragStatus(status
);
280 event
.SetDragRect(dragRect
);
281 GetEventHandler()->ProcessEvent(event
);
283 else if ( event
.LeftUp() )
287 m_mouseCaptured
= FALSE
;
289 else if (event
.Moving() && !event
.Dragging())
291 // Just change the cursor if required
292 if ( sashHit
!= wxSASH_NONE
)
294 if ( (sashHit
== wxSASH_LEFT
) || (sashHit
== wxSASH_RIGHT
) )
296 SetCursor(*m_sashCursorWE
);
300 SetCursor(*m_sashCursorNS
);
305 SetCursor(wxNullCursor
);
308 else if ( event
.Dragging() &&
309 ((m_dragMode
== wxSASH_DRAG_DRAGGING
) ||
310 (m_dragMode
== wxSASH_DRAG_LEFT_DOWN
)) )
312 if ( (m_draggingEdge
== wxSASH_LEFT
) || (m_draggingEdge
== wxSASH_RIGHT
) )
314 SetCursor(*m_sashCursorWE
);
318 SetCursor(*m_sashCursorNS
);
321 if (m_dragMode
== wxSASH_DRAG_LEFT_DOWN
)
323 m_dragMode
= wxSASH_DRAG_DRAGGING
;
324 DrawSashTracker(m_draggingEdge
, x
, y
);
328 if ( m_dragMode
== wxSASH_DRAG_DRAGGING
)
331 DrawSashTracker(m_draggingEdge
, m_oldX
, m_oldY
);
334 DrawSashTracker(m_draggingEdge
, x
, y
);
340 else if ( event
.LeftDClick() )
349 void wxSashWindow::OnSize(wxSizeEvent
& WXUNUSED(event
))
354 wxSashEdgePosition
wxSashWindow::SashHitTest(int x
, int y
, int WXUNUSED(tolerance
))
357 GetClientSize(& cx
, & cy
);
360 for (i
= 0; i
< 4; i
++)
362 wxSashEdge
& edge
= m_sashes
[i
];
363 wxSashEdgePosition position
= (wxSashEdgePosition
) i
;
371 if (y
>= 0 && y
<= GetEdgeMargin(position
))
377 if ((x
>= cx
- GetEdgeMargin(position
)) && (x
<= cx
))
383 if ((y
>= cy
- GetEdgeMargin(position
)) && (y
<= cy
))
384 return wxSASH_BOTTOM
;
389 if ((x
<= GetEdgeMargin(position
)) && (x
>= 0))
403 // Draw 3D effect borders
404 void wxSashWindow::DrawBorders(wxDC
& dc
)
407 GetClientSize(&w
, &h
);
409 wxPen
mediumShadowPen(m_mediumShadowColour
, 1, wxSOLID
);
410 wxPen
darkShadowPen(m_darkShadowColour
, 1, wxSOLID
);
411 wxPen
lightShadowPen(m_lightShadowColour
, 1, wxSOLID
);
412 wxPen
hilightPen(m_hilightColour
, 1, wxSOLID
);
414 if ( GetWindowStyleFlag() & wxSW_3DBORDER
)
416 dc
.SetPen(mediumShadowPen
);
417 dc
.DrawLine(0, 0, w
-1, 0);
418 dc
.DrawLine(0, 0, 0, h
- 1);
420 dc
.SetPen(darkShadowPen
);
421 dc
.DrawLine(1, 1, w
-2, 1);
422 dc
.DrawLine(1, 1, 1, h
-2);
424 dc
.SetPen(hilightPen
);
425 dc
.DrawLine(0, h
-1, w
-1, h
-1);
426 dc
.DrawLine(w
-1, 0, w
-1, h
); // Surely the maximum y pos. should be h - 1.
427 /// Anyway, h is required for MSW.
429 dc
.SetPen(lightShadowPen
);
430 dc
.DrawLine(w
-2, 1, w
-2, h
-2); // Right hand side
431 dc
.DrawLine(1, h
-2, w
-1, h
-2); // Bottom
433 else if ( GetWindowStyleFlag() & wxSW_BORDER
)
435 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
436 dc
.SetPen(*wxBLACK_PEN
);
437 dc
.DrawRectangle(0, 0, w
-1, h
-1);
440 dc
.SetPen(wxNullPen
);
441 dc
.SetBrush(wxNullBrush
);
444 void wxSashWindow::DrawSashes(wxDC
& dc
)
447 for (i
= 0; i
< 4; i
++)
448 if (m_sashes
[i
].m_show
)
449 DrawSash((wxSashEdgePosition
) i
, dc
);
453 void wxSashWindow::DrawSash(wxSashEdgePosition edge
, wxDC
& dc
)
456 GetClientSize(&w
, &h
);
458 wxPen
facePen(m_faceColour
, 1, wxSOLID
);
459 wxBrush
faceBrush(m_faceColour
, wxSOLID
);
460 wxPen
mediumShadowPen(m_mediumShadowColour
, 1, wxSOLID
);
461 wxPen
darkShadowPen(m_darkShadowColour
, 1, wxSOLID
);
462 wxPen
lightShadowPen(m_lightShadowColour
, 1, wxSOLID
);
463 wxPen
hilightPen(m_hilightColour
, 1, wxSOLID
);
464 wxPen
blackPen(wxColour(0, 0, 0), 1, wxSOLID
);
465 wxPen
whitePen(wxColour(255, 255, 255), 1, wxSOLID
);
467 if ( edge
== wxSASH_LEFT
|| edge
== wxSASH_RIGHT
)
469 int sashPosition
= 0;
470 if (edge
== wxSASH_LEFT
)
473 sashPosition
= w
- GetEdgeMargin(edge
);
476 dc
.SetBrush(faceBrush
);
477 dc
.DrawRectangle(sashPosition
, 0, GetEdgeMargin(edge
), h
);
479 if (GetWindowStyleFlag() & wxSW_3DSASH
)
481 if (edge
== wxSASH_LEFT
)
483 // Draw a dark grey line on the left to indicate that the
485 dc
.SetPen(mediumShadowPen
);
486 dc
.DrawLine(GetEdgeMargin(edge
), 0, GetEdgeMargin(edge
), h
);
490 // Draw a highlight line on the right to indicate that the
492 dc
.SetPen(hilightPen
);
493 dc
.DrawLine(w
- GetEdgeMargin(edge
), 0, w
- GetEdgeMargin(edge
), h
);
497 else // top or bottom
499 int sashPosition
= 0;
500 if (edge
== wxSASH_TOP
)
503 sashPosition
= h
- GetEdgeMargin(edge
);
506 dc
.SetBrush(faceBrush
);
507 dc
.DrawRectangle(0, sashPosition
, w
, GetEdgeMargin(edge
));
509 if (GetWindowStyleFlag() & wxSW_3DSASH
)
511 if (edge
== wxSASH_BOTTOM
)
513 // Draw a highlight line on the bottom to indicate that the
515 dc
.SetPen(hilightPen
);
516 dc
.DrawLine(0, h
- GetEdgeMargin(edge
), w
, h
- GetEdgeMargin(edge
));
520 // Draw a drak grey line on the top to indicate that the
522 dc
.SetPen(mediumShadowPen
);
523 dc
.DrawLine(1, GetEdgeMargin(edge
), w
-1, GetEdgeMargin(edge
));
528 dc
.SetPen(wxNullPen
);
529 dc
.SetBrush(wxNullBrush
);
532 // Draw the sash tracker (for whilst moving the sash)
533 void wxSashWindow::DrawSashTracker(wxSashEdgePosition edge
, int x
, int y
)
536 GetClientSize(&w
, &h
);
542 if ( edge
== wxSASH_LEFT
|| edge
== wxSASH_RIGHT
)
547 if ( (edge
== wxSASH_LEFT
) && (x1
> w
) )
551 else if ( (edge
== wxSASH_RIGHT
) && (x1
< 0) )
561 if ( (edge
== wxSASH_TOP
) && (y1
> h
) )
566 else if ( (edge
== wxSASH_BOTTOM
) && (y1
< 0) )
573 ClientToScreen(&x1
, &y1
);
574 ClientToScreen(&x2
, &y2
);
576 wxPen
sashTrackerPen(*wxBLACK
, 2, wxSOLID
);
578 screenDC
.SetLogicalFunction(wxINVERT
);
579 screenDC
.SetPen(sashTrackerPen
);
580 screenDC
.SetBrush(*wxTRANSPARENT_BRUSH
);
582 screenDC
.DrawLine(x1
, y1
, x2
, y2
);
584 screenDC
.SetLogicalFunction(wxCOPY
);
586 screenDC
.SetPen(wxNullPen
);
587 screenDC
.SetBrush(wxNullBrush
);
590 // Position and size subwindows.
591 // Note that the border size applies to each subwindow, not
592 // including the edges next to the sash.
593 void wxSashWindow::SizeWindows()
596 GetClientSize(&cw
, &ch
);
598 if (GetChildren().Number() == 1)
600 wxWindow
* child
= (wxWindow
*) (GetChildren().First()->Data());
608 if (m_sashes
[0].m_show
)
611 height
-= m_borderSize
;
613 y
+= m_extraBorderSize
;
616 if (m_sashes
[3].m_show
)
619 width
-= m_borderSize
;
621 x
+= m_extraBorderSize
;
624 if (m_sashes
[1].m_show
)
626 width
-= m_borderSize
;
628 width
-= 2*m_extraBorderSize
;
631 if (m_sashes
[2].m_show
)
633 height
-= m_borderSize
;
635 height
-= 2*m_extraBorderSize
;
637 child
->SetSize(x
, y
, width
, height
);
639 else if (GetChildren().Number() > 1)
641 // Perhaps multiple children are themselves sash windows.
642 // TODO: this doesn't really work because the subwindows sizes/positions
643 // must be set to leave a gap for the parent's sash (hit-test and decorations).
644 // Perhaps we can allow for this within LayoutWindow, testing whether the parent
645 // is a sash window, and if so, allowing some space for the edges.
646 wxLayoutAlgorithm layout
;
647 layout
.LayoutWindow(this);
655 // Initialize colours
656 void wxSashWindow::InitColours()
660 m_faceColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
);
661 m_mediumShadowColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DSHADOW
);
662 m_darkShadowColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DDKSHADOW
);
663 m_lightShadowColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT
);
664 m_hilightColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DHILIGHT
);
666 m_faceColour
= *(wxTheColourDatabase
->FindColour("LIGHT GREY"));
667 m_mediumShadowColour
= *(wxTheColourDatabase
->FindColour("GREY"));
668 m_darkShadowColour
= *(wxTheColourDatabase
->FindColour("BLACK"));
669 m_lightShadowColour
= *(wxTheColourDatabase
->FindColour("LIGHT GREY"));
670 m_hilightColour
= *(wxTheColourDatabase
->FindColour("WHITE"));
674 void wxSashWindow::SetSashVisible(wxSashEdgePosition edge
, bool sash
)
676 m_sashes
[edge
].m_show
= sash
;
678 m_sashes
[edge
].m_margin
= m_borderSize
;
680 m_sashes
[edge
].m_margin
= 0;