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