]> git.saurik.com Git - wxWidgets.git/blob - src/generic/splitter.cpp
Only draw borders if wxSP_3DBORDER specified, as before
[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 if (HasFlag(wxSP_3DBORDER))
432 wxRendererNative::Get().DrawSplitterBorder
433 (
434 this,
435 dc,
436 GetClientRect()
437 );
438
439 // don't draw sash if we're not split
440 if ( m_sashPosition == 0 || !m_windowTwo )
441 return;
442
443 // nor if we're configured to not show it
444 if ( HasFlag(wxSP_NOSASH) )
445 return;
446
447 wxRendererNative::Get().DrawSplitterSash
448 (
449 this,
450 dc,
451 GetClientSize(),
452 m_sashPosition,
453 m_splitMode == wxSPLIT_VERTICAL ? wxVERTICAL
454 : wxHORIZONTAL,
455 m_isHot ? wxCONTROL_CURRENT : 0
456 );
457 }
458
459 // Draw the sash tracker (for whilst moving the sash)
460 void wxSplitterWindow::DrawSashTracker(int x, int y)
461 {
462 int w, h;
463 GetClientSize(&w, &h);
464
465 wxScreenDC screenDC;
466 int x1, y1;
467 int x2, y2;
468
469 if ( m_splitMode == wxSPLIT_VERTICAL )
470 {
471 x1 = x; y1 = 2;
472 x2 = x; y2 = h-2;
473
474 if ( x1 > w )
475 {
476 x1 = w; x2 = w;
477 }
478 else if ( x1 < 0 )
479 {
480 x1 = 0; x2 = 0;
481 }
482 }
483 else
484 {
485 x1 = 2; y1 = y;
486 x2 = w-2; y2 = y;
487
488 if ( y1 > h )
489 {
490 y1 = h;
491 y2 = h;
492 }
493 else if ( y1 < 0 )
494 {
495 y1 = 0;
496 y2 = 0;
497 }
498 }
499
500 ClientToScreen(&x1, &y1);
501 ClientToScreen(&x2, &y2);
502
503 screenDC.SetLogicalFunction(wxINVERT);
504 screenDC.SetPen(*m_sashTrackerPen);
505 screenDC.SetBrush(*wxTRANSPARENT_BRUSH);
506
507 screenDC.DrawLine(x1, y1, x2, y2);
508
509 screenDC.SetLogicalFunction(wxCOPY);
510 }
511
512 int wxSplitterWindow::GetWindowSize() const
513 {
514 wxSize size = GetClientSize();
515
516 return m_splitMode == wxSPLIT_VERTICAL ? size.x : size.y;
517 }
518
519 int wxSplitterWindow::AdjustSashPosition(int sashPos) const
520 {
521 int window_size = GetWindowSize();
522
523 wxWindow *win;
524
525 win = GetWindow1();
526 if ( win )
527 {
528 // the window shouldn't be smaller than its own minimal size nor
529 // smaller than the minimual pane size specified for this splitter
530 int minSize = m_splitMode == wxSPLIT_VERTICAL ? win->GetMinWidth()
531 : win->GetMinHeight();
532
533 if ( minSize == -1 || m_minimumPaneSize > minSize )
534 minSize = m_minimumPaneSize;
535
536 minSize += GetBorderSize();
537
538 if ( sashPos < minSize )
539 sashPos = minSize;
540 }
541
542 win = GetWindow2();
543 if ( win )
544 {
545 int minSize = m_splitMode == wxSPLIT_VERTICAL ? win->GetMinWidth()
546 : win->GetMinHeight();
547
548 if ( minSize == -1 || m_minimumPaneSize > minSize )
549 minSize = m_minimumPaneSize;
550
551 int maxSize = window_size - minSize - GetBorderSize();
552 if ( sashPos > maxSize )
553 sashPos = maxSize;
554 }
555
556 return sashPos;
557 }
558
559 bool wxSplitterWindow::DoSetSashPosition(int sashPos)
560 {
561 int newSashPosition = AdjustSashPosition(sashPos);
562
563 if ( newSashPosition == m_sashPosition )
564 return FALSE;
565
566 m_sashPosition = newSashPosition;
567
568 return TRUE;
569 }
570
571 void wxSplitterWindow::SetSashPositionAndNotify(int sashPos)
572 {
573 if ( DoSetSashPosition(sashPos) )
574 {
575 wxSplitterEvent event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED, this);
576 event.m_data.pos = m_sashPosition;
577
578 (void)DoSendEvent(event);
579 }
580 }
581
582 // Position and size subwindows.
583 // Note that the border size applies to each subwindow, not
584 // including the edges next to the sash.
585 void wxSplitterWindow::SizeWindows()
586 {
587 // check if we have delayed setting the real sash position
588 if ( m_requestedSashPosition != INT_MAX )
589 {
590 int newSashPosition = ConvertSashPosition(m_requestedSashPosition);
591 if ( newSashPosition != m_sashPosition )
592 {
593 DoSetSashPosition(newSashPosition);
594 }
595
596 if ( newSashPosition <= m_sashPosition
597 && newSashPosition >= m_sashPosition - GetBorderSize() )
598 {
599 // don't update it any more
600 m_requestedSashPosition = INT_MAX;
601 }
602 }
603
604 int w, h;
605 GetClientSize(&w, &h);
606
607 if ( GetWindow1() && !GetWindow2() )
608 {
609 GetWindow1()->SetSize(GetBorderSize(), GetBorderSize(),
610 w - 2*GetBorderSize(), h - 2*GetBorderSize());
611 }
612 else if ( GetWindow1() && GetWindow2() )
613 {
614 const int border = GetBorderSize(),
615 sash = GetSashSize();
616
617 int size1 = GetSashPosition() - border,
618 size2 = GetSashPosition() + sash;
619
620 int x2, y2, w1, h1, w2, h2;
621 if ( GetSplitMode() == wxSPLIT_VERTICAL )
622 {
623 w1 = size1;
624 w2 = w - 2*border - sash - w1;
625 h1 =
626 h2 = h - 2*border;
627 x2 = size2;
628 y2 = border;
629 }
630 else // horz splitter
631 {
632 w1 =
633 w2 = w - 2*border;
634 h1 = size1;
635 h2 = h - 2*border - sash - h1;
636 x2 = border;
637 y2 = size2;
638 }
639
640 GetWindow1()->SetSize(border, border, w1, h1);
641 GetWindow2()->SetSize(x2, y2, w2, h2);
642 }
643
644 wxClientDC dc(this);
645 DrawSash(dc);
646
647 SetNeedUpdating(FALSE);
648 }
649
650 // Set pane for unsplit window
651 void wxSplitterWindow::Initialize(wxWindow *window)
652 {
653 wxASSERT_MSG( window && window->GetParent() == this,
654 _T("windows in the splitter should have it as parent!") );
655
656 m_windowOne = window;
657 m_windowTwo = (wxWindow *) NULL;
658 DoSetSashPosition(0);
659 }
660
661 // Associates the given window with window 2, drawing the appropriate sash
662 // and changing the split mode.
663 // Does nothing and returns FALSE if the window is already split.
664 bool wxSplitterWindow::DoSplit(wxSplitMode mode,
665 wxWindow *window1, wxWindow *window2,
666 int sashPosition)
667 {
668 if ( IsSplit() )
669 return FALSE;
670
671 wxCHECK_MSG( window1 && window2, FALSE,
672 _T("can not split with NULL window(s)") );
673
674 wxCHECK_MSG( window1->GetParent() == this && window2->GetParent() == this, FALSE,
675 _T("windows in the splitter should have it as parent!") );
676
677 m_splitMode = mode;
678 m_windowOne = window1;
679 m_windowTwo = window2;
680
681 // remember the sash position we want to set for later if we can't set it
682 // right now (e.g. because the window is too small)
683 m_requestedSashPosition = sashPosition;
684
685 DoSetSashPosition(ConvertSashPosition(sashPosition));
686
687 SizeWindows();
688
689 return TRUE;
690 }
691
692 int wxSplitterWindow::ConvertSashPosition(int sashPosition) const
693 {
694 if ( sashPosition > 0 )
695 {
696 return sashPosition;
697 }
698 else if ( sashPosition < 0 )
699 {
700 // It's negative so adding is subtracting
701 return GetWindowSize() + sashPosition;
702 }
703 else // sashPosition == 0
704 {
705 // default, put it in the centre
706 return GetWindowSize() / 2;
707 }
708 }
709
710 // Remove the specified (or second) window from the view
711 // Doesn't actually delete the window.
712 bool wxSplitterWindow::Unsplit(wxWindow *toRemove)
713 {
714 if ( ! IsSplit() )
715 return FALSE;
716
717 wxWindow *win = NULL;
718 if ( toRemove == NULL || toRemove == m_windowTwo)
719 {
720 win = m_windowTwo ;
721 m_windowTwo = (wxWindow *) NULL;
722 }
723 else if ( toRemove == m_windowOne )
724 {
725 win = m_windowOne ;
726 m_windowOne = m_windowTwo;
727 m_windowTwo = (wxWindow *) NULL;
728 }
729 else
730 {
731 wxFAIL_MSG(wxT("splitter: attempt to remove a non-existent window"));
732
733 return FALSE;
734 }
735
736 OnUnsplit(win);
737 DoSetSashPosition(0);
738 SizeWindows();
739
740 return TRUE;
741 }
742
743 // Replace a window with another one
744 bool wxSplitterWindow::ReplaceWindow(wxWindow *winOld, wxWindow *winNew)
745 {
746 wxCHECK_MSG( winOld, FALSE, wxT("use one of Split() functions instead") );
747 wxCHECK_MSG( winNew, FALSE, wxT("use Unsplit() functions instead") );
748
749 if ( winOld == m_windowTwo )
750 {
751 m_windowTwo = winNew;
752 }
753 else if ( winOld == m_windowOne )
754 {
755 m_windowOne = winNew;
756 }
757 else
758 {
759 wxFAIL_MSG(wxT("splitter: attempt to replace a non-existent window"));
760
761 return FALSE;
762 }
763
764 SizeWindows();
765
766 return TRUE;
767 }
768
769 void wxSplitterWindow::SetMinimumPaneSize(int min)
770 {
771 m_minimumPaneSize = min;
772 SetSashPosition(m_sashPosition); // re-check limits
773 }
774
775 void wxSplitterWindow::SetSashPosition(int position, bool redraw)
776 {
777 DoSetSashPosition(position);
778
779 if ( redraw )
780 {
781 SizeWindows();
782 }
783 }
784
785 bool wxSplitterWindow::DoSendEvent(wxSplitterEvent& event)
786 {
787 return !GetEventHandler()->ProcessEvent(event) || event.IsAllowed();
788 }
789
790 // ---------------------------------------------------------------------------
791 // wxSplitterWindow virtual functions: they now just generate the events
792 // ---------------------------------------------------------------------------
793
794 bool wxSplitterWindow::OnSashPositionChange(int WXUNUSED(newSashPosition))
795 {
796 // always allow by default
797 return TRUE;
798 }
799
800 int wxSplitterWindow::OnSashPositionChanging(int newSashPosition)
801 {
802 // If within UNSPLIT_THRESHOLD from edge, set to edge to cause closure.
803 const int UNSPLIT_THRESHOLD = 4;
804
805 // first of all, check if OnSashPositionChange() doesn't forbid this change
806 if ( !OnSashPositionChange(newSashPosition) )
807 {
808 // it does
809 return -1;
810 }
811
812 // Obtain relevant window dimension for bottom / right threshold check
813 int window_size = GetWindowSize();
814
815 bool unsplit_scenario = FALSE;
816 if ( m_permitUnsplitAlways || m_minimumPaneSize == 0 )
817 {
818 // Do edge detection if unsplit premitted
819 if ( newSashPosition <= UNSPLIT_THRESHOLD )
820 {
821 // threshold top / left check
822 newSashPosition = 0;
823 unsplit_scenario = TRUE;
824 }
825 if ( newSashPosition >= window_size - UNSPLIT_THRESHOLD )
826 {
827 // threshold bottom/right check
828 newSashPosition = window_size;
829 unsplit_scenario = TRUE;
830 }
831 }
832
833 if ( !unsplit_scenario )
834 {
835 // If resultant pane would be too small, enlarge it
836 newSashPosition = AdjustSashPosition(newSashPosition);
837 }
838
839 // If the result is out of bounds it means minimum size is too big,
840 // so split window in half as best compromise.
841 if ( newSashPosition < 0 || newSashPosition > window_size )
842 newSashPosition = window_size / 2;
843
844 // now let the event handler have it
845 //
846 // FIXME: shouldn't we do it before the adjustments above so as to ensure
847 // that the sash position is always reasonable?
848 wxSplitterEvent event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING, this);
849 event.m_data.pos = newSashPosition;
850
851 if ( !DoSendEvent(event) )
852 {
853 // the event handler vetoed the change
854 newSashPosition = -1;
855 }
856 else
857 {
858 // it could have been changed by it
859 newSashPosition = event.GetSashPosition();
860 }
861
862 return newSashPosition;
863 }
864
865 // Called when the sash is double-clicked. The default behaviour is to remove
866 // the sash if the minimum pane size is zero.
867 void wxSplitterWindow::OnDoubleClickSash(int x, int y)
868 {
869 wxCHECK_RET(m_windowTwo, wxT("splitter: no window to remove"));
870
871 // new code should handle events instead of using the virtual functions
872 wxSplitterEvent event(wxEVT_COMMAND_SPLITTER_DOUBLECLICKED, this);
873 event.m_data.pt.x = x;
874 event.m_data.pt.y = y;
875 if ( DoSendEvent(event) )
876 {
877 if ( GetMinimumPaneSize() == 0 || m_permitUnsplitAlways )
878 {
879 wxWindow* win = m_windowTwo;
880 if ( Unsplit(win) )
881 {
882 wxSplitterEvent unsplitEvent(wxEVT_COMMAND_SPLITTER_UNSPLIT, this);
883 unsplitEvent.m_data.win = win;
884 (void)DoSendEvent(unsplitEvent);
885 }
886 }
887 }
888 //else: blocked by user
889 }
890
891 void wxSplitterWindow::OnUnsplit(wxWindow *winRemoved)
892 {
893 // call this before calling the event handler which may delete the window
894 winRemoved->Show(FALSE);
895 }
896
897 #if defined( __WXMSW__ ) || defined( __WXMAC__)
898
899 // this is currently called (and needed) under MSW only...
900 void wxSplitterWindow::OnSetCursor(wxSetCursorEvent& event)
901 {
902 // if we don't do it, the resizing cursor might be set for child window:
903 // and like this we explicitly say that our cursor should not be used for
904 // children windows which overlap us
905
906 if ( SashHitTest(event.GetX(), event.GetY()) )
907 {
908 // default processing is ok
909 event.Skip();
910 }
911 //else: do nothing, in particular, don't call Skip()
912 }
913
914 #endif // wxMSW || wxMac
915
916 #endif // wxUSE_SPLITTER
917