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