]> git.saurik.com Git - wxWidgets.git/blame - src/generic/splitter.cpp
fixed typo (no code changes)
[wxWidgets.git] / src / generic / splitter.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
63ec9dbb 2// Name: src/generic/splitter.cpp
c801d85f
KB
3// Purpose: wxSplitterWindow implementation
4// Author: Julian Smart
5// Modified by:
6// Created: 01/02/97
7// RCS-ID: $Id$
6aa89a22 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
14f355c2 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
f4621a09 13 #pragma implementation "splitter.h"
c801d85f
KB
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
d7260478 19#if wxUSE_SPLITTER
b3208e11 20
c801d85f 21#ifdef __BORLANDC__
f4621a09 22 #pragma hdrstop
c801d85f
KB
23#endif
24
25#ifndef WX_PRECOMP
2b5f62a0
VZ
26 #include "wx/string.h"
27 #include "wx/utils.h"
28 #include "wx/log.h"
29
30 #include "wx/dcscreen.h"
31
2f073eb2
RR
32 #include "wx/window.h"
33 #include "wx/dialog.h"
34 #include "wx/frame.h"
c801d85f 35
2b5f62a0
VZ
36 #include "wx/settings.h"
37#endif
c801d85f 38
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/*
ca65c044
WS
53 TODO PROPERTIES
54 style wxSP_3D
55 sashpos (long , 0 )
56 minsize (long -1 )
57 object, object_ref
58 orientation
066f1b7a
SC
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 }
ca65c044 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;
ca65c044 211
84e7741a
RR
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;
ca65c044 236
84e7741a 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;
ca65c044 426
4395fb21 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{
e1366cdb
VS
585 // we must reset the request here, otherwise the sash would be stuck at
586 // old position if the user attempted to move the sash after invalid
587 // (e.g. smaller than minsize) sash position was requested using
588 // SetSashPosition():
589 m_requestedSashPosition = INT_MAX;
590
63ec9dbb
VZ
591 if ( DoSetSashPosition(sashPos) )
592 {
74c57d1f
VZ
593 wxSplitterEvent event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED, this);
594 event.m_data.pos = m_sashPosition;
3e58dcb9 595
74c57d1f
VZ
596 (void)DoSendEvent(event);
597 }
ca39e409
VS
598}
599
c801d85f
KB
600// Position and size subwindows.
601// Note that the border size applies to each subwindow, not
602// including the edges next to the sash.
0d559d69 603void wxSplitterWindow::SizeWindows()
c801d85f 604{
74c57d1f 605 // check if we have delayed setting the real sash position
4395fb21 606 if ( m_checkRequestedSashPosition && m_requestedSashPosition != INT_MAX )
74c57d1f
VZ
607 {
608 int newSashPosition = ConvertSashPosition(m_requestedSashPosition);
609 if ( newSashPosition != m_sashPosition )
610 {
611 DoSetSashPosition(newSashPosition);
612 }
613
2b5f62a0
VZ
614 if ( newSashPosition <= m_sashPosition
615 && newSashPosition >= m_sashPosition - GetBorderSize() )
74c57d1f
VZ
616 {
617 // don't update it any more
618 m_requestedSashPosition = INT_MAX;
619 }
620 }
ca39e409 621
c801d85f
KB
622 int w, h;
623 GetClientSize(&w, &h);
624
f6bcfd97 625 if ( GetWindow1() && !GetWindow2() )
c801d85f 626 {
63ec9dbb 627 GetWindow1()->SetSize(GetBorderSize(), GetBorderSize(),
ca39e409 628 w - 2*GetBorderSize(), h - 2*GetBorderSize());
c801d85f 629 }
f6bcfd97 630 else if ( GetWindow1() && GetWindow2() )
c801d85f 631 {
b3208e11
VZ
632 const int border = GetBorderSize(),
633 sash = GetSashSize();
634
635 int size1 = GetSashPosition() - border,
636 size2 = GetSashPosition() + sash;
637
638 int x2, y2, w1, h1, w2, h2;
639 if ( GetSplitMode() == wxSPLIT_VERTICAL )
c801d85f 640 {
b3208e11
VZ
641 w1 = size1;
642 w2 = w - 2*border - sash - w1;
643 h1 =
644 h2 = h - 2*border;
645 x2 = size2;
646 y2 = border;
c801d85f 647 }
b3208e11 648 else // horz splitter
c801d85f 649 {
b3208e11
VZ
650 w1 =
651 w2 = w - 2*border;
652 h1 = size1;
653 h2 = h - 2*border - sash - h1;
654 x2 = border;
655 y2 = size2;
c801d85f 656 }
b3208e11
VZ
657
658 GetWindow1()->SetSize(border, border, w1, h1);
659 GetWindow2()->SetSize(x2, y2, w2, h2);
c801d85f 660 }
b3208e11 661
c801d85f 662 wxClientDC dc(this);
c801d85f 663 DrawSash(dc);
00a32dc1 664
4395fb21 665 SetNeedUpdating(false);
c801d85f
KB
666}
667
668// Set pane for unsplit window
669void wxSplitterWindow::Initialize(wxWindow *window)
670{
f938940e 671 wxASSERT_MSG( (!window || (window && window->GetParent() == this)),
fe6dc50a
VZ
672 _T("windows in the splitter should have it as parent!") );
673
c801d85f 674 m_windowOne = window;
c67daf87 675 m_windowTwo = (wxWindow *) NULL;
ca39e409 676 DoSetSashPosition(0);
c801d85f
KB
677}
678
679// Associates the given window with window 2, drawing the appropriate sash
680// and changing the split mode.
4395fb21 681// Does nothing and returns false if the window is already split.
2e8cc3e8
VZ
682bool wxSplitterWindow::DoSplit(wxSplitMode mode,
683 wxWindow *window1, wxWindow *window2,
684 int sashPosition)
c801d85f
KB
685{
686 if ( IsSplit() )
4395fb21 687 return false;
c801d85f 688
4395fb21 689 wxCHECK_MSG( window1 && window2, false,
2b5f62a0
VZ
690 _T("can not split with NULL window(s)") );
691
4395fb21 692 wxCHECK_MSG( window1->GetParent() == this && window2->GetParent() == this, false,
fe6dc50a
VZ
693 _T("windows in the splitter should have it as parent!") );
694
2e8cc3e8 695 m_splitMode = mode;
c801d85f
KB
696 m_windowOne = window1;
697 m_windowTwo = window2;
c801d85f 698
74c57d1f
VZ
699 // remember the sash position we want to set for later if we can't set it
700 // right now (e.g. because the window is too small)
701 m_requestedSashPosition = sashPosition;
4395fb21 702 m_checkRequestedSashPosition = false;
ca65c044 703
74c57d1f
VZ
704 DoSetSashPosition(ConvertSashPosition(sashPosition));
705
706 SizeWindows();
707
4395fb21 708 return true;
74c57d1f
VZ
709}
710
711int wxSplitterWindow::ConvertSashPosition(int sashPosition) const
712{
0d559d69 713 if ( sashPosition > 0 )
2e8cc3e8 714 {
74c57d1f 715 return sashPosition;
2e8cc3e8 716 }
0d559d69 717 else if ( sashPosition < 0 )
2e8cc3e8
VZ
718 {
719 // It's negative so adding is subtracting
74c57d1f 720 return GetWindowSize() + sashPosition;
2e8cc3e8 721 }
74c57d1f 722 else // sashPosition == 0
2e8cc3e8 723 {
74c57d1f
VZ
724 // default, put it in the centre
725 return GetWindowSize() / 2;
2e8cc3e8 726 }
c801d85f
KB
727}
728
c801d85f
KB
729// Remove the specified (or second) window from the view
730// Doesn't actually delete the window.
731bool wxSplitterWindow::Unsplit(wxWindow *toRemove)
732{
733 if ( ! IsSplit() )
4395fb21 734 return false;
c801d85f 735
999836aa 736 wxWindow *win;
c801d85f
KB
737 if ( toRemove == NULL || toRemove == m_windowTwo)
738 {
3ad5e06b 739 win = m_windowTwo ;
c67daf87 740 m_windowTwo = (wxWindow *) NULL;
c801d85f
KB
741 }
742 else if ( toRemove == m_windowOne )
743 {
3ad5e06b 744 win = m_windowOne ;
c801d85f 745 m_windowOne = m_windowTwo;
c67daf87 746 m_windowTwo = (wxWindow *) NULL;
c801d85f
KB
747 }
748 else
dbc208e9 749 {
223d09f6 750 wxFAIL_MSG(wxT("splitter: attempt to remove a non-existent window"));
dbc208e9 751
4395fb21 752 return false;
dbc208e9 753 }
c801d85f 754
0e4cfcdd 755 OnUnsplit(win);
ca39e409 756 DoSetSashPosition(0);
3ad5e06b
VZ
757 SizeWindows();
758
4395fb21 759 return true;
3ad5e06b
VZ
760}
761
762// Replace a window with another one
763bool wxSplitterWindow::ReplaceWindow(wxWindow *winOld, wxWindow *winNew)
764{
4395fb21
RD
765 wxCHECK_MSG( winOld, false, wxT("use one of Split() functions instead") );
766 wxCHECK_MSG( winNew, false, wxT("use Unsplit() functions instead") );
3ad5e06b
VZ
767
768 if ( winOld == m_windowTwo )
769 {
770 m_windowTwo = winNew;
771 }
772 else if ( winOld == m_windowOne )
773 {
774 m_windowOne = winNew;
775 }
776 else
777 {
223d09f6 778 wxFAIL_MSG(wxT("splitter: attempt to replace a non-existent window"));
3ad5e06b 779
4395fb21 780 return false;
3ad5e06b
VZ
781 }
782
783 SizeWindows();
784
4395fb21 785 return true;
c801d85f
KB
786}
787
ca39e409
VS
788void wxSplitterWindow::SetMinimumPaneSize(int min)
789{
790 m_minimumPaneSize = min;
4395fb21
RD
791 int pos = m_requestedSashPosition != INT_MAX ? m_requestedSashPosition : m_sashPosition;
792 SetSashPosition(pos); // re-check limits
ca39e409
VS
793}
794
debe6624 795void wxSplitterWindow::SetSashPosition(int position, bool redraw)
c801d85f 796{
4395fb21
RD
797 // remember the sash position we want to set for later if we can't set it
798 // right now (e.g. because the window is too small)
799 m_requestedSashPosition = position;
800 m_checkRequestedSashPosition = false;
ca65c044 801
4395fb21 802 DoSetSashPosition(ConvertSashPosition(position));
c801d85f
KB
803
804 if ( redraw )
805 {
806 SizeWindows();
807 }
808}
809
7e521b01
JS
810// Make sure the child window sizes are updated. This is useful
811// for reducing flicker by updating the sizes before a
812// window is shown, if you know the overall size is correct.
813void wxSplitterWindow::UpdateSize()
814{
815 m_checkRequestedSashPosition = true;
816 SizeWindows();
817 m_checkRequestedSashPosition = false;
818}
819
3e58dcb9 820bool wxSplitterWindow::DoSendEvent(wxSplitterEvent& event)
42e69d6b 821{
3e58dcb9 822 return !GetEventHandler()->ProcessEvent(event) || event.IsAllowed();
42e69d6b
VZ
823}
824
825// ---------------------------------------------------------------------------
3e58dcb9 826// wxSplitterWindow virtual functions: they now just generate the events
42e69d6b
VZ
827// ---------------------------------------------------------------------------
828
3e58dcb9
VZ
829bool wxSplitterWindow::OnSashPositionChange(int WXUNUSED(newSashPosition))
830{
831 // always allow by default
4395fb21 832 return true;
3e58dcb9
VZ
833}
834
835int wxSplitterWindow::OnSashPositionChanging(int newSashPosition)
42e69d6b
VZ
836{
837 // If within UNSPLIT_THRESHOLD from edge, set to edge to cause closure.
838 const int UNSPLIT_THRESHOLD = 4;
839
3e58dcb9
VZ
840 // first of all, check if OnSashPositionChange() doesn't forbid this change
841 if ( !OnSashPositionChange(newSashPosition) )
842 {
843 // it does
844 return -1;
845 }
42e69d6b
VZ
846
847 // Obtain relevant window dimension for bottom / right threshold check
2e8cc3e8 848 int window_size = GetWindowSize();
42e69d6b 849
4395fb21 850 bool unsplit_scenario = false;
3e58dcb9 851 if ( m_permitUnsplitAlways || m_minimumPaneSize == 0 )
bc79aa6b 852 {
370938d9
UB
853 // Do edge detection if unsplit premitted
854 if ( newSashPosition <= UNSPLIT_THRESHOLD )
855 {
856 // threshold top / left check
857 newSashPosition = 0;
4395fb21 858 unsplit_scenario = true;
370938d9
UB
859 }
860 if ( newSashPosition >= window_size - UNSPLIT_THRESHOLD )
861 {
862 // threshold bottom/right check
863 newSashPosition = window_size;
4395fb21 864 unsplit_scenario = true;
370938d9 865 }
bc79aa6b
UB
866 }
867
370938d9 868 if ( !unsplit_scenario )
bc79aa6b
UB
869 {
870 // If resultant pane would be too small, enlarge it
2e8cc3e8 871 newSashPosition = AdjustSashPosition(newSashPosition);
bc79aa6b 872 }
42e69d6b
VZ
873
874 // If the result is out of bounds it means minimum size is too big,
875 // so split window in half as best compromise.
876 if ( newSashPosition < 0 || newSashPosition > window_size )
877 newSashPosition = window_size / 2;
878
3e58dcb9
VZ
879 // now let the event handler have it
880 //
881 // FIXME: shouldn't we do it before the adjustments above so as to ensure
882 // that the sash position is always reasonable?
883 wxSplitterEvent event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING, this);
884 event.m_data.pos = newSashPosition;
885
886 if ( !DoSendEvent(event) )
42e69d6b 887 {
3e58dcb9 888 // the event handler vetoed the change
42e69d6b
VZ
889 newSashPosition = -1;
890 }
3e58dcb9
VZ
891 else
892 {
893 // it could have been changed by it
894 newSashPosition = event.GetSashPosition();
895 }
42e69d6b 896
3e58dcb9 897 return newSashPosition;
42e69d6b
VZ
898}
899
900// Called when the sash is double-clicked. The default behaviour is to remove
901// the sash if the minimum pane size is zero.
3e58dcb9 902void wxSplitterWindow::OnDoubleClickSash(int x, int y)
42e69d6b 903{
a2f9a636
JS
904 wxCHECK_RET(m_windowTwo, wxT("splitter: no window to remove"));
905
3e58dcb9
VZ
906 // new code should handle events instead of using the virtual functions
907 wxSplitterEvent event(wxEVT_COMMAND_SPLITTER_DOUBLECLICKED, this);
908 event.m_data.pt.x = x;
909 event.m_data.pt.y = y;
910 if ( DoSendEvent(event) )
42e69d6b 911 {
3e58dcb9
VZ
912 if ( GetMinimumPaneSize() == 0 || m_permitUnsplitAlways )
913 {
a2f9a636 914 wxWindow* win = m_windowTwo;
0e4cfcdd
JS
915 if ( Unsplit(win) )
916 {
917 wxSplitterEvent unsplitEvent(wxEVT_COMMAND_SPLITTER_UNSPLIT, this);
918 unsplitEvent.m_data.win = win;
919 (void)DoSendEvent(unsplitEvent);
920 }
3e58dcb9 921 }
42e69d6b 922 }
3e58dcb9 923 //else: blocked by user
42e69d6b
VZ
924}
925
3e58dcb9 926void wxSplitterWindow::OnUnsplit(wxWindow *winRemoved)
42e69d6b 927{
0e4cfcdd 928 // call this before calling the event handler which may delete the window
4395fb21 929 winRemoved->Show(false);
42e69d6b 930}
43b5058d 931
64407854 932#if defined( __WXMSW__ ) || defined( __WXMAC__)
43b5058d 933
3e58dcb9
VZ
934// this is currently called (and needed) under MSW only...
935void wxSplitterWindow::OnSetCursor(wxSetCursorEvent& event)
936{
43b5058d
VZ
937 // if we don't do it, the resizing cursor might be set for child window:
938 // and like this we explicitly say that our cursor should not be used for
939 // children windows which overlap us
940
4395fb21 941 if ( SashHitTest(event.GetX(), event.GetY(), 0) )
43b5058d
VZ
942 {
943 // default processing is ok
944 event.Skip();
945 }
946 //else: do nothing, in particular, don't call Skip()
43b5058d 947}
3e58dcb9 948
b3208e11
VZ
949#endif // wxMSW || wxMac
950
d7260478 951#endif // wxUSE_SPLITTER
3e58dcb9 952