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