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