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