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