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"
33 #include "mondrian.xpm"
35 #include "bitmaps/new.xpm"
36 #include "bitmaps/save.xpm"
37 #include "bitmaps/help.xpm"
38 #include "SVGlogo24.xpm"
42 // Define a new application
43 class MyApp
: public wxApp
50 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 OnClose(wxCloseEvent
& event
);
67 void FileSavePicture (wxCommandEvent
& WXUNUSED(event
) ) ;
73 class MyCanvas
: public wxScrolledWindow
79 MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
);
80 virtual void OnDraw(wxDC
& dc
);
85 class MyChild
: public wxMDIChildFrame
91 //////////////////// Methods
93 MyChild(wxMDIParentFrame
*parent
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
, const long style
);
96 void OnActivate(wxActivateEvent
& event
);
97 void OnQuit(wxCommandEvent
& event
);
98 void OnClose(wxCloseEvent
& event
);
99 bool OnSave(wxString filename
) ;
101 DECLARE_EVENT_TABLE()
118 // ---------------------------------------------------------------------------
120 // ---------------------------------------------------------------------------
122 MyFrame
*frame
= (MyFrame
*) NULL
;
124 // ---------------------------------------------------------------------------
126 // ---------------------------------------------------------------------------
128 BEGIN_EVENT_TABLE(MyFrame
, wxMDIParentFrame
)
129 EVT_MENU(MDI_ABOUT
, MyFrame::OnAbout
)
130 EVT_MENU(MDI_NEW_WINDOW
, MyFrame::OnNewWindow
)
131 EVT_MENU(MDI_QUIT
, MyFrame::OnQuit
)
132 EVT_MENU (MDI_SAVE
, MyFrame::FileSavePicture
)
133 EVT_CLOSE(MyFrame::OnClose
)
135 EVT_SIZE(MyFrame::OnSize
)
138 // ===========================================================================
140 // ===========================================================================
142 // ---------------------------------------------------------------------------
144 // ---------------------------------------------------------------------------
146 // Initialise this in OnInit, not statically
149 // Create the main frame window
151 frame
= new MyFrame((wxFrame
*)NULL
, -1, wxT("SVG Demo"),
152 wxPoint(-1, -1), wxSize(500, 400),
153 wxDEFAULT_FRAME_STYLE
| wxHSCROLL
| wxVSCROLL
);
157 wxMenu
*file_menu
= new wxMenu
;
159 file_menu
->Append(MDI_NEW_WINDOW
, wxT("&New test\tCtrl+N"));
160 file_menu
->Append(MDI_QUIT
, wxT("&Exit\tAlt+X"));
162 wxMenu
*help_menu
= new wxMenu
;
163 help_menu
->Append(MDI_ABOUT
, wxT("&About"));
165 wxMenuBar
*menu_bar
= new wxMenuBar
;
167 menu_bar
->Append(file_menu
, wxT("&File"));
168 menu_bar
->Append(help_menu
, wxT("&Help"));
170 // Associate the menu bar with the frame
171 frame
->SetMenuBar(menu_bar
);
174 frame
->CreateStatusBar();
175 #endif // wxUSE_STATUSBAR
185 // ---------------------------------------------------------------------------
187 // ---------------------------------------------------------------------------
189 // Define my frame constructor
190 MyFrame::MyFrame(wxWindow
*parent
, const wxWindowID id
, const wxString
& title
,
191 const wxPoint
& pos
, const wxSize
& size
, const long style
)
192 : wxMDIParentFrame(parent
, id
, title
, pos
, size
, style
)
197 SetIcon(wxICON(mondrian
));
199 CreateToolBar(wxNO_BORDER
| wxTB_FLAT
| wxTB_HORIZONTAL
);
200 InitToolBar(GetToolBar());
205 void MyFrame::OnClose(wxCloseEvent
& event
)
207 if ( !event
.CanVeto() )
212 if ( m_children
.GetCount () < 1 )
217 // now try the children
218 wxObjectList::compatibility_iterator pNode
= m_children
.GetFirst ();
219 wxObjectList::compatibility_iterator pNext
;
223 pNext
= pNode
-> GetNext ();
224 pChild
= (MyChild
*) pNode
-> GetData ();
225 if (pChild
-> Close ())
227 m_children
.Erase(pNode
) ;
240 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
246 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
248 (void)wxMessageBox(wxT("wxWidgets 2.0 SVG 1.0 Test\n")
249 wxT("Author: Chris Elliott (c) 2002\n")
250 wxT("Usage: svg.exe \nClick File | New to show tests\n\n"), wxT("About SVG Test"));
254 void MyFrame::OnNewWindow(wxCommandEvent
& WXUNUSED(event
) )
256 // Make another frame, containing a canvas
259 m_children
.Append (new MyChild(frame
, wxT("SVG Frame"),
260 wxPoint(-1, -1), wxSize(-1, -1),
261 wxDEFAULT_FRAME_STYLE
) ) ;
263 subframe
= (MyChild
*) m_children
.GetLast() -> GetData ();
265 title
.Printf(wxT("SVG Test Window %d"), nWinCreated
);
266 // counts number of children previously, even if now closed
269 // Give it a title and icon
270 subframe
->SetTitle(title
);
271 subframe
->SetIcon(wxICON(mondrian
));
274 wxMenu
*file_menu
= new wxMenu
;
276 file_menu
->Append(MDI_NEW_WINDOW
, wxT("&Another test\tCtrl+N"));
277 file_menu
->Append(MDI_SAVE
, wxT("&Save\tCtrl+S"), wxT("Save in SVG format"));
278 file_menu
->Append(MDI_CHILD_QUIT
, wxT("&Close child\tCtrl+F4"));
279 file_menu
->Append(MDI_QUIT
, wxT("&Exit\tAlt+X"));
281 wxMenu
*help_menu
= new wxMenu
;
282 help_menu
->Append(MDI_ABOUT
, wxT("&About"));
284 wxMenuBar
*menu_bar
= new wxMenuBar
;
286 menu_bar
->Append(file_menu
, wxT("&File"));
287 menu_bar
->Append(help_menu
, wxT("&Help"));
289 // Associate the menu bar with the frame
290 subframe
->SetMenuBar(menu_bar
);
292 subframe
->Show(true);
296 void MyFrame::OnSize(wxSizeEvent
& event
)
299 GetClientSize(&w
, &h
);
301 GetClientWindow()->SetSize(0, 0, w
, h
);
306 void MyFrame::InitToolBar(wxToolBar
* toolBar
)
308 const int maxBitmaps
= 3 ;
309 wxBitmap
* bitmaps
[maxBitmaps
];
311 bitmaps
[0] = new wxBitmap( new_xpm
);
312 bitmaps
[1] = new wxBitmap( save_xpm
);
313 bitmaps
[2] = new wxBitmap( help_xpm
);
318 toolBar
->AddTool( MDI_NEW_WINDOW
, *(bitmaps
[0]), wxNullBitmap
, false, currentX
, wxDefaultCoord
, (wxObject
*) NULL
, wxT("New SVG test window"));
319 currentX
+= width
+ 5;
320 toolBar
->AddTool( MDI_SAVE
, *bitmaps
[1], wxNullBitmap
, false, currentX
, wxDefaultCoord
, (wxObject
*) NULL
, wxT("Save test in SVG format"));
321 currentX
+= width
+ 5;
322 toolBar
->AddSeparator();
323 toolBar
->AddTool(MDI_ABOUT
, *bitmaps
[2], wxNullBitmap
, false, currentX
, wxDefaultCoord
, (wxObject
*) NULL
, wxT("Help"));
328 for (i
= 0; i
< maxBitmaps
; i
++)
333 void MyFrame::FileSavePicture (wxCommandEvent
& WXUNUSED(event
) )
336 MyChild
* pChild
= (MyChild
*)GetActiveChild ();
342 wxFileDialog
dialog(this, wxT("Save Picture as"), wxEmptyString
, pChild
->GetTitle(),
343 wxT("SVG vector picture files (*.svg)|*.svg"),
344 wxFD_SAVE
|wxFD_OVERWRITE_PROMPT
);
346 if (dialog
.ShowModal() == wxID_OK
)
348 if (!pChild
-> OnSave ( dialog
.GetPath() ))
354 #endif // wxUSE_FILEDLG
358 // Note that MDI_NEW_WINDOW and MDI_ABOUT commands get passed
359 // to the parent window for processing, so no need to
360 // duplicate event handlers here.
361 BEGIN_EVENT_TABLE(MyChild
, wxMDIChildFrame
)
362 EVT_MENU(MDI_CHILD_QUIT
, MyChild::OnQuit
)
363 EVT_CLOSE(MyChild::OnClose
)
366 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
370 // ---------------------------------------------------------------------------
372 // ---------------------------------------------------------------------------
374 // Define a constructor for my canvas
375 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
)
376 : wxScrolledWindow(parent
, -1, pos
, size
,
377 wxSUNKEN_BORDER
|wxVSCROLL
|wxHSCROLL
)
379 m_child
= (MyChild
*) parent
;
380 SetBackgroundColour(wxColour(_T("WHITE")));
381 m_index
= m_child
->m_frame
->nWinCreated
% 7 ;
385 // Define the repainting behaviour
386 void MyCanvas::OnDraw(wxDC
& dc
)
391 #endif // wxUSE_STATUSBAR
398 dc
.SetFont(*wxSWISS_FONT
);
399 dc
.SetPen(*wxGREEN_PEN
);
406 // draw lines to make a cross
407 dc
.DrawLine(0, 0, 200, 200);
408 dc
.DrawLine(200, 0, 0, 200);
409 // draw point colored line and spline
414 dc
.DrawPoint (25,15) ;
415 dc
.DrawLine(50, 30, 200, 30);
416 dc
.DrawSpline(50, 200, 50, 100, 200, 10);
418 s
= wxT("Green Cross, Cyan Line and spline");
419 #endif // wxUSE_STATUSBAR
423 // draw standard shapes
424 dc
.SetBrush(*wxCYAN_BRUSH
);
425 dc
.SetPen(*wxRED_PEN
);
426 dc
.DrawRectangle(10, 10, 100, 70);
427 wB
= wxBrush (_T("DARK ORCHID"), wxTRANSPARENT
);
429 dc
.DrawRoundedRectangle(50, 50, 100, 70, 20);
430 dc
.SetBrush (wxBrush(_T("GOLDENROD"), wxSOLID
) );
431 dc
.DrawEllipse(100, 100, 100, 50);
433 points
[0].x
= 100; points
[0].y
= 200;
434 points
[1].x
= 70; points
[1].y
= 260;
435 points
[2].x
= 160; points
[2].y
= 230;
436 points
[3].x
= 40; points
[3].y
= 230;
437 points
[4].x
= 130; points
[4].y
= 260;
438 points
[5].x
= 100; points
[5].y
= 200;
440 dc
.DrawPolygon(5, points
);
441 dc
.DrawLines (6, points
, 160);
443 s
= wxT("Blue rectangle, red edge, clear rounded rectangle, gold ellipse, gold and clear stars");
444 #endif // wxUSE_STATUSBAR
448 // draw text in Arial or similar font
449 dc
.DrawLine(50,25,50,35);
450 dc
.DrawLine(45,30,55,30);
451 dc
.DrawText(wxT("This is a Swiss-style string"), 50, 30);
452 wC
= dc
.GetTextForeground() ;
453 dc
.SetTextForeground (_T("FIREBRICK"));
455 // no effect in msw ??
456 dc
.SetTextBackground (_T("WHEAT"));
457 dc
.DrawText(wxT("This is a Red string"), 50, 200);
458 dc
.DrawRotatedText(wxT("This is a 45 deg string"), 50, 200, 45);
459 dc
.DrawRotatedText(wxT("This is a 90 deg string"), 50, 200, 90);
460 wF
= wxFont ( 18, wxROMAN
, wxITALIC
, wxBOLD
, false, wxT("Times New Roman"));
462 dc
.SetTextForeground (wC
) ;
463 dc
.DrawText(wxT("This is a Times-style string"), 50, 60);
465 s
= wxT("Swiss, Times text; red text, rotated and colored orange");
466 #endif // wxUSE_STATUSBAR
470 // four arcs start and end points, center
471 dc
.SetBrush(*wxGREEN_BRUSH
);
472 dc
.DrawArc ( 200,300, 370,230, 300,300 );
473 dc
.SetBrush(*wxBLUE_BRUSH
);
474 dc
.DrawArc ( 270-50, 270-86, 270-86, 270-50, 270,270 );
475 dc
.SetDeviceOrigin(-10,-10);
476 dc
.DrawArc ( 270-50, 270-86, 270-86, 270-50, 270,270 );
477 dc
.SetDeviceOrigin(0,0);
479 wP
.SetColour (_T("CADET BLUE"));
481 dc
.DrawArc ( 75,125, 110, 40, 75, 75 );
483 wP
.SetColour (_T("SALMON"));
485 dc
.SetBrush(*wxRED_BRUSH
);
486 //top left corner, width and height, start and end angle
487 // 315 same center and x-radius as last pie-arc, half Y radius
488 dc
.DrawEllipticArc(25,50,100,50,180.0,45.0) ;
494 dc
.SetBrush (wxBrush (_T("SALMON"),wxSOLID
)) ;
495 dc
.DrawEllipticArc(300, 0,200,100, 0.0,145.0) ;
497 dc
.DrawEllipticArc(300, 50,200,100,90.0,145.0) ;
498 dc
.DrawEllipticArc(300,100,200,100,90.0,345.0) ;
501 s
= wxT("This is an arc test page");
502 #endif // wxUSE_STATUSBAR
506 dc
.DrawCheckMark ( 30,30,25,25);
507 dc
.SetBrush (wxBrush (_T("SALMON"),wxTRANSPARENT
));
508 dc
.DrawCheckMark ( 80,50,75,75);
509 dc
.DrawRectangle ( 80,50,75,75);
511 s
= wxT("Two check marks");
512 #endif // wxUSE_STATUSBAR
516 wF
= wxFont ( 18, wxROMAN
, wxITALIC
, wxBOLD
, false, wxT("Times New Roman"));
518 dc
.DrawLine(0, 0, 200, 200);
519 dc
.DrawLine(200, 0, 0, 200);
520 dc
.DrawText(wxT("This is an 18pt string"), 50, 60);
522 // rescale and draw in blue
525 dc
.SetUserScale (2.0,0.5);
526 dc
.SetDeviceOrigin(200,0);
527 dc
.DrawLine(0, 0, 200, 200);
528 dc
.DrawLine(200, 0, 0, 200);
529 dc
.DrawText(wxT("This is an 18pt string 2 x 0.5 UserScaled"), 50, 60);
530 dc
.SetUserScale (2.0,2.0);
531 dc
.SetDeviceOrigin(200,200);
532 dc
.DrawText(wxT("This is an 18pt string 2 x 2 UserScaled"), 50, 60);
536 dc
.SetUserScale (1.0,1.0);
537 dc
.SetDeviceOrigin(0,10);
538 dc
.SetMapMode (wxMM_METRIC
) ; //svg ignores this
539 dc
.DrawLine(0, 0, 200, 200);
540 dc
.DrawLine(200, 0, 0, 200);
541 dc
.DrawText(wxT("This is an 18pt string in MapMode"), 50, 60);
543 s
= wxT("Scaling test page");
544 #endif // wxUSE_STATUSBAR
548 dc
.DrawIcon( wxIcon(mondrian_xpm
), 10, 10 );
549 dc
.DrawBitmap ( wxBitmap(svgbitmap_xpm
), 50,15);
551 s
= wxT("Icon and Bitmap ");
552 #endif // wxUSE_STATUSBAR
557 m_child
->SetStatusText(s
);
558 #endif // wxUSE_STATUSBAR
564 // ---------------------------------------------------------------------------
566 // ---------------------------------------------------------------------------
568 MyChild::MyChild(wxMDIParentFrame
*parent
, const wxString
& title
,
569 const wxPoint
& pos
, const wxSize
& size
,
571 : wxMDIChildFrame(parent
, -1, title
, pos
, size
, style
)
574 m_frame
= (MyFrame
*) parent
;
577 SetStatusText(title
);
578 #endif // wxUSE_STATUSBAR
581 GetClientSize ( &w
, &h
);
582 m_canvas
= new MyCanvas(this, wxPoint(0, 0), wxSize (w
,h
) );
583 // Give it scrollbars
584 m_canvas
->SetScrollbars(20, 20, 50, 50);
591 m_frame
->m_children
.DeleteObject(this);
595 void MyChild::OnQuit(wxCommandEvent
& WXUNUSED(event
))
601 bool MyChild::OnSave(wxString filename
)
603 wxSVGFileDC
svgDC (filename
, 600, 650) ;
604 m_canvas
->OnDraw (svgDC
);
610 void MyChild::OnActivate(wxActivateEvent
& event
)
612 if ( event
.GetActive() && m_canvas
)
613 m_canvas
->SetFocus();
617 void MyChild::OnClose(wxCloseEvent
& event
)