]> git.saurik.com Git - wxWidgets.git/blob - src/generic/splitter.cpp
Added always-unsplittable style option (wxSP_PERMIT_UNSPLIT)
[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(wxXOR);
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 wxClientDC dc(this);
603 DrawBorders(dc);
604 DrawSash(dc);
605 }
606
607 // Set pane for unsplit window
608 void wxSplitterWindow::Initialize(wxWindow *window)
609 {
610 m_windowOne = window;
611 m_windowTwo = (wxWindow *) NULL;
612 m_sashPosition = 0;
613 }
614
615 // Associates the given window with window 2, drawing the appropriate sash
616 // and changing the split mode.
617 // Does nothing and returns FALSE if the window is already split.
618 bool wxSplitterWindow::SplitVertically(wxWindow *window1, wxWindow *window2, int sashPosition)
619 {
620 if ( IsSplit() )
621 return FALSE;
622
623 int w, h;
624 GetClientSize(&w, &h);
625
626 m_splitMode = wxSPLIT_VERTICAL;
627 m_windowOne = window1;
628 m_windowTwo = window2;
629 if ( sashPosition > 0 )
630 m_sashPosition = sashPosition;
631 else if ( sashPosition < 0 )
632 m_sashPosition = w - sashPosition;
633 else // default
634 m_sashPosition = w/2;
635
636 SizeWindows();
637
638 return TRUE;
639 }
640
641 bool wxSplitterWindow::SplitHorizontally(wxWindow *window1, wxWindow *window2, int sashPosition)
642 {
643 if ( IsSplit() )
644 return FALSE;
645
646 int w, h;
647 GetClientSize(&w, &h);
648
649 m_splitMode = wxSPLIT_HORIZONTAL;
650 m_windowOne = window1;
651 m_windowTwo = window2;
652 if ( sashPosition > 0 )
653 m_sashPosition = sashPosition;
654 else if ( sashPosition < 0 )
655 m_sashPosition = h - sashPosition;
656 else // default
657 m_sashPosition = h/2;
658
659 SizeWindows();
660
661 return TRUE;
662 }
663
664
665 // Remove the specified (or second) window from the view
666 // Doesn't actually delete the window.
667 bool wxSplitterWindow::Unsplit(wxWindow *toRemove)
668 {
669 if ( ! IsSplit() )
670 return FALSE;
671
672 wxWindow *win = NULL;
673 if ( toRemove == NULL || toRemove == m_windowTwo)
674 {
675 win = m_windowTwo ;
676 m_windowTwo = (wxWindow *) NULL;
677 }
678 else if ( toRemove == m_windowOne )
679 {
680 win = m_windowOne ;
681 m_windowOne = m_windowTwo;
682 m_windowTwo = (wxWindow *) NULL;
683 }
684 else
685 {
686 wxFAIL_MSG(_T("splitter: attempt to remove a non-existent window"));
687
688 return FALSE;
689 }
690
691 SendUnsplitEvent(win);
692 m_sashPosition = 0;
693 SizeWindows();
694
695 return TRUE;
696 }
697
698 // Replace a window with another one
699 bool wxSplitterWindow::ReplaceWindow(wxWindow *winOld, wxWindow *winNew)
700 {
701 wxCHECK_MSG( winOld, FALSE, _T("use one of Split() functions instead") );
702 wxCHECK_MSG( winNew, FALSE, _T("use Unsplit() functions instead") );
703
704 if ( winOld == m_windowTwo )
705 {
706 m_windowTwo = winNew;
707 }
708 else if ( winOld == m_windowOne )
709 {
710 m_windowOne = winNew;
711 }
712 else
713 {
714 wxFAIL_MSG(_T("splitter: attempt to replace a non-existent window"));
715
716 return FALSE;
717 }
718
719 SizeWindows();
720
721 return TRUE;
722 }
723
724 void wxSplitterWindow::SetSashPosition(int position, bool redraw)
725 {
726 m_sashPosition = position;
727
728 if ( redraw )
729 {
730 SizeWindows();
731 }
732 }
733
734 // Initialize colours
735 void wxSplitterWindow::InitColours()
736 {
737 wxDELETE( m_facePen );
738 wxDELETE( m_faceBrush );
739 wxDELETE( m_mediumShadowPen );
740 wxDELETE( m_darkShadowPen );
741 wxDELETE( m_lightShadowPen );
742 wxDELETE( m_hilightPen );
743
744 // Shadow colours
745 #if defined(__WIN95__)
746 wxColour faceColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
747 m_facePen = new wxPen(faceColour, 1, wxSOLID);
748 m_faceBrush = new wxBrush(faceColour, wxSOLID);
749
750 wxColour mediumShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DSHADOW));
751 m_mediumShadowPen = new wxPen(mediumShadowColour, 1, wxSOLID);
752
753 wxColour darkShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DDKSHADOW));
754 m_darkShadowPen = new wxPen(darkShadowColour, 1, wxSOLID);
755
756 wxColour lightShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT));
757 m_lightShadowPen = new wxPen(lightShadowColour, 1, wxSOLID);
758
759 wxColour hilightColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DHILIGHT));
760 m_hilightPen = new wxPen(hilightColour, 1, wxSOLID);
761 #else // !Win32
762 m_facePen = new wxPen("LIGHT GREY", 1, wxSOLID);
763 m_faceBrush = new wxBrush("LIGHT GREY", wxSOLID);
764 m_mediumShadowPen = new wxPen("GREY", 1, wxSOLID);
765 m_darkShadowPen = new wxPen("BLACK", 1, wxSOLID);
766 m_lightShadowPen = new wxPen("LIGHT GREY", 1, wxSOLID);
767 m_hilightPen = new wxPen("WHITE", 1, wxSOLID);
768 #endif // Win32/!Win32
769 }
770
771 void wxSplitterWindow::SendUnsplitEvent(wxWindow *winRemoved)
772 {
773 wxSplitterEvent event(wxEVT_COMMAND_SPLITTER_UNSPLIT, this);
774 event.m_data.win = winRemoved;
775
776 (void)GetEventHandler()->ProcessEvent(event);
777 }
778
779 // ---------------------------------------------------------------------------
780 // splitter event handlers
781 // ---------------------------------------------------------------------------
782
783 void wxSplitterWindow::OnSashPosChanged(wxSplitterEvent& event)
784 {
785 // If within UNSPLIT_THRESHOLD from edge, set to edge to cause closure.
786 const int UNSPLIT_THRESHOLD = 4;
787
788 int newSashPosition = event.GetSashPosition();
789
790 // Obtain relevant window dimension for bottom / right threshold check
791 int w, h;
792 GetClientSize(&w, &h);
793 int window_size = (m_splitMode == wxSPLIT_VERTICAL) ? w : h ;
794
795 bool unsplit_scenario = FALSE;
796 if ( m_permitUnsplitAlways
797 || m_minimumPaneSize == 0 )
798 {
799 // Do edge detection if unsplit premitted
800 if ( newSashPosition <= UNSPLIT_THRESHOLD )
801 {
802 // threshold top / left check
803 newSashPosition = 0;
804 unsplit_scenario = TRUE;
805 }
806 if ( newSashPosition >= window_size - UNSPLIT_THRESHOLD )
807 {
808 // threshold bottom/right check
809 newSashPosition = window_size;
810 unsplit_scenario = TRUE;
811 }
812 }
813
814 if ( !unsplit_scenario )
815 {
816 // If resultant pane would be too small, enlarge it
817 if ( newSashPosition < m_minimumPaneSize )
818 newSashPosition = m_minimumPaneSize;
819 if ( newSashPosition > window_size - m_minimumPaneSize )
820 newSashPosition = window_size - m_minimumPaneSize;
821 }
822
823 // If the result is out of bounds it means minimum size is too big,
824 // so split window in half as best compromise.
825 if ( newSashPosition < 0 || newSashPosition > window_size )
826 newSashPosition = window_size / 2;
827
828 // for compatibility, call the virtual function
829 if ( !OnSashPositionChange(newSashPosition) )
830 {
831 newSashPosition = -1;
832 }
833
834 event.SetSashPosition(newSashPosition);
835 }
836
837 // Called when the sash is double-clicked. The default behaviour is to remove
838 // the sash if the minimum pane size is zero.
839 void wxSplitterWindow::OnDoubleClick(wxSplitterEvent& event)
840 {
841 // for compatibility, call the virtual function
842 OnDoubleClickSash(event.GetX(), event.GetY());
843
844 if ( GetMinimumPaneSize() == 0
845 || m_permitUnsplitAlways)
846 {
847 Unsplit();
848 }
849 }
850
851 void wxSplitterWindow::OnUnsplitEvent(wxSplitterEvent& event)
852 {
853 wxWindow *win = event.GetWindowBeingRemoved();
854
855 // for compatibility, call the virtual function
856 OnUnsplit(win);
857
858 win->Show(FALSE);
859 }