Subversion Repositories filter_foundry

Rev

Rev 15 | Rev 21 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*
  2.     This file is part of "Filter Foundry", a filter plugin for Adobe Photoshop
  3.     Copyright (C) 2003-5 Toby Thain, toby@telegraphics.com.au
  4.  
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by  
  7.     the Free Software Foundation; either version 2 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License  
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18. */
  19.  
  20. /* Portions Copyright 1996 - 1999 Adobe Systems Incorporated                */
  21. /* All Rights Reserved.                                            */
  22.  
  23. //#include <stdio.h>
  24.  
  25. //#include "world.h" // must come before Photoshop includes
  26.  
  27. #include "ff.h"
  28.  
  29. #include "scripting.h"
  30. //#include "ui.h"
  31. #include "dbg.h"
  32.  
  33. //extern FilterRecordPtr gpb;
  34.  
  35. OSErr put_cstring(PIWriteDescriptor token,DescriptorKeyID key,char *s){
  36.         int n = strlen(s);
  37.         Ptr p;
  38.         Handle h = PINEWHANDLE(n);
  39.         p = PILOCKHANDLE(h,false);
  40.         memcpy(p,s,n);
  41.         PIUNLOCKHANDLE(h);
  42.         return PIPutText(token,key,h);
  43.         /* FIXME: not sure if we are supposed to dispose of handle */
  44. }
  45.  
  46. char *get_cstring(PIReadDescriptor token){
  47.         int n;
  48.         Ptr p;
  49.         char *str = NULL;
  50.         Handle h;
  51.         OSErr e = PIGetText(token,&h);
  52.  
  53.         if(!e && h){
  54.                 n = PIGETHANDLESIZE(h);
  55.                 p = PILOCKHANDLE(h,false);
  56.                 //sprintf(str,"get_cstring: token=%#x s=%#x h=%#x p=%#x n=%d",token,s,h,p,n); dbg(str);
  57.                 if(str = malloc(n+1)){
  58.                         memcpy(str,p,n);
  59.                         str[n] = 0;
  60.                 }
  61.                 PIUNLOCKHANDLE(h);
  62.                 /* FIXME: not sure if we are supposed to dispose of handle */
  63.         }
  64.         return str;
  65. }
  66.  
  67. /* return true if dialog should be shown */
  68.  
  69. Boolean ReadScriptParamsOnRead(void)
  70. {
  71.         PIReadDescriptor token;
  72.         DescriptorKeyID key;
  73.         DescriptorTypeID type;
  74.         DescriptorKeyIDArray array = { NULLID };
  75.         int32 flags;
  76.         OSErr stickyError;
  77.         long v;
  78.        
  79.         if (DescriptorAvailable(NULL)){ /* playing back.  Do our thing. */
  80.                 token = OpenReader(array);
  81.                 if (token){
  82.                         while (PIGetKey(token, &key, &type, &flags)){
  83.                                 switch (key){char s[10];
  84.                                         case PARAM_R_KEY: expr[0] = get_cstring(token); break;
  85.                                         case PARAM_G_KEY: expr[1] = get_cstring(token); break;
  86.                                         case PARAM_B_KEY: expr[2] = get_cstring(token); break;
  87.                                         case PARAM_A_KEY: expr[3] = get_cstring(token); break;
  88.                                         default:
  89.                                                 key -= PARAM_CTL0_KEY;
  90.                                                 if(key >=0 && key < 8){
  91.                                                         PIGetInt(token,&v);
  92.                                                         slider[key] = v;
  93.                                                 }
  94.                                                 //sprintf(s,"%d:%d",key,v);dbg(s);
  95.                                                 break;
  96.                                 }
  97.                         }
  98.  
  99.                         stickyError = CloseReader(&token); // closes & disposes.
  100.                                
  101.                         // all Filter Foundry parameters are regarded as optional,
  102.                         // so we needn't worry if any are missing
  103.                         if (stickyError){
  104.                                 if (stickyError == errMissingParameter) // missedParamErr == -1715
  105.                                         ;
  106.                                         /* (descriptorKeyIDArray != NULL)
  107.                                            missing parameter somewhere.  Walk IDarray to find which one. */
  108.                                 else
  109.                                         ; //gResult = stickyError;
  110.                         }
  111.                 }
  112.                
  113.                 return gpb->descriptorParameters->playInfo == plugInDialogDisplay; /* TRUE if want to show our Dialog */               
  114.         }
  115.        
  116.         return true;
  117. }
  118.  
  119. OSErr WriteScriptParamsOnRead(void)
  120. {
  121.         PIWriteDescriptor token;
  122.         OSErr gotErr = noErr;
  123.         extern int ctls[],maps[];
  124.         int i,allctls;
  125.                        
  126.         if (DescriptorAvailable(NULL)){ /* recording.  Do our thing. */
  127.                 token = OpenWriter();
  128.                 if (token){
  129.                         // write keys here
  130.                         if(!gdata->standalone){
  131.                                 put_cstring(token, PARAM_R_KEY, expr[0]);
  132.                                 put_cstring(token, PARAM_G_KEY, expr[1]);
  133.                                 put_cstring(token, PARAM_B_KEY, expr[2]);
  134.                                 put_cstring(token, PARAM_A_KEY, expr[3]);
  135.                         }
  136.  
  137.                         /* only write values for the sliders that are actually used! */
  138.                         allctls = checksliders(4,ctls,maps);
  139.                         for(i=0;i<8;++i)
  140.                                 if(allctls||ctls[i])
  141.                                         PIPutInt(token, PARAM_CTL0_KEY+i, slider[i]);
  142.  
  143.                         gotErr = CloseWriter(&token); /* closes and sets dialog optional */
  144.                         /* done.  Now pass handle on to Photoshop */
  145.                 }
  146.         }
  147.         return gotErr;
  148. }
  149.  
  150.  
  151. //-------------------------------------------------------------------------------
  152. //
  153. //      HostDescriptorAvailable
  154. //     
  155. //      Determines whether the PIDescriptorParameters callback is available.
  156. //
  157. //      Check for valid suite version, routine suite version, and routine count.
  158. //      Also check that the subset of routines we actually use is actually present.
  159. //
  160. //-------------------------------------------------------------------------------
  161.  
  162. Boolean HostDescriptorAvailable (PIDescriptorParameters *procs,
  163.                                                                  Boolean *outNewerVersion)
  164. {
  165.         if(outNewerVersion)
  166.                 *outNewerVersion = procs->descriptorParametersVersion > kCurrentDescriptorParametersVersion
  167.                         || procs->readDescriptorProcs->readDescriptorProcsVersion > kCurrentReadDescriptorProcsVersion
  168.                         || procs->writeDescriptorProcs->writeDescriptorProcsVersion > kCurrentWriteDescriptorProcsVersion ;
  169.        
  170.         return procs != NULL
  171.                 && procs->descriptorParametersVersion == kCurrentDescriptorParametersVersion
  172.  
  173.                 && procs->readDescriptorProcs != NULL
  174.                 && procs->readDescriptorProcs->readDescriptorProcsVersion == kCurrentReadDescriptorProcsVersion
  175.                 && procs->readDescriptorProcs->numReadDescriptorProcs >= kCurrentReadDescriptorProcsCount
  176.                 && procs->readDescriptorProcs->openReadDescriptorProc != NULL
  177.                 && procs->readDescriptorProcs->closeReadDescriptorProc != NULL
  178.                 && procs->readDescriptorProcs->getKeyProc != NULL
  179.                 && procs->readDescriptorProcs->getTextProc != NULL
  180.                 && procs->readDescriptorProcs->getIntegerProc != NULL
  181.  
  182.                 && procs->writeDescriptorProcs != NULL
  183.                 && procs->writeDescriptorProcs->writeDescriptorProcsVersion == kCurrentWriteDescriptorProcsVersion
  184.                 && procs->writeDescriptorProcs->numWriteDescriptorProcs >= kCurrentWriteDescriptorProcsCount
  185.                 && procs->writeDescriptorProcs->openWriteDescriptorProc != NULL
  186.                 && procs->writeDescriptorProcs->closeWriteDescriptorProc != NULL
  187.                 && procs->writeDescriptorProcs->putTextProc != NULL
  188.                 && procs->writeDescriptorProcs->putIntegerProc != NULL ;
  189. }
  190.  
  191.  
  192. //-------------------------------------------------------------------------------
  193. //
  194. //      HostCloseReader
  195. //     
  196. //      Closes a read token, disposes its handle, sets the token to NULL, and
  197. //      sets the parameter blocks' descriptor to NULL.
  198. //
  199. //      The Descriptor Parameters suite are callbacks designed for
  200. //      scripting and automation.  See PIActions.h.
  201. //
  202. //      Inputs:
  203. //              PIDescriptorParameters *procs   Pointer to Descriptor Parameters suite.
  204. //
  205. //              HandleProcs *hProcs                             Pointer to HandleProcs callback.
  206. //
  207. //              PIReadDescriptor *token                 Pointer to token to close.
  208. //
  209. //              procs->descriptor                               Pointer to original read handle.
  210. //
  211. //      Outputs:
  212. //              PIReadDescriptor *token                 Set to NULL.
  213. //
  214. //              procs->descriptor                               Disposed then set to NULL.
  215. //
  216. //              returns OSErr                                   noErr or error if one occurred.
  217. //
  218. //-------------------------------------------------------------------------------
  219.  
  220. OSErr HostCloseReader (PIDescriptorParameters *procs,
  221.                                            HandleProcs *hProcs,
  222.                                            PIReadDescriptor *token)
  223. {
  224.         // Close token:
  225.         OSErr err = procs->readDescriptorProcs->closeReadDescriptorProc(*token);
  226.        
  227.         // Dispose the parameter block descriptor:
  228.         hProcs->disposeProc(procs->descriptor);
  229.        
  230.         // Set the descriptor and the read token to NULL:
  231.         procs->descriptor = NULL;
  232.         *token = NULL;
  233.        
  234.         return err;
  235.  
  236. } // end HostCloseReader
  237.  
  238. //-------------------------------------------------------------------------------
  239. //
  240. //      HostCloseWriter
  241. //     
  242. //      Closes a write token, stores its handle in the global parameter block for
  243. //      the host to use, sets the token to NULL, and sets the recordInfo to
  244. //      plugInDialogOptional (the default).
  245. //
  246. //      The Descriptor Parameters suite are callbacks designed for
  247. //      scripting and automation.  See PIActions.h.
  248. //
  249. //      Inputs:
  250. //              PIDescriptorParameters *procs   Pointer to Descriptor Parameters suite.
  251. //
  252. //              HandleProcs *hProcs                             Pointer to HandleProcs callback.
  253. //
  254. //              PIWriteDescriptor *token                Pointer to token to close and pass on.
  255. //
  256. //              procs->descriptor                               Should be NULL.  If not, its contents
  257. //                                                                              will be disposed and replaced.
  258. //
  259. //      Outputs:
  260. //              PIWriteDescriptor *token                Set to NULL.
  261. //
  262. //              procs->descriptor                               Set to descriptor handle.
  263. //
  264. //              returns OSErr                                   noErr or error if one occurred.
  265. //
  266. //-------------------------------------------------------------------------------
  267.  
  268. OSErr   HostCloseWriter(PIDescriptorParameters *procs,
  269.                                                 HandleProcs *hProcs,
  270.                                                 PIWriteDescriptor *token)
  271. {
  272.         OSErr err = noErr; // assume no error
  273.         PIDescriptorHandle h = NULL;
  274.        
  275.         if (procs->descriptor != NULL) // don't need descriptor passed to us
  276.                 hProcs->disposeProc(procs->descriptor); // dispose.
  277.         procs->writeDescriptorProcs->closeWriteDescriptorProc(*token, &h);
  278.         procs->descriptor = h;
  279.        
  280.         // Set recordInfo to default.  Options are: plugInDialogOptional,
  281.         // plugInDialogRequire, plugInDialogNone:
  282.         procs->recordInfo = plugInDialogOptional;
  283.  
  284.         *token = NULL;
  285.        
  286.         return err;
  287.        
  288. } // end HostCloseWriter
  289.