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