| Build Your Own Context-Sensitive
Menu
Every new application comes with a learning curve. Maplesoft is going
to great lengths to assist novice users in making this learning curve
as short as possible. You’ve seen how easy it is to build a graphical
user interface to access the Maple kernel using the Maplets package.
Do you know that you can build your own context-sensitive menu just as
easily using the context package? A context-sensitive menu associates
Maple output expressions with specific Maple actions. Once an action is
selected, not only is the action performed on the expression, but the
Maple routine is also displayed to show users the corresponding Maple
command. When the full expression is selected the context package
is used for the creation and maintenance of procedures that generate a
context-sensitive menu.
In this article, we will build a simple context-sensitive menu to some
of the routines in the Student[Calculus1] package. Specifically,
this menu will allow users to ask Maple for a hint, undo a rule or apply
the product rule to differentiation problems..
> with(context);
[buildcontext, clearlabels, defaultcontext, display, installconext,
restoredefault, testactions, troubleshoot]
Context-sensitive menus are built using classes. A class requires a unique
name for the menu, a Boolean statement that will test expressions for
membership in this class, and an explanation of what this class contains.
In the following example, the unique name is “Differentiate”. The Boolean
statement tests if an expression is in the inert differentiation form.
This is required to access the routines from within the Student[Calculus1]
package. A second test is performed to verify if a hint can be applied
to the problem. This is needed to invoke the context-sensitive menu on
expressions that are incomplete while in the single-stepping process.
The description for this menu is, “Interface to Student[Calculus1]”.
Once the class has been defined, you need to design the menu items with
respective actions, called CF_ACTION. CF_ACTION definitions require the
following entries:
MenuString |
The string that appears in the context-sensitive menu. |
HelpString |
The text that appears in the bubble help |
CodeType |
The type of action. |
Code |
The action that will be invoked once selected. |
> CMCalc:=
CF_CLASS(
"Key" = "Differentiate",
"Test" = "type(f, specfunc(anything, Diff))
or evalb(Student:-Calculus1:-Hint(f)<>[NULL])",
"Description" = "Interface to Student[Calculus1]",
CF_ACTION(
"MenuString" = "Hint",
"HelpString" = "Ask for a hint",
"CodeType" = "complete",
"Code" = "Student:-Calculus1:-Hint(f)"),
CF_ACTION(
"MenuString" = "Undo",
"HelpString" = "Undo the previous rule",
"CodeType" = "complete",
"Code" = "Student:-Calculus1:-Undo(f)"),
CF_ACTION(
"MenuString" = "Product Rule",
"HelpString" = "Apply the Product Rule",
"CodeType" = "complete",
"Code" = "if nops([Student:-Calculus1:-Rule
[product](f), Student:-Calculus1:-GetMessage()])=1 then
Student:-Calculus1:-Rule[product](f)
else
`Product Rule is not applicable` end if:")
):
The last action requires a procedure as the Code. This procedure
verifies if the product Rule is applicable to this expression. If not,
an error message will display.
Now that the context-sensitive menu definition is complete, we need to
build the menu.
> CM := buildcontext(CMCalc):
Install the context-sensitive menu.
> installcontext(CM):
Now let's try it out. Right click on the output to display the menu.
> Diff(sin(x)*cos(x), x);
> if nops([Student:-Calculus1:-Rule[product](Diff(sin(x)*x,x)),
Student:-Calculus1:-GetMessage()]) = 1 then Student:-Calculus1:-
Rule[product](Diff(sin(x)*x,x)) else `Product Rule is not
applicable` end if;
> R2 := Student:-Calculus1:-Hint(Diff(sin(x)*cos(x),x));
R2 := [product]
Installing your context-sensitive menu, temporarily disables the default
menus. Restoring the default menus is done with the restoredefault
command.
> restoredefault();
For more information, to edit the default context-sensitive menu, or
to see the advanced options available in the context package, visit
Maple’s Help page by entering ?context at the Maple prompt.
| Article: Build Your Own Context-Sensitive Menu |
|