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