]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/animate/anitest.cpp
Use wxRefCounter instead of wxObjecrDataRef
[wxWidgets.git] / samples / animate / anitest.cpp
index b15193b813ce8b4521c72c69b8c545350505c6cc..f775d03f671d4354185594861d7b19e5c0530811 100644 (file)
     #include "sample.xpm"
 #endif
 
+#include "wx/aboutdlg.h"
+#include "wx/artprov.h"
+#include "wx/colordlg.h"
+#include "wx/wfstream.h"
+
 #include "anitest.h"
 
+#if !wxUSE_ANIMATIONCTRL
+    #error Cannot compile this sample if wxAnimationCtrl is not enabled
+#endif
+
+
 IMPLEMENT_APP(MyApp)
 
 // ---------------------------------------------------------------------------
@@ -46,11 +56,20 @@ IMPLEMENT_APP(MyApp)
 
 enum
 {
-    ID_PLAY = 1
+    ID_PLAY = 1,
+    ID_SET_NULL_ANIMATION,
+    ID_SET_INACTIVE_BITMAP,
+    ID_SET_NO_AUTO_RESIZE,
+    ID_SET_BGCOLOR
 };
 
 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
     EVT_MENU(ID_PLAY, MyFrame::OnPlay)
+    EVT_MENU(ID_SET_NULL_ANIMATION, MyFrame::OnSetNullAnimation)
+    EVT_MENU(ID_SET_INACTIVE_BITMAP, MyFrame::OnSetInactiveBitmap)
+    EVT_MENU(ID_SET_NO_AUTO_RESIZE, MyFrame::OnSetNoAutoResize)
+    EVT_MENU(ID_SET_BGCOLOR, MyFrame::OnSetBgColor)
+
     EVT_MENU(wxID_STOP, MyFrame::OnStop)
     EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
     EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
@@ -73,12 +92,36 @@ END_EVENT_TABLE()
 // Initialise this in OnInit, not statically
 bool MyApp::OnInit()
 {
+    if ( !wxApp::OnInit() )
+        return false;
+
     // Create the main frame window
 
-    MyFrame* frame = new MyFrame((wxFrame *)NULL, wxID_ANY, _T("Animation Demo"));
+    MyFrame* frame = new MyFrame((wxFrame *)NULL, wxID_ANY, _T("Animation Demo"),
+                                 wxDefaultPosition, wxSize(500, 400),
+                                 wxDEFAULT_FRAME_STYLE);
+    frame->Show(true);
+
+    SetTopWindow(frame);
+
+    return true;
+}
+
+// ---------------------------------------------------------------------------
+// MyFrame
+// ---------------------------------------------------------------------------
 
