/*
 *	@(#)text.h 1.18 91/09/14 Copyright 1990-91 Sun Microsystems
 *
 * This file is a product of Sun Microsystems, Inc. and is provided for
 * unrestricted use provided that this legend is included on all tape
 * media and as a part of the software program in whole or part.  Users
 * may copy or modify this file without charge, but are not authorized to
 * license or distribute it to anyone else except as part of a product
 * or program developed by the user.
 * 
 * THIS FILE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
 * 
 * This file is provided with no support and without any obligation on the
 * part of Sun Microsystems, Inc. to assist in its use, correction,
 * modification or enhancement.
 * 
 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS FILE
 * OR ANY PART THEREOF.
 * 
 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
 * or profits or other special, indirect and consequential damages, even
 * if Sun has been advised of the possibility of such damages.
 * 
 * Sun Microsystems, Inc.
 * 2550 Garcia Avenue
 * Mountain View, California  94043
 */

#ifndef _TEXT_INCLUDED_
#define _TEXT_INCLUDED_

#include <portable/c_varieties.h>
#include <wire/wire.h>
#include <jot/bytestring.h>

/* well known formatter parameters, and values */

#define Jot_LINE_STYLE	    0
#   define Jot_JUSTIFY		200
#   define Jot_LEFT_ALIGN	201
#   define Jot_RIGHT_ALIGN	202
#   define Jot_CENTER		203
#   define Jot_CHARACTER_WRAP	204
#   define Jot_CHARACTER_CLIP	205
#define Jot_FIRST_INDENT    1
#define Jot_LEFT_INDENT	    2
#define Jot_RIGHT_INDENT    3
#define Jot_SPACE_BEFORE    4
#define Jot_SPACE_AFTER	    5
#define Jot_LINE_SPACING    6
#define Jot_TAB_STOPS	    7
#   define Jot_isparagraph_parameter(p)	((p) <= Jot_TAB_STOPS)
#define Jot_FONT	    8
#define Jot_BOLD	    9
#define Jot_ITALIC	    10
#define Jot_UNDERLINE	    11
#define Jot_STRIKETHRU	    12
#   define Jot_OFF		0
#   define Jot_ON		1
#   define Jot_INVERT		2
#define Jot_FONT_SIZE	    13
#define Jot_BASELINE_OFFSET 14
#define Jot_FG_COLOR	    15
#define Jot_BG_COLOR	    16
#define Jot_NPARAMS	    17
#define Jot_RULER_NAME	    18
#define Jot_NULL_PARAMETER  Jot_NPARAMS

/* A snapshot of the values of the various formatter parameters, and
   other pertinent info. */
typedef struct Jot_formatter_info {
    int		    parameters[Jot_NPARAMS];
    int		    next_style_pos;	/* pos of next style change */
    struct Jot_font *font;		/* font resulting from current state
					   of parameters */
} Jot_formatter_info;

typedef struct {
    char    which;	/* what parameter we're modifying */
    int	    value;	/* the value used in the modification */
} Jot_mparam;

struct JotRuler {
    Bytestring	    *mods;	    /* parameter modifications */
    char	    *name;	    /* name of this style */
    int		    nmods;	    /* number of mods */
    JotRuler	    *following;	    /* ruler than follows this one */
    JotRuler	    *basedon;	    /* this ruler inherits from basedon */
};

/* A singing & dancing text */
struct JotText {
    Bytestring	    *data;	    /* the data data part of the text */
    Bytestring	    *stylerefs;	    /* the array of style sheet references */
    Bytestring	    *rulers;	    /* array of rulers */
    short	    nstylerefs;	    /* the number of style references */
    short	    nrulers;	    /* number of rulers */
    JotRuler	    *default_ruler; /* default ruler used to calculate
				       defaultinfo (below) */
    Jot_formatter_info defaultinfo; /* default info for the view, based
				       on Jot_defaultinfo + application
				       of mods described in default_ruler */
    struct {
	Bytestring	edits;	    /* array of molecules */
	unsigned short	index;	    /* current index into list */
	unsigned short	count;	    /* number of existing molecules */
	unsigned short	max;	    /* user settable limit */
	short		bundle;	    /* while this is > 0, add to the current
				       molecule, don't begin a new one */
	unsigned	enabled:1;  /* are we currently recording? */
	unsigned	in_undo:1;  /* while we're undoing an operation, don't
				       record any edits we make */
	unsigned	new_grp:1;  /* begin a new group regardless */
    } eh;			    /* edit history for undo/redo */
    int		    modified;	    /* number of modifications */
    unsigned int    in_rtf_read:1;  /* if reading rtf, styles are affected */
    JotSpan	    *spans;	    /* spans that refer to this text */
    JotSpan	    *caret;	    /* the caret for this text */
    JotView	    *views;	    /* the views on this document */
    wire_TokenArray holders;	    /* server side handles for text object */
};

