Subversion Repositories filter_foundry

Rev

Rev 15 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 toby 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
Boolean ReadScriptParamsOnRead(void)
68
{
69
        PIReadDescriptor token;
70
        DescriptorKeyID key;
71
        DescriptorTypeID type;
72
        DescriptorKeyIDArray array = { NULLID };
73
        int32 flags;
74
        OSErr stickyError;
75
        Boolean returnValue = true;
76
        long v;
77
 
78
        if (DescriptorAvailable(NULL)){ /* playing back.  Do our thing. */
79
                token = OpenReader(array);
80
                if (token){
81
                        while (PIGetKey(token, &key, &type, &flags)){
82
                                switch (key){
83
                                        case PARAM_R_KEY: expr[0] = get_cstring(token); break;
84
                                        case PARAM_G_KEY: expr[1] = get_cstring(token); break;
85
                                        case PARAM_B_KEY: expr[2] = get_cstring(token); break;
86
                                        case PARAM_A_KEY: expr[3] = get_cstring(token); break;
87
                                        default:
88
                                                key -= PARAM_CTL0_KEY;
89
                                                if(key >=0 && key < 8){
90
                                                        PIGetInt(token,&v);
91
                                                        slider[key] = v;
92
                                                }
93
                                                break;
94
                                }
95
                        }
96
 
97
                        stickyError = CloseReader(&token); // closes & disposes.
98
 
99
                        // all Filter Foundry parameters are regarded as optional,
100
                        // so we needn't worry if any are missing
101
                        if (stickyError){
102
                                if (stickyError == errMissingParameter) // missedParamErr == -1715
103
                                        ;
104
                                        /* (descriptorKeyIDArray != NULL)
105
                                           missing parameter somewhere.  Walk IDarray to find which one. */
106
                                else
107
                                        ; //gResult = stickyError;
108
                        }
109
                }
110
 
111
                returnValue = gpb->descriptorParameters->playInfo == plugInDialogDisplay;
112
                /* return TRUE if want to show our Dialog */           
113
        }
114
 
115
        return returnValue;
116
}
117
 
118
OSErr WriteScriptParamsOnRead(void)
119
{
120
        PIWriteDescriptor token;
121
        OSErr gotErr = noErr;
122
        extern int ctls[],maps[];
123
        int i,allctls;
124
 
125
        if (DescriptorAvailable(NULL)){ /* recording.  Do our thing. */
126
                token = OpenWriter();
127
                if (token){
128
                        // write keys here
129
                        if(!gdata->standalone){
130
                                put_cstring(token, PARAM_R_KEY, expr[0]);
131
                                put_cstring(token, PARAM_G_KEY, expr[1]);
132
                                put_cstring(token, PARAM_B_KEY, expr[2]);
133
                                put_cstring(token, PARAM_A_KEY, expr[3]);
134
                        }
135
 
136
                        /* only write values for the sliders that are actually used! */
137
                        allctls = checksliders(4,ctls,maps);
138
                        for(i=0;i<8;++i)
139
                                if(allctls||ctls[i])
140
                                        PIPutInt(token, PARAM_CTL0_KEY+i, slider[i]);
141
 
142
                        gotErr = CloseWriter(&token); /* closes and sets dialog optional */
143
                        /* done.  Now pass handle on to Photoshop */
144
                }
145
        }
146
        return gotErr;
147
}
148
 
149
 
150
//-------------------------------------------------------------------------------
151
//
152
//      HostDescriptorAvailable
153
//      
154
//      Determines whether the PIDescriptorParameters callback is available.
155
//
156
//      Check for valid suite version, routine suite version, and routine count.
157
//      Also check that the subset of routines we actually use is actually present.
158
//
159
//-------------------------------------------------------------------------------
160
 
161
Boolean HostDescriptorAvailable (PIDescriptorParameters *procs,
162
                                                                 Boolean *outNewerVersion)
