various splitter fixes:
[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 m_requestedSashPosition = sashPos;
722 m_sashPosition = sashPos == 0 ? 0 : AdjustSashPosition(sashPos);
723
724 wxSplitterEvent event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED, this);
725 event.m_data.pos = m_sashPosition;
726
727 (void)DoSendEvent(event);
728 }
729
730 // Position and size subwindows.
731 // Note that the border size applies to each subwindow, not
732 // including the edges next to the sash.
733 void wxSplitterWindow::SizeWindows()
734 {
735 if ( m_requestedSashPosition != m_sashPosition )
736 DoSetSashPosition(m_requestedSashPosition);
737
738 int w, h;
739 GetClientSize(&w, &h);
740
741 if ( GetWindow1() && !GetWindow2() )
742 {
743 GetWindow1()->SetSize(GetBorderSize(), GetBorderSize(),
744 w - 2*GetBorderSize(), h - 2*GetBorderSize());
745 }
746 else if ( GetWindow1() && GetWindow2() )
747 {
748 if (GetSplitMode() == wxSPLIT_VERTICAL)
749 {
750 int x1 = GetBorderSize();
751 int y1 = GetBorderSize();
752 int w1 = GetSashPosition() - GetBorderSize();
753 int h1 = h - 2*GetBorderSize();
754
755 int x2 = GetSashPosition() + GetSashSize();
756 int y2 = GetBorderSize();
757 int w2 = w - 2*GetBorderSize() - GetSashSize() - w1;
758 int h2 = h - 2*GetBorderSize();
759
760 GetWindow1()->SetSize(x1, y1, w1, h1);
761 GetWindow2()->SetSize(x2, y2, w2, h2);
762 }
763 else
764 {
765 GetWindow1()->SetSize(GetBorderSize(), GetBorderSize(),
766 w - 2*GetBorderSize(), GetSashPosition() - GetBorderSize());
767 GetWindow2()->SetSize(GetBorderSize(), GetSashPosition() + GetSashSize(),
768 w - 2*GetBorderSize(), h - 2*GetBorderSize() - GetSashSize() - (GetSashPosition() - GetBorderSize()));
769 }
770 }
771 wxClientDC dc(this);
772 if ( GetBorderSize() > 0 )
773 DrawBorders(dc);
774 DrawSash(dc);
775
776 SetNeedUpdating(FALSE);
777 }
778
779 // Set pane for unsplit window
780 void wxSplitterWindow::Initialize(wxWindow *window)
781 {
782 m_windowOne = window;
783 m_windowTwo = (wxWindow *) NULL;
784 DoSetSashPosition(0);
785 }
786
787 // Associates the given window with window 2, drawing the appropriate sash
788 // and changing the split mode.
789 // Does nothing and returns FALSE if the window is already split.
790 bool wxSplitterWindow::DoSplit(wxSplitMode mode,
791 wxWindow *window1, wxWindow *window2,
792 int sashPosition)
793 {
794 if ( IsSplit() )
795 return FALSE;
796
797 int window_size = GetWindowSize();
798
799 m_splitMode = mode;
800 m_windowOne = window1;
801 m_windowTwo = window2;
802
803 if ( sashPosition > 0 )
804 {
805 DoSetSashPosition(sashPosition);
806 }
807 else if ( sashPosition < 0 )
808 {
809 // It's negative so adding is subtracting
810 DoSetSashPosition(window_size + sashPosition);
811 }
812 else
813 {
814 // default
815 DoSetSashPosition(window_size/2);
816 }
817
818 SizeWindows();
819
820 return TRUE;
821 }
822
823 // Remove the specified (or second) window from the view
824 // Doesn't actually delete the window.
825 bool wxSplitterWindow::Unsplit(wxWindow *toRemove)
826 {
827 if ( ! IsSplit() )
828 return FALSE;
829
830 wxWindow *win = NULL;
831 if ( toRemove == NULL || toRemove == m_windowTwo)
832 {
833 win = m_windowTwo ;
834 m_windowTwo = (wxWindow *) NULL;
835 }
836 else if ( toRemove == m_windowOne )
837 {
838 win = m_windowOne ;
839 m_windowOne = m_windowTwo;
840 m_windowTwo = (wxWindow *) NULL;
841 }
842 else
843 {
844 wxFAIL_MSG(wxT("splitter: attempt to remove a non-existent window"));
845
846 return FALSE;
847 }
848
849 OnUnsplit(win);
850 DoSetSashPosition(0);
851 SizeWindows();
852
853 return TRUE;
854 }
855
856 // Replace a window with another one
857 bool wxSplitterWindow::ReplaceWindow(wxWindow *winOld, wxWindow *winNew)
858 {
859 wxCHECK_MSG( winOld, FALSE, wxT("use one of Split() functions instead") );
860 wxCHECK_MSG( winNew, FALSE, wxT("use Unsplit() functions instead") );
861
862 if ( winOld == m_windowTwo )
863 {
864 m_windowTwo = winNew;
865 }
866 else if ( winOld == m_windowOne )
867 {
868 m_windowOne = winNew;
869 }
870 else
871 {
872 wxFAIL_MSG(wxT("splitter: attempt to replace a non-existent window"));
873
874 return FALSE;
875 }
876
877 SizeWindows();
878
879 return TRUE;
880 }
881
882 void wxSplitterWindow::SetMinimumPaneSize(int min)
883 {
884 m_minimumPaneSize = min;
885 SetSashPosition(m_sashPosition); // re-check limits
886 }
887
888 void wxSplitterWindow::SetSashPosition(int position, bool redraw)
889 {
890 DoSetSashPosition(position);
891
892 if ( redraw )
893 {
894 SizeWindows();
895 }
896 }
897
898 // Initialize colours
899 void wxSplitterWindow::InitColours()
900 {
901 wxDELETE( m_facePen );
902 wxDELETE( m_faceBrush );
903 wxDELETE( m_mediumShadowPen );
904 wxDELETE( m_darkShadowPen );
905 wxDELETE( m_lightShadowPen );
906 wxDELETE( m_hilightPen );
907
908 // Shadow colours
909 #ifndef __WIN16__
910 wxColour faceColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
911 m_facePen = new wxPen(faceColour, 1, wxSOLID);
912 m_faceBrush = new wxBrush(faceColour, wxSOLID);
913
914 wxColour mediumShadowColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW));
915 m_mediumShadowPen = new wxPen(mediumShadowColour, 1, wxSOLID);
916
917 wxColour darkShadowColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW));
918 m_darkShadowPen = new wxPen(darkShadowColour, 1, wxSOLID);
919
920 wxColour lightShadowColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT));
921 m_lightShadowPen = new wxPen(lightShadowColour, 1, wxSOLID);
922
923 wxColour hilightColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DHILIGHT));
924 m_hilightPen = new wxPen(hilightColour, 1, wxSOLID);
925 #else
926 m_facePen = new wxPen("LIGHT GREY", 1, wxSOLID);
927 m_faceBrush = new wxBrush("LIGHT GREY", wxSOLID);
928 m_mediumShadowPen = new wxPen("GREY", 1, wxSOLID);
929 m_darkShadowPen = new wxPen("BLACK", 1, wxSOLID);
930 m_lightShadowPen = new wxPen("LIGHT GREY", 1, wxSOLID);
931 m_hilightPen = new wxPen("WHITE", 1, wxSOLID);
932 #endif // __WIN16__
933 }
934
935 bool wxSplitterWindow::DoSendEvent(wxSplitterEvent& event)
936 {
937 return !GetEventHandler()->ProcessEvent(event) || event.IsAllowed();
938 }
939
940 // ---------------------------------------------------------------------------
941 // wxSplitterWindow virtual functions: they now just generate the events
942 // ---------------------------------------------------------------------------
943
944 bool wxSplitterWindow::OnSashPositionChange(int WXUNUSED(newSashPosition))
945 {
946 // always allow by default
947 return TRUE;
948 }
949
950 int wxSplitterWindow::OnSashPositionChanging(int newSashPosition)
951 {
952 // If within UNSPLIT_THRESHOLD from edge, set to edge to cause closure.
953 const int UNSPLIT_THRESHOLD = 4;
954
955 // first of all, check if OnSashPositionChange() doesn't forbid this change
956 if ( !OnSashPositionChange(newSashPosition) )
957 {
958 // it does
959 return -1;
960 }
961
962 // Obtain relevant window dimension for bottom / right threshold check
963 int window_size = GetWindowSize();
964
965 bool unsplit_scenario = FALSE;
966 if ( m_permitUnsplitAlways || m_minimumPaneSize == 0 )
967 {
968 // Do edge detection if unsplit premitted
969 if ( newSashPosition <= UNSPLIT_THRESHOLD )
970 {
971 // threshold top / left check
972 newSashPosition = 0;
973 unsplit_scenario = TRUE;
974 }
975 if ( newSashPosition >= window_size - UNSPLIT_THRESHOLD )
976 {
977 // threshold bottom/right check
978 newSashPosition = window_size;
979 unsplit_scenario = TRUE;
980 }
981 }
982
983 if ( !unsplit_scenario )
984 {
985 // If resultant pane would be too small, enlarge it
986 newSashPosition = AdjustSashPosition(newSashPosition);
987 }
988
989 // If the result is out of bounds it means minimum size is too big,
990 // so split window in half as best compromise.
991 if ( newSashPosition < 0 || newSashPosition > window_size )
992 newSashPosition = window_size / 2;
993
994 // now let the event handler have it
995 //
996 // FIXME: shouldn't we do it before the adjustments above so as to ensure
997 // that the sash position is always reasonable?
998 wxSplitterEvent event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING, this);
999 event.m_data.pos = newSashPosition;
1000
1001 if ( !DoSendEvent(event) )
1002 {
1003 // the event handler vetoed the change
1004 newSashPosition = -1;
1005 }
1006 else
1007 {
1008 // it could have been changed by it
1009 newSashPosition = event.GetSashPosition();
1010 }
1011
1012 return newSashPosition;
1013 }
1014
1015 // Called when the sash is double-clicked. The default behaviour is to remove
1016 // the sash if the minimum pane size is zero.
1017 void wxSplitterWindow::OnDoubleClickSash(int x, int y)
1018 {
1019 // new code should handle events instead of using the virtual functions
1020 wxSplitterEvent event(wxEVT_COMMAND_SPLITTER_DOUBLECLICKED, this);
1021 event.m_data.pt.x = x;
1022 event.m_data.pt.y = y;
1023 if ( DoSendEvent(event) )
1024 {
1025 if ( GetMinimumPaneSize() == 0 || m_permitUnsplitAlways )
1026 {
1027 Unsplit();
1028 }
1029 }
1030 //else: blocked by user
1031 }
1032
1033 void wxSplitterWindow::OnUnsplit(wxWindow *winRemoved)
1034 {
1035 // do it before calling the event handler which may delete the window
1036 winRemoved->Show(FALSE);
1037
1038 wxSplitterEvent event(wxEVT_COMMAND_SPLITTER_UNSPLIT, this);
1039 event.m_data.win = winRemoved;
1040
1041 (void)DoSendEvent(event);
1042 }
1043
1044 #ifdef __WXMSW__
1045
1046 // this is currently called (and needed) under MSW only...
1047 void wxSplitterWindow::OnSetCursor(wxSetCursorEvent& event)
1048 {
1049 // if we don't do it, the resizing cursor might be set for child window:
1050 // and like this we explicitly say that our cursor should not be used for
1051 // children windows which overlap us
1052
1053 if ( SashHitTest(event.GetX(), event.GetY()) )
1054 {
1055 // default processing is ok
1056 event.Skip();
1057 }
1058 //else: do nothing, in particular, don't call Skip()
1059 }
1060
1061 #endif // wxMSW
1062