home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Snippets / MovieScroll 1.0.2 / MovieScroll.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-01  |  6.7 KB  |  295 lines  |  [TEXT/CWIE]

  1. // MovieScroll
  2. // by Ken Long <kenlong@netcom.com>
  3. // updated for CW7 on 951201
  4.  
  5. // MovieScroll demonstrates "movie" type scrolling of credits, just
  6. // like the ones you see at the trailing end of movies or videos.
  7. // If you want the text drawn over black, like in the movies, then
  8. // some other "girations" are necessary.  This just shows the
  9. // scrolling, with style.
  10.  
  11. void InitMacintosh (void);
  12. void SetUpMenus (void);
  13. void HandleEvent (void);
  14. void HandleMouseDown (EventRecord *theEvent);
  15. void AdjustMenus (void);
  16. static void enable (MenuHandle menu, short item, short ok);
  17. void HandleMenu (long mSelect);
  18. void main (void);
  19. pascal void OutlineDefault (WindowPtr myDialog, short itemNo);
  20. void DoAbout (void);
  21.  
  22. #define aboutID 128
  23. TEHandle textHand;
  24.  
  25. MenuHandle    appleMenu, fileMenu;
  26.  
  27. enum    {
  28.     appleID = 1,
  29.     fileID
  30. };
  31.  
  32. enum    {
  33.     quitItem = 1
  34. };
  35.  
  36. WindowPtr    shell_window;
  37.  
  38. void InitMacintosh(void)
  39. {
  40.     MaxApplZone();
  41.     
  42.     InitGraf(&qd.thePort);
  43.     InitFonts();
  44.     FlushEvents(everyEvent, 0);
  45.     InitWindows();
  46.     InitMenus();
  47.     TEInit();
  48.     InitDialogs(0L);
  49.     InitCursor();
  50.  
  51. }
  52.  
  53. void SetUpMenus (void)
  54. {
  55.     InsertMenu (appleMenu = NewMenu (appleID, "\p\024"), 0);
  56.     InsertMenu (fileMenu = NewMenu (fileID, "\pFile"), 0);
  57.     DrawMenuBar ();
  58.     AppendMenu (appleMenu, "\pAbout item");
  59.     AddResMenu (appleMenu, 'DRVR');
  60.     AppendMenu (fileMenu, "\pQuit/Q");
  61. }
  62.  
  63. void HandleEvent(void)
  64. {
  65.     int            ok;
  66.     EventRecord    theEvent;
  67.  
  68.     HiliteMenu(0);
  69.     SystemTask ();        //• Handle desk accessories.
  70.     
  71.     ok = GetNextEvent (everyEvent, &theEvent);
  72.     if (ok)
  73.         switch (theEvent.what)
  74.         {
  75.             case mouseDown:
  76.                 HandleMouseDown(&theEvent);
  77.             break;
  78.             
  79.             case keyDown: 
  80.             case autoKey:
  81.             if ((theEvent.modifiers & cmdKey) != 0)
  82.             {
  83.                    AdjustMenus();
  84.                 HandleMenu(MenuKey((char) (theEvent.message & charCodeMask)));
  85.             }
  86.             break;
  87.             
  88.             case updateEvt:
  89.                 BeginUpdate(shell_window);
  90.                 EndUpdate(shell_window);
  91.             break;
  92.             
  93.             case activateEvt:
  94.                 InvalRect(&shell_window->portRect);
  95.             break;
  96.         }
  97. }
  98.  
  99. void HandleMouseDown (EventRecord    *theEvent)
  100. {
  101.     WindowPtr    theWindow;
  102.     int            windowCode = FindWindow (theEvent->where, &theWindow);
  103.     
  104.     switch (windowCode)
  105.     {
  106.         case inSysWindow: 
  107.             SystemClick (theEvent, theWindow);
  108.         break;
  109.         
  110.         case inMenuBar:
  111.               AdjustMenus();        //• Left over - not really needed.
  112.             HandleMenu(MenuSelect(theEvent->where));
  113.         break;
  114.             
  115.         case inContent:
  116.             if (theWindow == shell_window)
  117.             {
  118.                 if (theWindow != FrontWindow())
  119.                     SelectWindow(shell_window);
  120.                 else
  121.                     InvalRect(&shell_window->portRect);
  122.             }
  123.         break;
  124.           
  125.         case inGoAway:
  126.         if (theWindow == shell_window && 
  127.             TrackGoAway(shell_window, theEvent->where))
  128.             HideWindow(shell_window);
  129.         break;
  130.     }
  131. }
  132.  
  133. void AdjustMenus(void)
  134. {
  135.     register WindowPeek wp = (WindowPeek) FrontWindow();
  136.     short kind = wp ? wp->windowKind : 0;
  137.     Boolean DA = kind < 0;
  138.         
  139. }
  140.  
  141.  
  142. static void enable(MenuHandle menu, short item, short ok)
  143. {
  144.     if (ok)
  145.         EnableItem(menu, item);
  146.     else
  147.         DisableItem(menu, item);
  148. }
  149.  
  150. void HandleMenu (long mSelect)
  151. {
  152.     int            menuID = HiWord(mSelect);
  153.     int            menuItem = LoWord(mSelect);
  154.     Str255        name;
  155.     GrafPtr        savePort;
  156.     WindowPeek    frontWindow;
  157.     
  158.     switch (menuID)
  159.     {
  160.         case    appleID:
  161.             if (menuItem == 1)
  162.                 DoAbout ();
  163.             else
  164.                 {
  165.                     GetPort (&savePort);
  166.                     GetItem (appleMenu, menuItem, name);
  167.                     OpenDeskAcc (name);
  168.                     SetPort (savePort);
  169.             }
  170.         break;
  171.     
  172.         case    fileID:
  173.             switch (menuItem)
  174.             {              
  175.                 if (frontWindow->windowKind < 0)
  176.                     CloseDeskAcc(frontWindow->windowKind);
  177.                 else 
  178.                     if ((frontWindow = (WindowPeek) shell_window) != NULL)
  179.                         HideWindow(shell_window);
  180.                   break;
  181.                         
  182.                 case    quitItem:
  183.                     ExitToShell();
  184.                 break;
  185.             }
  186.         break;
  187.     }
  188. }
  189.  
  190.  
  191. void main( void)
  192. {
  193.     InitMacintosh();
  194.     SetUpMenus();
  195.     DoAbout ();
  196.     for (;;)
  197.         HandleEvent();
  198. }
  199.  
  200. pascal void OutlineDefault (WindowPtr myDialog, short itemNo)
  201. {
  202.     short x;
  203.     Rect rectangle;
  204.     Handle handle;
  205.  
  206.     GetDItem((DialogPtr) myDialog, itemNo, &x, &handle, &rectangle);
  207.     PenSize(3, 3);
  208.     InsetRect(&rectangle, -4, -4);
  209.     FrameRoundRect(&rectangle, 16, 16);
  210.     PenSize(1, 1);
  211. }
  212.  
  213. void DoAbout(void)        //• 'About...' dialog box.
  214. {
  215.     #define SCREEN        2        //• Text window useritem.
  216.     #define SHOWTIME    3
  217.     short USERITEM = 7;            //• Used to outline default button.
  218.     short textID = 128;            //• rsrc ID# for text used.
  219.     short styleID = 128;        //• rsrc ID# for styl used.
  220.  
  221.     GrafPtr saveWPtr;            //• Holds previous grafPtr.
  222.     DialogPtr aboutPtr;            //• Pointer to dialog.
  223.     short theItem;                //• Item selected by user.
  224.     Point mLoc;                    //• Mouse location.
  225.     Rect rectangle;
  226.     short x;
  227.     Handle handle;
  228.     Rect txtRect;                //• Used to hold viewRect.
  229.     TEHandle saveTxtHdl;        //• Text handle.
  230.     Boolean finished;            //• Pushed 'The End' button yet?
  231.     StScrpHandle styleHdl;
  232.     OSErr err;
  233.  
  234.     GetPort(&saveWPtr);                //• Save the old port.
  235.     saveTxtHdl = textHand;            //• Save the old text hdl.
  236.  
  237.     //• Get dialog box pointer.
  238.     aboutPtr = GetNewDialog(aboutID, nil, (WindowPtr)-1);    
  239.  
  240.     //• This next makes the static text field font monaco 9.
  241.     SetPort((GrafPtr) aboutPtr);    //• Output to dialog.
  242.     TextSize(9);                    //• Set text size.
  243.     TextFont(monaco);                //• Set text font.
  244.  
  245.     //• Get text window rect.
  246.     GetDItem((DialogPtr) aboutPtr, SCREEN, &x, &handle, &txtRect);    
  247.     InsetRect(&txtRect, 5, 1);        //• Leave margins for text.
  248.  
  249.     //• Create styled TERecord.
  250.     textHand = TEStylNew(&txtRect, &txtRect);
  251.  
  252.     //• Read the TEXT resource.
  253.     handle = GetResource('TEXT', textID);    
  254.     HLock(handle);                    //• Lock handle.
  255.  
  256.     //• Get the style handle.
  257.     styleHdl = (StScrpHandle)(Get1Resource('styl', styleID));    
  258.     TEStylInsert(*handle, SizeResource(handle), styleHdl, textHand);    //• move text into text record.
  259.     TESetJust (1, textHand);
  260.     HUnlock(handle);                    //• Unlock handle.
  261.  
  262.     ShowWindow(aboutPtr);                //• Show dialog box now.
  263.     TEUpdate(&txtRect, textHand);        //• Draw text in viewRect.
  264.     InsetRect(&txtRect, -5, -1);        //• Leave margins for text.
  265.     FrameRect(&txtRect);                //• Draw frame around text.
  266.     OutlineDefault(aboutPtr, USERITEM);
  267.     GetDItem((DialogPtr) aboutPtr, USERITEM, &x, &handle, &rectangle);    //• outline default button.
  268.     SetDItem(aboutPtr, USERITEM, x, (Handle) OutlineDefault, &rectangle);    //• redraw if erased*/
  269.  
  270.     finished = false;                //• Reset flag.
  271.     do {                            //• Repeat until finished.
  272.         ModalDialog(0L, &theItem);    //• Show dialog/get result.
  273.         switch (theItem) {            //• Control hit.
  274.             case 1:
  275.                 finished = true;    //• "The End" button hit / close.
  276.                 break;
  277.             case SHOWTIME:
  278.                 do
  279.                 {
  280.                     //• scroll up a pixel
  281.                     TEScroll(0, -1, textHand);        
  282.                 }while (! Button ());
  283.                 break;
  284.             default:
  285.                 break;                //• None of the above, avoid range error.
  286.         }                            //• End of  case.
  287.     } while (!finished);            //• End of  mainloop.
  288.  
  289.     TEDispose(textHand);            //• Reclaim heap space.
  290.     textHand = saveTxtHdl;            //• Restore global textHand.
  291.     DisposDialog(aboutPtr);            //• Get rid of dialog box.
  292.     SetPort(saveWPtr);                //• Restore the old port.
  293. }
  294.  
  295.