]>
Commit | Line | Data |
---|---|---|
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$ | |
6aa89a22 | 8 | // Copyright: (c) Julian Smart |
65571936 | 9 | // Licence: wxWindows licence |
c801d85f KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
14f355c2 | 12 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
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 | ||
d7260478 | 19 | #if wxUSE_SPLITTER |
b3208e11 | 20 | |
c801d85f | 21 | #ifdef __BORLANDC__ |
f4621a09 | 22 | #pragma hdrstop |
c801d85f KB |
23 | #endif |
24 | ||
25 | #ifndef WX_PRECOMP | |
2b5f62a0 VZ |
26 | #include "wx/string.h" |
27 | #include "wx/utils.h" | |
28 | #include "wx/log.h" | |
29 | ||
30 | #include "wx/dcscreen.h" | |
31 | ||
2f073eb2 RR |
32 | #include "wx/window.h" |
33 | #include "wx/dialog.h" | |
34 | #include "wx/frame.h" | |
c801d85f | 35 | |
2b5f62a0 VZ |
36 | #include "wx/settings.h" |
37 | #endif | |
c801d85f | 38 | |
7a96ca3a SC |
39 | #ifdef __WXMAC__ |
40 | #include "wx/mac/private.h" | |
41 | #endif | |
42 | ||
b3208e11 VZ |
43 | #include "wx/renderer.h" |
44 | ||
c801d85f | 45 | #include "wx/splitter.h" |
2b5f62a0 VZ |
46 | |
47 | #include <stdlib.h> | |
c801d85f | 48 | |
2e4df4bf VZ |
49 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED) |
50 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING) | |
51 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPLITTER_DOUBLECLICKED) | |
52 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPLITTER_UNSPLIT) | |
53 | ||
c801d85f | 54 | IMPLEMENT_DYNAMIC_CLASS(wxSplitterWindow, wxWindow) |
066f1b7a SC |
55 | |
56 | /* | |
ca65c044 WS |
57 | TODO PROPERTIES |
58 | style wxSP_3D | |
59 | sashpos (long , 0 ) | |
60 | minsize (long -1 ) | |
61 | object, object_ref | |
62 | orientation | |
066f1b7a SC |
63 | */ |
64 | ||
3e58dcb9 | 65 | IMPLEMENT_DYNAMIC_CLASS(wxSplitterEvent, wxNotifyEvent) |
c801d85f KB |
66 | |
67 | BEGIN_EVENT_TABLE(wxSplitterWindow, wxWindow) | |
68 | EVT_PAINT(wxSplitterWindow::OnPaint) | |
69 | EVT_SIZE(wxSplitterWindow::OnSize) | |
70 | EVT_MOUSE_EVENTS(wxSplitterWindow::OnMouseEvent) | |
42e69d6b | 71 | |
64407854 | 72 | #if defined( __WXMSW__ ) || defined( __WXMAC__) |
43b5058d | 73 | EVT_SET_CURSOR(wxSplitterWindow::OnSetCursor) |
3e58dcb9 | 74 | #endif // wxMSW |
6b55490a VZ |
75 | |
76 | WX_EVENT_TABLE_CONTROL_CONTAINER(wxSplitterWindow) | |
c801d85f | 77 | END_EVENT_TABLE() |
c801d85f | 78 | |
6b55490a VZ |
79 | WX_DELEGATE_TO_CONTROL_CONTAINER(wxSplitterWindow); |
80 | ||
f6bcfd97 | 81 | bool wxSplitterWindow::Create(wxWindow *parent, wxWindowID id, |
0d559d69 VZ |
82 | const wxPoint& pos, |
83 | const wxSize& size, | |
84 | long style, | |
85 | const wxString& name) | |
c801d85f | 86 | { |
6b55490a VZ |
87 | // allow TABbing from one window to the other |
88 | style |= wxTAB_TRAVERSAL; | |
89 | ||
b3208e11 VZ |
90 | // we draw our border ourselves to blend the sash with it |
91 | style &= ~wxBORDER_MASK; | |
92 | style |= wxBORDER_NONE; | |
f6bcfd97 | 93 | |
b3208e11 | 94 | if ( !wxWindow::Create(parent, id, pos, size, style, name) ) |
4395fb21 | 95 | return false; |
2e8cc3e8 | 96 | |
b3208e11 | 97 | m_permitUnsplitAlways = (style & wxSP_PERMIT_UNSPLIT) != 0; |
f6bcfd97 | 98 | |
658f53c7 VZ |
99 | // don't erase the splitter background, it's pointless as we overwrite it |
100 | // anyhow | |
101 | SetBackgroundStyle(wxBG_STYLE_CUSTOM); | |
102 | ||
4395fb21 | 103 | return true; |
f6bcfd97 BP |
104 | } |
105 | ||
106 | void wxSplitterWindow::Init() | |
107 | { | |
9948d31f VZ |
108 | m_container.SetContainerWindow(this); |
109 | ||
f6bcfd97 | 110 | m_splitMode = wxSPLIT_VERTICAL; |
4395fb21 | 111 | m_permitUnsplitAlways = true; |
c67daf87 UR |
112 | m_windowOne = (wxWindow *) NULL; |
113 | m_windowTwo = (wxWindow *) NULL; | |
c801d85f KB |
114 | m_dragMode = wxSPLIT_DRAG_NONE; |
115 | m_oldX = 0; | |
116 | m_oldY = 0; | |
117 | m_firstX = 0; | |
118 | m_firstY = 0; | |
ca39e409 | 119 | m_sashPosition = m_requestedSashPosition = 0; |
14b4c0ff VZ |
120 | m_sashGravity = 0.0; |
121 | m_lastSize = wxSize(0,0); | |
4395fb21 | 122 | m_checkRequestedSashPosition = false; |
c801d85f | 123 | m_minimumPaneSize = 0; |
153b7996 VZ |
124 | m_sashCursorWE = wxCursor(wxCURSOR_SIZEWE); |
125 | m_sashCursorNS = wxCursor(wxCURSOR_SIZENS); | |
c801d85f | 126 | m_sashTrackerPen = new wxPen(*wxBLACK, 2, wxSOLID); |
c801d85f | 127 | |
4395fb21 | 128 | m_needUpdating = false; |
af99040c | 129 | m_isHot = false; |
c801d85f KB |
130 | } |
131 | ||
0d559d69 | 132 | wxSplitterWindow::~wxSplitterWindow() |
c801d85f | 133 | { |
c801d85f | 134 | delete m_sashTrackerPen; |
c801d85f KB |
135 | } |
136 | ||
af99040c VZ |
137 | // ---------------------------------------------------------------------------- |
138 | // entering/leaving sash | |
139 | // ---------------------------------------------------------------------------- | |
140 | ||
141 | void wxSplitterWindow::RedrawIfHotSensitive(bool isHot) | |
142 | { | |
143 | if ( wxRendererNative::Get().GetSplitterParams(this).isHotSensitive ) | |
144 | { | |
145 | m_isHot = isHot; | |
146 | ||
147 | wxClientDC dc(this); | |
148 | DrawSash(dc); | |
149 | } | |
150 | //else: we don't change our appearance, don't redraw to avoid flicker | |
151 | } | |
152 | ||
153 | void wxSplitterWindow::OnEnterSash() | |
154 | { | |
155 | SetResizeCursor(); | |
156 | ||
157 | RedrawIfHotSensitive(true); | |
158 | } | |
159 | ||
160 | void wxSplitterWindow::OnLeaveSash() | |
161 | { | |
162 | SetCursor(*wxSTANDARD_CURSOR); | |
163 | ||
164 | RedrawIfHotSensitive(false); | |
165 | } | |
166 | ||
153b7996 VZ |
167 | void wxSplitterWindow::SetResizeCursor() |
168 | { | |
169 | SetCursor(m_splitMode == wxSPLIT_VERTICAL ? m_sashCursorWE | |
170 | : m_sashCursorNS); | |
171 | } | |
172 | ||
af99040c VZ |
173 | // ---------------------------------------------------------------------------- |
174 | // other event handlers | |
175 | // ---------------------------------------------------------------------------- | |
176 | ||
c801d85f KB |
177 | void wxSplitterWindow::OnPaint(wxPaintEvent& WXUNUSED(event)) |
178 | { | |
179 | wxPaintDC dc(this); | |
180 | ||
c801d85f KB |
181 | DrawSash(dc); |
182 | } | |
183 | ||
5180055b | 184 | void wxSplitterWindow::OnInternalIdle() |
72195a0f | 185 | { |
5180055b | 186 | wxWindow::OnInternalIdle(); |
4395fb21 RD |
187 | |
188 | // if this is the first idle time after a sash position has potentially | |
189 | // been set, allow SizeWindows to check for a requested size. | |
190 | if (!m_checkRequestedSashPosition) | |
191 | { | |
192 | m_checkRequestedSashPosition = true; | |
193 | SizeWindows(); | |
194 | return; // it won't needUpdating in this case | |
195 | } | |
ca65c044 | 196 | |
72195a0f RR |
197 | if (m_needUpdating) |
198 | SizeWindows(); | |
199 | } | |
4c013092 | 200 | |
c801d85f KB |
201 | void wxSplitterWindow::OnMouseEvent(wxMouseEvent& event) |
202 | { | |
2e8cc3e8 VZ |
203 | int x = (int)event.GetX(), |
204 | y = (int)event.GetY(); | |
c801d85f | 205 | |
f6bcfd97 BP |
206 | if (GetWindowStyle() & wxSP_NOSASH) |
207 | return; | |
58d1c1ae | 208 | |
153b7996 VZ |
209 | // with wxSP_LIVE_UPDATE style the splitter windows are always resized |
210 | // following the mouse movement while it drags the sash, without it we only | |
211 | // draw the sash at the new position but only resize the windows when the | |
212 | // dragging is finished | |
d394825d | 213 | #if defined( __WXMAC__ ) && TARGET_API_MAC_OSX == 1 |
a20d7ea2 SC |
214 | bool isLive = true ; |
215 | #else | |
153b7996 | 216 | bool isLive = (GetWindowStyleFlag() & wxSP_LIVE_UPDATE) != 0; |
a20d7ea2 | 217 | #endif |
0d559d69 VZ |
218 | if (event.LeftDown()) |
219 | { | |
c801d85f KB |
220 | if ( SashHitTest(x, y) ) |
221 | { | |
84e7741a | 222 | // Start the drag now |
4a33eba6 | 223 | m_dragMode = wxSPLIT_DRAG_DRAGGING; |
ca65c044 | 224 | |
84e7741a RR |
225 | // Capture mouse and set the cursor |
226 | CaptureMouse(); | |
227 | SetResizeCursor(); | |
f4621a09 | 228 | |
153b7996 | 229 | if ( !isLive ) |
4419ba31 | 230 | { |
153b7996 VZ |
231 | // remember the initial sash position and draw the initial |
232 | // shadow sash | |
233 | m_sashPositionCurrent = m_sashPosition; | |
234 | ||
4419ba31 VZ |
235 | DrawSashTracker(x, y); |
236 | } | |
a6aa9b1e | 237 | |
4a33eba6 RR |
238 | m_oldX = x; |
239 | m_oldY = y; | |
9f334bea | 240 | |
153b7996 | 241 | SetResizeCursor(); |
f4621a09 | 242 | return; |
c801d85f | 243 | } |
0d559d69 | 244 | } |
0d559d69 VZ |
245 | else if (event.LeftUp() && m_dragMode == wxSPLIT_DRAG_DRAGGING) |
246 | { | |
c801d85f KB |
247 | // We can stop dragging now and see what we've got. |
248 | m_dragMode = wxSPLIT_DRAG_NONE; | |
ca65c044 | 249 | |
84e7741a | 250 | // Release mouse and unset the cursor |
0d559d69 | 251 | ReleaseMouse(); |
84e7741a | 252 | SetCursor(* wxSTANDARD_CURSOR); |
dbc208e9 | 253 | |
2b5f62a0 VZ |
254 | // exit if unsplit after doubleclick |
255 | if ( !IsSplit() ) | |
256 | { | |
257 | return; | |
258 | } | |
259 | ||
c801d85f | 260 | // Erase old tracker |
153b7996 | 261 | if ( !isLive ) |
4419ba31 | 262 | { |
72195a0f | 263 | DrawSashTracker(m_oldX, m_oldY); |
4419ba31 | 264 | } |
c801d85f | 265 | |
3e58dcb9 VZ |
266 | // the position of the click doesn't exactly correspond to |
267 | // m_sashPosition, rather it changes it by the distance by which the | |
268 | // mouse has moved | |
269 | int diff = m_splitMode == wxSPLIT_VERTICAL ? x - m_oldX : y - m_oldY; | |
4c013092 | 270 | |
153b7996 VZ |
271 | int posSashOld = isLive ? m_sashPosition : m_sashPositionCurrent; |
272 | int posSashNew = OnSashPositionChanging(posSashOld + diff); | |
3e58dcb9 | 273 | if ( posSashNew == -1 ) |
42e69d6b | 274 | { |
3e58dcb9 VZ |
275 | // change not allowed |
276 | return; | |
42e69d6b | 277 | } |
4c013092 | 278 | |
43b5058d | 279 | if ( m_permitUnsplitAlways || m_minimumPaneSize == 0 ) |
4c013092 | 280 | { |
370938d9 | 281 | // Deal with possible unsplit scenarios |
3e58dcb9 | 282 | if ( posSashNew == 0 ) |
370938d9 UB |
283 | { |
284 | // We remove the first window from the view | |
285 | wxWindow *removedWindow = m_windowOne; | |
286 | m_windowOne = m_windowTwo; | |
287 | m_windowTwo = (wxWindow *) NULL; | |
3e58dcb9 | 288 | OnUnsplit(removedWindow); |
0e4cfcdd JS |
289 | wxSplitterEvent event(wxEVT_COMMAND_SPLITTER_UNSPLIT, this); |
290 | event.m_data.win = removedWindow; | |
291 | (void)DoSendEvent(event); | |
63ec9dbb | 292 | SetSashPositionAndNotify(0); |
370938d9 | 293 | } |
3e58dcb9 | 294 | else if ( posSashNew == GetWindowSize() ) |
370938d9 UB |
295 | { |
296 | // We remove the second window from the view | |
297 | wxWindow *removedWindow = m_windowTwo; | |
298 | m_windowTwo = (wxWindow *) NULL; | |
3e58dcb9 | 299 | OnUnsplit(removedWindow); |
0e4cfcdd JS |
300 | wxSplitterEvent event(wxEVT_COMMAND_SPLITTER_UNSPLIT, this); |
301 | event.m_data.win = removedWindow; | |
302 | (void)DoSendEvent(event); | |
63ec9dbb | 303 | SetSashPositionAndNotify(0); |
370938d9 UB |
304 | } |
305 | else | |
306 | { | |
63ec9dbb | 307 | SetSashPositionAndNotify(posSashNew); |
370938d9 | 308 | } |
c801d85f | 309 | } |
4c013092 | 310 | else |
c801d85f | 311 | { |
63ec9dbb | 312 | SetSashPositionAndNotify(posSashNew); |
4c013092 | 313 | } |
42e69d6b | 314 | |
c801d85f | 315 | SizeWindows(); |
4a33eba6 | 316 | } // left up && dragging |
21b07f95 | 317 | else if ((event.Moving() || event.Leaving() || event.Entering()) && (m_dragMode == wxSPLIT_DRAG_NONE)) |
0d559d69 | 318 | { |
af99040c VZ |
319 | if ( event.Leaving() || !SashHitTest(x, y) ) |
320 | OnLeaveSash(); | |
58d1c1ae | 321 | else |
af99040c | 322 | OnEnterSash(); |
0d559d69 | 323 | } |
a6aa9b1e | 324 | else if (event.Dragging() && (m_dragMode == wxSPLIT_DRAG_DRAGGING)) |
0d559d69 | 325 | { |
3e58dcb9 | 326 | int diff = m_splitMode == wxSPLIT_VERTICAL ? x - m_oldX : y - m_oldY; |
8dcfd2f9 VZ |
327 | if ( !diff ) |
328 | { | |
329 | // nothing to do, mouse didn't really move far enough | |
330 | return; | |
331 | } | |
370938d9 | 332 | |
153b7996 VZ |
333 | int posSashOld = isLive ? m_sashPosition : m_sashPositionCurrent; |
334 | int posSashNew = OnSashPositionChanging(posSashOld + diff); | |
3e58dcb9 | 335 | if ( posSashNew == -1 ) |
370938d9 | 336 | { |
3e58dcb9 VZ |
337 | // change not allowed |
338 | return; | |
370938d9 | 339 | } |
00a32dc1 | 340 | |
3e58dcb9 | 341 | if ( posSashNew == m_sashPosition ) |
7c1122c4 | 342 | return; |
370938d9 | 343 | |
4a33eba6 | 344 | // Erase old tracker |
153b7996 | 345 | if ( !isLive ) |
4419ba31 | 346 | { |
72195a0f | 347 | DrawSashTracker(m_oldX, m_oldY); |
4419ba31 | 348 | } |
c801d85f | 349 | |
370938d9 | 350 | if (m_splitMode == wxSPLIT_VERTICAL) |
3e58dcb9 | 351 | x = posSashNew; |
370938d9 | 352 | else |
3e58dcb9 | 353 | y = posSashNew; |
370938d9 | 354 | |
7c1122c4 RR |
355 | // Remember old positions |
356 | m_oldX = x; | |
357 | m_oldY = y; | |
370938d9 | 358 | |
d66a042c VZ |
359 | #ifdef __WXMSW__ |
360 | // As we captured the mouse, we may get the mouse events from outside | |
361 | // our window - for example, negative values in x, y. This has a weird | |
362 | // consequence under MSW where we use unsigned values sometimes and | |
363 | // signed ones other times: the coordinates turn as big positive | |
364 | // numbers and so the sash is drawn on the *right* side of the window | |
365 | // instead of the left (or bottom instead of top). Correct this. | |
d66a042c VZ |
366 | if ( (short)m_oldX < 0 ) |
367 | m_oldX = 0; | |
368 | if ( (short)m_oldY < 0 ) | |
369 | m_oldY = 0; | |
370 | #endif // __WXMSW__ | |
371 | ||
372 | // Draw new one | |
153b7996 | 373 | if ( !isLive ) |
4419ba31 | 374 | { |
153b7996 VZ |
375 | m_sashPositionCurrent = posSashNew; |
376 | ||
72195a0f | 377 | DrawSashTracker(m_oldX, m_oldY); |
4419ba31 VZ |
378 | } |
379 | else | |
380 | { | |
63ec9dbb | 381 | SetSashPositionAndNotify(posSashNew); |
4395fb21 | 382 | m_needUpdating = true; |
4419ba31 | 383 | } |
0d559d69 | 384 | } |
a2f9a636 | 385 | else if ( event.LeftDClick() && m_windowTwo ) |
c801d85f | 386 | { |
3e58dcb9 | 387 | OnDoubleClickSash(x, y); |
c801d85f | 388 | } |
c801d85f KB |
389 | } |
390 | ||
a8731351 | 391 | void wxSplitterWindow::OnSize(wxSizeEvent& event) |
c801d85f | 392 | { |
a8731351 VZ |
393 | // only process this message if we're not iconized - otherwise iconizing |
394 | // and restoring a window containing the splitter has a funny side effect | |
395 | // of changing the splitter position! | |
4395fb21 | 396 | wxWindow *parent = wxGetTopLevelParent(this); |
999836aa | 397 | bool iconized; |
2e8cc3e8 VZ |
398 | |
399 | wxTopLevelWindow *winTop = wxDynamicCast(parent, wxTopLevelWindow); | |
400 | if ( winTop ) | |
401 | { | |
402 | iconized = winTop->IsIconized(); | |
403 | } | |
a8731351 VZ |
404 | else |
405 | { | |
2e8cc3e8 VZ |
406 | wxFAIL_MSG(wxT("should have a top level parent!")); |
407 | ||
4395fb21 | 408 | iconized = false; |
a8731351 | 409 | } |
63ec9dbb | 410 | |
a8731351 VZ |
411 | if ( iconized ) |
412 | { | |
14b4c0ff VZ |
413 | m_lastSize = wxSize(0,0); |
414 | ||
a8731351 | 415 | event.Skip(); |
2e8cc3e8 VZ |
416 | |
417 | return; | |
a8731351 | 418 | } |
2e8cc3e8 | 419 | |
2e8cc3e8 | 420 | if ( m_windowTwo ) |
a8731351 | 421 | { |
b3208e11 VZ |
422 | int w, h; |
423 | GetClientSize(&w, &h); | |
424 | ||
425 | int size = m_splitMode == wxSPLIT_VERTICAL ? w : h; | |
14b4c0ff VZ |
426 | |
427 | int old_size = m_splitMode == wxSPLIT_VERTICAL ? m_lastSize.x : m_lastSize.y; | |
428 | if ( old_size != 0 ) | |
429 | { | |
430 | int delta = (int) ( (size - old_size)*m_sashGravity ); | |
431 | if ( delta != 0 ) | |
432 | { | |
433 | int newPosition = m_sashPosition + delta; | |
434 | if( newPosition < m_minimumPaneSize ) | |
435 | newPosition = m_minimumPaneSize; | |
436 | SetSashPositionAndNotify(newPosition); | |
437 | } | |
438 | } | |
439 | ||
b3208e11 VZ |
440 | if ( m_sashPosition >= size - 5 ) |
441 | SetSashPositionAndNotify(wxMax(10, size - 40)); | |
14b4c0ff | 442 | m_lastSize = wxSize(w,h); |
c801d85f | 443 | } |
2e8cc3e8 VZ |
444 | |
445 | SizeWindows(); | |
c801d85f KB |
446 | } |
447 | ||
14b4c0ff VZ |
448 | void wxSplitterWindow::SetSashGravity(double gravity) |
449 | { | |
450 | wxCHECK_RET( gravity >= 0. && gravity <= 1., | |
451 | _T("invalid gravity value") ); | |
452 | ||
453 | m_sashGravity = gravity; | |
454 | } | |
455 | ||
debe6624 | 456 | bool wxSplitterWindow::SashHitTest(int x, int y, int tolerance) |
c801d85f KB |
457 | { |
458 | if ( m_windowTwo == NULL || m_sashPosition == 0) | |
4395fb21 | 459 | return false; // No sash |
c801d85f | 460 | |
b3208e11 | 461 | int z = m_splitMode == wxSPLIT_VERTICAL ? x : y; |
4395fb21 RD |
462 | int hitMin = m_sashPosition - tolerance; |
463 | int hitMax = m_sashPosition + GetSashSize() + tolerance; | |
ca65c044 | 464 | |
4395fb21 | 465 | return z >= hitMin && z <= hitMax; |
c801d85f KB |
466 | } |
467 | ||
b3208e11 | 468 | int wxSplitterWindow::GetSashSize() const |
c801d85f | 469 | { |
af99040c | 470 | return wxRendererNative::Get().GetSplitterParams(this).widthSash; |
b3208e11 | 471 | } |
c801d85f | 472 | |
b3208e11 VZ |
473 | int wxSplitterWindow::GetBorderSize() const |
474 | { | |
af99040c | 475 | return wxRendererNative::Get().GetSplitterParams(this).border; |
c801d85f KB |
476 | } |
477 | ||
478 | // Draw the sash | |
479 | void wxSplitterWindow::DrawSash(wxDC& dc) | |
480 | { | |
3255bce3 JS |
481 | if (HasFlag(wxSP_3DBORDER)) |
482 | wxRendererNative::Get().DrawSplitterBorder | |
b3208e11 VZ |
483 | ( |
484 | this, | |
485 | dc, | |
486 | GetClientRect() | |
487 | ); | |
488 | ||
489 | // don't draw sash if we're not split | |
490 | if ( m_sashPosition == 0 || !m_windowTwo ) | |
c801d85f | 491 | return; |
c801d85f | 492 | |
b3208e11 VZ |
493 | // nor if we're configured to not show it |
494 | if ( HasFlag(wxSP_NOSASH) ) | |
495 | return; | |
c801d85f | 496 | |
b3208e11 VZ |
497 | wxRendererNative::Get().DrawSplitterSash |
498 | ( | |
499 | this, | |
62dc9cb4 VZ |
500 | dc, |
501 | GetClientSize(), | |
502 | m_sashPosition, | |
af99040c VZ |
503 | m_splitMode == wxSPLIT_VERTICAL ? wxVERTICAL |
504 | : wxHORIZONTAL, | |
505 | m_isHot ? wxCONTROL_CURRENT : 0 | |
b3208e11 | 506 | ); |
c801d85f KB |
507 | } |
508 | ||
509 | // Draw the sash tracker (for whilst moving the sash) | |
debe6624 | 510 | void wxSplitterWindow::DrawSashTracker(int x, int y) |
c801d85f KB |
511 | { |
512 | int w, h; | |
513 | GetClientSize(&w, &h); | |
514 | ||
515 | wxScreenDC screenDC; | |
516 | int x1, y1; | |
517 | int x2, y2; | |
518 | ||
519 | if ( m_splitMode == wxSPLIT_VERTICAL ) | |
520 | { | |
521 | x1 = x; y1 = 2; | |
522 | x2 = x; y2 = h-2; | |
523 | ||
524 | if ( x1 > w ) | |
525 | { | |
526 | x1 = w; x2 = w; | |
527 | } | |
528 | else if ( x1 < 0 ) | |
529 | { | |
530 | x1 = 0; x2 = 0; | |
531 | } | |
532 | } | |
533 | else | |
534 | { | |
535 | x1 = 2; y1 = y; | |
536 | x2 = w-2; y2 = y; | |
537 | ||
538 | if ( y1 > h ) | |
539 | { | |
540 | y1 = h; | |
541 | y2 = h; | |
542 | } | |
543 | else if ( y1 < 0 ) | |
544 | { | |
545 | y1 = 0; | |
546 | y2 = 0; | |
547 | } | |
548 | } | |
549 | ||
550 | ClientToScreen(&x1, &y1); | |
551 | ClientToScreen(&x2, &y2); | |
552 | ||
3c679789 | 553 | screenDC.SetLogicalFunction(wxINVERT); |
c801d85f KB |
554 | screenDC.SetPen(*m_sashTrackerPen); |
555 | screenDC.SetBrush(*wxTRANSPARENT_BRUSH); | |
556 | ||
557 | screenDC.DrawLine(x1, y1, x2, y2); | |
558 | ||
559 | screenDC.SetLogicalFunction(wxCOPY); | |
c801d85f KB |
560 | } |
561 | ||
2e8cc3e8 | 562 | int wxSplitterWindow::GetWindowSize() const |
d76ac8ed | 563 | { |
2e8cc3e8 VZ |
564 | wxSize size = GetClientSize(); |
565 | ||
566 | return m_splitMode == wxSPLIT_VERTICAL ? size.x : size.y; | |
567 | } | |
568 | ||
569 | int wxSplitterWindow::AdjustSashPosition(int sashPos) const | |
570 | { | |
571 | int window_size = GetWindowSize(); | |
572 | ||
d76ac8ed VS |
573 | wxWindow *win; |
574 | ||
d76ac8ed VS |
575 | win = GetWindow1(); |
576 | if ( win ) | |
577 | { | |
2e8cc3e8 VZ |
578 | // the window shouldn't be smaller than its own minimal size nor |
579 | // smaller than the minimual pane size specified for this splitter | |
580 | int minSize = m_splitMode == wxSPLIT_VERTICAL ? win->GetMinWidth() | |
581 | : win->GetMinHeight(); | |
582 | ||
583 | if ( minSize == -1 || m_minimumPaneSize > minSize ) | |
584 | minSize = m_minimumPaneSize; | |
585 | ||
586 | minSize += GetBorderSize(); | |
587 | ||
588 | if ( sashPos < minSize ) | |
589 | sashPos = minSize; | |
d76ac8ed VS |
590 | } |
591 | ||
592 | win = GetWindow2(); | |
593 | if ( win ) | |
594 | { | |
2e8cc3e8 VZ |
595 | int minSize = m_splitMode == wxSPLIT_VERTICAL ? win->GetMinWidth() |
596 | : win->GetMinHeight(); | |
597 | ||
598 | if ( minSize == -1 || m_minimumPaneSize > minSize ) | |
599 | minSize = m_minimumPaneSize; | |
600 | ||
4d89e13e | 601 | int maxSize = window_size - minSize - GetBorderSize() - GetSashSize(); |
2e8cc3e8 VZ |
602 | if ( sashPos > maxSize ) |
603 | sashPos = maxSize; | |
d76ac8ed | 604 | } |
2e8cc3e8 VZ |
605 | |
606 | return sashPos; | |
d76ac8ed VS |
607 | } |
608 | ||
63ec9dbb | 609 | bool wxSplitterWindow::DoSetSashPosition(int sashPos) |
ca39e409 | 610 | { |
74c57d1f | 611 | int newSashPosition = AdjustSashPosition(sashPos); |
3e58dcb9 | 612 | |
63ec9dbb | 613 | if ( newSashPosition == m_sashPosition ) |
4395fb21 | 614 | return false; |
74c57d1f | 615 | |
63ec9dbb VZ |
616 | m_sashPosition = newSashPosition; |
617 | ||
4395fb21 | 618 | return true; |
63ec9dbb VZ |
619 | } |
620 | ||
621 | void wxSplitterWindow::SetSashPositionAndNotify(int sashPos) | |
622 | { | |
e1366cdb VS |
623 | // we must reset the request here, otherwise the sash would be stuck at |
624 | // old position if the user attempted to move the sash after invalid | |
14b4c0ff | 625 | // (e.g. smaller than minsize) sash position was requested using |
e1366cdb VS |
626 | // SetSashPosition(): |
627 | m_requestedSashPosition = INT_MAX; | |
628 | ||
63ec9dbb VZ |
629 | if ( DoSetSashPosition(sashPos) ) |
630 | { | |
74c57d1f VZ |
631 | wxSplitterEvent event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED, this); |
632 | event.m_data.pos = m_sashPosition; | |
3e58dcb9 | 633 | |
74c57d1f VZ |
634 | (void)DoSendEvent(event); |
635 | } | |
ca39e409 VS |
636 | } |
637 | ||
c801d85f KB |
638 | // Position and size subwindows. |
639 | // Note that the border size applies to each subwindow, not | |
640 | // including the edges next to the sash. | |
0d559d69 | 641 | void wxSplitterWindow::SizeWindows() |
c801d85f | 642 | { |
74c57d1f | 643 | // check if we have delayed setting the real sash position |
4395fb21 | 644 | if ( m_checkRequestedSashPosition && m_requestedSashPosition != INT_MAX ) |
74c57d1f VZ |
645 | { |
646 | int newSashPosition = ConvertSashPosition(m_requestedSashPosition); | |
647 | if ( newSashPosition != m_sashPosition ) | |
648 | { | |
649 | DoSetSashPosition(newSashPosition); | |
650 | } | |
651 | ||
2b5f62a0 VZ |
652 | if ( newSashPosition <= m_sashPosition |
653 | && newSashPosition >= m_sashPosition - GetBorderSize() ) | |
74c57d1f VZ |
654 | { |
655 | // don't update it any more | |
656 | m_requestedSashPosition = INT_MAX; | |
657 | } | |
658 | } | |
ca39e409 | 659 | |
c801d85f KB |
660 | int w, h; |
661 | GetClientSize(&w, &h); | |
662 | ||
f6bcfd97 | 663 | if ( GetWindow1() && !GetWindow2() ) |
c801d85f | 664 | { |
63ec9dbb | 665 | GetWindow1()->SetSize(GetBorderSize(), GetBorderSize(), |
ca39e409 | 666 | w - 2*GetBorderSize(), h - 2*GetBorderSize()); |
c801d85f | 667 | } |
f6bcfd97 | 668 | else if ( GetWindow1() && GetWindow2() ) |
c801d85f | 669 | { |
b3208e11 VZ |
670 | const int border = GetBorderSize(), |
671 | sash = GetSashSize(); | |
672 | ||
673 | int size1 = GetSashPosition() - border, | |
674 | size2 = GetSashPosition() + sash; | |
675 | ||
676 | int x2, y2, w1, h1, w2, h2; | |
677 | if ( GetSplitMode() == wxSPLIT_VERTICAL ) | |
c801d85f | 678 | { |
b3208e11 VZ |
679 | w1 = size1; |
680 | w2 = w - 2*border - sash - w1; | |
681 | h1 = | |
682 | h2 = h - 2*border; | |
683 | x2 = size2; | |
684 | y2 = border; | |
c801d85f | 685 | } |
b3208e11 | 686 | else // horz splitter |
c801d85f | 687 | { |
b3208e11 VZ |
688 | w1 = |
689 | w2 = w - 2*border; | |
690 | h1 = size1; | |
691 | h2 = h - 2*border - sash - h1; | |
692 | x2 = border; | |
693 | y2 = size2; | |
c801d85f | 694 | } |
b3208e11 VZ |
695 | |
696 | GetWindow1()->SetSize(border, border, w1, h1); | |
697 | GetWindow2()->SetSize(x2, y2, w2, h2); | |
c801d85f | 698 | } |
b3208e11 | 699 | |
c801d85f | 700 | wxClientDC dc(this); |
c801d85f | 701 | DrawSash(dc); |
00a32dc1 | 702 | |
4395fb21 | 703 | SetNeedUpdating(false); |
c801d85f KB |
704 | } |
705 | ||
706 | // Set pane for unsplit window | |
707 | void wxSplitterWindow::Initialize(wxWindow *window) | |
708 | { | |
f938940e | 709 | wxASSERT_MSG( (!window || (window && window->GetParent() == this)), |
fe6dc50a VZ |
710 | _T("windows in the splitter should have it as parent!") ); |
711 | ||
c801d85f | 712 | m_windowOne = window; |
c67daf87 | 713 | m_windowTwo = (wxWindow *) NULL; |
ca39e409 | 714 | DoSetSashPosition(0); |
c801d85f KB |
715 | } |
716 | ||
717 | // Associates the given window with window 2, drawing the appropriate sash | |
718 | // and changing the split mode. | |
4395fb21 | 719 | // Does nothing and returns false if the window is already split. |
2e8cc3e8 VZ |
720 | bool wxSplitterWindow::DoSplit(wxSplitMode mode, |
721 | wxWindow *window1, wxWindow *window2, | |
722 | int sashPosition) | |
c801d85f KB |
723 | { |
724 | if ( IsSplit() ) | |
4395fb21 | 725 | return false; |
c801d85f | 726 | |
4395fb21 | 727 | wxCHECK_MSG( window1 && window2, false, |
2b5f62a0 VZ |
728 | _T("can not split with NULL window(s)") ); |
729 | ||
4395fb21 | 730 | wxCHECK_MSG( window1->GetParent() == this && window2->GetParent() == this, false, |
fe6dc50a VZ |
731 | _T("windows in the splitter should have it as parent!") ); |
732 | ||
2e8cc3e8 | 733 | m_splitMode = mode; |
c801d85f KB |
734 | m_windowOne = window1; |
735 | m_windowTwo = window2; | |
c801d85f | 736 | |
74c57d1f VZ |
737 | // remember the sash position we want to set for later if we can't set it |
738 | // right now (e.g. because the window is too small) | |
739 | m_requestedSashPosition = sashPosition; | |
4395fb21 | 740 | m_checkRequestedSashPosition = false; |
ca65c044 | 741 | |
74c57d1f VZ |
742 | DoSetSashPosition(ConvertSashPosition(sashPosition)); |
743 | ||
744 | SizeWindows(); | |
745 | ||
4395fb21 | 746 | return true; |
74c57d1f VZ |
747 | } |
748 | ||
749 | int wxSplitterWindow::ConvertSashPosition(int sashPosition) const | |
750 | { | |
0d559d69 | 751 | if ( sashPosition > 0 ) |
2e8cc3e8 | 752 | { |
74c57d1f | 753 | return sashPosition; |
2e8cc3e8 | 754 | } |
0d559d69 | 755 | else if ( sashPosition < 0 ) |
2e8cc3e8 VZ |
756 | { |
757 | // It's negative so adding is subtracting | |
74c57d1f | 758 | return GetWindowSize() + sashPosition; |
2e8cc3e8 | 759 | } |
74c57d1f | 760 | else // sashPosition == 0 |
2e8cc3e8 | 761 | { |
74c57d1f VZ |
762 | // default, put it in the centre |
763 | return GetWindowSize() / 2; | |
2e8cc3e8 | 764 | } |
c801d85f KB |
765 | } |
766 | ||
c801d85f KB |
767 | // Remove the specified (or second) window from the view |
768 | // Doesn't actually delete the window. | |
769 | bool wxSplitterWindow::Unsplit(wxWindow *toRemove) | |
770 | { | |
771 | if ( ! IsSplit() ) | |
4395fb21 | 772 | return false; |
c801d85f | 773 | |
999836aa | 774 | wxWindow *win; |
c801d85f KB |
775 | if ( toRemove == NULL || toRemove == m_windowTwo) |
776 | { | |
3ad5e06b | 777 | win = m_windowTwo ; |
c67daf87 | 778 | m_windowTwo = (wxWindow *) NULL; |
c801d85f KB |
779 | } |
780 | else if ( toRemove == m_windowOne ) | |
781 | { | |
3ad5e06b | 782 | win = m_windowOne ; |
c801d85f | 783 | m_windowOne = m_windowTwo; |
c67daf87 | 784 | m_windowTwo = (wxWindow *) NULL; |
c801d85f KB |
785 | } |
786 | else | |
dbc208e9 | 787 | { |
223d09f6 | 788 | wxFAIL_MSG(wxT("splitter: attempt to remove a non-existent window")); |
dbc208e9 | 789 | |
4395fb21 | 790 | return false; |
dbc208e9 | 791 | } |
c801d85f | 792 | |
0e4cfcdd | 793 | OnUnsplit(win); |
ca39e409 | 794 | DoSetSashPosition(0); |
3ad5e06b VZ |
795 | SizeWindows(); |
796 | ||
4395fb21 | 797 | return true; |
3ad5e06b VZ |
798 | } |
799 | ||
800 | // Replace a window with another one | |
801 | bool wxSplitterWindow::ReplaceWindow(wxWindow *winOld, wxWindow *winNew) | |
802 | { | |
4395fb21 RD |
803 | wxCHECK_MSG( winOld, false, wxT("use one of Split() functions instead") ); |
804 | wxCHECK_MSG( winNew, false, wxT("use Unsplit() functions instead") ); | |
3ad5e06b VZ |
805 | |
806 | if ( winOld == m_windowTwo ) | |
807 | { | |
808 | m_windowTwo = winNew; | |
809 | } | |
810 | else if ( winOld == m_windowOne ) | |
811 | { | |
812 | m_windowOne = winNew; | |
813 | } | |
814 | else | |
815 | { | |
223d09f6 | 816 | wxFAIL_MSG(wxT("splitter: attempt to replace a non-existent window")); |
3ad5e06b | 817 | |
4395fb21 | 818 | return false; |
3ad5e06b VZ |
819 | } |
820 | ||
821 | SizeWindows(); | |
822 | ||
4395fb21 | 823 | return true; |
c801d85f KB |
824 | } |
825 | ||
ca39e409 VS |
826 | void wxSplitterWindow::SetMinimumPaneSize(int min) |
827 | { | |
828 | m_minimumPaneSize = min; | |
4395fb21 RD |
829 | int pos = m_requestedSashPosition != INT_MAX ? m_requestedSashPosition : m_sashPosition; |
830 | SetSashPosition(pos); // re-check limits | |
ca39e409 VS |
831 | } |
832 | ||
debe6624 | 833 | void wxSplitterWindow::SetSashPosition(int position, bool redraw) |
c801d85f | 834 | { |
4395fb21 RD |
835 | // remember the sash position we want to set for later if we can't set it |
836 | // right now (e.g. because the window is too small) | |
837 | m_requestedSashPosition = position; | |
838 | m_checkRequestedSashPosition = false; | |
ca65c044 | 839 | |
4395fb21 | 840 | DoSetSashPosition(ConvertSashPosition(position)); |
c801d85f KB |
841 | |
842 | if ( redraw ) | |
843 | { | |
844 | SizeWindows(); | |
845 | } | |
846 | } | |
847 | ||
7e521b01 JS |
848 | // Make sure the child window sizes are updated. This is useful |
849 | // for reducing flicker by updating the sizes before a | |
850 | // window is shown, if you know the overall size is correct. | |
851 | void wxSplitterWindow::UpdateSize() | |
852 | { | |
853 | m_checkRequestedSashPosition = true; | |
854 | SizeWindows(); | |
855 | m_checkRequestedSashPosition = false; | |
856 | } | |
857 | ||
3e58dcb9 | 858 | bool wxSplitterWindow::DoSendEvent(wxSplitterEvent& event) |
42e69d6b | 859 | { |
3e58dcb9 | 860 | return !GetEventHandler()->ProcessEvent(event) || event.IsAllowed(); |
42e69d6b VZ |
861 | } |
862 | ||
863 | // --------------------------------------------------------------------------- | |
3e58dcb9 | 864 | // wxSplitterWindow virtual functions: they now just generate the events |
42e69d6b VZ |
865 | // --------------------------------------------------------------------------- |
866 | ||
3e58dcb9 VZ |
867 | bool wxSplitterWindow::OnSashPositionChange(int WXUNUSED(newSashPosition)) |
868 | { | |
869 | // always allow by default | |
4395fb21 | 870 | return true; |
3e58dcb9 VZ |
871 | } |
872 | ||
873 | int wxSplitterWindow::OnSashPositionChanging(int newSashPosition) | |
42e69d6b VZ |
874 | { |
875 | // If within UNSPLIT_THRESHOLD from edge, set to edge to cause closure. | |
876 | const int UNSPLIT_THRESHOLD = 4; | |
877 | ||
3e58dcb9 VZ |
878 | // first of all, check if OnSashPositionChange() doesn't forbid this change |
879 | if ( !OnSashPositionChange(newSashPosition) ) | |
880 | { | |
881 | // it does | |
882 | return -1; | |
883 | } | |
42e69d6b VZ |
884 | |
885 | // Obtain relevant window dimension for bottom / right threshold check | |
2e8cc3e8 | 886 | int window_size = GetWindowSize(); |
42e69d6b | 887 | |
4395fb21 | 888 | bool unsplit_scenario = false; |
3e58dcb9 | 889 | if ( m_permitUnsplitAlways || m_minimumPaneSize == 0 ) |
bc79aa6b | 890 | { |
370938d9 UB |
891 | // Do edge detection if unsplit premitted |
892 | if ( newSashPosition <= UNSPLIT_THRESHOLD ) | |
893 | { | |
894 | // threshold top / left check | |
895 | newSashPosition = 0; | |
4395fb21 | 896 | unsplit_scenario = true; |
370938d9 UB |
897 | } |
898 | if ( newSashPosition >= window_size - UNSPLIT_THRESHOLD ) | |
899 | { | |
900 | // threshold bottom/right check | |
901 | newSashPosition = window_size; | |
4395fb21 | 902 | unsplit_scenario = true; |
370938d9 | 903 | } |
bc79aa6b UB |
904 | } |
905 | ||
370938d9 | 906 | if ( !unsplit_scenario ) |
bc79aa6b UB |
907 | { |
908 | // If resultant pane would be too small, enlarge it | |
2e8cc3e8 | 909 | newSashPosition = AdjustSashPosition(newSashPosition); |
bc79aa6b | 910 | } |
42e69d6b VZ |
911 | |
912 | // If the result is out of bounds it means minimum size is too big, | |
913 | // so split window in half as best compromise. | |
914 | if ( newSashPosition < 0 || newSashPosition > window_size ) | |
915 | newSashPosition = window_size / 2; | |
916 | ||
3e58dcb9 VZ |
917 | // now let the event handler have it |
918 | // | |
919 | // FIXME: shouldn't we do it before the adjustments above so as to ensure | |
920 | // that the sash position is always reasonable? | |
921 | wxSplitterEvent event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING, this); | |
922 | event.m_data.pos = newSashPosition; | |
923 | ||
924 | if ( !DoSendEvent(event) ) | |
42e69d6b | 925 | { |
3e58dcb9 | 926 | // the event handler vetoed the change |
42e69d6b VZ |
927 | newSashPosition = -1; |
928 | } | |
3e58dcb9 VZ |
929 | else |
930 | { | |
931 | // it could have been changed by it | |
932 | newSashPosition = event.GetSashPosition(); | |
933 | } | |
42e69d6b | 934 | |
3e58dcb9 | 935 | return newSashPosition; |
42e69d6b VZ |
936 | } |
937 | ||
938 | // Called when the sash is double-clicked. The default behaviour is to remove | |
939 | // the sash if the minimum pane size is zero. | |
3e58dcb9 | 940 | void wxSplitterWindow::OnDoubleClickSash(int x, int y) |
42e69d6b | 941 | { |
a2f9a636 JS |
942 | wxCHECK_RET(m_windowTwo, wxT("splitter: no window to remove")); |
943 | ||
3e58dcb9 VZ |
944 | // new code should handle events instead of using the virtual functions |
945 | wxSplitterEvent event(wxEVT_COMMAND_SPLITTER_DOUBLECLICKED, this); | |
946 | event.m_data.pt.x = x; | |
947 | event.m_data.pt.y = y; | |
948 | if ( DoSendEvent(event) ) | |
42e69d6b | 949 | { |
3e58dcb9 VZ |
950 | if ( GetMinimumPaneSize() == 0 || m_permitUnsplitAlways ) |
951 | { | |
a2f9a636 | 952 | wxWindow* win = m_windowTwo; |
0e4cfcdd JS |
953 | if ( Unsplit(win) ) |
954 | { | |
955 | wxSplitterEvent unsplitEvent(wxEVT_COMMAND_SPLITTER_UNSPLIT, this); | |
956 | unsplitEvent.m_data.win = win; | |
957 | (void)DoSendEvent(unsplitEvent); | |
958 | } | |
3e58dcb9 | 959 | } |
42e69d6b | 960 | } |
3e58dcb9 | 961 | //else: blocked by user |
42e69d6b VZ |
962 | } |
963 | ||
3e58dcb9 | 964 | void wxSplitterWindow::OnUnsplit(wxWindow *winRemoved) |
42e69d6b | 965 | { |
0e4cfcdd | 966 | // call this before calling the event handler which may delete the window |
4395fb21 | 967 | winRemoved->Show(false); |
42e69d6b | 968 | } |
43b5058d | 969 | |
64407854 | 970 | #if defined( __WXMSW__ ) || defined( __WXMAC__) |
43b5058d | 971 | |
3e58dcb9 VZ |
972 | // this is currently called (and needed) under MSW only... |
973 | void wxSplitterWindow::OnSetCursor(wxSetCursorEvent& event) | |
974 | { | |
43b5058d VZ |
975 | // if we don't do it, the resizing cursor might be set for child window: |
976 | // and like this we explicitly say that our cursor should not be used for | |
977 | // children windows which overlap us | |
978 | ||
4395fb21 | 979 | if ( SashHitTest(event.GetX(), event.GetY(), 0) ) |
43b5058d VZ |
980 | { |
981 | // default processing is ok | |
982 | event.Skip(); | |
983 | } | |
984 | //else: do nothing, in particular, don't call Skip() | |
43b5058d | 985 | } |
3e58dcb9 | 986 | |
b3208e11 VZ |
987 | #endif // wxMSW || wxMac |
988 | ||
d7260478 | 989 | #endif // wxUSE_SPLITTER |
3e58dcb9 | 990 |