Subversion Repositories filter_foundry

Rev

Rev 39 | Rev 57 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 toby 1
/*
18 toby 2
    This file is part of "Filter Foundry", a filter plugin for Adobe Photoshop
2 toby 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
/* This is PLATFORM INDEPENDENT user interface code - mainly dialog logic */
21
 
22
#include "ff.h"
23
 
24
#include "node.h"
25
#include "funcs.h"
26
#include "y.tab.h"
27
#include "choosefile.h"
28
#include "sprintf_tiny.h"
29
 
30
#ifdef MAC_ENV
31
        #include <files.h>
32
        #include <plstringfuncs.h>
33
 
34
        #define GETSLIDERVALUE GetDlgControlValue
35
        #define SETSLIDERVALUE SetDlgControlValue
36
        #define GETCTLTEXT getctltext
37
        #define SETCTLTEXT setctltext
38
        #define GETCTLTEXTINT getctltextint
39
        #define SETCTLTEXTINT setctltextint
40
        #define SELECTCTLTEXT selectctltext
41
#else
42
        #include <commctrl.h>
43
        #include "compat_string.h"
44
 
45
        #define GETSLIDERVALUE(d,i) SendDlgItemMessage(d,i,TBM_GETPOS,0,0)
46
        #define SETSLIDERVALUE(d,i,v) SendDlgItemMessage(d,i,TBM_SETPOS,TRUE,v)
47
        #define GETCTLTEXT GetDlgItemText
48
        #define SETCTLTEXT SetDlgItemText
49
        #define SELECTCTLTEXT SELECTDLGITEMTEXT
50
        #define GETCTLTEXTINT GetDlgItemInt
51
        #define SETCTLTEXTINT SetDlgItemInt
52
#endif
53
 
54
Boolean doupdates = true;
55
double zoomfactor,fitzoom;
56
 
57
void updateglobals(DIALOGREF dp);
58
struct node *updateexpr(DIALOGREF dp,int i);
59
void updatedialog(DIALOGREF dp);
60
void slidertextchanged(DIALOGREF dp,int item);
61
void updatezoom(DIALOGREF dp);
62
 
63
void updatedialog(DIALOGREF dp){
64
        int i;
65
 
66
        doupdates = false;
67
 
68
        for( i=0 ; i<8 ; ++i ){
69
                SETSLIDERVALUE(dp,FIRSTCTLITEM+i,slider[i]);
70
                SETCTLTEXTINT(dp,FIRSTCTLTEXTITEM+i,slider[i],false);
71
        }
72
 
73
        for( i=0 ; i<4 ; ++i ){
74
                if(!gdata->standalone) SETCTLTEXT(dp,FIRSTEXPRITEM+i,expr[i] ? expr[i] : "oups! expr[i] is nil!");
75
                if(i<nplanes)
76
                        updateexpr(dp,FIRSTEXPRITEM+i);
77
        }
78
 
79
        if(!gdata->standalone) SELECTCTLTEXT(dp,FIRSTEXPRITEM,0,-1);
80
 
81
        doupdates = true;
82
}
83
 
84
/* copy dialog settings to global variables (sliders, expressions) */
85
 
86
void updateglobals(DIALOGREF dp){
87
        int i;
88
        char s[MAXEXPR+1];
89
 
90
        for( i=0 ; i<8 ; ++i )
91
                slider[i] = GETSLIDERVALUE(dp,FIRSTCTLITEM+i);
92
 
93
        if(!gdata->standalone)
94
                for( i=0 ; i<4 ; ++i ){
95
                        /* stash expression strings */
96
                        if(GETCTLTEXT(dp,FIRSTEXPRITEM+i,s,MAXEXPR)){
97
                                if(expr[i])
98
                                        free(expr[i]);
99
                                if(!(expr[i] = my_strdup(s)))
100
                                        dbg("updateglobals: my_strdup returned zero??");
101
                        }else
102
                                dbg("updateglobals: GETCTLTEXT returned zero??");
103
                }
104
}
105
 
106
struct node *updateexpr(DIALOGREF dp,int item){
107
        char s[MAXEXPR+1];
108
        int i;
109
 
110
//dbg("updateexpr");
111
 
112
        i = item - FIRSTEXPRITEM;
113
 
114
        freetree(tree[i]);
115
 
116
        if(!gdata->standalone){
117
                GETCTLTEXT(dp,item,s,MAXEXPR);
118
 
119
                if(expr[i])
120
                        free(expr[i]);
121
                expr[i] = my_strdup(s);
122
        }
123
 
124
        tree[i] = parseexpr(expr[i]);
125
 
126
        if(!gdata->standalone){
127
                if(tree[i])
128
                        HideDialogItem(dp,FIRSTICONITEM+i);
129
                else{
130
                        err[i] = errstr;
131
                        errstart[i] = tokstart;
132
                        errpos[i] = tokpos;
133
                        ShowDialogItem(dp,FIRSTICONITEM+i);
134
                }
135
        }
136
        return tree[i];
137
}
138
 
