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