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