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