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