/************************************************************
mapper.h

eli.wings.ee
Tue Feb  3 12:07:28 EET 1998
Thu Mar  5 16:12:41 EET 1998
Wed Apr 22 13:20:11 EET DST 1998

************************************************************/

#ifndef MAPPER_H
#define MAPPER_H 1

/************************************************************/

#ifndef _XLIB_H_
#include <X11/Xlib.h>
#endif

#ifndef _INTRINSIC_H_
#include <X11/Intrinsic.h>
#endif

/************************************************************/
/*
	max nr of different displays or screens available
*/
#define MAX_DISPLAYS 4

/*
	actual attribute and class name lengths 
	MUST BE _smaller_ BY ONE (i.e. 6 characters in both cases)
*/
#define ATTR_NAME_LEN 7
#define CLASS_NAME_LEN 7
#define COLOR_NAME_LEN 6

/*
	situation model codes
*/
#define SM_UNKNOWN 0
#define SM_S57 1

/*
	presentation model codes
*/
#define PM_UNKNOWN 0
#define PM_S52 1

/*
	flags to be ORed together for MapSetPresentationParams 
	or MapGetPresentationParams
*/
#define PP_NORTH_ARROW (1L<<0)
#define PP_TWO_SHADES (1L<<1)
#define PP_SHALLOW_PATTERN (1L<<2)
#define PP_TEXT_OFF (1L<<3)
#define PP_SAFETY_DEPTH (1L<<4)
#define PP_SHALLOW_CONTOUR (1L<<5)
#define PP_SAFETY_CONTOUR (1L<<6)
#define PP_DEEP_CONTOUR (1L<<7)
#define PP_SYMBOL_SET (1L<<8)
#define PP_VISUALS (1L<<9)
#define PP_COLOR_MAPS (1L<<10)

/* 
	diffrent color coding schemes 
*/

#define CCS_DAY_BRIGHT 		   0
#define CCS_DAY_BLACKBACK 	   1
#define CCS_DUSK_BLACKBACK 	   2
#define CCS_NIGHT_FILTERED 	   3

/************************************************************/

/*
	error codes from Mapper functions (in MapperError 
	variable)
*/
typedef	enum errcode {
	MNOERROR = 0,	/* sucess */
	MFOPEN	=  1,	/* error opening file*/
	MFREAD	=  2,	/* error reading file */
	MMAP	=  3,	/* not a valid map structure */
	MDISP	=  4,	/* not a valid mapdisp structure */
	MOBJ	=  5,	/* not a valid obj structure */
	MSINSTR	=  6,	/* not a valid symbolization instruction */
	MIDISP	=  7,	/* request for too many displays */
	MINIT	=  8,	/* mapper not properly initialized */
	MNOOBJ	=  9,	/* map contains no objects */
	MFTYPE	=  10,	/* unknown feature obj type */
	MFNEXT	=  11,	/* findnext without findfirst */
	MIPRES	=  12,	/* cannot init presentation model */
	MNOMEM  =  13,	/* cannot alloc memory */
	MNOCOLOR = 14,	/* no such color */
	MCONSISTENCY = 15,	/* consistency have been lost */
	MCSCHEME = 16,	/* invalid color coding scheme */
	MNCLASS = 17,	/* unknown object class */
	MRCOLORS = 18,	/* cannot read RGBs from file */
	MRSYMBOLS = 19,	/* cannot read symbols */
	MHASH = 20,	/* error creating hash table */
	MIATTRN = 21,	/* invalid attribute */
	MIPRESENTATION = 22,	/* invalid presentation model */
	MISITUATION = 23	/* invalid situation model */
} ERRCODE;

typedef
struct	attribute {
	char Name[ ATTR_NAME_LEN ];
	char Type;
	union {
		char *String;
		long Number;
	} Value;	
} ATTRIBUTE;

typedef struct point
{
	long x;
	long y;
} POINT;

typedef struct sounding
{
	long x;
	long y;
	long d;
} SOUNDING;

typedef struct segment
{
	long ID;	/* segment identifier !!!READ_ONLY */
	int Type;	/* type of segment 1 - 3D (sounding) 0 - 2D */
/* 
	number of points in segment 
*/	
	unsigned long PointCount;	
	union
	{
		POINT Point;
		SOUNDING Sounding;
	} *Points;
} SEGMENT;

typedef struct contour
{
/* 
	0 - exterior (or line or point) 
	1 - interior
*/
	int Type;	
/* 
	number of segments in contour
*/	
	unsigned long SegmentCount;	
	SEGMENT *Segmets;
} CONTOUR;

