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