/*************************************************************************
	File:	tga_init.c

	FormZ image bitmap translator sample.  Imports and exports Targa images.

	COPYRIGHT (C) 2001.  All Rights Reserved.
	auto*des*sys, Inc., Columbus, Ohio, USA.
*************************************************************************/

#include "tga_prvt.h"




/************************************************************************
	Set default values for custom export options and override default
	values for common export options.
*************************************************************************/
fzrt_error_td tga_write_opts_default(fz_ffmt_ref_td ffmt_id)
{
	fzrt_error_td 					err = FZRT_NOERR;
	fzrt_int							flags;
	short							platform = FZ_FFMT_WRITE_NL_METH_CRLF;
	fz_type_td 						fz_type;

	/* Custom export options */
	_write_flags = 0;
			
	/* Override common export options */
	/* Get the options bit flags. */
	_ffmt_2d_funcs.fz_ffmt_image_write_opts_parm_get(ffmt_id, FZ_FFMT_IMAGE_WRITE_OPTS_PARM_FLAGS, &fz_type);
	_type_funcs.fz_type_get_int(&fz_type, &flags);

	/* Set the binary file flag. */
	FZ_SETBIT(flags, FZ_FFMT_IMAGE_WRITE_OPTS_BINARY_BIT);

	/* Set the Include Background flag. */
	FZ_SETBIT(flags, FZ_FFMT_IMAGE_WRITE_OPTS_BACKGROUND_BIT);

	/* Set the modified options bit flags. */
	_type_funcs.fz_type_set_int(&flags, &fz_type);
	_ffmt_2d_funcs.fz_ffmt_image_write_opts_parm_set(ffmt_id, FZ_FFMT_IMAGE_WRITE_OPTS_PARM_FLAGS, &fz_type);

	/* Override the default line termination. */
	_type_funcs.fz_type_set_short(&platform, &fz_type);
	_ffmt_2d_funcs.fz_ffmt_image_write_opts_parm_set(ffmt_id, FZ_FFMT_IMAGE_WRITE_OPTS_PARM_PLATFORM, &fz_type);

	return(err);
}

/************************************************************************
	Given an error id, returns to formZ an error string associated with a targa error code.
*************************************************************************/
fzrt_boolean tga_error_str_func(fzrt_int err, char *str, short str_len)
{
	char	msg[256];

	/* Get the string form the plugin's resource file. */
	_fzrt_base_funcs.fzrt_fzr_get_string(_fz_rsrc_ref, TGA_MESSAGES, err, msg);
	strncpy(str, msg, str_len);
	
	/* If we sucessfully got the string return TRUE otherwise return FALSE. */
	return(msg[0] == '\0' ? FALSE : TRUE);
}

/************************************************************************
	Adds custom items to the export options dialog.
*************************************************************************/
fzrt_error_td tga_dlog_cust_func(fz_fuim_tmpl_ptr fuim_tmpl, short parent, fz_ffmt_ref_td ref_id)
{
short		gindx;
char		title[256];

	/* Get a string for the Compress custom option item. */
	_fzrt_base_funcs.fzrt_fzr_get_string(_fz_rsrc_ref, TGA_STRINGS, TGA_COMPRESS_STR, title);
	
	/* Create a check box the Compress custom option item. */
	if((gindx = _fuim_funcs.fz_fuim_new_check(fuim_tmpl, parent, 0, FZ_FUIM_FLAG_GFLT | FZ_FUIM_FLAG_HORZ, title, NULL, NULL)) != -1)
	{
		/* Associate a bit flag variable with the check box. */
		_fuim_funcs.fz_fuim_item_encod_int(fuim_tmpl, gindx, &_write_flags , TRUE, FZ_FUIM_BIT2_MASK(TGA_COMPRESS_BIT));
	}
	
	return(FZRT_NOERR);
}

/************************************************************************
	Save and restore custom options.
*************************************************************************/
fzrt_error_td tga_opts_io(fz_iost_ptr iost, fz_iost_dir_td_enum dir, fzpl_vers_td * const version, fzrt_unsigned_int size)
{
	fzrt_error_td	err = FZRT_NOERR;
	
	if(dir == FZ_IOST_WRITE)	/* Write */
	{
		/* Set the version of the translator */
		*version = TGA_PLUGIN_VERSION;
	}
	else	/* Read */
	{
		/* If the version is newer than this version of the translator, skip the block and return an error. */
		if(*version > TGA_PLUGIN_VERSION)
		{
			_iost_funcs.fz_iost_block_skip(iost, size);
			err = _fzrt_base_funcs.fzrt_error_set_with_detail(FZPL_BAD_VERSION_ERROR, FZRT_ERROR_SEVERITY_WARNING, FZRT_ERROR_CONTEXT_FZRT, FZPL_CONTEXT_ID, 0 );
		}
	}

	if(err == FZRT_NOERR)
	{
		/* Perform the read or write. */
		err = _iost_funcs.fz_iost_int(iost, &_write_flags, 1);
	}

	return(err);
}

