]>
git.saurik.com Git - wxWidgets.git/blob - samples/svg/svgtest.cpp
1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Chris Elliott
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
9 // ===========================================================================
11 // ===========================================================================
13 // ---------------------------------------------------------------------------
15 // ---------------------------------------------------------------------------
17 // For compilers that support precompilation, includes "wx/wx.h".
18 #include "wx/wxprec.h"
29 #include "wx/toolbar.h"
31 #include "wx/vector.h"
33 #include "bitmaps/new.xpm"
34 #include "bitmaps/save.xpm"
35 #include "bitmaps/help.xpm"
36 #include "SVGlogo24.xpm"
38 #ifndef wxHAS_IMAGES_IN_RESOURCES
39 #include "../sample.xpm"
47 // ---------------------------------------------------------------------------
49 // ---------------------------------------------------------------------------
51 class MyApp
: public wxApp
57 class MyFrame
: public wxMDIParentFrame
60 MyFrame(wxWindow
*parent
, const wxWindowID id
, const wxString
& title
,
61 const wxPoint
& pos
, const wxSize
& size
, const long style
);
63 void InitToolBar(wxToolBar
* toolBar
);
65 void OnSize(wxSizeEvent
& event
);
66 void OnAbout(wxCommandEvent
& event
);
67 void OnNewWindow(wxCommandEvent
& event
);
68 void OnQuit(wxCommandEvent
& event
);
69 void FileSavePicture (wxCommandEvent
& event
);
71 unsigned int GetCountOfChildren() const
72 { return m_nWinCreated
; }
75 unsigned int m_nWinCreated
;
80 class MyChild
: public wxMDIChildFrame
83 MyChild(wxMDIParentFrame
*parent
, const wxString
& title
,
84 const wxPoint
& pos
= wxDefaultPosition
,
85 const wxSize
& size
= wxDefaultSize
,
86 const long style
= wxDEFAULT_FRAME_STYLE
);
89 void OnActivate(wxActivateEvent
& event
);
90 void OnQuit(wxCommandEvent
& event
);
91 bool OnSave(wxString filename
);
100 DECLARE_EVENT_TABLE()
103 class MyCanvas
: public wxScrolledWindow
106 MyCanvas(MyChild
*parent
, const wxPoint
& pos
, const wxSize
& size
);
107 virtual void OnDraw(wxDC
& dc
);
113 DECLARE_EVENT_TABLE()
116 // ---------------------------------------------------------------------------
118 // ---------------------------------------------------------------------------
131 // ---------------------------------------------------------------------------
133 // ---------------------------------------------------------------------------
135 BEGIN_EVENT_TABLE(MyFrame
, wxMDIParentFrame
)
136 EVT_MENU(MDI_ABOUT
, MyFrame::OnAbout
)
137 EVT_MENU(MDI_NEW_WINDOW
, MyFrame::OnNewWindow
)
138 EVT_MENU(MDI_QUIT
, MyFrame::OnQuit
)
139 EVT_MENU (MDI_SAVE
, MyFrame::FileSavePicture
)
141 EVT_SIZE(MyFrame::OnSize
)
144 // ===========================================================================
146 // ===========================================================================
148 // ---------------------------------------------------------------------------
150 // ---------------------------------------------------------------------------
156 // Create the main frame window
158 MyFrame
* frame
= new MyFrame((wxFrame
*)NULL
, -1, wxT("SVG Demo"),
159 wxDefaultPosition
, wxSize(500, 400),
160 wxDEFAULT_FRAME_STYLE
| wxHSCROLL
| wxVSCROLL
);
168 // ---------------------------------------------------------------------------
170 // ---------------------------------------------------------------------------
172 // Define my frame constructor
173 MyFrame::MyFrame(wxWindow
*parent
, const wxWindowID id
, const wxString
& title
,
174 const wxPoint
& pos
, const wxSize
& size
, const long style
)
175 : wxMDIParentFrame(parent
, id
, title
, pos
, size
, style
)
179 SetIcon(wxICON(sample
));
182 wxMenu
*file_menu
= new wxMenu
;
184 file_menu
->Append(MDI_NEW_WINDOW
, wxT("&New test\tCtrl+N"));
185 file_menu
->Append(MDI_QUIT
, wxT("&Exit\tAlt+X"));
187 wxMenu
*help_menu
= new wxMenu
;
188 help_menu
->Append(MDI_ABOUT
, wxT("&About"));
190 wxMenuBar
*menu_bar
= new wxMenuBar
;
192 menu_bar
->Append(file_menu
, wxT("&File"));
193 menu_bar
->Append(help_menu
, wxT("&Help"));
195 // Associate the menu bar with the frame
196 SetMenuBar(menu_bar
);
200 #endif // wxUSE_STATUSBAR
202 CreateToolBar(wxNO_BORDER
| wxTB_FLAT
| wxTB_HORIZONTAL
);
203 InitToolBar(GetToolBar());
206 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
211 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
213 (void)wxMessageBox(wxT("wxWidgets SVG sample\n")
214 wxT("Author: Chris Elliott (c) 2002-2009\n")
215 wxT("Usage: click File|New to show tests"),
216 wxT("About SVG Test"));
219 void MyFrame::OnNewWindow(wxCommandEvent
& WXUNUSED(event
) )
221 // Make another frame, containing a canvas
222 MyChild
*subframe
= new MyChild(this, wxT("SVG Frame"));
225 title
.Printf(wxT("SVG Test Window %d"), m_nWinCreated
);
227 // counts number of children previously, even if now closed
230 // Give it a title and icon
231 subframe
->SetTitle(title
);
232 subframe
->SetIcon(wxICON(sample
));
235 wxMenu
*file_menu
= new wxMenu
;
237 file_menu
->Append(MDI_NEW_WINDOW
, wxT("&Another test\tCtrl+N"));
238 file_menu
->Append(MDI_SAVE
, wxT("&Save\tCtrl+S"), wxT("Save in SVG format"));
239 file_menu
->Append(MDI_CHILD_QUIT
, wxT("&Close child\tCtrl+F4"));
240 file_menu
->Append(MDI_QUIT
, wxT("&Exit\tAlt+X"));
242 wxMenu
*help_menu
= new wxMenu
;
243 help_menu
->Append(MDI_ABOUT
, wxT("&About"));
245 wxMenuBar
*menu_bar
= new wxMenuBar
;
247 menu_bar
->Append(file_menu
, wxT("&File"));
248 menu_bar
->Append(help_menu
, wxT("&Help"));
250 // Associate the menu bar with the frame
251 subframe
->SetMenuBar(menu_bar
);
253 subframe
->Show(true);
256 void MyFrame::OnSize(wxSizeEvent
& event
)
259 GetClientSize(&w
, &h
);
261 GetClientWindow()->SetSize(0, 0, w
, h
);
265 void MyFrame::InitToolBar(wxToolBar
* toolBar
)
267 const int maxBitmaps
= 3;
268 wxBitmap
* bitmaps
[maxBitmaps
];
270 bitmaps
[0] = new wxBitmap( new_xpm
);
271 bitmaps
[1] = new wxBitmap( save_xpm
);
272 bitmaps
[2] = new wxBitmap( help_xpm
);
274 toolBar
->AddTool(MDI_NEW_WINDOW
, wxEmptyString
, *(bitmaps
[0]), wxS("New SVG test window"));
275 toolBar
->AddTool(MDI_SAVE
, wxEmptyString
, *bitmaps
[1], wxS("Save test in SVG format"));
276 toolBar
->AddSeparator();
277 toolBar
->AddTool(MDI_ABOUT
, wxEmptyString
, *bitmaps
[2], wxS("Help"));
282 for (i
= 0; i
< maxBitmaps
; i
++)
286 void MyFrame::FileSavePicture (wxCommandEvent
& WXUNUSED(event
) )
289 MyChild
* pChild
= (MyChild
*)GetActiveChild();
295 wxFileDialog
dialog(this, wxT("Save Picture as"), wxEmptyString
, pChild
->GetTitle(),
296 wxT("SVG vector picture files (*.svg)|*.svg"),
297 wxFD_SAVE
|wxFD_OVERWRITE_PROMPT
);
299 if (dialog
.ShowModal() == wxID_OK
)
301 if (!pChild
->OnSave ( dialog
.GetPath() ))
307 #endif // wxUSE_FILEDLG
311 // ---------------------------------------------------------------------------
313 // ---------------------------------------------------------------------------
315 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
318 // Define a constructor for my canvas
319 MyCanvas::MyCanvas(MyChild
*parent
, const wxPoint
& pos
, const wxSize
& size
)
320 : wxScrolledWindow(parent
, wxID_ANY
, pos
, size
, wxSUNKEN_BORDER
|wxVSCROLL
|wxHSCROLL
)
322 SetBackgroundColour(*wxWHITE
);
325 m_index
= m_child
->GetFrame()->GetCountOfChildren() % 9;
328 // Define the repainting behaviour
329 void MyCanvas::OnDraw(wxDC
& dc
)
334 #endif // wxUSE_STATUSBAR
341 dc
.SetFont(*wxSWISS_FONT
);
342 dc
.SetPen(*wxGREEN_PEN
);
348 // draw lines to make a cross
349 dc
.DrawLine(0, 0, 200, 200);
350 dc
.DrawLine(200, 0, 0, 200);
351 // draw point colored line and spline
356 dc
.DrawPoint (25,15);
357 dc
.DrawLine(50, 30, 200, 30);
358 dc
.DrawSpline(50, 200, 50, 100, 200, 10);
360 s
= wxT("Green Cross, Cyan Line and spline");
361 #endif // wxUSE_STATUSBAR
365 // draw standard shapes
366 dc
.SetBrush(*wxCYAN_BRUSH
);
367 dc
.SetPen(*wxRED_PEN
);
368 dc
.DrawRectangle(10, 10, 100, 70);
369 wB
= wxBrush (wxT("DARK ORCHID"), wxBRUSHSTYLE_TRANSPARENT
);
371 dc
.DrawRoundedRectangle(50, 50, 100, 70, 20);
372 dc
.SetBrush (wxBrush(wxT("GOLDENROD")) );
373 dc
.DrawEllipse(100, 100, 100, 50);
375 points
[0].x
= 100; points
[0].y
= 200;
376 points
[1].x
= 70; points
[1].y
= 260;
377 points
[2].x
= 160; points
[2].y
= 230;
378 points
[3].x
= 40; points
[3].y
= 230;
379 points
[4].x
= 130; points
[4].y
= 260;
380 points
[5].x
= 100; points
[5].y
= 200;
382 dc
.DrawPolygon(5, points
);
383 dc
.DrawLines (6, points
, 160);
385 s
= wxT("Blue rectangle, red edge, clear rounded rectangle, gold ellipse, gold and clear stars");
386 #endif // wxUSE_STATUSBAR
390 // draw text in Arial or similar font
391 dc
.DrawLine(50,25,50,35);
392 dc
.DrawLine(45,30,55,30);
393 dc
.DrawText(wxT("This is a Swiss-style string"), 50, 30);
394 wC
= dc
.GetTextForeground();
395 dc
.SetTextForeground (wxT("FIREBRICK"));
397 // no effect in msw ??
398 dc
.SetTextBackground (wxT("WHEAT"));
399 dc
.DrawText(wxT("This is a Red string"), 50, 200);
400 dc
.DrawRotatedText(wxT("This is a 45 deg string"), 50, 200, 45);
401 dc
.DrawRotatedText(wxT("This is a 90 deg string"), 50, 200, 90);
402 wF
= wxFont ( 18, wxROMAN
, wxITALIC
, wxBOLD
, false, wxT("Times New Roman"));
404 dc
.SetTextForeground (wC
);
405 dc
.DrawText(wxT("This is a Times-style string"), 50, 60);
407 s
= wxT("Swiss, Times text; red text, rotated and colored orange");
408 #endif // wxUSE_STATUSBAR
412 // four arcs start and end points, center
413 dc
.SetBrush(*wxGREEN_BRUSH
);
414 dc
.DrawArc ( 200,300, 370,230, 300,300 );
415 dc
.SetBrush(*wxBLUE_BRUSH
);
416 dc
.DrawArc ( 270-50, 270-86, 270-86, 270-50, 270,270 );
417 dc
.SetDeviceOrigin(-10,-10);
418 dc
.DrawArc ( 270-50, 270-86, 270-86, 270-50, 270,270 );
419 dc
.SetDeviceOrigin(0,0);
421 wP
.SetColour (wxT("CADET BLUE"));
423 dc
.DrawArc ( 75,125, 110, 40, 75, 75 );
425 wP
.SetColour (wxT("SALMON"));
427 dc
.SetBrush(*wxRED_BRUSH
);
428 //top left corner, width and height, start and end angle
429 // 315 same center and x-radius as last pie-arc, half Y radius
430 dc
.DrawEllipticArc(25,50,100,50,180.0,45.0);
436 dc
.SetBrush (wxBrush (wxT("SALMON")));
437 dc
.DrawEllipticArc(300, 0,200,100, 0.0,145.0);
439 dc
.DrawEllipticArc(300, 50,200,100,90.0,145.0);
440 dc
.DrawEllipticArc(300,100,200,100,90.0,345.0);
443 s
= wxT("This is an arc test page");
444 #endif // wxUSE_STATUSBAR
448 dc
.DrawCheckMark ( 30,30,25,25);
449 dc
.SetBrush (wxBrush (wxT("SALMON"),wxBRUSHSTYLE_TRANSPARENT
));
450 dc
.DrawCheckMark ( 80,50,75,75);
451 dc
.DrawRectangle ( 80,50,75,75);
453 s
= wxT("Two check marks");
454 #endif // wxUSE_STATUSBAR
458 wF
= wxFont ( 18, wxROMAN
, wxITALIC
, wxBOLD
, false, wxT("Times New Roman"));
460 dc
.DrawLine(0, 0, 200, 200);
461 dc
.DrawLine(200, 0, 0, 200);
462 dc
.DrawText(wxT("This is an 18pt string"), 50, 60);
464 // rescale and draw in blue
467 dc
.SetUserScale (2.0,0.5);
468 dc
.SetDeviceOrigin(200,0);
469 dc
.DrawLine(0, 0, 200, 200);
470 dc
.DrawLine(200, 0, 0, 200);
471 dc
.DrawText(wxT("This is an 18pt string 2 x 0.5 UserScaled"), 50, 60);
472 dc
.SetUserScale (2.0,2.0);
473 dc
.SetDeviceOrigin(200,200);
474 dc
.DrawText(wxT("This is an 18pt string 2 x 2 UserScaled"), 50, 60);
478 dc
.SetUserScale (1.0,1.0);
479 dc
.SetDeviceOrigin(0,10);
480 dc
.SetMapMode (wxMM_METRIC
); //svg ignores this
481 dc
.DrawLine(0, 0, 200, 200);
482 dc
.DrawLine(200, 0, 0, 200);
483 dc
.DrawText(wxT("This is an 18pt string in MapMode"), 50, 60);
485 s
= wxT("Scaling test page");
486 #endif // wxUSE_STATUSBAR
490 dc
.DrawIcon( wxICON(sample
), 10, 10 );
491 dc
.DrawBitmap ( wxBitmap(svgbitmap_xpm
), 50,15);
493 s
= wxT("Icon and Bitmap ");
494 #endif // wxUSE_STATUSBAR
498 dc
.SetTextForeground(wxT("RED"));
499 dc
.DrawText(wxT("Red = Clipping Off"), 30, 5);
500 dc
.SetTextForeground(wxT("GREEN"));
501 dc
.DrawText(wxT("Green = Clipping On"), 30, 25);
503 dc
.SetTextForeground(wxT("BLACK"));
505 dc
.SetPen(*wxRED_PEN
);
506 dc
.SetBrush (wxBrush (wxT("SALMON"),wxBRUSHSTYLE_TRANSPARENT
));
507 dc
.DrawCheckMark ( 80,50,75,75);
508 dc
.DrawRectangle ( 80,50,75,75);
510 dc
.SetPen(*wxGREEN_PEN
);
512 // Clipped checkmarks
513 dc
.DrawRectangle(180,50,75,75);
514 dc
.SetClippingRegion(180,50,75,75); // x,y,width,height version
515 dc
.DrawCheckMark ( 180,50,75,75);
516 dc
.DestroyClippingRegion();
518 dc
.DrawRectangle(wxRect(80,150,75,75));
519 dc
.SetClippingRegion(wxPoint(80,150),wxSize(75,75)); // pt,size version
520 dc
.DrawCheckMark ( 80,150,75,75);
521 dc
.DestroyClippingRegion();
523 dc
.DrawRectangle(wxRect(180,150,75,75));
524 dc
.SetClippingRegion(wxRect(180,150,75,75)); // rect version
525 dc
.DrawCheckMark ( 180,150,75,75);
526 dc
.DestroyClippingRegion();
528 dc
.DrawRectangle(wxRect( 80,250,50,65));
529 dc
.DrawRectangle(wxRect(105,260,50,65));
530 dc
.SetClippingRegion(wxRect( 80,250,50,65)); // second call to SetClippingRegion
531 dc
.SetClippingRegion(wxRect(105,260,50,65)); // forms intersection with previous
532 dc
.DrawCheckMark(80,250,75,75);
533 dc
.DestroyClippingRegion(); // only one call to destroy (there's no stack)
536 ** Clipping by wxRegion not implemented for SVG. Should be
537 ** possible, but need to access points that define the wxRegion
538 ** from inside DoSetDeviceClippingRegion() and wxRegion does not
539 ** implement anything like getPoints().
540 points[0].x = 180; points[0].y = 250;
541 points[1].x = 255; points[1].y = 250;
542 points[2].x = 180; points[2].y = 325;
543 points[3].x = 255; points[3].y = 325;
544 points[4].x = 180; points[4].y = 250;
546 dc.DrawLines (5, points);
547 wxRegion reg = wxRegion(5,points);
549 dc.SetClippingRegion(reg);
550 dc.DrawCheckMark ( 180,250,75,75);
551 dc.DestroyClippingRegion();
555 s
= wxT("Clipping region");
556 #endif // wxUSE_STATUSBAR
561 wxCoord txtX
, txtY
, txtW
, txtH
, txtDescent
, txtEL
;
566 //dc.SetBackgroundMode(wxBRUSHSTYLE_SOLID);
567 //dc.SetTextBackground(*wxBLUE);
570 txtStr
= wxT("Horizontal string");
571 dc
.GetTextExtent(txtStr
, &txtW
, &txtH
, &txtDescent
, &txtEL
);
574 dc
.DrawRectangle(txtX
, txtY
, txtW
+ 2*txtPad
, txtH
+ 2*txtPad
);
575 dc
.DrawText(txtStr
, txtX
+ txtPad
, txtY
+ txtPad
);
578 txtStr
= wxT("Vertical string");
579 dc
.GetTextExtent(txtStr
, &txtW
, &txtH
, &txtDescent
, &txtEL
);
582 dc
.DrawRectangle(txtX
, txtY
- (txtW
+ 2*txtPad
), txtH
+ 2*txtPad
, txtW
+ 2*txtPad
);
583 dc
.DrawRotatedText(txtStr
, txtX
+ txtPad
, txtY
- txtPad
, 90);
586 txtStr
= wxT("45 deg string");
587 dc
.GetTextExtent(txtStr
, &txtW
, &txtH
, &txtDescent
, &txtEL
);
588 double lenW
= (double)(txtW
+ 2*txtPad
) / sqrt(2.0);
589 double lenH
= (double)(txtH
+ 2*txtPad
) / sqrt(2.0);
590 double padding
= (double)txtPad
/ sqrt(2.0);
593 dc
.DrawLine(txtX
- padding
, txtY
, txtX
+ lenW
, txtY
- lenW
); // top
594 dc
.DrawLine(txtX
+ lenW
, txtY
- lenW
, txtX
- padding
+ lenH
+ lenW
, txtY
+ (lenH
- lenW
));
595 dc
.DrawLine(txtX
- padding
, txtY
, txtX
- padding
+ lenH
, txtY
+ lenH
);
596 dc
.DrawLine(txtX
- padding
+ lenH
, txtY
+ lenH
, txtX
- padding
+ lenH
+ lenW
, txtY
+ (lenH
- lenW
)); // bottom
597 dc
.DrawRotatedText(txtStr
, txtX
, txtY
, 45);
599 s
= wxT("Text position test page");
600 #endif // wxUSE_STATUSBAR
604 m_child
->SetStatusText(s
);
605 #endif // wxUSE_STATUSBAR
609 // ---------------------------------------------------------------------------
611 // ---------------------------------------------------------------------------
613 // Note that MDI_NEW_WINDOW and MDI_ABOUT commands get passed
614 // to the parent window for processing, so no need to
615 // duplicate event handlers here.
616 BEGIN_EVENT_TABLE(MyChild
, wxMDIChildFrame
)
617 EVT_MENU(MDI_CHILD_QUIT
, MyChild::OnQuit
)
620 MyChild::MyChild(wxMDIParentFrame
*parent
, const wxString
& title
,
621 const wxPoint
& pos
, const wxSize
& size
,
623 : wxMDIChildFrame(parent
, wxID_ANY
, title
, pos
, size
, style
)
625 m_frame
= (MyFrame
*) parent
;
629 SetStatusText(title
);
630 #endif // wxUSE_STATUSBAR
632 m_canvas
= new MyCanvas(this, wxPoint(0, 0), GetClientSize());
634 // Give it scrollbars
635 m_canvas
->SetScrollbars(20, 20, 50, 50);
642 void MyChild::OnQuit(wxCommandEvent
& WXUNUSED(event
))
647 bool MyChild::OnSave(wxString filename
)
649 wxSVGFileDC
svgDC (filename
, 600, 650);
650 m_canvas
->OnDraw (svgDC
);
654 void MyChild::OnActivate(wxActivateEvent
& event
)
656 if ( event
.GetActive() && m_canvas
)
657 m_canvas
->SetFocus();