/*------------------------------------------------------------------------*
  Filename....: view_palette_plugin.c
  Scope.......: Sample project palette plugin.

  Last Change.:

  Version      Date                Description      Author
	 1.0        06/17/02           New				Tim Hanes


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

#include "view_palette.h"

#include "fz_plugin_glue_api.h"

#include "fz_palt_api.h"


/************************************************************************
*************************************************************************/
static fzrt_error_td view_palette_info(fz_proj_level_enum *level)
/************************************************************************/
{
	*level = FZ_PROJ_LEVEL_MODEL;
	
	return (FZRT_NOERR);
}
/************************************************************************
*************************************************************************/
static fzrt_error_td view_palette_name(char *name, fzrt_int max_len)
/************************************************************************/
{
	char				str[256];
	
	fzrt_base_funcs.fzrt_fzr_get_string(rsrc_ref, STRINGS, STRING_NAME, str);
	strncpy(name, str, max_len);
	
	return (FZRT_NOERR);
}
/************************************************************************
*************************************************************************/
static fzrt_error_td view_palette_uuid(fzrt_UUID_td uuid)
/************************************************************************/
{

	fzrt_base_funcs.fzrt_UUID_copy(VIEW_PALETTE_ID, uuid);
	
	return (FZRT_NOERR);
}

/************************************************************************
************************************************************************/
static short view_palette_item_func(fz_fuim_tmpl_ptr fuim_tmpl, fzrt_int action, short item_id, void *func_data, fz_fuim_type_td *rv_data)
{
	short 				rv=FALSE;	
	fz_view_type_enum	vtype;
	fzrt_int				windex;
	fz_type_td 			type;
	
	windex = (fzrt_int)func_data;

	switch (action)
	{
		case FZ_FUIM_ACTN_GET_ACTIV :
			break;
		case FZ_FUIM_ACTN_GET_VALUE :
			break;
		case FZ_FUIM_ACTN_SET_VALUE :
			break;			
		case FZ_FUIM_ACTN_HIT:
			if (item_id == STRING_AXONOMETRIC) 
			{
				vtype = FZ_VIEW_TYPE_AXONOMETRIC;
				rv = TRUE;
			}
			else if (item_id == STRING_ISOMETRIC) 
			{
				vtype = FZ_VIEW_TYPE_ISOMETRIC;
				rv = TRUE;
			}
			else if (item_id == STRING_OBLIQUE) 
			{
				vtype = FZ_VIEW_TYPE_OBLIQUE;
				rv = TRUE;
			}
			else if (item_id == STRING_PERSPECTIVE) 
			{
				vtype = FZ_VIEW_TYPE_PERSPECTIVE;
				rv = TRUE;
			}
			else if (item_id == STRING_PANORAMIC) 
			{
				vtype = FZ_VIEW_TYPE_PANORAMIC;
				rv = TRUE;
			}
			break;
			
	}
	if(rv)
	{	type_funcs.fz_type_set_int((fzrt_int *)&vtype, &type);
		view_funcs.fz_view_set_parm_data(windex, NULL, FZ_VIEW_PARM_TYPE, &type);
	}

	return (rv);
}

