/* ======================================================================== */
/* = Name            : SimpleView V11.3					  = */
/* =									  = */
/* ======================================================================== */
/* = Author/Copyright : (c) 1993-95 by Andreas Ralph Kleinert.            = */
/* =		       Freeware. All rights reserved.			  = */
/* =									  = */
/* =                   Use it as an example for programming               = */
/* =                   superview.library !                                = */
/* =									  = */
/* ======================================================================== */
/* = Function	      : SVObject operations :                             = */
/* =		        - Write WB-Screen content (IFF-ILBM/-ACBM)        = */
/* =		        - Show graphics		 (all SVObjects)          = */
/* =		        - Convert graphics	 (IFF-ILBM/-ACBM)         = */
/* ======================================================================== */
/* = Last Update      : 02.04.1995					  = */
/* =									  = */
/* ======================================================================== */
/* = Comments         : Needs "asl.library" V37+                          = */
/* =		        and "superview.library" V11+.                     = */
/* =									  = */
/* =			Based on KILBM V1.32.                             = */
/* =									  = */
/* ======================================================================== */
/* = Compiler	      : SAS/C V6.55  					  = */
/* =		        (smakefile)                                       = */
/* ======================================================================== */

#include <exec/types.h>
#include <exec/memory.h>

#include <intuition/intuitionbase.h> /* accessing IntuitionBase->FirstScreen */

#include <superview/superview.h>
#include <superview/superviewbase.h>

#include <libraries/asl.h>
#include <workbench/startup.h>

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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


   /* Help- and Info- Texts */

char entry1_text  [] = "\2331;32;40mSimpleView V11.3 \2330;32;40m\2333;32;40m(FREEWARE)\2330;32;40m\n(c) 1993-95 by Andreas Ralph Kleinert.\nAndreas R. Kleinert, Grube Hohe Grethe 23, D-57074 Siegen, Germany.\n";
char entry2_text  [] = "Uses superview.library to write, show and convert IFF-ILBM/-ACBM.\n";
char entry3_text  [] = "USAGE : \2330;33;40mSimpleView\2330;31;40m -[w | wp | wa | r | s | ci | cip | ca | ?] <PicFileName>\n";
char entry4_text  [] = "   '-w'   -> Write current screen page to IFF-ILBM-file (unpacked).\n";
char entry5_text  [] = "   '-wp'  -> Write current screen page to IFF-ILBM-file (packed).\n";
char entry6_text  [] = "   '-wa'  -> Write current screen page to IFF-ACBM-file.\n";
char entry7_text  [] = "   '-s'   -> Show an IFF-ILBM-/ACBM-file (optional).\n";
char entry8_text  [] = "   '-ci'  -> Convert IFF-ACBM to IFF-ILBM (unpacked).\n";
char entry9_text  [] = "   '-cip' -> Convert IFF-ACBM to IFF-ILBM (packed).\n";
char entry10_text [] = "   '-ca'  -> Convert IFF-ILBM to IFF-ACBM.\n";
char entry11_text [] = "   '?'    -> This text.\n";


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


/* *************************************************** */
/* *						     * */
/* * Error-Messages for Leave() and KF_Message()     * */
/* *						     * */
/* *************************************************** */

char asllib_text 	[] = "You need \42asl.library\42 V37+ !";
char svlib_text 	[] = "You need \42superview.library\42 V11+ !";


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

void __regargs SimpleView_Show(char *filename);
void __regargs SimpleView_Write(char *filename, ULONG type);
void __regargs SimpleView_Convert(char *filename, ULONG type);


void __regargs Leave(char *endtext, long code);


   /* Functions from module "SimpleView_Subs.o" : */

extern void __stdargs K_Printf(char *formatstring, ...);


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

extern struct ExecBase *SysBase;

struct Library       *AslBase       = N;
struct SuperViewBase *SuperViewBase = N;
struct IntuitionBase *IntuitionBase = N; /* Copied from SuperViewBase.  */
                                         /* Do not close. Only used for */ 
                                         /* accessing FrontScreen       */


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

extern struct WBStartup *WBenchMsg;
char		        *wbloadname = N;
long                     wb         = FALSE;

