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