-    // Give it an icon
-    frame->SetIcon(wxICON(sample));
+// Define my frame constructor
+MyFrame::MyFrame(wxWindow *parent,
+                 const wxWindowID id,
+                 const wxString& title,
+                 const wxPoint& pos,
+                 const wxSize& size,
+                 const long style)
+       : wxFrame(parent, id, title, pos, size,
+                          style | wxNO_FULL_REPAINT_ON_RESIZE)
+{
+    SetIcon(wxICON(sample));
 
     // Make a menubar
     wxMenu *file_menu = new wxMenu;
@@ -91,6 +134,15 @@ bool MyApp::OnInit()
     wxMenu *play_menu = new wxMenu;
     play_menu->Append(ID_PLAY, _T("Play\tCtrl+P"), _T("Play the animation"));
     play_menu->Append(wxID_STOP, _T("Stop\tCtrl+P"), _T("Stop the animation"));
+    play_menu->AppendSeparator();
+    play_menu->Append(ID_SET_NULL_ANIMATION, _T("Set null animation"),
+                      _T("Sets the empty animation in the control"));
+    play_menu->AppendCheckItem(ID_SET_INACTIVE_BITMAP, _T("Set inactive bitmap"),
+                               _T("Sets an inactive bitmap for the control"));
+    play_menu->AppendCheckItem(ID_SET_NO_AUTO_RESIZE, _T("Set no autoresize"),
+                               _T("Tells the control not to resize automatically"));
+    play_menu->Append(ID_SET_BGCOLOR, _T("Set background colour..."),
+                      _T("Sets the background colour of the control"));
 
     wxMenu *help_menu = new wxMenu;
     help_menu->Append(wxID_ABOUT);
@@ -101,48 +153,26 @@ bool MyApp::OnInit()
     menu_bar->Append(play_menu, _T("&Animation"));
     menu_bar->Append(help_menu, _T("&Help"));
 
-    // Associate the menu bar with the frame
-    frame->SetMenuBar(menu_bar);
+    // Associate the menu bar with this frame
+    SetMenuBar(menu_bar);
 
 #if wxUSE_STATUSBAR
-    frame->CreateStatusBar();
+    CreateStatusBar();
 #endif // wxUSE_STATUSBAR
 
-    frame->Show(true);
-
-    SetTopWindow(frame);
-
-    return true;
-}
-
-// ---------------------------------------------------------------------------
-// MyFrame
-// ---------------------------------------------------------------------------
-
-#include "wx/wfstream.h"
+    // use a wxBoxSizer otherwise wxFrame will automatically
+    // resize the m_animationCtrl to fill its client area on
+    // user resizes
+    wxSizer *sz = new wxBoxSizer(wxVERTICAL);
+    sz->Add(new wxStaticText(this, wxID_ANY, wxT("wxAnimationCtrl:")),
+            wxSizerFlags().Centre().Border());
 
-// Define my frame constructor
-MyFrame::MyFrame(wxWindow *parent,
-                 const wxWindowID id,
-                 const wxString& title,
-                 const wxPoint& pos,
-                 const wxSize& size,
-                 const long style)
-       : wxFrame(parent, id, title, pos, size,
-                          style | wxNO_FULL_REPAINT_ON_RESIZE)
-{
-    //m_canvas = new MyCanvas(this, wxPoint(0, 0), wxDefaultSize);
-
-    //wxSizer *sz = new wxBoxSizer(wxVERTICAL);
-
-    m_animationCtrl = new wxAnimationCtrl(this, wxID_ANY, wxNullAnimation,
-                                          wxPoint(0,0),wxSize(100,100));
+    m_animationCtrl = new wxAnimationCtrl(this, wxID_ANY);
     if (m_animationCtrl->LoadFile(wxT("throbber.gif")))
         m_animationCtrl->Play();
 
-    //sz->Add(m_animationCtrl, 1, wxGROW);
-
-    //SetSizer(sz);
+    sz->Add(m_animationCtrl, wxSizerFlags().Centre().Border());
+    SetSizer(sz);
 }
 
 MyFrame::~MyFrame()
@@ -160,6 +190,62 @@ void MyFrame::OnStop(wxCommandEvent& WXUNUSED(event))
     m_animationCtrl->Stop();
 }
 
