]> git.saurik.com Git - wxWidgets.git/blame - samples/splitter/splitter.cpp
svn not cvs
[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{
45e6e6f8
VZ
151 if ( !wxApp::OnInit() )
152 return false;
153
2f294a9c
VZ
154 // create and show the main frame
155 MyFrame* frame = new MyFrame;
4a0b46a7 156
8ad18dc3 157 frame->Show(true);
c801d85f 158
8ad18dc3 159 return true;
c801d85f
KB
160}
161
2f294a9c
VZ
162// ----------------------------------------------------------------------------
163// MyFrame
164// ----------------------------------------------------------------------------
165
c801d85f 166BEGIN_EVENT_TABLE(MyFrame, wxFrame)
2f294a9c
VZ
167 EVT_MENU(SPLIT_VERTICAL, MyFrame::SplitVertical)
168 EVT_MENU(SPLIT_HORIZONTAL, MyFrame::SplitHorizontal)
169 EVT_MENU(SPLIT_UNSPLIT, MyFrame::Unsplit)
b795f9ca 170 EVT_MENU(SPLIT_LIVE, MyFrame::ToggleLive)
6faed57d 171 EVT_MENU(SPLIT_SETPOSITION, MyFrame::SetPosition)
2f294a9c 172 EVT_MENU(SPLIT_SETMINSIZE, MyFrame::SetMinSize)
14b4c0ff 173 EVT_MENU(SPLIT_SETGRAVITY, MyFrame::SetGravity)
8adaf733 174 EVT_MENU(SPLIT_REPLACE, MyFrame::Replace)
2f294a9c 175
b795f9ca
VZ
176 EVT_MENU(SPLIT_QUIT, MyFrame::Quit)
177
2f294a9c
VZ
178 EVT_UPDATE_UI(SPLIT_VERTICAL, MyFrame::UpdateUIVertical)
179 EVT_UPDATE_UI(SPLIT_HORIZONTAL, MyFrame::UpdateUIHorizontal)
180 EVT_UPDATE_UI(SPLIT_UNSPLIT, MyFrame::UpdateUIUnsplit)
c801d85f
KB
181END_EVENT_TABLE()
182
183// My frame constructor
2f294a9c 184MyFrame::MyFrame()
8ad18dc3 185 : wxFrame(NULL, wxID_ANY, _T("wxSplitterWindow sample"),
2f294a9c 186 wxDefaultPosition, wxSize(420, 300),
fe31f91c 187 wxDEFAULT_FRAME_STYLE | wxNO_FULL_REPAINT_ON_RESIZE)
c801d85f 188{
8520f137 189#if wxUSE_STATUSBAR
2f294a9c 190 CreateStatusBar(2);
8520f137 191#endif // wxUSE_STATUSBAR
b7346a70 192
2f294a9c 193 // Make a menubar
6faed57d
VZ
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"));
14b4c0ff
VZ
215 splitMenu->Append(SPLIT_SETGRAVITY,
216 _T("Set &gravity\tCtrl-G"),
217 _T("Set gravity of sash"));
6faed57d
VZ
218 splitMenu->AppendSeparator();
219
8adaf733
JS
220 splitMenu->Append(SPLIT_REPLACE,
221 _T("&Replace right window"),
222 _T("Replace right window"));
223 splitMenu->AppendSeparator();
224
6faed57d 225 splitMenu->Append(SPLIT_QUIT, _T("E&xit\tAlt-X"), _T("Exit"));
c801d85f 226
2f294a9c 227 wxMenuBar *menuBar = new wxMenuBar;
6faed57d 228 menuBar->Append(splitMenu, _T("&Splitter"));
c801d85f 229
2f294a9c 230 SetMenuBar(menuBar);
c801d85f 231
8ad18dc3 232 menuBar->Check(SPLIT_LIVE, true);
2f294a9c 233 m_splitter = new MySplitterWindow(this);
14b4c0ff
VZ
234
235 m_splitter->SetSashGravity(1.0);
4a0b46a7 236
6b55490a 237#if 1
a63e17c1 238 m_left = new MyCanvas(m_splitter, true);
2f294a9c 239 m_left->SetBackgroundColour(*wxRED);
a63e17c1 240 m_left->SetScrollbars(20, 20, 5, 5);
2f294a9c
VZ
241 m_left->SetCursor(wxCursor(wxCURSOR_MAGNIFIER));
242
a63e17c1 243 m_right = new MyCanvas(m_splitter, false);
2f294a9c 244 m_right->SetBackgroundColour(*wxCYAN);
a63e17c1 245 m_right->SetScrollbars(20, 20, 5, 5);
6b55490a 246#else // for testing kbd navigation inside the splitter
8ad18dc3
JS
247 m_left = new wxTextCtrl(m_splitter, wxID_ANY, _T("first text"));
248 m_right = new wxTextCtrl(m_splitter, wxID_ANY, _T("second text"));
6b55490a 249#endif
c801d85f 250
2f294a9c 251 // you can also do this to start with a single window
4a0b46a7 252#if 0
8ad18dc3 253 m_right->Show(false);
2f294a9c 254 m_splitter->Initialize(m_left);
4a0b46a7 255#else
74c57d1f 256 // you can also try -100
2f294a9c 257 m_splitter->SplitVertically(m_left, m_right, 100);
4a0b46a7
VZ
258#endif
259
8520f137 260#if wxUSE_STATUSBAR
2f294a9c 261 SetStatusText(_T("Min pane size = 0"), 1);
8520f137 262#endif // wxUSE_STATUSBAR
8adaf733
JS
263
264 m_replacewindow = (wxWindow *)0;
265}
266
267MyFrame::~MyFrame()
268{
269 if (m_replacewindow) {
270 m_replacewindow->Destroy();
271 m_replacewindow = (wxWindow *)0;
272 }
c801d85f
KB
273}
274
2f294a9c
VZ
275// menu command handlers
276
e3e65dac 277void MyFrame::Quit(wxCommandEvent& WXUNUSED(event) )
c801d85f 278{
8ad18dc3 279 Close(true);
c801d85f
KB
280}
281
e3e65dac 282void MyFrame::SplitHorizontal(wxCommandEvent& WXUNUSED(event) )
c801d85f 283{
2f294a9c
VZ
284 if ( m_splitter->IsSplit() )
285 m_splitter->Unsplit();
8ad18dc3
JS
286 m_left->Show(true);
287 m_right->Show(true);
2f294a9c 288 m_splitter->SplitHorizontally( m_left, m_right );
74c57d1f 289
8520f137 290#if wxUSE_STATUSBAR
74c57d1f 291 SetStatusText(_T("Splitter split horizontally"), 1);
8520f137 292#endif // wxUSE_STATUSBAR
c801d85f
KB
293}
294
e3e65dac 295void MyFrame::SplitVertical(wxCommandEvent& WXUNUSED(event) )
c801d85f 296{
2f294a9c
VZ
297 if ( m_splitter->IsSplit() )
298 m_splitter->Unsplit();
8ad18dc3
JS
299 m_left->Show(true);
300 m_right->Show(true);
2f294a9c 301 m_splitter->SplitVertically( m_left, m_right );
74c57d1f 302
8520f137 303#if wxUSE_STATUSBAR
74c57d1f 304 SetStatusText(_T("Splitter split vertically"), 1);
8520f137 305#endif // wxUSE_STATUSBAR
c801d85f
KB
306}
307
e3e65dac 308void MyFrame::Unsplit(wxCommandEvent& WXUNUSED(event) )
c801d85f 309{
2f294a9c
VZ
310 if ( m_splitter->IsSplit() )
311 m_splitter->Unsplit();
8520f137 312#if wxUSE_STATUSBAR
2f294a9c 313 SetStatusText(_T("No splitter"));
8520f137 314#endif // wxUSE_STATUSBAR
0d559d69
VZ
315}
316
b795f9ca
VZ
317void 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
6faed57d
VZ
328void 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
0d559d69
VZ
348void MyFrame::SetMinSize(wxCommandEvent& WXUNUSED(event) )
349{
2f294a9c 350 wxString str;
f565a6c2 351 str.Printf( wxT("%d"), m_splitter->GetMinimumPaneSize());
2f294a9c 352 str = wxGetTextFromUser(_T("Enter minimal size for panes:"), _T(""), str, this);
6faed57d 353 if ( str.empty() )
2f294a9c
VZ
354 return;
355
356 int minsize = wxStrtol( str, (wxChar**)NULL, 10 );
357 m_splitter->SetMinimumPaneSize(minsize);
8520f137 358#if wxUSE_STATUSBAR
f565a6c2 359 str.Printf( wxT("Min pane size = %d"), minsize);
2f294a9c 360 SetStatusText(str, 1);
8520f137 361#endif // wxUSE_STATUSBAR
c801d85f 362}
14b4c0ff
VZ
363void 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}
c801d85f 378
8adaf733
JS
379void 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
2f294a9c
VZ
394// Update UI handlers
395
c801d85f
KB
396void MyFrame::UpdateUIHorizontal(wxUpdateUIEvent& event)
397{
2f294a9c 398 event.Enable( (!m_splitter->IsSplit()) || (m_splitter->GetSplitMode() != wxSPLIT_HORIZONTAL) );
c801d85f
KB
399}
400
401void MyFrame::UpdateUIVertical(wxUpdateUIEvent& event)
402{
2f294a9c 403 event.Enable( ( (!m_splitter->IsSplit()) || (m_splitter->GetSplitMode() != wxSPLIT_VERTICAL) ) );
c801d85f
KB
404}
405
406void MyFrame::UpdateUIUnsplit(wxUpdateUIEvent& event)
407{
2f294a9c 408 event.Enable( m_splitter->IsSplit() );
c801d85f
KB
409}
410
2f294a9c
VZ
411// ----------------------------------------------------------------------------
412// MySplitterWindow
413// ----------------------------------------------------------------------------
414
415BEGIN_EVENT_TABLE(MySplitterWindow, wxSplitterWindow)
8ad18dc3
JS
416 EVT_SPLITTER_SASH_POS_CHANGED(wxID_ANY, MySplitterWindow::OnPositionChanged)
417 EVT_SPLITTER_SASH_POS_CHANGING(wxID_ANY, MySplitterWindow::OnPositionChanging)
2f294a9c 418
8ad18dc3 419 EVT_SPLITTER_DCLICK(wxID_ANY, MySplitterWindow::OnDClick)
2f294a9c 420
8ad18dc3 421 EVT_SPLITTER_UNSPLIT(wxID_ANY, MySplitterWindow::OnUnsplitEvent)
2f294a9c
VZ
422END_EVENT_TABLE()
423
424MySplitterWindow::MySplitterWindow(wxFrame *parent)
8ad18dc3 425 : wxSplitterWindow(parent, wxID_ANY,
2f294a9c 426 wxDefaultPosition, wxDefaultSize,
3c2544bb
JS
427 wxSP_3D | wxSP_LIVE_UPDATE |
428 wxCLIP_CHILDREN /* | wxSP_NO_XP_THEME */ )
c801d85f 429{
2f294a9c 430 m_frame = parent;
c801d85f
KB
431}
432
2f294a9c
VZ
433void 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
441void 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
449void MySplitterWindow::OnDClick(wxSplitterEvent& event)
450{
8520f137 451#if wxUSE_STATUSBAR
2f294a9c 452 m_frame->SetStatusText(_T("Splitter double clicked"), 1);
8520f137 453#endif // wxUSE_STATUSBAR
2f294a9c
VZ
454
455 event.Skip();
456}
457
8ad18dc3 458void MySplitterWindow::OnUnsplitEvent(wxSplitterEvent& event)
2f294a9c 459{
8520f137 460#if wxUSE_STATUSBAR
2f294a9c 461 m_frame->SetStatusText(_T("Splitter unsplit"), 1);
8520f137 462#endif // wxUSE_STATUSBAR
2f294a9c
VZ
463
464 event.Skip();
465}
466
467// ----------------------------------------------------------------------------
468// MyCanvas
469// ----------------------------------------------------------------------------
470
a63e17c1 471MyCanvas::MyCanvas(wxWindow* parent, bool mirror)
8ad18dc3 472 : wxScrolledWindow(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
a63e17c1 473 wxHSCROLL | wxVSCROLL | wxNO_FULL_REPAINT_ON_RESIZE)
c801d85f 474{
a63e17c1 475 m_mirror = mirror;
c801d85f
KB
476}
477
a63e17c1 478void MyCanvas::OnDraw(wxDC& dcOrig)
c801d85f 479{
a63e17c1
VZ
480 wxMirrorDC dc(dcOrig, m_mirror);
481
2f294a9c 482 dc.SetPen(*wxBLACK_PEN);
a63e17c1 483 dc.DrawLine(0, 0, 100, 200);
c801d85f 484
2f294a9c
VZ
485 dc.SetBackgroundMode(wxTRANSPARENT);
486 dc.DrawText(_T("Testing"), 50, 50);
b7346a70 487
2f294a9c
VZ
488 dc.SetPen(*wxRED_PEN);
489 dc.SetBrush(*wxGREEN_BRUSH);
490 dc.DrawRectangle(120, 120, 100, 80);
c801d85f 491}
2f294a9c 492