]> git.saurik.com Git - wxWidgets.git/blob - src/generic/sashwin.cpp
forgot to commit - no changes
[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 #ifndef WX_PRECOMP
26 #include "wx/wx.h"
27 #endif
28
29 #if wxUSE_SASH
30
31 #include <math.h>
32 #include <stdlib.h>
33
34 #include "wx/string.h"
35 #include "wx/dcscreen.h"
36 #include "wx/sashwin.h"
37 #include "wx/laywin.h"
38
39 DEFINE_EVENT_TYPE(wxEVT_SASH_DRAGGED)
40
41 IMPLEMENT_DYNAMIC_CLASS(wxSashWindow, wxWindow)
42 IMPLEMENT_DYNAMIC_CLASS(wxSashEvent, wxCommandEvent)
43
44 BEGIN_EVENT_TABLE(wxSashWindow, wxWindow)
45 EVT_PAINT(wxSashWindow::OnPaint)
46 EVT_SIZE(wxSashWindow::OnSize)
47 EVT_MOUSE_EVENTS(wxSashWindow::OnMouseEvent)
48 END_EVENT_TABLE()
49
50 bool wxSashWindow::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos,
51 const wxSize& size, long style, const wxString& name)
52 {
53 return wxWindow::Create(parent, id, pos, size, style, name);
54 }
55
56 wxSashWindow::~wxSashWindow()
57 {
58 delete m_sashCursorWE;
59 delete m_sashCursorNS;
60 }
61
62 void wxSashWindow::Init()
63 {
64 m_draggingEdge = wxSASH_NONE;
65 m_dragMode = wxSASH_DRAG_NONE;
66 m_oldX = 0;
67 m_oldY = 0;
68 m_firstX = 0;
69 m_firstY = 0;
70 m_borderSize = 3;
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;
79
80 // Eventually, we'll respond to colour change messages
81 InitColours();
82 }
83
84 void wxSashWindow::OnPaint(wxPaintEvent& WXUNUSED(event))
85 {
86 wxPaintDC dc(this);
87
88 // if ( m_borderSize > 0 )
89 DrawBorders(dc);
90
91 DrawSashes(dc);
92 }
93
94 void wxSashWindow::OnMouseEvent(wxMouseEvent& event)
95 {
96 wxCoord x, y;
97 event.GetPosition(&x, &y);
98
99 wxSashEdgePosition sashHit = SashHitTest(x, y);
100
101 // reset the cursor
102 #if defined(__WXMOTIF__) || defined(__WXGTK__)
103 // Not necessary and in fact inhibits proper cursor setting (JACS 8/2000)
104 //SetCursor(* wxSTANDARD_CURSOR);
105 #endif
106 #ifdef __WXMSW__
107 SetCursor(wxNullCursor);
108 #endif
109
110 if (event.LeftDown())
111 {
112 CaptureMouse();
113 m_mouseCaptured = TRUE;
114
115 if ( sashHit != wxSASH_NONE )
116 {
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;
124
125 while (parent && !parent->IsKindOf(CLASSINFO(wxDialog)) &&
126 !parent->IsKindOf(CLASSINFO(wxFrame)))
127 parent = parent->GetParent();
128
129 wxScreenDC::StartDrawingOnTop(parent);
130
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;
136 m_firstX = x;
137 m_firstY = y;
138
139 if ( (sashHit == wxSASH_LEFT) || (sashHit == wxSASH_RIGHT) )
140 {
141 SetCursor(*m_sashCursorWE);
142 }
143 else
144 {
145 SetCursor(*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 = -1,
188 newWidth = -1;
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 == -1 )
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 == -1 )
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 SetCursor(*m_sashCursorWE);
299 }
300 else
301 {
302 SetCursor(*m_sashCursorNS);
303 }
304 }
305 else
306 {
307 SetCursor(wxNullCursor);
308 }
309 }
310 else if ( event.Dragging() &&
311 ((m_dragMode == wxSASH_DRAG_DRAGGING) ||
312 (m_dragMode == wxSASH_DRAG_LEFT_DOWN)) )
313 {
314 if ( (m_draggingEdge == wxSASH_LEFT) || (m_draggingEdge == wxSASH_RIGHT) )
315 {
316 SetCursor(*m_sashCursorWE);
317 }
318 else
319 {
320 SetCursor(*m_sashCursorNS);
321 }
322
323 if (m_dragMode == wxSASH_DRAG_LEFT_DOWN)
324 {
325 m_dragMode = wxSASH_DRAG_DRAGGING;
326 DrawSashTracker(m_draggingEdge, x, y);
327 }
328 else
329 {
330 if ( m_dragMode == wxSASH_DRAG_DRAGGING )
331 {
332 // Erase old tracker
333 DrawSashTracker(m_draggingEdge, m_oldX, m_oldY);
334
335 // Draw new one
336 DrawSashTracker(m_draggingEdge, x, y);
337 }
338 }
339 m_oldX = x;
340 m_oldY = y;
341 }
342 else if ( event.LeftDClick() )
343 {
344 // Nothing
345 }
346 else
347 {
348 }
349 }
350
351 void wxSashWindow::OnSize(wxSizeEvent& WXUNUSED(event))
352 {
353 SizeWindows();
354 }
355
356 wxSashEdgePosition wxSashWindow::SashHitTest(int x, int y, int WXUNUSED(tolerance))
357 {
358 int cx, cy;
359 GetClientSize(& cx, & cy);
360
361 int i;
362 for (i = 0; i < 4; i++)
363 {
364 wxSashEdge& edge = m_sashes[i];
365 wxSashEdgePosition position = (wxSashEdgePosition) i ;
366
367 if (edge.m_show)
368 {
369 switch (position)
370 {
371 case wxSASH_TOP:
372 {
373 if (y >= 0 && y <= GetEdgeMargin(position))
374 return wxSASH_TOP;
375 break;
376 }
377 case wxSASH_RIGHT:
378 {
379 if ((x >= cx - GetEdgeMargin(position)) && (x <= cx))
380 return wxSASH_RIGHT;
381 break;
382 }
383 case wxSASH_BOTTOM:
384 {
385 if ((y >= cy - GetEdgeMargin(position)) && (y <= cy))
386 return wxSASH_BOTTOM;
387 break;
388 }
389 case wxSASH_LEFT:
390 {
391 if ((x <= GetEdgeMargin(position)) && (x >= 0))
392 return wxSASH_LEFT;
393 break;
394 }
395 case wxSASH_NONE:
396 {
397 break;
398 }
399 }
400 }
401 }
402 return wxSASH_NONE;
403 }
404
405 // Draw 3D effect borders
406 void wxSashWindow::DrawBorders(wxDC& dc)
407 {
408 int w, h;
409 GetClientSize(&w, &h);
410
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);
415
416 if ( GetWindowStyleFlag() & wxSW_3DBORDER )
417 {
418 dc.SetPen(mediumShadowPen);
419 dc.DrawLine(0, 0, w-1, 0);
420 dc.DrawLine(0, 0, 0, h - 1);
421
422 dc.SetPen(darkShadowPen);
423 dc.DrawLine(1, 1, w-2, 1);
424 dc.DrawLine(1, 1, 1, h-2);
425
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.
430
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
434 }
435 else if ( GetWindowStyleFlag() & wxSW_BORDER )
436 {
437 dc.SetBrush(*wxTRANSPARENT_BRUSH);
438 dc.SetPen(*wxBLACK_PEN);
439 dc.DrawRectangle(0, 0, w-1, h-1);
440 }
441
442 dc.SetPen(wxNullPen);
443 dc.SetBrush(wxNullBrush);
444 }
445
446 void wxSashWindow::DrawSashes(wxDC& dc)
447 {
448 int i;
449 for (i = 0; i < 4; i++)
450 if (m_sashes[i].m_show)
451 DrawSash((wxSashEdgePosition) i, dc);
452 }
453
454 // Draw the sash
455 void wxSashWindow::DrawSash(wxSashEdgePosition edge, wxDC& dc)
456 {
457 int w, h;
458 GetClientSize(&w, &h);
459
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);
468
469 if ( edge == wxSASH_LEFT || edge == wxSASH_RIGHT )
470 {
471 int sashPosition = 0;
472 if (edge == wxSASH_LEFT)
473 sashPosition = 0;
474 else
475 sashPosition = w - GetEdgeMargin(edge);
476
477 dc.SetPen(facePen);
478 dc.SetBrush(faceBrush);
479 dc.DrawRectangle(sashPosition, 0, GetEdgeMargin(edge), h);
480
481 if (GetWindowStyleFlag() & wxSW_3DSASH)
482 {
483 if (edge == wxSASH_LEFT)
484 {
485 // Draw a dark grey line on the left to indicate that the
486 // sash is raised
487 dc.SetPen(mediumShadowPen);
488 dc.DrawLine(GetEdgeMargin(edge), 0, GetEdgeMargin(edge), h);
489 }
490 else
491 {
492 // Draw a highlight line on the right to indicate that the
493 // sash is raised
494 dc.SetPen(hilightPen);
495 dc.DrawLine(w - GetEdgeMargin(edge), 0, w - GetEdgeMargin(edge), h);
496 }
497 }
498 }
499 else // top or bottom
500 {
501 int sashPosition = 0;
502 if (edge == wxSASH_TOP)
503 sashPosition = 0;
504 else
505 sashPosition = h - GetEdgeMargin(edge);
506
507 dc.SetPen(facePen);
508 dc.SetBrush(faceBrush);
509 dc.DrawRectangle(0, sashPosition, w, GetEdgeMargin(edge));
510
511 if (GetWindowStyleFlag() & wxSW_3DSASH)
512 {
513 if (edge == wxSASH_BOTTOM)
514 {
515 // Draw a highlight line on the bottom to indicate that the
516 // sash is raised
517 dc.SetPen(hilightPen);
518 dc.DrawLine(0, h - GetEdgeMargin(edge), w, h - GetEdgeMargin(edge));
519 }
520 else
521 {
522 // Draw a drak grey line on the top to indicate that the
523 // sash is raised
524 dc.SetPen(mediumShadowPen);
525 dc.DrawLine(1, GetEdgeMargin(edge), w-1, GetEdgeMargin(edge));
526 }
527 }
528 }
529
530 dc.SetPen(wxNullPen);
531 dc.SetBrush(wxNullBrush);
532 }
533
534 // Draw the sash tracker (for whilst moving the sash)
535 void wxSashWindow::DrawSashTracker(wxSashEdgePosition edge, int x, int y)
536 {
537 int w, h;
538 GetClientSize(&w, &h);
539
540 wxScreenDC screenDC;
541 int x1, y1;
542 int x2, y2;
543
544 if ( edge == wxSASH_LEFT || edge == wxSASH_RIGHT )
545 {
546 x1 = x; y1 = 2;
547 x2 = x; y2 = h-2;
548
549 if ( (edge == wxSASH_LEFT) && (x1 > w) )
550 {
551 x1 = w; x2 = w;
552 }
553 else if ( (edge == wxSASH_RIGHT) && (x1 < 0) )
554 {
555 x1 = 0; x2 = 0;
556 }
557 }
558 else
559 {
560 x1 = 2; y1 = y;
561 x2 = w-2; y2 = y;
562
563 if ( (edge == wxSASH_TOP) && (y1 > h) )
564 {
565 y1 = h;
566 y2 = h;
567 }
568 else if ( (edge == wxSASH_BOTTOM) && (y1 < 0) )
569 {
570 y1 = 0;
571 y2 = 0;
572 }
573 }
574
575 ClientToScreen(&x1, &y1);
576 ClientToScreen(&x2, &y2);
577
578 wxPen sashTrackerPen(*wxBLACK, 2, wxSOLID);
579
580 screenDC.SetLogicalFunction(wxINVERT);
581 screenDC.SetPen(sashTrackerPen);
582 screenDC.SetBrush(*wxTRANSPARENT_BRUSH);
583
584 screenDC.DrawLine(x1, y1, x2, y2);
585
586 screenDC.SetLogicalFunction(wxCOPY);
587
588 screenDC.SetPen(wxNullPen);
589 screenDC.SetBrush(wxNullBrush);
590 }
591
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()
596 {
597 int cw, ch;
598 GetClientSize(&cw, &ch);
599
600 if (GetChildren().Number() == 1)
601 {
602 wxWindow* child = (wxWindow*) (GetChildren().First()->Data());
603
604 int x = 0;
605 int y = 0;
606 int width = cw;
607 int height = ch;
608
609 // Top
610 if (m_sashes[0].m_show)
611 {
612 y = m_borderSize;
613 height -= m_borderSize;
614 }
615 y += m_extraBorderSize;
616
617 // Left
618 if (m_sashes[3].m_show)
619 {
620 x = m_borderSize;
621 width -= m_borderSize;
622 }
623 x += m_extraBorderSize;
624
625 // Right
626 if (m_sashes[1].m_show)
627 {
628 width -= m_borderSize;
629 }
630 width -= 2*m_extraBorderSize;
631
632 // Bottom
633 if (m_sashes[2].m_show)
634 {
635 height -= m_borderSize;
636 }
637 height -= 2*m_extraBorderSize;
638
639 child->SetSize(x, y, width, height);
640 }
641 else if (GetChildren().Number() > 1)
642 {
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);
650 }
651
652 wxClientDC dc(this);
653 DrawBorders(dc);
654 DrawSashes(dc);
655 }
656
657 // Initialize colours
658 void wxSashWindow::InitColours()
659 {
660 // Shadow colours
661 #ifndef __WIN16__
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);
667 #else
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"));
673 #endif
674 }
675
676 void wxSashWindow::SetSashVisible(wxSashEdgePosition edge, bool sash)
677 {
678 m_sashes[edge].m_show = sash;
679 if (sash)
680 m_sashes[edge].m_margin = m_borderSize;
681 else
682 m_sashes[edge].m_margin = 0;
683 }
684
685 #endif // wxUSE_SASH