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