/************************************************************************
	Returns the name of the translator to formZ.
*************************************************************************/
fzrt_error_td tga_get_name(char *fmt_name, fzrt_int fmt_name_max_len)
{
	strncpy(fmt_name, "TARGA - SDK Sample", fmt_name_max_len);
	
	return(FZRT_NOERR);
}

/************************************************************************
	Returns the id of the translator to formZ.
*************************************************************************/
fzrt_error_td tga_get_id(fzrt_UUID_td fmt_id)
{
	fzrt_error_td					err = FZRT_NOERR;

	/* Just use the plugin id for the translator's id. */
	_fzrt_base_funcs.fzrt_UUID_copy(TGA_PLUGIN_ID, fmt_id);
	
	return(err);
}

/************************************************************************
	Returns other information about the translator to formZ.
*************************************************************************/
fzrt_error_td tga_get_info(
			char			*sortby,
			fzrt_int			sortby_max_len,
			char			*file_ext,
			fzrt_int			file_ext_max_len,
			char			*keywd,
			fzrt_int			keywd_max_len,
			fzrt_int			*attr_flags
			)
{
	/* Set the string used to sort translators in menus. */
	strncpy(sortby, "tga", sortby_max_len);

	/* Set the format's file extension. */
	strncpy(file_ext, "tga:targa", file_ext_max_len);

	/* Set the format's key word. */
	strncpy(keywd, TGA_PLUGIN_KEYWD, keywd_max_len);

	/* Set the translator's attribute flags. */
	*attr_flags = 0;
	
	return(FZRT_NOERR);
}

/************************************************************************
	Returns the Macintosh file type and file creator to formZ.
*************************************************************************/
fzrt_error_td tga_get_ftype(
			fzrt_ostype		*type,
			fzrt_ostype		*creator
			)
{
	
	/* Set the format's Macintosh file type. */
	*type = 'TPIC';

	/* Set the format's Macintosh file creator. */
	*creator = 'VTVS';
	
	return(FZRT_NOERR);
}

/************************************************************************
	Returns the file format's icon to formZ.
*************************************************************************/
fzrt_error_td tga_get_icon(
			fzrt_icon_ptr 	*icon
			)
{
	fzrt_int							err = FZRT_NOERR;
	short							rlib_index;

	if(icon != NULL)
	{
		/* Get the plugin's library file's runtime id. */
		_plugin_query_funcs.fzpl_plugin_get_rlib_idx(_tga_plugin_runtime_id, &rlib_index);

		/* Get the file format icon from the library file's resources. */
		err = _fzrt_base_funcs.fzrt_rlib_load_icon(rlib_index, FZRT_LOAD_ICON_COLOR, TGA_ICON, icon);
	}
	
	return(err);
}

/************************************************************************
	Returns the translator's import capability flags to formZ.
*************************************************************************/
fzrt_error_td tga_read_opts_get_flags(fz_ffmt_ref_td ffmt_id, fzrt_int *flags, fzrt_int *opts_flags)
{
	fzrt_error_td			err = FZRT_NOERR;

	if(flags != NULL)
	{
		/* Set the bit flag to enable import as draft image. */
		FZ_SETBIT(*flags, FZ_FFMT_IMAGE_READ_IFACE_OPTS_ENABLEIMPORTASDRAFTBITMAP_BIT);

		/* Set the bit flag to enable the Keep Origonal Size item. */
		FZ_SETBIT(*flags, FZ_FFMT_IMAGE_READ_IFACE_OPTS_ENABLEORIGINALSIZE_BIT);

		/* Set the bit flag to enable the Keep Proportions item. */
		FZ_SETBIT(*flags, FZ_FFMT_IMAGE_READ_IFACE_OPTS_ENABLEPROPORTIONS_BIT);

		/* Set the bit flag to enable the Store Image In Project item. */
		FZ_SETBIT(*flags, FZ_FFMT_IMAGE_READ_IFACE_OPTS_ENABLESTOREINPROJECT_BIT);
	}

	if(opts_flags != NULL)
	{
		/* Set the bit flag to enable the Options button on the standard file open dialog. */
		FZ_SETBIT(*opts_flags, FZ_FFMT_OPTS_INIT_HAS_READ_OPTS_BIT);
	}

	return(err);
}

/************************************************************************
	Returns the translator's export capability bit flags to formZ.
*************************************************************************/
fzrt_error_td tga_write_opts_get_flags(fz_ffmt_ref_td ffmt_id, fzrt_int *flags, fzrt_int *opts_flags)
{
	fzrt_error_td			err = FZRT_NOERR;

	if(flags != NULL)
	{
		/* None of the common options items are enabled. */
		*flags =	0;
	}

	if(opts_flags != NULL)
	{
		/* Set the bit flag to enable the Options button on the standard file open dialog. */
		FZ_SETBIT(*opts_flags, FZ_FFMT_OPTS_INIT_HAS_WRITE_OPTS_BIT);
	}

	return(err);
}
