]> git.saurik.com Git - wxWidgets.git/blob - samples/svg/svgtest.cpp
Enable wxComboBox::SetString() test in the widgets sample.
[wxWidgets.git] / samples / svg / svgtest.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: svgtest.cpp
3 // Purpose: SVG sample
4 // Author: Chris Elliott
5 // Modified by:
6 // RCS-ID: $Id$
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // ===========================================================================
11 // declarations
12 // ===========================================================================
13
14 // ---------------------------------------------------------------------------
15 // headers
16 // ---------------------------------------------------------------------------
17
18 // For compilers that support precompilation, includes "wx/wx.h".
19 #include "wx/wxprec.h"
20
21 #ifdef __BORLANDC__
22 #pragma hdrstop
23 #endif
24
25 #ifndef WX_PRECOMP
26 #include "wx/wx.h"
27 #include "wx/mdi.h"
28 #endif
29
30 #include "wx/toolbar.h"
31 #include "wx/dcsvg.h"
32 #include "wx/vector.h"
33
34 #include "bitmaps/new.xpm"
35 #include "bitmaps/save.xpm"
36 #include "bitmaps/help.xpm"
37 #include "SVGlogo24.xpm"
38
39 #ifndef wxHAS_IMAGES_IN_RESOURCES
40 #include "../sample.xpm"
41 #endif
42
43 class MyChild;
44 class MyCanvas;
45
46 // ---------------------------------------------------------------------------
47 // classes
48 // ---------------------------------------------------------------------------
49
50 class MyApp : public wxApp
51 {
52 public:
53 bool OnInit();
54 };
55
56 class MyFrame : public wxMDIParentFrame
57 {
58 public:
59 MyFrame(wxWindow *parent, const wxWindowID id, const wxString& title,
60 const wxPoint& pos, const wxSize& size, const long style);
61
62 void InitToolBar(wxToolBar* toolBar);
63
64 void OnSize(wxSizeEvent& event);
65 void OnAbout(wxCommandEvent& event);
66 void OnNewWindow(wxCommandEvent& event);
67 void OnQuit(wxCommandEvent& event);
68 void FileSavePicture (wxCommandEvent& event);
69
70 unsigned int GetCountOfChildren() const
71 { return m_nWinCreated; }
72
73 private:
74 unsigned int m_nWinCreated;
75
76 DECLARE_EVENT_TABLE()
77 };
78
79 class MyChild: public wxMDIChildFrame
80 {
81 public:
82 MyChild(wxMDIParentFrame *parent, const wxString& title,
83 const wxPoint& pos = wxDefaultPosition,
84 const wxSize& size = wxDefaultSize,
85 const long style = wxDEFAULT_FRAME_STYLE);
86 ~MyChild();
87
88 void OnActivate(wxActivateEvent& event);
89 void OnQuit(wxCommandEvent& event);
90 bool OnSave(wxString filename);
91
92 MyFrame* GetFrame()
93 { return m_frame; }
94
95 private:
96 MyCanvas *m_canvas;
97 MyFrame *m_frame;
98
99 DECLARE_EVENT_TABLE()
100 };
101
102 class MyCanvas : public wxScrolledWindow
103 {
104 public:
105 MyCanvas(MyChild *parent, const wxPoint& pos, const wxSize& size);
106 virtual void OnDraw(wxDC& dc);
107
108 private:
109 int m_index;
110 MyChild* m_child;
111
112 DECLARE_EVENT_TABLE()
113 };
114
115 // ---------------------------------------------------------------------------
116 // constants
117 // ---------------------------------------------------------------------------
118
119 // menu items ids
120 enum
121 {
122 MDI_QUIT = 100,
123 MDI_NEW_WINDOW,
124 MDI_SAVE,
125 MDI_REFRESH,
126 MDI_CHILD_QUIT,
127 MDI_ABOUT
128 };
129
130 // ---------------------------------------------------------------------------
131 // event tables
132 // ---------------------------------------------------------------------------
133
134 BEGIN_EVENT_TABLE(MyFrame, wxMDIParentFrame)
135 EVT_MENU(MDI_ABOUT, MyFrame::OnAbout)
136 EVT_MENU(MDI_NEW_WINDOW, MyFrame::OnNewWindow)
137 EVT_MENU(MDI_QUIT, MyFrame::OnQuit)
138 EVT_MENU (MDI_SAVE, MyFrame::FileSavePicture)
139
140 EVT_SIZE(MyFrame::OnSize)
141 END_EVENT_TABLE()
142
143 // ===========================================================================
144 // implementation
145 // ===========================================================================
146
147 // ---------------------------------------------------------------------------
148 // MyApp
149 // ---------------------------------------------------------------------------
150
151 IMPLEMENT_APP(MyApp)
152
153 bool MyApp::OnInit()
154 {
155 // Create the main frame window
156
157 MyFrame* frame = new MyFrame((wxFrame *)NULL, -1, wxT("SVG Demo"),
158 wxDefaultPosition, wxSize(500, 400),
159 wxDEFAULT_FRAME_STYLE | wxHSCROLL | wxVSCROLL);
160
161 frame->Show(true);
162
163 return true;
164 }
165
166
167 // ---------------------------------------------------------------------------
168 // MyFrame
169 // ---------------------------------------------------------------------------
170
171 // Define my frame constructor
172 MyFrame::MyFrame(wxWindow *parent, const wxWindowID id, const wxString& title,
173 const wxPoint& pos, const wxSize& size, const long style)
174 : wxMDIParentFrame(parent, id, title, pos, size, style)
175 {
176 m_nWinCreated = 0;
177
178 SetIcon(wxICON(sample));
179
180 // Make a menubar
181 wxMenu *file_menu = new wxMenu;
182
183 file_menu->Append(MDI_NEW_WINDOW, wxT("&New test\tCtrl+N"));
184 file_menu->Append(MDI_QUIT, wxT("&Exit\tAlt+X"));
185
186 wxMenu *help_menu = new wxMenu;
187 help_menu->Append(MDI_ABOUT, wxT("&About"));
188
189 wxMenuBar *menu_bar = new wxMenuBar;
190
191 menu_bar->Append(file_menu, wxT("&File"));
192 menu_bar->Append(help_menu, wxT("&Help"));
193
194 // Associate the menu bar with the frame
195 SetMenuBar(menu_bar);
196
197 #if wxUSE_STATUSBAR
198 CreateStatusBar();
199 #endif // wxUSE_STATUSBAR
200
201 CreateToolBar(wxNO_BORDER | wxTB_FLAT | wxTB_HORIZONTAL);
202 InitToolBar(GetToolBar());
203 }
204
205 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
206 {
207 Close();
208 }
209
210 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
211 {
212 (void)wxMessageBox(wxT("wxWidgets SVG sample\n")
213 wxT("Author: Chris Elliott (c) 2002-2009\n")
214 wxT("Usage: click File|New to show tests"),
215 wxT("About SVG Test"));
216 }
217
218 void MyFrame::OnNewWindow(wxCommandEvent& WXUNUSED(event) )
219 {
220 // Make another frame, containing a canvas
221 MyChild *subframe = new MyChild(this, wxT("SVG Frame"));
222
223 wxString title;
224 title.Printf(wxT("SVG Test Window %d"), m_nWinCreated );
225
226 // counts number of children previously, even if now closed
227 m_nWinCreated ++;
228
229 // Give it a title and icon
230 subframe->SetTitle(title);
231 subframe->SetIcon(wxICON(sample));
232
233 // Make a menubar
234 wxMenu *file_menu = new wxMenu;
235
236 file_menu->Append(MDI_NEW_WINDOW, wxT("&Another test\tCtrl+N"));
237 file_menu->Append(MDI_SAVE, wxT("&Save\tCtrl+S"), wxT("Save in SVG format"));
238 file_menu->Append(MDI_CHILD_QUIT, wxT("&Close child\tCtrl+F4"));
239 file_menu->Append(MDI_QUIT, wxT("&Exit\tAlt+X"));
240
241 wxMenu *help_menu = new wxMenu;
242 help_menu->Append(MDI_ABOUT, wxT("&About"));
243
244 wxMenuBar *menu_bar = new wxMenuBar;
245
246 menu_bar->Append(file_menu, wxT("&File"));
247 menu_bar->Append(help_menu, wxT("&Help"));
248
249 // Associate the menu bar with the frame
250 subframe->SetMenuBar(menu_bar);
251
252 subframe->Show(true);
253 }
254
255 void MyFrame::OnSize(wxSizeEvent& event)
256 {
257 int w, h;
258 GetClientSize(&w, &h);
259
260 GetClientWindow()->SetSize(0, 0, w, h);
261 event.Skip();
262 }
263
264 void MyFrame::InitToolBar(wxToolBar* toolBar)
265 {
266 const int maxBitmaps = 3;
267 wxBitmap* bitmaps[maxBitmaps];
268
269 bitmaps[0] = new wxBitmap( new_xpm );
270 bitmaps[1] = new wxBitmap( save_xpm );
271 bitmaps[2] = new wxBitmap( help_xpm );
272
273 toolBar->AddTool(MDI_NEW_WINDOW, wxEmptyString, *(bitmaps[0]), wxS("New SVG test window"));
274 toolBar->AddTool(MDI_SAVE, wxEmptyString, *bitmaps[1], wxS("Save test in SVG format"));
275 toolBar->AddSeparator();
276 toolBar->AddTool(MDI_ABOUT, wxEmptyString, *bitmaps[2], wxS("Help"));
277
278 toolBar->Realize();
279
280 int i;
281 for (i = 0; i < maxBitmaps; i++)
282 delete bitmaps[i];
283 }
284
285 void MyFrame::FileSavePicture (wxCommandEvent & WXUNUSED(event) )
286 {
287 #if wxUSE_FILEDLG
288 MyChild * pChild = (MyChild *)GetActiveChild();
289 if (pChild == NULL)
290 {
291 return;
292 }
293
294 wxFileDialog dialog(this, wxT("Save Picture as"), wxEmptyString, pChild->GetTitle(),
295 wxT("SVG vector picture files (*.svg)|*.svg"),
296 wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
297
298 if (dialog.ShowModal() == wxID_OK)
299 {
300 if (!pChild->OnSave ( dialog.GetPath() ))
301 {
302 return;
303 }
304 }
305 return;
306 #endif // wxUSE_FILEDLG
307 }
308
309
310 // ---------------------------------------------------------------------------
311 // MyCanvas
312 // ---------------------------------------------------------------------------
313
314 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
315 END_EVENT_TABLE()
316
317 // Define a constructor for my canvas
318 MyCanvas::MyCanvas(MyChild *parent, const wxPoint& pos, const wxSize& size)
319 : wxScrolledWindow(parent, wxID_ANY, pos, size, wxSUNKEN_BORDER|wxVSCROLL|wxHSCROLL)
320 {
321 SetBackgroundColour(wxColour(wxT("WHITE")));
322
323 m_child = parent;
324 m_index = m_child->GetFrame()->GetCountOfChildren() % 7;
325 }
326
327 // Define the repainting behaviour
328 void MyCanvas::OnDraw(wxDC& dc)
329 {
330 // vars to use ...
331 #if wxUSE_STATUSBAR
332 wxString s;
333 #endif // wxUSE_STATUSBAR
334 wxPen wP;
335 wxBrush wB;
336 wxPoint points[6];
337 wxColour wC;
338 wxFont wF;
339
340 dc.SetFont(*wxSWISS_FONT);
341 dc.SetPen(*wxGREEN_PEN);
342
343 switch (m_index)
344 {
345 default:
346 case 0:
347 // draw lines to make a cross
348 dc.DrawLine(0, 0, 200, 200);
349 dc.DrawLine(200, 0, 0, 200);
350 // draw point colored line and spline
351 wP = *wxCYAN_PEN;
352 wP.SetWidth(3);
353 dc.SetPen(wP);
354
355 dc.DrawPoint (25,15);
356 dc.DrawLine(50, 30, 200, 30);
357 dc.DrawSpline(50, 200, 50, 100, 200, 10);
358 #if wxUSE_STATUSBAR
359 s = wxT("Green Cross, Cyan Line and spline");
360 #endif // wxUSE_STATUSBAR
361 break;
362
363 case 1:
364 // draw standard shapes
365 dc.SetBrush(*wxCYAN_BRUSH);
366 dc.SetPen(*wxRED_PEN);
367 dc.DrawRectangle(10, 10, 100, 70);
368 wB = wxBrush (wxT("DARK ORCHID"), wxBRUSHSTYLE_TRANSPARENT);
369 dc.SetBrush (wB);
370 dc.DrawRoundedRectangle(50, 50, 100, 70, 20);
371 dc.SetBrush (wxBrush(wxT("GOLDENROD")) );
372 dc.DrawEllipse(100, 100, 100, 50);
373
374 points[0].x = 100; points[0].y = 200;
375 points[1].x = 70; points[1].y = 260;
376 points[2].x = 160; points[2].y = 230;
377 points[3].x = 40; points[3].y = 230;
378 points[4].x = 130; points[4].y = 260;
379 points[5].x = 100; points[5].y = 200;
380
381 dc.DrawPolygon(5, points);
382 dc.DrawLines (6, points, 160);
383 #if wxUSE_STATUSBAR
384 s = wxT("Blue rectangle, red edge, clear rounded rectangle, gold ellipse, gold and clear stars");
385 #endif // wxUSE_STATUSBAR
386 break;
387
388 case 2:
389 // draw text in Arial or similar font
390 dc.DrawLine(50,25,50,35);
391 dc.DrawLine(45,30,55,30);
392 dc.DrawText(wxT("This is a Swiss-style string"), 50, 30);
393 wC = dc.GetTextForeground();
394 dc.SetTextForeground (wxT("FIREBRICK"));
395
396 // no effect in msw ??
397 dc.SetTextBackground (wxT("WHEAT"));
398 dc.DrawText(wxT("This is a Red string"), 50, 200);
399 dc.DrawRotatedText(wxT("This is a 45 deg string"), 50, 200, 45);
400 dc.DrawRotatedText(wxT("This is a 90 deg string"), 50, 200, 90);
401 wF = wxFont ( 18, wxROMAN, wxITALIC, wxBOLD, false, wxT("Times New Roman"));
402 dc.SetFont(wF);
403 dc.SetTextForeground (wC);
404 dc.DrawText(wxT("This is a Times-style string"), 50, 60);
405 #if wxUSE_STATUSBAR
406 s = wxT("Swiss, Times text; red text, rotated and colored orange");
407 #endif // wxUSE_STATUSBAR
408 break;
409
410 case 3 :
411 // four arcs start and end points, center
412 dc.SetBrush(*wxGREEN_BRUSH);
413 dc.DrawArc ( 200,300, 370,230, 300,300 );
414 dc.SetBrush(*wxBLUE_BRUSH);
415 dc.DrawArc ( 270-50, 270-86, 270-86, 270-50, 270,270 );
416 dc.SetDeviceOrigin(-10,-10);
417 dc.DrawArc ( 270-50, 270-86, 270-86, 270-50, 270,270 );
418 dc.SetDeviceOrigin(0,0);
419
420 wP.SetColour (wxT("CADET BLUE"));
421 dc.SetPen(wP);
422 dc.DrawArc ( 75,125, 110, 40, 75, 75 );
423
424 wP.SetColour (wxT("SALMON"));
425 dc.SetPen(wP);
426 dc.SetBrush(*wxRED_BRUSH);
427 //top left corner, width and height, start and end angle
428 // 315 same center and x-radius as last pie-arc, half Y radius
429 dc.DrawEllipticArc(25,50,100,50,180.0,45.0);
430
431 wP = *wxCYAN_PEN;
432 wP.SetWidth(3);
433 dc.SetPen(wP);
434 //wxTRANSPARENT));
435 dc.SetBrush (wxBrush (wxT("SALMON")));
436 dc.DrawEllipticArc(300, 0,200,100, 0.0,145.0);
437 //same end point
438 dc.DrawEllipticArc(300, 50,200,100,90.0,145.0);
439 dc.DrawEllipticArc(300,100,200,100,90.0,345.0);
440
441 #if wxUSE_STATUSBAR
442 s = wxT("This is an arc test page");
443 #endif // wxUSE_STATUSBAR
444 break;
445
446 case 4:
447 dc.DrawCheckMark ( 30,30,25,25);
448 dc.SetBrush (wxBrush (wxT("SALMON"),wxBRUSHSTYLE_TRANSPARENT));
449 dc.DrawCheckMark ( 80,50,75,75);
450 dc.DrawRectangle ( 80,50,75,75);
451 #if wxUSE_STATUSBAR
452 s = wxT("Two check marks");
453 #endif // wxUSE_STATUSBAR
454 break;
455
456 case 5:
457 wF = wxFont ( 18, wxROMAN, wxITALIC, wxBOLD, false, wxT("Times New Roman"));
458 dc.SetFont(wF);
459 dc.DrawLine(0, 0, 200, 200);
460 dc.DrawLine(200, 0, 0, 200);
461 dc.DrawText(wxT("This is an 18pt string"), 50, 60);
462
463 // rescale and draw in blue
464 wP = *wxCYAN_PEN;
465 dc.SetPen(wP);
466 dc.SetUserScale (2.0,0.5);
467 dc.SetDeviceOrigin(200,0);
468 dc.DrawLine(0, 0, 200, 200);
469 dc.DrawLine(200, 0, 0, 200);
470 dc.DrawText(wxT("This is an 18pt string 2 x 0.5 UserScaled"), 50, 60);
471 dc.SetUserScale (2.0,2.0);
472 dc.SetDeviceOrigin(200,200);
473 dc.DrawText(wxT("This is an 18pt string 2 x 2 UserScaled"), 50, 60);
474
475 wP = *wxRED_PEN;
476 dc.SetPen(wP);
477 dc.SetUserScale (1.0,1.0);
478 dc.SetDeviceOrigin(0,10);
479 dc.SetMapMode (wxMM_METRIC); //svg ignores this
480 dc.DrawLine(0, 0, 200, 200);
481 dc.DrawLine(200, 0, 0, 200);
482 dc.DrawText(wxT("This is an 18pt string in MapMode"), 50, 60);
483 #if wxUSE_STATUSBAR
484 s = wxT("Scaling test page");
485 #endif // wxUSE_STATUSBAR
486 break;
487
488 case 6:
489 dc.DrawIcon( wxICON(sample), 10, 10 );
490 dc.DrawBitmap ( wxBitmap(svgbitmap_xpm), 50,15);
491 #if wxUSE_STATUSBAR
492 s = wxT("Icon and Bitmap ");
493 #endif // wxUSE_STATUSBAR
494 break;
495
496 }
497 #if wxUSE_STATUSBAR
498 m_child->SetStatusText(s);
499 #endif // wxUSE_STATUSBAR
500 }
501
502
503 // ---------------------------------------------------------------------------
504 // MyChild
505 // ---------------------------------------------------------------------------
506
507 // Note that MDI_NEW_WINDOW and MDI_ABOUT commands get passed
508 // to the parent window for processing, so no need to
509 // duplicate event handlers here.
510 BEGIN_EVENT_TABLE(MyChild, wxMDIChildFrame)
511 EVT_MENU(MDI_CHILD_QUIT, MyChild::OnQuit)
512 END_EVENT_TABLE()
513
514 MyChild::MyChild(wxMDIParentFrame *parent, const wxString& title,
515 const wxPoint& pos, const wxSize& size,
516 const long style)
517 : wxMDIChildFrame(parent, wxID_ANY, title, pos, size, style)
518 {
519 m_frame = (MyFrame *) parent;
520
521 #if wxUSE_STATUSBAR
522 CreateStatusBar();
523 SetStatusText(title);
524 #endif // wxUSE_STATUSBAR
525
526 m_canvas = new MyCanvas(this, wxPoint(0, 0), GetClientSize());
527
528 // Give it scrollbars
529 m_canvas->SetScrollbars(20, 20, 50, 50);
530 }
531
532 MyChild::~MyChild()
533 {
534 }
535
536 void MyChild::OnQuit(wxCommandEvent& WXUNUSED(event))
537 {
538 Close(true);
539 }
540
541 bool MyChild::OnSave(wxString filename)
542 {
543 wxSVGFileDC svgDC (filename, 600, 650);
544 m_canvas->OnDraw (svgDC);
545 return svgDC.IsOk();
546 }
547
548 void MyChild::OnActivate(wxActivateEvent& event)
549 {
550 if ( event.GetActive() && m_canvas )
551 m_canvas->SetFocus();
552 }
553