]> git.saurik.com Git - wxWidgets.git/blob - src/generic/splitter.cpp
stop usign a deprecated method, and ensure that the imagelist
[wxWidgets.git] / src / generic / splitter.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/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
9 // Licence: wxWindows licence
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 #if wxUSE_SPLITTER
20 #ifdef __BORLANDC__
21 #pragma hdrstop
22 #endif
23
24 #ifndef WX_PRECOMP
25 #include "wx/string.h"
26 #include "wx/utils.h"
27 #include "wx/log.h"
28
29 #include "wx/dcscreen.h"
30
31 #include "wx/window.h"
32 #include "wx/dialog.h"
33 #include "wx/frame.h"
34
35 #include "wx/settings.h"
36 #endif
37
38 #include "wx/splitter.h"
39
40 #include <stdlib.h>
41
42 DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED)
43 DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING)
44 DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPLITTER_DOUBLECLICKED)
45 DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPLITTER_UNSPLIT)
46
47 IMPLEMENT_DYNAMIC_CLASS(wxSplitterWindow, wxWindow)
48 IMPLEMENT_DYNAMIC_CLASS(wxSplitterEvent, wxNotifyEvent)
49
50 BEGIN_EVENT_TABLE(wxSplitterWindow, wxWindow)
51 EVT_PAINT(wxSplitterWindow::OnPaint)
52 EVT_SIZE(wxSplitterWindow::OnSize)
53 EVT_MOUSE_EVENTS(wxSplitterWindow::OnMouseEvent)
54
55 #if defined( __WXMSW__ ) || defined( __WXMAC__)
56 EVT_SET_CURSOR(wxSplitterWindow::OnSetCursor)
57 #endif // wxMSW
58
59 WX_EVENT_TABLE_CONTROL_CONTAINER(wxSplitterWindow)
60 END_EVENT_TABLE()
61
62 WX_DELEGATE_TO_CONTROL_CONTAINER(wxSplitterWindow);
63
64 bool wxSplitterWindow::Create(wxWindow *parent, wxWindowID id,
65 const wxPoint& pos,
66 const wxSize& size,
67 long style,
68 const wxString& name)
69 {
70 // allow TABbing from one window to the other
71 style |= wxTAB_TRAVERSAL;
72
73 if (!wxWindow::Create(parent, id, pos, size, style, name))
74 return FALSE;
75
76 m_permitUnsplitAlways = (style & wxSP_PERMIT_UNSPLIT) != 0;
77
78 if ( style & wxSP_3DSASH )
79 m_sashSize = 7;
80 else
81 m_sashSize = 3;
82
83 if ( style & wxSP_3DBORDER )
84 m_borderSize = 2;
85 else if ( style & wxSP_BORDER )
86 m_borderSize = 1;
87 else
88 m_borderSize = 0;
89
90 #ifdef __WXMAC__
91 int major,minor;
92 wxGetOsVersion( &major, &minor );
93 if (major >= 10)
94 m_windowStyle |= wxSP_SASH_AQUA;
95 #endif
96
97 return TRUE;
98 }
99
100 void wxSplitterWindow::Init()
101 {
102 m_container.SetContainerWindow(this);
103
104 m_splitMode = wxSPLIT_VERTICAL;
105 m_permitUnsplitAlways = TRUE;
106 m_windowOne = (wxWindow *) NULL;
107 m_windowTwo = (wxWindow *) NULL;
108 m_dragMode = wxSPLIT_DRAG_NONE;
109 m_oldX = 0;
110 m_oldY = 0;
111 m_firstX = 0;
112 m_firstY = 0;
113 m_sashSize = 3;
114 m_borderSize = 0;
115 m_sashPosition = m_requestedSashPosition = 0;
116 m_minimumPaneSize = 0;
117 m_sashCursorWE = wxCursor(wxCURSOR_SIZEWE);
118 m_sashCursorNS = wxCursor(wxCURSOR_SIZENS);
119 m_sashTrackerPen = new wxPen(*wxBLACK, 2, wxSOLID);
120 m_lightShadowPen = (wxPen *) NULL;
121 m_mediumShadowPen = (wxPen *) NULL;
122 m_darkShadowPen = (wxPen *) NULL;
123 m_faceBrush = (wxBrush *) NULL;
124 m_facePen = (wxPen *) NULL;
125 m_hilightPen = (wxPen *) NULL;
126
127 InitColours();
128
129 m_needUpdating = FALSE;
130 }
131
132 wxSplitterWindow::~wxSplitterWindow()
133 {
134 delete m_sashTrackerPen;
135 delete m_lightShadowPen;
136 delete m_darkShadowPen;
137 delete m_mediumShadowPen;
138 delete m_hilightPen;
139 delete m_facePen;
140 delete m_faceBrush;
141 }
142
143 void wxSplitterWindow::SetResizeCursor()
144 {
145 SetCursor(m_splitMode == wxSPLIT_VERTICAL ? m_sashCursorWE
146 : m_sashCursorNS);
147 }
148
149 void wxSplitterWindow::OnPaint(wxPaintEvent& WXUNUSED(event))
150 {
151 wxPaintDC dc(this);
152
153 if ( m_borderSize > 0 )
154 DrawBorders(dc);
155 DrawSash(dc);
156 }
157
158 void wxSplitterWindow::OnInternalIdle()
159 {
160 wxWindow::OnInternalIdle();
161
162 if (m_needUpdating)
163 SizeWindows();
164 }
165
166 void wxSplitterWindow::OnMouseEvent(wxMouseEvent& event)
167 {
168 int x = (int)event.GetX(),
169 y = (int)event.GetY();
170
171 #if defined(__WXMSW__)
172 // SetCursor(wxCursor()); // Is this required?
173 #endif
174
175 if (GetWindowStyle() & wxSP_NOSASH)
176 return;
177
178 // with wxSP_LIVE_UPDATE style the splitter windows are always resized
179 // following the mouse movement while it drags the sash, without it we only
180 // draw the sash at the new position but only resize the windows when the
181 // dragging is finished
182 bool isLive = (GetWindowStyleFlag() & wxSP_LIVE_UPDATE) != 0;
183
184 if (event.LeftDown())
185 {
186 if ( SashHitTest(x, y) )
187 {
188 // Start the drag now
189 m_dragMode = wxSPLIT_DRAG_DRAGGING;
190
191 // Capture mouse and set the cursor
192 CaptureMouse();
193 SetResizeCursor();
194
195 if ( !isLive )
196 {
197 // remember the initial sash position and draw the initial
198 // shadow sash
199 m_sashPositionCurrent = m_sashPosition;
200
201 DrawSashTracker(x, y);
202 }
203
204 m_oldX = x;
205 m_oldY = y;
206
207 SetResizeCursor();
208 return;
209 }
210 }
211 else if (event.LeftUp() && m_dragMode == wxSPLIT_DRAG_DRAGGING)
212 {
213 // We can stop dragging now and see what we've got.
214 m_dragMode = wxSPLIT_DRAG_NONE;
215
216 // Release mouse and unset the cursor
217 ReleaseMouse();
218 SetCursor(* wxSTANDARD_CURSOR);
219
220 // exit if unsplit after doubleclick
221 if ( !IsSplit() )
222 {
223 return;
224 }
225
226 // Erase old tracker
227 if ( !isLive )
228 {
229 DrawSashTracker(m_oldX, m_oldY);
230 }
231
232 // the position of the click doesn't exactly correspond to
233 // m_sashPosition, rather it changes it by the distance by which the
234 // mouse has moved
235 int diff = m_splitMode == wxSPLIT_VERTICAL ? x - m_oldX : y - m_oldY;
236
237 int posSashOld = isLive ? m_sashPosition : m_sashPositionCurrent;
238 int posSashNew = OnSashPositionChanging(posSashOld + diff);
239 if ( posSashNew == -1 )
240 {
241 // change not allowed
242 return;
243 }
244
245 if ( m_permitUnsplitAlways || m_minimumPaneSize == 0 )
246 {
247 // Deal with possible unsplit scenarios
248 if ( posSashNew == 0 )
249 {
250 // We remove the first window from the view
251 wxWindow *removedWindow = m_windowOne;
252 m_windowOne = m_windowTwo;
253 m_windowTwo = (wxWindow *) NULL;
254 OnUnsplit(removedWindow);
255 wxSplitterEvent event(wxEVT_COMMAND_SPLITTER_UNSPLIT, this);
256 event.m_data.win = removedWindow;
257 (void)DoSendEvent(event);
258 SetSashPositionAndNotify(0);
259 }
260 else if ( posSashNew == GetWindowSize() )
261 {
262 // We remove the second window from the view
263 wxWindow *removedWindow = m_windowTwo;
264 m_windowTwo = (wxWindow *) NULL;
265 OnUnsplit(removedWindow);
266 wxSplitterEvent event(wxEVT_COMMAND_SPLITTER_UNSPLIT, this);
267 event.m_data.win = removedWindow;
268 (void)DoSendEvent(event);
269 SetSashPositionAndNotify(0);
270 }
271 else
272 {
273 SetSashPositionAndNotify(posSashNew);
274 }
275 }
276 else
277 {
278 SetSashPositionAndNotify(posSashNew);
279 }
280
281 SizeWindows();
282 } // left up && dragging
283 else if ((event.Moving() || event.Leaving() || event.Entering()) && (m_dragMode == wxSPLIT_DRAG_NONE))
284 {
285 // Just change the cursor as required
286 if ( !event.Leaving() && SashHitTest(x, y) )
287 {
288 SetResizeCursor();
289 }
290 else
291 {
292 SetCursor(* wxSTANDARD_CURSOR);
293 }
294 }
295 else if (event.Dragging() && (m_dragMode == wxSPLIT_DRAG_DRAGGING))
296 {
297 int diff = m_splitMode == wxSPLIT_VERTICAL ? x - m_oldX : y - m_oldY;
298 if ( !diff )
299 {
300 // nothing to do, mouse didn't really move far enough
301 return;
302 }
303
304 int posSashOld = isLive ? m_sashPosition : m_sashPositionCurrent;
305 int posSashNew = OnSashPositionChanging(posSashOld + diff);
306 if ( posSashNew == -1 )
307 {
308 // change not allowed
309 return;
310 }
311
312 if ( posSashNew == m_sashPosition )
313 return;
314
315 // Erase old tracker
316 if ( !isLive )
317 {
318 DrawSashTracker(m_oldX, m_oldY);
319 }
320
321 if (m_splitMode == wxSPLIT_VERTICAL)
322 x = posSashNew;
323 else
324 y = posSashNew;
325
326 // Remember old positions
327 m_oldX = x;
328 m_oldY = y;
329
330 #ifdef __WXMSW__
331 // As we captured the mouse, we may get the mouse events from outside
332 // our window - for example, negative values in x, y. This has a weird
333 // consequence under MSW where we use unsigned values sometimes and
334 // signed ones other times: the coordinates turn as big positive
335 // numbers and so the sash is drawn on the *right* side of the window
336 // instead of the left (or bottom instead of top). Correct this.
337 if ( (short)m_oldX < 0 )
338 m_oldX = 0;
339 if ( (short)m_oldY < 0 )
340 m_oldY = 0;
341 #endif // __WXMSW__
342
343 // Draw new one
344 if ( !isLive )
345 {
346 m_sashPositionCurrent = posSashNew;
347
348 DrawSashTracker(m_oldX, m_oldY);
349 }
350 else
351 {
352 SetSashPositionAndNotify(posSashNew);
353 m_needUpdating = TRUE;
354 }
355 }
356 else if ( event.LeftDClick() && m_windowTwo )
357 {
358 OnDoubleClickSash(x, y);
359 }
360 }
361
362 void wxSplitterWindow::OnSize(wxSizeEvent& event)
363 {
364 // only process this message if we're not iconized - otherwise iconizing
365 // and restoring a window containing the splitter has a funny side effect
366 // of changing the splitter position!
367 wxWindow *parent = GetParent();
368 while ( parent && !parent->IsTopLevel() )
369 {
370 parent = parent->GetParent();
371 }
372
373 bool iconized = FALSE;
374
375 // wxMotif doesn't yet have a wxTopLevelWindow implementation
376 #ifdef __WXMOTIF__
377 wxFrame *winTop = wxDynamicCast(parent, wxFrame);
378 #else
379 wxTopLevelWindow *winTop = wxDynamicCast(parent, wxTopLevelWindow);
380 #endif
381 if ( winTop )
382 {
383 iconized = winTop->IsIconized();
384 }
385 #ifndef __WXMOTIF__
386 else
387 {
388 wxFAIL_MSG(wxT("should have a top level parent!"));
389
390 iconized = FALSE;
391 }
392 #endif
393
394 if ( iconized )
395 {
396 event.Skip();
397
398 return;
399 }
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 SetSashPositionAndNotify(wxMax(10, cw - 40));
409 }
410 else // m_splitMode == wxSPLIT_HORIZONTAL
411 {
412 if ( m_sashPosition >= (ch - 5) )
413 SetSashPositionAndNotify(wxMax(10, ch - 40));
414 }
415 }
416
417 SizeWindows();
418 }
419
420 bool wxSplitterWindow::SashHitTest(int x, int y, int tolerance)
421 {
422 if ( m_windowTwo == NULL || m_sashPosition == 0)
423 return FALSE; // No sash
424
425 if ( m_splitMode == wxSPLIT_VERTICAL )
426 {
427 if ( (x >= m_sashPosition - tolerance) && (x <= m_sashPosition + m_sashSize + tolerance) )
428 return TRUE;
429 else
430 return FALSE;
431 }
432 else
433 {
434 if ( (y >= (m_sashPosition- tolerance)) && (y <= (m_sashPosition + m_sashSize + tolerance)) )
435 return TRUE;
436 else
437 return FALSE;
438 }
439 }
440
441 // Draw 3D effect borders
442 void wxSplitterWindow::DrawBorders(wxDC& dc)
443 {
444 int w, h;
445 GetClientSize(&w, &h);
446
447 if ( GetWindowStyleFlag() & wxSP_3DBORDER )
448 {
449
450 dc.SetPen(*m_facePen);
451 dc.SetBrush(*m_faceBrush);
452 dc.DrawRectangle(1, 1 , w-1, m_borderSize-2 ); //high
453 dc.DrawRectangle(1, m_borderSize-2 , m_borderSize-2, h-1 ); // left
454 dc.DrawRectangle(w-m_borderSize+2, m_borderSize-2 , w-1, h-1 ); // right
455 dc.DrawRectangle(m_borderSize-2, h-m_borderSize+2 , w-m_borderSize+2, h-1 ); //bottom
456
457 dc.SetPen(*m_mediumShadowPen);
458 dc.DrawLine(m_borderSize-2, m_borderSize-2, w-m_borderSize+1, m_borderSize-2);
459 dc.DrawLine(m_borderSize-2, m_borderSize-2, m_borderSize-2, h-m_borderSize+1);
460
461 dc.SetPen(*m_darkShadowPen);
462 dc.DrawLine(m_borderSize-1, m_borderSize-1, w-m_borderSize, m_borderSize-1);
463 dc.DrawLine(m_borderSize-1, m_borderSize-1, m_borderSize-1, h-m_borderSize);
464
465 dc.SetPen(*m_hilightPen);
466 dc.DrawLine(m_borderSize - 2, h-m_borderSize+1, w-m_borderSize+1, h-m_borderSize+1);
467 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.
468 /// Anyway, h is required for MSW.
469
470 dc.SetPen(*m_lightShadowPen);
471 dc.DrawLine(w-m_borderSize, m_borderSize-1, w-m_borderSize, h-m_borderSize); // Right hand side
472 dc.DrawLine(m_borderSize-1, h-m_borderSize, w-m_borderSize+1, h-m_borderSize); // Bottom
473 }
474 else if ( GetWindowStyleFlag() & wxSP_BORDER )
475 {
476 dc.SetBrush(*wxTRANSPARENT_BRUSH);
477 dc.SetPen(*wxBLACK_PEN);
478 dc.DrawRectangle(0, 0, w-1, h-1);
479 }
480
481 dc.SetPen(wxNullPen);
482 dc.SetBrush(wxNullBrush);
483 }
484
485 // Draw the sash
486 void wxSplitterWindow::DrawSash(wxDC& dc)
487 {
488 if ( m_sashPosition == 0 || !m_windowTwo)
489 return;
490 if (GetWindowStyle() & wxSP_NOSASH)
491 return;
492
493 int w, h;
494 GetClientSize(&w, &h);
495
496 if ( GetWindowStyleFlag() & wxSP_3DSASH )
497 {
498 if ( m_splitMode == wxSPLIT_VERTICAL )
499 {
500 dc.SetPen(*m_facePen);
501
502 if (HasFlag( wxSP_SASH_AQUA ))
503 dc.SetBrush(*wxWHITE_BRUSH);
504 else
505 dc.SetBrush(*m_faceBrush);
506 dc.DrawRectangle(m_sashPosition + 2, 0 , m_sashSize - 4, h );
507
508 dc.SetBrush(*wxTRANSPARENT_BRUSH);
509
510 dc.SetPen(*m_lightShadowPen);
511 int xShadow = m_borderSize ? m_borderSize - 1 : 0 ;
512 dc.DrawLine(m_sashPosition, xShadow , m_sashPosition, h-m_borderSize);
513
514 dc.SetPen(*m_hilightPen);
515 dc.DrawLine(m_sashPosition+1, m_borderSize - 2, m_sashPosition+1, h - m_borderSize+2);
516
517 if (!HasFlag( wxSP_SASH_AQUA ))
518 dc.SetPen(*m_mediumShadowPen);
519
520 int yMedium = m_borderSize ? h-m_borderSize+1 : h ;
521 dc.DrawLine(m_sashPosition+m_sashSize-2, xShadow, m_sashPosition+m_sashSize-2, yMedium);
522
523 if (HasFlag( wxSP_SASH_AQUA ))
524 dc.SetPen(*m_lightShadowPen);
525 else
526 dc.SetPen(*m_darkShadowPen);
527 dc.DrawLine(m_sashPosition+m_sashSize-1, m_borderSize, m_sashPosition+m_sashSize-1, h-m_borderSize );
528
529 // Draw the top and bottom edges of the sash, if requested
530 if (GetWindowStyle() & wxSP_FULLSASH)
531 {
532 // Top
533 dc.SetPen(*m_hilightPen);
534 dc.DrawLine(m_sashPosition+1, m_borderSize, m_sashPosition+m_sashSize-1, m_borderSize);
535
536 // Bottom
537 dc.SetPen(*m_darkShadowPen);
538 dc.DrawLine(m_sashPosition+1, h-m_borderSize-1, m_sashPosition+m_sashSize-1, h-m_borderSize-1);
539 }
540 }
541 else // wxSPLIT_HORIZONTAL
542 {
543 dc.SetPen(*m_facePen);
544 if (HasFlag( wxSP_SASH_AQUA ))
545 dc.SetBrush(*wxWHITE_BRUSH);
546 else
547 dc.SetBrush(*m_faceBrush);
548 dc.DrawRectangle( m_borderSize-2, m_sashPosition + 2, w-m_borderSize+2, m_sashSize - 4);
549
550 dc.SetBrush(*wxTRANSPARENT_BRUSH);
551
552 dc.SetPen(*m_lightShadowPen);
553 dc.DrawLine(m_borderSize-1, m_sashPosition, w-m_borderSize, m_sashPosition);
554
555 dc.SetPen(*m_hilightPen);
556 dc.DrawLine(m_borderSize-2, m_sashPosition+1, w-m_borderSize+1, m_sashPosition+1);
557
558 if (!HasFlag( wxSP_SASH_AQUA ))
559 dc.SetPen(*m_mediumShadowPen);
560 dc.DrawLine(m_borderSize-1, m_sashPosition+m_sashSize-2, w-m_borderSize+1, m_sashPosition+m_sashSize-2);
561
562 if (HasFlag( wxSP_SASH_AQUA ))
563 dc.SetPen(*m_lightShadowPen);
564 else
565 dc.SetPen(*m_darkShadowPen);
566 dc.DrawLine(m_borderSize, m_sashPosition+m_sashSize-1, w-m_borderSize, m_sashPosition+m_sashSize-1);
567
568 // Draw the left and right edges of the sash, if requested
569 if (GetWindowStyle() & wxSP_FULLSASH)
570 {
571 // Left
572 dc.SetPen(*m_hilightPen);
573 dc.DrawLine(m_borderSize, m_sashPosition, m_borderSize, m_sashPosition+m_sashSize);
574
575 // Right
576 dc.SetPen(*m_darkShadowPen);
577 dc.DrawLine(w-m_borderSize-1, m_sashPosition+1, w-m_borderSize-1, m_sashPosition+m_sashSize-1);
578 }
579 }
580 }
581 else // !wxSP_3DSASH
582 {
583 dc.SetPen(*wxTRANSPARENT_PEN);
584 dc.SetBrush(*m_faceBrush);
585 if ( m_splitMode == wxSPLIT_VERTICAL )
586 {
587 int h1 = h-1;
588 int y1 = 0;
589 if ( (GetWindowStyleFlag() & wxSP_BORDER) != wxSP_BORDER && (GetWindowStyleFlag() & wxSP_3DBORDER) != wxSP_3DBORDER )
590 h1 += 1; // Not sure why this is necessary...
591 if ( (GetWindowStyleFlag() & wxSP_3DBORDER) == wxSP_3DBORDER)
592 {
593 y1 = 2; h1 -= 3;
594 }
595 dc.DrawRectangle(m_sashPosition, y1, m_sashSize, h1);
596 }
597 else // wxSPLIT_HORIZONTAL
598 {
599 int w1 = w-1;
600 int x1 = 0;
601 if ( (GetWindowStyleFlag() & wxSP_BORDER) != wxSP_BORDER && (GetWindowStyleFlag() & wxSP_3DBORDER) != wxSP_3DBORDER )
602 w1 ++;
603 if ( (GetWindowStyleFlag() & wxSP_3DBORDER) == wxSP_3DBORDER)
604 {
605 x1 = 2; w1 -= 3;
606 }
607 dc.DrawRectangle(x1, m_sashPosition, w1, m_sashSize);
608 }
609 }
610
611 dc.SetPen(wxNullPen);
612 dc.SetBrush(wxNullBrush);
613 }
614
615 // Draw the sash tracker (for whilst moving the sash)
616 void wxSplitterWindow::DrawSashTracker(int x, int y)
617 {
618 int w, h;
619 GetClientSize(&w, &h);
620
621 wxScreenDC screenDC;
622 int x1, y1;
623 int x2, y2;
624
625 if ( m_splitMode == wxSPLIT_VERTICAL )
626 {
627 x1 = x; y1 = 2;
628 x2 = x; y2 = h-2;
629
630 if ( x1 > w )
631 {
632 x1 = w; x2 = w;
633 }
634 else if ( x1 < 0 )
635 {
636 x1 = 0; x2 = 0;
637 }
638 }
639 else
640 {
641 x1 = 2; y1 = y;
642 x2 = w-2; y2 = y;
643
644 if ( y1 > h )
645 {
646 y1 = h;
647 y2 = h;
648 }
649 else if ( y1 < 0 )
650 {
651 y1 = 0;
652 y2 = 0;
653 }
654 }
655
656 ClientToScreen(&x1, &y1);
657 ClientToScreen(&x2, &y2);
658
659 screenDC.SetLogicalFunction(wxINVERT);
660 screenDC.SetPen(*m_sashTrackerPen);
661 screenDC.SetBrush(*wxTRANSPARENT_BRUSH);
662
663 screenDC.DrawLine(x1, y1, x2, y2);
664
665 screenDC.SetLogicalFunction(wxCOPY);
666
667 screenDC.SetPen(wxNullPen);
668 screenDC.SetBrush(wxNullBrush);
669 }
670
671 int wxSplitterWindow::GetWindowSize() const
672 {
673 wxSize size = GetClientSize();
674
675 return m_splitMode == wxSPLIT_VERTICAL ? size.x : size.y;
676 }
677
678 int wxSplitterWindow::AdjustSashPosition(int sashPos) const
679 {
680 int window_size = GetWindowSize();
681
682 wxWindow *win;
683
684 win = GetWindow1();
685 if ( win )
686 {
687 // the window shouldn't be smaller than its own minimal size nor
688 // smaller than the minimual pane size specified for this splitter
689 int minSize = m_splitMode == wxSPLIT_VERTICAL ? win->GetMinWidth()
690 : win->GetMinHeight();
691
692 if ( minSize == -1 || m_minimumPaneSize > minSize )
693 minSize = m_minimumPaneSize;
694
695 minSize += GetBorderSize();
696
697 if ( sashPos < minSize )
698 sashPos = minSize;
699 }
700
701 win = GetWindow2();
702 if ( win )
703 {
704 int minSize = m_splitMode == wxSPLIT_VERTICAL ? win->GetMinWidth()
705 : win->GetMinHeight();
706
707 if ( minSize == -1 || m_minimumPaneSize > minSize )
708 minSize = m_minimumPaneSize;
709
710 int maxSize = window_size - minSize - GetBorderSize();
711 if ( sashPos > maxSize )
712 sashPos = maxSize;
713 }
714
715 return sashPos;
716 }
717
718 bool wxSplitterWindow::DoSetSashPosition(int sashPos)
719 {
720 int newSashPosition = AdjustSashPosition(sashPos);
721
722 if ( newSashPosition == m_sashPosition )
723 return FALSE;
724
725 m_sashPosition = newSashPosition;
726
727 return TRUE;
728 }
729
730 void wxSplitterWindow::SetSashPositionAndNotify(int sashPos)
731 {
732 if ( DoSetSashPosition(sashPos) )
733 {
734 wxSplitterEvent event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED, this);
735 event.m_data.pos = m_sashPosition;
736
737 (void)DoSendEvent(event);
738 }
739 }
740
741 // Position and size subwindows.
742 // Note that the border size applies to each subwindow, not
743 // including the edges next to the sash.
744 void wxSplitterWindow::SizeWindows()
745 {
746 // check if we have delayed setting the real sash position
747 if ( m_requestedSashPosition != INT_MAX )
748 {
749 int newSashPosition = ConvertSashPosition(m_requestedSashPosition);
750 if ( newSashPosition != m_sashPosition )
751 {
752 DoSetSashPosition(newSashPosition);
753 }
754
755 if ( newSashPosition <= m_sashPosition
756 && newSashPosition >= m_sashPosition - GetBorderSize() )
757 {
758 // don't update it any more
759 m_requestedSashPosition = INT_MAX;
760 }
761 }
762
763 int w, h;
764 GetClientSize(&w, &h);
765
766 if ( GetWindow1() && !GetWindow2() )
767 {
768 GetWindow1()->SetSize(GetBorderSize(), GetBorderSize(),
769 w - 2*GetBorderSize(), h - 2*GetBorderSize());
770 }
771 else if ( GetWindow1() && GetWindow2() )
772 {
773 if (GetSplitMode() == wxSPLIT_VERTICAL)
774 {
775 int x1 = GetBorderSize();
776 int y1 = GetBorderSize();
777 int w1 = GetSashPosition() - GetBorderSize();
778 int h1 = h - 2*GetBorderSize();
779
780 int x2 = GetSashPosition() + GetSashSize();
781 int y2 = GetBorderSize();
782 int w2 = w - 2*GetBorderSize() - GetSashSize() - w1;
783 int h2 = h - 2*GetBorderSize();
784
785 GetWindow1()->SetSize(x1, y1, w1, h1);
786 GetWindow2()->SetSize(x2, y2, w2, h2);
787 }
788 else
789 {
790 GetWindow1()->SetSize(GetBorderSize(), GetBorderSize(),
791 w - 2*GetBorderSize(), GetSashPosition() - GetBorderSize());
792 GetWindow2()->SetSize(GetBorderSize(), GetSashPosition() + GetSashSize(),
793 w - 2*GetBorderSize(), h - 2*GetBorderSize() - GetSashSize() - (GetSashPosition() - GetBorderSize()));
794 }
795 }
796 wxClientDC dc(this);
797 if ( GetBorderSize() > 0 )
798 DrawBorders(dc);
799 DrawSash(dc);
800
801 SetNeedUpdating(FALSE);
802 }
803
804 // Set pane for unsplit window
805 void wxSplitterWindow::Initialize(wxWindow *window)
806 {
807 wxASSERT_MSG( window && window->GetParent() == this,
808 _T("windows in the splitter should have it as parent!") );
809
810 m_windowOne = window;
811 m_windowTwo = (wxWindow *) NULL;
812 DoSetSashPosition(0);
813 }
814
815 // Associates the given window with window 2, drawing the appropriate sash
816 // and changing the split mode.
817 // Does nothing and returns FALSE if the window is already split.
818 bool wxSplitterWindow::DoSplit(wxSplitMode mode,
819 wxWindow *window1, wxWindow *window2,
820 int sashPosition)
821 {
822 if ( IsSplit() )
823 return FALSE;
824
825 wxCHECK_MSG( window1 && window2, FALSE,
826 _T("can not split with NULL window(s)") );
827
828 wxCHECK_MSG( window1->GetParent() == this && window2->GetParent() == this, FALSE,
829 _T("windows in the splitter should have it as parent!") );
830
831 m_splitMode = mode;
832 m_windowOne = window1;
833 m_windowTwo = window2;
834
835 // remember the sash position we want to set for later if we can't set it
836 // right now (e.g. because the window is too small)
837 m_requestedSashPosition = sashPosition;
838
839 DoSetSashPosition(ConvertSashPosition(sashPosition));
840
841 SizeWindows();
842
843 return TRUE;
844 }
845
846 int wxSplitterWindow::ConvertSashPosition(int sashPosition) const
847 {
848 if ( sashPosition > 0 )
849 {
850 return sashPosition;
851 }
852 else if ( sashPosition < 0 )
853 {
854 // It's negative so adding is subtracting
855 return GetWindowSize() + sashPosition;
856 }
857 else // sashPosition == 0
858 {
859 // default, put it in the centre
860 return GetWindowSize() / 2;
861 }
862 }
863
864 // Remove the specified (or second) window from the view
865 // Doesn't actually delete the window.
866 bool wxSplitterWindow::Unsplit(wxWindow *toRemove)
867 {
868 if ( ! IsSplit() )
869 return FALSE;
870
871 wxWindow *win = NULL;
872 if ( toRemove == NULL || toRemove == m_windowTwo)
873 {
874 win = m_windowTwo ;
875 m_windowTwo = (wxWindow *) NULL;
876 }
877 else if ( toRemove == m_windowOne )
878 {
879 win = m_windowOne ;
880 m_windowOne = m_windowTwo;
881 m_windowTwo = (wxWindow *) NULL;
882 }
883 else
884 {
885 wxFAIL_MSG(wxT("splitter: attempt to remove a non-existent window"));
886
887 return FALSE;
888 }
889
890 OnUnsplit(win);
891 DoSetSashPosition(0);
892 SizeWindows();
893
894 return TRUE;
895 }
896
897 // Replace a window with another one
898 bool wxSplitterWindow::ReplaceWindow(wxWindow *winOld, wxWindow *winNew)
899 {
900 wxCHECK_MSG( winOld, FALSE, wxT("use one of Split() functions instead") );
901 wxCHECK_MSG( winNew, FALSE, wxT("use Unsplit() functions instead") );
902
903 if ( winOld == m_windowTwo )
904 {
905 m_windowTwo = winNew;
906 }
907 else if ( winOld == m_windowOne )
908 {
909 m_windowOne = winNew;
910 }
911 else
912 {
913 wxFAIL_MSG(wxT("splitter: attempt to replace a non-existent window"));
914
915 return FALSE;
916 }
917
918 SizeWindows();
919
920 return TRUE;
921 }
922
923 void wxSplitterWindow::SetMinimumPaneSize(int min)
924 {
925 m_minimumPaneSize = min;
926 SetSashPosition(m_sashPosition); // re-check limits
927 }
928
929 void wxSplitterWindow::SetSashPosition(int position, bool redraw)
930 {
931 DoSetSashPosition(position);
932
933 if ( redraw )
934 {
935 SizeWindows();
936 }
937 }
938
939 // Initialize colours
940 void wxSplitterWindow::InitColours()
941 {
942 wxDELETE( m_facePen );
943 wxDELETE( m_faceBrush );
944 wxDELETE( m_mediumShadowPen );
945 wxDELETE( m_darkShadowPen );
946 wxDELETE( m_lightShadowPen );
947 wxDELETE( m_hilightPen );
948
949 // Shadow colours
950 #ifndef __WIN16__
951 wxColour faceColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
952 m_facePen = new wxPen(faceColour, 1, wxSOLID);
953 m_faceBrush = new wxBrush(faceColour, wxSOLID);
954
955 wxColour mediumShadowColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW));
956 m_mediumShadowPen = new wxPen(mediumShadowColour, 1, wxSOLID);
957
958 wxColour darkShadowColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW));
959 m_darkShadowPen = new wxPen(darkShadowColour, 1, wxSOLID);
960
961 wxColour lightShadowColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT));
962 m_lightShadowPen = new wxPen(lightShadowColour, 1, wxSOLID);
963
964 wxColour hilightColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DHILIGHT));
965 m_hilightPen = new wxPen(hilightColour, 1, wxSOLID);
966 #else
967 m_facePen = new wxPen("LIGHT GREY", 1, wxSOLID);
968 m_faceBrush = new wxBrush("LIGHT GREY", wxSOLID);
969 m_mediumShadowPen = new wxPen("GREY", 1, wxSOLID);
970 m_darkShadowPen = new wxPen("BLACK", 1, wxSOLID);
971 m_lightShadowPen = new wxPen("LIGHT GREY", 1, wxSOLID);
972 m_hilightPen = new wxPen("WHITE", 1, wxSOLID);
973 #endif // __WIN16__
974 }
975
976 bool wxSplitterWindow::DoSendEvent(wxSplitterEvent& event)
977 {
978 return !GetEventHandler()->ProcessEvent(event) || event.IsAllowed();
979 }
980
981 // ---------------------------------------------------------------------------
982 // wxSplitterWindow virtual functions: they now just generate the events
983 // ---------------------------------------------------------------------------
984
985 bool wxSplitterWindow::OnSashPositionChange(int WXUNUSED(newSashPosition))
986 {
987 // always allow by default
988 return TRUE;
989 }
990
991 int wxSplitterWindow::OnSashPositionChanging(int newSashPosition)
992 {
993 // If within UNSPLIT_THRESHOLD from edge, set to edge to cause closure.
994 const int UNSPLIT_THRESHOLD = 4;
995
996 // first of all, check if OnSashPositionChange() doesn't forbid this change
997 if ( !OnSashPositionChange(newSashPosition) )
998 {
999 // it does
1000 return -1;
1001 }
1002
1003 // Obtain relevant window dimension for bottom / right threshold check
1004 int window_size = GetWindowSize();
1005
1006 bool unsplit_scenario = FALSE;
1007 if ( m_permitUnsplitAlways || m_minimumPaneSize == 0 )
1008 {
1009 // Do edge detection if unsplit premitted
1010 if ( newSashPosition <= UNSPLIT_THRESHOLD )
1011 {
1012 // threshold top / left check
1013 newSashPosition = 0;
1014 unsplit_scenario = TRUE;
1015 }
1016 if ( newSashPosition >= window_size - UNSPLIT_THRESHOLD )
1017 {
1018 // threshold bottom/right check
1019 newSashPosition = window_size;
1020 unsplit_scenario = TRUE;
1021 }
1022 }
1023
1024 if ( !unsplit_scenario )
1025 {
1026 // If resultant pane would be too small, enlarge it
1027 newSashPosition = AdjustSashPosition(newSashPosition);
1028 }
1029
1030 // If the result is out of bounds it means minimum size is too big,
1031 // so split window in half as best compromise.
1032 if ( newSashPosition < 0 || newSashPosition > window_size )
1033 newSashPosition = window_size / 2;
1034
1035 // now let the event handler have it
1036 //
1037 // FIXME: shouldn't we do it before the adjustments above so as to ensure
1038 // that the sash position is always reasonable?
1039 wxSplitterEvent event(wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING, this);
1040 event.m_data.pos = newSashPosition;
1041
1042 if ( !DoSendEvent(event) )
1043 {
1044 // the event handler vetoed the change
1045 newSashPosition = -1;
1046 }
1047 else
1048 {
1049 // it could have been changed by it
1050 newSashPosition = event.GetSashPosition();
1051 }
1052
1053 return newSashPosition;
1054 }
1055
1056 // Called when the sash is double-clicked. The default behaviour is to remove
1057 // the sash if the minimum pane size is zero.
1058 void wxSplitterWindow::OnDoubleClickSash(int x, int y)
1059 {
1060 wxCHECK_RET(m_windowTwo, wxT("splitter: no window to remove"));
1061
1062 // new code should handle events instead of using the virtual functions
1063 wxSplitterEvent event(wxEVT_COMMAND_SPLITTER_DOUBLECLICKED, this);
1064 event.m_data.pt.x = x;
1065 event.m_data.pt.y = y;
1066 if ( DoSendEvent(event) )
1067 {
1068 if ( GetMinimumPaneSize() == 0 || m_permitUnsplitAlways )
1069 {
1070 wxWindow* win = m_windowTwo;
1071 if ( Unsplit(win) )
1072 {
1073 wxSplitterEvent unsplitEvent(wxEVT_COMMAND_SPLITTER_UNSPLIT, this);
1074 unsplitEvent.m_data.win = win;
1075 (void)DoSendEvent(unsplitEvent);
1076 }
1077 }
1078 }
1079 //else: blocked by user
1080 }
1081
1082 void wxSplitterWindow::OnUnsplit(wxWindow *winRemoved)
1083 {
1084 // call this before calling the event handler which may delete the window
1085 winRemoved->Show(FALSE);
1086 }
1087
1088 #if defined( __WXMSW__ ) || defined( __WXMAC__)
1089
1090 // this is currently called (and needed) under MSW only...
1091 void wxSplitterWindow::OnSetCursor(wxSetCursorEvent& event)
1092 {
1093 // if we don't do it, the resizing cursor might be set for child window:
1094 // and like this we explicitly say that our cursor should not be used for
1095 // children windows which overlap us
1096
1097 if ( SashHitTest(event.GetX(), event.GetY()) )
1098 {
1099 // default processing is ok
1100 event.Skip();
1101 }
1102 //else: do nothing, in particular, don't call Skip()
1103 }
1104
1105 #endif // wxUSE_SPLITTER
1106 #endif // wxMSW
1107