139
void updatezoom(DIALOGREF dp){
140
        char s[10],*q = int_str(s,(int)(100./zoomfactor),10);
141
        *q++ = '%';
142
        *q = 0;
143
        SETCTLTEXT(dp,ZOOMLEVELITEM,s);
144
        zoomfactor > 1. ? ShowDialogItem(dp,ZOOMINITEM) : HideDialogItem(dp,ZOOMINITEM);
145
        zoomfactor < fitzoom ? ShowDialogItem(dp,ZOOMOUTITEM) : HideDialogItem(dp,ZOOMOUTITEM);
146
}
147
 
148
/* traverse expression tree, looking for constant references to sliders */
149
 
150
int checksl(struct node*p,int ctlflags[],int mapflags[]);
151
int checksl(struct node*p,int ctlflags[],int mapflags[]){
152
        if(p){
153
                int s;
154
                if( (p->kind==TOK_FN1 && p->v.sym->fn == (pfunc_type)ff_ctl)
155
                 || (p->kind==TOK_FN3 && p->v.sym->fn == (pfunc_type)ff_val) ){
156
                        if(p->child[0]->kind == TOK_NUM){
157
                                s = p->child[0]->v.value;
158
                                if(s>=0 && s<=7)
159
                                        ctlflags[s] = 1;
160
                        }else
161
                                return true; /* can't determine which ctl() */
23 toby 162
                }else if( p->kind==TOK_FN2 && p->v.sym->fn == (pfunc_type)ff_map ){
2 toby 163
                        if(p->child[0]->kind == TOK_NUM){
164
                                s = p->child[0]->v.value;
165
                                if(s>=0 && s<=3){
166
                                        mapflags[s] = 1;
167
                                        ctlflags[s*2] = ctlflags[s*2+1] = 1;
168
                                }
169
                        }else
170
                                return true; /* can't determine which map() */
23 toby 171
                 }
2 toby 172
 
173
                return checksl(p->child[0],ctlflags,mapflags)
174
                        || checksl(p->child[1],ctlflags,mapflags)
175
                        || checksl(p->child[2],ctlflags,mapflags)
176
                        || checksl(p->child[3],ctlflags,mapflags)
177
                        || checksl(p->child[4],ctlflags,mapflags);     
178
        }else return false;
179
}
180
 
181
Boolean checksliders(int exprs,int ctlflags[],int mapflags[]){
182
        int i,f = false;
183
 
184
        for(i=4;i--;)
185
                mapflags[i] = 0;
186
        for(i=8;i--;)
187
                ctlflags[i] = 0;
188
 
189
        for(i=0;i<exprs;i++)
190
                if(checksl(tree[i],ctlflags,mapflags))
191
                        f = true;
192
 
193
        return f;
194
}
195
 
196
void slidermoved(DIALOGREF dp,int i){
197
        int v = GETSLIDERVALUE(dp,i);
198
        i -= FIRSTCTLITEM;
199
        slider[i] = v;
200
        SETCTLTEXTINT(dp,i+FIRSTCTLTEXTITEM,v,false);
201
}
202
 
203
void slidertextchanged(DIALOGREF dp,int i){
204
        int v = GETCTLTEXTINT(dp,i,NULL,false);
205
        i -= FIRSTCTLTEXTITEM;
206
        SETSLIDERVALUE(dp,i+FIRSTCTLITEM,v);
207
        slider[i] = v;
208
}
209
 
210
void maindlgupdate(DIALOGREF dp){
211
        int i,unknown,ctls[8],maps[4];
212
 
213
        unknown = checksliders(nplanes,ctls,maps);
214
 
215
        for(i=0;i<8;i++)
216
                if(unknown || ctls[i]){
217
                        ENABLEDLGITEM(dp,FIRSTCTLITEM+i);
218
                        ShowDialogItem(dp,FIRSTCTLTEXTITEM+i); /* FIXME: this changes keyboard focus */
219
                }else{
220
                        DISABLEDLGITEM(dp,FIRSTCTLITEM+i);
221
                        HideDialogItem(dp,FIRSTCTLTEXTITEM+i); /* FIXME: this changes keyboard focus */
222
                }
223
 
224
        for( i=0 ; i<nplanes ; i++ )
225
                if(!tree[i]){
226
                        /* uh oh, couldn't parse one of the saved expressions...this is fatal */
227
                        DISABLEDLGITEM(dp,IDOK);
228
                        if(gdata->standalone){
229
                                alertuser("Can't run this filter (there is a problem with the saved expressions).","");
230
                        }else{
231
                                DISABLEDLGITEM(dp,SAVEITEM);
232
                                DISABLEDLGITEM(dp,MAKEITEM);
233
                        }
234
                        return;
235
                }
236
 
237
        /* we have valid expression trees in all slots...proceed! */
238
        updateglobals(dp);
239
        if(setup(gpb))
240
                recalc_preview(gpb,dp);
241
 
242
        ENABLEDLGITEM(dp,IDOK);
243
        if(!gdata->standalone){
244
                ENABLEDLGITEM(dp,SAVEITEM);
245
                ENABLEDLGITEM(dp,MAKEITEM);
246
        }
247
}
248
 
