Creating Package-Specific Context Menus - Maple Help
For the best experience, we recommend viewing online help using Google Chrome or Microsoft Edge.

Online Help

All Products    Maple    MapleSim

Home : Support : Online Help : Share Maple Content : Creating Package-Specific Context Menus

Package-Specific Context Entries


This example outlines how to create package-specific context entries.


The Context Panel provides easy clickable access to many commands in Maple. In Maple's user interface, when your cursor is on either input or output for 2-D math expressions (or output from 1-D math expressions), context-sensitive operations can be found in the Context Panel on the right side of the screen.


In order to make sure that as many people as possible can access commands in your package, it is important to consider adding one or more of your commands to the context-sensitive infrastructure. The following example will show you how that's possible.


Getting Started

First, start with a package that you have authored.

Here is a quick example of a package module that has three exports:
squared: a procedure that takes an integer and squares it

cubed: a procedure that takes an integer and cubes it

parsestring: a procedure that takes a string and attempts to parse it

Here is the output of the package exports.










Adding Context Panel Entries

There are several important parts of building individual entries for the context panel.

Most importantly, you want to know:


WHAT to call the entry; the label to show in the context panel


WHICH command you want to use; the command to apply when the label is clicked


WHEN to show the entry; under what type conditions the option is shown


HOW you will help the user use it; the tooltip to help describe what the command does


The command to add an entry to the current context-sensitive infrastructure is called ContextMenu:-CurrentContext:-Entries:-Add.

Here is a short example of using the command where you add an entry to the context panel that negates a boolean value:


  "Negate",                              #label to show
  "not %EXPR",                           #command to apply
  boolean,                               #Type conditions
  'helpstring'="Negate the expression"): #tooltip to show

Try clicking on the output below and using the "Negate" option from the context panel.




Using "Negate" gives the following:

not (2.1);



The most important part of specifying a context panel entry is determining WHEN to show it. Typically, you should try to show context-sensitive entries whenever Maple detects input that is of the same type as is expected for your routines. For example, the above module expects type integer for the procedures squared and cubed, and type string for the procedure parsestring.


Here is another example using the module above. In this example, you will add a new entry called "Square" that is shown for an integer input.

  "Square",                              #label to show
  ":-MyPackage:-squared(%EXPR)",         #command to apply
  integer,                               #Type conditions
  'helpstring'="Square the expression"): #tooltip to show

Click on the output below, then use the "Square" option from the context panel.




Using "Square" gives the following:




Since the example for cubed will be similar to squared, you can skip that and do one more example for the parsestring procedure.

  "Parse string",                        #label to show
  ":-MyPackage:-parsestring(%EXPR)",     #command to apply
  string,                                #Type conditions
  'helpstring'="Parse the string"):      #tooltip to show








Adding Context Panel Entries to a Package

Next, add the entries for each of the procedures created above to the code for the package module. For a package implemented as a module, new and relevant context-sensitive entries can be created inside the module's ModuleLoad export; this will cause the new context panel entries to be automatically created upon the first invocation or use of the package.


Before adding the entries for the procedures to the package module, make a copy of the current context panel. You can now freely customize the copied panel.

Note: The added entries will not be visible in the Maple context panel until you execute the Install command.

pCM := ContextMenu:-CurrentContext:-Copy():

Queries can be useful while determining when to show your context-sensitive items. New context panel entries can be configured so as to only appear under specific circumstances - such as when the package is loaded (using with).

Add a query to test if your package is loaded or not:

             "Is MyPackage Loaded",
                  member( ':-MyPackage', packages() );
               end proc);

The AddMultiple command adds multiple entries in one call:

                [        "Square",
                          'helpstring'="Square the expression" ],
                  [        "Cube",
                          'helpstring'="Cube the expression" ],
                [        "Parse string",
                          'helpstring'="Parse the string"],
                  'submenu'=["MyPackage"],              #Add a sub-folder for MyPackage
                  'active'="Is MyPackage Loaded");      #the active option tests whether some condition is met( i.e. if "Is MyPackage loaded"? );



The Install command adds the entries to the context panel:


Next, load the package to activate the entries:





Finally, you can put this together in the original procedure, creating a package that contains its own context-sensitive panel code. In order for the context-sensitive code to be available when a package is run, it is required that the code for the actions are added to the ModuleLoad section of the module. If a ModuleLoad local or export is present, then this procedure is called when the module is read from the Maple repository in which it is located (in the code below, this is done manually by calling ModuleLoad() ).



When MyPackage is loaded, the ModuleLoad section is initialized, making the commands available in the context-sensitive system.







Thus, clicking on 3 now shows a submenu for "MyPackage" as well as several available commands.






There are more examples that discuss package authoring and context panels in Maple, including:

Creating a New Package Workbook


Adding an Entry to the Context Menu Module


Return to Index for Example Worksheets