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