]>
git.saurik.com Git - wxWidgets.git/blob - samples/svg/svgtest.cpp
1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Chris Elliott
7 // Licence: wxWindows license
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 "mondrian.xpm"
36 #include "bitmaps/new.xpm"
37 #include "bitmaps/save.xpm"
38 #include "bitmaps/help.xpm"
39 #include "SVGlogo24.xpm"
44 // ---------------------------------------------------------------------------
46 // ---------------------------------------------------------------------------
48 class MyApp
: public wxApp
54 class MyFrame
: public wxMDIParentFrame
57 MyFrame(wxWindow
*parent
, const wxWindowID id
, const wxString
& title
,
58 const wxPoint
& pos
, const wxSize
& size
, const long style
);
60 void InitToolBar(wxToolBar
* toolBar
);
62 void OnSize(wxSizeEvent
& event
);
63 void OnAbout(wxCommandEvent
& event
);
64 void OnNewWindow(wxCommandEvent
& event
);
65 void OnQuit(wxCommandEvent
& event
);
66 void FileSavePicture (wxCommandEvent
& event
);
68 unsigned int GetCountOfChildren() const
69 { return m_nWinCreated
; }
72 unsigned int m_nWinCreated
;
77 class MyChild
: public wxMDIChildFrame
80 MyChild(wxMDIParentFrame
*parent
, const wxString
& title
,
81 const wxPoint
& pos
= wxDefaultPosition
,
82 const wxSize
& size
= wxDefaultSize
,
83 const long style
= wxDEFAULT_FRAME_STYLE
);
86 void OnActivate(wxActivateEvent
& event
);
87 void OnQuit(wxCommandEvent
& event
);
88 bool OnSave(wxString filename
);
100 class MyCanvas
: public wxScrolledWindow
103 MyCanvas(MyChild
*parent
, const wxPoint
& pos
, const wxSize
& size
);
104 virtual void OnDraw(wxDC
& dc
);
110 DECLARE_EVENT_TABLE()
113 // ---------------------------------------------------------------------------
115 // ---------------------------------------------------------------------------
128 // ---------------------------------------------------------------------------
130 // ---------------------------------------------------------------------------
132 BEGIN_EVENT_TABLE(MyFrame
, wxMDIParentFrame
)
133 EVT_MENU(MDI_ABOUT
, MyFrame::OnAbout
)
134 EVT_MENU(MDI_NEW_WINDOW
, MyFrame::OnNewWindow
)
135 EVT_MENU(MDI_QUIT
, MyFrame::OnQuit
)
136 EVT_MENU (MDI_SAVE
, MyFrame::FileSavePicture
)
138 EVT_SIZE(MyFrame::OnSize
)
141 // ===========================================================================
143 // ===========================================================================
145 // ---------------------------------------------------------------------------
147 // ---------------------------------------------------------------------------
153 // Create the main frame window
155 MyFrame
* frame
= new MyFrame((wxFrame
*)NULL
, -1, wxT("SVG Demo"),
156 wxDefaultPosition
, wxSize(500, 400),
157 wxDEFAULT_FRAME_STYLE
| wxHSCROLL
| wxVSCROLL
);
167 // ---------------------------------------------------------------------------
169 // ---------------------------------------------------------------------------
171 // Define my frame constructor
172 MyFrame::MyFrame(wxWindow
*parent
, const wxWindowID id
, const wxString
& title
,
173 const wxPoint
& pos
, const wxSize
& size
, const long style
)
174 : wxMDIParentFrame(parent
, id
, title
, pos
, size
, style
)
178 SetIcon(wxICON(mondrian
));
181 wxMenu
*file_menu
= new wxMenu
;
183 file_menu
->Append(MDI_NEW_WINDOW
, wxT("&New test\tCtrl+N"));
184 file_menu
->Append(MDI_QUIT
, wxT("&Exit\tAlt+X"));
186 wxMenu
*help_menu
= new wxMenu
;
187 help_menu
->Append(MDI_ABOUT
, wxT("&About"));
189 wxMenuBar
*menu_bar
= new wxMenuBar
;
191 menu_bar
->Append(file_menu
, wxT("&File"));
192 menu_bar
->Append(help_menu
, wxT("&Help"));
194 // Associate the menu bar with the frame
195 SetMenuBar(menu_bar
);
199 #endif // wxUSE_STATUSBAR
201 CreateToolBar(wxNO_BORDER
| wxTB_FLAT
| wxTB_HORIZONTAL
);
202 InitToolBar(GetToolBar());
205 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
210 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
212 (void)wxMessageBox(wxT("wxWidgets SVG sample\n")
213 wxT("Author: Chris Elliott (c) 2002-2009\n")
214 wxT("Usage: click File|New to show tests"),
215 wxT("About SVG Test"));
218 void MyFrame::OnNewWindow(wxCommandEvent
& WXUNUSED(event
) )
220 // Make another frame, containing a canvas
221 MyChild
*subframe
= new MyChild(this, wxT("SVG Frame"));
224 title
.Printf(wxT("SVG Test Window %d"), m_nWinCreated
);
226 // counts number of children previously, even if now closed
229 // Give it a title and icon
230 subframe
->SetTitle(title
);
231 subframe
->SetIcon(wxICON(mondrian
));
234 wxMenu
*file_menu
= new wxMenu
;
236 file_menu
->Append(MDI_NEW_WINDOW
, wxT("&Another test\tCtrl+N"));
237 file_menu
->Append(MDI_SAVE
, wxT("&Save\tCtrl+S"), wxT("Save in SVG format"));
238 file_menu
->Append(MDI_CHILD_QUIT
, wxT("&Close child\tCtrl+F4"));
239 file_menu
->Append(MDI_QUIT
, wxT("&Exit\tAlt+X"));
241 wxMenu
*help_menu
= new wxMenu
;
242 help_menu
->Append(MDI_ABOUT
, wxT("&About"));
244 wxMenuBar
*menu_bar
= new wxMenuBar
;
246 menu_bar
->Append(file_menu
, wxT("&File"));
247 menu_bar
->Append(help_menu
, wxT("&Help"));
249 // Associate the menu bar with the frame
250 subframe
->SetMenuBar(menu_bar
);
252 subframe
->Show(true);
255 void MyFrame::OnSize(wxSizeEvent
& event
)
258 GetClientSize(&w
, &h
);
260 GetClientWindow()->SetSize(0, 0, w
, h
);
264 void MyFrame::InitToolBar(wxToolBar
* toolBar
)
266 const int maxBitmaps
= 3;
267 wxBitmap
* bitmaps
[maxBitmaps
];
269 bitmaps
[0] = new wxBitmap( new_xpm
);
270 bitmaps
[1] = new wxBitmap( save_xpm
);
271 bitmaps
[2] = new wxBitmap( help_xpm
);
273 toolBar
->AddTool(MDI_NEW_WINDOW
, wxEmptyString
, *(bitmaps
[0]), wxS("New SVG test window"));
274 toolBar
->AddTool(MDI_SAVE
, wxEmptyString
, *bitmaps
[1], wxS("Save test in SVG format"));
275 toolBar
->AddSeparator();
276 toolBar
->AddTool(MDI_ABOUT
, wxEmptyString
, *bitmaps
[2], wxS("Help"));
281 for (i
= 0; i
< maxBitmaps
; i
++)
285 void MyFrame::FileSavePicture (wxCommandEvent
& WXUNUSED(event
) )
288 MyChild
* pChild
= (MyChild
*)GetActiveChild();
294 wxFileDialog
dialog(this, wxT("Save Picture as"), wxEmptyString
, pChild
->GetTitle(),
295 wxT("SVG vector picture files (*.svg)|*.svg"),
296 wxFD_SAVE
|wxFD_OVERWRITE_PROMPT
);
298 if (dialog
.ShowModal() == wxID_OK
)
300 if (!pChild
->OnSave ( dialog
.GetPath() ))
306 #endif // wxUSE_FILEDLG
310 // ---------------------------------------------------------------------------
312 // ---------------------------------------------------------------------------
314 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
317 // Define a constructor for my canvas
318 MyCanvas::MyCanvas(MyChild
*parent
, const wxPoint
& pos
, const wxSize
& size
)
319 : wxScrolledWindow(parent
, wxID_ANY
, pos
, size
, wxSUNKEN_BORDER
|wxVSCROLL
|wxHSCROLL
)
321 SetBackgroundColour(wxColour(_T("WHITE")));
324 m_index
= m_child
->GetFrame()->GetCountOfChildren() % 7;
327 // Define the repainting behaviour
328 void MyCanvas::OnDraw(wxDC
& dc
)
333 #endif // wxUSE_STATUSBAR
340 dc
.SetFont(*wxSWISS_FONT
);
341 dc
.SetPen(*wxGREEN_PEN
);
347 // draw lines to make a cross
348 dc
.DrawLine(0, 0, 200, 200);
349 dc
.DrawLine(200, 0, 0, 200);
350 // draw point colored line and spline
355 dc
.DrawPoint (25,15);
356 dc
.DrawLine(50, 30, 200, 30);
357 dc
.DrawSpline(50, 200, 50, 100, 200, 10);
359 s
= wxT("Green Cross, Cyan Line and spline");
360 #endif // wxUSE_STATUSBAR
364 // draw standard shapes
365 dc
.SetBrush(*wxCYAN_BRUSH
);
366 dc
.SetPen(*wxRED_PEN
);
367 dc
.DrawRectangle(10, 10, 100, 70);
368 wB
= wxBrush (_T("DARK ORCHID"), wxBRUSHSTYLE_TRANSPARENT
);
370 dc
.DrawRoundedRectangle(50, 50, 100, 70, 20);
371 dc
.SetBrush (wxBrush(_T("GOLDENROD")) );
372 dc
.DrawEllipse(100, 100, 100, 50);
374 points
[0].x
= 100; points
[0].y
= 200;
375 points
[1].x
= 70; points
[1].y
= 260;
376 points
[2].x
= 160; points
[2].y
= 230;
377 points
[3].x
= 40; points
[3].y
= 230;
378 points
[4].x
= 130; points
[4].y
= 260;
379 points
[5].x
= 100; points
[5].y
= 200;
381 dc
.DrawPolygon(5, points
);
382 dc
.DrawLines (6, points
, 160);
384 s
= wxT("Blue rectangle, red edge, clear rounded rectangle, gold ellipse, gold and clear stars");
385 #endif // wxUSE_STATUSBAR
389 // draw text in Arial or similar font
390 dc
.DrawLine(50,25,50,35);
391 dc
.DrawLine(45,30,55,30);
392 dc
.DrawText(wxT("This is a Swiss-style string"), 50, 30);
393 wC
= dc
.GetTextForeground();
394 dc
.SetTextForeground (_T("FIREBRICK"));
396 // no effect in msw ??
397 dc
.SetTextBackground (_T("WHEAT"));
398 dc
.DrawText(wxT("This is a Red string"), 50, 200);
399 dc
.DrawRotatedText(wxT("This is a 45 deg string"), 50, 200, 45);
400 dc
.DrawRotatedText(wxT("This is a 90 deg string"), 50, 200, 90);
401 wF
= wxFont ( 18, wxROMAN
, wxITALIC
, wxBOLD
, false, wxT("Times New Roman"));
403 dc
.SetTextForeground (wC
);
404 dc
.DrawText(wxT("This is a Times-style string"), 50, 60);
406 s
= wxT("Swiss, Times text; red text, rotated and colored orange");
407 #endif // wxUSE_STATUSBAR
411 // four arcs start and end points, center
412 dc
.SetBrush(*wxGREEN_BRUSH
);
413 dc
.DrawArc ( 200,300, 370,230, 300,300 );
414 dc
.SetBrush(*wxBLUE_BRUSH
);
415 dc
.DrawArc ( 270-50, 270-86, 270-86, 270-50, 270,270 );
416 dc
.SetDeviceOrigin(-10,-10);
417 dc
.DrawArc ( 270-50, 270-86, 270-86, 270-50, 270,270 );
418 dc
.SetDeviceOrigin(0,0);
420 wP
.SetColour (_T("CADET BLUE"));
422 dc
.DrawArc ( 75,125, 110, 40, 75, 75 );
424 wP
.SetColour (_T("SALMON"));
426 dc
.SetBrush(*wxRED_BRUSH
);
427 //top left corner, width and height, start and end angle
428 // 315 same center and x-radius as last pie-arc, half Y radius
429 dc
.DrawEllipticArc(25,50,100,50,180.0,45.0);
435 dc
.SetBrush (wxBrush (_T("SALMON")));
436 dc
.DrawEllipticArc(300, 0,200,100, 0.0,145.0);
438 dc
.DrawEllipticArc(300, 50,200,100,90.0,145.0);
439 dc
.DrawEllipticArc(300,100,200,100,90.0,345.0);
442 s
= wxT("This is an arc test page");
443 #endif // wxUSE_STATUSBAR
447 dc
.DrawCheckMark ( 30,30,25,25);
448 dc
.SetBrush (wxBrush (_T("SALMON"),wxBRUSHSTYLE_TRANSPARENT
));
449 dc
.DrawCheckMark ( 80,50,75,75);
450 dc
.DrawRectangle ( 80,50,75,75);
452 s
= wxT("Two check marks");
453 #endif // wxUSE_STATUSBAR
457 wF
= wxFont ( 18, wxROMAN
, wxITALIC
, wxBOLD
, false, wxT("Times New Roman"));
459 dc
.DrawLine(0, 0, 200, 200);
460 dc
.DrawLine(200, 0, 0, 200);
461 dc
.DrawText(wxT("This is an 18pt string"), 50, 60);
463 // rescale and draw in blue
466 dc
.SetUserScale (2.0,0.5);
467 dc
.SetDeviceOrigin(200,0);
468 dc
.DrawLine(0, 0, 200, 200);
469 dc
.DrawLine(200, 0, 0, 200);
470 dc
.DrawText(wxT("This is an 18pt string 2 x 0.5 UserScaled"), 50, 60);
471 dc
.SetUserScale (2.0,2.0);
472 dc
.SetDeviceOrigin(200,200);
473 dc
.DrawText(wxT("This is an 18pt string 2 x 2 UserScaled"), 50, 60);
477 dc
.SetUserScale (1.0,1.0);
478 dc
.SetDeviceOrigin(0,10);
479 dc
.SetMapMode (wxMM_METRIC
); //svg ignores this
480 dc
.DrawLine(0, 0, 200, 200);
481 dc
.DrawLine(200, 0, 0, 200);
482 dc
.DrawText(wxT("This is an 18pt string in MapMode"), 50, 60);
484 s
= wxT("Scaling test page");
485 #endif // wxUSE_STATUSBAR
489 dc
.DrawIcon( wxIcon(mondrian_xpm
), 10, 10 );
490 dc
.DrawBitmap ( wxBitmap(svgbitmap_xpm
), 50,15);
492 s
= wxT("Icon and Bitmap ");
493 #endif // wxUSE_STATUSBAR
498 m_child
->SetStatusText(s
);
499 #endif // wxUSE_STATUSBAR
503 // ---------------------------------------------------------------------------
505 // ---------------------------------------------------------------------------
507 // Note that MDI_NEW_WINDOW and MDI_ABOUT commands get passed
508 // to the parent window for processing, so no need to
509 // duplicate event handlers here.
510 BEGIN_EVENT_TABLE(MyChild
, wxMDIChildFrame
)
511 EVT_MENU(MDI_CHILD_QUIT
, MyChild::OnQuit
)
514 MyChild::MyChild(wxMDIParentFrame
*parent
, const wxString
& title
,
515 const wxPoint
& pos
, const wxSize
& size
,
517 : wxMDIChildFrame(parent
, wxID_ANY
, title
, pos
, size
, style
)
519 m_frame
= (MyFrame
*) parent
;
523 SetStatusText(title
);
524 #endif // wxUSE_STATUSBAR
526 m_canvas
= new MyCanvas(this, wxPoint(0, 0), GetClientSize());
528 // Give it scrollbars
529 m_canvas
->SetScrollbars(20, 20, 50, 50);
536 void MyChild::OnQuit(wxCommandEvent
& WXUNUSED(event
))
541 bool MyChild::OnSave(wxString filename
)
543 wxSVGFileDC
svgDC (filename
, 600, 650);
544 m_canvas
->OnDraw (svgDC
);
548 void MyChild::OnActivate(wxActivateEvent
& event
)
550 if ( event
.GetActive() && m_canvas
)
551 m_canvas
->SetFocus();