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