Subversion Repositories filter_foundry

Rev

Rev 169 | Rev 172 | 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
171 dmarschall 3
    Copyright (C) 2003-2019 Toby Thain, toby@telegraphics.com.au
2 toby 4
 
5
    This program is free software; you can redistribute it and/or modify
106 dmarschall 6
    it under the terms of the GNU General Public License as published by
2 toby 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
 
106 dmarschall 15
    You should have received a copy of the GNU General Public License
2 toby 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 <plstringfuncs.h>
32
#endif
33
 
34
Boolean doupdates = true;
35
 
36
void updateglobals(DIALOGREF dp);
37
struct node *updateexpr(DIALOGREF dp,int i);
38
void updatedialog(DIALOGREF dp);
39
void slidertextchanged(DIALOGREF dp,int item);
40
void updatezoom(DIALOGREF dp);
41
 
42
void updatedialog(DIALOGREF dp){
43
        int i;
44
 
45
        doupdates = false;
46
 
78 toby 47
        for(i = 0; i < 8; ++i){
2 toby 48
                SETSLIDERVALUE(dp,FIRSTCTLITEM+i,slider[i]);
49
                SETCTLTEXTINT(dp,FIRSTCTLTEXTITEM+i,slider[i],false);
50
        }
51
 
78 toby 52
        for(i = 0; i < 4; ++i){
53
                if(!gdata->standalone)
54
                        SETCTLTEXT(dp,FIRSTEXPRITEM+i,expr[i] ? expr[i] : "");
106 dmarschall 55
                if(i < nplanes)
2 toby 56
                        updateexpr(dp,FIRSTEXPRITEM+i);
57
        }
58
 
78 toby 59
        if(!gdata->standalone)
60
                SELECTCTLTEXT(dp,FIRSTEXPRITEM,0,-1);
2 toby 61
 
62
        doupdates = true;
63
}
64
 
65
/* copy dialog settings to global variables (sliders, expressions) */
66
 
67
void updateglobals(DIALOGREF dp){
68
        int i;
69
        char s[MAXEXPR+1];
70
 
78 toby 71
        for(i = 0; i < 8; ++i)
2 toby 72
                slider[i] = GETSLIDERVALUE(dp,FIRSTCTLITEM+i);
73
 
74
        if(!gdata->standalone)
78 toby 75
                for(i = 0; i < 4; ++i){
2 toby 76
                        /* stash expression strings */
77
                        if(GETCTLTEXT(dp,FIRSTEXPRITEM+i,s,MAXEXPR)){
101 toby 78
                                if(expr[i])
2 toby 79
                                        free(expr[i]);
101 toby 80
                                expr[i] = my_strdup(s);
81
                        }
82
                        if(!expr[i])
83
                                expr[i] = my_strdup("c");
2 toby 84
                }
85
}
86
 
87
struct node *updateexpr(DIALOGREF dp,int item){
88
        char s[MAXEXPR+1];
89
        int i;
90
 
91
        i = item - FIRSTEXPRITEM;
92
 
93
        freetree(tree[i]);
94
 
95
        if(!gdata->standalone){
96
                GETCTLTEXT(dp,item,s,MAXEXPR);
106 dmarschall 97
 
2 toby 98
                if(expr[i])
99
                        free(expr[i]);
100
                expr[i] = my_strdup(s);
101
        }
102
 
103
        tree[i] = parseexpr(expr[i]);
106 dmarschall 104
 
2 toby 105
        if(!gdata->standalone){
106
                if(tree[i])
107
                        HideDialogItem(dp,FIRSTICONITEM+i);
108
                else{
109
                        err[i] = errstr;
110
                        errstart[i] = tokstart;
111
                        errpos[i] = tokpos;
112
                        ShowDialogItem(dp,FIRSTICONITEM+i);
113
                }
114
        }
115
        return tree[i];
116
}
117
 
