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