]> git.saurik.com Git - wxWidgets.git/blame - src/generic/splitter.cpp
typo
[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
19#ifdef __BORLANDC__
f4621a09 20 #pragma hdrstop
c801d85f
KB
21#endif
22
23#ifndef WX_PRECOMP
2b5f62a0
VZ
24 #include "wx/string.h"
25 #include "wx/utils.h"
26 #include "wx/log.h"
27
28 #include "wx/dcscreen.h"
29
2f073eb2
RR
30 #include "wx/window.h"
31 #include "wx/dialog.h"
32 #include "wx/frame.h"
c801d85f 33
2b5f62a0
VZ
34 #include "wx/settings.h"
35#endif
c801d85f 36
c801d85f 37#include "wx/splitter.h"
2b5f62a0
VZ
38
39#include <stdlib.h>
c801d85f 40
2e4df4bf
VZ
41DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED)
42DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING)
43DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPLITTER_DOUBLECLICKED)
44DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPLITTER_UNSPLIT)
45
c801d85f 46IMPLEMENT_DYNAMIC_CLASS(wxSplitterWindow, wxWindow)
3e58dcb9 47IMPLEMENT_DYNAMIC_CLASS(wxSplitterEvent, wxNotifyEvent)
c801d85f
KB
48
49BEGIN_EVENT_TABLE(wxSplitterWindow, wxWindow)
50 EVT_PAINT(wxSplitterWindow::OnPaint)
51 EVT_SIZE(wxSplitterWindow::OnSize)
72195a0f 52 EVT_IDLE(wxSplitterWindow::OnIdle)
c801d85f 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
da599db2 158void wxSplitterWindow::OnIdle(wxIdleEvent& event)
72195a0f
RR
159{
160 if (m_needUpdating)
161 SizeWindows();
da599db2
VZ
162
163 event.Skip();
72195a0f 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);
63ec9dbb 255 SetSashPositionAndNotify(0);
370938d9 256 }
3e58dcb9 257 else if ( posSashNew == GetWindowSize() )
370938d9
UB
258 {
259 // We remove the second window from the view
260 wxWindow *removedWindow = m_windowTwo;
261 m_windowTwo = (wxWindow *) NULL;
3e58dcb9 262 OnUnsplit(removedWindow);
63ec9dbb 263 SetSashPositionAndNotify(0);
370938d9
UB
264 }
265 else
266 {
63ec9dbb 267 SetSashPositionAndNotify(posSashNew);
370938d9 268 }
c801d85f 269 }
4c013092 270 else
c801d85f 271 {
63ec9dbb 272 SetSashPositionAndNotify(posSashNew);
4c013092 273 }
42e69d6b 274
c801d85f 275 SizeWindows();
4a33eba6 276 } // left up && dragging
0d559d69
VZ
277 else if (event.Moving() && !event.Dragging())
278 {
84e7741a 279 // Just change the cursor as required
c801d85f 280 if ( SashHitTest(x, y) )
153b7996 281 SetResizeCursor();
58d1c1ae 282 else
58d1c1ae 283 SetCursor(* wxSTANDARD_CURSOR);
0d559d69 284 }
a6aa9b1e 285 else if (event.Dragging() && (m_dragMode == wxSPLIT_DRAG_DRAGGING))
0d559d69 286 {
3e58dcb9 287 int diff = m_splitMode == wxSPLIT_VERTICAL ? x - m_oldX : y - m_oldY;
8dcfd2f9
VZ
288 if ( !diff )
289 {
290 // nothing to do, mouse didn't really move far enough
291 return;
292 }
370938d9 293
153b7996
VZ
294 int posSashOld = isLive ? m_sashPosition : m_sashPositionCurrent;
295 int posSashNew = OnSashPositionChanging(posSashOld + diff);
3e58dcb9 296 if ( posSashNew == -1 )
370938d9 297 {
3e58dcb9
VZ
298 // change not allowed
299 return;
370938d9 300 }
00a32dc1 301
3e58dcb9 302 if ( posSashNew == m_sashPosition )
7c1122c4 303 return;
370938d9 304
4a33eba6 305 // Erase old tracker
153b7996 306 if ( !isLive )
4419ba31 307 {
72195a0f 308 DrawSashTracker(m_oldX, m_oldY);
4419ba31 309 }
c801d85f 310
370938d9 311 if (m_splitMode == wxSPLIT_VERTICAL)
3e58dcb9 312 x = posSashNew;
370938d9 313 else
3e58dcb9 314 y = posSashNew;
370938d9 315
7c1122c4
RR
316 // Remember old positions
317 m_oldX = x;
318 m_oldY = y;
370938d9 319
d66a042c
VZ
320#ifdef __WXMSW__
321 // As we captured the mouse, we may get the mouse events from outside
322 // our window - for example, negative values in x, y. This has a weird
323 // consequence under MSW where we use unsigned values sometimes and
324 // signed ones other times: the coordinates turn as big positive
325 // numbers and so the sash is drawn on the *right* side of the window
326 // instead of the left (or bottom instead of top). Correct this.
d66a042c
VZ
327 if ( (short)m_oldX < 0 )
328 m_oldX = 0;
329 if ( (short)m_oldY < 0 )
330 m_oldY = 0;
331#endif // __WXMSW__
332
333 // Draw new one
153b7996 334 if ( !isLive )
4419ba31 335 {
153b7996
VZ
336 m_sashPositionCurrent = posSashNew;
337
72195a0f 338 DrawSashTracker(m_oldX, m_oldY);
4419ba31
VZ
339 }
340 else
341 {
63ec9dbb 342 SetSashPositionAndNotify(posSashNew);
4419ba31
VZ
343 m_needUpdating = TRUE;
344 }
0d559d69 345 }
a2f9a636 346 else if ( event.LeftDClick() && m_windowTwo )
c801d85f 347 {
3e58dcb9 348 OnDoubleClickSash(x, y);
c801d85f 349 }
c801d85f
KB
350}
351
a8731351 352void wxSplitterWindow::OnSize(wxSizeEvent& event)
c801d85f 353{
a8731351
VZ
354 // only process this message if we're not iconized - otherwise iconizing
355 // and restoring a window containing the splitter has a funny side effect
356 // of changing the splitter position!
357 wxWindow *parent = GetParent();
358 while ( parent && !parent->IsTopLevel() )
c801d85f 359 {
a8731351
VZ
360 parent = parent->GetParent();
361 }
362
642d2dc8 363 bool iconized = FALSE;
2e8cc3e8 364
642d2dc8
JS
365 // wxMotif doesn't yet have a wxTopLevelWindow implementation
366#ifdef __WXMOTIF__
367 wxFrame *winTop = wxDynamicCast(parent, wxFrame);
368#else
2e8cc3e8 369 wxTopLevelWindow *winTop = wxDynamicCast(parent, wxTopLevelWindow);
642d2dc8 370#endif
2e8cc3e8
VZ
371 if ( winTop )
372 {
373 iconized = winTop->IsIconized();
374 }
642d2dc8 375#ifndef __WXMOTIF__
a8731351
VZ
376 else
377 {
2e8cc3e8
VZ
378 wxFAIL_MSG(wxT("should have a top level parent!"));
379
380 iconized = FALSE;
a8731351 381 }
642d2dc8 382#endif
63ec9dbb 383
a8731351
VZ
384 if ( iconized )
385 {
386 event.Skip();
2e8cc3e8
VZ
387
388 return;
a8731351 389 }
2e8cc3e8
VZ
390
391 int cw, ch;
392 GetClientSize( &cw, &ch );
393 if ( m_windowTwo )
a8731351 394 {
2e8cc3e8 395 if ( m_splitMode == wxSPLIT_VERTICAL )
c801d85f 396 {
2e8cc3e8 397 if ( m_sashPosition >= (cw - 5) )
63ec9dbb 398 SetSashPositionAndNotify(wxMax(10, cw - 40));
2e8cc3e8
VZ
399 }
400 else // m_splitMode == wxSPLIT_HORIZONTAL
401 {
402 if ( m_sashPosition >= (ch - 5) )
63ec9dbb 403 SetSashPositionAndNotify(wxMax(10, ch - 40));
c801d85f
KB
404 }
405 }
2e8cc3e8
VZ
406
407 SizeWindows();
c801d85f
KB
408}
409
debe6624 410bool wxSplitterWindow::SashHitTest(int x, int y, int tolerance)
c801d85f
KB
411{
412 if ( m_windowTwo == NULL || m_sashPosition == 0)
413 return FALSE; // No sash
414
415 if ( m_splitMode == wxSPLIT_VERTICAL )
416 {
417 if ( (x >= m_sashPosition - tolerance) && (x <= m_sashPosition + m_sashSize + tolerance) )
418 return TRUE;
419 else
420 return FALSE;
421 }
422 else
423 {
424 if ( (y >= (m_sashPosition- tolerance)) && (y <= (m_sashPosition + m_sashSize + tolerance)) )
425 return TRUE;
426 else
427 return FALSE;
428 }
c801d85f
KB
429}
430
431// Draw 3D effect borders
432void wxSplitterWindow::DrawBorders(wxDC& dc)
433{
434 int w, h;
435 GetClientSize(&w, &h);
436
f6bcfd97 437 if ( GetWindowStyleFlag() & wxSP_3DBORDER )
c801d85f 438 {
a6aa9b1e 439
1e2c86ca
PA
440 dc.SetPen(*m_facePen);
441 dc.SetBrush(*m_faceBrush);
442 dc.DrawRectangle(1, 1 , w-1, m_borderSize-2 ); //high
443 dc.DrawRectangle(1, m_borderSize-2 , m_borderSize-2, h-1 ); // left
444 dc.DrawRectangle(w-m_borderSize+2, m_borderSize-2 , w-1, h-1 ); // right
445 dc.DrawRectangle(m_borderSize-2, h-m_borderSize+2 , w-m_borderSize+2, h-1 ); //bottom
446
c801d85f 447 dc.SetPen(*m_mediumShadowPen);
1e2c86ca
PA
448 dc.DrawLine(m_borderSize-2, m_borderSize-2, w-m_borderSize+1, m_borderSize-2);
449 dc.DrawLine(m_borderSize-2, m_borderSize-2, m_borderSize-2, h-m_borderSize+1);
c801d85f
KB
450
451 dc.SetPen(*m_darkShadowPen);
1e2c86ca
PA
452 dc.DrawLine(m_borderSize-1, m_borderSize-1, w-m_borderSize, m_borderSize-1);
453 dc.DrawLine(m_borderSize-1, m_borderSize-1, m_borderSize-1, h-m_borderSize);
c801d85f
KB
454
455 dc.SetPen(*m_hilightPen);
1e2c86ca
PA
456 dc.DrawLine(m_borderSize - 2, h-m_borderSize+1, w-m_borderSize+1, h-m_borderSize+1);
457 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
458 /// Anyway, h is required for MSW.
459
460 dc.SetPen(*m_lightShadowPen);
1e2c86ca
PA
461 dc.DrawLine(w-m_borderSize, m_borderSize-1, w-m_borderSize, h-m_borderSize); // Right hand side
462 dc.DrawLine(m_borderSize-1, h-m_borderSize, w-m_borderSize+1, h-m_borderSize); // Bottom
c801d85f
KB
463 }
464 else if ( GetWindowStyleFlag() & wxSP_BORDER )
465 {
466 dc.SetBrush(*wxTRANSPARENT_BRUSH);
467 dc.SetPen(*wxBLACK_PEN);
468 dc.DrawRectangle(0, 0, w-1, h-1);
469 }
470
471 dc.SetPen(wxNullPen);
472 dc.SetBrush(wxNullBrush);
473}
474
475// Draw the sash
476void wxSplitterWindow::DrawSash(wxDC& dc)
477{
478 if ( m_sashPosition == 0 || !m_windowTwo)
479 return;
f6bcfd97
BP
480 if (GetWindowStyle() & wxSP_NOSASH)
481 return;
c801d85f
KB
482
483 int w, h;
484 GetClientSize(&w, &h);
485
f6bcfd97 486 if ( GetWindowStyleFlag() & wxSP_3DSASH )
c801d85f
KB
487 {
488 if ( m_splitMode == wxSPLIT_VERTICAL )
489 {
490 dc.SetPen(*m_facePen);
d30f0930
RR
491
492 if (HasFlag( wxSP_SASH_AQUA ))
493 dc.SetBrush(*wxWHITE_BRUSH);
494 else
495 dc.SetBrush(*m_faceBrush);
1e2c86ca 496 dc.DrawRectangle(m_sashPosition + 2, 0 , m_sashSize - 4, h );
c801d85f
KB
497
498 dc.SetBrush(*wxTRANSPARENT_BRUSH);
499
500 dc.SetPen(*m_lightShadowPen);
f6bcfd97 501 int xShadow = m_borderSize ? m_borderSize - 1 : 0 ;
1e2c86ca 502 dc.DrawLine(m_sashPosition, xShadow , m_sashPosition, h-m_borderSize);
c801d85f
KB
503
504 dc.SetPen(*m_hilightPen);
1e2c86ca 505 dc.DrawLine(m_sashPosition+1, m_borderSize - 2, m_sashPosition+1, h - m_borderSize+2);
c801d85f 506
d30f0930
RR
507 if (!HasFlag( wxSP_SASH_AQUA ))
508 dc.SetPen(*m_mediumShadowPen);
509
510 int yMedium = m_borderSize ? h-m_borderSize+1 : h ;
1e2c86ca 511 dc.DrawLine(m_sashPosition+m_sashSize-2, xShadow, m_sashPosition+m_sashSize-2, yMedium);
c801d85f 512
d30f0930
RR
513 if (HasFlag( wxSP_SASH_AQUA ))
514 dc.SetPen(*m_lightShadowPen);
515 else
516 dc.SetPen(*m_darkShadowPen);
1e2c86ca 517 dc.DrawLine(m_sashPosition+m_sashSize-1, m_borderSize, m_sashPosition+m_sashSize-1, h-m_borderSize );
2e8cc3e8 518
f6bcfd97
BP
519 // Draw the top and bottom edges of the sash, if requested
520 if (GetWindowStyle() & wxSP_FULLSASH)
521 {
522 // Top
523 dc.SetPen(*m_hilightPen);
524 dc.DrawLine(m_sashPosition+1, m_borderSize, m_sashPosition+m_sashSize-1, m_borderSize);
525
526 // Bottom
527 dc.SetPen(*m_darkShadowPen);
528 dc.DrawLine(m_sashPosition+1, h-m_borderSize-1, m_sashPosition+m_sashSize-1, h-m_borderSize-1);
529 }
0d559d69 530 }
2b5f62a0 531 else // wxSPLIT_HORIZONTAL
c801d85f
KB
532 {
533 dc.SetPen(*m_facePen);
d30f0930
RR
534 if (HasFlag( wxSP_SASH_AQUA ))
535 dc.SetBrush(*wxWHITE_BRUSH);
536 else
537 dc.SetBrush(*m_faceBrush);
1e2c86ca 538 dc.DrawRectangle( m_borderSize-2, m_sashPosition + 2, w-m_borderSize+2, m_sashSize - 4);
c801d85f
KB
539
540 dc.SetBrush(*wxTRANSPARENT_BRUSH);
541
542 dc.SetPen(*m_lightShadowPen);
1e2c86ca 543 dc.DrawLine(m_borderSize-1, m_sashPosition, w-m_borderSize, m_sashPosition);
c801d85f
KB
544
545 dc.SetPen(*m_hilightPen);
1e2c86ca 546 dc.DrawLine(m_borderSize-2, m_sashPosition+1, w-m_borderSize+1, m_sashPosition+1);
c801d85f 547
d30f0930
RR
548 if (!HasFlag( wxSP_SASH_AQUA ))
549 dc.SetPen(*m_mediumShadowPen);
1e2c86ca 550 dc.DrawLine(m_borderSize-1, m_sashPosition+m_sashSize-2, w-m_borderSize+1, m_sashPosition+m_sashSize-2);
c801d85f 551
d30f0930
RR
552 if (HasFlag( wxSP_SASH_AQUA ))
553 dc.SetPen(*m_lightShadowPen);
554 else
555 dc.SetPen(*m_darkShadowPen);
1e2c86ca 556 dc.DrawLine(m_borderSize, m_sashPosition+m_sashSize-1, w-m_borderSize, m_sashPosition+m_sashSize-1);
f6bcfd97
BP
557
558 // Draw the left and right edges of the sash, if requested
559 if (GetWindowStyle() & wxSP_FULLSASH)
560 {
561 // Left
562 dc.SetPen(*m_hilightPen);
563 dc.DrawLine(m_borderSize, m_sashPosition, m_borderSize, m_sashPosition+m_sashSize);
564
565 // Right
566 dc.SetPen(*m_darkShadowPen);
567 dc.DrawLine(w-m_borderSize-1, m_sashPosition+1, w-m_borderSize-1, m_sashPosition+m_sashSize-1);
568 }
c801d85f
KB
569 }
570 }
2b5f62a0 571 else // !wxSP_3DSASH
c801d85f 572 {
2b5f62a0
VZ
573 dc.SetPen(*wxTRANSPARENT_PEN);
574 dc.SetBrush(*m_faceBrush);
c801d85f
KB
575 if ( m_splitMode == wxSPLIT_VERTICAL )
576 {
c801d85f 577 int h1 = h-1;
f6bcfd97
BP
578 int y1 = 0;
579 if ( (GetWindowStyleFlag() & wxSP_BORDER) != wxSP_BORDER && (GetWindowStyleFlag() & wxSP_3DBORDER) != wxSP_3DBORDER )
c801d85f 580 h1 += 1; // Not sure why this is necessary...
f6bcfd97
BP
581 if ( (GetWindowStyleFlag() & wxSP_3DBORDER) == wxSP_3DBORDER)
582 {
583 y1 = 2; h1 -= 3;
584 }
585 dc.DrawRectangle(m_sashPosition, y1, m_sashSize, h1);
c801d85f 586 }
2b5f62a0 587 else // wxSPLIT_HORIZONTAL
c801d85f 588 {
c801d85f 589 int w1 = w-1;
f6bcfd97
BP
590 int x1 = 0;
591 if ( (GetWindowStyleFlag() & wxSP_BORDER) != wxSP_BORDER && (GetWindowStyleFlag() & wxSP_3DBORDER) != wxSP_3DBORDER )
c801d85f 592 w1 ++;
f6bcfd97
BP
593 if ( (GetWindowStyleFlag() & wxSP_3DBORDER) == wxSP_3DBORDER)
594 {
595 x1 = 2; w1 -= 3;
596 }
597 dc.DrawRectangle(x1, m_sashPosition, w1, m_sashSize);
c801d85f 598 }
c801d85f
KB
599 }
600
601 dc.SetPen(wxNullPen);
602 dc.SetBrush(wxNullBrush);
603}
604
605// Draw the sash tracker (for whilst moving the sash)
debe6624 606void wxSplitterWindow::DrawSashTracker(int x, int y)
c801d85f
KB
607{
608 int w, h;
609 GetClientSize(&w, &h);
610
611 wxScreenDC screenDC;
612 int x1, y1;
613 int x2, y2;
614
615 if ( m_splitMode == wxSPLIT_VERTICAL )
616 {
617 x1 = x; y1 = 2;
618 x2 = x; y2 = h-2;
619
620 if ( x1 > w )
621 {
622 x1 = w; x2 = w;
623 }
624 else if ( x1 < 0 )
625 {
626 x1 = 0; x2 = 0;
627 }
628 }
629 else
630 {
631 x1 = 2; y1 = y;
632 x2 = w-2; y2 = y;
633
634 if ( y1 > h )
635 {
636 y1 = h;
637 y2 = h;
638 }
639 else if ( y1 < 0 )
640 {
641 y1 = 0;
642 y2 = 0;
643 }
644 }
645
646 ClientToScreen(&x1, &y1);
647 ClientToScreen(&x2, &y2);
648
3c679789 649 screenDC.SetLogicalFunction(wxINVERT);
c801d85f
KB
650 screenDC.SetPen(*m_sashTrackerPen);
651 screenDC.SetBrush(*wxTRANSPARENT_BRUSH);
652
653 screenDC.DrawLine(x1, y1, x2, y2);
654
655 screenDC.SetLogicalFunction(wxCOPY);
656
657 screenDC.SetPen(wxNullPen);
658 screenDC.SetBrush(wxNullBrush);
659}
660
2e8cc3e8 661int wxSplitterWindow::GetWindowSize() const
d76ac8ed 662{
2e8cc3e8
VZ
663 wxSize size = GetClientSize();
664
665 return m_splitMode == wxSPLIT_VERTICAL ? size.x : size.y;
666}
667
668int wxSplitterWindow::AdjustSashPosition(int sashPos) const
669{
670 int window_size = GetWindowSize();
671
d76ac8ed
VS
672 wxWindow *win;
673
d76ac8ed
VS
674 win = GetWindow1();
675 if ( win )
676 {
2e8cc3e8
VZ
677 // the window shouldn't be smaller than its own minimal size nor
678 // smaller than the minimual pane size specified for this splitter
679 int minSize = m_splitMode == wxSPLIT_VERTICAL ? win->GetMinWidth()
680 : win->GetMinHeight();
681
682 if ( minSize == -1 || m_minimumPaneSize > minSize )
683 minSize = m_minimumPaneSize;
684
685 minSize += GetBorderSize();
686
687 if ( sashPos < minSize )
688 sashPos = minSize;
d76ac8ed
VS
689 }
690
691 win = GetWindow2();
692 if ( win )
693 {
2e8cc3e8
VZ
694 int minSize = m_splitMode == wxSPLIT_VERTICAL ? win->GetMinWidth()
695 : win->GetMinHeight();
696
697 if ( minSize == -1 || m_minimumPaneSize > minSize )
698 minSize = m_minimumPaneSize;
699
700 int maxSize = window_size - minSize - GetBorderSize();
701 if ( sashPos > maxSize )
702 sashPos = maxSize;
d76ac8ed 703 }
2e8cc3e8
VZ
704
705 return sashPos;
d76ac8ed
VS
706}
707
63ec9dbb 708bool wxSplitterWindow::DoSetSashPosition(int sashPos)
ca39e409 709{
74c57d1f 710 int newSashPosition = AdjustSashPosition(sashPos);
3e58dcb9 711
63ec9dbb
VZ
712 if ( newSashPosition == m_sashPosition )
713 return FALSE;
74c57d1f 714
63ec9dbb
VZ
715 m_sashPosition = newSashPosition;
716
717 return TRUE;
718}
719
720void wxSplitterWindow::SetSashPositionAndNotify(int sashPos)
721{
722 if ( DoSetSashPosition(sashPos) )
723 {
74c57d1f
VZ
724 wxSplitterEvent event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED, this);
725 event.m_data.pos = m_sashPosition;
3e58dcb9 726
74c57d1f
VZ
727 (void)DoSendEvent(event);
728 }
ca39e409
VS
729}
730
c801d85f
KB
731// Position and size subwindows.
732// Note that the border size applies to each subwindow, not
733// including the edges next to the sash.
0d559d69 734void wxSplitterWindow::SizeWindows()
c801d85f 735{
74c57d1f
VZ
736 // check if we have delayed setting the real sash position
737 if ( m_requestedSashPosition != INT_MAX )
738 {
739 int newSashPosition = ConvertSashPosition(m_requestedSashPosition);
740 if ( newSashPosition != m_sashPosition )
741 {
742 DoSetSashPosition(newSashPosition);
743 }
744
2b5f62a0
VZ
745 if ( newSashPosition <= m_sashPosition
746 && newSashPosition >= m_sashPosition - GetBorderSize() )
74c57d1f
VZ
747 {
748 // don't update it any more
749 m_requestedSashPosition = INT_MAX;
750 }
751 }
ca39e409 752
c801d85f
KB
753 int w, h;
754 GetClientSize(&w, &h);
755
f6bcfd97 756 if ( GetWindow1() && !GetWindow2() )
c801d85f 757 {
63ec9dbb 758 GetWindow1()->SetSize(GetBorderSize(), GetBorderSize(),
ca39e409 759 w - 2*GetBorderSize(), h - 2*GetBorderSize());
c801d85f 760 }
f6bcfd97 761 else if ( GetWindow1() && GetWindow2() )
c801d85f 762 {
f6bcfd97 763 if (GetSplitMode() == wxSPLIT_VERTICAL)
c801d85f 764 {
f6bcfd97
BP
765 int x1 = GetBorderSize();
766 int y1 = GetBorderSize();
767 int w1 = GetSashPosition() - GetBorderSize();
768 int h1 = h - 2*GetBorderSize();
769
770 int x2 = GetSashPosition() + GetSashSize();
771 int y2 = GetBorderSize();
772 int w2 = w - 2*GetBorderSize() - GetSashSize() - w1;
773 int h2 = h - 2*GetBorderSize();
774
775 GetWindow1()->SetSize(x1, y1, w1, h1);
776 GetWindow2()->SetSize(x2, y2, w2, h2);
c801d85f
KB
777 }
778 else
779 {
f6bcfd97
BP
780 GetWindow1()->SetSize(GetBorderSize(), GetBorderSize(),
781 w - 2*GetBorderSize(), GetSashPosition() - GetBorderSize());
782 GetWindow2()->SetSize(GetBorderSize(), GetSashPosition() + GetSashSize(),
783 w - 2*GetBorderSize(), h - 2*GetBorderSize() - GetSashSize() - (GetSashPosition() - GetBorderSize()));
c801d85f
KB
784 }
785 }
786 wxClientDC dc(this);
f6bcfd97 787 if ( GetBorderSize() > 0 )
4419ba31 788 DrawBorders(dc);
c801d85f 789 DrawSash(dc);
00a32dc1 790
f6bcfd97 791 SetNeedUpdating(FALSE);
c801d85f
KB
792}
793
794// Set pane for unsplit window
795void wxSplitterWindow::Initialize(wxWindow *window)
796{
2b5f62a0 797 wxASSERT_MSG( window && window->GetParent() == this,
fe6dc50a
VZ
798 _T("windows in the splitter should have it as parent!") );
799
c801d85f 800 m_windowOne = window;
c67daf87 801 m_windowTwo = (wxWindow *) NULL;
ca39e409 802 DoSetSashPosition(0);
c801d85f
KB
803}
804
805// Associates the given window with window 2, drawing the appropriate sash
806// and changing the split mode.
807// Does nothing and returns FALSE if the window is already split.
2e8cc3e8
VZ
808bool wxSplitterWindow::DoSplit(wxSplitMode mode,
809 wxWindow *window1, wxWindow *window2,
810 int sashPosition)
c801d85f
KB
811{
812 if ( IsSplit() )
813 return FALSE;
814
2b5f62a0
VZ
815 wxCHECK_MSG( window1 && window2, FALSE,
816 _T("can not split with NULL window(s)") );
817
818 wxCHECK_MSG( window1->GetParent() == this && window2->GetParent() == this, FALSE,
fe6dc50a
VZ
819 _T("windows in the splitter should have it as parent!") );
820
2e8cc3e8 821 m_splitMode = mode;
c801d85f
KB
822 m_windowOne = window1;
823 m_windowTwo = window2;
c801d85f 824
74c57d1f
VZ
825 // remember the sash position we want to set for later if we can't set it
826 // right now (e.g. because the window is too small)
827 m_requestedSashPosition = sashPosition;
828
829 DoSetSashPosition(ConvertSashPosition(sashPosition));
830
831 SizeWindows();
832
833 return TRUE;
834}
835
836int wxSplitterWindow::ConvertSashPosition(int sashPosition) const
837{
0d559d69 838 if ( sashPosition > 0 )
2e8cc3e8 839 {
74c57d1f 840 return sashPosition;
2e8cc3e8 841 }
0d559d69 842 else if ( sashPosition < 0 )
2e8cc3e8
VZ
843 {
844 // It's negative so adding is subtracting
74c57d1f 845 return GetWindowSize() + sashPosition;
2e8cc3e8 846 }
74c57d1f 847 else // sashPosition == 0
2e8cc3e8 848 {
74c57d1f
VZ
849 // default, put it in the centre
850 return GetWindowSize() / 2;
2e8cc3e8 851 }
c801d85f
KB
852}
853
c801d85f
KB
854// Remove the specified (or second) window from the view
855// Doesn't actually delete the window.
856bool wxSplitterWindow::Unsplit(wxWindow *toRemove)
857{
858 if ( ! IsSplit() )
859 return FALSE;
860
3ad5e06b 861 wxWindow *win = NULL;
c801d85f
KB
862 if ( toRemove == NULL || toRemove == m_windowTwo)
863 {
3ad5e06b 864 win = m_windowTwo ;
c67daf87 865 m_windowTwo = (wxWindow *) NULL;
c801d85f
KB
866 }
867 else if ( toRemove == m_windowOne )
868 {
3ad5e06b 869 win = m_windowOne ;
c801d85f 870 m_windowOne = m_windowTwo;
c67daf87 871 m_windowTwo = (wxWindow *) NULL;
c801d85f
KB
872 }
873 else
dbc208e9 874 {
223d09f6 875 wxFAIL_MSG(wxT("splitter: attempt to remove a non-existent window"));
dbc208e9 876
c801d85f 877 return FALSE;
dbc208e9 878 }
c801d85f 879
a2f9a636 880 win->Show(FALSE);
ca39e409 881 DoSetSashPosition(0);
3ad5e06b
VZ
882 SizeWindows();
883
884 return TRUE;
885}
886
887// Replace a window with another one
888bool wxSplitterWindow::ReplaceWindow(wxWindow *winOld, wxWindow *winNew)
889{
223d09f6
KB
890 wxCHECK_MSG( winOld, FALSE, wxT("use one of Split() functions instead") );
891 wxCHECK_MSG( winNew, FALSE, wxT("use Unsplit() functions instead") );
3ad5e06b
VZ
892
893 if ( winOld == m_windowTwo )
894 {
895 m_windowTwo = winNew;
896 }
897 else if ( winOld == m_windowOne )
898 {
899 m_windowOne = winNew;
900 }
901 else
902 {
223d09f6 903 wxFAIL_MSG(wxT("splitter: attempt to replace a non-existent window"));
3ad5e06b
VZ
904
905 return FALSE;
906 }
907
908 SizeWindows();
909
c801d85f
KB
910 return TRUE;
911}
912
ca39e409
VS
913void wxSplitterWindow::SetMinimumPaneSize(int min)
914{
915 m_minimumPaneSize = min;
916 SetSashPosition(m_sashPosition); // re-check limits
917}
918
debe6624 919void wxSplitterWindow::SetSashPosition(int position, bool redraw)
c801d85f 920{
ca39e409 921 DoSetSashPosition(position);
c801d85f
KB
922
923 if ( redraw )
924 {
925 SizeWindows();
926 }
927}
928
c801d85f 929// Initialize colours
0d559d69 930void wxSplitterWindow::InitColours()
c801d85f 931{
0d559d69
VZ
932 wxDELETE( m_facePen );
933 wxDELETE( m_faceBrush );
934 wxDELETE( m_mediumShadowPen );
935 wxDELETE( m_darkShadowPen );
936 wxDELETE( m_lightShadowPen );
937 wxDELETE( m_hilightPen );
c801d85f
KB
938
939 // Shadow colours
37d403aa 940#ifndef __WIN16__
a756f210 941 wxColour faceColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
c801d85f
KB
942 m_facePen = new wxPen(faceColour, 1, wxSOLID);
943 m_faceBrush = new wxBrush(faceColour, wxSOLID);
944
a756f210 945 wxColour mediumShadowColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW));
c801d85f
KB
946 m_mediumShadowPen = new wxPen(mediumShadowColour, 1, wxSOLID);
947
a756f210 948 wxColour darkShadowColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW));
c801d85f
KB
949 m_darkShadowPen = new wxPen(darkShadowColour, 1, wxSOLID);
950
a756f210 951 wxColour lightShadowColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT));
c801d85f
KB
952 m_lightShadowPen = new wxPen(lightShadowColour, 1, wxSOLID);
953
a756f210 954 wxColour hilightColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DHILIGHT));
c801d85f 955 m_hilightPen = new wxPen(hilightColour, 1, wxSOLID);
37d403aa 956#else
c801d85f
KB
957 m_facePen = new wxPen("LIGHT GREY", 1, wxSOLID);
958 m_faceBrush = new wxBrush("LIGHT GREY", wxSOLID);
959 m_mediumShadowPen = new wxPen("GREY", 1, wxSOLID);
960 m_darkShadowPen = new wxPen("BLACK", 1, wxSOLID);
961 m_lightShadowPen = new wxPen("LIGHT GREY", 1, wxSOLID);
962 m_hilightPen = new wxPen("WHITE", 1, wxSOLID);
37d403aa 963#endif // __WIN16__
c801d85f
KB
964}
965
3e58dcb9 966bool wxSplitterWindow::DoSendEvent(wxSplitterEvent& event)
42e69d6b 967{
3e58dcb9 968 return !GetEventHandler()->ProcessEvent(event) || event.IsAllowed();
42e69d6b
VZ
969}
970
971// ---------------------------------------------------------------------------
3e58dcb9 972// wxSplitterWindow virtual functions: they now just generate the events
42e69d6b
VZ
973// ---------------------------------------------------------------------------
974
3e58dcb9
VZ
975bool wxSplitterWindow::OnSashPositionChange(int WXUNUSED(newSashPosition))
976{
977 // always allow by default
978 return TRUE;
979}
980
981int wxSplitterWindow::OnSashPositionChanging(int newSashPosition)
42e69d6b
VZ
982{
983 // If within UNSPLIT_THRESHOLD from edge, set to edge to cause closure.
984 const int UNSPLIT_THRESHOLD = 4;
985
3e58dcb9
VZ
986 // first of all, check if OnSashPositionChange() doesn't forbid this change
987 if ( !OnSashPositionChange(newSashPosition) )
988 {
989 // it does
990 return -1;
991 }
42e69d6b
VZ
992
993 // Obtain relevant window dimension for bottom / right threshold check
2e8cc3e8 994 int window_size = GetWindowSize();
42e69d6b 995
370938d9 996 bool unsplit_scenario = FALSE;
3e58dcb9 997 if ( m_permitUnsplitAlways || m_minimumPaneSize == 0 )
bc79aa6b 998 {
370938d9
UB
999 // Do edge detection if unsplit premitted
1000 if ( newSashPosition <= UNSPLIT_THRESHOLD )
1001 {
1002 // threshold top / left check
1003 newSashPosition = 0;
1004 unsplit_scenario = TRUE;
1005 }
1006 if ( newSashPosition >= window_size - UNSPLIT_THRESHOLD )
1007 {
1008 // threshold bottom/right check
1009 newSashPosition = window_size;
1010 unsplit_scenario = TRUE;
1011 }
bc79aa6b
UB
1012 }
1013
370938d9 1014 if ( !unsplit_scenario )
bc79aa6b
UB
1015 {
1016 // If resultant pane would be too small, enlarge it
2e8cc3e8 1017 newSashPosition = AdjustSashPosition(newSashPosition);
bc79aa6b 1018 }
42e69d6b
VZ
1019
1020 // If the result is out of bounds it means minimum size is too big,
1021 // so split window in half as best compromise.
1022 if ( newSashPosition < 0 || newSashPosition > window_size )
1023 newSashPosition = window_size / 2;
1024
3e58dcb9
VZ
1025 // now let the event handler have it
1026 //
1027 // FIXME: shouldn't we do it before the adjustments above so as to ensure
1028 // that the sash position is always reasonable?
1029 wxSplitterEvent event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING, this);
1030 event.m_data.pos = newSashPosition;
1031
1032 if ( !DoSendEvent(event) )
42e69d6b 1033 {
3e58dcb9 1034 // the event handler vetoed the change
42e69d6b
VZ
1035 newSashPosition = -1;
1036 }
3e58dcb9
VZ
1037 else
1038 {
1039 // it could have been changed by it
1040 newSashPosition = event.GetSashPosition();
1041 }
42e69d6b 1042
3e58dcb9 1043 return newSashPosition;
42e69d6b
VZ
1044}
1045
1046// Called when the sash is double-clicked. The default behaviour is to remove
1047// the sash if the minimum pane size is zero.
3e58dcb9 1048void wxSplitterWindow::OnDoubleClickSash(int x, int y)
42e69d6b 1049{
a2f9a636
JS
1050 wxCHECK_RET(m_windowTwo, wxT("splitter: no window to remove"));
1051
3e58dcb9
VZ
1052 // new code should handle events instead of using the virtual functions
1053 wxSplitterEvent event(wxEVT_COMMAND_SPLITTER_DOUBLECLICKED, this);
1054 event.m_data.pt.x = x;
1055 event.m_data.pt.y = y;
1056 if ( DoSendEvent(event) )
42e69d6b 1057 {
3e58dcb9
VZ
1058 if ( GetMinimumPaneSize() == 0 || m_permitUnsplitAlways )
1059 {
a2f9a636
JS
1060 wxWindow* win = m_windowTwo;
1061 if (Unsplit(win))
1062 OnUnsplit(win);
3e58dcb9 1063 }
42e69d6b 1064 }
3e58dcb9 1065 //else: blocked by user
42e69d6b
VZ
1066}
1067
3e58dcb9 1068void wxSplitterWindow::OnUnsplit(wxWindow *winRemoved)
42e69d6b 1069{
3e58dcb9
VZ
1070 // do it before calling the event handler which may delete the window
1071 winRemoved->Show(FALSE);
42e69d6b 1072
3e58dcb9
VZ
1073 wxSplitterEvent event(wxEVT_COMMAND_SPLITTER_UNSPLIT, this);
1074 event.m_data.win = winRemoved;
4419ba31 1075
3e58dcb9 1076 (void)DoSendEvent(event);
42e69d6b 1077}
43b5058d 1078
64407854 1079#if defined( __WXMSW__ ) || defined( __WXMAC__)
43b5058d 1080
3e58dcb9
VZ
1081// this is currently called (and needed) under MSW only...
1082void wxSplitterWindow::OnSetCursor(wxSetCursorEvent& event)
1083{
43b5058d
VZ
1084 // if we don't do it, the resizing cursor might be set for child window:
1085 // and like this we explicitly say that our cursor should not be used for
1086 // children windows which overlap us
1087
1088 if ( SashHitTest(event.GetX(), event.GetY()) )
1089 {
1090 // default processing is ok
1091 event.Skip();
1092 }
1093 //else: do nothing, in particular, don't call Skip()
43b5058d 1094}
3e58dcb9
VZ
1095
1096#endif // wxMSW
1097