118
void updatezoom(DIALOGREF dp){
102 toby 119
        char s[10];
103 toby 120
        sprintf(s, "%d%%", (int)(100./zoomfactor));
106 dmarschall 121
SETCTLTEXT(dp,ZOOMLEVELITEM,s);
78 toby 122
        if(zoomfactor > 1.)
123
                ShowDialogItem(dp,ZOOMINITEM);
124
        else
125
                HideDialogItem(dp,ZOOMINITEM);
126
        if(zoomfactor < fitzoom)
127
                ShowDialogItem(dp,ZOOMOUTITEM);
128
        else
129
                HideDialogItem(dp,ZOOMOUTITEM);
2 toby 130
}
131
 
130 dmarschall 132
/* traverse expression tree, looking for constant references to sliders/maps */
2 toby 133
 
130 dmarschall 134
static int _checksl(struct node*p,int ctlflags[],int mapflags[]){
135
        int s, i, result;
78 toby 136
 
130 dmarschall 137
        result = 0;
2 toby 138
        if(p){
139
                if( (p->kind==TOK_FN1 && p->v.sym->fn == (pfunc_type)ff_ctl)
140
                 || (p->kind==TOK_FN3 && p->v.sym->fn == (pfunc_type)ff_val) ){
141
                        if(p->child[0]->kind == TOK_NUM){
142
                                s = p->child[0]->v.value;
143
                                if(s>=0 && s<=7)
144
                                        ctlflags[s] = 1;
106 dmarschall 145
                        }else
130 dmarschall 146
                                result |= CHECKSLIDERS_CTL_AMBIGUOUS; /* can't determine which ctl() */
78 toby 147
                }else if(p->kind==TOK_FN2 && p->v.sym->fn == (pfunc_type)ff_map){
2 toby 148
                        if(p->child[0]->kind == TOK_NUM){
149
                                s = p->child[0]->v.value;
150
                                if(s>=0 && s<=3){
151
                                        mapflags[s] = 1;
152
                                        ctlflags[s*2] = ctlflags[s*2+1] = 1;
153
                                }
106 dmarschall 154
                        }else
130 dmarschall 155
                                result |= CHECKSLIDERS_MAP_AMBIGUOUS; /* can't determine which map() */
23 toby 156
                 }
2 toby 157
 
130 dmarschall 158
                for( i = 0 ; i < MAXCHILDREN ; ++i )
159
                        result |= _checksl(p->child[i],ctlflags,mapflags);
160
        }
161
 
162
        return result;
2 toby 163
}
164
 
130 dmarschall 165
int checksliders(int exprs,int ctlflags[],int mapflags[]){
166
        int i, result;
2 toby 167
 
130 dmarschall 168
        result = 0;
169
 
78 toby 170
        for(i = 4; i--;)
2 toby 171
                mapflags[i] = 0;
78 toby 172
        for(i = 8; i--;)
2 toby 173
                ctlflags[i] = 0;
174
 
78 toby 175
        for(i = 0; i < exprs; i++)
130 dmarschall 176
                result |= _checksl(tree[i],ctlflags,mapflags);
2 toby 177
 
130 dmarschall 178
        return result;
2 toby 179
}
180
 
181
void slidermoved(DIALOGREF dp,int i){
182
        int v = GETSLIDERVALUE(dp,i);
183
        i -= FIRSTCTLITEM;
184
        slider[i] = v;
185
        SETCTLTEXTINT(dp,i+FIRSTCTLTEXTITEM,v,false);
186
}
187
 
188
void slidertextchanged(DIALOGREF dp,int i){
189
        int v = GETCTLTEXTINT(dp,i,NULL,false);
190
        i -= FIRSTCTLTEXTITEM;
191
        SETSLIDERVALUE(dp,i+FIRSTCTLITEM,v);
192
        slider[i] = v;
193
}
194
 
