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 #if !USE_SHARED_LIBRARY
40 IMPLEMENT_DYNAMIC_CLASS(wxSashWindow
, wxWindow
)
41 IMPLEMENT_DYNAMIC_CLASS(wxSashEvent
, wxCommandEvent
)
43 BEGIN_EVENT_TABLE(wxSashWindow
, wxWindow
)
44 EVT_PAINT(wxSashWindow::OnPaint
)
45 EVT_SIZE(wxSashWindow::OnSize
)
46 EVT_MOUSE_EVENTS(wxSashWindow::OnMouseEvent
)
50 wxSashWindow::wxSashWindow()
52 m_draggingEdge
= wxSASH_NONE
;
53 m_dragMode
= wxSASH_DRAG_NONE
;
59 m_extraBorderSize
= 0;
60 m_sashCursorWE
= NULL
;
61 m_sashCursorNS
= NULL
;
63 m_minimumPaneSizeX
= 0;
64 m_minimumPaneSizeY
= 0;
65 m_maximumPaneSizeX
= 10000;
66 m_maximumPaneSizeY
= 10000;
69 wxSashWindow::wxSashWindow(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
,
70 const wxSize
& size
, long style
, const wxString
& name
)
71 :wxWindow(parent
, id
, pos
, size
, style
, name
)
73 m_draggingEdge
= wxSASH_NONE
;
74 m_dragMode
= wxSASH_DRAG_NONE
;
80 m_extraBorderSize
= 0;
81 m_minimumPaneSizeX
= 0;
82 m_minimumPaneSizeY
= 0;
83 m_maximumPaneSizeX
= 10000;
84 m_maximumPaneSizeY
= 10000;
85 m_sashCursorWE
= new wxCursor(wxCURSOR_SIZEWE
);
86 m_sashCursorNS
= new wxCursor(wxCURSOR_SIZENS
);
88 // Eventually, we'll respond to colour change messages
92 wxSashWindow::~wxSashWindow()
94 delete m_sashCursorWE
;
95 delete m_sashCursorNS
;
98 void wxSashWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
102 // if ( m_borderSize > 0 )
108 void wxSashWindow::OnMouseEvent(wxMouseEvent
& event
)
111 event
.GetPosition(&x
, &y
);
113 wxSashEdgePosition sashHit
= SashHitTest(x
, y
);
117 SetCursor(* wxSTANDARD_CURSOR
);
120 SetCursor(wxCursor());
123 if (event
.LeftDown())
125 if ( sashHit
!= wxSASH_NONE
)
129 // Required for X to specify that
130 // that we wish to draw on top of all windows
131 // - and we optimise by specifying the area
132 // for creating the overlap window.
133 // Find the first frame or dialog and use this to specify
134 // the area to draw on.
135 wxWindow
* parent
= this;
137 while (parent
&& !parent
->IsKindOf(CLASSINFO(wxDialog
)) &&
138 !parent
->IsKindOf(CLASSINFO(wxFrame
)))
139 parent
= parent
->GetParent();
141 wxScreenDC::StartDrawingOnTop(parent
);
143 // We don't say we're dragging yet; we leave that
144 // decision for the Dragging() branch, to ensure
145 // the user has dragged a little bit.
146 m_dragMode
= wxSASH_DRAG_LEFT_DOWN
;
147 m_draggingEdge
= sashHit
;
152 else if ( event
.LeftUp() && m_dragMode
== wxSASH_DRAG_LEFT_DOWN
)
154 // Wasn't a proper drag
156 wxScreenDC::EndDrawingOnTop();
157 m_dragMode
= wxSASH_DRAG_NONE
;
158 m_draggingEdge
= wxSASH_NONE
;
160 else if (event
.LeftUp() && m_dragMode
== wxSASH_DRAG_DRAGGING
)
162 // We can stop dragging now and see what we've got.
163 m_dragMode
= wxSASH_DRAG_NONE
;
166 DrawSashTracker(m_draggingEdge
, m_oldX
, m_oldY
);
168 // End drawing on top (frees the window used for drawing
170 wxScreenDC::EndDrawingOnTop();
175 GetPosition(&xp
, &yp
);
177 wxSashEdgePosition edge
= m_draggingEdge
;
178 m_draggingEdge
= wxSASH_NONE
;
181 wxSashDragStatus status
= wxSASH_STATUS_OK
;
183 // the new height and width of the window - if -1, it didn't change
187 // NB: x and y may be negative and they're relative to the sash window
188 // upper left corner, while xp and yp are expressed in the parent
189 // window system of coordinates, so adjust them! After this
190 // adjustment, all coordinates are relative to the parent window.
199 // top sash shouldn't get below the bottom one
200 status
= wxSASH_STATUS_OUT_OF_RANGE
;
204 newHeight
= h
- (y
- yp
);
211 // bottom sash shouldn't get above the top one
212 status
= wxSASH_STATUS_OUT_OF_RANGE
;
223 // left sash shouldn't get beyond the right one
224 status
= wxSASH_STATUS_OUT_OF_RANGE
;
228 newWidth
= w
- (x
- xp
);
235 // and the right sash, finally, shouldn't be beyond the
237 status
= wxSASH_STATUS_OUT_OF_RANGE
;
246 // can this happen at all?
250 if ( newHeight
== -1 )
257 // make sure it's in m_minimumPaneSizeY..m_maximumPaneSizeY range
258 newHeight
= wxMax(newHeight
, m_minimumPaneSizeY
);
259 newHeight
= wxMin(newHeight
, m_maximumPaneSizeY
);
262 if ( newWidth
== -1 )
269 // make sure it's in m_minimumPaneSizeY..m_maximumPaneSizeY range
270 newWidth
= wxMax(newWidth
, m_minimumPaneSizeX
);
271 newWidth
= wxMin(newWidth
, m_maximumPaneSizeX
);
274 dragRect
= wxRect(x
, y
, newWidth
, newHeight
);
276 wxSashEvent
event(GetId(), edge
);
277 event
.SetEventObject(this);
278 event
.SetDragStatus(status
);
279 event
.SetDragRect(dragRect
);
280 GetEventHandler()->ProcessEvent(event
);
282 else if (event
.Moving() && !event
.Dragging())
284 // Just change the cursor if required
285 if ( sashHit
!= wxSASH_NONE
)
287 if ( (sashHit
== wxSASH_LEFT
) || (sashHit
== wxSASH_RIGHT
) )
289 SetCursor(*m_sashCursorWE
);
293 SetCursor(*m_sashCursorNS
);
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_3D
)
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_3D
)
470 if (edge
== wxSASH_LEFT
)
472 // Draw a black line on the left to indicate that the
475 dc
.DrawLine(GetEdgeMargin(edge
), 0, GetEdgeMargin(edge
), h
);
479 // Draw a white line on the right to indicate that the
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_3D
)
500 if (edge
== wxSASH_BOTTOM
)
502 // Draw a black line on the bottom to indicate that the
505 dc
.DrawLine(0, h
- GetEdgeMargin(edge
), w
, h
- GetEdgeMargin(edge
));
509 // Draw a white line on the top to indicate that the
512 dc
.DrawLine(0, GetEdgeMargin(edge
), w
, 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;