]> git.saurik.com Git - wxWidgets.git/blame - samples/splitter/splitter.cpp
Fix Cygwin and MinGW compilation
[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,
44 SPLIT_SETMINSIZE
45};
46
47// ----------------------------------------------------------------------------
48// our classes
49// ----------------------------------------------------------------------------
c801d85f
KB
50
51class MyApp: public wxApp
52{
53public:
2f294a9c 54 bool OnInit();
c801d85f
KB
55};
56
2f294a9c 57class MyFrame: public wxFrame
c801d85f
KB
58{
59public:
2f294a9c
VZ
60 MyFrame();
61 virtual ~MyFrame();
0d559d69 62
2f294a9c
VZ
63 // Menu commands
64 void SplitHorizontal(wxCommandEvent& event);
65 void SplitVertical(wxCommandEvent& event);
66 void Unsplit(wxCommandEvent& event);
67 void SetMinSize(wxCommandEvent& event);
68 void Quit(wxCommandEvent& event);
4a0b46a7 69
2f294a9c
VZ
70 // Menu command update functions
71 void UpdateUIHorizontal(wxUpdateUIEvent& event);
72 void UpdateUIVertical(wxUpdateUIEvent& event);
73 void UpdateUIUnsplit(wxUpdateUIEvent& event);
4a0b46a7 74
0d559d69 75private:
2f294a9c
VZ
76 wxScrolledWindow *m_left, *m_right;
77
78 wxSplitterWindow* m_splitter;
79
80 DECLARE_EVENT_TABLE()
0d559d69 81};
c801d85f 82
2f294a9c 83class MySplitterWindow : public wxSplitterWindow
0d559d69
VZ
84{
85public:
2f294a9c 86 MySplitterWindow(wxFrame *parent);
c801d85f 87
2f294a9c
VZ
88 // event handlers
89 void OnPositionChanged(wxSplitterEvent& event);
90 void OnPositionChanging(wxSplitterEvent& event);
91 void OnDClick(wxSplitterEvent& event);
92 void OnUnsplit(wxSplitterEvent& event);
c801d85f
KB
93
94private:
2f294a9c 95 wxFrame *m_frame;
0d559d69 96
2f294a9c 97 DECLARE_EVENT_TABLE()
c801d85f
KB
98};
99
100class MyCanvas: public wxScrolledWindow
101{
102public:
2f294a9c 103 MyCanvas(wxWindow* parent);
33611ebb 104 virtual ~MyCanvas();
c801d85f 105
2f294a9c 106 virtual void OnDraw(wxDC& dc);
c801d85f
KB
107};
108
2f294a9c
VZ
109// ============================================================================
110// implementation
111// ============================================================================
c801d85f 112
2f294a9c
VZ
113// ----------------------------------------------------------------------------
114// MyApp
115// ----------------------------------------------------------------------------
0d57be45 116
c801d85f
KB
117IMPLEMENT_APP(MyApp)
118
2f294a9c 119bool MyApp::OnInit()
c801d85f 120{
2f294a9c
VZ
121 // create and show the main frame
122 MyFrame* frame = new MyFrame;
4a0b46a7 123
2f294a9c 124 frame->Show(TRUE);
c801d85f 125
2f294a9c 126 return TRUE;
c801d85f
KB
127}
128
2f294a9c
VZ
129// ----------------------------------------------------------------------------
130// MyFrame
131// ----------------------------------------------------------------------------
132
c801d85f 133BEGIN_EVENT_TABLE(MyFrame, wxFrame)
2f294a9c
VZ
134 EVT_MENU(SPLIT_VERTICAL, MyFrame::SplitVertical)
135 EVT_MENU(SPLIT_HORIZONTAL, MyFrame::SplitHorizontal)
136 EVT_MENU(SPLIT_UNSPLIT, MyFrame::Unsplit)
137 EVT_MENU(SPLIT_QUIT, MyFrame::Quit)
138 EVT_MENU(SPLIT_SETMINSIZE, MyFrame::SetMinSize)
139
140 EVT_UPDATE_UI(SPLIT_VERTICAL, MyFrame::UpdateUIVertical)
141 EVT_UPDATE_UI(SPLIT_HORIZONTAL, MyFrame::UpdateUIHorizontal)
142 EVT_UPDATE_UI(SPLIT_UNSPLIT, MyFrame::UpdateUIUnsplit)
c801d85f
KB
143END_EVENT_TABLE()
144
145// My frame constructor
2f294a9c
VZ
146MyFrame::MyFrame()
147 : wxFrame(NULL, -1, _T("wxSplitterWindow sample"),
148 wxDefaultPosition, wxSize(420, 300),
fe31f91c 149 wxDEFAULT_FRAME_STYLE | wxNO_FULL_REPAINT_ON_RESIZE)
c801d85f 150{
2f294a9c 151 CreateStatusBar(2);
b7346a70 152
2f294a9c
VZ
153 // Make a menubar
154 wxMenu *fileMenu = new wxMenu;
155 fileMenu->Append(SPLIT_VERTICAL, _T("Split &Vertically\tCtrl-V"), _T("Split vertically"));
156 fileMenu->Append(SPLIT_HORIZONTAL, _T("Split &Horizontally\tCtrl-H"), _T("Split horizontally"));
157 fileMenu->Append(SPLIT_UNSPLIT, _T("&Unsplit\tCtrl-U"), _T("Unsplit"));
158 fileMenu->AppendSeparator();
159 fileMenu->Append(SPLIT_SETMINSIZE, _T("Set &min size"), _T("Set minimum pane size"));
160 fileMenu->AppendSeparator();
161 fileMenu->Append(SPLIT_QUIT, _T("E&xit\tAlt-X"), _T("Exit"));
c801d85f 162
2f294a9c
VZ
163 wxMenuBar *menuBar = new wxMenuBar;
164 menuBar->Append(fileMenu, _T("&File"));
c801d85f 165
2f294a9c 166 SetMenuBar(menuBar);
c801d85f 167
2f294a9c 168 m_splitter = new MySplitterWindow(this);
4a0b46a7 169
6b55490a 170#if 1
2f294a9c
VZ
171 m_left = new MyCanvas(m_splitter);
172 m_left->SetBackgroundColour(*wxRED);
173 m_left->SetScrollbars(20, 20, 50, 50);
174 m_left->SetCursor(wxCursor(wxCURSOR_MAGNIFIER));
175
176 m_right = new MyCanvas(m_splitter);
177 m_right->SetBackgroundColour(*wxCYAN);
178 m_right->SetScrollbars(20, 20, 50, 50);
6b55490a 179#else // for testing kbd navigation inside the splitter
2f294a9c
VZ
180 m_left = new wxTextCtrl(m_splitter, -1, _T("first text"));
181 m_right = new wxTextCtrl(m_splitter, -1, _T("second text"));
6b55490a 182#endif
c801d85f 183
2f294a9c 184 // you can also do this to start with a single window
4a0b46a7 185#if 0
2f294a9c
VZ
186 m_right->Show(FALSE);
187 m_splitter->Initialize(m_left);
4a0b46a7 188#else
2f294a9c 189 m_splitter->SplitVertically(m_left, m_right, 100);
4a0b46a7
VZ
190#endif
191
2f294a9c 192 SetStatusText(_T("Min pane size = 0"), 1);
c801d85f
KB
193}
194
195MyFrame::~MyFrame()
196{
197}
198
2f294a9c
VZ
199// menu command handlers
200
e3e65dac 201void MyFrame::Quit(wxCommandEvent& WXUNUSED(event) )
c801d85f 202{
2f294a9c 203 Close(TRUE);
c801d85f
KB
204}
205
e3e65dac 206void MyFrame::SplitHorizontal(wxCommandEvent& WXUNUSED(event) )
c801d85f 207{
2f294a9c
VZ
208 if ( m_splitter->IsSplit() )
209 m_splitter->Unsplit();
210 m_left->Show(TRUE);
211 m_right->Show(TRUE);
212 m_splitter->SplitHorizontally( m_left, m_right );
c801d85f
KB
213}
214
e3e65dac 215void MyFrame::SplitVertical(wxCommandEvent& WXUNUSED(event) )
c801d85f 216{
2f294a9c
VZ
217 if ( m_splitter->IsSplit() )
218 m_splitter->Unsplit();
219 m_left->Show(TRUE);
220 m_right->Show(TRUE);
221 m_splitter->SplitVertically( m_left, m_right );
c801d85f
KB
222}
223
e3e65dac 224void MyFrame::Unsplit(wxCommandEvent& WXUNUSED(event) )
c801d85f 225{
2f294a9c
VZ
226 if ( m_splitter->IsSplit() )
227 m_splitter->Unsplit();
228 SetStatusText(_T("No splitter"));
0d559d69
VZ
229}
230
231void MyFrame::SetMinSize(wxCommandEvent& WXUNUSED(event) )
232{
2f294a9c
VZ
233 wxString str;
234 str.Printf( _T(_T("%d")), m_splitter->GetMinimumPaneSize());
235 str = wxGetTextFromUser(_T("Enter minimal size for panes:"), _T(""), str, this);
236 if ( str.IsEmpty() )
237 return;
238
239 int minsize = wxStrtol( str, (wxChar**)NULL, 10 );
240 m_splitter->SetMinimumPaneSize(minsize);
241 str.Printf( _T(_T("Min pane size = %d")), minsize);
242 SetStatusText(str, 1);
c801d85f
KB
243}
244
2f294a9c
VZ
245// Update UI handlers
246
c801d85f
KB
247void MyFrame::UpdateUIHorizontal(wxUpdateUIEvent& event)
248{
2f294a9c 249 event.Enable( (!m_splitter->IsSplit()) || (m_splitter->GetSplitMode() != wxSPLIT_HORIZONTAL) );
c801d85f
KB
250}
251
252void MyFrame::UpdateUIVertical(wxUpdateUIEvent& event)
253{
2f294a9c 254 event.Enable( ( (!m_splitter->IsSplit()) || (m_splitter->GetSplitMode() != wxSPLIT_VERTICAL) ) );
c801d85f
KB
255}
256
257void MyFrame::UpdateUIUnsplit(wxUpdateUIEvent& event)
258{
2f294a9c 259 event.Enable( m_splitter->IsSplit() );
c801d85f
KB
260}
261
2f294a9c
VZ
262// ----------------------------------------------------------------------------
263// MySplitterWindow
264// ----------------------------------------------------------------------------
265
266BEGIN_EVENT_TABLE(MySplitterWindow, wxSplitterWindow)
267 EVT_SPLITTER_SASH_POS_CHANGED(-1, MySplitterWindow::OnPositionChanged)
268 EVT_SPLITTER_SASH_POS_CHANGING(-1, MySplitterWindow::OnPositionChanging)
269
270 EVT_SPLITTER_DCLICK(-1, MySplitterWindow::OnDClick)
271
272 EVT_SPLITTER_UNSPLIT(-1, MySplitterWindow::OnUnsplit)
273END_EVENT_TABLE()
274
275MySplitterWindow::MySplitterWindow(wxFrame *parent)
276 : wxSplitterWindow(parent, -1,
277 wxDefaultPosition, wxDefaultSize,
278 wxSP_3D | wxSP_LIVE_UPDATE | wxCLIP_CHILDREN)
c801d85f 279{
2f294a9c 280 m_frame = parent;
c801d85f
KB
281}
282
2f294a9c
VZ
283void MySplitterWindow::OnPositionChanged(wxSplitterEvent& event)
284{
285 wxLogStatus(m_frame, _T("Position has changed, now = %d (or %d)"),
286 event.GetSashPosition(), GetSashPosition());
287
288 event.Skip();
289}
290
291void MySplitterWindow::OnPositionChanging(wxSplitterEvent& event)
292{
293 wxLogStatus(m_frame, _T("Position is changing, now = %d (or %d)"),
294 event.GetSashPosition(), GetSashPosition());
295
296 event.Skip();
297}
298
299void MySplitterWindow::OnDClick(wxSplitterEvent& event)
300{
301 m_frame->SetStatusText(_T("Splitter double clicked"), 1);
302
303 event.Skip();
304}
305
306void MySplitterWindow::OnUnsplit(wxSplitterEvent& event)
307{
308 m_frame->SetStatusText(_T("Splitter unsplit"), 1);
309
310 event.Skip();
311}
312
313// ----------------------------------------------------------------------------
314// MyCanvas
315// ----------------------------------------------------------------------------
316
317MyCanvas::MyCanvas(wxWindow* parent)
318 : wxScrolledWindow(parent, -1)
c801d85f
KB
319{
320}
321
322MyCanvas::~MyCanvas()
323{
324}
325
326void MyCanvas::OnDraw(wxDC& dc)
327{
2f294a9c
VZ
328 dc.SetPen(*wxBLACK_PEN);
329 dc.DrawLine(0, 0, 100, 100);
c801d85f 330
2f294a9c
VZ
331 dc.SetBackgroundMode(wxTRANSPARENT);
332 dc.DrawText(_T("Testing"), 50, 50);
b7346a70 333
2f294a9c
VZ
334 dc.SetPen(*wxRED_PEN);
335 dc.SetBrush(*wxGREEN_BRUSH);
336 dc.DrawRectangle(120, 120, 100, 80);
c801d85f 337}
2f294a9c 338