Fixes for wxUSE_STATUSBAR.
[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/dcmirror.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 MyApp() { }
66
67 virtual bool OnInit();
68
69 DECLARE_NO_COPY_CLASS(MyApp)
70 };
71
72 class MyFrame: public wxFrame
73 {
74 public:
75 MyFrame();
76 virtual ~MyFrame();
77
78 // Menu commands
79 void SplitHorizontal(wxCommandEvent& event);
80 void SplitVertical(wxCommandEvent& event);
81 void Unsplit(wxCommandEvent& event);
82 void ToggleLive(wxCommandEvent& event);
83 void SetPosition(wxCommandEvent& event);
84 void SetMinSize(wxCommandEvent& event);
85
86 void Quit(wxCommandEvent& event);
87
88 // Menu command update functions
89 void UpdateUIHorizontal(wxUpdateUIEvent& event);
90 void UpdateUIVertical(wxUpdateUIEvent& event);
91 void UpdateUIUnsplit(wxUpdateUIEvent& event);
92
93 private:
94 wxScrolledWindow *m_left, *m_right;
95
96 wxSplitterWindow* m_splitter;
97
98 DECLARE_EVENT_TABLE()
99 DECLARE_NO_COPY_CLASS(MyFrame)
100 };
101
102 class MySplitterWindow : public wxSplitterWindow
103 {
104 public:
105 MySplitterWindow(wxFrame *parent);
106
107 // event handlers
108 void OnPositionChanged(wxSplitterEvent& event);
109 void OnPositionChanging(wxSplitterEvent& event);
110 void OnDClick(wxSplitterEvent& event);
111 void OnUnsplitEvent(wxSplitterEvent& event);
112
113 private:
114 wxFrame *m_frame;
115
116 DECLARE_EVENT_TABLE()
117 DECLARE_NO_COPY_CLASS(MySplitterWindow)
118 };
119
120 class MyCanvas: public wxScrolledWindow
121 {
122 public:
123 MyCanvas(wxWindow* parent, bool mirror);
124 virtual ~MyCanvas();
125
126 virtual void OnDraw(wxDC& dc);
127
128 private:
129 bool m_mirror;
130
131 DECLARE_NO_COPY_CLASS(MyCanvas)
132 };
133
134 // ============================================================================
135 // implementation
136 // ============================================================================
137
138 // ----------------------------------------------------------------------------
139 // MyApp
140 // ----------------------------------------------------------------------------
141
142 IMPLEMENT_APP(MyApp)
143
144 bool MyApp::OnInit()
145 {
146 // create and show the main frame
147 MyFrame* frame = new MyFrame;
148
149 frame->Show(true);
150
151 return true;
152 }
153
154 // ----------------------------------------------------------------------------
155 // MyFrame
156 // ----------------------------------------------------------------------------
157
158 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
159 EVT_MENU(SPLIT_VERTICAL, MyFrame::SplitVertical)
160 EVT_MENU(SPLIT_HORIZONTAL, MyFrame::SplitHorizontal)
161 EVT_MENU(SPLIT_UNSPLIT, MyFrame::Unsplit)
162 EVT_MENU(SPLIT_LIVE, MyFrame::ToggleLive)
163 EVT_MENU(SPLIT_SETPOSITION, MyFrame::SetPosition)
164 EVT_MENU(SPLIT_SETMINSIZE, MyFrame::SetMinSize)
165
166 EVT_MENU(SPLIT_QUIT, MyFrame::Quit)
167
168 EVT_UPDATE_UI(SPLIT_VERTICAL, MyFrame::UpdateUIVertical)
169 EVT_UPDATE_UI(SPLIT_HORIZONTAL, MyFrame::UpdateUIHorizontal)
170 EVT_UPDATE_UI(SPLIT_UNSPLIT, MyFrame::UpdateUIUnsplit)
171 END_EVENT_TABLE()
172
173 // My frame constructor
174 MyFrame::MyFrame()
175 : wxFrame(NULL, wxID_ANY, _T("wxSplitterWindow sample"),
176 wxDefaultPosition, wxSize(420, 300),
177 wxDEFAULT_FRAME_STYLE | wxNO_FULL_REPAINT_ON_RESIZE)
178 {
179 #if wxUSE_STATUSBAR
180 CreateStatusBar(2);
181 #endif // wxUSE_STATUSBAR
182
183 // Make a menubar
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"));
208
209 wxMenuBar *menuBar = new wxMenuBar;
210 menuBar->Append(splitMenu, _T("&Splitter"));
211
212 SetMenuBar(menuBar);
213
214 menuBar->Check(SPLIT_LIVE, true);
215 m_splitter = new MySplitterWindow(this);
216
217 #if 1
218 m_left = new MyCanvas(m_splitter, true);
219 m_left->SetBackgroundColour(*wxRED);
220 m_left->SetScrollbars(20, 20, 5, 5);
221 m_left->SetCursor(wxCursor(wxCURSOR_MAGNIFIER));
222
223 m_right = new MyCanvas(m_splitter, false);
224 m_right->SetBackgroundColour(*wxCYAN);
225 m_right->SetScrollbars(20, 20, 5, 5);
226 #else // for testing kbd navigation inside the splitter
227 m_left = new wxTextCtrl(m_splitter, wxID_ANY, _T("first text"));
228 m_right = new wxTextCtrl(m_splitter, wxID_ANY, _T("second text"));
229 #endif
230
231 // you can also do this to start with a single window
232 #if 0
233 m_right->Show(false);
234 m_splitter->Initialize(m_left);
235 #else
236 // you can also try -100
237 m_splitter->SplitVertically(m_left, m_right, 100);
238 #endif
239
240 #if wxUSE_STATUSBAR
241 SetStatusText(_T("Min pane size = 0"), 1);
242 #endif // wxUSE_STATUSBAR
243 }
244
245 MyFrame::~MyFrame()
246 {
247 }
248
249 // menu command handlers
250
251 void MyFrame::Quit(wxCommandEvent& WXUNUSED(event) )
252 {
253 Close(true);
254 }
255
256 void MyFrame::SplitHorizontal(wxCommandEvent& WXUNUSED(event) )
257 {
258 if ( m_splitter->IsSplit() )
259 m_splitter->Unsplit();
260 m_left->Show(true);
261 m_right->Show(true);
262 m_splitter->SplitHorizontally( m_left, m_right );
263
264 #if wxUSE_STATUSBAR
265 SetStatusText(_T("Splitter split horizontally"), 1);
266 #endif // wxUSE_STATUSBAR
267 }
268
269 void MyFrame::SplitVertical(wxCommandEvent& WXUNUSED(event) )
270 {
271 if ( m_splitter->IsSplit() )
272 m_splitter->Unsplit();
273 m_left->Show(true);
274 m_right->Show(true);
275 m_splitter->SplitVertically( m_left, m_right );
276
277 #if wxUSE_STATUSBAR
278 SetStatusText(_T("Splitter split vertically"), 1);
279 #endif // wxUSE_STATUSBAR
280 }
281
282 void MyFrame::Unsplit(wxCommandEvent& WXUNUSED(event) )
283 {
284 if ( m_splitter->IsSplit() )
285 m_splitter->Unsplit();
286 #if wxUSE_STATUSBAR
287 SetStatusText(_T("No splitter"));
288 #endif // wxUSE_STATUSBAR
289 }
290
291 void 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
302 void 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
322 void MyFrame::SetMinSize(wxCommandEvent& WXUNUSED(event) )
323 {
324 wxString str;
325 str.Printf( wxT("%d"), m_splitter->GetMinimumPaneSize());
326 str = wxGetTextFromUser(_T("Enter minimal size for panes:"), _T(""), str, this);
327 if ( str.empty() )
328 return;
329
330 int minsize = wxStrtol( str, (wxChar**)NULL, 10 );
331 m_splitter->SetMinimumPaneSize(minsize);
332 #if wxUSE_STATUSBAR
333 str.Printf( wxT("Min pane size = %d"), minsize);
334 SetStatusText(str, 1);
335 #endif // wxUSE_STATUSBAR
336 }
337
338 // Update UI handlers
339
340 void MyFrame::UpdateUIHorizontal(wxUpdateUIEvent& event)
341 {
342 event.Enable( (!m_splitter->IsSplit()) || (m_splitter->GetSplitMode() != wxSPLIT_HORIZONTAL) );
343 }
344
345 void MyFrame::UpdateUIVertical(wxUpdateUIEvent& event)
346 {
347 event.Enable( ( (!m_splitter->IsSplit()) || (m_splitter->GetSplitMode() != wxSPLIT_VERTICAL) ) );
348 }
349
350 void MyFrame::UpdateUIUnsplit(wxUpdateUIEvent& event)
351 {
352 event.Enable( m_splitter->IsSplit() );
353 }
354
355 // ----------------------------------------------------------------------------
356 // MySplitterWindow
357 // ----------------------------------------------------------------------------
358
359 BEGIN_EVENT_TABLE(MySplitterWindow, wxSplitterWindow)
360 EVT_SPLITTER_SASH_POS_CHANGED(wxID_ANY, MySplitterWindow::OnPositionChanged)
361 EVT_SPLITTER_SASH_POS_CHANGING(wxID_ANY, MySplitterWindow::OnPositionChanging)
362
363 EVT_SPLITTER_DCLICK(wxID_ANY, MySplitterWindow::OnDClick)
364
365 EVT_SPLITTER_UNSPLIT(wxID_ANY, MySplitterWindow::OnUnsplitEvent)
366 END_EVENT_TABLE()
367
368 MySplitterWindow::MySplitterWindow(wxFrame *parent)
369 : wxSplitterWindow(parent, wxID_ANY,
370 wxDefaultPosition, wxDefaultSize,
371 wxSP_3D | wxSP_LIVE_UPDATE |
372 wxCLIP_CHILDREN /* | wxSP_NO_XP_THEME */ )
373 {
374 m_frame = parent;
375 }
376
377 void 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
385 void 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
393 void MySplitterWindow::OnDClick(wxSplitterEvent& event)
394 {
395 #if wxUSE_STATUSBAR
396 m_frame->SetStatusText(_T("Splitter double clicked"), 1);
397 #endif // wxUSE_STATUSBAR
398
399 event.Skip();
400 }
401
402 void MySplitterWindow::OnUnsplitEvent(wxSplitterEvent& event)
403 {
404 #if wxUSE_STATUSBAR
405 m_frame->SetStatusText(_T("Splitter unsplit"), 1);
406 #endif // wxUSE_STATUSBAR
407
408 event.Skip();
409 }
410
411 // ----------------------------------------------------------------------------
412 // MyCanvas
413 // ----------------------------------------------------------------------------
414
415 MyCanvas::MyCanvas(wxWindow* parent, bool mirror)
416 : wxScrolledWindow(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
417 wxHSCROLL | wxVSCROLL | wxNO_FULL_REPAINT_ON_RESIZE)
418 {
419 m_mirror = mirror;
420 }
421
422 MyCanvas::~MyCanvas()
423 {
424 }
425
426 void MyCanvas::OnDraw(wxDC& dcOrig)
427 {
428 wxMirrorDC dc(dcOrig, m_mirror);
429
430 dc.SetPen(*wxBLACK_PEN);
431 dc.DrawLine(0, 0, 100, 200);
432
433 dc.SetBackgroundMode(wxTRANSPARENT);
434 dc.DrawText(_T("Testing"), 50, 50);
435
436 dc.SetPen(*wxRED_PEN);
437 dc.SetBrush(*wxGREEN_BRUSH);
438 dc.DrawRectangle(120, 120, 100, 80);
439 }
440