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