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