/* Copyright (C) 2002-2012 by George Williams */ /* * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #if !defined(_NO_FFSCRIPT) || !defined(_NO_PYTHON) #include "cvundoes.h" #include "fontforgeui.h" #include "gfile.h" #include "gkeysym.h" #include "scriptfuncs.h" #include "scripting.h" #include "ustring.h" #include "utype.h" struct sd_data { int done; FontView *fv; SplineChar *sc; int layer; GWindow gw; int oldh; }; #define SD_Width 250 #define SD_Height 270 #define CID_Script 1001 #define CID_Box 1002 #define CID_OK 1003 #define CID_Call 1004 #define CID_Cancel 1005 #define CID_Python 1006 #define CID_FF 1007 static int SD_Call(GGadget *g, GEvent *e) { if ( e->type==et_controlevent && e->u.control.subtype == et_buttonactivate ) { char *fn; unichar_t *insert; fn = gwwv_open_filename(_("Call Script"), NULL, "*",NULL); if ( fn==NULL ) return(true); insert = malloc((strlen(fn)+10)*sizeof(unichar_t)); *insert = '"'; utf82u_strcpy(insert+1,fn); uc_strcat(insert,"\"()"); GTextFieldReplace(GWidgetGetControl(GGadgetGetWindow(g),CID_Script),insert); free(insert); free(fn); } return( true ); } #if !defined(_NO_FFSCRIPT) static void ExecNative(GGadget *g, GEvent *e) { struct sd_data *sd = GDrawGetUserData(GGadgetGetWindow(g)); Context c; Val args[1]; jmp_buf env; memset( &c,0,sizeof(c)); memset( args,0,sizeof(args)); running_script = true; c.a.argc = 1; c.a.vals = args; c.filename = args[0].u.sval = "ScriptDlg"; args[0].type = v_str; c.return_val.type = v_void; c.err_env = &env; c.curfv = (FontViewBase *) sd->fv; if ( setjmp(env)!=0 ) { running_script = false; return; /* Error return */ } c.script = GFileTmpfile(); if ( c.script==NULL ) ScriptError(&c, "Can't create temporary file"); else { const unichar_t *ret = _GGadgetGetTitle(GWidgetGetControl(sd->gw,CID_Script)); while ( *ret ) { /* There's a bug here. Filenames need to be converted to the local charset !!!! */ putc(*ret,c.script); ++ret; } rewind(c.script); ff_VerboseCheck(); c.lineno = 1; while ( !c.returned && !c.broken && ff_NextToken(&c)!=tt_eof ) { ff_backuptok(&c); ff_statement(&c); } fclose(c.script); sd->done = true; } running_script = false; } #endif #if !defined(_NO_PYTHON) static void ExecPython(GGadget *g, GEvent *e) { struct sd_data *sd = GDrawGetUserData(GGadgetGetWindow(g)); char *str; running_script = true; str = GGadgetGetTitle8(GWidgetGetControl(sd->gw,CID_Script)); PyFF_ScriptString((FontViewBase *) sd->fv,sd->sc,sd->layer,str); free(str); running_script = false; } #endif #if !defined(_NO_FFSCRIPT) && !defined(_NO_PYTHON) static void _SD_LangChanged(struct sd_data *sd) { GGadgetSetEnabled(GWidgetGetControl(sd->gw,CID_Call), !GGadgetIsChecked(GWidgetGetControl(sd->gw,CID_Python))); } static int SD_LangChanged(GGadget *g, GEvent *e) { if ( e->type==et_controlevent && e->u.control.subtype == et_radiochanged ) { struct sd_data *sd = GDrawGetUserData(GGadgetGetWindow(g)); _SD_LangChanged(sd); } return( true ); } #endif static int SD_OK(GGadget *g, GEvent *e) { if ( e->type==et_controlevent && e->u.control.subtype == et_buttonactivate ) { struct sd_data *sd = GDrawGetUserData(GGadgetGetWindow(g)); #if !defined(_NO_FFSCRIPT) && !defined(_NO_PYTHON) if ( GGadgetIsChecked(GWidgetGetControl(GGadgetGetWindow(g),CID_Python)) ) ExecPython(g,e); else ExecNative(g,e); #elif !defined(_NO_PYTHON) ExecPython(g,e); #elif !defined(_NO_FFSCRIPT) ExecNative(g,e); #endif sd->done = true; } return( true ); } static void SD_DoCancel(struct sd_data *sd) { sd->done = true; } static int SD_Cancel(GGadget *g, GEvent *e) { if ( e->type==et_controlevent && e->u.control.subtype == et_buttonactivate ) { SD_DoCancel( GDrawGetUserData(GGadgetGetWindow(g))); } return( true ); } static int sd_e_h(GWindow gw, GEvent *event) { struct sd_data *sd = GDrawGetUserData(gw); if ( sd==NULL ) return( true ); if ( event->type==et_close ) { SD_DoCancel( sd ); } else if ( event->type==et_controlevent && event->u.control.subtype==et_textchanged ) { sd->fv->script_unsaved = !GTextFieldIsEmpty(GWidgetGetControl(sd->gw,CID_Script)); } else if ( event->type==et_controlevent && event->u.control.subtype==et_save ) { sd->fv->script_unsaved = false; } else if ( event->type==et_char ) { if ( event->u.chr.keysym == GK_F1 || event->u.chr.keysym == GK_Help ) { help("scripting/scripting.html", NULL); return( true ); } return( false ); } else if ( event->type == et_map ) /* Above palettes */ GDrawRaise(gw); else if ( event->type == et_resize ) GDrawRequestExpose(gw,NULL,false); return( true ); } void ScriptDlg(FontView *fv,CharView *cv) { GRect pos; static GWindow gw; GWindowAttrs wattrs; GGadgetCreateData gcd[12], boxes[5], *barray[4][8], *hvarray[4][2]; #if !defined(_NO_FFSCRIPT) && !defined(_NO_PYTHON) GGadgetCreateData *rarray[4]; #endif GTextInfo label[12]; struct sd_data sd; FontView *list; int i,l; memset(&sd,0,sizeof(sd)); sd.fv = fv; sd.sc = cv==NULL ? NULL : cv->b.sc; sd.layer = cv==NULL ? ly_fore : CVLayer((CharViewBase *) cv); sd.oldh = pos.height = GDrawPointsToPixels(NULL,SD_Height); if ( gw==NULL ) { memset(&wattrs,0,sizeof(wattrs)); wattrs.mask = wam_events|wam_cursor|wam_utf8_wtitle|wam_undercursor|wam_restrict|wam_isdlg; wattrs.event_masks = ~(1<b.next ) GDrawRequestExpose(list->v,NULL,false); GDrawSync(NULL); GDrawProcessPendingEvents(NULL); GDrawSetUserData(gw,NULL); } #endif /* No scripting */