commented out/unused code removed; use wxAboutDialog (part of patch 1586730)
[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 "sample.xpm"
33 #endif
34
35 #include "wx/aboutdlg.h"
36 #include "anitest.h"
37
38 #if !wxUSE_ANIMATIONCTRL
39 #error Cannot compile this sample if wxAnimationCtrl is not enabled
40 #endif
41
42
43 IMPLEMENT_APP(MyApp)
44
45 // ---------------------------------------------------------------------------
46 // global variables
47 // ---------------------------------------------------------------------------
48
49 // ---------------------------------------------------------------------------
50 // event tables
51 // ---------------------------------------------------------------------------
52
53 enum
54 {
55 ID_PLAY = 1
56 };
57
58 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
59 EVT_MENU(ID_PLAY, MyFrame::OnPlay)
60 EVT_MENU(wxID_STOP, MyFrame::OnStop)
61 EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
62 EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
63 #if wxUSE_FILEDLG
64 EVT_MENU(wxID_OPEN, MyFrame::OnOpen)
65 #endif // wxUSE_FILEDLG
66
67 EVT_SIZE(MyFrame::OnSize)
68 EVT_UPDATE_UI(wxID_ANY, MyFrame::OnUpdateUI)
69 END_EVENT_TABLE()
70
71 // ===========================================================================
72 // implementation
73 // ===========================================================================
74
75 // ---------------------------------------------------------------------------
76 // MyApp
77 // ---------------------------------------------------------------------------
78
79 // Initialise this in OnInit, not statically
80 bool MyApp::OnInit()
81 {
82 // Create the main frame window
83
84 MyFrame* frame = new MyFrame((wxFrame *)NULL, wxID_ANY, _T("Animation Demo"),
85 wxDefaultPosition, wxSize(500, 400),
86 wxDEFAULT_FRAME_STYLE);
87
88 // Give it an icon
89 frame->SetIcon(wxICON(sample));
90
91 // Make a menubar
92 wxMenu *file_menu = new wxMenu;
93
94 #if wxUSE_FILEDLG
95 file_menu->Append(wxID_OPEN, _T("&Open Animation...\tCtrl+O"), _T("Loads an animation"));
96 #endif // wxUSE_FILEDLG
97 file_menu->Append(wxID_EXIT);
98
99 wxMenu *play_menu = new wxMenu;
100 play_menu->Append(ID_PLAY, _T("Play\tCtrl+P"), _T("Play the animation"));
101 play_menu->Append(wxID_STOP, _T("Stop\tCtrl+P"), _T("Stop the animation"));
102
103 wxMenu *help_menu = new wxMenu;
104 help_menu->Append(wxID_ABOUT);
105
106 wxMenuBar *menu_bar = new wxMenuBar;
107
108 menu_bar->Append(file_menu, _T("&File"));
109 menu_bar->Append(play_menu, _T("&Animation"));
110 menu_bar->Append(help_menu, _T("&Help"));
111
112 // Associate the menu bar with the frame
113 frame->SetMenuBar(menu_bar);
114
115 #if wxUSE_STATUSBAR
116 frame->CreateStatusBar();
117 #endif // wxUSE_STATUSBAR
118
119 frame->Show(true);
120
121 SetTopWindow(frame);
122
123 return true;
124 }
125
126 // ---------------------------------------------------------------------------
127 // MyFrame
128 // ---------------------------------------------------------------------------
129
130 #include "wx/wfstream.h"
131
132 // Define my frame constructor
133 MyFrame::MyFrame(wxWindow *parent,
134 const wxWindowID id,
135 const wxString& title,
136 const wxPoint& pos,
137 const wxSize& size,
138 const long style)
139 : wxFrame(parent, id, title, pos, size,
140 style | wxNO_FULL_REPAINT_ON_RESIZE)
141 {
142 m_animationCtrl = new wxAnimationCtrl(this, wxID_ANY, wxNullAnimation,
143 wxPoint(0,0),wxSize(100,100));
144 if (m_animationCtrl->LoadFile(wxT("throbber.gif")))
145 m_animationCtrl->Play();
146 }
147
148 MyFrame::~MyFrame()
149 {
150 }
151
152 void MyFrame::OnPlay(wxCommandEvent& WXUNUSED(event))
153 {
154 if (!m_animationCtrl->Play())
155 wxLogError(wxT("Invalid animation"));
156 }
157
158 void MyFrame::OnStop(wxCommandEvent& WXUNUSED(event))
159 {
160 m_animationCtrl->Stop();
161 }
162
163 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
164 {
165 Close();
166 }
167
168 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
169 {
170 wxAboutDialogInfo info;
171 info.SetName(_("wxAnimationCtrl and wxAnimation sample"));
172 info.SetDescription(_("This sample program demonstrates the usage of wxAnimationCtrl"));
173 info.SetCopyright(_T("(C) 2006 Julian Smart"));
174
175 info.AddDeveloper(_T("Julian Smart"));
176 info.AddDeveloper(_T("Guillermo Rodriguez Garcia"));
177 info.AddDeveloper(_T("Francesco Montorsi"));
178
179 wxAboutBox(info);
180 }
181
182 #if wxUSE_FILEDLG
183 void MyFrame::OnOpen(wxCommandEvent& WXUNUSED(event))
184 {
185 wxFileDialog dialog(this, _T("Please choose an animation"),
186 wxEmptyString, wxEmptyString, wxT("*.gif;*.ani"), wxFD_OPEN);
187 if (dialog.ShowModal() == wxID_OK)
188 {
189 wxString filename(dialog.GetPath());
190
191 // enable one of the two chunk of codes to test different parts of wxAnimation/wxAnimationCtrl
192 #if 0
193 if (m_animationCtrl->LoadFile(filename))
194 m_animationCtrl->Play();
195 else
196 wxMessageBox(_T("Sorry, this animation is not a valid format for wxAnimation."));
197 #else
198 #if 0
199 wxAnimation temp;
200 if (!temp.LoadFile(filename))
201 {
202 wxLogError(wxT("Sorry, this animation is not a valid format for wxAnimation."));
203 return;
204 }
205
206 m_animationCtrl->SetAnimation(temp);
207 m_animationCtrl->Play();
208 #else
209 wxFileInputStream stream(filename);
210 if (!stream.Ok())
211 {
212 wxLogError(wxT("Sorry, this animation is not a valid format for wxAnimation."));
213 return;
214 }
215
216 wxAnimation temp;
217 if (!temp.Load(stream))
218 {
219 wxLogError(wxT("Sorry, this animation is not a valid format for wxAnimation."));
220 return;
221 }
222
223 m_animationCtrl->SetAnimation(temp);
224 m_animationCtrl->Play();
225 #endif
226 #endif
227 }
228 }
229 #endif // wxUSE_FILEDLG
230
231 void MyFrame::OnUpdateUI(wxUpdateUIEvent& WXUNUSED(event) )
232 {
233 GetMenuBar()->FindItem(wxID_STOP)->Enable(m_animationCtrl->IsPlaying());
234 GetMenuBar()->FindItem(ID_PLAY)->Enable(!m_animationCtrl->IsPlaying());
235 }
236