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 DEFINE_EVENT_TYPE(wxEVT_SASH_DRAGGED
)
41 IMPLEMENT_DYNAMIC_CLASS(wxSashWindow
, wxWindow
)
42 IMPLEMENT_DYNAMIC_CLASS(wxSashEvent
, wxCommandEvent
)
44 BEGIN_EVENT_TABLE(wxSashWindow
, wxWindow
)
45 EVT_PAINT(wxSashWindow::OnPaint
)
46 EVT_SIZE(wxSashWindow::OnSize
)
47 EVT_MOUSE_EVENTS(wxSashWindow::OnMouseEvent
)
50 bool wxSashWindow::Create(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
,
51 const wxSize
& size
, long style
, const wxString
& name
)
53 return wxWindow::Create(parent
, id
, pos
, size
, style
, name
);
56 wxSashWindow::~wxSashWindow()
58 delete m_sashCursorWE
;
59 delete m_sashCursorNS
;
62 void wxSashWindow::Init()
64 m_draggingEdge
= wxSASH_NONE
;
65 m_dragMode
= wxSASH_DRAG_NONE
;
71 m_extraBorderSize
= 0;
72 m_minimumPaneSizeX
= 0;
73 m_minimumPaneSizeY
= 0;
74 m_maximumPaneSizeX
= 10000;
75 m_maximumPaneSizeY
= 10000;
76 m_sashCursorWE
= new wxCursor(wxCURSOR_SIZEWE
);
77 m_sashCursorNS
= new wxCursor(wxCURSOR_SIZENS
);
78 m_mouseCaptured
= FALSE
;
80 // Eventually, we'll respond to colour change messages
84 void wxSashWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
88 // if ( m_borderSize > 0 )
94 void wxSashWindow::OnMouseEvent(wxMouseEvent
& event
)
97 event
.GetPosition(&x
, &y
);
99 wxSashEdgePosition sashHit
= SashHitTest(x
, y
);
102 #if defined(__WXMOTIF__) || defined(__WXGTK__)
103 // Not necessary and in fact inhibits proper cursor setting (JACS 8/2000)
104 //SetCursor(* wxSTANDARD_CURSOR);
107 SetCursor(wxNullCursor
);
110 if (event
.LeftDown())
113 m_mouseCaptured
= TRUE
;
115 if ( sashHit
!= wxSASH_NONE
)
117 // Required for X to specify that
118 // that we wish to draw on top of all windows
119 // - and we optimise by specifying the area
120 // for creating the overlap window.
121 // Find the first frame or dialog and use this to specify
122 // the area to draw on.
123 wxWindow
* parent
= this;
125 while (parent
&& !parent
->IsKindOf(CLASSINFO(wxDialog
)) &&
126 !parent
->IsKindOf(CLASSINFO(wxFrame
)))
127 parent
= parent
->GetParent();
129 wxScreenDC::StartDrawingOnTop(parent
);
131 // We don't say we're dragging yet; we leave that
132 // decision for the Dragging() branch, to ensure
133 // the user has dragged a little bit.
134 m_dragMode
= wxSASH_DRAG_LEFT_DOWN
;
135 m_draggingEdge
= sashHit
;
139 if ( (sashHit
== wxSASH_LEFT
) || (sashHit
== wxSASH_RIGHT
) )
141 SetCursor(*m_sashCursorWE
);
145 SetCursor(*m_sashCursorNS
);
149 else if ( event
.LeftUp() && m_dragMode
== wxSASH_DRAG_LEFT_DOWN
)
151 // Wasn't a proper drag
154 m_mouseCaptured
= FALSE
;
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 m_mouseCaptured
= FALSE
;
169 DrawSashTracker(m_draggingEdge
, m_oldX
, m_oldY
);
171 // End drawing on top (frees the window used for drawing
173 wxScreenDC::EndDrawingOnTop();
178 GetPosition(&xp
, &yp
);
180 wxSashEdgePosition edge
= m_draggingEdge
;
181 m_draggingEdge
= wxSASH_NONE
;
184 wxSashDragStatus status
= wxSASH_STATUS_OK
;
186 // the new height and width of the window - if -1, it didn't change
190 // NB: x and y may be negative and they're relative to the sash window
191 // upper left corner, while xp and yp are expressed in the parent
192 // window system of coordinates, so adjust them! After this
193 // adjustment, all coordinates are relative to the parent window.
202 // top sash shouldn't get below the bottom one
203 status
= wxSASH_STATUS_OUT_OF_RANGE
;
207 newHeight
= h
- (y
- yp
);
214 // bottom sash shouldn't get above the top one
215 status
= wxSASH_STATUS_OUT_OF_RANGE
;
226 // left sash shouldn't get beyond the right one
227 status
= wxSASH_STATUS_OUT_OF_RANGE
;
231 newWidth
= w
- (x
- xp
);
238 // and the right sash, finally, shouldn't be beyond the
240 status
= wxSASH_STATUS_OUT_OF_RANGE
;
249 // can this happen at all?
253 if ( newHeight
== -1 )
260 // make sure it's in m_minimumPaneSizeY..m_maximumPaneSizeY range
261 newHeight
= wxMax(newHeight
, m_minimumPaneSizeY
);
262 newHeight
= wxMin(newHeight
, m_maximumPaneSizeY
);
265 if ( newWidth
== -1 )
272 // make sure it's in m_minimumPaneSizeY..m_maximumPaneSizeY range
273 newWidth
= wxMax(newWidth
, m_minimumPaneSizeX
);
274 newWidth
= wxMin(newWidth
, m_maximumPaneSizeX
);
277 dragRect
= wxRect(x
, y
, newWidth
, newHeight
);
279 wxSashEvent
event(GetId(), edge
);
280 event
.SetEventObject(this);
281 event
.SetDragStatus(status
);
282 event
.SetDragRect(dragRect
);
283 GetEventHandler()->ProcessEvent(event
);
285 else if ( event
.LeftUp() )
289 m_mouseCaptured
= FALSE
;
291 else if (event
.Moving() && !event
.Dragging())
293 // Just change the cursor if required
294 if ( sashHit
!= wxSASH_NONE
)
296 if ( (sashHit
== wxSASH_LEFT
) || (sashHit
== wxSASH_RIGHT
) )
298 SetCursor(*m_sashCursorWE
);
302 SetCursor(*m_sashCursorNS
);
307 SetCursor(wxNullCursor
);
310 else if ( event
.Dragging() &&
311 ((m_dragMode
== wxSASH_DRAG_DRAGGING
) ||
312 (m_dragMode
== wxSASH_DRAG_LEFT_DOWN
)) )
314 if ( (m_draggingEdge
== wxSASH_LEFT
) || (m_draggingEdge
== wxSASH_RIGHT
) )
316 SetCursor(*m_sashCursorWE
);
320 SetCursor(*m_sashCursorNS
);
323 if (m_dragMode
== wxSASH_DRAG_LEFT_DOWN
)
325 m_dragMode
= wxSASH_DRAG_DRAGGING
;
326 DrawSashTracker(m_draggingEdge
, x
, y
);
330 if ( m_dragMode
== wxSASH_DRAG_DRAGGING
)
333 DrawSashTracker(m_draggingEdge
, m_oldX
, m_oldY
);
336 DrawSashTracker(m_draggingEdge
, x
, y
);
342 else if ( event
.LeftDClick() )
351 void wxSashWindow::OnSize(wxSizeEvent
& WXUNUSED(event
))
356 wxSashEdgePosition
wxSashWindow::SashHitTest(int x
, int y
, int WXUNUSED(tolerance
))
359 GetClientSize(& cx
, & cy
);
362 for (i
= 0; i
< 4; i
++)
364 wxSashEdge
& edge
= m_sashes
[i
];
365 wxSashEdgePosition position
= (wxSashEdgePosition
) i
;
373 if (y
>= 0 && y
<= GetEdgeMargin(position
))
379 if ((x
>= cx
- GetEdgeMargin(position
)) && (x
<= cx
))
385 if ((y
>= cy
- GetEdgeMargin(position
)) && (y
<= cy
))
386 return wxSASH_BOTTOM
;
391 if ((x
<= GetEdgeMargin(position
)) && (x
>= 0))
405 // Draw 3D effect borders
406 void wxSashWindow::DrawBorders(wxDC
& dc
)
409 GetClientSize(&w
, &h
);
411 wxPen
mediumShadowPen(m_mediumShadowColour
, 1, wxSOLID
);
412 wxPen
darkShadowPen(m_darkShadowColour
, 1, wxSOLID
);
413 wxPen
lightShadowPen(m_lightShadowColour
, 1, wxSOLID
);
414 wxPen
hilightPen(m_hilightColour
, 1, wxSOLID
);
416 if ( GetWindowStyleFlag() & wxSW_3DBORDER
)
418 dc
.SetPen(mediumShadowPen
);
419 dc
.DrawLine(0, 0, w
-1, 0);
420 dc
.DrawLine(0, 0, 0, h
- 1);
422 dc
.SetPen(darkShadowPen
);
423 dc
.DrawLine(1, 1, w
-2, 1);
424 dc
.DrawLine(1, 1, 1, h
-2);
426 dc
.SetPen(hilightPen
);
427 dc
.DrawLine(0, h
-1, w
-1, h
-1);
428 dc
.DrawLine(w
-1, 0, w
-1, h
); // Surely the maximum y pos. should be h - 1.
429 /// Anyway, h is required for MSW.
431 dc
.SetPen(lightShadowPen
);
432 dc
.DrawLine(w
-2, 1, w
-2, h
-2); // Right hand side
433 dc
.DrawLine(1, h
-2, w
-1, h
-2); // Bottom
435 else if ( GetWindowStyleFlag() & wxSW_BORDER
)
437 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
438 dc
.SetPen(*wxBLACK_PEN
);
439 dc
.DrawRectangle(0, 0, w
-1, h
-1);
442 dc
.SetPen(wxNullPen
);
443 dc
.SetBrush(wxNullBrush
);
446 void wxSashWindow::DrawSashes(wxDC
& dc
)
449 for (i
= 0; i
< 4; i
++)
450 if (m_sashes
[i
].m_show
)
451 DrawSash((wxSashEdgePosition
) i
, dc
);
455 void wxSashWindow::DrawSash(wxSashEdgePosition edge
, wxDC
& dc
)
458 GetClientSize(&w
, &h
);
460 wxPen
facePen(m_faceColour
, 1, wxSOLID
);
461 wxBrush
faceBrush(m_faceColour
, wxSOLID
);
462 wxPen
mediumShadowPen(m_mediumShadowColour
, 1, wxSOLID
);
463 wxPen
darkShadowPen(m_darkShadowColour
, 1, wxSOLID
);
464 wxPen
lightShadowPen(m_lightShadowColour
, 1, wxSOLID
);
465 wxPen
hilightPen(m_hilightColour
, 1, wxSOLID
);
466 wxPen
blackPen(wxColour(0, 0, 0), 1, wxSOLID
);
467 wxPen
whitePen(wxColour(255, 255, 255), 1, wxSOLID
);
469 if ( edge
== wxSASH_LEFT
|| edge
== wxSASH_RIGHT
)
471 int sashPosition
= 0;
472 if (edge
== wxSASH_LEFT
)
475 sashPosition
= w
- GetEdgeMargin(edge
);
478 dc
.SetBrush(faceBrush
);
479 dc
.DrawRectangle(sashPosition
, 0, GetEdgeMargin(edge
), h
);
481 if (GetWindowStyleFlag() & wxSW_3DSASH
)
483 if (edge
== wxSASH_LEFT
)
485 // Draw a dark grey line on the left to indicate that the
487 dc
.SetPen(mediumShadowPen
);
488 dc
.DrawLine(GetEdgeMargin(edge
), 0, GetEdgeMargin(edge
), h
);
492 // Draw a highlight line on the right to indicate that the
494 dc
.SetPen(hilightPen
);
495 dc
.DrawLine(w
- GetEdgeMargin(edge
), 0, w
- GetEdgeMargin(edge
), h
);
499 else // top or bottom
501 int sashPosition
= 0;
502 if (edge
== wxSASH_TOP
)
505 sashPosition
= h
- GetEdgeMargin(edge
);
508 dc
.SetBrush(faceBrush
);
509 dc
.DrawRectangle(0, sashPosition
, w
, GetEdgeMargin(edge
));
511 if (GetWindowStyleFlag() & wxSW_3DSASH
)
513 if (edge
== wxSASH_BOTTOM
)
515 // Draw a highlight line on the bottom to indicate that the
517 dc
.SetPen(hilightPen
);
518 dc
.DrawLine(0, h
- GetEdgeMargin(edge
), w
, h
- GetEdgeMargin(edge
));
522 // Draw a drak grey line on the top to indicate that the
524 dc
.SetPen(mediumShadowPen
);
525 dc
.DrawLine(1, GetEdgeMargin(edge
), w
-1, GetEdgeMargin(edge
));
530 dc
.SetPen(wxNullPen
);
531 dc
.SetBrush(wxNullBrush
);
534 // Draw the sash tracker (for whilst moving the sash)
535 void wxSashWindow::DrawSashTracker(wxSashEdgePosition edge
, int x
, int y
)
538 GetClientSize(&w
, &h
);
544 if ( edge
== wxSASH_LEFT
|| edge
== wxSASH_RIGHT
)
549 if ( (edge
== wxSASH_LEFT
) && (x1
> w
) )
553 else if ( (edge
== wxSASH_RIGHT
) && (x1
< 0) )
563 if ( (edge
== wxSASH_TOP
) && (y1
> h
) )
568 else if ( (edge
== wxSASH_BOTTOM
) && (y1
< 0) )
575 ClientToScreen(&x1
, &y1
);
576 ClientToScreen(&x2
, &y2
);
578 wxPen
sashTrackerPen(*wxBLACK
, 2, wxSOLID
);
580 screenDC
.SetLogicalFunction(wxINVERT
);
581 screenDC
.SetPen(sashTrackerPen
);
582 screenDC
.SetBrush(*wxTRANSPARENT_BRUSH
);
584 screenDC
.DrawLine(x1
, y1
, x2
, y2
);
586 screenDC
.SetLogicalFunction(wxCOPY
);
588 screenDC
.SetPen(wxNullPen
);
589 screenDC
.SetBrush(wxNullBrush
);
592 // Position and size subwindows.
593 // Note that the border size applies to each subwindow, not
594 // including the edges next to the sash.
595 void wxSashWindow::SizeWindows()
598 GetClientSize(&cw
, &ch
);
600 if (GetChildren().Number() == 1)
602 wxWindow
* child
= (wxWindow
*) (GetChildren().First()->Data());
610 if (m_sashes
[0].m_show
)
613 height
-= m_borderSize
;
615 y
+= m_extraBorderSize
;
618 if (m_sashes
[3].m_show
)
621 width
-= m_borderSize
;
623 x
+= m_extraBorderSize
;
626 if (m_sashes
[1].m_show
)
628 width
-= m_borderSize
;
630 width
-= 2*m_extraBorderSize
;
633 if (m_sashes
[2].m_show
)
635 height
-= m_borderSize
;
637 height
-= 2*m_extraBorderSize
;
639 child
->SetSize(x
, y
, width
, height
);
641 else if (GetChildren().Number() > 1)
643 // Perhaps multiple children are themselves sash windows.
644 // TODO: this doesn't really work because the subwindows sizes/positions
645 // must be set to leave a gap for the parent's sash (hit-test and decorations).
646 // Perhaps we can allow for this within LayoutWindow, testing whether the parent
647 // is a sash window, and if so, allowing some space for the edges.
648 wxLayoutAlgorithm layout
;
649 layout
.LayoutWindow(this);
657 // Initialize colours
658 void wxSashWindow::InitColours()
662 m_faceColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
);
663 m_mediumShadowColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DSHADOW
);
664 m_darkShadowColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DDKSHADOW
);
665 m_lightShadowColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT
);
666 m_hilightColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DHILIGHT
);
668 m_faceColour
= *(wxTheColourDatabase
->FindColour("LIGHT GREY"));
669 m_mediumShadowColour
= *(wxTheColourDatabase
->FindColour("GREY"));
670 m_darkShadowColour
= *(wxTheColourDatabase
->FindColour("BLACK"));
671 m_lightShadowColour
= *(wxTheColourDatabase
->FindColour("LIGHT GREY"));
672 m_hilightColour
= *(wxTheColourDatabase
->FindColour("WHITE"));
676 void wxSashWindow::SetSashVisible(wxSashEdgePosition edge
, bool sash
)
678 m_sashes
[edge
].m_show
= sash
;
680 m_sashes
[edge
].m_margin
= m_borderSize
;
682 m_sashes
[edge
].m_margin
= 0;