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