Cleaning.
[wxWidgets.git] / samples / sashtest / sashtest.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: sashtest.cpp
3 // Purpose: Layout/sash sample
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #ifndef WX_PRECOMP
20 #include "wx/wx.h"
21 #include "wx/mdi.h"
22 #endif
23
24 #include "wx/toolbar.h"
25 #include "wx/laywin.h"
26
27 #include "sashtest.h"
28
29 MyFrame *frame = NULL;
30 wxList my_children;
31
32 IMPLEMENT_APP(MyApp)
33
34 // For drawing lines in a canvas
35 long xpos = -1;
36 long ypos = -1;
37
38 int winNumber = 1;
39
40 // Initialise this in OnInit, not statically
41 bool MyApp::OnInit(void)
42 {
43 // Create the main frame window
44
45 frame = new MyFrame(NULL, wxID_ANY, _T("Sash Demo"), wxPoint(0, 0), wxSize(500, 400),
46 wxDEFAULT_FRAME_STYLE |
47 wxNO_FULL_REPAINT_ON_RESIZE |
48 wxHSCROLL | wxVSCROLL);
49
50 // Give it an icon (this is ignored in MDI mode: uses resources)
51 #ifdef __WXMSW__
52 frame->SetIcon(wxIcon(_T("sashtest_icn")));
53 #endif
54
55 // Make a menubar
56 wxMenu *file_menu = new wxMenu;
57
58 file_menu->Append(SASHTEST_NEW_WINDOW, _T("&New window"));
59 file_menu->Append(SASHTEST_TOGGLE_WINDOW, _T("&Toggle window"));
60 file_menu->Append(SASHTEST_QUIT, _T("&Exit"));
61
62 wxMenu *help_menu = new wxMenu;
63 help_menu->Append(SASHTEST_ABOUT, _T("&About"));
64
65 wxMenuBar *menu_bar = new wxMenuBar;
66
67 menu_bar->Append(file_menu, _T("&File"));
68 menu_bar->Append(help_menu, _T("&Help"));
69
70 // Associate the menu bar with the frame
71 frame->SetMenuBar(menu_bar);
72
73 frame->CreateStatusBar();
74
75 frame->Show(true);
76
77 SetTopWindow(frame);
78
79 return true;
80 }
81
82 BEGIN_EVENT_TABLE(MyFrame, wxMDIParentFrame)
83 EVT_MENU(SASHTEST_ABOUT, MyFrame::OnAbout)
84 EVT_MENU(SASHTEST_NEW_WINDOW, MyFrame::OnNewWindow)
85 EVT_SIZE(MyFrame::OnSize)
86 EVT_MENU(SASHTEST_QUIT, MyFrame::OnQuit)
87 EVT_MENU(SASHTEST_TOGGLE_WINDOW, MyFrame::OnToggleWindow)
88 EVT_SASH_DRAGGED_RANGE(ID_WINDOW_TOP, ID_WINDOW_BOTTOM, MyFrame::OnSashDrag)
89 END_EVENT_TABLE()
90
91
92 // Define my frame constructor
93 MyFrame::MyFrame(wxWindow *parent, const wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size,
94 const long style):
95 wxMDIParentFrame(parent, id, title, pos, size, style)
96 {
97 // Create some dummy layout windows
98
99 // A window like a toolbar
100 wxSashLayoutWindow* win =
101 new wxSashLayoutWindow(this, ID_WINDOW_TOP,
102 wxDefaultPosition, wxSize(200, 30),
103 wxNO_BORDER | wxSW_3D | wxCLIP_CHILDREN);
104
105 win->SetDefaultSize(wxSize(1000, 30));
106 win->SetOrientation(wxLAYOUT_HORIZONTAL);
107 win->SetAlignment(wxLAYOUT_TOP);
108 win->SetBackgroundColour(wxColour(255, 0, 0));
109 win->SetSashVisible(wxSASH_BOTTOM, true);
110
111 m_topWindow = win;
112
113 // A window like a statusbar
114 win = new wxSashLayoutWindow(this, ID_WINDOW_BOTTOM,
115 wxDefaultPosition, wxSize(200, 30),
116 wxNO_BORDER | wxSW_3D | wxCLIP_CHILDREN);
117 win->SetDefaultSize(wxSize(1000, 30));
118 win->SetOrientation(wxLAYOUT_HORIZONTAL);
119 win->SetAlignment(wxLAYOUT_BOTTOM);
120 win->SetBackgroundColour(wxColour(0, 0, 255));
121 win->SetSashVisible(wxSASH_TOP, true);
122
123 m_bottomWindow = win;
124
125 // A window to the left of the client window
126 win = new wxSashLayoutWindow(this, ID_WINDOW_LEFT1,
127 wxDefaultPosition, wxSize(200, 30),
128 wxNO_BORDER | wxSW_3D | wxCLIP_CHILDREN);
129 win->SetDefaultSize(wxSize(120, 1000));
130 win->SetOrientation(wxLAYOUT_VERTICAL);
131 win->SetAlignment(wxLAYOUT_LEFT);
132 win->SetBackgroundColour(wxColour(0, 255, 0));
133 win->SetSashVisible(wxSASH_RIGHT, true);
134 win->SetExtraBorderSize(10);
135
136 wxTextCtrl* textWindow = new wxTextCtrl(win, wxID_ANY, _T(""), wxDefaultPosition, wxDefaultSize,
137 wxTE_MULTILINE|wxSUNKEN_BORDER);
138 // wxTE_MULTILINE|wxNO_BORDER);
139 textWindow->SetValue(_T("A help window"));
140
141 m_leftWindow1 = win;
142
143 // Another window to the left of the client window
144 win = new wxSashLayoutWindow(this, ID_WINDOW_LEFT2,
145 wxDefaultPosition, wxSize(200, 30),
146 wxNO_BORDER | wxSW_3D | wxCLIP_CHILDREN);
147 win->SetDefaultSize(wxSize(120, 1000));
148 win->SetOrientation(wxLAYOUT_VERTICAL);
149 win->SetAlignment(wxLAYOUT_LEFT);
150 win->SetBackgroundColour(wxColour(0, 255, 255));
151 win->SetSashVisible(wxSASH_RIGHT, true);
152
153 m_leftWindow2 = win;
154 }
155
156 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
157 {
158 Close(true);
159 }
160
161 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
162 {
163 (void)wxMessageBox(_T("wxWidgets 2.0 Sash Demo\nAuthor: Julian Smart (c) 1998"), _T("About Sash Demo"));
164 }
165
166 void MyFrame::OnToggleWindow(wxCommandEvent& WXUNUSED(event))
167 {
168 if (m_leftWindow1->IsShown())
169 {
170 m_leftWindow1->Show(false);
171 }
172 else
173 {
174 m_leftWindow1->Show(true);
175 }
176 wxLayoutAlgorithm layout;
177 layout.LayoutMDIFrame(this);
178 }
179
180 void MyFrame::OnSashDrag(wxSashEvent& event)
181 {
182 if (event.GetDragStatus() == wxSASH_STATUS_OUT_OF_RANGE)
183 return;
184
185 switch (event.GetId())
186 {
187 case ID_WINDOW_TOP:
188 {
189 m_topWindow->SetDefaultSize(wxSize(1000, event.GetDragRect().height));
190 break;
191 }
192 case ID_WINDOW_LEFT1:
193 {
194 m_leftWindow1->SetDefaultSize(wxSize(event.GetDragRect().width, 1000));
195 break;
196 }
197 case ID_WINDOW_LEFT2:
198 {
199 m_leftWindow2->SetDefaultSize(wxSize(event.GetDragRect().width, 1000));
200 break;
201 }
202 case ID_WINDOW_BOTTOM:
203 {
204 m_bottomWindow->SetDefaultSize(wxSize(1000, event.GetDragRect().height));
205 break;
206 }
207 }
208 wxLayoutAlgorithm layout;
209 layout.LayoutMDIFrame(this);
210
211 // Leaves bits of itself behind sometimes
212 GetClientWindow()->Refresh();
213 }
214
215 void MyFrame::OnNewWindow(wxCommandEvent& WXUNUSED(event))
216 {
217 // Make another frame, containing a canvas
218 MyChild *subframe = new MyChild(frame, _T("Canvas Frame"),
219 wxPoint(10, 10), wxSize(300, 300),
220 wxDEFAULT_FRAME_STYLE |
221 wxNO_FULL_REPAINT_ON_RESIZE);
222
223 subframe->SetTitle(wxString::Format(_T("Canvas Frame %d"), winNumber));
224 winNumber ++;
225
226 // Give it an icon (this is ignored in MDI mode: uses resources)
227 #ifdef __WXMSW__
228 subframe->SetIcon(wxIcon(_T("sashtest_icn")));
229 #endif
230
231 // Give it a status line
232 subframe->CreateStatusBar();
233
234 // Make a menubar
235 wxMenu *file_menu = new wxMenu;
236
237 file_menu->Append(SASHTEST_NEW_WINDOW, _T("&New window"));
238 file_menu->Append(SASHTEST_CHILD_QUIT, _T("&Close child"));
239 file_menu->Append(SASHTEST_QUIT, _T("&Exit"));
240
241 wxMenu *option_menu = new wxMenu;
242
243 // Dummy option
244 option_menu->Append(SASHTEST_REFRESH, _T("&Refresh picture"));
245
246 wxMenu *help_menu = new wxMenu;
247 help_menu->Append(SASHTEST_ABOUT, _T("&About"));
248
249 wxMenuBar *menu_bar = new wxMenuBar;
250
251 menu_bar->Append(file_menu, _T("&File"));
252 menu_bar->Append(option_menu, _T("&Options"));
253 menu_bar->Append(help_menu, _T("&Help"));
254
255 // Associate the menu bar with the frame
256 subframe->SetMenuBar(menu_bar);
257
258 int width, height;
259 subframe->GetClientSize(&width, &height);
260 MyCanvas *canvas = new MyCanvas(subframe, wxPoint(0, 0), wxSize(width, height));
261 canvas->SetCursor(wxCursor(wxCURSOR_PENCIL));
262 subframe->canvas = canvas;
263
264 // Give it scrollbars
265 canvas->SetScrollbars(20, 20, 50, 50);
266
267 subframe->Show(true);
268 }
269
270 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
271 EVT_MOUSE_EVENTS(MyCanvas::OnEvent)
272 END_EVENT_TABLE()
273
274 // Define a constructor for my canvas
275 MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size)
276 : wxScrolledWindow(parent, wxID_ANY, pos, size,
277 wxSUNKEN_BORDER | wxNO_FULL_REPAINT_ON_RESIZE)
278 {
279 SetBackgroundColour(* wxWHITE);
280 }
281
282 // Define the repainting behaviour
283 void MyCanvas::OnDraw(wxDC& dc)
284 {
285 dc.SetFont(*wxSWISS_FONT);
286 dc.SetPen(*wxGREEN_PEN);
287 dc.DrawLine(0, 0, 200, 200);
288 dc.DrawLine(200, 0, 0, 200);
289
290 dc.SetBrush(*wxCYAN_BRUSH);
291 dc.SetPen(*wxRED_PEN);
292 dc.DrawRectangle(100, 100, 100, 50);
293 dc.DrawRoundedRectangle(150, 150, 100, 50, 20);
294
295 dc.DrawEllipse(250, 250, 100, 50);
296 #if wxUSE_SPLINES
297 dc.DrawSpline(50, 200, 50, 100, 200, 10);
298 #endif // wxUSE_SPLINES
299 dc.DrawLine(50, 230, 200, 230);
300 dc.DrawText(_T("This is a test string"), 50, 230);
301
302 wxPoint points[3];
303 points[0].x = 200; points[0].y = 300;
304 points[1].x = 100; points[1].y = 400;
305 points[2].x = 300; points[2].y = 400;
306
307 dc.DrawPolygon(3, points);
308 }
309
310 // This implements a tiny doodling program! Drag the mouse using
311 // the left button.
312 void MyCanvas::OnEvent(wxMouseEvent& event)
313 {
314 wxClientDC dc(this);
315 PrepareDC(dc);
316
317 wxPoint pt(event.GetLogicalPosition(dc));
318
319 if (xpos > -1 && ypos > -1 && event.Dragging())
320 {
321 dc.SetPen(*wxBLACK_PEN);
322 dc.DrawLine(xpos, ypos, pt.x, pt.y);
323 }
324 xpos = pt.x;
325 ypos = pt.y;
326 }
327
328 void MyFrame::OnSize(wxSizeEvent& WXUNUSED(event))
329 {
330 wxLayoutAlgorithm layout;
331 layout.LayoutMDIFrame(this);
332 }
333
334 // Note that SASHTEST_NEW_WINDOW and SASHTEST_ABOUT commands get passed
335 // to the parent window for processing, so no need to
336 // duplicate event handlers here.
337
338 BEGIN_EVENT_TABLE(MyChild, wxMDIChildFrame)
339 EVT_MENU(SASHTEST_CHILD_QUIT, MyChild::OnQuit)
340 END_EVENT_TABLE()
341
342 MyChild::MyChild(wxMDIParentFrame *parent, const wxString& title, const wxPoint& pos, const wxSize& size,
343 const long style):
344 wxMDIChildFrame(parent, wxID_ANY, title, pos, size, style)
345 {
346 canvas = NULL;
347 my_children.Append(this);
348 }
349
350 MyChild::~MyChild(void)
351 {
352 my_children.DeleteObject(this);
353 }
354
355 void MyChild::OnQuit(wxCommandEvent& WXUNUSED(event))
356 {
357 Close(true);
358 }
359
360 void MyChild::OnActivate(wxActivateEvent& event)
361 {
362 if (event.GetActive() && canvas)
363 canvas->SetFocus();
364 }
365