]> git.saurik.com Git - wxWidgets.git/blame - samples/splitter/splitter.cpp
compiles under win64
[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);
111 void OnUnsplit(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
2f294a9c 149 frame->Show(TRUE);
c801d85f 150
2f294a9c 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
VZ
174MyFrame::MyFrame()
175 : wxFrame(NULL, -1, _T("wxSplitterWindow sample"),
176 wxDefaultPosition, wxSize(420, 300),
fe31f91c 177 wxDEFAULT_FRAME_STYLE | wxNO_FULL_REPAINT_ON_RESIZE)
c801d85f 178{
2f294a9c 179 CreateStatusBar(2);
b7346a70 180
2f294a9c 181 // Make a menubar
6faed57d
VZ
182 wxMenu *splitMenu = new wxMenu;
183 splitMenu->Append(SPLIT_VERTICAL,
184 _T("Split &Vertically\tCtrl-V"),
185 _T("Split vertically"));
186 splitMenu->Append(SPLIT_HORIZONTAL,
187 _T("Split &Horizontally\tCtrl-H"),
188 _T("Split horizontally"));
189 splitMenu->Append(SPLIT_UNSPLIT,
190 _T("&Unsplit\tCtrl-U"),
191 _T("Unsplit"));
192 splitMenu->AppendSeparator();
193
194 splitMenu->AppendCheckItem(SPLIT_LIVE,
195 _T("&Live update\tCtrl-L"),
196 _T("Toggle live update mode"));
197 splitMenu->Append(SPLIT_SETPOSITION,
198 _T("Set splitter &position\tCtrl-P"),
199 _T("Set the splitter position"));
200 splitMenu->Append(SPLIT_SETMINSIZE,
201 _T("Set &min size\tCtrl-M"),
202 _T("Set minimum pane size"));
203 splitMenu->AppendSeparator();
204
205 splitMenu->Append(SPLIT_QUIT, _T("E&xit\tAlt-X"), _T("Exit"));
c801d85f 206
2f294a9c 207 wxMenuBar *menuBar = new wxMenuBar;
6faed57d 208 menuBar->Append(splitMenu, _T("&Splitter"));
c801d85f 209
2f294a9c 210 SetMenuBar(menuBar);
c801d85f 211
b795f9ca 212 menuBar->Check(SPLIT_LIVE, TRUE);
2f294a9c 213 m_splitter = new MySplitterWindow(this);
4a0b46a7 214
6b55490a 215#if 1
a63e17c1 216 m_left = new MyCanvas(m_splitter, true);
2f294a9c 217 m_left->SetBackgroundColour(*wxRED);
a63e17c1 218 m_left->SetScrollbars(20, 20, 5, 5);
2f294a9c
VZ
219 m_left->SetCursor(wxCursor(wxCURSOR_MAGNIFIER));
220
a63e17c1 221 m_right = new MyCanvas(m_splitter, false);
2f294a9c 222 m_right->SetBackgroundColour(*wxCYAN);
a63e17c1 223 m_right->SetScrollbars(20, 20, 5, 5);
6b55490a 224#else // for testing kbd navigation inside the splitter
2f294a9c
VZ
225 m_left = new wxTextCtrl(m_splitter, -1, _T("first text"));
226 m_right = new wxTextCtrl(m_splitter, -1, _T("second text"));
6b55490a 227#endif
c801d85f 228
2f294a9c 229 // you can also do this to start with a single window
4a0b46a7 230#if 0
2f294a9c
VZ
231 m_right->Show(FALSE);
232 m_splitter->Initialize(m_left);
4a0b46a7 233#else
74c57d1f 234 // you can also try -100
2f294a9c 235 m_splitter->SplitVertically(m_left, m_right, 100);
4a0b46a7
VZ
236#endif
237
2f294a9c 238 SetStatusText(_T("Min pane size = 0"), 1);
c801d85f
KB
239}
240
241MyFrame::~MyFrame()
242{
243}
244
2f294a9c
VZ
245// menu command handlers
246
e3e65dac 247void MyFrame::Quit(wxCommandEvent& WXUNUSED(event) )
c801d85f 248{
2f294a9c 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();
256 m_left->Show(TRUE);
257 m_right->Show(TRUE);
258 m_splitter->SplitHorizontally( m_left, m_right );
74c57d1f
VZ
259
260 SetStatusText(_T("Splitter split horizontally"), 1);
c801d85f
KB
261}
262
e3e65dac 263void MyFrame::SplitVertical(wxCommandEvent& WXUNUSED(event) )
c801d85f 264{
2f294a9c
VZ
265 if ( m_splitter->IsSplit() )
266 m_splitter->Unsplit();
267 m_left->Show(TRUE);
268 m_right->Show(TRUE);
269 m_splitter->SplitVertically( m_left, m_right );
74c57d1f
VZ
270
271 SetStatusText(_T("Splitter split vertically"), 1);
c801d85f
KB
272}
273
e3e65dac 274void MyFrame::Unsplit(wxCommandEvent& WXUNUSED(event) )
c801d85f 275{
2f294a9c
VZ
276 if ( m_splitter->IsSplit() )
277 m_splitter->Unsplit();
278 SetStatusText(_T("No splitter"));
0d559d69
VZ
279}
280
b795f9ca
VZ
281void MyFrame::ToggleLive(wxCommandEvent& event )
282{
283 long style = m_splitter->GetWindowStyleFlag();
284 if ( event.IsChecked() )
285 style |= wxSP_LIVE_UPDATE;
286 else
287 style &= ~wxSP_LIVE_UPDATE;
288
289 m_splitter->SetWindowStyleFlag(style);
290}
291
6faed57d
VZ
292void MyFrame::SetPosition(wxCommandEvent& WXUNUSED(event) )
293{
294 wxString str;
295 str.Printf( wxT("%d"), m_splitter->GetSashPosition());
296 str = wxGetTextFromUser(_T("Enter splitter position:"), _T(""), str, this);
297 if ( str.empty() )
298 return;
299
300 long pos;
301 if ( !str.ToLong(&pos) )
302 {
303 wxLogError(_T("The splitter position should be an integer."));
304 return;
305 }
306
307 m_splitter->SetSashPosition(pos);
308
309 wxLogStatus(this, _T("Splitter position set to %ld"), pos);
310}
311
0d559d69
VZ
312void MyFrame::SetMinSize(wxCommandEvent& WXUNUSED(event) )
313{
2f294a9c 314 wxString str;
f565a6c2 315 str.Printf( wxT("%d"), m_splitter->GetMinimumPaneSize());
2f294a9c 316 str = wxGetTextFromUser(_T("Enter minimal size for panes:"), _T(""), str, this);
6faed57d 317 if ( str.empty() )
2f294a9c
VZ
318 return;
319
320 int minsize = wxStrtol( str, (wxChar**)NULL, 10 );
321 m_splitter->SetMinimumPaneSize(minsize);
f565a6c2 322 str.Printf( wxT("Min pane size = %d"), minsize);
2f294a9c 323 SetStatusText(str, 1);
c801d85f
KB
324}
325
2f294a9c
VZ
326// Update UI handlers
327
c801d85f
KB
328void MyFrame::UpdateUIHorizontal(wxUpdateUIEvent& event)
329{
2f294a9c 330 event.Enable( (!m_splitter->IsSplit()) || (m_splitter->GetSplitMode() != wxSPLIT_HORIZONTAL) );
c801d85f
KB
331}
332
333void MyFrame::UpdateUIVertical(wxUpdateUIEvent& event)
334{
2f294a9c 335 event.Enable( ( (!m_splitter->IsSplit()) || (m_splitter->GetSplitMode() != wxSPLIT_VERTICAL) ) );
c801d85f
KB
336}
337
338void MyFrame::UpdateUIUnsplit(wxUpdateUIEvent& event)
339{
2f294a9c 340 event.Enable( m_splitter->IsSplit() );
c801d85f
KB
341}
342
2f294a9c
VZ
343// ----------------------------------------------------------------------------
344// MySplitterWindow
345// ----------------------------------------------------------------------------
346
347BEGIN_EVENT_TABLE(MySplitterWindow, wxSplitterWindow)
348 EVT_SPLITTER_SASH_POS_CHANGED(-1, MySplitterWindow::OnPositionChanged)
349 EVT_SPLITTER_SASH_POS_CHANGING(-1, MySplitterWindow::OnPositionChanging)
350
351 EVT_SPLITTER_DCLICK(-1, MySplitterWindow::OnDClick)
352
353 EVT_SPLITTER_UNSPLIT(-1, MySplitterWindow::OnUnsplit)
354END_EVENT_TABLE()
355
356MySplitterWindow::MySplitterWindow(wxFrame *parent)
357 : wxSplitterWindow(parent, -1,
358 wxDefaultPosition, wxDefaultSize,
a63e17c1 359 0x700| wxSP_LIVE_UPDATE | wxCLIP_CHILDREN)
c801d85f 360{
2f294a9c 361 m_frame = parent;
c801d85f
KB
362}
363
2f294a9c
VZ
364void MySplitterWindow::OnPositionChanged(wxSplitterEvent& event)
365{
366 wxLogStatus(m_frame, _T("Position has changed, now = %d (or %d)"),
367 event.GetSashPosition(), GetSashPosition());
368
369 event.Skip();
370}
371
372void MySplitterWindow::OnPositionChanging(wxSplitterEvent& event)
373{
374 wxLogStatus(m_frame, _T("Position is changing, now = %d (or %d)"),
375 event.GetSashPosition(), GetSashPosition());
376
377 event.Skip();
378}
379
380void MySplitterWindow::OnDClick(wxSplitterEvent& event)
381{
382 m_frame->SetStatusText(_T("Splitter double clicked"), 1);
383
384 event.Skip();
385}
386
387void MySplitterWindow::OnUnsplit(wxSplitterEvent& event)
388{
389 m_frame->SetStatusText(_T("Splitter unsplit"), 1);
390
391 event.Skip();
392}
393
394// ----------------------------------------------------------------------------
395// MyCanvas
396// ----------------------------------------------------------------------------
397
a63e17c1
VZ
398MyCanvas::MyCanvas(wxWindow* parent, bool mirror)
399 : wxScrolledWindow(parent, -1, wxDefaultPosition, wxDefaultSize,
400 wxHSCROLL | wxVSCROLL | wxNO_FULL_REPAINT_ON_RESIZE)
c801d85f 401{
a63e17c1 402 m_mirror = mirror;
c801d85f
KB
403}
404
405MyCanvas::~MyCanvas()
406{
407}
408
a63e17c1 409void MyCanvas::OnDraw(wxDC& dcOrig)
c801d85f 410{
a63e17c1
VZ
411 wxMirrorDC dc(dcOrig, m_mirror);
412
2f294a9c 413 dc.SetPen(*wxBLACK_PEN);
a63e17c1 414 dc.DrawLine(0, 0, 100, 200);
c801d85f 415
2f294a9c
VZ
416 dc.SetBackgroundMode(wxTRANSPARENT);
417 dc.DrawText(_T("Testing"), 50, 50);
b7346a70 418
2f294a9c
VZ
419 dc.SetPen(*wxRED_PEN);
420 dc.SetBrush(*wxGREEN_BRUSH);
421 dc.DrawRectangle(120, 120, 100, 80);
c801d85f 422}
2f294a9c 423