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