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