revise the sample, simplifying and cleaning it where possible

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@59522 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Francesco Montorsi
2009-03-14 01:01:12 +00:00
parent 76e2b5703a
commit 49e3b82c03

View File

@@ -29,6 +29,7 @@
#include "wx/toolbar.h" #include "wx/toolbar.h"
#include "wx/dcsvg.h" #include "wx/dcsvg.h"
#include "wx/vector.h"
#include "mondrian.xpm" #include "mondrian.xpm"
@@ -38,22 +39,21 @@
#include "SVGlogo24.xpm" #include "SVGlogo24.xpm"
class MyChild; class MyChild;
class MyCanvas;
// ---------------------------------------------------------------------------
// classes
// ---------------------------------------------------------------------------
// Define a new application
class MyApp : public wxApp class MyApp : public wxApp
{ {
public: public:
bool OnInit(); bool OnInit();
}; };
// Define a new frame
class MyFrame : public wxMDIParentFrame class MyFrame : public wxMDIParentFrame
{ {
public: public:
int nWinCreated;
wxList m_children;
MyFrame(wxWindow *parent, const wxWindowID id, const wxString& title, MyFrame(wxWindow *parent, const wxWindowID id, const wxString& title,
const wxPoint& pos, const wxSize& size, const long style); const wxPoint& pos, const wxSize& size, const long style);
@@ -63,21 +63,13 @@ class MyFrame : public wxMDIParentFrame
void OnAbout(wxCommandEvent& event); void OnAbout(wxCommandEvent& event);
void OnNewWindow(wxCommandEvent& event); void OnNewWindow(wxCommandEvent& event);
void OnQuit(wxCommandEvent& event); void OnQuit(wxCommandEvent& event);
void OnClose(wxCloseEvent& event); void FileSavePicture (wxCommandEvent& event);
void FileSavePicture (wxCommandEvent & WXUNUSED(event) ) ;
DECLARE_EVENT_TABLE() unsigned int GetCountOfChildren() const
}; { return m_nWinCreated; }
private:
class MyCanvas : public wxScrolledWindow unsigned int m_nWinCreated;
{
public:
int m_index ;
MyChild * m_child ;
MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size);
virtual void OnDraw(wxDC& dc);
DECLARE_EVENT_TABLE() DECLARE_EVENT_TABLE()
}; };
@@ -85,22 +77,43 @@ class MyCanvas : public wxScrolledWindow
class MyChild: public wxMDIChildFrame class MyChild: public wxMDIChildFrame
{ {
public: public:
MyCanvas *m_canvas; MyChild(wxMDIParentFrame *parent, const wxString& title,
MyFrame *m_frame ; const wxPoint& pos = wxDefaultPosition,
const wxSize& size = wxDefaultSize,
//////////////////// Methods const long style = wxDEFAULT_FRAME_STYLE);
MyChild(wxMDIParentFrame *parent, const wxString& title, const wxPoint& pos, const wxSize& size, const long style);
~MyChild(); ~MyChild();
void OnActivate(wxActivateEvent& event); void OnActivate(wxActivateEvent& event);
void OnQuit(wxCommandEvent& event); void OnQuit(wxCommandEvent& event);
void OnClose(wxCloseEvent& event);
bool OnSave(wxString filename); bool OnSave(wxString filename);
MyFrame* GetFrame()
{ return m_frame; }
private:
MyCanvas *m_canvas;
MyFrame *m_frame;
DECLARE_EVENT_TABLE() DECLARE_EVENT_TABLE()
}; };
class MyCanvas : public wxScrolledWindow
{
public:
MyCanvas(MyChild *parent, const wxPoint& pos, const wxSize& size);
virtual void OnDraw(wxDC& dc);
private:
int m_index;
MyChild* m_child;
DECLARE_EVENT_TABLE()
};
// ---------------------------------------------------------------------------
// constants
// ---------------------------------------------------------------------------
// menu items ids // menu items ids
enum enum
{ {
@@ -112,15 +125,6 @@ enum
MDI_ABOUT MDI_ABOUT
}; };
IMPLEMENT_APP(MyApp)
// ---------------------------------------------------------------------------
// global variables
// ---------------------------------------------------------------------------
MyFrame *frame = (MyFrame *) NULL;
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// event tables // event tables
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@@ -130,7 +134,6 @@ EVT_MENU(MDI_ABOUT, MyFrame::OnAbout)
EVT_MENU(MDI_NEW_WINDOW, MyFrame::OnNewWindow) EVT_MENU(MDI_NEW_WINDOW, MyFrame::OnNewWindow)
EVT_MENU(MDI_QUIT, MyFrame::OnQuit) EVT_MENU(MDI_QUIT, MyFrame::OnQuit)
EVT_MENU (MDI_SAVE, MyFrame::FileSavePicture) EVT_MENU (MDI_SAVE, MyFrame::FileSavePicture)
EVT_CLOSE(MyFrame::OnClose)
EVT_SIZE(MyFrame::OnSize) EVT_SIZE(MyFrame::OnSize)
END_EVENT_TABLE() END_EVENT_TABLE()
@@ -143,37 +146,16 @@ END_EVENT_TABLE()
// MyApp // MyApp
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Initialise this in OnInit, not statically IMPLEMENT_APP(MyApp)
bool MyApp::OnInit() bool MyApp::OnInit()
{ {
// Create the main frame window // Create the main frame window
frame = new MyFrame((wxFrame *)NULL, -1, wxT("SVG Demo"), MyFrame* frame = new MyFrame((wxFrame *)NULL, -1, wxT("SVG Demo"),
wxPoint(-1, -1), wxSize(500, 400), wxDefaultPosition, wxSize(500, 400),
wxDEFAULT_FRAME_STYLE | wxHSCROLL | wxVSCROLL); wxDEFAULT_FRAME_STYLE | wxHSCROLL | wxVSCROLL);
// Make a menubar
wxMenu *file_menu = new wxMenu;
file_menu->Append(MDI_NEW_WINDOW, wxT("&New test\tCtrl+N"));
file_menu->Append(MDI_QUIT, wxT("&Exit\tAlt+X"));
wxMenu *help_menu = new wxMenu;
help_menu->Append(MDI_ABOUT, wxT("&About"));
wxMenuBar *menu_bar = new wxMenuBar;
menu_bar->Append(file_menu, wxT("&File"));
menu_bar->Append(help_menu, wxT("&Help"));
// Associate the menu bar with the frame
frame->SetMenuBar(menu_bar);
#if wxUSE_STATUSBAR
frame->CreateStatusBar();
#endif // wxUSE_STATUSBAR
frame->Show(true); frame->Show(true);
SetTopWindow(frame); SetTopWindow(frame);
@@ -191,80 +173,58 @@ MyFrame::MyFrame(wxWindow *parent, const wxWindowID id, const wxString& title,
const wxPoint& pos, const wxSize& size, const long style) const wxPoint& pos, const wxSize& size, const long style)
: wxMDIParentFrame(parent, id, title, pos, size, style) : wxMDIParentFrame(parent, id, title, pos, size, style)
{ {
nWinCreated = 0 ; m_nWinCreated = 0;
// Give it an icon
SetIcon(wxICON(mondrian)); SetIcon(wxICON(mondrian));
// Make a menubar
wxMenu *file_menu = new wxMenu;
file_menu->Append(MDI_NEW_WINDOW, wxT("&New test\tCtrl+N"));
file_menu->Append(MDI_QUIT, wxT("&Exit\tAlt+X"));
wxMenu *help_menu = new wxMenu;
help_menu->Append(MDI_ABOUT, wxT("&About"));
wxMenuBar *menu_bar = new wxMenuBar;
menu_bar->Append(file_menu, wxT("&File"));
menu_bar->Append(help_menu, wxT("&Help"));
// Associate the menu bar with the frame
SetMenuBar(menu_bar);
#if wxUSE_STATUSBAR
CreateStatusBar();
#endif // wxUSE_STATUSBAR
CreateToolBar(wxNO_BORDER | wxTB_FLAT | wxTB_HORIZONTAL); CreateToolBar(wxNO_BORDER | wxTB_FLAT | wxTB_HORIZONTAL);
InitToolBar(GetToolBar()); InitToolBar(GetToolBar());
} }
void MyFrame::OnClose(wxCloseEvent& event)
{
if ( !event.CanVeto() )
{
event.Skip();
return ;
}
if ( m_children.GetCount () < 1 )
{
event.Skip();
return ;
}
// now try the children
wxObjectList::compatibility_iterator pNode = m_children.GetFirst ();
wxObjectList::compatibility_iterator pNext ;
MyChild * pChild ;
while ( pNode )
{
pNext = pNode -> GetNext ();
pChild = (MyChild*) pNode -> GetData ();
if (pChild -> Close ())
{
m_children.Erase(pNode) ;
}
else
{
event.Veto();
return;
}
pNode = pNext ;
}
event.Skip();
}
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{ {
Close(); Close();
} }
void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) ) void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
{ {
(void)wxMessageBox(wxT("wxWidgets 2.0 SVG 1.0 Test\n") (void)wxMessageBox(wxT("wxWidgets SVG sample\n")
wxT("Author: Chris Elliott (c) 2002\n") wxT("Author: Chris Elliott (c) 2002-2009\n")
wxT("Usage: svg.exe \nClick File | New to show tests\n\n"), wxT("About SVG Test")); wxT("Usage: click File|New to show tests"),
wxT("About SVG Test"));
} }
void MyFrame::OnNewWindow(wxCommandEvent& WXUNUSED(event) ) void MyFrame::OnNewWindow(wxCommandEvent& WXUNUSED(event) )
{ {
// Make another frame, containing a canvas // Make another frame, containing a canvas
MyChild *subframe ; MyChild *subframe = new MyChild(this, wxT("SVG Frame"));
m_children.Append (new MyChild(frame, wxT("SVG Frame"),
wxPoint(-1, -1), wxSize(-1, -1),
wxDEFAULT_FRAME_STYLE ) ) ;
subframe = (MyChild *) m_children.GetLast() -> GetData ();
wxString title; wxString title;
title.Printf(wxT("SVG Test Window %d"), nWinCreated ); title.Printf(wxT("SVG Test Window %d"), m_nWinCreated );
// counts number of children previously, even if now closed // counts number of children previously, even if now closed
nWinCreated ++ ; m_nWinCreated ++;
// Give it a title and icon // Give it a title and icon
subframe->SetTitle(title); subframe->SetTitle(title);
@@ -292,7 +252,6 @@ void MyFrame::OnNewWindow(wxCommandEvent& WXUNUSED(event) )
subframe->Show(true); subframe->Show(true);
} }
void MyFrame::OnSize(wxSizeEvent& event) void MyFrame::OnSize(wxSizeEvent& event)
{ {
int w, h; int w, h;
@@ -302,7 +261,6 @@ void MyFrame::OnSize(wxSizeEvent& event)
event.Skip(); event.Skip();
} }
void MyFrame::InitToolBar(wxToolBar* toolBar) void MyFrame::InitToolBar(wxToolBar* toolBar)
{ {
const int maxBitmaps = 3; const int maxBitmaps = 3;
@@ -324,7 +282,6 @@ void MyFrame::InitToolBar(wxToolBar* toolBar)
delete bitmaps[i]; delete bitmaps[i];
} }
void MyFrame::FileSavePicture (wxCommandEvent & WXUNUSED(event) ) void MyFrame::FileSavePicture (wxCommandEvent & WXUNUSED(event) )
{ {
#if wxUSE_FILEDLG #if wxUSE_FILEDLG
@@ -350,32 +307,22 @@ void MyFrame::FileSavePicture (wxCommandEvent & WXUNUSED(event) )
} }
// Note that MDI_NEW_WINDOW and MDI_ABOUT commands get passed
// to the parent window for processing, so no need to
// duplicate event handlers here.
BEGIN_EVENT_TABLE(MyChild, wxMDIChildFrame)
EVT_MENU(MDI_CHILD_QUIT, MyChild::OnQuit)
EVT_CLOSE(MyChild::OnClose)
END_EVENT_TABLE()
BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
END_EVENT_TABLE()
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// MyCanvas // MyCanvas
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Define a constructor for my canvas BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size) END_EVENT_TABLE()
: wxScrolledWindow(parent, -1, pos, size,
wxSUNKEN_BORDER|wxVSCROLL|wxHSCROLL)
{
m_child = (MyChild *) parent ;
SetBackgroundColour(wxColour(_T("WHITE")));
m_index = m_child->m_frame->nWinCreated % 7 ;
}
// Define a constructor for my canvas
MyCanvas::MyCanvas(MyChild *parent, const wxPoint& pos, const wxSize& size)
: wxScrolledWindow(parent, wxID_ANY, pos, size, wxSUNKEN_BORDER|wxVSCROLL|wxHSCROLL)
{
SetBackgroundColour(wxColour(_T("WHITE")));
m_child = parent;
m_index = m_child->GetFrame()->GetCountOfChildren() % 7;
}
// Define the repainting behaviour // Define the repainting behaviour
void MyCanvas::OnDraw(wxDC& dc) void MyCanvas::OnDraw(wxDC& dc)
@@ -393,7 +340,6 @@ void MyCanvas::OnDraw(wxDC& dc)
dc.SetFont(*wxSWISS_FONT); dc.SetFont(*wxSWISS_FONT);
dc.SetPen(*wxGREEN_PEN); dc.SetPen(*wxGREEN_PEN);
switch (m_index) switch (m_index)
{ {
default: default:
@@ -554,45 +500,44 @@ void MyCanvas::OnDraw(wxDC& dc)
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// MyChild // MyChild
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Note that MDI_NEW_WINDOW and MDI_ABOUT commands get passed
// to the parent window for processing, so no need to
// duplicate event handlers here.
BEGIN_EVENT_TABLE(MyChild, wxMDIChildFrame)
EVT_MENU(MDI_CHILD_QUIT, MyChild::OnQuit)
END_EVENT_TABLE()
MyChild::MyChild(wxMDIParentFrame *parent, const wxString& title, MyChild::MyChild(wxMDIParentFrame *parent, const wxString& title,
const wxPoint& pos, const wxSize& size, const wxPoint& pos, const wxSize& size,
const long style) const long style)
: wxMDIChildFrame(parent, -1, title, pos, size, style) : wxMDIChildFrame(parent, wxID_ANY, title, pos, size, style)
{ {
m_frame = (MyFrame *) parent; m_frame = (MyFrame *) parent;
#if wxUSE_STATUSBAR #if wxUSE_STATUSBAR
CreateStatusBar(); CreateStatusBar();
SetStatusText(title); SetStatusText(title);
#endif // wxUSE_STATUSBAR #endif // wxUSE_STATUSBAR
int w, h ; m_canvas = new MyCanvas(this, wxPoint(0, 0), GetClientSize());
GetClientSize ( &w, &h );
m_canvas = new MyCanvas(this, wxPoint(0, 0), wxSize (w,h) );
// Give it scrollbars // Give it scrollbars
m_canvas->SetScrollbars(20, 20, 50, 50); m_canvas->SetScrollbars(20, 20, 50, 50);
} }
MyChild::~MyChild() MyChild::~MyChild()
{ {
m_frame->m_children.DeleteObject(this);
} }
void MyChild::OnQuit(wxCommandEvent& WXUNUSED(event)) void MyChild::OnQuit(wxCommandEvent& WXUNUSED(event))
{ {
Close(true); Close(true);
} }
bool MyChild::OnSave(wxString filename) bool MyChild::OnSave(wxString filename)
{ {
wxSVGFileDC svgDC (filename, 600, 650); wxSVGFileDC svgDC (filename, 600, 650);
@@ -600,16 +545,9 @@ bool MyChild::OnSave(wxString filename)
return svgDC.IsOk(); return svgDC.IsOk();
} }
void MyChild::OnActivate(wxActivateEvent& event) void MyChild::OnActivate(wxActivateEvent& event)
{ {
if ( event.GetActive() && m_canvas ) if ( event.GetActive() && m_canvas )
m_canvas->SetFocus(); m_canvas->SetFocus();
} }
void MyChild::OnClose(wxCloseEvent& event)
{
event.Skip();
}