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