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