]>
git.saurik.com Git - wxWidgets.git/blob - samples/svg/svgtest.cpp
1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Chris Elliott
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // ===========================================================================
12 // ===========================================================================
14 // ---------------------------------------------------------------------------
16 // ---------------------------------------------------------------------------
18 // For compilers that support precompilation, includes "wx/wx.h".
19 #include "wx/wxprec.h"
30 #include "wx/toolbar.h"
32 #include "wx/vector.h"
34 #include "bitmaps/new.xpm"
35 #include "bitmaps/save.xpm"
36 #include "bitmaps/help.xpm"
37 #include "SVGlogo24.xpm"
39 #ifndef wxHAS_IMAGES_IN_RESOURCES
40 #include "../sample.xpm"
48 // ---------------------------------------------------------------------------
50 // ---------------------------------------------------------------------------
52 class MyApp
: public wxApp
58 class MyFrame
: public wxMDIParentFrame
61 MyFrame(wxWindow
*parent
, const wxWindowID id
, const wxString
& title
,
62 const wxPoint
& pos
, const wxSize
& size
, const long style
);
64 void InitToolBar(wxToolBar
* toolBar
);
66 void OnSize(wxSizeEvent
& event
);
67 void OnAbout(wxCommandEvent
& event
);
68 void OnNewWindow(wxCommandEvent
& event
);
69 void OnQuit(wxCommandEvent
& event
);
70 void FileSavePicture (wxCommandEvent
& event
);
72 unsigned int GetCountOfChildren() const
73 { return m_nWinCreated
; }
76 unsigned int m_nWinCreated
;
81 class MyChild
: public wxMDIChildFrame
84 MyChild(wxMDIParentFrame
*parent
, const wxString
& title
,
85 const wxPoint
& pos
= wxDefaultPosition
,
86 const wxSize
& size
= wxDefaultSize
,
87 const long style
= wxDEFAULT_FRAME_STYLE
);
90 void OnActivate(wxActivateEvent
& event
);
91 void OnQuit(wxCommandEvent
& event
);
92 bool OnSave(wxString filename
);
101 DECLARE_EVENT_TABLE()
104 class MyCanvas
: public wxScrolledWindow
107 MyCanvas(MyChild
*parent
, const wxPoint
& pos
, const wxSize
& size
);
108 virtual void OnDraw(wxDC
& dc
);
114 DECLARE_EVENT_TABLE()
117 // ---------------------------------------------------------------------------
119 // ---------------------------------------------------------------------------
132 // ---------------------------------------------------------------------------
134 // ---------------------------------------------------------------------------
136 BEGIN_EVENT_TABLE(MyFrame
, wxMDIParentFrame
)
137 EVT_MENU(MDI_ABOUT
, MyFrame::OnAbout
)
138 EVT_MENU(MDI_NEW_WINDOW
, MyFrame::OnNewWindow
)
139 EVT_MENU(MDI_QUIT
, MyFrame::OnQuit
)
140 EVT_MENU (MDI_SAVE
, MyFrame::FileSavePicture
)
142 EVT_SIZE(MyFrame::OnSize
)
145 // ===========================================================================
147 // ===========================================================================
149 // ---------------------------------------------------------------------------
151 // ---------------------------------------------------------------------------
157 // Create the main frame window
159 MyFrame
* frame
= new MyFrame((wxFrame
*)NULL
, -1, wxT("SVG Demo"),
160 wxDefaultPosition
, wxSize(500, 400),
161 wxDEFAULT_FRAME_STYLE
| wxHSCROLL
| wxVSCROLL
);
169 // ---------------------------------------------------------------------------
171 // ---------------------------------------------------------------------------
173 // Define my frame constructor
174 MyFrame::MyFrame(wxWindow
*parent
, const wxWindowID id
, const wxString
& title
,
175 const wxPoint
& pos
, const wxSize
& size
, const long style
)
176 : wxMDIParentFrame(parent
, id
, title
, pos
, size
, style
)
180 SetIcon(wxICON(sample
));
183 wxMenu
*file_menu
= new wxMenu
;
185 file_menu
->Append(MDI_NEW_WINDOW
, wxT("&New test\tCtrl+N"));
186 file_menu
->Append(MDI_QUIT
, wxT("&Exit\tAlt+X"));
188 wxMenu
*help_menu
= new wxMenu
;
189 help_menu
->Append(MDI_ABOUT
, wxT("&About"));
191 wxMenuBar
*menu_bar
= new wxMenuBar
;
193 menu_bar
->Append(file_menu
, wxT("&File"));
194 menu_bar
->Append(help_menu
, wxT("&Help"));
196 // Associate the menu bar with the frame
197 SetMenuBar(menu_bar
);
201 #endif // wxUSE_STATUSBAR
203 CreateToolBar(wxNO_BORDER
| wxTB_FLAT
| wxTB_HORIZONTAL
);
204 InitToolBar(GetToolBar());
207 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
212 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
214 (void)wxMessageBox(wxT("wxWidgets SVG sample\n")
215 wxT("Author: Chris Elliott (c) 2002-2009\n")
216 wxT("Usage: click File|New to show tests"),
217 wxT("About SVG Test"));
220 void MyFrame::OnNewWindow(wxCommandEvent
& WXUNUSED(event
) )
222 // Make another frame, containing a canvas
223 MyChild
*subframe
= new MyChild(this, wxT("SVG Frame"));
226 title
.Printf(wxT("SVG Test Window %d"), m_nWinCreated
);
228 // counts number of children previously, even if now closed
231 // Give it a title and icon
232 subframe
->SetTitle(title
);
233 subframe
->SetIcon(wxICON(sample
));
236 wxMenu
*file_menu
= new wxMenu
;
238 file_menu
->Append(MDI_NEW_WINDOW
, wxT("&Another test\tCtrl+N"));
239 file_menu
->Append(MDI_SAVE
, wxT("&Save\tCtrl+S"), wxT("Save in SVG format"));
240 file_menu
->Append(MDI_CHILD_QUIT
, wxT("&Close child\tCtrl+F4"));
241 file_menu
->Append(MDI_QUIT
, wxT("&Exit\tAlt+X"));
243 wxMenu
*help_menu
= new wxMenu
;
244 help_menu
->Append(MDI_ABOUT
, wxT("&About"));
246 wxMenuBar
*menu_bar
= new wxMenuBar
;
248 menu_bar
->Append(file_menu
, wxT("&File"));
249 menu_bar
->Append(help_menu
, wxT("&Help"));
251 // Associate the menu bar with the frame
252 subframe
->SetMenuBar(menu_bar
);
254 subframe
->Show(true);
257 void MyFrame::OnSize(wxSizeEvent
& event
)
260 GetClientSize(&w
, &h
);
262 GetClientWindow()->SetSize(0, 0, w
, h
);
266 void MyFrame::InitToolBar(wxToolBar
* toolBar
)
268 const int maxBitmaps
= 3;
269 wxBitmap
* bitmaps
[maxBitmaps
];
271 bitmaps
[0] = new wxBitmap( new_xpm
);
272 bitmaps
[1] = new wxBitmap( save_xpm
);
273 bitmaps
[2] = new wxBitmap( help_xpm
);
275 toolBar
->AddTool(MDI_NEW_WINDOW
, wxEmptyString
, *(bitmaps
[0]), wxS("New SVG test window"));
276 toolBar
->AddTool(MDI_SAVE
, wxEmptyString
, *bitmaps
[1], wxS("Save test in SVG format"));
277 toolBar
->AddSeparator();
278 toolBar
->AddTool(MDI_ABOUT
, wxEmptyString
, *bitmaps
[2], wxS("Help"));
283 for (i
= 0; i
< maxBitmaps
; i
++)
287 void MyFrame::FileSavePicture (wxCommandEvent
& WXUNUSED(event
) )
290 MyChild
* pChild
= (MyChild
*)GetActiveChild();
296 wxFileDialog
dialog(this, wxT("Save Picture as"), wxEmptyString
, pChild
->GetTitle(),
297 wxT("SVG vector picture files (*.svg)|*.svg"),
298 wxFD_SAVE
|wxFD_OVERWRITE_PROMPT
);
300 if (dialog
.ShowModal() == wxID_OK
)
302 if (!pChild
->OnSave ( dialog
.GetPath() ))
308 #endif // wxUSE_FILEDLG
312 // ---------------------------------------------------------------------------
314 // ---------------------------------------------------------------------------
316 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
319 // Define a constructor for my canvas
320 MyCanvas::MyCanvas(MyChild
*parent
, const wxPoint
& pos
, const wxSize
& size
)
321 : wxScrolledWindow(parent
, wxID_ANY
, pos
, size
, wxSUNKEN_BORDER
|wxVSCROLL
|wxHSCROLL
)
323 SetBackgroundColour(wxColour(wxT("WHITE")));
326 m_index
= m_child
->GetFrame()->GetCountOfChildren() % 9;
329 // Define the repainting behaviour
330 void MyCanvas::OnDraw(wxDC
& dc
)
335 #endif // wxUSE_STATUSBAR
342 dc
.SetFont(*wxSWISS_FONT
);
343 dc
.SetPen(*wxGREEN_PEN
);
349 // draw lines to make a cross
350 dc
.DrawLine(0, 0, 200, 200);
351 dc
.DrawLine(200, 0, 0, 200);
352 // draw point colored line and spline
357 dc
.DrawPoint (25,15);
358 dc
.DrawLine(50, 30, 200, 30);
359 dc
.DrawSpline(50, 200, 50, 100, 200, 10);
361 s
= wxT("Green Cross, Cyan Line and spline");
362 #endif // wxUSE_STATUSBAR
366 // draw standard shapes
367 dc
.SetBrush(*wxCYAN_BRUSH
);
368 dc
.SetPen(*wxRED_PEN
);
369 dc
.DrawRectangle(10, 10, 100, 70);
370 wB
= wxBrush (wxT("DARK ORCHID"), wxBRUSHSTYLE_TRANSPARENT
);
372 dc
.DrawRoundedRectangle(50, 50, 100, 70, 20);
373 dc
.SetBrush (wxBrush(wxT("GOLDENROD")) );
374 dc
.DrawEllipse(100, 100, 100, 50);
376 points
[0].x
= 100; points
[0].y
= 200;
377 points
[1].x
= 70; points
[1].y
= 260;
378 points
[2].x
= 160; points
[2].y
= 230;
379 points
[3].x
= 40; points
[3].y
= 230;
380 points
[4].x
= 130; points
[4].y
= 260;
381 points
[5].x
= 100; points
[5].y
= 200;
383 dc
.DrawPolygon(5, points
);
384 dc
.DrawLines (6, points
, 160);
386 s
= wxT("Blue rectangle, red edge, clear rounded rectangle, gold ellipse, gold and clear stars");
387 #endif // wxUSE_STATUSBAR
391 // draw text in Arial or similar font
392 dc
.DrawLine(50,25,50,35);
393 dc
.DrawLine(45,30,55,30);
394 dc
.DrawText(wxT("This is a Swiss-style string"), 50, 30);
395 wC
= dc
.GetTextForeground();
396 dc
.SetTextForeground (wxT("FIREBRICK"));
398 // no effect in msw ??
399 dc
.SetTextBackground (wxT("WHEAT"));
400 dc
.DrawText(wxT("This is a Red string"), 50, 200);
401 dc
.DrawRotatedText(wxT("This is a 45 deg string"), 50, 200, 45);
402 dc
.DrawRotatedText(wxT("This is a 90 deg string"), 50, 200, 90);
403 wF
= wxFont ( 18, wxROMAN
, wxITALIC
, wxBOLD
, false, wxT("Times New Roman"));
405 dc
.SetTextForeground (wC
);
406 dc
.DrawText(wxT("This is a Times-style string"), 50, 60);
408 s
= wxT("Swiss, Times text; red text, rotated and colored orange");
409 #endif // wxUSE_STATUSBAR
413 // four arcs start and end points, center
414 dc
.SetBrush(*wxGREEN_BRUSH
);
415 dc
.DrawArc ( 200,300, 370,230, 300,300 );
416 dc
.SetBrush(*wxBLUE_BRUSH
);
417 dc
.DrawArc ( 270-50, 270-86, 270-86, 270-50, 270,270 );
418 dc
.SetDeviceOrigin(-10,-10);
419 dc
.DrawArc ( 270-50, 270-86, 270-86, 270-50, 270,270 );
420 dc
.SetDeviceOrigin(0,0);
422 wP
.SetColour (wxT("CADET BLUE"));
424 dc
.DrawArc ( 75,125, 110, 40, 75, 75 );
426 wP
.SetColour (wxT("SALMON"));
428 dc
.SetBrush(*wxRED_BRUSH
);
429 //top left corner, width and height, start and end angle
430 // 315 same center and x-radius as last pie-arc, half Y radius
431 dc
.DrawEllipticArc(25,50,100,50,180.0,45.0);
437 dc
.SetBrush (wxBrush (wxT("SALMON")));
438 dc
.DrawEllipticArc(300, 0,200,100, 0.0,145.0);
440 dc
.DrawEllipticArc(300, 50,200,100,90.0,145.0);
441 dc
.DrawEllipticArc(300,100,200,100,90.0,345.0);
444 s
= wxT("This is an arc test page");
445 #endif // wxUSE_STATUSBAR
449 dc
.DrawCheckMark ( 30,30,25,25);
450 dc
.SetBrush (wxBrush (wxT("SALMON"),wxBRUSHSTYLE_TRANSPARENT
));
451 dc
.DrawCheckMark ( 80,50,75,75);
452 dc
.DrawRectangle ( 80,50,75,75);
454 s
= wxT("Two check marks");
455 #endif // wxUSE_STATUSBAR
459 wF
= wxFont ( 18, wxROMAN
, wxITALIC
, wxBOLD
, false, wxT("Times New Roman"));
461 dc
.DrawLine(0, 0, 200, 200);
462 dc
.DrawLine(200, 0, 0, 200);
463 dc
.DrawText(wxT("This is an 18pt string"), 50, 60);
465 // rescale and draw in blue
468 dc
.SetUserScale (2.0,0.5);
469 dc
.SetDeviceOrigin(200,0);
470 dc
.DrawLine(0, 0, 200, 200);
471 dc
.DrawLine(200, 0, 0, 200);
472 dc
.DrawText(wxT("This is an 18pt string 2 x 0.5 UserScaled"), 50, 60);
473 dc
.SetUserScale (2.0,2.0);
474 dc
.SetDeviceOrigin(200,200);
475 dc
.DrawText(wxT("This is an 18pt string 2 x 2 UserScaled"), 50, 60);
479 dc
.SetUserScale (1.0,1.0);
480 dc
.SetDeviceOrigin(0,10);
481 dc
.SetMapMode (wxMM_METRIC
); //svg ignores this
482 dc
.DrawLine(0, 0, 200, 200);
483 dc
.DrawLine(200, 0, 0, 200);
484 dc
.DrawText(wxT("This is an 18pt string in MapMode"), 50, 60);
486 s
= wxT("Scaling test page");
487 #endif // wxUSE_STATUSBAR
491 dc
.DrawIcon( wxICON(sample
), 10, 10 );
492 dc
.DrawBitmap ( wxBitmap(svgbitmap_xpm
), 50,15);
494 s
= wxT("Icon and Bitmap ");
495 #endif // wxUSE_STATUSBAR
499 dc
.SetTextForeground(wxT("RED"));
500 dc
.DrawText(wxT("Red = Clipping Off"), 30, 5);
501 dc
.SetTextForeground(wxT("GREEN"));
502 dc
.DrawText(wxT("Green = Clipping On"), 30, 25);
504 dc
.SetTextForeground(wxT("BLACK"));
506 dc
.SetPen(*wxRED_PEN
);
507 dc
.SetBrush (wxBrush (wxT("SALMON"),wxBRUSHSTYLE_TRANSPARENT
));
508 dc
.DrawCheckMark ( 80,50,75,75);
509 dc
.DrawRectangle ( 80,50,75,75);
511 dc
.SetPen(*wxGREEN_PEN
);
513 // Clipped checkmarks
514 dc
.DrawRectangle(180,50,75,75);
515 dc
.SetClippingRegion(180,50,75,75); // x,y,width,height version
516 dc
.DrawCheckMark ( 180,50,75,75);
517 dc
.DestroyClippingRegion();
519 dc
.DrawRectangle(wxRect(80,150,75,75));
520 dc
.SetClippingRegion(wxPoint(80,150),wxSize(75,75)); // pt,size version
521 dc
.DrawCheckMark ( 80,150,75,75);
522 dc
.DestroyClippingRegion();
524 dc
.DrawRectangle(wxRect(180,150,75,75));
525 dc
.SetClippingRegion(wxRect(180,150,75,75)); // rect version
526 dc
.DrawCheckMark ( 180,150,75,75);
527 dc
.DestroyClippingRegion();
529 dc
.DrawRectangle(wxRect( 80,250,50,65));
530 dc
.DrawRectangle(wxRect(105,260,50,65));
531 dc
.SetClippingRegion(wxRect( 80,250,50,65)); // second call to SetClippingRegion
532 dc
.SetClippingRegion(wxRect(105,260,50,65)); // forms intersection with previous
533 dc
.DrawCheckMark(80,250,75,75);
534 dc
.DestroyClippingRegion(); // only one call to destroy (there's no stack)
537 ** Clipping by wxRegion not implemented for SVG. Should be
538 ** possible, but need to access points that define the wxRegion
539 ** from inside DoSetDeviceClippingRegion() and wxRegion does not
540 ** implement anything like getPoints().
541 points[0].x = 180; points[0].y = 250;
542 points[1].x = 255; points[1].y = 250;
543 points[2].x = 180; points[2].y = 325;
544 points[3].x = 255; points[3].y = 325;
545 points[4].x = 180; points[4].y = 250;
547 dc.DrawLines (5, points);
548 wxRegion reg = wxRegion(5,points);
550 dc.SetClippingRegion(reg);
551 dc.DrawCheckMark ( 180,250,75,75);
552 dc.DestroyClippingRegion();
556 s
= wxT("Clipping region");
557 #endif // wxUSE_STATUSBAR
562 wxCoord txtX
, txtY
, txtW
, txtH
, txtDescent
, txtEL
;
567 //dc.SetBackgroundMode(wxBRUSHSTYLE_SOLID);
568 //dc.SetTextBackground(*wxBLUE);
571 txtStr
= wxT("Horizontal string");
572 dc
.GetTextExtent(txtStr
, &txtW
, &txtH
, &txtDescent
, &txtEL
);
575 dc
.DrawRectangle(txtX
, txtY
, txtW
+ 2*txtPad
, txtH
+ 2*txtPad
);
576 dc
.DrawText(txtStr
, txtX
+ txtPad
, txtY
+ txtPad
);
579 txtStr
= wxT("Vertical string");
580 dc
.GetTextExtent(txtStr
, &txtW
, &txtH
, &txtDescent
, &txtEL
);
583 dc
.DrawRectangle(txtX
, txtY
- (txtW
+ 2*txtPad
), txtH
+ 2*txtPad
, txtW
+ 2*txtPad
);
584 dc
.DrawRotatedText(txtStr
, txtX
+ txtPad
, txtY
- txtPad
, 90);
587 txtStr
= wxT("45 deg string");
588 dc
.GetTextExtent(txtStr
, &txtW
, &txtH
, &txtDescent
, &txtEL
);
589 double lenW
= (double)(txtW
+ 2*txtPad
) / sqrt(2.0);
590 double lenH
= (double)(txtH
+ 2*txtPad
) / sqrt(2.0);
591 double padding
= (double)txtPad
/ sqrt(2.0);
594 dc
.DrawLine(txtX
- padding
, txtY
, txtX
+ lenW
, txtY
- lenW
); // top
595 dc
.DrawLine(txtX
+ lenW
, txtY
- lenW
, txtX
- padding
+ lenH
+ lenW
, txtY
+ (lenH
- lenW
));
596 dc
.DrawLine(txtX
- padding
, txtY
, txtX
- padding
+ lenH
, txtY
+ lenH
);
597 dc
.DrawLine(txtX
- padding
+ lenH
, txtY
+ lenH
, txtX
- padding
+ lenH
+ lenW
, txtY
+ (lenH
- lenW
)); // bottom
598 dc
.DrawRotatedText(txtStr
, txtX
, txtY
, 45);
600 s
= wxT("Text position test page");
601 #endif // wxUSE_STATUSBAR
605 m_child
->SetStatusText(s
);
606 #endif // wxUSE_STATUSBAR
610 // ---------------------------------------------------------------------------
612 // ---------------------------------------------------------------------------
614 // Note that MDI_NEW_WINDOW and MDI_ABOUT commands get passed
615 // to the parent window for processing, so no need to
616 // duplicate event handlers here.
617 BEGIN_EVENT_TABLE(MyChild
, wxMDIChildFrame
)
618 EVT_MENU(MDI_CHILD_QUIT
, MyChild::OnQuit
)
621 MyChild::MyChild(wxMDIParentFrame
*parent
, const wxString
& title
,
622 const wxPoint
& pos
, const wxSize
& size
,
624 : wxMDIChildFrame(parent
, wxID_ANY
, title
, pos
, size
, style
)
626 m_frame
= (MyFrame
*) parent
;
630 SetStatusText(title
);
631 #endif // wxUSE_STATUSBAR
633 m_canvas
= new MyCanvas(this, wxPoint(0, 0), GetClientSize());
635 // Give it scrollbars
636 m_canvas
->SetScrollbars(20, 20, 50, 50);
643 void MyChild::OnQuit(wxCommandEvent
& WXUNUSED(event
))
648 bool MyChild::OnSave(wxString filename
)
650 wxSVGFileDC
svgDC (filename
, 600, 650);
651 m_canvas
->OnDraw (svgDC
);
655 void MyChild::OnActivate(wxActivateEvent
& event
)
657 if ( event
.GetActive() && m_canvas
)
658 m_canvas
->SetFocus();