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