249
/* one-time initialisation of dialog box */
250
 
251
void maindlginit(DIALOGREF dp){
252
        char s[0x100];
253
        int i;
254
 
255
        /* hide unused expression items */
256
        if(gdata->standalone){
257
                myp2cstrcpy(s,gdata->parm.author); SetDlgItemText(dp,PARAMAUTHORITEM,s);
258
                myp2cstrcpy(s,gdata->parm.copyright); SetDlgItemText(dp,PARAMCOPYITEM,s);
259
                for(i=0;i<8;++i){
260
                        if(gdata->parm.ctl_used[i]){
261
                                myp2cstrcpy(s,gdata->parm.ctl[i]); SetDlgItemText(dp,FIRSTCTLLABELITEM+i,s);
262
                        }else if(gdata->parm.map_used[i/2]){
263
                                if(i&1)
264
                                        HideDialogItem(dp,FIRSTCTLLABELITEM+i);
265
                                else{
266
                                        myp2cstrcpy(s,gdata->parm.map[i/2]); SetDlgItemText(dp,FIRSTCTLLABELITEM+i,s);
267
                                }
268
                        }else{
269
                                HideDialogItem(dp,FIRSTCTLITEM+i);
270
                                HideDialogItem(dp,FIRSTCTLTEXTITEM+i);
271
                                HideDialogItem(dp,FIRSTCTLLABELITEM+i);
272
                        }
273
                }
274
        }else
275
                for(i=nplanes;i<4;++i){
276
                        HideDialogItem(dp,FIRSTICONITEM+i);
277
                        HideDialogItem(dp,FIRSTEXPRITEM+i);
278
                        HideDialogItem(dp,FIRSTLABELITEM+i);
279
                }
280
 
281
        if(setup_preview(gpb)){
282
                extern int preview_w,preview_h;
283
                double zh = (gpb->filterRect.right-gpb->filterRect.left)/(double)preview_w,
37 toby 284
                           zv = (gpb->filterRect.bottom-gpb->filterRect.top)/(double)preview_h,k;
2 toby 285
                fitzoom = zh > zv ? zh : zv;
37 toby 286
 
287
                // On very large images, processing a fully zoomed out preview (the initial default)
288
                // can cause out of memory errors, because Photoshop can't page in all data
289
                // during advanceState. To prevent this problem, zoom in until we aren't
290
                // previewing more than say 10% of Photoshop's indicated maxSpace.
291
                // (e.g., on a 1GB WinXP system, PS CS2 reports 520MB maxSpace, so this will let us
292
                // preview about 50MB of image data.)
293
 
294
                k = maxSpace/(10.*preview_w*preview_h*nplanes);
295
                for( zoomfactor = fitzoom ; zoomfactor >= 2. && zoomfactor*zoomfactor > k ; )
296
                        zoomfactor /= 2.;
297
 
2 toby 298
                updatezoom(dp);
299
        }else{
300
                HideDialogItem(dp,ZOOMINITEM);
301
                HideDialogItem(dp,ZOOMOUTITEM);
302
                HideDialogItem(dp,ZOOMLEVELITEM);
303
        }
304
 
305
#ifdef WIN_ENV
9 toby 306
  // can't build standalone filter on less than NT platform :-(
307
  // due to absence of resource editing API (UpdateResource etc)
308
  if(!isWin32NT())
309
    HideDialogItem(dp,MAKEITEM);
2 toby 310
#endif
311
 
312
        updatedialog(dp);
313
        maindlgupdate(dp);
314
}
315
 
316
 
317
/* process an item hit. return false if the dialog is finished; otherwise return true. */
318
 
