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