1 // biol75@york.ac.uk (Chris Elliott) March 2000
7 //---------------------------------------------------------------------------
8 #define WinMain WinMain
10 /////////////////////////////////////////////////////////////////////////////
12 // Purpose: SVG sample
13 // Author: Chris Elliott
16 // Licence: wxWindows license
17 /////////////////////////////////////////////////////////////////////////////
19 // ===========================================================================
21 // ===========================================================================
23 // ---------------------------------------------------------------------------
25 // ---------------------------------------------------------------------------
27 // For compilers that support precompilation, includes "wx/wx.h".
28 #include "wx/wxprec.h"
39 #include "wx/toolbar.h"
40 #include "wx/svg/dcsvg.h"
42 #include "mondrian.xpm"
44 #include "bitmaps/new.xpm"
45 #include "bitmaps/save.xpm"
46 #include "bitmaps/help.xpm"
47 #include "SVGlogo24.xpm"
51 // Define a new application
52 class MyApp
: public wxApp
59 class MyFrame
: public wxMDIParentFrame
66 MyFrame(wxWindow
*parent
, const wxWindowID id
, const wxString
& title
,
67 const wxPoint
& pos
, const wxSize
& size
, const long style
);
69 void InitToolBar(wxToolBar
* toolBar
);
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
) ) ;
82 class MyCanvas
: public wxScrolledWindow
88 MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
);
89 virtual void OnDraw(wxDC
& dc
);
94 class MyChild
: public wxMDIChildFrame
100 //////////////////// Methods
102 MyChild(wxMDIParentFrame
*parent
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
, const long style
);
105 void OnActivate(wxActivateEvent
& event
);
106 void OnQuit(wxCommandEvent
& event
);
107 void OnClose(wxCloseEvent
& event
);
108 bool OnSave(wxString filename
) ;
110 DECLARE_EVENT_TABLE()
127 // ---------------------------------------------------------------------------
129 // ---------------------------------------------------------------------------
131 MyFrame
*frame
= (MyFrame
*) NULL
;
133 // ---------------------------------------------------------------------------
135 // ---------------------------------------------------------------------------
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
)
144 EVT_SIZE(MyFrame::OnSize
)
147 // ===========================================================================
149 // ===========================================================================
151 // ---------------------------------------------------------------------------
153 // ---------------------------------------------------------------------------
155 // Initialise this in OnInit, not statically
158 // Create the main frame window
160 frame
= new MyFrame((wxFrame
*)NULL
, -1, wxT("SVG Demo"),
161 wxPoint(-1, -1), wxSize(500, 400),
162 wxDEFAULT_FRAME_STYLE
| wxHSCROLL
| wxVSCROLL
);
166 wxMenu
*file_menu
= new wxMenu
;
168 file_menu
->Append(MDI_NEW_WINDOW
, wxT("&New test\tCtrl+N"));
169 file_menu
->Append(MDI_QUIT
, wxT("&Exit\tAlt+X"));
171 wxMenu
*help_menu
= new wxMenu
;
172 help_menu
->Append(MDI_ABOUT
, wxT("&About"));
174 wxMenuBar
*menu_bar
= new wxMenuBar
;
176 menu_bar
->Append(file_menu
, wxT("&File"));
177 menu_bar
->Append(help_menu
, wxT("&Help"));
179 // Associate the menu bar with the frame
180 frame
->SetMenuBar(menu_bar
);
183 frame
->CreateStatusBar();
184 #endif // wxUSE_STATUSBAR
194 // ---------------------------------------------------------------------------
196 // ---------------------------------------------------------------------------
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
)
206 SetIcon(wxICON(mondrian
));
208 CreateToolBar(wxNO_BORDER
| wxTB_FLAT
| wxTB_HORIZONTAL
);
209 InitToolBar(GetToolBar());
214 void MyFrame::OnClose(wxCloseEvent
& event
)
216 if ( !event
.CanVeto() )
221 if ( m_children
.GetCount () < 1 )
226 // now try the children
227 wxObjectList::compatibility_iterator pNode
= m_children
.GetFirst ();
228 wxObjectList::compatibility_iterator pNext
;
232 pNext
= pNode
-> GetNext ();
233 pChild
= (MyChild
*) pNode
-> GetData ();
234 if (pChild
-> Close ())
236 m_children
.Erase(pNode
) ;
249 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
255 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
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"));
263 void MyFrame::OnNewWindow(wxCommandEvent
& WXUNUSED(event
) )
265 // Make another frame, containing a canvas
268 m_children
.Append (new MyChild(frame
, wxT("SVG Frame"),
269 wxPoint(-1, -1), wxSize(-1, -1),
270 wxDEFAULT_FRAME_STYLE
) ) ;
272 subframe
= (MyChild
*) m_children
.GetLast() -> GetData ();
274 title
.Printf(wxT("SVG Test Window %d"), nWinCreated
);
275 // counts number of children previously, even if now closed
278 // Give it a title and icon
279 subframe
->SetTitle(title
);
280 subframe
->SetIcon(wxICON(mondrian
));
283 wxMenu
*file_menu
= new wxMenu
;
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"));
290 wxMenu
*help_menu
= new wxMenu
;
291 help_menu
->Append(MDI_ABOUT
, wxT("&About"));
293 wxMenuBar
*menu_bar
= new wxMenuBar
;
295 menu_bar
->Append(file_menu
, wxT("&File"));
296 menu_bar
->Append(help_menu
, wxT("&Help"));
298 // Associate the menu bar with the frame
299 subframe
->SetMenuBar(menu_bar
);
301 subframe
->Show(true);
305 void MyFrame::OnSize(wxSizeEvent
& event
)
308 GetClientSize(&w
, &h
);
310 GetClientWindow()->SetSize(0, 0, w
, h
);
315 void MyFrame::InitToolBar(wxToolBar
* toolBar
)
317 const int maxBitmaps
= 3 ;
318 wxBitmap
* bitmaps
[maxBitmaps
];
320 bitmaps
[0] = new wxBitmap( new_xpm
);
321 bitmaps
[1] = new wxBitmap( save_xpm
);
322 bitmaps
[2] = new wxBitmap( help_xpm
);
327 toolBar
->AddTool( MDI_NEW_WINDOW
, *(bitmaps
[0]), wxNullBitmap
, false, currentX
, wxDefaultCoord
, (wxObject
*) NULL
, wxT("New SVG test window"));
328 currentX
+= width
+ 5;
329 toolBar
->AddTool( MDI_SAVE
, *bitmaps
[1], wxNullBitmap
, false, currentX
, wxDefaultCoord
, (wxObject
*) NULL
, wxT("Save test in SVG format"));
330 currentX
+= width
+ 5;
331 toolBar
->AddSeparator();
332 toolBar
->AddTool(MDI_ABOUT
, *bitmaps
[2], wxNullBitmap
, false, currentX
, wxDefaultCoord
, (wxObject
*) NULL
, wxT("Help"));
337 for (i
= 0; i
< maxBitmaps
; i
++)
342 void MyFrame::FileSavePicture (wxCommandEvent
& WXUNUSED(event
) )
345 MyChild
* pChild
= (MyChild
*)GetActiveChild ();
351 wxFileDialog
dialog(this, wxT("Save Picture as"), wxEmptyString
, pChild
->GetTitle(),
352 wxT("SVG vector picture files (*.svg)|*.svg"),
353 wxFD_SAVE
|wxFD_OVERWRITE_PROMPT
);
355 if (dialog
.ShowModal() == wxID_OK
)
357 if (!pChild
-> OnSave ( dialog
.GetPath() ))
363 #endif // wxUSE_FILEDLG
367 // Note that MDI_NEW_WINDOW and MDI_ABOUT commands get passed
368 // to the parent window for processing, so no need to
369 // duplicate event handlers here.
370 BEGIN_EVENT_TABLE(MyChild
, wxMDIChildFrame
)
371 EVT_MENU(MDI_CHILD_QUIT
, MyChild::OnQuit
)
372 EVT_CLOSE(MyChild::OnClose
)
375 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
379 // ---------------------------------------------------------------------------
381 // ---------------------------------------------------------------------------
383 // Define a constructor for my canvas
384 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
)
385 : wxScrolledWindow(parent
, -1, pos
, size
,
386 wxSUNKEN_BORDER
|wxVSCROLL
|wxHSCROLL
)
388 m_child
= (MyChild
*) parent
;
389 SetBackgroundColour(wxColour(_T("WHITE")));
390 m_index
= m_child
->m_frame
->nWinCreated
% 7 ;
394 // Define the repainting behaviour
395 void MyCanvas::OnDraw(wxDC
& dc
)
400 #endif // wxUSE_STATUSBAR
407 dc
.SetFont(*wxSWISS_FONT
);
408 dc
.SetPen(*wxGREEN_PEN
);
415 // draw lines to make a cross
416 dc
.DrawLine(0, 0, 200, 200);
417 dc
.DrawLine(200, 0, 0, 200);
418 // draw point colored line and spline
423 dc
.DrawPoint (25,15) ;
424 dc
.DrawLine(50, 30, 200, 30);
425 dc
.DrawSpline(50, 200, 50, 100, 200, 10);
427 s
= wxT("Green Cross, Cyan Line and spline");
428 #endif // wxUSE_STATUSBAR
432 // draw standard shapes
433 dc
.SetBrush(*wxCYAN_BRUSH
);
434 dc
.SetPen(*wxRED_PEN
);
435 dc
.DrawRectangle(10, 10, 100, 70);
436 wB
= wxBrush (_T("DARK ORCHID"), wxTRANSPARENT
);
438 dc
.DrawRoundedRectangle(50, 50, 100, 70, 20);
439 dc
.SetBrush (wxBrush(_T("GOLDENROD"), wxSOLID
) );
440 dc
.DrawEllipse(100, 100, 100, 50);
442 points
[0].x
= 100; points
[0].y
= 200;
443 points
[1].x
= 70; points
[1].y
= 260;
444 points
[2].x
= 160; points
[2].y
= 230;
445 points
[3].x
= 40; points
[3].y
= 230;
446 points
[4].x
= 130; points
[4].y
= 260;
447 points
[5].x
= 100; points
[5].y
= 200;
449 dc
.DrawPolygon(5, points
);
450 dc
.DrawLines (6, points
, 160);
452 s
= wxT("Blue rectangle, red edge, clear rounded rectangle, gold ellipse, gold and clear stars");
453 #endif // wxUSE_STATUSBAR
457 // draw text in Arial or similar font
458 dc
.DrawLine(50,25,50,35);
459 dc
.DrawLine(45,30,55,30);
460 dc
.DrawText(wxT("This is a Swiss-style string"), 50, 30);
461 wC
= dc
.GetTextForeground() ;
462 dc
.SetTextForeground (_T("FIREBRICK"));
464 // no effect in msw ??
465 dc
.SetTextBackground (_T("WHEAT"));
466 dc
.DrawText(wxT("This is a Red string"), 50, 200);
467 dc
.DrawRotatedText(wxT("This is a 45 deg string"), 50, 200, 45);
468 dc
.DrawRotatedText(wxT("This is a 90 deg string"), 50, 200, 90);
469 wF
= wxFont ( 18, wxROMAN
, wxITALIC
, wxBOLD
, false, wxT("Times New Roman"));
471 dc
.SetTextForeground (wC
) ;
472 dc
.DrawText(wxT("This is a Times-style string"), 50, 60);
474 s
= wxT("Swiss, Times text; red text, rotated and colored orange");
475 #endif // wxUSE_STATUSBAR
479 // four arcs start and end points, center
480 dc
.SetBrush(*wxGREEN_BRUSH
);
481 dc
.DrawArc ( 200,300, 370,230, 300,300 );
482 dc
.SetBrush(*wxBLUE_BRUSH
);
483 dc
.DrawArc ( 270-50, 270-86, 270-86, 270-50, 270,270 );
484 dc
.SetDeviceOrigin(-10,-10);
485 dc
.DrawArc ( 270-50, 270-86, 270-86, 270-50, 270,270 );
486 dc
.SetDeviceOrigin(0,0);
488 wP
.SetColour (_T("CADET BLUE"));
490 dc
.DrawArc ( 75,125, 110, 40, 75, 75 );
492 wP
.SetColour (_T("SALMON"));
494 dc
.SetBrush(*wxRED_BRUSH
);
495 //top left corner, width and height, start and end angle
496 // 315 same center and x-radius as last pie-arc, half Y radius
497 dc
.DrawEllipticArc(25,50,100,50,180.0,45.0) ;
503 dc
.SetBrush (wxBrush (_T("SALMON"),wxSOLID
)) ;
504 dc
.DrawEllipticArc(300, 0,200,100, 0.0,145.0) ;
506 dc
.DrawEllipticArc(300, 50,200,100,90.0,145.0) ;
507 dc
.DrawEllipticArc(300,100,200,100,90.0,345.0) ;
510 s
= wxT("This is an arc test page");
511 #endif // wxUSE_STATUSBAR
515 dc
.DrawCheckMark ( 30,30,25,25);
516 dc
.SetBrush (wxBrush (_T("SALMON"),wxTRANSPARENT
));
517 dc
.DrawCheckMark ( 80,50,75,75);
518 dc
.DrawRectangle ( 80,50,75,75);
520 s
= wxT("Two check marks");
521 #endif // wxUSE_STATUSBAR
525 wF
= wxFont ( 18, wxROMAN
, wxITALIC
, wxBOLD
, false, wxT("Times New Roman"));
527 dc
.DrawLine(0, 0, 200, 200);
528 dc
.DrawLine(200, 0, 0, 200);
529 dc
.DrawText(wxT("This is an 18pt string"), 50, 60);
531 // rescale and draw in blue
534 dc
.SetUserScale (2.0,0.5);
535 dc
.SetDeviceOrigin(200,0);
536 dc
.DrawLine(0, 0, 200, 200);
537 dc
.DrawLine(200, 0, 0, 200);
538 dc
.DrawText(wxT("This is an 18pt string 2 x 0.5 UserScaled"), 50, 60);
539 dc
.SetUserScale (2.0,2.0);
540 dc
.SetDeviceOrigin(200,200);
541 dc
.DrawText(wxT("This is an 18pt string 2 x 2 UserScaled"), 50, 60);
545 dc
.SetUserScale (1.0,1.0);
546 dc
.SetDeviceOrigin(0,10);
547 dc
.SetMapMode (wxMM_METRIC
) ; //svg ignores this
548 dc
.DrawLine(0, 0, 200, 200);
549 dc
.DrawLine(200, 0, 0, 200);
550 dc
.DrawText(wxT("This is an 18pt string in MapMode"), 50, 60);
552 s
= wxT("Scaling test page");
553 #endif // wxUSE_STATUSBAR
557 dc
.DrawIcon( wxIcon(mondrian_xpm
), 10, 10 );
558 dc
.DrawBitmap ( wxBitmap(svgbitmap_xpm
), 50,15);
560 s
= wxT("Icon and Bitmap ");
561 #endif // wxUSE_STATUSBAR
566 m_child
->SetStatusText(s
);
567 #endif // wxUSE_STATUSBAR
573 // ---------------------------------------------------------------------------
575 // ---------------------------------------------------------------------------
577 MyChild::MyChild(wxMDIParentFrame
*parent
, const wxString
& title
,
578 const wxPoint
& pos
, const wxSize
& size
,
580 : wxMDIChildFrame(parent
, -1, title
, pos
, size
, style
)
583 m_frame
= (MyFrame
*) parent
;
586 SetStatusText(title
);
587 #endif // wxUSE_STATUSBAR
590 GetClientSize ( &w
, &h
);
591 m_canvas
= new MyCanvas(this, wxPoint(0, 0), wxSize (w
,h
) );
592 // Give it scrollbars
593 m_canvas
->SetScrollbars(20, 20, 50, 50);
600 m_frame
->m_children
.DeleteObject(this);
604 void MyChild::OnQuit(wxCommandEvent
& WXUNUSED(event
))
610 bool MyChild::OnSave(wxString filename
)
612 wxSVGFileDC
svgDC (filename
, 600, 650) ;
613 m_canvas
->OnDraw (svgDC
);
619 void MyChild::OnActivate(wxActivateEvent
& event
)
621 if ( event
.GetActive() && m_canvas
)
622 m_canvas
->SetFocus();
626 void MyChild::OnClose(wxCloseEvent
& event
)