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