]> git.saurik.com Git - wxWidgets.git/blob - src/generic/splitter.cpp
VC6 compilation fixes for wxTreeListCtrl.
[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 SizeWindows();
207 }
208
209 void wxSplitterWindow::OnMouseEvent(wxMouseEvent& event)
210 {
211 int x = (int)event.GetX(),
212 y = (int)event.GetY();
213
214 if ( GetWindowStyle() & wxSP_NOSASH )
215 {
216 event.Skip();
217 return;
218 }
219
220 bool isLive = IsLive(this);
221
222 if (event.LeftDown())
223 {
224 if ( SashHitTest(x, y) )
225 {
226 // Start the drag now
227 m_dragMode = wxSPLIT_DRAG_DRAGGING;
228
229 // Capture mouse and set the cursor
230 CaptureMouse();
231 SetResizeCursor();
232
233 if ( !isLive )
234 {
235 // remember the initial sash position and draw the initial
236 // shadow sash
237 m_sashPositionCurrent = m_sashPosition;
238
239 m_oldX = (m_splitMode == wxSPLIT_VERTICAL ? m_sashPositionCurrent : x);
240 m_oldY = (m_splitMode != wxSPLIT_VERTICAL ? m_sashPositionCurrent : y);
241 DrawSashTracker(m_oldX, m_oldY);
242 }
243
244 m_ptStart = wxPoint(x,y);
245 m_sashStart = m_sashPosition;
246 return;
247 }
248 }
249 else if (event.LeftUp() && m_dragMode == wxSPLIT_DRAG_DRAGGING)
250 {
251 // We can stop dragging now and see what we've got.
252 m_dragMode = wxSPLIT_DRAG_NONE;
253
254 // Release mouse and unset the cursor
255 ReleaseMouse();
256 SetCursor(* wxSTANDARD_CURSOR);
257
258 // exit if unsplit after doubleclick
259 if ( !IsSplit() )
260 {
261 return;
262 }
263
264 // Erase old tracker
265 if ( !isLive )
266 {
267 DrawSashTracker(m_oldX, m_oldY);
268 }
269
270 // the position of the click doesn't exactly correspond to
271 // m_sashPosition, rather it changes it by the distance by which the
272 // mouse has moved
273 int diff = m_splitMode == wxSPLIT_VERTICAL ? x - m_ptStart.x : y - m_ptStart.y;
274
275 int posSashNew = OnSashPositionChanging(m_sashStart + diff);
276 if ( posSashNew == -1 )
277 {
278 // change not allowed
279 return;
280 }
281
282 if ( m_permitUnsplitAlways || m_minimumPaneSize == 0 )
283 {
284 // Deal with possible unsplit scenarios
285 if ( posSashNew == 0 )
286 {
287 // We remove the first window from the view
288 wxWindow *removedWindow = m_windowOne;
289 m_windowOne = m_windowTwo;
290 m_windowTwo = NULL;
291 OnUnsplit(removedWindow);
292 wxSplitterEvent eventUnsplit(wxEVT_COMMAND_SPLITTER_UNSPLIT, this);
293 eventUnsplit.m_data.win = removedWindow;
294 (void)DoSendEvent(eventUnsplit);
295 SetSashPositionAndNotify(0);
296 }
297 else if ( posSashNew == GetWindowSize() )
298 {
299 // We remove the second window from the view
300 wxWindow *removedWindow = m_windowTwo;
301 m_windowTwo = NULL;
302 OnUnsplit(removedWindow);
303 wxSplitterEvent eventUnsplit(wxEVT_COMMAND_SPLITTER_UNSPLIT, this);
304 eventUnsplit.m_data.win = removedWindow;
305 (void)DoSendEvent(eventUnsplit);
306 SetSashPositionAndNotify(0);
307 }
308 else
309 {
310 SetSashPositionAndNotify(posSashNew);
311 }
312 }
313 else
314 {
315 SetSashPositionAndNotify(posSashNew);
316 }
317
318 SizeWindows();
319 } // left up && dragging
320 else if ((event.Moving() || event.Leaving() || event.Entering()) && (m_dragMode == wxSPLIT_DRAG_NONE))
321 {
322 if ( event.Leaving() || !SashHitTest(x, y) )
323 OnLeaveSash();
324 else
325 OnEnterSash();
326 }
327 else if (event.Dragging() && (m_dragMode == wxSPLIT_DRAG_DRAGGING))
328 {
329 int diff = m_splitMode == wxSPLIT_VERTICAL ? x - m_ptStart.x : y - m_ptStart.y;
330
331 int posSashNew = OnSashPositionChanging(m_sashStart + diff);
332 if ( posSashNew == -1 )
333 {
334 // change not allowed
335 return;
336 }
337
338 if ( !isLive )
339 {
340 if ( posSashNew == m_sashPositionCurrent )
341 return;
342
343 m_sashPositionCurrent = posSashNew;
344
345 // Erase old tracker
346 DrawSashTracker(m_oldX, m_oldY);
347
348 m_oldX = (m_splitMode == wxSPLIT_VERTICAL ? m_sashPositionCurrent : x);
349 m_oldY = (m_splitMode != wxSPLIT_VERTICAL ? m_sashPositionCurrent : y);
350
351 #ifdef __WXMSW__
352 // As we captured the mouse, we may get the mouse events from outside
353 // our window - for example, negative values in x, y. This has a weird
354 // consequence under MSW where we use unsigned values sometimes and
355 // signed ones other times: the coordinates turn as big positive
356 // numbers and so the sash is drawn on the *right* side of the window
357 // instead of the left (or bottom instead of top). Correct this.
358 if ( (short)m_oldX < 0 )
359 m_oldX = 0;
360 if ( (short)m_oldY < 0 )
361 m_oldY = 0;
362 #endif // __WXMSW__
363
364 // Draw new one
365 DrawSashTracker(m_oldX, m_oldY);
366 }
367 else
368 {
369 if ( posSashNew == m_sashPosition )
370 return;
371
372 DoSetSashPosition(posSashNew);
373
374 // in live mode, the new position is the actual sash position, clear requested position!
375 m_requestedSashPosition = INT_MAX;
376 m_needUpdating = true;
377 }
378 }
379 else if ( event.LeftDClick() && m_windowTwo )
380 {
381 OnDoubleClickSash(x, y);
382 }
383 else
384 {
385 event.Skip();
386 }
387 }
388
389 void wxSplitterWindow::OnMouseCaptureLost(wxMouseCaptureLostEvent& WXUNUSED(event))
390 {
391 if (m_dragMode != wxSPLIT_DRAG_DRAGGING)
392 return;
393
394 m_dragMode = wxSPLIT_DRAG_NONE;
395
396 SetCursor(* wxSTANDARD_CURSOR);
397
398 // Erase old tracker
399 if ( !IsLive(this) )
400 {
401 DrawSashTracker(m_oldX, m_oldY);
402 }
403 }
404
405 void wxSplitterWindow::OnSize(wxSizeEvent& event)
406 {
407 // only process this message if we're not iconized - otherwise iconizing
408 // and restoring a window containing the splitter has a funny side effect
409 // of changing the splitter position!
410 wxWindow *parent = wxGetTopLevelParent(this);
411 bool iconized;
412
413 wxTopLevelWindow *winTop = wxDynamicCast(parent, wxTopLevelWindow);
414 if ( winTop )
415 {
416 iconized = winTop->IsIconized();
417 }
418 else
419 {
420 wxFAIL_MSG(wxT("should have a top level parent!"));
421
422 iconized = false;
423 }
424
425 if ( iconized )
426 {
427 m_lastSize = wxSize(0,0);
428
429 event.Skip();
430
431 return;
432 }
433
434 if ( m_windowTwo )
435 {
436 int w, h;
437 GetClientSize(&w, &h);
438
439 int size = m_splitMode == wxSPLIT_VERTICAL ? w : h;
440
441 int old_size = m_splitMode == wxSPLIT_VERTICAL ? m_lastSize.x : m_lastSize.y;
442
443 // Don't do anything if the size didn't really change. In particular,
444 // it is important that we don't reset our sash position because it's
445 // out of current range in this case as otherwise the really requested
446 // position would be lost and never set. Wait until we get a real size
447 // event with our non-initial size to do it.
448 if ( size != old_size )
449 {
450 int delta = (int) ( (size - old_size)*m_sashGravity );
451 if ( delta != 0 )
452 {
453 int newPosition = m_sashPosition + delta;
454 if( newPosition < m_minimumPaneSize )
455 newPosition = m_minimumPaneSize;
456 SetSashPositionAndNotify(newPosition);
457 }
458
459 if ( m_sashPosition >= size - 5 )
460 SetSashPositionAndNotify(wxMax(10, size - 40));
461 m_lastSize = wxSize(w,h);
462 }
463 }
464
465 SizeWindows();
466 }
467
468 void wxSplitterWindow::SetSashGravity(double gravity)
469 {
470 wxCHECK_RET( gravity >= 0. && gravity <= 1.,
471 wxT("invalid gravity value") );
472
473 m_sashGravity = gravity;
474 }
475
476 bool wxSplitterWindow::SashHitTest(int x, int y, int tolerance)
477 {
478 if ( m_windowTwo == NULL || m_sashPosition == 0)
479 return false; // No sash
480
481 int z = m_splitMode == wxSPLIT_VERTICAL ? x : y;
482 int hitMin = m_sashPosition - tolerance;
483 int hitMax = m_sashPosition + GetSashSize() + tolerance;
484
485 return z >= hitMin && z <= hitMax;
486 }
487
488 int wxSplitterWindow::GetSashSize() const
489 {
490 return wxRendererNative::Get().GetSplitterParams(this).widthSash;
491 }
492
493 int wxSplitterWindow::GetBorderSize() const
494 {
495 return wxRendererNative::Get().GetSplitterParams(this).border;
496 }
497
498 // Draw the sash
499 void wxSplitterWindow::DrawSash(wxDC& dc)
500 {
501 if (HasFlag(wxSP_3DBORDER))
502 wxRendererNative::Get().DrawSplitterBorder
503 (
504 this,
505 dc,
506 GetClientRect()
507 );
508
509 // don't draw sash if we're not split
510 if ( m_sashPosition == 0 || !m_windowTwo )
511 return;
512
513 // nor if we're configured to not show it
514 if ( HasFlag(wxSP_NOSASH) )
515 return;
516
517 wxRendererNative::Get().DrawSplitterSash
518 (
519 this,
520 dc,
521 GetClientSize(),
522 m_sashPosition,
523 m_splitMode == wxSPLIT_VERTICAL ? wxVERTICAL
524 : wxHORIZONTAL,
525 m_isHot ? (int)wxCONTROL_CURRENT : 0
526 );
527 }
528
529 // Draw the sash tracker (for whilst moving the sash)
530 void wxSplitterWindow::DrawSashTracker(int x, int y)
531 {
532 int w, h;
533 GetClientSize(&w, &h);
534
535 wxScreenDC screenDC;
536 int x1, y1;
537 int x2, y2;
538
539 if ( m_splitMode == wxSPLIT_VERTICAL )
540 {
541 x1 = x2 = wxClip(x, 0, w) + m_sashTrackerPen->GetWidth()/2;
542 y1 = 2;
543 y2 = h-2;
544 }
545 else
546 {
547 y1 = y2 = wxClip(y, 0, h) + m_sashTrackerPen->GetWidth()/2;
548 x1 = 2;
549 x2 = w-2;
550 }
551
552 ClientToScreen(&x1, &y1);
553 ClientToScreen(&x2, &y2);
554
555 screenDC.SetLogicalFunction(wxINVERT);
556 screenDC.SetPen(*m_sashTrackerPen);
557 screenDC.SetBrush(*wxTRANSPARENT_BRUSH);
558
559 screenDC.DrawLine(x1, y1, x2, y2);
560
561 screenDC.SetLogicalFunction(wxCOPY);
562 }
563
564 int wxSplitterWindow::GetWindowSize() const
565 {
566 wxSize size = GetClientSize();
567
568 return m_splitMode == wxSPLIT_VERTICAL ? size.x : size.y;
569 }
570
571 int wxSplitterWindow::AdjustSashPosition(int sashPos) const
572 {
573 wxWindow *win;
574
575 win = GetWindow1();
576 if ( win )
577 {
578 // the window shouldn't be smaller than its own minimal size nor
579 // smaller than the minimual pane size specified for this splitter
580 int minSize = m_splitMode == wxSPLIT_VERTICAL ? win->GetMinWidth()
581 : win->GetMinHeight();
582
583 if ( minSize == -1 || m_minimumPaneSize > minSize )
584 minSize = m_minimumPaneSize;
585
586 minSize += GetBorderSize();
587
588 if ( sashPos < minSize )
589 sashPos = minSize;
590 }
591
592 win = GetWindow2();
593 if ( win )
594 {
595 int minSize = m_splitMode == wxSPLIT_VERTICAL ? win->GetMinWidth()
596 : win->GetMinHeight();
597
598 if ( minSize == -1 || m_minimumPaneSize > minSize )
599 minSize = m_minimumPaneSize;
600
601 int maxSize = GetWindowSize() - minSize - GetBorderSize() - GetSashSize();
602 if ( maxSize > 0 && sashPos > maxSize && maxSize >= m_minimumPaneSize)
603 sashPos = maxSize;
604 }
605
606 return sashPos;
607 }
608
609 bool wxSplitterWindow::DoSetSashPosition(int sashPos)
610 {
611 int newSashPosition = AdjustSashPosition(sashPos);
612
613 if ( newSashPosition == m_sashPosition )
614 return false;
615
616 m_sashPosition = newSashPosition;
617
618 return true;
619 }
620
621 void wxSplitterWindow::SetSashPositionAndNotify(int sashPos)
622 {
623 // we must reset the request here, otherwise the sash would be stuck at
624 // old position if the user attempted to move the sash after invalid
625 // (e.g. smaller than minsize) sash position was requested using
626 // SetSashPosition():
627 m_requestedSashPosition = INT_MAX;
628
629 // note that we must send the event in any case, i.e. even if the sash
630 // position hasn't changed and DoSetSashPosition() returns false because we
631 // must generate a CHANGED event at the end of resizing
632 DoSetSashPosition(sashPos);
633
634 wxSplitterEvent event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED, this);
635 event.m_data.pos = m_sashPosition;
636
637 (void)DoSendEvent(event);
638 }
639
640 // Position and size subwindows.
641 // Note that the border size applies to each subwindow, not
642 // including the edges next to the sash.
643 void wxSplitterWindow::SizeWindows()
644 {
645 // check if we have delayed setting the real sash position
646 if ( m_checkRequestedSashPosition && m_requestedSashPosition != INT_MAX )
647 {
648 int newSashPosition = ConvertSashPosition(m_requestedSashPosition);
649 if ( newSashPosition != m_sashPosition )
650 {
651 DoSetSashPosition(newSashPosition);
652 }
653
654 if ( newSashPosition <= m_sashPosition
655 && newSashPosition >= m_sashPosition - GetBorderSize() )
656 {
657 // don't update it any more
658 m_requestedSashPosition = INT_MAX;
659 }
660 }
661
662 int w, h;
663 GetClientSize(&w, &h);
664
665 if ( GetWindow1() && !GetWindow2() )
666 {
667 GetWindow1()->SetSize(GetBorderSize(), GetBorderSize(),
668 w - 2*GetBorderSize(), h - 2*GetBorderSize());
669 }
670 else if ( GetWindow1() && GetWindow2() )
671 {
672 const int border = GetBorderSize(),
673 sash = GetSashSize();
674
675 int size1 = GetSashPosition() - border,
676 size2 = GetSashPosition() + sash;
677
678 int x2, y2, w1, h1, w2, h2;
679 if ( GetSplitMode() == wxSPLIT_VERTICAL )
680 {
681 w1 = size1;
682 w2 = w - 2*border - sash - w1;
683 if (w2 < 0)
684 w2 = 0;
685 h2 = h - 2*border;
686 if (h2 < 0)
687 h2 = 0;
688 h1 = h2;
689 x2 = size2;
690 y2 = border;
691 }
692 else // horz splitter
693 {
694 w2 = w - 2*border;
695 if (w2 < 0)
696 w2 = 0;
697 w1 = w2;
698 h1 = size1;
699 h2 = h - 2*border - sash - h1;
700 if (h2 < 0)
701 h2 = 0;
702 x2 = border;
703 y2 = size2;
704 }
705
706 GetWindow2()->SetSize(x2, y2, w2, h2);
707 GetWindow1()->SetSize(border, border, w1, h1);
708 }
709
710 wxClientDC dc(this);
711 DrawSash(dc);
712
713 SetNeedUpdating(false);
714 }
715
716 // Set pane for unsplit window
717 void wxSplitterWindow::Initialize(wxWindow *window)
718 {
719 wxASSERT_MSG( (!window || window->GetParent() == this),
720 wxT("windows in the splitter should have it as parent!") );
721
722 if (window && !window->IsShown())
723 window->Show();
724
725 m_windowOne = window;
726 m_windowTwo = NULL;
727 DoSetSashPosition(0);
728 }
729
730 // Associates the given window with window 2, drawing the appropriate sash
731 // and changing the split mode.
732 // Does nothing and returns false if the window is already split.
733 bool wxSplitterWindow::DoSplit(wxSplitMode mode,
734 wxWindow *window1, wxWindow *window2,
735 int sashPosition)
736 {
737 if ( IsSplit() )
738 return false;
739
740 wxCHECK_MSG( window1 && window2, false,
741 wxT("cannot split with NULL window(s)") );
742
743 wxCHECK_MSG( window1->GetParent() == this && window2->GetParent() == this, false,
744 wxT("windows in the splitter should have it as parent!") );
745
746 if (! window1->IsShown())
747 window1->Show();
748 if (! window2->IsShown())
749 window2->Show();
750
751 m_splitMode = mode;
752 m_windowOne = window1;
753 m_windowTwo = window2;
754
755
756 SetSashPosition(sashPosition, true);
757 return true;
758 }
759
760 int wxSplitterWindow::ConvertSashPosition(int sashPosition) const
761 {
762 if ( sashPosition > 0 )
763 {
764 return sashPosition;
765 }
766 else if ( sashPosition < 0 )
767 {
768 // It's negative so adding is subtracting
769 return GetWindowSize() + sashPosition;
770 }
771 else // sashPosition == 0
772 {
773 // default, put it in the centre
774 return GetWindowSize() / 2;
775 }
776 }
777
778 // Remove the specified (or second) window from the view
779 // Doesn't actually delete the window.
780 bool wxSplitterWindow::Unsplit(wxWindow *toRemove)
781 {
782 if ( ! IsSplit() )
783 return false;
784
785 wxWindow *win;
786 if ( toRemove == NULL || toRemove == m_windowTwo)
787 {
788 win = m_windowTwo ;
789 m_windowTwo = NULL;
790 }
791 else if ( toRemove == m_windowOne )
792 {
793 win = m_windowOne ;
794 m_windowOne = m_windowTwo;
795 m_windowTwo = NULL;
796 }
797 else
798 {
799 wxFAIL_MSG(wxT("splitter: attempt to remove a non-existent window"));
800
801 return false;
802 }
803
804 OnUnsplit(win);
805 DoSetSashPosition(0);
806 SizeWindows();
807
808 return true;
809 }
810
811 // Replace a window with another one
812 bool wxSplitterWindow::ReplaceWindow(wxWindow *winOld, wxWindow *winNew)
813 {
814 wxCHECK_MSG( winOld, false, wxT("use one of Split() functions instead") );
815 wxCHECK_MSG( winNew, false, wxT("use Unsplit() functions instead") );
816
817 if ( winOld == m_windowTwo )
818 {
819 m_windowTwo = winNew;
820 }
821 else if ( winOld == m_windowOne )
822 {
823 m_windowOne = winNew;
824 }
825 else
826 {
827 wxFAIL_MSG(wxT("splitter: attempt to replace a non-existent window"));
828
829 return false;
830 }
831
832 SizeWindows();
833
834 return true;
835 }
836
837 void wxSplitterWindow::SetMinimumPaneSize(int min)
838 {
839 m_minimumPaneSize = min;
840 int pos = m_requestedSashPosition != INT_MAX ? m_requestedSashPosition : m_sashPosition;
841 SetSashPosition(pos); // re-check limits
842 }
843
844 void wxSplitterWindow::SetSashPosition(int position, bool redraw)
845 {
846 // remember the sash position we want to set for later if we can't set it
847 // right now (e.g. because the window is too small)
848 m_requestedSashPosition = position;
849 m_checkRequestedSashPosition = false;
850
851 DoSetSashPosition(ConvertSashPosition(position));
852
853 if ( redraw )
854 {
855 SizeWindows();
856 }
857 }
858
859 // Make sure the child window sizes are updated. This is useful
860 // for reducing flicker by updating the sizes before a
861 // window is shown, if you know the overall size is correct.
862 void wxSplitterWindow::UpdateSize()
863 {
864 m_checkRequestedSashPosition = true;
865 SizeWindows();
866 m_checkRequestedSashPosition = false;
867 }
868
869 bool wxSplitterWindow::DoSendEvent(wxSplitterEvent& event)
870 {
871 return !GetEventHandler()->ProcessEvent(event) || event.IsAllowed();
872 }
873
874 wxSize wxSplitterWindow::DoGetBestSize() const
875 {
876 // get best sizes of subwindows
877 wxSize size1, size2;
878 if ( m_windowOne )
879 size1 = m_windowOne->GetEffectiveMinSize();
880 if ( m_windowTwo )
881 size2 = m_windowTwo->GetEffectiveMinSize();
882
883 // sum them
884 //
885 // pSash points to the size component to which sash size must be added
886 int *pSash;
887 wxSize sizeBest;
888 if ( m_splitMode == wxSPLIT_VERTICAL )
889 {
890 sizeBest.y = wxMax(size1.y, size2.y);
891 sizeBest.x = wxMax(size1.x, m_minimumPaneSize) +
892 wxMax(size2.x, m_minimumPaneSize);
893
894 pSash = &sizeBest.x;
895 }
896 else // wxSPLIT_HORIZONTAL
897 {
898 sizeBest.x = wxMax(size1.x, size2.x);
899 sizeBest.y = wxMax(size1.y, m_minimumPaneSize) +
900 wxMax(size2.y, m_minimumPaneSize);
901
902 pSash = &sizeBest.y;
903 }
904
905 // account for the sash if the window is actually split
906 if ( m_windowOne && m_windowTwo )
907 *pSash += GetSashSize();
908
909 // account for the border too
910 int border = 2*GetBorderSize();
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 // If the result is out of bounds it means minimum size is too big,
966 // so split window in half as best compromise.
967 if ( newSashPosition < 0 || newSashPosition > window_size )
968 newSashPosition = window_size / 2;
969 }
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