]> git.saurik.com Git - wxWidgets.git/blob - src/generic/splitter.cpp
Restore the use of the correct brush for toolbar background erasing.
[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
198 DrawSash(dc);
199 }
200
201 void wxSplitterWindow::OnInternalIdle()
202 {
203 wxWindow::OnInternalIdle();
204
205 // if this is the first idle time after a sash position has potentially
206 // been set, allow SizeWindows to check for a requested size.
207 if (!m_checkRequestedSashPosition)
208 {
209 m_checkRequestedSashPosition = true;
210 SizeWindows();
211 return; // it won't needUpdating in this case
212 }
213
214 if (m_needUpdating)
215 SizeWindows();
216 }
217
218 void wxSplitterWindow::OnMouseEvent(wxMouseEvent& event)
219 {
220 int x = (int)event.GetX(),
221 y = (int)event.GetY();
222
223 if ( GetWindowStyle() & wxSP_NOSASH )
224 {
225 event.Skip();
226 return;
227 }
228
229 bool isLive = IsLive(this);
230
231 if (event.LeftDown())
232 {
233 if ( SashHitTest(x, y) )
234 {
235 // Start the drag now
236 m_dragMode = wxSPLIT_DRAG_DRAGGING;
237
238 // Capture mouse and set the cursor
239 CaptureMouse();
240 SetResizeCursor();
241
242 if ( !isLive )
243 {
244 // remember the initial sash position and draw the initial
245 // shadow sash
246 m_sashPositionCurrent = m_sashPosition;
247
248 m_oldX = (m_splitMode == wxSPLIT_VERTICAL ? m_sashPositionCurrent : x);
249 m_oldY = (m_splitMode != wxSPLIT_VERTICAL ? m_sashPositionCurrent : y);
250 DrawSashTracker(m_oldX, m_oldY);
251 }
252
253 m_ptStart = wxPoint(x,y);
254 m_sashStart = m_sashPosition;
255 return;
256 }
257 }
258 else if (event.LeftUp() && m_dragMode == wxSPLIT_DRAG_DRAGGING)
259 {
260 // We can stop dragging now and see what we've got.
261 m_dragMode = wxSPLIT_DRAG_NONE;
262
263 // Release mouse and unset the cursor
264 ReleaseMouse();
265 SetCursor(* wxSTANDARD_CURSOR);
266
267 // exit if unsplit after doubleclick
268 if ( !IsSplit() )
269 {
270 return;
271 }
272
273 // Erase old tracker
274 if ( !isLive )
275 {
276 DrawSashTracker(m_oldX, m_oldY);
277 }
278
279 // the position of the click doesn't exactly correspond to
280 // m_sashPosition, rather it changes it by the distance by which the
281 // mouse has moved
282 int diff = m_splitMode == wxSPLIT_VERTICAL ? x - m_ptStart.x : y - m_ptStart.y;
283
284 int posSashNew = OnSashPositionChanging(m_sashStart + diff);
285 if ( posSashNew == -1 )
286 {
287 // change not allowed
288 return;
289 }
290
291 if ( m_permitUnsplitAlways || m_minimumPaneSize == 0 )
292 {
293 // Deal with possible unsplit scenarios
294 if ( posSashNew == 0 )
295 {
296 // We remove the first window from the view
297 wxWindow *removedWindow = m_windowOne;
298 m_windowOne = m_windowTwo;
299 m_windowTwo = NULL;
300 OnUnsplit(removedWindow);
301 wxSplitterEvent eventUnsplit(wxEVT_COMMAND_SPLITTER_UNSPLIT, this);
302 eventUnsplit.m_data.win = removedWindow;
303 (void)DoSendEvent(eventUnsplit);
304 SetSashPositionAndNotify(0);
305 }
306 else if ( posSashNew == GetWindowSize() )
307 {
308 // We remove the second window from the view
309 wxWindow *removedWindow = m_windowTwo;
310 m_windowTwo = NULL;
311 OnUnsplit(removedWindow);
312 wxSplitterEvent eventUnsplit(wxEVT_COMMAND_SPLITTER_UNSPLIT, this);
313 eventUnsplit.m_data.win = removedWindow;
314 (void)DoSendEvent(eventUnsplit);
315 SetSashPositionAndNotify(0);
316 }
317 else
318 {
319 SetSashPositionAndNotify(posSashNew);
320 }
321 }
322 else
323 {
324 SetSashPositionAndNotify(posSashNew);
325 }
326
327 SizeWindows();
328 } // left up && dragging
329 else if ((event.Moving() || event.Leaving() || event.Entering()) && (m_dragMode == wxSPLIT_DRAG_NONE))
330 {
331 if ( event.Leaving() || !SashHitTest(x, y) )
332 OnLeaveSash();
333 else
334 OnEnterSash();
335 }
336 else if (event.Dragging() && (m_dragMode == wxSPLIT_DRAG_DRAGGING))
337 {
338 int diff = m_splitMode == wxSPLIT_VERTICAL ? x - m_ptStart.x : y - m_ptStart.y;
339
340 int posSashNew = OnSashPositionChanging(m_sashStart + diff);
341 if ( posSashNew == -1 )
342 {
343 // change not allowed
344 return;
345 }
346
347 if ( !isLive )
348 {
349 if ( posSashNew == m_sashPositionCurrent )
350 return;
351
352 m_sashPositionCurrent = posSashNew;
353
354 // Erase old tracker
355 DrawSashTracker(m_oldX, m_oldY);
356
357 m_oldX = (m_splitMode == wxSPLIT_VERTICAL ? m_sashPositionCurrent : x);
358 m_oldY = (m_splitMode != wxSPLIT_VERTICAL ? m_sashPositionCurrent : y);
359
360 #ifdef __WXMSW__
361 // As we captured the mouse, we may get the mouse events from outside
362 // our window - for example, negative values in x, y. This has a weird
363 // consequence under MSW where we use unsigned values sometimes and
364 // signed ones other times: the coordinates turn as big positive
365 // numbers and so the sash is drawn on the *right* side of the window
366 // instead of the left (or bottom instead of top). Correct this.
367 if ( (short)m_oldX < 0 )
368 m_oldX = 0;
369 if ( (short)m_oldY < 0 )
370 m_oldY = 0;
371 #endif // __WXMSW__
372
373 // Draw new one
374 DrawSashTracker(m_oldX, m_oldY);
375 }
376 else
377 {
378 if ( posSashNew == m_sashPosition )
379 return;
380
381 DoSetSashPosition(posSashNew);
382
383 // in live mode, the new position is the actual sash position, clear requested position!
384 m_requestedSashPosition = INT_MAX;
385 m_needUpdating = true;
386 }
387 }
388 else if ( event.LeftDClick() && m_windowTwo )
389 {
390 OnDoubleClickSash(x, y);
391 }
392 else
393 {
394 event.Skip();
395 }
396 }
397
398 void wxSplitterWindow::OnMouseCaptureLost(wxMouseCaptureLostEvent& WXUNUSED(event))
399 {
400 if (m_dragMode != wxSPLIT_DRAG_DRAGGING)
401 return;
402
403 m_dragMode = wxSPLIT_DRAG_NONE;
404
405 SetCursor(* wxSTANDARD_CURSOR);
406
407 // Erase old tracker
408 if ( !IsLive(this) )
409 {
410 DrawSashTracker(m_oldX, m_oldY);
411 }
412 }
413
414 void wxSplitterWindow::OnSize(wxSizeEvent& event)
415 {
416 // only process this message if we're not iconized - otherwise iconizing
417 // and restoring a window containing the splitter has a funny side effect
418 // of changing the splitter position!
419 wxWindow *parent = wxGetTopLevelParent(this);
420 bool iconized;
421
422 wxTopLevelWindow *winTop = wxDynamicCast(parent, wxTopLevelWindow);
423 if ( winTop )
424 {
425 iconized = winTop->IsIconized();
426 }
427 else
428 {
429 wxFAIL_MSG(wxT("should have a top level parent!"));
430
431 iconized = false;
432 }
433
434 if ( iconized )
435 {
436 m_lastSize = wxSize(0,0);
437
438 event.Skip();
439
440 return;
441 }
442
443 if ( m_windowTwo )
444 {
445 int w, h;
446 GetClientSize(&w, &h);
447
448 int size = m_splitMode == wxSPLIT_VERTICAL ? w : h;
449
450 int old_size = m_splitMode == wxSPLIT_VERTICAL ? m_lastSize.x : m_lastSize.y;
451 if ( old_size != 0 )
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
463 if ( m_sashPosition >= size - 5 )
464 SetSashPositionAndNotify(wxMax(10, size - 40));
465 m_lastSize = wxSize(w,h);
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 m_sashSize > -1 ? m_sashSize : 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 SetNeedUpdating(false);
717 }
718
719 // Set pane for unsplit window
720 void wxSplitterWindow::Initialize(wxWindow *window)
721 {
722 wxASSERT_MSG( (!window || window->GetParent() == this),
723 wxT("windows in the splitter should have it as parent!") );
724
725 if (window && !window->IsShown())
726 window->Show();
727
728 m_windowOne = window;
729 m_windowTwo = NULL;
730 DoSetSashPosition(0);
731 }
732
733 // Associates the given window with window 2, drawing the appropriate sash
734 // and changing the split mode.
735 // Does nothing and returns false if the window is already split.
736 bool wxSplitterWindow::DoSplit(wxSplitMode mode,
737 wxWindow *window1, wxWindow *window2,
738 int sashPosition)
739 {
740 if ( IsSplit() )
741 return false;
742
743 wxCHECK_MSG( window1 && window2, false,
744 wxT("can not split with NULL window(s)") );
745
746 wxCHECK_MSG( window1->GetParent() == this && window2->GetParent() == this, false,
747 wxT("windows in the splitter should have it as parent!") );
748
749 if (! window1->IsShown())
750 window1->Show();
751 if (! window2->IsShown())
752 window2->Show();
753
754 m_splitMode = mode;
755 m_windowOne = window1;
756 m_windowTwo = window2;
757
758
759 SetSashPosition(sashPosition, true);
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 = NULL;
793 }
794 else if ( toRemove == m_windowOne )
795 {
796 win = m_windowOne ;
797 m_windowOne = m_windowTwo;
798 m_windowTwo = 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->GetEffectiveMinSize();
883 if ( m_windowTwo )
884 size2 = m_windowTwo->GetEffectiveMinSize();
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 sash if the window is actually split
909 if ( m_windowOne && m_windowTwo )
910 *pSash += GetSashSize();
911
912 // account for the border too
913 int border = 2*GetBorderSize();
914 sizeBest.x += border;
915 sizeBest.y += border;
916
917 return sizeBest;
918 }
919
920 // ---------------------------------------------------------------------------
921 // wxSplitterWindow virtual functions: they now just generate the events
922 // ---------------------------------------------------------------------------
923
924 bool wxSplitterWindow::OnSashPositionChange(int WXUNUSED(newSashPosition))
925 {
926 // always allow by default
927 return true;
928 }
929
930 int wxSplitterWindow::OnSashPositionChanging(int newSashPosition)
931 {
932 // If within UNSPLIT_THRESHOLD from edge, set to edge to cause closure.
933 const int UNSPLIT_THRESHOLD = 4;
934
935 // first of all, check if OnSashPositionChange() doesn't forbid this change
936 if ( !OnSashPositionChange(newSashPosition) )
937 {
938 // it does
939 return -1;
940 }
941
942 // Obtain relevant window dimension for bottom / right threshold check
943 int window_size = GetWindowSize();
944
945 bool unsplit_scenario = false;
946 if ( m_permitUnsplitAlways || m_minimumPaneSize == 0 )
947 {
948 // Do edge detection if unsplit premitted
949 if ( newSashPosition <= UNSPLIT_THRESHOLD )
950 {
951 // threshold top / left check
952 newSashPosition = 0;
953 unsplit_scenario = true;
954 }
955 if ( newSashPosition >= window_size - UNSPLIT_THRESHOLD )
956 {
957 // threshold bottom/right check
958 newSashPosition = window_size;
959 unsplit_scenario = true;
960 }
961 }
962
963 if ( !unsplit_scenario )
964 {
965 // If resultant pane would be too small, enlarge it
966 newSashPosition = AdjustSashPosition(newSashPosition);
967
968 // If the result is out of bounds it means minimum size is too big,
969 // so split window in half as best compromise.
970 if ( newSashPosition < 0 || newSashPosition > window_size )
971 newSashPosition = window_size / 2;
972 }
973
974 // now let the event handler have it
975 //
976 // FIXME: shouldn't we do it before the adjustments above so as to ensure
977 // that the sash position is always reasonable?
978 wxSplitterEvent event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING, this);
979 event.m_data.pos = newSashPosition;
980
981 if ( !DoSendEvent(event) )
982 {
983 // the event handler vetoed the change
984 newSashPosition = -1;
985 }
986 else
987 {
988 // it could have been changed by it
989 newSashPosition = event.GetSashPosition();
990 }
991
992 return newSashPosition;
993 }
994
995 // Called when the sash is double-clicked. The default behaviour is to remove
996 // the sash if the minimum pane size is zero.
997 void wxSplitterWindow::OnDoubleClickSash(int x, int y)
998 {
999 wxCHECK_RET(m_windowTwo, wxT("splitter: no window to remove"));
1000
1001 // new code should handle events instead of using the virtual functions
1002 wxSplitterEvent event(wxEVT_COMMAND_SPLITTER_DOUBLECLICKED, this);
1003 event.m_data.pt.x = x;
1004 event.m_data.pt.y = y;
1005 if ( DoSendEvent(event) )
1006 {
1007 if ( GetMinimumPaneSize() == 0 || m_permitUnsplitAlways )
1008 {
1009 wxWindow* win = m_windowTwo;
1010 if ( Unsplit(win) )
1011 {
1012 wxSplitterEvent unsplitEvent(wxEVT_COMMAND_SPLITTER_UNSPLIT, this);
1013 unsplitEvent.m_data.win = win;
1014 (void)DoSendEvent(unsplitEvent);
1015 }
1016 }
1017 }
1018 //else: blocked by user
1019 }
1020
1021 void wxSplitterWindow::OnUnsplit(wxWindow *winRemoved)
1022 {
1023 // call this before calling the event handler which may delete the window
1024 winRemoved->Show(false);
1025 }
1026
1027 #if defined( __WXMSW__ ) || defined( __WXMAC__)
1028
1029 // this is currently called (and needed) under MSW only...
1030 void wxSplitterWindow::OnSetCursor(wxSetCursorEvent& event)
1031 {
1032 // if we don't do it, the resizing cursor might be set for child window:
1033 // and like this we explicitly say that our cursor should not be used for
1034 // children windows which overlap us
1035
1036 if ( SashHitTest(event.GetX(), event.GetY(), 0) )
1037 {
1038 // default processing is ok
1039 event.Skip();
1040 }
1041 //else: do nothing, in particular, don't call Skip()
1042 }
1043
1044 #endif // wxMSW || wxMac
1045
1046 #endif // wxUSE_SPLITTER
1047