]> git.saurik.com Git - wxWidgets.git/blame - src/generic/splitter.cpp
bug fixes
[wxWidgets.git] / src / generic / splitter.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: splitter.cpp
3// Purpose: wxSplitterWindow implementation
4// Author: Julian Smart
5// Modified by:
6// Created: 01/02/97
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart and Markus Holzem
f4621a09 9// Licence: wxWindows license
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
2f073eb2
RR
24 #include "wx/window.h"
25 #include "wx/dialog.h"
26 #include "wx/frame.h"
c801d85f
KB
27#endif
28
c801d85f
KB
29#include <stdlib.h>
30
31#include "wx/string.h"
32#include "wx/splitter.h"
33#include "wx/dcscreen.h"
1a7f3062 34#include "wx/settings.h"
a8a0b892 35#include "wx/log.h"
c801d85f 36
c801d85f 37IMPLEMENT_DYNAMIC_CLASS(wxSplitterWindow, wxWindow)
42e69d6b 38IMPLEMENT_DYNAMIC_CLASS(wxSplitterEvent, wxCommandEvent)
c801d85f
KB
39
40BEGIN_EVENT_TABLE(wxSplitterWindow, wxWindow)
41 EVT_PAINT(wxSplitterWindow::OnPaint)
42 EVT_SIZE(wxSplitterWindow::OnSize)
72195a0f 43 EVT_IDLE(wxSplitterWindow::OnIdle)
c801d85f 44 EVT_MOUSE_EVENTS(wxSplitterWindow::OnMouseEvent)
42e69d6b 45
43b5058d
VZ
46 EVT_SET_CURSOR(wxSplitterWindow::OnSetCursor)
47
42e69d6b 48 EVT_SPLITTER_SASH_POS_CHANGED(-1, wxSplitterWindow::OnSashPosChanged)
370938d9
UB
49 // NB: we borrow OnSashPosChanged for purposes of
50 // EVT_SPLITTER_SASH_POS_CHANGING since default implementation is identical
51 EVT_SPLITTER_SASH_POS_CHANGING(-1, wxSplitterWindow::OnSashPosChanged)
42e69d6b
VZ
52 EVT_SPLITTER_DCLICK(-1, wxSplitterWindow::OnDoubleClick)
53 EVT_SPLITTER_UNSPLIT(-1, wxSplitterWindow::OnUnsplitEvent)
c801d85f 54END_EVENT_TABLE()
c801d85f 55
0d559d69 56wxSplitterWindow::wxSplitterWindow()
c801d85f
KB
57{
58 m_splitMode = wxSPLIT_VERTICAL;
370938d9 59 m_permitUnsplitAlways = FALSE;
c67daf87
UR
60 m_windowOne = (wxWindow *) NULL;
61 m_windowTwo = (wxWindow *) NULL;
c801d85f
KB
62 m_dragMode = wxSPLIT_DRAG_NONE;
63 m_oldX = 0;
64 m_oldY = 0;
65 m_firstX = 0;
66 m_firstY = 0;
67 m_sashSize = 7;
68 m_borderSize = 2;
69 m_sashPosition = 0;
c67daf87
UR
70 m_sashCursorWE = (wxCursor *) NULL;
71 m_sashCursorNS = (wxCursor *) NULL;
72 m_sashTrackerPen = (wxPen *) NULL;
73 m_lightShadowPen = (wxPen *) NULL;
74 m_mediumShadowPen = (wxPen *) NULL;
75 m_darkShadowPen = (wxPen *) NULL;
76 m_faceBrush = (wxBrush *) NULL;
77 m_facePen = (wxPen *) NULL;
78 m_hilightPen = (wxPen *) NULL;
c801d85f 79 m_minimumPaneSize = 0;
72195a0f 80 m_needUpdating = FALSE;
c801d85f
KB
81}
82
0d559d69
VZ
83wxSplitterWindow::wxSplitterWindow(wxWindow *parent, wxWindowID id,
84 const wxPoint& pos,
85 const wxSize& size,
86 long style,
87 const wxString& name)
88 : wxWindow(parent, id, pos, size, style, name)
c801d85f
KB
89{
90 m_splitMode = wxSPLIT_VERTICAL;
370938d9 91 m_permitUnsplitAlways = (style & wxSP_PERMIT_UNSPLIT) != 0;
c67daf87
UR
92 m_windowOne = (wxWindow *) NULL;
93 m_windowTwo = (wxWindow *) NULL;
c801d85f
KB
94 m_dragMode = wxSPLIT_DRAG_NONE;
95 m_oldX = 0;
96 m_oldY = 0;
97 m_firstX = 0;
98 m_firstY = 0;
99 m_sashSize = 7;
100 m_borderSize = 2;
101 m_sashPosition = 0;
102 m_minimumPaneSize = 0;
103 m_sashCursorWE = new wxCursor(wxCURSOR_SIZEWE);
104 m_sashCursorNS = new wxCursor(wxCURSOR_SIZENS);
105 m_sashTrackerPen = new wxPen(*wxBLACK, 2, wxSOLID);
c67daf87
UR
106 m_lightShadowPen = (wxPen *) NULL;
107 m_mediumShadowPen = (wxPen *) NULL;
108 m_darkShadowPen = (wxPen *) NULL;
109 m_faceBrush = (wxBrush *) NULL;
110 m_facePen = (wxPen *) NULL;
111 m_hilightPen = (wxPen *) NULL;
c801d85f
KB
112
113 if ( style & wxSP_3D )
114 {
115 m_borderSize = 2;
116 m_sashSize = 7;
117 }
118 else if ( style & wxSP_BORDER )
119 {
120 m_borderSize = 1;
121 m_sashSize = 3;
122 }
123 else
124 {
125 m_borderSize = 0;
126 m_sashSize = 3;
127 }
128
129 // Eventually, we'll respond to colour change messages
130 InitColours();
131
c801d85f
KB
132 // For debugging purposes, to see the background.
133// SetBackground(wxBLUE_BRUSH);
72195a0f
RR
134
135 m_needUpdating = FALSE;
c801d85f
KB
136}
137
0d559d69 138wxSplitterWindow::~wxSplitterWindow()
c801d85f
KB
139{
140 delete m_sashCursorWE;
141 delete m_sashCursorNS;
142 delete m_sashTrackerPen;
143 delete m_lightShadowPen;
144 delete m_darkShadowPen;
145 delete m_mediumShadowPen;
146 delete m_hilightPen;
147 delete m_facePen;
148 delete m_faceBrush;
149}
150
151void wxSplitterWindow::OnPaint(wxPaintEvent& WXUNUSED(event))
152{
153 wxPaintDC dc(this);
154
155 if ( m_borderSize > 0 )
156 DrawBorders(dc);
157 DrawSash(dc);
158}
159
72195a0f
RR
160void wxSplitterWindow::OnIdle(wxIdleEvent& WXUNUSED(event))
161{
162 if (m_needUpdating)
163 SizeWindows();
164}
4c013092 165
c801d85f
KB
166void wxSplitterWindow::OnMouseEvent(wxMouseEvent& event)
167{
6f2a55e3
VZ
168 wxCoord x = (wxCoord)event.GetX(),
169 y = (wxCoord)event.GetY();
c801d85f 170
f4621a09 171 // reset the cursor
b69f1bd1
JS
172#ifdef __WXMOTIF__
173 SetCursor(* wxSTANDARD_CURSOR);
17867d61
RR
174#endif
175#ifdef __WXMSW__
f4621a09 176 SetCursor(wxCursor());
b69f1bd1 177#endif
f4621a09 178
58d1c1ae 179
0d559d69
VZ
180 if (event.LeftDown())
181 {
c801d85f
KB
182 if ( SashHitTest(x, y) )
183 {
0d559d69 184 CaptureMouse();
c801d85f 185
4a33eba6 186 m_dragMode = wxSPLIT_DRAG_DRAGGING;
f4621a09 187
72195a0f 188 if ((GetWindowStyleFlag() & wxSP_LIVE_UPDATE) == 0)
4419ba31
VZ
189 {
190 DrawSashTracker(x, y);
191 }
a6aa9b1e 192
4a33eba6
RR
193 m_oldX = x;
194 m_oldY = y;
9f334bea
JS
195
196 if ( m_splitMode == wxSPLIT_VERTICAL )
197 {
198 SetCursor(*m_sashCursorWE);
199 }
200 else
201 {
202 SetCursor(*m_sashCursorNS);
203 }
f4621a09 204 return;
c801d85f 205 }
0d559d69 206 }
0d559d69
VZ
207 else if (event.LeftUp() && m_dragMode == wxSPLIT_DRAG_DRAGGING)
208 {
c801d85f
KB
209 // We can stop dragging now and see what we've got.
210 m_dragMode = wxSPLIT_DRAG_NONE;
0d559d69 211 ReleaseMouse();
dbc208e9 212
c801d85f 213 // Erase old tracker
72195a0f 214 if ((GetWindowStyleFlag() & wxSP_LIVE_UPDATE) == 0)
4419ba31 215 {
72195a0f 216 DrawSashTracker(m_oldX, m_oldY);
4419ba31 217 }
c801d85f 218
4c013092
UB
219 // Obtain window size. We are only interested in the dimension the sash
220 // splits up
c801d85f 221 int w, h;
0d559d69 222 GetClientSize(&w, &h);
4c013092
UB
223 int window_size = (m_splitMode == wxSPLIT_VERTICAL ? w : h );
224 int new_sash_position =
225 (int) ( m_splitMode == wxSPLIT_VERTICAL ? x : y );
226
42e69d6b
VZ
227 wxSplitterEvent eventSplitter(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED,
228 this);
229 eventSplitter.m_data.pos = new_sash_position;
230 if ( GetEventHandler()->ProcessEvent(eventSplitter) )
231 {
232 new_sash_position = eventSplitter.GetSashPosition();
233 if ( new_sash_position == -1 )
234 {
235 // change not allowed
236 return;
237 }
238 }
4c013092 239
43b5058d 240 if ( m_permitUnsplitAlways || m_minimumPaneSize == 0 )
4c013092 241 {
370938d9
UB
242 // Deal with possible unsplit scenarios
243 if ( new_sash_position == 0 )
244 {
245 // We remove the first window from the view
246 wxWindow *removedWindow = m_windowOne;
247 m_windowOne = m_windowTwo;
248 m_windowTwo = (wxWindow *) NULL;
249 SendUnsplitEvent(removedWindow);
250 m_sashPosition = 0;
251 }
252 else if ( new_sash_position == window_size )
253 {
254 // We remove the second window from the view
255 wxWindow *removedWindow = m_windowTwo;
256 m_windowTwo = (wxWindow *) NULL;
257 SendUnsplitEvent(removedWindow);
258 m_sashPosition = 0;
259 }
260 else
261 {
262 m_sashPosition = new_sash_position;
263 }
c801d85f 264 }
4c013092 265 else
c801d85f 266 {
4c013092
UB
267 m_sashPosition = new_sash_position;
268 }
42e69d6b 269
c801d85f 270 SizeWindows();
6e395e9c 271 m_needUpdating = FALSE;
4a33eba6 272 } // left up && dragging
0d559d69
VZ
273 else if (event.Moving() && !event.Dragging())
274 {
c801d85f
KB
275 // Just change the cursor if required
276 if ( SashHitTest(x, y) )
277 {
0d559d69 278 if ( m_splitMode == wxSPLIT_VERTICAL )
c801d85f 279 {
0d559d69 280 SetCursor(*m_sashCursorWE);
c801d85f
KB
281 }
282 else
283 {
0d559d69 284 SetCursor(*m_sashCursorNS);
c801d85f
KB
285 }
286 }
c0bcc480 287#if defined(__WXGTK__) || defined(__WXMSW__)
58d1c1ae 288 else
42e69d6b 289 {
c0bcc480
JS
290 // We must set the normal cursor in MSW, because
291 // if the child window doesn't have a cursor, the
292 // parent's (splitter window) will be used, and this
293 // must be the standard cursor.
294 wxLogDebug("wxSplitterWindow: Setting to standard cursor");
295
42e69d6b 296 // where else do we unset the cursor?
58d1c1ae 297 SetCursor(* wxSTANDARD_CURSOR);
42e69d6b
VZ
298 }
299#endif // __WXGTK__
7e8583bd
UN
300
301 m_needUpdating = FALSE;
0d559d69 302 }
a6aa9b1e 303 else if (event.Dragging() && (m_dragMode == wxSPLIT_DRAG_DRAGGING))
0d559d69 304 {
9f334bea
JS
305#ifdef __WXMSW__
306 // Otherwise, the cursor sometimes reverts to the normal cursor
307 // during dragging.
308 if ( m_splitMode == wxSPLIT_VERTICAL )
309 {
310 SetCursor(*m_sashCursorWE);
311 }
312 else
313 {
314 SetCursor(*m_sashCursorNS);
315 }
316#endif
317
370938d9
UB
318 // Obtain window size. We are only interested in the dimension the sash
319 // splits up
320 int new_sash_position =
321 (int) ( m_splitMode == wxSPLIT_VERTICAL ? x : y );
322
323 wxSplitterEvent eventSplitter(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING,
324 this);
325 eventSplitter.m_data.pos = new_sash_position;
326 if ( GetEventHandler()->ProcessEvent(eventSplitter) )
327 {
328 new_sash_position = eventSplitter.GetSashPosition();
329 if ( new_sash_position == -1 )
330 {
331 // change not allowed
332 return;
333 }
334 }
335
4a33eba6 336 // Erase old tracker
72195a0f 337 if ((GetWindowStyleFlag() & wxSP_LIVE_UPDATE) == 0)
4419ba31 338 {
72195a0f 339 DrawSashTracker(m_oldX, m_oldY);
4419ba31 340 }
c801d85f 341
370938d9
UB
342 if (m_splitMode == wxSPLIT_VERTICAL)
343 x = new_sash_position;
344 else
345 y = new_sash_position;
346
347 if (new_sash_position != -1)
348 {
349 // Only modify if permitted
350 m_oldX = x;
351 m_oldY = y;
352 }
353
d66a042c
VZ
354#ifdef __WXMSW__
355 // As we captured the mouse, we may get the mouse events from outside
356 // our window - for example, negative values in x, y. This has a weird
357 // consequence under MSW where we use unsigned values sometimes and
358 // signed ones other times: the coordinates turn as big positive
359 // numbers and so the sash is drawn on the *right* side of the window
360 // instead of the left (or bottom instead of top). Correct this.
d66a042c
VZ
361 if ( (short)m_oldX < 0 )
362 m_oldX = 0;
363 if ( (short)m_oldY < 0 )
364 m_oldY = 0;
365#endif // __WXMSW__
366
367 // Draw new one
72195a0f 368 if ((GetWindowStyleFlag() & wxSP_LIVE_UPDATE) == 0)
4419ba31 369 {
72195a0f 370 DrawSashTracker(m_oldX, m_oldY);
4419ba31
VZ
371 }
372 else
373 {
72195a0f 374 m_sashPosition = new_sash_position;
4419ba31
VZ
375 m_needUpdating = TRUE;
376 }
0d559d69 377 }
c801d85f
KB
378 else if ( event.LeftDClick() )
379 {
42e69d6b
VZ
380 wxSplitterEvent eventSplitter(wxEVT_COMMAND_SPLITTER_DOUBLECLICKED,
381 this);
382 eventSplitter.m_data.pt.x = x;
383 eventSplitter.m_data.pt.y = y;
384
385 (void)GetEventHandler()->ProcessEvent(eventSplitter);
c801d85f 386 }
c801d85f
KB
387}
388
a8731351 389void wxSplitterWindow::OnSize(wxSizeEvent& event)
c801d85f 390{
a8731351
VZ
391 // only process this message if we're not iconized - otherwise iconizing
392 // and restoring a window containing the splitter has a funny side effect
393 // of changing the splitter position!
394 wxWindow *parent = GetParent();
395 while ( parent && !parent->IsTopLevel() )
c801d85f 396 {
a8731351
VZ
397 parent = parent->GetParent();
398 }
399
58c7cd12 400 bool iconized = FALSE;
a8731351
VZ
401 wxFrame *frame = wxDynamicCast(parent, wxFrame);
402 if ( frame )
403 iconized = frame->IsIconized();
404 else
405 {
406 wxDialog *dialog = wxDynamicCast(parent, wxDialog);
407 if ( dialog )
408 iconized = dialog->IsIconized();
409 else
223d09f6 410 wxFAIL_MSG(wxT("should have a top level frame or dialog parent!"));
a8731351
VZ
411 }
412
413 if ( iconized )
414 {
415 event.Skip();
416 }
417 else
418 {
419 int cw, ch;
420 GetClientSize( &cw, &ch );
421 if ( m_windowTwo )
c801d85f 422 {
a8731351
VZ
423 if ( m_splitMode == wxSPLIT_VERTICAL )
424 {
425 if ( m_sashPosition >= (cw - 5) )
426 m_sashPosition = wxMax(10, cw - 40);
427 }
428 if ( m_splitMode == wxSPLIT_HORIZONTAL )
429 {
430 if ( m_sashPosition >= (ch - 5) )
431 m_sashPosition = wxMax(10, ch - 40);
432 }
c801d85f 433 }
a8731351
VZ
434
435 SizeWindows();
c801d85f 436 }
c801d85f
KB
437}
438
debe6624 439bool wxSplitterWindow::SashHitTest(int x, int y, int tolerance)
c801d85f
KB
440{
441 if ( m_windowTwo == NULL || m_sashPosition == 0)
442 return FALSE; // No sash
443
444 if ( m_splitMode == wxSPLIT_VERTICAL )
445 {
446 if ( (x >= m_sashPosition - tolerance) && (x <= m_sashPosition + m_sashSize + tolerance) )
447 return TRUE;
448 else
449 return FALSE;
450 }
451 else
452 {
453 if ( (y >= (m_sashPosition- tolerance)) && (y <= (m_sashPosition + m_sashSize + tolerance)) )
454 return TRUE;
455 else
456 return FALSE;
457 }
c801d85f
KB
458}
459
460// Draw 3D effect borders
461void wxSplitterWindow::DrawBorders(wxDC& dc)
462{
463 int w, h;
464 GetClientSize(&w, &h);
465
466 if ( GetWindowStyleFlag() & wxSP_3D )
467 {
a6aa9b1e 468
1e2c86ca
PA
469 dc.SetPen(*m_facePen);
470 dc.SetBrush(*m_faceBrush);
471 dc.DrawRectangle(1, 1 , w-1, m_borderSize-2 ); //high
472 dc.DrawRectangle(1, m_borderSize-2 , m_borderSize-2, h-1 ); // left
473 dc.DrawRectangle(w-m_borderSize+2, m_borderSize-2 , w-1, h-1 ); // right
474 dc.DrawRectangle(m_borderSize-2, h-m_borderSize+2 , w-m_borderSize+2, h-1 ); //bottom
475
c801d85f 476 dc.SetPen(*m_mediumShadowPen);
1e2c86ca
PA
477 dc.DrawLine(m_borderSize-2, m_borderSize-2, w-m_borderSize+1, m_borderSize-2);
478 dc.DrawLine(m_borderSize-2, m_borderSize-2, m_borderSize-2, h-m_borderSize+1);
c801d85f
KB
479
480 dc.SetPen(*m_darkShadowPen);
1e2c86ca
PA
481 dc.DrawLine(m_borderSize-1, m_borderSize-1, w-m_borderSize, m_borderSize-1);
482 dc.DrawLine(m_borderSize-1, m_borderSize-1, m_borderSize-1, h-m_borderSize);
c801d85f
KB
483
484 dc.SetPen(*m_hilightPen);
1e2c86ca
PA
485 dc.DrawLine(m_borderSize - 2, h-m_borderSize+1, w-m_borderSize+1, h-m_borderSize+1);
486 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
487 /// Anyway, h is required for MSW.
488
489 dc.SetPen(*m_lightShadowPen);
1e2c86ca
PA
490 dc.DrawLine(w-m_borderSize, m_borderSize-1, w-m_borderSize, h-m_borderSize); // Right hand side
491 dc.DrawLine(m_borderSize-1, h-m_borderSize, w-m_borderSize+1, h-m_borderSize); // Bottom
c801d85f
KB
492 }
493 else if ( GetWindowStyleFlag() & wxSP_BORDER )
494 {
495 dc.SetBrush(*wxTRANSPARENT_BRUSH);
496 dc.SetPen(*wxBLACK_PEN);
497 dc.DrawRectangle(0, 0, w-1, h-1);
498 }
499
500 dc.SetPen(wxNullPen);
501 dc.SetBrush(wxNullBrush);
502}
503
504// Draw the sash
505void wxSplitterWindow::DrawSash(wxDC& dc)
506{
507 if ( m_sashPosition == 0 || !m_windowTwo)
508 return;
509
510 int w, h;
511 GetClientSize(&w, &h);
512
513 if ( GetWindowStyleFlag() & wxSP_3D )
514 {
515 if ( m_splitMode == wxSPLIT_VERTICAL )
516 {
517 dc.SetPen(*m_facePen);
518 dc.SetBrush(*m_faceBrush);
1e2c86ca 519 dc.DrawRectangle(m_sashPosition + 2, 0 , m_sashSize - 4, h );
c801d85f
KB
520
521 dc.SetBrush(*wxTRANSPARENT_BRUSH);
522
523 dc.SetPen(*m_lightShadowPen);
4419ba31 524 int xShadow = m_borderSize ? m_borderSize - 1 : 0 ;
1e2c86ca 525 dc.DrawLine(m_sashPosition, xShadow , m_sashPosition, h-m_borderSize);
c801d85f
KB
526
527 dc.SetPen(*m_hilightPen);
1e2c86ca 528 dc.DrawLine(m_sashPosition+1, m_borderSize - 2, m_sashPosition+1, h - m_borderSize+2);
c801d85f
KB
529
530 dc.SetPen(*m_mediumShadowPen);
4419ba31 531 int yMedium = m_borderSize ? h-m_borderSize+1 : h ;
1e2c86ca 532 dc.DrawLine(m_sashPosition+m_sashSize-2, xShadow, m_sashPosition+m_sashSize-2, yMedium);
c801d85f
KB
533
534 dc.SetPen(*m_darkShadowPen);
1e2c86ca 535 dc.DrawLine(m_sashPosition+m_sashSize-1, m_borderSize, m_sashPosition+m_sashSize-1, h-m_borderSize );
0d559d69 536 }
c801d85f
KB
537 else
538 {
539 dc.SetPen(*m_facePen);
540 dc.SetBrush(*m_faceBrush);
1e2c86ca 541 dc.DrawRectangle( m_borderSize-2, m_sashPosition + 2, w-m_borderSize+2, m_sashSize - 4);
c801d85f
KB
542
543 dc.SetBrush(*wxTRANSPARENT_BRUSH);
544
545 dc.SetPen(*m_lightShadowPen);
1e2c86ca 546 dc.DrawLine(m_borderSize-1, m_sashPosition, w-m_borderSize, m_sashPosition);
c801d85f
KB
547
548 dc.SetPen(*m_hilightPen);
1e2c86ca 549 dc.DrawLine(m_borderSize-2, m_sashPosition+1, w-m_borderSize+1, m_sashPosition+1);
c801d85f
KB
550
551 dc.SetPen(*m_mediumShadowPen);
1e2c86ca 552 dc.DrawLine(m_borderSize-1, m_sashPosition+m_sashSize-2, w-m_borderSize+1, m_sashPosition+m_sashSize-2);
c801d85f
KB
553
554 dc.SetPen(*m_darkShadowPen);
1e2c86ca 555 dc.DrawLine(m_borderSize, m_sashPosition+m_sashSize-1, w-m_borderSize, m_sashPosition+m_sashSize-1);
c801d85f
KB
556 }
557 }
558 else
559 {
560 if ( m_splitMode == wxSPLIT_VERTICAL )
561 {
562 dc.SetPen(*wxBLACK_PEN);
563 dc.SetBrush(*wxBLACK_BRUSH);
564 int h1 = h-1;
565 if ( (GetWindowStyleFlag() & wxSP_BORDER) != wxSP_BORDER )
566 h1 += 1; // Not sure why this is necessary...
567 dc.DrawRectangle(m_sashPosition, 0, m_sashSize, h1);
568 }
569 else
570 {
571 dc.SetPen(*wxBLACK_PEN);
572 dc.SetBrush(*wxBLACK_BRUSH);
573 int w1 = w-1;
574 if ( (GetWindowStyleFlag() & wxSP_BORDER) != wxSP_BORDER )
575 w1 ++;
576
577 dc.DrawRectangle(0, m_sashPosition, w1, m_sashSize);
578 }
579
580 }
581
582 dc.SetPen(wxNullPen);
583 dc.SetBrush(wxNullBrush);
584}
585
586// Draw the sash tracker (for whilst moving the sash)
debe6624 587void wxSplitterWindow::DrawSashTracker(int x, int y)
c801d85f
KB
588{
589 int w, h;
590 GetClientSize(&w, &h);
591
592 wxScreenDC screenDC;
593 int x1, y1;
594 int x2, y2;
595
596 if ( m_splitMode == wxSPLIT_VERTICAL )
597 {
598 x1 = x; y1 = 2;
599 x2 = x; y2 = h-2;
600
601 if ( x1 > w )
602 {
603 x1 = w; x2 = w;
604 }
605 else if ( x1 < 0 )
606 {
607 x1 = 0; x2 = 0;
608 }
609 }
610 else
611 {
612 x1 = 2; y1 = y;
613 x2 = w-2; y2 = y;
614
615 if ( y1 > h )
616 {
617 y1 = h;
618 y2 = h;
619 }
620 else if ( y1 < 0 )
621 {
622 y1 = 0;
623 y2 = 0;
624 }
625 }
626
627 ClientToScreen(&x1, &y1);
628 ClientToScreen(&x2, &y2);
629
3c679789 630 screenDC.SetLogicalFunction(wxINVERT);
c801d85f
KB
631 screenDC.SetPen(*m_sashTrackerPen);
632 screenDC.SetBrush(*wxTRANSPARENT_BRUSH);
633
634 screenDC.DrawLine(x1, y1, x2, y2);
635
636 screenDC.SetLogicalFunction(wxCOPY);
637
638 screenDC.SetPen(wxNullPen);
639 screenDC.SetBrush(wxNullBrush);
640}
641
642// Position and size subwindows.
643// Note that the border size applies to each subwindow, not
644// including the edges next to the sash.
0d559d69 645void wxSplitterWindow::SizeWindows()
c801d85f
KB
646{
647 int w, h;
648 GetClientSize(&w, &h);
649
650 if ( m_windowOne && !m_windowTwo )
651 {
652 m_windowOne->SetSize(m_borderSize, m_borderSize, w - 2*m_borderSize, h - 2*m_borderSize);
a6aa9b1e 653
c801d85f
KB
654 }
655 else if ( m_windowOne && m_windowTwo )
656 {
657 if (m_splitMode == wxSPLIT_VERTICAL)
658 {
659 int x1 = m_borderSize;
660 int y1 = m_borderSize;
661 int w1 = m_sashPosition - m_borderSize;
662 int h1 = h - 2*m_borderSize;
663
664 int x2 = m_sashPosition + m_sashSize;
665 int y2 = m_borderSize;
666 int w2 = w - 2*m_borderSize - m_sashSize - w1;
667 int h2 = h - 2*m_borderSize;
668
0d559d69
VZ
669 m_windowOne->SetSize(x1, y1, w1, h1);
670 m_windowTwo->SetSize(x2, y2, w2, h2);
a6aa9b1e 671
c801d85f
KB
672 }
673 else
674 {
675 m_windowOne->SetSize(m_borderSize, m_borderSize,
676 w - 2*m_borderSize, m_sashPosition - m_borderSize);
677 m_windowTwo->SetSize(m_borderSize, m_sashPosition + m_sashSize,
678 w - 2*m_borderSize, h - 2*m_borderSize - m_sashSize - (m_sashPosition - m_borderSize));
a6aa9b1e 679
c801d85f
KB
680 }
681 }
682 wxClientDC dc(this);
4419ba31
VZ
683 if ( m_borderSize > 0 )
684 DrawBorders(dc);
c801d85f
KB
685 DrawSash(dc);
686}
687
688// Set pane for unsplit window
689void wxSplitterWindow::Initialize(wxWindow *window)
690{
691 m_windowOne = window;
c67daf87 692 m_windowTwo = (wxWindow *) NULL;
c801d85f
KB
693 m_sashPosition = 0;
694}
695
696// Associates the given window with window 2, drawing the appropriate sash
697// and changing the split mode.
698// Does nothing and returns FALSE if the window is already split.
debe6624 699bool wxSplitterWindow::SplitVertically(wxWindow *window1, wxWindow *window2, int sashPosition)
c801d85f
KB
700{
701 if ( IsSplit() )
702 return FALSE;
703
0d559d69
VZ
704 int w, h;
705 GetClientSize(&w, &h);
706
c801d85f
KB
707 m_splitMode = wxSPLIT_VERTICAL;
708 m_windowOne = window1;
709 m_windowTwo = window2;
0d559d69 710 if ( sashPosition > 0 )
c801d85f 711 m_sashPosition = sashPosition;
0d559d69
VZ
712 else if ( sashPosition < 0 )
713 m_sashPosition = w - sashPosition;
714 else // default
715 m_sashPosition = w/2;
c801d85f
KB
716
717 SizeWindows();
718
719 return TRUE;
720}
721
debe6624 722bool wxSplitterWindow::SplitHorizontally(wxWindow *window1, wxWindow *window2, int sashPosition)
c801d85f
KB
723{
724 if ( IsSplit() )
725 return FALSE;
726
0d559d69
VZ
727 int w, h;
728 GetClientSize(&w, &h);
729
c801d85f
KB
730 m_splitMode = wxSPLIT_HORIZONTAL;
731 m_windowOne = window1;
732 m_windowTwo = window2;
0d559d69 733 if ( sashPosition > 0 )
c801d85f 734 m_sashPosition = sashPosition;
0d559d69
VZ
735 else if ( sashPosition < 0 )
736 m_sashPosition = h - sashPosition;
737 else // default
738 m_sashPosition = h/2;
c801d85f
KB
739
740 SizeWindows();
741
742 return TRUE;
743}
744
745
746// Remove the specified (or second) window from the view
747// Doesn't actually delete the window.
748bool wxSplitterWindow::Unsplit(wxWindow *toRemove)
749{
750 if ( ! IsSplit() )
751 return FALSE;
752
3ad5e06b 753 wxWindow *win = NULL;
c801d85f
KB
754 if ( toRemove == NULL || toRemove == m_windowTwo)
755 {
3ad5e06b 756 win = m_windowTwo ;
c67daf87 757 m_windowTwo = (wxWindow *) NULL;
c801d85f
KB
758 }
759 else if ( toRemove == m_windowOne )
760 {
3ad5e06b 761 win = m_windowOne ;
c801d85f 762 m_windowOne = m_windowTwo;
c67daf87 763 m_windowTwo = (wxWindow *) NULL;
c801d85f
KB
764 }
765 else
dbc208e9 766 {
223d09f6 767 wxFAIL_MSG(wxT("splitter: attempt to remove a non-existent window"));
dbc208e9 768
c801d85f 769 return FALSE;
dbc208e9 770 }
c801d85f 771
42e69d6b 772 SendUnsplitEvent(win);
3ad5e06b
VZ
773 m_sashPosition = 0;
774 SizeWindows();
775
776 return TRUE;
777}
778
779// Replace a window with another one
780bool wxSplitterWindow::ReplaceWindow(wxWindow *winOld, wxWindow *winNew)
781{
223d09f6
KB
782 wxCHECK_MSG( winOld, FALSE, wxT("use one of Split() functions instead") );
783 wxCHECK_MSG( winNew, FALSE, wxT("use Unsplit() functions instead") );
3ad5e06b
VZ
784
785 if ( winOld == m_windowTwo )
786 {
787 m_windowTwo = winNew;
788 }
789 else if ( winOld == m_windowOne )
790 {
791 m_windowOne = winNew;
792 }
793 else
794 {
223d09f6 795 wxFAIL_MSG(wxT("splitter: attempt to replace a non-existent window"));
3ad5e06b
VZ
796
797 return FALSE;
798 }
799
800 SizeWindows();
801
c801d85f
KB
802 return TRUE;
803}
804
debe6624 805void wxSplitterWindow::SetSashPosition(int position, bool redraw)
c801d85f
KB
806{
807 m_sashPosition = position;
808
809 if ( redraw )
810 {
811 SizeWindows();
812 }
813}
814
c801d85f 815// Initialize colours
0d559d69 816void wxSplitterWindow::InitColours()
c801d85f 817{
0d559d69
VZ
818 wxDELETE( m_facePen );
819 wxDELETE( m_faceBrush );
820 wxDELETE( m_mediumShadowPen );
821 wxDELETE( m_darkShadowPen );
822 wxDELETE( m_lightShadowPen );
823 wxDELETE( m_hilightPen );
c801d85f
KB
824
825 // Shadow colours
826#if defined(__WIN95__)
c801d85f
KB
827 wxColour faceColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
828 m_facePen = new wxPen(faceColour, 1, wxSOLID);
829 m_faceBrush = new wxBrush(faceColour, wxSOLID);
830
c801d85f
KB
831 wxColour mediumShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DSHADOW));
832 m_mediumShadowPen = new wxPen(mediumShadowColour, 1, wxSOLID);
833
c801d85f
KB
834 wxColour darkShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DDKSHADOW));
835 m_darkShadowPen = new wxPen(darkShadowColour, 1, wxSOLID);
836
c801d85f
KB
837 wxColour lightShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT));
838 m_lightShadowPen = new wxPen(lightShadowColour, 1, wxSOLID);
839
c801d85f
KB
840 wxColour hilightColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DHILIGHT));
841 m_hilightPen = new wxPen(hilightColour, 1, wxSOLID);
0d559d69 842#else // !Win32
c801d85f
KB
843 m_facePen = new wxPen("LIGHT GREY", 1, wxSOLID);
844 m_faceBrush = new wxBrush("LIGHT GREY", wxSOLID);
845 m_mediumShadowPen = new wxPen("GREY", 1, wxSOLID);
846 m_darkShadowPen = new wxPen("BLACK", 1, wxSOLID);
847 m_lightShadowPen = new wxPen("LIGHT GREY", 1, wxSOLID);
848 m_hilightPen = new wxPen("WHITE", 1, wxSOLID);
0d559d69 849#endif // Win32/!Win32
c801d85f
KB
850}
851
42e69d6b
VZ
852void wxSplitterWindow::SendUnsplitEvent(wxWindow *winRemoved)
853{
854 wxSplitterEvent event(wxEVT_COMMAND_SPLITTER_UNSPLIT, this);
855 event.m_data.win = winRemoved;
856
857 (void)GetEventHandler()->ProcessEvent(event);
858}
859
860// ---------------------------------------------------------------------------
861// splitter event handlers
862// ---------------------------------------------------------------------------
863
864void wxSplitterWindow::OnSashPosChanged(wxSplitterEvent& event)
865{
866 // If within UNSPLIT_THRESHOLD from edge, set to edge to cause closure.
867 const int UNSPLIT_THRESHOLD = 4;
868
869 int newSashPosition = event.GetSashPosition();
870
871 // Obtain relevant window dimension for bottom / right threshold check
872 int w, h;
873 GetClientSize(&w, &h);
874 int window_size = (m_splitMode == wxSPLIT_VERTICAL) ? w : h ;
875
370938d9
UB
876 bool unsplit_scenario = FALSE;
877 if ( m_permitUnsplitAlways
878 || m_minimumPaneSize == 0 )
bc79aa6b 879 {
370938d9
UB
880 // Do edge detection if unsplit premitted
881 if ( newSashPosition <= UNSPLIT_THRESHOLD )
882 {
883 // threshold top / left check
884 newSashPosition = 0;
885 unsplit_scenario = TRUE;
886 }
887 if ( newSashPosition >= window_size - UNSPLIT_THRESHOLD )
888 {
889 // threshold bottom/right check
890 newSashPosition = window_size;
891 unsplit_scenario = TRUE;
892 }
bc79aa6b
UB
893 }
894
370938d9 895 if ( !unsplit_scenario )
bc79aa6b
UB
896 {
897 // If resultant pane would be too small, enlarge it
370938d9
UB
898 if ( newSashPosition < m_minimumPaneSize )
899 newSashPosition = m_minimumPaneSize;
900 if ( newSashPosition > window_size - m_minimumPaneSize )
901 newSashPosition = window_size - m_minimumPaneSize;
bc79aa6b 902 }
42e69d6b
VZ
903
904 // If the result is out of bounds it means minimum size is too big,
905 // so split window in half as best compromise.
906 if ( newSashPosition < 0 || newSashPosition > window_size )
907 newSashPosition = window_size / 2;
908
909 // for compatibility, call the virtual function
910 if ( !OnSashPositionChange(newSashPosition) )
911 {
912 newSashPosition = -1;
913 }
914
915 event.SetSashPosition(newSashPosition);
916}
917
918// Called when the sash is double-clicked. The default behaviour is to remove
919// the sash if the minimum pane size is zero.
920void wxSplitterWindow::OnDoubleClick(wxSplitterEvent& event)
921{
922 // for compatibility, call the virtual function
923 OnDoubleClickSash(event.GetX(), event.GetY());
924
4419ba31 925 if ( GetMinimumPaneSize() == 0 || m_permitUnsplitAlways )
42e69d6b
VZ
926 {
927 Unsplit();
928 }
929}
930
931void wxSplitterWindow::OnUnsplitEvent(wxSplitterEvent& event)
932{
933 wxWindow *win = event.GetWindowBeingRemoved();
934
4419ba31
VZ
935 // do it before calling OnUnsplit() which may delete the window
936 win->Show(FALSE);
937
42e69d6b
VZ
938 // for compatibility, call the virtual function
939 OnUnsplit(win);
42e69d6b 940}
43b5058d
VZ
941
942void wxSplitterWindow::OnSetCursor(wxSetCursorEvent& event)
943{
944 // this is currently called (and needed) under MSW only...
945#ifdef __WXMSW__
946
947 // if we don't do it, the resizing cursor might be set for child window:
948 // and like this we explicitly say that our cursor should not be used for
949 // children windows which overlap us
950
951 if ( SashHitTest(event.GetX(), event.GetY()) )
952 {
953 // default processing is ok
954 event.Skip();
955 }
956 //else: do nothing, in particular, don't call Skip()
957#endif // wxMSW
958}