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