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