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