]> git.saurik.com Git - wxWidgets.git/blob - contrib/samples/animate/anitest.cpp
glibc's vswprintf doesn't nul terminate on truncation.
[wxWidgets.git] / contrib / samples / animate / anitest.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: anitest.cpp
3 // Purpose: Animation sample
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 02/07/2001
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ===========================================================================
13 // declarations
14 // ===========================================================================
15
16 // ---------------------------------------------------------------------------
17 // headers
18 // ---------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #ifndef WX_PRECOMP
28 #include "wx/wx.h"
29 #endif
30
31 #ifndef __WXMSW__
32 #include "mondrian.xpm"
33 #endif
34
35 #include "anitest.h"
36
37 IMPLEMENT_APP(MyApp)
38
39 // ---------------------------------------------------------------------------
40 // global variables
41 // ---------------------------------------------------------------------------
42
43 // ---------------------------------------------------------------------------
44 // event tables
45 // ---------------------------------------------------------------------------
46
47 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
48 EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
49 EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
50 #if wxUSE_FILEDLG
51 EVT_MENU(wxID_OPEN, MyFrame::OnOpen)
52 #endif // wxUSE_FILEDLG
53
54 EVT_SIZE(MyFrame::OnSize)
55 END_EVENT_TABLE()
56
57 // ===========================================================================
58 // implementation
59 // ===========================================================================
60
61 // ---------------------------------------------------------------------------
62 // MyApp
63 // ---------------------------------------------------------------------------
64
65 // Initialise this in OnInit, not statically
66 bool MyApp::OnInit()
67 {
68 // Create the main frame window
69
70 MyFrame* frame = new MyFrame((wxFrame *)NULL, -1, _T("Animation Demo"),
71 wxPoint(-1, -1), wxSize(500, 400),
72 wxDEFAULT_FRAME_STYLE);
73
74 // Give it an icon
75 #ifdef __WXMSW__
76 frame->SetIcon(wxIcon(_T("mdi_icn")));
77 #else
78 frame->SetIcon(wxIcon( mondrian_xpm ));
79 #endif
80
81 // Make a menubar
82 wxMenu *file_menu = new wxMenu;
83
84 #if wxUSE_FILEDLG
85 file_menu->Append(wxID_OPEN, _T("&Open Animation...\tCtrl+O"), _T("Open a GIF animation"));
86 #endif // wxUSE_FILEDLG
87 file_menu->Append(wxID_EXIT, _T("&Exit\tAlt+X"), _T("Quit the program"));
88
89 wxMenu *help_menu = new wxMenu;
90 help_menu->Append(wxID_ABOUT, _T("&About\tF1"));
91
92 wxMenuBar *menu_bar = new wxMenuBar;
93
94 menu_bar->Append(file_menu, _T("&File"));
95 menu_bar->Append(help_menu, _T("&Help"));
96
97 // Associate the menu bar with the frame
98 frame->SetMenuBar(menu_bar);
99
100 #if wxUSE_STATUSBAR
101 frame->CreateStatusBar();
102 #endif // wxUSE_STATUSBAR
103
104 frame->Show(true);
105
106 SetTopWindow(frame);
107
108 return true;
109 }
110
111 // ---------------------------------------------------------------------------
112 // MyFrame
113 // ---------------------------------------------------------------------------
114
115 // Define my frame constructor
116 MyFrame::MyFrame(wxWindow *parent,
117 const wxWindowID id,
118 const wxString& title,
119 const wxPoint& pos,
120 const wxSize& size,
121 const long style)
122 : wxFrame(parent, id, title, pos, size,
123 style | wxNO_FULL_REPAINT_ON_RESIZE)
124 {
125 // m_animation = NULL;
126 m_canvas = new MyCanvas(this, wxPoint(0, 0), wxDefaultSize);
127 #if 0
128 m_player.SetDestroyAnimation(false);
129 m_player.SetWindow(m_canvas);
130 m_player.SetPosition(wxPoint(0, 0));
131 #endif
132 m_animationCtrl = new wxGIFAnimationCtrl(m_canvas, -1, wxEmptyString,
133 wxPoint(0, 0), wxSize(200, 200));
134 }
135
136 MyFrame::~MyFrame()
137 {
138 // m_player.Stop();
139 }
140
141 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
142 {
143 Close();
144 }
145
146 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
147 {
148 (void)wxMessageBox(_T("wxWidgets 2 Animation Demo\n")
149 _T("Author: Julian Smart (c) 2001\n"),
150 _T("About Animation Demo"));
151 }
152
153 #if wxUSE_FILEDLG
154 void MyFrame::OnOpen(wxCommandEvent& WXUNUSED(event))
155 {
156 wxFileDialog dialog(this, _T("Please choose an animated GIF"),
157 wxEmptyString, wxEmptyString, wxT("*.gif"), wxFD_OPEN);
158 if (dialog.ShowModal() == wxID_OK)
159 {
160 wxString filename(dialog.GetPath());
161
162 m_animationCtrl->Stop();
163 if (m_animationCtrl->LoadFile(filename))
164 {
165 m_animationCtrl->Play();
166 }
167 else
168 {
169 wxMessageBox(_T("Sorry, this animation was not a valid animated GIF."));
170 }
171 }
172 }
173 #endif // wxUSE_FILEDLG
174
175
176 // ---------------------------------------------------------------------------
177 // MyCanvas
178 // ---------------------------------------------------------------------------
179
180 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
181 EVT_PAINT(MyCanvas::OnPaint)
182 END_EVENT_TABLE()
183
184 // Define a constructor for my canvas
185 MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size)
186 : wxScrolledWindow(parent, -1, pos, size,
187 wxSUNKEN_BORDER |
188 wxNO_FULL_REPAINT_ON_RESIZE |
189 wxVSCROLL | wxHSCROLL)
190 {
191 SetBackgroundColour(wxColour(_T("YELLOW")));
192 }
193
194 void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(event))
195 {
196 wxPaintDC dc(this);
197 #if 0
198 MyFrame* frame = (MyFrame*) GetParent();
199 if (frame->GetPlayer().IsPlaying())
200 {
201 frame->GetPlayer().Draw(dc);
202 }
203 #endif
204 }