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

  1. // NewsFlash
  2. // version 1.0.2
  3. // ported to CodeWarrior by Ken Long <kenlong@netcom.com>
  4. // updated for CW7 on 951201
  5.  
  6. //• --------------------------------------------------------------- •//
  7. //• An itty bitty bytes™ production, for the benefit of anyone        •//
  8. //• who wants use it.                                                •//
  9.  
  10. //• The routine was from the old Pascal source - File v1.1, by        •//
  11. //• Cary Clark, 13 May 1985, which I ported to C.  The program        •//
  12. //• was a simple text file opening/saving program.  The about box    •//
  13. //• was the only cool thing about the whole program.                •//
  14.  
  15. //• It's not the cleanest port in the world, but HEY!  It works!    •//
  16. //• I put a 3 tick delay in the scaling loop, to make the text        •//
  17. //• arrival a little more viewable.  The other 180 tick delay was    •//
  18. //• originally 300 ticks.                                            •//
  19.  
  20. //• Enjoy!                                                            •//
  21.  
  22. //• --------------------------------------------------------------- •//
  23.  
  24. void InitMacintosh (void);
  25. void SetUpMenus (void);
  26. void HandleEvent (void);
  27. void HandleMouseDown (EventRecord *theEvent);
  28. void AdjustMenus (void);
  29. static void enable (MenuHandle menu, short item, short ok);
  30. void HandleMenu (long mSelect);
  31. void main (void);
  32. void NewAbout (void);
  33.  
  34. #define aboutID 128
  35.  
  36. MenuHandle    appleMenu, fileMenu;
  37.  
  38. enum    {
  39.     appleID = 1,
  40.     fileID
  41. };
  42.  
  43. enum    {
  44.     quitItem = 1
  45. };
  46.  
  47. WindowPtr    shell_window;
  48.  
  49. void InitMacintosh(void)
  50. {
  51.     MaxApplZone();
  52.     
  53.     InitGraf(&qd.thePort);
  54.     InitFonts();
  55.     FlushEvents(everyEvent, 0);
  56.     InitWindows();
  57.     InitMenus();
  58.     TEInit();
  59.     InitDialogs(0L);
  60.     InitCursor();
  61.  
  62. }
  63.  
  64. void SetUpMenus (void)
  65. {
  66.     InsertMenu (appleMenu = NewMenu (appleID, "\p\024"), 0);
  67.     InsertMenu (fileMenu = NewMenu (fileID, "\pFile"), 0);
  68.     DrawMenuBar ();
  69.     AppendMenu (appleMenu, "\pAbout item");
  70.     AddResMenu (appleMenu, 'DRVR');
  71.     AppendMenu (fileMenu, "\pQuit/Q");
  72. }
  73.  
  74. void HandleEvent(void)
  75. {
  76.     int            ok;
  77.     EventRecord    theEvent;
  78.  
  79.     HiliteMenu(0);
  80.     SystemTask ();        //• Handle desk accessories.
  81.     
  82.     ok = GetNextEvent (everyEvent, &theEvent);
  83.     if (ok)
  84.         switch (theEvent.what)
  85.         {
  86.             case mouseDown:
  87.                 HandleMouseDown(&theEvent);
  88.             break;
  89.             
  90.             case keyDown: 
  91.             case autoKey:
  92.             if ((theEvent.modifiers & cmdKey) != 0)
  93.             {
  94.                    AdjustMenus();
  95.                 HandleMenu(MenuKey((char) (theEvent.message & charCodeMask)));
  96.             }
  97.             break;
  98.             
  99.             case updateEvt:
  100.                 BeginUpdate(shell_window);
  101.                 EndUpdate(shell_window);
  102.             break;
  103.             
  104.             case activateEvt:
  105.                 InvalRect(&shell_window->portRect);
  106.             break;
  107.         }
  108. }
  109.  
  110. void HandleMouseDown (EventRecord    *theEvent)
  111. {
  112.     WindowPtr    theWindow;
  113.     int            windowCode = FindWindow (theEvent->where, &theWindow);
  114.     
  115.     switch (windowCode)
  116.     {
  117.         case inSysWindow: 
  118.             SystemClick (theEvent, theWindow);
  119.         break;
  120.         
  121.         case inMenuBar:
  122.               AdjustMenus();        //• Left over - not really needed.
  123.             HandleMenu(MenuSelect(theEvent->where));
  124.         break;
  125.             
  126.         case inContent:
  127.             if (theWindow == shell_window)
  128.             {
  129.                 if (theWindow != FrontWindow())
  130.                     SelectWindow(shell_window);
  131.                 else
  132.                     InvalRect(&shell_window->portRect);
  133.             }
  134.         break;
  135.           
  136.         case inGoAway:
  137.         if (theWindow == shell_window && 
  138.             TrackGoAway(shell_window, theEvent->where))
  139.             HideWindow(shell_window);
  140.         break;
  141.     }
  142. }
  143.  
  144. void AdjustMenus(void)
  145. {
  146.     register WindowPeek wp = (WindowPeek) FrontWindow();
  147.     short kind = wp ? wp->windowKind : 0;
  148.     Boolean DA = kind < 0;
  149.         
  150. }
  151.  
  152.  
  153. static void enable(MenuHandle menu, short item, short ok)
  154. {
  155.     if (ok)
  156.         EnableItem(menu, item);
  157.     else
  158.         DisableItem(menu, item);
  159. }
  160.  
  161. void HandleMenu (long mSelect)
  162. {
  163.     int            menuID = HiWord(mSelect);
  164.     int            menuItem = LoWord(mSelect);
  165.     Str255        name;
  166.     GrafPtr        savePort;
  167.     WindowPeek    frontWindow;
  168.     
  169.     switch (menuID)
  170.     {
  171.         case    appleID:
  172.             if (menuItem == 1)
  173.                 NewAbout ();
  174.             else
  175.                 {
  176.                     GetPort (&savePort);
  177.                     GetItem (appleMenu, menuItem, name);
  178.                     OpenDeskAcc (name);
  179.                     SetPort (savePort);
  180.             }
  181.         break;
  182.     
  183.         case    fileID:
  184.             switch (menuItem)
  185.             {              
  186.                 if (frontWindow->windowKind < 0)
  187.                     CloseDeskAcc(frontWindow->windowKind);
  188.                 else 
  189.                     if ((frontWindow = (WindowPeek) shell_window) != NULL)
  190.                         HideWindow(shell_window);
  191.                   break;
  192.                         
  193.                 case    quitItem:
  194.                     ExitToShell();
  195.                 break;
  196.             }
  197.         break;
  198.     }
  199. }
  200.  
  201.  
  202. void main( void)
  203. {
  204.     InitMacintosh();
  205.     SetUpMenus();
  206.         NewAbout ();
  207.     for (;;)
  208.         HandleEvent();
  209. }
  210.  
  211. void NewAbout(void)
  212. {
  213.     #define mouseKey mDownMask + keyDownMask
  214.     StringHandle str1hdl;
  215.     Str255 str1, str2;
  216.     WindowPtr myWindow;
  217.     short width, height, counter, strWidth, strDepth, factor, remainder, adjust;
  218.     long newCount;
  219.     FontInfo txtInfo;
  220.     Rect tempRect, tRect1;
  221.     BitMap offScreen, tempBits;
  222.     EventRecord    theEvent;
  223.  
  224.     str1hdl = (StringHandle)(GetResource (0L, 0L));
  225.     GetIndString (str1, 128, 1);
  226.     GetIndString (str2, 128, 2);
  227.     HLock ((Handle) str1hdl);
  228.     myWindow = GetNewWindow (128, 0L, (WindowPtr)-1L);
  229.     SetPort (myWindow);
  230.     TextFont (0);
  231.     TextSize (12);
  232.     GetFontInfo (&txtInfo);
  233.     width = myWindow->portRect.right - myWindow->portRect.left;
  234.     height = myWindow->portRect.bottom - myWindow->portRect.top;
  235.     strWidth = StringWidth ((StringPtr) &str1hdl);
  236.     if (( StringWidth (str1) > strWidth ) 
  237.     || ( StringWidth (str2) > strWidth ))
  238.     { 
  239.         strWidth = StringWidth (str1);
  240.         strWidth = StringWidth (str2);
  241.     }
  242.     strDepth = txtInfo.ascent * 2 + 
  243.                 txtInfo.descent * 2 + 
  244.                 txtInfo.leading + 1;
  245.                 
  246.     //• rowbytes needs to be even.
  247.     offScreen.rowBytes = (strWidth + 15) / 16 * 2;
  248.     SetRect (&offScreen.bounds, 0,0,strWidth,strDepth);
  249.     offScreen.baseAddr = NewPtrClear (offScreen.rowBytes * strDepth);
  250.     
  251.     tempBits = myWindow->portBits;
  252.     SetPortBits (&offScreen);
  253.     MoveTo ((strWidth - StringWidth (str1)) / 2, strDepth - txtInfo.ascent - 6);
  254.     DrawString (str1);
  255.     MoveTo ((strWidth - StringWidth (str2)) / 2, strDepth - txtInfo.descent);
  256.     DrawString (str2);
  257.     HUnlock ((Handle) str1hdl);
  258.     
  259.     SetPortBits (&tempBits);
  260.     factor = strWidth / strDepth;
  261.     remainder = strWidth % strDepth;
  262.     SetRect (&tRect1, (width - remainder) / 2 - factor, height / 2 - 1,
  263.     (width + remainder) / 2 + factor, height / 2 + 1);
  264.     counter = 1;
  265.     do
  266.     {
  267.         SystemTask ();
  268.         CopyBits (&offScreen, &myWindow->portBits, &offScreen.bounds, &tRect1, srcCopy, 0L);
  269.         InsetRect (&tRect1, - factor, -1);
  270.         counter = counter + 2;
  271.         Delay (3L, &newCount);
  272.     } while (counter != strDepth +1) ;
  273.  
  274.     Delay (180L, &newCount);    //• Wait 5 seconds.
  275.  
  276.     tempRect = offScreen.bounds;
  277.     OffsetRect (&tempRect, (width - strWidth) / 2, (height - strDepth) / 2);
  278.     tRect1 = offScreen.bounds;
  279.  
  280.     while (! EventAvail(mouseKey, &theEvent) &&
  281.           (tRect1.right - factor * 2 > tRect1.left) )
  282.     {
  283.         SystemTask (); //• the clock still ticks!.
  284.         factor = tRect1.right / strDepth;
  285.         if ( tempRect.left > 0 ) 
  286.             InsetRect (&tempRect, - factor, -2);
  287.         else 
  288.             if ( tempRect.top > 0 )
  289.             {
  290.                  InsetRect (&tRect1, factor, 0);
  291.                  InsetRect (&tempRect, 0, -2);
  292.             } 
  293.             else 
  294.                 InsetRect (&tRect1, factor, 2);
  295.                 CopyBits (&offScreen, &myWindow->portBits, &tRect1, &tempRect, srcCopy, 0L);
  296.     }
  297.     DisposPtr(offScreen.baseAddr);
  298.     DisposeWindow (myWindow);
  299. }
  300.