]> git.saurik.com Git - wxWidgets.git/blob - src/generic/splitter.cpp
dd3ea592ab8551fe9b5848a6da46382f873d7fa0
[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/wx.h"
25 #endif
26
27 #include <stdlib.h>
28
29 #include "wx/string.h"
30 #include "wx/splitter.h"
31 #include "wx/dcscreen.h"
32
33 #if !USE_SHARED_LIBRARY
34 IMPLEMENT_DYNAMIC_CLASS(wxSplitterWindow, wxWindow)
35 IMPLEMENT_DYNAMIC_CLASS(wxSplitterEvent, wxCommandEvent)
36
37 BEGIN_EVENT_TABLE(wxSplitterWindow, wxWindow)
38 EVT_PAINT(wxSplitterWindow::OnPaint)
39 EVT_SIZE(wxSplitterWindow::OnSize)
40 EVT_MOUSE_EVENTS(wxSplitterWindow::OnMouseEvent)
41
42 EVT_SPLITTER_SASH_POS_CHANGED(-1, wxSplitterWindow::OnSashPosChanged)
43 // NB: we borrow OnSashPosChanged for purposes of
44 // EVT_SPLITTER_SASH_POS_CHANGING since default implementation is identical
45 EVT_SPLITTER_SASH_POS_CHANGING(-1, wxSplitterWindow::OnSashPosChanged)
46 EVT_SPLITTER_DCLICK(-1, wxSplitterWindow::OnDoubleClick)
47 EVT_SPLITTER_UNSPLIT(-1, wxSplitterWindow::OnUnsplitEvent)
48 END_EVENT_TABLE()
49 #endif
50
51 wxSplitterWindow::wxSplitterWindow()
52 {
53 m_splitMode = wxSPLIT_VERTICAL;
54 m_permitUnsplitAlways = FALSE;
55 m_windowOne = (wxWindow *) NULL;
56 m_windowTwo = (wxWindow *) NULL;
57 m_dragMode = wxSPLIT_DRAG_NONE;
58 m_oldX = 0;
59 m_oldY = 0;
60 m_firstX = 0;
61 m_firstY = 0;
62 m_sashSize = 7;
63 m_borderSize = 2;
64 m_sashPosition = 0;
65 m_sashCursorWE = (wxCursor *) NULL;
66 m_sashCursorNS = (wxCursor *) NULL;
67 m_sashTrackerPen = (wxPen *) NULL;
68 m_lightShadowPen = (wxPen *) NULL;
69 m_mediumShadowPen = (wxPen *) NULL;
70 m_darkShadowPen = (wxPen *) NULL;
71 m_faceBrush = (wxBrush *) NULL;
72 m_facePen = (wxPen *) NULL;
73 m_hilightPen = (wxPen *) NULL;
74 m_minimumPaneSize = 0;
75 }
76
77 wxSplitterWindow::wxSplitterWindow(wxWindow *parent, wxWindowID id,
78 const wxPoint& pos,
79 const wxSize& size,
80 long style,
81 const wxString& name)
82 : wxWindow(parent, id, pos, size, style, name)
83 {
84 m_splitMode = wxSPLIT_VERTICAL;
85 m_permitUnsplitAlways = (style & wxSP_PERMIT_UNSPLIT) != 0;
86 m_windowOne = (wxWindow *) NULL;
87 m_windowTwo = (wxWindow *) NULL;
88 m_dragMode = wxSPLIT_DRAG_NONE;
89 m_oldX = 0;
90 m_oldY = 0;
91 m_firstX = 0;
92 m_firstY = 0;
93 m_sashSize = 7;
94 m_borderSize = 2;
95 m_sashPosition = 0;
96 m_minimumPaneSize = 0;
97 m_sashCursorWE = new wxCursor(wxCURSOR_SIZEWE);
98 m_sashCursorNS = new wxCursor(wxCURSOR_SIZENS);
99 m_sashTrackerPen = new wxPen(*wxBLACK, 2, wxSOLID);
100 m_lightShadowPen = (wxPen *) NULL;
101 m_mediumShadowPen = (wxPen *) NULL;
102 m_darkShadowPen = (wxPen *) NULL;
103 m_faceBrush = (wxBrush *) NULL;
104 m_facePen = (wxPen *) NULL;
105 m_hilightPen = (wxPen *) NULL;
106
107 if ( style & wxSP_3D )
108 {
109 m_borderSize = 2;
110 m_sashSize = 7;
111 }
112 else if ( style & wxSP_BORDER )
113 {
114 m_borderSize = 1;
115 m_sashSize = 3;
116 }
117 else
118 {
119 m_borderSize = 0;
120 m_sashSize = 3;
121 }
122
123 // Eventually, we'll respond to colour change messages
124 InitColours();
125
126 // For debugging purposes, to see the background.
127 // SetBackground(wxBLUE_BRUSH);
128 }
129
130 wxSplitterWindow::~wxSplitterWindow()
131 {
132 delete m_sashCursorWE;
133 delete m_sashCursorNS;
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::OnPaint(wxPaintEvent& WXUNUSED(event))
144 {
145 wxPaintDC dc(this);
146
147 if ( m_borderSize > 0 )
148 DrawBorders(dc);
149 DrawSash(dc);
150 }
151
152
153 void wxSplitterWindow::OnMouseEvent(wxMouseEvent& event)
154 {
155 long x = event.GetX();
156 long y = event.GetY();
157
158 // reset the cursor
159 #ifdef __WXMOTIF__
160 SetCursor(* wxSTANDARD_CURSOR);
161 #endif
162 #ifdef __WXMSW__
163 SetCursor(wxCursor());
164 #endif
165
166 // under wxGTK the method above causes the mouse
167 // to flicker so we set the standard cursor only
168 // when leaving the window and when moving over
169 // non-sash parts of the window. this should work
170 // on the other platforms as well, but who knows.
171 #ifdef __WXGTK__
172 if (event.Leaving())
173 SetCursor(* wxSTANDARD_CURSOR);
174 #endif
175
176 if (event.LeftDown())
177 {
178 if ( SashHitTest(x, y) )
179 {
180 CaptureMouse();
181
182 m_dragMode = wxSPLIT_DRAG_DRAGGING;
183
184 DrawSashTracker(x, y);
185 m_oldX = x;
186 m_oldY = y;
187 return;
188 }
189 }
190 else if (event.LeftUp() && m_dragMode == wxSPLIT_DRAG_DRAGGING)
191 {
192 // We can stop dragging now and see what we've got.
193 m_dragMode = wxSPLIT_DRAG_NONE;
194 ReleaseMouse();
195
196 // Erase old tracker
197 DrawSashTracker(m_oldX, m_oldY);
198
199 // Obtain window size. We are only interested in the dimension the sash
200 // splits up
201 int w, h;
202 GetClientSize(&w, &h);
203 int window_size = (m_splitMode == wxSPLIT_VERTICAL ? w : h );
204 int new_sash_position =
205 (int) ( m_splitMode == wxSPLIT_VERTICAL ? x : y );
206
207 wxSplitterEvent eventSplitter(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED,
208 this);
209 eventSplitter.m_data.pos = new_sash_position;
210 if ( GetEventHandler()->ProcessEvent(eventSplitter) )
211 {
212 new_sash_position = eventSplitter.GetSashPosition();
213 if ( new_sash_position == -1 )
214 {
215 // change not allowed
216 return;
217 }
218 }
219
220 if ( m_permitUnsplitAlways
221 || m_minimumPaneSize == 0 )
222 {
223 // Deal with possible unsplit scenarios
224 if ( new_sash_position == 0 )
225 {
226 // We remove the first window from the view
227 wxWindow *removedWindow = m_windowOne;
228 m_windowOne = m_windowTwo;
229 m_windowTwo = (wxWindow *) NULL;
230 SendUnsplitEvent(removedWindow);
231 m_sashPosition = 0;
232 }
233 else if ( new_sash_position == window_size )
234 {
235 // We remove the second window from the view
236 wxWindow *removedWindow = m_windowTwo;
237 m_windowTwo = (wxWindow *) NULL;
238 SendUnsplitEvent(removedWindow);
239 m_sashPosition = 0;
240 }
241 else
242 {
243 m_sashPosition = new_sash_position;
244 }
245 }
246 else
247 {
248 m_sashPosition = new_sash_position;
249 }
250
251 SizeWindows();
252 } // left up && dragging
253 else if (event.Moving() && !event.Dragging())
254 {
255 // Just change the cursor if required
256 if ( SashHitTest(x, y) )
257 {
258 if ( m_splitMode == wxSPLIT_VERTICAL )
259 {
260 SetCursor(*m_sashCursorWE);
261 }
262 else
263 {
264 SetCursor(*m_sashCursorNS);
265 }
266 }
267 #ifdef __WXGTK__
268 else
269 {
270 // where else do we unset the cursor?
271 SetCursor(* wxSTANDARD_CURSOR);
272 }
273 #endif // __WXGTK__
274 }
275 else if (event.Dragging() && (m_dragMode == wxSPLIT_DRAG_DRAGGING))
276 {
277 // Obtain window size. We are only interested in the dimension the sash
278 // splits up
279 int new_sash_position =
280 (int) ( m_splitMode == wxSPLIT_VERTICAL ? x : y );
281
282 wxSplitterEvent eventSplitter(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING,
283 this);
284 eventSplitter.m_data.pos = new_sash_position;
285 if ( GetEventHandler()->ProcessEvent(eventSplitter) )
286 {
287 new_sash_position = eventSplitter.GetSashPosition();
288 if ( new_sash_position == -1 )
289 {
290 // change not allowed
291 return;
292 }
293 }
294
295 // Erase old tracker
296 DrawSashTracker(m_oldX, m_oldY);
297
298 if (m_splitMode == wxSPLIT_VERTICAL)
299 x = new_sash_position;
300 else
301 y = new_sash_position;
302
303 if (new_sash_position != -1)
304 {
305 // Only modify if permitted
306 m_oldX = x;
307 m_oldY = y;
308 }
309
310 #ifdef __WXMSW__
311 // As we captured the mouse, we may get the mouse events from outside
312 // our window - for example, negative values in x, y. This has a weird
313 // consequence under MSW where we use unsigned values sometimes and
314 // signed ones other times: the coordinates turn as big positive
315 // numbers and so the sash is drawn on the *right* side of the window
316 // instead of the left (or bottom instead of top). Correct this.
317 if ( (short)m_oldX < 0 )
318 m_oldX = 0;
319 if ( (short)m_oldY < 0 )
320 m_oldY = 0;
321 #endif // __WXMSW__
322
323 // Draw new one
324 DrawSashTracker(m_oldX, m_oldY);
325 }
326 else if ( event.LeftDClick() )
327 {
328 wxSplitterEvent eventSplitter(wxEVT_COMMAND_SPLITTER_DOUBLECLICKED,
329 this);
330 eventSplitter.m_data.pt.x = x;
331 eventSplitter.m_data.pt.y = y;
332
333 (void)GetEventHandler()->ProcessEvent(eventSplitter);
334 }
335 }
336
337 void wxSplitterWindow::OnSize(wxSizeEvent& WXUNUSED(event))
338 {
339 int cw, ch;
340 GetClientSize( &cw, &ch );
341 if ( m_windowTwo )
342 {
343 if ( m_splitMode == wxSPLIT_VERTICAL )
344 {
345 if ( m_sashPosition >= (cw - 5) )
346 m_sashPosition = wxMax(10, cw - 40);
347 }
348 if ( m_splitMode == wxSPLIT_HORIZONTAL )
349 {
350 if ( m_sashPosition >= (ch - 5) )
351 m_sashPosition = wxMax(10, ch - 40);
352 }
353 }
354 SizeWindows();
355 }
356
357 bool wxSplitterWindow::SashHitTest(int x, int y, int tolerance)
358 {
359 if ( m_windowTwo == NULL || m_sashPosition == 0)
360 return FALSE; // No sash
361
362 if ( m_splitMode == wxSPLIT_VERTICAL )
363 {
364 if ( (x >= m_sashPosition - tolerance) && (x <= m_sashPosition + m_sashSize + tolerance) )
365 return TRUE;
366 else
367 return FALSE;
368 }
369 else
370 {
371 if ( (y >= (m_sashPosition- tolerance)) && (y <= (m_sashPosition + m_sashSize + tolerance)) )
372 return TRUE;
373 else
374 return FALSE;
375 }
376
377 return FALSE;
378 }
379
380 // Draw 3D effect borders
381 void wxSplitterWindow::DrawBorders(wxDC& dc)
382 {
383 int w, h;
384 GetClientSize(&w, &h);
385
386 if ( GetWindowStyleFlag() & wxSP_3D )
387 {
388 dc.SetPen(*m_mediumShadowPen);
389 dc.DrawLine(0, 0, w-1, 0);
390 dc.DrawLine(0, 0, 0, h - 1);
391
392 dc.SetPen(*m_darkShadowPen);
393 dc.DrawLine(1, 1, w-2, 1);
394 dc.DrawLine(1, 1, 1, h-2);
395
396 dc.SetPen(*m_hilightPen);
397 dc.DrawLine(0, h-1, w-1, h-1);
398 dc.DrawLine(w-1, 0, w-1, h); // Surely the maximum y pos. should be h - 1.
399 /// Anyway, h is required for MSW.
400
401 dc.SetPen(*m_lightShadowPen);
402 dc.DrawLine(w-2, 1, w-2, h-2); // Right hand side
403 dc.DrawLine(1, h-2, w-1, h-2); // Bottom
404 }
405 else if ( GetWindowStyleFlag() & wxSP_BORDER )
406 {
407 dc.SetBrush(*wxTRANSPARENT_BRUSH);
408 dc.SetPen(*wxBLACK_PEN);
409 dc.DrawRectangle(0, 0, w-1, h-1);
410 }
411
412 dc.SetPen(wxNullPen);
413 dc.SetBrush(wxNullBrush);
414 }
415
416 // Draw the sash
417 void wxSplitterWindow::DrawSash(wxDC& dc)
418 {
419 if ( m_sashPosition == 0 || !m_windowTwo)
420 return;
421
422 int w, h;
423 GetClientSize(&w, &h);
424
425 if ( GetWindowStyleFlag() & wxSP_3D )
426 {
427 if ( m_splitMode == wxSPLIT_VERTICAL )
428 {
429 dc.SetPen(*m_facePen);
430 dc.SetBrush(*m_faceBrush);
431 dc.DrawRectangle(m_sashPosition + 2, 0, m_sashSize - 4, h);
432
433 dc.SetBrush(*wxTRANSPARENT_BRUSH);
434
435 dc.SetPen(*m_lightShadowPen);
436 dc.DrawLine(m_sashPosition, 1, m_sashPosition, h-2);
437
438 dc.SetPen(*m_hilightPen);
439 dc.DrawLine(m_sashPosition+1, 0, m_sashPosition+1, h);
440
441 dc.SetPen(*m_mediumShadowPen);
442 dc.DrawLine(m_sashPosition+m_sashSize-2, 1, m_sashPosition+m_sashSize-2, h-1);
443
444 dc.SetPen(*m_darkShadowPen);
445 dc.DrawLine(m_sashPosition+m_sashSize-1, 2, m_sashPosition+m_sashSize-1, h-2);
446 }
447 else
448 {
449 dc.SetPen(*m_facePen);
450 dc.SetBrush(*m_faceBrush);
451 dc.DrawRectangle(0, m_sashPosition + 2, w, m_sashSize - 4);
452
453 dc.SetBrush(*wxTRANSPARENT_BRUSH);
454
455 dc.SetPen(*m_lightShadowPen);
456 dc.DrawLine(1, m_sashPosition, w-2, m_sashPosition);
457
458 dc.SetPen(*m_hilightPen);
459 dc.DrawLine(0, m_sashPosition+1, w, m_sashPosition+1);
460
461 dc.SetPen(*m_mediumShadowPen);
462 dc.DrawLine(1, m_sashPosition+m_sashSize-2, w-1, m_sashPosition+m_sashSize-2);
463
464 dc.SetPen(*m_darkShadowPen);
465 dc.DrawLine(2, m_sashPosition+m_sashSize-1, w-2, m_sashPosition+m_sashSize-1);
466 }
467 }
468 else
469 {
470 if ( m_splitMode == wxSPLIT_VERTICAL )
471 {
472 dc.SetPen(*wxBLACK_PEN);
473 dc.SetBrush(*wxBLACK_BRUSH);
474 int h1 = h-1;
475 if ( (GetWindowStyleFlag() & wxSP_BORDER) != wxSP_BORDER )
476 h1 += 1; // Not sure why this is necessary...
477 dc.DrawRectangle(m_sashPosition, 0, m_sashSize, h1);
478 }
479 else
480 {
481 dc.SetPen(*wxBLACK_PEN);
482 dc.SetBrush(*wxBLACK_BRUSH);
483 int w1 = w-1;
484 if ( (GetWindowStyleFlag() & wxSP_BORDER) != wxSP_BORDER )
485 w1 ++;
486
487 dc.DrawRectangle(0, m_sashPosition, w1, m_sashSize);
488 }
489
490 }
491
492 dc.SetPen(wxNullPen);
493 dc.SetBrush(wxNullBrush);
494 }
495
496 // Draw the sash tracker (for whilst moving the sash)
497 void wxSplitterWindow::DrawSashTracker(int x, int y)
498 {
499 int w, h;
500 GetClientSize(&w, &h);
501
502 wxScreenDC screenDC;
503 int x1, y1;
504 int x2, y2;
505
506 if ( m_splitMode == wxSPLIT_VERTICAL )
507 {
508 x1 = x; y1 = 2;
509 x2 = x; y2 = h-2;
510
511 if ( x1 > w )
512 {
513 x1 = w; x2 = w;
514 }
515 else if ( x1 < 0 )
516 {
517 x1 = 0; x2 = 0;
518 }
519 }
520 else
521 {
522 x1 = 2; y1 = y;
523 x2 = w-2; y2 = y;
524
525 if ( y1 > h )
526 {
527 y1 = h;
528 y2 = h;
529 }
530 else if ( y1 < 0 )
531 {
532 y1 = 0;
533 y2 = 0;
534 }
535 }
536
537 ClientToScreen(&x1, &y1);
538 ClientToScreen(&x2, &y2);
539
540 screenDC.SetLogicalFunction(wxINVERT);
541 screenDC.SetPen(*m_sashTrackerPen);
542 screenDC.SetBrush(*wxTRANSPARENT_BRUSH);
543
544 screenDC.DrawLine(x1, y1, x2, y2);
545
546 screenDC.SetLogicalFunction(wxCOPY);
547
548 screenDC.SetPen(wxNullPen);
549 screenDC.SetBrush(wxNullBrush);
550 }
551
552 // Position and size subwindows.
553 // Note that the border size applies to each subwindow, not
554 // including the edges next to the sash.
555 void wxSplitterWindow::SizeWindows()
556 {
557 int w, h;
558 GetClientSize(&w, &h);
559
560 if ( m_windowOne && !m_windowTwo )
561 {
562 m_windowOne->SetSize(m_borderSize, m_borderSize, w - 2*m_borderSize, h - 2*m_borderSize);
563
564 if (m_windowOne->GetAutoLayout())
565 m_windowOne->Layout();
566 }
567 else if ( m_windowOne && m_windowTwo )
568 {
569 if (m_splitMode == wxSPLIT_VERTICAL)
570 {
571 int x1 = m_borderSize;
572 int y1 = m_borderSize;
573 int w1 = m_sashPosition - m_borderSize;
574 int h1 = h - 2*m_borderSize;
575
576 int x2 = m_sashPosition + m_sashSize;
577 int y2 = m_borderSize;
578 int w2 = w - 2*m_borderSize - m_sashSize - w1;
579 int h2 = h - 2*m_borderSize;
580
581 m_windowOne->SetSize(x1, y1, w1, h1);
582 m_windowTwo->SetSize(x2, y2, w2, h2);
583
584 if (m_windowOne->GetAutoLayout())
585 m_windowOne->Layout();
586 if (m_windowTwo->GetAutoLayout())
587 m_windowTwo->Layout();
588 }
589 else
590 {
591 m_windowOne->SetSize(m_borderSize, m_borderSize,
592 w - 2*m_borderSize, m_sashPosition - m_borderSize);
593 m_windowTwo->SetSize(m_borderSize, m_sashPosition + m_sashSize,
594 w - 2*m_borderSize, h - 2*m_borderSize - m_sashSize - (m_sashPosition - m_borderSize));
595
596 if (m_windowOne->GetAutoLayout())
597 m_windowOne->Layout();
598 if (m_windowTwo->GetAutoLayout())
599 m_windowTwo->Layout();
600 }
601 }
602 #ifdef __WXGTK__
603 Refresh();
604 #else
605 wxClientDC dc(this);
606 DrawBorders(dc);
607 DrawSash(dc);
608 #endif
609 }
610
611 // Set pane for unsplit window
612 void wxSplitterWindow::Initialize(wxWindow *window)
613 {
614 m_windowOne = window;
615 m_windowTwo = (wxWindow *) NULL;
616 m_sashPosition = 0;
617 }
618
619 // Associates the given window with window 2, drawing the appropriate sash
620 // and changing the split mode.
621 // Does nothing and returns FALSE if the window is already split.
622 bool wxSplitterWindow::SplitVertically(wxWindow *window1, wxWindow *window2, int sashPosition)
623 {
624 if ( IsSplit() )
625 return FALSE;
626
627 int w, h;
628 GetClientSize(&w, &h);
629
630 m_splitMode = wxSPLIT_VERTICAL;
631 m_windowOne = window1;
632 m_windowTwo = window2;
633 if ( sashPosition > 0 )
634 m_sashPosition = sashPosition;
635 else if ( sashPosition < 0 )
636 m_sashPosition = w - sashPosition;
637 else // default
638 m_sashPosition = w/2;
639
640 SizeWindows();
641
642 return TRUE;
643 }
644
645 bool wxSplitterWindow::SplitHorizontally(wxWindow *window1, wxWindow *window2, int sashPosition)
646 {
647 if ( IsSplit() )
648 return FALSE;
649
650 int w, h;
651 GetClientSize(&w, &h);
652
653 m_splitMode = wxSPLIT_HORIZONTAL;
654 m_windowOne = window1;
655 m_windowTwo = window2;
656 if ( sashPosition > 0 )
657 m_sashPosition = sashPosition;
658 else if ( sashPosition < 0 )
659 m_sashPosition = h - sashPosition;
660 else // default
661 m_sashPosition = h/2;
662
663 SizeWindows();
664
665 return TRUE;
666 }
667
668
669 // Remove the specified (or second) window from the view
670 // Doesn't actually delete the window.
671 bool wxSplitterWindow::Unsplit(wxWindow *toRemove)
672 {
673 if ( ! IsSplit() )
674 return FALSE;
675
676 wxWindow *win = NULL;
677 if ( toRemove == NULL || toRemove == m_windowTwo)
678 {
679 win = m_windowTwo ;
680 m_windowTwo = (wxWindow *) NULL;
681 }
682 else if ( toRemove == m_windowOne )
683 {
684 win = m_windowOne ;
685 m_windowOne = m_windowTwo;
686 m_windowTwo = (wxWindow *) NULL;
687 }
688 else
689 {
690 wxFAIL_MSG(_T("splitter: attempt to remove a non-existent window"));
691
692 return FALSE;
693 }
694
695 SendUnsplitEvent(win);
696 m_sashPosition = 0;
697 SizeWindows();
698
699 return TRUE;
700 }
701
702 // Replace a window with another one
703 bool wxSplitterWindow::ReplaceWindow(wxWindow *winOld, wxWindow *winNew)
704 {
705 wxCHECK_MSG( winOld, FALSE, _T("use one of Split() functions instead") );
706 wxCHECK_MSG( winNew, FALSE, _T("use Unsplit() functions instead") );
707
708 if ( winOld == m_windowTwo )
709 {
710 m_windowTwo = winNew;
711 }
712 else if ( winOld == m_windowOne )
713 {
714 m_windowOne = winNew;
715 }
716 else
717 {
718 wxFAIL_MSG(_T("splitter: attempt to replace a non-existent window"));
719
720 return FALSE;
721 }
722
723 SizeWindows();
724
725 return TRUE;
726 }
727
728 void wxSplitterWindow::SetSashPosition(int position, bool redraw)
729 {
730 m_sashPosition = position;
731
732 if ( redraw )
733 {
734 SizeWindows();
735 }
736 }
737
738 // Initialize colours
739 void wxSplitterWindow::InitColours()
740 {
741 wxDELETE( m_facePen );
742 wxDELETE( m_faceBrush );
743 wxDELETE( m_mediumShadowPen );
744 wxDELETE( m_darkShadowPen );
745 wxDELETE( m_lightShadowPen );
746 wxDELETE( m_hilightPen );
747
748 // Shadow colours
749 #if defined(__WIN95__)
750 wxColour faceColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
751 m_facePen = new wxPen(faceColour, 1, wxSOLID);
752 m_faceBrush = new wxBrush(faceColour, wxSOLID);
753
754 wxColour mediumShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DSHADOW));
755 m_mediumShadowPen = new wxPen(mediumShadowColour, 1, wxSOLID);
756
757 wxColour darkShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DDKSHADOW));
758 m_darkShadowPen = new wxPen(darkShadowColour, 1, wxSOLID);
759
760 wxColour lightShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT));
761 m_lightShadowPen = new wxPen(lightShadowColour, 1, wxSOLID);
762
763 wxColour hilightColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DHILIGHT));
764 m_hilightPen = new wxPen(hilightColour, 1, wxSOLID);
765 #else // !Win32
766 m_facePen = new wxPen("LIGHT GREY", 1, wxSOLID);
767 m_faceBrush = new wxBrush("LIGHT GREY", wxSOLID);
768 m_mediumShadowPen = new wxPen("GREY", 1, wxSOLID);
769 m_darkShadowPen = new wxPen("BLACK", 1, wxSOLID);
770 m_lightShadowPen = new wxPen("LIGHT GREY", 1, wxSOLID);
771 m_hilightPen = new wxPen("WHITE", 1, wxSOLID);
772 #endif // Win32/!Win32
773 }
774
775 void wxSplitterWindow::SendUnsplitEvent(wxWindow *winRemoved)
776 {
777 wxSplitterEvent event(wxEVT_COMMAND_SPLITTER_UNSPLIT, this);
778 event.m_data.win = winRemoved;
779
780 (void)GetEventHandler()->ProcessEvent(event);
781 }
782
783 // ---------------------------------------------------------------------------
784 // splitter event handlers
785 // ---------------------------------------------------------------------------
786
787 void wxSplitterWindow::OnSashPosChanged(wxSplitterEvent& event)
788 {
789 // If within UNSPLIT_THRESHOLD from edge, set to edge to cause closure.
790 const int UNSPLIT_THRESHOLD = 4;
791
792 int newSashPosition = event.GetSashPosition();
793
794 // Obtain relevant window dimension for bottom / right threshold check
795 int w, h;
796 GetClientSize(&w, &h);
797 int window_size = (m_splitMode == wxSPLIT_VERTICAL) ? w : h ;
798
799 bool unsplit_scenario = FALSE;
800 if ( m_permitUnsplitAlways
801 || m_minimumPaneSize == 0 )
802 {
803 // Do edge detection if unsplit premitted
804 if ( newSashPosition <= UNSPLIT_THRESHOLD )
805 {
806 // threshold top / left check
807 newSashPosition = 0;
808 unsplit_scenario = TRUE;
809 }
810 if ( newSashPosition >= window_size - UNSPLIT_THRESHOLD )
811 {
812 // threshold bottom/right check
813 newSashPosition = window_size;
814 unsplit_scenario = TRUE;
815 }
816 }
817
818 if ( !unsplit_scenario )
819 {
820 // If resultant pane would be too small, enlarge it
821 if ( newSashPosition < m_minimumPaneSize )
822 newSashPosition = m_minimumPaneSize;
823 if ( newSashPosition > window_size - m_minimumPaneSize )
824 newSashPosition = window_size - m_minimumPaneSize;
825 }
826
827 // If the result is out of bounds it means minimum size is too big,
828 // so split window in half as best compromise.
829 if ( newSashPosition < 0 || newSashPosition > window_size )
830 newSashPosition = window_size / 2;
831
832 // for compatibility, call the virtual function
833 if ( !OnSashPositionChange(newSashPosition) )
834 {
835 newSashPosition = -1;
836 }
837
838 event.SetSashPosition(newSashPosition);
839 }
840
841 // Called when the sash is double-clicked. The default behaviour is to remove
842 // the sash if the minimum pane size is zero.
843 void wxSplitterWindow::OnDoubleClick(wxSplitterEvent& event)
844 {
845 // for compatibility, call the virtual function
846 OnDoubleClickSash(event.GetX(), event.GetY());
847
848 if ( GetMinimumPaneSize() == 0
849 || m_permitUnsplitAlways)
850 {
851 Unsplit();
852 }
853 }
854
855 void wxSplitterWindow::OnUnsplitEvent(wxSplitterEvent& event)
856 {
857 wxWindow *win = event.GetWindowBeingRemoved();
858
859 // for compatibility, call the virtual function
860 OnUnsplit(win);
861
862 win->Show(FALSE);
863 }