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