void main(long argc, char **argv)
{
 if(!argc)
  {
   wb = TRUE;

   if(WBenchMsg->sm_NumArgs>1) wbloadname = WBenchMsg->sm_ArgList[1].wa_Name;
  }

 if( argc > 3 || (argv[1][0] =='?')||(argv[2][0] =='?'))
  {
   K_Printf("%s%s%s%s%s%s%s%s%s%s%s", entry1_text, entry2_text, entry3_text, entry4_text, entry5_text, entry6_text, entry7_text, entry8_text, entry9_text, entry10_text, entry11_text);

   Leave(N, 0);
  }

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

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

 IntuitionBase = SuperViewBase->svb_IntuitionBase;

 /* ^ if NULL, sv-lib wouldn't have opened ! */


 /* Show */

 if(argc < 2 && !wbloadname)
  {
   struct FileRequester     *request;
   struct TagItem __aligned  tags[4];
   char                      namebuffer [256];

   namebuffer[0] = (char) 0;

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

   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) SimpleView_Show(namebuffer);
      }

     FreeAslRequest(request);
    }
  }else
  {
   if(wbloadname) SimpleView_Show(wbloadname);
    else
     {
      /* Show */

      if(argv[1][0]!=(char) '-') SimpleView_Show(argv[1]); /* no option given,  */
                                                           /* but PicFileName ! */

      if(!stricmp(argv [1],"-s"))  SimpleView_Show(argv[2]);

      /* Write */

      if(!stricmp(argv [1],"-w"))  SimpleView_Write(argv[2], SV_SUBTYPE_ILBM);
      if(!stricmp(argv [1],"-wp")) SimpleView_Write(argv[2], SV_SUBTYPE_ILBM_01);
      if(!stricmp(argv [1],"-wa")) SimpleView_Write(argv[2], SV_SUBTYPE_ACBM);

      /* Convert (Show & Write) */

      if(!stricmp(argv [1],"-ci"))  SimpleView_Convert(argv[2], SV_SUBTYPE_ILBM);
      if(!stricmp(argv [1],"-cip")) SimpleView_Convert(argv[2], SV_SUBTYPE_ILBM_01);
      if(!stricmp(argv [1],"-ca"))  SimpleView_Convert(argv[2], SV_SUBTYPE_ACBM);
     }
  }

 Leave(N, 0);
}

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

void __regargs SimpleView_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) Leave(SVL_GetErrorString(retval), 0);
}

/* *************************************************** */
/* *						     * */
/* * Write-Function				     * */
/* *						     * */
/* *************************************************** */

void __regargs SimpleView_Write(char *filename, ULONG type)
{
 APTR handle;
 ULONG retval = SVERR_NO_ERROR;

 handle = SVL_AllocHandle(N);
 if(handle)
  {
   if(!(retval = SVL_InitHandleAsDOS(handle, N)))
    {
     if(!(retval = SVL_SetWriteType(handle, type, N)))
      {
       if(!(retval = SVL_SetWriteName(handle, filename, N)))
        {
         if(!(retval = SVL_SetWriteScreen(handle, IntuitionBase->FirstScreen, N)))
          {
           retval = SVL_SuperWrite(handle);
          }
        }
      }
    }
   
   SVL_FreeHandle(handle);

  }else retval = SVERR_NO_HANDLE;

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

/* *************************************************** */
/* *						     * */
/* * Convert-Function				     * */
/* *						     * */
/* *************************************************** */

void __regargs SimpleView_Convert(char *filename, ULONG type)
{
 APTR handleS, handleD;

 struct Screen       *sc        = N;
 struct SV_GfxBuffer *gfxbuffer = N;

 ULONG retval = SVERR_NO_ERROR;
 char newfilename [256];


 handleS = SVL_AllocHandle(N);
 if(handleS)
  {
   if(!(retval = SVL_InitHandleAsDOS(handleS, N)))
    {
     if(!(retval = SVL_ReadToGfxBuffer(handleS, filename))) retval = SVL_GetGfxBuffer(handleS, &gfxbuffer, N);

     if(!gfxbuffer)
      {
       if(!(retval = SVL_SetWindowIDCMP(handleS, IDCMP_MOUSEBUTTONS, N)))
        {
         if(!(retval = SVL_SetScreenType(handleS, CUSTOMSCREEN, N)))
          {
           if(!(retval = SVL_SuperView(handleS, filename))) retval = SVL_GetScreenAddress(handleS, &sc, N);
          }
        }
      }

     if(!retval)
      {
       handleD = SVL_AllocHandle(N);
       if(handleD)
        {
         if(!(retval = SVL_InitHandleAsDOS(handleD, N)))
          {
           strcpy(newfilename, filename);
           strcat(newfilename, ".conv");

           if(!(retval = SVL_SetWriteName(handleD, newfilename, N)))
            {
             if(!(retval = SVL_SetWriteType(handleD, type, N)))
              {
               if(sc)        retval = SVL_SetWriteScreen(handleD, sc,        N);
               if(gfxbuffer) retval = SVL_SetGfxBuffer(  handleD, gfxbuffer, N);

               if(!retval) retval = SVL_SuperWrite(handleD);
              }
            }
          }
 
         SVL_FreeHandle(handleD);
        }
      }
    }
   
   SVL_FreeHandle(handleS);

  }else retval = SVERR_NO_HANDLE;

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


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

void __regargs Leave(char *endtext, long code)
{
 if(SuperViewBase) CloseLibrary((APTR) SuperViewBase);
 if(AslBase)       CloseLibrary((APTR) AslBase);

 if(endtext)       if(!wb) K_Printf("%s\n", endtext);

 exit(code);
}
