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