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