#include "Globals.h"

#define FP 1  /*Pen for filenames in YAFR (white)*/
#define DP 3  /* "   "  dirnames   "   "  (orange)*/

extern ColorReq();

/*I think most of these are self explanatory (HandleNew() handles the New*/
/*item in the Project menu, etc.)*/
void HandleNew()
{
   if(Modified && !AreYouSure(Window))
      return;

   NewAll();
}

void MakeFilename(File,Dir,Complete)
char *File,*Dir,*Complete;
{
   strcpy(Complete,Dir);
   if(Complete[strlen(Complete)-1]!=':' && Complete[0] != NULL)
      strcat(Complete,"/");
   strcat(Complete,File);
}

HandleOpen()
{
   UBYTE *stat;

    MainReq->rf_Hail=OpenHail;
   stat=RequestFile(MainReq);

   if(*stat!=FALSE)
   {
      MakeFilename(SFileName,SDirName,FileName);
      if(Modified)
	 if(!AreYouSure(Window))
	    return(FALSE);

      if(ReadItemList(FileName,FALSE))
      {
	 Modified=FALSE;
	 TitleErrorCancel(); /*Write current filename in title bar*/
      } 		  /*if there was no error*/
   }
}

void Save(arexx)
BYTE arexx;
{
   if(FileName[0]==NULL)
      HandleSaveAs();
   else
      {
      WriteItemList(FileName,arexx);
      Modified=FALSE;
      TitleErrorCancel();
      }
}

void HandleSaveAs()  /*Save As...  never called from ARexx*/
{
   UBYTE *stat;

   MainReq->rf_Hail=SaveHail;

   stat=RequestFile(MainReq);

   if(*stat != FALSE)
   {
      MakeFilename(SFileName,SDirName,FileName);
      if(WriteItemList(FileName,FALSE))
      {
	 Modified=FALSE;
	 TitleErrorCancel();     /*Update filename in title bar*/
      } 			 /*if there was no error*/
   }
}

HandlePrintDisk(arexxstat,filename)
BYTE arexxstat;   /*Called from arexx or not?*/
char *filename;   /*Filename if called from ARexx*/
{
   UBYTE *stat;

   BPTR fp;
   char chip buffer[90];
   struct LineItem *Item;

   PTDReq->rf_Hail=PrintHail;

   if(arexxstat)
      strcpy(PDName,filename);
   else
   {
      stat=RequestFile(PTDReq);
      if(*stat==FALSE)
	 return(FALSE);
      MakeFilename(PFileName,PDirName,PDName);
   }

   if((fp=(BPTR)Open(PDName,MODE_NEWFILE))==NULL)
   {
      if(!arexxstat)
	 TitleError("Can't open file!");
      return(FALSE);
   }

   Item=(struct LineItem *)FirstItem;
   while(Item != NULL)
   {
      MakeTextLine(buffer,Item);
      Write(fp,buffer,strlen(buffer));
      Item=(struct LineItem *)Item->NextItem;
   }

   Close(fp);
   return(TRUE);
}

HandlePrintPrinter() /*Send an outline to the printer*/
{
   BPTR fp;
   char chip RetBuf[8];
   char Printed=0;
   char chip buffer[90];
   struct LineItem *Item;
   struct Preferences preferences;
   UWORD maxlines;

   if((fp=(BPTR)Open("prt:",MODE_OLDFILE))==NULL)
      {
      TitleError("Can't open printer!");
      return(FALSE);
      }

   GetPrefs(&preferences,sizeof(struct Preferences));
   maxlines=preferences.PaperLength-1; /*Get maximum page size*/

   RetBuf[0]=0x0d;
   RetBuf[1]=0x0a;

   if(prefs.DS)
      {
      RetBuf[2]=0x0d;
      RetBuf[3]=0x0a;
      RetBuf[4]=0;
      }
   else
      RetBuf[2]=0;

   Item=(struct LineItem *)FirstItem;
   while(Item != NULL)
      {
      for(Printed=2;Printed < maxlines && Item != NULL;
	    Printed+=(prefs.DS) ? 2 : 1)
	 {
	 strcpy(buffer,"");
	 GetOutlineChars(Item,buffer);
	 strcat(buffer,"  ");
	 strcat(buffer,Item->Text);
	 Write(fp,&buffer[Indent],strlen(&buffer[Indent]));
	 strcpy(buffer,RetBuf);
	 Write(fp,buffer,strlen(buffer));
	 Item=(struct LineItem *)Item->NextItem;
	 }
      buffer[0]=NEWPAGE;
      Write(fp,buffer,1);
      }
   Close(fp);
   return(TRUE);
}

void HandleQuit()
{
   if(Modified) /*Ask "Are you sure?" only if outline isn't saved*/
   {
      if(AreYouSure(Window))
	  CloseStuff();
   }
   else
      CloseStuff();
}

