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