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