+void MyFrame::OnSetNullAnimation(wxCommandEvent& WXUNUSED(event))
+{
+    m_animationCtrl->SetAnimation(wxNullAnimation);
+}
+
+void MyFrame::OnSetInactiveBitmap(wxCommandEvent& event)
+{
+    if (event.IsChecked())
+    {
+        // set a dummy bitmap as the inactive bitmap
+        wxBitmap bmp = wxArtProvider::GetBitmap(wxART_MISSING_IMAGE);
+        m_animationCtrl->SetInactiveBitmap(bmp);
+    }
+    else
+        m_animationCtrl->SetInactiveBitmap(wxNullBitmap);
+}
+
+void MyFrame::OnSetNoAutoResize(wxCommandEvent& event)
+{
+    // recreate the control with the new flag if necessary
+    long style = wxAC_DEFAULT_STYLE |
+                    (event.IsChecked() ? wxAC_NO_AUTORESIZE : 0);
+
+    if (style != m_animationCtrl->GetWindowStyle())
+    {
+        // save status of the control before destroying it
+        wxAnimation curr = m_animationCtrl->GetAnimation();
+        wxBitmap inactive = m_animationCtrl->GetInactiveBitmap();
+        wxColour bg = m_animationCtrl->GetBackgroundColour();
+
+        // destroy & rebuild
+        wxAnimationCtrl *old = m_animationCtrl;
+        m_animationCtrl = new wxAnimationCtrl(this, wxID_ANY, curr,
+                                              wxDefaultPosition, wxDefaultSize,
+                                              style);
+
+        GetSizer()->Replace(old, m_animationCtrl);
+        delete old;
+
+        // load old status in new control
+        m_animationCtrl->SetInactiveBitmap(inactive);
+        m_animationCtrl->SetBackgroundColour(bg);
+
+        GetSizer()->Layout();
+    }
+}
+
+void MyFrame::OnSetBgColor(wxCommandEvent& WXUNUSED(event))
+{
+    wxColour clr = wxGetColourFromUser(this, m_animationCtrl->GetBackgroundColour(),
+                                       wxT("Choose the background colour"));
+
+    if (clr.IsOk())
+        m_animationCtrl->SetBackgroundColour(clr);
+}
+
 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
 {
     Close();
@@ -167,16 +253,16 @@ void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
 
 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
 {
-    /*
-
-    FIXME: on wxGTK at least using File->About command it shows
-    the message dialog but does not focus it
+    wxAboutDialogInfo info;
+    info.SetName(_("wxAnimationCtrl and wxAnimation sample"));
+    info.SetDescription(_("This sample program demonstrates the usage of wxAnimationCtrl"));
+    info.SetCopyright(_T("(C) 2006 Julian Smart"));
 
-    */
+    info.AddDeveloper(_T("Julian Smart"));
+    info.AddDeveloper(_T("Guillermo Rodriguez Garcia"));
+    info.AddDeveloper(_T("Francesco Montorsi"));
 
-    (void)wxMessageBox(_T("wxWidgets 2 Animation Demo\n")
-                       _T("Author: Julian Smart (c) 2001\n"),
-                       _T("About Animation Demo"));
+    wxAboutBox(info);
 }
 
 #if wxUSE_FILEDLG
@@ -224,6 +310,8 @@ void MyFrame::OnOpen(wxCommandEvent& WXUNUSED(event))
         m_animationCtrl->Play();
     #endif
 #endif
+
+        GetSizer()->Layout();
     }
 }
 #endif // wxUSE_FILEDLG
@@ -232,29 +320,6 @@ void MyFrame::OnUpdateUI(wxUpdateUIEvent& WXUNUSED(event) )
 {
     GetMenuBar()->FindItem(wxID_STOP)->Enable(m_animationCtrl->IsPlaying());
     GetMenuBar()->FindItem(ID_PLAY)->Enable(!m_animationCtrl->IsPlaying());
+    GetMenuBar()->FindItem(ID_SET_NO_AUTO_RESIZE)->Enable(!m_animationCtrl->IsPlaying());
 }
 
-// ---------------------------------------------------------------------------
-// MyCanvas
-// ---------------------------------------------------------------------------
-
-BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
-    //EVT_PAINT(MyCanvas::OnPaint)
-END_EVENT_TABLE()
-
-// Define a constructor for my canvas
-MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size)
-        : wxScrolledWindow(parent, wxID_ANY, pos, size,
-                           wxSUNKEN_BORDER |
-                           wxNO_FULL_REPAINT_ON_RESIZE |
-                           wxVSCROLL | wxHSCROLL)
-{
-    SetBackgroundColour(wxColour(_T("YELLOW")));
-}
-
-void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(event))
-{
-    //wxPaintDC dc(this);
-
-    //dc.DrawRotatedText(wxT("Background"),
-}