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