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