/* Graphik Export mittels MetaView */

METAVIEW = ':Aminet/MetaView/MetaView'    /* Please complete the path */

OPTIONS RESULTS
SIGNAL ON FAILURE
SIGNAL ON SYNTAX

/*
** Looking for our start process
*/

APLPORT = ADDRESS()
say APLPORT
if (LEFT(APLPORT, 10) ~= "APPLICATION") then do /* not started from Application */
    say "Please start me from Application!"
    EXIT
end

/*
** Create a new Metaview process:
** first look for the allready running processes
** then the new Port will be the next one
*/

do NUMBER = 0 to 20
    if (SHOW(PORTS,"METAVIEW." || NUMBER) = 0) then
    leave
end

/*
** Searching for MetaView: 1. our path above,
**    2. path in env:MetaView.path
** or 3. you must have a assign "MetaView:"
*/

if (EXISTS(METAVIEW)=0) then do
    if OPEN("MVVAR","env:MetaView.path","Read") then do
        METAVIEW = READLN("MVVAR")
    end
    if (EXISTS(METAVIEW)=0) then do
        METAVIEW = "MetaView:MetaView"
    end
end

/*
** Enable warnings for WaitForPort
*/

OPTIONS FAILAT 5
ADDRESS COMMAND
    "run " || METAVIEW || " NODISPLAY"
    MVPORT = "METAVIEW." || NUMBER
    "WaitForPort " || MVPORT

/*
** Ignore the other errors
*/

OPTIONS FAILAT 21

/*
**  Export the temporary file in application
*/

ADDRESS VALUE APLPORT
    SAVE "t:test.dr2d"

/*
** Do all needed thinks with MetaView (LOAD,SAVE,QUIT...)
*/

ADDRESS VALUE MVPORT
    LOAD "t:test.dr2d"
    REQUESTCHOICE TITLE "Request" BODY """Choose the format to save""" BUTTONS "AMF|WMF|DR2D|CGM|GEM|EPS|AI|HPGL|ILBM"
    CHOICE = rc
    REQUESTFILE "RAM:" TITLE """Please choose filename"""
    FILENAME = result
    if (CHOICE = 1) then do
      SAVE FILENAME AS AMF
    end
    if (CHOICE = 2) then do
      SAVE FILENAME AS WMF
    end
    if (CHOICE = 3) then do
      SAVE FILENAME AS DR2D
    end
    if (CHOICE = 4) then do
      SAVE FILENAME AS CGM
    end
    if (CHOICE = 5) then do
      SAVE FILENAME AS GEM
    end
    if (CHOICE = 6) then do
      SAVE FILENAME AS EPS
    end
    if (CHOICE = 7) then do
      SAVE FILENAME AS AI
    end
    if (CHOICE = 8) then do
      SAVE FILENAME AS HPGL
    end
    if (CHOICE = 0) then do
      SAVE FILENAME AS ILBM
    end
    QUIT

EXIT

FAILURE:
    ADDRESS COMMAND
    REQUESTCHOICE "Error" """Can't find" METAVIEW "!""" "OK"
    EXIT

SYNTAX:
    say "Error on line" SIGL ":" ERRORTEXT(RC) "!"
    EXIT
