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