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