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