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