anitest.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: anitest.cpp
  3. // Purpose: Animation sample
  4. // Author: Julian Smart
  5. // Modified by: Francesco Montorsi
  6. // Created: 02/07/2001
  7. // Copyright: (c) Julian Smart
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. // ===========================================================================
  11. // declarations
  12. // ===========================================================================
  13. // ---------------------------------------------------------------------------
  14. // headers
  15. // ---------------------------------------------------------------------------
  16. // For compilers that support precompilation, includes "wx/wx.h".
  17. #include "wx/wxprec.h"
  18. #ifdef __BORLANDC__
  19. #pragma hdrstop
  20. #endif
  21. #ifndef WX_PRECOMP
  22. #include "wx/wx.h"
  23. #endif
  24. #ifndef wxHAS_IMAGES_IN_RESOURCES
  25. #include "../sample.xpm"
  26. #endif
  27. #include "wx/aboutdlg.h"
  28. #include "wx/artprov.h"
  29. #include "wx/colordlg.h"
  30. #include "wx/wfstream.h"
  31. #include "anitest.h"
  32. #if !wxUSE_ANIMATIONCTRL
  33. #error Cannot compile this sample if wxAnimationCtrl is not enabled
  34. #endif
  35. IMPLEMENT_APP(MyApp)
  36. // ---------------------------------------------------------------------------
  37. // global variables
  38. // ---------------------------------------------------------------------------
  39. // ---------------------------------------------------------------------------
  40. // event tables
  41. // ---------------------------------------------------------------------------
  42. enum
  43. {
  44. ID_PLAY = 1,
  45. ID_SET_NULL_ANIMATION,
  46. ID_SET_INACTIVE_BITMAP,
  47. ID_SET_NO_AUTO_RESIZE,
  48. ID_SET_BGCOLOR
  49. };
  50. wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
  51. EVT_MENU(ID_PLAY, MyFrame::OnPlay)
  52. EVT_MENU(ID_SET_NULL_ANIMATION, MyFrame::OnSetNullAnimation)
  53. EVT_MENU(ID_SET_INACTIVE_BITMAP, MyFrame::OnSetInactiveBitmap)
  54. EVT_MENU(ID_SET_NO_AUTO_RESIZE, MyFrame::OnSetNoAutoResize)
  55. EVT_MENU(ID_SET_BGCOLOR, MyFrame::OnSetBgColor)
  56. EVT_MENU(wxID_STOP, MyFrame::OnStop)
  57. EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
  58. EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
  59. #if wxUSE_FILEDLG
  60. EVT_MENU(wxID_OPEN, MyFrame::OnOpen)
  61. #endif // wxUSE_FILEDLG
  62. EVT_SIZE(MyFrame::OnSize)
  63. EVT_UPDATE_UI(wxID_ANY, MyFrame::OnUpdateUI)
  64. wxEND_EVENT_TABLE()
  65. // ===========================================================================
  66. // implementation
  67. // ===========================================================================
  68. // ---------------------------------------------------------------------------
  69. // MyApp
  70. // ---------------------------------------------------------------------------
  71. // Initialise this in OnInit, not statically
  72. bool MyApp::OnInit()
  73. {
  74. if ( !wxApp::OnInit() )
  75. return false;
  76. // Create the main frame window
  77. MyFrame* frame = new MyFrame((wxFrame *)NULL, wxID_ANY, wxT("Animation Demo"),
  78. wxDefaultPosition, wxSize(500, 400),
  79. wxDEFAULT_FRAME_STYLE);
  80. frame->Show(true);
  81. return true;
  82. }
  83. // ---------------------------------------------------------------------------
  84. // MyFrame
  85. // ---------------------------------------------------------------------------
  86. // Define my frame constructor
  87. MyFrame::MyFrame(wxWindow *parent,
  88. const wxWindowID id,
  89. const wxString& title,
  90. const wxPoint& pos,
  91. const wxSize& size,
  92. const long style)
  93. : wxFrame(parent, id, title, pos, size,
  94. style | wxNO_FULL_REPAINT_ON_RESIZE)
  95. {
  96. SetIcon(wxICON(sample));
  97. // Make a menubar
  98. wxMenu *file_menu = new wxMenu;
  99. #if wxUSE_FILEDLG
  100. file_menu->Append(wxID_OPEN, wxT("&Open Animation...\tCtrl+O"), wxT("Loads an animation"));
  101. #endif // wxUSE_FILEDLG
  102. file_menu->Append(wxID_EXIT);
  103. wxMenu *play_menu = new wxMenu;
  104. play_menu->Append(ID_PLAY, wxT("Play\tCtrl+P"), wxT("Play the animation"));
  105. play_menu->Append(wxID_STOP, wxT("Stop\tCtrl+S"), wxT("Stop the animation"));
  106. play_menu->AppendSeparator();
  107. play_menu->Append(ID_SET_NULL_ANIMATION, wxT("Set null animation"),
  108. wxT("Sets the empty animation in the control"));
  109. play_menu->AppendCheckItem(ID_SET_INACTIVE_BITMAP, wxT("Set inactive bitmap"),
  110. wxT("Sets an inactive bitmap for the control"));
  111. play_menu->AppendCheckItem(ID_SET_NO_AUTO_RESIZE, wxT("Set no autoresize"),
  112. wxT("Tells the control not to resize automatically"));
  113. play_menu->Append(ID_SET_BGCOLOR, wxT("Set background colour..."),
  114. wxT("Sets the background colour of the control"));
  115. wxMenu *help_menu = new wxMenu;
  116. help_menu->Append(wxID_ABOUT);
  117. wxMenuBar *menu_bar = new wxMenuBar;
  118. menu_bar->Append(file_menu, wxT("&File"));
  119. menu_bar->Append(play_menu, wxT("&Animation"));
  120. menu_bar->Append(help_menu, wxT("&Help"));
  121. // Associate the menu bar with this frame
  122. SetMenuBar(menu_bar);
  123. #if wxUSE_STATUSBAR
  124. CreateStatusBar();
  125. #endif // wxUSE_STATUSBAR
  126. // use a wxBoxSizer otherwise wxFrame will automatically
  127. // resize the m_animationCtrl to fill its client area on
  128. // user resizes
  129. wxSizer *sz = new wxBoxSizer(wxVERTICAL);
  130. sz->Add(new wxStaticText(this, wxID_ANY, wxT("wxAnimationCtrl:")),
  131. wxSizerFlags().Centre().Border());
  132. m_animationCtrl = new wxAnimationCtrl(this, wxID_ANY);
  133. if (m_animationCtrl->LoadFile(wxT("throbber.gif")))
  134. m_animationCtrl->Play();
  135. sz->Add(m_animationCtrl, wxSizerFlags().Centre().Border());
  136. SetSizer(sz);
  137. }
  138. MyFrame::~MyFrame()
  139. {
  140. }
  141. void MyFrame::OnPlay(wxCommandEvent& WXUNUSED(event))
  142. {
  143. if (!m_animationCtrl->Play())
  144. {
  145. wxLogError(wxT("Invalid animation"));
  146. }
  147. }
  148. void MyFrame::OnStop(wxCommandEvent& WXUNUSED(event))
  149. {
  150. m_animationCtrl->Stop();
  151. }
  152. void MyFrame::OnSetNullAnimation(wxCommandEvent& WXUNUSED(event))
  153. {
  154. m_animationCtrl->SetAnimation(wxNullAnimation);
  155. }
  156. void MyFrame::OnSetInactiveBitmap(wxCommandEvent& event)
  157. {
  158. if (event.IsChecked())
  159. {
  160. // set a dummy bitmap as the inactive bitmap
  161. wxBitmap bmp = wxArtProvider::GetBitmap(wxART_MISSING_IMAGE);
  162. m_animationCtrl->SetInactiveBitmap(bmp);
  163. }
  164. else
  165. m_animationCtrl->SetInactiveBitmap(wxNullBitmap);
  166. }
  167. void MyFrame::OnSetNoAutoResize(wxCommandEvent& event)
  168. {
  169. // recreate the control with the new flag if necessary
  170. long style = wxAC_DEFAULT_STYLE |
  171. (event.IsChecked() ? wxAC_NO_AUTORESIZE : 0);
  172. if (style != m_animationCtrl->GetWindowStyle())
  173. {
  174. // save status of the control before destroying it
  175. wxAnimation curr = m_animationCtrl->GetAnimation();
  176. wxBitmap inactive = m_animationCtrl->GetInactiveBitmap();
  177. wxColour bg = m_animationCtrl->GetBackgroundColour();
  178. // destroy & rebuild
  179. wxAnimationCtrl *old = m_animationCtrl;
  180. m_animationCtrl = new wxAnimationCtrl(this, wxID_ANY, curr,
  181. wxDefaultPosition, wxDefaultSize,
  182. style);
  183. GetSizer()->Replace(old, m_animationCtrl);
  184. delete old;
  185. // load old status in new control
  186. m_animationCtrl->SetInactiveBitmap(inactive);
  187. m_animationCtrl->SetBackgroundColour(bg);
  188. GetSizer()->Layout();
  189. }
  190. }
  191. void MyFrame::OnSetBgColor(wxCommandEvent& WXUNUSED(event))
  192. {
  193. wxColour clr = wxGetColourFromUser(this, m_animationCtrl->GetBackgroundColour(),
  194. wxT("Choose the background colour"));
  195. if (clr.IsOk())
  196. m_animationCtrl->SetBackgroundColour(clr);
  197. }
  198. void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
  199. {
  200. Close();
  201. }
  202. void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
  203. {
  204. wxAboutDialogInfo info;
  205. info.SetName(_("wxAnimationCtrl and wxAnimation sample"));
  206. info.SetDescription(_("This sample program demonstrates the usage of wxAnimationCtrl"));
  207. info.SetCopyright(wxT("(C) 2006 Julian Smart"));
  208. info.AddDeveloper(wxT("Julian Smart"));
  209. info.AddDeveloper(wxT("Guillermo Rodriguez Garcia"));
  210. info.AddDeveloper(wxT("Francesco Montorsi"));
  211. wxAboutBox(info);
  212. }
  213. #if wxUSE_FILEDLG
  214. void MyFrame::OnOpen(wxCommandEvent& WXUNUSED(event))
  215. {
  216. wxFileDialog dialog(this, wxT("Please choose an animation"),
  217. wxEmptyString, wxEmptyString, wxT("*.gif;*.ani"), wxFD_OPEN);
  218. if (dialog.ShowModal() == wxID_OK)
  219. {
  220. wxString filename(dialog.GetPath());
  221. // enable one of the two chunk of codes to test different parts of wxAnimation/wxAnimationCtrl
  222. #if 0
  223. if (m_animationCtrl->LoadFile(filename))
  224. m_animationCtrl->Play();
  225. else
  226. wxMessageBox(wxT("Sorry, this animation is not a valid format for wxAnimation."));
  227. #else
  228. #if 0
  229. wxAnimation temp;
  230. if (!temp.LoadFile(filename))
  231. {
  232. wxLogError(wxT("Sorry, this animation is not a valid format for wxAnimation."));
  233. return;
  234. }
  235. m_animationCtrl->SetAnimation(temp);
  236. m_animationCtrl->Play();
  237. #else
  238. wxFileInputStream stream(filename);
  239. if (!stream.IsOk())
  240. {
  241. wxLogError(wxT("Sorry, this animation is not a valid format for wxAnimation."));
  242. return;
  243. }
  244. wxAnimation temp;
  245. if (!temp.Load(stream))
  246. {
  247. wxLogError(wxT("Sorry, this animation is not a valid format for wxAnimation."));
  248. return;
  249. }
  250. m_animationCtrl->SetAnimation(temp);
  251. m_animationCtrl->Play();
  252. #endif
  253. #endif
  254. GetSizer()->Layout();
  255. }
  256. }
  257. #endif // wxUSE_FILEDLG
  258. void MyFrame::OnUpdateUI(wxUpdateUIEvent& WXUNUSED(event) )
  259. {
  260. GetMenuBar()->FindItem(wxID_STOP)->Enable(m_animationCtrl->IsPlaying());
  261. GetMenuBar()->FindItem(ID_PLAY)->Enable(!m_animationCtrl->IsPlaying());
  262. GetMenuBar()->FindItem(ID_SET_NO_AUTO_RESIZE)->Enable(!m_animationCtrl->IsPlaying());
  263. }