]> git.saurik.com Git - wxWidgets.git/blame - src/generic/splitter.cpp
do NOT hard code univ sources in x11 port
[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"
ed2e7e59 36#include "wx/utils.h"
c801d85f 37
2e4df4bf
VZ
38DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED)
39DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING)
40DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPLITTER_DOUBLECLICKED)
41DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPLITTER_UNSPLIT)
42
c801d85f 43IMPLEMENT_DYNAMIC_CLASS(wxSplitterWindow, wxWindow)
42e69d6b 44IMPLEMENT_DYNAMIC_CLASS(wxSplitterEvent, wxCommandEvent)
c801d85f
KB
45
46BEGIN_EVENT_TABLE(wxSplitterWindow, wxWindow)
47 EVT_PAINT(wxSplitterWindow::OnPaint)
48 EVT_SIZE(wxSplitterWindow::OnSize)
72195a0f 49 EVT_IDLE(wxSplitterWindow::OnIdle)
c801d85f 50 EVT_MOUSE_EVENTS(wxSplitterWindow::OnMouseEvent)
42e69d6b 51
43b5058d
VZ
52 EVT_SET_CURSOR(wxSplitterWindow::OnSetCursor)
53
42e69d6b 54 EVT_SPLITTER_SASH_POS_CHANGED(-1, wxSplitterWindow::OnSashPosChanged)
370938d9
UB
55 // NB: we borrow OnSashPosChanged for purposes of
56 // EVT_SPLITTER_SASH_POS_CHANGING since default implementation is identical
57 EVT_SPLITTER_SASH_POS_CHANGING(-1, wxSplitterWindow::OnSashPosChanged)
42e69d6b
VZ
58 EVT_SPLITTER_DCLICK(-1, wxSplitterWindow::OnDoubleClick)
59 EVT_SPLITTER_UNSPLIT(-1, wxSplitterWindow::OnUnsplitEvent)
6b55490a
VZ
60
61 WX_EVENT_TABLE_CONTROL_CONTAINER(wxSplitterWindow)
c801d85f 62END_EVENT_TABLE()
c801d85f 63
6b55490a
VZ
64WX_DELEGATE_TO_CONTROL_CONTAINER(wxSplitterWindow);
65
f6bcfd97 66bool wxSplitterWindow::Create(wxWindow *parent, wxWindowID id,
0d559d69
VZ
67 const wxPoint& pos,
68 const wxSize& size,
69 long style,
70 const wxString& name)
c801d85f 71{
6b55490a
VZ
72 // allow TABbing from one window to the other
73 style |= wxTAB_TRAVERSAL;
74
f6bcfd97
BP
75 if (!wxWindow::Create(parent, id, pos, size, style, name))
76 return FALSE;
77
370938d9 78 m_permitUnsplitAlways = (style & wxSP_PERMIT_UNSPLIT) != 0;
f6bcfd97
BP
79
80 if ( style & wxSP_3DSASH )
81 m_sashSize = 7;
82 else
83 m_sashSize = 3;
84
85 if ( style & wxSP_3DBORDER )
86 m_borderSize = 2;
87 else if ( style & wxSP_BORDER )
88 m_borderSize = 1;
89 else
90 m_borderSize = 0;
ed2e7e59
RR
91
92#ifdef __WXMAC__
93 int major,minor;
94 wxGetOsVersion( &major, &minor );
95 if (major >= 10)
96 m_windowStyle |= wxSP_SASH_AQUA;
97#endif
f6bcfd97
BP
98
99 return TRUE;
100}
101
102void wxSplitterWindow::Init()
103{
9948d31f
VZ
104 m_container.SetContainerWindow(this);
105
f6bcfd97
BP
106 m_splitMode = wxSPLIT_VERTICAL;
107 m_permitUnsplitAlways = TRUE;
c67daf87
UR
108 m_windowOne = (wxWindow *) NULL;
109 m_windowTwo = (wxWindow *) NULL;
c801d85f
KB
110 m_dragMode = wxSPLIT_DRAG_NONE;
111 m_oldX = 0;
112 m_oldY = 0;
113 m_firstX = 0;
114 m_firstY = 0;
115 m_sashSize = 7;
116 m_borderSize = 2;
117 m_sashPosition = 0;
118 m_minimumPaneSize = 0;
119 m_sashCursorWE = new wxCursor(wxCURSOR_SIZEWE);
120 m_sashCursorNS = new wxCursor(wxCURSOR_SIZENS);
121 m_sashTrackerPen = new wxPen(*wxBLACK, 2, wxSOLID);
c67daf87
UR
122 m_lightShadowPen = (wxPen *) NULL;
123 m_mediumShadowPen = (wxPen *) NULL;
124 m_darkShadowPen = (wxPen *) NULL;
125 m_faceBrush = (wxBrush *) NULL;
126 m_facePen = (wxPen *) NULL;
127 m_hilightPen = (wxPen *) NULL;
c801d85f 128
f6bcfd97
BP
129 m_borderSize = 0;
130 m_sashSize = 3;
c801d85f 131
c801d85f
KB
132 InitColours();
133
72195a0f 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
da599db2 159void wxSplitterWindow::OnIdle(wxIdleEvent& event)
72195a0f
RR
160{
161 if (m_needUpdating)
162 SizeWindows();
da599db2
VZ
163
164 event.Skip();
72195a0f 165}
4c013092 166
c801d85f
KB
167void wxSplitterWindow::OnMouseEvent(wxMouseEvent& event)
168{
6f2a55e3
VZ
169 wxCoord x = (wxCoord)event.GetX(),
170 y = (wxCoord)event.GetY();
c801d85f 171
f4621a09 172 // reset the cursor
b69f1bd1
JS
173#ifdef __WXMOTIF__
174 SetCursor(* wxSTANDARD_CURSOR);
17867d61
RR
175#endif
176#ifdef __WXMSW__
f4621a09 177 SetCursor(wxCursor());
b69f1bd1 178#endif
f4621a09 179
f6bcfd97
BP
180 if (GetWindowStyle() & wxSP_NOSASH)
181 return;
58d1c1ae 182
0d559d69
VZ
183 if (event.LeftDown())
184 {
c801d85f
KB
185 if ( SashHitTest(x, y) )
186 {
0d559d69 187 CaptureMouse();
c801d85f 188
4a33eba6 189 m_dragMode = wxSPLIT_DRAG_DRAGGING;
f4621a09 190
72195a0f 191 if ((GetWindowStyleFlag() & wxSP_LIVE_UPDATE) == 0)
4419ba31
VZ
192 {
193 DrawSashTracker(x, y);
194 }
a6aa9b1e 195
4a33eba6
RR
196 m_oldX = x;
197 m_oldY = y;
9f334bea
JS
198
199 if ( m_splitMode == wxSPLIT_VERTICAL )
200 {
201 SetCursor(*m_sashCursorWE);
202 }
203 else
204 {
205 SetCursor(*m_sashCursorNS);
206 }
f4621a09 207 return;
c801d85f 208 }
0d559d69 209 }
0d559d69
VZ
210 else if (event.LeftUp() && m_dragMode == wxSPLIT_DRAG_DRAGGING)
211 {
c801d85f
KB
212 // We can stop dragging now and see what we've got.
213 m_dragMode = wxSPLIT_DRAG_NONE;
0d559d69 214 ReleaseMouse();
dbc208e9 215
c801d85f 216 // Erase old tracker
72195a0f 217 if ((GetWindowStyleFlag() & wxSP_LIVE_UPDATE) == 0)
4419ba31 218 {
72195a0f 219 DrawSashTracker(m_oldX, m_oldY);
4419ba31 220 }
c801d85f 221
4c013092
UB
222 // Obtain window size. We are only interested in the dimension the sash
223 // splits up
c801d85f 224 int w, h;
0d559d69 225 GetClientSize(&w, &h);
4c013092
UB
226 int window_size = (m_splitMode == wxSPLIT_VERTICAL ? w : h );
227 int new_sash_position =
228 (int) ( m_splitMode == wxSPLIT_VERTICAL ? x : y );
229
42e69d6b
VZ
230 wxSplitterEvent eventSplitter(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED,
231 this);
232 eventSplitter.m_data.pos = new_sash_position;
233 if ( GetEventHandler()->ProcessEvent(eventSplitter) )
234 {
235 new_sash_position = eventSplitter.GetSashPosition();
236 if ( new_sash_position == -1 )
237 {
238 // change not allowed
239 return;
240 }
241 }
4c013092 242
43b5058d 243 if ( m_permitUnsplitAlways || m_minimumPaneSize == 0 )
4c013092 244 {
370938d9
UB
245 // Deal with possible unsplit scenarios
246 if ( new_sash_position == 0 )
247 {
248 // We remove the first window from the view
249 wxWindow *removedWindow = m_windowOne;
250 m_windowOne = m_windowTwo;
251 m_windowTwo = (wxWindow *) NULL;
252 SendUnsplitEvent(removedWindow);
253 m_sashPosition = 0;
254 }
255 else if ( new_sash_position == window_size )
256 {
257 // We remove the second window from the view
258 wxWindow *removedWindow = m_windowTwo;
259 m_windowTwo = (wxWindow *) NULL;
260 SendUnsplitEvent(removedWindow);
261 m_sashPosition = 0;
262 }
263 else
264 {
265 m_sashPosition = new_sash_position;
266 }
c801d85f 267 }
4c013092 268 else
c801d85f 269 {
4c013092
UB
270 m_sashPosition = new_sash_position;
271 }
42e69d6b 272
c801d85f 273 SizeWindows();
4a33eba6 274 } // left up && dragging
0d559d69
VZ
275 else if (event.Moving() && !event.Dragging())
276 {
c801d85f
KB
277 // Just change the cursor if required
278 if ( SashHitTest(x, y) )
279 {
0d559d69 280 if ( m_splitMode == wxSPLIT_VERTICAL )
c801d85f 281 {
0d559d69 282 SetCursor(*m_sashCursorWE);
c801d85f
KB
283 }
284 else
285 {
0d559d69 286 SetCursor(*m_sashCursorNS);
c801d85f
KB
287 }
288 }
c0bcc480 289#if defined(__WXGTK__) || defined(__WXMSW__)
58d1c1ae 290 else
42e69d6b 291 {
c0bcc480
JS
292 // We must set the normal cursor in MSW, because
293 // if the child window doesn't have a cursor, the
294 // parent's (splitter window) will be used, and this
295 // must be the standard cursor.
c0bcc480 296
42e69d6b 297 // where else do we unset the cursor?
58d1c1ae 298 SetCursor(* wxSTANDARD_CURSOR);
42e69d6b
VZ
299 }
300#endif // __WXGTK__
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 }
00a32dc1 334
7c1122c4
RR
335 if (new_sash_position == m_sashPosition)
336 return;
370938d9 337
4a33eba6 338 // Erase old tracker
72195a0f 339 if ((GetWindowStyleFlag() & wxSP_LIVE_UPDATE) == 0)
4419ba31 340 {
72195a0f 341 DrawSashTracker(m_oldX, m_oldY);
4419ba31 342 }
c801d85f 343
370938d9
UB
344 if (m_splitMode == wxSPLIT_VERTICAL)
345 x = new_sash_position;
346 else
347 y = new_sash_position;
348
7c1122c4
RR
349 // Remember old positions
350 m_oldX = x;
351 m_oldY = y;
370938d9 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
f6bcfd97 465 if ( GetWindowStyleFlag() & wxSP_3DBORDER )
c801d85f 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;
f6bcfd97
BP
508 if (GetWindowStyle() & wxSP_NOSASH)
509 return;
c801d85f
KB
510
511 int w, h;
512 GetClientSize(&w, &h);
513
f6bcfd97 514 if ( GetWindowStyleFlag() & wxSP_3DSASH )
c801d85f
KB
515 {
516 if ( m_splitMode == wxSPLIT_VERTICAL )
517 {
518 dc.SetPen(*m_facePen);
d30f0930
RR
519
520 if (HasFlag( wxSP_SASH_AQUA ))
521 dc.SetBrush(*wxWHITE_BRUSH);
522 else
523 dc.SetBrush(*m_faceBrush);
1e2c86ca 524 dc.DrawRectangle(m_sashPosition + 2, 0 , m_sashSize - 4, h );
c801d85f
KB
525
526 dc.SetBrush(*wxTRANSPARENT_BRUSH);
527
528 dc.SetPen(*m_lightShadowPen);
f6bcfd97 529 int xShadow = m_borderSize ? m_borderSize - 1 : 0 ;
1e2c86ca 530 dc.DrawLine(m_sashPosition, xShadow , m_sashPosition, h-m_borderSize);
c801d85f
KB
531
532 dc.SetPen(*m_hilightPen);
1e2c86ca 533 dc.DrawLine(m_sashPosition+1, m_borderSize - 2, m_sashPosition+1, h - m_borderSize+2);
c801d85f 534
d30f0930
RR
535 if (!HasFlag( wxSP_SASH_AQUA ))
536 dc.SetPen(*m_mediumShadowPen);
537
538 int yMedium = m_borderSize ? h-m_borderSize+1 : h ;
1e2c86ca 539 dc.DrawLine(m_sashPosition+m_sashSize-2, xShadow, m_sashPosition+m_sashSize-2, yMedium);
c801d85f 540
d30f0930
RR
541 if (HasFlag( wxSP_SASH_AQUA ))
542 dc.SetPen(*m_lightShadowPen);
543 else
544 dc.SetPen(*m_darkShadowPen);
1e2c86ca 545 dc.DrawLine(m_sashPosition+m_sashSize-1, m_borderSize, m_sashPosition+m_sashSize-1, h-m_borderSize );
ac6bdf40 546
f6bcfd97
BP
547 // Draw the top and bottom edges of the sash, if requested
548 if (GetWindowStyle() & wxSP_FULLSASH)
549 {
550 // Top
551 dc.SetPen(*m_hilightPen);
552 dc.DrawLine(m_sashPosition+1, m_borderSize, m_sashPosition+m_sashSize-1, m_borderSize);
553
554 // Bottom
555 dc.SetPen(*m_darkShadowPen);
556 dc.DrawLine(m_sashPosition+1, h-m_borderSize-1, m_sashPosition+m_sashSize-1, h-m_borderSize-1);
557 }
0d559d69 558 }
c801d85f
KB
559 else
560 {
561 dc.SetPen(*m_facePen);
d30f0930
RR
562 if (HasFlag( wxSP_SASH_AQUA ))
563 dc.SetBrush(*wxWHITE_BRUSH);
564 else
565 dc.SetBrush(*m_faceBrush);
1e2c86ca 566 dc.DrawRectangle( m_borderSize-2, m_sashPosition + 2, w-m_borderSize+2, m_sashSize - 4);
c801d85f
KB
567
568 dc.SetBrush(*wxTRANSPARENT_BRUSH);
569
570 dc.SetPen(*m_lightShadowPen);
1e2c86ca 571 dc.DrawLine(m_borderSize-1, m_sashPosition, w-m_borderSize, m_sashPosition);
c801d85f
KB
572
573 dc.SetPen(*m_hilightPen);
1e2c86ca 574 dc.DrawLine(m_borderSize-2, m_sashPosition+1, w-m_borderSize+1, m_sashPosition+1);
c801d85f 575
d30f0930
RR
576 if (!HasFlag( wxSP_SASH_AQUA ))
577 dc.SetPen(*m_mediumShadowPen);
1e2c86ca 578 dc.DrawLine(m_borderSize-1, m_sashPosition+m_sashSize-2, w-m_borderSize+1, m_sashPosition+m_sashSize-2);
c801d85f 579
d30f0930
RR
580 if (HasFlag( wxSP_SASH_AQUA ))
581 dc.SetPen(*m_lightShadowPen);
582 else
583 dc.SetPen(*m_darkShadowPen);
1e2c86ca 584 dc.DrawLine(m_borderSize, m_sashPosition+m_sashSize-1, w-m_borderSize, m_sashPosition+m_sashSize-1);
f6bcfd97
BP
585
586 // Draw the left and right edges of the sash, if requested
587 if (GetWindowStyle() & wxSP_FULLSASH)
588 {
589 // Left
590 dc.SetPen(*m_hilightPen);
591 dc.DrawLine(m_borderSize, m_sashPosition, m_borderSize, m_sashPosition+m_sashSize);
592
593 // Right
594 dc.SetPen(*m_darkShadowPen);
595 dc.DrawLine(w-m_borderSize-1, m_sashPosition+1, w-m_borderSize-1, m_sashPosition+m_sashSize-1);
596 }
c801d85f
KB
597 }
598 }
599 else
600 {
601 if ( m_splitMode == wxSPLIT_VERTICAL )
602 {
603 dc.SetPen(*wxBLACK_PEN);
604 dc.SetBrush(*wxBLACK_BRUSH);
605 int h1 = h-1;
f6bcfd97
BP
606 int y1 = 0;
607 if ( (GetWindowStyleFlag() & wxSP_BORDER) != wxSP_BORDER && (GetWindowStyleFlag() & wxSP_3DBORDER) != wxSP_3DBORDER )
c801d85f 608 h1 += 1; // Not sure why this is necessary...
f6bcfd97
BP
609 if ( (GetWindowStyleFlag() & wxSP_3DBORDER) == wxSP_3DBORDER)
610 {
611 y1 = 2; h1 -= 3;
612 }
613 dc.DrawRectangle(m_sashPosition, y1, m_sashSize, h1);
c801d85f
KB
614 }
615 else
616 {
617 dc.SetPen(*wxBLACK_PEN);
618 dc.SetBrush(*wxBLACK_BRUSH);
619 int w1 = w-1;
f6bcfd97
BP
620 int x1 = 0;
621 if ( (GetWindowStyleFlag() & wxSP_BORDER) != wxSP_BORDER && (GetWindowStyleFlag() & wxSP_3DBORDER) != wxSP_3DBORDER )
c801d85f 622 w1 ++;
f6bcfd97
BP
623 if ( (GetWindowStyleFlag() & wxSP_3DBORDER) == wxSP_3DBORDER)
624 {
625 x1 = 2; w1 -= 3;
626 }
627 dc.DrawRectangle(x1, m_sashPosition, w1, m_sashSize);
c801d85f
KB
628 }
629
630 }
631
632 dc.SetPen(wxNullPen);
633 dc.SetBrush(wxNullBrush);
634}
635
636// Draw the sash tracker (for whilst moving the sash)
debe6624 637void wxSplitterWindow::DrawSashTracker(int x, int y)
c801d85f
KB
638{
639 int w, h;
640 GetClientSize(&w, &h);
641
642 wxScreenDC screenDC;
643 int x1, y1;
644 int x2, y2;
645
646 if ( m_splitMode == wxSPLIT_VERTICAL )
647 {
648 x1 = x; y1 = 2;
649 x2 = x; y2 = h-2;
650
651 if ( x1 > w )
652 {
653 x1 = w; x2 = w;
654 }
655 else if ( x1 < 0 )
656 {
657 x1 = 0; x2 = 0;
658 }
659 }
660 else
661 {
662 x1 = 2; y1 = y;
663 x2 = w-2; y2 = y;
664
665 if ( y1 > h )
666 {
667 y1 = h;
668 y2 = h;
669 }
670 else if ( y1 < 0 )
671 {
672 y1 = 0;
673 y2 = 0;
674 }
675 }
676
677 ClientToScreen(&x1, &y1);
678 ClientToScreen(&x2, &y2);
679
3c679789 680 screenDC.SetLogicalFunction(wxINVERT);
c801d85f
KB
681 screenDC.SetPen(*m_sashTrackerPen);
682 screenDC.SetBrush(*wxTRANSPARENT_BRUSH);
683
684 screenDC.DrawLine(x1, y1, x2, y2);
685
686 screenDC.SetLogicalFunction(wxCOPY);
687
688 screenDC.SetPen(wxNullPen);
689 screenDC.SetBrush(wxNullBrush);
690}
691
d76ac8ed
VS
692void wxSplitterWindow::AdjustSashPosition(int &sashPos)
693{
694 int w, h;
695 GetClientSize(&w, &h);
696 int window_size = (m_splitMode == wxSPLIT_VERTICAL) ? w : h;
697 wxWindow *win;
698
699 if ( sashPos < m_minimumPaneSize )
700 sashPos = m_minimumPaneSize;
701 else if ( sashPos > window_size - m_minimumPaneSize )
702 sashPos = window_size - m_minimumPaneSize;
703
704 win = GetWindow1();
705 if ( win )
706 {
707 int minSize = (m_splitMode == wxSPLIT_VERTICAL) ?
708 win->GetMinWidth() : win->GetMinHeight();
709 if ( minSize != -1 && sashPos < minSize + GetBorderSize() )
710 sashPos = minSize + GetBorderSize();
711 }
712
713 win = GetWindow2();
714 if ( win )
715 {
716 int minSize = (m_splitMode == wxSPLIT_VERTICAL) ?
717 win->GetMinWidth() : win->GetMinHeight();
718 if ( minSize != -1 && sashPos > window_size - minSize - GetBorderSize() )
719 sashPos = window_size - minSize - GetBorderSize();
720 }
721}
722
c801d85f
KB
723// Position and size subwindows.
724// Note that the border size applies to each subwindow, not
725// including the edges next to the sash.
0d559d69 726void wxSplitterWindow::SizeWindows()
c801d85f
KB
727{
728 int w, h;
729 GetClientSize(&w, &h);
730
f6bcfd97 731 if ( GetWindow1() && !GetWindow2() )
c801d85f 732 {
f6bcfd97 733 GetWindow1()->SetSize(GetBorderSize(), GetBorderSize(), w - 2*GetBorderSize(), h - 2*GetBorderSize());
a6aa9b1e 734
c801d85f 735 }
f6bcfd97 736 else if ( GetWindow1() && GetWindow2() )
c801d85f 737 {
f6bcfd97 738 if (GetSplitMode() == wxSPLIT_VERTICAL)
c801d85f 739 {
f6bcfd97
BP
740 int x1 = GetBorderSize();
741 int y1 = GetBorderSize();
742 int w1 = GetSashPosition() - GetBorderSize();
743 int h1 = h - 2*GetBorderSize();
744
745 int x2 = GetSashPosition() + GetSashSize();
746 int y2 = GetBorderSize();
747 int w2 = w - 2*GetBorderSize() - GetSashSize() - w1;
748 int h2 = h - 2*GetBorderSize();
749
750 GetWindow1()->SetSize(x1, y1, w1, h1);
751 GetWindow2()->SetSize(x2, y2, w2, h2);
c801d85f
KB
752 }
753 else
754 {
f6bcfd97
BP
755 GetWindow1()->SetSize(GetBorderSize(), GetBorderSize(),
756 w - 2*GetBorderSize(), GetSashPosition() - GetBorderSize());
757 GetWindow2()->SetSize(GetBorderSize(), GetSashPosition() + GetSashSize(),
758 w - 2*GetBorderSize(), h - 2*GetBorderSize() - GetSashSize() - (GetSashPosition() - GetBorderSize()));
c801d85f
KB
759 }
760 }
761 wxClientDC dc(this);
f6bcfd97 762 if ( GetBorderSize() > 0 )
4419ba31 763 DrawBorders(dc);
c801d85f 764 DrawSash(dc);
00a32dc1 765
f6bcfd97 766 SetNeedUpdating(FALSE);
c801d85f
KB
767}
768
769// Set pane for unsplit window
770void wxSplitterWindow::Initialize(wxWindow *window)
771{
772 m_windowOne = window;
c67daf87 773 m_windowTwo = (wxWindow *) NULL;
c801d85f
KB
774 m_sashPosition = 0;
775}
776
777// Associates the given window with window 2, drawing the appropriate sash
778// and changing the split mode.
779// Does nothing and returns FALSE if the window is already split.
debe6624 780bool wxSplitterWindow::SplitVertically(wxWindow *window1, wxWindow *window2, int sashPosition)
c801d85f
KB
781{
782 if ( IsSplit() )
783 return FALSE;
784
0d559d69
VZ
785 int w, h;
786 GetClientSize(&w, &h);
787
c801d85f
KB
788 m_splitMode = wxSPLIT_VERTICAL;
789 m_windowOne = window1;
790 m_windowTwo = window2;
0d559d69 791 if ( sashPosition > 0 )
c801d85f 792 m_sashPosition = sashPosition;
0d559d69 793 else if ( sashPosition < 0 )
00a32dc1 794 m_sashPosition = w + sashPosition; // It's negative so adding is subtracting
0d559d69
VZ
795 else // default
796 m_sashPosition = w/2;
d76ac8ed
VS
797
798 AdjustSashPosition(m_sashPosition);
c801d85f
KB
799
800 SizeWindows();
801
802 return TRUE;
803}
804
debe6624 805bool wxSplitterWindow::SplitHorizontally(wxWindow *window1, wxWindow *window2, int sashPosition)
c801d85f
KB
806{
807 if ( IsSplit() )
808 return FALSE;
809
0d559d69
VZ
810 int w, h;
811 GetClientSize(&w, &h);
812
c801d85f
KB
813 m_splitMode = wxSPLIT_HORIZONTAL;
814 m_windowOne = window1;
815 m_windowTwo = window2;
0d559d69 816 if ( sashPosition > 0 )
c801d85f 817 m_sashPosition = sashPosition;
0d559d69 818 else if ( sashPosition < 0 )
00a32dc1 819 m_sashPosition = h + sashPosition; // It's negative so adding is subtracting
0d559d69
VZ
820 else // default
821 m_sashPosition = h/2;
c801d85f 822
d76ac8ed
VS
823 AdjustSashPosition(m_sashPosition);
824
c801d85f
KB
825 SizeWindows();
826
827 return TRUE;
828}
829
830
831// Remove the specified (or second) window from the view
832// Doesn't actually delete the window.
833bool wxSplitterWindow::Unsplit(wxWindow *toRemove)
834{
835 if ( ! IsSplit() )
836 return FALSE;
837
3ad5e06b 838 wxWindow *win = NULL;
c801d85f
KB
839 if ( toRemove == NULL || toRemove == m_windowTwo)
840 {
3ad5e06b 841 win = m_windowTwo ;
c67daf87 842 m_windowTwo = (wxWindow *) NULL;
c801d85f
KB
843 }
844 else if ( toRemove == m_windowOne )
845 {
3ad5e06b 846 win = m_windowOne ;
c801d85f 847 m_windowOne = m_windowTwo;
c67daf87 848 m_windowTwo = (wxWindow *) NULL;
c801d85f
KB
849 }
850 else
dbc208e9 851 {
223d09f6 852 wxFAIL_MSG(wxT("splitter: attempt to remove a non-existent window"));
dbc208e9 853
c801d85f 854 return FALSE;
dbc208e9 855 }
c801d85f 856
42e69d6b 857 SendUnsplitEvent(win);
3ad5e06b
VZ
858 m_sashPosition = 0;
859 SizeWindows();
860
861 return TRUE;
862}
863
864// Replace a window with another one
865bool wxSplitterWindow::ReplaceWindow(wxWindow *winOld, wxWindow *winNew)
866{
223d09f6
KB
867 wxCHECK_MSG( winOld, FALSE, wxT("use one of Split() functions instead") );
868 wxCHECK_MSG( winNew, FALSE, wxT("use Unsplit() functions instead") );
3ad5e06b
VZ
869
870 if ( winOld == m_windowTwo )
871 {
872 m_windowTwo = winNew;
873 }
874 else if ( winOld == m_windowOne )
875 {
876 m_windowOne = winNew;
877 }
878 else
879 {
223d09f6 880 wxFAIL_MSG(wxT("splitter: attempt to replace a non-existent window"));
3ad5e06b
VZ
881
882 return FALSE;
883 }
884
885 SizeWindows();
886
c801d85f
KB
887 return TRUE;
888}
889
debe6624 890void wxSplitterWindow::SetSashPosition(int position, bool redraw)
c801d85f
KB
891{
892 m_sashPosition = position;
d76ac8ed 893 AdjustSashPosition(m_sashPosition);
c801d85f
KB
894
895 if ( redraw )
896 {
897 SizeWindows();
898 }
899}
900
c801d85f 901// Initialize colours
0d559d69 902void wxSplitterWindow::InitColours()
c801d85f 903{
0d559d69
VZ
904 wxDELETE( m_facePen );
905 wxDELETE( m_faceBrush );
906 wxDELETE( m_mediumShadowPen );
907 wxDELETE( m_darkShadowPen );
908 wxDELETE( m_lightShadowPen );
909 wxDELETE( m_hilightPen );
c801d85f
KB
910
911 // Shadow colours
37d403aa 912#ifndef __WIN16__
a756f210 913 wxColour faceColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
c801d85f
KB
914 m_facePen = new wxPen(faceColour, 1, wxSOLID);
915 m_faceBrush = new wxBrush(faceColour, wxSOLID);
916
a756f210 917 wxColour mediumShadowColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW));
c801d85f
KB
918 m_mediumShadowPen = new wxPen(mediumShadowColour, 1, wxSOLID);
919
a756f210 920 wxColour darkShadowColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW));
c801d85f
KB
921 m_darkShadowPen = new wxPen(darkShadowColour, 1, wxSOLID);
922
a756f210 923 wxColour lightShadowColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT));
c801d85f
KB
924 m_lightShadowPen = new wxPen(lightShadowColour, 1, wxSOLID);
925
a756f210 926 wxColour hilightColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DHILIGHT));
c801d85f 927 m_hilightPen = new wxPen(hilightColour, 1, wxSOLID);
37d403aa 928#else
c801d85f
KB
929 m_facePen = new wxPen("LIGHT GREY", 1, wxSOLID);
930 m_faceBrush = new wxBrush("LIGHT GREY", wxSOLID);
931 m_mediumShadowPen = new wxPen("GREY", 1, wxSOLID);
932 m_darkShadowPen = new wxPen("BLACK", 1, wxSOLID);
933 m_lightShadowPen = new wxPen("LIGHT GREY", 1, wxSOLID);
934 m_hilightPen = new wxPen("WHITE", 1, wxSOLID);
37d403aa 935#endif // __WIN16__
c801d85f
KB
936}
937
42e69d6b
VZ
938void wxSplitterWindow::SendUnsplitEvent(wxWindow *winRemoved)
939{
940 wxSplitterEvent event(wxEVT_COMMAND_SPLITTER_UNSPLIT, this);
941 event.m_data.win = winRemoved;
942
943 (void)GetEventHandler()->ProcessEvent(event);
944}
945
946// ---------------------------------------------------------------------------
947// splitter event handlers
948// ---------------------------------------------------------------------------
949
950void wxSplitterWindow::OnSashPosChanged(wxSplitterEvent& event)
951{
952 // If within UNSPLIT_THRESHOLD from edge, set to edge to cause closure.
953 const int UNSPLIT_THRESHOLD = 4;
954
955 int newSashPosition = event.GetSashPosition();
956
957 // Obtain relevant window dimension for bottom / right threshold check
958 int w, h;
959 GetClientSize(&w, &h);
960 int window_size = (m_splitMode == wxSPLIT_VERTICAL) ? w : h ;
961
370938d9
UB
962 bool unsplit_scenario = FALSE;
963 if ( m_permitUnsplitAlways
964 || m_minimumPaneSize == 0 )
bc79aa6b 965 {
370938d9
UB
966 // Do edge detection if unsplit premitted
967 if ( newSashPosition <= UNSPLIT_THRESHOLD )
968 {
969 // threshold top / left check
970 newSashPosition = 0;
971 unsplit_scenario = TRUE;
972 }
973 if ( newSashPosition >= window_size - UNSPLIT_THRESHOLD )
974 {
975 // threshold bottom/right check
976 newSashPosition = window_size;
977 unsplit_scenario = TRUE;
978 }
bc79aa6b
UB
979 }
980
370938d9 981 if ( !unsplit_scenario )
bc79aa6b
UB
982 {
983 // If resultant pane would be too small, enlarge it
d76ac8ed 984 AdjustSashPosition(newSashPosition);
bc79aa6b 985 }
42e69d6b
VZ
986
987 // If the result is out of bounds it means minimum size is too big,
988 // so split window in half as best compromise.
989 if ( newSashPosition < 0 || newSashPosition > window_size )
990 newSashPosition = window_size / 2;
991
992 // for compatibility, call the virtual function
993 if ( !OnSashPositionChange(newSashPosition) )
994 {
995 newSashPosition = -1;
996 }
997
998 event.SetSashPosition(newSashPosition);
999}
1000
1001// Called when the sash is double-clicked. The default behaviour is to remove
1002// the sash if the minimum pane size is zero.
1003void wxSplitterWindow::OnDoubleClick(wxSplitterEvent& event)
1004{
1005 // for compatibility, call the virtual function
1006 OnDoubleClickSash(event.GetX(), event.GetY());
1007
4419ba31 1008 if ( GetMinimumPaneSize() == 0 || m_permitUnsplitAlways )
42e69d6b
VZ
1009 {
1010 Unsplit();
1011 }
1012}
1013
1014void wxSplitterWindow::OnUnsplitEvent(wxSplitterEvent& event)
1015{
1016 wxWindow *win = event.GetWindowBeingRemoved();
1017
4419ba31
VZ
1018 // do it before calling OnUnsplit() which may delete the window
1019 win->Show(FALSE);
1020
42e69d6b
VZ
1021 // for compatibility, call the virtual function
1022 OnUnsplit(win);
42e69d6b 1023}
43b5058d 1024
bc1dcfc1
VZ
1025#if defined(__WXMSW__)
1026 #define WXUNUSED_UNLESS_MSW(identifier) identifier
1027#else
1028 #define WXUNUSED_UNLESS_MSW(identifier) WXUNUSED(identifier)
1029#endif
1030
1031void wxSplitterWindow::OnSetCursor(wxSetCursorEvent& WXUNUSED_UNLESS_MSW(event))
43b5058d
VZ
1032{
1033 // this is currently called (and needed) under MSW only...
1034#ifdef __WXMSW__
1035
1036 // if we don't do it, the resizing cursor might be set for child window:
1037 // and like this we explicitly say that our cursor should not be used for
1038 // children windows which overlap us
1039
1040 if ( SashHitTest(event.GetX(), event.GetY()) )
1041 {
1042 // default processing is ok
1043 event.Skip();
1044 }
1045 //else: do nothing, in particular, don't call Skip()
1046#endif // wxMSW
1047}