/****************************************************************
 * Filename  :	Item.c
 * Functions :	
 *		Item_New
 *		Item_New_Dep
 *		Item_Delete
 *		Item_Delete_Dep
 *		Item_Initialize
 *		Item_Initialize_Dep
 *		Item_Display
 *		Item_Hide
 *		Item_SetValue
 *		Item_SetPosition
 *		Item_SetFont
 *		Item_SetColor
 *		Item_SetMode
 *		Item_SetLineWidth
 *		Item_SetLineStyle
 *		Item_SetResources
 *		Item_SetDmName
 *		Item_Refresh
 *		Item_Erase
 * Purpose   : 	Functions for the Item class
 $Revision: 1.1 $
 ***************************************************************/

#include "dmvideo.h"                    /* data manager definitions           */
#include "OVtypes.h"                    /* video overlay type definitions     */
#include "dy4std.h"                     /* DY4 standard definitions           */
#include "video.h"                      /* DY4 video definitions              */
#include "mygraphics.h"                 /* graphics alignment definitions     */
#include "Item.h"                       /* item definitions                   */
#include "Context.h"                    /* context definitions                */
#include "Text.h"                       /* text definitions                   */
#include "String.h"                     /* string definitions                 */
#include "Counter.h"                    /* counter definitions                */
#include "Logo.h"                    	/* logo definitions                   */
#include "Gauge.h"                    	/* gauge definitions                  */
#include "Curve.h"                    	/* curve definitions                  */
#include "Icon.h"                    	/* icon definitions                   */

/*---------------------------------------------------------------
 | Function  :	Item_New
 | Purpose   : 	Return a new item structure
 | Inputs    :	none
 | Outputs   :	none
 | Side FX   :	none
 | comments  :
 | Author    :	sylvain.coudray MBARI-IFREMER/diti/dsi/ie
 ---------------------------------------------------------------*/
    OV_Item*
Item_New(OV_item_type *type, OV_char *name)
{
    OV_Item 	*p_item;	/* an item */

    p_item = (OV_Item *) malloc ( sizeof(OV_Item));

    p_item->p_context = Context_New();
    p_item->p_curve = Curve_New();
    p_item->name = name;
    p_item->type = type;

    return (p_item);

} /* Item_New */

/*---------------------------------------------------------------
 | Function  :	Item_New_Dep
 | Purpose   : 	Constructor for Dependent Item class.
 | Inputs    :	None.
 | Outputs   :	Returns pointer to dependent item structure.
 | Author    :  Janice Tarrant
 ---------------------------------------------------------------*/
    OV_Dep_Item*
Item_New_Dep(Void)
{

    OV_Dep_Item *p_depItem;		/* dependent item structure pointer   */

    p_depItem = (OV_Dep_Item *) malloc ( sizeof(OV_Dep_Item));

    return(p_depItem);

} /* Item_New_Dep */

/*---------------------------------------------------------------
 | Function  :	Item_Delete
 | Purpose   : 	Free memory from structure passed in parameter
 | Inputs    :	none
 | Outputs   :	none
 | Side FX   :	none
 | comments  :
 | Author    :	sylvain.coudray MBARI-IFREMER/diti/dsi/ie
 | Modified  :  tarrant 8/7/96
 |                to free all malloced components of structure
 ---------------------------------------------------------------*/
    void
Item_Delete(OV_Item *p_item)
{
    if (p_item->name != NULL)
	free(p_item->name);

    if (p_item->type != NULL)
	free(p_item->type);

    if (p_item->value != NULL)
	free(p_item->value);

    if (p_item->old_value != NULL)
	free(p_item->old_value);

    if (p_item->dm_name != NULL)
	free(p_item->dm_name);

    Context_Delete(p_item->p_context);
    Curve_Delete(p_item->p_curve);

    free(p_item);

    return;

} /* Item_Delete */

/*---------------------------------------------------------------
 | Function  :	Item_Delete_Dep
 | Purpose   : 	Destructor for Dependent Item class.
 | Inputs    :	Pointer to dependent item structure.
 | Outputs   :	None.
 | Author    :	Janice Tarrant
 ---------------------------------------------------------------*/
    void
Item_Delete_Dep(OV_Dep_Item *p_depItem)
{

    if (p_depItem->p_item != NULL)
    	Item_Delete(p_depItem->p_item);

    free(p_depItem);

    return;

} /* Item_Delete_Dep */

/*---------------------------------------------------------------
 | Function  :	Item_Initialize
 | Purpose   : 	object initialization
 | Inputs    :	none
 | Outputs   :	none
 | Side FX   :	none
 | comments  :
 | Author    :	sylvain.coudray MBARI-IFREMER/diti/dsi/ie
 | Modified  :  tarrant 9/5/96 - added p_item->dm_type
 ---------------------------------------------------------------*/
    void
