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