Sample Maplet Application: Selection
This worksheet demonstrates how to write Maplet applications that function similarly to the Selection 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.
[
"Make a selection:",
# The GridLayout element creates a matrix-like
# layout. Each row of the GridLayout element
# contains a checkbox.
GridLayout([
["a", CheckBox['ChB1']()],
["b", CheckBox['ChB2']()],
["c", CheckBox['ChB3']()]
]),
# The "OK" button shuts down the Maplet application and
# returns the results of the check boxes.
[Button( "OK", Shutdown( ['ChB1', 'ChB2', 'ChB3'] ) ),
# The "Cancel" button shuts down the Maplet application
# without returning any results.
Button( "Cancel", Shutdown() )]
]
):
# Run the Maplet application.
Maplets[Display](maplet):
|
|
|
Refining the Simple Example Maplet Application
|
|
To refine the Maplet application, add a title to the Maplet application.
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 "Make a selection:" is wrapped in a BoxCell element.
>
|
maplet := Maplet(
# Label the Maplet application title bar.
Window( 'title' = "Selection",
[
BoxCell( "Make a selection:" ),
GridLayout([
["a", CheckBox['ChB1']()],
["b", CheckBox['ChB2']()],
["c", CheckBox['ChB3']()]
]),
[Button( "OK", Shutdown( ['ChB1', 'ChB2', 'ChB3'] ) ),
Button( "Cancel", Shutdown() )]
]
)
):
Maplets[Display](maplet):
|
|
|
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;
# Invoke the Maplets Elements subpackage.
use Maplets[Elements] in
# Define the Maplet application mymaplet.
mymaplet := Maplet(
Window( 'title' = "Selection",
[
BoxCell( "Make a selection:" ),
GridLayout([
["a", CheckBox['ChB1']()],
["b", CheckBox['ChB2']()],
["c", CheckBox['ChB3']()]
]),
[Button( "OK", Shutdown( ['ChB1', 'ChB2', 'ChB3'] ) ),
Button( "Cancel", Shutdown() )]
]
)
);
end use;
# 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, no selection was made";
else
seq( parse( i ), i = result );
end if;
end proc;
end module:
|
>
|
# Invoke the module MyModule and call the procedure
# MyProc.
MyModule[MyProc]();
|
|
|
Using Radio Buttons instead of Check Boxes
|
|
To create a Maplet application where the user can select exactly one object from a, b, or c:
1. Replace the check boxes with radio buttons.
All radio buttons which have the same group = X option are logically grouped so that at any time, only one button may be selected.
2. Include a button group.
>
|
with(Maplets[Elements]):
maplet := Maplet(
Window( 'title' = "Selection",
[
BoxCell( "Make a selection:" ),
# The check boxes have been replaced with radio
# buttons. A button group has been included.
GridLayout([
["a", RadioButton[ChB1]( 'group' = 'BG1' )],
["b", RadioButton[ChB2]( true, 'group' = 'BG1' )],
["c", RadioButton[ChB3]( 'group' = 'BG1' )]
]),
[Button( "OK", Shutdown( ['ChB1', 'ChB2', 'ChB3'] ) ),
Button( "Cancel", Shutdown() )]
]
),
ButtonGroup['BG1']()
):
Maplets[Display](maplet):
|
Note: It is convention to use radio buttons when only one option is to be selected, and check boxes when zero or more options may be selected.
|
|
Maplets[Examples][Selection]
|
|
The Maplets[Examples][Selection] function displays a similar Maplet application to that of the previous examples. The function, however, allows the user to choose from an arbitrary list of objects.
For help on this Maplet application, see:
>
|
?Maplets[Examples][Selection]
|
To view the source code, enter:
>
|
print( Maplets[Examples][Selection] );
|
|
Return to Index for Example Worksheets
|