Sample Maplet Application: Get Equation
This worksheet demonstrates how to write Maplet applications that function similarly to the Get Equation Maplet application available in the Maplets[Examples] package. It is designed for experienced Maple authors.
|
Simple Example
|
|
>
|
# Invoke the Maplets Elements subpackage.
with( Maplets[Elements] ):
|
>
|
# Define the Maplet application.
maplet := Maplet(
# Include a sentence prompt.
[
"Enter an equation:",
# Assign a variable to store the
# equation.
TextField['TF1'](),
# The OK button shuts down the Maplet application and
# returns the equation.
Button( "OK", Shutdown( ['TF1'] ) )
]
):
# Run the Maplet application.
Maplets[Display](maplet);
|
|
|
Refining the Simple Example
|
|
To refine the Maplet application:
1. Add a title to the Maplet application.
2. Process the output.
By default, one window is used in the first example. To customize the window with, for example, a title, the layout must be wrapped in a Window element. Similarly, the text "Enter an equation:" is wrapped in a BoxCell element.
>
|
with(Maplets[Elements]):
maplet := Maplet(
# Label the Maplet application title bar.
Window( 'title' = "Enter an Equation",
[
BoxCell( "Enter an equation:" ),
TextField['TF1'](),
Button( "OK", Shutdown( ['TF1'] ) )
]
)
):
# Assign result to the value returned by the Maplet application.
result := Maplets[Display](maplet):
# Specify the error returned if an equation is not
# entered.
if result = NULL or result = [""] then
error "request failure, an equation was not entered";
else
lprint(result);
result := parse( result[1] );
# Check that the result is type equation.
if type( [result], ['equation'] ) then
result;
else
error "%1 is not of type `%2`", [result], [equation];
end if;
end if;
|
|
|
Using Maplet applications in Modules
|
|
In the following example, the Maplet application is assigned to a local variable when the module is created. This reduces the need for the Maplet application to be constructed each time the procedure MyProc is called and results in faster response times.
>
|
# Define the module MyModule.
MyModule := module()
# Declare the module as a package and
# protect its exports.
option package;
# Declare mymaplet as a local variable.
local mymaplet;
# Declare MyProc as an export.
export MyProc;
# Define the Maplet application mymaplet.
mymaplet := Maplets[Elements][Maplet](
# Label the Maplet application title bar.
Maplets[Elements][Window]( 'title' = "Enter an Equation",
[
Maplets[Elements][BoxCell]( "Enter an equation:" ),
Maplets[Elements][TextField]['TF1'](),
Maplets[Elements][Button]( "OK", Maplets[Elements][Shutdown]( ['TF1'] ) )
]
)
);
# Define the procedure MyProc.
MyProc := proc()
local result;
# Run the Maplet application. Assign result to the value
# returned by the Maplet application.
result := Maplets[Display]( mymaplet );
if result = NULL or result = [""] then
error "request failure, a value was not entered";
else
try
result := parse( result[1] );
catch:
error "the expression `%1` does not parse", result[1];
end try;
if type( [result], ['equation'] ) then
result;
elif type( [result], ['algebraic'] ) then
result = 0;
else
error "the expression `%0` is not of the appropriate type",
result;
end if;
end if;
end proc;
end module:
|
>
|
# Invoke the module MyModule.
with(MyModule):
|
>
|
# Call the procedure MyProc.
MyProc();
|
|
|
Maplets[Examples][GetEquation]
|
|
The Maplets[Examples][GetEquation] function displays a Maplet application similar to that of the previous examples. The function, however, checks if the user entered something of type algebraic, in which case, it automatically equates it to 0.
For help on this Maplet application, see:
>
|
?Maplets[Examples][GetEquation]
|
To view the source code, enter:
>
|
print( Maplets[Examples][GetEquation] );
|
|
Return to Index for Example Worksheets
|