]> git.saurik.com Git - wxWidgets.git/blame - samples/splitter/splitter.cpp
Added $(EXTRADLLFLAGS) to CFLAGS
[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$
8// Copyright: (c) Julian Smart and Markus Holzem
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"
40
2f294a9c
VZ
41// ----------------------------------------------------------------------------
42// constants
43// ----------------------------------------------------------------------------
44
45// ID for the menu commands
46enum
47{
48 SPLIT_QUIT,
49 SPLIT_HORIZONTAL,
50 SPLIT_VERTICAL,
51 SPLIT_UNSPLIT,
b795f9ca 52 SPLIT_LIVE,
6faed57d 53 SPLIT_SETPOSITION,
2f294a9c
VZ
54 SPLIT_SETMINSIZE
55};
56
57// ----------------------------------------------------------------------------
58// our classes
59// ----------------------------------------------------------------------------
c801d85f
KB
60
61class MyApp: public wxApp
62{
63public:
2f294a9c 64 bool OnInit();
c801d85f
KB
65};
66
2f294a9c 67class MyFrame: public wxFrame
c801d85f
KB
68{
69public:
2f294a9c
VZ
70 MyFrame();
71 virtual ~MyFrame();
0d559d69 72
2f294a9c
VZ
73 // Menu commands
74 void SplitHorizontal(wxCommandEvent& event);
75 void SplitVertical(wxCommandEvent& event);
76 void Unsplit(wxCommandEvent& event);
b795f9ca 77 void ToggleLive(wxCommandEvent& event);
6faed57d 78 void SetPosition(wxCommandEvent& event);
2f294a9c 79 void SetMinSize(wxCommandEvent& event);
b795f9ca 80
2f294a9c 81 void Quit(wxCommandEvent& event);
4a0b46a7 82
2f294a9c
VZ
83 // Menu command update functions
84 void UpdateUIHorizontal(wxUpdateUIEvent& event);
85 void UpdateUIVertical(wxUpdateUIEvent& event);
86 void UpdateUIUnsplit(wxUpdateUIEvent& event);
4a0b46a7 87
0d559d69 88private:
2f294a9c
VZ
89 wxScrolledWindow *m_left, *m_right;
90
91 wxSplitterWindow* m_splitter;
92
93 DECLARE_EVENT_TABLE()
0d559d69 94};
c801d85f 95
2f294a9c 96class MySplitterWindow : public wxSplitterWindow
0d559d69
VZ
97{
98public:
2f294a9c 99 MySplitterWindow(wxFrame *parent);
c801d85f 100
2f294a9c
VZ
101 // event handlers
102 void OnPositionChanged(wxSplitterEvent& event);
103 void OnPositionChanging(wxSplitterEvent& event);
104 void OnDClick(wxSplitterEvent& event);
105 void OnUnsplit(wxSplitterEvent& event);
c801d85f
KB
106
107private:
2f294a9c 108 wxFrame *m_frame;
0d559d69 109
2f294a9c 110 DECLARE_EVENT_TABLE()
c801d85f
KB
111};
112
113class MyCanvas: public wxScrolledWindow
114{
115public:
2f294a9c 116 MyCanvas(wxWindow* parent);
33611ebb 117 virtual ~MyCanvas();
c801d85f 118
2f294a9c 119 virtual void OnDraw(wxDC& dc);
c801d85f
KB
120};
121
2f294a9c
VZ
122// ============================================================================
123// implementation
124// ============================================================================
c801d85f 125
2f294a9c
VZ
126// ----------------------------------------------------------------------------
127// MyApp
128// ----------------------------------------------------------------------------
0d57be45 129
c801d85f
KB
130IMPLEMENT_APP(MyApp)
131
2f294a9c 132bool MyApp::OnInit()
c801d85f 133{
2f294a9c
VZ
134 // create and show the main frame
135 MyFrame* frame = new MyFrame;
4a0b46a7 136
2f294a9c 137 frame->Show(TRUE);
c801d85f 138
2f294a9c 139 return TRUE;
c801d85f
KB
140}
141
2f294a9c
VZ
142// ----------------------------------------------------------------------------
143// MyFrame
144// ----------------------------------------------------------------------------
145
c801d85f 146BEGIN_EVENT_TABLE(MyFrame, wxFrame)
2f294a9c
VZ
147 EVT_MENU(SPLIT_VERTICAL, MyFrame::SplitVertical)
148 EVT_MENU(SPLIT_HORIZONTAL, MyFrame::SplitHorizontal)
149 EVT_MENU(SPLIT_UNSPLIT, MyFrame::Unsplit)
b795f9ca 150 EVT_MENU(SPLIT_LIVE, MyFrame::ToggleLive)
6faed57d 151 EVT_MENU(SPLIT_SETPOSITION, MyFrame::SetPosition)
2f294a9c
VZ
152 EVT_MENU(SPLIT_SETMINSIZE, MyFrame::SetMinSize)
153
b795f9ca
VZ
154 EVT_MENU(SPLIT_QUIT, MyFrame::Quit)
155
2f294a9c
VZ
156 EVT_UPDATE_UI(SPLIT_VERTICAL, MyFrame::UpdateUIVertical)
157 EVT_UPDATE_UI(SPLIT_HORIZONTAL, MyFrame::UpdateUIHorizontal)
158 EVT_UPDATE_UI(SPLIT_UNSPLIT, MyFrame::UpdateUIUnsplit)
c801d85f
KB
159END_EVENT_TABLE()
160
161// My frame constructor
2f294a9c
VZ
162MyFrame::MyFrame()
163 : wxFrame(NULL, -1, _T("wxSplitterWindow sample"),
164 wxDefaultPosition, wxSize(420, 300),
fe31f91c 165 wxDEFAULT_FRAME_STYLE | wxNO_FULL_REPAINT_ON_RESIZE)
c801d85f 166{
2f294a9c 167 CreateStatusBar(2);
b7346a70 168
2f294a9c 169 // Make a menubar
6faed57d
VZ
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"));
c801d85f 194
2f294a9c 195 wxMenuBar *menuBar = new wxMenuBar;
6faed57d 196 menuBar->Append(splitMenu, _T("&Splitter"));
c801d85f 197
2f294a9c 198 SetMenuBar(menuBar);
c801d85f 199
b795f9ca 200 menuBar->Check(SPLIT_LIVE, TRUE);
2f294a9c 201 m_splitter = new MySplitterWindow(this);
4a0b46a7 202
6b55490a 203#if 1
2f294a9c
VZ
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);
6b55490a 212#else // for testing kbd navigation inside the splitter
2f294a9c
VZ
213 m_left = new wxTextCtrl(m_splitter, -1, _T("first text"));
214 m_right = new wxTextCtrl(m_splitter, -1, _T("second text"));
6b55490a 215#endif
c801d85f 216
2f294a9c 217 // you can also do this to start with a single window
4a0b46a7 218#if 0
2f294a9c
VZ
219 m_right->Show(FALSE);
220 m_splitter->Initialize(m_left);
4a0b46a7 221#else
74c57d1f 222 // you can also try -100
2f294a9c 223 m_splitter->SplitVertically(m_left, m_right, 100);
4a0b46a7
VZ
224#endif
225
2f294a9c 226 SetStatusText(_T("Min pane size = 0"), 1);
c801d85f
KB
227}
228
229MyFrame::~MyFrame()
230{
231}
232
2f294a9c
VZ
233// menu command handlers
234
e3e65dac 235void MyFrame::Quit(wxCommandEvent& WXUNUSED(event) )
c801d85f 236{
2f294a9c 237 Close(TRUE);
c801d85f
KB
238}
239
e3e65dac 240void MyFrame::SplitHorizontal(wxCommandEvent& WXUNUSED(event) )
c801d85f 241{
2f294a9c
VZ
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 );
74c57d1f
VZ
247
248 SetStatusText(_T("Splitter split horizontally"), 1);
c801d85f
KB
249}
250
e3e65dac 251void MyFrame::SplitVertical(wxCommandEvent& WXUNUSED(event) )
c801d85f 252{
2f294a9c
VZ
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 );
74c57d1f
VZ
258
259 SetStatusText(_T("Splitter split vertically"), 1);
c801d85f
KB
260}
261
e3e65dac 262void MyFrame::Unsplit(wxCommandEvent& WXUNUSED(event) )
c801d85f 263{
2f294a9c
VZ
264 if ( m_splitter->IsSplit() )
265 m_splitter->Unsplit();
266 SetStatusText(_T("No splitter"));
0d559d69
VZ
267}
268
b795f9ca
VZ
269void 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
6faed57d
VZ
280void 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
0d559d69
VZ
300void MyFrame::SetMinSize(wxCommandEvent& WXUNUSED(event) )
301{
2f294a9c 302 wxString str;
f565a6c2 303 str.Printf( wxT("%d"), m_splitter->GetMinimumPaneSize());
2f294a9c 304 str = wxGetTextFromUser(_T("Enter minimal size for panes:"), _T(""), str, this);
6faed57d 305 if ( str.empty() )
2f294a9c
VZ
306 return;
307
308 int minsize = wxStrtol( str, (wxChar**)NULL, 10 );
309 m_splitter->SetMinimumPaneSize(minsize);
f565a6c2 310 str.Printf( wxT("Min pane size = %d"), minsize);
2f294a9c 311 SetStatusText(str, 1);
c801d85f
KB
312}
313
2f294a9c
VZ
314// Update UI handlers
315
c801d85f
KB
316void MyFrame::UpdateUIHorizontal(wxUpdateUIEvent& event)
317{
2f294a9c 318 event.Enable( (!m_splitter->IsSplit()) || (m_splitter->GetSplitMode() != wxSPLIT_HORIZONTAL) );
c801d85f
KB
319}
320
321void MyFrame::UpdateUIVertical(wxUpdateUIEvent& event)
322{
2f294a9c 323 event.Enable( ( (!m_splitter->IsSplit()) || (m_splitter->GetSplitMode() != wxSPLIT_VERTICAL) ) );
c801d85f
KB
324}
325
326void MyFrame::UpdateUIUnsplit(wxUpdateUIEvent& event)
327{
2f294a9c 328 event.Enable( m_splitter->IsSplit() );
c801d85f
KB
329}
330
2f294a9c
VZ
331// ----------------------------------------------------------------------------
332// MySplitterWindow
333// ----------------------------------------------------------------------------
334
335BEGIN_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)
342END_EVENT_TABLE()
343
344MySplitterWindow::MySplitterWindow(wxFrame *parent)
345 : wxSplitterWindow(parent, -1,
346 wxDefaultPosition, wxDefaultSize,
347 wxSP_3D | wxSP_LIVE_UPDATE | wxCLIP_CHILDREN)
c801d85f 348{
2f294a9c 349 m_frame = parent;
c801d85f
KB
350}
351
2f294a9c
VZ
352void 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
360void 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
368void MySplitterWindow::OnDClick(wxSplitterEvent& event)
369{
370 m_frame->SetStatusText(_T("Splitter double clicked"), 1);
371
372 event.Skip();
373}
374
375void MySplitterWindow::OnUnsplit(wxSplitterEvent& event)
376{
377 m_frame->SetStatusText(_T("Splitter unsplit"), 1);
378
379 event.Skip();
380}
381
382// ----------------------------------------------------------------------------
383// MyCanvas
384// ----------------------------------------------------------------------------
385
386MyCanvas::MyCanvas(wxWindow* parent)
387 : wxScrolledWindow(parent, -1)
c801d85f
KB
388{
389}
390
391MyCanvas::~MyCanvas()
392{
393}
394
395void MyCanvas::OnDraw(wxDC& dc)
396{
2f294a9c
VZ
397 dc.SetPen(*wxBLACK_PEN);
398 dc.DrawLine(0, 0, 100, 100);
c801d85f 399
2f294a9c
VZ
400 dc.SetBackgroundMode(wxTRANSPARENT);
401 dc.DrawText(_T("Testing"), 50, 50);
b7346a70 402
2f294a9c
VZ
403 dc.SetPen(*wxRED_PEN);
404 dc.SetBrush(*wxGREEN_BRUSH);
405 dc.DrawRectangle(120, 120, 100, 80);
c801d85f 406}
2f294a9c 407