#include "struct.c"
#include "OutlineFont.h"
#include "WinPlot.h"

       struct MsgPort *RexxReplyPort = NULL;
static struct RexxMsg *OutRexxMsg = NULL;
static        UBYTE   *CmdArgstring = NULL;

/* Init the ARexx Port */
void _STI_250_OpenRexx(void)
{
 RexxReplyPort = CreateMsgPort();
 OutRexxMsg = CreateRexxMsg(RexxReplyPort,NULL,"REXXPLOT");
}

/* Close the ARexx Port */
void _STD_250_CloseRexx(void)
{
 if (RexxReplyPort) DeleteMsgPort(RexxReplyPort);
 if (OutRexxMsg) DeleteRexxMsg(OutRexxMsg);
}

/* the Rexx command list */
static struct List RexxList =
{
 (struct Node *)&RexxList.lh_Tail, NULL, (struct Node *)&RexxList.lh_Head
};

static void SendFirst(void)
{
 struct MsgPort *TargetPort;
 char           *Command = RexxList.lh_Head->ln_Name;

 if (CmdArgstring=CreateArgstring(Command,strlen(Command)))
  {
   OutRexxMsg->rm_Action=RXCOMM|RXFF_NOIO|RXFF_STRING;
   OutRexxMsg->rm_Args[0]=(STRPTR)CmdArgstring;
    
   Forbid();
   if (TargetPort=FindPort(RXSDIR))
    PutMsg(TargetPort,&OutRexxMsg->rm_Node);
   Permit();
  }
}

void SendRexxMsg(char *Command)
{
 struct Node    *RexxNode;
 char           *QuotedCommand, *s, *t;
 int             Quotes, Length;
 BOOL            Send;
 
 Send = RexxList.lh_Head->ln_Succ == NULL;
 
 Quotes = Length = 0;
 
 /* count ' and determine stringlength */
 for(s = Command; *s; s++)
  {
   Length++;
   if (*s == '\'') Quotes++;
  }
 
 /* ' => '' */
 if (QuotedCommand = malloc(Length+Quotes+3))
  {
   t = QuotedCommand; *t++ = '\'';
   
   for(s = Command; *s; s++)
    {
     *t++ = *s;
     if (*s == '\'') *t++ = *s;
    }
   
   *t++ = '\''; *t = '\0';
   
   if (RexxNode = malloc(sizeof(struct Node)))
    {
     RexxNode->ln_Name = QuotedCommand;
     AddTail(&RexxList, RexxNode);

     if (Send && RexxReplyPort && OutRexxMsg)
      SendFirst();
    }

   free(QuotedCommand);
  }
}

void ReplyRexxMsg(void)
{
 struct Node *First;

 DeleteArgstring(CmdArgstring);
 
 First = RexxList.lh_Head;
 RemHead(&RexxList);

 free(First->ln_Name);
 free(First);
 if (RexxList.lh_Head->ln_Succ != NULL)
  SendFirst();
}

void LoadFunc(void)
{
 struct Window *wnd;
 char           Buffer[255], Buffer2[255];
 
 set(AP_WinPlot, MUIA_Application_Sleep, TRUE);
 get(Windows[ActiveWindow].WI_Plot, MUIA_Window_Window, &wnd);
 if (MUI_AslRequestTags(FileReq,
			ASLFR_DoSaveMode, FALSE,
			ASLFR_Window, wnd,
			ASLFR_TitleText,   "Please select new file to plot",
			ASLFR_DrawersOnly, FALSE,
			TAG_END))
  {
   strcpy(Buffer2, FileReq->fr_Drawer);
   AddPart(Buffer2, FileReq->fr_File, 255);
   sprintf(Buffer, "load \"%s\"", Buffer2);
   SendRexxMsg(Buffer);
  }
 set(AP_WinPlot, MUIA_Application_Sleep, FALSE);
}

void SaveFunc(void)
{
 struct Window *wnd;
 char           Buffer[255], Buffer2[255];

 set(AP_WinPlot, MUIA_Application_Sleep, TRUE);
 get(Windows[ActiveWindow].WI_Plot, MUIA_Window_Window, &wnd);
 if (MUI_AslRequestTags(FileReq,
			ASLFR_DoSaveMode, TRUE,
			ASLFR_Window,      wnd,
			ASLFR_TitleText, "Please select file to save gnuplot state",
			ASLFR_DrawersOnly, FALSE,
			TAG_END))
  {
   strcpy(Buffer2, FileReq->fr_Drawer);
   AddPart(Buffer2, FileReq->fr_File, 255);
   sprintf(Buffer, "save \"%s\"", Buffer2);
   SendRexxMsg(Buffer);
  }
 set(AP_WinPlot, MUIA_Application_Sleep, FALSE);
}

void CDFunc(void)
{
 struct Window *wnd;
 char           Buffer[255];

 set(AP_WinPlot, MUIA_Application_Sleep, TRUE);
 get(Windows[ActiveWindow].WI_Plot, MUIA_Window_Window, &wnd);
 if (MUI_AslRequestTags(FileReq,
			ASLFR_DoSaveMode, FALSE,
			ASLFR_Window,      wnd,
			ASLFR_TitleText,   "Please select new current directory",
			ASLFR_DrawersOnly, TRUE,
			TAG_END))
  {
   sprintf(Buffer, "cd \"%s\"", FileReq->fr_Drawer);
   SendRexxMsg(Buffer);
  }
 set(AP_WinPlot, MUIA_Application_Sleep, FALSE);
}

void LoadConf()
{
 FILE *f;
 BOOL FontFound = FALSE;
 char FontName[80];
 int  i;

 if (f = fopen(CONFIG, "r"))
  {
   LONG Page;

   fscanf(f, "%s\n", FontName);
   fread(ColorEntries, sizeof(struct MUI_Palette_Entry), NUMCOLORS+1, f);
   fread(&Page, sizeof(Page), 1, f);
   set(RG_Set, MUIA_Group_ActivePage, Page);

   fclose(f);

   for(i=0;;i++)
    {
     char *FontName2;

     DoMethod(LV_Font, MUIM_List_GetEntry, i, &FontName2);
     if (!FontName2) break;
     if (FontFound = strcmp(FontName, FontName2) == 0) break;
    }
  }
 if (FontFound)
 {
  OpenOutlineFont(FontName);
  set(LV_Font, MUIA_List_Active, i);
 }
 else
  {
   char *FontName;

   DoMethod(LV_Font, MUIM_List_GetEntry, 0, &FontName);
   OpenOutlineFont(FontName);
  }
}

void SaveConf()
{
 FILE *f;
 LONG Page;
 char *FontName;

 if (f = fopen(CONFIG, "w"))
  {
   DoMethod(LV_Font, MUIM_List_GetEntry, MUIV_List_GetEntry_Active, &FontName); 
   fprintf(f, "%s\n", FontName);
   fwrite(ColorEntries, sizeof(struct MUI_Palette_Entry), NUMCOLORS+1, f);
   get(RG_Set, MUIA_Group_ActivePage, &Page);
   fwrite(&Page, sizeof(Page), 1, f);

   fclose(f);
  }
}