/************************************************************************
*************************************************************************/
static fzrt_error_td view_palette_ifac_tmpl(fzrt_int windex, fz_fuim_tmpl_ptr fuim_tmpl, fzrt_ptr tmpl_data)
/************************************************************************/
{
	short				gindx;
	char				str[256];
	fzrt_error_td		err;
	
	fzrt_base_funcs.fzrt_fzr_get_string(rsrc_ref, STRINGS, STRING_NAME, str);
	if((err = fuim_funcs.fz_fuim_tmpl_init(fuim_tmpl, str, 0, VIEW_PALETTE_ID, 0)) == FZRT_NOERR)
	{
		fzrt_base_funcs.fzrt_fzr_get_string(rsrc_ref, STRINGS, STRING_TYPE, str);
		gindx = fuim_funcs.fz_fuim_new_text_static(fuim_tmpl, -1, FZ_FUIM_NONE, FZ_FUIM_FLAG_BRDR | FZ_FUIM_FLAG_EQSZ, str, NULL, NULL);
		
		fuim_funcs.fz_fuim_stack_put(fuim_tmpl, 1, sizeof(fzrt_int), &windex);

			fzrt_base_funcs.fzrt_fzr_get_string(rsrc_ref, STRINGS, STRING_AXONOMETRIC, str);
			fuim_funcs.fz_fuim_new_button(fuim_tmpl, gindx, STRING_AXONOMETRIC, FZ_FUIM_FLAG_NONE, str, view_palette_item_func, NULL);

			fzrt_base_funcs.fzrt_fzr_get_string(rsrc_ref, STRINGS, STRING_ISOMETRIC, str);
			fuim_funcs.fz_fuim_new_button(fuim_tmpl, gindx, STRING_ISOMETRIC, FZ_FUIM_FLAG_NONE, str, view_palette_item_func, NULL);
			
			fzrt_base_funcs.fzrt_fzr_get_string(rsrc_ref, STRINGS, STRING_OBLIQUE, str);
			fuim_funcs.fz_fuim_new_button(fuim_tmpl, gindx, STRING_OBLIQUE, FZ_FUIM_FLAG_NONE, str, view_palette_item_func, NULL);
			
			fzrt_base_funcs.fzrt_fzr_get_string(rsrc_ref, STRINGS, STRING_PERSPECTIVE, str);
			fuim_funcs.fz_fuim_new_button(fuim_tmpl, gindx, STRING_PERSPECTIVE, FZ_FUIM_FLAG_NONE, str, view_palette_item_func, NULL);
			
			fzrt_base_funcs.fzrt_fzr_get_string(rsrc_ref, STRINGS, STRING_PANORAMIC, str);
			fuim_funcs.fz_fuim_new_button(fuim_tmpl, gindx, STRING_PANORAMIC, FZ_FUIM_FLAG_NONE, str, view_palette_item_func, NULL);
	}

	return (err);
}






/************************************************************************
	view_palette_acquire_fsets:
			acquires all application provided service function sets.
*************************************************************************/
static fzrt_error_td view_palette_acquire_fsets ( void )
/************************************************************************/
{
	fzrt_error_td err = FZRT_NOERR;


	
	if(err == FZRT_NOERR)
	{
		err = (*fset_glue->fzpl_fset_acquire) ( FZ_MATH_FSET_TYPE, FZ_MATH_FSET_VERSION, FZRT_UUID_NULL,
									 FZPL_TYPE_STRING(fz_math_fset), sizeof(math_funcs),
									 (fzpl_fset_td *)&math_funcs );
	}
	
	if(err == FZRT_NOERR)
	{
		err =(*fset_glue->fzpl_fset_acquire)(FZ_MODEL_FSET_TYPE, FZ_MODEL_FSET_VERSION, FZRT_UUID_NULL,
									 FZPL_TYPE_STRING(fz_model_fset), sizeof(model_funcs),
									(fzpl_fset_td *)&model_funcs);
	}

	if ( !err )
	{
		err = (*fset_glue->fzpl_fset_acquire) ( FZ_TYPE_FSET_TYPE, FZ_TYPE_FSET_VERSION, FZRT_UUID_NULL,
									 FZPL_TYPE_STRING(fz_type_fset), sizeof(type_funcs),
									 (fzpl_fset_td *)&type_funcs );
	}

	if ( !err )
	{
		err = (*fset_glue->fzpl_fset_acquire) ( FZ_FUIM_FSET_TYPE, FZ_FUIM_FSET_VERSION, FZRT_UUID_NULL,
									 FZPL_TYPE_STRING(fz_fuim_fset), sizeof(fuim_funcs),
									 (fzpl_fset_td *)&fuim_funcs );
	}
	if(err == FZRT_NOERR)
	{
		err = (*fset_glue->fzpl_fset_acquire) ( FZ_FZRT_BASE_FSET_TYPE, FZ_FZRT_BASE_FSET_VERSION, FZRT_UUID_NULL,
									 FZPL_TYPE_STRING(fz_fzrt_base_fset), sizeof(fzrt_base_funcs),
									 (fzpl_fset_td *)&fzrt_base_funcs );
	}
	if(err == FZRT_NOERR)
	{
		err = (*fset_glue->fzpl_fset_acquire) ( FZ_FZRT_FILE_FSET_TYPE, FZ_FZRT_FILE_FSET_VERSION, FZRT_UUID_NULL,
									 FZPL_TYPE_STRING(fz_fzrt_file_fset), sizeof(fzrt_file_funcs),
									 (fzpl_fset_td *)&fzrt_file_funcs );
	}
	if(err == FZRT_NOERR)
	{
		err = (*fset_glue->fzpl_fset_acquire) ( FZ_VIEW_FSET_TYPE, FZ_VIEW_FSET_VERSION, FZRT_UUID_NULL,
									 FZPL_TYPE_STRING(fz_view_fset), sizeof(view_funcs),
									 (fzpl_fset_td *)&view_funcs );
	}
	
	return err;
}

