1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Animation sample
4 // Author: Julian Smart
5 // Modified by: Francesco Montorsi
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
14 // ===========================================================================
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
31 #ifndef wxHAS_IMAGES_IN_RESOURCES
32 #include "../sample.xpm"
35 #include "wx/aboutdlg.h"
36 #include "wx/artprov.h"
37 #include "wx/colordlg.h"
38 #include "wx/wfstream.h"
42 #if !wxUSE_ANIMATIONCTRL
43 #error Cannot compile this sample if wxAnimationCtrl is not enabled
49 // ---------------------------------------------------------------------------
51 // ---------------------------------------------------------------------------
53 // ---------------------------------------------------------------------------
55 // ---------------------------------------------------------------------------
60 ID_SET_NULL_ANIMATION
,
61 ID_SET_INACTIVE_BITMAP
,
62 ID_SET_NO_AUTO_RESIZE
,
66 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
67 EVT_MENU(ID_PLAY
, MyFrame::OnPlay
)
68 EVT_MENU(ID_SET_NULL_ANIMATION
, MyFrame::OnSetNullAnimation
)
69 EVT_MENU(ID_SET_INACTIVE_BITMAP
, MyFrame::OnSetInactiveBitmap
)
70 EVT_MENU(ID_SET_NO_AUTO_RESIZE
, MyFrame::OnSetNoAutoResize
)
71 EVT_MENU(ID_SET_BGCOLOR
, MyFrame::OnSetBgColor
)
73 EVT_MENU(wxID_STOP
, MyFrame::OnStop
)
74 EVT_MENU(wxID_ABOUT
, MyFrame::OnAbout
)
75 EVT_MENU(wxID_EXIT
, MyFrame::OnQuit
)
77 EVT_MENU(wxID_OPEN
, MyFrame::OnOpen
)
78 #endif // wxUSE_FILEDLG
80 EVT_SIZE(MyFrame::OnSize
)
81 EVT_UPDATE_UI(wxID_ANY
, MyFrame::OnUpdateUI
)
84 // ===========================================================================
86 // ===========================================================================
88 // ---------------------------------------------------------------------------
90 // ---------------------------------------------------------------------------
92 // Initialise this in OnInit, not statically
95 if ( !wxApp::OnInit() )
98 // Create the main frame window
100 MyFrame
* frame
= new MyFrame((wxFrame
*)NULL
, wxID_ANY
, wxT("Animation Demo"),
101 wxDefaultPosition
, wxSize(500, 400),
102 wxDEFAULT_FRAME_STYLE
);
108 // ---------------------------------------------------------------------------
110 // ---------------------------------------------------------------------------
112 // Define my frame constructor
113 MyFrame::MyFrame(wxWindow
*parent
,
115 const wxString
& title
,
119 : wxFrame(parent
, id
, title
, pos
, size
,
120 style
| wxNO_FULL_REPAINT_ON_RESIZE
)
122 SetIcon(wxICON(sample
));
125 wxMenu
*file_menu
= new wxMenu
;
128 file_menu
->Append(wxID_OPEN
, wxT("&Open Animation...\tCtrl+O"), wxT("Loads an animation"));
129 #endif // wxUSE_FILEDLG
130 file_menu
->Append(wxID_EXIT
);
132 wxMenu
*play_menu
= new wxMenu
;
133 play_menu
->Append(ID_PLAY
, wxT("Play\tCtrl+P"), wxT("Play the animation"));
134 play_menu
->Append(wxID_STOP
, wxT("Stop\tCtrl+S"), wxT("Stop the animation"));
135 play_menu
->AppendSeparator();
136 play_menu
->Append(ID_SET_NULL_ANIMATION
, wxT("Set null animation"),
137 wxT("Sets the empty animation in the control"));
138 play_menu
->AppendCheckItem(ID_SET_INACTIVE_BITMAP
, wxT("Set inactive bitmap"),
139 wxT("Sets an inactive bitmap for the control"));
140 play_menu
->AppendCheckItem(ID_SET_NO_AUTO_RESIZE
, wxT("Set no autoresize"),
141 wxT("Tells the control not to resize automatically"));
142 play_menu
->Append(ID_SET_BGCOLOR
, wxT("Set background colour..."),
143 wxT("Sets the background colour of the control"));
145 wxMenu
*help_menu
= new wxMenu
;
146 help_menu
->Append(wxID_ABOUT
);
148 wxMenuBar
*menu_bar
= new wxMenuBar
;
150 menu_bar
->Append(file_menu
, wxT("&File"));
151 menu_bar
->Append(play_menu
, wxT("&Animation"));
152 menu_bar
->Append(help_menu
, wxT("&Help"));
154 // Associate the menu bar with this frame
155 SetMenuBar(menu_bar
);
159 #endif // wxUSE_STATUSBAR
161 // use a wxBoxSizer otherwise wxFrame will automatically
162 // resize the m_animationCtrl to fill its client area on
164 wxSizer
*sz
= new wxBoxSizer(wxVERTICAL
);
165 sz
->Add(new wxStaticText(this, wxID_ANY
, wxT("wxAnimationCtrl:")),
166 wxSizerFlags().Centre().Border());
168 m_animationCtrl
= new wxAnimationCtrl(this, wxID_ANY
);
169 if (m_animationCtrl
->LoadFile(wxT("throbber.gif")))
170 m_animationCtrl
->Play();
172 sz
->Add(m_animationCtrl
, wxSizerFlags().Centre().Border());
180 void MyFrame::OnPlay(wxCommandEvent
& WXUNUSED(event
))
182 if (!m_animationCtrl
->Play())
184 wxLogError(wxT("Invalid animation"));
188 void MyFrame::OnStop(wxCommandEvent
& WXUNUSED(event
))
190 m_animationCtrl
->Stop();
193 void MyFrame::OnSetNullAnimation(wxCommandEvent
& WXUNUSED(event
))
195 m_animationCtrl
->SetAnimation(wxNullAnimation
);
198 void MyFrame::OnSetInactiveBitmap(wxCommandEvent
& event
)
200 if (event
.IsChecked())
202 // set a dummy bitmap as the inactive bitmap
203 wxBitmap bmp
= wxArtProvider::GetBitmap(wxART_MISSING_IMAGE
);
204 m_animationCtrl
->SetInactiveBitmap(bmp
);
207 m_animationCtrl
->SetInactiveBitmap(wxNullBitmap
);
210 void MyFrame::OnSetNoAutoResize(wxCommandEvent
& event
)
212 // recreate the control with the new flag if necessary
213 long style
= wxAC_DEFAULT_STYLE
|
214 (event
.IsChecked() ? wxAC_NO_AUTORESIZE
: 0);
216 if (style
!= m_animationCtrl
->GetWindowStyle())
218 // save status of the control before destroying it
219 wxAnimation curr
= m_animationCtrl
->GetAnimation();
220 wxBitmap inactive
= m_animationCtrl
->GetInactiveBitmap();
221 wxColour bg
= m_animationCtrl
->GetBackgroundColour();
224 wxAnimationCtrl
*old
= m_animationCtrl
;
225 m_animationCtrl
= new wxAnimationCtrl(this, wxID_ANY
, curr
,
226 wxDefaultPosition
, wxDefaultSize
,
229 GetSizer()->Replace(old
, m_animationCtrl
);
232 // load old status in new control
233 m_animationCtrl
->SetInactiveBitmap(inactive
);
234 m_animationCtrl
->SetBackgroundColour(bg
);
236 GetSizer()->Layout();
240 void MyFrame::OnSetBgColor(wxCommandEvent
& WXUNUSED(event
))
242 wxColour clr
= wxGetColourFromUser(this, m_animationCtrl
->GetBackgroundColour(),
243 wxT("Choose the background colour"));
246 m_animationCtrl
->SetBackgroundColour(clr
);
249 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
254 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
256 wxAboutDialogInfo info
;
257 info
.SetName(_("wxAnimationCtrl and wxAnimation sample"));
258 info
.SetDescription(_("This sample program demonstrates the usage of wxAnimationCtrl"));
259 info
.SetCopyright(wxT("(C) 2006 Julian Smart"));
261 info
.AddDeveloper(wxT("Julian Smart"));
262 info
.AddDeveloper(wxT("Guillermo Rodriguez Garcia"));
263 info
.AddDeveloper(wxT("Francesco Montorsi"));
269 void MyFrame::OnOpen(wxCommandEvent
& WXUNUSED(event
))
271 wxFileDialog
dialog(this, wxT("Please choose an animation"),
272 wxEmptyString
, wxEmptyString
, wxT("*.gif;*.ani"), wxFD_OPEN
);
273 if (dialog
.ShowModal() == wxID_OK
)
275 wxString
filename(dialog
.GetPath());
277 // enable one of the two chunk of codes to test different parts of wxAnimation/wxAnimationCtrl
279 if (m_animationCtrl
->LoadFile(filename
))
280 m_animationCtrl
->Play();
282 wxMessageBox(wxT("Sorry, this animation is not a valid format for wxAnimation."));
286 if (!temp
.LoadFile(filename
))
288 wxLogError(wxT("Sorry, this animation is not a valid format for wxAnimation."));
292 m_animationCtrl
->SetAnimation(temp
);
293 m_animationCtrl
->Play();
295 wxFileInputStream
stream(filename
);
298 wxLogError(wxT("Sorry, this animation is not a valid format for wxAnimation."));
303 if (!temp
.Load(stream
))
305 wxLogError(wxT("Sorry, this animation is not a valid format for wxAnimation."));
309 m_animationCtrl
->SetAnimation(temp
);
310 m_animationCtrl
->Play();
314 GetSizer()->Layout();
317 #endif // wxUSE_FILEDLG
319 void MyFrame::OnUpdateUI(wxUpdateUIEvent
& WXUNUSED(event
) )
321 GetMenuBar()->FindItem(wxID_STOP
)->Enable(m_animationCtrl
->IsPlaying());
322 GetMenuBar()->FindItem(ID_PLAY
)->Enable(!m_animationCtrl
->IsPlaying());
323 GetMenuBar()->FindItem(ID_SET_NO_AUTO_RESIZE
)->Enable(!m_animationCtrl
->IsPlaying());