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