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