]> git.saurik.com Git - wxWidgets.git/blob - samples/sashtest/sashtest.cpp
fixed a typo and uncommented some methods
[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_STYLE | 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_STYLE);
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 SetBackgroundColour(* wxWHITE);
270 }
271
272 // Define the repainting behaviour
273 void MyCanvas::OnDraw(wxDC& dc)
274 {
275 dc.SetFont(*wxSWISS_FONT);
276 dc.SetPen(*wxGREEN_PEN);
277 dc.DrawLine(0, 0, 200, 200);
278 dc.DrawLine(200, 0, 0, 200);
279
280 dc.SetBrush(*wxCYAN_BRUSH);
281 dc.SetPen(*wxRED_PEN);
282 dc.DrawRectangle(100, 100, 100, 50);
283 dc.DrawRoundedRectangle(150, 150, 100, 50, 20);
284
285 dc.DrawEllipse(250, 250, 100, 50);
286 #if wxUSE_SPLINES
287 dc.DrawSpline(50, 200, 50, 100, 200, 10);
288 #endif // wxUSE_SPLINES
289 dc.DrawLine(50, 230, 200, 230);
290 dc.DrawText("This is a test string", 50, 230);
291
292 wxPoint points[3];
293 points[0].x = 200; points[0].y = 300;
294 points[1].x = 100; points[1].y = 400;
295 points[2].x = 300; points[2].y = 400;
296
297 dc.DrawPolygon(3, points);
298 }
299
300 // This implements a tiny doodling program! Drag the mouse using
301 // the left button.
302 void MyCanvas::OnEvent(wxMouseEvent& event)
303 {
304 wxClientDC dc(this);
305 PrepareDC(dc);
306
307 wxPoint pt(event.GetLogicalPosition(dc));
308
309 if (xpos > -1 && ypos > -1 && event.Dragging())
310 {
311 dc.SetPen(*wxBLACK_PEN);
312 dc.DrawLine(xpos, ypos, pt.x, pt.y);
313 }
314 xpos = pt.x;
315 ypos = pt.y;
316 }
317
318 void MyFrame::OnSize(wxSizeEvent& WXUNUSED(event))
319 {
320 wxLayoutAlgorithm layout;
321 layout.LayoutMDIFrame(this);
322 }
323
324 // Note that SASHTEST_NEW_WINDOW and SASHTEST_ABOUT commands get passed
325 // to the parent window for processing, so no need to
326 // duplicate event handlers here.
327
328 BEGIN_EVENT_TABLE(MyChild, wxMDIChildFrame)
329 EVT_MENU(SASHTEST_CHILD_QUIT, MyChild::OnQuit)
330 END_EVENT_TABLE()
331
332 MyChild::MyChild(wxMDIParentFrame *parent, const wxString& title, const wxPoint& pos, const wxSize& size,
333 const long style):
334 wxMDIChildFrame(parent, -1, title, pos, size, style)
335 {
336 canvas = NULL;
337 my_children.Append(this);
338 }
339
340 MyChild::~MyChild(void)
341 {
342 my_children.DeleteObject(this);
343 }
344
345 void MyChild::OnQuit(wxCommandEvent& WXUNUSED(event))
346 {
347 Close(TRUE);
348 }
349
350 void MyChild::OnActivate(wxActivateEvent& event)
351 {
352 if (event.GetActive() && canvas)
353 canvas->SetFocus();
354 }
355