]> git.saurik.com Git - wxWidgets.git/blame - samples/splitter/splitter.cpp
Make Univ/GTK compile again
[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
2f294a9c 28 #include "wx/wx.h"
c801d85f
KB
29#endif
30
31#include "wx/splitter.h"
32
2f294a9c
VZ
33// ----------------------------------------------------------------------------
34// constants
35// ----------------------------------------------------------------------------
36
37// ID for the menu commands
38enum
39{
40 SPLIT_QUIT,
41 SPLIT_HORIZONTAL,
42 SPLIT_VERTICAL,
43 SPLIT_UNSPLIT,
b795f9ca 44 SPLIT_LIVE,
2f294a9c
VZ
45 SPLIT_SETMINSIZE
46};
47
48// ----------------------------------------------------------------------------
49// our classes
50// ----------------------------------------------------------------------------
c801d85f
KB
51
52class MyApp: public wxApp
53{
54public:
2f294a9c 55 bool OnInit();
c801d85f
KB
56};
57
2f294a9c 58class MyFrame: public wxFrame
c801d85f
KB
59{
60public:
2f294a9c
VZ
61 MyFrame();
62 virtual ~MyFrame();
0d559d69 63
2f294a9c
VZ
64 // Menu commands
65 void SplitHorizontal(wxCommandEvent& event);
66 void SplitVertical(wxCommandEvent& event);
67 void Unsplit(wxCommandEvent& event);
b795f9ca 68 void ToggleLive(wxCommandEvent& event);
2f294a9c 69 void SetMinSize(wxCommandEvent& event);
b795f9ca 70
2f294a9c 71 void Quit(wxCommandEvent& event);
4a0b46a7 72
2f294a9c
VZ
73 // Menu command update functions
74 void UpdateUIHorizontal(wxUpdateUIEvent& event);
75 void UpdateUIVertical(wxUpdateUIEvent& event);
76 void UpdateUIUnsplit(wxUpdateUIEvent& event);
4a0b46a7 77
0d559d69 78private:
2f294a9c
VZ
79 wxScrolledWindow *m_left, *m_right;
80
81 wxSplitterWindow* m_splitter;
82
83 DECLARE_EVENT_TABLE()
0d559d69 84};
c801d85f 85
2f294a9c 86class MySplitterWindow : public wxSplitterWindow
0d559d69
VZ
87{
88public:
2f294a9c 89 MySplitterWindow(wxFrame *parent);
c801d85f 90
2f294a9c
VZ
91 // event handlers
92 void OnPositionChanged(wxSplitterEvent& event);
93 void OnPositionChanging(wxSplitterEvent& event);
94 void OnDClick(wxSplitterEvent& event);
95 void OnUnsplit(wxSplitterEvent& event);
c801d85f
KB
96
97private:
2f294a9c 98 wxFrame *m_frame;
0d559d69 99
2f294a9c 100 DECLARE_EVENT_TABLE()
c801d85f
KB
101};
102
103class MyCanvas: public wxScrolledWindow
104{
105public:
2f294a9c 106 MyCanvas(wxWindow* parent);
33611ebb 107 virtual ~MyCanvas();
c801d85f 108
2f294a9c 109 virtual void OnDraw(wxDC& dc);
c801d85f
KB
110};
111
2f294a9c
VZ
112// ============================================================================
113// implementation
114// ============================================================================
c801d85f 115
2f294a9c
VZ
116// ----------------------------------------------------------------------------
117// MyApp
118// ----------------------------------------------------------------------------
0d57be45 119
c801d85f
KB
120IMPLEMENT_APP(MyApp)
121
2f294a9c 122bool MyApp::OnInit()
c801d85f 123{
2f294a9c
VZ
124 // create and show the main frame
125 MyFrame* frame = new MyFrame;
4a0b46a7 126
2f294a9c 127 frame->Show(TRUE);
c801d85f 128
2f294a9c 129 return TRUE;
c801d85f
KB
130}
131
2f294a9c
VZ
132// ----------------------------------------------------------------------------
133// MyFrame
134// ----------------------------------------------------------------------------
135
c801d85f 136BEGIN_EVENT_TABLE(MyFrame, wxFrame)
2f294a9c
VZ
137 EVT_MENU(SPLIT_VERTICAL, MyFrame::SplitVertical)
138 EVT_MENU(SPLIT_HORIZONTAL, MyFrame::SplitHorizontal)
139 EVT_MENU(SPLIT_UNSPLIT, MyFrame::Unsplit)
b795f9ca 140 EVT_MENU(SPLIT_LIVE, MyFrame::ToggleLive)
2f294a9c
VZ
141 EVT_MENU(SPLIT_SETMINSIZE, MyFrame::SetMinSize)
142
b795f9ca
VZ
143 EVT_MENU(SPLIT_QUIT, MyFrame::Quit)
144
2f294a9c
VZ
145 EVT_UPDATE_UI(SPLIT_VERTICAL, MyFrame::UpdateUIVertical)
146 EVT_UPDATE_UI(SPLIT_HORIZONTAL, MyFrame::UpdateUIHorizontal)
147 EVT_UPDATE_UI(SPLIT_UNSPLIT, MyFrame::UpdateUIUnsplit)
c801d85f
KB
148END_EVENT_TABLE()
149
150// My frame constructor
2f294a9c
VZ
151MyFrame::MyFrame()
152 : wxFrame(NULL, -1, _T("wxSplitterWindow sample"),
153 wxDefaultPosition, wxSize(420, 300),
fe31f91c 154 wxDEFAULT_FRAME_STYLE | wxNO_FULL_REPAINT_ON_RESIZE)
c801d85f 155{
2f294a9c 156 CreateStatusBar(2);
b7346a70 157
2f294a9c
VZ
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();
b795f9ca 164 fileMenu->Append(SPLIT_LIVE, _T("&Live update"), _T("Toggle live update mode"), TRUE);
2f294a9c
VZ
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"));
c801d85f 168
2f294a9c
VZ
169 wxMenuBar *menuBar = new wxMenuBar;
170 menuBar->Append(fileMenu, _T("&File"));
c801d85f 171
2f294a9c 172 SetMenuBar(menuBar);
c801d85f 173
b795f9ca 174 menuBar->Check(SPLIT_LIVE, TRUE);
2f294a9c 175 m_splitter = new MySplitterWindow(this);
4a0b46a7 176
6b55490a 177#if 1
2f294a9c
VZ
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);
6b55490a 186#else // for testing kbd navigation inside the splitter
2f294a9c
VZ
187 m_left = new wxTextCtrl(m_splitter, -1, _T("first text"));
188 m_right = new wxTextCtrl(m_splitter, -1, _T("second text"));
6b55490a 189#endif
c801d85f 190
2f294a9c 191 // you can also do this to start with a single window
4a0b46a7 192#if 0
2f294a9c
VZ
193 m_right->Show(FALSE);
194 m_splitter->Initialize(m_left);
4a0b46a7 195#else
74c57d1f 196 // you can also try -100
2f294a9c 197 m_splitter->SplitVertically(m_left, m_right, 100);
4a0b46a7
VZ
198#endif
199
2f294a9c 200 SetStatusText(_T("Min pane size = 0"), 1);
c801d85f
KB
201}
202
203MyFrame::~MyFrame()
204{
205}
206
2f294a9c
VZ
207// menu command handlers
208
e3e65dac 209void MyFrame::Quit(wxCommandEvent& WXUNUSED(event) )
c801d85f 210{
2f294a9c 211 Close(TRUE);
c801d85f
KB
212}
213
e3e65dac 214void MyFrame::SplitHorizontal(wxCommandEvent& WXUNUSED(event) )
c801d85f 215{
2f294a9c
VZ
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 );
74c57d1f
VZ
221
222 SetStatusText(_T("Splitter split horizontally"), 1);
c801d85f
KB
223}
224
e3e65dac 225void MyFrame::SplitVertical(wxCommandEvent& WXUNUSED(event) )
c801d85f 226{
2f294a9c
VZ
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 );
74c57d1f
VZ
232
233 SetStatusText(_T("Splitter split vertically"), 1);
c801d85f
KB
234}
235
e3e65dac 236void MyFrame::Unsplit(wxCommandEvent& WXUNUSED(event) )
c801d85f 237{
2f294a9c
VZ
238 if ( m_splitter->IsSplit() )
239 m_splitter->Unsplit();
240 SetStatusText(_T("No splitter"));
0d559d69
VZ
241}
242
b795f9ca
VZ
243void 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
0d559d69
VZ
254void MyFrame::SetMinSize(wxCommandEvent& WXUNUSED(event) )
255{
2f294a9c 256 wxString str;
f565a6c2 257 str.Printf( wxT("%d"), m_splitter->GetMinimumPaneSize());
2f294a9c
VZ
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);
f565a6c2 264 str.Printf( wxT("Min pane size = %d"), minsize);
2f294a9c 265 SetStatusText(str, 1);
c801d85f
KB
266}
267
2f294a9c
VZ
268// Update UI handlers
269
c801d85f
KB
270void MyFrame::UpdateUIHorizontal(wxUpdateUIEvent& event)
271{
2f294a9c 272 event.Enable( (!m_splitter->IsSplit()) || (m_splitter->GetSplitMode() != wxSPLIT_HORIZONTAL) );
c801d85f
KB
273}
274
275void MyFrame::UpdateUIVertical(wxUpdateUIEvent& event)
276{
2f294a9c 277 event.Enable( ( (!m_splitter->IsSplit()) || (m_splitter->GetSplitMode() != wxSPLIT_VERTICAL) ) );
c801d85f
KB
278}
279
280void MyFrame::UpdateUIUnsplit(wxUpdateUIEvent& event)
281{
2f294a9c 282 event.Enable( m_splitter->IsSplit() );
c801d85f
KB
283}
284
2f294a9c
VZ
285// ----------------------------------------------------------------------------
286// MySplitterWindow
287// ----------------------------------------------------------------------------
288
289BEGIN_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)
296END_EVENT_TABLE()
297
298MySplitterWindow::MySplitterWindow(wxFrame *parent)
299 : wxSplitterWindow(parent, -1,
300 wxDefaultPosition, wxDefaultSize,
301 wxSP_3D | wxSP_LIVE_UPDATE | wxCLIP_CHILDREN)
c801d85f 302{
2f294a9c 303 m_frame = parent;
c801d85f
KB
304}
305
2f294a9c
VZ
306void 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
314void 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
322void MySplitterWindow::OnDClick(wxSplitterEvent& event)
323{
324 m_frame->SetStatusText(_T("Splitter double clicked"), 1);
325
326 event.Skip();
327}
328
329void MySplitterWindow::OnUnsplit(wxSplitterEvent& event)
330{
331 m_frame->SetStatusText(_T("Splitter unsplit"), 1);
332
333 event.Skip();
334}
335
336// ----------------------------------------------------------------------------
337// MyCanvas
338// ----------------------------------------------------------------------------
339
340MyCanvas::MyCanvas(wxWindow* parent)
341 : wxScrolledWindow(parent, -1)
c801d85f
KB
342{
343}
344
345MyCanvas::~MyCanvas()
346{
347}
348
349void MyCanvas::OnDraw(wxDC& dc)
350{
2f294a9c
VZ
351 dc.SetPen(*wxBLACK_PEN);
352 dc.DrawLine(0, 0, 100, 100);
c801d85f 353
2f294a9c
VZ
354 dc.SetBackgroundMode(wxTRANSPARENT);
355 dc.DrawText(_T("Testing"), 50, 50);
b7346a70 356
2f294a9c
VZ
357 dc.SetPen(*wxRED_PEN);
358 dc.SetBrush(*wxGREEN_BRUSH);
359 dc.DrawRectangle(120, 120, 100, 80);
c801d85f 360}
2f294a9c 361