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