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