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