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