Skip to content

Creating Dynamic Menu Items

Dynamic menu items can also be created where menu items or even entire menu systems are defined at runtime instead of at design-time. If you do create dynamic menus, you may have two different needs:

  • Create the menus one time and use them for the duration of the program. An example of this might be a menu system where the contents of one or more of your popup menus are defined by information stored in a database.

  • Create new menus each time you invoke a popup menu. An example of this would be a popup menu containing a list of recently used files.

These two needs require different strategies.

Creating Dynamic Menu Items as Part of Initialization

If you wish to create your dynamic menus one time, you will do so by augmenting the cCJMenuItem CreateComInstance message. If you create all of the child menu item objects needed before forwarding CreateComInstance, the child menus will appear as desired. The following example shows a dynamic menu whose items are created as the menu system is being created.

Object oFindMenu is a cCJMenuItem
    Set psCaption to "Find"
    Set peControlType to xtpControlPopup

    Procedure CreateComInstance Handle hoControls
        Handle hoMenu
        // before creating the child COM menu objects we must
        // create the proxy child objects
        Get Create U_cCJFindMenuItem to hoMenu
        Get Create U_cCJFindNextMenuItem to hoMenu
        Get Create U_cCJFindPreviousMenuItem to hoMenu
        Get Create U_cCJFindLastMenuItem to hoMenu
        // this will create the menu system based on what we just created
        Forward Send CreateComInstance hoControls
    End_Procedure
End_Object

Creating Dynamic Menu Items Each Time a Popup Menu is Invoked

If you need to create menus each time you invoke a popup menu, you will do so inside of the OnPopupInit message. The basic steps for this are:

  1. Destroy any existing dynamic child menu items.
  2. Add the new dynamic menu items by creating the new menu item object and adding it to the popup menu by sending the AddDynamicControl message.

A very simple example of this is provided below. This creates extra menu items based on the user’s rights. This is not a particularly realistic example, but it does show the basic steps. See OnPopupInit for a better and more complete example of this.

Procedure OnPopupInit Variant vCommandBarControl Handle hCommandBarControls
    Handle hoMenu
    Boolean bFullRights
    Variant vItem

    // delete the dynamic child items
    Get phoDelete to hoMenu
    If hoMenu Begin
        Send Destroy of hoMenu
    End

    Get phoSave to hoMenu
    If hoMenu Begin
        Send Destroy of hoMenu
    End

    Set phoDelete to 0
    Set phoSave to 0

    // create the two extra menu items as needed
    Get pbFullRights of oRightsManager to bFullRights
    If bFullRights Begin
        Get Create U_cCJSaveMenuItem to hoMenu
        Get AddDynamicControl of hoMenu hCommandBarControls to vItem
        Get Create U_cCJDeleteMenuItem to hoMenu
        Get AddDynamicControl of hoMenu hCommandBarControls to vItem
    End
End_Procedure

Note this sample would have been much more easily implemented by hiding the unavailable items by adding IsVisible to each of the menu item objects as follows:

Object oSaveMenu is a cCJSaveMenuItem
    Function IsVisible Returns Boolean
        Boolean bFullRights
        Get pbFullRights of oRightsManager to bFullRights
        Function_return bFullRights
    End_Function
End_Object

This would have the advantage that you could visually model the items.

See Also