319
Boolean maindlgitem(DIALOGREF dp,int item){
37 toby 320
        extern int previewerr;
321
 
2 toby 322
        StandardFileReply sfr;
323
        NavReplyRecord reply;
324
        static OSType types[] = {TEXT_FILETYPE,PS_FILTER_FILETYPE};
325
        char *reason;
326
        Str255 fname;
327
 
328
        switch(item){
329
        case IDOK:     
330
//              updateglobals(dp);
331
        case IDCANCEL:
332
                dispose_preview();
333
                return false; // end dialog
334
        case OPENITEM:
32 toby 335
                if(!gdata->standalone && choosefiletypes("\pChoose filter settings",&sfr,&reply,types,2,
336
                                        "All supported files (.AFS, .8BF, .TXT)\0*.AFS;*.8BF;*.TXT\0All files (*.*)\0*.*\0\0")){
2 toby 337
                        if(loadfile(&sfr,&reason)){
338
                                updatedialog(dp);
339
                                maindlgupdate(dp);
340
                        }else alertuser("Cannot load settings.",reason);
341
                }
342
                break;
343
        case SAVEITEM:
39 toby 344
                if(!gdata->standalone && putfile("\pSave filter settings",(StringPtr)"",
32 toby 345
                                                                                 TEXT_FILETYPE,SIG_SIMPLETEXT,&reply,&sfr,
346
                                                                                 "AFS","Settings file (.AFS, .TXT)\0*.AFS;*.TXT\0\0",1)){
2 toby 347
//                      updateglobals(dp);
348
                        if(savefile(&sfr))
349
                                completesave(&reply);
350
                }
351
                break;
352
        case MAKEITEM:
353
                if( !gdata->standalone && builddialog(gpb) ){
354
                        PLstrcpy(fname,gdata->parm.title);
11 toby 355
#ifdef MACMACHO
356
      PLstrcat(fname,(StringPtr)"\p.plugin");
357
#endif
2 toby 358
                        if( putfile("\pMake standalone filter",fname,
32 toby 359
                                                PS_FILTER_FILETYPE,kPhotoshopSignature,&reply,&sfr,
360
                                                "8BF","Filter plugin file (.8BF)\0*.8BF\0\0",1) )
2 toby 361
                                make_standalone(&sfr);
362
                }
363
                break;
364
        case ZOOMINITEM:
365
                zoomfactor = zoomfactor>2. ? zoomfactor/2. : 1.;
366
                updatezoom(dp);
37 toby 367
                previewerr = false;
2 toby 368
                recalc_preview(gpb,dp);
369
                break;
370
        case ZOOMOUTITEM:
371
                zoomfactor *= 2.;
372
                if(zoomfactor > fitzoom)
373
                        zoomfactor = fitzoom;
374
                updatezoom(dp);
37 toby 375
                previewerr = false;
2 toby 376
                recalc_preview(gpb,dp);
377
                break;
378
        case ZOOMLEVELITEM:
41 toby 379
                zoomfactor = zoomfactor > 1. ? 1. : fitzoom;
2 toby 380
                updatezoom(dp);
37 toby 381
                previewerr = false;
2 toby 382
                recalc_preview(gpb,dp);
383
                break;
384
        case FIRSTCTLITEM:
385
        case FIRSTCTLITEM+1:
386
        case FIRSTCTLITEM+2:
387
        case FIRSTCTLITEM+3:
388
        case FIRSTCTLITEM+4:
389
        case FIRSTCTLITEM+5:
390
        case FIRSTCTLITEM+6:
391
        case FIRSTCTLITEM+7:
392
                slidermoved(dp,item);
393
                recalc_preview(gpb,dp);
394
                break;
395
        case FIRSTCTLTEXTITEM:
396
        case FIRSTCTLTEXTITEM+1:
397
        case FIRSTCTLTEXTITEM+2:
398
        case FIRSTCTLTEXTITEM+3:
399
        case FIRSTCTLTEXTITEM+4:
400
        case FIRSTCTLTEXTITEM+5:
401
        case FIRSTCTLTEXTITEM+6:
402
        case FIRSTCTLTEXTITEM+7:
403
                slidertextchanged(dp,item);
404
                recalc_preview(gpb,dp);
405
                break;
406
        case FIRSTICONITEM:
407
        case FIRSTICONITEM+1:
408
        case FIRSTICONITEM+2:
409
        case FIRSTICONITEM+3:
410
                item -= FIRSTICONITEM;
411
                alertuser(err[item],"");
412
                SELECTCTLTEXT(dp,FIRSTEXPRITEM+item,errstart[item],errpos[item]);
413
                break;
414
        case FIRSTEXPRITEM:
415
        case FIRSTEXPRITEM+1:
416
        case FIRSTEXPRITEM+2:
417
        case FIRSTEXPRITEM+3:
418
//              dbg("expritem hit");
419
                if( (item-FIRSTEXPRITEM)<nplanes ){
420
                        updateexpr(dp,item);
421
                        maindlgupdate(dp);
422
                }
423
                break;
424
        }
425
 
426
        return true; // keep going
427
}
428
 
429
Boolean alertuser(char *err,char *more){
430
        char *s = malloc(strlen(err)+strlen(more)+2),*q;
431
        Boolean res;
432
 
433
        q = cat(s,err);
434
        *q++ = '\n';
435
        q = cat(q,more);
436
        *q = 0;
437
        res = simplealert(s);
438
        free(s);
439
        return res;
440
}