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
);
77 // Eventually, we'll respond to colour change messages
81 void wxSashWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
85 // if ( m_borderSize > 0 )
91 void wxSashWindow::OnMouseEvent(wxMouseEvent
& event
)
94 event
.GetPosition(&x
, &y
);
96 wxSashEdgePosition sashHit
= SashHitTest(x
, y
);
99 #if defined(__WXMOTIF__) || defined(__WXGTK__)
100 SetCursor(* wxSTANDARD_CURSOR
);
103 SetCursor(wxNullCursor
);
106 if (event
.LeftDown())
110 if ( sashHit
!= wxSASH_NONE
)
112 // Required for X to specify that
113 // that we wish to draw on top of all windows
114 // - and we optimise by specifying the area
115 // for creating the overlap window.
116 // Find the first frame or dialog and use this to specify
117 // the area to draw on.
118 wxWindow
* parent
= this;
120 while (parent
&& !parent
->IsKindOf(CLASSINFO(wxDialog
)) &&
121 !parent
->IsKindOf(CLASSINFO(wxFrame
)))
122 parent
= parent
->GetParent();
124 wxScreenDC::StartDrawingOnTop(parent
);
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
;
134 if ( (sashHit
== wxSASH_LEFT
) || (sashHit
== wxSASH_RIGHT
) )
136 SetCursor(*m_sashCursorWE
);
140 SetCursor(*m_sashCursorNS
);
144 else if ( event
.LeftUp() && m_dragMode
== wxSASH_DRAG_LEFT_DOWN
)
146 // Wasn't a proper drag
148 wxScreenDC::EndDrawingOnTop();
149 m_dragMode
= wxSASH_DRAG_NONE
;
150 m_draggingEdge
= wxSASH_NONE
;
152 else if (event
.LeftUp() && m_dragMode
== wxSASH_DRAG_DRAGGING
)
154 // We can stop dragging now and see what we've got.
155 m_dragMode
= wxSASH_DRAG_NONE
;
158 DrawSashTracker(m_draggingEdge
, m_oldX
, m_oldY
);
160 // End drawing on top (frees the window used for drawing
162 wxScreenDC::EndDrawingOnTop();
167 GetPosition(&xp
, &yp
);
169 wxSashEdgePosition edge
= m_draggingEdge
;
170 m_draggingEdge
= wxSASH_NONE
;
173 wxSashDragStatus status
= wxSASH_STATUS_OK
;
175 // the new height and width of the window - if -1, it didn't change
179 // NB: x and y may be negative and they're relative to the sash window
180 // upper left corner, while xp and yp are expressed in the parent
181 // window system of coordinates, so adjust them! After this
182 // adjustment, all coordinates are relative to the parent window.
191 // top sash shouldn't get below the bottom one
192 status
= wxSASH_STATUS_OUT_OF_RANGE
;
196 newHeight
= h
- (y
- yp
);
203 // bottom sash shouldn't get above the top one
204 status
= wxSASH_STATUS_OUT_OF_RANGE
;
215 // left sash shouldn't get beyond the right one
216 status
= wxSASH_STATUS_OUT_OF_RANGE
;
220 newWidth
= w
- (x
- xp
);
227 // and the right sash, finally, shouldn't be beyond the
229 status
= wxSASH_STATUS_OUT_OF_RANGE
;
238 // can this happen at all?
242 if ( newHeight
== -1 )
249 // make sure it's in m_minimumPaneSizeY..m_maximumPaneSizeY range
250 newHeight
= wxMax(newHeight
, m_minimumPaneSizeY
);
251 newHeight
= wxMin(newHeight
, m_maximumPaneSizeY
);
254 if ( newWidth
== -1 )
261 // make sure it's in m_minimumPaneSizeY..m_maximumPaneSizeY range
262 newWidth
= wxMax(newWidth
, m_minimumPaneSizeX
);
263 newWidth
= wxMin(newWidth
, m_maximumPaneSizeX
);
266 dragRect
= wxRect(x
, y
, newWidth
, newHeight
);
268 wxSashEvent
event(GetId(), edge
);
269 event
.SetEventObject(this);
270 event
.SetDragStatus(status
);
271 event
.SetDragRect(dragRect
);
272 GetEventHandler()->ProcessEvent(event
);
274 else if ( event
.LeftUp() )
278 else if (event
.Moving() && !event
.Dragging())
280 // Just change the cursor if required
281 if ( sashHit
!= wxSASH_NONE
)
283 if ( (sashHit
== wxSASH_LEFT
) || (sashHit
== wxSASH_RIGHT
) )
285 SetCursor(*m_sashCursorWE
);
289 SetCursor(*m_sashCursorNS
);
294 SetCursor(wxNullCursor
);
297 else if ( event
.Dragging() &&
298 ((m_dragMode
== wxSASH_DRAG_DRAGGING
) ||
299 (m_dragMode
== wxSASH_DRAG_LEFT_DOWN
)) )
301 if ( (m_draggingEdge
== wxSASH_LEFT
) || (m_draggingEdge
== wxSASH_RIGHT
) )
303 SetCursor(*m_sashCursorWE
);
307 SetCursor(*m_sashCursorNS
);
310 if (m_dragMode
== wxSASH_DRAG_LEFT_DOWN
)
312 m_dragMode
= wxSASH_DRAG_DRAGGING
;
313 DrawSashTracker(m_draggingEdge
, x
, y
);
317 if ( m_dragMode
== wxSASH_DRAG_DRAGGING
)
320 DrawSashTracker(m_draggingEdge
, m_oldX
, m_oldY
);
323 DrawSashTracker(m_draggingEdge
, x
, y
);
329 else if ( event
.LeftDClick() )
338 void wxSashWindow::OnSize(wxSizeEvent
& WXUNUSED(event
))
343 wxSashEdgePosition
wxSashWindow::SashHitTest(int x
, int y
, int WXUNUSED(tolerance
))
346 GetClientSize(& cx
, & cy
);
349 for (i
= 0; i
< 4; i
++)
351 wxSashEdge
& edge
= m_sashes
[i
];
352 wxSashEdgePosition position
= (wxSashEdgePosition
) i
;
360 if (y
>= 0 && y
<= GetEdgeMargin(position
))
366 if ((x
>= cx
- GetEdgeMargin(position
)) && (x
<= cx
))
372 if ((y
>= cy
- GetEdgeMargin(position
)) && (y
<= cy
))
373 return wxSASH_BOTTOM
;
378 if ((x
<= GetEdgeMargin(position
)) && (x
>= 0))
392 // Draw 3D effect borders
393 void wxSashWindow::DrawBorders(wxDC
& dc
)
396 GetClientSize(&w
, &h
);
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
);
403 if ( GetWindowStyleFlag() & wxSW_3DBORDER
)
405 dc
.SetPen(mediumShadowPen
);
406 dc
.DrawLine(0, 0, w
-1, 0);
407 dc
.DrawLine(0, 0, 0, h
- 1);
409 dc
.SetPen(darkShadowPen
);
410 dc
.DrawLine(1, 1, w
-2, 1);
411 dc
.DrawLine(1, 1, 1, h
-2);
413 dc
.SetPen(hilightPen
);
414 dc
.DrawLine(0, h
-1, w
-1, h
-1);
415 dc
.DrawLine(w
-1, 0, w
-1, h
); // Surely the maximum y pos. should be h - 1.
416 /// Anyway, h is required for MSW.
418 dc
.SetPen(lightShadowPen
);
419 dc
.DrawLine(w
-2, 1, w
-2, h
-2); // Right hand side
420 dc
.DrawLine(1, h
-2, w
-1, h
-2); // Bottom
422 else if ( GetWindowStyleFlag() & wxSW_BORDER
)
424 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
425 dc
.SetPen(*wxBLACK_PEN
);
426 dc
.DrawRectangle(0, 0, w
-1, h
-1);
429 dc
.SetPen(wxNullPen
);
430 dc
.SetBrush(wxNullBrush
);
433 void wxSashWindow::DrawSashes(wxDC
& dc
)
436 for (i
= 0; i
< 4; i
++)
437 if (m_sashes
[i
].m_show
)
438 DrawSash((wxSashEdgePosition
) i
, dc
);
442 void wxSashWindow::DrawSash(wxSashEdgePosition edge
, wxDC
& dc
)
445 GetClientSize(&w
, &h
);
447 wxPen
facePen(m_faceColour
, 1, wxSOLID
);
448 wxBrush
faceBrush(m_faceColour
, wxSOLID
);
449 wxPen
mediumShadowPen(m_mediumShadowColour
, 1, wxSOLID
);
450 wxPen
darkShadowPen(m_darkShadowColour
, 1, wxSOLID
);
451 wxPen
lightShadowPen(m_lightShadowColour
, 1, wxSOLID
);
452 wxPen
hilightPen(m_hilightColour
, 1, wxSOLID
);
453 wxPen
blackPen(wxColour(0, 0, 0), 1, wxSOLID
);
454 wxPen
whitePen(wxColour(255, 255, 255), 1, wxSOLID
);
456 if ( edge
== wxSASH_LEFT
|| edge
== wxSASH_RIGHT
)
458 int sashPosition
= 0;
459 if (edge
== wxSASH_LEFT
)
462 sashPosition
= w
- GetEdgeMargin(edge
);
465 dc
.SetBrush(faceBrush
);
466 dc
.DrawRectangle(sashPosition
, 0, GetEdgeMargin(edge
), h
);
468 if (GetWindowStyleFlag() & wxSW_3DSASH
)
470 if (edge
== wxSASH_LEFT
)
472 // Draw a dark grey line on the left to indicate that the
474 dc
.SetPen(mediumShadowPen
);
475 dc
.DrawLine(GetEdgeMargin(edge
), 0, GetEdgeMargin(edge
), h
);
479 // Draw a highlight line on the right to indicate that the
481 dc
.SetPen(hilightPen
);
482 dc
.DrawLine(w
- GetEdgeMargin(edge
), 0, w
- GetEdgeMargin(edge
), h
);
486 else // top or bottom
488 int sashPosition
= 0;
489 if (edge
== wxSASH_TOP
)
492 sashPosition
= h
- GetEdgeMargin(edge
);
495 dc
.SetBrush(faceBrush
);
496 dc
.DrawRectangle(0, sashPosition
, w
, GetEdgeMargin(edge
));
498 if (GetWindowStyleFlag() & wxSW_3DSASH
)
500 if (edge
== wxSASH_BOTTOM
)
502 // Draw a highlight line on the bottom to indicate that the
504 dc
.SetPen(hilightPen
);
505 dc
.DrawLine(0, h
- GetEdgeMargin(edge
), w
, h
- GetEdgeMargin(edge
));
509 // Draw a drak grey line on the top to indicate that the
511 dc
.SetPen(mediumShadowPen
);
512 dc
.DrawLine(1, GetEdgeMargin(edge
), w
-1, GetEdgeMargin(edge
));
517 dc
.SetPen(wxNullPen
);
518 dc
.SetBrush(wxNullBrush
);
521 // Draw the sash tracker (for whilst moving the sash)
522 void wxSashWindow::DrawSashTracker(wxSashEdgePosition edge
, int x
, int y
)
525 GetClientSize(&w
, &h
);
531 if ( edge
== wxSASH_LEFT
|| edge
== wxSASH_RIGHT
)
536 if ( (edge
== wxSASH_LEFT
) && (x1
> w
) )
540 else if ( (edge
== wxSASH_RIGHT
) && (x1
< 0) )
550 if ( (edge
== wxSASH_TOP
) && (y1
> h
) )
555 else if ( (edge
== wxSASH_BOTTOM
) && (y1
< 0) )
562 ClientToScreen(&x1
, &y1
);
563 ClientToScreen(&x2
, &y2
);
565 wxPen
sashTrackerPen(*wxBLACK
, 2, wxSOLID
);
567 screenDC
.SetLogicalFunction(wxINVERT
);
568 screenDC
.SetPen(sashTrackerPen
);
569 screenDC
.SetBrush(*wxTRANSPARENT_BRUSH
);
571 screenDC
.DrawLine(x1
, y1
, x2
, y2
);
573 screenDC
.SetLogicalFunction(wxCOPY
);
575 screenDC
.SetPen(wxNullPen
);
576 screenDC
.SetBrush(wxNullBrush
);
579 // Position and size subwindows.
580 // Note that the border size applies to each subwindow, not
581 // including the edges next to the sash.
582 void wxSashWindow::SizeWindows()
585 GetClientSize(&cw
, &ch
);
587 if (GetChildren().Number() == 1)
589 wxWindow
* child
= (wxWindow
*) (GetChildren().First()->Data());
597 if (m_sashes
[0].m_show
)
600 height
-= m_borderSize
;
602 y
+= m_extraBorderSize
;
605 if (m_sashes
[3].m_show
)
608 width
-= m_borderSize
;
610 x
+= m_extraBorderSize
;
613 if (m_sashes
[1].m_show
)
615 width
-= m_borderSize
;
617 width
-= 2*m_extraBorderSize
;
620 if (m_sashes
[2].m_show
)
622 height
-= m_borderSize
;
624 height
-= 2*m_extraBorderSize
;
626 child
->SetSize(x
, y
, width
, height
);
628 else if (GetChildren().Number() > 1)
630 // Perhaps multiple children are themselves sash windows.
631 // TODO: this doesn't really work because the subwindows sizes/positions
632 // must be set to leave a gap for the parent's sash (hit-test and decorations).
633 // Perhaps we can allow for this within LayoutWindow, testing whether the parent
634 // is a sash window, and if so, allowing some space for the edges.
635 wxLayoutAlgorithm layout
;
636 layout
.LayoutWindow(this);
644 // Initialize colours
645 void wxSashWindow::InitColours()
648 #if defined(__WIN95__)
649 m_faceColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
);
650 m_mediumShadowColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DSHADOW
);
651 m_darkShadowColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DDKSHADOW
);
652 m_lightShadowColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT
);
653 m_hilightColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DHILIGHT
);
655 m_faceColour
= *(wxTheColourDatabase
->FindColour("LIGHT GREY"));
656 m_mediumShadowColour
= *(wxTheColourDatabase
->FindColour("GREY"));
657 m_darkShadowColour
= *(wxTheColourDatabase
->FindColour("BLACK"));
658 m_lightShadowColour
= *(wxTheColourDatabase
->FindColour("LIGHT GREY"));
659 m_hilightColour
= *(wxTheColourDatabase
->FindColour("WHITE"));
663 void wxSashWindow::SetSashVisible(wxSashEdgePosition edge
, bool sash
)
665 m_sashes
[edge
].m_show
= sash
;
667 m_sashes
[edge
].m_margin
= m_borderSize
;
669 m_sashes
[edge
].m_margin
= 0;