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