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