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