summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Gardiner <paulg.artifex@glidos.net>2012-08-10 10:07:01 +0100
committerPaul Gardiner <paulg.artifex@glidos.net>2012-08-10 10:07:01 +0100
commitbdcfc3fe13e819db0f29eebdb0e46c4aaa6a27b1 (patch)
tree49523fb6b40ab821223d4a1b4e9d0494fe551653
parent278604d0b3aa34f9b71bde739fe51149e4109495 (diff)
downloadmupdf-bdcfc3fe13e819db0f29eebdb0e46c4aaa6a27b1.tar.xz
Forms: alter the handling of the javascript event object
-rw-r--r--pdf/mupdf-internal.h10
-rw-r--r--pdf/pdf_form.c13
-rw-r--r--pdf/pdf_js.c27
-rw-r--r--pdf/pdf_js_none.c34
4 files changed, 58 insertions, 26 deletions
diff --git a/pdf/mupdf-internal.h b/pdf/mupdf-internal.h
index 65e968fb..b8011575 100644
--- a/pdf/mupdf-internal.h
+++ b/pdf/mupdf-internal.h
@@ -624,10 +624,16 @@ void pdf_widget_choice_set_value(pdf_document *doc, fz_widget *tw, int n, char *
/*
* Javascript handler
*/
+typedef struct pdf_js_event_s
+{
+ pdf_obj *target;
+ char *value;
+} pdf_js_event;
+
pdf_js *pdf_new_js(pdf_document *doc);
void pdf_drop_js(pdf_js *js);
-void pdf_js_setup_event(pdf_js *js, pdf_obj *target);
-char *pdf_js_getEventValue(pdf_js *js);
+void pdf_js_setup_event(pdf_js *js, pdf_js_event *e);
+pdf_js_event *pdf_js_get_event(pdf_js *js);
void pdf_js_execute(pdf_js *js, char *code);
void pdf_js_execute_count(pdf_js *js, char *code, int count);
diff --git a/pdf/pdf_form.c b/pdf/pdf_form.c
index 10b70824..b505de25 100644
--- a/pdf/pdf_form.c
+++ b/pdf/pdf_form.c
@@ -1030,6 +1030,9 @@ static void update_text_field_value(fz_context *ctx, pdf_obj *obj, char *text)
pdf_obj *sobj = NULL;
pdf_obj *grp;
+ if (!text)
+ text = "";
+
/* All fields of the same name should be updated, so
* set the value at the head of the group */
grp = find_head_of_field_group(obj);
@@ -1495,7 +1498,11 @@ static void execute_action(pdf_document *doc, pdf_obj *obj, pdf_obj *a)
char *code = pdf_to_utf8(doc, js);
fz_try(ctx)
{
- pdf_js_setup_event(doc->js, obj);
+ pdf_js_event e;
+
+ e.target = obj;
+ e.value = pdf_field_getValue(doc, obj);
+ pdf_js_setup_event(doc->js, &e);
pdf_js_execute(doc->js, code);
}
fz_always(ctx)
@@ -1561,7 +1568,7 @@ void pdf_update_appearance(pdf_document *doc, pdf_obj *obj)
/* Apply formatting */
execute_action(doc, obj, formatting);
/* Update appearance from JS event.value */
- update_text_appearance(doc, obj, pdf_js_getEventValue(doc->js));
+ update_text_appearance(doc, obj, pdf_js_get_event(doc->js)->value);
}
else
{
@@ -1871,7 +1878,7 @@ static void recalculate(pdf_document *doc)
execute_action(doc, field, calc);
/* A calculate action, updates event.value. We need
* to place the value in the field */
- update_text_field_value(doc->ctx, field, pdf_js_getEventValue(doc->js));
+ update_text_field_value(doc->ctx, field, pdf_js_get_event(doc->js)->value);
pdf_field_mark_dirty(doc->ctx, field);
}
}
diff --git a/pdf/pdf_js.c b/pdf/pdf_js.c
index e8e56c26..953d7965 100644
--- a/pdf/pdf_js.c
+++ b/pdf/pdf_js.c
@@ -1,12 +1,6 @@
#include "fitz-internal.h"
#include "mupdf-internal.h"
-typedef struct pdf_js_event_s
-{
- pdf_obj *target;
- char *value;
-} pdf_js_event;
-
struct pdf_js_s
{
pdf_document *doc;
@@ -175,8 +169,9 @@ 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, js->event.value);
+ return pdf_jsimp_fromString(js->imp, v?v:"");
}
static void event_setValue(void *jsctx, void *obj, pdf_jsimp_obj *val)
@@ -375,24 +370,26 @@ void pdf_drop_js(pdf_js *js)
}
}
-void pdf_js_setup_event(pdf_js *js, pdf_obj *target)
+void pdf_js_setup_event(pdf_js *js, pdf_js_event *e)
{
if (js)
{
fz_context *ctx = js->doc->ctx;
- char *val = pdf_field_getValue(js->doc, target);
- js->event.target = target;
+ js->event.target = e->target;
- fz_free(ctx, js->event.value);
- js->event.value = NULL;
- js->event.value = fz_strdup(ctx, val?val:"");
+ if (e->value)
+ {
+ fz_free(ctx, js->event.value);
+ js->event.value = NULL;
+ js->event.value = fz_strdup(ctx, e->value);
+ }
}
}
-char *pdf_js_getEventValue(pdf_js *js)
+pdf_js_event *pdf_js_get_event(pdf_js *js)
{
- return js ? js->event.value : NULL;
+ return js ? &js->event : NULL;
}
void pdf_js_execute(pdf_js *js, char *code)
diff --git a/pdf/pdf_js_none.c b/pdf/pdf_js_none.c
index 34c7c491..6d9836ef 100644
--- a/pdf/pdf_js_none.c
+++ b/pdf/pdf_js_none.c
@@ -1,25 +1,47 @@
#include "fitz-internal.h"
#include "mupdf-internal.h"
+struct pdf_js_s
+{
+ fz_context *ctx;
+ pdf_js_event event;
+};
pdf_js *pdf_new_js(pdf_document *doc)
{
- static int x;
-
- return (pdf_js *)&x;
+ fz_context *ctx = doc->ctx;
+ pdf_js *js = fz_malloc_struct(ctx, pdf_js);
+
+ fz_try(ctx)
+ {
+ js->ctx = doc->ctx;
+ js->event.target = NULL;
+ js->event.value = fz_strdup(ctx, "");
+ }
+ fz_catch(ctx)
+ {
+ pdf_drop_js(js);
+ }
+
+ return js;
}
void pdf_drop_js(pdf_js *js)
{
+ if (js)
+ {
+ fz_free(js->ctx, js->event.value);
+ fz_free(js->ctx, js);
+ }
}
-void pdf_js_setup_event(pdf_js *js, pdf_obj *target)
+void pdf_js_setup_event(pdf_js *js, pdf_js_event *e)
{
}
-char *pdf_js_getEventValue(pdf_js *js)
+pdf_js_event *pdf_js_get_event(pdf_js *js)
{
- return "";
+ return js ? &js->event : NULL;
}
void pdf_js_execute(pdf_js *js, char *code)