Rev 30 | Rev 37 | 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, |
||
284 | zv = (gpb->filterRect.bottom-gpb->filterRect.top)/(double)preview_h; |
||
285 | fitzoom = zh > zv ? zh : zv; |
||
286 | zoomfactor = fitzoom; |
||
287 | updatezoom(dp); |
||
288 | }else{ |
||
289 | HideDialogItem(dp,ZOOMINITEM); |
||
290 | HideDialogItem(dp,ZOOMOUTITEM); |
||
291 | HideDialogItem(dp,ZOOMLEVELITEM); |
||
292 | } |
||
293 | |||
294 | #ifdef WIN_ENV |
||
9 | toby | 295 | // can't build standalone filter on less than NT platform :-( |
296 | // due to absence of resource editing API (UpdateResource etc) |
||
297 | if(!isWin32NT()) |
||
298 | HideDialogItem(dp,MAKEITEM); |
||
2 | toby | 299 | #endif |
300 | |||
301 | updatedialog(dp); |
||
302 | maindlgupdate(dp); |
||
303 | } |
||
304 | |||
305 | |||
306 | /* process an item hit. return false if the dialog is finished; otherwise return true. */ |
||
307 | |||
308 | Boolean maindlgitem(DIALOGREF dp,int item){ |
||
309 | StandardFileReply sfr; |
||
310 | NavReplyRecord reply; |
||
311 | static OSType types[] = {TEXT_FILETYPE,PS_FILTER_FILETYPE}; |
||
312 | char *reason; |
||
313 | Str255 fname; |
||
314 | |||
315 | switch(item){ |
||
316 | case IDOK: |
||
317 | // updateglobals(dp); |
||
318 | case IDCANCEL: |
||
319 | dispose_preview(); |
||
320 | return false; // end dialog |
||
321 | case OPENITEM: |
||
32 | toby | 322 | if(!gdata->standalone && choosefiletypes("\pChoose filter settings",&sfr,&reply,types,2, |
323 | "All supported files (.AFS, .8BF, .TXT)\0*.AFS;*.8BF;*.TXT\0All files (*.*)\0*.*\0\0")){ |
||
2 | toby | 324 | if(loadfile(&sfr,&reason)){ |
325 | updatedialog(dp); |
||
326 | maindlgupdate(dp); |
||
327 | }else alertuser("Cannot load settings.",reason); |
||
328 | } |
||
329 | break; |
||
330 | case SAVEITEM: |
||
30 | toby | 331 | if(!gdata->standalone && putfile("\pSave filter settings","", |
32 | toby | 332 | TEXT_FILETYPE,SIG_SIMPLETEXT,&reply,&sfr, |
333 | "AFS","Settings file (.AFS, .TXT)\0*.AFS;*.TXT\0\0",1)){ |
||
2 | toby | 334 | // updateglobals(dp); |
335 | if(savefile(&sfr)) |
||
336 | completesave(&reply); |
||
337 | } |
||
338 | break; |
||
339 | case MAKEITEM: |
||
340 | if( !gdata->standalone && builddialog(gpb) ){ |
||
341 | PLstrcpy(fname,gdata->parm.title); |
||
11 | toby | 342 | #ifdef MACMACHO |
343 | PLstrcat(fname,(StringPtr)"\p.plugin"); |
||
344 | #endif |
||
2 | toby | 345 | if( putfile("\pMake standalone filter",fname, |
32 | toby | 346 | PS_FILTER_FILETYPE,kPhotoshopSignature,&reply,&sfr, |
347 | "8BF","Filter plugin file (.8BF)\0*.8BF\0\0",1) ) |
||
2 | toby | 348 | make_standalone(&sfr); |
349 | } |
||
350 | break; |
||
351 | case ZOOMINITEM: |
||
352 | zoomfactor = zoomfactor>2. ? zoomfactor/2. : 1.; |
||
353 | updatezoom(dp); |
||
354 | recalc_preview(gpb,dp); |
||
355 | break; |
||
356 | case ZOOMOUTITEM: |
||
357 | zoomfactor *= 2.; |
||
358 | if(zoomfactor > fitzoom) |
||
359 | zoomfactor = fitzoom; |
||
360 | updatezoom(dp); |
||
361 | recalc_preview(gpb,dp); |
||
362 | break; |
||
363 | case ZOOMLEVELITEM: |
||
364 | zoomfactor = (zoomfactor == fitzoom) ? 1. : fitzoom; |
||
365 | updatezoom(dp); |
||
366 | recalc_preview(gpb,dp); |
||
367 | break; |
||
368 | case FIRSTCTLITEM: |
||
369 | case FIRSTCTLITEM+1: |
||
370 | case FIRSTCTLITEM+2: |
||
371 | case FIRSTCTLITEM+3: |
||
372 | case FIRSTCTLITEM+4: |
||
373 | case FIRSTCTLITEM+5: |
||
374 | case FIRSTCTLITEM+6: |
||
375 | case FIRSTCTLITEM+7: |
||
376 | slidermoved(dp,item); |
||
377 | recalc_preview(gpb,dp); |
||
378 | break; |
||
379 | case FIRSTCTLTEXTITEM: |
||
380 | case FIRSTCTLTEXTITEM+1: |
||
381 | case FIRSTCTLTEXTITEM+2: |
||
382 | case FIRSTCTLTEXTITEM+3: |
||
383 | case FIRSTCTLTEXTITEM+4: |
||
384 | case FIRSTCTLTEXTITEM+5: |
||
385 | case FIRSTCTLTEXTITEM+6: |
||
386 | case FIRSTCTLTEXTITEM+7: |
||
387 | slidertextchanged(dp,item); |
||
388 | recalc_preview(gpb,dp); |
||
389 | break; |
||
390 | case FIRSTICONITEM: |
||
391 | case FIRSTICONITEM+1: |
||
392 | case FIRSTICONITEM+2: |
||
393 | case FIRSTICONITEM+3: |
||
394 | item -= FIRSTICONITEM; |
||
395 | alertuser(err[item],""); |
||
396 | SELECTCTLTEXT(dp,FIRSTEXPRITEM+item,errstart[item],errpos[item]); |
||
397 | break; |
||
398 | case FIRSTEXPRITEM: |
||
399 | case FIRSTEXPRITEM+1: |
||
400 | case FIRSTEXPRITEM+2: |
||
401 | case FIRSTEXPRITEM+3: |
||
402 | // dbg("expritem hit"); |
||
403 | if( (item-FIRSTEXPRITEM)<nplanes ){ |
||
404 | updateexpr(dp,item); |
||
405 | maindlgupdate(dp); |
||
406 | } |
||
407 | break; |
||
408 | } |
||
409 | |||
410 | return true; // keep going |
||
411 | } |
||
412 | |||
413 | Boolean alertuser(char *err,char *more){ |
||
414 | char *s = malloc(strlen(err)+strlen(more)+2),*q; |
||
415 | Boolean res; |
||
416 | |||
417 | q = cat(s,err); |
||
418 | *q++ = '\n'; |
||
419 | q = cat(q,more); |
||
420 | *q = 0; |
||
421 | res = simplealert(s); |
||
422 | free(s); |
||
423 | return res; |
||
424 | } |