Tutorial: Creating a Maplet Application
A First Maplet Application
This Maplet application displays a window with the text Hello world.
The top-level element of all Maplet applications must be the element Maplet.
restart;
with(Maplets[Elements]):
Maplets[Display]( Maplet( ["Hello world"] ) );
When the [x] (close icon) in the window corner is clicked, the Maplet application closes and control is returned to the Maple session. Note: The close icon varies with window manager.
NestedLists
A Maplet application is defined by using a box layout structure. The box layout structure is based on the Maple concept of nested lists (list of lists). In Maple, a list is an ordered sequence of comma-delimited expressions that is enclosed in square brackets ([ ]).
For example:
List := [1,5,7];
A nested list is an ordered sequence of expressions that is enclosed in square brackets, in which each expression can be a list.
NestedList := [1, [2,3], [4, 5,6], 7, 8, [9,10]];
When writing a Maplet application, text strings, user prompts, or text fields are defined as expressions in lists.
A Maplet application window definition includes the main list which contains the lists of text strings, user prompts, and other elements.
Adding a Button
In this example, an OK button is included, which the user can click to close the Maplet application.
The button is defined by using the Button element. It contains two pieces of information:
- The text that appears on the button.
- The action that occurs when the button is clicked.
In this example, the Maplet application accesses the Shutdown action element without any arguments when the button is clicked.
In the A First Maplet Application example, the nested list is interpreted as a window with a text box.
To add a button, insert a Button element into the nested list. In this Maplet application, the text is displayed
beside the button.
maplet2a := Maplet( [ ["Hello world", Button("OK", Shutdown())] ] ): Maplets[Display]( maplet2a );
To specify that the button is located below the text (that is, in a second row), place the text and button in distinct lists in the nested list.
maplet2b := Maplet( [ "Hello world", Button("OK", Shutdown()) ] ): Maplets[Display]( maplet2b );
Adding a Title
In this example, a title is added to the window.
In the Adding a Button Maplet application examples, the nested list is interpreted, by default, as a window.
To specify options other than the default values, you must explicitly include the Window element.
To specify a title for the window, wrap the nested list in a Window element.
maplet3 := Maplet( Window( 'title'= "3rd", [ "Hello world", Button("OK", Shutdown()) ] ) ): Maplets[Display]( maplet3 );
Getting Input from the User
In this example, an input field is included. There are two buttons:
- OK, which closes the Maplet application and returns the input text to the Maple session.
- Clear, which resets the input field value to "".
Insert a TextField element in the Maplet application. The input field requires a reference so that its value can be returned. Any element in a Maplet application can be given a unique identifier by placing a reference in the element index, for example, TextField[TF1]().
To clear the input field when the user clicks the Clear button, use the SetOption action element.
maplet4 := Maplet( Window( 'title'="4th", [ "Enter something:", TextField['TF1'](), [Button("OK", Shutdown(['TF1'])), Button("Clear", SetOption('TF1' = ""))] ] ) ): Maplets[Display]( maplet4 );
Note: The return value is a list containing the string corresponding to the value entered by the user. If more than one reference is given in the Shutdown action element, then a list of strings is returned.
Returning Output in the Maplet Application Window
In this example, an output field displays the output of a call to the Maple engine. The Maplet application prompts the user for an integrand and a variable of integration. When the user clicks the Integrate button, the Maplet application calls int to calculate the integral and displays the anti-derivative in the output box. When the Maplet application is closed, the value of the integrand, variable of integration, and the anti-derivative are returned to the Maple session.
Two input fields TF1 and TF2 are required.
The output is placed in a TextBox element TB1 of height 3 and width 40. The user should not be allowed to edit the output: use the option not editable.
The element Evaluate( TB1 = 'int( TF1, TF2 )' ) calls int using the contents of TF1 and TF2, and places the result in the box TB1.
maplet5 := Maplet( Window( 'title'="5th", [ ["Integrand: ", TextField['TF1']()], ["Variable of Integration: ", TextField['TF2'](3)], TextBox['TB1']('editable' = 'false', 3..40 ), [Button("Integrate", Evaluate('TB1' = 'int(TF1, TF2)')), Button("OK", Shutdown(['TF1', 'TF2', 'TB1'])), Button("Clear", SetOption('TF1' = ""))] ] ) ): Maplets[Display]( maplet5 );
Error Handling
The previous example (maplet5) does not provide useful error messages, for example, the error messages generated when you do not enter data in both input fields.
The following Maplet application uses the Get routine found in the Maplets[Tools] package. The Get routine checks whether an argument parses and is of the appropriate type.
In this example, an initial procedure is added to the Maplet application. The Maplet application evaluates the input value after it is checked for errors in the MyInt procedure.
MyInt := proc() local integrand, var; use Maplets[Tools] in integrand := Get( 'TF1'::algebraic ); var := Get( 'TF2'::name ); end use; int( integrand, var ); end proc:
maplet6 := Maplet( Window( 'title'="6th", [ ["Integrand: ", TextField['TF1']()], ["Variable of Integration: ", TextField['TF2'](3)], TextBox['TB1']( 'editable' = 'false', 3..40 ), [Button( "Integrate", Evaluate('TB1' = "MyInt" ) ), Button( "OK", Shutdown(['TF1', 'TF2', 'TB1'] ) ), Button( "Clear", SetOption('TF1' = "" ) )] ] ) ): Maplets[Display]( maplet6 );
Adding a Help Dialog and a Plot
In this example, the Maplet application contains:
A button that displays a help dialog.
A plot window with a slider that is used to indicate the right end point of the plot.
A vertical scroll bar that allows a user to view buttons if the Maplet application is reduced on the display monitor.
The plot window is defined by using a Plotter element. The plot is displayed when the user clicks the Plot button, which runs the Evaluate( PL1 = 'plot([TF1, TB1], TF2=0..SL1)' ) element.
The slider is implemented by using a Slider element. The slider value varies from 1 to 20 with a default value of 5. The RunDialog action element, which refers to the MessageDialog element in the Maplet application, displays a help dialog.
maplet7 := Maplet( Window( 'title'="7th", BoxColumn('vscroll'='always', ["Integrand: ", TextField['TF1']()], ["Variable of Integration: ", TextField['TF2'](3)], TextBox['TB1']( 'editable' = 'false', 3..40 ), Plotter['PL1'](), Slider['SL1']( 0..20, 5, 'showticks', 'majorticks'=5, 'minorticks'=1, 'visible'='false', Evaluate( 'PL1' = 'plot([TF1, TB1], TF2=0..SL1)' ) ), [Button("Integrate", Action(Evaluate('TB1' = 'int(TF1, TF2)'), SetOption('B1'(enabled)='true'))), Button("OK", Shutdown(['TF1', 'TF2', 'TB1'])), Button("Clear", Action(SetOption('TF1' = ""), SetOption('TF2' = ""), SetOption('TB1' = ""), SetOption('B1'(enabled)='false'), SetOption('SL1'('visible')='false'), Evaluate( 'PL1' = 'plot(undefined, x=0..SL1)' ))), Button("Help", RunDialog('MD1')), Button['B1']("Plot", 'enabled'='false', Action( SetOption('SL1'('visible')='true'), Evaluate( 'PL1' = 'plot([TF1, TB1], TF2=0..SL1)' ) ) )] ) ), MessageDialog['MD1']( "An expression which, being integrated, produces a given integral.", 'type'='information' ) ): Maplets[Display](maplet7);
Other Elements
There are many elements in the Maplets package. A few of the most important are discussed in the following subsections.
Window Body Elements
Button
A button can be selected by the user which may in turn trigger an action. A button has a caption but no value, unlike the ToggleButton which may take either a true or false value.
Maplets[Display]( Maplet( [ Button("OK", Shutdown()) ] ) );
CheckBox
A check box takes a true or false value.
Maplets[Display]( Maplet( [ [CheckBox['ChB1']( 'value' = 'true' )], [Button("OK", Shutdown(['ChB1']))] ] ) );
ComboBox
A combo box is a list of strings from which the user can either choose an entry or type in a value. The default value of the combination box is set by using the 'value' = "string" option. Otherwise, it is the first string in the box. Once the user selects a string, the value of the combination box is the most recently selected string. This is useful where the list of objects cannot contain all possible valid entries.
Maplets[Display]( Maplet( [ ComboBox['CoB1']( 'value' = "blue", ["red", "orange", "yellow", "green", "blue", "violet"] ), Button("OK", Shutdown(['CoB1'])) ] ) );
DropDownBox
A drop-down box is a list of strings from which the user chooses. The default value of the combination box is set by using the 'value' = "string" option. Otherwise, it is the first string in the box. Once the user selects a string, the value of the combination box is the most recently selected string. This is preferable to a ComboBox when the set of acceptable entries is well defined.
Maplets[Display]( Maplet( [ DropDownBox['DDB1']( 'value' = "Victoria", [ "Victoria", "Edmonton", "Regina", "Winnipeg", "Toronto", "Quebec City", "Fredericton", "Halifax", "Charlottetown", "St. John's", "Whitehorse", "Yellowknife", "Iqaluit"] ), Button("OK", Shutdown(['DDB1'])) ] ) );
Label
The label may contain either text or an image. The text cannot be selected or changed by the user. Within a layout, strings are automatically wrapped in Label elements, though if a non-standard font is to be used, an explicit Label element must be specified.
Maplets[Display]( Maplet( [ "Standard text", Label( "Italicized text", 'font' = Font( helvetica, italic, 25 ) ), Button("OK", Shutdown()) ] ) );
ListBox
A list box allows the user to select zero or more entries from a list. To make multiple selections, the user must hold the [Ctrl] key while clicking the items in the list . The result from a list box is a comma separated list which may be split using the Maplets[Tools][ListBoxSplit] routine.
result := Maplets[Display]( Maplet( [ ListBox['LB1']( 'value' = "Victoria", [ "Victoria", "Edmonton", "Regina", "Winnipeg", "Toronto", "Quebec City", "Fredericton", "Halifax", "Charlottetown", "St. John's", "Whitehorse", "Yellowknife", "Iqaluit"] ), Button("OK", Shutdown(['LB1'])) ] ) ): if result <> NULL then Maplets[Tools][ListBoxSplit]( result[1] ); end if;
MathML Viewer
The MathMLViewer element displays MathML. While the default value may be a non-MathML expression, any subsequent assignments must be valid MathML strings.
Maplets[Display]( Maplet( [ "Enter some expression to display in MathML:", TextField['TF1'](), MathMLViewer['MMLV1']( 'value' = x^2 - 4*x + 3 ), [Button("Display", Evaluate( 'MMLV1' = 'MathML[Export](TF1)' ) ), Button("OK", Shutdown())] ] ) );
Plotter
The Plotter element displays either a 2-D or a 3-D plot.
Maplets[Display]( Maplet( [ "Enter some expression to plot on x=0..10:", TextField['TF1']( 'value' = x^2 - 4*x + 3), Plotter['PL1']( 'value' = plot( x^2 - 4*x + 3, x=0..10 ) ), [Button("Display", Evaluate( 'PL1' = 'plot(TF1, x=0..10)' ) ), Button("OK", Shutdown())] ] ) );
RadioButton
Like a check box, a radio button takes a true or false value. If the radio button is part of a button group (defined by a ButtonGroup element), then at most one button can have the value true.
Maplets[Display]( Maplet( [ [RadioButton['RB1']( "1st", true, 'group' = BG1 ), RadioButton['RB2']( "2nd", 'group' = 'BG1' )], [Button("OK", Shutdown(['RB1', 'RB2']))]], ButtonGroup['BG1']() ) );
Slider
A slider can take on integer values. The option majorticks defines how often a numbered tick mark should occur, and minorticks defines how often a smaller unnumbered tick mark should occur.
Maplets[Display]( Maplet( [ "Select an integer:", Slider['SL1']( 10, 0..20, 'majorticks'=10, 'minorticks'=2, 'showticks' ), Button("OK", Shutdown(['SL1'])) ] ) );
Table
A table is used to display textual information in an organized format.
IL := [sin(x), cos(x), tan(x), sec(x), csc(x), cot(x)]: Maplets[Display]( Maplet( [ Table( ["integrand", "integral"], [seq( [i, int( i, x )], i = IL )], 'width'=400 ), Button("OK", Shutdown()) ] ) );
Text Box
A text box produces a multiple line box for input, output, or labels.
The content of a text box can be selected, and a pop-up menu is displayed if the text box is right-clicked.
with(Maplets[Elements]): maplet := Maplet([ ["Enter text: ", BoxCell(TextBox['IB1'](3..30), 'as_needed')], [Button("OK", Shutdown(['IB1'])), Button("Cancel", Shutdown())] ]): Maplets[Display](maplet);
A text box can be used to both display information to the user and to get information from the user.
Maplets[Display]( Maplet( [ "Enter an expression to integrate w.r.t. x:", [TextBox['TF1']('value' = x^2 - 4*x + 3)], [Button("Integrate", Evaluate( 'TF1' = 'int(TF1, x)' ) ), Button("OK", Shutdown(['TF1']))] ] ) );
To create a text box for output, set the editable option to false.
with(Maplets[Elements]): maplet := Maplet([ [TextBox['IB1']('editable'='false', "This text is inside the TextBox. You cannot type in this box.")], [Button("OK", Shutdown(['IB1'])), Button("Cancel", Shutdown())] ]): Maplets[Display](maplet);
Text Field
A text field produces a single line input or output field. A text field can be used to both display information to the user and to get information from the user. The content of a text field can be selected, and a pop-up menu is displayed if the text field is right-clicked.
Maplets[Display]( Maplet( [ "Enter an expression to integrate w.r.t. x:", TextField['TF1']( 'value' = x^2 - 4*x + 3), [Button("Integrate", Evaluate( 'TF1' = 'int(TF1, x)' ) ), Button("OK", Shutdown(['TF1']))] ] ) );
Toggle Button
Like a check box or radio button, a toggle button takes a true or false value.
Maplets[Display]( Maplet( [ [ToggleButton['RB1']( "1st", true ), ToggleButton['RB2']( "2nd" )], Button("OK", Shutdown(['RB1', 'RB2']))]) );
Dialog Elements
Maplet application dialog elements have a predefined layout. For a dialog, an author cannot specify elements. This is different from window elements, which can contain other elements, for example, buttons and layout elements. In most cases, dialog elements have a value (with the exception of the MessageDialog element) and the results can be used by the Maplet application for further processing.
Alert Dialog
The alert dialog allows the user to respond to a caption either by selecting OK or Cancel.
Maplets[Display]( Maplet( AlertDialog( "Assuming x > 0 leads to a contradiction", 'onapprove' = Shutdown('true'), 'oncancel' = Shutdown("FAIL") ) ) );
Color Dialog
The color dialog allows the user to select a color using either swatches or RGB and HSB palettes.
Maplets[Display]( Maplet( ColorDialog['CD1']( 'onapprove' = Shutdown(['CD1']), 'oncancel' = Shutdown() ) ) );
Confirm Dialog
The confirm dialog allows the user to respond to a caption by selecting either Yes, No, or Cancel.
Maplets[Display]( Maplet(ConfirmDialog( 'question', "Is x > 0 ?", 'onapprove' = Shutdown('true'), 'ondecline' = Shutdown('false'), 'oncancel' = Shutdown("FAIL") ) ) );
File Dialog
The file dialog allows the user to select a file.
Maplets[Display]( Maplet( FileDialog['FD1']( 'onapprove' = Shutdown(['FD1']), 'oncancel' = Shutdown() ) ) );
Input Dialog
The input dialog allows the user to respond to a caption either by entering text and selecting OK, or by selecting Cancel.
Maplets[Display]( Maplet( InputDialog['ID1']("Enter an integer", 'onapprove'=Shutdown(['ID1']), 'oncancel'=Shutdown() ) ) );
Message Dialog
The message dialog displays a caption which the user may dismiss by selecting the OK button.
Maplets[Display]( Maplet( MessageDialog( warning, "Contradictory assumptions on `x`", 'onapprove'=Shutdown() ) ) );
Question Dialog
The question dialog allows the user to respond to a question by selecting either Yes or No.
Maplets[Display]( Maplet( QuestionDialog("Is x > 0?", 'onapprove'=Shutdown('true'), 'ondecline'=Shutdown('false') ) ) );
Menu Elements
A menu bar can be inserted in a window. This menu bar can contain any number of menus, each of which is made up of menu items and separators.
Maplets[Display]( Maplet( Window('title'="Integration and Differentiation", 'menubar'='MB1', ["Enter an expression and select a command from the menu:",[TextField['TF1']()], Button("Exit", Shutdown("Closed from button", ['TF1']))]), MenuBar['MB1']( Menu("File", MenuItem("Close", Shutdown("Closed from menu", ['TF1']))), Menu("Commands", MenuItem("Integrate", Evaluate('TF1' = 'int(TF1, x)') ), MenuSeparator(), MenuItem("Differentiate", Evaluate('TF1' = 'diff(TF1, x)')) ) ) ) );
Toolbar Elements
A toolbar can be inserted in a window. This toolbar can contain any number of toolbar buttons and separators.
Maplets[Display]( Maplet( Window('toolbar'='TB1', [TextField['TF1'](), Button("OK", Shutdown(['TF1']))]), ToolBar['TB1']( ToolBarButton("Integrate", Evaluate('TF1' = 'int(TF1, x)') ), ToolBarSeparator(), ToolBarButton("Differentiate", Evaluate('TF1' = 'diff(TF1, x)')) ) ) );
Command Elements
An Action element may contain any number of command elements. When an action is performed, each command element is executed in order.
Close Window
Close a window.
with(Maplets[Elements]): maplet := Maplet('onstartup' = 'A1', Window['W1']("1", [Button("Run New Window", RunWindow('W2')), Button("Exit", Shutdown("1"))] ), Window['W2']("2", [Button("Close This Window", CloseWindow('W2')), Button("Exit", Shutdown("2"))] ), Action['A1'](RunWindow('W1')) ): Maplets[Display](maplet);
Evaluate
Evaluate a maple expression.
Maplets[Display]( Maplet( [ ["Enter an expression", TextField['TF1']('width' = 30)], [ "Differentiate w.r.t. x:", Button("Calculate", Evaluate('TF1' = 'diff(TF1, x)')), Button("OK", Shutdown(['TF1'])) ] ] ) );
RunDialog
Display a dialog element.
Maplets[Display]( Maplet( Window([ [TextField['TF1']()], [ Button("Diff w.r.t. x", Evaluate('TF1' = 'diff(TF1, x)')), Button("Help", RunDialog('MD1')), Button("OK", Shutdown(['TF1'])) ] ]), MessageDialog['MD1']("See ?diff for help with the differentiation command") ) );
RunWindow
Display a window element.
with(Maplets[Elements]): maplet := Maplet('onstartup' = 'A1', Window['W1']('title' = "Select Method", 'layout' = 'BL0'), BoxLayout['BL0']( BoxColumn( BoxRow("Select a method"), BoxRow( Button("Differentiation", RunWindow('W2')), Button("Integration", RunWindow('W3')) ) ) ), Window['W2']('title'="Differentiation", [ [ "Enter an expression:", TextField['TF1']() ], [ Button("Differentiate with respect to x", Evaluate('TF1' = 'diff(TF1, x)')), Button("Exit", Shutdown(['TF1'])) ] ]), Window['W3']('title'="Integration", [ [ "Enter an integrand:", TextField['TF2']() ], [ Button("Integrate with respect to x", Evaluate('TF2' = 'int(TF2, x)')), Button("Exit", Shutdown(['TF2'])) ] ]), Action['A1'](RunWindow('W1')) ): Maplets[Display](maplet);
SetOption
Set a Maplet application option to a value.
Maplets[Display]( Maplet( [ "Enter some text:", TextField['B1'](20), TextField['B2'](20, 'editable'='false'), [ Button("Clear 1st Field", SetOption('target' = 'B1', 'value' = "")), Button("Copy to 2nd Field", SetOption('target' = 'B2', Argument('B1'))), Button("Return 2nd Field", Shutdown(['B2'])) ] ] ) );
Shutdown
Shut the Maplet application down, possibly returning a value or option values within the Maplet application.
Maplets[Display](Maplet([[Button("OK", Shutdown())]]));
Layout Elements
Box Layout
A box layout arranges objects in columns or rows. Columns and rows can be nested . Alignment can be fixed in either the horizontal or vertical direction, but never both. A list within a Window or Maplet element is assumed to be a box layout.
Maplets[Display]( Maplet( Window(["A", [["B", "C"], "D", [["E", "F", "G"], "H", "I"], "J"], Button("OK", Shutdown())]) ) );
Grid Layout
A grid layout allows for a square grid with both vertical and horizontal alignment.
Maplets[Display]( Maplet( Window([GridLayout( [["A", "B"], ["C", "D"], ["E", "F"]] ), Button("OK", Shutdown())]) ) );
See Also
Authoring Maplet Applications for MapleNet, Maplets[Elements], Maplets Style Guide, Overview of Maplet Applications
Return to Index for Example Worksheets
Download Help Document