summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Gardiner <paulg.artifex@glidos.net>2012-08-10 10:09:34 +0100
committerPaul Gardiner <paulg.artifex@glidos.net>2012-08-10 10:09:34 +0100
commit465ddf7b0e7e9ffb8345117dc19fbd9576ad73ff (patch)
treeb692d3e4632b5464d0a7b2b90f3444a968939eea
parent2fba705a0b45b1192578de71c969f056a77bd789 (diff)
downloadmupdf-465ddf7b0e7e9ffb8345117dc19fbd9576ad73ff.tar.xz
Forms: handle Keystroke and Validate actions
-rw-r--r--pdf/mupdf-internal.h2
-rw-r--r--pdf/pdf_form.c21
-rw-r--r--pdf/pdf_js.c35
-rw-r--r--pdf/pdf_js_none.c1
-rw-r--r--pdf/pdf_jsimp_cpp.c10
-rw-r--r--pdf/pdf_jsimp_cpp.h1
-rw-r--r--pdf/pdf_jsimp_v8.cpp11
7 files changed, 78 insertions, 3 deletions
diff --git a/pdf/mupdf-internal.h b/pdf/mupdf-internal.h
index b8011575..1bbd9e35 100644
--- a/pdf/mupdf-internal.h
+++ b/pdf/mupdf-internal.h
@@ -628,6 +628,7 @@ typedef struct pdf_js_event_s
{
pdf_obj *target;
char *value;
+ int rc;
} pdf_js_event;
pdf_js *pdf_new_js(pdf_document *doc);
@@ -664,6 +665,7 @@ void pdf_jsimp_drop_obj(pdf_jsimp *imp, pdf_jsimp_obj *obj);
pdf_jsimp_obj *pdf_jsimp_fromString(pdf_jsimp *imp, char *str);
char *pdf_jsimp_toString(pdf_jsimp *imp, pdf_jsimp_obj *obj);
+pdf_jsimp_obj *pdf_jsimp_fromNumber(pdf_jsimp *imp, double num);
double pdf_jsimp_toNumber(pdf_jsimp *imp, pdf_jsimp_obj *obj);
int pdf_jsimp_array_len(pdf_jsimp *imp, pdf_jsimp_obj *obj);
diff --git a/pdf/pdf_form.c b/pdf/pdf_form.c
index 68559ac8..710d8c2a 100644
--- a/pdf/pdf_form.c
+++ b/pdf/pdf_form.c
@@ -1901,8 +1901,29 @@ static void recalculate(pdf_document *doc)
void pdf_field_setValue(pdf_document *doc, pdf_obj *field, char *text)
{
+ pdf_obj *k, *v;
doc->dirty = 1;
+ k = pdf_dict_getp(field, "AA/K");
+ v = pdf_dict_getp(field, "AA/V");
+
+ if (k || v)
+ {
+ pdf_js_event e;
+
+ e.target = field;
+ e.value = text;
+ pdf_js_setup_event(doc->js, &e);
+
+ if (k)
+ execute_action(doc, field, k);
+
+ if (v)
+ execute_action(doc, field, v);
+
+ text = pdf_js_get_event(doc->js)->value;
+ }
+
update_text_field_value(doc->ctx, field, text);
recalculate(doc);
pdf_field_mark_dirty(doc->ctx, field);
diff --git a/pdf/pdf_js.c b/pdf/pdf_js.c
index 953d7965..2b2d479b 100644
--- a/pdf/pdf_js.c
+++ b/pdf/pdf_js.c
@@ -154,9 +154,8 @@ static void field_setValue(void *jsctx, void *obj, pdf_jsimp_obj *val)
static pdf_jsimp_obj *event_getTarget(void *jsctx, void *obj)
{
pdf_js *js = (pdf_js *)jsctx;
- pdf_js_event *e = (pdf_js_event *)obj;
- return pdf_jsimp_new_obj(js->imp, js->fieldtype, e->target);
+ return pdf_jsimp_new_obj(js->imp, js->fieldtype, js->event.target);
}
static void event_setTarget(void *jsctx, void *obj, pdf_jsimp_obj *val)
@@ -168,7 +167,6 @@ static void event_setTarget(void *jsctx, void *obj, pdf_jsimp_obj *val)
static pdf_jsimp_obj *event_getValue(void *jsctx, void *obj)
{
pdf_js *js = (pdf_js *)jsctx;
- pdf_js_event *e = (pdf_js_event *)obj;
char *v = js->event.value;
return pdf_jsimp_fromString(js->imp, v?v:"");
@@ -183,6 +181,33 @@ static void event_setValue(void *jsctx, void *obj, pdf_jsimp_obj *val)
js->event.value = fz_strdup(ctx, pdf_jsimp_toString(js->imp, val));
}
+static pdf_jsimp_obj *event_getWillCommit(void *jsctx, void *obj)
+{
+ pdf_js *js = (pdf_js *)jsctx;
+
+ return pdf_jsimp_fromNumber(js->imp, 1.0);
+}
+
+static void event_setWillCommit(void *jsctx, void *obj, pdf_jsimp_obj *val)
+{
+ pdf_js *js = (pdf_js *)jsctx;
+ fz_warn(js->doc->ctx, "Unexpected call to event_setWillCommit");
+}
+
+static pdf_jsimp_obj *event_getRC(void *jsctx, void *obj)
+{
+ pdf_js *js = (pdf_js *)jsctx;
+
+ return pdf_jsimp_fromNumber(js->imp, (double)js->event.rc);
+}
+
+static void event_setRC(void *jsctx, void *obj, pdf_jsimp_obj *val)
+{
+ pdf_js *js = (pdf_js *)jsctx;
+
+ js->event.rc = (int)pdf_jsimp_toNumber(js->imp, val);
+}
+
static pdf_jsimp_obj *doc_getEvent(void *jsctx, void *obj)
{
pdf_js *js = (pdf_js *)jsctx;
@@ -258,6 +283,8 @@ static void declare_dom(pdf_js *js)
js->eventtype = pdf_jsimp_new_type(imp, NULL);
pdf_jsimp_addproperty(imp, js->eventtype, "target", event_getTarget, event_setTarget);
pdf_jsimp_addproperty(imp, js->eventtype, "value", event_getValue, event_setValue);
+ pdf_jsimp_addproperty(imp, js->eventtype, "willCommit", event_getWillCommit, event_setWillCommit);
+ pdf_jsimp_addproperty(imp, js->eventtype, "rc", event_getRC, event_setRC);
/* Create the field type */
js->fieldtype = pdf_jsimp_new_type(imp, NULL);
@@ -384,6 +411,8 @@ void pdf_js_setup_event(pdf_js *js, pdf_js_event *e)
js->event.value = NULL;
js->event.value = fz_strdup(ctx, e->value);
}
+
+ js->event.rc = 1;
}
}
diff --git a/pdf/pdf_js_none.c b/pdf/pdf_js_none.c
index 6d9836ef..6632db5b 100644
--- a/pdf/pdf_js_none.c
+++ b/pdf/pdf_js_none.c
@@ -17,6 +17,7 @@ pdf_js *pdf_new_js(pdf_document *doc)
js->ctx = doc->ctx;
js->event.target = NULL;
js->event.value = fz_strdup(ctx, "");
+ js->event.rc = 1;
}
fz_catch(ctx)
{
diff --git a/pdf/pdf_jsimp_cpp.c b/pdf/pdf_jsimp_cpp.c
index 3457ee5b..8bd1aad5 100644
--- a/pdf/pdf_jsimp_cpp.c
+++ b/pdf/pdf_jsimp_cpp.c
@@ -103,6 +103,16 @@ char *pdf_jsimp_toString(pdf_jsimp *imp, pdf_jsimp_obj *obj)
return str;
}
+pdf_jsimp_obj *pdf_jsimp_fromNumber(pdf_jsimp *imp, double num)
+{
+ pdf_jsimp_obj *obj = NULL;
+ char *err = pdf_jsimp_fromNumber_cpp(imp, num, &obj);
+ if (err != NULL)
+ fz_throw(pdf_jsimp_ctx_cpp(imp), "%s", err);
+
+ return obj;
+}
+
double pdf_jsimp_toNumber(pdf_jsimp *imp, pdf_jsimp_obj *obj)
{
double num;
diff --git a/pdf/pdf_jsimp_cpp.h b/pdf/pdf_jsimp_cpp.h
index de57bf97..1562ed15 100644
--- a/pdf/pdf_jsimp_cpp.h
+++ b/pdf/pdf_jsimp_cpp.h
@@ -14,6 +14,7 @@ char *pdf_jsimp_new_obj_cpp(pdf_jsimp *imp, pdf_jsimp_type *type, void *natobj,
char *pdf_jsimp_drop_obj_cpp(pdf_jsimp *imp, pdf_jsimp_obj *obj);
char *pdf_jsimp_fromString_cpp(pdf_jsimp *imp, char *str, pdf_jsimp_obj **obj);
char *pdf_jsimp_toString_cpp(pdf_jsimp *imp, pdf_jsimp_obj *obj, char **str);
+char *pdf_jsimp_fromNumber_cpp(pdf_jsimp *imp, double num, pdf_jsimp_obj **obj);
char *pdf_jsimp_toNumber_cpp(pdf_jsimp *imp, pdf_jsimp_obj *obj, double *num);
char *pdf_jsimp_array_len_cpp(pdf_jsimp *imp, pdf_jsimp_obj *obj, int *len);
char *pdf_jsimp_array_item_cpp(pdf_jsimp *imp, pdf_jsimp_obj *obj, int i, pdf_jsimp_obj **item);
diff --git a/pdf/pdf_jsimp_v8.cpp b/pdf/pdf_jsimp_v8.cpp
index 70234d0f..bc5d3b10 100644
--- a/pdf/pdf_jsimp_v8.cpp
+++ b/pdf/pdf_jsimp_v8.cpp
@@ -143,6 +143,11 @@ public:
pobj = Persistent<Value>::New(String::New(str));
}
+ PDFJSImpObject(double num): utf8(NULL)
+ {
+ pobj = Persistent<Value>::New(Number::New(num));
+ }
+
~PDFJSImpObject()
{
delete utf8;
@@ -372,6 +377,12 @@ extern "C" char *pdf_jsimp_toString_cpp(pdf_jsimp *imp, pdf_jsimp_obj *obj, char
return NULL;
}
+extern "C" char *pdf_jsimp_fromNumber_cpp(pdf_jsimp *imp, double num, pdf_jsimp_obj **obj)
+{
+ *obj = reinterpret_cast<pdf_jsimp_obj *>(new PDFJSImpObject(num));
+ return NULL;
+}
+
extern "C" char *pdf_jsimp_toNumber_cpp(pdf_jsimp *imp, pdf_jsimp_obj *obj, double *num)
{
*num = reinterpret_cast<PDFJSImpObject *>(obj)->toNumber();