Difference between revisions of "Widgets"

From Wiki
Jump to navigation Jump to search
(add current date example)
(still working on the example / explaining radio buttons)
Line 21: Line 21:
 
</texcode>
 
</texcode>
  
'''Beware''', in MkIV (as of 2015-04-01) the JS code is only copied to the PDF if there is a \goto referencing one of the defined functions!
+
'''Beware''', in MkIV the JS code is only copied to the PDF if there is a \goto referencing one of the defined functions!
  
 
You can pass values to a JS function:
 
You can pass values to a JS function:
Line 84: Line 84:
  
 
===Setting the current date===
 
===Setting the current date===
This is more usable than the example above:
+
Similar, but more usable than the example above:
  
 
<texcode>
 
<texcode>
 +
\starttext
 +
 
\startJSpreamble {EXAMPLE}
 
\startJSpreamble {EXAMPLE}
 +
function Dummy(){
 +
return 0;
 +
}
 +
 
function setCurrentDate(fieldname) {
 
function setCurrentDate(fieldname) {
 
var f = this.getField(fieldname);
 
var f = this.getField(fieldname);
Line 97: Line 103:
  
 
\setupfield[dateString][reset,horizontal][width=5em,option=printable]
 
\setupfield[dateString][reset,horizontal][width=5em,option=printable]
\definefield[myDateField][line][dateString][][]
+
\definefield[myDateField][line][dateString][][JavaScript should replace this text with the current date]
 +
 
 +
Current date: \field[myDateField]
  
 +
\goto{END}[JS(Dummy)] % for MkIV bug
 +
\stoptext
 
</texcode>
 
</texcode>
  
Line 144: Line 154:
 
==Radiobuttons==
 
==Radiobuttons==
  
Sample from the manual: (Note that background=screen is depricated in MKIV and should be replaced by something like background=color,backgroundcolor=lightgray.)
+
Example from the manual:
  
 
<texcode>
 
<texcode>
Line 151: Line 161:
 
         height=4cm,
 
         height=4cm,
 
         frame=off,
 
         frame=off,
         background=screen]
+
         background=color,
 +
        backgroundcolor=lightgray]
  
 
\definefield[Logos] [radio] [LogoSetup][ConTeXt,PPCHTEX,TeXUtil] [PPCHTEX]
 
\definefield[Logos] [radio] [LogoSetup][ConTeXt,PPCHTEX,TeXUtil] [PPCHTEX]
Line 165: Line 176:
 
\hbox to \hsize{\hss\field[ConTeXt]\hss\field[PPCHTEX]\hss\field[TeXUtil]\hss}
 
\hbox to \hsize{\hss\field[ConTeXt]\hss\field[PPCHTEX]\hss\field[TeXUtil]\hss}
 
</texcode>
 
</texcode>
 +
 +
As usual, first you need to define a class of fields ({{cmd|setupfield}}). Then you define the (invisible) group of radio buttons ({{cmd|definefield}} with "radio"). At last you define the single radio buttons with {{cmd|setupsubfield}}.
 +
 +
Arguments of {{cmd|definefield}}:
 +
# field name
 +
# field type "radio"
 +
# setup class, as defined by {{cmd|setupfield}}
 +
# list of field names that should be part of the group
 +
# name of default (activated) button
 +
 +
Arguments of {{cmd|definesubfield}}:
 +
# field name
 +
# setup class (default is inherited, but you can use a different one)
 +
# content symbol, defined by {{cmd|definesymbol}}
  
 
==Checkboxes==
 
==Checkboxes==
Line 181: Line 206:
 
=Tricks and Traps=
 
=Tricks and Traps=
  
Beware, there is no {{cmd|setupfields}} (plural), at least in MkIV as of 2015-10-05.
+
'''Beware''', in MkIV the JS code is only copied to the PDF if there is a \goto referencing one of the defined functions!
 +
(reported 2015-04-01, still true 2015-10-05)
 +
 
 +
'''Beware''', there is no {{cmd|setupfields}} (plural)!
  
 
This helps debugging (MkII only!):
 
This helps debugging (MkII only!):

Revision as of 08:14, 5 October 2015

< Visuals | Interaction >

You can find more about interactive form elements in Widgets uncovered. It’s written for MkII, but still mostly valid.

At the moment all of the following examples are from mwidget manual. We will cook up our own later.

JavaScript

If you need to check or otherwise process the input of your forms, you need JavaScript to handle interaction. For simple forms without input validation, you can skip this section.

\startJSpreamble {name}
MyCounter = 0 ;
\stopJSpreamble

\startJScode {increment}
MyCounter = MyCounter + 1 ; // or: ++MyCounter ;
\stopJScode

\goto {advance by one} [JS(increment)]

Beware, in MkIV the JS code is only copied to the PDF if there is a \goto referencing one of the defined functions!

You can pass values to a JS function:

\startJScode {increment}
MyCounter = MyCounter + JS_V_1 ;
\stopJScode

