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