call OnInit() from all samples to allow using standard command line options with...
[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 license
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 // ----------------------------------------------------------------------------
43 // constants
44 // ----------------------------------------------------------------------------
45
46 // ID for the menu commands
47 enum
48 {
49 SPLIT_QUIT = 1,
50 SPLIT_HORIZONTAL,
51 SPLIT_VERTICAL,
52 SPLIT_UNSPLIT,
53 SPLIT_LIVE,
54 SPLIT_SETPOSITION,
55 SPLIT_SETMINSIZE,
56 SPLIT_SETGRAVITY,
57 SPLIT_REPLACE
58 };
59
60 // ----------------------------------------------------------------------------
61 // our classes
62 // ----------------------------------------------------------------------------
63
64 class MyApp: public wxApp
65 {
66 public:
67 MyApp() { }
68
69 virtual bool OnInit();
70
71 DECLARE_NO_COPY_CLASS(MyApp)
72 };
73
74 class MyFrame: public wxFrame
75 {
76 public:
77 MyFrame();
78 virtual ~MyFrame();
79
80 // Menu commands
81 void SplitHorizontal(wxCommandEvent& event);
82 void SplitVertical(wxCommandEvent& event);
83 void Unsplit(wxCommandEvent& event);
84 void ToggleLive(wxCommandEvent& event);
85 void SetPosition(wxCommandEvent& event);
86 void SetMinSize(wxCommandEvent& event);
87 void SetGravity(wxCommandEvent& event);
88 void Replace(wxCommandEvent &event);
89
90 void Quit(wxCommandEvent& event);
91
92 // Menu command update functions
93 void UpdateUIHorizontal(wxUpdateUIEvent& event);
94 void UpdateUIVertical(wxUpdateUIEvent& event);
95 void UpdateUIUnsplit(wxUpdateUIEvent& event);
96
97 private:
98 wxScrolledWindow *m_left, *m_right;
99
100 wxSplitterWindow* m_splitter;
101 wxWindow *m_replacewindow;
102
103 DECLARE_EVENT_TABLE()
104 DECLARE_NO_COPY_CLASS(MyFrame)
105 };
106
107 class MySplitterWindow : public wxSplitterWindow
108 {
109 public:
110 MySplitterWindow(wxFrame *parent);
111
112 // event handlers
113 void OnPositionChanged(wxSplitterEvent& event);
114 void OnPositionChanging(wxSplitterEvent& event);
115 void OnDClick(wxSplitterEvent& event);
116 void OnUnsplitEvent(wxSplitterEvent& event);
117
118 private:
119 wxFrame *m_frame;
120
121 DECLARE_EVENT_TABLE()
122 DECLARE_NO_COPY_CLASS(MySplitterWindow)
123 };
124
125 class MyCanvas: public wxScrolledWindow
126 {
127 public:
128 MyCanvas(wxWindow* parent, bool mirror);
129 virtual ~MyCanvas(){};
130
131 virtual void OnDraw(wxDC& dc);
132
133 private:
134 bool m_mirror;
135
136 DECLARE_NO_COPY_CLASS(MyCanvas)
137 };
138
139 // ============================================================================
140 // implementation
141 // ============================================================================
142
143 // ----------------------------------------------------------------------------
144 // MyApp
145 // ----------------------------------------------------------------------------
146
147 IMPLEMENT_APP(MyApp)
148
149 bool MyApp::OnInit()
150 {
151 if ( !wxApp::OnInit() )
152 return false;
153
154 // create and show the main frame
155 MyFrame* frame = new MyFrame;
156
157 frame->Show(true);
158
159 return true;
160 }
161
162 // ----------------------------------------------------------------------------
163 // MyFrame
164 // ----------------------------------------------------------------------------
165
166 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
167 EVT_MENU(SPLIT_VERTICAL, MyFrame::SplitVertical)
168 EVT_MENU(SPLIT_HORIZONTAL, MyFrame::SplitHorizontal)
169 EVT_MENU(SPLIT_UNSPLIT, MyFrame::Unsplit)
170 EVT_MENU(SPLIT_LIVE, MyFrame::ToggleLive)
171 EVT_MENU(SPLIT_SETPOSITION, MyFrame::SetPosition)
172 EVT_MENU(SPLIT_SETMINSIZE, MyFrame::SetMinSize)
173 EVT_MENU(SPLIT_SETGRAVITY, MyFrame::SetGravity)
174 EVT_MENU(SPLIT_REPLACE, MyFrame::Replace)
175
176 EVT_MENU(SPLIT_QUIT, MyFrame::Quit)
177
178 EVT_UPDATE_UI(SPLIT_VERTICAL, MyFrame::UpdateUIVertical)
179 EVT_UPDATE_UI(SPLIT_HORIZONTAL, MyFrame::UpdateUIHorizontal)
180 EVT_UPDATE_UI(SPLIT_UNSPLIT, MyFrame::UpdateUIUnsplit)
181 END_EVENT_TABLE()
182
183 // My frame constructor
184 MyFrame::MyFrame()
185 : wxFrame(NULL, wxID_ANY, _T("wxSplitterWindow sample"),
186 wxDefaultPosition, wxSize(420, 300),
187 wxDEFAULT_FRAME_STYLE | wxNO_FULL_REPAINT_ON_RESIZE)
188 {
189 #if wxUSE_STATUSBAR
190 CreateStatusBar(2);
191 #endif // wxUSE_STATUSBAR
192
193 // Make a menubar
194 wxMenu *splitMenu = new wxMenu;
195 splitMenu->Append(SPLIT_VERTICAL,
196 _T("Split &Vertically\tCtrl-V"),
197 _T("Split vertically"));
198 splitMenu->Append(SPLIT_HORIZONTAL,
199 _T("Split &Horizontally\tCtrl-H"),
200 _T("Split horizontally"));
201 splitMenu->Append(SPLIT_UNSPLIT,
202 _T("&Unsplit\tCtrl-U"),
203 _T("Unsplit"));
204 splitMenu->AppendSeparator();
205
206 splitMenu->AppendCheckItem(SPLIT_LIVE,
207 _T("&Live update\tCtrl-L"),
208 _T("Toggle live update mode"));
209 splitMenu->Append(SPLIT_SETPOSITION,
210 _T("Set splitter &position\tCtrl-P"),
211 _T("Set the splitter position"));
212 splitMenu->Append(SPLIT_SETMINSIZE,
213 _T("Set &min size\tCtrl-M"),
214 _T("Set minimum pane size"));
215 splitMenu->Append(SPLIT_SETGRAVITY,
216 _T("Set &gravity\tCtrl-G"),
217 _T("Set gravity of sash"));
218 splitMenu->AppendSeparator();
219
220 splitMenu->Append(SPLIT_REPLACE,
221 _T("&Replace right window"),
222 _T("Replace right window"));
223 splitMenu->AppendSeparator();
224
225 splitMenu->Append(SPLIT_QUIT, _T("E&xit\tAlt-X"), _T("Exit"));
226
227 wxMenuBar *menuBar = new wxMenuBar;
228 menuBar->Append(splitMenu, _T("&Splitter"));
229
230 SetMenuBar(menuBar);
231
232 menuBar->Check(SPLIT_LIVE, true);
233 m_splitter = new MySplitterWindow(this);
234
235 m_splitter->SetSashGravity(1.0);
236
237 #if 1
238 m_left = new MyCanvas(m_splitter, true);
239 m_left->SetBackgroundColour(*wxRED);
240 m_left->SetScrollbars(20, 20, 5, 5);
241 m_left->SetCursor(wxCursor(wxCURSOR_MAGNIFIER));
242
243 m_right = new MyCanvas(m_splitter, false);
244 m_right->SetBackgroundColour(*wxCYAN);
245 m_right->SetScrollbars(20, 20, 5, 5);
246 #else // for testing kbd navigation inside the splitter
247 m_left = new wxTextCtrl(m_splitter, wxID_ANY, _T("first text"));
248 m_right = new wxTextCtrl(m_splitter, wxID_ANY, _T("second text"));
249 #endif
250
251 // you can also do this to start with a single window
252 #if 0
253 m_right->Show(false);
254 m_splitter->Initialize(m_left);
255 #else
256 // you can also try -100
257 m_splitter->SplitVertically(m_left, m_right, 100);
258 #endif
259
260 #if wxUSE_STATUSBAR
261 SetStatusText(_T("Min pane size = 0"), 1);
262 #endif // wxUSE_STATUSBAR
263
264 m_replacewindow = (wxWindow *)0;
265 }
266
267 MyFrame::~MyFrame()
268 {
269 if (m_replacewindow) {
270 m_replacewindow->Destroy();
271 m_replacewindow = (wxWindow *)0;
272 }
273 }
274
275 // menu command handlers
276
277 void MyFrame::Quit(wxCommandEvent& WXUNUSED(event) )
278 {
279 Close(true);
280 }
281
282 void MyFrame::SplitHorizontal(wxCommandEvent& WXUNUSED(event) )
283 {
284 if ( m_splitter->IsSplit() )
285 m_splitter->Unsplit();
286 m_left->Show(true);
287 m_right->Show(true);
288 m_splitter->SplitHorizontally( m_left, m_right );
289
290 #if wxUSE_STATUSBAR
291 SetStatusText(_T("Splitter split horizontally"), 1);
292 #endif // wxUSE_STATUSBAR
293 }
294
295 void MyFrame::SplitVertical(wxCommandEvent& WXUNUSED(event) )
296 {
297 if ( m_splitter->IsSplit() )
298 m_splitter->Unsplit();
299 m_left->Show(true);
300 m_right->Show(true);
301 m_splitter->SplitVertically( m_left, m_right );
302
303 #if wxUSE_STATUSBAR
304 SetStatusText(_T("Splitter split vertically"), 1);
305 #endif // wxUSE_STATUSBAR
306 }
307
308 void MyFrame::Unsplit(wxCommandEvent& WXUNUSED(event) )
309 {
310 if ( m_splitter->IsSplit() )
311 m_splitter->Unsplit();
312 #if wxUSE_STATUSBAR
313 SetStatusText(_T("No splitter"));
314 #endif // wxUSE_STATUSBAR
315 }
316
317 void MyFrame::ToggleLive(wxCommandEvent& event )
318 {
319 long style = m_splitter->GetWindowStyleFlag();
320 if ( event.IsChecked() )
321 style |= wxSP_LIVE_UPDATE;
322 else
323 style &= ~wxSP_LIVE_UPDATE;
324
325 m_splitter->SetWindowStyleFlag(style);
326 }
327
328 void MyFrame::SetPosition(wxCommandEvent& WXUNUSED(event) )
329 {
330 wxString str;
331 str.Printf( wxT("%d"), m_splitter->GetSashPosition());
332 str = wxGetTextFromUser(_T("Enter splitter position:"), _T(""), str, this);
333 if ( str.empty() )
334 return;
335
336 long pos;
337 if ( !str.ToLong(&pos) )
338 {
339 wxLogError(_T("The splitter position should be an integer."));
340 return;
341 }
342
343 m_splitter->SetSashPosition(pos);
344
345 wxLogStatus(this, _T("Splitter position set to %ld"), pos);
346 }
347
348 void MyFrame::SetMinSize(wxCommandEvent& WXUNUSED(event) )
349 {
350 wxString str;
351 str.Printf( wxT("%d"), m_splitter->GetMinimumPaneSize());
352 str = wxGetTextFromUser(_T("Enter minimal size for panes:"), _T(""), str, this);
353 if ( str.empty() )
354 return;
355
356 int minsize = wxStrtol( str, (wxChar**)NULL, 10 );
357 m_splitter->SetMinimumPaneSize(minsize);
358 #if wxUSE_STATUSBAR
359 str.Printf( wxT("Min pane size = %d"), minsize);
360 SetStatusText(str, 1);
361 #endif // wxUSE_STATUSBAR
362 }
363 void MyFrame::SetGravity(wxCommandEvent& WXUNUSED(event) )
364 {
365 wxString str;
366 str.Printf( wxT("%g"), m_splitter->GetSashGravity());
367 str = wxGetTextFromUser(_T("Enter sash gravity (0,1):"), _T(""), str, this);
368 if ( str.empty() )
369 return;
370
371 double gravity = wxStrtod( str, (wxChar**)NULL);
372 m_splitter->SetSashGravity(gravity);
373 #if wxUSE_STATUSBAR
374 str.Printf( wxT("Gravity = %g"), gravity);
375 SetStatusText(str, 1);
376 #endif // wxUSE_STATUSBAR
377 }
378
379 void MyFrame::Replace(wxCommandEvent& WXUNUSED(event) )
380 {
381 if (m_replacewindow == 0) {
382 m_replacewindow = m_splitter->GetWindow2();
383 m_splitter->ReplaceWindow(m_replacewindow, new wxPanel(m_splitter, wxID_ANY));
384 m_replacewindow->Hide();
385 } else {
386 wxWindow *empty = m_splitter->GetWindow2();
387 m_splitter->ReplaceWindow(empty, m_replacewindow);
388 m_replacewindow->Show();
389 m_replacewindow = 0;
390 empty->Destroy();
391 }
392 }
393
394 // Update UI handlers
395
396 void MyFrame::UpdateUIHorizontal(wxUpdateUIEvent& event)
397 {
398 event.Enable( (!m_splitter->IsSplit()) || (m_splitter->GetSplitMode() != wxSPLIT_HORIZONTAL) );
399 }
400
401 void MyFrame::UpdateUIVertical(wxUpdateUIEvent& event)
402 {
403 event.Enable( ( (!m_splitter->IsSplit()) || (m_splitter->GetSplitMode() != wxSPLIT_VERTICAL) ) );
404 }
405
406 void MyFrame::UpdateUIUnsplit(wxUpdateUIEvent& event)
407 {
408 event.Enable( m_splitter->IsSplit() );
409 }
410
411 // ----------------------------------------------------------------------------
412 // MySplitterWindow
413 // ----------------------------------------------------------------------------
414
415 BEGIN_EVENT_TABLE(MySplitterWindow, wxSplitterWindow)
416 EVT_SPLITTER_SASH_POS_CHANGED(wxID_ANY, MySplitterWindow::OnPositionChanged)
417 EVT_SPLITTER_SASH_POS_CHANGING(wxID_ANY, MySplitterWindow::OnPositionChanging)
418
419 EVT_SPLITTER_DCLICK(wxID_ANY, MySplitterWindow::OnDClick)
420
421 EVT_SPLITTER_UNSPLIT(wxID_ANY, MySplitterWindow::OnUnsplitEvent)
422 END_EVENT_TABLE()
423
424 MySplitterWindow::MySplitterWindow(wxFrame *parent)
425 : wxSplitterWindow(parent, wxID_ANY,
426 wxDefaultPosition, wxDefaultSize,
427 wxSP_3D | wxSP_LIVE_UPDATE |
428 wxCLIP_CHILDREN /* | wxSP_NO_XP_THEME */ )
429 {
430 m_frame = parent;
431 }
432
433 void MySplitterWindow::OnPositionChanged(wxSplitterEvent& event)
434 {
435 wxLogStatus(m_frame, _T("Position has changed, now = %d (or %d)"),
436 event.GetSashPosition(), GetSashPosition());
437
438 event.Skip();
439 }
440
441 void MySplitterWindow::OnPositionChanging(wxSplitterEvent& event)
442 {
443 wxLogStatus(m_frame, _T("Position is changing, now = %d (or %d)"),
444 event.GetSashPosition(), GetSashPosition());
445
446 event.Skip();
447 }
448
449 void MySplitterWindow::OnDClick(wxSplitterEvent& event)
450 {
451 #if wxUSE_STATUSBAR
452 m_frame->SetStatusText(_T("Splitter double clicked"), 1);
453 #endif // wxUSE_STATUSBAR
454
455 event.Skip();
456 }
457
458 void MySplitterWindow::OnUnsplitEvent(wxSplitterEvent& event)
459 {
460 #if wxUSE_STATUSBAR
461 m_frame->SetStatusText(_T("Splitter unsplit"), 1);
462 #endif // wxUSE_STATUSBAR
463
464 event.Skip();
465 }
466
467 // ----------------------------------------------------------------------------
468 // MyCanvas
469 // ----------------------------------------------------------------------------
470
471 MyCanvas::MyCanvas(wxWindow* parent, bool mirror)
472 : wxScrolledWindow(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
473 wxHSCROLL | wxVSCROLL | wxNO_FULL_REPAINT_ON_RESIZE)
474 {
475 m_mirror = mirror;
476 }
477
478 void MyCanvas::OnDraw(wxDC& dcOrig)
479 {
480 wxMirrorDC dc(dcOrig, m_mirror);
481
482 dc.SetPen(*wxBLACK_PEN);
483 dc.DrawLine(0, 0, 100, 200);
484
485 dc.SetBackgroundMode(wxTRANSPARENT);
486 dc.DrawText(_T("Testing"), 50, 50);
487
488 dc.SetPen(*wxRED_PEN);
489 dc.SetBrush(*wxGREEN_BRUSH);
490 dc.DrawRectangle(120, 120, 100, 80);
491 }
492