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