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