]> git.saurik.com Git - wxWidgets.git/blob - src/generic/splitter.cpp
Applied dir control token patch.
[wxWidgets.git] / src / generic / splitter.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/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
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "splitter.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #if wxUSE_SPLITTER
20
21 #ifdef __BORLANDC__
22 #pragma hdrstop
23 #endif
24
25 #ifndef WX_PRECOMP
26 #include "wx/string.h"
27 #include "wx/utils.h"
28 #include "wx/log.h"
29
30 #include "wx/dcscreen.h"
31
32 #include "wx/window.h"
33 #include "wx/dialog.h"
34 #include "wx/frame.h"
35
36 #include "wx/settings.h"
37 #endif
38
39 #ifdef __WXMAC__
40 #include "wx/mac/private.h"
41 #endif
42
43 #include "wx/renderer.h"
44
45 #include "wx/splitter.h"
46
47 #include <stdlib.h>
48
49 DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED)
50 DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING)
51 DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPLITTER_DOUBLECLICKED)
52 DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPLITTER_UNSPLIT)
53
54 IMPLEMENT_DYNAMIC_CLASS(wxSplitterWindow, wxWindow)
55
56 /*
57 TODO PROPERTIES
58 style wxSP_3D
59 sashpos (long , 0 )
60 minsize (long -1 )
61 object, object_ref
62 orientation
63 */
64
65 IMPLEMENT_DYNAMIC_CLASS(wxSplitterEvent, wxNotifyEvent)
66
67 BEGIN_EVENT_TABLE(wxSplitterWindow, wxWindow)
68 EVT_PAINT(wxSplitterWindow::OnPaint)
69 EVT_SIZE(wxSplitterWindow::OnSize)
70 EVT_MOUSE_EVENTS(wxSplitterWindow::OnMouseEvent)
71
72 #if defined( __WXMSW__ ) || defined( __WXMAC__)
73 EVT_SET_CURSOR(wxSplitterWindow::OnSetCursor)
74 #endif // wxMSW
75
76 WX_EVENT_TABLE_CONTROL_CONTAINER(wxSplitterWindow)
77 END_EVENT_TABLE()
78
79 WX_DELEGATE_TO_CONTROL_CONTAINER(wxSplitterWindow);
80
81 bool wxSplitterWindow::Create(wxWindow *parent, wxWindowID id,
82 const wxPoint& pos,
83 const wxSize& size,
84 long style,
85 const wxString& name)
86 {
87 // allow TABbing from one window to the other
88 style |= wxTAB_TRAVERSAL;
89
90 // we draw our border ourselves to blend the sash with it
91 style &= ~wxBORDER_MASK;
92 style |= wxBORDER_NONE;
93
94 if ( !wxWindow::Create(parent, id, pos, size, style, name) )
95 return false;
96
97 m_permitUnsplitAlways = (style & wxSP_PERMIT_UNSPLIT) != 0;
98
99 // FIXME: with this line the background is not erased at all under GTK1,
100 // so temporary avoid it there
101 #if !defined(__WXGTK__) || defined(__WXGTK20__)
102 // don't erase the splitter background, it's pointless as we overwrite it
103 // anyhow
104 SetBackgroundStyle(wxBG_STYLE_CUSTOM);
105 #endif
106
107 return true;
108 }
109
110 void wxSplitterWindow::Init()
111 {
112 m_container.SetContainerWindow(this);
113
114 m_splitMode = wxSPLIT_VERTICAL;
115 m_permitUnsplitAlways = true;
116 m_windowOne = (wxWindow *) NULL;
117 m_windowTwo = (wxWindow *) NULL;
118 m_dragMode = wxSPLIT_DRAG_NONE;
119 m_oldX = 0;
120 m_oldY = 0;
121 m_firstX = 0;
122 m_firstY = 0;
123 m_sashPosition = m_requestedSashPosition = 0;
124 m_sashGravity = 0.0;
125 m_lastSize = wxSize(0,0);
126 m_checkRequestedSashPosition = false;
127 m_minimumPaneSize = 0;
128 m_sashCursorWE = wxCursor(wxCURSOR_SIZEWE);
129 m_sashCursorNS = wxCursor(wxCURSOR_SIZENS);
130 m_sashTrackerPen = new wxPen(*wxBLACK, 2, wxSOLID);
131
132 m_needUpdating = false;
133 m_isHot = false;
134 }
135
136 wxSplitterWindow::~wxSplitterWindow()
137 {
138 delete m_sashTrackerPen;
139 }
140
141 // ----------------------------------------------------------------------------
142 // entering/leaving sash
143 // ----------------------------------------------------------------------------
144
145 void wxSplitterWindow::RedrawIfHotSensitive(bool isHot)
146 {
147 if ( wxRendererNative::Get().GetSplitterParams(this).isHotSensitive )
148 {
149 m_isHot = isHot;
150
151 wxClientDC dc(this);
152 DrawSash(dc);
153 }
154 //else: we don't change our appearance, don't redraw to avoid flicker
155 }
156
157 void wxSplitterWindow::OnEnterSash()
158 {
159 SetResizeCursor();
160
161 RedrawIfHotSensitive(true);
162 }
163
164 void wxSplitterWindow::OnLeaveSash()
165 {
166 SetCursor(*wxSTANDARD_CURSOR);
167
168 RedrawIfHotSensitive(false);
169 }
170
171 void wxSplitterWindow::SetResizeCursor()
172 {
173 SetCursor(m_splitMode == wxSPLIT_VERTICAL ? m_sashCursorWE
174 : m_sashCursorNS);
175 }
176
177 // ----------------------------------------------------------------------------
178 // other event handlers
179 // ----------------------------------------------------------------------------
180
181 void wxSplitterWindow::OnPaint(wxPaintEvent& WXUNUSED(event))
182 {
183 wxPaintDC dc(this);
184
185 DrawSash(dc);
186 }
187
188 void wxSplitterWindow::OnInternalIdle()
189 {
190 wxWindow::OnInternalIdle();
191
192 // if this is the first idle time after a sash position has potentially
193 // been set, allow SizeWindows to check for a requested size.
194 if (!m_checkRequestedSashPosition)
195 {
196 m_checkRequestedSashPosition = true;
197 SizeWindows();
198 return; // it won't needUpdating in this case
199 }
200
201 if (m_needUpdating)
202 SizeWindows();
203 }
204
205 void wxSplitterWindow::OnMouseEvent(wxMouseEvent& event)
206 {
207 int x = (int)event.GetX(),
208 y = (int)event.GetY();
209
210 if (GetWindowStyle() & wxSP_NOSASH)
211 return;
212
213 // with wxSP_LIVE_UPDATE style the splitter windows are always resized
214 // following the mouse movement while it drags the sash, without it we only
215 // draw the sash at the new position but only resize the windows when the
216 // dragging is finished
217 #if defined( __WXMAC__ ) && TARGET_API_MAC_OSX == 1
218 bool isLive = true ;
219 #else
220 bool isLive = (GetWindowStyleFlag() & wxSP_LIVE_UPDATE) != 0;
221 #endif
222 if (event.LeftDown())
223 {
224 if ( SashHitTest(x, y) )
225 {
226 // Start the drag now
227 m_dragMode = wxSPLIT_DRAG_DRAGGING;
228
229 // Capture mouse and set the cursor
230 CaptureMouse();
231 SetResizeCursor();
232
233 if ( !isLive )
234 {
235 // remember the initial sash position and draw the initial
236 // shadow sash
237 m_sashPositionCurrent = m_sashPosition;
238
239 DrawSashTracker(x, y);
240 }
241
242 m_oldX = x;
243 m_oldY = y;
244
245 SetResizeCursor();
246 return;
247 }
248 }
249 else if (event.LeftUp() && m_dragMode == wxSPLIT_DRAG_DRAGGING)
250 {
251 // We can stop dragging now and see what we've got.
252 m_dragMode = wxSPLIT_DRAG_NONE;
253
254 // Release mouse and unset the cursor
255 ReleaseMouse();
256 SetCursor(* wxSTANDARD_CURSOR);
257
258 // exit if unsplit after doubleclick
259 if ( !IsSplit() )
260 {
261 return;
262 }
263
264 // Erase old tracker
265 if ( !isLive )
266 {
267 DrawSashTracker(m_oldX, m_oldY);
268 }
269
270 // the position of the click doesn't exactly correspond to
271 // m_sashPosition, rather it changes it by the distance by which the
272 // mouse has moved
273 int diff = m_splitMode == wxSPLIT_VERTICAL ? x - m_oldX : y - m_oldY;
274
275 int posSashOld = isLive ? m_sashPosition : m_sashPositionCurrent;
276 int posSashNew = OnSashPositionChanging(posSashOld + diff);
277 if ( posSashNew == -1 )
278 {
279 // change not allowed
280 return;
281 }
282
283 if ( m_permitUnsplitAlways || m_minimumPaneSize == 0 )
284 {
285 // Deal with possible unsplit scenarios
286 if ( posSashNew == 0 )
287 {
288 // We remove the first window from the view
289 wxWindow *removedWindow = m_windowOne;
290 m_windowOne = m_windowTwo;
291 m_windowTwo = (wxWindow *) NULL;
292 OnUnsplit(removedWindow);
293 wxSplitterEvent event(wxEVT_COMMAND_SPLITTER_UNSPLIT, this);
294 event.m_data.win = removedWindow;
295 (void)DoSendEvent(event);
296 SetSashPositionAndNotify(0);
297 }
298 else if ( posSashNew == GetWindowSize() )
299 {
300 // We remove the second window from the view
301 wxWindow *removedWindow = m_windowTwo;
302 m_windowTwo = (wxWindow *) NULL;
303 OnUnsplit(removedWindow);
304 wxSplitterEvent event(wxEVT_COMMAND_SPLITTER_UNSPLIT, this);
305 event.m_data.win = removedWindow;
306 (void)DoSendEvent(event);
307 SetSashPositionAndNotify(0);
308 }
309 else
310 {
311 SetSashPositionAndNotify(posSashNew);
312 }
313 }
314 else
315 {
316 SetSashPositionAndNotify(posSashNew);
317 }
318
319 SizeWindows();
320 } // left up && dragging
321 else if ((event.Moving() || event.Leaving() || event.Entering()) && (m_dragMode == wxSPLIT_DRAG_NONE))
322 {
323 if ( event.Leaving() || !SashHitTest(x, y) )
324 OnLeaveSash();
325 else
326 OnEnterSash();
327 }
328 else if (event.Dragging() && (m_dragMode == wxSPLIT_DRAG_DRAGGING))
329 {
330 int diff = m_splitMode == wxSPLIT_VERTICAL ? x - m_oldX : y - m_oldY;
331 if ( !diff )
332 {
333 // nothing to do, mouse didn't really move far enough
334 return;
335 }
336
337 int posSashOld = isLive ? m_sashPosition : m_sashPositionCurrent;
338 int posSashNew = OnSashPositionChanging(posSashOld + diff);
339 if ( posSashNew == -1 )
340 {
341 // change not allowed
342 return;
343 }
344
345 if ( posSashNew == m_sashPosition )
346 return;
347
348 // Erase old tracker
349 if ( !isLive )
350 {
351 DrawSashTracker(m_oldX, m_oldY);
352 }
353
354 if (m_splitMode == wxSPLIT_VERTICAL)
355 x = posSashNew;
356 else
357 y = posSashNew;
358
359 // Remember old positions
360 m_oldX = x;
361 m_oldY = y;
362
363 #ifdef __WXMSW__
364 // As we captured the mouse, we may get the mouse events from outside
365 // our window - for example, negative values in x, y. This has a weird
366 // consequence under MSW where we use unsigned values sometimes and
367 // signed ones other times: the coordinates turn as big positive
368 // numbers and so the sash is drawn on the *right* side of the window
369 // instead of the left (or bottom instead of top). Correct this.
370 if ( (short)m_oldX < 0 )
371 m_oldX = 0;
372 if ( (short)m_oldY < 0 )
373 m_oldY = 0;
374 #endif // __WXMSW__
375
376 // Draw new one
377 if ( !isLive )
378 {
379 m_sashPositionCurrent = posSashNew;
380
381 DrawSashTracker(m_oldX, m_oldY);
382 }
383 else
384 {
385 SetSashPositionAndNotify(posSashNew);
386 m_needUpdating = true;
387 }
388 }
389 else if ( event.LeftDClick() && m_windowTwo )
390 {
391 OnDoubleClickSash(x, y);
392 }
393 }
394
395 void wxSplitterWindow::OnSize(wxSizeEvent& event)
396 {
397 // only process this message if we're not iconized - otherwise iconizing
398 // and restoring a window containing the splitter has a funny side effect
399 // of changing the splitter position!
400 wxWindow *parent = wxGetTopLevelParent(this);
401 bool iconized;
402
403 wxTopLevelWindow *winTop = wxDynamicCast(parent, wxTopLevelWindow);
404 if ( winTop )
405 {
406 iconized = winTop->IsIconized();
407 }
408 else
409 {
410 wxFAIL_MSG(wxT("should have a top level parent!"));
411
412 iconized = false;
413 }
414
415 if ( iconized )
416 {
417 m_lastSize = wxSize(0,0);
418
419 event.Skip();
420
421 return;
422 }
423
424 if ( m_windowTwo )
425 {
426 int w, h;
427 GetClientSize(&w, &h);
428
429 int size = m_splitMode == wxSPLIT_VERTICAL ? w : h;
430
431 int old_size = m_splitMode == wxSPLIT_VERTICAL ? m_lastSize.x : m_lastSize.y;
432 if ( old_size != 0 )
433 {
434 int delta = (int) ( (size - old_size)*m_sashGravity );
435 if ( delta != 0 )
436 {
437 int newPosition = m_sashPosition + delta;
438 if( newPosition < m_minimumPaneSize )
439 newPosition = m_minimumPaneSize;
440 SetSashPositionAndNotify(newPosition);
441 }
442 }
443
444 if ( m_sashPosition >= size - 5 )
445 SetSashPositionAndNotify(wxMax(10, size - 40));
446 m_lastSize = wxSize(w,h);
447 }
448
449 SizeWindows();
450 }
451
452 void wxSplitterWindow::SetSashGravity(double gravity)
453 {
454 wxCHECK_RET( gravity >= 0. && gravity <= 1.,
455 _T("invalid gravity value") );
456
457 m_sashGravity = gravity;
458 }
459
460 bool wxSplitterWindow::SashHitTest(int x, int y, int tolerance)
461 {
462 if ( m_windowTwo == NULL || m_sashPosition == 0)
463 return false; // No sash
464
465 int z = m_splitMode == wxSPLIT_VERTICAL ? x : y;
466 int hitMin = m_sashPosition - tolerance;
467 int hitMax = m_sashPosition + GetSashSize() + tolerance;
468
469 return z >= hitMin && z <= hitMax;
470 }
471
472 int wxSplitterWindow::GetSashSize() const
473 {
474 return wxRendererNative::Get().GetSplitterParams(this).widthSash;
475 }
476
477 int wxSplitterWindow::GetBorderSize() const
478 {
479 return wxRendererNative::Get().GetSplitterParams(this).border;
480 }
481
482 // Draw the sash
483 void wxSplitterWindow::DrawSash(wxDC& dc)
484 {
485 if (HasFlag(wxSP_3DBORDER))
486 wxRendererNative::Get().DrawSplitterBorder
487 (
488 this,
489 dc,
490 GetClientRect()
491 );
492
493 // don't draw sash if we're not split
494 if ( m_sashPosition == 0 || !m_windowTwo )
495 return;
496
497 // nor if we're configured to not show it
498 if ( HasFlag(wxSP_NOSASH) )
499 return;
500
501 wxRendererNative::Get().DrawSplitterSash
502 (
503 this,
504 dc,
505 GetClientSize(),
506 m_sashPosition,
507 m_splitMode == wxSPLIT_VERTICAL ? wxVERTICAL
508 : wxHORIZONTAL,
509 m_isHot ? (int)wxCONTROL_CURRENT : 0
510 );
511 }
512
513 // Draw the sash tracker (for whilst moving the sash)
514 void wxSplitterWindow::DrawSashTracker(int x, int y)
515 {
516 int w, h;
517 GetClientSize(&w, &h);
518
519 wxScreenDC screenDC;
520 int x1, y1;
521 int x2, y2;
522
523 if ( m_splitMode == wxSPLIT_VERTICAL )
524 {
525 x1 = x; y1 = 2;
526 x2 = x; y2 = h-2;
527
528 if ( x1 > w )
529 {
530 x1 = w; x2 = w;
531 }
532 else if ( x1 < 0 )
533 {
534 x1 = 0; x2 = 0;
535 }
536 }
537 else
538 {
539 x1 = 2; y1 = y;
540 x2 = w-2; y2 = y;
541
542 if ( y1 > h )
543 {
544 y1 = h;
545 y2 = h;
546 }
547 else if ( y1 < 0 )
548 {
549 y1 = 0;
550 y2 = 0;
551 }
552 }
553
554 ClientToScreen(&x1, &y1);
555 ClientToScreen(&x2, &y2);
556
557 screenDC.SetLogicalFunction(wxINVERT);
558 screenDC.SetPen(*m_sashTrackerPen);
559 screenDC.SetBrush(*wxTRANSPARENT_BRUSH);
560
561 screenDC.DrawLine(x1, y1, x2, y2);
562
563 screenDC.SetLogicalFunction(wxCOPY);
564 }
565
566 int wxSplitterWindow::GetWindowSize() const
567 {
568 wxSize size = GetClientSize();
569
570 return m_splitMode == wxSPLIT_VERTICAL ? size.x : size.y;
571 }
572
573 int wxSplitterWindow::AdjustSashPosition(int sashPos) const
574 {
575 int window_size = GetWindowSize();
576
577 wxWindow *win;
578
579 win = GetWindow1();
580 if ( win )
581 {
582 // the window shouldn't be smaller than its own minimal size nor
583 // smaller than the minimual pane size specified for this splitter
584 int minSize = m_splitMode == wxSPLIT_VERTICAL ? win->GetMinWidth()
585 : win->GetMinHeight();
586
587 if ( minSize == -1 || m_minimumPaneSize > minSize )
588 minSize = m_minimumPaneSize;
589
590 minSize += GetBorderSize();
591
592 if ( sashPos < minSize )
593 sashPos = minSize;
594 }
595
596 win = GetWindow2();
597 if ( win )
598 {
599 int minSize = m_splitMode == wxSPLIT_VERTICAL ? win->GetMinWidth()
600 : win->GetMinHeight();
601
602 if ( minSize == -1 || m_minimumPaneSize > minSize )
603 minSize = m_minimumPaneSize;
604
605 int maxSize = window_size - minSize - GetBorderSize() - GetSashSize();
606 if ( sashPos > maxSize )
607 sashPos = maxSize;
608 }
609
610 return sashPos;
611 }
612
613 bool wxSplitterWindow::DoSetSashPosition(int sashPos)
614 {
615 int newSashPosition = AdjustSashPosition(sashPos);
616
617 if ( newSashPosition == m_sashPosition )
618 return false;
619
620 m_sashPosition = newSashPosition;
621
622 return true;
623 }
624
625 void wxSplitterWindow::SetSashPositionAndNotify(int sashPos)
626 {
627 // we must reset the request here, otherwise the sash would be stuck at
628 // old position if the user attempted to move the sash after invalid
629 // (e.g. smaller than minsize) sash position was requested using
630 // SetSashPosition():
631 m_requestedSashPosition = INT_MAX;
632
633 if ( DoSetSashPosition(sashPos) )
634 {
635 wxSplitterEvent event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED, this);
636 event.m_data.pos = m_sashPosition;
637
638 (void)DoSendEvent(event);
639 }
640 }
641
642 // Position and size subwindows.
643 // Note that the border size applies to each subwindow, not
644 // including the edges next to the sash.
645 void wxSplitterWindow::SizeWindows()
646 {
647 // check if we have delayed setting the real sash position
648 if ( m_checkRequestedSashPosition && m_requestedSashPosition != INT_MAX )
649 {
650 int newSashPosition = ConvertSashPosition(m_requestedSashPosition);
651 if ( newSashPosition != m_sashPosition )
652 {
653 DoSetSashPosition(newSashPosition);
654 }
655
656 if ( newSashPosition <= m_sashPosition
657 && newSashPosition >= m_sashPosition - GetBorderSize() )
658 {
659 // don't update it any more
660 m_requestedSashPosition = INT_MAX;
661 }
662 }
663
664 int w, h;
665 GetClientSize(&w, &h);
666
667 if ( GetWindow1() && !GetWindow2() )
668 {
669 GetWindow1()->SetSize(GetBorderSize(), GetBorderSize(),
670 w - 2*GetBorderSize(), h - 2*GetBorderSize());
671 }
672 else if ( GetWindow1() && GetWindow2() )
673 {
674 const int border = GetBorderSize(),
675 sash = GetSashSize();
676
677 int size1 = GetSashPosition() - border,
678 size2 = GetSashPosition() + sash;
679
680 int x2, y2, w1, h1, w2, h2;
681 if ( GetSplitMode() == wxSPLIT_VERTICAL )
682 {
683 w1 = size1;
684 w2 = w - 2*border - sash - w1;
685 h1 =
686 h2 = h - 2*border;
687 x2 = size2;
688 y2 = border;
689 }
690 else // horz splitter
691 {
692 w1 =
693 w2 = w - 2*border;
694 h1 = size1;
695 h2 = h - 2*border - sash - h1;
696 x2 = border;
697 y2 = size2;
698 }
699
700 GetWindow1()->SetSize(border, border, w1, h1);
701 GetWindow2()->SetSize(x2, y2, w2, h2);
702 }
703
704 wxClientDC dc(this);
705 DrawSash(dc);
706
707 SetNeedUpdating(false);
708 }
709
710 // Set pane for unsplit window
711 void wxSplitterWindow::Initialize(wxWindow *window)
712 {
713 wxASSERT_MSG( (!window || (window && window->GetParent() == this)),
714 _T("windows in the splitter should have it as parent!") );
715
716 if (! window->IsShown())
717 window->Show();
718
719 m_windowOne = window;
720 m_windowTwo = (wxWindow *) NULL;
721 DoSetSashPosition(0);
722 }
723
724 // Associates the given window with window 2, drawing the appropriate sash
725 // and changing the split mode.
726 // Does nothing and returns false if the window is already split.
727 bool wxSplitterWindow::DoSplit(wxSplitMode mode,
728 wxWindow *window1, wxWindow *window2,
729 int sashPosition)
730 {
731 if ( IsSplit() )
732 return false;
733
734 wxCHECK_MSG( window1 && window2, false,
735 _T("can not split with NULL window(s)") );
736
737 wxCHECK_MSG( window1->GetParent() == this && window2->GetParent() == this, false,
738 _T("windows in the splitter should have it as parent!") );
739
740 if (! window1->IsShown())
741 window1->Show();
742 if (! window2->IsShown())
743 window2->Show();
744
745 m_splitMode = mode;
746 m_windowOne = window1;
747 m_windowTwo = window2;
748
749 // remember the sash position we want to set for later if we can't set it
750 // right now (e.g. because the window is too small)
751 m_requestedSashPosition = sashPosition;
752 m_checkRequestedSashPosition = false;
753
754 DoSetSashPosition(ConvertSashPosition(sashPosition));
755
756 SizeWindows();
757
758 return true;
759 }
760
761 int wxSplitterWindow::ConvertSashPosition(int sashPosition) const
762 {
763 if ( sashPosition > 0 )
764 {
765 return sashPosition;
766 }
767 else if ( sashPosition < 0 )
768 {
769 // It's negative so adding is subtracting
770 return GetWindowSize() + sashPosition;
771 }
772 else // sashPosition == 0
773 {
774 // default, put it in the centre
775 return GetWindowSize() / 2;
776 }
777 }
778
779 // Remove the specified (or second) window from the view
780 // Doesn't actually delete the window.
781 bool wxSplitterWindow::Unsplit(wxWindow *toRemove)
782 {
783 if ( ! IsSplit() )
784 return false;
785
786 wxWindow *win;
787 if ( toRemove == NULL || toRemove == m_windowTwo)
788 {
789 win = m_windowTwo ;
790 m_windowTwo = (wxWindow *) NULL;
791 }
792 else if ( toRemove == m_windowOne )
793 {
794 win = m_windowOne ;
795 m_windowOne = m_windowTwo;
796 m_windowTwo = (wxWindow *) NULL;
797 }
798 else
799 {
800 wxFAIL_MSG(wxT("splitter: attempt to remove a non-existent window"));
801
802 return false;
803 }
804
805 OnUnsplit(win);
806 DoSetSashPosition(0);
807 SizeWindows();
808
809 return true;
810 }
811
812 // Replace a window with another one
813 bool wxSplitterWindow::ReplaceWindow(wxWindow *winOld, wxWindow *winNew)
814 {
815 wxCHECK_MSG( winOld, false, wxT("use one of Split() functions instead") );
816 wxCHECK_MSG( winNew, false, wxT("use Unsplit() functions instead") );
817
818 if ( winOld == m_windowTwo )
819 {
820 m_windowTwo = winNew;
821 }
822 else if ( winOld == m_windowOne )
823 {
824 m_windowOne = winNew;
825 }
826 else
827 {
828 wxFAIL_MSG(wxT("splitter: attempt to replace a non-existent window"));
829
830 return false;
831 }
832
833 SizeWindows();
834
835 return true;
836 }
837
838 void wxSplitterWindow::SetMinimumPaneSize(int min)
839 {
840 m_minimumPaneSize = min;
841 int pos = m_requestedSashPosition != INT_MAX ? m_requestedSashPosition : m_sashPosition;
842 SetSashPosition(pos); // re-check limits
843 }
844
845 void wxSplitterWindow::SetSashPosition(int position, bool redraw)
846 {
847 // remember the sash position we want to set for later if we can't set it
848 // right now (e.g. because the window is too small)
849 m_requestedSashPosition = position;
850 m_checkRequestedSashPosition = false;
851
852 DoSetSashPosition(ConvertSashPosition(position));
853
854 if ( redraw )
855 {
856 SizeWindows();
857 }
858 }
859
860 // Make sure the child window sizes are updated. This is useful
861 // for reducing flicker by updating the sizes before a
862 // window is shown, if you know the overall size is correct.
863 void wxSplitterWindow::UpdateSize()
864 {
865 m_checkRequestedSashPosition = true;
866 SizeWindows();
867 m_checkRequestedSashPosition = false;
868 }
869
870 bool wxSplitterWindow::DoSendEvent(wxSplitterEvent& event)
871 {
872 return !GetEventHandler()->ProcessEvent(event) || event.IsAllowed();
873 }
874
875 wxSize wxSplitterWindow::DoGetBestSize() const
876 {
877 // get best sizes of subwindows
878 wxSize size1, size2;
879 if ( m_windowOne )
880 size1 = m_windowOne->GetAdjustedBestSize();
881 if ( m_windowTwo )
882 size2 = m_windowTwo->GetAdjustedBestSize();
883
884 // sum them
885 //
886 // pSash points to the size component to which sash size must be added
887 int *pSash;
888 wxSize sizeBest;
889 if ( m_splitMode == wxSPLIT_VERTICAL )
890 {
891 sizeBest.y = wxMax(size1.y, size2.y);
892 sizeBest.x = wxMax(size1.x, m_minimumPaneSize) +
893 wxMax(size2.x, m_minimumPaneSize);
894
895 pSash = &sizeBest.x;
896 }
897 else // wxSPLIT_HORIZONTAL
898 {
899 sizeBest.x = wxMax(size1.x, size2.x);
900 sizeBest.y = wxMax(size1.y, m_minimumPaneSize) +
901 wxMax(size2.y, m_minimumPaneSize);
902
903 pSash = &sizeBest.y;
904 }
905
906 // account for the border and the sash
907 int border = 2*GetBorderSize();
908 *pSash += GetSashSize();
909 sizeBest.x += border;
910 sizeBest.y += border;
911
912 return sizeBest;
913 }
914
915 // ---------------------------------------------------------------------------
916 // wxSplitterWindow virtual functions: they now just generate the events
917 // ---------------------------------------------------------------------------
918
919 bool wxSplitterWindow::OnSashPositionChange(int WXUNUSED(newSashPosition))
920 {
921 // always allow by default
922 return true;
923 }
924
925 int wxSplitterWindow::OnSashPositionChanging(int newSashPosition)
926 {
927 // If within UNSPLIT_THRESHOLD from edge, set to edge to cause closure.
928 const int UNSPLIT_THRESHOLD = 4;
929
930 // first of all, check if OnSashPositionChange() doesn't forbid this change
931 if ( !OnSashPositionChange(newSashPosition) )
932 {
933 // it does
934 return -1;
935 }
936
937 // Obtain relevant window dimension for bottom / right threshold check
938 int window_size = GetWindowSize();
939
940 bool unsplit_scenario = false;
941 if ( m_permitUnsplitAlways || m_minimumPaneSize == 0 )
942 {
943 // Do edge detection if unsplit premitted
944 if ( newSashPosition <= UNSPLIT_THRESHOLD )
945 {
946 // threshold top / left check
947 newSashPosition = 0;
948 unsplit_scenario = true;
949 }
950 if ( newSashPosition >= window_size - UNSPLIT_THRESHOLD )
951 {
952 // threshold bottom/right check
953 newSashPosition = window_size;
954 unsplit_scenario = true;
955 }
956 }
957
958 if ( !unsplit_scenario )
959 {
960 // If resultant pane would be too small, enlarge it
961 newSashPosition = AdjustSashPosition(newSashPosition);
962 }
963
964 // If the result is out of bounds it means minimum size is too big,
965 // so split window in half as best compromise.
966 if ( newSashPosition < 0 || newSashPosition > window_size )
967 newSashPosition = window_size / 2;
968
969 // now let the event handler have it
970 //
971 // FIXME: shouldn't we do it before the adjustments above so as to ensure
972 // that the sash position is always reasonable?
973 wxSplitterEvent event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING, this);
974 event.m_data.pos = newSashPosition;
975
976 if ( !DoSendEvent(event) )
977 {
978 // the event handler vetoed the change
979 newSashPosition = -1;
980 }
981 else
982 {
983 // it could have been changed by it
984 newSashPosition = event.GetSashPosition();
985 }
986
987 return newSashPosition;
988 }
989
990 // Called when the sash is double-clicked. The default behaviour is to remove
991 // the sash if the minimum pane size is zero.
992 void wxSplitterWindow::OnDoubleClickSash(int x, int y)
993 {
994 wxCHECK_RET(m_windowTwo, wxT("splitter: no window to remove"));
995
996 // new code should handle events instead of using the virtual functions
997 wxSplitterEvent event(wxEVT_COMMAND_SPLITTER_DOUBLECLICKED, this);
998 event.m_data.pt.x = x;
999 event.m_data.pt.y = y;
1000 if ( DoSendEvent(event) )
1001 {
1002 if ( GetMinimumPaneSize() == 0 || m_permitUnsplitAlways )
1003 {
1004 wxWindow* win = m_windowTwo;
1005 if ( Unsplit(win) )
1006 {
1007 wxSplitterEvent unsplitEvent(wxEVT_COMMAND_SPLITTER_UNSPLIT, this);
1008 unsplitEvent.m_data.win = win;
1009 (void)DoSendEvent(unsplitEvent);
1010 }
1011 }
1012 }
1013 //else: blocked by user
1014 }
1015
1016 void wxSplitterWindow::OnUnsplit(wxWindow *winRemoved)
1017 {
1018 // call this before calling the event handler which may delete the window
1019 winRemoved->Show(false);
1020 }
1021
1022 #if defined( __WXMSW__ ) || defined( __WXMAC__)
1023
1024 // this is currently called (and needed) under MSW only...
1025 void wxSplitterWindow::OnSetCursor(wxSetCursorEvent& event)
1026 {
1027 // if we don't do it, the resizing cursor might be set for child window:
1028 // and like this we explicitly say that our cursor should not be used for
1029 // children windows which overlap us
1030
1031 if ( SashHitTest(event.GetX(), event.GetY(), 0) )
1032 {
1033 // default processing is ok
1034 event.Skip();
1035 }
1036 //else: do nothing, in particular, don't call Skip()
1037 }
1038
1039 #endif // wxMSW || wxMac
1040
1041 #endif // wxUSE_SPLITTER
1042