/* $Revision Header *** Header built automatically - do not edit! ***********
 *
 *	(C) Copyright 1991 by Torsten Jürgeleit
 *
 *	Name .....: save.c
 *	Created ..: Sunday 22-Dec-91 21:23:04
 *	Revision .: 1
 *
 *	Date        Author                 Comment
 *	=========   ====================   ====================
 *	31-Dec-91   Torsten Jürgeleit      new font management
 *	22-Dec-91   Torsten Jürgeleit      Created this file!
 *
 ****************************************************************************
 *
 *	Save ISUP files
 *
 * $Revision Header ********************************************************/

	/* Includes */

#include "includes.h"
#include "defines.h"
#include "imports.h"
#include "protos.h"

	/* Save project */

   SHORT
save_project(VOID)
{
   struct FileRequester  *freq = project_file_requester;
   SHORT status = EDITOR_STATUS_NORMAL;

   /* Display ARP file requester and check if user selected cancel */
   IChangeMousePointer(ewin, NULL);
   freq->fr_FuncFlags |= FRF_DoColor;
   freq->fr_Hail       = PROJECT_SAVE_HAIL_TEXT;
   if (FileRequest(freq)) {
      struct TemplateList  *tl = &template_list;
      BPTR  fh;
      BYTE  path[LONG_DSIZE + LONG_FSIZE + 1];
      SHORT len = strlen(freq->fr_File) - 4;

      /* Prepare file name and project name */
      if (len < 1 || Strcmp(freq->fr_File + len, ".tpl")) {
	 strcat(freq->fr_File, ".tpl");
	 len += 4;
      }
      change_project_name(tl, freq->fr_File, len);

      /* Build full path for project file */
      strcpy(&path[0], freq->fr_Dir);
      TackOn(&path[0], freq->fr_File);

      /* Check if file already exists and inform user */
      if (fh = Open(&path[0], (LONG)MODE_OLDFILE)) {
	 Close(fh);
      }
      if (!fh || ok_cancel_requester(" Save ", "File already exists.\\n\\n"
			     "Do you really want to overwrite?") == TRUE) {
	 /* Open file and write project data */
	 if (!(fh = Open(&path[0], (LONG)MODE_NEWFILE))) {
	    status = EDITOR_ERROR_OPEN_FAILED;
	 } else {
	    if ((status = write_project_header(fh, tl)) ==
						     EDITOR_STATUS_NORMAL) {
	       if ((status = write_project_fonts(fh, tl)) ==
						     EDITOR_STATUS_NORMAL) {
		  status = write_project_templates(fh, tl);
	       }
	    }
	    Close(fh);

	    /* If any error then delete incomplete project file */
	    if (status != EDITOR_STATUS_NORMAL) {
	       DeleteFile(&path[0]);
	    } else {
	       tl->tl_Flags &= ~TEMPLATE_LIST_FLAG_CHANGED;
	    }
	 }
      }
   }
   IRestoreMousePointer(ewin);
   if (status != EDITOR_STATUS_NORMAL) {
      show_error(status);
   }
   return(status);
}
	/* Write project header to save file */

   STATIC SHORT
write_project_header(BPTR fh, struct TemplateList  *tl)
{
   SHORT status;

   if (FPrintf(fh, "\n\t/* Project: %s */\n\n"
		"BEGIN PROJECTHEADER\n"
		"   LEFTEDGE=%d\n"
		"   TOPEDGE=%d\n"
		"   WIDTH=%d\n"
		"   HEIGHT=%d\n"
		"   FLAGS=%d\n"
		"   ID=\"%s\"\n"
		"END PROJECTHEADER\n", &tl->tl_ProjectName[0],
		   pwin->LeftEdge, pwin->TopEdge, pwin->Width, pwin->Height,
			       tl->tl_Flags, &tl->tl_ProjectID[0]) == -1L) {
      status = EDITOR_ERROR_WRITE_FAILED;
   } else {
      status = EDITOR_STATUS_NORMAL;
   }
   return(status);
}
	/* Write all template fonts to save file */

   STATIC SHORT
write_project_fonts(BPTR fh, struct TemplateList  *tl)
{
   struct TemplateFont  *tf;
   USHORT count = 0;
   SHORT  status = EDITOR_STATUS_NORMAL;

   for (count = 0, tf = get_head((struct List *)&tl->tl_Fonts);
			      tf && status == EDITOR_STATUS_NORMAL; count++,
			    tf = get_succ((struct Node *)&tf->tf_MinNode)) {
      struct TextAttr  *ta = &tf->tf_TextAttr;

      if (FPrintf(fh, "\n\t/* Font %d */\n\n"
		"BEGIN FONT\n"
		"   NAME=\"%s\"\n"
		"   YSIZE=%d\n"
		"END FONT\n", count, ta->ta_Name, ta->ta_YSize) == -1L) {
	 status = EDITOR_ERROR_WRITE_FAILED;
      }
   }
   return(status);
}
	/* Write all templates to save file */

   STATIC SHORT
