]> git.saurik.com Git - wxWidgets.git/blob - src/generic/splitter.cpp
reset m_sashPosition to 0 after calling OnUnsplit(), not before
[wxWidgets.git] / src / generic / splitter.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: splitter.cpp
3 // Purpose: wxSplitterWindow implementation
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "splitter.h"
14 // #pragma interface
15 #endif
16
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
19
20 #ifdef __BORLANDC__
21 #pragma hdrstop
22 #endif
23
24 #ifndef WX_PRECOMP
25 #include "wx/wx.h"
26 #endif
27
28 #include <math.h>
29 #include <stdlib.h>
30
31 #include "wx/string.h"
32 #include "wx/splitter.h"
33 #include "wx/dcscreen.h"
34
35 #if !USE_SHARED_LIBRARY
36 IMPLEMENT_DYNAMIC_CLASS(wxSplitterWindow, wxWindow)
37
38 BEGIN_EVENT_TABLE(wxSplitterWindow, wxWindow)
39 EVT_PAINT(wxSplitterWindow::OnPaint)
40 EVT_SIZE(wxSplitterWindow::OnSize)
41 EVT_MOUSE_EVENTS(wxSplitterWindow::OnMouseEvent)
42 END_EVENT_TABLE()
43 #endif
44
45 wxSplitterWindow::wxSplitterWindow()
46 {
47 m_splitMode = wxSPLIT_VERTICAL;
48 m_windowOne = (wxWindow *) NULL;
49 m_windowTwo = (wxWindow *) NULL;
50 m_dragMode = wxSPLIT_DRAG_NONE;
51 m_oldX = 0;
52 m_oldY = 0;
53 m_firstX = 0;
54 m_firstY = 0;
55 m_sashSize = 7;
56 m_borderSize = 2;
57 m_sashPosition = 0;
58 m_sashCursorWE = (wxCursor *) NULL;
59 m_sashCursorNS = (wxCursor *) NULL;
60 m_sashTrackerPen = (wxPen *) NULL;
61 m_lightShadowPen = (wxPen *) NULL;
62 m_mediumShadowPen = (wxPen *) NULL;
63 m_darkShadowPen = (wxPen *) NULL;
64 m_faceBrush = (wxBrush *) NULL;
65 m_facePen = (wxPen *) NULL;
66 m_hilightPen = (wxPen *) NULL;
67 m_minimumPaneSize = 0;
68 }
69
70 wxSplitterWindow::wxSplitterWindow(wxWindow *parent, wxWindowID id,
71 const wxPoint& pos,
72 const wxSize& size,
73 long style,
74 const wxString& name)
75 : wxWindow(parent, id, pos, size, style, name)
76 {
77 m_splitMode = wxSPLIT_VERTICAL;
78 m_windowOne = (wxWindow *) NULL;
79 m_windowTwo = (wxWindow *) NULL;
80 m_dragMode = wxSPLIT_DRAG_NONE;
81 m_oldX = 0;
82 m_oldY = 0;
83 m_firstX = 0;
84 m_firstY = 0;
85 m_sashSize = 7;
86 m_borderSize = 2;
87 m_sashPosition = 0;
88 m_minimumPaneSize = 0;
89 m_sashCursorWE = new wxCursor(wxCURSOR_SIZEWE);
90 m_sashCursorNS = new wxCursor(wxCURSOR_SIZENS);
91 m_sashTrackerPen = new wxPen(*wxBLACK, 2, wxSOLID);
92 m_lightShadowPen = (wxPen *) NULL;
93 m_mediumShadowPen = (wxPen *) NULL;
94 m_darkShadowPen = (wxPen *) NULL;
95 m_faceBrush = (wxBrush *) NULL;
96 m_facePen = (wxPen *) NULL;
97 m_hilightPen = (wxPen *) NULL;
98
99 if ( style & wxSP_3D )
100 {
101 m_borderSize = 2;
102 m_sashSize = 7;
103 }
104 else if ( style & wxSP_BORDER )
105 {
106 m_borderSize = 1;
107 m_sashSize = 3;
108 }
109 else
110 {
111 m_borderSize = 0;
112 m_sashSize = 3;
113 }
114
115 // Eventually, we'll respond to colour change messages
116 InitColours();
117
118 // For debugging purposes, to see the background.
119 // SetBackground(wxBLUE_BRUSH);
120 }
121
122 wxSplitterWindow::~wxSplitterWindow()
123 {
124 delete m_sashCursorWE;
125 delete m_sashCursorNS;
126 delete m_sashTrackerPen;
127 delete m_lightShadowPen;
128 delete m_darkShadowPen;
129 delete m_mediumShadowPen;
130 delete m_hilightPen;
131 delete m_facePen;
132 delete m_faceBrush;
133 }
134
135 void wxSplitterWindow::OnPaint(wxPaintEvent& WXUNUSED(event))
136 {
137 wxPaintDC dc(this);
138
139 if ( m_borderSize > 0 )
140 DrawBorders(dc);
141 DrawSash(dc);
142 }
143
144 void wxSplitterWindow::OnMouseEvent(wxMouseEvent& event)
145 {
146 long x, y;
147 event.Position(&x, &y);
148
149 if (event.LeftDown())
150 {
151 if ( SashHitTest(x, y) )
152 {
153 CaptureMouse();
154
155 m_dragMode = wxSPLIT_DRAG_DRAGGING;
156
157 DrawSashTracker(x, y);
158 m_oldX = x;
159 m_oldY = y;
160 return;
161 }
162 }
163 else if (event.LeftUp() && m_dragMode == wxSPLIT_DRAG_DRAGGING)
164 {
165 // We can stop dragging now and see what we've got.
166 m_dragMode = wxSPLIT_DRAG_NONE;
167 ReleaseMouse();
168
169 // Erase old tracker
170 DrawSashTracker(m_oldX, m_oldY);
171
172 int w, h;
173 GetClientSize(&w, &h);
174 if ( m_splitMode == wxSPLIT_VERTICAL )
175 {
176 if ( !OnSashPositionChange(x) )
177 return;
178
179 if ( x <= 4 )
180 {
181 // We remove the first window from the view
182 wxWindow *removedWindow = m_windowOne;
183 m_windowOne = m_windowTwo;
184 m_windowTwo = (wxWindow *) NULL;
185
186 OnUnsplit(removedWindow);
187 m_sashPosition = 0;
188 }
189 else if ( x >= (w - 4) )
190 {
191 // We remove the second window from the view
192 wxWindow *removedWindow = m_windowTwo;
193 m_windowTwo = (wxWindow *) NULL;
194 OnUnsplit(removedWindow);
195 m_sashPosition = 0;
196 }
197 else
198 {
199 m_sashPosition = x;
200 }
201 }
202 else // m_splitMode == wxSPLIT_VERTICAL
203 {
204 if ( !OnSashPositionChange(y) )
205 return;
206
207 if ( y <= 4 )
208 {
209 // We remove the first window from the view
210 wxWindow *removedWindow = m_windowOne;
211 m_windowOne = m_windowTwo;
212 m_windowTwo = (wxWindow *) NULL;
213
214 OnUnsplit(removedWindow);
215 m_sashPosition = 0;
216 }
217 else if ( y >= (h - 4) )
218 {
219 // We remove the second window from the view
220 wxWindow *removedWindow = m_windowTwo;
221 m_windowTwo = (wxWindow *) NULL;
222 OnUnsplit(removedWindow);
223 m_sashPosition = 0;
224 }
225 else
226 {
227 m_sashPosition = y;
228 }
229 } // m_splitMode == wxSPLIT_VERTICAL
230 SizeWindows();
231 } // left up && dragging
232 else if (event.Moving() && !event.Dragging())
233 {
234 // Just change the cursor if required
235 if ( SashHitTest(x, y) )
236 {
237 if ( m_splitMode == wxSPLIT_VERTICAL )
238 {
239 SetCursor(*m_sashCursorWE);
240 }
241 else
242 {
243 SetCursor(*m_sashCursorNS);
244 }
245 }
246 else
247 {
248 SetCursor(*wxSTANDARD_CURSOR);
249 }
250 }
251 else if (event.Dragging() && (m_dragMode == wxSPLIT_DRAG_DRAGGING))
252 {
253 // Erase old tracker
254 DrawSashTracker(m_oldX, m_oldY);
255
256 // Draw new one
257 DrawSashTracker(x, y);
258
259 m_oldX = x;
260 m_oldY = y;
261 }
262 else if ( event.LeftDClick() )
263 {
264 OnDoubleClickSash(x, y);
265 }
266 }
267
268 void wxSplitterWindow::OnSize(wxSizeEvent& WXUNUSED(event))
269 {
270 int cw, ch;
271 GetClientSize( &cw, &ch );
272 if ( m_windowTwo )
273 {
274 if ( m_splitMode == wxSPLIT_VERTICAL )
275 {
276 if ( m_sashPosition >= (cw - 5) )
277 m_sashPosition = wxMax(10, cw - 40);
278 }
279 if ( m_splitMode == wxSPLIT_HORIZONTAL )
280 {
281 if ( m_sashPosition >= (ch - 5) )
282 m_sashPosition = wxMax(10, ch - 40);
283 }
284 }
285 SizeWindows();
286 }
287
288 bool wxSplitterWindow::SashHitTest(int x, int y, int tolerance)
289 {
290 if ( m_windowTwo == NULL || m_sashPosition == 0)
291 return FALSE; // No sash
292
293 if ( m_splitMode == wxSPLIT_VERTICAL )
294 {
295 if ( (x >= m_sashPosition - tolerance) && (x <= m_sashPosition + m_sashSize + tolerance) )
296 return TRUE;
297 else
298 return FALSE;
299 }
300 else
301 {
302 if ( (y >= (m_sashPosition- tolerance)) && (y <= (m_sashPosition + m_sashSize + tolerance)) )
303 return TRUE;
304 else
305 return FALSE;
306 }
307
308 return FALSE;
309 }
310
311 // Draw 3D effect borders
312 void wxSplitterWindow::DrawBorders(wxDC& dc)
313 {
314 int w, h;
315 GetClientSize(&w, &h);
316
317 if ( GetWindowStyleFlag() & wxSP_3D )
318 {
319 dc.SetPen(*m_mediumShadowPen);
320 dc.DrawLine(0, 0, w-1, 0);
321 dc.DrawLine(0, 0, 0, h - 1);
322
323 dc.SetPen(*m_darkShadowPen);
324 dc.DrawLine(1, 1, w-2, 1);
325 dc.DrawLine(1, 1, 1, h-2);
326
327 dc.SetPen(*m_hilightPen);
328 dc.DrawLine(0, h-1, w-1, h-1);
329 dc.DrawLine(w-1, 0, w-1, h); // Surely the maximum y pos. should be h - 1.
330 /// Anyway, h is required for MSW.
331
332 dc.SetPen(*m_lightShadowPen);
333 dc.DrawLine(w-2, 1, w-2, h-2); // Right hand side
334 dc.DrawLine(1, h-2, w-1, h-2); // Bottom
335 }
336 else if ( GetWindowStyleFlag() & wxSP_BORDER )
337 {
338 dc.SetBrush(*wxTRANSPARENT_BRUSH);
339 dc.SetPen(*wxBLACK_PEN);
340 dc.DrawRectangle(0, 0, w-1, h-1);
341 }
342
343 dc.SetPen(wxNullPen);
344 dc.SetBrush(wxNullBrush);
345 }
346
347 // Draw the sash
348 void wxSplitterWindow::DrawSash(wxDC& dc)
349 {
350 if ( m_sashPosition == 0 || !m_windowTwo)
351 return;
352
353 int w, h;
354 GetClientSize(&w, &h);
355
356 if ( GetWindowStyleFlag() & wxSP_3D )
357 {
358 if ( m_splitMode == wxSPLIT_VERTICAL )
359 {
360 dc.SetPen(*m_facePen);
361 dc.SetBrush(*m_faceBrush);
362 dc.DrawRectangle(m_sashPosition + 2, 0, m_sashSize - 4, h);
363
364 dc.SetBrush(*wxTRANSPARENT_BRUSH);
365
366 dc.SetPen(*m_lightShadowPen);
367 dc.DrawLine(m_sashPosition, 1, m_sashPosition, h-2);
368
369 dc.SetPen(*m_hilightPen);
370 dc.DrawLine(m_sashPosition+1, 0, m_sashPosition+1, h);
371
372 dc.SetPen(*m_mediumShadowPen);
373 dc.DrawLine(m_sashPosition+m_sashSize-2, 1, m_sashPosition+m_sashSize-2, h-1);
374
375 dc.SetPen(*m_darkShadowPen);
376 dc.DrawLine(m_sashPosition+m_sashSize-1, 2, m_sashPosition+m_sashSize-1, h-2);
377 }
378 else
379 {
380 dc.SetPen(*m_facePen);
381 dc.SetBrush(*m_faceBrush);
382 dc.DrawRectangle(0, m_sashPosition + 2, w, m_sashSize - 4);
383
384 dc.SetBrush(*wxTRANSPARENT_BRUSH);
385
386 dc.SetPen(*m_lightShadowPen);
387 dc.DrawLine(1, m_sashPosition, w-2, m_sashPosition);
388
389 dc.SetPen(*m_hilightPen);
390 dc.DrawLine(0, m_sashPosition+1, w, m_sashPosition+1);
391
392 dc.SetPen(*m_mediumShadowPen);
393 dc.DrawLine(1, m_sashPosition+m_sashSize-2, w-1, m_sashPosition+m_sashSize-2);
394
395 dc.SetPen(*m_darkShadowPen);
396 dc.DrawLine(2, m_sashPosition+m_sashSize-1, w-2, m_sashPosition+m_sashSize-1);
397 }
398 }
399 else
400 {
401 if ( m_splitMode == wxSPLIT_VERTICAL )
402 {
403 dc.SetPen(*wxBLACK_PEN);
404 dc.SetBrush(*wxBLACK_BRUSH);
405 int h1 = h-1;
406 if ( (GetWindowStyleFlag() & wxSP_BORDER) != wxSP_BORDER )
407 h1 += 1; // Not sure why this is necessary...
408 dc.DrawRectangle(m_sashPosition, 0, m_sashSize, h1);
409 }
410 else
411 {
412 dc.SetPen(*wxBLACK_PEN);
413 dc.SetBrush(*wxBLACK_BRUSH);
414 int w1 = w-1;
415 if ( (GetWindowStyleFlag() & wxSP_BORDER) != wxSP_BORDER )
416 w1 ++;
417
418 dc.DrawRectangle(0, m_sashPosition, w1, m_sashSize);
419 }
420
421 }
422
423 dc.SetPen(wxNullPen);
424 dc.SetBrush(wxNullBrush);
425 }
426
427 // Draw the sash tracker (for whilst moving the sash)
428 void wxSplitterWindow::DrawSashTracker(int x, int y)
429 {
430 int w, h;
431 GetClientSize(&w, &h);
432
433 wxScreenDC screenDC;
434 int x1, y1;
435 int x2, y2;
436
437 if ( m_splitMode == wxSPLIT_VERTICAL )
438 {
439 x1 = x; y1 = 2;
440 x2 = x; y2 = h-2;
441
442 if ( x1 > w )
443 {
444 x1 = w; x2 = w;
445 }
446 else if ( x1 < 0 )
447 {
448 x1 = 0; x2 = 0;
449 }
450 }
451 else
452 {
453 x1 = 2; y1 = y;
454 x2 = w-2; y2 = y;
455
456 if ( y1 > h )
457 {
458 y1 = h;
459 y2 = h;
460 }
461 else if ( y1 < 0 )
462 {
463 y1 = 0;
464 y2 = 0;
465 }
466 }
467
468 ClientToScreen(&x1, &y1);
469 ClientToScreen(&x2, &y2);
470
471 screenDC.SetLogicalFunction(wxXOR);
472 screenDC.SetPen(*m_sashTrackerPen);
473 screenDC.SetBrush(*wxTRANSPARENT_BRUSH);
474
475 screenDC.DrawLine(x1, y1, x2, y2);
476
477 screenDC.SetLogicalFunction(wxCOPY);
478
479 screenDC.SetPen(wxNullPen);
480 screenDC.SetBrush(wxNullBrush);
481 }
482
483 // Position and size subwindows.
484 // Note that the border size applies to each subwindow, not
485 // including the edges next to the sash.
486 void wxSplitterWindow::SizeWindows()
487 {
488 int w, h;
489 GetClientSize(&w, &h);
490
491 if ( m_windowOne && !m_windowTwo )
492 {
493 m_windowOne->SetSize(m_borderSize, m_borderSize, w - 2*m_borderSize, h - 2*m_borderSize);
494 }
495 else if ( m_windowOne && m_windowTwo )
496 {
497 if (m_splitMode == wxSPLIT_VERTICAL)
498 {
499 int x1 = m_borderSize;
500 int y1 = m_borderSize;
501 int w1 = m_sashPosition - m_borderSize;
502 int h1 = h - 2*m_borderSize;
503
504 int x2 = m_sashPosition + m_sashSize;
505 int y2 = m_borderSize;
506 int w2 = w - 2*m_borderSize - m_sashSize - w1;
507 int h2 = h - 2*m_borderSize;
508
509 m_windowOne->SetSize(x1, y1, w1, h1);
510 m_windowTwo->SetSize(x2, y2, w2, h2);
511 }
512 else
513 {
514 m_windowOne->SetSize(m_borderSize, m_borderSize,
515 w - 2*m_borderSize, m_sashPosition - m_borderSize);
516 m_windowTwo->SetSize(m_borderSize, m_sashPosition + m_sashSize,
517 w - 2*m_borderSize, h - 2*m_borderSize - m_sashSize - (m_sashPosition - m_borderSize));
518 }
519 }
520 wxClientDC dc(this);
521 DrawBorders(dc);
522 DrawSash(dc);
523 }
524
525 // Set pane for unsplit window
526 void wxSplitterWindow::Initialize(wxWindow *window)
527 {
528 m_windowOne = window;
529 m_windowTwo = (wxWindow *) NULL;
530 m_sashPosition = 0;
531 }
532
533 // Associates the given window with window 2, drawing the appropriate sash
534 // and changing the split mode.
535 // Does nothing and returns FALSE if the window is already split.
536 bool wxSplitterWindow::SplitVertically(wxWindow *window1, wxWindow *window2, int sashPosition)
537 {
538 if ( IsSplit() )
539 return FALSE;
540
541 int w, h;
542 GetClientSize(&w, &h);
543
544 m_splitMode = wxSPLIT_VERTICAL;
545 m_windowOne = window1;
546 m_windowTwo = window2;
547 if ( sashPosition > 0 )
548 m_sashPosition = sashPosition;
549 else if ( sashPosition < 0 )
550 m_sashPosition = w - sashPosition;
551 else // default
552 m_sashPosition = w/2;
553
554 SizeWindows();
555
556 return TRUE;
557 }
558
559 bool wxSplitterWindow::SplitHorizontally(wxWindow *window1, wxWindow *window2, int sashPosition)
560 {
561 if ( IsSplit() )
562 return FALSE;
563
564 int w, h;
565 GetClientSize(&w, &h);
566
567 m_splitMode = wxSPLIT_HORIZONTAL;
568 m_windowOne = window1;
569 m_windowTwo = window2;
570 if ( sashPosition > 0 )
571 m_sashPosition = sashPosition;
572 else if ( sashPosition < 0 )
573 m_sashPosition = h - sashPosition;
574 else // default
575 m_sashPosition = h/2;
576
577 SizeWindows();
578
579 return TRUE;
580 }
581
582
583 // Remove the specified (or second) window from the view
584 // Doesn't actually delete the window.
585 bool wxSplitterWindow::Unsplit(wxWindow *toRemove)
586 {
587 if ( ! IsSplit() )
588 return FALSE;
589
590 if ( toRemove == NULL || toRemove == m_windowTwo)
591 {
592 wxWindow *win = m_windowTwo ;
593 m_windowTwo = (wxWindow *) NULL;
594 OnUnsplit(win);
595 m_sashPosition = 0;
596 SizeWindows();
597 }
598 else if ( toRemove == m_windowOne )
599 {
600 wxWindow *win = m_windowOne ;
601 m_windowOne = m_windowTwo;
602 m_windowTwo = (wxWindow *) NULL;
603 OnUnsplit(win);
604 m_sashPosition = 0;
605 SizeWindows();
606 }
607 else
608 {
609 wxFAIL_MSG("splitter: attempt to remove a non-existent window");
610
611 return FALSE;
612 }
613
614 return TRUE;
615 }
616
617 void wxSplitterWindow::SetSashPosition(int position, bool redraw)
618 {
619 m_sashPosition = position;
620
621 if ( redraw )
622 {
623 SizeWindows();
624 }
625 }
626
627 bool wxSplitterWindow::OnSashPositionChange(int newSashPosition)
628 {
629 // is the left/upper pane too small?
630 if ( newSashPosition < m_minimumPaneSize )
631 return NULL;
632
633 // is the right/lower pane too small?
634 int w, h;
635 GetClientSize(&w, &h);
636
637 if ( m_splitMode == wxSPLIT_VERTICAL )
638 {
639 if ( w - newSashPosition < m_minimumPaneSize )
640 return FALSE;
641 }
642 else // m_splitMode = wxSPLIT_HORIZONTAL
643 {
644 if ( h - newSashPosition < m_minimumPaneSize )
645 return FALSE;
646 }
647
648 // it's ok to move sash
649 return TRUE;
650 }
651
652 // Called when the sash is double-clicked.
653 // The default behaviour is to remove the sash if the
654 // minimum pane size is zero.
655 void wxSplitterWindow::OnDoubleClickSash(int WXUNUSED(x), int WXUNUSED(y) )
656 {
657 if ( GetMinimumPaneSize() == 0 )
658 {
659 Unsplit();
660 }
661 }
662
663 // Initialize colours
664 void wxSplitterWindow::InitColours()
665 {
666 wxDELETE( m_facePen );
667 wxDELETE( m_faceBrush );
668 wxDELETE( m_mediumShadowPen );
669 wxDELETE( m_darkShadowPen );
670 wxDELETE( m_lightShadowPen );
671 wxDELETE( m_hilightPen );
672
673 // Shadow colours
674 #if defined(__WIN95__)
675 wxColour faceColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
676 m_facePen = new wxPen(faceColour, 1, wxSOLID);
677 m_faceBrush = new wxBrush(faceColour, wxSOLID);
678
679 wxColour mediumShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DSHADOW));
680 m_mediumShadowPen = new wxPen(mediumShadowColour, 1, wxSOLID);
681
682 wxColour darkShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DDKSHADOW));
683 m_darkShadowPen = new wxPen(darkShadowColour, 1, wxSOLID);
684
685 wxColour lightShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT));
686 m_lightShadowPen = new wxPen(lightShadowColour, 1, wxSOLID);
687
688 wxColour hilightColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DHILIGHT));
689 m_hilightPen = new wxPen(hilightColour, 1, wxSOLID);
690 #else // !Win32
691 m_facePen = new wxPen("LIGHT GREY", 1, wxSOLID);
692 m_faceBrush = new wxBrush("LIGHT GREY", wxSOLID);
693 m_mediumShadowPen = new wxPen("GREY", 1, wxSOLID);
694 m_darkShadowPen = new wxPen("BLACK", 1, wxSOLID);
695 m_lightShadowPen = new wxPen("LIGHT GREY", 1, wxSOLID);
696 m_hilightPen = new wxPen("WHITE", 1, wxSOLID);
697 #endif // Win32/!Win32
698 }
699