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