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