Item_Initialize(OV_Item *p_item)
{

	/* initialize associated objects */	
	Context_Initialize ( p_item->p_context);
	Curve_Initialize ( p_item->p_curve);

	/* give default values for all attributes */
	p_item->x = 0;
	p_item->y = 0;
	p_item->value = NULL;
	p_item->old_value = NULL;
	p_item->dm_name = NULL;
	p_item->dm_type = DM_EMPTY;
	p_item->dm_item = NULL;
	p_item->draw_cursor = 1;

        return;

} /* Item_Initialize */

/*---------------------------------------------------------------
 | Function  :	Item_Initialize_Dep
 | Purpose   : 	Initializes Dependent Item class.
 | Inputs    :	none
 | Outputs   :	none
 | Author    :	Janice Tarrant
 ---------------------------------------------------------------*/
	void
Item_Initialize_Dep(OV_Dep_Item *p_depItem)
{
    p_depItem->prev_on_display = FALSE;
    p_depItem->on_display = NULL;

} /* Item_Initialize_Dep */

/*---------------------------------------------------------------
 | Function  :	Item_Display
 | Purpose   : 	Display the Item on the screen
 | Inputs    :	none
 | Outputs   :	none
 | Side FX   :	none
 | comments  :
 | Author    :	sylvain.coudray MBARI-IFREMER/diti/dsi/ie
 | Modified  :  tarrant 08/20/96 to support dependent items (TEXT and COUNTER)
 |           :  tarrant 10/02/96 to support square icons
 |           :	tarrant 10/17/96 to support dependent gauge line
 |           :	tarrant 10/24/96 to support non-zero counters (NZ_COUNTER)
 |	     :  tarrant 10/24/96 to support crosshair icons
 |           :  tarrant 12/09/96 to support strings
 |           :  tarrant 07/14/97 replaced globals with alignment structure
 ---------------------------------------------------------------*/
    void
Item_Display(OV_alignment *alignment, OV_Item *p_item)
{
    OV_int counter;			/* counter value        */

/*debug
logMsg("Item Display %s, value\n", p_item->name, p_item->value);
enddebug*/
	/* modify resources */

	My_string_resources(p_item->p_context->text_font,
		            p_item->p_context->foreground_color,
		            p_item->p_context->display_mode,
		            p_item->p_context->line_width,
		            p_item->p_context->line_style);

	/* ................................... */
	/* ... Simulate Method inheritance ... */
	/* ................................... */

	if ((strcmp(p_item->type, "TEXT") == 0)  ||
	    (strcmp(p_item->type, "DEP_TEXT") == 0))
	{
	   Text_Display(alignment, p_item);
           return;
	}

        if (strcmp(p_item->type, "STRING") == 0)
        {
           String_Display(alignment, p_item);
           return;
        }

	if ( ( strcmp(p_item->type, "COUNTER") == 0) ||
	     ( strcmp(p_item->type, "DEP_COUNTER") == 0) ||
	     ( strcmp(p_item->type, "DATE") == 0) ||
	     ( strcmp(p_item->type, "TIME") == 0))
	{
	   Counter_Display(alignment, p_item);
           return;
	}
	
	if (strcmp(p_item->type, "NZ_COUNTER") == 0)
	{
	   sscanf(p_item->value, "%d", &counter);
	   if (counter != 0)
	       Counter_Display(alignment, p_item);
           return;
	}
	
	if (strcmp(p_item->type, "LOGO") == 0)
	{
	   Logo_Display(alignment, p_item);
           return;
	}

	if (strcmp(p_item->type, "GAUGE") == 0)
	{
	   Gauge_Display(alignment, p_item);
           return;
	}

	if (strcmp(p_item->type, "DEP_GAUGE_LINE") == 0)
	{
	   Gauge_Line(alignment, p_item);
           return;
	}

	if ( strcmp(p_item->type, "CURVE") == 0)
	{
	   Curve_Display(alignment, p_item);
           return;
	}

	if (strcmp(p_item->type, "SQUARE_ICON") == 0)
	{
	   Square_Icon_Display(alignment, p_item);
           return;
	}

	if (strcmp(p_item->type, "CROSSHAIR") == 0)
	{
	   Crosshair_Icon_Display(alignment, p_item);
           return;
	}

        return;

} /* Item_Display */


/*---------------------------------------------------------------
 | Function  :	Item_SetValue
 | Purpose   : 	set the current value of the item.
 | Inputs    :	none
 | Outputs   :	none
 | Side FX   :	none
 | comments  :
 | Author    :	sylvain.coudray MBARI-IFREMER/diti/dsi/ie
 | Modified  :  tarrant 8/20/96
 |                to support dependent items (TEXT and COUNTER)
 |              tarrant 10/2/96
 |                to support square icons
 |		tarrant 10/24/96
 |		  to support non-zero counters (NZ_COUNTER)
 |		  to support crosshair icons
 ---------------------------------------------------------------*/
    void
