home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 2 / MacMania 2.toast / Demo's / Tools&Utilities / Programming / QT MovieToolBox / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-19  |  11.6 KB  |  575 lines  |  [TEXT/KAHL]

  1. /* File:     main.c
  2.  
  3.  
  4. The main program structure:
  5.     
  6.     int FixMenus()
  7.     - Activate/Deactive menu as needed
  8.     
  9.     Edit Menu Stubs -- null routines...
  10.     
  11.     DoCommand(long    mResult)
  12.     - Handle all memu commands
  13.     
  14.     Boolean HandleEvents(EventRecord *myEvent);
  15.  
  16.     Initialize()
  17.     - Setup up menus and Macintosh enviroment
  18.  
  19.     Main()    
  20.     - Setup QuickTime movie enviroment
  21.     - Call Initialision routines
  22.     - Call Main Event Handling routines
  23.     - Cleanup QuickTime Movie Enviroment
  24.     
  25.     History..
  26.         RMF    Created from DTS from sample source code.
  27. */
  28.  
  29. #include "gGlobals.h"
  30.  
  31. #include    <pascal.h>
  32. #include    <Scrap.h>
  33. #include    <Desk.h>
  34. #include    <string.h>
  35. #include    <stdio.h>
  36. #include    <Fonts.h>
  37. #include    <Math.h>
  38.  
  39. /* *****************************************************************************
  40.  
  41.     The following three routines are here because the MPW 3.2 c compiler 
  42.     screw up compiling the DoMovie routine if you use the "straight" calls.
  43.     
  44.     Try it and see and complain to DTS.
  45. */
  46.  
  47. void DoHLock(Handle h)
  48. {
  49.     HLock(h);
  50. }
  51. void DoHUnlock(Handle h)
  52. {
  53.     HUnlock( h);
  54. }
  55.  
  56. void DoDisposHandle(Handle h)
  57. {
  58.     DisposHandle(h);
  59. }
  60.  
  61. /*********************************************
  62.  
  63.     Clean up menu hiliting
  64.     
  65. *********************************************/
  66.  
  67. int FixMenus()
  68. {
  69.     Boolean    gotAllWindows;
  70.     Boolean    gotAnyWindows = gSrcWindow != nil || gAltWindow != nil ;
  71.  
  72.     gotAllWindows = gSrcWindow != nil &&
  73.                     (!gRequiresAlternate || gAltWindow != nil );
  74.     DisableItem(gMenus[FILE_MENU],FILE_M_SAVE);    
  75.     if ( gotAnyWindows )
  76.         EnableItem(gMenus[FILE_MENU],FILE_M_CLOSE);
  77.     else
  78.         DisableItem(gMenus[FILE_MENU],FILE_M_CLOSE);
  79.     
  80.     if (gotAllWindows ) 
  81.         DisableItem(gMenus[FILE_MENU],FILE_M_OPEN);
  82.     else 
  83.         EnableItem(gMenus[FILE_MENU],FILE_M_OPEN);
  84.  
  85.     if ( !gotAllWindows ) {
  86.         DisableItem(gMenus[MOVIE_MENU],MOVIE_M_APPEND);
  87.         DisableItem(gMenus[MOVIE_MENU],MOVIE_M_MOVIE);
  88.         DisableItem(gMenus[MOVIE_MENU],MOVIE_M_PREVIEW);
  89.         DisableItem(gMenus[MOVIE_MENU],MOVIE_M_OVERSAMPLE);
  90.     } else {
  91.         EnableItem(gMenus[MOVIE_MENU],MOVIE_M_APPEND);
  92.         EnableItem(gMenus[MOVIE_MENU],MOVIE_M_MOVIE);
  93.         EnableItem(gMenus[MOVIE_MENU],MOVIE_M_PREVIEW);
  94.         EnableItem(gMenus[MOVIE_MENU],MOVIE_M_OVERSAMPLE);
  95.     }
  96.     
  97.     DisableItem(gMenus[EDIT_MENU],EDIT_M_CUT);
  98.     DisableItem(gMenus[EDIT_MENU],EDIT_M_COPY);
  99.     DisableItem(gMenus[EDIT_MENU],EDIT_M_PASTE);
  100.     
  101.     DisableItem(gMenus[EDIT_MENU],EDIT_M_CLEAR);
  102.     DisableItem(gMenus[EDIT_MENU],EDIT_M_UNDO);
  103.     CheckItem(gMenus[MOVIE_MENU],MOVIE_M_OVERSAMPLE,gOversample);
  104.     
  105.     CheckItem(gMenus[EFFECT_MENU],EFFECT_BACKWARDS,gBackwards);
  106.     CheckItem(gMenus[EFFECT_MENU],EFFECT_Roll,gDoRollFill);
  107.     
  108.     CheckItem(gMenus[EFFECT_MENU],gDoCrossFade,true);
  109.  
  110. }
  111.  
  112.  
  113. /*********************************************
  114.  
  115.     Stubs
  116.     
  117. ********************************************/
  118.  
  119. OSErr DoCopy()
  120. {
  121.  
  122. }
  123.  
  124. OSErr DoCut()
  125. {
  126. }
  127.  
  128.  
  129. OSErr DoPaste()
  130. {
  131.  
  132. }
  133.  
  134. void DoClear()
  135. {
  136.  
  137.  
  138. }
  139.  
  140. OSErr DoUndo()
  141. {
  142.  
  143.  
  144. }
  145.  
  146.  
  147. /*********************************************
  148.  
  149.     Process menu command.
  150.     
  151. ********************************************/
  152.  
  153. DoCommand(long    mResult)
  154. {
  155.     short         theMenu, theItem;
  156.     Str255        daName;
  157.     GDHandle    saveGD;
  158.     CGrafPtr    savePort;
  159.     OSErr        res = 0;
  160.  
  161. #define HIshort(aLong)        (((aLong) >> 16) & 0xFFFF)
  162. #define LOshort(aLong)        ((aLong) & 0xFFFF)
  163.  
  164.     theItem = LOshort(mResult);
  165.     theMenu = HIshort(mResult);        /* This is the resource ID */
  166.  
  167.  
  168.     switch (theMenu) {
  169.         case APPLE_MENU_ID:
  170.             if (theItem == 1) {
  171.                 ShowAboutBox();
  172.             } else {
  173.                 GetItem(gMenus[0], theItem, daName);
  174.                 GetGWorld(&savePort,&saveGD);
  175.                 (void) OpenDeskAcc(daName);
  176.                 SetGWorld(savePort,saveGD);
  177.             }
  178.             break;
  179.  
  180.         case FILE_MENU_ID:
  181.             {
  182.  
  183.                 switch (theItem) {
  184.                 case FILE_M_OPEN:
  185.                     DoOpen(nil);
  186.                     FixMenus();
  187.                     break;
  188.                 case FILE_M_CLOSE:
  189.                     DoClose(gActiveWindow);
  190.                     FixMenus();
  191.                     res = 1;
  192.                     break;
  193.                 case FILE_M_SAVE:
  194.                     FixMenus();
  195.                     break;
  196.                 
  197.                 case fileImport:
  198.                     importMovie();
  199.                     break;
  200.                     
  201.                 case fileExport:
  202.                     exportMovie();
  203.                     break;
  204.                             
  205.                     
  206.                 case FILE_M_QUIT:
  207.                     { 
  208.                         Boolean abortion = false;
  209.                         while ( gActiveWindow ) {
  210.                             if ( DoClose(gActiveWindow) < 0 ) {
  211.                                 abortion = true;
  212.                                 break;
  213.                             }
  214.                         }
  215.                         if ( !abortion ) 
  216.                             gExitFlag = true;            /* Request exit */
  217.                         res = 1;
  218.                         break;
  219.                     }
  220.                 }
  221.             }
  222.             break;
  223.         case EDIT_MENU_ID:
  224.             if ( !SystemEdit(theItem-1) ) {
  225.                 switch ( theItem ) {
  226.                 case EDIT_M_UNDO:
  227.                     DoUndo();
  228.                     break;
  229.                 case EDIT_M_CUT:
  230.                     DoCut();
  231.                     break;
  232.                 case EDIT_M_COPY:
  233.                     DoCopy();
  234.                     break;
  235.                 case EDIT_M_PASTE:
  236.                     DoPaste();
  237.                     break;
  238.                 case EDIT_M_CLEAR:
  239.                     DoClear();
  240.                     break;
  241.                 }
  242.             }
  243.             FixMenus();
  244.             break;
  245.             
  246.         case EFFECT_MENU_ID:
  247.             switch (theItem) {
  248.             case EFFECT_BACKWARDS:
  249.                 DoClose((WindowPtr)gDstWindow);
  250.                 gBackwards = !gBackwards;
  251.                 break;
  252.             case EFFECT_Roll:
  253.                 gDoRollFill = 1 - gDoRollFill;
  254.                 break;
  255.             default:
  256.             
  257.             CheckItem(gMenus[EFFECT_MENU],gDoCrossFade,false);
  258.             gDoCrossFade = theItem;
  259.             CheckItem(gMenus[EFFECT_MENU],gDoCrossFade,true);
  260.                     
  261.                     gStandardP.flags = scShowMotionSettings;
  262.     gStandardP.theCodecType = kCodecType;
  263.     gStandardP.theCodec = kCodecID;
  264.     gStandardP.spatialQuality = kCodecQuality;
  265.     gStandardP.temporalQuality = 0;
  266.     gStandardP.depth = kCodecDepth;
  267.     gStandardP.frameRate = kFrameRate<<16;
  268.     gStandardP.keyFrameRate = kFrameRate;
  269.  
  270.                     
  271.             if ( !gDoCrossFade != EFFECT_CrossFad ) {        
  272.                     gDepth = 40;                            // randomdotstereogram needs grayscale buffers
  273.                     gStandardP.theCodecType = 'smc ';        // graphics compressor is good choice
  274.                     gStandardP.depth = gDepth;                // prime depth for std compression
  275.                     gRequiresAlternate = false;                // only needs one pict to do processing
  276.                 } else {
  277.                     gStandardP.depth = 32;                    // full resolution for best effect
  278.                     gRequiresAlternate = true;                // needs two picts to do processing
  279.                 }
  280.                 }    /* End switch */
  281.             
  282.             FixMenus();
  283.             break;
  284.             
  285.         case MOVIE_MENU_ID:
  286.         
  287.             switch ( theItem ) {
  288.             case MOVIE_M_COMPRESS :
  289.                 SetCompression();
  290.                 break;
  291.                 
  292.             case MOVIE_M_STAGES :
  293.                 DoClose((WindowPtr)gDstWindow);
  294.                 SetStages();
  295.                 break;
  296.                 
  297.             case MOVIE_M_OVERSAMPLE:
  298.                 gOversample = !gOversample;
  299.                 CheckItem(gMenus[MOVIE_MENU],theItem,gOversample);
  300.                 break;
  301.                 
  302.             case MOVIE_M_PREVIEW :
  303.                 DoPreview();
  304.                 break;
  305.                 
  306.             case MOVIE_M_MOVIE :
  307.                 DoMovie(false);
  308.                 break;
  309.             case MOVIE_M_APPEND :
  310.                 DoMovie(true);
  311.                 break;
  312.                 
  313.             }
  314.             break;
  315.  
  316.         default:
  317.             break;
  318.  
  319.     }/*endsw theMenu*/
  320.  
  321.     HiliteMenu(0);
  322.     return(res);
  323. }    /* End of () */
  324.  
  325.  
  326. /********************************************
  327.  
  328.     Process events.
  329.     
  330. ********************************************/
  331.  
  332. Boolean HandleEvents(EventRecord *myEvent)
  333. {
  334.     Rect        dragRect;
  335.     WindowPtr    whichWindow;
  336.     short        res = 0;
  337.     GWorldPtr    saveWorld;
  338.     GDHandle    saveGD;
  339.  
  340.     GetGWorld(&saveWorld,&saveGD);
  341.  
  342.     switch (myEvent->what) {
  343.     case mouseDown:
  344.         switch ((short)FindWindow(myEvent->where, &whichWindow)) {
  345.         case inSysWindow:
  346.             SystemClick(myEvent, whichWindow);
  347.             break;
  348.             
  349.         case inMenuBar:
  350.             res = DoCommand(MenuSelect(myEvent->where));
  351.             break;
  352.  
  353.         case inDrag:
  354.             SetPort((GrafPtr)whichWindow);
  355.             SetRect(&dragRect, 4, 20 + 4, qd.screenBits.bounds.right-4, qd.screenBits.bounds.bottom-4);
  356.             DragWindow(whichWindow, myEvent->where, &dragRect);
  357.             break;
  358.  
  359.         case inGrow:
  360.             break;
  361.  
  362.         case inGoAway:
  363.             if ( TrackGoAway(whichWindow,myEvent->where)  ) {
  364.                 DoClose(whichWindow);
  365.                 FixMenus();
  366.                 res = true;
  367.             }
  368.             break;
  369.  
  370.         case inZoomIn:
  371.             break;
  372.  
  373.         case inZoomOut:
  374.             break;
  375.  
  376.         case inContent:
  377.             
  378.             if ( whichWindow != FrontWindow() ) {
  379.                 SelectWindow(whichWindow);
  380.                 gActiveWindow = whichWindow;
  381.             }
  382.             FixMenus();
  383.             break;
  384.         
  385.         default:
  386.             break;
  387.         }
  388.         break;
  389.  
  390.     case keyDown:
  391.         if ( ((myEvent->modifiers & cmdKey) != 0) ) {
  392.             res = DoCommand(MenuKey(myEvent->message & charCodeMask));
  393.         }
  394.         break;
  395.     
  396.     case updateEvt :
  397.  
  398.         DoUpdate((CWindowPtr)myEvent->message);
  399.         break;
  400.     
  401.     case activateEvt:
  402.         whichWindow=(WindowPtr)myEvent->message;
  403.         if ( (myEvent->modifiers & activeFlag) ) {
  404.             gActiveWindow = whichWindow;
  405.         }
  406.         break;
  407.  
  408.     default:
  409.         break;
  410.         
  411.     }
  412.     SetGWorld(saveWorld,saveGD);
  413.     return(res);
  414. }    /* End of () */
  415.  
  416.  
  417. /*********************************************
  418.  
  419.     Initialize menu bar.
  420.  
  421. *********************************************/
  422.  
  423. SetupMenus()
  424. {
  425.     gMenus[APPLE_MENU] = GetMenu(APPLE_MENU_ID);
  426.     AddResMenu(gMenus[APPLE_MENU], (ResType) 'DRVR');
  427.     InsertMenu(gMenus[APPLE_MENU], 0);
  428.     gMenus[FILE_MENU] = GetMenu(FILE_MENU_ID);
  429.     InsertMenu(gMenus[FILE_MENU], 0);
  430.     gMenus[EDIT_MENU] = GetMenu(EDIT_MENU_ID);
  431.     InsertMenu(gMenus[EDIT_MENU], 0);
  432.     gMenus[MOVIE_MENU] = GetMenu(MOVIE_MENU_ID);
  433.     InsertMenu(gMenus[MOVIE_MENU], 0);
  434.     gMenus[EFFECT_MENU] = GetMenu(EFFECT_MENU_ID);
  435.     InsertMenu(gMenus[EFFECT_MENU], 0);
  436.     
  437.     DrawMenuBar();
  438.     FixMenus();
  439. }    /* End of () */
  440.  
  441. /*********************************************
  442.  
  443.     Prepare for work.
  444.     
  445. *********************************************/
  446.  
  447. int Initialize()
  448. {    OSErr err;
  449.     Ptr    size;
  450.     long    resp;
  451.     
  452.     size = GetApplLimit();
  453.     SetApplLimit(size - 32*1024);        /* make room on stack so Quickdraw can do big pictures */
  454.     MaxApplZone();
  455.  
  456.     /*    initialize managers */
  457.  
  458.     InitGraf(&qd.thePort);
  459.     InitFonts();
  460.     InitWindows();
  461.     InitMenus();
  462.     InitDialogs(nil);
  463.     InitCursor();
  464.     FlushEvents(everyEvent, 0);
  465.     
  466.     SetupMenus();                        /* Check off correct memus and hilight them */
  467.     
  468.     CouldAlert(140);                    /* Load Alert into Memory ... incase of out of memory errors */
  469.     
  470.  
  471.     if ( NGetTrapAddress(0xab1d,ToolTrap) == NGetTrapAddress(0x9f,ToolTrap) ) {
  472.         return -1;
  473.         }
  474.     if ( Gestalt(gestaltQuickTime, &resp) != 0 ) {
  475.         return -1;
  476.         }
  477.     
  478.     gHasNewStdFile = false;                /* Check for preview and new standard file stuff */
  479.     err = Gestalt(gestaltStandardFileAttr,&resp);
  480.     if ((err == 0) && ((resp & (0x00000001 << gestaltStandardFile58)) != 0))
  481.             gHasNewStdFile = true;
  482.             
  483.     InitSetCompression();                /* Setup default QT compression settings */
  484.           
  485.     return 0;
  486. }    /* End of () */
  487.  
  488.  
  489. /*********************************************
  490.  
  491.     If some kind of fatal error happened come here.
  492.  
  493. *********************************************/
  494.  
  495. void Error(char *msg, OSErr code)
  496. {    Str15 buf;
  497.     GWorldPtr    saveWorld;
  498.     GDHandle    saveGD;
  499.     CGrafPtr    wmgrPort;
  500.     extern char *moviesErrorNames[];
  501.     
  502.     GetGWorld(&saveWorld,&saveGD);    GetCWMgrPort(&wmgrPort);
  503.     SetGWorld(wmgrPort,nil);
  504.     if (code ) {
  505.         NumToString(code, buf);
  506.     } else buf[0] = 0;
  507.     
  508.     c2pstr(msg);
  509.     if (code >= -2051 && code<= -2000) {
  510.         c2pstr(moviesErrorNames[-code - 2000]);
  511.         ParamText((StringPtr)msg,buf,(StringPtr) moviesErrorNames[-code - 2000],0);
  512.         p2cstr((StringPtr)moviesErrorNames[-code - 2000]);
  513.     } else
  514.         ParamText((StringPtr)msg,buf,0,0);
  515.     if ( code == 0 )
  516.         Alert(140,nil);
  517.     else
  518.         StopAlert(140,nil);
  519.     p2cstr((StringPtr) msg);
  520.     
  521.     SetGWorld(saveWorld,saveGD);
  522. }    /* End of () */
  523.  
  524.  
  525. ShowAboutBox()
  526. {
  527.  
  528.     ParamText(    "\pCreated from:\r\rPictMovier & ImportExportMovie QuickTime Sample Code",
  529.                 "\p",
  530.                 "\p\r\rChange By R.Mark Fleming <MarkF@post.QueensU.CA",0);
  531.     Alert(140,nil);
  532. }    /* End of () */
  533.  
  534.  
  535. /********************************************
  536.  
  537.     The main program
  538.     
  539.     - Setup QuickTime movie enviroment
  540.     - Call Initialision routines
  541.     - Call Main Event Handling routines
  542.     - Cleanup QuickTime Movie Enviroment
  543.     
  544. ********************************************/
  545.  
  546. main()
  547. {    EventRecord myEvent;
  548.     
  549.     if (Initialize()) {
  550.         SysBeep(0);    ExitToShell();
  551.     }    
  552.     
  553.     EnterMovies();    
  554.         
  555.     if ( !gDoCrossFade  ) {        
  556.         gDepth = 40;                            // randomdotstereogram needs grayscale buffers
  557.         gStandardP.theCodecType = 'smc ';        // graphics compressor is good choice
  558.         gStandardP.depth = gDepth;                // prime depth for std compression
  559.         gRequiresAlternate = false;                // only needs one pict to do processing
  560.     } else {
  561.         gStandardP.depth = 32;                    // full resolution for best effect
  562.         gRequiresAlternate = true;                // needs two picts to do processing
  563.         }
  564.         
  565.     while ( !gExitFlag ) {
  566.         if ( WaitNextEvent( everyEvent, &myEvent, 1, nil) != 0 ) {
  567.             HandleEvents(&myEvent);
  568.         }
  569.     }
  570.  
  571. done:
  572.     ExitMovies();    
  573.     ExitToShell();
  574. }    /* End of () */
  575.