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