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