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