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