]> git.saurik.com Git - wxWidgets.git/blame - src/generic/splitter.cpp
Fix wx[Sorted]ArrayString::Index when wxUSE_STL=1, because
[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
c801d85f 20#ifdef __BORLANDC__
f4621a09 21 #pragma hdrstop
c801d85f
KB
22#endif
23
24#ifndef WX_PRECOMP
2b5f62a0
VZ
25 #include "wx/string.h"
26 #include "wx/utils.h"
27 #include "wx/log.h"
28
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
c801d85f 38#include "wx/splitter.h"
2b5f62a0
VZ
39
40#include <stdlib.h>
c801d85f 41
2e4df4bf
VZ
42DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED)
43DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING)
44DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPLITTER_DOUBLECLICKED)
45DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPLITTER_UNSPLIT)
46
c801d85f 47IMPLEMENT_DYNAMIC_CLASS(wxSplitterWindow, wxWindow)
3e58dcb9 48IMPLEMENT_DYNAMIC_CLASS(wxSplitterEvent, wxNotifyEvent)
c801d85f
KB
49
50BEGIN_EVENT_TABLE(wxSplitterWindow, wxWindow)
51 EVT_PAINT(wxSplitterWindow::OnPaint)
52 EVT_SIZE(wxSplitterWindow::OnSize)
53 EVT_MOUSE_EVENTS(wxSplitterWindow::OnMouseEvent)
42e69d6b 54
64407854 55#if defined( __WXMSW__ ) || defined( __WXMAC__)
43b5058d 56 EVT_SET_CURSOR(wxSplitterWindow::OnSetCursor)
3e58dcb9 57#endif // wxMSW
6b55490a
VZ
58
59 WX_EVENT_TABLE_CONTROL_CONTAINER(wxSplitterWindow)
c801d85f 60END_EVENT_TABLE()
c801d85f 61
6b55490a
VZ
62WX_DELEGATE_TO_CONTROL_CONTAINER(wxSplitterWindow);
63
f6bcfd97 64bool wxSplitterWindow::Create(wxWindow *parent, wxWindowID id,
0d559d69
VZ
65 const wxPoint& pos,
66 const wxSize& size,
67 long style,
68 const wxString& name)
c801d85f 69{
6b55490a
VZ
70 // allow TABbing from one window to the other
71 style |= wxTAB_TRAVERSAL;
72
f6bcfd97
BP
73 if (!wxWindow::Create(parent, id, pos, size, style, name))
74 return FALSE;
75
370938d9 76 m_permitUnsplitAlways = (style & wxSP_PERMIT_UNSPLIT) != 0;
f6bcfd97
BP
77
78 if ( style & wxSP_3DSASH )
79 m_sashSize = 7;
80 else
81 m_sashSize = 3;
82
83 if ( style & wxSP_3DBORDER )
84 m_borderSize = 2;
85 else if ( style & wxSP_BORDER )
86 m_borderSize = 1;
87 else
88 m_borderSize = 0;
2e8cc3e8 89
ed2e7e59
RR
90#ifdef __WXMAC__
91 int major,minor;
92 wxGetOsVersion( &major, &minor );
93 if (major >= 10)
94 m_windowStyle |= wxSP_SASH_AQUA;
95#endif
f6bcfd97
BP
96
97 return TRUE;
98}
99
100void wxSplitterWindow::Init()
101{
9948d31f
VZ
102 m_container.SetContainerWindow(this);
103
f6bcfd97
BP
104 m_splitMode = wxSPLIT_VERTICAL;
105 m_permitUnsplitAlways = TRUE;
c67daf87
UR
106 m_windowOne = (wxWindow *) NULL;
107 m_windowTwo = (wxWindow *) NULL;
c801d85f
KB
108 m_dragMode = wxSPLIT_DRAG_NONE;
109 m_oldX = 0;
110 m_oldY = 0;
111 m_firstX = 0;
112 m_firstY = 0;
2b5f62a0
VZ
113 m_sashSize = 3;
114 m_borderSize = 0;
ca39e409 115 m_sashPosition = m_requestedSashPosition = 0;
c801d85f 116 m_minimumPaneSize = 0;
153b7996
VZ
117 m_sashCursorWE = wxCursor(wxCURSOR_SIZEWE);
118 m_sashCursorNS = wxCursor(wxCURSOR_SIZENS);
c801d85f 119 m_sashTrackerPen = new wxPen(*wxBLACK, 2, wxSOLID);
c67daf87
UR
120 m_lightShadowPen = (wxPen *) NULL;
121 m_mediumShadowPen = (wxPen *) NULL;
122 m_darkShadowPen = (wxPen *) NULL;
123 m_faceBrush = (wxBrush *) NULL;
124 m_facePen = (wxPen *) NULL;
125 m_hilightPen = (wxPen *) NULL;
c801d85f 126
c801d85f
KB
127 InitColours();
128
72195a0f 129 m_needUpdating = FALSE;
c801d85f
KB
130}
131
0d559d69 132wxSplitterWindow::~wxSplitterWindow()
c801d85f 133{
c801d85f
KB
134 delete m_sashTrackerPen;
135 delete m_lightShadowPen;
136 delete m_darkShadowPen;
137 delete m_mediumShadowPen;
138 delete m_hilightPen;
139 delete m_facePen;
140 delete m_faceBrush;
141}
142
153b7996
VZ
143void wxSplitterWindow::SetResizeCursor()
144{
145 SetCursor(m_splitMode == wxSPLIT_VERTICAL ? m_sashCursorWE
146 : m_sashCursorNS);
147}
148
c801d85f
KB
149void wxSplitterWindow::OnPaint(wxPaintEvent& WXUNUSED(event))
150{
151 wxPaintDC dc(this);
152
153 if ( m_borderSize > 0 )
154 DrawBorders(dc);
155 DrawSash(dc);
156}
157
5180055b 158void wxSplitterWindow::OnInternalIdle()
72195a0f 159{
5180055b
JS
160 wxWindow::OnInternalIdle();
161
72195a0f
RR
162 if (m_needUpdating)
163 SizeWindows();
164}
4c013092 165
c801d85f
KB
166void wxSplitterWindow::OnMouseEvent(wxMouseEvent& event)
167{
2e8cc3e8
VZ
168 int x = (int)event.GetX(),
169 y = (int)event.GetY();
c801d85f 170
84e7741a
RR
171#if defined(__WXMSW__)
172 // SetCursor(wxCursor()); // Is this required?
b69f1bd1 173#endif
f4621a09 174
f6bcfd97
BP
175 if (GetWindowStyle() & wxSP_NOSASH)
176 return;
58d1c1ae 177
153b7996
VZ
178 // with wxSP_LIVE_UPDATE style the splitter windows are always resized
179 // following the mouse movement while it drags the sash, without it we only
180 // draw the sash at the new position but only resize the windows when the
181 // dragging is finished
182 bool isLive = (GetWindowStyleFlag() & wxSP_LIVE_UPDATE) != 0;
183
0d559d69
VZ
184 if (event.LeftDown())
185 {
c801d85f
KB
186 if ( SashHitTest(x, y) )
187 {
84e7741a 188 // Start the drag now
4a33eba6 189 m_dragMode = wxSPLIT_DRAG_DRAGGING;
84e7741a
RR
190
191 // Capture mouse and set the cursor
192 CaptureMouse();
193 SetResizeCursor();
f4621a09 194
153b7996 195 if ( !isLive )
4419ba31 196 {
153b7996
VZ
197 // remember the initial sash position and draw the initial
198 // shadow sash
199 m_sashPositionCurrent = m_sashPosition;
200
4419ba31
VZ
201 DrawSashTracker(x, y);
202 }
a6aa9b1e 203
4a33eba6
RR
204 m_oldX = x;
205 m_oldY = y;
9f334bea 206
153b7996 207 SetResizeCursor();
f4621a09 208 return;
c801d85f 209 }
0d559d69 210 }
0d559d69
VZ
211 else if (event.LeftUp() && m_dragMode == wxSPLIT_DRAG_DRAGGING)
212 {
c801d85f
KB
213 // We can stop dragging now and see what we've got.
214 m_dragMode = wxSPLIT_DRAG_NONE;
84e7741a
RR
215
216 // Release mouse and unset the cursor
0d559d69 217 ReleaseMouse();
84e7741a 218 SetCursor(* wxSTANDARD_CURSOR);
dbc208e9 219
2b5f62a0
VZ
220 // exit if unsplit after doubleclick
221 if ( !IsSplit() )
222 {
223 return;
224 }
225
c801d85f 226 // Erase old tracker
153b7996 227 if ( !isLive )
4419ba31 228 {
72195a0f 229 DrawSashTracker(m_oldX, m_oldY);
4419ba31 230 }
c801d85f 231
3e58dcb9
VZ
232 // the position of the click doesn't exactly correspond to
233 // m_sashPosition, rather it changes it by the distance by which the
234 // mouse has moved
235 int diff = m_splitMode == wxSPLIT_VERTICAL ? x - m_oldX : y - m_oldY;
4c013092 236
153b7996
VZ
237 int posSashOld = isLive ? m_sashPosition : m_sashPositionCurrent;
238 int posSashNew = OnSashPositionChanging(posSashOld + diff);
3e58dcb9 239 if ( posSashNew == -1 )
42e69d6b 240 {
3e58dcb9
VZ
241 // change not allowed
242 return;
42e69d6b 243 }
4c013092 244
43b5058d 245 if ( m_permitUnsplitAlways || m_minimumPaneSize == 0 )
4c013092 246 {
370938d9 247 // Deal with possible unsplit scenarios
3e58dcb9 248 if ( posSashNew == 0 )
370938d9
UB
249 {
250 // We remove the first window from the view
251 wxWindow *removedWindow = m_windowOne;
252 m_windowOne = m_windowTwo;
253 m_windowTwo = (wxWindow *) NULL;
3e58dcb9 254 OnUnsplit(removedWindow);
0e4cfcdd
JS
255 wxSplitterEvent event(wxEVT_COMMAND_SPLITTER_UNSPLIT, this);
256 event.m_data.win = removedWindow;
257 (void)DoSendEvent(event);
63ec9dbb 258 SetSashPositionAndNotify(0);
370938d9 259 }
3e58dcb9 260 else if ( posSashNew == GetWindowSize() )
370938d9
UB
261 {
262 // We remove the second window from the view
263 wxWindow *removedWindow = 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
UB
270 }
271 else
272 {
63ec9dbb 273 SetSashPositionAndNotify(posSashNew);
370938d9 274 }
c801d85f 275 }
4c013092 276 else
c801d85f 277 {
63ec9dbb 278 SetSashPositionAndNotify(posSashNew);
4c013092 279 }
42e69d6b 280
c801d85f 281 SizeWindows();
4a33eba6 282 } // left up && dragging
21b07f95 283 else if ((event.Moving() || event.Leaving() || event.Entering()) && (m_dragMode == wxSPLIT_DRAG_NONE))
0d559d69 284 {
84e7741a 285 // Just change the cursor as required
21b07f95
RR
286 if ( !event.Leaving() && SashHitTest(x, y) )
287 {
153b7996 288 SetResizeCursor();
21b07f95 289 }
58d1c1ae 290 else
21b07f95 291 {
58d1c1ae 292 SetCursor(* wxSTANDARD_CURSOR);
21b07f95 293 }
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 374
642d2dc8
JS
375 // wxMotif doesn't yet have a wxTopLevelWindow implementation
376#ifdef __WXMOTIF__
377 wxFrame *winTop = wxDynamicCast(parent, wxFrame);
378#else
2e8cc3e8 379 wxTopLevelWindow *winTop = wxDynamicCast(parent, wxTopLevelWindow);
642d2dc8 380#endif
2e8cc3e8
VZ
381 if ( winTop )
382 {
383 iconized = winTop->IsIconized();
384 }
642d2dc8 385#ifndef __WXMOTIF__
a8731351
VZ
386 else
387 {
2e8cc3e8
VZ
388 wxFAIL_MSG(wxT("should have a top level parent!"));
389
390 iconized = FALSE;
a8731351 391 }
642d2dc8 392#endif
63ec9dbb 393
a8731351
VZ
394 if ( iconized )
395 {
396 event.Skip();
2e8cc3e8
VZ
397
398 return;
a8731351 399 }
2e8cc3e8
VZ
400
401 int cw, ch;
402 GetClientSize( &cw, &ch );
403 if ( m_windowTwo )
a8731351 404 {
2e8cc3e8 405 if ( m_splitMode == wxSPLIT_VERTICAL )
c801d85f 406 {
2e8cc3e8 407 if ( m_sashPosition >= (cw - 5) )
63ec9dbb 408 SetSashPositionAndNotify(wxMax(10, cw - 40));
2e8cc3e8
VZ
409 }
410 else // m_splitMode == wxSPLIT_HORIZONTAL
411 {
412 if ( m_sashPosition >= (ch - 5) )
63ec9dbb 413 SetSashPositionAndNotify(wxMax(10, ch - 40));
c801d85f
KB
414 }
415 }
2e8cc3e8
VZ
416
417 SizeWindows();
c801d85f
KB
418}
419
debe6624 420bool wxSplitterWindow::SashHitTest(int x, int y, int tolerance)
c801d85f
KB
421{
422 if ( m_windowTwo == NULL || m_sashPosition == 0)
423 return FALSE; // No sash
424
425 if ( m_splitMode == wxSPLIT_VERTICAL )
426 {
427 if ( (x >= m_sashPosition - tolerance) && (x <= m_sashPosition + m_sashSize + tolerance) )
428 return TRUE;
429 else
430 return FALSE;
431 }
432 else
433 {
434 if ( (y >= (m_sashPosition- tolerance)) && (y <= (m_sashPosition + m_sashSize + tolerance)) )
435 return TRUE;
436 else
437 return FALSE;
438 }
c801d85f
KB
439}
440
441// Draw 3D effect borders
442void wxSplitterWindow::DrawBorders(wxDC& dc)
443{
444 int w, h;
445 GetClientSize(&w, &h);
446
f6bcfd97 447 if ( GetWindowStyleFlag() & wxSP_3DBORDER )
c801d85f 448 {
a6aa9b1e 449
1e2c86ca
PA
450 dc.SetPen(*m_facePen);
451 dc.SetBrush(*m_faceBrush);
452 dc.DrawRectangle(1, 1 , w-1, m_borderSize-2 ); //high
453 dc.DrawRectangle(1, m_borderSize-2 , m_borderSize-2, h-1 ); // left
454 dc.DrawRectangle(w-m_borderSize+2, m_borderSize-2 , w-1, h-1 ); // right
455 dc.DrawRectangle(m_borderSize-2, h-m_borderSize+2 , w-m_borderSize+2, h-1 ); //bottom
456
c801d85f 457 dc.SetPen(*m_mediumShadowPen);
1e2c86ca
PA
458 dc.DrawLine(m_borderSize-2, m_borderSize-2, w-m_borderSize+1, m_borderSize-2);
459 dc.DrawLine(m_borderSize-2, m_borderSize-2, m_borderSize-2, h-m_borderSize+1);
c801d85f
KB
460
461 dc.SetPen(*m_darkShadowPen);
1e2c86ca
PA
462 dc.DrawLine(m_borderSize-1, m_borderSize-1, w-m_borderSize, m_borderSize-1);
463 dc.DrawLine(m_borderSize-1, m_borderSize-1, m_borderSize-1, h-m_borderSize);
c801d85f
KB
464
465 dc.SetPen(*m_hilightPen);
1e2c86ca
PA
466 dc.DrawLine(m_borderSize - 2, h-m_borderSize+1, w-m_borderSize+1, h-m_borderSize+1);
467 dc.DrawLine(w-m_borderSize+1, m_borderSize - 2, w-m_borderSize+1, h-m_borderSize+2); // Surely the maximum y pos. should be h - 1.
c801d85f
KB
468 /// Anyway, h is required for MSW.
469
470 dc.SetPen(*m_lightShadowPen);
1e2c86ca
PA
471 dc.DrawLine(w-m_borderSize, m_borderSize-1, w-m_borderSize, h-m_borderSize); // Right hand side
472 dc.DrawLine(m_borderSize-1, h-m_borderSize, w-m_borderSize+1, h-m_borderSize); // Bottom
c801d85f
KB
473 }
474 else if ( GetWindowStyleFlag() & wxSP_BORDER )
475 {
476 dc.SetBrush(*wxTRANSPARENT_BRUSH);
477 dc.SetPen(*wxBLACK_PEN);
478 dc.DrawRectangle(0, 0, w-1, h-1);
479 }
480
481 dc.SetPen(wxNullPen);
482 dc.SetBrush(wxNullBrush);
483}
484
485// Draw the sash
486void wxSplitterWindow::DrawSash(wxDC& dc)
487{
488 if ( m_sashPosition == 0 || !m_windowTwo)
489 return;
f6bcfd97
BP
490 if (GetWindowStyle() & wxSP_NOSASH)
491 return;
c801d85f
KB
492
493 int w, h;
494 GetClientSize(&w, &h);
495
f6bcfd97 496 if ( GetWindowStyleFlag() & wxSP_3DSASH )
c801d85f
KB
497 {
498 if ( m_splitMode == wxSPLIT_VERTICAL )
499 {
500 dc.SetPen(*m_facePen);
d30f0930
RR
501
502 if (HasFlag( wxSP_SASH_AQUA ))
503 dc.SetBrush(*wxWHITE_BRUSH);
504 else
505 dc.SetBrush(*m_faceBrush);
1e2c86ca 506 dc.DrawRectangle(m_sashPosition + 2, 0 , m_sashSize - 4, h );
c801d85f
KB
507
508 dc.SetBrush(*wxTRANSPARENT_BRUSH);
509
510 dc.SetPen(*m_lightShadowPen);
f6bcfd97 511 int xShadow = m_borderSize ? m_borderSize - 1 : 0 ;
1e2c86ca 512 dc.DrawLine(m_sashPosition, xShadow , m_sashPosition, h-m_borderSize);
c801d85f
KB
513
514 dc.SetPen(*m_hilightPen);
1e2c86ca 515 dc.DrawLine(m_sashPosition+1, m_borderSize - 2, m_sashPosition+1, h - m_borderSize+2);
c801d85f 516
d30f0930
RR
517 if (!HasFlag( wxSP_SASH_AQUA ))
518 dc.SetPen(*m_mediumShadowPen);
519
520 int yMedium = m_borderSize ? h-m_borderSize+1 : h ;
1e2c86ca 521 dc.DrawLine(m_sashPosition+m_sashSize-2, xShadow, m_sashPosition+m_sashSize-2, yMedium);
c801d85f 522
d30f0930
RR
523 if (HasFlag( wxSP_SASH_AQUA ))
524 dc.SetPen(*m_lightShadowPen);
525 else
526 dc.SetPen(*m_darkShadowPen);
1e2c86ca 527 dc.DrawLine(m_sashPosition+m_sashSize-1, m_borderSize, m_sashPosition+m_sashSize-1, h-m_borderSize );
2e8cc3e8 528
f6bcfd97
BP
529 // Draw the top and bottom edges of the sash, if requested
530 if (GetWindowStyle() & wxSP_FULLSASH)
531 {
532 // Top
533 dc.SetPen(*m_hilightPen);
534 dc.DrawLine(m_sashPosition+1, m_borderSize, m_sashPosition+m_sashSize-1, m_borderSize);
535
536 // Bottom
537 dc.SetPen(*m_darkShadowPen);
538 dc.DrawLine(m_sashPosition+1, h-m_borderSize-1, m_sashPosition+m_sashSize-1, h-m_borderSize-1);
539 }
0d559d69 540 }
2b5f62a0 541 else // wxSPLIT_HORIZONTAL
c801d85f
KB
542 {
543 dc.SetPen(*m_facePen);
d30f0930
RR
544 if (HasFlag( wxSP_SASH_AQUA ))
545 dc.SetBrush(*wxWHITE_BRUSH);
546 else
547 dc.SetBrush(*m_faceBrush);
1e2c86ca 548 dc.DrawRectangle( m_borderSize-2, m_sashPosition + 2, w-m_borderSize+2, m_sashSize - 4);
c801d85f
KB
549
550 dc.SetBrush(*wxTRANSPARENT_BRUSH);
551
552 dc.SetPen(*m_lightShadowPen);
1e2c86ca 553 dc.DrawLine(m_borderSize-1, m_sashPosition, w-m_borderSize, m_sashPosition);
c801d85f
KB
554
555 dc.SetPen(*m_hilightPen);
1e2c86ca 556 dc.DrawLine(m_borderSize-2, m_sashPosition+1, w-m_borderSize+1, m_sashPosition+1);
c801d85f 557
d30f0930
RR
558 if (!HasFlag( wxSP_SASH_AQUA ))
559 dc.SetPen(*m_mediumShadowPen);
1e2c86ca 560 dc.DrawLine(m_borderSize-1, m_sashPosition+m_sashSize-2, w-m_borderSize+1, m_sashPosition+m_sashSize-2);
c801d85f 561
d30f0930
RR
562 if (HasFlag( wxSP_SASH_AQUA ))
563 dc.SetPen(*m_lightShadowPen);
564 else
565 dc.SetPen(*m_darkShadowPen);
1e2c86ca 566 dc.DrawLine(m_borderSize, m_sashPosition+m_sashSize-1, w-m_borderSize, m_sashPosition+m_sashSize-1);
f6bcfd97
BP
567
568 // Draw the left and right edges of the sash, if requested
569 if (GetWindowStyle() & wxSP_FULLSASH)
570 {
571 // Left
572 dc.SetPen(*m_hilightPen);
573 dc.DrawLine(m_borderSize, m_sashPosition, m_borderSize, m_sashPosition+m_sashSize);
574
575 // Right
576 dc.SetPen(*m_darkShadowPen);
577 dc.DrawLine(w-m_borderSize-1, m_sashPosition+1, w-m_borderSize-1, m_sashPosition+m_sashSize-1);
578 }
c801d85f
KB
579 }
580 }
2b5f62a0 581 else // !wxSP_3DSASH
c801d85f 582 {
2b5f62a0
VZ
583 dc.SetPen(*wxTRANSPARENT_PEN);
584 dc.SetBrush(*m_faceBrush);
c801d85f
KB
585 if ( m_splitMode == wxSPLIT_VERTICAL )
586 {
c801d85f 587 int h1 = h-1;
f6bcfd97
BP
588 int y1 = 0;
589 if ( (GetWindowStyleFlag() & wxSP_BORDER) != wxSP_BORDER && (GetWindowStyleFlag() & wxSP_3DBORDER) != wxSP_3DBORDER )
c801d85f 590 h1 += 1; // Not sure why this is necessary...
f6bcfd97
BP
591 if ( (GetWindowStyleFlag() & wxSP_3DBORDER) == wxSP_3DBORDER)
592 {
593 y1 = 2; h1 -= 3;
594 }
595 dc.DrawRectangle(m_sashPosition, y1, m_sashSize, h1);
c801d85f 596 }
2b5f62a0 597 else // wxSPLIT_HORIZONTAL
c801d85f 598 {
c801d85f 599 int w1 = w-1;
f6bcfd97
BP
600 int x1 = 0;
601 if ( (GetWindowStyleFlag() & wxSP_BORDER) != wxSP_BORDER && (GetWindowStyleFlag() & wxSP_3DBORDER) != wxSP_3DBORDER )
c801d85f 602 w1 ++;
f6bcfd97
BP
603 if ( (GetWindowStyleFlag() & wxSP_3DBORDER) == wxSP_3DBORDER)
604 {
605 x1 = 2; w1 -= 3;
606 }
607 dc.DrawRectangle(x1, m_sashPosition, w1, m_sashSize);
c801d85f 608 }
c801d85f
KB
609 }
610
611 dc.SetPen(wxNullPen);
612 dc.SetBrush(wxNullBrush);
613}
614
615// Draw the sash tracker (for whilst moving the sash)
debe6624 616void wxSplitterWindow::DrawSashTracker(int x, int y)
c801d85f
KB
617{
618 int w, h;
619 GetClientSize(&w, &h);
620
621 wxScreenDC screenDC;
622 int x1, y1;
623 int x2, y2;
624
625 if ( m_splitMode == wxSPLIT_VERTICAL )
626 {
627 x1 = x; y1 = 2;
628 x2 = x; y2 = h-2;
629
630 if ( x1 > w )
631 {
632 x1 = w; x2 = w;
633 }
634 else if ( x1 < 0 )
635 {
636 x1 = 0; x2 = 0;
637 }
638 }
639 else
640 {
641 x1 = 2; y1 = y;
642 x2 = w-2; y2 = y;
643
644 if ( y1 > h )
645 {
646 y1 = h;
647 y2 = h;
648 }
649 else if ( y1 < 0 )
650 {
651 y1 = 0;
652 y2 = 0;
653 }
654 }
655
656 ClientToScreen(&x1, &y1);
657 ClientToScreen(&x2, &y2);
658
3c679789 659 screenDC.SetLogicalFunction(wxINVERT);
c801d85f
KB
660 screenDC.SetPen(*m_sashTrackerPen);
661 screenDC.SetBrush(*wxTRANSPARENT_BRUSH);
662
663 screenDC.DrawLine(x1, y1, x2, y2);
664
665 screenDC.SetLogicalFunction(wxCOPY);
666
667 screenDC.SetPen(wxNullPen);
668 screenDC.SetBrush(wxNullBrush);
669}
670
2e8cc3e8 671int wxSplitterWindow::GetWindowSize() const
d76ac8ed 672{
2e8cc3e8
VZ
673 wxSize size = GetClientSize();
674
675 return m_splitMode == wxSPLIT_VERTICAL ? size.x : size.y;
676}
677
678int wxSplitterWindow::AdjustSashPosition(int sashPos) const
679{
680 int window_size = GetWindowSize();
681
d76ac8ed
VS
682 wxWindow *win;
683
d76ac8ed
VS
684 win = GetWindow1();
685 if ( win )
686 {
2e8cc3e8
VZ
687 // the window shouldn't be smaller than its own minimal size nor
688 // smaller than the minimual pane size specified for this splitter
689 int minSize = m_splitMode == wxSPLIT_VERTICAL ? win->GetMinWidth()
690 : win->GetMinHeight();
691
692 if ( minSize == -1 || m_minimumPaneSize > minSize )
693 minSize = m_minimumPaneSize;
694
695 minSize += GetBorderSize();
696
697 if ( sashPos < minSize )
698 sashPos = minSize;
d76ac8ed
VS
699 }
700
701 win = GetWindow2();
702 if ( win )
703 {
2e8cc3e8
VZ
704 int minSize = m_splitMode == wxSPLIT_VERTICAL ? win->GetMinWidth()
705 : win->GetMinHeight();
706
707 if ( minSize == -1 || m_minimumPaneSize > minSize )
708 minSize = m_minimumPaneSize;
709
710 int maxSize = window_size - minSize - GetBorderSize();
711 if ( sashPos > maxSize )
712 sashPos = maxSize;
d76ac8ed 713 }
2e8cc3e8
VZ
714
715 return sashPos;
d76ac8ed
VS
716}
717
63ec9dbb 718bool wxSplitterWindow::DoSetSashPosition(int sashPos)
ca39e409 719{
74c57d1f 720 int newSashPosition = AdjustSashPosition(sashPos);
3e58dcb9 721
63ec9dbb
VZ
722 if ( newSashPosition == m_sashPosition )
723 return FALSE;
74c57d1f 724
63ec9dbb
VZ
725 m_sashPosition = newSashPosition;
726
727 return TRUE;
728}
729
730void wxSplitterWindow::SetSashPositionAndNotify(int sashPos)
731{
732 if ( DoSetSashPosition(sashPos) )
733 {
74c57d1f
VZ
734 wxSplitterEvent event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED, this);
735 event.m_data.pos = m_sashPosition;
3e58dcb9 736
74c57d1f
VZ
737 (void)DoSendEvent(event);
738 }
ca39e409
VS
739}
740
c801d85f
KB
741// Position and size subwindows.
742// Note that the border size applies to each subwindow, not
743// including the edges next to the sash.
0d559d69 744void wxSplitterWindow::SizeWindows()
c801d85f 745{
74c57d1f
VZ
746 // check if we have delayed setting the real sash position
747 if ( m_requestedSashPosition != INT_MAX )
748 {
749 int newSashPosition = ConvertSashPosition(m_requestedSashPosition);
750 if ( newSashPosition != m_sashPosition )
751 {
752 DoSetSashPosition(newSashPosition);
753 }
754
2b5f62a0
VZ
755 if ( newSashPosition <= m_sashPosition
756 && newSashPosition >= m_sashPosition - GetBorderSize() )
74c57d1f
VZ
757 {
758 // don't update it any more
759 m_requestedSashPosition = INT_MAX;
760 }
761 }
ca39e409 762
c801d85f
KB
763 int w, h;
764 GetClientSize(&w, &h);
765
f6bcfd97 766 if ( GetWindow1() && !GetWindow2() )
c801d85f 767 {
63ec9dbb 768 GetWindow1()->SetSize(GetBorderSize(), GetBorderSize(),
ca39e409 769 w - 2*GetBorderSize(), h - 2*GetBorderSize());
c801d85f 770 }
f6bcfd97 771 else if ( GetWindow1() && GetWindow2() )
c801d85f 772 {
f6bcfd97 773 if (GetSplitMode() == wxSPLIT_VERTICAL)
c801d85f 774 {
f6bcfd97
BP
775 int x1 = GetBorderSize();
776 int y1 = GetBorderSize();
777 int w1 = GetSashPosition() - GetBorderSize();
778 int h1 = h - 2*GetBorderSize();
779
780 int x2 = GetSashPosition() + GetSashSize();
781 int y2 = GetBorderSize();
782 int w2 = w - 2*GetBorderSize() - GetSashSize() - w1;
783 int h2 = h - 2*GetBorderSize();
784
785 GetWindow1()->SetSize(x1, y1, w1, h1);
786 GetWindow2()->SetSize(x2, y2, w2, h2);
c801d85f
KB
787 }
788 else
789 {
f6bcfd97
BP
790 GetWindow1()->SetSize(GetBorderSize(), GetBorderSize(),
791 w - 2*GetBorderSize(), GetSashPosition() - GetBorderSize());
792 GetWindow2()->SetSize(GetBorderSize(), GetSashPosition() + GetSashSize(),
793 w - 2*GetBorderSize(), h - 2*GetBorderSize() - GetSashSize() - (GetSashPosition() - GetBorderSize()));
c801d85f
KB
794 }
795 }
796 wxClientDC dc(this);
f6bcfd97 797 if ( GetBorderSize() > 0 )
4419ba31 798 DrawBorders(dc);
c801d85f 799 DrawSash(dc);
00a32dc1 800
f6bcfd97 801 SetNeedUpdating(FALSE);
c801d85f
KB
802}
803
804// Set pane for unsplit window
805void wxSplitterWindow::Initialize(wxWindow *window)
806{
2b5f62a0 807 wxASSERT_MSG( window && window->GetParent() == this,
fe6dc50a
VZ
808 _T("windows in the splitter should have it as parent!") );
809
c801d85f 810 m_windowOne = window;
c67daf87 811 m_windowTwo = (wxWindow *) NULL;
ca39e409 812 DoSetSashPosition(0);
c801d85f
KB
813}
814
815// Associates the given window with window 2, drawing the appropriate sash
816// and changing the split mode.
817// Does nothing and returns FALSE if the window is already split.
2e8cc3e8
VZ
818bool wxSplitterWindow::DoSplit(wxSplitMode mode,
819 wxWindow *window1, wxWindow *window2,
820 int sashPosition)
c801d85f
KB
821{
822 if ( IsSplit() )
823 return FALSE;
824
2b5f62a0
VZ
825 wxCHECK_MSG( window1 && window2, FALSE,
826 _T("can not split with NULL window(s)") );
827
828 wxCHECK_MSG( window1->GetParent() == this && window2->GetParent() == this, FALSE,
fe6dc50a
VZ
829 _T("windows in the splitter should have it as parent!") );
830
2e8cc3e8 831 m_splitMode = mode;
c801d85f
KB
832 m_windowOne = window1;
833 m_windowTwo = window2;
c801d85f 834
74c57d1f
VZ
835 // remember the sash position we want to set for later if we can't set it
836 // right now (e.g. because the window is too small)
837 m_requestedSashPosition = sashPosition;
838
839 DoSetSashPosition(ConvertSashPosition(sashPosition));
840
841 SizeWindows();
842
843 return TRUE;
844}
845
846int wxSplitterWindow::ConvertSashPosition(int sashPosition) const
847{
0d559d69 848 if ( sashPosition > 0 )
2e8cc3e8 849 {
74c57d1f 850 return sashPosition;
2e8cc3e8 851 }
0d559d69 852 else if ( sashPosition < 0 )
2e8cc3e8
VZ
853 {
854 // It's negative so adding is subtracting
74c57d1f 855 return GetWindowSize() + sashPosition;
2e8cc3e8 856 }
74c57d1f 857 else // sashPosition == 0
2e8cc3e8 858 {
74c57d1f
VZ
859 // default, put it in the centre
860 return GetWindowSize() / 2;
2e8cc3e8 861 }
c801d85f
KB
862}
863
c801d85f
KB
864// Remove the specified (or second) window from the view
865// Doesn't actually delete the window.
866bool wxSplitterWindow::Unsplit(wxWindow *toRemove)
867{
868 if ( ! IsSplit() )
869 return FALSE;
870
3ad5e06b 871 wxWindow *win = NULL;
c801d85f
KB
872 if ( toRemove == NULL || toRemove == m_windowTwo)
873 {
3ad5e06b 874 win = m_windowTwo ;
c67daf87 875 m_windowTwo = (wxWindow *) NULL;
c801d85f
KB
876 }
877 else if ( toRemove == m_windowOne )
878 {
3ad5e06b 879 win = m_windowOne ;
c801d85f 880 m_windowOne = m_windowTwo;
c67daf87 881 m_windowTwo = (wxWindow *) NULL;
c801d85f
KB
882 }
883 else
dbc208e9 884 {
223d09f6 885 wxFAIL_MSG(wxT("splitter: attempt to remove a non-existent window"));
dbc208e9 886
c801d85f 887 return FALSE;
dbc208e9 888 }
c801d85f 889
0e4cfcdd 890 OnUnsplit(win);
ca39e409 891 DoSetSashPosition(0);
3ad5e06b
VZ
892 SizeWindows();
893
894 return TRUE;
895}
896
897// Replace a window with another one
898bool wxSplitterWindow::ReplaceWindow(wxWindow *winOld, wxWindow *winNew)
899{
223d09f6
KB
900 wxCHECK_MSG( winOld, FALSE, wxT("use one of Split() functions instead") );
901 wxCHECK_MSG( winNew, FALSE, wxT("use Unsplit() functions instead") );
3ad5e06b
VZ
902
903 if ( winOld == m_windowTwo )
904 {
905 m_windowTwo = winNew;
906 }
907 else if ( winOld == m_windowOne )
908 {
909 m_windowOne = winNew;
910 }
911 else
912 {
223d09f6 913 wxFAIL_MSG(wxT("splitter: attempt to replace a non-existent window"));
3ad5e06b
VZ
914
915 return FALSE;
916 }
917
918 SizeWindows();
919
c801d85f
KB
920 return TRUE;
921}
922
ca39e409
VS
923void wxSplitterWindow::SetMinimumPaneSize(int min)
924{
925 m_minimumPaneSize = min;
926 SetSashPosition(m_sashPosition); // re-check limits
927}
928
debe6624 929void wxSplitterWindow::SetSashPosition(int position, bool redraw)
c801d85f 930{
ca39e409 931 DoSetSashPosition(position);
c801d85f
KB
932
933 if ( redraw )
934 {
935 SizeWindows();
936 }
937}
938
c801d85f 939// Initialize colours
0d559d69 940void wxSplitterWindow::InitColours()
c801d85f 941{
0d559d69
VZ
942 wxDELETE( m_facePen );
943 wxDELETE( m_faceBrush );
944 wxDELETE( m_mediumShadowPen );
945 wxDELETE( m_darkShadowPen );
946 wxDELETE( m_lightShadowPen );
947 wxDELETE( m_hilightPen );
c801d85f
KB
948
949 // Shadow colours
37d403aa 950#ifndef __WIN16__
a756f210 951 wxColour faceColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
c801d85f
KB
952 m_facePen = new wxPen(faceColour, 1, wxSOLID);
953 m_faceBrush = new wxBrush(faceColour, wxSOLID);
954
a756f210 955 wxColour mediumShadowColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW));
c801d85f
KB
956 m_mediumShadowPen = new wxPen(mediumShadowColour, 1, wxSOLID);
957
a756f210 958 wxColour darkShadowColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW));
c801d85f
KB
959 m_darkShadowPen = new wxPen(darkShadowColour, 1, wxSOLID);
960
a756f210 961 wxColour lightShadowColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT));
c801d85f
KB
962 m_lightShadowPen = new wxPen(lightShadowColour, 1, wxSOLID);
963
a756f210 964 wxColour hilightColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DHILIGHT));
c801d85f 965 m_hilightPen = new wxPen(hilightColour, 1, wxSOLID);
37d403aa 966#else
c801d85f
KB
967 m_facePen = new wxPen("LIGHT GREY", 1, wxSOLID);
968 m_faceBrush = new wxBrush("LIGHT GREY", wxSOLID);
969 m_mediumShadowPen = new wxPen("GREY", 1, wxSOLID);
970 m_darkShadowPen = new wxPen("BLACK", 1, wxSOLID);
971 m_lightShadowPen = new wxPen("LIGHT GREY", 1, wxSOLID);
972 m_hilightPen = new wxPen("WHITE", 1, wxSOLID);
37d403aa 973#endif // __WIN16__
c801d85f
KB
974}
975
3e58dcb9 976bool wxSplitterWindow::DoSendEvent(wxSplitterEvent& event)
42e69d6b 977{
3e58dcb9 978 return !GetEventHandler()->ProcessEvent(event) || event.IsAllowed();
42e69d6b
VZ
979}
980
981// ---------------------------------------------------------------------------
3e58dcb9 982// wxSplitterWindow virtual functions: they now just generate the events
42e69d6b
VZ
983// ---------------------------------------------------------------------------
984
3e58dcb9
VZ
985bool wxSplitterWindow::OnSashPositionChange(int WXUNUSED(newSashPosition))
986{
987 // always allow by default
988 return TRUE;
989}
990
991int wxSplitterWindow::OnSashPositionChanging(int newSashPosition)
42e69d6b
VZ
992{
993 // If within UNSPLIT_THRESHOLD from edge, set to edge to cause closure.
994 const int UNSPLIT_THRESHOLD = 4;
995
3e58dcb9
VZ
996 // first of all, check if OnSashPositionChange() doesn't forbid this change
997 if ( !OnSashPositionChange(newSashPosition) )
998 {
999 // it does
1000 return -1;
1001 }
42e69d6b
VZ
1002
1003 // Obtain relevant window dimension for bottom / right threshold check
2e8cc3e8 1004 int window_size = GetWindowSize();
42e69d6b 1005
370938d9 1006 bool unsplit_scenario = FALSE;
3e58dcb9 1007 if ( m_permitUnsplitAlways || m_minimumPaneSize == 0 )
bc79aa6b 1008 {
370938d9
UB
1009 // Do edge detection if unsplit premitted
1010 if ( newSashPosition <= UNSPLIT_THRESHOLD )
1011 {
1012 // threshold top / left check
1013 newSashPosition = 0;
1014 unsplit_scenario = TRUE;
1015 }
1016 if ( newSashPosition >= window_size - UNSPLIT_THRESHOLD )
1017 {
1018 // threshold bottom/right check
1019 newSashPosition = window_size;
1020 unsplit_scenario = TRUE;
1021 }
bc79aa6b
UB
1022 }
1023
370938d9 1024 if ( !unsplit_scenario )
bc79aa6b
UB
1025 {
1026 // If resultant pane would be too small, enlarge it
2e8cc3e8 1027 newSashPosition = AdjustSashPosition(newSashPosition);
bc79aa6b 1028 }
42e69d6b
VZ
1029
1030 // If the result is out of bounds it means minimum size is too big,
1031 // so split window in half as best compromise.
1032 if ( newSashPosition < 0 || newSashPosition > window_size )
1033 newSashPosition = window_size / 2;
1034
3e58dcb9
VZ
1035 // now let the event handler have it
1036 //
1037 // FIXME: shouldn't we do it before the adjustments above so as to ensure
1038 // that the sash position is always reasonable?
1039 wxSplitterEvent event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING, this);
1040 event.m_data.pos = newSashPosition;
1041
1042 if ( !DoSendEvent(event) )
42e69d6b 1043 {
3e58dcb9 1044 // the event handler vetoed the change
42e69d6b
VZ
1045 newSashPosition = -1;
1046 }
3e58dcb9
VZ
1047 else
1048 {
1049 // it could have been changed by it
1050 newSashPosition = event.GetSashPosition();
1051 }
42e69d6b 1052
3e58dcb9 1053 return newSashPosition;
42e69d6b
VZ
1054}
1055
1056// Called when the sash is double-clicked. The default behaviour is to remove
1057// the sash if the minimum pane size is zero.
3e58dcb9 1058void wxSplitterWindow::OnDoubleClickSash(int x, int y)
42e69d6b 1059{
a2f9a636
JS
1060 wxCHECK_RET(m_windowTwo, wxT("splitter: no window to remove"));
1061
3e58dcb9
VZ
1062 // new code should handle events instead of using the virtual functions
1063 wxSplitterEvent event(wxEVT_COMMAND_SPLITTER_DOUBLECLICKED, this);
1064 event.m_data.pt.x = x;
1065 event.m_data.pt.y = y;
1066 if ( DoSendEvent(event) )
42e69d6b 1067 {
3e58dcb9
VZ
1068 if ( GetMinimumPaneSize() == 0 || m_permitUnsplitAlways )
1069 {
a2f9a636 1070 wxWindow* win = m_windowTwo;
0e4cfcdd
JS
1071 if ( Unsplit(win) )
1072 {
1073 wxSplitterEvent unsplitEvent(wxEVT_COMMAND_SPLITTER_UNSPLIT, this);
1074 unsplitEvent.m_data.win = win;
1075 (void)DoSendEvent(unsplitEvent);
1076 }
3e58dcb9 1077 }
42e69d6b 1078 }
3e58dcb9 1079 //else: blocked by user
42e69d6b
VZ
1080}
1081
3e58dcb9 1082void wxSplitterWindow::OnUnsplit(wxWindow *winRemoved)
42e69d6b 1083{
0e4cfcdd 1084 // call this before calling the event handler which may delete the window
3e58dcb9 1085 winRemoved->Show(FALSE);
42e69d6b 1086}
43b5058d 1087
64407854 1088#if defined( __WXMSW__ ) || defined( __WXMAC__)
43b5058d 1089
3e58dcb9
VZ
1090// this is currently called (and needed) under MSW only...
1091void wxSplitterWindow::OnSetCursor(wxSetCursorEvent& event)
1092{
43b5058d
VZ
1093 // if we don't do it, the resizing cursor might be set for child window:
1094 // and like this we explicitly say that our cursor should not be used for
1095 // children windows which overlap us
1096
1097 if ( SashHitTest(event.GetX(), event.GetY()) )
1098 {
1099 // default processing is ok
1100 event.Skip();
1101 }
1102 //else: do nothing, in particular, don't call Skip()
43b5058d 1103}
3e58dcb9 1104
d7260478 1105#endif // wxUSE_SPLITTER
3e58dcb9
VZ
1106#endif // wxMSW
1107