home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / ANIMCTL.PAK / ANIMCTLX.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  5.1 KB  |  236 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (C) 1995, 1995 by Borland International, All Rights Reserved
  4. //
  5. // Illustrates usage of TAnimateCtl class
  6. //----------------------------------------------------------------------------
  7. #include <owl/pch.h>
  8. #include <owl/animctrl.h>
  9. #include "animctl.h"
  10.  
  11. //
  12. // class TSampleApp
  13. // ~~~~~ ~~~~~~~~~~
  14. class TSampleApp : public TApplication {
  15.   public:
  16.     void InitMainWindow();
  17. };
  18.  
  19. //
  20. // class TAnimDialog
  21. // ~~~~~ ~~~~~~~~~~~
  22. //
  23. class TAnimDialog : public TDialog {
  24.   public:
  25.     TAnimDialog(TWindow* parent, TResId res = TResId(IDD_ANIMATE));
  26.  
  27.     void            ButtonClicked(uint btnId);
  28.     void            ButtonEnabler(TCommandEnabler& ce);
  29.  
  30.   protected:
  31.     bool            Playing;
  32.     TAnimateCtrl*   AnimCtrl;
  33.     
  34.   DECLARE_RESPONSE_TABLE(TAnimDialog);
  35. };
  36.  
  37. //
  38. // Dialog's response table
  39. //
  40. DEFINE_RESPONSE_TABLE1(TAnimDialog, TDialog)
  41.   EV_COMMAND_AND_ID(IDC_REWIND, ButtonClicked),
  42.   EV_COMMAND_AND_ID(IDC_STOP, ButtonClicked),
  43.   EV_COMMAND_AND_ID(IDC_PLAY, ButtonClicked),
  44.   EV_COMMAND_AND_ID(IDC_FORWARD, ButtonClicked),
  45.   EV_COMMAND_ENABLE(IDC_REWIND, ButtonEnabler),
  46.   EV_COMMAND_ENABLE(IDC_STOP, ButtonEnabler),
  47.   EV_COMMAND_ENABLE(IDC_PLAY, ButtonEnabler),
  48.   EV_COMMAND_ENABLE(IDC_FORWARD, ButtonEnabler),
  49. END_RESPONSE_TABLE;
  50.  
  51. //
  52. // Constructor of animation dialog: Create AnimCtl object to alias control
  53. //
  54. TAnimDialog::TAnimDialog(TWindow* parent, TResId res) :
  55.              TDialog(parent, res), Playing(false)
  56. {
  57.   AnimCtrl = new TAnimateCtrl(this, IDC_ANIMATE);
  58. }
  59.  
  60. //
  61. // Responds to BN_CLICK notifications from buttons
  62. //
  63. void
  64. TAnimDialog::ButtonClicked(uint btnId)
  65. {
  66.   switch (btnId) {
  67.     case IDC_REWIND:  AnimCtrl->Seek(0);            
  68.                       break;
  69.  
  70.     case IDC_STOP:    AnimCtrl->Stop();             
  71.                       Playing = false;
  72.                       break;
  73.  
  74.     case IDC_PLAY:    AnimCtrl->Play(0, -1, -1);    
  75.                       Playing = true;
  76.                       break;
  77.  
  78.     case IDC_FORWARD: AnimCtrl->Seek(USHRT_MAX);   
  79.                       break;
  80.   }
  81. }
  82.  
  83. //
  84. // Responds to command enablers for buttons
  85. //
  86. void
  87. TAnimDialog::ButtonEnabler(TCommandEnabler& ce)
  88. {
  89.   switch (ce.GetId()) {
  90.     case IDC_STOP:    ce.Enable(Playing);   break;
  91.     case IDC_PLAY:    ce.Enable(!Playing);  break;
  92.     case IDC_REWIND:  ce.Enable(!Playing);  break;
  93.     case IDC_FORWARD: ce.Enable(!Playing);  break;
  94.   }
  95. }
  96.  
  97. //
  98. // class TClientWindow
  99. // ~~~~~ ~~~~~~~~~~~~~
  100. class TClientWindow : public TWindow {
  101.   public:
  102.     TClientWindow(TWindow* parent= 0);
  103.  
  104.   protected:
  105.  
  106.     // Message Handlers
  107.     //
  108.     void        EvSize(uint, TSize&);
  109.  
  110.     // Command handlers
  111.     //
  112.     void        CmAnimate();
  113.     void        CeAnimate(TCommandEnabler& ce);
  114.     void        CmStop();
  115.     void        CeStop(TCommandEnabler& ce);
  116.     void        CmDialog();
  117.  
  118.     // State info.
  119.     //
  120.     bool        Playing;
  121.  
  122.     TAnimateCtrl* AnimateCtrl;
  123.  
  124.   DECLARE_RESPONSE_TABLE(TClientWindow);
  125. };
  126.  
  127.  
  128. //
  129. // Response table of client window
  130. //
  131. DEFINE_RESPONSE_TABLE1(TClientWindow, TWindow)
  132.   EV_COMMAND(CM_ANIMATE, CmAnimate),
  133.   EV_COMMAND(CM_STOP, CmStop),
  134.   EV_COMMAND_ENABLE(CM_ANIMATE, CeAnimate),
  135.   EV_COMMAND_ENABLE(CM_STOP, CeStop),
  136.   EV_COMMAND(CM_DIALOG, CmDialog),
  137.   EV_WM_SIZE,
  138. END_RESPONSE_TABLE;
  139.  
  140. //
  141. // Client window: Contains an animation control
  142. //
  143. TClientWindow::TClientWindow(TWindow* parent) : TWindow(parent), 
  144.                                                 Playing(false)
  145. {
  146.   ModifyStyle(0, WS_CLIPSIBLINGS | WS_CLIPCHILDREN);
  147.   AnimateCtrl = new TAnimateCtrl(this, 0x100, 0, 0, 0, 0);
  148. };
  149.  
  150.  
  151. //
  152. // WM_SIZE Handler: Layout animation control within client window
  153. //
  154. void
  155. TClientWindow::EvSize(uint, TSize& size)
  156. {
  157.   if (AnimateCtrl && AnimateCtrl->IsWindow())
  158.     AnimateCtrl->MoveWindow(0, 0, size.cx, size.cy);
  159. }
  160.  
  161. //
  162. // Animate the in the client window
  163. //
  164. void
  165. TClientWindow::CmAnimate()
  166. {
  167.   if (AnimateCtrl->Open(MAKEINTRESOURCE(SAMPLE_ONE))) {
  168.  
  169.     // Play from beginning(0) to end(-1), indefinitely(-1)
  170.     //
  171.     AnimateCtrl->Play(0, -1, -1);
  172.     Playing = true;
  173.   }
  174.   else {
  175.     MessageBox("Unable to open AVI resource", "Error");
  176.   }
  177. }
  178.  
  179. //
  180. // Enable 'ANIMATE' option is not currently playing
  181. //
  182. void
  183. TClientWindow::CeAnimate(TCommandEnabler& ce)
  184. {
  185.   ce.Enable(!Playing);
  186. }
  187.  
  188.  
  189. //
  190. // Stop any animation
  191. //
  192. void
  193. TClientWindow::CmStop()
  194. {
  195.   AnimateCtrl->Stop();
  196.   Playing = false;
  197. }
  198.  
  199. //
  200. // Enable STOP if currently playing
  201. //
  202. void
  203. TClientWindow::CeStop(TCommandEnabler& ce)
  204. {
  205.   ce.Enable(Playing == true);
  206. }
  207.  
  208. //
  209. // Display a dialog containing an animation control
  210. //
  211. void
  212. TClientWindow::CmDialog()
  213. {
  214.   TAnimDialog(this).Execute();
  215. }
  216.  
  217.  
  218. //
  219. // Set Main window object
  220. //
  221. void
  222. TSampleApp::InitMainWindow()
  223. {
  224.   SetMainWindow(new TFrameWindow(0, "Animation", new TClientWindow()));
  225.   GetMainWindow()->AssignMenu(IDM_APPMENU);
  226. }
  227.  
  228. //
  229. // Entry point of application
  230. //
  231. int
  232. OwlMain(int, char*[])
  233. {
  234.   return TSampleApp().Run();
  235. }
  236.