home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Utility Spectacular / Developer / macgzip_022-src / macos / think / SPDCProg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-26  |  7.9 KB  |  323 lines  |  [TEXT/KAHL]

  1. /*
  2.  * Copyright (C) 1993  SPDsoft
  3.  * 
  4.  */
  5.  
  6. #include "SPDProg.h"
  7. /*            SpinCursors by
  8.  *            America Online: LISPer
  9.  *             Internet: tree@uvm.edu
  10. */
  11. #define HiWrd(aLong)    (((aLong) >> 16) & 0xFFFF)
  12.  
  13. typedef struct                /* The structure of an 'acur' resource */
  14. {
  15.     short numberOfFrames;        /* number of cursors to animate */
  16.     short whichFrame;        /* current frame number */
  17.     CursHandle frame[];        /* Pointer to the first cursor */
  18. } acur, *acurPtr, **acurHandle;
  19.  
  20. unsigned long int    SPDBackCycles;
  21.  
  22. Boolean        UseAnimatedCursor=false;
  23. Boolean        CanBreak=false;
  24. acurHandle    gFrameList;
  25.  
  26. Boolean InitAnimatedCursors(short acurID)
  27. {
  28.     register short i=0;
  29.     register short cursID;
  30.     Boolean noErrFlag = FALSE;
  31.     SPDBackCycles=0;
  32.     
  33.     if((gFrameList = (acurHandle) GetResource('acur',acurID))) {
  34.         /* got it! */
  35.         noErrFlag = TRUE;
  36.         while((i<(*gFrameList)->numberOfFrames) && noErrFlag) {
  37.             /* The id of the cursor is stored in the high word of the frame handle */
  38.             cursID = (int) HiWrd((long) (*gFrameList)->frame[i]);
  39.             (*gFrameList)->frame[i] = GetCursor(cursID);
  40.             if((*gFrameList)->frame[i])
  41.                 i++;            /* get the next one */
  42.             else
  43.                 noErrFlag=FALSE;    /* foo! we couldn't find the cursor */
  44.         }
  45.     }
  46.     if(noErrFlag) {
  47.  
  48.         (*gFrameList)->whichFrame = 0;
  49.         UseAnimatedCursor=true;
  50.     }
  51.     return noErrFlag;
  52. }
  53.  
  54. /* Free up the storage used by the current animated cursor and all
  55.    of its frames */
  56. void ReleaseAnimatedCursors()
  57. {
  58.     int i;
  59.     
  60.     if(UseAnimatedCursor)
  61.     {
  62.         for(i=0;i<(*gFrameList)->numberOfFrames;i++)
  63.             ReleaseResource((Handle) (*gFrameList)->frame[i]);
  64.         ReleaseResource((Handle) gFrameList);
  65.     }
  66.     UseAnimatedCursor=false;
  67.     SetCursor(&arrow);
  68.  
  69. }
  70.  
  71. /************************************************************************************/
  72.  
  73. /* Opens the MModal dialog. */
  74. /* Just for grins, I'm also saving and restoring it's position in*/
  75. /* a very simple way */
  76.  
  77. void DrawMovable(WindowPtr drawIt);
  78.  
  79. Boolean    UseMModalProg=false;
  80.  
  81. static Rect        barRect; 
  82. static Rect        greyRect;
  83.  
  84. static ControlHandle    ButtonHndl;
  85.  
  86. static short int mypat[] = { 0x55AA,0x55AA,0x55AA,0x55AA };    /* Grey */
  87. unsigned long int SPDEnd, SPDNow;
  88.  
  89. Str255    SPDPstr="\p";
  90. char*    SPDpstr=(char*)SPDPstr;
  91.  
  92. static    Boolean        gModalUp;
  93.     Boolean        gSavedPos;
  94.     Point        gSavedPoint;
  95.  
  96. #define    kMyModalKind    1000
  97.  
  98. void InitMovableModal(unsigned long int theEnd)
  99. {
  100.     WindowPtr    temp;
  101.     int        newWid, newLen, wid;
  102.     Rect    r, mierda;
  103.  
  104.  
  105.     temp = GetNewWindow(150, nil, (WindowPtr)-1);
  106.     /* install drawing proc */
  107.     SetWRefCon(temp, (long)DrawMovable);
  108.     
  109.     /* set the kind to my MModalwindow */
  110.     ((WindowPeek)temp)->windowKind = kMyModalKind;
  111.     gModalUp = true;
  112.     
  113.     if (gSavedPos) {
  114.         /* move it to the saved position */
  115.         /* move it to 0,0 to avoid any math at all */
  116.         MoveWindow(temp, 0, 0, false);
  117.         MoveWindow(temp, gSavedPoint.h, gSavedPoint.v, false);
  118.     }
  119.     
  120.     r = temp->portRect;
  121.     
  122.     SPDNow=0;
  123.     SPDEnd=(theEnd==0?1:theEnd);
  124.  
  125.     UseMModalProg=true;
  126.     
  127.         
  128.     CtoPstr(SPDPstr);
  129.     wid = (r.right - r.left) - 40;
  130.     newWid = StringWidth(SPDPstr);
  131.     if (newWid > wid )
  132.     {
  133.         newLen = (int) SPDPstr[0];
  134.         wid = wid - CharWidth('…');
  135.         do
  136.         {
  137.             newWid = newWid - CharWidth(SPDPstr[newLen]);
  138.             newLen--;
  139.         }while((newWid > wid) && (SPDPstr[0] != 0x00));
  140.  
  141.         newLen ++;
  142.         SPDPstr[newLen] = '…';
  143.         SPDPstr[0] = (char)newLen;
  144.     }
  145.  
  146.  
  147.         
  148.     barRect.left =    r.left+20;
  149.     barRect.right =    r.right-20;
  150.     barRect.bottom =    r.bottom - 20;
  151.     barRect.top =    barRect.bottom-20;
  152.     
  153.     if(CanBreak)
  154.     {
  155.     
  156.     ButtonHndl = GetNewControl(128, temp);
  157.     
  158.     barRect.right  -= (20+ (*ButtonHndl)->contrlRect.right);
  159.     OffsetRect( &((*ButtonHndl)->contrlRect), barRect.right + 20, barRect.top );
  160.  
  161.     ShowControl(ButtonHndl);
  162.     
  163.     
  164.     }   
  165.  
  166.  
  167.     greyRect.left =    barRect.left+1;
  168.     greyRect.right =    greyRect.left;
  169.     greyRect.bottom =    barRect.bottom-1;
  170.     greyRect.top =    barRect.top+1;
  171.     
  172.     ShowWindow(temp);
  173.     DrawMovable(temp);
  174.  
  175.     SetPort(temp);
  176. }
  177.  
  178. void ReleaseMovableModal(void)
  179. {
  180.     WindowPtr temp = FrontWindow();
  181.  
  182.     
  183.     
  184.      if(UseMModalProg)
  185.     {
  186.         if(CanBreak)
  187.         {
  188.         DisposeControl(ButtonHndl);
  189.         }
  190.  
  191.         /* the front window really should be my modal window */
  192.         /* if it isn't, I'm bailing.*/
  193.         gSavedPos = true;
  194.         gSavedPoint.h = temp->portRect.left;
  195.         gSavedPoint.v = temp->portRect.top;
  196.         LocalToGlobal(&gSavedPoint);
  197.         if (((WindowPeek)temp)->windowKind == kMyModalKind)
  198.             CloseWindow(temp);
  199.     
  200.         gModalUp = false;
  201.         UseMModalProg=false;
  202.     }
  203.     
  204.     
  205. }
  206.  
  207. static void DrawMovable(WindowPtr drawIt)
  208. {
  209.     WindowPtr tempWP;
  210.     unsigned long int wbar;
  211.     Str255 stemp;
  212.  
  213.     BeginUpdate(drawIt);
  214.     GetPort(&tempWP);
  215.     SetPort(drawIt);
  216.     
  217.         MoveTo(20,20);/*h,v*/
  218.         DrawString(SPDPstr);
  219.  
  220.     wbar = ((float)SPDNow/SPDEnd)*(barRect.right-barRect.left) + barRect.left; 
  221.     if ( wbar >= barRect.right ) wbar = barRect.right;
  222.     FrameRect(&barRect);
  223.     greyRect.right=wbar-1;
  224.     FillRect(&greyRect,mypat);
  225.  
  226.     DrawControls(drawIt);
  227.     EndUpdate(drawIt);
  228.     SetPort(tempWP);
  229. }
  230.  
  231. /************************************************************************************/
  232.  
  233. Boolean    SPDSystemTask()
  234. {
  235.     typedef long (*MyProcPtr)();
  236.     
  237.     EventRecord    theEvent;
  238.     MyProcPtr    drawProc;
  239.     WindowPtr    twindow;
  240.     static unsigned long int TheLastTime;
  241.     ControlHandle    WhichCtl;
  242.     
  243.     if(UseMModalProg)
  244.     {
  245.         InvalRect(&barRect);
  246.     }
  247.     while(GetNextEvent(everyEvent, &theEvent))
  248.     {
  249.     if ((theEvent.what == keyDown)&&(CanBreak))
  250.     {
  251.         if (((theEvent.modifiers & cmdKey) != 0)
  252.         && ((theEvent.message & charCodeMask) == '.' ))
  253.         {
  254.             SetCursor(&arrow);
  255.             return(true);
  256.         }
  257.     }else if ((theEvent.what == updateEvt)&&(UseMModalProg))
  258.     {
  259.                 /* Make sure it's my window before I jump through the refCon */
  260.                 /* Why, since DA's have they're own layer in 7.0? */
  261.                 /* BECAUSE there are other people in the universe who will */
  262.                 /* add things to your windowList.BalloonWriter, for example, */
  263.                 /* so you still need to be careful */
  264.                 if (((WindowPeek)theEvent.message)->windowKind == kMyModalKind)
  265.                 {
  266.                     /* get the drawing proc from the refCon */
  267.                     drawProc = (MyProcPtr)GetWRefCon((WindowPtr)theEvent.message);
  268.                     /* jump to it */
  269.  
  270.                     drawProc((WindowPtr)theEvent.message);
  271.  
  272.                 }
  273.         }else if (theEvent.what == mouseDown)
  274.         {
  275.             switch (FindWindow(theEvent.where, &twindow))
  276.             {
  277.                 case inSysWindow:
  278.                                 /* pass to the system */
  279.                                 SystemClick(&theEvent, twindow);
  280.                                 break;
  281.                         case inDrag:
  282.                             if (twindow != FrontWindow() && gModalUp)
  283.                             {
  284.                             /* don't do anything, can't drag a back window */
  285.                                SysBeep(1);
  286.                             } else
  287.                             {
  288.                             DragWindow(twindow, theEvent.where, &qd.screenBits.bounds);
  289.                             }
  290.                             break;
  291.                         
  292.                         case inContent:
  293.                         
  294.                                GlobalToLocal(&theEvent.where);
  295.                                
  296.                                if(inButton == FindControl(theEvent.where, twindow, &WhichCtl))
  297.                                {
  298.                                    if(0!=TrackControl(WhichCtl, theEvent.where, nil))
  299.                                      return(true);
  300.                             }            
  301.                             break;
  302.                            
  303.                         
  304.                         default:
  305.                                                                
  306.                             break;
  307.  
  308.                 
  309.             }
  310.     }
  311.     
  312.     if(( theEvent.when-TheLastTime > kTicksCursor )&&(UseAnimatedCursor))
  313.     {
  314.         SetCursor(*((*gFrameList)->frame[(*gFrameList)->whichFrame++]));
  315.         if((*gFrameList)->whichFrame == (*gFrameList)->numberOfFrames)
  316.             (*gFrameList)->whichFrame = 0;
  317.             
  318.         TheLastTime=theEvent.when;
  319.         /* This will be the last time... */
  320.     }
  321.     }
  322.     return(false);
  323. }