Howto insert a menu reference into the MainMenu

From Axaptapedia

Jump to: navigation, search

I don't know how to do it via API (if you call AOTAdd it just inserts given menu itself). The only way I've foun is to export the MainMenu to XPO, change it, and import again.

Here is example code:

static void Edictum_InstallMainMenu(Args _args)
{
    Menu m=new Menu('MainMenu');
    TextBuffer b=new TextBuffer();
    str tempFile=WinApi::getTempPath()+'tmp_main_menu.xpo';
    int toInsert;
 
    /// find the last occurence of the string _s
    int findLast(str _s)
    {
        int ret;
        int pos = 1;
        while(b.find(_s, pos))
        {
            ret = b.matchPos();
            pos = b.matchPos() + b.matchLen();
        }
        return ret;
    }
    /// import file by full path
    void import(str _fileName)
    {
        SysImportElements import=new SysImportElements();
        boolean examinedFile=false;
    ;
        import.newFile(_fileName);
        import.parmAddToProject( false);
        import.parmDeleteSubNodes(true);
        import.parmImportWithIds(false);
        import.parmOverrideLocks( false);
        import.parmImportLabels(false);
        import.parmImportAot(true);
        import.import();
    }
;
    // export main menu to the file in the temporary directory
    m.treeNodeExport(tempFile);
    b.fromFile(tempFile);
    // finding the last occurence of menu end
    toInsert = findLast('\n: *ENDMENU\n');
    if (toInsert)
    {
        // insert reference code
        b.insert(@'
    MENUREFERENCE
      PROPERTIES
        Name                #DOC_Main
      ENDPROPERTIES
    ENDMENUREFERENCE
', toInsert);
        // save the code back to the file
        b.toFile(tempFile);
        // import changed xpo
        import(tempFile);
        info('ok');
    }
    else
        info('bad');
}
Personal tools