The translation mechanism provides the application developers with a powerful syntax for specifying sequences of events and means to map them to actions. Actually, a level of redirection is introduced at this level. The event sequences are mapped into strings, which are mapped to executable procedures further in the program. This is intended to allow the mapping of events to actions to be specified through the resource database.
To specify translations programmatically the programmer can use one of
these predicates: xtAugmentTranslations/2
or
xtOverrideTranslations/2
. Both of these predicates register a
translation table with a particular widget.
xtAugmentTranslations/2
merges the new translation table with the
current one while xtOverrideTranslations/2
not only does that but
also replaces existing translations with entries from the new
translation table whenever there is a conflict.
The translation table is a structure that is opaque to the Prolog
programmer. One can use the predicate xtParseTranslationTable/2
to
parse the translations string and generate the translation table. The
exact syntax of the translations string is explained in the X Toolkit
documentation.
The predicate xtAppAddActions/2
is used to map the action names to
executable procedures. The first argument is the application context and
the second argument is a list of terms of type
Action. In ProXT the translation procedures have three arguments --
the first argument is the widget where the event occurred, the second
argument is the event that invoked the action and the third is a list of
parameters specified in the action name.
Following is a reimplementation of the previous example using translations for event handling.
create_button :- xtAppInitialize(App,'Demo',[],Shell), xmCreateLabel(Shell,push_button, [xmNwidth(100), xmNheight(100)],Button), xtManageChild(Button), xtAppAddActions(App, [action(foo,pressed)]), xtParseTranslationTable('<Btn1Down: foo("Hello Quintus")', TranslationTable), xtAugmentTranslations(Button,TranslationTable), xtRealizeWidget(Shell), xtAppMainLoop(App). pressed(Widget, Event, ParamList) :- write(Widget), nl, write(Event), nl, write(ParamList), nl.
Registering translations through the resource database is covered to the section dedicated on the resource database.