typedef struct pparams
{
/* 
	toggles presentation of north arrow and scalebar 
*/
	int NorthArrow; 
	int TwoShades;	/* only two-color depth areas */
	int ShallowPattern; /* shallow water highlight */
	int SymbolSet;	/* 0 - simplified / 1 - traditional */
	int TextOff;	/* 1 if text is switched off */
	
	short SafetyDepth; /* selected safety depth [m] */
/* 
	selected shallow water contour [m] 
*/
	short ShallowContour; 
	short SafetyContour; /* selected safety contour [m]*/
	short DeepContour; /* selected deep water contour [m]*/

/* 
	visuals to be used in creating windows 
	!!! Read-only 
*/	
	Visual 	*Visuals[ MAX_DISPLAYS ];	
/* 
	colormaps to be used in creating windows  
	!!! Read-only 
*/	
	Colormap ColorMaps[ MAX_DISPLAYS ];	
} PPARAMS;

/************************************************************/

/* 
	returns version of the mapper library
	mapper needs not to be initialized
	major version number is in higher word (16bit), 
	minor version in lower word
*/
unsigned long MapVersion( void );

/*
	initializes mapper
	all other functions depend on successful completition of 
	mapinit()
*/
int MapInit( unsigned char DspCnt, Display *display[], 
	int SituationModel, int PresentationModel, 
	const char *ConfigFile );

/* 
	get names and descriptions of all available map object 
	classes 
	return total number of classes
*/
int MapGetClassNames( char ***Classes, char ***Descriptions );

/*
	returns string of last error message
*/
char *MapErrorString( void );

/* sets pixel per meter values for both axes */
int MapSetPixPerM( unsigned char DspNr, 
	long PixPerMx, long PixPerMy );

/* sets or resets visibility flag on class ClassName */
int	MapClassCntl( void *Map, 
	const char ClassName[ CLASS_NAME_LEN ], int Visibility );

/* sets or resets visibility flag on all classes */
int	MapCntl( void *Map, int Visibility );

/* allocates MapDisp structure */
void *MapDispCreate( void );

/* set map display center & zooming factor */
void *MapDispSet( void *MapDisp, int DspNr, Window Win, 
	short WinCX, short WinCY, long MagF, 
	long MapCX, long MapCY, double MPX, double MPY );

/* frees MapDisp structure */
int MapDispDestroy( void *MapDisp );

/* draws map to display area MapDisp */					
int	MapDraw( void *Map, void *MapDisp, int Changed );

/* 
	draws map to display area MapDisp 
	p1 (UL) & p2 (LR corner) specify the clipping rectangle
*/
int	MapAreaDraw( void *Map, void *MapDisp, int Changed, 
	POINT p1, POINT p2 );

/* draws one object according to MapDisp */
int	MapObjDraw( void *Map, void *Obj, void *MapDisp, 
	int Changed );

/* 
	draws one object according to MapDisp 
	in clipping rectangle (UL p1, LR p2)
*/
int	MapObjAreaDraw( void *Map, void *Obj, void *MapDisp, 
	int Changed, POINT p1, POINT p2 );

/* 
	returns pixel value of requested map color or 
	-1 on error 
*/
Pixel MapGetColor( void *MapDisp, 
	const char colorname[ COLOR_NAME_LEN ]);

/* 
	return pixel values and color names of active color 
	coding scheme. 
	application is responsible for freeing memory allocated 
	for colors_return and color_names_return
*/
int MapGetColors( void *MapDisp, Pixel **ColorsReturn, 
	char ***ColorNamesReturn );

/* 
	returns current color coding scheme or negative value on 
	error 
*/
int MapGetColorScheme( void *MapDisp );

/* 
	sets current color coding scheme to specified value. 
	use defined constants CCS* for Sceme's value
*/
int MapSetColorScheme( void *MapDisp, unsigned char Scheme );

/* set map presentation parameters */
int MapSetPresentationParams( void *MapDisp, 
	unsigned long ParamMask, PPARAMS *Params ); 
 
/* get map presentation parameters */
int MapGetPresentationParams( void *MapDisp, 
	unsigned long ParamMask, PPARAMS *Params ); 

/*
	allocate memory for map structure
	returns pointer to map structure
	have to be called prior to any operation with map
*/
void *MapCreate( void );

/* load map data from file MapName.map */
int	MapLoad( void *Map, const char *MapName );

/* frees all map data for new MapLoad */
int	MapUnLoad( void *Map );

/* saves map to file MapName.map */
int MapSave( void *Map, const char *MapName );

/* frees map structure */
int	MapDestroy( void *Map );

/* request coordinate magnification factor from map */
long MapGetCoordinateMagnification( void *Map );

/* adjust coordinate magnification factor of map */
int MapSetCoordinateMagnification( void *Map, long MagF );

/* request coordinate magnification factor from map */
long MapGetSoundingMagnification( void *Map );

