]> git.saurik.com Git - wxWidgets.git/blame - src/generic/splitter.cpp
[1231183] 'cleanup: mismatched indentation' and other cleanings.
[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{
581 int window_size = GetWindowSize();
582
d76ac8ed
VS
583 wxWindow *win;
584
d76ac8ed
VS
585 win = GetWindow1();
586 if ( win )
587 {
2e8cc3e8
VZ
588 // the window shouldn't be smaller than its own minimal size nor
589 // smaller than the minimual pane size specified for this splitter
590 int minSize = m_splitMode == wxSPLIT_VERTICAL ? win->GetMinWidth()
591 : win->GetMinHeight();
592
593 if ( minSize == -1 || m_minimumPaneSize > minSize )
594 minSize = m_minimumPaneSize;
595
596 minSize += GetBorderSize();
597
598 if ( sashPos < minSize )
599 sashPos = minSize;
d76ac8ed
VS
600 }
601
602 win = GetWindow2();
603 if ( win )
604 {
2e8cc3e8
VZ
605 int minSize = m_splitMode == wxSPLIT_VERTICAL ? win->GetMinWidth()
606 : win->GetMinHeight();
607
608 if ( minSize == -1 || m_minimumPaneSize > minSize )
609 minSize = m_minimumPaneSize;
610
4d89e13e 611 int maxSize = window_size - minSize - GetBorderSize() - GetSashSize();
2e8cc3e8
VZ
612 if ( sashPos > maxSize )
613 sashPos = maxSize;
d76ac8ed 614 }
2e8cc3e8
VZ
615
616 return sashPos;
d76ac8ed
VS
617}
618
63ec9dbb 619bool wxSplitterWindow::DoSetSashPosition(int sashPos)
ca39e409 620{
74c57d1f 621 int newSashPosition = AdjustSashPosition(sashPos);
3e58dcb9 622
63ec9dbb 623 if ( newSashPosition == m_sashPosition )
4395fb21 624 return false;
74c57d1f 625
63ec9dbb
VZ
626 m_sashPosition = newSashPosition;
627
4395fb21 628 return true;
63ec9dbb
VZ
629}
630
631void wxSplitterWindow::SetSashPositionAndNotify(int sashPos)
632{
e1366cdb
VS
633 // we must reset the request here, otherwise the sash would be stuck at
634 // old position if the user attempted to move the sash after invalid
14b4c0ff 635 // (e.g. smaller than minsize) sash position was requested using
e1366cdb
VS
636 // SetSashPosition():
637 m_requestedSashPosition = INT_MAX;
638
9ec0e7da
VZ
639 // note that we must send the event in any case, i.e. even if the sash
640 // position hasn't changed and DoSetSashPosition() returns false because we
641 // must generate a CHANGED event at the end of resizing
642 DoSetSashPosition(sashPos);
3e58dcb9 643
9ec0e7da
VZ
644 wxSplitterEvent event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED, this);
645 event.m_data.pos = m_sashPosition;
646
647 (void)DoSendEvent(event);
ca39e409
VS
648}
649
c801d85f
KB
650// Position and size subwindows.
651// Note that the border size applies to each subwindow, not
652// including the edges next to the sash.
0d559d69 653void wxSplitterWindow::SizeWindows()
c801d85f 654{
74c57d1f 655 // check if we have delayed setting the real sash position
4395fb21 656 if ( m_checkRequestedSashPosition && m_requestedSashPosition != INT_MAX )
74c57d1f
VZ
657 {
658 int newSashPosition = ConvertSashPosition(m_requestedSashPosition);
659 if ( newSashPosition != m_sashPosition )
660 {
661 DoSetSashPosition(newSashPosition);
662 }
663
2b5f62a0
VZ
664 if ( newSashPosition <= m_sashPosition
665 && newSashPosition >= m_sashPosition - GetBorderSize() )
74c57d1f
VZ
666 {
667 // don't update it any more
668 m_requestedSashPosition = INT_MAX;
669 }
670 }
ca39e409 671
c801d85f
KB
672 int w, h;
673 GetClientSize(&w, &h);
674
f6bcfd97 675 if ( GetWindow1() && !GetWindow2() )
c801d85f 676 {
63ec9dbb 677 GetWindow1()->SetSize(GetBorderSize(), GetBorderSize(),
ca39e409 678 w - 2*GetBorderSize(), h - 2*GetBorderSize());
c801d85f 679 }
f6bcfd97 680 else if ( GetWindow1() && GetWindow2() )
c801d85f 681 {
b3208e11
VZ
682 const int border = GetBorderSize(),
683 sash = GetSashSize();
684
685 int size1 = GetSashPosition() - border,
686 size2 = GetSashPosition() + sash;
687
688 int x2, y2, w1, h1, w2, h2;
689 if ( GetSplitMode() == wxSPLIT_VERTICAL )
c801d85f 690 {
b3208e11
VZ
691 w1 = size1;
692 w2 = w - 2*border - sash - w1;
693 h1 =
694 h2 = h - 2*border;
695 x2 = size2;
696 y2 = border;
c801d85f 697 }
b3208e11 698 else // horz splitter
c801d85f 699 {
b3208e11
VZ
700 w1 =
701 w2 = w - 2*border;
702 h1 = size1;
703 h2 = h - 2*border - sash - h1;
704 x2 = border;
705 y2 = size2;
c801d85f 706 }
b3208e11 707
b3208e11 708 GetWindow2()->SetSize(x2, y2, w2, h2);
aa69b468 709 GetWindow1()->SetSize(border, border, w1, h1);
c801d85f 710 }
b3208e11 711
c801d85f 712 wxClientDC dc(this);
c801d85f 713 DrawSash(dc);
00a32dc1 714
4395fb21 715 SetNeedUpdating(false);
c801d85f
KB
716}
717
718// Set pane for unsplit window
719void wxSplitterWindow::Initialize(wxWindow *window)
720{
f938940e 721 wxASSERT_MSG( (!window || (window && window->GetParent() == this)),
fe6dc50a
VZ
722 _T("windows in the splitter should have it as parent!") );
723
5755c2b2 724 if (window && !window->IsShown())
b256b4ef 725 window->Show();
a09cd189 726
c801d85f 727 m_windowOne = window;
c67daf87 728 m_windowTwo = (wxWindow *) NULL;
ca39e409 729 DoSetSashPosition(0);
c801d85f
KB
730}
731
732// Associates the given window with window 2, drawing the appropriate sash
733// and changing the split mode.
4395fb21 734// Does nothing and returns false if the window is already split.
2e8cc3e8
VZ
735bool wxSplitterWindow::DoSplit(wxSplitMode mode,
736 wxWindow *window1, wxWindow *window2,
737 int sashPosition)
c801d85f
KB
738{
739 if ( IsSplit() )
4395fb21 740 return false;
c801d85f 741
4395fb21 742 wxCHECK_MSG( window1 && window2, false,
2b5f62a0
VZ
743 _T("can not split with NULL window(s)") );
744
4395fb21 745 wxCHECK_MSG( window1->GetParent() == this && window2->GetParent() == this, false,
fe6dc50a
VZ
746 _T("windows in the splitter should have it as parent!") );
747
874d01fe
RD
748 if (! window1->IsShown())
749 window1->Show();
750 if (! window2->IsShown())
751 window2->Show();
a09cd189 752
2e8cc3e8 753 m_splitMode = mode;
c801d85f
KB
754 m_windowOne = window1;
755 m_windowTwo = window2;
c801d85f 756
74c57d1f
VZ
757 // remember the sash position we want to set for later if we can't set it
758 // right now (e.g. because the window is too small)
759 m_requestedSashPosition = sashPosition;
4395fb21 760 m_checkRequestedSashPosition = false;
ca65c044 761
74c57d1f
VZ
762 DoSetSashPosition(ConvertSashPosition(sashPosition));
763
764 SizeWindows();
765
4395fb21 766 return true;
74c57d1f
VZ
767}
768
769int wxSplitterWindow::ConvertSashPosition(int sashPosition) const
770{
0d559d69 771 if ( sashPosition > 0 )
2e8cc3e8 772 {
74c57d1f 773 return sashPosition;
2e8cc3e8 774 }
0d559d69 775 else if ( sashPosition < 0 )
2e8cc3e8
VZ
776 {
777 // It's negative so adding is subtracting
74c57d1f 778 return GetWindowSize() + sashPosition;
2e8cc3e8 779 }
74c57d1f 780 else // sashPosition == 0
2e8cc3e8 781 {
74c57d1f
VZ
782 // default, put it in the centre
783 return GetWindowSize() / 2;
2e8cc3e8 784 }
c801d85f
KB
785}
786
c801d85f
KB
787// Remove the specified (or second) window from the view
788// Doesn't actually delete the window.
789bool wxSplitterWindow::Unsplit(wxWindow *toRemove)
790{
791 if ( ! IsSplit() )
4395fb21 792 return false;
c801d85f 793
999836aa 794 wxWindow *win;
c801d85f
KB
795 if ( toRemove == NULL || toRemove == m_windowTwo)
796 {
3ad5e06b 797 win = m_windowTwo ;
c67daf87 798 m_windowTwo = (wxWindow *) NULL;
c801d85f
KB
799 }
800 else if ( toRemove == m_windowOne )
801 {
3ad5e06b 802 win = m_windowOne ;
c801d85f 803 m_windowOne = m_windowTwo;
c67daf87 804 m_windowTwo = (wxWindow *) NULL;
c801d85f
KB
805 }
806 else
dbc208e9 807 {
223d09f6 808 wxFAIL_MSG(wxT("splitter: attempt to remove a non-existent window"));
dbc208e9 809
4395fb21 810 return false;
dbc208e9 811 }
c801d85f 812
0e4cfcdd 813 OnUnsplit(win);
ca39e409 814 DoSetSashPosition(0);
3ad5e06b
VZ
815 SizeWindows();
816
4395fb21 817 return true;
3ad5e06b
VZ
818}
819
820// Replace a window with another one
821bool wxSplitterWindow::ReplaceWindow(wxWindow *winOld, wxWindow *winNew)
822{
4395fb21
RD
823 wxCHECK_MSG( winOld, false, wxT("use one of Split() functions instead") );
824 wxCHECK_MSG( winNew, false, wxT("use Unsplit() functions instead") );
3ad5e06b
VZ
825
826 if ( winOld == m_windowTwo )
827 {
828 m_windowTwo = winNew;
829 }
830 else if ( winOld == m_windowOne )
831 {
832 m_windowOne = winNew;
833 }
834 else
835 {
223d09f6 836 wxFAIL_MSG(wxT("splitter: attempt to replace a non-existent window"));
3ad5e06b 837
4395fb21 838 return false;
3ad5e06b
VZ
839 }
840
841 SizeWindows();
842
4395fb21 843 return true;
c801d85f
KB
844}
845
ca39e409
VS
846void wxSplitterWindow::SetMinimumPaneSize(int min)
847{
848 m_minimumPaneSize = min;
4395fb21
RD
849 int pos = m_requestedSashPosition != INT_MAX ? m_requestedSashPosition : m_sashPosition;
850 SetSashPosition(pos); // re-check limits
ca39e409
VS
851}
852
debe6624 853void wxSplitterWindow::SetSashPosition(int position, bool redraw)
c801d85f 854{
4395fb21
RD
855 // remember the sash position we want to set for later if we can't set it
856 // right now (e.g. because the window is too small)
857 m_requestedSashPosition = position;
858 m_checkRequestedSashPosition = false;
ca65c044 859
4395fb21 860 DoSetSashPosition(ConvertSashPosition(position));
c801d85f
KB
861
862 if ( redraw )
863 {
864 SizeWindows();
865 }
866}
867
7e521b01
JS
868// Make sure the child window sizes are updated. This is useful
869// for reducing flicker by updating the sizes before a
870// window is shown, if you know the overall size is correct.
871void wxSplitterWindow::UpdateSize()
872{
873 m_checkRequestedSashPosition = true;
874 SizeWindows();
875 m_checkRequestedSashPosition = false;
876}
877
3e58dcb9 878bool wxSplitterWindow::DoSendEvent(wxSplitterEvent& event)
42e69d6b 879{
3e58dcb9 880 return !GetEventHandler()->ProcessEvent(event) || event.IsAllowed();
42e69d6b
VZ
881}
882
976b3cb3
VZ
883wxSize wxSplitterWindow::DoGetBestSize() const
884{
885 // get best sizes of subwindows
886 wxSize size1, size2;
887 if ( m_windowOne )
1c968698 888 size1 = m_windowOne->GetAdjustedBestSize();
976b3cb3 889 if ( m_windowTwo )
1c968698 890 size2 = m_windowTwo->GetAdjustedBestSize();
976b3cb3
VZ
891
892 // sum them
893 //
894 // pSash points to the size component to which sash size must be added
895 int *pSash;
896 wxSize sizeBest;
897 if ( m_splitMode == wxSPLIT_VERTICAL )
898 {
899 sizeBest.y = wxMax(size1.y, size2.y);
900 sizeBest.x = wxMax(size1.x, m_minimumPaneSize) +
901 wxMax(size2.x, m_minimumPaneSize);
902
903 pSash = &sizeBest.x;
904 }
905 else // wxSPLIT_HORIZONTAL
906 {
907 sizeBest.x = wxMax(size1.x, size2.x);
908 sizeBest.y = wxMax(size1.y, m_minimumPaneSize) +
909 wxMax(size2.y, m_minimumPaneSize);
910
911 pSash = &sizeBest.y;
912 }
913
914 // account for the border and the sash
915 int border = 2*GetBorderSize();
916 *pSash += GetSashSize();
917 sizeBest.x += border;
918 sizeBest.y += border;
919
920 return sizeBest;
921}
922
42e69d6b 923// ---------------------------------------------------------------------------
3e58dcb9 924// wxSplitterWindow virtual functions: they now just generate the events
42e69d6b
VZ
925// ---------------------------------------------------------------------------
926
3e58dcb9
VZ
927bool wxSplitterWindow::OnSashPositionChange(int WXUNUSED(newSashPosition))
928{
929 // always allow by default
4395fb21 930 return true;
3e58dcb9
VZ
931}
932
933int wxSplitterWindow::OnSashPositionChanging(int newSashPosition)
42e69d6b
VZ
934{
935 // If within UNSPLIT_THRESHOLD from edge, set to edge to cause closure.
936 const int UNSPLIT_THRESHOLD = 4;
937
3e58dcb9
VZ
938 // first of all, check if OnSashPositionChange() doesn't forbid this change
939 if ( !OnSashPositionChange(newSashPosition) )
940 {
941 // it does
942 return -1;
943 }
42e69d6b
VZ
944
945 // Obtain relevant window dimension for bottom / right threshold check
2e8cc3e8 946 int window_size = GetWindowSize();
42e69d6b 947
4395fb21 948 bool unsplit_scenario = false;
3e58dcb9 949 if ( m_permitUnsplitAlways || m_minimumPaneSize == 0 )
bc79aa6b 950 {
370938d9
UB
951 // Do edge detection if unsplit premitted
952 if ( newSashPosition <= UNSPLIT_THRESHOLD )
953 {
954 // threshold top / left check
955 newSashPosition = 0;
4395fb21 956 unsplit_scenario = true;
370938d9
UB
957 }
958 if ( newSashPosition >= window_size - UNSPLIT_THRESHOLD )
959 {
960 // threshold bottom/right check
961 newSashPosition = window_size;
4395fb21 962 unsplit_scenario = true;
370938d9 963 }
bc79aa6b
UB
964 }
965
370938d9 966 if ( !unsplit_scenario )
bc79aa6b
UB
967 {
968 // If resultant pane would be too small, enlarge it
2e8cc3e8 969 newSashPosition = AdjustSashPosition(newSashPosition);
bc79aa6b 970 }
42e69d6b
VZ
971
972 // If the result is out of bounds it means minimum size is too big,
973 // so split window in half as best compromise.
974 if ( newSashPosition < 0 || newSashPosition > window_size )
975 newSashPosition = window_size / 2;
976
3e58dcb9
VZ
977 // now let the event handler have it
978 //
979 // FIXME: shouldn't we do it before the adjustments above so as to ensure
980 // that the sash position is always reasonable?
981 wxSplitterEvent event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING, this);
982 event.m_data.pos = newSashPosition;
983
984 if ( !DoSendEvent(event) )
42e69d6b 985 {
3e58dcb9 986 // the event handler vetoed the change
42e69d6b
VZ
987 newSashPosition = -1;
988 }
3e58dcb9
VZ
989 else
990 {
991 // it could have been changed by it
992 newSashPosition = event.GetSashPosition();
993 }
42e69d6b 994
3e58dcb9 995 return newSashPosition;
42e69d6b
VZ
996}
997
998// Called when the sash is double-clicked. The default behaviour is to remove
999// the sash if the minimum pane size is zero.
3e58dcb9 1000void wxSplitterWindow::OnDoubleClickSash(int x, int y)
42e69d6b 1001{
a2f9a636
JS
1002 wxCHECK_RET(m_windowTwo, wxT("splitter: no window to remove"));
1003
3e58dcb9
VZ
1004 // new code should handle events instead of using the virtual functions
1005 wxSplitterEvent event(wxEVT_COMMAND_SPLITTER_DOUBLECLICKED, this);
1006 event.m_data.pt.x = x;
1007 event.m_data.pt.y = y;
1008 if ( DoSendEvent(event) )
42e69d6b 1009 {
3e58dcb9
VZ
1010 if ( GetMinimumPaneSize() == 0 || m_permitUnsplitAlways )
1011 {
a2f9a636 1012 wxWindow* win = m_windowTwo;
0e4cfcdd
JS
1013 if ( Unsplit(win) )
1014 {
1015 wxSplitterEvent unsplitEvent(wxEVT_COMMAND_SPLITTER_UNSPLIT, this);
1016 unsplitEvent.m_data.win = win;
1017 (void)DoSendEvent(unsplitEvent);
1018 }
3e58dcb9 1019 }
42e69d6b 1020 }
3e58dcb9 1021 //else: blocked by user
42e69d6b
VZ
1022}
1023
3e58dcb9 1024void wxSplitterWindow::OnUnsplit(wxWindow *winRemoved)
42e69d6b 1025{
0e4cfcdd 1026 // call this before calling the event handler which may delete the window
4395fb21 1027 winRemoved->Show(false);
42e69d6b 1028}
43b5058d 1029
64407854 1030#if defined( __WXMSW__ ) || defined( __WXMAC__)
43b5058d 1031
3e58dcb9
VZ
1032// this is currently called (and needed) under MSW only...
1033void wxSplitterWindow::OnSetCursor(wxSetCursorEvent& event)
1034{
43b5058d
VZ
1035 // if we don't do it, the resizing cursor might be set for child window:
1036 // and like this we explicitly say that our cursor should not be used for
1037 // children windows which overlap us
1038
4395fb21 1039 if ( SashHitTest(event.GetX(), event.GetY(), 0) )
43b5058d
VZ
1040 {
1041 // default processing is ok
1042 event.Skip();
1043 }
1044 //else: do nothing, in particular, don't call Skip()
43b5058d 1045}
3e58dcb9 1046
b3208e11
VZ
1047#endif // wxMSW || wxMac
1048
d7260478 1049#endif // wxUSE_SPLITTER
3e58dcb9 1050