Function Object Callbacks
The syntax of a function object callback is the following:
<callback> (o_field g_scope@rest_args)
-
o_field
is the field whose callback is triggered. It can be used to access the properties of the field (o_field->value). -
g_scope is the location where the field is instantiated. It can be a HI form, a scroll-region field, or a page of a tab field. It can be used to access fields that are placed in the same scroll-region, tab page, or form where the field (o_field) for which the callback is triggered placed. To access the fields in the same form but outside g_scope, a function is provided. The function name is hiGetFieldScope.
The syntax for hiGetFieldScope is(hiGetFieldScopeg_field). When the given g_field is a HI field, which can be a scroll-region, tab or tab-page field, the function returns the scope containing the field. The return value can be a scroll-region, a tab page, a tab field, form, or nil. -
@rest_args is a variable argument list that is used for those fields whose callback expects one or more additional arguments.
Examples
In this example, the function make_radians_degrees_fields() creates two type-in fields, Radians and Degrees with a local callback converter. The callback is used to set the value of the Radians field based on the value of the Degrees field and vice versa. Also, in this example, the callback is not defined in the global namespace and that there is no need to know where the two type-in fields are to be placed.
(defun converter (field scope)
(let ((scale 180.0/3.14159265))
(caseq field->hiFieldSym
(radians scope->degrees->value = (round field->value * scale))
(degrees scope->radians->value = field->value / scale)
(t (assert "this shouldn't happen")))))
(defun make_radians_degrees_fields ()
(list
(hiCreateFloatField ?name 'radians ?callback 'converter ?prompt "Radians" ?value 0.0)
(hiCreateIntField ?name 'degrees ?callback 'converter ?prompt "Degrees" ?value 0)))
;; create the form
Form2D = hiCreateAppForm(
?name 'Form2D
?formTitle "R2D&D2R Converter"
?fields (make_radians_degrees_fields)
)
hiDisplayForm(Form2D)
In this example, the function list_png_files() searches for all the .png files in the given directory, lists them in a cyclic field, and prints the filename when an icon is selected from the cyclic field.
(defun list_png_files ( dir )
(let ((png_files (rexMatchList ".png$" (getDirFiles dir)))
(png_table (makeTable "icon to .png file name" nil)))
;; establish mapping
(mapcar (lambda (x) png_table[ (hiLoadIconFile x 24 24) ] = x) png_files)
(hiDisplayForm
(hiCreateAppForm
?name 'png_list_form ?formTitle "PNG List" ?buttonLayout 'Close
?fields (list (hiCreateCyclicField
?name 'icons ?prompt "Icons" ?choices png_table->?
?callback (lambda (field scope item)
(printf "selected item is %L\n" png_table[ item]))))))))
(list_png_files (prependInstallPath "../../share/cdssetup/icons/24x24"))
Return to top