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