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