/*************************************************************************
*                                                                        *
*   IFFCONVERT.C                       Modified: 16.4.93                 *
*                                                                        *
*   cc iffconvert.c                                                      *
*                                                                        *
*   ln iffconvert.o getfile.o iffpack.o                                  *
*                                                                        *
*   V1.0                                                                 *
*                                                                        *
*   V1.1   reqtools Filerequester                                        *
*          Topaz 8 fest eingebaut, sonst Probleme mit anderen Fonts      *
*                                                                        *
*   V1.11  Bug mit übergroßen Windows entfernt, nur noch Englische Vers. *
*                                                                        *
*   V1.12  BODY-Chunk jetzt immer auf Wortgröße, 256 Farben              *
*                                                                        *
**************************************************************************/

#include <intuition/intuition.h>
#include <intuition/intuitionbase.h>
#include <exec/memory.h>
#include <proto/exec.h>
#include <proto/intuition.h>
#include <proto/graphics.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define LOAD   0
#define SAVE   1
#define DELETE 2

#include "iffmenu.h"

#define ERRORSTRINGS

#include "iffpack.h"
#include "iffconvert.h"


#define Trace(s) /* {puts(s);getchar();} */

#define SAS_C

struct NewScreen ns;
struct NewWindow nw;

struct Library *ReqToolsBase;

struct IntuitionBase *IntuitionBase=NULL;
struct GfxBase *GfxBase=NULL;
ULONG File;

struct Screen *s=NULL;
struct Window *w=NULL;

struct TextAttr txtattr= {
   (STRPTR)"topaz.font",TOPAZ_EIGHTY,FS_NORMAL,FPF_ROMFONT};

FILE *fp=NULL;

char *FileRequest();

long selected=0;

void _abort(void);

main()
{
   int err;
   int loaded=0;
   char *name;
   struct IntuiMessage *msg;
   USHORT num;
   ULONG class;
   USHORT code;


   short ende=0;

   new_window.FirstGadget=NULL;

   if(!(IntuitionBase = (struct IntuitionBase *)
         OpenLibrary("intuition.library",0L))) quit("No intuition-library");

   if(!(GfxBase = (struct GfxBase *)
         OpenLibrary("graphics.library",0L))) quit("No graphics-library");

   if(!(ReqToolsBase=(struct Library *)OpenLibrary("reqtools.library",37L)))
      quit("No reqtools-library V37");

   if(InitRequest())
      quit("No Memory");

   OpenDefaultScreen();
   PrintIText(w->RPort,FIRSTTEXT,0L,0L);

   while(!ende) {
      Wait(1L<<w->UserPort->mp_SigBit);
      while(msg=(struct IntuiMessage *)GetMsg(w->UserPort)) {
         class=msg->Class;
         code=msg->Code;
         ReplyMsg((struct Message *)msg);
         if(class==MENUPICK) {
            num=code;
            while(num!=MENUNULL) {
               if(MENUNUM(num)==0){
                  switch(ITEMNUM(num)) {
                     case 0:  if(loaded) {
                                 SetRGB4(&s->ViewPort,0,0,0,0);
                                 SetRGB4(&s->ViewPort,1,15,15,15);
                              }
                              if(name=FileRequest(w,LOAD)) {
                                 if(fp=fopen(name,"r")) {
                                    Trace("LoadScreen");
                                    LoadScreen(fp);
                                    Trace("fclose");
                                    fclose(fp);
                                    fp=NULL;
                                    loaded=1;
                                 }  else DisplayBeep(s);
                              } else if(loaded) SetColors(&s->ViewPort);
                              break;
                     case 1:  if(loaded) {
                                 SetRGB4(&s->ViewPort,0,0,0,0);
                                 SetRGB4(&s->ViewPort,1,15,15,15);
                              }
                              if(name=FileRequest(w,SAVE)) {
                                 if(fp=fopen(name,"w")) {
                                    SetWait(w);
                                    switch(SUBNUM(num)) {
                                       case 0:  err=WriteWindow(fp,w,0,loaded);
                                                if(err) DisplayError(errortext[err]);
                                                break;
                                       case 1:  err=WriteWindow(fp,w,1,loaded);
                                                if(err) DisplayError(errortext[err]);
                                                break;
                                       case 2:  err=WriteWindow(fp,w,2,loaded);
                                                if(err) DisplayError(errortext[err]);
                                                break;
                                    }
                                    ClearWait(w);
                                    fclose(fp);
                                    fp=NULL;
                                    if(err) unlink(name);
                                 } else DisplayError("Cannot open file");
                              } else if(loaded) SetColors(&s->ViewPort);
                              break;
                     case 2:  if(loaded) {
                                 SetRGB4(&s->ViewPort,0,0,0,0);
                                 SetRGB4(&s->ViewPort,1,15,15,15);
                              }
                              if(name=FileRequest(w,DELETE)) {
                                 unlink(name);
                              } else if(loaded) SetColors(&s->ViewPort);
                              break;
                     case 3:  break;
                     case 4:  selected=ItemAddress(FIRSTMENU,(long)num)
                                          ->Flags&CHECKED;
                              ShowTitle(s,selected);
                              break;
                     case 5:  break;
                     case 6:  ende=1;
                              break;
                  }
               }
               num=ItemAddress(FIRSTMENU,(long)num)->NextSelect;
            }

            Trace("SetColors");
            if(loaded) SetColors(&s->ViewPort);

         } else if(class==MENUVERIFY) {
            if(loaded) {
               SetRGB4(&s->ViewPort,0,0,0,0);
               SetRGB4(&s->ViewPort,1,15,15,15);
            }
         }
         Trace("Reply");
      }
   }
   quit(NULL);
}
void _abort()
{
   quit(NULL);
}
void quit(char *err)
{
   if(err) DisplayError(err);
   IFFCleanup();
   ClearWait(NULL);
   if(fp) fclose(fp);
   if(w) CloseWindow(w);
   if(s) CloseScreen(s);
   FreeRequest();
   if(ReqToolsBase) CloseLibrary((struct Library *)ReqToolsBase);
   if(GfxBase) CloseLibrary((struct Library *)GfxBase);
   if(IntuitionBase) CloseLibrary((struct Library *)IntuitionBase);
   exit(0);
}

