]> git.saurik.com Git - wxWidgets.git/blame - samples/splitter/splitter.cpp
documented GetIcon(s), extended SetIcons() doc
[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
VZ
75 MyFrame();
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);
33611ebb 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
245MyFrame::~MyFrame()
246{
247}
248
2f294a9c
VZ
249// menu command handlers
250
e3e65dac 251void MyFrame::Quit(wxCommandEvent& WXUNUSED(event) )
c801d85f 252{
8ad18dc3 253 Close(true);
c801d85f
KB
254}
255
e3e65dac 256void MyFrame::SplitHorizontal(wxCommandEvent& WXUNUSED(event) )
c801d85f 257{
2f294a9c
VZ
258 if ( m_splitter->IsSplit() )
259 m_splitter->Unsplit();
8ad18dc3
JS
260 m_left->Show(true);
261 m_right->Show(true);
2f294a9c 262 m_splitter->SplitHorizontally( m_left, m_right );
74c57d1f 263
8520f137 264#if wxUSE_STATUSBAR
74c57d1f 265 SetStatusText(_T("Splitter split horizontally"), 1);
8520f137 266#endif // wxUSE_STATUSBAR
c801d85f
KB
267}
268
e3e65dac 269void MyFrame::SplitVertical(wxCommandEvent& WXUNUSED(event) )
c801d85f 270{
2f294a9c
VZ
271 if ( m_splitter->IsSplit() )
272 m_splitter->Unsplit();
8ad18dc3
JS
273 m_left->Show(true);
274 m_right->Show(true);
2f294a9c 275 m_splitter->SplitVertically( m_left, m_right );
74c57d1f 276
8520f137 277#if wxUSE_STATUSBAR
74c57d1f 278 SetStatusText(_T("Splitter split vertically"), 1);
8520f137 279#endif // wxUSE_STATUSBAR
c801d85f
KB
280}
281
e3e65dac 282void MyFrame::Unsplit(wxCommandEvent& WXUNUSED(event) )
c801d85f 283{
2f294a9c
VZ
284 if ( m_splitter->IsSplit() )
285 m_splitter->Unsplit();
8520f137 286#if wxUSE_STATUSBAR
2f294a9c 287 SetStatusText(_T("No splitter"));
8520f137 288#endif // wxUSE_STATUSBAR
0d559d69
VZ
289}
290
b795f9ca
VZ
291void MyFrame::ToggleLive(wxCommandEvent& event )
292{
293 long style = m_splitter->GetWindowStyleFlag();
294 if ( event.IsChecked() )
295 style |= wxSP_LIVE_UPDATE;
296 else
297 style &= ~wxSP_LIVE_UPDATE;
298
299 m_splitter->SetWindowStyleFlag(style);
300}
301
6faed57d
VZ
302void MyFrame::SetPosition(wxCommandEvent& WXUNUSED(event) )
303{
304 wxString str;
305 str.Printf( wxT("%d"), m_splitter->GetSashPosition());
306 str = wxGetTextFromUser(_T("Enter splitter position:"), _T(""), str, this);
307 if ( str.empty() )
308 return;
309
310 long pos;
311 if ( !str.ToLong(&pos) )
312 {
313 wxLogError(_T("The splitter position should be an integer."));
314 return;
315 }
316
317 m_splitter->SetSashPosition(pos);
318
319 wxLogStatus(this, _T("Splitter position set to %ld"), pos);
320}
321
0d559d69
VZ
322void MyFrame::SetMinSize(wxCommandEvent& WXUNUSED(event) )
323{
2f294a9c 324 wxString str;
f565a6c2 325 str.Printf( wxT("%d"), m_splitter->GetMinimumPaneSize());
2f294a9c 326 str = wxGetTextFromUser(_T("Enter minimal size for panes:"), _T(""), str, this);
6faed57d 327 if ( str.empty() )
2f294a9c
VZ
328 return;
329
330 int minsize = wxStrtol( str, (wxChar**)NULL, 10 );
331 m_splitter->SetMinimumPaneSize(minsize);
8520f137 332#if wxUSE_STATUSBAR
f565a6c2 333 str.Printf( wxT("Min pane size = %d"), minsize);
2f294a9c 334 SetStatusText(str, 1);
8520f137 335#endif // wxUSE_STATUSBAR
c801d85f
KB
336}
337
2f294a9c
VZ
338// Update UI handlers
339
c801d85f
KB
340void MyFrame::UpdateUIHorizontal(wxUpdateUIEvent& event)
341{
2f294a9c 342 event.Enable( (!m_splitter->IsSplit()) || (m_splitter->GetSplitMode() != wxSPLIT_HORIZONTAL) );
c801d85f
KB
343}
344
345void MyFrame::UpdateUIVertical(wxUpdateUIEvent& event)
346{
2f294a9c 347 event.Enable( ( (!m_splitter->IsSplit()) || (m_splitter->GetSplitMode() != wxSPLIT_VERTICAL) ) );
c801d85f
KB
348}
349
350void MyFrame::UpdateUIUnsplit(wxUpdateUIEvent& event)
351{
2f294a9c 352 event.Enable( m_splitter->IsSplit() );
c801d85f
KB
353}
354
2f294a9c
VZ
355// ----------------------------------------------------------------------------
356// MySplitterWindow
357// ----------------------------------------------------------------------------
358
359BEGIN_EVENT_TABLE(MySplitterWindow, wxSplitterWindow)
8ad18dc3
JS
360 EVT_SPLITTER_SASH_POS_CHANGED(wxID_ANY, MySplitterWindow::OnPositionChanged)
361 EVT_SPLITTER_SASH_POS_CHANGING(wxID_ANY, MySplitterWindow::OnPositionChanging)
2f294a9c 362
8ad18dc3 363 EVT_SPLITTER_DCLICK(wxID_ANY, MySplitterWindow::OnDClick)
2f294a9c 364
8ad18dc3 365 EVT_SPLITTER_UNSPLIT(wxID_ANY, MySplitterWindow::OnUnsplitEvent)
2f294a9c
VZ
366END_EVENT_TABLE()
367
368MySplitterWindow::MySplitterWindow(wxFrame *parent)
8ad18dc3 369 : wxSplitterWindow(parent, wxID_ANY,
2f294a9c 370 wxDefaultPosition, wxDefaultSize,
3c2544bb
JS
371 wxSP_3D | wxSP_LIVE_UPDATE |
372 wxCLIP_CHILDREN /* | wxSP_NO_XP_THEME */ )
c801d85f 373{
2f294a9c 374 m_frame = parent;
c801d85f
KB
375}
376
2f294a9c
VZ
377void MySplitterWindow::OnPositionChanged(wxSplitterEvent& event)
378{
379 wxLogStatus(m_frame, _T("Position has changed, now = %d (or %d)"),
380 event.GetSashPosition(), GetSashPosition());
381
382 event.Skip();
383}
384
385void MySplitterWindow::OnPositionChanging(wxSplitterEvent& event)
386{
387 wxLogStatus(m_frame, _T("Position is changing, now = %d (or %d)"),
388 event.GetSashPosition(), GetSashPosition());
389
390 event.Skip();
391}
392
393void MySplitterWindow::OnDClick(wxSplitterEvent& event)
394{
8520f137 395#if wxUSE_STATUSBAR
2f294a9c 396 m_frame->SetStatusText(_T("Splitter double clicked"), 1);
8520f137 397#endif // wxUSE_STATUSBAR
2f294a9c
VZ
398
399 event.Skip();
400}
401
8ad18dc3 402void MySplitterWindow::OnUnsplitEvent(wxSplitterEvent& event)
2f294a9c 403{
8520f137 404#if wxUSE_STATUSBAR
2f294a9c 405 m_frame->SetStatusText(_T("Splitter unsplit"), 1);
8520f137 406#endif // wxUSE_STATUSBAR
2f294a9c
VZ
407
408 event.Skip();
409}
410
411// ----------------------------------------------------------------------------
412// MyCanvas
413// ----------------------------------------------------------------------------
414
a63e17c1 415MyCanvas::MyCanvas(wxWindow* parent, bool mirror)
8ad18dc3 416 : wxScrolledWindow(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
a63e17c1 417 wxHSCROLL | wxVSCROLL | wxNO_FULL_REPAINT_ON_RESIZE)
c801d85f 418{
a63e17c1 419 m_mirror = mirror;
c801d85f
KB
420}
421
422MyCanvas::~MyCanvas()
423{
424}
425
a63e17c1 426void MyCanvas::OnDraw(wxDC& dcOrig)
c801d85f 427{
a63e17c1
VZ
428 wxMirrorDC dc(dcOrig, m_mirror);
429
2f294a9c 430 dc.SetPen(*wxBLACK_PEN);
a63e17c1 431 dc.DrawLine(0, 0, 100, 200);
c801d85f 432
2f294a9c
VZ
433 dc.SetBackgroundMode(wxTRANSPARENT);
434 dc.DrawText(_T("Testing"), 50, 50);
b7346a70 435
2f294a9c
VZ
436 dc.SetPen(*wxRED_PEN);
437 dc.SetBrush(*wxGREEN_BRUSH);
438 dc.DrawRectangle(120, 120, 100, 80);
c801d85f 439}
2f294a9c 440