Tutorial
Submenus
The tutorial looks at the writing macros with submenus. It assumes that you are already
familiar with macros. If not,
it is suggested that you look at the first 3 lessons in the
Introduction to macros
Tutorial. It also assumes that you are familiar the "menu"
commands: {MenuOptionX ...}, {ShowMenu}, {IfOptionX ...} and {ElseOption ...}.
If not, please look at the tutorial Writing Macros with Menus.
{Comment
**** This macro demonstrates one way of setting up submenus.
**** Method 1. All the menus are contained in the same macro.
**** First set up and display the primary menu
}
{MenuOption}{MenuOptionA Pets}{MenuOptionB African animals}{ShowMenu}{Comment
**** Process option A setting up a submenu.
}
{IfOptionA {MenuOption}{MenuOptionC Cat}{MenuOptionD Dog}{ShowMenu}{Comment
}
{IfOptionC Cats meow. }{IfOptionD Dogs bark. }}{Comment
**** Process option B setting up another submenu.
}
{IfOptionB {MenuOption}{MenuOptionE Elephant}{MenuOptionT Tiger}{ShowMenu}{Comment
}
{IfOptionE Elephants are big. }{IfOptionT Tigers roar. }}
If you copy or type this macro, make sure that you don't include any blank
spaces or lines in the macro.
Right click to download SubmenusMethod1.macro.
Displaying submenus doesn't require any new commands. It just requires responding
to one menu item with another set of menu commands.
The example is obviously trivial and is designed to keep things as simple as
possible.
Method 1
Lets look at the commands.
-
{MenuOption}{MenuOptionA Pet}{MenuOptionB African animals}{ShowMenu}:
The main menu has two options: A: "Pet" and B: "African animals". The first "{MenuOption}"
just clears any previous menu. {ShowMenu} shows the menu.
-
The second menu is invoked if the user selected "Pets"
Tigers roar. Cats meow.
{IfOptionA {MenuOption}{MenuOptionC Cat}{MenuOptionD Dog}{ShowMenu}{Comment
}{IfOptionC Cats meow. }{IfOptionD Dogs bark. }}
Again their are just two menu items C, "Cat" and D: "Dog". When the user selects
one of these items, "Cats meow."
or "Dogs bark." is typed into the file.
-
{IfOptionB {MenuOption}{MenuOptionE Elephant}{MenuOptionT Tiger}{ShowMenu}{Comment
}{IfOptionE Elephants are big. }{IfOptionT Tigers roar. }}
On the other hand if the user selected B, "African animals", the user gets the choice of
Elephants and Tigers. Either "Elephants are big" or "Tigers roar" will be added to the file.
If you run this macro, you will discover that these submenu are different than the normal
Windows submenus. When you pick an item from the main menu, it disappears and is replaced
by the appropriate submenu.
{Comment
**** This macro demonstrates one way of setting up submenus.
**** Method 2. Process submenu items separately.
**** First set up and display the primary menu
}
{MenuOption}{MenuOptionA Pets}{MenuOptionB African animals}{ShowMenu}{Comment
**** Set up and display option A submenu.
}
{IfOptionA {MenuOption}{MenuOptionC Cat}{MenuOptionD Dog}{ShowMenu}}{Comment
**** Set up and display option B submenu.
}
{IfOptionB {MenuOption}{MenuOptionE Elephant}{MenuOptionT Tiger}{ShowMenu}}{Comment
**** Process submenu items
}
{IfOptionC Cats meow. }{Comment
}
{IfOptionD Dogs bark. }{Comment
}
{IfOptionE Elephants are big. }{Comment
}
{IfOptionT Tigers roar. }
If you copy or type this macro, make sure that you don't include any blank
spaces or lines lines in the macro.
Right click to download SubmenusMethod2.macro.
Method 2
Options A and B got pretty complicated in Method 1. Because all the option characters
are distinct, we can arrange the macro a differently to make it look simpler and more
readable. The user will be unable to tell the difference so the menus will not be repeated here.
The main menu is unchanged so we begin with processing options B and C.
-
{IfOptionA {MenuOption}{MenuOptionC Cat}{MenuOptionD Dog}{ShowMenu}}
When processing option A, all we do is set up its submenu.
-
{IfOptionB {MenuOption}{MenuOptionE Elephant}{MenuOptionT Tiger}{ShowMenu}}
Likewise for option B
-
{IfOptionC Cats meow. }{Comment
}{IfOptionD Dogs bark. }{Comment
}{IfOptionE Elephants are big. }{Comment
}{IfOptionT Tigers roar. }
Finally we specify how to process the submenu items. In this case, each item is quite
simple.
Macro for the main menu
{Comment
**** This macro demonstrates one way of setting up submenus.
**** Method 3. Process submenu items in separate files.
**** First set up and display the primary menu
}
{MenuOption}{MenuOptionA Pets}{MenuOptionB African animals}{ShowMenu}{Comment
**** Set up and display option A submenu.
}
{IfOptionA {Read SubmenusMethod3Pets.macro}}{Comment
**** Set up and display option B submenu.
}
{IfOptionB {Read SubmenusMethod3Africa.macro}}
Macro for the Pet submenu - SubmenusMethod3Pets.macro
{Comment
**** Submenu for pets
}
{MenuOption}{MenuOptionC Cat}{MenuOptionD Dog}{ShowMenu}}{Comment
Process menu items
}
{IfOptionC Cats meow. }{Comment
}
{IfOptionD Dogs bark. }
Macro for the Africa submenu - SubmenusMethod3Africa.macro
{Comment
**** Submenu for Africa
}
{MenuOption}{MenuOptionE Elephant}{MenuOptionT Tiger}{ShowMenu}}{Comment
**** Process submenu items
}
{IfOptionE Elephants are big. }{Comment
}
{IfOptionT Tigers roar. }
If you copy or type these macros, make sure that you don't include any blank
spaces or lines in the macro.
Right click to download SubmenusMethod3.macro,.
SubmenusMethod3Pets.macro, and
SubmenusMethod3Pets.macro
Method 3
In this method we will put the submenus in their own file. One macro file serves as
the main menu. While we end up with multiple
files, each of the files is pretty simple. Moreover the submenu macros could also be
used in a stand alone manner or in another menu.
The main menu macro takes advantage of the read command to read in the submenu
macros. Lets look at those commands.
-
{IfOptionA {Read SubmenusMethod3Pets.macro}}
{IfOptionB {Read SubmenusMethod3Africa.macro}}
We see that if option A is selected from the main menu, SubmenusMethod3Pets.macro
is read. It has the Cat and Dog options. If option B is selected, SubmenusMethod3Africa
is read. It presents the Elephant and Tiger options.
The other commands are just about the same as in the previous macro.
One of the ways that this method could be used is to combine several macros into "one" so
that they could all be accessed from the Macro Control or a single tool button.
Return to the beginning of this file
Return to the tutorial home page
Revised: 10/5/2011