Item_SetValue(OV_Item *p_item, OV_char *value)
{
	OV_int	nb_char;	/* number of characters */

/* free the previous old value */
	if (p_item->old_value != NULL) free ( p_item->old_value);

/* store the current value (to erase it further) */

	if (p_item->value != NULL)
	{
	   nb_char = strlen ( p_item->value);
	   p_item->old_value = (char *) malloc ( nb_char+1);
	   strcpy ( p_item->old_value, p_item->value);
	}	

/* free the current value */
	if (p_item->value != NULL) 
	{
/*debug
printf ("current value = %s\n", p_item->value);
enddebug*/
	  free ( p_item->value);
	}


	/* ................................... */
	/* ... Simulate Method inheritance ... */
	/* ................................... */

	if ( ( strcmp ( p_item->type, "COUNTER") == 0) || 
	     ( strcmp ( p_item->type, "DEP_COUNTER") == 0) ||
	     ( strcmp ( p_item->type, "NZ_COUNTER") == 0) ||
	     ( strcmp ( p_item->type, "DATE") == 0) ||
	     ( strcmp ( p_item->type, "TIME") == 0))
	{
	   Counter_SetValue ( p_item, value);
	   return;
	}

	if (strcmp(p_item->type, "SQUARE_ICON") == 0)
	{
	    Square_Icon_SetValue(p_item, value);
	    return;
	}

	if (strcmp(p_item->type, "CROSSHAIR") == 0)
	{
	    Crosshair_Icon_SetValue(p_item, value);
	    return;
	}

	/* ................................. */
	/* ... Begin specific statements ... */
	/* ................................. */

/* modify the current value for other objects */
	nb_char = strlen ( value);
   	p_item->value = (char *) malloc ( nb_char);
	strcpy ( p_item->value, value);

        return;

} /* Item_SetValue */


/*---------------------------------------------------------------
 | Function  :	Item_SetPosition
 | Purpose   : 	set the current value of the item.
 | Inputs    :	none
 | Outputs   :	none
 | Side FX   :	none
 | comments  :
 | Author    :	sylvain.coudray MBARI-IFREMER/diti/dsi/ie
 ---------------------------------------------------------------*/
    void
Item_SetPosition(OV_Item *p_item, OV_float x, OV_float y)
{
	p_item->x = x;
	p_item->y = y;

        return;

} /* Item_SetPosition */


/*---------------------------------------------------------------
 | Function  :	Item_SetFont
 | Purpose   : 	set the current font of the item.
 | Inputs    :	none
 | Outputs   :	none
 | Side FX   :	none
 | comments  :
 | Author    :	sylvain.coudray MBARI-IFREMER/diti/dsi/ie
 ---------------------------------------------------------------*/
    void
Item_SetFont(OV_Item *p_item, OV_font *font)
{

	p_item->p_context->text_font = font;	

        return;

} /* Item_SetFont */


/*---------------------------------------------------------------
 | Function  :	Item_SetColor
 | Purpose   : 	set the current color of the item.
 | Inputs    :	none
 | Outputs   :	none
 | Side FX   :	none
 | comments  :
 | Author    :	sylvain.coudray MBARI-IFREMER/diti/dsi/ie
 ---------------------------------------------------------------*/
    void
Item_SetColor(OV_Item *p_item, OV_color *color)
{

	p_item->p_context->foreground_color = color;	

        return;

} /* Item_SetColor */

/*---------------------------------------------------------------
 | Function  :	Item_SetMode
 | Purpose   : 	set the current display mode of the item.
 | Inputs    :	none
 | Outputs   :	none
 | Side FX   :	none
 | comments  :
 | Author    :	sylvain.coudray MBARI-IFREMER/diti/dsi/ie
 ---------------------------------------------------------------*/
    void
Item_SetMode(OV_Item *p_item, OV_mode *mode)
{
	p_item->p_context->display_mode = mode;	

        return;

} /* Item_SetMode */

/*---------------------------------------------------------------
 | Function  :	Item_SetFormat
 | Purpose   : 	set the current display format for values
 | Inputs    :	none
 | Outputs   :	none
 | Side FX   :	none
 | comments  :
 | Author    :	sylvain.coudray MBARI-IFREMER/diti/dsi/ie
 ---------------------------------------------------------------*/
    void
Item_SetFormat(OV_Item *p_item, OV_format format)
{
	p_item->p_context->value_format = format;	

        return;

} /* Item_SetFormat */

