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