]> git.saurik.com Git - wxWidgets.git/blob - src/generic/splitter.cpp
Build fixes.
[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 if (! window->IsShown())
713 window->Show();
714
715 m_windowOne = window;
716 m_windowTwo = (wxWindow *) NULL;
717 DoSetSashPosition(0);
718 }
719
720 // Associates the given window with window 2, drawing the appropriate sash
721 // and changing the split mode.
722 // Does nothing and returns false if the window is already split.
723 bool wxSplitterWindow::DoSplit(wxSplitMode mode,
724 wxWindow *window1, wxWindow *window2,
725 int sashPosition)
726 {
727 if ( IsSplit() )
728 return false;
729
730 wxCHECK_MSG( window1 && window2, false,
731 _T("can not split with NULL window(s)") );
732
733 wxCHECK_MSG( window1->GetParent() == this && window2->GetParent() == this, false,
734 _T("windows in the splitter should have it as parent!") );
735
736 if (! window1->IsShown())
737 window1->Show();
738 if (! window2->IsShown())
739 window2->Show();
740
741 m_splitMode = mode;
742 m_windowOne = window1;
743 m_windowTwo = window2;
744
745 // remember the sash position we want to set for later if we can't set it
746 // right now (e.g. because the window is too small)
747 m_requestedSashPosition = sashPosition;
748 m_checkRequestedSashPosition = false;
749
750 DoSetSashPosition(ConvertSashPosition(sashPosition));
751
752 SizeWindows();
753
754 return true;
755 }
756
757 int wxSplitterWindow::ConvertSashPosition(int sashPosition) const
758 {
759 if ( sashPosition > 0 )
760 {
761 return sashPosition;
762 }
763 else if ( sashPosition < 0 )
764 {
765 // It's negative so adding is subtracting
766 return GetWindowSize() + sashPosition;
767 }
768 else // sashPosition == 0
769 {
770 // default, put it in the centre
771 return GetWindowSize() / 2;
772 }
773 }
774
775 // Remove the specified (or second) window from the view
776 // Doesn't actually delete the window.
777 bool wxSplitterWindow::Unsplit(wxWindow *toRemove)
778 {
779 if ( ! IsSplit() )
780 return false;
781
782 wxWindow *win;
783 if ( toRemove == NULL || toRemove == m_windowTwo)
784 {
785 win = m_windowTwo ;
786 m_windowTwo = (wxWindow *) NULL;
787 }
788 else if ( toRemove == m_windowOne )
789 {
790 win = m_windowOne ;
791 m_windowOne = m_windowTwo;
792 m_windowTwo = (wxWindow *) NULL;
793 }
794 else
795 {
796 wxFAIL_MSG(wxT("splitter: attempt to remove a non-existent window"));
797
798 return false;
799 }
800
801 OnUnsplit(win);
802 DoSetSashPosition(0);
803 SizeWindows();
804
805 return true;
806 }
807
808 // Replace a window with another one
809 bool wxSplitterWindow::ReplaceWindow(wxWindow *winOld, wxWindow *winNew)
810 {
811 wxCHECK_MSG( winOld, false, wxT("use one of Split() functions instead") );
812 wxCHECK_MSG( winNew, false, wxT("use Unsplit() functions instead") );
813
814 if ( winOld == m_windowTwo )
815 {
816 m_windowTwo = winNew;
817 }
818 else if ( winOld == m_windowOne )
819 {
820 m_windowOne = winNew;
821 }
822 else
823 {
824 wxFAIL_MSG(wxT("splitter: attempt to replace a non-existent window"));
825
826 return false;
827 }
828
829 SizeWindows();
830
831 return true;
832 }
833
834 void wxSplitterWindow::SetMinimumPaneSize(int min)
835 {
836 m_minimumPaneSize = min;
837 int pos = m_requestedSashPosition != INT_MAX ? m_requestedSashPosition : m_sashPosition;
838 SetSashPosition(pos); // re-check limits
839 }
840
841 void wxSplitterWindow::SetSashPosition(int position, bool redraw)
842 {
843 // remember the sash position we want to set for later if we can't set it
844 // right now (e.g. because the window is too small)
845 m_requestedSashPosition = position;
846 m_checkRequestedSashPosition = false;
847
848 DoSetSashPosition(ConvertSashPosition(position));
849
850 if ( redraw )
851 {
852 SizeWindows();
853 }
854 }
855
856 // Make sure the child window sizes are updated. This is useful
857 // for reducing flicker by updating the sizes before a
858 // window is shown, if you know the overall size is correct.
859 void wxSplitterWindow::UpdateSize()
860 {
861 m_checkRequestedSashPosition = true;
862 SizeWindows();
863 m_checkRequestedSashPosition = false;
864 }
865
866 bool wxSplitterWindow::DoSendEvent(wxSplitterEvent& event)
867 {
868 return !GetEventHandler()->ProcessEvent(event) || event.IsAllowed();
869 }
870
871 wxSize wxSplitterWindow::DoGetBestSize() const
872 {
873 // get best sizes of subwindows
874 wxSize size1, size2;
875 if ( m_windowOne )
876 size1 = m_windowOne->GetBestSize();
877 if ( m_windowTwo )
878 size2 = m_windowTwo->GetBestSize();
879
880 // sum them
881 //
882 // pSash points to the size component to which sash size must be added
883 int *pSash;
884 wxSize sizeBest;
885 if ( m_splitMode == wxSPLIT_VERTICAL )
886 {
887 sizeBest.y = wxMax(size1.y, size2.y);
888 sizeBest.x = wxMax(size1.x, m_minimumPaneSize) +
889 wxMax(size2.x, m_minimumPaneSize);
890
891 pSash = &sizeBest.x;
892 }
893 else // wxSPLIT_HORIZONTAL
894 {
895 sizeBest.x = wxMax(size1.x, size2.x);
896 sizeBest.y = wxMax(size1.y, m_minimumPaneSize) +
897 wxMax(size2.y, m_minimumPaneSize);
898
899 pSash = &sizeBest.y;
900 }
901
902 // account for the border and the sash
903 int border = 2*GetBorderSize();
904 *pSash += GetSashSize();
905 sizeBest.x += border;
906 sizeBest.y += border;
907
908 return sizeBest;
909 }
910
911 // ---------------------------------------------------------------------------
912 // wxSplitterWindow virtual functions: they now just generate the events
913 // ---------------------------------------------------------------------------
914
915 bool wxSplitterWindow::OnSashPositionChange(int WXUNUSED(newSashPosition))
916 {
917 // always allow by default
918 return true;
919 }
920
921 int wxSplitterWindow::OnSashPositionChanging(int newSashPosition)
922 {
923 // If within UNSPLIT_THRESHOLD from edge, set to edge to cause closure.
924 const int UNSPLIT_THRESHOLD = 4;
925
926 // first of all, check if OnSashPositionChange() doesn't forbid this change
927 if ( !OnSashPositionChange(newSashPosition) )
928 {
929 // it does
930 return -1;
931 }
932
933 // Obtain relevant window dimension for bottom / right threshold check
934 int window_size = GetWindowSize();
935
936 bool unsplit_scenario = false;
937 if ( m_permitUnsplitAlways || m_minimumPaneSize == 0 )
938 {
939 // Do edge detection if unsplit premitted
940 if ( newSashPosition <= UNSPLIT_THRESHOLD )
941 {
942 // threshold top / left check
943 newSashPosition = 0;
944 unsplit_scenario = true;
945 }
946 if ( newSashPosition >= window_size - UNSPLIT_THRESHOLD )
947 {
948 // threshold bottom/right check
949 newSashPosition = window_size;
950 unsplit_scenario = true;
951 }
952 }
953
954 if ( !unsplit_scenario )
955 {
956 // If resultant pane would be too small, enlarge it
957 newSashPosition = AdjustSashPosition(newSashPosition);
958 }
959
960 // If the result is out of bounds it means minimum size is too big,
961 // so split window in half as best compromise.
962 if ( newSashPosition < 0 || newSashPosition > window_size )
963 newSashPosition = window_size / 2;
964
965 // now let the event handler have it
966 //
967 // FIXME: shouldn't we do it before the adjustments above so as to ensure
968 // that the sash position is always reasonable?
969 wxSplitterEvent event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING, this);
970 event.m_data.pos = newSashPosition;
971
972 if ( !DoSendEvent(event) )
973 {
974 // the event handler vetoed the change
975 newSashPosition = -1;
976 }
977 else
978 {
979 // it could have been changed by it
980 newSashPosition = event.GetSashPosition();
981 }
982
983 return newSashPosition;
984 }
985
986 // Called when the sash is double-clicked. The default behaviour is to remove
987 // the sash if the minimum pane size is zero.
988 void wxSplitterWindow::OnDoubleClickSash(int x, int y)
989 {
990 wxCHECK_RET(m_windowTwo, wxT("splitter: no window to remove"));
991
992 // new code should handle events instead of using the virtual functions
993 wxSplitterEvent event(wxEVT_COMMAND_SPLITTER_DOUBLECLICKED, this);
994 event.m_data.pt.x = x;
995 event.m_data.pt.y = y;
996 if ( DoSendEvent(event) )
997 {
998 if ( GetMinimumPaneSize() == 0 || m_permitUnsplitAlways )
999 {
1000 wxWindow* win = m_windowTwo;
1001 if ( Unsplit(win) )
1002 {
1003 wxSplitterEvent unsplitEvent(wxEVT_COMMAND_SPLITTER_UNSPLIT, this);
1004 unsplitEvent.m_data.win = win;
1005 (void)DoSendEvent(unsplitEvent);
1006 }
1007 }
1008 }
1009 //else: blocked by user
1010 }
1011
1012 void wxSplitterWindow::OnUnsplit(wxWindow *winRemoved)
1013 {
1014 // call this before calling the event handler which may delete the window
1015 winRemoved->Show(false);
1016 }
1017
1018 #if defined( __WXMSW__ ) || defined( __WXMAC__)
1019
1020 // this is currently called (and needed) under MSW only...
1021 void wxSplitterWindow::OnSetCursor(wxSetCursorEvent& event)
1022 {
1023 // if we don't do it, the resizing cursor might be set for child window:
1024 // and like this we explicitly say that our cursor should not be used for
1025 // children windows which overlap us
1026
1027 if ( SashHitTest(event.GetX(), event.GetY(), 0) )
1028 {
1029 // default processing is ok
1030 event.Skip();
1031 }
1032 //else: do nothing, in particular, don't call Skip()
1033 }
1034
1035 #endif // wxMSW || wxMac
1036
1037 #endif // wxUSE_SPLITTER
1038