]> git.saurik.com Git - wxWidgets.git/blob - samples/splitter/splitter.cpp
Corrections for loss of XLOG2DEV and etc. macros in MSW. I assume
[wxWidgets.git] / samples / splitter / splitter.cpp
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/wx.h"
29 #endif
30
31 #include "wx/splitter.h"
32
33 // ----------------------------------------------------------------------------
34 // constants
35 // ----------------------------------------------------------------------------
36
37 // ID for the menu commands
38 enum
39 {
40 SPLIT_QUIT,
41 SPLIT_HORIZONTAL,
42 SPLIT_VERTICAL,
43 SPLIT_UNSPLIT,
44 SPLIT_SETMINSIZE
45 };
46
47 // ----------------------------------------------------------------------------
48 // our classes
49 // ----------------------------------------------------------------------------
50
51 class MyApp: public wxApp
52 {
53 public:
54 bool OnInit();
55 };
56
57 class MyFrame: public wxFrame
58 {
59 public:
60 MyFrame();
61 virtual ~MyFrame();
62
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);
69
70 // Menu command update functions
71 void UpdateUIHorizontal(wxUpdateUIEvent& event);
72 void UpdateUIVertical(wxUpdateUIEvent& event);
73 void UpdateUIUnsplit(wxUpdateUIEvent& event);
74
75 private:
76 wxScrolledWindow *m_left, *m_right;
77
78 wxSplitterWindow* m_splitter;
79
80 DECLARE_EVENT_TABLE()
81 };
82
83 class MySplitterWindow : public wxSplitterWindow
84 {
85 public:
86 MySplitterWindow(wxFrame *parent);
87
88 // event handlers
89 void OnPositionChanged(wxSplitterEvent& event);
90 void OnPositionChanging(wxSplitterEvent& event);
91 void OnDClick(wxSplitterEvent& event);
92 void OnUnsplit(wxSplitterEvent& event);
93
94 private:
95 wxFrame *m_frame;
96
97 DECLARE_EVENT_TABLE()
98 };
99
100 class MyCanvas: public wxScrolledWindow
101 {
102 public:
103 MyCanvas(wxWindow* parent);
104 virtual ~MyCanvas();
105
106 virtual void OnDraw(wxDC& dc);
107 };
108
109 // ============================================================================
110 // implementation
111 // ============================================================================
112
113 // ----------------------------------------------------------------------------
114 // MyApp
115 // ----------------------------------------------------------------------------
116
117 IMPLEMENT_APP(MyApp)
118
119 bool MyApp::OnInit()
120 {
121 // create and show the main frame
122 MyFrame* frame = new MyFrame;
123
124 frame->Show(TRUE);
125
126 return TRUE;
127 }
128
129 // ----------------------------------------------------------------------------
130 // MyFrame
131 // ----------------------------------------------------------------------------
132
133 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
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)
143 END_EVENT_TABLE()
144
145 // My frame constructor
146 MyFrame::MyFrame()
147 : wxFrame(NULL, -1, _T("wxSplitterWindow sample"),
148 wxDefaultPosition, wxSize(420, 300),
149 wxDEFAULT_FRAME_STYLE | wxNO_FULL_REPAINT_ON_RESIZE)
150 {
151 CreateStatusBar(2);
152
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"));
162
163 wxMenuBar *menuBar = new wxMenuBar;
164 menuBar->Append(fileMenu, _T("&File"));
165
166 SetMenuBar(menuBar);
167
168 m_splitter = new MySplitterWindow(this);
169
170 #if 1
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);
179 #else // for testing kbd navigation inside the splitter
180 m_left = new wxTextCtrl(m_splitter, -1, _T("first text"));
181 m_right = new wxTextCtrl(m_splitter, -1, _T("second text"));
182 #endif
183
184 // you can also do this to start with a single window
185 #if 0
186 m_right->Show(FALSE);
187 m_splitter->Initialize(m_left);
188 #else
189 m_splitter->SplitVertically(m_left, m_right, 100);
190 #endif
191
192 SetStatusText(_T("Min pane size = 0"), 1);
193 }
194
195 MyFrame::~MyFrame()
196 {
197 }
198
199 // menu command handlers
200
201 void MyFrame::Quit(wxCommandEvent& WXUNUSED(event) )
202 {
203 Close(TRUE);
204 }
205
206 void MyFrame::SplitHorizontal(wxCommandEvent& WXUNUSED(event) )
207 {
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 );
213 }
214
215 void MyFrame::SplitVertical(wxCommandEvent& WXUNUSED(event) )
216 {
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 );
222 }
223
224 void MyFrame::Unsplit(wxCommandEvent& WXUNUSED(event) )
225 {
226 if ( m_splitter->IsSplit() )
227 m_splitter->Unsplit();
228 SetStatusText(_T("No splitter"));
229 }
230
231 void MyFrame::SetMinSize(wxCommandEvent& WXUNUSED(event) )
232 {
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);
243 }
244
245 // Update UI handlers
246
247 void MyFrame::UpdateUIHorizontal(wxUpdateUIEvent& event)
248 {
249 event.Enable( (!m_splitter->IsSplit()) || (m_splitter->GetSplitMode() != wxSPLIT_HORIZONTAL) );
250 }
251
252 void MyFrame::UpdateUIVertical(wxUpdateUIEvent& event)
253 {
254 event.Enable( ( (!m_splitter->IsSplit()) || (m_splitter->GetSplitMode() != wxSPLIT_VERTICAL) ) );
255 }
256
257 void MyFrame::UpdateUIUnsplit(wxUpdateUIEvent& event)
258 {
259 event.Enable( m_splitter->IsSplit() );
260 }
261
262 // ----------------------------------------------------------------------------
263 // MySplitterWindow
264 // ----------------------------------------------------------------------------
265
266 BEGIN_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)
273 END_EVENT_TABLE()
274
275 MySplitterWindow::MySplitterWindow(wxFrame *parent)
276 : wxSplitterWindow(parent, -1,
277 wxDefaultPosition, wxDefaultSize,
278 wxSP_3D | wxSP_LIVE_UPDATE | wxCLIP_CHILDREN)
279 {
280 m_frame = parent;
281 }
282
283 void 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
291 void 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
299 void MySplitterWindow::OnDClick(wxSplitterEvent& event)
300 {
301 m_frame->SetStatusText(_T("Splitter double clicked"), 1);
302
303 event.Skip();
304 }
305
306 void MySplitterWindow::OnUnsplit(wxSplitterEvent& event)
307 {
308 m_frame->SetStatusText(_T("Splitter unsplit"), 1);
309
310 event.Skip();
311 }
312
313 // ----------------------------------------------------------------------------
314 // MyCanvas
315 // ----------------------------------------------------------------------------
316
317 MyCanvas::MyCanvas(wxWindow* parent)
318 : wxScrolledWindow(parent, -1)
319 {
320 }
321
322 MyCanvas::~MyCanvas()
323 {
324 }
325
326 void MyCanvas::OnDraw(wxDC& dc)
327 {
328 dc.SetPen(*wxBLACK_PEN);
329 dc.DrawLine(0, 0, 100, 100);
330
331 dc.SetBackgroundMode(wxTRANSPARENT);
332 dc.DrawText(_T("Testing"), 50, 50);
333
334 dc.SetPen(*wxRED_PEN);
335 dc.SetBrush(*wxGREEN_BRUSH);
336 dc.DrawRectangle(120, 120, 100, 80);
337 }
338