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