]> git.saurik.com Git - wxWidgets.git/blame - samples/animate/anitest.cpp
New animate sample described.
[wxWidgets.git] / samples / animate / anitest.cpp
CommitLineData
72045d57
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: anitest.cpp
3// Purpose: Animation sample
4// Author: Julian Smart
5// Modified by: Francesco Montorsi
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__
3652fd78 32 #include "sample.xpm"
72045d57
VZ
33#endif
34
35#include "anitest.h"
36
37IMPLEMENT_APP(MyApp)
38
39// ---------------------------------------------------------------------------
40// global variables
41// ---------------------------------------------------------------------------
42
43// ---------------------------------------------------------------------------
44// event tables
45// ---------------------------------------------------------------------------
46
47enum
48{
c3d6e0aa 49 ID_PLAY = 1
72045d57
VZ
50};
51
52BEGIN_EVENT_TABLE(MyFrame, wxFrame)
c3d6e0aa 53 EVT_MENU(ID_PLAY, MyFrame::OnPlay)
72045d57
VZ
54 EVT_MENU(wxID_STOP, MyFrame::OnStop)
55 EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
56 EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
57#if wxUSE_FILEDLG
58 EVT_MENU(wxID_OPEN, MyFrame::OnOpen)
59#endif // wxUSE_FILEDLG
60
61 EVT_SIZE(MyFrame::OnSize)
62 EVT_UPDATE_UI(wxID_ANY, MyFrame::OnUpdateUI)
63END_EVENT_TABLE()
64
65// ===========================================================================
66// implementation
67// ===========================================================================
68
69// ---------------------------------------------------------------------------
70// MyApp
71// ---------------------------------------------------------------------------
72
73// Initialise this in OnInit, not statically
74bool MyApp::OnInit()
75{
76 // Create the main frame window
77
78 MyFrame* frame = new MyFrame((wxFrame *)NULL, -1, _T("Animation Demo"),
79 wxPoint(-1, -1), wxSize(500, 400),
80 wxDEFAULT_FRAME_STYLE);
81
82 // Give it an icon
73565531 83 frame->SetIcon(wxICON(sample));
72045d57
VZ
84
85 // Make a menubar
86 wxMenu *file_menu = new wxMenu;
87
88#if wxUSE_FILEDLG
89 file_menu->Append(wxID_OPEN, _T("&Open Animation...\tCtrl+O"), _T("Loads an animation"));
90#endif // wxUSE_FILEDLG
91 file_menu->Append(wxID_EXIT);
92
93 wxMenu *play_menu = new wxMenu;
c3d6e0aa 94 play_menu->Append(ID_PLAY, _T("Play\tCtrl+P"), _T("Play the animation"));
72045d57
VZ
95 play_menu->Append(wxID_STOP, _T("Stop\tCtrl+P"), _T("Stop the animation"));
96
97 wxMenu *help_menu = new wxMenu;
98 help_menu->Append(wxID_ABOUT);
99
100 wxMenuBar *menu_bar = new wxMenuBar;
101
102 menu_bar->Append(file_menu, _T("&File"));
103 menu_bar->Append(play_menu, _T("&Animation"));
104 menu_bar->Append(help_menu, _T("&Help"));
105
106 // Associate the menu bar with the frame
107 frame->SetMenuBar(menu_bar);
108
109#if wxUSE_STATUSBAR
110 frame->CreateStatusBar();
111#endif // wxUSE_STATUSBAR
112
113 frame->Show(true);
114
115 SetTopWindow(frame);
116
117 return true;
118}
119
120// ---------------------------------------------------------------------------
121// MyFrame
122// ---------------------------------------------------------------------------
123
124#include "wx/wfstream.h"
125
126// Define my frame constructor
127MyFrame::MyFrame(wxWindow *parent,
128 const wxWindowID id,
129 const wxString& title,
130 const wxPoint& pos,
131 const wxSize& size,
132 const long style)
133 : wxFrame(parent, id, title, pos, size,
134 style | wxNO_FULL_REPAINT_ON_RESIZE)
135{
136 //m_canvas = new MyCanvas(this, wxPoint(0, 0), wxDefaultSize);
137
138 //wxSizer *sz = new wxBoxSizer(wxVERTICAL);
139
140 m_animationCtrl = new wxAnimationCtrl(this, wxID_ANY, wxNullAnimation,
141 wxPoint(0,0),wxSize(100,100));
142 if (m_animationCtrl->LoadFile(wxT("throbber.gif")))
143 m_animationCtrl->Play();
144
145 //sz->Add(m_animationCtrl, 1, wxGROW);
146
147 //SetSizer(sz);
148}
149
150MyFrame::~MyFrame()
151{
152}
153
154void MyFrame::OnPlay(wxCommandEvent& WXUNUSED(event))
155{
156 if (!m_animationCtrl->Play())
157 wxLogError(wxT("Invalid animation"));
158}
159
160void MyFrame::OnStop(wxCommandEvent& WXUNUSED(event))
161{
162 m_animationCtrl->Stop();
163}
164
165void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
166{
167 Close();
168}
169
170void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
171{
172 /*
173
174 FIXME: on wxGTK at least using File->About command it shows
175 the message dialog but does not focus it
176
177 */
178
179 (void)wxMessageBox(_T("wxWidgets 2 Animation Demo\n")
180 _T("Author: Julian Smart (c) 2001\n"),
181 _T("About Animation Demo"));
182}
183
184#if wxUSE_FILEDLG
185void MyFrame::OnOpen(wxCommandEvent& WXUNUSED(event))
186{
187 wxFileDialog dialog(this, _T("Please choose an animation"),
188 wxEmptyString, wxEmptyString, wxT("*.gif;*.ani"), wxFD_OPEN);
189 if (dialog.ShowModal() == wxID_OK)
190 {
191 wxString filename(dialog.GetPath());
192
193 // enable one of the two chunk of codes to test different parts of wxAnimation/wxAnimationCtrl
194#if 0
195 if (m_animationCtrl->LoadFile(filename))
196 m_animationCtrl->Play();
197 else
198 wxMessageBox(_T("Sorry, this animation is not a valid format for wxAnimation."));
199#else
200 #if 0
201 wxAnimation temp;
202 if (!temp.LoadFile(filename))
203 {
204 wxLogError(wxT("Sorry, this animation is not a valid format for wxAnimation."));
205 return;
206 }
207
208 m_animationCtrl->SetAnimation(temp);
209 m_animationCtrl->Play();
210 #else
211 wxFileInputStream stream(filename);
212 if (!stream.Ok())
213 {
214 wxLogError(wxT("Sorry, this animation is not a valid format for wxAnimation."));
215 return;
216 }
217
218 wxAnimation temp;
219 if (!temp.Load(stream))
220 {
221 wxLogError(wxT("Sorry, this animation is not a valid format for wxAnimation."));
222 return;
223 }
224
225 m_animationCtrl->SetAnimation(temp);
226 m_animationCtrl->Play();
227 #endif
228#endif
229 }
230}
231#endif // wxUSE_FILEDLG
232
233void MyFrame::OnUpdateUI(wxUpdateUIEvent& WXUNUSED(event) )
234{
235 GetMenuBar()->FindItem(wxID_STOP)->Enable(m_animationCtrl->IsPlaying());
c3d6e0aa 236 GetMenuBar()->FindItem(ID_PLAY)->Enable(!m_animationCtrl->IsPlaying());
72045d57
VZ
237}
238
239// ---------------------------------------------------------------------------
240// MyCanvas
241// ---------------------------------------------------------------------------
242
243BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
244 //EVT_PAINT(MyCanvas::OnPaint)
245END_EVENT_TABLE()
246
247// Define a constructor for my canvas
248MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size)
249 : wxScrolledWindow(parent, -1, pos, size,
250 wxSUNKEN_BORDER |
251 wxNO_FULL_REPAINT_ON_RESIZE |
252 wxVSCROLL | wxHSCROLL)
253{
254 SetBackgroundColour(wxColour(_T("YELLOW")));
255}
256
257void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(event))
258{
259 //wxPaintDC dc(this);
260
261 //dc.DrawRotatedText(wxT("Background"),
262}