/* adjust coordinate magnification factor of map */
int MapSetSoundingMagnification( void *Map, long MagF );

/* get projection parameters currently used */
int MapGetProjection( void *Map, int *Projection, 
	double *Par1, double *Par2, double *Par3, double *Par4 );

/* query map horizontal datum */
int MapGetHorDatum( void *Map, int *Datum );

/* returns minimal lattitude or MAXLONG on error */
long MapGetMinY( void *Map );

/* returns minimal longitude or MAXLONG on error */
long MapGetMinX( void *Map );

/* returns maximal lattitude or MINLONG on error */
long MapGetMaxY( void *Map );

/* returns maximal longitude or MINLONG on error */
long MapGetMaxX( void *Map );

/* 
	returns first object in specified class or 
	NULL on error 
*/
void *MapGetFirstObjectInClass( void *Map, 
	const char ClassName[ CLASS_NAME_LEN ]);

/* 
	returns next object in class 
	or NULL on error or when no more objects
*/
void *MapGetNextObjectInClass( void *Map, 
	const char ClassName[ CLASS_NAME_LEN ]);

/* finds object by its coordinates with tolerance Delta */
int	MapGetObjByCoord( void *Map, void *MapDisp, long MagF, 
	POINT *Point, long Delta, void ***Mobj );

/* 
	returns first object of map or 
	NULL on error 
*/
void *MapGetFirstObject( void *Map );

/* 
	returns next object of map ( from all classes ) 
	or NULL on error (or when no more objects ) 
*/
void *MapGetNextObject( void *Map );

/* returns minimal longitude or MAXLONG on error */
long MapObjGetMinX( void *Obj );

/* returns minimal lattitude or MAXLONG on error */
long MapObjGetMinY( void *Obj );

/* returns maximal longitude or MINLONG on error */
long MapObjGetMaxX( void *Obj );

/* returns maximal lattitude or MINLONG on error */
long MapObjGetMaxY( void *Obj );

/* return nr of attributes */
int MapObjGetAttributeCount( void *Obj );

/* 
	returns the value of named attribute in Attr 
	does not allocate storage
	return 0 on sucess, -1 on error
*/
int MapObjGetAttributeValue( void *Obj, ATTRIBUTE *Attr );

/* 
	copies attribs of Obj to Attributes and returns number 
	of attributes 
	allocates storage for Attributes which must be freed by 
	caller
*/
int	MapObjGetAttributes( void *Obj, ATTRIBUTE **Attributes );

/* 
	returns nr of points in Obj (total for all exterior and 
	interior contours) 
*/
int MapObjGetPointCount( void *Obj );

/* 
	copies points of Obj to Points and returns nr of points 
	storage is allocated
*/
int	MapObjGetPoints( void *Obj, 
	unsigned long *ContourCount, CONTOUR **Points );

/* returns class name of specified object */
char *MapObjGetClass( void *Obj );

/* 
	returns type of specified object 
	'A' - area
	'L'	- line
	'P'	- point
	on error '\0' is returned
*/
char MapObjGetType( void *Obj );

/* 
	sets object's class, also modifies presentation 
		instrucion.
	returns 0 if succesful 
*/
int	MapObjSetClass( void *Obj, 
	const char ClassName[ CLASS_NAME_LEN ]);

/* 
	sets object's points. old points are freed. 
	changes Min & Max points 
	returns total point count 
*/
int	MapObjSetPoints( void *Obj, 
	unsigned long ContoursCount, CONTOUR *Contours );

/* 
	sets Obj attributes from Attributes, returns Count. 
	modifies presentation instruction 
*/
int	MapObjSetAttributes( void *Obj, int Count, 
	ATTRIBUTE *Attributes );

/* 
	deletes one attribute entry, returns 0 if successful
	also modifies presentation instrucion 
*/
int	MapObjDelAttribute( void *Obj, 
	const char Name[ ATTR_NAME_LEN ]);

/* 
	deletes all attributes of an object 
	modifies presentation instruction 
*/
int	MapObjDelAttributes( void *Obj );

/* sets the value of named attribute */
int MapObjSetAttributeValue( void *Obj, 
	const char Name[ ATTR_NAME_LEN ], char *Value );

/* 
	create a object and add to map database 
*/
void *MapObjCreate( void *Map, 
	const char ClassName[ CLASS_NAME_LEN ],	char Primitive, 
	unsigned long ContourCount, CONTOUR *Contours, 
	int AttributeCount, ATTRIBUTE *Attributes );
/*
	turn on/off highlightning for given object
*/
int MapObjHighlight( void *Obj, int Highlight );

/* frees map object */
int MapObjDestroy( void *Obj );

/************************************************************/

extern ERRCODE MapError;	/* last error code */

/************************************************************/

#endif
/*
	#ifndef MAPPER_H
*/

