Copying a Menu

Ever needed to create copy of a menu you defined using CREATE MENU?
For example to use it in 2 separate processes with different settings or different states of enabling/disabling or different lines added or removed?
Well, despair not, here is a piece of code that might come in handy:

  // ----------------------------------------------------
  // Method: MENU_Copy
  // ----------------------------------------------------
  // Call:   MenuRef:=MENU_Copy(SourceMenu)
  // ----------------------------------------------------
  // UserName (OS): Alexander Heintz
  // Date and Time: 21.01.15, 19:06:33
  // ----------------------------------------------------
  // Does:
  //      recursively copies a menu based on a menu reference
  //      including all submenus
  // ----------------------------------------------------
  // Parameters:
  // ->  $1     text        the menu to copy
  // <-  $0     text        the reference to the menu copy
  // ----------------------------------------------------
  // Parameter Definition
C_TEXT($1)
C_TEXT($0)
  // ----------------------------------------------------
  // Local Variable Definition
ARRAY TEXT($at_Submenus;0)
ARRAY TEXT($at_Titles;0)
C_LONGINT($l_Line)
C_LONGINT($l_DefaultAction)
C_LONGINT($l_Key)
C_LONGINT($l_Modifier)
C_LONGINT($l_NewProcess)
C_LONGINT($l_Privileges)
C_LONGINT($l_Stye)
C_TEXT($t_Icon)
C_TEXT($t_Mark)
C_TEXT($t_Method)
C_TEXT($t_Param)
C_TEXT($tx_bMenu)
C_TEXT($tx_Menu)
C_TEXT($tx_Source)
  // ----------------------------------------------------
  // Parameter Assignment
$tx_Source:=$1
  // ----------------------------------------------------

$tx_Menu:=Create menu

ARRAY TEXT($at_Titles;0)
ARRAY TEXT($at_Submenus;0)

GET MENU ITEMS($tx_bMenu;$at_Titles;$at_Submenus)

For ($l_Line;1;Size of array($at_Titles))
  $l_Key:=Get menu item key($tx_Source;$l_Line)
  $l_Modifier:=Get menu item modifiers($tx_Source;$l_Line)
  $t_Mark:=Get menu item mark($tx_Source;$l_Line)
  $t_Method:=Get menu item method($tx_Source;$l_Line)
  $t_Param:=Get menu item parameter($tx_Source;$l_Line)
  $l_Stye:=Get menu item style($tx_Source;$l_Line)

  GET MENU ITEM ICON($tx_Source;$l_Line;$t_Icon)
  GET MENU ITEM PROPERTY($tx_Source;$l_Line;Access privileges;$l_Privileges)
  GET MENU ITEM PROPERTY($tx_Source;$l_Line;Associated standard action;$l_DefaultAction)
  GET MENU ITEM PROPERTY($tx_Source;$l_Line;Start a new process;$l_NewProcess)

  If ($at_Submenus{$l_Line}="")
    APPEND MENU ITEM($tx_Menu;$at_Titles{$l_Line})
  Else
    //create a copy of the submenu and do that recursively 
    $tx_SubMenu:=MENU_Copy ($at_Submenus{$l_Line})
    APPEND MENU ITEM($tx_Menu;$at_Titles{$l_Line};$tx_SubMenu)
  End if 
  If ($l_Key#0)
    SET MENU ITEM SHORTCUT($tx_Menu;-1;$l_Key;$l_Modifier)
  End if 
  If ($t_Mark#"")
    SET MENU ITEM MARK($tx_Menu;-1;$t_Mark)
  End if 
  If ($t_Method#"")
    SET MENU ITEM METHOD($tx_Menu;-1;$t_Method)
  End if 
  If ($t_Param#"")
    SET MENU ITEM PARAMETER($tx_Menu;-1;$t_Param)
  End if 
  SET MENU ITEM STYLE($tx_Menu;-1;$l_Stye)

  SET MENU ITEM PROPERTY($tx_Menu;-1;Access privileges;$l_Privileges)
  SET MENU ITEM PROPERTY($tx_Menu;-1;Associated standard action;$l_DefaultAction)
  SET MENU ITEM PROPERTY($tx_Menu;-1;Start a new process;$l_NewProcess)
  If ($t_Icon#"")
    SET MENU ITEM ICON($tx_Menu;-1;$t_Icon)
  End if 
End for 

$0:=$tx_Menu

Oh, and if you like to conserve memory, please clean up if you you dot need the menu anymore by calling

RELEASE MENU(MenuRef)

I will release some code soonish that will take care of cleaning up all the submenus as well...

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.