\goto {advance by five} [JS(increment{V{5}})]
  • V{} is verbose, defaults to string
  • S{} = as string
  • R{} = as reference
  • JS_V_n, JS_S_n, JS_R_n are the names of the variables
  • JS_N keeps the number of arguments

Documentation

JavaScript in Acrobat is different than in a web context. Documentation is even more sparse that on ConTeXt ;) Debugging is only possible in Acrobat Pro, and also there very inconvenient. Additionally, Acrobat’s possibilities change with every version.

Examples

Setting a default value

Here we set a date field to the current date on opening the document. Additionally we have a button that can hide/show a form field.

\starttext
\setupinteraction	[state=start]

\startJSpreamble {EXAMPLE}
var d = new Date();
var df = this.getField("CurDate");
df.value = util.printd("dd.mm.yyyy", d);

function toggleField(){
	var f = this.getField("CurDate");
	f.display = ! f.display;
}
\stopJSpreamble

% this button makes no sense for the example, but in MkIV we need one \goto,
% otherwise we get no JS into the PDF
\goto{toggle}[JS(toggleField)]
\blank

\setupfield[shortString][reset,horizontal][height=5mm, width=50mm, frame=off, bottomframe=on]
\definefield[CurDate][line][shortString][][JavaScript should replace this text with the current date]

Current date: \field[CurDate]

\stoptext

Setting the current date

Similar, but more usable than the example above:

\starttext

\startJSpreamble {EXAMPLE}
function Dummy(){
	return 0;
}

function setCurrentDate(fieldname) {
	var f = this.getField(fieldname);
	f.value = util.printd("yyyy-mm-dd", new Date());
}

setCurrentDate("myDateField");
\stopJSpreamble

\setupfield[dateString][reset,horizontal][width=5em,option=printable]
\definefield[myDateField][line][dateString][][JavaScript should replace this text with the current date]

Current date: \field[myDateField]

\goto{END}[JS(Dummy)] % for MkIV bug
\stoptext


Fields


Someone thinks this entry needs some more explanation. (See: Needs Explanation?, To-Do List.)


Relevant commands:

Field types:

  • line: one line of text
  • text: more lines of text
  • radio: radiobutton (only one of a group can be active)
  • check: checkbox

Beware, for fillinfields in MkIV you need \usemodule[fields]! They’re meant for clozes (texts with gaps, like in questionnaires).

In MkIV (as of 2015-04-01) default values are always used verbatim, i.e. JS() doesn’t work.

Other fields you must first define and then use. That might look complicated, but you can use the same field several times, and the contents will automatically repeat themselves if you need the same content at several places, even on different pages.

Text Entries

\fillinfield[name]{text that defines field length}

or

\setupfield[ShortLine][horizontal][width=2cm]
\definefield [Email] [line] [ShortLine] [] [sample@contextgarden.net]
\field [Email] [your email]

Radiobuttons

Example from the manual:

\setupfield [LogoSetup]
        [width=4cm,
        height=4cm,
        frame=off,
        background=color,
        backgroundcolor=lightgray]

\definefield[Logos] [radio] [LogoSetup][ConTeXt,PPCHTEX,TeXUtil] [PPCHTEX]

\definesubfield [ConTeXt] [] [ConTeXtLogo]
\definesubfield [PPCHTEX] [] [PPCHTEXLogo]
\definesubfield [TeXUtil] [] [TeXUtilLogo]

\definesymbol [ConTeXtLogo] [{\externalfigure[mpcont.502]}]
\definesymbol [PPCHTEXLogo] [{\externalfigure[mpcont.503]}]
\definesymbol [TeXUtilLogo] [{\externalfigure[mpcont.504]}]

\hbox to \hsize{\hss\field[ConTeXt]\hss\field[PPCHTEX]\hss\field[TeXUtil]\hss}

As usual, first you need to define a class of fields (\setupfield). Then you define the (invisible) group of radio buttons (\definefield with "radio"). At last you define the single radio buttons with \setupsubfield.

Arguments of \definefield:

  1. field name
  2. field type "radio"
  3. setup class, as defined by \setupfield
  4. list of field names that should be part of the group
  5. name of default (activated) button

Arguments of \definesubfield:

  1. field name
  2. setup class (default is inherited, but you can use a different one)
  3. content symbol, defined by \definesymbol

Checkboxes

\setupfield[setup 3]
        [width=2cm, height=2cm,
        rulethickness=3pt, corner=round, framecolor=red]

\definesymbol [yes] [{\externalfigure[mpcont.502]}]
\definesymbol [no] []
\definefield [checkme][check] [setup 3] [yes,no] [no]
\field[checkme]

Tricks and Traps

Beware, in MkIV the JS code is only copied to the PDF if there is a \goto referencing one of the defined functions! (reported 2015-04-01, still true 2015-10-05)

Beware, there is no \setupfields (plural)!

This helps debugging (MkII only!):

\tracefieldstrue
\showfields  % typeset a table of field relations
\logfields     % logs field descriptions to a file fields.log

Samples