]> git.saurik.com Git - wxWidgets.git/blob - samples/splitter/splitter.cpp
4fa75721c6b33ce8fb6adbeaf94f497d51ad3f1c
[wxWidgets.git] / samples / splitter / splitter.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: splitter.cpp
3 // Purpose: wxSplitterWindow sample
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #ifndef WX_PRECOMP
28 #include "wx/log.h"
29
30 #include "wx/app.h"
31 #include "wx/frame.h"
32
33 #include "wx/scrolwin.h"
34 #include "wx/menu.h"
35
36 #include "wx/textdlg.h" // for wxGetTextFromUser
37 #endif
38
39 #include "wx/splitter.h"
40 #include "wx/dcmirror.h"
41
42 #ifndef wxHAS_IMAGES_IN_RESOURCES
43 #include "../sample.xpm"
44 #endif
45
46 // ----------------------------------------------------------------------------
47 // constants
48 // ----------------------------------------------------------------------------
49
50 // ID for the menu commands
51 enum
52 {
53 SPLIT_QUIT = 1,
54 SPLIT_HORIZONTAL,
55 SPLIT_VERTICAL,
56 SPLIT_UNSPLIT,
57 SPLIT_LIVE,
58 SPLIT_BORDER,
59 SPLIT_3DSASH,
60 SPLIT_SETPOSITION,
61 SPLIT_SETMINSIZE,
62 SPLIT_SETGRAVITY,
63 SPLIT_REPLACE,
64 SPLIT_INVISIBLE
65 };
66
67 // ----------------------------------------------------------------------------
68 // our classes
69 // ----------------------------------------------------------------------------
70
71 class MyApp: public wxApp
72 {
73 public:
74 MyApp() { }
75
76 virtual bool OnInit();
77
78 wxDECLARE_NO_COPY_CLASS(MyApp);
79 };
80
81 class MyFrame: public wxFrame
82 {
83 public:
84 MyFrame();
85 virtual ~MyFrame();
86
87 void ToggleFlag(int flag, bool enable);
88
89 // Menu commands
90 void OnSplitHorizontal(wxCommandEvent& event);
91 void OnSplitVertical(wxCommandEvent& event);
92 void OnUnsplit(wxCommandEvent& event);
93 void OnToggleLive(wxCommandEvent& event)
94 { ToggleFlag(wxSP_LIVE_UPDATE, event.IsChecked()); }
95 void OnToggleBorder(wxCommandEvent& event)
96 { ToggleFlag(wxSP_BORDER, event.IsChecked()); }
97 void OnToggle3DSash(wxCommandEvent& event)
98 { ToggleFlag(wxSP_3DSASH, event.IsChecked()); }
99 void OnSetPosition(wxCommandEvent& event);
100 void OnSetMinSize(wxCommandEvent& event);
101 void OnSetGravity(wxCommandEvent& event);
102 void OnReplace(wxCommandEvent &event);
103 void OnToggleInvisible(wxCommandEvent &event);
104
105 void OnQuit(wxCommandEvent& event);
106
107 // Menu command update functions
108 void OnUpdateUIHorizontal(wxUpdateUIEvent& event);
109 void OnUpdateUIVertical(wxUpdateUIEvent& event);
110 void OnUpdateUIUnsplit(wxUpdateUIEvent& event);
111 void OnUpdateUIInvisible(wxUpdateUIEvent& event);
112
113 private:
114 wxWindow *m_left, *m_right;
115
116 wxSplitterWindow* m_splitter;
117 wxWindow *m_replacewindow;
118
119 DECLARE_EVENT_TABLE()
120 wxDECLARE_NO_COPY_CLASS(MyFrame);
121 };
122
123 class MySplitterWindow : public wxSplitterWindow
124 {
125 public:
126 MySplitterWindow(wxFrame *parent);
127
128 // event handlers
129 void OnPositionChanged(wxSplitterEvent& event);
130 void OnPositionChanging(wxSplitterEvent& event);
131 void OnDClick(wxSplitterEvent& event);
132 void OnUnsplitEvent(wxSplitterEvent& event);
133
134 private:
135 wxFrame *m_frame;
136
137 DECLARE_EVENT_TABLE()
138 wxDECLARE_NO_COPY_CLASS(MySplitterWindow);
139 };
140
141 class MyCanvas: public wxScrolledWindow
142 {
143 public:
144 MyCanvas(wxWindow* parent, bool mirror);
145 virtual ~MyCanvas(){};
146
147 virtual void OnDraw(wxDC& dc);
148
149 private:
150 bool m_mirror;
151
152 wxDECLARE_NO_COPY_CLASS(MyCanvas);
153 };
154
155 // ============================================================================
156 // implementation
157 // ============================================================================
158
159 // ----------------------------------------------------------------------------
160 // MyApp
161 // ----------------------------------------------------------------------------
162
163 IMPLEMENT_APP(MyApp)
164
165 bool MyApp::OnInit()
166 {
167 if ( !wxApp::OnInit() )
168 return false;
169
170 // create and show the main frame
171 MyFrame* frame = new MyFrame;
172
173 frame->Show(true);
174
175 return true;
176 }
177
178 // ----------------------------------------------------------------------------
179 // MyFrame
180 // ----------------------------------------------------------------------------
181
182 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
183 EVT_MENU(SPLIT_VERTICAL, MyFrame::OnSplitVertical)
184 EVT_MENU(SPLIT_HORIZONTAL, MyFrame::OnSplitHorizontal)
185 EVT_MENU(SPLIT_UNSPLIT, MyFrame::OnUnsplit)
186 EVT_MENU(SPLIT_LIVE, MyFrame::OnToggleLive)
187 EVT_MENU(SPLIT_BORDER, MyFrame::OnToggleBorder)
188 EVT_MENU(SPLIT_3DSASH, MyFrame::OnToggle3DSash)
189 EVT_MENU(SPLIT_SETPOSITION, MyFrame::OnSetPosition)
190 EVT_MENU(SPLIT_SETMINSIZE, MyFrame::OnSetMinSize)
191 EVT_MENU(SPLIT_SETGRAVITY, MyFrame::OnSetGravity)
192 EVT_MENU(SPLIT_REPLACE, MyFrame::OnReplace)
193 EVT_MENU(SPLIT_INVISIBLE, MyFrame::OnToggleInvisible)
194
195 EVT_MENU(SPLIT_QUIT, MyFrame::OnQuit)
196
197 EVT_UPDATE_UI(SPLIT_VERTICAL, MyFrame::OnUpdateUIVertical)
198 EVT_UPDATE_UI(SPLIT_HORIZONTAL, MyFrame::OnUpdateUIHorizontal)
199 EVT_UPDATE_UI(SPLIT_UNSPLIT, MyFrame::OnUpdateUIUnsplit)
200 EVT_UPDATE_UI(SPLIT_INVISIBLE, MyFrame::OnUpdateUIInvisible)
201 END_EVENT_TABLE()
202
203 // My frame constructor
204 MyFrame::MyFrame()
205 : wxFrame(NULL, wxID_ANY, wxT("wxSplitterWindow sample"),
206 wxDefaultPosition, wxSize(420, 300),
207 wxDEFAULT_FRAME_STYLE | wxNO_FULL_REPAINT_ON_RESIZE)
208 {
209 SetIcon(wxICON(sample));
210
211 #if wxUSE_STATUSBAR
212 CreateStatusBar(2);
213 #endif // wxUSE_STATUSBAR
214
215 // Make a menubar
216 wxMenu *splitMenu = new wxMenu;
217 splitMenu->Append(SPLIT_VERTICAL,
218 wxT("Split &Vertically\tCtrl-V"),
219 wxT("Split vertically"));
220 splitMenu->Append(SPLIT_HORIZONTAL,
221 wxT("Split &Horizontally\tCtrl-H"),
222 wxT("Split horizontally"));
223 splitMenu->Append(SPLIT_UNSPLIT,
224 wxT("&Unsplit\tCtrl-U"),
225 wxT("Unsplit"));
226 splitMenu->AppendCheckItem(SPLIT_INVISIBLE,
227 wxT("Toggle sash &invisibility\tCtrl-I"),
228 wxT("Toggle sash invisibility"));
229 splitMenu->AppendSeparator();
230
231 splitMenu->AppendCheckItem(SPLIT_LIVE,
232 wxT("&Live update\tCtrl-L"),
233 wxT("Toggle live update mode"));
234 splitMenu->AppendCheckItem(SPLIT_BORDER,
235 wxT("3D &Border"),
236 wxT("Toggle wxSP_BORDER flag"));
237 splitMenu->Check(SPLIT_BORDER, true);
238 splitMenu->AppendCheckItem(SPLIT_3DSASH,
239 wxT("&3D Sash"),
240 wxT("Toggle wxSP_3DSASH flag"));
241 splitMenu->Check(SPLIT_3DSASH, true);
242 splitMenu->Append(SPLIT_SETPOSITION,
243 wxT("Set splitter &position\tCtrl-P"),
244 wxT("Set the splitter position"));
245 splitMenu->Append(SPLIT_SETMINSIZE,
246 wxT("Set &min size\tCtrl-M"),
247 wxT("Set minimum pane size"));
248 splitMenu->Append(SPLIT_SETGRAVITY,
249 wxT("Set &gravity\tCtrl-G"),
250 wxT("Set gravity of sash"));
251 splitMenu->AppendSeparator();
252
253 splitMenu->Append(SPLIT_REPLACE,
254 wxT("&Replace right window"),
255 wxT("Replace right window"));
256 splitMenu->AppendSeparator();
257
258 splitMenu->Append(SPLIT_QUIT, wxT("E&xit\tAlt-X"), wxT("Exit"));
259
260 wxMenuBar *menuBar = new wxMenuBar;
261 menuBar->Append(splitMenu, wxT("&Splitter"));
262
263 SetMenuBar(menuBar);
264
265 menuBar->Check(SPLIT_LIVE, true);
266 m_splitter = new MySplitterWindow(this);
267
268 // If you use non-zero gravity you must initialize the splitter with its
269 // correct initial size, otherwise it will change the sash position by a
270 // huge amount when it's resized from its initial default size to its real
271 // size when the frame lays it out. This wouldn't be necessary if default
272 // zero gravity were used (although it would do no harm neither).
273 m_splitter->SetSize(GetClientSize());
274 m_splitter->SetSashGravity(1.0);
275
276 #if 1
277 m_left = new MyCanvas(m_splitter, true);
278 m_left->SetBackgroundColour(*wxRED);
279 m_left->SetCursor(wxCursor(wxCURSOR_MAGNIFIER));
280
281 m_right = new MyCanvas(m_splitter, false);
282 m_right->SetBackgroundColour(*wxCYAN);
283 #else // for testing kbd navigation inside the splitter
284 m_left = new wxTextCtrl(m_splitter, wxID_ANY, wxT("first text"));
285 m_right = new wxTextCtrl(m_splitter, wxID_ANY, wxT("second text"));
286 #endif
287
288 // you can also do this to start with a single window
289 #if 0
290 m_right->Show(false);
291 m_splitter->Initialize(m_left);
292 #else
293 // you can also try -100
294 m_splitter->SplitVertically(m_left, m_right, 100);
295 #endif
296
297 #if wxUSE_STATUSBAR
298 SetStatusText(wxT("Min pane size = 0"), 1);
299 #endif // wxUSE_STATUSBAR
300
301 m_replacewindow = NULL;
302 }
303
304 MyFrame::~MyFrame()
305 {
306 if (m_replacewindow) {
307 m_replacewindow->Destroy();
308 }
309 }
310
311 // menu command handlers
312
313 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
314 {
315 Close(true);
316 }
317
318 void MyFrame::OnSplitHorizontal(wxCommandEvent& WXUNUSED(event) )
319 {
320 if ( m_splitter->IsSplit() )
321 m_splitter->Unsplit();
322 m_left->Show(true);
323 m_right->Show(true);
324 m_splitter->SplitHorizontally( m_left, m_right );
325 m_replacewindow = NULL;
326
327 #if wxUSE_STATUSBAR
328 SetStatusText(wxT("Splitter split horizontally"), 1);
329 #endif // wxUSE_STATUSBAR
330 }
331
332 void MyFrame::OnSplitVertical(wxCommandEvent& WXUNUSED(event) )
333 {
334 if ( m_splitter->IsSplit() )
335 m_splitter->Unsplit();
336 m_left->Show(true);
337 m_right->Show(true);
338 m_splitter->SplitVertically( m_left, m_right );
339 m_replacewindow = NULL;
340
341 #if wxUSE_STATUSBAR
342 SetStatusText(wxT("Splitter split vertically"), 1);
343 #endif // wxUSE_STATUSBAR
344 }
345
346 void MyFrame::OnUnsplit(wxCommandEvent& WXUNUSED(event) )
347 {
348 if ( m_splitter->IsSplit() )
349 m_splitter->Unsplit();
350 #if wxUSE_STATUSBAR
351 SetStatusText(wxT("No splitter"));
352 #endif // wxUSE_STATUSBAR
353 }
354
355 void MyFrame::ToggleFlag(int flag, bool enable)
356 {
357 long style = m_splitter->GetWindowStyleFlag();
358 if ( enable )
359 style |= flag;
360 else
361 style &= ~flag;
362
363 m_splitter->SetWindowStyleFlag(style);
364
365 // we need to move sash to redraw it
366 int pos = m_splitter->GetSashPosition();
367 m_splitter->SetSashPosition(pos + 1);
368 m_splitter->SetSashPosition(pos);
369 }
370
371 void MyFrame::OnSetPosition(wxCommandEvent& WXUNUSED(event) )
372 {
373 wxString str;
374 str.Printf( wxT("%d"), m_splitter->GetSashPosition());
375 #if wxUSE_TEXTDLG
376 str = wxGetTextFromUser(wxT("Enter splitter position:"), wxT(""), str, this);
377 #endif
378 if ( str.empty() )
379 return;
380
381 long pos;
382 if ( !str.ToLong(&pos) )
383 {
384 wxLogError(wxT("The splitter position should be an integer."));
385 return;
386 }
387
388 m_splitter->SetSashPosition(pos);
389
390 wxLogStatus(this, wxT("Splitter position set to %ld"), pos);
391 }
392
393 void MyFrame::OnSetMinSize(wxCommandEvent& WXUNUSED(event) )
394 {
395 wxString str;
396 str.Printf( wxT("%d"), m_splitter->GetMinimumPaneSize());
397 #if wxUSE_TEXTDLG
398 str = wxGetTextFromUser(wxT("Enter minimal size for panes:"), wxT(""), str, this);
399 #endif
400 if ( str.empty() )
401 return;
402
403 int minsize = wxStrtol( str, (wxChar**)NULL, 10 );
404 m_splitter->SetMinimumPaneSize(minsize);
405 #if wxUSE_STATUSBAR
406 str.Printf( wxT("Min pane size = %d"), minsize);
407 SetStatusText(str, 1);
408 #endif // wxUSE_STATUSBAR
409 }
410
411 void MyFrame::OnSetGravity(wxCommandEvent& WXUNUSED(event) )
412 {
413 wxString str;
414 str.Printf( wxT("%g"), m_splitter->GetSashGravity());
415 #if wxUSE_TEXTDLG
416 str = wxGetTextFromUser(wxT("Enter sash gravity (0,1):"), wxT(""), str, this);
417 #endif
418 if ( str.empty() )
419 return;
420
421 double gravity = wxStrtod( str, (wxChar**)NULL);
422 m_splitter->SetSashGravity(gravity);
423 #if wxUSE_STATUSBAR
424 str.Printf( wxT("Gravity = %g"), gravity);
425 SetStatusText(str, 1);
426 #endif // wxUSE_STATUSBAR
427 }
428
429 void MyFrame::OnReplace(wxCommandEvent& WXUNUSED(event) )
430 {
431 if (m_replacewindow == NULL) {
432 m_replacewindow = m_splitter->GetWindow2();
433 m_splitter->ReplaceWindow(m_replacewindow, new wxPanel(m_splitter, wxID_ANY));
434 m_replacewindow->Hide();
435 } else {
436 wxWindow *empty = m_splitter->GetWindow2();
437 wxASSERT(empty != m_replacewindow);
438 m_splitter->ReplaceWindow(empty, m_replacewindow);
439 m_replacewindow->Show();
440 m_replacewindow = NULL;
441 empty->Destroy();
442 }
443 }
444
445 void MyFrame::OnToggleInvisible(wxCommandEvent& WXUNUSED(event) )
446 {
447 m_splitter->SetSashInvisible(!m_splitter->IsSashInvisible());
448 m_splitter->SizeWindows();
449 }
450
451 // Update UI handlers
452
453 void MyFrame::OnUpdateUIHorizontal(wxUpdateUIEvent& event)
454 {
455 event.Enable( (!m_splitter->IsSplit()) || (m_splitter->GetSplitMode() != wxSPLIT_HORIZONTAL) );
456 }
457
458 void MyFrame::OnUpdateUIVertical(wxUpdateUIEvent& event)
459 {
460 event.Enable( ( (!m_splitter->IsSplit()) || (m_splitter->GetSplitMode() != wxSPLIT_VERTICAL) ) );
461 }
462
463 void MyFrame::OnUpdateUIUnsplit(wxUpdateUIEvent& event)
464 {
465 event.Enable( m_splitter->IsSplit() );
466 }
467
468 void MyFrame::OnUpdateUIInvisible(wxUpdateUIEvent& event)
469 {
470 event.Check( m_splitter->IsSashInvisible() );
471 }
472
473 // ----------------------------------------------------------------------------
474 // MySplitterWindow
475 // ----------------------------------------------------------------------------
476
477 BEGIN_EVENT_TABLE(MySplitterWindow, wxSplitterWindow)
478 EVT_SPLITTER_SASH_POS_CHANGED(wxID_ANY, MySplitterWindow::OnPositionChanged)
479 EVT_SPLITTER_SASH_POS_CHANGING(wxID_ANY, MySplitterWindow::OnPositionChanging)
480
481 EVT_SPLITTER_DCLICK(wxID_ANY, MySplitterWindow::OnDClick)
482
483 EVT_SPLITTER_UNSPLIT(wxID_ANY, MySplitterWindow::OnUnsplitEvent)
484 END_EVENT_TABLE()
485
486 MySplitterWindow::MySplitterWindow(wxFrame *parent)
487 : wxSplitterWindow(parent, wxID_ANY,
488 wxDefaultPosition, wxDefaultSize,
489 wxSP_3D | wxSP_LIVE_UPDATE |
490 wxCLIP_CHILDREN /* | wxSP_NO_XP_THEME */ )
491 {
492 m_frame = parent;
493 }
494
495 void MySplitterWindow::OnPositionChanged(wxSplitterEvent& event)
496 {
497 wxLogStatus(m_frame, wxT("Position has changed, now = %d (or %d)"),
498 event.GetSashPosition(), GetSashPosition());
499
500 event.Skip();
501 }
502
503 void MySplitterWindow::OnPositionChanging(wxSplitterEvent& event)
504 {
505 wxLogStatus(m_frame, wxT("Position is changing, now = %d (or %d)"),
506 event.GetSashPosition(), GetSashPosition());
507
508 event.Skip();
509 }
510
511 void MySplitterWindow::OnDClick(wxSplitterEvent& event)
512 {
513 #if wxUSE_STATUSBAR
514 m_frame->SetStatusText(wxT("Splitter double clicked"), 1);
515 #endif // wxUSE_STATUSBAR
516
517 event.Skip();
518 }
519
520 void MySplitterWindow::OnUnsplitEvent(wxSplitterEvent& event)
521 {
522 #if wxUSE_STATUSBAR
523 m_frame->SetStatusText(wxT("Splitter unsplit"), 1);
524 #endif // wxUSE_STATUSBAR
525
526 event.Skip();
527 }
528
529 // ----------------------------------------------------------------------------
530 // MyCanvas
531 // ----------------------------------------------------------------------------
532
533 MyCanvas::MyCanvas(wxWindow* parent, bool mirror)
534 : wxScrolledWindow(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
535 wxHSCROLL | wxVSCROLL | wxNO_FULL_REPAINT_ON_RESIZE)
536 {
537 m_mirror = mirror;
538 SetScrollbars(20, 20, 5, 5);
539 }
540
541 void MyCanvas::OnDraw(wxDC& dcOrig)
542 {
543 wxMirrorDC dc(dcOrig, m_mirror);
544
545 dc.SetPen(*wxBLACK_PEN);
546 dc.DrawLine(0, 0, 100, 200);
547
548 dc.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT);
549 dc.DrawText(wxT("Testing"), 50, 50);
550
551 dc.SetPen(*wxRED_PEN);
552 dc.SetBrush(*wxGREEN_BRUSH);
553 dc.DrawRectangle(120, 120, 100, 80);
554 }
555