]> git.saurik.com Git - wxWidgets.git/blob - samples/splitter/splitter.cpp
c869422b91e4f4e2650b9fdc41d0138fa5fa2fee
[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 OnUnsplit(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, -1, _T("wxSplitterWindow sample"),
176 wxDefaultPosition, wxSize(420, 300),
177 wxDEFAULT_FRAME_STYLE | wxNO_FULL_REPAINT_ON_RESIZE)
178 {
179 CreateStatusBar(2);
180
181 // Make a menubar
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"));
206
207 wxMenuBar *menuBar = new wxMenuBar;
208 menuBar->Append(splitMenu, _T("&Splitter"));
209
210 SetMenuBar(menuBar);
211
212 menuBar->Check(SPLIT_LIVE, TRUE);
213 m_splitter = new MySplitterWindow(this);
214
215 #if 1
216 m_left = new MyCanvas(m_splitter, true);
217 m_left->SetBackgroundColour(*wxRED);
218 m_left->SetScrollbars(20, 20, 5, 5);
219 m_left->SetCursor(wxCursor(wxCURSOR_MAGNIFIER));
220
221 m_right = new MyCanvas(m_splitter, false);
222 m_right->SetBackgroundColour(*wxCYAN);
223 m_right->SetScrollbars(20, 20, 5, 5);
224 #else // for testing kbd navigation inside the splitter
225 m_left = new wxTextCtrl(m_splitter, -1, _T("first text"));
226 m_right = new wxTextCtrl(m_splitter, -1, _T("second text"));
227 #endif
228
229 // you can also do this to start with a single window
230 #if 0
231 m_right->Show(FALSE);
232 m_splitter->Initialize(m_left);
233 #else
234 // you can also try -100
235 m_splitter->SplitVertically(m_left, m_right, 100);
236 #endif
237
238 SetStatusText(_T("Min pane size = 0"), 1);
239 }
240
241 MyFrame::~MyFrame()
242 {
243 }
244
245 // menu command handlers
246
247 void MyFrame::Quit(wxCommandEvent& WXUNUSED(event) )
248 {
249 Close(TRUE);
250 }
251
252 void MyFrame::SplitHorizontal(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->SplitHorizontally( m_left, m_right );
259
260 SetStatusText(_T("Splitter split horizontally"), 1);
261 }
262
263 void MyFrame::SplitVertical(wxCommandEvent& WXUNUSED(event) )
264 {
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 );
270
271 SetStatusText(_T("Splitter split vertically"), 1);
272 }
273
274 void MyFrame::Unsplit(wxCommandEvent& WXUNUSED(event) )
275 {
276 if ( m_splitter->IsSplit() )
277 m_splitter->Unsplit();
278 SetStatusText(_T("No splitter"));
279 }
280
281 void 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
292 void 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
312 void MyFrame::SetMinSize(wxCommandEvent& WXUNUSED(event) )
313 {
314 wxString str;
315 str.Printf( wxT("%d"), m_splitter->GetMinimumPaneSize());
316 str = wxGetTextFromUser(_T("Enter minimal size for panes:"), _T(""), str, this);
317 if ( str.empty() )
318 return;
319
320 int minsize = wxStrtol( str, (wxChar**)NULL, 10 );
321 m_splitter->SetMinimumPaneSize(minsize);
322 str.Printf( wxT("Min pane size = %d"), minsize);
323 SetStatusText(str, 1);
324 }
325
326 // Update UI handlers
327
328 void MyFrame::UpdateUIHorizontal(wxUpdateUIEvent& event)
329 {
330 event.Enable( (!m_splitter->IsSplit()) || (m_splitter->GetSplitMode() != wxSPLIT_HORIZONTAL) );
331 }
332
333 void MyFrame::UpdateUIVertical(wxUpdateUIEvent& event)
334 {
335 event.Enable( ( (!m_splitter->IsSplit()) || (m_splitter->GetSplitMode() != wxSPLIT_VERTICAL) ) );
336 }
337
338 void MyFrame::UpdateUIUnsplit(wxUpdateUIEvent& event)
339 {
340 event.Enable( m_splitter->IsSplit() );
341 }
342
343 // ----------------------------------------------------------------------------
344 // MySplitterWindow
345 // ----------------------------------------------------------------------------
346
347 BEGIN_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)
354 END_EVENT_TABLE()
355
356 MySplitterWindow::MySplitterWindow(wxFrame *parent)
357 : wxSplitterWindow(parent, -1,
358 wxDefaultPosition, wxDefaultSize,
359 0x700| wxSP_LIVE_UPDATE | wxCLIP_CHILDREN)
360 {
361 m_frame = parent;
362 }
363
364 void 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
372 void 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
380 void MySplitterWindow::OnDClick(wxSplitterEvent& event)
381 {
382 m_frame->SetStatusText(_T("Splitter double clicked"), 1);
383
384 event.Skip();
385 }
386
387 void MySplitterWindow::OnUnsplit(wxSplitterEvent& event)
388 {
389 m_frame->SetStatusText(_T("Splitter unsplit"), 1);
390
391 event.Skip();
392 }
393
394 // ----------------------------------------------------------------------------
395 // MyCanvas
396 // ----------------------------------------------------------------------------
397
398 MyCanvas::MyCanvas(wxWindow* parent, bool mirror)
399 : wxScrolledWindow(parent, -1, wxDefaultPosition, wxDefaultSize,
400 wxHSCROLL | wxVSCROLL | wxNO_FULL_REPAINT_ON_RESIZE)
401 {
402 m_mirror = mirror;
403 }
404
405 MyCanvas::~MyCanvas()
406 {
407 }
408
409 void MyCanvas::OnDraw(wxDC& dcOrig)
410 {
411 wxMirrorDC dc(dcOrig, m_mirror);
412
413 dc.SetPen(*wxBLACK_PEN);
414 dc.DrawLine(0, 0, 100, 200);
415
416 dc.SetBackgroundMode(wxTRANSPARENT);
417 dc.DrawText(_T("Testing"), 50, 50);
418
419 dc.SetPen(*wxRED_PEN);
420 dc.SetBrush(*wxGREEN_BRUSH);
421 dc.DrawRectangle(120, 120, 100, 80);
422 }
423