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