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