void DisplayError(char *err)
{
   if(!IntuitionBase) return; /* Kein Alert möglich */
   if(IntuitionBase->LibNode.lib_Version<36) {
      char buffer[40];
      short len;
      buffer[0]=0;
       strcpy(&buffer[1],"\240\30");
      strcpy(&buffer[3],err);
      len=strlen(err);
      /* 2 Null-Bytes als Abschluß */
      buffer[len+3+1]=0;
      buffer[len+3+2]=0;
      DisplayAlert(RECOVERY_ALERT,buffer,40L);
   } else {
      ULONG Flag=IDCMP_RAWKEY;
      struct EasyStruct ES={
         sizeof(struct EasyStruct),0,"IFFConvert 1.12",0,"OK"};
      ES.es_TextFormat=(UBYTE *)err;

      EasyRequest(w,&ES,&Flag);
   }
}

void OpenDefaultScreen(void)
{
   if(w) {
      ClearMenuStrip(w);
      CloseWindow(w);
   }
   w=NULL;
   if(s) CloseScreen(s);
   s=NULL;
   nw=new_window;
   ns=NewScreen;
   ns.Font=&txtattr;
   if(!(s=OpenScreen(&ns)))
      quit("FATAL ERROR: Cannot open default Screen");
   nw.Screen=s;
   if(!(w=OpenWindow(&nw)))
   quit("FATAL ERROR: Cannot open default Window");
   SetMenuStrip(w,FIRSTMENU);
   ShowTitle(s,(long)selected);
}
void LoadScreen(FILE *fp)
{
   int err;

   ns=NewScreen;
   nw=new_window;

   SetWait(w);
   if(err=ReadPicSize(fp,&nw.Width,&nw.Height,
                         &ns.Width,&ns.Height,&ns.Depth,&ns.ViewModes)) {
      DisplayError(errortext[err]);
      ClearWait(w);
      return;
   }
   ClearWait(w);

   if(ns.Width<320) ns.Width=320;
   if(ns.Height<200) ns.Height=200;
   if(ns.Width<nw.Width) ns.Width=nw.Width;
   if(ns.Height<nw.Height) ns.Height=nw.Height;

   ClearMenuStrip(w);
   CloseWindow(w);
   w=NULL;
   CloseScreen(s);
   s=NULL;
   if(!(s=OpenScreen(&ns))) {
      DisplayError("Cannot open screen");
      OpenDefaultScreen();
      return;
   }
   nw.Screen=s;

   if(!(w=OpenWindow(&nw))) {
      DisplayError("Cannot open window");
      OpenDefaultScreen();
      return;
   }

   SetMenuStrip(w,FIRSTMENU);
   ShowTitle(s,(long)selected);
   SetColors(&s->ViewPort);

   SetWait(w);
   if(err=ReadBody(w->RPort,fp))
        DisplayError(errortext[err]);

   ClearWait(w);
}

#ifdef SAS_C

__chip USHORT Pointer[]= {
   0x0000,0x0000,
   0x0f80,0x0d80,
   0x1040,0x1040,
   0x2220,0x2020,
   0x4210,0x4010,
   0x8208,0x8008,
   0x8208,0x8008,
   0x8388,0x0000,
   0x8008,0x8008,
   0x8008,0x8008,
   0x4010,0x4010,
   0x2020,0x2020,
   0x1040,0x1040,
   0x0f80,0x0D80,
   0x0000,0x0000
   };

void SetWait(struct Window *w)
{
   SetPointer(w,Pointer,13L,16L,-6L,-6L);
}
void ClearWait(struct Window *w)
{
   if(w) ClearPointer(w);
}

#else

USHORT Pointer[]= {
   0x0000,0x0000,
   0x0f80,0x0d80,
   0x1040,0x1040,
   0x2220,0x2020,
   0x4210,0x4010,
   0x8208,0x8008,
   0x8208,0x8008,
   0x8388,0x0000,
   0x8008,0x8008,
   0x8008,0x8008,
   0x4010,0x4010,
   0x2020,0x2020,
   0x1040,0x1040,
   0x0f80,0x0D80,
   0x0000,0x0000
   };

#define POINTERMEM (15L*4L)
static USHORT *chipmem=0;

void SetWait(struct Window *w)
{
   short a;
   ClearWait(w);
   if(chipmem=AllocMem(POINTERMEM,MEMF_CHIP)) {
      for(a=0;a<30;a++) chipmem[a]=Pointer[a];
      SetPointer(w,chipmem,13L,16L,-6L,-6L);
   }
}
void ClearWait(struct Window *w)
{
   if(w) ClearPointer(w);
   if(chipmem) FreeMem(chipmem,POINTERMEM);
   chipmem=0;
}

#endif
