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