write_project_templates(BPTR fh, struct TemplateList  *tl)
{
   struct Template  *tp;
   UBYTE count;
   SHORT status = EDITOR_STATUS_NORMAL;

   for (count = 0, tp = get_head((struct List *)&tl->tl_Templates);
				       tp && status == EDITOR_STATUS_NORMAL;
				     count++, tp = get_succ(&tp->tp_Node)) {
      struct Box  *box = &tp->tp_Box;
      UBYTE type = tp->tp_Type;

      /* Write standard data */
      if (FPrintf(fh, "\n\t/* Template %d: %s */\n\n"
		"BEGIN TEMPLATE\n"
		"   NAME=\"%s\"\n"
		"   TYPE=%d\n"
		"   FLAGS=%d\n"
		"   BEGIN BOX\n"
		"      X1=%d\n"
		"      Y1=%d\n"
		"      X2=%d\n"
		"      Y2=%d\n"
		"   END BOX\n", count, template_type_text_array[type],
		    &tp->tp_TemplateName[0], type, tp->tp_Flags, box->bo_X1,
			       box->bo_Y1, box->bo_X2, box->bo_Y2) == -1L) {
	 status = EDITOR_ERROR_WRITE_FAILED;
      } else {

	 /* Write text list if any */
	 if (type == TEMPLATE_TYPE_MX || type == TEMPLATE_TYPE_CYCLE ||
					   type == TEMPLATE_TYPE_LISTVIEW) {
	    if (FPrintf(fh, "   BEGIN TEXTLIST\n") == -1L) {
	       status = EDITOR_ERROR_WRITE_FAILED;
	    } else {
	       struct Node  *node;

	       for (node = get_head(&tp->tp_TextList); node &&
		    status == EDITOR_STATUS_NORMAL; node = get_succ(node)) {
		  if (FPrintf(fh, "      TEXT=\"%s\"\n",
						    node->ln_Name) == -1L) {
		     status = EDITOR_ERROR_WRITE_FAILED;
		  }
	       }
	       if (status == EDITOR_STATUS_NORMAL) {
		  if (FPrintf(fh, "   END TEXTLIST\n") == -1L) {
		     status = EDITOR_ERROR_WRITE_FAILED;
		  }
	       }
	    }
	 }

	 /* Write template data */
	 if (status == EDITOR_STATUS_NORMAL) {
	    struct BorderData  *bd;
	    struct TextData    *td;
	    struct GadgetData  *gd;
	    BYTE *fmt;

	    switch (TEMPLATE_GROUP(tp)) {
	       case TEMPLATE_GROUP_BORDER :

		  /* Write border data */
		  bd = &tp->tp_Data.tp_BorderData;
		  if (FPrintf(fh,
			"   BEGIN BORDERDATA\n"
			"      TYPE=%d\n"
			"   END BORDERDATA\n", bd->bd_Type) == -1L) {
		     status = EDITOR_ERROR_WRITE_FAILED;
		  }
		  break;

	       case TEMPLATE_GROUP_TEXT :

		  /* Write text data */
		  td = &tp->tp_Data.tp_TextData;
		  if (td->td_Type == TEXT_DATA_TYPE_TEXT) {
		     fmt = "   BEGIN TEXTDATA\n"
			"      TYPE=%d\n"
			"      FLAGS=%d\n"
			"      TEXT=\"%s\"\n"
			"      TEXTATTR=%d\n"
			"   END TEXTDATA\n";
		  } else {
		     fmt = "   BEGIN TEXTDATA\n"
			"      TYPE=%d\n"
			"      FLAGS=%d\n"
			"      TEXT=%ld\n"
			"      TEXTATTR=%d\n"
			"   END TEXTDATA\n";
		  }
		  if (FPrintf(fh, fmt, td->td_Type, td->td_Flags,
				      td->td_Text, get_template_font_num(tl,
						 td->td_TextAttr)) == -1L) {
		     status = EDITOR_ERROR_WRITE_FAILED;
		  }
		  break;

	       case TEMPLATE_GROUP_GADGET :

	   	  /* Write gadget data */
	   	  gd = &tp->tp_Data.tp_GadgetData;
	   	  if (FPrintf(fh,
			"   BEGIN GADGETDATA\n"
			"      TYPE=%ld\n"
			"      FLAGS=%ld\n", gd->gd_Type,
						     gd->gd_Flags) == -1L) {
		     status = EDITOR_ERROR_WRITE_FAILED;
		  } else {
		     if (!gd->gd_Text) {
			fmt = "      TEXT=%ld\n";
		     } else {
			fmt = "      TEXT=\"%s\"\n";
		     }
		     if (FPrintf(fh, fmt, gd->gd_Text) == -1L) {
			status = EDITOR_ERROR_WRITE_FAILED;
		     } else {
			if (gd->gd_Type == GADGET_DATA_TYPE_STRING) {
			   fmt = "      TEXTATTR=%d\n"
				 "      SPECIAL1=%ld\n"
				 "      SPECIAL2=%ld\n"
				 "      SPECIAL3=\"%s\"\n";
			} else {
			   fmt = "      TEXTATTR=%d\n"
				 "      SPECIAL1=%ld\n"
				 "      SPECIAL2=%ld\n"
				 "      SPECIAL3=%ld\n";
			}
			if (FPrintf(fh, fmt, get_template_font_num(tl,
							   gd->gd_TextAttr),
					gd->gd_SpecialData.gd_Data.gd_Data1,
					gd->gd_SpecialData.gd_Data.gd_Data2,
			      gd->gd_SpecialData.gd_Data.gd_Data3) == -1L) {
			   status = EDITOR_ERROR_WRITE_FAILED;
			} else {
			   if (FPrintf(fh, "   END GADGETDATA\n") == -1L) {
			      status = EDITOR_ERROR_WRITE_FAILED;
			   }
			}
		     }
		  }
		  break;
	    }
	 }
	 if (status == EDITOR_STATUS_NORMAL) {
	    if (FPrintf(fh, "END TEMPLATE\n") == -1L) {
	       status = EDITOR_ERROR_WRITE_FAILED;
	    }
	 }
      }
   }
   return(status);
}