195
void maindlgupdate(DIALOGREF dp){
196
        int i,unknown,ctls[8],maps[4];
197
 
198
        unknown = checksliders(nplanes,ctls,maps);
199
 
78 toby 200
        for(i = 0; i < 8; i++)
2 toby 201
                if(unknown || ctls[i]){
202
                        ENABLEDLGITEM(dp,FIRSTCTLITEM+i);
122 dmarschall 203
                        ENABLEDLGITEM(dp,FIRSTCTLLABELITEM+i);
2 toby 204
                        ShowDialogItem(dp,FIRSTCTLTEXTITEM+i); /* FIXME: this changes keyboard focus */
205
                }else{
206
                        DISABLEDLGITEM(dp,FIRSTCTLITEM+i);
122 dmarschall 207
                        DISABLEDLGITEM(dp,FIRSTCTLLABELITEM+i);
2 toby 208
                        HideDialogItem(dp,FIRSTCTLTEXTITEM+i); /* FIXME: this changes keyboard focus */
209
                }
210
 
78 toby 211
        for(i = 0; i < nplanes; i++)
2 toby 212
                if(!tree[i]){
213
                        /* uh oh, couldn't parse one of the saved expressions...this is fatal */
214
                        DISABLEDLGITEM(dp,IDOK);
215
                        if(gdata->standalone){
216
                                alertuser("Can't run this filter (there is a problem with the saved expressions).","");
217
                        }else{
218
                                DISABLEDLGITEM(dp,SAVEITEM);
219
                                DISABLEDLGITEM(dp,MAKEITEM);
220
                        }
221
                        return;
222
                }
223
 
224
        /* we have valid expression trees in all slots...proceed! */
225
        updateglobals(dp);
226
        if(setup(gpb))
227
                recalc_preview(gpb,dp);
228
 
229
        ENABLEDLGITEM(dp,IDOK);
230
        if(!gdata->standalone){
231
                ENABLEDLGITEM(dp,SAVEITEM);
232
                ENABLEDLGITEM(dp,MAKEITEM);
233
        }
234
}
235
 
236
/* one-time initialisation of dialog box */
237
 
238
void maindlginit(DIALOGREF dp){
239
        char s[0x100];
240
        int i;
89 toby 241
        char *channelsuffixes[] = {
159 dmarschall 242
                "", "KA", "I", "RGBA",
89 toby 243
                "CMYK", "HSL", "HSB", "1234",
244
                "DA", "LabA"
245
        };
2 toby 246
 
247
        /* hide unused expression items */
248
        if(gdata->standalone){
78 toby 249
                myp2cstrcpy(s,gdata->parm.author);
250
                SetDlgItemText(dp,PARAMAUTHORITEM,s);
251
                myp2cstrcpy(s,gdata->parm.copyright);
252
                SetDlgItemText(dp,PARAMCOPYITEM,s);
106 dmarschall 253
 
57 toby 254
                // update labels for map() or ctl() sliders
78 toby 255
                for(i = 0; i < 8; ++i){
123 dmarschall 256
                        if(gdata->parm.map_used[i/2]){
2 toby 257
                                if(i&1)
258
                                        HideDialogItem(dp,FIRSTCTLLABELITEM+i);
259
                                else{
78 toby 260
                                        myp2cstrcpy(s,gdata->parm.map[i/2]);
261
                                        SetDlgItemText(dp,FIRSTCTLLABELITEM+i,s);
2 toby 262
                                }
123 dmarschall 263
                        } else if(gdata->parm.ctl_used[i]){
264
                                myp2cstrcpy(s,gdata->parm.ctl[i]);
265
                                SetDlgItemText(dp,FIRSTCTLLABELITEM+i,s);
2 toby 266
                        }else{
267
                                HideDialogItem(dp,FIRSTCTLITEM+i);
268
                                HideDialogItem(dp,FIRSTCTLTEXTITEM+i);
269
                                HideDialogItem(dp,FIRSTCTLLABELITEM+i);
270
                        }
271
                }
89 toby 272
        }
273
 
274
        strcpy(s,"X =");
275
        for(i = 0; i < 4; ++i){
276
                if(i >= nplanes){
2 toby 277
                        HideDialogItem(dp,FIRSTICONITEM+i);
278
                        HideDialogItem(dp,FIRSTEXPRITEM+i);
279
                        HideDialogItem(dp,FIRSTLABELITEM+i);
89 toby 280
                }else{
281
                        s[0] = channelsuffixes[gpb->imageMode][i];
282
                        SetDlgItemText(dp,FIRSTLABELITEM+i,s);
2 toby 283
                }
89 toby 284
        }
2 toby 285
 
94 toby 286
        if(setup_preview(gpb,nplanes)){
37 toby 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.)
106 dmarschall 293
 
169 dmarschall 294
                /* Workaround: GIMP/PSPI sets maxSpace to 100 MB hardcoded, so the zoom is not adjusted correctly. */
295
                int disable_zoom_memory_check = false;
296
                #if MAC_ENV
297
                        if (gpb->hostSig == 'GIMP') disable_zoom_memory_check = true;
298
                #else
299
                        if (gpb->hostSig == 'PMIG') disable_zoom_memory_check = true;
300
                #endif
301
 
302
                if (!disable_zoom_memory_check) {
303
                        zoomfactor = sqrt(gpb->maxSpace/(10.*preview_w*preview_h*nplanes));
304
                        if(zoomfactor > fitzoom)
305
                                zoomfactor = fitzoom;
306
                        if(zoomfactor < 1.)
307
                                zoomfactor = 1.;
308
                } else {
85 toby 309
                        zoomfactor = fitzoom;
169 dmarschall 310
                }
106 dmarschall 311
 
2 toby 312
                updatezoom(dp);
313
        }else{
314
                HideDialogItem(dp,ZOOMINITEM);
315
                HideDialogItem(dp,ZOOMOUTITEM);
316
                HideDialogItem(dp,ZOOMLEVELITEM);
317
        }
