]> git.saurik.com Git - wxWidgets.git/blob - src/generic/splitter.cpp
Patch [ 1314868 ] [wxMSW] Show MDI child menubars when ShowFullScreen(false)
[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 #if wxUSE_SPLITTER
16
17 #ifdef __BORLANDC__
18 #pragma hdrstop
19 #endif
20
21 #ifndef WX_PRECOMP
22 #include "wx/string.h"
23 #include "wx/utils.h"
24 #include "wx/log.h"
25
26 #include "wx/dcscreen.h"
27
28 #include "wx/window.h"
29 #include "wx/dialog.h"
30 #include "wx/frame.h"
31
32 #include "wx/settings.h"
33 #endif
34
35 #ifdef __WXMAC__
36 #include "wx/mac/private.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
52 /*
53 TODO PROPERTIES
54 style wxSP_3D
55 sashpos (long , 0 )
56 minsize (long -1 )
57 object, object_ref
58 orientation
59 */
60
61 IMPLEMENT_DYNAMIC_CLASS(wxSplitterEvent, wxNotifyEvent)
62
63 BEGIN_EVENT_TABLE(wxSplitterWindow, wxWindow)
64 EVT_PAINT(wxSplitterWindow::OnPaint)
65 EVT_SIZE(wxSplitterWindow::OnSize)
66 EVT_MOUSE_EVENTS(wxSplitterWindow::OnMouseEvent)
67
68 #if defined( __WXMSW__ ) || defined( __WXMAC__)
69 EVT_SET_CURSOR(wxSplitterWindow::OnSetCursor)
70 #endif // wxMSW
71
72 WX_EVENT_TABLE_CONTROL_CONTAINER(wxSplitterWindow)
73 END_EVENT_TABLE()
74
75 WX_DELEGATE_TO_CONTROL_CONTAINER(wxSplitterWindow)
76
77 bool wxSplitterWindow::Create(wxWindow *parent, wxWindowID id,
78 const wxPoint& pos,
79 const wxSize& size,
80 long style,
81 const wxString& name)
82 {
83 // allow TABbing from one window to the other
84 style |= wxTAB_TRAVERSAL;
85
86 // we draw our border ourselves to blend the sash with it
87 style &= ~wxBORDER_MASK;
88 style |= wxBORDER_NONE;
89
90 if ( !wxWindow::Create(parent, id, pos, size, style, name) )
91 return false;
92
93 if (size.x >= 0)
94 m_lastSize.x = size.x;
95 if (size.y >= 0)
96 m_lastSize.y = size.y;
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_container.SetContainerWindow(this);
114
115 m_splitMode = wxSPLIT_VERTICAL;
116 m_permitUnsplitAlways = true;
117 m_windowOne = (wxWindow *) NULL;
118 m_windowTwo = (wxWindow *) NULL;
119 m_dragMode = wxSPLIT_DRAG_NONE;
120 m_oldX = 0;
121 m_oldY = 0;
122 m_firstX = 0;
123 m_firstY = 0;
124 m_sashPosition = m_requestedSashPosition = 0;
125 m_sashGravity = 0.0;
126 m_sashSize = -1; // -1 means use the native sash size
127 m_lastSize = wxSize(0,0);
128 m_checkRequestedSashPosition = false;
129 m_minimumPaneSize = 0;
130 m_sashCursorWE = wxCursor(wxCURSOR_SIZEWE);
131 m_sashCursorNS = wxCursor(wxCURSOR_SIZENS);
132 m_sashTrackerPen = new wxPen(*wxBLACK, 2, wxSOLID);
133
134 m_needUpdating = false;
135 m_isHot = false;
136 }
137
138 wxSplitterWindow::~wxSplitterWindow()
139 {
140 delete m_sashTrackerPen;
141 }
142
143 // ----------------------------------------------------------------------------
144 // entering/leaving sash
145 // ----------------------------------------------------------------------------
146
147 void wxSplitterWindow::RedrawIfHotSensitive(bool isHot)
148 {
149 if ( wxRendererNative::Get().GetSplitterParams(this).isHotSensitive )
150 {
151 m_isHot = isHot;
152
153 wxClientDC dc(this);
154 DrawSash(dc);
155 }
156 //else: we don't change our appearance, don't redraw to avoid flicker
157 }
158
159 void wxSplitterWindow::OnEnterSash()
160 {
161 SetResizeCursor();
162
163 RedrawIfHotSensitive(true);
164 }
165
166 void wxSplitterWindow::OnLeaveSash()
167 {
168 SetCursor(*wxSTANDARD_CURSOR);
169
170 RedrawIfHotSensitive(false);
171 }
172
173 void wxSplitterWindow::SetResizeCursor()
174 {
175 SetCursor(m_splitMode == wxSPLIT_VERTICAL ? m_sashCursorWE
176 : m_sashCursorNS);
177 }
178
179 // ----------------------------------------------------------------------------
180 // other event handlers
181 // ----------------------------------------------------------------------------
182
183 void wxSplitterWindow::OnPaint(wxPaintEvent& WXUNUSED(event))
184 {
185 wxPaintDC dc(this);
186
187 DrawSash(dc);
188 }
189
190 void wxSplitterWindow::OnInternalIdle()
191 {
192 wxWindow::OnInternalIdle();
193
194 // if this is the first idle time after a sash position has potentially
195 // been set, allow SizeWindows to check for a requested size.
196 if (!m_checkRequestedSashPosition)
197 {
198 m_checkRequestedSashPosition = true;
199 SizeWindows();
200 return; // it won't needUpdating in this case
201 }
202
203 if (m_needUpdating)
204 SizeWindows();
205 }
206
207 void wxSplitterWindow::OnMouseEvent(wxMouseEvent& event)
208 {
209 int x = (int)event.GetX(),
210 y = (int)event.GetY();
211
212 if (GetWindowStyle() & wxSP_NOSASH)
213 return;
214
215 // with wxSP_LIVE_UPDATE style the splitter windows are always resized
216 // following the mouse movement while it drags the sash, without it we only
217 // draw the sash at the new position but only resize the windows when the
218 // dragging is finished
219 #if defined( __WXMAC__ ) && TARGET_API_MAC_OSX == 1
220 bool isLive = true ;
221 #else
222 bool isLive = (GetWindowStyleFlag() & wxSP_LIVE_UPDATE) != 0;
223 #endif
224 if (event.LeftDown())
225 {
226 if ( SashHitTest(x, y) )
227 {
228 // Start the drag now
229 m_dragMode = wxSPLIT_DRAG_DRAGGING;
230
231 // Capture mouse and set the cursor
232 CaptureMouse();
233 SetResizeCursor();
234
235 if ( !isLive )
236 {
237 // remember the initial sash position and draw the initial
238 // shadow sash
239 m_sashPositionCurrent = m_sashPosition;
240
241 DrawSashTracker(x, y);
242 }
243
244 m_oldX = x;
245 m_oldY = y;
246
247 SetResizeCursor();
248 return;
249 }
250 }
251 else if (event.LeftUp() && m_dragMode == wxSPLIT_DRAG_DRAGGING)
252 {
253 // We can stop dragging now and see what we've got.
254 m_dragMode = wxSPLIT_DRAG_NONE;
255
256 // Release mouse and unset the cursor
257 ReleaseMouse();
258 SetCursor(* wxSTANDARD_CURSOR);
259
260 // exit if unsplit after doubleclick
261 if ( !IsSplit() )
262 {
263 return;
264 }
265
266 // Erase old tracker
267 if ( !isLive )
268 {
269 DrawSashTracker(m_oldX, m_oldY);
270 }
271
272 // the position of the click doesn't exactly correspond to
273 // m_sashPosition, rather it changes it by the distance by which the
274 // mouse has moved
275 int diff = m_splitMode == wxSPLIT_VERTICAL ? x - m_oldX : y - m_oldY;
276
277 int posSashOld = isLive ? m_sashPosition : m_sashPositionCurrent;
278 int posSashNew = OnSashPositionChanging(posSashOld + 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 = (wxWindow *) 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 = (wxWindow *) 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_oldX : y - m_oldY;
333 if ( !diff )
334 {
335 // nothing to do, mouse didn't really move far enough
336 return;
337 }
338
339 int posSashOld = isLive ? m_sashPosition : m_sashPositionCurrent;
340 int posSashNew = OnSashPositionChanging(posSashOld + diff);
341 if ( posSashNew == -1 )
342 {
343 // change not allowed
344 return;
345 }
346
347 if ( posSashNew == m_sashPosition )
348 return;
349
350 // Erase old tracker
351 if ( !isLive )
352 {
353 DrawSashTracker(m_oldX, m_oldY);
354 }
355
356 if (m_splitMode == wxSPLIT_VERTICAL)
357 x = posSashNew;
358 else
359 y = posSashNew;
360
361 // Remember old positions
362 m_oldX = x;
363 m_oldY = y;
364
365 #ifdef __WXMSW__
366 // As we captured the mouse, we may get the mouse events from outside
367 // our window - for example, negative values in x, y. This has a weird
368 // consequence under MSW where we use unsigned values sometimes and
369 // signed ones other times: the coordinates turn as big positive
370 // numbers and so the sash is drawn on the *right* side of the window
371 // instead of the left (or bottom instead of top). Correct this.
372 if ( (short)m_oldX < 0 )
373 m_oldX = 0;
374 if ( (short)m_oldY < 0 )
375 m_oldY = 0;
376 #endif // __WXMSW__
377
378 // Draw new one
379 if ( !isLive )
380 {
381 m_sashPositionCurrent = posSashNew;
382
383 DrawSashTracker(m_oldX, m_oldY);
384 }
385 else
386 {
387 DoSetSashPosition(posSashNew);
388 m_needUpdating = true;
389 }
390 }
391 else if ( event.LeftDClick() && m_windowTwo )
392 {
393 OnDoubleClickSash(x, y);
394 }
395 }
396
397 void wxSplitterWindow::OnSize(wxSizeEvent& event)
398 {
399 // only process this message if we're not iconized - otherwise iconizing
400 // and restoring a window containing the splitter has a funny side effect
401 // of changing the splitter position!
402 wxWindow *parent = wxGetTopLevelParent(this);
403 bool iconized;
404
405 wxTopLevelWindow *winTop = wxDynamicCast(parent, wxTopLevelWindow);
406 if ( winTop )
407 {
408 iconized = winTop->IsIconized();
409 }
410 else
411 {
412 wxFAIL_MSG(wxT("should have a top level parent!"));
413
414 iconized = false;
415 }
416
417 if ( iconized )
418 {
419 m_lastSize = wxSize(0,0);
420
421 event.Skip();
422
423 return;
424 }
425
426 if ( m_windowTwo )
427 {
428 int w, h;
429 GetClientSize(&w, &h);
430
431 int size = m_splitMode == wxSPLIT_VERTICAL ? w : h;
432
433 int old_size = m_splitMode == wxSPLIT_VERTICAL ? m_lastSize.x : m_lastSize.y;
434 if ( old_size != 0 )
435 {
436 int delta = (int) ( (size - old_size)*m_sashGravity );
437 if ( delta != 0 )
438 {
439 int newPosition = m_sashPosition + delta;
440 if( newPosition < m_minimumPaneSize )
441 newPosition = m_minimumPaneSize;
442 SetSashPositionAndNotify(newPosition);
443 }
444 }
445
446 if ( m_sashPosition >= size - 5 )
447 SetSashPositionAndNotify(wxMax(10, size - 40));
448 m_lastSize = wxSize(w,h);
449 }
450
451 SizeWindows();
452 }
453
454 void wxSplitterWindow::SetSashGravity(double gravity)
455 {
456 wxCHECK_RET( gravity >= 0. && gravity <= 1.,
457 _T("invalid gravity value") );
458
459 m_sashGravity = gravity;
460 }
461
462 bool wxSplitterWindow::SashHitTest(int x, int y, int tolerance)
463 {
464 if ( m_windowTwo == NULL || m_sashPosition == 0)
465 return false; // No sash
466
467 int z = m_splitMode == wxSPLIT_VERTICAL ? x : y;
468 int hitMin = m_sashPosition - tolerance;
469 int hitMax = m_sashPosition + GetSashSize() + tolerance;
470
471 return z >= hitMin && z <= hitMax;
472 }
473
474 int wxSplitterWindow::GetSashSize() const
475 {
476 return m_sashSize > -1 ? m_sashSize : wxRendererNative::Get().GetSplitterParams(this).widthSash;
477 }
478
479 int wxSplitterWindow::GetBorderSize() const
480 {
481 return wxRendererNative::Get().GetSplitterParams(this).border;
482 }
483
484 // Draw the sash
485 void wxSplitterWindow::DrawSash(wxDC& dc)
486 {
487 if (HasFlag(wxSP_3DBORDER))
488 wxRendererNative::Get().DrawSplitterBorder
489 (
490 this,
491 dc,
492 GetClientRect()
493 );
494
495 // don't draw sash if we're not split
496 if ( m_sashPosition == 0 || !m_windowTwo )
497 return;
498
499 // nor if we're configured to not show it
500 if ( HasFlag(wxSP_NOSASH) )
501 return;
502
503 wxRendererNative::Get().DrawSplitterSash
504 (
505 this,
506 dc,
507 GetClientSize(),
508 m_sashPosition,
509 m_splitMode == wxSPLIT_VERTICAL ? wxVERTICAL
510 : wxHORIZONTAL,
511 m_isHot ? (int)wxCONTROL_CURRENT : 0
512 );
513 }
514
515 // Draw the sash tracker (for whilst moving the sash)
516 void wxSplitterWindow::DrawSashTracker(int x, int y)
517 {
518 int w, h;
519 GetClientSize(&w, &h);
520
521 wxScreenDC screenDC;
522 int x1, y1;
523 int x2, y2;
524
525 if ( m_splitMode == wxSPLIT_VERTICAL )
526 {
527 x1 = x; y1 = 2;
528 x2 = x; y2 = h-2;
529
530 if ( x1 > w )
531 {
532 x1 = w; x2 = w;
533 }
534 else if ( x1 < 0 )
535 {
536 x1 = 0; x2 = 0;
537 }
538 }
539 else
540 {
541 x1 = 2; y1 = y;
542 x2 = w-2; y2 = y;
543
544 if ( y1 > h )
545 {
546 y1 = h;
547 y2 = h;
548 }
549 else if ( y1 < 0 )
550 {
551 y1 = 0;
552 y2 = 0;
553 }
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 )
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 h1 =
688 h2 = h - 2*border;
689 x2 = size2;
690 y2 = border;
691 }
692 else // horz splitter
693 {
694 w1 =
695 w2 = w - 2*border;
696 h1 = size1;
697 h2 = h - 2*border - sash - h1;
698 x2 = border;
699 y2 = size2;
700 }
701
702 GetWindow2()->SetSize(x2, y2, w2, h2);
703 GetWindow1()->SetSize(border, border, w1, h1);
704 }
705
706 wxClientDC dc(this);
707 DrawSash(dc);
708
709 SetNeedUpdating(false);
710 }
711
712 // Set pane for unsplit window
713 void wxSplitterWindow::Initialize(wxWindow *window)
714 {
715 wxASSERT_MSG( (!window || (window && window->GetParent() == this)),
716 _T("windows in the splitter should have it as parent!") );
717
718 if (window && !window->IsShown())
719 window->Show();
720
721 m_windowOne = window;
722 m_windowTwo = (wxWindow *) NULL;
723 DoSetSashPosition(0);
724 }
725
726 // Associates the given window with window 2, drawing the appropriate sash
727 // and changing the split mode.
728 // Does nothing and returns false if the window is already split.
729 bool wxSplitterWindow::DoSplit(wxSplitMode mode,
730 wxWindow *window1, wxWindow *window2,
731 int sashPosition)
732 {
733 if ( IsSplit() )
734 return false;
735
736 wxCHECK_MSG( window1 && window2, false,
737 _T("can not split with NULL window(s)") );
738
739 wxCHECK_MSG( window1->GetParent() == this && window2->GetParent() == this, false,
740 _T("windows in the splitter should have it as parent!") );
741
742 if (! window1->IsShown())
743 window1->Show();
744 if (! window2->IsShown())
745 window2->Show();
746
747 m_splitMode = mode;
748 m_windowOne = window1;
749 m_windowTwo = window2;
750
751 // remember the sash position we want to set for later if we can't set it
752 // right now (e.g. because the window is too small)
753 m_requestedSashPosition = sashPosition;
754 m_checkRequestedSashPosition = false;
755
756 DoSetSashPosition(ConvertSashPosition(sashPosition));
757
758 SizeWindows();
759
760 return true;
761 }
762
763 int wxSplitterWindow::ConvertSashPosition(int sashPosition) const
764 {
765 if ( sashPosition > 0 )
766 {
767 return sashPosition;
768 }
769 else if ( sashPosition < 0 )
770 {
771 // It's negative so adding is subtracting
772 return GetWindowSize() + sashPosition;
773 }
774 else // sashPosition == 0
775 {
776 // default, put it in the centre
777 return GetWindowSize() / 2;
778 }
779 }
780
781 // Remove the specified (or second) window from the view
782 // Doesn't actually delete the window.
783 bool wxSplitterWindow::Unsplit(wxWindow *toRemove)
784 {
785 if ( ! IsSplit() )
786 return false;
787
788 wxWindow *win;
789 if ( toRemove == NULL || toRemove == m_windowTwo)
790 {
791 win = m_windowTwo ;
792 m_windowTwo = (wxWindow *) NULL;
793 }
794 else if ( toRemove == m_windowOne )
795 {
796 win = m_windowOne ;
797 m_windowOne = m_windowTwo;
798 m_windowTwo = (wxWindow *) NULL;
799 }
800 else
801 {
802 wxFAIL_MSG(wxT("splitter: attempt to remove a non-existent window"));
803
804 return false;
805 }
806
807 OnUnsplit(win);
808 DoSetSashPosition(0);
809 SizeWindows();
810
811 return true;
812 }
813
814 // Replace a window with another one
815 bool wxSplitterWindow::ReplaceWindow(wxWindow *winOld, wxWindow *winNew)
816 {
817 wxCHECK_MSG( winOld, false, wxT("use one of Split() functions instead") );
818 wxCHECK_MSG( winNew, false, wxT("use Unsplit() functions instead") );
819
820 if ( winOld == m_windowTwo )
821 {
822 m_windowTwo = winNew;
823 }
824 else if ( winOld == m_windowOne )
825 {
826 m_windowOne = winNew;
827 }
828 else
829 {
830 wxFAIL_MSG(wxT("splitter: attempt to replace a non-existent window"));
831
832 return false;
833 }
834
835 SizeWindows();
836
837 return true;
838 }
839
840 void wxSplitterWindow::SetMinimumPaneSize(int min)
841 {
842 m_minimumPaneSize = min;
843 int pos = m_requestedSashPosition != INT_MAX ? m_requestedSashPosition : m_sashPosition;
844 SetSashPosition(pos); // re-check limits
845 }
846
847 void wxSplitterWindow::SetSashPosition(int position, bool redraw)
848 {
849 // remember the sash position we want to set for later if we can't set it
850 // right now (e.g. because the window is too small)
851 m_requestedSashPosition = position;
852 m_checkRequestedSashPosition = false;
853
854 DoSetSashPosition(ConvertSashPosition(position));
855
856 if ( redraw )
857 {
858 SizeWindows();
859 }
860 }
861
862 // Make sure the child window sizes are updated. This is useful
863 // for reducing flicker by updating the sizes before a
864 // window is shown, if you know the overall size is correct.
865 void wxSplitterWindow::UpdateSize()
866 {
867 m_checkRequestedSashPosition = true;
868 SizeWindows();
869 m_checkRequestedSashPosition = false;
870 }
871
872 bool wxSplitterWindow::DoSendEvent(wxSplitterEvent& event)
873 {
874 return !GetEventHandler()->ProcessEvent(event) || event.IsAllowed();
875 }
876
877 wxSize wxSplitterWindow::DoGetBestSize() const
878 {
879 // get best sizes of subwindows
880 wxSize size1, size2;
881 if ( m_windowOne )
882 size1 = m_windowOne->GetAdjustedBestSize();
883 if ( m_windowTwo )
884 size2 = m_windowTwo->GetAdjustedBestSize();
885
886 // sum them
887 //
888 // pSash points to the size component to which sash size must be added
889 int *pSash;
890 wxSize sizeBest;
891 if ( m_splitMode == wxSPLIT_VERTICAL )
892 {
893 sizeBest.y = wxMax(size1.y, size2.y);
894 sizeBest.x = wxMax(size1.x, m_minimumPaneSize) +
895 wxMax(size2.x, m_minimumPaneSize);
896
897 pSash = &sizeBest.x;
898 }
899 else // wxSPLIT_HORIZONTAL
900 {
901 sizeBest.x = wxMax(size1.x, size2.x);
902 sizeBest.y = wxMax(size1.y, m_minimumPaneSize) +
903 wxMax(size2.y, m_minimumPaneSize);
904
905 pSash = &sizeBest.y;
906 }
907
908 // account for the border and the sash
909 int border = 2*GetBorderSize();
910 *pSash += GetSashSize();
911 sizeBest.x += border;
912 sizeBest.y += border;
913
914 return sizeBest;
915 }
916
917 // ---------------------------------------------------------------------------
918 // wxSplitterWindow virtual functions: they now just generate the events
919 // ---------------------------------------------------------------------------
920
921 bool wxSplitterWindow::OnSashPositionChange(int WXUNUSED(newSashPosition))
922 {
923 // always allow by default
924 return true;
925 }
926
927 int wxSplitterWindow::OnSashPositionChanging(int newSashPosition)
928 {
929 // If within UNSPLIT_THRESHOLD from edge, set to edge to cause closure.
930 const int UNSPLIT_THRESHOLD = 4;
931
932 // first of all, check if OnSashPositionChange() doesn't forbid this change
933 if ( !OnSashPositionChange(newSashPosition) )
934 {
935 // it does
936 return -1;
937 }
938
939 // Obtain relevant window dimension for bottom / right threshold check
940 int window_size = GetWindowSize();
941
942 bool unsplit_scenario = false;
943 if ( m_permitUnsplitAlways || m_minimumPaneSize == 0 )
944 {
945 // Do edge detection if unsplit premitted
946 if ( newSashPosition <= UNSPLIT_THRESHOLD )
947 {
948 // threshold top / left check
949 newSashPosition = 0;
950 unsplit_scenario = true;
951 }
952 if ( newSashPosition >= window_size - UNSPLIT_THRESHOLD )
953 {
954 // threshold bottom/right check
955 newSashPosition = window_size;
956 unsplit_scenario = true;
957 }
958 }
959
960 if ( !unsplit_scenario )
961 {
962 // If resultant pane would be too small, enlarge it
963 newSashPosition = AdjustSashPosition(newSashPosition);
964 }
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 // now let the event handler have it
972 //
973 // FIXME: shouldn't we do it before the adjustments above so as to ensure
974 // that the sash position is always reasonable?
975 wxSplitterEvent event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING, this);
976 event.m_data.pos = newSashPosition;
977
978 if ( !DoSendEvent(event) )
979 {
980 // the event handler vetoed the change
981 newSashPosition = -1;
982 }
983 else
984 {
985 // it could have been changed by it
986 newSashPosition = event.GetSashPosition();
987 }
988
989 return newSashPosition;
990 }
991
992 // Called when the sash is double-clicked. The default behaviour is to remove
993 // the sash if the minimum pane size is zero.
994 void wxSplitterWindow::OnDoubleClickSash(int x, int y)
995 {
996 wxCHECK_RET(m_windowTwo, wxT("splitter: no window to remove"));
997
998 // new code should handle events instead of using the virtual functions
999 wxSplitterEvent event(wxEVT_COMMAND_SPLITTER_DOUBLECLICKED, this);
1000 event.m_data.pt.x = x;
1001 event.m_data.pt.y = y;
1002 if ( DoSendEvent(event) )
1003 {
1004 if ( GetMinimumPaneSize() == 0 || m_permitUnsplitAlways )
1005 {
1006 wxWindow* win = m_windowTwo;
1007 if ( Unsplit(win) )
1008 {
1009 wxSplitterEvent unsplitEvent(wxEVT_COMMAND_SPLITTER_UNSPLIT, this);
1010 unsplitEvent.m_data.win = win;
1011 (void)DoSendEvent(unsplitEvent);
1012 }
1013 }
1014 }
1015 //else: blocked by user
1016 }
1017
1018 void wxSplitterWindow::OnUnsplit(wxWindow *winRemoved)
1019 {
1020 // call this before calling the event handler which may delete the window
1021 winRemoved->Show(false);
1022 }
1023
1024 #if defined( __WXMSW__ ) || defined( __WXMAC__)
1025
1026 // this is currently called (and needed) under MSW only...
1027 void wxSplitterWindow::OnSetCursor(wxSetCursorEvent& event)
1028 {
1029 // if we don't do it, the resizing cursor might be set for child window:
1030 // and like this we explicitly say that our cursor should not be used for
1031 // children windows which overlap us
1032
1033 if ( SashHitTest(event.GetX(), event.GetY(), 0) )
1034 {
1035 // default processing is ok
1036 event.Skip();
1037 }
1038 //else: do nothing, in particular, don't call Skip()
1039 }
1040
1041 #endif // wxMSW || wxMac
1042
1043 #endif // wxUSE_SPLITTER
1044