]> git.saurik.com Git - wxWidgets.git/blame - samples/svg/svgtest.cpp
removed contrib files from the scripts
[wxWidgets.git] / samples / svg / svgtest.cpp
CommitLineData
cdf3a589
CE
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
ae0fbcee 30#include "wx/toolbar.h"
18cea871 31#include "wx/dcsvg.h"
cdf3a589 32
a5e6cfa8 33#include "mondrian.xpm"
756d3e7f 34
cdf3a589
CE
35#include "bitmaps/new.xpm"
36#include "bitmaps/save.xpm"
37#include "bitmaps/help.xpm"
38#include "SVGlogo24.xpm"
a5e6cfa8 39
cdf3a589
CE
40class MyChild;
41
42// Define a new application
43class MyApp : public wxApp
44{
45 public:
46 bool OnInit();
47};
48
49// Define a new frame
50class 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
73class MyCanvas : public wxScrolledWindow
74{
75 public:
76 int m_index ;
a5e6cfa8 77
cdf3a589
CE
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
85class 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
105enum
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
116IMPLEMENT_APP(MyApp)
117
118// ---------------------------------------------------------------------------
119// global variables
120// ---------------------------------------------------------------------------
121
122MyFrame *frame = (MyFrame *) NULL;
123
124// ---------------------------------------------------------------------------
125// event tables
126// ---------------------------------------------------------------------------
127
128BEGIN_EVENT_TABLE(MyFrame, wxMDIParentFrame)
129EVT_MENU(MDI_ABOUT, MyFrame::OnAbout)
130EVT_MENU(MDI_NEW_WINDOW, MyFrame::OnNewWindow)
131EVT_MENU(MDI_QUIT, MyFrame::OnQuit)
132EVT_MENU (MDI_SAVE, MyFrame::FileSavePicture)
133EVT_CLOSE(MyFrame::OnClose)
134
135EVT_SIZE(MyFrame::OnSize)
136END_EVENT_TABLE()
137
138// ===========================================================================
139// implementation
140// ===========================================================================
141
142// ---------------------------------------------------------------------------
143// MyApp
144// ---------------------------------------------------------------------------
145
146// Initialise this in OnInit, not statically
147bool 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);
a5e6cfa8 154
cdf3a589
CE
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
d96cdd4a 173#if wxUSE_STATUSBAR
cdf3a589 174 frame->CreateStatusBar();
d96cdd4a 175#endif // wxUSE_STATUSBAR
cdf3a589 176
d96cdd4a 177 frame->Show(true);
cdf3a589
CE
178
179 SetTopWindow(frame);
180
c54e5eb0 181 return true;
cdf3a589
CE
182}
183
184
185// ---------------------------------------------------------------------------
186// MyFrame
187// ---------------------------------------------------------------------------
188
189// Define my frame constructor
190MyFrame::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
205void MyFrame::OnClose(wxCloseEvent& event)
206{
207 if ( !event.CanVeto() )
208 {
209 event.Skip();
210 return ;
211 }
216db41f 212 if ( m_children.GetCount () < 1 )
cdf3a589
CE
213 {
214 event.Skip();
215 return ;
216 }
217 // now try the children
36ca94a2
WS
218 wxObjectList::compatibility_iterator pNode = m_children.GetFirst ();
219 wxObjectList::compatibility_iterator pNext ;
cdf3a589
CE
220 MyChild * pChild ;
221 while ( pNode )
222 {
223 pNext = pNode -> GetNext ();
216db41f 224 pChild = (MyChild*) pNode -> GetData ();
cdf3a589
CE
225 if (pChild -> Close ())
226 {
36ca94a2 227 m_children.Erase(pNode) ;
cdf3a589
CE
228 }
229 else
230 {
231 event.Veto();
232 return;
233 }
234 pNode = pNext ;
235 }
236 event.Skip();
237}
238
239
240void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
241{
242 Close();
243}
244
245
246void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
247{
be5a51fb 248 (void)wxMessageBox(wxT("wxWidgets 2.0 SVG 1.0 Test\n")
756d3e7f
CE
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"));
cdf3a589
CE
251}
252
253
254void 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
216db41f 263 subframe = (MyChild *) m_children.GetLast() -> GetData ();
cdf3a589
CE
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
c54e5eb0 292 subframe->Show(true);
cdf3a589
CE
293}
294
295
756d3e7f 296void MyFrame::OnSize(wxSizeEvent& event)
cdf3a589
CE
297{
298 int w, h;
299 GetClientSize(&w, &h);
300
301 GetClientWindow()->SetSize(0, 0, w, h);
756d3e7f 302 event.Skip();
cdf3a589
CE
303}
304
305
306void 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 int width = 16;
316 int currentX = 5;
317
c54e5eb0 318 toolBar->AddTool( MDI_NEW_WINDOW, *(bitmaps[0]), wxNullBitmap, false, currentX, wxDefaultCoord, (wxObject *) NULL, wxT("New SVG test window"));
cdf3a589 319 currentX += width + 5;
c54e5eb0 320 toolBar->AddTool( MDI_SAVE, *bitmaps[1], wxNullBitmap, false, currentX, wxDefaultCoord, (wxObject *) NULL, wxT("Save test in SVG format"));
cdf3a589 321 currentX += width + 5;
a5e6cfa8 322 toolBar->AddSeparator();
c54e5eb0 323 toolBar->AddTool(MDI_ABOUT, *bitmaps[2], wxNullBitmap, false, currentX, wxDefaultCoord, (wxObject *) NULL, wxT("Help"));
cdf3a589
CE
324
325 toolBar->Realize();
326
327 int i;
328 for (i = 0; i < maxBitmaps; i++)
329 delete bitmaps[i];
330}
331
332
333void MyFrame::FileSavePicture (wxCommandEvent & WXUNUSED(event) )
334{
c54e5eb0 335#if wxUSE_FILEDLG
cdf3a589 336 MyChild * pChild = (MyChild *)GetActiveChild ();
a5e6cfa8 337 if (pChild == NULL)
cdf3a589
CE
338 {
339 return ;
340 }
341
342 wxFileDialog dialog(this, wxT("Save Picture as"), wxEmptyString, pChild->GetTitle(),
343 wxT("SVG vector picture files (*.svg)|*.svg"),
ff3e84ff 344 wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
cdf3a589
CE
345
346 if (dialog.ShowModal() == wxID_OK)
347 {
348 if (!pChild -> OnSave ( dialog.GetPath() ))
349 {
350 return ;
351 }
352 }
353 return ;
c54e5eb0 354#endif // wxUSE_FILEDLG
cdf3a589
CE
355}
356
357
358// Note that MDI_NEW_WINDOW and MDI_ABOUT commands get passed
359// to the parent window for processing, so no need to
360// duplicate event handlers here.
361BEGIN_EVENT_TABLE(MyChild, wxMDIChildFrame)
362 EVT_MENU(MDI_CHILD_QUIT, MyChild::OnQuit)
363 EVT_CLOSE(MyChild::OnClose)
364END_EVENT_TABLE()
365
366BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
367
368END_EVENT_TABLE()
369
370// ---------------------------------------------------------------------------
371// MyCanvas
372// ---------------------------------------------------------------------------
373
374// Define a constructor for my canvas
375MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size)
376: wxScrolledWindow(parent, -1, pos, size,
377wxSUNKEN_BORDER|wxVSCROLL|wxHSCROLL)
378{
379 m_child = (MyChild *) parent ;
87728739 380 SetBackgroundColour(wxColour(_T("WHITE")));
cdf3a589
CE
381 m_index = m_child->m_frame->nWinCreated % 7 ;
382}
383
384
385// Define the repainting behaviour
386void MyCanvas::OnDraw(wxDC& dc)
387{
388 // vars to use ...
d96cdd4a 389#if wxUSE_STATUSBAR
cdf3a589 390 wxString s ;
d96cdd4a 391#endif // wxUSE_STATUSBAR
cdf3a589
CE
392 wxPen wP ;
393 wxBrush wB ;
394 wxPoint points[6];
395 wxColour wC;
396 wxFont wF ;
397
398 dc.SetFont(*wxSWISS_FONT);
399 dc.SetPen(*wxGREEN_PEN);
400
a5e6cfa8 401
cdf3a589
CE
402 switch (m_index)
403 {
404 default:
405 case 0:
406 // draw lines to make a cross
407 dc.DrawLine(0, 0, 200, 200);
408 dc.DrawLine(200, 0, 0, 200);
409 // draw point colored line and spline
410 wP = *wxCYAN_PEN ;
411 wP.SetWidth(3);
412 dc.SetPen(wP);
413
414 dc.DrawPoint (25,15) ;
415 dc.DrawLine(50, 30, 200, 30);
416 dc.DrawSpline(50, 200, 50, 100, 200, 10);
d96cdd4a 417#if wxUSE_STATUSBAR
cdf3a589 418 s = wxT("Green Cross, Cyan Line and spline");
d96cdd4a 419#endif // wxUSE_STATUSBAR
cdf3a589
CE
420 break ;
421
422 case 1:
423 // draw standard shapes
424 dc.SetBrush(*wxCYAN_BRUSH);
425 dc.SetPen(*wxRED_PEN);
426 dc.DrawRectangle(10, 10, 100, 70);
87728739 427 wB = wxBrush (_T("DARK ORCHID"), wxTRANSPARENT);
cdf3a589
CE
428 dc.SetBrush (wB);
429 dc.DrawRoundedRectangle(50, 50, 100, 70, 20);
87728739 430 dc.SetBrush (wxBrush(_T("GOLDENROD"), wxSOLID) );
cdf3a589
CE
431 dc.DrawEllipse(100, 100, 100, 50);
432
433 points[0].x = 100; points[0].y = 200;
434 points[1].x = 70; points[1].y = 260;
435 points[2].x = 160; points[2].y = 230;
436 points[3].x = 40; points[3].y = 230;
437 points[4].x = 130; points[4].y = 260;
438 points[5].x = 100; points[5].y = 200;
439
440 dc.DrawPolygon(5, points);
441 dc.DrawLines (6, points, 160);
d96cdd4a 442#if wxUSE_STATUSBAR
cdf3a589 443 s = wxT("Blue rectangle, red edge, clear rounded rectangle, gold ellipse, gold and clear stars");
d96cdd4a 444#endif // wxUSE_STATUSBAR
cdf3a589
CE
445 break ;
446
447 case 2:
448 // draw text in Arial or similar font
449 dc.DrawLine(50,25,50,35);
450 dc.DrawLine(45,30,55,30);
451 dc.DrawText(wxT("This is a Swiss-style string"), 50, 30);
452 wC = dc.GetTextForeground() ;
87728739 453 dc.SetTextForeground (_T("FIREBRICK"));
a5e6cfa8 454
cdf3a589 455 // no effect in msw ??
87728739 456 dc.SetTextBackground (_T("WHEAT"));
cdf3a589
CE
457 dc.DrawText(wxT("This is a Red string"), 50, 200);
458 dc.DrawRotatedText(wxT("This is a 45 deg string"), 50, 200, 45);
459 dc.DrawRotatedText(wxT("This is a 90 deg string"), 50, 200, 90);
c54e5eb0 460 wF = wxFont ( 18, wxROMAN, wxITALIC, wxBOLD, false, wxT("Times New Roman"));
cdf3a589
CE
461 dc.SetFont(wF);
462 dc.SetTextForeground (wC) ;
463 dc.DrawText(wxT("This is a Times-style string"), 50, 60);
d96cdd4a 464#if wxUSE_STATUSBAR
cdf3a589 465 s = wxT("Swiss, Times text; red text, rotated and colored orange");
d96cdd4a 466#endif // wxUSE_STATUSBAR
cdf3a589
CE
467 break ;
468
469 case 3 :
470 // four arcs start and end points, center
471 dc.SetBrush(*wxGREEN_BRUSH);
216db41f 472 dc.DrawArc ( 200,300, 370,230, 300,300 );
cdf3a589 473 dc.SetBrush(*wxBLUE_BRUSH);
216db41f 474 dc.DrawArc ( 270-50, 270-86, 270-86, 270-50, 270,270 );
cdf3a589 475 dc.SetDeviceOrigin(-10,-10);
216db41f 476 dc.DrawArc ( 270-50, 270-86, 270-86, 270-50, 270,270 );
cdf3a589
CE
477 dc.SetDeviceOrigin(0,0);
478
87728739 479 wP.SetColour (_T("CADET BLUE"));
cdf3a589 480 dc.SetPen(wP);
216db41f 481 dc.DrawArc ( 75,125, 110, 40, 75, 75 );
cdf3a589 482
87728739 483 wP.SetColour (_T("SALMON"));
cdf3a589
CE
484 dc.SetPen(wP);
485 dc.SetBrush(*wxRED_BRUSH);
486 //top left corner, width and height, start and end angle
487 // 315 same center and x-radius as last pie-arc, half Y radius
488 dc.DrawEllipticArc(25,50,100,50,180.0,45.0) ;
489
490 wP = *wxCYAN_PEN ;
491 wP.SetWidth(3);
492 dc.SetPen(wP);
493 //wxTRANSPARENT));
87728739 494 dc.SetBrush (wxBrush (_T("SALMON"),wxSOLID)) ;
cdf3a589
CE
495 dc.DrawEllipticArc(300, 0,200,100, 0.0,145.0) ;
496 //same end point
497 dc.DrawEllipticArc(300, 50,200,100,90.0,145.0) ;
498 dc.DrawEllipticArc(300,100,200,100,90.0,345.0) ;
499
d96cdd4a 500#if wxUSE_STATUSBAR
cdf3a589 501 s = wxT("This is an arc test page");
d96cdd4a 502#endif // wxUSE_STATUSBAR
cdf3a589
CE
503 break ;
504
505 case 4:
506 dc.DrawCheckMark ( 30,30,25,25);
87728739 507 dc.SetBrush (wxBrush (_T("SALMON"),wxTRANSPARENT));
cdf3a589
CE
508 dc.DrawCheckMark ( 80,50,75,75);
509 dc.DrawRectangle ( 80,50,75,75);
d96cdd4a 510#if wxUSE_STATUSBAR
cdf3a589 511 s = wxT("Two check marks");
d96cdd4a 512#endif // wxUSE_STATUSBAR
cdf3a589
CE
513 break ;
514
515 case 5:
c54e5eb0 516 wF = wxFont ( 18, wxROMAN, wxITALIC, wxBOLD, false, wxT("Times New Roman"));
cdf3a589
CE
517 dc.SetFont(wF);
518 dc.DrawLine(0, 0, 200, 200);
519 dc.DrawLine(200, 0, 0, 200);
520 dc.DrawText(wxT("This is an 18pt string"), 50, 60);
521
522 // rescale and draw in blue
523 wP = *wxCYAN_PEN ;
524 dc.SetPen(wP);
525 dc.SetUserScale (2.0,0.5);
526 dc.SetDeviceOrigin(200,0);
527 dc.DrawLine(0, 0, 200, 200);
528 dc.DrawLine(200, 0, 0, 200);
529 dc.DrawText(wxT("This is an 18pt string 2 x 0.5 UserScaled"), 50, 60);
530 dc.SetUserScale (2.0,2.0);
531 dc.SetDeviceOrigin(200,200);
532 dc.DrawText(wxT("This is an 18pt string 2 x 2 UserScaled"), 50, 60);
533
534 wP = *wxRED_PEN ;
535 dc.SetPen(wP);
536 dc.SetUserScale (1.0,1.0);
537 dc.SetDeviceOrigin(0,10);
538 dc.SetMapMode (wxMM_METRIC) ; //svg ignores this
539 dc.DrawLine(0, 0, 200, 200);
540 dc.DrawLine(200, 0, 0, 200);
a5e6cfa8 541 dc.DrawText(wxT("This is an 18pt string in MapMode"), 50, 60);
d96cdd4a 542#if wxUSE_STATUSBAR
cdf3a589 543 s = wxT("Scaling test page");
d96cdd4a 544#endif // wxUSE_STATUSBAR
cdf3a589
CE
545 break ;
546
547 case 6:
756d3e7f
CE
548 dc.DrawIcon( wxIcon(mondrian_xpm), 10, 10 );
549 dc.DrawBitmap ( wxBitmap(svgbitmap_xpm), 50,15);
d96cdd4a 550#if wxUSE_STATUSBAR
cdf3a589 551 s = wxT("Icon and Bitmap ");
d96cdd4a 552#endif // wxUSE_STATUSBAR
cdf3a589
CE
553 break ;
554
555 }
d96cdd4a 556#if wxUSE_STATUSBAR
cdf3a589 557 m_child->SetStatusText(s);
d96cdd4a 558#endif // wxUSE_STATUSBAR
cdf3a589
CE
559}
560
561
562
563
564// ---------------------------------------------------------------------------
565// MyChild
566// ---------------------------------------------------------------------------
567
568MyChild::MyChild(wxMDIParentFrame *parent, const wxString& title,
569const wxPoint& pos, const wxSize& size,
570const long style)
571: wxMDIChildFrame(parent, -1, title, pos, size, style)
572{
573
574 m_frame = (MyFrame *) parent ;
d96cdd4a 575#if wxUSE_STATUSBAR
cdf3a589 576 CreateStatusBar();
a5e6cfa8 577 SetStatusText(title);
d96cdd4a 578#endif // wxUSE_STATUSBAR
cdf3a589
CE
579
580 int w, h ;
581 GetClientSize ( &w, &h );
582 m_canvas = new MyCanvas(this, wxPoint(0, 0), wxSize (w,h) );
583 // Give it scrollbars
584 m_canvas->SetScrollbars(20, 20, 50, 50);
585
586}
587
588
589MyChild::~MyChild()
590{
591 m_frame->m_children.DeleteObject(this);
592}
593
594
595void MyChild::OnQuit(wxCommandEvent& WXUNUSED(event))
596{
c54e5eb0 597 Close(true);
cdf3a589
CE
598}
599
600
601bool MyChild::OnSave(wxString filename)
602{
603 wxSVGFileDC svgDC (filename, 600, 650) ;
604 m_canvas->OnDraw (svgDC);
605 return svgDC.Ok();
606}
607
608
609
610void MyChild::OnActivate(wxActivateEvent& event)
611{
612 if ( event.GetActive() && m_canvas )
613 m_canvas->SetFocus();
614}
615
616
617void MyChild::OnClose(wxCloseEvent& event)
618{
619 event.Skip();
620}