Example 4: Collection of Menu Items

This example shows how to create a collection of menu items which correspond the the selected entries in a list view.

When an item is selected in the menu, it is deselected in the list view.

Define the Collection

In this example we are using a collection style collection to add menu items to the menu item collection. This is how the collection of the menu items is defined:

Define_Com Class(#PRIM_KCOL<#PRIM_MITM #EMPNO>) Name(#MENU_ITEMS) Collects(#PRIM_MITM) Keyedby(#EMPNO) Style(Collection)

Submenu

The submenu which will contain the menu items is defined, but it is not associated with the menu title yet because initially it contains no items and it is defined with a dynamic reference which means that the submenu will be created only when a reference is explicitly set to it:

DEFINE_COM class(#PRIM_SMNU) name(#Sub_menu) reference(*dynamic)

Add Items to the Collection

A menu item is added to the MENU_ITEMS collection when an employee in the list is selected:

 

Evtroutine Handling(#LTVW_1.ItemGotSelection) Options(*NOCLEARMESSAGES *NOCLEARERRORS)

* Create submenu item if it doesn't already exist

If_Ref Com(#Sub_menu) Is(*null)

Set_Ref Com(#Sub_menu) To(*create_as #prim_smnu)

Set Com(#Sub_menu) Parent(#menu_title)

Endif

* Create menu item based on the employee details

Set_Ref Com(#menu_items<#empno>) To(*create_as #prim_mitm)

* Define menu item's text

Use Builtin(BCONCAT) With_Args(#EMPNO #GIVENAME #SURNAME) To_Get(#STD_TEXTL)

* Set menu item's properties

Set Com(#menu_items<#empno>) Caption(#std_textl) Parent(#Sub_menu)

Endroutine

 

Destroy an Item in the Collection

When a list entry loses selection,  the menu item is destroyed from memory by setting its reference to *NULL:

 

EVTROUTINE handling(#LTVW_1.ItemLostSelection) options(*NOCLEARMESSAGES *NOCLEARERRORS)

SET_REF com(#menu_items<#empno>) to(*null)

ENDROUTINE

Source Code for Collection Example 4

To see how the Collection Style collection works, copy and paste this code to a form and then compile and execute the form.

 

Function Options(*DIRECT)

Begin_Com Role(*EXTENDS #PRIM_FORM) Clientheight(310) Clientwidth(393) Height(356) Layoutmanager(#ATLM_1) Left(308) Menubar(#MBAR_1) Top(120) Width(401)

Define_Com Class(#PRIM_ATLM) Name(#ATLM_1)

Define_Com Class(#PRIM_ATLI) Name(#ATLI_1) Attachment(Center) Manage(#LTVW_1) Parent(#ATLM_1)

Define_Com Class(#PRIM_MBAR) Name(#MBAR_1) Parent(#COM_OWNER)

Define_Com Class(#PRIM_MITM) Name(#MENU_TITLE) Caption('Selected Employees') Displayposition(1) Parent(#MBAR_1)

Define_Com Class(#PRIM_LTVW) Name(#LTVW_1) Displayposition(1) Fullrowselect(True) Height(310) Left(0) Parent(#COM_OWNER) Tabposition(1) Top(0) Width(393)

Define_Com Class(#PRIM_LVCL) Name(#LVCL_1) Displayposition(1) Parent(#LTVW_1) Source(#EMPNO) Width(26)

Define_Com Class(#PRIM_LVCL) Name(#LVCL_2) Displayposition(2) Parent(#LTVW_1) Source(#SURNAME) Width(38)

Define_Com Class(#PRIM_LVCL) Name(#LVCL_3) Displayposition(3) Parent(#LTVW_1) Source(#GIVENAME) Width(20) Widthtype(Remainder)

* Collection of selected menu items

* STYLE(Collection) requires that the items in the collection are explicitly created at run time by the application

Define_Com Class(#PRIM_KCOL<#PRIM_MITM #EMPNO>) Name(#MENU_ITEMS) Collects(#PRIM_MITM) Keyedby(#EMPNO) Style(Collection)

* Submenu component required for menu items

Define_Com Class(#PRIM_SMNU) Name(#Sub_menu) Reference(*dynamic)

* Populate list view with known employees

Evtroutine Handling(#COM_OWNER.CreateInstance) Options(*NOCLEARMESSAGES *NOCLEARERRORS)

Select Fields(#LTVW_1) From_File(PSLMST)

Add_Entry To_List(#LTVW_1)

Endselect

Endroutine

* Create a menu item of selected employees

Evtroutine Handling(#LTVW_1.ItemGotSelection) Options(*NOCLEARMESSAGES *NOCLEARERRORS)

* Create submenu item if it doesn't already exist

If_Ref Com(#Sub_menu) Is(*null)

Set_Ref Com(#Sub_menu) To(*create_as #prim_smnu)

Set Com(#Sub_menu) Parent(#menu_title)

Endif

* Create menu item based on the employee details

Set_Ref Com(#menu_items<#empno>) To(*create_as #prim_mitm)

* Define menu item's text

Use Builtin(BCONCAT) With_Args(#EMPNO #GIVENAME #SURNAME) To_Get(#STD_TEXTL)

* Set menu item's properties

Set Com(#menu_items<#empno>) Caption(#std_textl) Parent(#Sub_menu)

Endroutine

* When a list entry loses selection, destroy the menu item

Evtroutine Handling(#LTVW_1.ItemLostSelection) Options(*NOCLEARMESSAGES *NOCLEARERRORS)

Set_Ref Com(#menu_items<#empno>) To(*null)

Endroutine

* When an employee is selected in the menu, deselect it in the list

Evtroutine Handling(#menu_items<>.Click) Com_Sender(#Selected_menu_items)

Define Field(#W_EMPNO) Reffld(#EMPNO)

* Use menu item caption to find out which employee number this item represents

Change Field(#STD_TEXTL) To('#Selected_menu_items.Caption')

Substring Field(#STD_TEXTL 1 5) Into_Field(#W_EMPNO)

Selectlist Named(#LTVW_1)

Continue If('#w_empno *ne #empno')

* Set this employee to be unselected

Set Com(#ltvw_1.currentitem) Selected(false)

* Destroy the menu item

Set_Ref Com(#menu_items<#w_empno>) To(*NULL)

Leave

Endselect

Endroutine

End_Com