]> git.saurik.com Git - wxWidgets.git/blob - src/generic/sashwin.cpp
b2cd42c75979c77e7954acaa5e530d043fae5423
[wxWidgets.git] / src / generic / sashwin.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: sashwin.cpp
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
7 // Modified by:
8 // Created: 01/02/97
9 // RCS-ID: $Id$
10 // Copyright: (c) Julian Smart
11 // Licence: wxWindows licence
12 /////////////////////////////////////////////////////////////////////////////
13
14 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
15 #pragma implementation "sashwin.h"
16 #endif
17
18 // For compilers that support precompilation, includes "wx.h".
19 #include "wx/wxprec.h"
20
21 #ifdef __BORLANDC__
22 #pragma hdrstop
23 #endif
24
25 #if wxUSE_SASH
26
27 #ifndef WX_PRECOMP
28 #include "wx/dialog.h"
29 #include "wx/frame.h"
30 #include "wx/settings.h"
31 #endif
32
33 #include "wx/math.h"
34
35 #include <stdlib.h>
36
37 #include "wx/dcscreen.h"
38 #include "wx/sashwin.h"
39 #include "wx/laywin.h"
40
41 DEFINE_EVENT_TYPE(wxEVT_SASH_DRAGGED)
42
43 IMPLEMENT_DYNAMIC_CLASS(wxSashWindow, wxWindow)
44 IMPLEMENT_DYNAMIC_CLASS(wxSashEvent, wxCommandEvent)
45
46 BEGIN_EVENT_TABLE(wxSashWindow, wxWindow)
47 EVT_PAINT(wxSashWindow::OnPaint)
48 EVT_SIZE(wxSashWindow::OnSize)
49 EVT_MOUSE_EVENTS(wxSashWindow::OnMouseEvent)
50 #if defined( __WXMSW__ ) || defined( __WXMAC__)
51 EVT_SET_CURSOR(wxSashWindow::OnSetCursor)
52 #endif // wxMSW
53
54 END_EVENT_TABLE()
55
56 bool wxSashWindow::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos,
57 const wxSize& size, long style, const wxString& name)
58 {
59 return wxWindow::Create(parent, id, pos, size, style, name);
60 }
61
62 wxSashWindow::~wxSashWindow()
63 {
64 delete m_sashCursorWE;
65 delete m_sashCursorNS;
66 }
67
68 void wxSashWindow::Init()
69 {
70 m_draggingEdge = wxSASH_NONE;
71 m_dragMode = wxSASH_DRAG_NONE;
72 m_oldX = 0;
73 m_oldY = 0;
74 m_firstX = 0;
75 m_firstY = 0;
76 m_borderSize = 3;
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);
84 m_mouseCaptured = false;
85 m_currentCursor = NULL;
86
87 // Eventually, we'll respond to colour change messages
88 InitColours();
89 }
90
91 void wxSashWindow::OnPaint(wxPaintEvent& WXUNUSED(event))
92 {
93 wxPaintDC dc(this);
94
95 DrawBorders(dc);
96 DrawSashes(dc);
97 }
98
99 void wxSashWindow::OnMouseEvent(wxMouseEvent& event)
100 {
101 wxCoord x, y;
102 event.GetPosition(&x, &y);
103
104 wxSashEdgePosition sashHit = SashHitTest(x, y);
105
106 if (event.LeftDown())
107 {
108 CaptureMouse();
109 m_mouseCaptured = true;
110
111 if ( sashHit != wxSASH_NONE )
112 {
113 // Required for X to specify that
114 // that we wish to draw on top of all windows
115 // - and we optimise by specifying the area
116 // for creating the overlap window.
117 // Find the first frame or dialog and use this to specify
118 // the area to draw on.
119 wxWindow* parent = this;
120
121 while (parent && !parent->IsKindOf(CLASSINFO(wxDialog)) &&
122 !parent->IsKindOf(CLASSINFO(wxFrame)))
123 parent = parent->GetParent();
124
125 wxScreenDC::StartDrawingOnTop(parent);
126
127 // We don't say we're dragging yet; we leave that
128 // decision for the Dragging() branch, to ensure
129 // the user has dragged a little bit.
130 m_dragMode = wxSASH_DRAG_LEFT_DOWN;
131 m_draggingEdge = sashHit;
132 m_firstX = x;
133 m_firstY = y;
134
135 if ( (sashHit == wxSASH_LEFT) || (sashHit == wxSASH_RIGHT) )
136 {
137 if (m_currentCursor != m_sashCursorWE)
138 {
139 SetCursor(*m_sashCursorWE);
140 }
141 m_currentCursor = m_sashCursorWE;
142 }
143 else
144 {
145 if (m_currentCursor != m_sashCursorNS)
146 {
147 SetCursor(*m_sashCursorNS);
148 }
149 m_currentCursor = m_sashCursorNS;
150 }
151 }
152 }
153 else if ( event.LeftUp() && m_dragMode == wxSASH_DRAG_LEFT_DOWN )
154 {
155 // Wasn't a proper drag
156 if (m_mouseCaptured)
157 ReleaseMouse();
158 m_mouseCaptured = false;
159
160 wxScreenDC::EndDrawingOnTop();
161 m_dragMode = wxSASH_DRAG_NONE;
162 m_draggingEdge = wxSASH_NONE;
163 }
164 else if (event.LeftUp() && m_dragMode == wxSASH_DRAG_DRAGGING)
165 {
166 // We can stop dragging now and see what we've got.
167 m_dragMode = wxSASH_DRAG_NONE;
168 if (m_mouseCaptured)
169 ReleaseMouse();
170 m_mouseCaptured = false;
171
172 // Erase old tracker
173 DrawSashTracker(m_draggingEdge, m_oldX, m_oldY);
174
175 // End drawing on top (frees the window used for drawing
176 // over the screen)
177 wxScreenDC::EndDrawingOnTop();
178
179 int w, h;
180 GetSize(&w, &h);
181 int xp, yp;
182 GetPosition(&xp, &yp);
183
184 wxSashEdgePosition edge = m_draggingEdge;
185 m_draggingEdge = wxSASH_NONE;
186
187 wxRect dragRect;
188 wxSashDragStatus status = wxSASH_STATUS_OK;
189
190 // the new height and width of the window - if -1, it didn't change
191 int newHeight = wxDefaultCoord,
192 newWidth = wxDefaultCoord;
193
194 // NB: x and y may be negative and they're relative to the sash window
195 // upper left corner, while xp and yp are expressed in the parent
196 // window system of coordinates, so adjust them! After this
197 // adjustment, all coordinates are relative to the parent window.
198 y += yp;
199 x += xp;
200
201 switch (edge)
202 {
203 case wxSASH_TOP:
204 if ( y > yp + h )
205 {
206 // top sash shouldn't get below the bottom one
207 status = wxSASH_STATUS_OUT_OF_RANGE;
208 }
209 else
210 {
211 newHeight = h - (y - yp);
212 }
213 break;
214
215 case wxSASH_BOTTOM:
216 if ( y < yp )
217 {
218 // bottom sash shouldn't get above the top one
219 status = wxSASH_STATUS_OUT_OF_RANGE;
220 }
221 else
222 {
223 newHeight = y - yp;
224 }
225 break;
226
227 case wxSASH_LEFT:
228 if ( x > xp + w )
229 {
230 // left sash shouldn't get beyond the right one
231 status = wxSASH_STATUS_OUT_OF_RANGE;
232 }
233 else
234 {
235 newWidth = w - (x - xp);
236 }
237 break;
238
239 case wxSASH_RIGHT:
240 if ( x < xp )
241 {
242 // and the right sash, finally, shouldn't be beyond the
243 // left one
244 status = wxSASH_STATUS_OUT_OF_RANGE;
245 }
246 else
247 {
248 newWidth = x - xp;
249 }
250 break;
251
252 case wxSASH_NONE:
253 // can this happen at all?
254 break;
255 }
256
257 if ( newHeight == wxDefaultCoord )
258 {
259 // didn't change
260 newHeight = h;
261 }
262 else
263 {
264 // make sure it's in m_minimumPaneSizeY..m_maximumPaneSizeY range
265 newHeight = wxMax(newHeight, m_minimumPaneSizeY);
266 newHeight = wxMin(newHeight, m_maximumPaneSizeY);
267 }
268
269 if ( newWidth == wxDefaultCoord )
270 {
271 // didn't change
272 newWidth = w;
273 }
274 else
275 {
276 // make sure it's in m_minimumPaneSizeY..m_maximumPaneSizeY range
277 newWidth = wxMax(newWidth, m_minimumPaneSizeX);
278 newWidth = wxMin(newWidth, m_maximumPaneSizeX);
279 }
280
281 dragRect = wxRect(x, y, newWidth, newHeight);
282
283 wxSashEvent event(GetId(), edge);
284 event.SetEventObject(this);
285 event.SetDragStatus(status);
286 event.SetDragRect(dragRect);
287 GetEventHandler()->ProcessEvent(event);
288 }
289 else if ( event.LeftUp() )
290 {
291 if (m_mouseCaptured)
292 ReleaseMouse();
293 m_mouseCaptured = false;
294 }
295 else if (event.Moving() && !event.Dragging())
296 {
297 // Just change the cursor if required
298 if ( sashHit != wxSASH_NONE )
299 {
300 if ( (sashHit == wxSASH_LEFT) || (sashHit == wxSASH_RIGHT) )
301 {
302 if (m_currentCursor != m_sashCursorWE)
303 {
304 SetCursor(*m_sashCursorWE);
305 }
306 m_currentCursor = m_sashCursorWE;
307 }
308 else
309 {
310 if (m_currentCursor != m_sashCursorNS)
311 {
312 SetCursor(*m_sashCursorNS);
313 }
314 m_currentCursor = m_sashCursorNS;
315 }
316 }
317 else
318 {
319 SetCursor(wxNullCursor);
320 m_currentCursor = NULL;
321 }
322 }
323 else if ( event.Dragging() &&
324 ((m_dragMode == wxSASH_DRAG_DRAGGING) ||
325 (m_dragMode == wxSASH_DRAG_LEFT_DOWN)) )
326 {
327 if ( (m_draggingEdge == wxSASH_LEFT) || (m_draggingEdge == wxSASH_RIGHT) )
328 {
329 if (m_currentCursor != m_sashCursorWE)
330 {
331 SetCursor(*m_sashCursorWE);
332 }
333 m_currentCursor = m_sashCursorWE;
334 }
335 else
336 {
337 if (m_currentCursor != m_sashCursorNS)
338 {
339 SetCursor(*m_sashCursorNS);
340 }
341 m_currentCursor = m_sashCursorNS;
342 }
343
344 if (m_dragMode == wxSASH_DRAG_LEFT_DOWN)
345 {
346 m_dragMode = wxSASH_DRAG_DRAGGING;
347 DrawSashTracker(m_draggingEdge, x, y);
348 }
349 else
350 {
351 if ( m_dragMode == wxSASH_DRAG_DRAGGING )
352 {
353 // Erase old tracker
354 DrawSashTracker(m_draggingEdge, m_oldX, m_oldY);
355
356 // Draw new one
357 DrawSashTracker(m_draggingEdge, x, y);
358 }
359 }
360 m_oldX = x;
361 m_oldY = y;
362 }
363 else if ( event.LeftDClick() )
364 {
365 // Nothing
366 }
367 else
368 {
369 }
370 }
371
372 void wxSashWindow::OnSize(wxSizeEvent& WXUNUSED(event))
373 {
374 SizeWindows();
375 }
376
377 wxSashEdgePosition wxSashWindow::SashHitTest(int x, int y, int WXUNUSED(tolerance))
378 {
379 int cx, cy;
380 GetClientSize(& cx, & cy);
381
382 int i;
383 for (i = 0; i < 4; i++)
384 {
385 wxSashEdge& edge = m_sashes[i];
386 wxSashEdgePosition position = (wxSashEdgePosition) i ;
387
388 if (edge.m_show)
389 {
390 switch (position)
391 {
392 case wxSASH_TOP:
393 {
394 if (y >= 0 && y <= GetEdgeMargin(position))
395 return wxSASH_TOP;
396 break;
397 }
398 case wxSASH_RIGHT:
399 {
400 if ((x >= cx - GetEdgeMargin(position)) && (x <= cx))
401 return wxSASH_RIGHT;
402 break;
403 }
404 case wxSASH_BOTTOM:
405 {
406 if ((y >= cy - GetEdgeMargin(position)) && (y <= cy))
407 return wxSASH_BOTTOM;
408 break;
409 }
410 case wxSASH_LEFT:
411 {
412 if ((x <= GetEdgeMargin(position)) && (x >= 0))
413 return wxSASH_LEFT;
414 break;
415 }
416 case wxSASH_NONE:
417 {
418 break;
419 }
420 }
421 }
422 }
423 return wxSASH_NONE;
424 }
425
426 // Draw 3D effect borders
427 void wxSashWindow::DrawBorders(wxDC& dc)
428 {
429 int w, h;
430 GetClientSize(&w, &h);
431
432 wxPen mediumShadowPen(m_mediumShadowColour, 1, wxSOLID);
433 wxPen darkShadowPen(m_darkShadowColour, 1, wxSOLID);
434 wxPen lightShadowPen(m_lightShadowColour, 1, wxSOLID);
435 wxPen hilightPen(m_hilightColour, 1, wxSOLID);
436
437 if ( GetWindowStyleFlag() & wxSW_3DBORDER )
438 {
439 dc.SetPen(mediumShadowPen);
440 dc.DrawLine(0, 0, w-1, 0);
441 dc.DrawLine(0, 0, 0, h - 1);
442
443 dc.SetPen(darkShadowPen);
444 dc.DrawLine(1, 1, w-2, 1);
445 dc.DrawLine(1, 1, 1, h-2);
446
447 dc.SetPen(hilightPen);
448 dc.DrawLine(0, h-1, w-1, h-1);
449 dc.DrawLine(w-1, 0, w-1, h); // Surely the maximum y pos. should be h - 1.
450 /// Anyway, h is required for MSW.
451
452 dc.SetPen(lightShadowPen);
453 dc.DrawLine(w-2, 1, w-2, h-2); // Right hand side
454 dc.DrawLine(1, h-2, w-1, h-2); // Bottom
455 }
456 else if ( GetWindowStyleFlag() & wxSW_BORDER )
457 {
458 dc.SetBrush(*wxTRANSPARENT_BRUSH);
459 dc.SetPen(*wxBLACK_PEN);
460 dc.DrawRectangle(0, 0, w-1, h-1);
461 }
462
463 dc.SetPen(wxNullPen);
464 dc.SetBrush(wxNullBrush);
465 }
466
467 void wxSashWindow::DrawSashes(wxDC& dc)
468 {
469 int i;
470 for (i = 0; i < 4; i++)
471 if (m_sashes[i].m_show)
472 DrawSash((wxSashEdgePosition) i, dc);
473 }
474
475 // Draw the sash
476 void wxSashWindow::DrawSash(wxSashEdgePosition edge, wxDC& dc)
477 {
478 int w, h;
479 GetClientSize(&w, &h);
480
481 wxPen facePen(m_faceColour, 1, wxSOLID);
482 wxBrush faceBrush(m_faceColour, wxSOLID);
483 wxPen mediumShadowPen(m_mediumShadowColour, 1, wxSOLID);
484 wxPen darkShadowPen(m_darkShadowColour, 1, wxSOLID);
485 wxPen lightShadowPen(m_lightShadowColour, 1, wxSOLID);
486 wxPen hilightPen(m_hilightColour, 1, wxSOLID);
487 wxColour blackClr(0, 0, 0);
488 wxColour whiteClr(255, 255, 255);
489 wxPen blackPen(blackClr, 1, wxSOLID);
490 wxPen whitePen(whiteClr, 1, wxSOLID);
491
492 if ( edge == wxSASH_LEFT || edge == wxSASH_RIGHT )
493 {
494 int sashPosition = (edge == wxSASH_LEFT) ? 0 : ( w - GetEdgeMargin(edge) );
495
496 dc.SetPen(facePen);
497 dc.SetBrush(faceBrush);
498 dc.DrawRectangle(sashPosition, 0, GetEdgeMargin(edge), h);
499
500 if (GetWindowStyleFlag() & wxSW_3DSASH)
501 {
502 if (edge == wxSASH_LEFT)
503 {
504 // Draw a dark grey line on the left to indicate that the
505 // sash is raised
506 dc.SetPen(mediumShadowPen);
507 dc.DrawLine(GetEdgeMargin(edge), 0, GetEdgeMargin(edge), h);
508 }
509 else
510 {
511 // Draw a highlight line on the right to indicate that the
512 // sash is raised
513 dc.SetPen(hilightPen);
514 dc.DrawLine(w - GetEdgeMargin(edge), 0, w - GetEdgeMargin(edge), h);
515 }
516 }
517 }
518 else // top or bottom
519 {
520 int sashPosition = (edge == wxSASH_TOP) ? 0 : ( h - GetEdgeMargin(edge) );
521
522 dc.SetPen(facePen);
523 dc.SetBrush(faceBrush);
524 dc.DrawRectangle(0, sashPosition, w, GetEdgeMargin(edge));
525
526 if (GetWindowStyleFlag() & wxSW_3DSASH)
527 {
528 if (edge == wxSASH_BOTTOM)
529 {
530 // Draw a highlight line on the bottom to indicate that the
531 // sash is raised
532 dc.SetPen(hilightPen);
533 dc.DrawLine(0, h - GetEdgeMargin(edge), w, h - GetEdgeMargin(edge));
534 }
535 else
536 {
537 // Draw a drak grey line on the top to indicate that the
538 // sash is raised
539 dc.SetPen(mediumShadowPen);
540 dc.DrawLine(1, GetEdgeMargin(edge), w-1, GetEdgeMargin(edge));
541 }
542 }
543 }
544
545 dc.SetPen(wxNullPen);
546 dc.SetBrush(wxNullBrush);
547 }
548
549 // Draw the sash tracker (for whilst moving the sash)
550 void wxSashWindow::DrawSashTracker(wxSashEdgePosition edge, int x, int y)
551 {
552 int w, h;
553 GetClientSize(&w, &h);
554
555 wxScreenDC screenDC;
556 int x1, y1;
557 int x2, y2;
558
559 if ( edge == wxSASH_LEFT || edge == wxSASH_RIGHT )
560 {
561 x1 = x; y1 = 2;
562 x2 = x; y2 = h-2;
563
564 if ( (edge == wxSASH_LEFT) && (x1 > w) )
565 {
566 x1 = w; x2 = w;
567 }
568 else if ( (edge == wxSASH_RIGHT) && (x1 < 0) )
569 {
570 x1 = 0; x2 = 0;
571 }
572 }
573 else
574 {
575 x1 = 2; y1 = y;
576 x2 = w-2; y2 = y;
577
578 if ( (edge == wxSASH_TOP) && (y1 > h) )
579 {
580 y1 = h;
581 y2 = h;
582 }
583 else if ( (edge == wxSASH_BOTTOM) && (y1 < 0) )
584 {
585 y1 = 0;
586 y2 = 0;
587 }
588 }
589
590 ClientToScreen(&x1, &y1);
591 ClientToScreen(&x2, &y2);
592
593 wxPen sashTrackerPen(*wxBLACK, 2, wxSOLID);
594
595 screenDC.SetLogicalFunction(wxINVERT);
596 screenDC.SetPen(sashTrackerPen);
597 screenDC.SetBrush(*wxTRANSPARENT_BRUSH);
598
599 screenDC.DrawLine(x1, y1, x2, y2);
600
601 screenDC.SetLogicalFunction(wxCOPY);
602
603 screenDC.SetPen(wxNullPen);
604 screenDC.SetBrush(wxNullBrush);
605 }
606
607 // Position and size subwindows.
608 // Note that the border size applies to each subwindow, not
609 // including the edges next to the sash.
610 void wxSashWindow::SizeWindows()
611 {
612 int cw, ch;
613 GetClientSize(&cw, &ch);
614
615 if (GetChildren().GetCount() == 1)
616 {
617 wxWindow* child = GetChildren().GetFirst()->GetData();
618
619 int x = 0;
620 int y = 0;
621 int width = cw;
622 int height = ch;
623
624 // Top
625 if (m_sashes[0].m_show)
626 {
627 y = m_borderSize;
628 height -= m_borderSize;
629 }
630 y += m_extraBorderSize;
631
632 // Left
633 if (m_sashes[3].m_show)
634 {
635 x = m_borderSize;
636 width -= m_borderSize;
637 }
638 x += m_extraBorderSize;
639
640 // Right
641 if (m_sashes[1].m_show)
642 {
643 width -= m_borderSize;
644 }
645 width -= 2*m_extraBorderSize;
646
647 // Bottom
648 if (m_sashes[2].m_show)
649 {
650 height -= m_borderSize;
651 }
652 height -= 2*m_extraBorderSize;
653
654 child->SetSize(x, y, width, height);
655 }
656 else if (GetChildren().GetCount() > 1)
657 {
658 // Perhaps multiple children are themselves sash windows.
659 // TODO: this doesn't really work because the subwindows sizes/positions
660 // must be set to leave a gap for the parent's sash (hit-test and decorations).
661 // Perhaps we can allow for this within LayoutWindow, testing whether the parent
662 // is a sash window, and if so, allowing some space for the edges.
663 wxLayoutAlgorithm layout;
664 layout.LayoutWindow(this);
665 }
666
667 wxClientDC dc(this);
668 DrawBorders(dc);
669 DrawSashes(dc);
670 }
671
672 // Initialize colours
673 void wxSashWindow::InitColours()
674 {
675 // Shadow colours
676 m_faceColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
677 m_mediumShadowColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW);
678 m_darkShadowColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW);
679 m_lightShadowColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT);
680 m_hilightColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DHILIGHT);
681 }
682
683 void wxSashWindow::SetSashVisible(wxSashEdgePosition edge, bool sash)
684 {
685 m_sashes[edge].m_show = sash;
686 if (sash)
687 m_sashes[edge].m_margin = m_borderSize;
688 else
689 m_sashes[edge].m_margin = 0;
690 }
691
692 #if defined( __WXMSW__ ) || defined( __WXMAC__)
693
694 // this is currently called (and needed) under MSW only...
695 void wxSashWindow::OnSetCursor(wxSetCursorEvent& event)
696 {
697 // if we don't do it, the resizing cursor might be set for child window:
698 // and like this we explicitly say that our cursor should not be used for
699 // children windows which overlap us
700
701 if ( SashHitTest(event.GetX(), event.GetY()) != wxSASH_NONE)
702 {
703 // default processing is ok
704 event.Skip();
705 }
706 //else: do nothing, in particular, don't call Skip()
707 }
708
709 #endif // wxMSW
710
711 #endif // wxUSE_SASH