home *** CD-ROM | disk | FTP | other *** search
/ CLIX - Fazer Clix Custa Nix / CLIX-CD.cdr / mac / lib / Mac / StandardFile.xs < prev    next >
Text File  |  1998-04-05  |  10KB  |  374 lines

  1. /* $Header: /home/neeri/MacCVS/MacPerl/perl/ext/Mac/ExtUtils/MakeToolboxModule,v 1.1 1997/04/07 20:49:35 neeri Exp 
  2.  *    Copyright (c) 1997 Matthias Neeracher
  3.  *
  4.  *    You may distribute under the terms of the Perl Artistic License,
  5.  *    as specified in the README file.
  6.  *
  7.  * $Log: MakeToolboxModule,v  Revision 1.1  1997/04/07 20:49:35  neeri
  8.  * Synchronized with MacPerl 5.1.4a1
  9.  * 
  10.  */
  11.  
  12. #define MAC_CONTEXT
  13.  
  14. #include "EXTERN.h"
  15. #include "perl.h"
  16. #include "XSUB.h"
  17. #include <Types.h>
  18. #include <StandardFile.h>
  19. #include <TFileSpec.h>
  20.  
  21. typedef struct {
  22.     SV *    fileFilter;
  23.     SV *    dlgHook;
  24.     SV *    modalFilter;
  25.     SV *    activate;
  26. } SFProcs;
  27.  
  28. typedef EventRecord * ToolboxEvent;
  29. typedef CInfoPBPtr  CatInfo;
  30.  
  31. static SFProcs * sCurSFProcs;
  32.  
  33. static pascal Boolean CallFileFilter(CatInfo pb, SFProcs * procs)
  34. {
  35.     Boolean res;
  36.     
  37.     dSP;
  38.     ENTER;
  39.     SAVETMPS;
  40.     
  41.     PUSHMARK(sp);
  42.     XS_XPUSH(CatInfo, pb);
  43.     PUTBACK;
  44.     
  45.     perl_call_sv(procs->fileFilter, G_SCALAR);
  46.     
  47.     SPAGAIN;
  48.     
  49.     XS_POP(Boolean, res);
  50.     
  51.     PUTBACK;
  52.     FREETMPS;
  53.     LEAVE;
  54.     
  55.     return res;
  56. }
  57.  
  58. static pascal Boolean CallSimpleFileFilter(CatInfo pb)
  59. {
  60.     return CallFileFilter(pb, sCurSFProcs);
  61. }
  62.  
  63. pascal short CallDlgHook(short item, DialogPtr theDialog, SFProcs * procs)
  64. {
  65.     short   res;
  66.     
  67.     dSP;
  68.     ENTER;
  69.     SAVETMPS;
  70.     
  71.     PUSHMARK(sp);
  72.     XS_XPUSH(short, item);
  73.     XS_XPUSH(GrafPtr, theDialog);
  74.     PUTBACK;
  75.     
  76.     perl_call_sv(procs->dlgHook, G_SCALAR);
  77.     
  78.     SPAGAIN;
  79.     
  80.     XS_POP(short, res);
  81.     
  82.     PUTBACK;
  83.     FREETMPS;
  84.     LEAVE;
  85.     
  86.     return res;
  87. }
  88.  
  89. pascal Boolean CallModalFilter(DialogPtr theDialog, EventRecord *theEvent, short *itemHit, SFProcs * procs)
  90. {
  91.     Boolean res;
  92.     int     count;
  93.     
  94.     dSP;
  95.     ENTER;
  96.     SAVETMPS;
  97.     
  98.     PUSHMARK(sp);
  99.     XS_XPUSH(GrafPtr, theDialog);
  100.     XS_XPUSH(ToolboxEvent, theEvent);
  101.     XS_XPUSH(short, *itemHit);
  102.     PUTBACK;
  103.     
  104.     count = perl_call_sv(procs->modalFilter, G_ARRAY);
  105.     
  106.     SPAGAIN;
  107.     
  108.     while (count-- > 1)
  109.         XS_POP(short, *itemHit);
  110.     if (count < 0)
  111.         res = false;
  112.     else
  113.         XS_POP(Boolean, res);
  114.     
  115.     PUTBACK;
  116.     FREETMPS;
  117.     LEAVE;
  118.     
  119.     return res;
  120. }
  121.  
  122. pascal void CallActivate(DialogPtr theDialog, short itemNo, Boolean activating, SFProcs * procs)
  123. {
  124.     dSP;
  125.     
  126.     PUSHMARK(sp);
  127.     XS_XPUSH(GrafPtr, theDialog);
  128.     XS_XPUSH(short, itemNo);
  129.     XS_XPUSH(Boolean, activating);
  130.     PUTBACK;
  131.     
  132.     perl_call_sv(procs->activate, G_DISCARD);
  133. }
  134.  
  135. #if GENERATINGCFM
  136. RoutineDescriptor sCallFileFilter = 
  137.     BUILD_ROUTINE_DESCRIPTOR(uppFileFilterYDProcInfo, CallFileFilter);
  138. RoutineDescriptor sCallSimpleFileFilter = 
  139.     BUILD_ROUTINE_DESCRIPTOR(uppFileFilterProcInfo, CallSimpleFileFilter);
  140. RoutineDescriptor sCallDlgHook = 
  141.     BUILD_ROUTINE_DESCRIPTOR(uppDlgHookYDProcInfo, CallDlgHook);
  142. RoutineDescriptor sCallModalFilter = 
  143.     BUILD_ROUTINE_DESCRIPTOR(uppModalFilterYDProcInfo, CallModalFilter);
  144. RoutineDescriptor sCallActivate = 
  145.     BUILD_ROUTINE_DESCRIPTOR(uppActivateYDProcInfo, CallActivate);
  146. #else
  147. #define sCallFileFilter         *NewFileFilterYDProc(CallFileFilter)
  148. #define sCallSimpleFileFilter   *NewFileFilterProc(CallSimpleFileFilter)
  149. #define sCallDlgHook            *NewDlgHookYDProc(CallDlgHook)
  150. #define sCallModalFilter        *NewModalFilterYDProc(CallModalFilter)
  151. #define sCallActivate           *NewActivateYDProc(CallActivate)
  152. #endif
  153.  
  154. MODULE = Mac::StandardFile  PACKAGE = Mac::StandardFile
  155.  
  156. =head2 Functions
  157.  
  158. =over 4
  159.  
  160. =item StandardFileReply
  161.  
  162. A structure holding the result of a standard file dialog. Fields are:
  163.  
  164.     Boolean                         sfGood;
  165.     Boolean                         sfReplacing;
  166.     OSType                          sfType;
  167.     FSSpec                          sfFile;
  168.     ScriptCode                      sfScript;
  169.     short                           sfFlags;
  170.     Boolean                         sfIsFolder;
  171.     Boolean                         sfIsVolume;
  172.  
  173. =cut
  174. STRUCT StandardFileReply
  175.     Boolean                         sfGood;
  176.     Boolean                         sfReplacing;
  177.     OSType                          sfType;
  178.     FSSpec                          sfFile;
  179.     I8                              sfScript;
  180.     short                           sfFlags;
  181.     Boolean                         sfIsFolder;
  182.     Boolean                         sfIsVolume;
  183.  
  184.  
  185. =item StandardPutFile PROMPT, DEFAULTNAME 
  186.  
  187. Display a dialog prompting for a new file.
  188.  
  189. =cut
  190. StandardFileReply
  191. StandardPutFile(prompt, defaultName)
  192.     Str255  prompt
  193.     Str255  defaultName
  194.     CODE:
  195.     StandardPutFile(prompt, defaultName, &RETVAL);
  196.     OUTPUT:
  197.     RETVAL
  198.  
  199.  
  200. =item StandardGetFile FILEFILTER, TYPELIST 
  201.  
  202. Display a dialog prompting for an existing file.
  203.  
  204. =cut
  205. StandardFileReply
  206. StandardGetFile(fileFilter, typeList)
  207.     SV *    fileFilter
  208.     SV *    typeList
  209.     CODE:
  210.     {
  211.         short   numTypes;
  212.         STRLEN  len;
  213.         char *  types;
  214.         char *  typeBuf;
  215.         SFProcs procs;
  216.         
  217.         typeBuf  = 0;
  218.         if (!SvOK(typeList)) {
  219.             numTypes = -1;
  220.             types    = 0;
  221.         } else {
  222.             types = SvPV(typeList, len);
  223.             if (len & 3 && looks_like_number(typeList)) {
  224.                 XS_INPUT(short, numTypes, typeList);
  225.             } else {
  226.                 numTypes = len >> 2;
  227.                 if (numTypes && (long)types & 1) {          /* Ensure alignment */
  228.                     typeBuf = (char *)malloc(numTypes << 2);
  229.                     memcpy(typeBuf, types, numTypes << 2);
  230.                     types = typeBuf;
  231.                 } 
  232.             }
  233.         }
  234.         if (SvTRUE(fileFilter)) {
  235.             sCurSFProcs = &procs;
  236.             procs.fileFilter = fileFilter;
  237.             StandardGetFile(&sCallSimpleFileFilter, numTypes, (OSType*)types, &RETVAL);
  238.         } else
  239.             StandardGetFile(nil, numTypes, (OSType*)types, &RETVAL);
  240.         if (typeBuf)
  241.             free(typeBuf);
  242.     }
  243.     OUTPUT:
  244.     RETVAL
  245.  
  246.  
  247. =item CustomPutFile PROMPT, DEFAULTNAME, DLGID, WHERE [, DLGHOOK [, FILTERPROC [, ACTIVATE, ... ]]]
  248.  
  249. Display a more sophisticated dialog for a new file.
  250.  
  251. =cut
  252. StandardFileReply
  253. CustomPutFile(prompt, defaultName, dlgID, where, dlgHook=&sv_undef, filterProc=&sv_undef, activate=&sv_undef, ...)
  254.     Str255  prompt
  255.     Str255  defaultName
  256.     short   dlgID
  257.     Point   where
  258.     SV *    dlgHook
  259.     SV *    filterProc
  260.     SV *    activate
  261.     CODE:
  262.     {
  263.         SFProcs procs;
  264.         short * activationOrder = nil;
  265.         
  266.         procs.modalFilter = filterProc;
  267.         procs.dlgHook     = dlgHook;
  268.         procs.activate    = activate;
  269.         
  270.         if (items > 7) {
  271.             activationOrder = (short *)malloc(2*(items-6));
  272.             activationOrder[0] = items-7;
  273.             while (items-- > 7) {
  274.                 XS_INPUT(short, activationOrder[items-6], ST(items));
  275.             }
  276.         }
  277.         
  278.         CustomPutFile(
  279.             prompt,
  280.             defaultName,
  281.             &RETVAL,
  282.             dlgID,
  283.             where,
  284.             SvTRUE(dlgHook) ? &sCallDlgHook : nil,
  285.             SvTRUE(filterProc) ? &sCallModalFilter : nil,
  286.             activationOrder,
  287.             SvTRUE(activate) ? &sCallActivate : nil,
  288.             &procs);
  289.         
  290.         if (activationOrder)
  291.             free(activationOrder);
  292.     }
  293.     OUTPUT:
  294.     RETVAL
  295.  
  296.  
  297. =item CustomGetFile FILEFILTER, TYPELIST, DLGID, WHERE [, DLGHOOK [, FILTERPROC [, ACTIVATE, ... ]]]
  298.  
  299. Display a more sophisticated dialog for an existing file.
  300.  
  301. =cut
  302. StandardFileReply
  303. CustomGetFile(fileFilter, typeList, dlgID, where, dlgHook=&sv_undef, filterProc=&sv_undef, activate=&sv_undef, ...)
  304.     SV *    fileFilter
  305.     SV *    typeList
  306.     short   dlgID
  307.     Point   where
  308.     SV *    dlgHook
  309.     SV *    filterProc
  310.     SV *    activate
  311.     CODE:
  312.     {
  313.         SFProcs procs;
  314.         short   numTypes;
  315.         STRLEN  len;
  316.         char *  types;
  317.         char *  typeBuf;
  318.         short * activationOrder = nil;
  319.         
  320.         procs.fileFilter  = fileFilter;
  321.         procs.modalFilter = filterProc;
  322.         procs.dlgHook     = dlgHook;
  323.         procs.activate    = activate;
  324.         
  325.         typeBuf  = 0;
  326.         if (!SvOK(typeList)) {
  327.             numTypes = -1;
  328.             types    = 0;
  329.         } else {
  330.             types = SvPV(typeList, len);
  331.             if (len & 3 && looks_like_number(typeList)) {
  332.                 XS_INPUT(short, numTypes, typeList);
  333.             } else {
  334.                 numTypes = len >> 2;
  335.                 if (numTypes && (long)types & 1) {          /* Ensure alignment */
  336.                     typeBuf = (char *)malloc(numTypes << 2);
  337.                     memcpy(typeBuf, types, numTypes << 2);
  338.                     types = typeBuf;
  339.                 } 
  340.             }
  341.         }
  342.         if (items > 7) {
  343.             activationOrder = (short *)malloc(2*(items-6));
  344.             activationOrder[0] = items-7;
  345.             while (items-- > 7) {
  346.                 XS_INPUT(short, activationOrder[items-6], ST(items));
  347.             }
  348.         }
  349.         
  350.         CustomGetFile(
  351.             SvTRUE(fileFilter) ? &sCallFileFilter : nil,
  352.             numTypes,
  353.             (OSType *)types,
  354.             &RETVAL,
  355.             dlgID,
  356.             where,
  357.             SvTRUE(dlgHook) ? &sCallDlgHook : nil,
  358.             SvTRUE(filterProc) ? &sCallModalFilter : nil,
  359.             activationOrder,
  360.             SvTRUE(activate) ? &sCallActivate : nil,
  361.             &procs);
  362.         
  363.         if (activationOrder)
  364.             free(activationOrder);
  365.         if (typeBuf)
  366.             free(typeBuf);
  367.     }
  368.     OUTPUT:
  369.     RETVAL
  370.  
  371. =back 
  372.  
  373. =cut
  374.