home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / source / piemnsrc.sit / PiePopupXCMD.c < prev    next >
C/C++ Source or Header  |  1989-09-14  |  4KB  |  181 lines

  1. /*
  2.  * PiePopupXCMD.c
  3.  *
  4.  *        XCMD interface for pie popup menus.
  5.  *
  6.  */
  7. #include    <MacTypes.h>
  8. #include    <MenuMgr.h>
  9. #include    <QuickDraw.h>
  10. #include    <SetUpA4.h>
  11. #include    "HyperXCmd.h"
  12.  
  13.  
  14. /*
  15.  *  Local function prototypes.
  16.  */
  17. pascal    void main(XCmdBlockPtr);
  18. int        GetLocOfCardWindow(XCmdBlockPtr, Point *);
  19. Handle    CopyStrToHand(char *);
  20. long    HandleToNum(XCmdBlockPtr, Handle);
  21. void    HandleToPstr(Str255, Handle);
  22. char    *ToCstr(char *);
  23. char    *ToPstr(char *);
  24.  
  25.  
  26.  
  27. #define    ERRORFLAG            (short)    -1
  28. #define    USAGE "Usage: put PiePopup(\"Semicolon;separated;menu;list\") into container"
  29.  
  30.  
  31.  
  32.  
  33. pascal    void
  34. main(paramPtr)
  35.     XCmdBlockPtr    paramPtr;
  36. {
  37.     Str255        menuList;            /* parsed list of menu items */
  38.     int            num_menu_items;        /* number of menu items to display */
  39.     char        tempStr[32];        /* scratch area for num to string conversions */
  40.     int            selection;            /* the choice the user made from the popup */
  41.     Point        cardWindowLoc;        /* used to align popup on monitors > 9 inches */
  42.     
  43.  
  44.     /* LightSpeed C allows global variables to be referenced from A4 */
  45.     RememberA0();
  46.     SetUpA4();
  47.         
  48.     /* Make sure we were called with only one argument */
  49.     if (paramPtr->paramCount != 1) {
  50.         paramPtr->returnValue = (Handle) CopyStrToHand(USAGE);
  51.         return;
  52.     }        
  53.                 
  54.     /* We need to bias the origin of the popup by the HyperCard's window location */
  55.     if ((GetLocOfCardWindow(paramPtr, &cardWindowLoc)) == ERRORFLAG) {
  56.         return;
  57.     }
  58.  
  59.     /* Buffer the menu list. */
  60.     strcpy(menuList, *paramPtr->params[0]);
  61.         
  62.     /* 
  63.      * Strip the features that are supported by the Mac's menu manager but we ignore.
  64.      * These include: line separators, check marks, icons, etc.
  65.      */
  66.     num_menu_items = ParseMenuList(menuList);
  67.         
  68.     /* Put up the pie popup menu */
  69.     selection = DrawPieMenu(cardWindowLoc, menuList, num_menu_items, 0, (int) geneva, 9);
  70.         
  71.     /* Return the menu selection to HyperCard */
  72.     NumToStr(paramPtr, (long) selection, (unsigned char *) tempStr);
  73.     paramPtr->returnValue = (Handle) CopyStrToHand(ToCstr((char *) tempStr));
  74.         
  75.     /* We're done, recover original A4 and return */    
  76.     RestoreA4();
  77.     return;
  78. }
  79.  
  80.  
  81.  
  82. /********************************************************************************/
  83. /* The following routines are from the book:    XCMD's for HyperCard            */
  84. /*                                                by: Gary Bond                    */
  85. /*                                                ISBN: 0-943518-85-7                */
  86. /********************************************************************************/
  87.  
  88. int
  89. GetLocOfCardWindow(paramPtr, loc)
  90.     XCmdBlockPtr    paramPtr;
  91.     Point            *loc;
  92. {
  93.     Handle    hndl;
  94.     char    str[256];
  95.     
  96.     strcpy(str, "item 1 of rect of card window");
  97.     hndl = EvalExpr(paramPtr, (StringPtr) CtoPstr(str));
  98.     if (paramPtr->result != noErr) {
  99.         SysBeep(5);
  100.         paramPtr->returnValue = (Handle) CopyStrToHand("Can't get rect of card window");
  101.         return((int) ERRORFLAG);
  102.     }
  103.     if (paramPtr->result == noErr) {
  104.         loc->h = HandleToNum(paramPtr, hndl);
  105.         DisposHandle(hndl);
  106.         
  107.         strcpy(str, "item 2 of rect of card window");
  108.         hndl = EvalExpr(paramPtr, (StringPtr) CtoPstr(str));
  109.         if (paramPtr->result == noErr) {
  110.             loc->v = HandleToNum(paramPtr, hndl);
  111.             DisposHandle(hndl);
  112.             return(noErr);
  113.         }
  114.     }
  115.     paramPtr->returnValue = (Handle) CopyStrToHand("Can't get rect of card window");
  116.     return ((int) ERRORFLAG);
  117. }
  118.  
  119.  
  120.  
  121. Handle
  122. CopyStrToHand(str)
  123.     char *str;
  124. {
  125.     Handle newHndl;
  126.     
  127.     newHndl = (Handle) NewHandle((long) strlen(str) + 1);
  128.     strcpy((char *) (*newHndl) , str);
  129.     return(newHndl);
  130. }
  131.  
  132.  
  133.  
  134. long
  135. HandleToNum(paramPtr, hndl)
  136.     XCmdBlockPtr    paramPtr;
  137.     Handle            hndl;
  138. {
  139.     char    str[32];
  140.     long    num;
  141.     
  142.     strcpy(str, *hndl);
  143.     num = StrToLong(paramPtr, (unsigned char *) ToPstr(str));
  144.     return(num);
  145. }
  146.  
  147.  
  148.  
  149. char *
  150. ToCstr(str)
  151.     char *str;
  152. {
  153.     unsigned char    length,i;
  154.     
  155.     length = str[0];
  156.     for (i = 0; i < length; ++i)
  157.         str[i] = str[i+1];
  158.     str[length] = 0;
  159.     return(str);
  160. }
  161.  
  162.  
  163.  
  164. char *
  165. ToPstr(str)
  166.     char *str;
  167. {
  168.     unsigned char    length, i;
  169.     
  170.     for (i = 0, length = 0; str[i] != 0; ++i)
  171.         ++length;
  172.     
  173.     while (i--)
  174.         str[i+1] = str[i];
  175.     
  176.     str[0] = length;
  177.     return(str);
  178. }
  179.  
  180.  
  181.