/*************************************************************************
*                                                                        *
*   IFFCONVERT.C                       Modified: 26.5.92                 *
*                                                                        *
*   Tool to convert various IFF-ILBM formats using the iffpack routines  *
*                                                                        *
*   cc iffconvert.c                                                      *
*                                                                        *
*   ln iffconvert.o filereq.o iffpack.o reqtoolsglue.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. *
*                                                                        *
**************************************************************************/

#include <intuition/intuition.h>
#include <exec/memory.h>
#include <functions.h>
#include <stdio.h>

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

#include "iffmenu.h"

#include "iffpack.h"

struct NewScreen ns;
struct NewWindow nw;

ULONG 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;

main()
{
   long view;
   short mode=0;
   int err;
   char *name;
   struct IntuiMessage *msg;
   USHORT num;

   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=(ULONG)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)) {
         if(msg->Class==MENUPICK) {
            num=msg->Code;
            while(num!=MENUNULL) {
               if(MENUNUM(num)==0){
                  switch(ITEMNUM(num)) {
                     case 0:  if(name=FileRequest(w,LOAD)) {
                                 if(fp=fopen(name,"r")) {
                                    LoadScreen(fp);
                                    fclose(fp);
                                    fp=NULL;
                                 }  else DisplayBeep(s);
                              }
                              break;
                     case 1:  if(name=FileRequest(w,SAVE)) {
                                 if(fp=fopen(name,"w")) {
                                    SetWait(w);
                                    switch(SUBNUM(num)) {
                                       case 0:  err=WriteWindow(fp,w,0);
                                                if(err) SetAlert(errortext[err]);
                                                break;
                                       case 1:  err=WriteWindow(fp,w,1);
                                                if(err) SetAlert(errortext[err]);
                                                break;
                                       case 2:  err=WriteWindow(fp,w,2);
                                                if(err) SetAlert(errortext[err]);
                                                break;
                                    }
                                    ClearWait(w);
                                    fclose(fp);
                                    fp=NULL;
                                    if(err) unlink(name);
                                 } else SetAlert("Cannot open file");
                              }
                              break;
                     case 2:  if(name=FileRequest(w,DELETE)) {
                                 unlink(name);
                              }
                              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;
            }
         }
      }
   }
   quit(NULL);
}
_abort()
{
   quit(NULL);
}
quit(err)
char *err;
{
   if(err) SetAlert(err);
   IFFCleanup();
   ClearWait(NULL);
   if(fp) fclose(fp);
   if(w) CloseWindow(w);
   if(s) CloseScreen(s);
   FreeRequest();
   if(ReqToolsBase) CloseLibrary(ReqToolsBase);
   if(GfxBase) CloseLibrary(GfxBase);
   if(IntuitionBase) CloseLibrary(IntuitionBase);
   exit(0);
}

SetAlert(err)
char *err;
{
   char buffer[40];
   short len;
   buffer[0]=0;
   if(!IntuitionBase) return(); /* Kein Alert möglich */
   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);
}

OpenDefaultScreen()
{
   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);
}
LoadScreen(fp)
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)) {
      SetAlert(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))) {
      SetAlert("Cannot open screen");
      OpenDefaultScreen();
      return();
   }
   nw.Screen=s;

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

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

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

   ClearWait(w);
   fclose(fp);fp=NULL;
}

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;

SetWait(w)
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);
   }
}
ClearWait(w)
struct Window *w;
{
   if(w) ClearPointer(w);
   if(chipmem) FreeMem(chipmem,POINTERMEM);
   chipmem=0;
}



