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