EXTERN_FUNCTION( int	    JotText_CharacterAt,	(JotText* text, int pos) );
EXTERN_FUNCTION( void	    JotText_Clear,		(JotText* text) );
EXTERN_FUNCTION( int	    JotText_DeleteCharacters,	(JotText* text, int pos, int n) );
EXTERN_FUNCTION( JotSpan   *JotText_FirstSpan,		(JotText *text) );
EXTERN_FUNCTION( JotView   *JotText_FirstView,		(JotText *text) );
EXTERN_FUNCTION( void	    JotText_Free,		(JotText* text) );
EXTERN_FUNCTION( int	    JotText_InsertCharacters,	(JotText* text, int pos, char* buffer, int n) );
EXTERN_FUNCTION( int	    JotText_InsertString,	(JotText* text, int pos, char* string) );
EXTERN_FUNCTION( JotText   *JotText_New,		(int length) );
EXTERN_FUNCTION( int	    JotText_Newlines,		(JotText* text) );
EXTERN_FUNCTION( JotSpan   *JotText_NextSpan,		(JotSpan *span) );
EXTERN_FUNCTION( JotView   *JotText_NextView,		(JotView *view) );
EXTERN_FUNCTION( int	    JotText_Read,		(JotText* text, int pos, int fp) );
EXTERN_FUNCTION( int	    JotText_ReadRtf,		(JotText* text, int pos, int fp) );
EXTERN_FUNCTION( int	    JotText_ReplaceCharacters,	(JotText* text, int pos, int length, char* rstring, int rlength) );
EXTERN_FUNCTION( int	    JotText_ScanCharacter,	(JotText* text, int pos, int c, int n) );
EXTERN_FUNCTION( boolean    JotText_SetCaret,		(JotText* text, int pos) );
EXTERN_FUNCTION( int	    JotText_Write,		(JotText* text, int pos, int length, int fp) );
EXTERN_FUNCTION( int	    JotText_WriteRtf,		(JotText* text, int pos, int length, int fp) );
EXTERN_FUNCTION(void JotText_SetDefaultRuler, (JotText *text, JotRuler *ruler));
EXTERN_FUNCTION(JotRuler *JotText_DefaultRuler, (JotText *text));

#define JotText_Caret(t)		JotSpan_Position((t)->caret)
#define JotText_Characters(t)		Bytestring_Length((t)->data)
#define JotText_FastCharacterAt(t, p)	Bytestring_CharAt((t)->data, (p))
#define JotText_FirstView(t)		((t)->views)
#define JotText_HasStyles(t)		((t)->nstylerefs > 0 || (t)->nrulers > 0)
#define JotText_Modified(t)		((t)->modified)
#define JotText_NextView(v)		((v)->viewnext)

EXTERN_FUNCTION(JotRuler *JotRuler_New, (_VOID_) );
EXTERN_FUNCTION(boolean JotRuler_Insert, (JotText *text, JotRuler *ruler, int pos) );
EXTERN_FUNCTION(void JotRuler_SetBasedOn, (JotRuler *first, JotRuler *basedon) );
EXTERN_FUNCTION(void JotRuler_SetFollowing, (JotRuler *first, JotRuler *second) );
EXTERN_FUNCTION(void JotRuler_Clear, (JotRuler *ruler) );
EXTERN_FUNCTION(void JotRuler_SetParameters, (JotRuler *ruler, DOTDOTDOT) );
EXTERN_FUNCTION(void JotRuler_GetParameters, (JotRuler *r, JotText *t, DOTDOTDOT));
EXTERN_FUNCTION(void JotRuler_Free, (JotRuler *ruler) );
EXTERN_FUNCTION(JotRuler *JotRuler_RulerAt, (JotText *t, int pos));
EXTERN_FUNCTION(JotRuler *JotRuler_Delete, (JotText *t, int pos));
EXTERN_FUNCTION(boolean JotRuler_Span, (JotText *t, int pos, JotSpan *span));
EXTERN_FUNCTION(boolean JotRuler_Copy, (JotRuler *dest, JotRuler *src));

EXTERN_FUNCTION(boolean JotStyle_SetParameters, (JotSpan *s, DOTDOTDOT));
EXTERN_FUNCTION(void JotStyle_GetParameters, (JotText *t, int pos, DOTDOTDOT));

#define JotRuler_Name(r)	(r)->name
#define JotRuler_Following(r)	(r)->following
#define JotRuler_BasedOn(r)	(r)->basedon

#endif
