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