HandleCut()
{
   if(NOINV==InvsMode)
      return(FALSE);
   if(HandleCopy())   /*Copy to buffer.  If it was successful,*/
      HandleErase();    /*erase the block copied from*/
   else
      return(FALSE);
   CheckModified();
}

HandleCopy()
{
   struct WhichOne which;

   if(ClipMode < NOINV)
      FreeListMem(ClipStart,ClipEnd);
   if(InvsMode > NOINV)
      if(StartChar < EndChar)
	 WriteInvsTextToClip(StartChar,EndChar-StartChar+1,CurrentItem->Text);
      else
	 WriteInvsTextToClip(EndChar,StartChar-EndChar+1,CurrentItem->Text);
   else
   {	       /*Find the true start/end*/
      FindStartEnd(StartIItem,EndIItem,&which);

	 /*If cutting out a bunch of continuations, get the parent and all*/
	 /*the child continuations as well*/
      which.Start=(struct LineItem *)FindPrevNonCont(which.Start);
      if((which.End = (struct LineItem *)FindNextNonCont(which.End))==NULL)
	 which.End=(struct LineItem *)LastItem;

      if(which.End->NextItem != NULL && !which.End->cont &&
	    which.End->NextItem->cont)
	 return(FALSE);
      if(!SnagBlock(which.Start,which.End))
	 return(FALSE);
   }
   ClipMode=InvsMode;
   return(TRUE);
}

void HandlePaste()
{
   int TempX,TempY;
   char text[160];

   CancelInvs();
   CheckModified();
   if(ClipMode < NOINV)
   {
      if((!CurrentItem->NextItem->cont) || (CurrentItem->NextItem == NULL))
	 AddBlock(CurrentItem);
   }
   else
   {
      strcpy(text,CurrentItem->Text);
      ReadItemString(PosInText(CurrentItem->Level),
	 79-PosInText(CurrentItem->Level),text);

      TempX=CurX;
      PlotCursor(1,(TempY=CurY));

	 /*Break line apart if paste overruns the line*/
      if(BreakLineApart(CurrentItem,CurrentItem->NextItem,
	    text)==CurrentItem)
	 PrintItem(CurrentItem);
      else
	 PrintItemList(CurrentItem,CurY);
      PlotCursor(TempX > MaxX(CurrentItem)?MaxX(CurrentItem):TempX,TempY);
   }
}

HandleErase()
{
   BYTE status=TRUE;

   if(InvsMode==NOINV)
      return(FALSE);

   if(InvsMode > NOINV)
      DelTextBlock();
   else
      status=HandleDelBlock();
   if(status)
   {
      InvsMode=NOINV;
      CheckModified();
   }
}

void Refresh() /*Refresh the entire screen*/
{
   UBYTE OldX,OldY;
   OldX=CurX;
   OldY=CurY;

   PrintItemList(FirstScrnItem,1);
   if(OldX < MinX(CurrentItem))
      OldX=MinX(CurrentItem);
   else
      if(OldX > MaxX(CurrentItem))
	 OldX=MaxX(CurrentItem);

   PlotCursor(OldX,OldY);
}

void CheckModified() /*Put an '*' in the title bar if it isn't there*/
{
   if(Modified==FALSE)
      {
      Modified=TRUE;
      TitleErrorCancel();
      }
}

void MakeTextLine(Buffer,Item)  /*Convert a LineItem into a text string*/
char *Buffer;
struct LineItem *Item;
{
   UBYTE buflen;
   strcpy(Buffer,"");
   GetOutlineChars(Item,Buffer); /*Get the number*/
   strcat(Buffer,"  ");
   strcat(Buffer,Item->Text); /*Append the text*/
   buflen=strlen(Buffer);
   Buffer[buflen]=0x0a;
   if(prefs.DS)
   {
      Buffer[buflen+1]=0x0a;
      Buffer[buflen+2]=NULL;
   }
   else
      Buffer[buflen+1]=NULL;
}

void HandlePalette() /*Change the color palette*/
{
   ColorReq(Screen,prefs.ScreenType,VisInfo);
   SaveColors(&(Screen->ViewPort));
}

void HandleReloadPrefs()
{
   UBYTE X,Y;
   X=CurX;
   Y=CurY;

   GetLinerPrefs("liner:liner.prefs");   /*Get the preferences from disk*/
   CloseGraphics();
   MakeMenus();
   InterpretPrefs();          /*Install them*/

   PrintItemList(FirstScrnItem,1); /*Restore the current outline*/
   if(Y > DispRows)  /*if the old laced Y is > the current possible rows*/
      {
      Y=DispRows;    /*Y now = the last screen item*/
      CurrentItem=(struct LineItem *)ScrnBtm;
      }
   if(X > MaxX(CurrentItem))  /*Out of bounds*/
      X=MaxX(CurrentItem);
   else if(X < MinX(CurrentItem))
      X=MinX(CurrentItem);

   PlotCursor(X,Y);     /*Restore cursor position*/
}

/*~~~End of menu.c*/
