14. Creating a Frame at Runtime : How You Can Build a Frame Dynamically : How You Can Create the FrameSource Object : How You Can Create the Frame Menu Items
 
Share this page                  
How You Can Create the Frame Menu Items
The generated frame created by the DynamicFrame frame contains a menu bar that has two selections, File and Edit. If the user selects File, two new choices appear, Commit and Close, and Rollback and Close. If the user selects Edit, three new choices appear: Delete and Next, Update and Next, and Next.
The bar across the top of the generated frame's window, which displays the initial menu choices, is an instance of the MenuBar class. Each initial menu option and the submenu options accessed through the initial option is an instance of the MenuGroup class. In the generated frame, each of the submenu choices is an instance of the MenuButton class.
In the FrameSource object, which contains the frame's definition, the menu bar is contained in the StartMenu attribute. Therefore, to construct a menu for a frame, first create the MenuBar object and assign it to the StartMenu attribute. Then name the menu. The following code creates the named menu bar and attaches it to the generated frame:
test_frame.StartMenu = MenuBar.Create();
test_frame.StartMenu.Name = 'menu';
How You Can Construct a File Menu Group
The following discussion steps through the construction of the File menu group.
1. Create the MenuGroup object:
top_menu = MenuGroup.Create(); /* File Menu */
2. Define the TextLabel and internal name for the File menu group:
top_menu.TextLabel = 'File';
top_menu.Name = 'file_menu';
The text assigned to the TextLabel attribute is displayed by the frame in its menu bar. The preceding code, therefore, causes the word "File" to appear as one of the selections available in the frame's initial menu. The value assigned to the Name attribute is the name of the reference variable pointing to that MenuGroup object.
3. Define the two submenu options that appear when the user selects the File option from the menu bar: “Commit and Close” and “Rollback and Close.” Defining these operations follows the same procedures as creating the MenuGroup:
Create the object for each item (in this case a MenuButton object).
Define the TextLabel and Name for each menu item.
The following code creates the first submenu item:
test_menu = MenuButton.Create();
test_menu.Name = 'commit_menu';
test_menu.TextLabel = 'Commit and Close';
4. In addition to letting the user commit the transaction and close the frame by selecting the Commit menu button, the dynamic frame provides a second way to commit and close. The example frame provides alternative access to the Commit operation by defining a speed key, which lets the user select the Close operation from the keyboard rather than using the mouse.
The following code defines the speed key:
test_menu.SpeedKey = SK_CLOSE;
5. After each submenu option is defined, the following code attaches it to the field MenuGroup object:
test_menu.ParentMenuGroup = top_menu;
The preceding statement makes the MenuGroup object represented by top_menu to be the parent of the MenuButton object represented by test_menu.
6. After defining all of the individual items for the MenuGroup, the following code attaches the MenuGroup itself to the MenuBar object:
top_menu.ParenTMenuGroup = test_frame.StartMenu;
The following is the complete code that constructs both menu groups that belong to the menu bar:
top_menu = MenuGroup.Create();   /* File Menu */
top_menu.TextLabel = 'File';
top_menu.Name = 'file_menu';

test_menu = MenuButton.Create();   
    /* Commit and close menu item */
test_menu.Name = 'commit_menu';
test_menu.TextLabel = 'Commit and Close';
test_menu.SpeedKey = SK_CLOSE;
test_menu.ParentMenuGroup = top_menu; 
    /* Attach to File menu */
test_menu = MenuButton.Create();  
    /* Rollback and close menu item */
test_menu.Name = 'rollback_menu';
test_menu.TextLabel = 'Rollback and Close';
test_menu.ParentMenuGroup = top_menu;  
    /* Attach to File menu */
top_menu.ParentMenuGroup = test_frame.StartMenu;
/* Now, create the Edit menu */
top_menu = MenuGroup.Create();   /* Edit Menu */
top_menu.TextLabel = 'Edit';
top_menu.Name = 'edit_menu';
test_menu = MenuButton.Create();      
    /* Delete menu item */
test_menu.Name = 'delete_menu';
test_menu.TextLabel = 'Delete and Next';
test_menu.SpeedKey = SK_DELETE;
test_menu.ParentMenuGroup = top_menu; 
    /* Attach to Edit menu */
test_menu = MenuButton.Create();     
    /* Update menu item */
test_menu.Name = 'update_menu';
test_menu.TextLabel = 'Update and Next';
test_menu.ParentMenuGroup = top_menu;   
    /* Attach to Edit menu */
test_menu = MenuButton.Create(); 
    /* Next menu item */
test_menu.Name = 'next_menu';
test_menu.SpeedKey = SK_NEXT;
test_menu.TextLabel = 'Next';
test_menu.ParentMenuGroup = top_menu;     
    /* Attach to Edit menu */

/* Attach to Frame */
top_menu.ParentMenuGroup = test_frame.StartMenu;
For more information about the various types of menu objects, see the Language Reference Guide.