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