Text menu: populating with content
This lesson demonstrates how to populate a text menu with content by script.
The populateTextMenu command
The populateTextMenu command sets up three cascading menus, text fonts, text sizes and text colors. The command takes 2 parameters
pSizes: a list of the text sizes to show
pColors: a list of the text colors to show
This command takes two parameters: the list of font sizes and the list of text colors. You can use this command with both parameters, with one, or with neither parameter.
It assumes that there is already a button called "Text". Usually this button will be part of a group of buttons that makes up a menu bar. (Remember that in Revolution, menus are implemented as buttons, and a menu bar is a group of menu buttons.)
This custom command is used with a statement such as the following:
populateTextMenu empty,field "Color Names"
The "Text" button
The list of fonts, sizes, colors, and styles is placed in the button. Each line of the button contents becomes a separate menu item when the menu is displayed. Because each of the the fonts, sizes, and colors is preceded with a tab character, they become submenu items. The menu's items are:
Checking the parameters
If either parameter is empty the handler assigns default values. In other words, if you don't specify pSizes when you use the handler, it assumes you want the Size submenu to include 9, 10, 12, 14, 18, 20, 24, and 36 point sizes. If you don't specify pColors, it assumes you want a standard list of colors: black, yellow, red, blue, and green.
if pSizes is empty then put "9,10,12,14,18,24,36" into pSizes end if
if pColors is empty then put "Black,Yellow,Red,Blue,Green" into pColors end if
You can use this method with any handler to assign a default value to one or more of the parameters. Just check whether the parameter is empty. If it is, then no value has been passed, and you can simply put the desired default into the parameter.
Setting up the menus
After checking the parameters, the handler sets up the three cascading menus. First, it uses the fontNames function to obtain a list of installed fonts. Since the list is not necessarily returned in alphabetical order, the handler sorts the font names. Then it puts the line "Font" into the Text menu button: this is the first menu item in the Text menu, the title of the cascading menu. Next, the handler adds a tab character to the start of each line in the font list, and places the list after the "Font" line in the Text menu button.
Note: If a line in a menu starts with a tab, it is made into a cascading menu item. Therefore, adding the tabs makes the list of fonts into a Font submenu.
The handler follows a similar procedure to set up the Size and Color cascading menus, using the lists of sizes and colors that were passed as the pSizes and pColors parameters.
Finally, the handler places the separator line and the style menu items at the end of the menu. The style items (Plain, Bold, Italic, and Underline) are plain menu items, not cascading menus, so no tab characters are needed here.
The populateTextMenu command code
on populateTextMenu pSizes, pColors local tFont, tSize, tColor ## set up defaults for the size and color lists if pSizes is empty then put "9,10,12,14,18,24,36" into pSizes end if if pColors is empty then put "Black,Yellow,Red,Blue,Green" into pColors end if ## set up the Font submenu: get the fontNames sort it put "Font" into button "Text" repeat for each line tFont in it put return & tab & tFont after button "Text" end repeat ## set up the Size submenu: put return & "Size" after button "Text" repeat for each item tSize in pSizes put return & tab & tSize after button "Text" end repeat ## set up the Color submenu: put return & "Color" after button "Text" repeat for each item tColor in pColors put return & tab & tColor after button "Text" end repeat ## set up style choices: put return & "(-" & return \ & "Plain" & return \ & "Bold" & return \ & "Italic" & return \ & "Underline" after button "Text" end populateTextMenu