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