/************************************************************************
	view_palette_release_fsets:
			releases all application provided service function sets.

	TAH 06/17/02: NEW FUNCTION
*************************************************************************/

static void view_palette_release_fsets ( void )
/************************************************************************/
{
	(*fset_glue->fzpl_fset_release) ( (fzpl_fset_td *)&fuim_funcs );
	(*fset_glue->fzpl_fset_release) ( (fzpl_fset_td *)&type_funcs );
	(*fset_glue->fzpl_fset_release) ( (fzpl_fset_td *)&model_funcs );
	(*fset_glue->fzpl_fset_release) ( (fzpl_fset_td *)&math_funcs );
	(*fset_glue->fzpl_fset_release) ( (fzpl_fset_td *)&fzrt_base_funcs );
	(*fset_glue->fzpl_fset_release) ( (fzpl_fset_td *)&fzrt_file_funcs );
	(*fset_glue->fzpl_fset_release) ( (fzpl_fset_td *)&view_funcs );
}

/************************************************************************
*************************************************************************/
static fzrt_error_td view_palette_cmds_fill ( const fzpl_fset_def_ptr fset_def, fzpl_fset_td * const fset )
/************************************************************************/
{
	fzrt_error_td				err = FZRT_NOERR;
	fz_palt_cbak_proj_fset 		*cmds_func;

	if ( fset_def && fset )
	{
		fset_glue->fzpl_fset_def_check ( fset_def, FZ_PALT_CBAK_PROJ_FSET_VERSION, FZPL_TYPE_STRING(fz_palt_cbak_proj_fset), sizeof ( cmds_func ), FZPL_VERSION_OP_NEWER );
		if ( err == FZRT_NOERR )
		{
			cmds_func = (fz_palt_cbak_proj_fset *)fset;

			cmds_func->fz_palt_cbak_proj_init				= NULL;
			cmds_func->fz_palt_cbak_proj_finit				= NULL;
			cmds_func->fz_palt_cbak_proj_info				= view_palette_info;
			cmds_func->fz_palt_cbak_proj_name				= view_palette_name;
			cmds_func->fz_palt_cbak_proj_uuid				= view_palette_uuid;
			cmds_func->fz_palt_cbak_proj_help				= NULL;
	
			cmds_func->fz_palt_cbak_proj_iface_tmpl			= view_palette_ifac_tmpl;

			cmds_func->fz_palt_cbak_proj_pref_io			= NULL;	
			cmds_func->fz_palt_cbak_proj_data_io			= NULL;	
			cmds_func->fz_palt_cbak_proj_wind_data_io		= NULL;	


		}
	}
	else
	{
		err = fzrt_base_funcs.fzrt_error_set_with_detail ( FZRT_BAD_PARAM_ERROR, FZRT_ERROR_SEVERITY_ERROR, FZRT_ERROR_CONTEXT_FZRT, 0, 3001 );
	}

	return err;
}

/************************************************************************
	view_palette_check_config:
			A first pass check to see if the plugin can be loaded.
			Checks licensing.
			Checks that application defined function sets which are
				needed by the plugins are available.
			Verifies that plugin and application are compatible.
			Verifies that plugin type is expected by application.
			Informs the application if this plugin is dependent on
				function sets from other plugins.


	TAH 06/17/02: NEW FUNCTION
*************************************************************************/

static fzrt_error_td view_palette_check_config ( const fzpl_fset_glue_fset * const fzpl_fset_glue, const fzpl_host_config_td * const hostConfig )
/************************************************************************/
{
	fzrt_error_td					err = FZRT_NOERR;

	fset_glue = fzpl_fset_glue;

	/*  Make sure required application defined function sets are available */
	/*  Release the function sets at unload. */

	err = view_palette_acquire_fsets();

	if ( err == FZRT_NOERR )
	{
		fzrt_file_funcs.fzrt_file_floc_init(&floc);
		err = (*fset_glue->fzpl_plugin_file_get_floc)(floc);
		if(err == FZRT_NOERR)
		{
			err = fzrt_base_funcs.fzrt_fzr_open(floc,"view_palette", &rsrc_ref);
		}
	}
	
	if ( err != FZRT_NOERR )
	{
		view_palette_release_fsets();
	}
	
	return err;
}

/************************************************************************
*************************************************************************/
static fzrt_boolean view_palette_error_str_func(fzrt_int err, char *str, short str_len)
/************************************************************************/
{
	char	msg[256];

	fzrt_base_funcs.fzrt_fzr_get_string(rsrc_ref, 1, err, msg);
	strncpy(str, msg, str_len);
	
	return(msg[0] == '\0' ? FALSE : TRUE);
}

