/* ======================================================================== */
/* = Name             : MicroView V11.3					  = */
/* =									  = */
/* ======================================================================== */
/* = Author/Copyright : (c) 1994-95 by Andreas Ralph Kleinert.            = */
/* =		       Freeware. All rights reserved.			  = */
/* =									  = */
/* =                   Use it as an example for programming               = */
/* =                   superview.library !                                = */
/* =									  = */
/* ======================================================================== */
/* = Function	      : Shows Graphics via superview.library.             = */
/* =									  = */
/* ======================================================================== */
/* = Last Update      : 02.04.1995					  = */
/* =									  = */
/* ======================================================================== */
/* = Comments         : Needs "asl.library" V37+                          = */
/* =		        and "superview.library" V11+.                     = */
/* =									  = */
/* =                    RUNS ONLY FROM CLI/SHELL !                        = */
/* =									  = */
/* ======================================================================== */
/* = Compiler	      : SAS/C V6.55  					  = */
/* =		        (smakefile)                                       = */
/* ======================================================================== */

/* to prevent this program from using standard startup-code ("LIB:c.o"),
   #define NO_STARTUP at this point and change smakefile not to link
   with startup-code.

   WARNING :
   Currently the version without startup-code does seem to crash,
   when it is the first program to open superview.library.
   If it has been opened by other programs before, there are no
   problems.
*/

#define __USE_SYSBASE

#include <intuition/intuition.h>

#include <superview/superview.h>
#include <dos/dosextens.h>
#include <dos/rdargs.h>
#include <libraries/asl.h>

#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/asl.h>
#include <proto/superview.h>


/* *************************************************** */
/* *						     * */
/* * Error-Messages for Leave() :                    * */
/* *						     * */
/* *************************************************** */

#define copyright_text "MicroView V11.3 (Freeware)\n(c) 1994-95 by Andreas R. Kleinert.\n"
#define options        "FILE/A"
#define OS37needed     "You need OS V2.04+ !"
#define asllib_text    "You need \42asl.library\42 V37+ !"
#define svlib_text     "You need \42superview.library\42 V11+ !"

char ver_text [] = "\0$VER: MicroView V11.3 (2.4.95)";


/* *************************************************** */
/* *						     * */
/* * MACROs for Version-Tests			     * */
/* *						     * */
/* *************************************************** */

#define LibVer(x) ( ((struct Library *) x)->lib_Version )
#define OS_VER	  LibVer(SysBase)


/* *************************************************** */
/* *						     * */
/* * Function Declarations			     * */
/* *						     * */
/* *************************************************** */

ULONG           main(void);
ULONG MicroView_Show(char *filename);
ULONG Leave(char *endtext, ULONG code);


/* *************************************************** */
/* *						     * */
/* * Additional Base Declarations		     * */
/* *						     * */
/* *************************************************** */

#ifdef NO_STARTUP
struct ExecBase      *SysBase       = N;
struct DosLibrary    *DOSBase       = N;
#else
extern struct ExecBase   *SysBase;
extern struct DosLibrary *DOSBase;
#endif /* NO_STARTUP */

struct Library       *AslBase       = N;
struct SuperViewBase *SuperViewBase = N;


/* *************************************************** */
/* *						     * */
/* * MAIN					     * */
/* *						     * */
/* *************************************************** */

ULONG          __aligned rdopts[8];
struct TagItem __aligned tags[4];

ULONG main(void)
{
 struct RDArgs  *rdargs;
 ULONG           retcode = 0;

#ifdef NO_STARTUP

 SysBase = (*((struct ExecBase **)4));
 DOSBase = (APTR) OpenLibrary("dos.library", 0);

#endif /* NO_STARTUP */

 if(   (OS_VER          < 37) 
    || (LibVer(DOSBase) < 37) ) return(Leave(OS37needed, 100));

 PutStr(copyright_text);

 AslBase = (struct Library *) OpenLibrary("asl.library", 37);
 if(!AslBase) return(Leave(asllib_text, 102));

 SuperViewBase = (struct SuperViewBase *) OpenLibrary("superview.library", 11);
 if(!SuperViewBase) return(Leave(svlib_text, 103));

 rdargs = ReadArgs(options, (LONG *) &rdopts[0], NULL);
 if(!rdargs)
  {
   struct FileRequester *request;
   char                  namebuffer [256];

   namebuffer[0] = (char) 0;

   tags[0].ti_Tag  = (Tag)   ASL_Hail;
   tags[0].ti_Data = (ULONG) "Select Picture File :";

   tags[1].ti_Tag  = (Tag)   ASL_OKText;
   tags[1].ti_Data = (ULONG) "Display";

   tags[2].ti_Tag  = (Tag)   ASL_CancelText;
   tags[2].ti_Data = (ULONG) "Quit";

   tags[3].ti_Tag  = (Tag)   TAG_DONE;
   tags[3].ti_Data = (ULONG) N;

   request = AllocAslRequest(ASL_FileRequest, N);
   if(request)
    {
     if(AslRequest(request, &tags[0]))
      {
       strcpy(namebuffer, request->rf_Dir);
       if(    (namebuffer[strlen(namebuffer)-1] !=      ':')
           && (namebuffer[strlen(namebuffer)-1] !=      '/')
           && (namebuffer[0]                    != (char) 0) ) strcat(namebuffer, "/");

       strcat(namebuffer, request->rf_File);

       if(namebuffer[0] != (char) 0) retcode = MicroView_Show(namebuffer);
      }

     FreeAslRequest(request);
    }
  }else
  {
   retcode = MicroView_Show((UBYTE *) rdopts[0]);

   FreeArgs(rdargs);
  }

 return(Leave(N, retcode));
}

/* *************************************************** */
/* *						     * */
/* * Show-Function				     * */
/* *						     * */
/* *************************************************** */

ULONG MicroView_Show(char *filename)
{
 APTR handle;
 struct Window *win;
 ULONG retval = SVERR_NO_ERROR;

 handle = SVL_AllocHandle(N);
 if(handle)
  {
   if(!(retval = SVL_InitHandleAsDOS(handle, N)))
    {
     if(!(retval = SVL_SetWindowIDCMP(handle, IDCMP_MOUSEBUTTONS, N)))
      {
       if(!(retval = SVL_SetScreenType(handle, CUSTOMSCREEN, N)))
        {
         if(!(retval = SVL_SuperView(handle, filename)))
          {
           if(!(retval = SVL_GetWindowAddress(handle, &win, N)))
            {
             if(win)
              {
               WaitPort(win->UserPort);
              }
            }
          }
        }
      }
    }
   
   SVL_FreeHandle(handle);

  }else retval = SVERR_NO_HANDLE;

 if(retval) return(Leave(SVL_GetErrorString(retval), 0));

 return(0);
}

/* *************************************************** */
/* *						     * */
/* * LEAVE : Global Exit Function Replacement	     * */
/* *						     * */
/* *************************************************** */

ULONG Leave(char *endtext, ULONG code)
{
 if(SuperViewBase) CloseLibrary((APTR) SuperViewBase);
 if(AslBase)       CloseLibrary((APTR) AslBase);
 if(DOSBase)
  {
   if(endtext)
    {
     Write(Output(), endtext, strlen(endtext));
     Write(Output(), "\n", 1);
    }
  }

#ifdef NO_STARTUP

 if(DOSBase) CloseLibrary((APTR) DOSBase);

#endif /* NO_STARTUP */

 return(code);
}
