home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Frameworks / PennyWise™ Framework / PennyView / Source / MovieWindow.c < prev    next >
Encoding:
Text File  |  1994-08-11  |  13.9 KB  |  437 lines  |  [TEXT/KAHL]

  1. //••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  2. //                                                                                //
  3. //                                                                                //
  4. //                    Copyright PennyWise Software, 1994.                            //
  5. //                                                                                //
  6. //            Part of the PennyWise Software Application Framework                //
  7. //                                                                                //
  8. //                                                                                //
  9. //            MovieWindow.c                Written by Peter Kaplan                    //
  10. //                                                                                //
  11. //                                                                                //
  12. //••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  13. //••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  14. //
  15. //    This is a template for a PennyWise Software Application Framework window
  16. //
  17. //••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  18. #include "PWFramework.h"
  19. #include "PWWindowList.h"
  20. #include "WindowID.h"
  21. //••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  22. #include "DefaultHandlers.h"
  23. #include "HandleMenus.h"
  24. #include "MovieWindow.h"
  25. #include <Movies.h>
  26. #include <PWPrefsMgr.h>
  27. #include <PWWindowUtils.h>
  28. //••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  29. //••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  30. // These procedures are static. They will only be called by name from here
  31. // outside refrences will use our window proc list.
  32. static    void    ThisWindowCreate    (EventRecord* theEvent, WindowPtr theWindow);
  33. static    short    ThisWindowDispose    (EventRecord* theEvent, WindowPtr theWindow);
  34. static    void    ThisWindowResize    (EventRecord* theEvent, WindowPtr theWindow);
  35. static    void    ThisWindowUpdate    (EventRecord* theEvent, WindowPtr theWindow);
  36. static    void    ThisWindowIdle        (EventRecord* theEvent, WindowPtr theWindow);
  37. static    void    ThisWindowCursor    (EventRecord* theEvent, WindowPtr theWindow);
  38. static    void    ThisWindowPreMenu    (EventRecord* theEvent, WindowPtr theWindow);
  39. static    void    ThisWindowPostMenu    (EventRecord* theEvent, WindowPtr theWindow);
  40. static    short    ThisWindowDoMenu    (EventRecord* theEvent, WindowPtr theWindow, short theMenu, short theItem, short theWindowID);
  41. static    void    ThisWindowGrowRect    (EventRecord* theEvent, WindowPtr theWindow, Rect* theRect);
  42. //••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  43. // This record holds all the information about this window
  44. // You can add fields to this record.
  45. // NOTE:ALL WINDOW RECORDS MUST START WITH THIS
  46. //        HEADER OR ELSE THE FRAMEWORK WILL NOT 
  47. //        FUNCTION PROPERLY. YOU'VE BEEN WARNED!
  48. typedef struct    OurWinRecord {
  49.     WindowParamHeader    theHeader;
  50.     short                isDirty;
  51.     MovieController        theController;
  52.     }OurWinRecord, *OurWinPtr, **OurWinHandle;
  53. //••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  54. // Macros for accessing the header data
  55. // you can add your own as you add fields
  56. // to the OurWinRecord. 
  57. // NOTE:THESE MACROS ASSUME theData HOLDS
  58. //        A VALID COPY OF OurWinHandle. 
  59. #define THE_ID        (*theData)->theHeader.theID
  60. #define IS_DIRTY    (*theData)->isDirty
  61. #define CONTROLLER    (*theData)->theController
  62. //••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  63. void InitMovieWindowHandlers()
  64. {
  65.     PWInstallWindowType    (kWINDOW_ID_MovieWindow, kWINDOW_TYPE_APPLICATION);
  66.     PWInstallCreate        (kWINDOW_ID_MovieWindow, ThisWindowCreate);
  67.     PWInstallDispose    (kWINDOW_ID_MovieWindow, ThisWindowDispose);
  68.     PWInstallZoomIn        (kWINDOW_ID_MovieWindow, ThisWindowIdle);
  69.     PWInstallZoomOut    (kWINDOW_ID_MovieWindow, ThisWindowIdle);
  70.     PWInstallResize        (kWINDOW_ID_MovieWindow, ThisWindowResize);
  71.     PWInstallClick        (kWINDOW_ID_MovieWindow, ThisWindowIdle);
  72.     PWInstallUpdate        (kWINDOW_ID_MovieWindow, ThisWindowUpdate);
  73.     PWInstallActivate    (kWINDOW_ID_MovieWindow, ThisWindowIdle);
  74.     PWInstallDeactivate    (kWINDOW_ID_MovieWindow, ThisWindowIdle);
  75.     PWInstallIdle        (kWINDOW_ID_MovieWindow, ThisWindowIdle);
  76.     PWInstallCursor        (kWINDOW_ID_MovieWindow, ThisWindowCursor);
  77.     PWInstallKeyDown    (kWINDOW_ID_MovieWindow, ThisWindowIdle);
  78.     PWInstallDrag        (kWINDOW_ID_MovieWindow, ThisWindowIdle);
  79.     PWInstallPreMenu    (kWINDOW_ID_MovieWindow, ThisWindowPreMenu);
  80.     PWInstallMenu        (kWINDOW_ID_MovieWindow, ThisWindowDoMenu);
  81.     PWInstallPostMenu    (kWINDOW_ID_MovieWindow, ThisWindowPostMenu);
  82.     PWInstallGrowRect    (kWINDOW_ID_MovieWindow, ThisWindowGrowRect);
  83.     PWInstallBackground    (kWINDOW_ID_MovieWindow, ThisWindowIdle);
  84.     PWInstallScrap2Appl    (kWINDOW_ID_MovieWindow, defaultWindowNothing);
  85.     PWInstallAppl2Scrap    (kWINDOW_ID_MovieWindow, defaultWindowNothing);
  86. }
  87. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  88. static    void    ThisWindowCreate    (EventRecord* theEvent, WindowPtr theWindow)
  89. {
  90. // This routine will very rarely contain anything worthwhile
  91. // You will make custom routines for most windows that will be exported
  92. // via the include file
  93. }
  94. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  95. //    Since we will be exporting this we don't have to conform to any particular input format
  96. void    MovieWindowOpen(FSSpecPtr    theSpec)
  97. {
  98. WindowPtr        theWindow;
  99. OurWinHandle    theData;
  100. Rect            windowBounds;
  101. Movie            theMovie;
  102. MovieController    theController;
  103. OSErr            theErr;
  104. short            movieResFile;
  105. Str63            movieName;
  106. short            movieResID;
  107. Boolean            wasChanged;
  108. WStateData        *wsdp;
  109.  
  110.     SetRect(&windowBounds, 4, 42, 800,800);
  111.     theWindow = NewCWindow(NULL,&windowBounds,theSpec->name,FALSE,zoomDocProc,(WindowPtr)-1,TRUE,0);
  112.     if (theWindow) {
  113.         // We sucessfully got the window
  114.         
  115.         // Now we have to allocate our storage
  116.         theData = (OurWinHandle) NewHandle(sizeof(OurWinRecord));
  117.         if (theData) {
  118.             // We sucessfully allocated storage
  119.             
  120.             // So Lets set the port to our new window
  121.             SetPort(theWindow);
  122.             
  123.             // Here we would do any screen/size manipulations
  124.             // before we show the window
  125.             theMovie = NULL;
  126.             theErr = OpenMovieFile(theSpec, &movieResFile, fsRdPerm);
  127.             if (theErr == noErr) {
  128.                 movieResID = 0;
  129.                 theErr = NewMovieFromFile( &theMovie, movieResFile, &movieResID, movieName, newMovieActive, &wasChanged);
  130.                 CloseMovieFile(movieResFile);
  131.                 }
  132.             
  133.             if (theMovie) {
  134.                 // Here we would allocate any other 
  135.                 // objects that we need. (Controls, etc.)
  136.                 
  137.                 // Make this real big. NewController will make it right
  138.                 SetRect(&windowBounds, 0, 0, 800, 800);
  139.                 
  140.                 theController = NewMovieController(theMovie, &windowBounds, mcTopLeftMovie);
  141.                 if (theController) {
  142.                     MCGetControllerBoundsRect(theController, &windowBounds);
  143.                     SizeWindow(theWindow, windowBounds.right, windowBounds.bottom, TRUE);
  144.                     MCDoAction(theController, mcActionSetKeysEnabled, (Ptr) TRUE);
  145.                     MCDoAction(theController, mcActionSetGrowBoxBounds, &screenBits.bounds);
  146.                     
  147.                     MCEnableEditing(theController, TRUE);
  148.                 
  149.                     
  150.                     // Now lets set the id
  151.                     THE_ID        = kWINDOW_ID_MovieWindow;
  152.                     IS_DIRTY    = FALSE;
  153.                     CONTROLLER    = theController;
  154.                     
  155.                     // Install our routines
  156.                     SetWRefCon(theWindow, (long) theData);
  157.                     
  158.                     CascadePosition(theWindow);
  159.                         
  160.                     // Now lets make the standard size this one
  161.                     wsdp = (WStateData*) *(((WindowPeek)theWindow)->dataHandle);
  162.                     LocalToGlobalRect(&windowBounds);
  163.                     wsdp->stdState = windowBounds;
  164.                         
  165.                     // Show it & select it before we leave
  166.                     ShowWindow(theWindow);
  167.                     SelectWindow(theWindow);
  168.                     }
  169.                 else {
  170.                     // We could not allocate memory for our window's data
  171.                     // So lets get rid of the data
  172.                     DisposeWindow(theWindow);
  173.                     theWindow = NULL;
  174.                     DisposeHandle((Handle)theData);
  175.                     DisposeMovie(theMovie);
  176.                     }
  177.                 }
  178.             else {
  179.                 // We could not allocate memory for our window's data
  180.                 // So lets get rid of the data
  181.                 DisposeWindow(theWindow);
  182.                 theWindow = NULL;
  183.                 DisposeHandle((Handle)theData);
  184.                 }
  185.             }
  186.         else {
  187.             // We could not allocate memory for our window's data
  188.             // So lets get rid of the data
  189.             DisposeWindow(theWindow);
  190.             theWindow = NULL;
  191.             }
  192.         }
  193.     
  194.     if (!theWindow) {
  195.         SysBeep(1);
  196.         // We did not create the window
  197.         // You will probably want to put up a dialog here to explain why
  198.         }
  199. }
  200. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  201. static    short    ThisWindowDispose    (EventRecord* theEvent, WindowPtr theWindow)
  202. {
  203. OurWinHandle    theData;
  204. short            theResults;
  205. MovieController    theController;
  206. Movie            theMovie;
  207.  
  208.     theData = (OurWinHandle) GetWRefCon(theWindow);
  209.     
  210.     // Want to save it
  211.     theResults = TRUE;
  212.     
  213.     if (IS_DIRTY) {
  214.         // Data has been changed we would probably want to put up 
  215.         // a “Save Changes to x” dialog here 
  216.         // But I'm just going to reset IS_DIRTY
  217.         IS_DIRTY = FALSE;
  218.         }
  219.     
  220.     // Do whatever saving you need to do here
  221.     theController = CONTROLLER;
  222.     theMovie = MCGetMovie(theController);
  223.     
  224.     DisposeMovieController(theController);
  225.     DisposeMovie(theMovie);
  226.     
  227.     // Now lets break it down
  228.     HideWindow(theWindow);
  229.     DisposeHandle((Handle)theData);
  230.     DisposeWindow(theWindow);
  231.     
  232. return theResults;    // True if we closed it, false if we did not [ex. pressed cancel in save dialog]    
  233. }
  234. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  235. static    void    ThisWindowResize    (EventRecord* theEvent, WindowPtr theWindow)
  236. {
  237. OurWinHandle    theData;
  238. GrafPtr            oldPort;
  239. MovieController    theController;
  240.  
  241.  
  242.     GetPort(&oldPort);
  243.     SetPort(theWindow);
  244.  
  245.     theData = (OurWinHandle) GetWRefCon(theWindow);
  246.     
  247.     EraseRect(&theWindow->portRect);
  248.  
  249.     // Do any control moving here
  250.     //•••••••••••••••••••••••••••
  251.     theController = CONTROLLER;
  252.     MCSetControllerBoundsRect(theController, &theWindow->portRect);
  253.         
  254.     InvalRect(&theWindow->portRect);
  255.  
  256.     SetPort(oldPort);
  257. }
  258. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  259. static    void    ThisWindowUpdate    (EventRecord* theEvent, WindowPtr theWindow)
  260. {
  261. OurWinHandle    theData;
  262. GrafPtr            oldPort;
  263. MovieController    theController;
  264.  
  265.  
  266.     GetPort(&oldPort);
  267.     SetPort(theWindow);
  268.  
  269.     theData = (OurWinHandle) GetWRefCon(theWindow);
  270.     
  271.     // Draw the window here
  272.     //•••••••••••••••••••••••••••
  273.     theController = CONTROLLER;
  274.     ThisWindowIdle(theEvent, theWindow);
  275.     MCDraw(theController, theWindow);
  276.     
  277.     SetPort(oldPort);
  278. }
  279. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  280. static    void    ThisWindowIdle        (EventRecord* theEvent, WindowPtr theWindow)
  281. {
  282. OurWinHandle    theData;
  283. GrafPtr            oldPort;
  284. MovieController    theController;
  285.  
  286.  
  287.     GetPort(&oldPort);
  288.     SetPort(theWindow);
  289.  
  290.     theData = (OurWinHandle) GetWRefCon(theWindow);
  291.     
  292.     // do your idle tasks here
  293.     //•••••••••••••••••••••••••••    
  294.     theController = CONTROLLER;
  295.     MCIsPlayerEvent(theController, theEvent);
  296.         
  297.     SetPort(oldPort);
  298. }
  299. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  300. static    void    ThisWindowCursor    (EventRecord* theEvent, WindowPtr theWindow)
  301. {
  302.     SetRectRgn(gMouseMovedRgn, -32768, -32768, 32767, 32767);
  303.     SetCursor(&qd.arrow);
  304. }
  305. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  306. static    void    ThisWindowPreMenu    (EventRecord* theEvent, WindowPtr theWindow)
  307. {
  308. OurWinHandle    theData;
  309. GrafPtr            oldPort;
  310. MovieController    theController;
  311.  
  312.  
  313.     GetPort(&oldPort);
  314.     SetPort(theWindow);
  315.  
  316.     theData = (OurWinHandle) GetWRefCon(theWindow);
  317.     
  318.     // Enable and disable items
  319.     // change item names etc.
  320.     DisableItem( gMenuFile, kFILE_PRINT);
  321.     DisableItem( gMenuFile, kFILE_PONE);
  322.     DisableItem( gMenuFile, kFILE_SAVE);
  323.     DisableItem( gMenuFile, kFILE_SAVEAS);
  324.  
  325.     theController = CONTROLLER;
  326.     MCSetUpEditMenu(theController, theEvent->modifiers, gMenuEdit);    
  327.     
  328.     SetPort(oldPort);
  329. }
  330. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  331. static    void    ThisWindowPostMenu    (EventRecord* theEvent, WindowPtr theWindow)
  332. {
  333. OurWinHandle    theData;
  334. GrafPtr            oldPort;
  335.  
  336.  
  337.     GetPort(&oldPort);
  338.     SetPort(theWindow);
  339.  
  340.     theData = (OurWinHandle) GetWRefCon(theWindow);
  341.     
  342.     // Enable and disable items
  343.     // change item names etc.
  344.     EnableItem( gMenuFile, kFILE_PRINT);
  345.     EnableItem( gMenuFile, kFILE_PONE);
  346.     EnableItem( gMenuFile, kFILE_SAVE);
  347.     EnableItem( gMenuFile, kFILE_SAVEAS);
  348.  
  349.     EnableItem( gMenuEdit, kEDIT_UNDO);
  350.     EnableItem( gMenuEdit, kEDIT_CUT);
  351.     EnableItem( gMenuEdit, kEDIT_COPY);
  352.     EnableItem( gMenuEdit, kEDIT_PASTE);
  353.     EnableItem( gMenuEdit, kEDIT_CLEAR);
  354.     
  355.     SetPort(oldPort);
  356. }
  357. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  358. static    short    ThisWindowDoMenu    (EventRecord* theEvent, WindowPtr theWindow, short theMenu, short theItem, short theWindowID)
  359. {
  360. short            theResult;
  361. OurWinHandle    theData;
  362. GrafPtr            oldPort;
  363. MovieController    theController;
  364. Movie            theMovie;
  365. Rect            theRect;
  366.  
  367.  
  368.     theData = (OurWinHandle) GetWRefCon(theWindow);
  369.     theController = CONTROLLER;
  370.  
  371.     theResult = TRUE;
  372.     theMovie = NULL;
  373.     
  374.     // if we don't handle it return false so the defaults will pick it up
  375.     switch (theMenu) {
  376.         case kMENU_ID_EDIT:
  377.             switch (theItem) {
  378.                 case kEDIT_UNDO:
  379.                     MCUndo(theController);
  380.                     break;
  381.                 case kEDIT_CUT:
  382.                     theMovie = MCCut(theController);
  383.                     PutMovieOnScrap(theMovie, 0);
  384.                     break;
  385.                 case kEDIT_COPY:
  386.                     theMovie = MCCopy(theController);
  387.                     PutMovieOnScrap(theMovie, 0);
  388.                     break;
  389.                 case kEDIT_PASTE:
  390.                     theMovie = NewMovieFromScrap(0);
  391.                     MCPaste(theController, theMovie);
  392.                     break;
  393.                 case kEDIT_CLEAR:
  394.                     MCClear(theController);
  395.                     break;
  396.                 default:
  397.                     theResult = FALSE;
  398.                 }
  399.             EraseRect(&theWindow->portRect);
  400.             MCGetControllerBoundsRect(theController, &theRect);
  401.             SizeWindow(theWindow, theRect.right, theRect.bottom, TRUE);
  402.             SetPort(theWindow);
  403.             InvalRect(&theWindow->portRect);
  404.             if (theMovie) DisposeMovie(theMovie);
  405.             break;
  406.         default:
  407.             theResult = FALSE;
  408.         }
  409.         
  410. return theResult;
  411. }
  412. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  413. static    void    ThisWindowGrowRect    (EventRecord* theEvent, WindowPtr theWindow, Rect* theRect)
  414. {
  415. OurWinHandle    theData;
  416. GrafPtr            oldPort;
  417. MovieController    theController;
  418.  
  419.  
  420.     GetPort(&oldPort);
  421.     SetPort(theWindow);
  422.  
  423.     theData = (OurWinHandle) GetWRefCon(theWindow);
  424.     
  425.     ThisWindowIdle(theEvent,  theWindow);
  426.     
  427.     // Set the rect
  428.     theController = CONTROLLER;
  429.     MCGetControllerBoundsRect(theController, theRect);
  430.     SizeWindow( theWindow, theRect->right, theRect->bottom, TRUE);
  431.     
  432.     // Tell the framework that we are handling it.
  433.     SetRect(theRect, 0, 0, 0, 0);
  434.     
  435.     SetPort(oldPort);
  436. }
  437.