163
{
164
        if(outNewerVersion)
165
                *outNewerVersion = procs->descriptorParametersVersion > kCurrentDescriptorParametersVersion
166
                        || procs->readDescriptorProcs->readDescriptorProcsVersion > kCurrentReadDescriptorProcsVersion
167
                        || procs->writeDescriptorProcs->writeDescriptorProcsVersion > kCurrentWriteDescriptorProcsVersion ;
168
 
169
        return procs != NULL
170
                && procs->descriptorParametersVersion == kCurrentDescriptorParametersVersion
171
 
172
                && procs->readDescriptorProcs != NULL
173
                && procs->readDescriptorProcs->readDescriptorProcsVersion == kCurrentReadDescriptorProcsVersion
174
                && procs->readDescriptorProcs->numReadDescriptorProcs >= kCurrentReadDescriptorProcsCount
175
                && procs->readDescriptorProcs->openReadDescriptorProc != NULL
176
                && procs->readDescriptorProcs->closeReadDescriptorProc != NULL
177
                && procs->readDescriptorProcs->getKeyProc != NULL
178
                && procs->readDescriptorProcs->getTextProc != NULL
179
                && procs->readDescriptorProcs->getIntegerProc != NULL
180
 
181
                && procs->writeDescriptorProcs != NULL
182
                && procs->writeDescriptorProcs->writeDescriptorProcsVersion == kCurrentWriteDescriptorProcsVersion
183
                && procs->writeDescriptorProcs->numWriteDescriptorProcs >= kCurrentWriteDescriptorProcsCount
184
                && procs->writeDescriptorProcs->openWriteDescriptorProc != NULL
185
                && procs->writeDescriptorProcs->closeWriteDescriptorProc != NULL
186
                && procs->writeDescriptorProcs->putTextProc != NULL
187
                && procs->writeDescriptorProcs->putIntegerProc != NULL ;
188
}
189
 
190
 
191
//-------------------------------------------------------------------------------
192
//
193
//      HostCloseReader
194
//      
195
//      Closes a read token, disposes its handle, sets the token to NULL, and
196
//      sets the parameter blocks' descriptor to NULL.
197
//
198
//      The Descriptor Parameters suite are callbacks designed for
199
//      scripting and automation.  See PIActions.h.
200
//
201
//      Inputs:
202
//              PIDescriptorParameters *procs   Pointer to Descriptor Parameters suite.
203
//
204
//              HandleProcs *hProcs                             Pointer to HandleProcs callback.
205
//
206
//              PIReadDescriptor *token                 Pointer to token to close.
207
//
208
//              procs->descriptor                               Pointer to original read handle.
209
//
210
//      Outputs:
211
//              PIReadDescriptor *token                 Set to NULL.
212
//
213
//              procs->descriptor                               Disposed then set to NULL.
214
//
215
//              returns OSErr                                   noErr or error if one occurred.
216
//
217
//-------------------------------------------------------------------------------
218
 
219
OSErr HostCloseReader (PIDescriptorParameters *procs,
220
                                           HandleProcs *hProcs,
221
                                           PIReadDescriptor *token)
222
{
223
        // Close token:
224
        OSErr err = procs->readDescriptorProcs->closeReadDescriptorProc(*token);
225
 
226
        // Dispose the parameter block descriptor:
227
        hProcs->disposeProc(procs->descriptor);
228
 
229
        // Set the descriptor and the read token to NULL:
230
        procs->descriptor = NULL;
231
        *token = NULL;
232
 
233
        return err;
234
 
235
} // end HostCloseReader
236
 
237
//-------------------------------------------------------------------------------
238
//
239
//      HostCloseWriter
240
//      
241
//      Closes a write token, stores its handle in the global parameter block for
242
//      the host to use, sets the token to NULL, and sets the recordInfo to 
243
//      plugInDialogOptional (the default).
244
//
245
//      The Descriptor Parameters suite are callbacks designed for
246
//      scripting and automation.  See PIActions.h.
247
//
248
//      Inputs:
249
//              PIDescriptorParameters *procs   Pointer to Descriptor Parameters suite.
250
//
251
//              HandleProcs *hProcs                             Pointer to HandleProcs callback.
252
//
253
//              PIWriteDescriptor *token                Pointer to token to close and pass on.
254
//
255
//              procs->descriptor                               Should be NULL.  If not, its contents
256
//                                                                              will be disposed and replaced.
257
//
258
//      Outputs:
259
//              PIWriteDescriptor *token                Set to NULL.
260
//
261
//              procs->descriptor                               Set to descriptor handle.
262
//
263
//              returns OSErr                                   noErr or error if one occurred.
264
//
265
//-------------------------------------------------------------------------------
266
 
267
OSErr   HostCloseWriter(PIDescriptorParameters *procs,
268
                                                HandleProcs *hProcs,
269
                                                PIWriteDescriptor *token)
270
{
271
        OSErr err = noErr; // assume no error
272
        PIDescriptorHandle h = NULL;
273
 
274
        if (procs->descriptor != NULL) // don't need descriptor passed to us
275
                hProcs->disposeProc(procs->descriptor); // dispose.
276
        procs->writeDescriptorProcs->closeWriteDescriptorProc(*token, &h);
277
        procs->descriptor = h;
278
 
279
        // Set recordInfo to default.  Options are: plugInDialogOptional,
280
        // plugInDialogRequire, plugInDialogNone:
281
        procs->recordInfo = plugInDialogOptional;
282
 
283
        *token = NULL;
284
 
285
        return err;
286
 
287
} // end HostCloseWriter