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