Subversion Repositories filter_foundry

Rev

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