Subversion Repositories filter_foundry

Rev

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