/*---------------------------------------------------------------
 | Function  :	Item_SetLineWidth
 | Purpose   : 	set the current display line_width of the item.
 | Inputs    :	none
 | Outputs   :	none
 | Side FX   :	none
 | comments  :
 | Author    :	sylvain.coudray MBARI-IFREMER/diti/dsi/ie
 ---------------------------------------------------------------*/
    void
Item_SetLineWidth(OV_Item *p_item, OV_width *line_width)
{
	p_item->p_context->line_width = line_width;	

        return;

} /* Item_SetLineWidth */

/*---------------------------------------------------------------
 | Function  :	Item_SetLineStyle
 | Purpose   : 	set the current display line_width of the item.
 | Inputs    :	none
 | Outputs   :	none
 | Side FX   :	none
 | comments  :
 | Author    :	sylvain.coudray MBARI-IFREMER/diti/dsi/ie
 ---------------------------------------------------------------*/
	void
Item_SetLineStyle(OV_Item *p_item, OV_style *line_style)
{
	p_item->p_context->line_style = line_style;	

        return;

} /* Item_SetLineStyle */

/*---------------------------------------------------------------
 | Function  :	Item_SetResources
 | Purpose   : 	Set font, color and mode for the current item
 | Inputs    :	none
 | Outputs   :	none
 | Side FX   :	none
 | comments  :
 | Author    :	sylvain.coudray MBARI-IFREMER/diti/dsi/ie
 ---------------------------------------------------------------*/
    void
Item_SetResources(OV_Item *p_item, OV_font *font, OV_color *color,
		  OV_mode *mode, OV_format format, OV_width *width,
		  OV_style *style)
{
	Item_SetFont(p_item, font);
	Item_SetColor(p_item, color);
	Item_SetMode(p_item, mode);
	Item_SetFormat(p_item, format);
	Item_SetLineWidth(p_item, width);
	Item_SetLineStyle(p_item, style);

        return;

} /* Item_SetResources */


/*---------------------------------------------------------------
 | Function  :	Item_SetDmName
 | Purpose   : 	Set DataManager item name
 | Inputs    :	string name
 | Outputs   :	none
 | Side FX   :	attribute dm_name modified
 | comments  :
 | Author    :	sylvain.coudray MBARI-IFREMER/diti/dsi/ie
 | Modified  :  tarrant 10/25/96
 |                to check malloc has NULL pointer
 ---------------------------------------------------------------*/
	void
Item_SetDmName(OV_Item *p_item, OV_char *name)
{

    if (p_item->dm_name != NULL)
	free(p_item->dm_name);
    p_item->dm_name = (char *) malloc (strlen(name));

    strcpy(p_item->dm_name, name);

    return;

} /* Item_SetDmName */


/*---------------------------------------------------------------
 | Function  :	Item_Refresh
 | Purpose   : 	Refresh Item only if value != old_value
 | Inputs    :	none
 | Outputs   :	none
 | Side FX   :	none
 | comments  :
 | Author    :	sylvain.coudray MBARI-IFREMER/diti/dsi/ie
 |           :  tarrant 07/14/97 replaced globals with alignment structure
 ---------------------------------------------------------------*/
    void
Item_Refresh(OV_alignment *alignment, OV_Item *p_item)
{
    OV_char *value;			/* current item value                 */

    if (strcmp(p_item->value, p_item->old_value) != 0)
    {
/* save current value                                                         */
    	value = p_item->value;

/* erase previous value                                                       */
    	p_item->value = p_item->old_value;
   	Item_Erase(alignment, p_item);

/* restore current value                                                      */
    	p_item->value = value;

/* display new value                                                          */
   	Set_pixel_processing_operation(0);
   	Item_Display(alignment, p_item);
    }

    return;

} /* Item_Refresh */


/*---------------------------------------------------------------
 | Function  :	Item_Erase
 | Purpose   : 	Erase Item on display.
 | Inputs    :	Pointer to Item.
 | Outputs   :	None.
 | Author    :	Janice Tarrant
 ---------------------------------------------------------------*/
    void
Item_Erase(OV_alignment *alignment, OV_Item *p_item)
{
    Set_pixel_processing_operation(10);
    Item_Display(alignment, p_item);

} /* Item_Erase */


/*---------------------------------------------------------------
 | Function  :	Item_cccccc
 | Purpose   : 	
 | Inputs    :	none
 | Outputs   :	none
 | Side FX   :	none
 | comments  :
 | Author    :	sylvain.coudray MBARI-IFREMER/diti/dsi/ie
 ---------------------------------------------------------------*/

	void
Item_cccccc ( p_item)
OV_Item		*p_item;
{

	/* ... Local Variables */
	/* ... Initializations */
	/* ... Begin */


	/* ... End */

        return;

} /* Item_cccccc */