106 dmarschall 318
 
2 toby 319
        updatedialog(dp);
320
        maindlgupdate(dp);
321
}
322
 
323
 
324
/* process an item hit. return false if the dialog is finished; otherwise return true. */
325
 
326
Boolean maindlgitem(DIALOGREF dp,int item){
37 toby 327
        extern int previewerr;
106 dmarschall 328
 
2 toby 329
        StandardFileReply sfr;
330
        NavReplyRecord reply;
331
        static OSType types[] = {TEXT_FILETYPE,PS_FILTER_FILETYPE};
332
        char *reason;
333
        Str255 fname;
334
 
335
        switch(item){
78 toby 336
        case IDOK:
2 toby 337
        case IDCANCEL:
338
                dispose_preview();
339
                return false; // end dialog
340
        case OPENITEM:
32 toby 341
                if(!gdata->standalone && choosefiletypes("\pChoose filter settings",&sfr,&reply,types,2,
152 dmarschall 342
                                        "All supported files (*.afs, *.8bf, *.pff, *.prm, *.bin, *.txt)\0*.afs;*.8bf;*.pff;*.prm;*.bin;*.txt\0Filter Factory Settings (*.afs, *.txt)\0*.afs;*.txt\0Filter Factory for Windows, Standalone Filter (*.8bf)\0*.8bf\0Premiere TF/FF Settings (*.pff, *.txt)\0*.pff;*.txt\0Premiere TT/FF for Windows, Standalone Filter (*.prm)\0*.prm\0FilterFactory for MacOS, Standalone Filter (*.bin)\0*.bin\0All files (*.*)\0*.*\0\0"
106 dmarschall 343
                                        #ifdef _WIN32
344
                                        ,gdata->hWndMainDlg
345
                                        #endif /* _WIN32 */
346
                                        )){
2 toby 347
                        if(loadfile(&sfr,&reason)){
348
                                updatedialog(dp);
349
                                maindlgupdate(dp);
78 toby 350
                        }else
351
                                alertuser("Cannot load settings.",reason);
2 toby 352
                }
353
                break;
354
        case SAVEITEM:
39 toby 355
                if(!gdata->standalone && putfile("\pSave filter settings",(StringPtr)"",
32 toby 356
                                                                                 TEXT_FILETYPE,SIG_SIMPLETEXT,&reply,&sfr,
106 dmarschall 357
                                                                                 "afs","Settings file (.afs, .txt)\0*.afs;*.txt\0\0",1
358
                                                                                 #ifdef _WIN32
359
                                                                                 ,gdata->hWndMainDlg
360
                                                                                 #endif /* _WIN32 */
361
                                                                                 )){
2 toby 362
                        if(savefile(&sfr))
363
                                completesave(&reply);
364
                }
365
                break;
366
        case MAKEITEM:
367
                if( !gdata->standalone && builddialog(gpb) ){
368
                        PLstrcpy(fname,gdata->parm.title);
11 toby 369
#ifdef MACMACHO
78 toby 370
                        PLstrcat(fname,(StringPtr)"\p.plugin");
11 toby 371
#endif
2 toby 372
                        if( putfile("\pMake standalone filter",fname,
32 toby 373
                                                PS_FILTER_FILETYPE,kPhotoshopSignature,&reply,&sfr,
106 dmarschall 374
                                                "8bf","Filter plugin file (.8bf)\0*.8bf\0\0",1
375
                                                #ifdef _WIN32
376
                                                ,gdata->hWndMainDlg
377
                                                #endif /* _WIN32 */
378
                                                ))
2 toby 379
                                make_standalone(&sfr);
380
                }
381
                break;
382
        case ZOOMINITEM:
78 toby 383
                zoomfactor = zoomfactor > 2. ? zoomfactor/2. : 1.;
2 toby 384
                updatezoom(dp);
37 toby 385
                previewerr = false;
2 toby 386
                recalc_preview(gpb,dp);
387
                break;
388
        case ZOOMOUTITEM:
389
                zoomfactor *= 2.;
390
                if(zoomfactor > fitzoom)
391
                        zoomfactor = fitzoom;
392
                updatezoom(dp);
37 toby 393
                previewerr = false;
2 toby 394
                recalc_preview(gpb,dp);
395
                break;
396
        case ZOOMLEVELITEM:
158 dmarschall 397
                zoomfactor = zoomfactor > 1. ? 1. : (fitzoom < 1. ? 1. : fitzoom);
2 toby 398
                updatezoom(dp);
37 toby 399
                previewerr = false;
2 toby 400
                recalc_preview(gpb,dp);
401
                break;
402
        case FIRSTCTLITEM:
403
        case FIRSTCTLITEM+1:
404
        case FIRSTCTLITEM+2:
405
        case FIRSTCTLITEM+3:
406
        case FIRSTCTLITEM+4:
407
        case FIRSTCTLITEM+5:
408
        case FIRSTCTLITEM+6:
409
        case FIRSTCTLITEM+7:
410
                slidermoved(dp,item);
411
                recalc_preview(gpb,dp);
412
                break;
413
        case FIRSTCTLTEXTITEM:
414
        case FIRSTCTLTEXTITEM+1:
415
        case FIRSTCTLTEXTITEM+2:
416
        case FIRSTCTLTEXTITEM+3:
417
        case FIRSTCTLTEXTITEM+4:
418
        case FIRSTCTLTEXTITEM+5:
419
        case FIRSTCTLTEXTITEM+6:
420
        case FIRSTCTLTEXTITEM+7:
421
                slidertextchanged(dp,item);
422
                recalc_preview(gpb,dp);
423
                break;
424
        case FIRSTICONITEM:
425
        case FIRSTICONITEM+1:
426
        case FIRSTICONITEM+2:
427
        case FIRSTICONITEM+3:
428
                item -= FIRSTICONITEM;
429
                alertuser(err[item],"");
430
                SELECTCTLTEXT(dp,FIRSTEXPRITEM+item,errstart[item],errpos[item]);
431
                break;
432
        case FIRSTEXPRITEM:
433
        case FIRSTEXPRITEM+1:
434
        case FIRSTEXPRITEM+2:
435
        case FIRSTEXPRITEM+3:
78 toby 436
                if((item-FIRSTEXPRITEM) < nplanes){
2 toby 437
                        updateexpr(dp,item);
438
                        maindlgupdate(dp);
439
                }
440
                break;
441
        }
442
 
443
        return true; // keep going
444
}
445
 
446
Boolean alertuser(char *err,char *more){
447
        char *s = malloc(strlen(err)+strlen(more)+2),*q;
448
        Boolean res;
106 dmarschall 449
 
2 toby 450
        q = cat(s,err);
451
        *q++ = '\n';
452
        q = cat(q,more);
453
        *q = 0;
454
        res = simplealert(s);
455
        free(s);
456
        return res;
457
}