/* Custom mainline for macro */

    call RXFuncAdd "VRLoadFuncs", "VROBJ", "VRLoadFuncs"
    call VRLoadFuncs

    _VREVersion = SubWord( VRVersion( "VRObj" ), 1, 1 )
    if( _VREVersion < 2.10 )then do
        call VRMessage "", "This program requires VX-REXX version 2.1 to run.", "Error!"
        return 32000
    end

    signal on SYNTAX name _VRESyntax
    signal _VREMain

_VRESyntax:
    parse source . . _VRESourceSpec
    call VRMessage "", "Syntax error in" _VRESourceSpec "line" SIGL":" ErrorText(rc), "Error!"
    exit 32000

_VREMain:
/*:VRX         Main
*/
/*
    codeString = VREDisp( <parent>, <object>, <dest> )

    Generate a VX-REXX code string.

    <parent>    The parent window for any dialogs.  
                If the parent is non-empty the operating    
                environment is assumed to be VRXEdit.
                Otherwise the environment variable
                VXREXX is expected to point at the 
                VX-REXX directory.
    
    <object>    The object to use in VRGet, VRSet, and
                VRMethod.  If the empty string is passed
                the general code dialog is used rather 
                than the object specific dialog.

    <dest>      The destination for the code string.
                If empty, the code string is simply
                returned.  If "CLIPBOARD", the code
                string is inserted into the OS/2 
                clipboard.  Otherwise it is assumed to
                be the handle of a VX-REXX section
                editor and the code is inserted into
                the editor.

    Returns the code string or the empty string.
*/
Main:

/*  Uncomment to debug
    call VRRedirectSTDIO "on"
    trace ?r
*/
    signal on novalue

    parse arg parent, object, destination

    codeString = ""

/*  Set VRXEdit and VRXPath
*/    
    ok = GetEnvironment( parent )
    if ok = 0 then signal quit

    if object = "" then do
        /*  General case 
        */
        objName = ""
        ret = VREDispG( parent, VRXEdit, VRXPath )
        parse VAR ret type ';' value
        if type = "" then signal quit
        if type = "OBJECTMACRO" then do
            parse VAR value objName ';' value
        end
    end
    else do
        /*  Object specific case
        */
        ret = VREDispO( parent, VRXEdit, VRXPath, object )
        parse VAR ret type ';' value 
        if ret = "" then signal quit
        objName = ""
        if VRXEdit = 1 then do
    	   objName = VRGet( object, "Name" )
        end
    end

/*  Call the string generating macro
*/
    if type \= "MACRO" & type \= "OBJECTMACRO" then do
        signal quit
    end

    parse VAR value macroname ";" parms
    parse var macroname macroname ":" dllname
    if( dllname = "" ) then dllname = "MACROS"
    fname = VRXPath || "SYSTEM\" || macroname || ".VRM"

    if( VRFileExists( fname ) ) then do
        interpret 'codeString = "' || fname || '"( parent, objName, parms )'
    end
    else do
        call RxFuncDrop "VRLoadDLLMacro"
        call RxFuncAdd "VRLoadDLLMacro", dllname, "VRLoadDLLMacro"

        codestring = VRLoadDLLMacro( macroname, parent, objName, parms )
    end

    if codeString = "" then signal quit

    if translate(destination) = "CLIPBOARD" then do
	call VRMethod "Application", "PutClipboard", codeString || "0d0a"x
    end
    else if destination \= "" then do
	call VRSEInsertBlock destination, codeString
	call VRSEActivate destination
    end

quit:

return codeString

/*  VREDispG    Call the external VREDispG
*/
VREDispG: procedure
    parse arg parent, edit, path
    interpret 'string = "' || path || 'VREDISPG.VRM"( parent, edit, path )'
return string

/*  VREDispO    Call the external VREDispO
*/
VREDispO: procedure
    parse arg parent, edit, path, object
    interpret 'string = "' || path || 'VREDISPO.VRM"( parent, edit, path, object )'
return string
/*:VRX         GetEnvironment
*/
GetEnvironment: procedure expose VRXEdit VRXPath

    parse arg parent

    ok = 0

/*  Assume if we have a parent that we are executing
    within VRXEdit.  Otherwise assume we are not 
    (e.g. we are executing from an external editor).
*/
    VRXEdit = (parent \= "")

/*  Set the path to the VXREXX home directory.  
    This is VREPath() within VRXEdit, and the environment
    variable VXREXX otherwise.
*/
    if VRXEdit = 1 then do
        VRXPath = VREPath()
        ok = 1
    end
    else do
        VRXPath = value( "VXREXX",,"OS2ENVIRONMENT" ) || '\'
        file = VRXPath || "VRXEDIT.EXE" 
        file = stream( file, 'c', 'query exists' )
        if file \= "" then do
            ok = 1
        end
        else do            
            call VRMessage parent, "The OS/2 environment variable VXREXX is not set to the VX-REXX directory.", "Error"
        end
    end

return ok

