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