]> git.saurik.com Git - wxWidgets.git/blame_incremental - samples/splitter/splitter.cpp
Warning fixes to unused variables, int/wxChar keycode and other typecasting, not...
[wxWidgets.git] / samples / splitter / splitter.cpp
... / ...
CommitLineData
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
47enum
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
62class MyApp: public wxApp
63{
64public:
65 MyApp() { }
66
67 virtual bool OnInit();
68
69 DECLARE_NO_COPY_CLASS(MyApp)
70};
71
72class MyFrame: public wxFrame
73{
74public:
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
93private:
94 wxScrolledWindow *m_left, *m_right;
95
96 wxSplitterWindow* m_splitter;
97
98 DECLARE_EVENT_TABLE()
99 DECLARE_NO_COPY_CLASS(MyFrame)
100};
101
102class MySplitterWindow : public wxSplitterWindow
103{
104public:
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
113private:
114 wxFrame *m_frame;
115
116 DECLARE_EVENT_TABLE()
117 DECLARE_NO_COPY_CLASS(MySplitterWindow)
118};
119
120class MyCanvas: public wxScrolledWindow
121{
122public:
123 MyCanvas(wxWindow* parent, bool mirror);
124 virtual ~MyCanvas(){};
125
126 virtual void OnDraw(wxDC& dc);
127
128private:
129 bool m_mirror;
130
131 DECLARE_NO_COPY_CLASS(MyCanvas)
132};
133
134// ============================================================================
135// implementation
136// ============================================================================
137
138// ----------------------------------------------------------------------------
139// MyApp
140// ----------------------------------------------------------------------------
141
142IMPLEMENT_APP(MyApp)
143
144bool 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
158BEGIN_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)
171END_EVENT_TABLE()
172
173// My frame constructor
174MyFrame::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// menu command handlers
246
247void MyFrame::Quit(wxCommandEvent& WXUNUSED(event) )
248{
249 Close(true);
250}
251
252void 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#if wxUSE_STATUSBAR
261 SetStatusText(_T("Splitter split horizontally"), 1);
262#endif // wxUSE_STATUSBAR
263}
264
265void MyFrame::SplitVertical(wxCommandEvent& WXUNUSED(event) )
266{
267 if ( m_splitter->IsSplit() )
268 m_splitter->Unsplit();
269 m_left->Show(true);
270 m_right->Show(true);
271 m_splitter->SplitVertically( m_left, m_right );
272
273#if wxUSE_STATUSBAR
274 SetStatusText(_T("Splitter split vertically"), 1);
275#endif // wxUSE_STATUSBAR
276}
277
278void MyFrame::Unsplit(wxCommandEvent& WXUNUSED(event) )
279{
280 if ( m_splitter->IsSplit() )
281 m_splitter->Unsplit();
282#if wxUSE_STATUSBAR
283 SetStatusText(_T("No splitter"));
284#endif // wxUSE_STATUSBAR
285}
286
287void MyFrame::ToggleLive(wxCommandEvent& event )
288{
289 long style = m_splitter->GetWindowStyleFlag();
290 if ( event.IsChecked() )
291 style |= wxSP_LIVE_UPDATE;
292 else
293 style &= ~wxSP_LIVE_UPDATE;
294
295 m_splitter->SetWindowStyleFlag(style);
296}
297
298void MyFrame::SetPosition(wxCommandEvent& WXUNUSED(event) )
299{
300 wxString str;
301 str.Printf( wxT("%d"), m_splitter->GetSashPosition());
302 str = wxGetTextFromUser(_T("Enter splitter position:"), _T(""), str, this);
303 if ( str.empty() )
304 return;
305
306 long pos;
307 if ( !str.ToLong(&pos) )
308 {
309 wxLogError(_T("The splitter position should be an integer."));
310 return;
311 }
312
313 m_splitter->SetSashPosition(pos);
314
315 wxLogStatus(this, _T("Splitter position set to %ld"), pos);
316}
317
318void MyFrame::SetMinSize(wxCommandEvent& WXUNUSED(event) )
319{
320 wxString str;
321 str.Printf( wxT("%d"), m_splitter->GetMinimumPaneSize());
322 str = wxGetTextFromUser(_T("Enter minimal size for panes:"), _T(""), str, this);
323 if ( str.empty() )
324 return;
325
326 int minsize = wxStrtol( str, (wxChar**)NULL, 10 );
327 m_splitter->SetMinimumPaneSize(minsize);
328#if wxUSE_STATUSBAR
329 str.Printf( wxT("Min pane size = %d"), minsize);
330 SetStatusText(str, 1);
331#endif // wxUSE_STATUSBAR
332}
333
334// Update UI handlers
335
336void MyFrame::UpdateUIHorizontal(wxUpdateUIEvent& event)
337{
338 event.Enable( (!m_splitter->IsSplit()) || (m_splitter->GetSplitMode() != wxSPLIT_HORIZONTAL) );
339}
340
341void MyFrame::UpdateUIVertical(wxUpdateUIEvent& event)
342{
343 event.Enable( ( (!m_splitter->IsSplit()) || (m_splitter->GetSplitMode() != wxSPLIT_VERTICAL) ) );
344}
345
346void MyFrame::UpdateUIUnsplit(wxUpdateUIEvent& event)
347{
348 event.Enable( m_splitter->IsSplit() );
349}
350
351// ----------------------------------------------------------------------------
352// MySplitterWindow
353// ----------------------------------------------------------------------------
354
355BEGIN_EVENT_TABLE(MySplitterWindow, wxSplitterWindow)
356 EVT_SPLITTER_SASH_POS_CHANGED(wxID_ANY, MySplitterWindow::OnPositionChanged)
357 EVT_SPLITTER_SASH_POS_CHANGING(wxID_ANY, MySplitterWindow::OnPositionChanging)
358
359 EVT_SPLITTER_DCLICK(wxID_ANY, MySplitterWindow::OnDClick)
360
361 EVT_SPLITTER_UNSPLIT(wxID_ANY, MySplitterWindow::OnUnsplitEvent)
362END_EVENT_TABLE()
363
364MySplitterWindow::MySplitterWindow(wxFrame *parent)
365 : wxSplitterWindow(parent, wxID_ANY,
366 wxDefaultPosition, wxDefaultSize,
367 wxSP_3D | wxSP_LIVE_UPDATE |
368 wxCLIP_CHILDREN /* | wxSP_NO_XP_THEME */ )
369{
370 m_frame = parent;
371}
372
373void MySplitterWindow::OnPositionChanged(wxSplitterEvent& event)
374{
375 wxLogStatus(m_frame, _T("Position has changed, now = %d (or %d)"),
376 event.GetSashPosition(), GetSashPosition());
377
378 event.Skip();
379}
380
381void MySplitterWindow::OnPositionChanging(wxSplitterEvent& event)
382{
383 wxLogStatus(m_frame, _T("Position is changing, now = %d (or %d)"),
384 event.GetSashPosition(), GetSashPosition());
385
386 event.Skip();
387}
388
389void MySplitterWindow::OnDClick(wxSplitterEvent& event)
390{
391#if wxUSE_STATUSBAR
392 m_frame->SetStatusText(_T("Splitter double clicked"), 1);
393#endif // wxUSE_STATUSBAR
394
395 event.Skip();
396}
397
398void MySplitterWindow::OnUnsplitEvent(wxSplitterEvent& event)
399{
400#if wxUSE_STATUSBAR
401 m_frame->SetStatusText(_T("Splitter unsplit"), 1);
402#endif // wxUSE_STATUSBAR
403
404 event.Skip();
405}
406
407// ----------------------------------------------------------------------------
408// MyCanvas
409// ----------------------------------------------------------------------------
410
411MyCanvas::MyCanvas(wxWindow* parent, bool mirror)
412 : wxScrolledWindow(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
413 wxHSCROLL | wxVSCROLL | wxNO_FULL_REPAINT_ON_RESIZE)
414{
415 m_mirror = mirror;
416}
417
418void MyCanvas::OnDraw(wxDC& dcOrig)
419{
420 wxMirrorDC dc(dcOrig, m_mirror);
421
422 dc.SetPen(*wxBLACK_PEN);
423 dc.DrawLine(0, 0, 100, 200);
424
425 dc.SetBackgroundMode(wxTRANSPARENT);
426 dc.DrawText(_T("Testing"), 50, 50);
427
428 dc.SetPen(*wxRED_PEN);
429 dc.SetBrush(*wxGREEN_BRUSH);
430 dc.DrawRectangle(120, 120, 100, 80);
431}
432