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