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