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