Debian Codegraph @

fontforgeexe/scriptingdlg.c

raw symbols SCIP
1 /* Copyright (C) 2002-2012 by George Williams */
2 /*
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are met:
5
6 * Redistributions of source code must retain the above copyright notice, this
7 * list of conditions and the following disclaimer.
8
9 * Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12
13 * The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <fontforge-config.h>
29
30 #if !defined(_NO_FFSCRIPT) || !defined(_NO_PYTHON)
31
32 #include "cvundoes.h"
33 #include "fontforgeui.h"
34 #include "gfile.h"
35 #include "gkeysym.h"
36 #include "scriptfuncs.h"
37 #include "scripting.h"
38 #include "ustring.h"
39 #include "utype.h"
40
41 struct sd_data {
42 int done;
43 FontView *fv;
44 SplineChar *sc;
45 int layer;
46 GWindow gw;
47 int oldh;
48 };
49
50 #define SD_Width 250
51 #define SD_Height 270
52 #define CID_Script 1001
53 #define CID_Box 1002
54 #define CID_OK 1003
55 #define CID_Call 1004
56 #define CID_Cancel 1005
57 #define CID_Python 1006
58 #define CID_FF 1007
59
60 static int SD_Call(GGadget *g, GEvent *e) {
61 if ( e->type==et_controlevent && e->u.control.subtype == et_buttonactivate ) {
62 char *fn;
63 unichar_t *insert;
64
65 fn = gwwv_open_filename(_("Call Script"), NULL, "*",NULL);
66 if ( fn==NULL )
67 return(true);
68 insert = malloc((strlen(fn)+10)*sizeof(unichar_t));
69 *insert = '"';
70 utf82u_strcpy(insert+1,fn);
71 uc_strcat(insert,"\"()");
72 GTextFieldReplace(GWidgetGetControl(GGadgetGetWindow(g),CID_Script),insert);
73 free(insert);
74 free(fn);
75 }
76 return( true );
77 }
78
79 #if !defined(_NO_FFSCRIPT)
80 static void ExecNative(GGadget *g, GEvent *e) {
81 struct sd_data *sd = GDrawGetUserData(GGadgetGetWindow(g));
82 Context c;
83 Val args[1];
84 jmp_buf env;
85
86 memset( &c,0,sizeof(c));
87 memset( args,0,sizeof(args));
88 running_script = true;
89 c.a.argc = 1;
90 c.a.vals = args;
91 c.filename = args[0].u.sval = "ScriptDlg";
92 args[0].type = v_str;
93 c.return_val.type = v_void;
94 c.err_env = &env;
95 c.curfv = (FontViewBase *) sd->fv;
96 if ( setjmp(env)!=0 ) {
97 running_script = false;
98 return; /* Error return */
99 }
100
101 c.script = GFileTmpfile();
102 if ( c.script==NULL )
103 ScriptError(&c, "Can't create temporary file");
104 else {
105 const unichar_t *ret = _GGadgetGetTitle(GWidgetGetControl(sd->gw,CID_Script));
106 while ( *ret ) {
107 /* There's a bug here. Filenames need to be converted to the local charset !!!! */
108 putc(*ret,c.script);
109 ++ret;
110 }
111 rewind(c.script);
112 ff_VerboseCheck();
113 c.lineno = 1;
114 while ( !c.returned && !c.broken && ff_NextToken(&c)!=tt_eof ) {
115 ff_backuptok(&c);
116 ff_statement(&c);
117 }
118 fclose(c.script);
119 sd->done = true;
120 }
121 running_script = false;
122 }
123 #endif
124
125 #if !defined(_NO_PYTHON)
126 static void ExecPython(GGadget *g, GEvent *e) {
127 struct sd_data *sd = GDrawGetUserData(GGadgetGetWindow(g));
128 char *str;
129
130 running_script = true;
131
132 str = GGadgetGetTitle8(GWidgetGetControl(sd->gw,CID_Script));
133 PyFF_ScriptString((FontViewBase *) sd->fv,sd->sc,sd->layer,str);
134 free(str);
135 running_script = false;
136 }
137 #endif
138
139 #if !defined(_NO_FFSCRIPT) && !defined(_NO_PYTHON)
140 static void _SD_LangChanged(struct sd_data *sd) {
141 GGadgetSetEnabled(GWidgetGetControl(sd->gw,CID_Call),
142 !GGadgetIsChecked(GWidgetGetControl(sd->gw,CID_Python)));
143 }
144
145 static int SD_LangChanged(GGadget *g, GEvent *e) {
146 if ( e->type==et_controlevent && e->u.control.subtype == et_radiochanged ) {
147 struct sd_data *sd = GDrawGetUserData(GGadgetGetWindow(g));
148 _SD_LangChanged(sd);
149 }
150 return( true );
151 }
152 #endif
153
154 static int SD_OK(GGadget *g, GEvent *e) {
155 if ( e->type==et_controlevent && e->u.control.subtype == et_buttonactivate ) {
156 struct sd_data *sd = GDrawGetUserData(GGadgetGetWindow(g));
157 #if !defined(_NO_FFSCRIPT) && !defined(_NO_PYTHON)
158 if ( GGadgetIsChecked(GWidgetGetControl(GGadgetGetWindow(g),CID_Python)) )
159 ExecPython(g,e);
160 else
161 ExecNative(g,e);
162 #elif !defined(_NO_PYTHON)
163 ExecPython(g,e);
164 #elif !defined(_NO_FFSCRIPT)
165 ExecNative(g,e);
166 #endif
167 sd->done = true;
168 }
169 return( true );
170 }
171
172 static void SD_DoCancel(struct sd_data *sd) {
173 sd->done = true;
174 }
175
176 static int SD_Cancel(GGadget *g, GEvent *e) {
177 if ( e->type==et_controlevent && e->u.control.subtype == et_buttonactivate ) {
178 SD_DoCancel( GDrawGetUserData(GGadgetGetWindow(g)));
179 }
180 return( true );
181 }
182
183 static int sd_e_h(GWindow gw, GEvent *event) {
184 struct sd_data *sd = GDrawGetUserData(gw);
185
186 if ( sd==NULL )
187 return( true );
188
189 if ( event->type==et_close ) {
190 SD_DoCancel( sd );
191 } else if ( event->type==et_controlevent && event->u.control.subtype==et_textchanged ) {
192 sd->fv->script_unsaved = !GTextFieldIsEmpty(GWidgetGetControl(sd->gw,CID_Script));
193 } else if ( event->type==et_controlevent && event->u.control.subtype==et_save ) {
194 sd->fv->script_unsaved = false;
195 } else if ( event->type==et_char ) {
196 if ( event->u.chr.keysym == GK_F1 || event->u.chr.keysym == GK_Help ) {
197 help("scripting/scripting.html", NULL);
198 return( true );
199 }
200 return( false );
201 } else if ( event->type == et_map ) /* Above palettes */
202 GDrawRaise(gw);
203 else if ( event->type == et_resize )
204 GDrawRequestExpose(gw,NULL,false);
205 return( true );
206 }
207
208 void ScriptDlg(FontView *fv,CharView *cv) {
209 GRect pos;
210 static GWindow gw;
211 GWindowAttrs wattrs;
212 GGadgetCreateData gcd[12], boxes[5], *barray[4][8], *hvarray[4][2];
213 #if !defined(_NO_FFSCRIPT) && !defined(_NO_PYTHON)
214 GGadgetCreateData *rarray[4];
215 #endif
216 GTextInfo label[12];
217 struct sd_data sd;
218 FontView *list;
219 int i,l;
220
221 memset(&sd,0,sizeof(sd));
222 sd.fv = fv;
223 sd.sc = cv==NULL ? NULL : cv->b.sc;
224 sd.layer = cv==NULL ? ly_fore : CVLayer((CharViewBase *) cv);
225 sd.oldh = pos.height = GDrawPointsToPixels(NULL,SD_Height);
226
227 if ( gw==NULL ) {
228 memset(&wattrs,0,sizeof(wattrs));
229 wattrs.mask = wam_events|wam_cursor|wam_utf8_wtitle|wam_undercursor|wam_restrict|wam_isdlg;
230 wattrs.event_masks = ~(1<<et_charup);
231 wattrs.restrict_input_to_me = 1;
232 wattrs.undercursor = 1;
233 wattrs.cursor = ct_pointer;
234 wattrs.utf8_window_title = _("Execute Script");
235 wattrs.is_dlg = true;
236 pos.x = pos.y = 0;
237 pos.width = GDrawPointsToPixels(NULL,GGadgetScale(SD_Width));
238 gw = GDrawCreateTopWindow(NULL,&pos,sd_e_h,&sd,&wattrs);
239
240 memset(&boxes,0,sizeof(boxes));
241 memset(&gcd,0,sizeof(gcd));
242 memset(&label,0,sizeof(label));
243
244 i = l = 0;
245 gcd[i].gd.pos.x = 10; gcd[i].gd.pos.y = 10;
246 gcd[i].gd.pos.width = SD_Width-20; gcd[i].gd.pos.height = SD_Height-54;
247 gcd[i].gd.flags = gg_visible | gg_enabled | gg_textarea_wrap;
248 gcd[i].gd.cid = CID_Script;
249 gcd[i++].creator = GTextAreaCreate;
250 hvarray[l][0] = &gcd[i-1]; hvarray[l++][1] = NULL;
251
252 #if !defined(_NO_FFSCRIPT) && !defined(_NO_PYTHON)
253 gcd[i-1].gd.pos.height -= 24;
254
255 gcd[i].gd.pos.x = 10; gcd[i].gd.pos.y = gcd[i-1].gd.pos.y+gcd[i-1].gd.pos.height+1;
256 gcd[i].gd.flags = gg_visible | gg_enabled | gg_cb_on;
257 gcd[i].gd.cid = CID_Python;
258 label[i].text = (unichar_t *) _("_Python");
259 label[i].text_is_1byte = true;
260 label[i].text_in_resource = true;
261 gcd[i].gd.label = &label[i];
262 gcd[i].gd.handle_controlevent = SD_LangChanged;
263 gcd[i++].creator = GRadioCreate;
264 rarray[0] = &gcd[i-1];
265
266 gcd[i].gd.pos.x = 70; gcd[i].gd.pos.y = gcd[i-1].gd.pos.y;
267 gcd[i].gd.flags = gg_visible | gg_enabled; /* disabled if cv!=NULL later */
268 gcd[i].gd.cid = CID_FF;
269 label[i].text = (unichar_t *) _("_FF");
270 label[i].text_is_1byte = true;
271 label[i].text_in_resource = true;
272 gcd[i].gd.label = &label[i];
273 gcd[i].gd.handle_controlevent = SD_LangChanged;
274 gcd[i++].creator = GRadioCreate;
275 rarray[1] = &gcd[i-1]; rarray[2] = GCD_Glue; rarray[3] = NULL;
276
277 boxes[2].gd.flags = gg_enabled | gg_visible;
278 boxes[2].gd.u.boxelements = rarray;
279 boxes[2].creator = GHBoxCreate;
280 hvarray[l][0] = &boxes[2]; hvarray[l++][1] = NULL;
281 #endif
282
283 barray[0][0] = barray[1][0] = barray[0][6] = barray[1][6] = GCD_Glue;
284 barray[0][2] = barray[1][2] = barray[0][4] = barray[1][4] = GCD_Glue;
285 barray[0][1] = barray[0][5] = GCD_RowSpan;
286 barray[0][7] = barray[1][7] = barray[2][0] = NULL;
287 gcd[i].gd.pos.x = 25-3; gcd[i].gd.pos.y = SD_Height-32-3;
288 gcd[i].gd.flags = gg_visible | gg_enabled | gg_but_default;
289 label[i].text = (unichar_t *) _("_OK");
290 label[i].text_is_1byte = true;
291 label[i].text_in_resource = true;
292 gcd[i].gd.mnemonic = 'O';
293 gcd[i].gd.label = &label[i];
294 gcd[i].gd.handle_controlevent = SD_OK;
295 gcd[i].gd.cid = CID_OK;
296 gcd[i++].creator = GButtonCreate;
297 barray[1][1] = &gcd[i-1];
298
299 gcd[i].gd.pos.x = -25; gcd[i].gd.pos.y = SD_Height-32;
300 gcd[i].gd.flags = gg_visible | gg_enabled | gg_but_cancel;
301 label[i].text = (unichar_t *) _("_Cancel");
302 label[i].text_is_1byte = true;
303 label[i].text_in_resource = true;
304 gcd[i].gd.label = &label[i];
305 gcd[i].gd.mnemonic = 'C';
306 gcd[i].gd.handle_controlevent = SD_Cancel;
307 gcd[i].gd.cid = CID_Cancel;
308 gcd[i++].creator = GButtonCreate;
309 barray[1][5] = &gcd[i-1];
310
311 gcd[i].gd.pos.x = (SD_Width-GIntGetResource(_NUM_Buttonsize)*100/GIntGetResource(_NUM_ScaleFactor))/2; gcd[i].gd.pos.y = SD_Height-40;
312 gcd[i].gd.flags = gg_visible | gg_enabled;
313 label[i].text = (unichar_t *) _("C_all...");
314 label[i].text_is_1byte = true;
315 label[i].text_in_resource = true;
316 gcd[i].gd.label = &label[i];
317 gcd[i].gd.mnemonic = 'a';
318 gcd[i].gd.handle_controlevent = SD_Call;
319 gcd[i].gd.cid = CID_Call;
320 gcd[i++].creator = GButtonCreate;
321 barray[0][3] = &gcd[i-1];
322
323 #if !defined(_NO_FFSCRIPT)
324 gcd[i].gd.pos.width = gcd[i].gd.pos.height = 5;
325 gcd[i].gd.flags = gg_visible | gg_enabled;
326 gcd[i++].creator = GSpacerCreate;
327 barray[1][3] = &gcd[i-1];
328 #else
329 barray[1][3] = GCD_RowSpan;
330 #endif
331
332 barray[3][0] = NULL;
333
334 boxes[3].gd.flags = gg_enabled | gg_visible;
335 boxes[3].gd.u.boxelements = barray[0];
336 boxes[3].creator = GHVBoxCreate;
337 hvarray[l][0] = &boxes[3]; hvarray[l++][1] = NULL;
338 hvarray[l][0] = NULL;
339
340 boxes[0].gd.pos.x = boxes[0].gd.pos.y = 2;
341 boxes[0].gd.flags = gg_enabled | gg_visible;
342 boxes[0].gd.u.boxelements = hvarray[0];
343 boxes[0].creator = GHVGroupCreate;
344
345 GGadgetsCreate(gw,boxes);
346 if ( boxes[2].ret!=NULL )
347 GHVBoxSetExpandableCol(boxes[2].ret,gb_expandglue);
348 GHVBoxSetExpandableCol(boxes[3].ret,gb_expandgluesame);
349 GHVBoxSetExpandableRow(boxes[0].ret,0);
350 GHVBoxFitWindow(boxes[0].ret);
351 }
352 #if !defined(_NO_FFSCRIPT) && !defined(_NO_PYTHON)
353 GGadgetSetEnabled(GWidgetGetControl(gw,CID_FF),cv==NULL);
354 #endif
355 sd.gw = gw;
356 GDrawSetUserData(gw,&sd);
357 GWidgetIndicateFocusGadget(GWidgetGetControl(gw,CID_Script));
358 #if !defined(_NO_FFSCRIPT) && !defined(_NO_PYTHON)
359 _SD_LangChanged(&sd);
360 #endif
361 GDrawSetVisible(gw,true);
362 while ( !sd.done )
363 GDrawProcessOneEvent(NULL);
364 GDrawSetVisible(gw,false);
365
366 /* Selection may be out of date, force a refresh */
367 for ( list = fv_list; list!=NULL; list=(FontView *) list->b.next )
368 GDrawRequestExpose(list->v,NULL,false);
369 GDrawSync(NULL);
370 GDrawProcessPendingEvents(NULL);
371 GDrawSetUserData(gw,NULL);
372 }
373 #endif /* No scripting */