/************************************************************************
	view_palette_register_plugins:
		Registers plugins and their assocoated function sets.

	TAH 06/17/02: NEW FUNCTION
*************************************************************************/

static fzrt_error_td view_palette_register_plugins ( const fzpl_fset_glue_fset * const fsf, const fzpl_host_config_td * const hostConfig )
/************************************************************************/
{
	fzrt_error_td					err = FZRT_NOERR;
	fzpl_plugin_query_fset			plugin_query_funcs;
	char							str[256];
	
	fzrt_base_funcs.fzrt_fzr_get_string(rsrc_ref, STRINGS, STRING_NAME, str);

	err = (*fset_glue->fzpl_plugin_register)(
						VIEW_PALETTE_PLUGIN_ID,
						str, 
						VIEW_PALETTE_PLUGIN_VERSION, 
						VIEW_PALETTE_PLUGIN_VENDOR,
						VIEW_PALETTE_PLUGIN_URL,
						FZ_PALT_EXTS_TYPE, 
						FZ_PALT_EXTS_VERSION,
						view_palette_error_str_func, 0, NULL,
						&plugin_runtime_id);

	if ( err == FZRT_NOERR )
	{
		err = (*fset_glue->fzpl_plugin_add_fset)( plugin_runtime_id, FZ_PALT_CBAK_PROJ_FSET_TYPE,
											FZ_PALT_CBAK_PROJ_FSET_VERSION, FZ_PALT_CBAK_PROJ_FSET_NAME,
											FZPL_TYPE_STRING(fz_palt_cbak_proj_fset), sizeof ( fz_palt_cbak_proj_fset ),
											view_palette_cmds_fill, FALSE );

		if(err == FZRT_NOERR)
		{	if((err = (fset_glue->fzpl_fset_acquire) ( FZPL_PLUGIN_QUERY_FSET_TYPE, FZPL_PLUGIN_QUERY_FSET_VERSION, FZRT_UUID_NULL,
						  FZPL_TYPE_STRING(fzpl_plugin_query_fset), sizeof(fzpl_plugin_query_fset),
						  (fzpl_fset_td *)&plugin_query_funcs)) == FZRT_NOERR )
			{
				plugin_query_funcs.fzpl_plugin_get_rlib_idx(plugin_runtime_id, &rlib_index);
				
				fzrt_file_funcs.fzrt_file_floc_init(&floc);
				plugin_query_funcs.fzpl_plugin_get_floc(plugin_runtime_id, floc);
				
				fset_glue->fzpl_fset_release( (fzpl_fset_td *)&plugin_query_funcs );

			}
		}				

		if ( err != FZRT_NOERR )
		{
			view_palette_release_fsets();
			err = fzrt_base_funcs.fzrt_error_set ( FZRT_UNKNOWN_ERROR, FZRT_ERROR_SEVERITY_ERROR, FZRT_ERROR_CONTEXT_FZRT, 0 );
		}
	}

	return err;
}

/************************************************************************
	view_palette_unregister_plugins:
		Unregisters plugins.

	TAH 06/17/02: NEW FUNCTION
*************************************************************************/

static fzrt_error_td view_palette_unregister_plugins ( const fzpl_fset_glue_fset * const fset_glue )
/************************************************************************/
{
	fzrt_error_td					err;

	err = (*fset_glue->fzpl_plugin_unregister)( plugin_runtime_id );

	view_palette_release_fsets();

	return err;
}

/************************************************************************
	fz_plugin_entry:
		The main plugin file entry point.  Plugin initialization
			happens via this function.

	TAH 06/17/02: NEW FUNCTION
*************************************************************************/

FZPL_PLUGIN_FUNC(fzrt_error_td) fz_plugin_entry( const fzpl_fset_glue_fset * const fsf, fzpl_command_td cmd, const fzpl_host_config_td * const hostConfig )
/************************************************************************/
{
	fzrt_error_td err = FZRT_NOERR;

	switch ( cmd )
	{
		case FZPL_PLUGIN_CHECK:
			err = view_palette_check_config ( fsf, hostConfig );
		break;
		
		case FZPL_PLUGIN_INITIALIZE:
			err = view_palette_register_plugins ( fsf, hostConfig );
		break;
		
		case FZPL_PLUGIN_EXIT:
			err = view_palette_unregister_plugins ( fsf );
		break;
	}

	return ( err );
}

