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