The DocumentTools:-Components subpackage contains commands for constructing Embedded Components as content.
>
|
|
| (2.1) |
An interactive application can be completely programmatically constructed as content.
The following example consists of constructing a Plot and a Button component, each with their own action code. Each time the mouse cursor is clicked within the Plot component a new data point is added to the list assigned to the global name L, and a least squares linear fit of this data is plotted. Clicking on the Button clears the plot.
The application is inserted into the current document for this first example. This example uses the commands Plot and Button to construct the components' contents.
This example also uses two procedures, update and clear, which must be defined (and left as defined) for the inserted application to function. These procedures are called whenever the respective components are clicked.
>
|
|
>
|
|
Next, the two components are constructed. Their respective action code, executed whenever the components are clicked upon, call the aforementioned procedures.
>
|
|
The content construction is then completed, forming a full worksheet in XML representation. Next, this is inserted into the current worksheet.
>
|
|
In the second example, the same application is regenerated, but now the defining of the supporting procedures update and clear is done within the constructed application itself. This allows the application to function even if it is launched or saved as a new worksheet.
In the following revision, the procedure definitions are part of the action code of the two components. An alternative approach is to put those procedure definitions into a Code Edit Region constructed as an additional part of the content.
>
|
|
>
|
|
>
|
|
The ContentToString command is used to create a text string of an actual worksheet. This string can be launched directly in a new window, or saved as a worksheet file.
>
|
|
This worksheet string can be launched directly in a new window using the Display command from the Worksheet package. In such a new window,the application functions independently of any definitions inside the current worksheet.
The following invocation uses the global name syntax :-Worksheet in order to reference the package by that name rather than the export by that same name in the previously loaded Layout package.
>
|
|
The test string of the worksheet can also be saved as a worksheet file.
>
|
|
The saved file is an application that functions independently of any definitions made in the current worksheet.
>
|
|