Sample Maplet Application: Get Expression
This worksheet demonstrates how to write Maplet applications that function similarly to the Get Expression 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 expression:",
# Assign a variable to store the expression.
TextField['TF1'](),
# The OK button shuts down the Maplet application and
# returns the expression.
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 expression:" is wrapped in a BoxCell element.
>
|
with(Maplets[Elements]):
maplet := Maplet(
# Label the Maplet application title bar.
Window( 'title' = "Enter an Expression",
[
BoxCell( "Enter an expression:", left ),
TextField['TF1'](),
Button( "OK", Shutdown( ['TF1'] ) )
]
)
):
# Assign result to the value returned by the Maplet application.
result := Maplets[Display](maplet):
# Specify the error message returned if an expression
# is not entered.
if result = NULL then
error "request failure, an expression was not entered";
else
result := parse( result[1] );
if type( [result], ['algebraic'] ) then
result;
else
error "%1 is not of type `%2`", [result], [algebraic];
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 Expression",
[
Maplets[Elements][BoxCell]( "Enter an expression:", left ),
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 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], ['algebraic'] ) then
result;
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][GetExpression]
|
|
The Maplets[Examples][GetExpression] function displays a Maplet application similar to that of the previous examples.
For help on this Maplet application, see:
>
|
?Maplets[Examples][GetExpression]
|
To view the source code, enter:
>
|
print( Maplets[Examples][GetExpression] );
|
|
Return to Index for Example Worksheets
|