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