fixes to handling of 0 and negative splitter position when splitting it initially
[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 // you can also try -100
190 m_splitter->SplitVertically(m_left, m_right, 100);
191 #endif
192
193 SetStatusText(_T("Min pane size = 0"), 1);
194 }
195
196 MyFrame::~MyFrame()
197 {
198 }
199
200 // menu command handlers
201
202 void MyFrame::Quit(wxCommandEvent& WXUNUSED(event) )
203 {
204 Close(TRUE);
205 }
206
207 void MyFrame::SplitHorizontal(wxCommandEvent& WXUNUSED(event) )
208 {
209 if ( m_splitter->IsSplit() )
210 m_splitter->Unsplit();
211 m_left->Show(TRUE);
212 m_right->Show(TRUE);
213 m_splitter->SplitHorizontally( m_left, m_right );
214
215 SetStatusText(_T("Splitter split horizontally"), 1);
216 }
217
218 void MyFrame::SplitVertical(wxCommandEvent& WXUNUSED(event) )
219 {
220 if ( m_splitter->IsSplit() )
221 m_splitter->Unsplit();
222 m_left->Show(TRUE);
223 m_right->Show(TRUE);
224 m_splitter->SplitVertically( m_left, m_right );
225
226 SetStatusText(_T("Splitter split vertically"), 1);
227 }
228
229 void MyFrame::Unsplit(wxCommandEvent& WXUNUSED(event) )
230 {
231 if ( m_splitter->IsSplit() )
232 m_splitter->Unsplit();
233 SetStatusText(_T("No splitter"));
234 }
235
236 void MyFrame::SetMinSize(wxCommandEvent& WXUNUSED(event) )
237 {
238 wxString str;
239 str.Printf( _T(_T("%d")), m_splitter->GetMinimumPaneSize());
240 str = wxGetTextFromUser(_T("Enter minimal size for panes:"), _T(""), str, this);
241 if ( str.IsEmpty() )
242 return;
243
244 int minsize = wxStrtol( str, (wxChar**)NULL, 10 );
245 m_splitter->SetMinimumPaneSize(minsize);
246 str.Printf( _T(_T("Min pane size = %d")), minsize);
247 SetStatusText(str, 1);
248 }
249
250 // Update UI handlers
251
252 void MyFrame::UpdateUIHorizontal(wxUpdateUIEvent& event)
253 {
254 event.Enable( (!m_splitter->IsSplit()) || (m_splitter->GetSplitMode() != wxSPLIT_HORIZONTAL) );
255 }
256
257 void MyFrame::UpdateUIVertical(wxUpdateUIEvent& event)
258 {
259 event.Enable( ( (!m_splitter->IsSplit()) || (m_splitter->GetSplitMode() != wxSPLIT_VERTICAL) ) );
260 }
261
262 void MyFrame::UpdateUIUnsplit(wxUpdateUIEvent& event)
263 {
264 event.Enable( m_splitter->IsSplit() );
265 }
266
267 // ----------------------------------------------------------------------------
268 // MySplitterWindow
269 // ----------------------------------------------------------------------------
270
271 BEGIN_EVENT_TABLE(MySplitterWindow, wxSplitterWindow)
272 EVT_SPLITTER_SASH_POS_CHANGED(-1, MySplitterWindow::OnPositionChanged)
273 EVT_SPLITTER_SASH_POS_CHANGING(-1, MySplitterWindow::OnPositionChanging)
274
275 EVT_SPLITTER_DCLICK(-1, MySplitterWindow::OnDClick)
276
277 EVT_SPLITTER_UNSPLIT(-1, MySplitterWindow::OnUnsplit)
278 END_EVENT_TABLE()
279
280 MySplitterWindow::MySplitterWindow(wxFrame *parent)
281 : wxSplitterWindow(parent, -1,
282 wxDefaultPosition, wxDefaultSize,
283 wxSP_3D | wxSP_LIVE_UPDATE | wxCLIP_CHILDREN)
284 {
285 m_frame = parent;
286 }
287
288 void MySplitterWindow::OnPositionChanged(wxSplitterEvent& event)
289 {
290 wxLogStatus(m_frame, _T("Position has changed, now = %d (or %d)"),
291 event.GetSashPosition(), GetSashPosition());
292
293 event.Skip();
294 }
295
296 void MySplitterWindow::OnPositionChanging(wxSplitterEvent& event)
297 {
298 wxLogStatus(m_frame, _T("Position is changing, now = %d (or %d)"),
299 event.GetSashPosition(), GetSashPosition());
300
301 event.Skip();
302 }
303
304 void MySplitterWindow::OnDClick(wxSplitterEvent& event)
305 {
306 m_frame->SetStatusText(_T("Splitter double clicked"), 1);
307
308 event.Skip();
309 }
310
311 void MySplitterWindow::OnUnsplit(wxSplitterEvent& event)
312 {
313 m_frame->SetStatusText(_T("Splitter unsplit"), 1);
314
315 event.Skip();
316 }
317
318 // ----------------------------------------------------------------------------
319 // MyCanvas
320 // ----------------------------------------------------------------------------
321
322 MyCanvas::MyCanvas(wxWindow* parent)
323 : wxScrolledWindow(parent, -1)
324 {
325 }
326
327 MyCanvas::~MyCanvas()
328 {
329 }
330
331 void MyCanvas::OnDraw(wxDC& dc)
332 {
333 dc.SetPen(*wxBLACK_PEN);
334 dc.DrawLine(0, 0, 100, 100);
335
336 dc.SetBackgroundMode(wxTRANSPARENT);
337 dc.DrawText(_T("Testing"), 50, 50);
338
339 dc.SetPen(*wxRED_PEN);
340 dc.SetBrush(*wxGREEN_BRUSH);
341 dc.DrawRectangle(120, 120, 100, 80);
342 }
343