Subversion Repositories filter_foundry

Rev

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