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