
/******
 * Include utili
 ******/

#include <exec/types.h>
#include <exec/ports.h>
#include <dos/dos.h>



/******
 * Define utili
 ******/

#define ASM		__asm
#define SAVEDS		__saveds
#define RG(x)		register __ ## x

#define MAXCOLOR	32	/* # Max di colori che il programma memorizza */
#define MAXDEPTH	5	/* Depth Max relativa */

#define MAXREXXARGS	10	/* # Max di argomenti per i comandi AREXX */

#define MAXFILENAME	121	/* Lunghezza massima per i file */

#define	INSCHAR		1	/* UNDO: Ins. car. (INS) (arg1=char) */
#define REPCHAR		2	/* UNDO: Rim. car. (OVR) (arg1=char) */
#define	DELCHAR		3	/* UNDO: Del. car. */
#define	BACKCHAR	4	/* UNDO: Can. car. */
#define	CUTLINE		5	/* UNDO: Can. linea */
#define	PASTELINE	6	/* UNDO: Paste linea */
#define	CLEARBLOCK	7	/* UNDO: Clear blocco */
#define	CUTBLOCK	8	/* UNDO: Cut blocco */
#define	PASTEBLOCK	9	/* UNDO: Paste blocco */
#define REPLACE		10	/* UNDO: Replace */
#define UPPERCASE	11	/* UNDO: Upper case letter */
#define LOWERCASE	12	/* UNDO: Lower case letter */
#define TOGGLECASE	13	/* UNDO: Toggle case letter */
#define SWAPLINE	14	/* UNDO: Swap line */
#define DOUBLELINE	15	/* UNDO: Double line */
#define UPPERCASEBLOCK	16	/* UNDO: Upper case block */
#define LOWERCASEBLOCK	17	/* UNDO: Lower case block */
#define TOGGLECASEBLOCK	18	/* UNDO: Toggle case block */
#define LAYOUTBLOCK	19	/* UNDO: Layout block */
#define INDENTBLOCK	20	/* UNDO: Indent block */

#define MAXMARKER	10	/* # Max di marker */

#define IND_MR		'0'	/* Le parole nel vettore Prefs.KeyWordInd[] */
#define IND_ML		'1'	/* devono terminare con uno di questi caratteri */
#define IND_SR		'2'	/* per identificare il tipo di indentazione */
#define IND_SL		'3'	/* da assegnare alla parola stessa. */
#define IND_MR_SR	'4'
#define IND_MR_SL	'5'
#define IND_ML_SR	'6'
#define IND_ML_SL	'7'

#define NO_SEL		0	/* Se PEDWindow.Block=NO_SEL -> nessun blocco selezionato */
#define BLOCK_SEL	1	/* Se PEDWindow.Block=BLOCK_SEL -> un blocco e' selezionato */
#define COLBLOCK_SEL	2	/* Se PEDWindow.Block=COLBLOCK_SEL -> un blocco colonnare e' sel. */



/******
 * Definizione struttura template
 ******/

#define MAXTEMPLATE	21

struct MyTemplate
{
	char			 Template[MAXTEMPLATE+1];
	struct MyProgram	*Prg;
	struct MyTemplate	*NextTemplate;
};



/******
 * Definizione struttura scanner
 ******/

#define MAXEXTSCAN	31
#define MAXSCANNAME	121

struct MyScan
{
	BPTR		 SegList;
	ULONG		(* ASM ScanFunction)(RG(a0) char *,RG(a1) char *);
	char		 ScanExtension[MAXEXTSCAN+1],
			 Name[MAXSCANNAME+1];
	struct MyScan	*NextScan;
};



/******
 * Definizione struttura che la funzione di gestione di un comando esterno riceve
 ******/

struct CommandData
{
	char			 *CommandLine;
	void			**CommandArgs;
	struct PEDWindow	 *CurrentWindow,
				 *FirstWindow;
	struct Prefs		 *CurrentPrefs;
	LONG ASM (*ExecuteInternalCommand)(RG(a0) char *);
};



/******
 * Definizione struttura per i programmi
 ******/

#define	TYPE_DUMMY	0
#define	TYPE_INTERNAL	1
#define	TYPE_SHELL	2
#define	TYPE_AREXX	3
#define	TYPE_TEXT	4

struct MyProgram
{
	char			*String,
				*Dir,
				*Output;
	UBYTE			 Type;
	UBYTE			 Async;
	struct MyProgram	*NextProgram;
};



/******
 * Definizione struttura per i messaggi API
 ******/



/* Struttura per la registrazione presso PED dei clienti */

struct APIClient
{
	struct MsgPort		*ac_ClientPort;	/* Porta a cui inviare i messaggi */
	ULONG			 ac_Notify;	/* Flag che indica i tipi di messaggi desiderati */
	char			*ac_name;	/* Nome (da mostrare all'utente) del cliente */
	struct APIClient	*ac_Next;	/* Prossimo cliente della lista (riempito dal PED) */
};

#define	NOTIFY_ON_SHOW_HIDE	0x00000001	/* Notifica i messaggi SHOW/HIDE */
#define	NOTIFY_ON_KEY		0x00000002	/* Notifica i messaggi KEY */



/* Struttura dei messaggi per la comunicazione PED<->cliente */

struct APIMessage
{
	struct Message		 am_Message;
	ULONG			 am_MsgType,
				 am_MsgArg[10],
				 am_RC;
};

/* Messaggi che PED spedisce ai clienti (nessuno dei quali presenta argomenti) */

#define PED_API_QUIT	0x80000000	/* PED sta per chiudere -> termina anche tu */
#define PED_API_HIDE	0x80000001	/* PED sta chiudendo lo schermo -> chiudi le eventuali finestre */
#define PED_API_SHOW	0x80000002	/* PED ha riaperto lo schermo -> riapri le eventuali finestre */
#define PED_API_KEY	0x80000003	/* L'utente ha battuto un tasto */

/* Messaggi che un cliente puo' spedire al PED */

#define	PED_API_REGISTER		0x90000000	/* Registrami: Arg0 = &struct APIClient */
#define	PED_API_UNREGISTER		0x90000001	/* Togli la registrazione: Arg0 = &struct APIClient */

#define PED_API_ADD_INTERNAL_COMMAND	0x90000002	/* Aggiungi un comando: Arg0 = &struct ArexxExtCmds */
#define PED_API_REM_INTERNAL_COMMAND	0x90000003	/* Rimuovi un comando: Arg0 =&struct ArexxExtCmds */

#define PED_API_GET_ACTIVE_WINDOW	0x90000004	/* Ottieni la finestra attiva */
#define PED_API_GET_WINDOW_LIST		0x90000005	/* Ottieni OpenWindows */
#define PED_API_GET_SCREEN_ADDRESS	0x90000006	/* Ottieni l'indirizzo dello schermo */
#define PED_API_GET_PREFS_ADDRESS	0x90000007	/* Ottieni l'indirizzo delle pref. */
#define PED_API_GET_PUBSCRNAME		0x90000008	/* Ottieni il nome dello schermo pubblico */



/******
 * Definizione struttura per i menu'
 ******/

#define MAXMENUNAME	30

struct MyMenu
{
	char			 Name[MAXMENUNAME+1];
	struct MyItem		*Items;
	struct MyMenu		*NextMenu;
};

struct MyItem
{
	char			 Name[MAXMENUNAME+1];
	char			 Key[MAXMENUNAME+1];
	struct MySub		*Subs;
	struct MyProgram	*Prg;
	UBYTE			 CheckType;	/* NB: DEVE RIMANERE DOPO Prg */
	struct MyItem		*NextItem;
};

struct MySub
{
	char			 Name[MAXMENUNAME+1];
	char			 Key[MAXMENUNAME+1];
	struct MyProgram	*Prg;
	UBYTE			 CheckType;	/* NB: DEVE RIMANERE DOPO Prg */
	struct MySub		*NextSub;
};

#define CHECK_NONE		0
#define CHECK_USEKEYWORDCOL	1
#define CHECK_USECOMMENTCOL	2
#define CHECK_ERASERIGHT	3
#define CHECK_SHOWCURPOS	4
#define CHECK_SHOWPATHNAME	5
#define CHECK_AUTOARRANGEVER	6
#define CHECK_AUTOARRANGEHOR	7
#define CHECK_ARRANGEFAVOURITE	8
#define CHECK_USEBACKUP		9
#define CHECK_SAFESAVE		10
#define CHECK_UNFOLDGOTOMARKER	11
#define CHECK_UNFOLDGOTOLINE	12
#define CHECK_UNFOLDGOTOBYTE	13
#define CHECK_UNFOLDGOTOSCAN	14
#define CHECK_AUTOFOLD		15
#define CHECK_USEKWINDENTATION	16
#define CHECK_FASTLOADING	17
#define CHECK_USECLOCK		18
#define CHECK_BLANKBORDERS	19
#define CHECK_AUTORELOAD	20
#define CHECK_USEMOUSEBLANKER	21
#define CHECK_USEAPPICON	22
#define CHECK_USEAPPITEM	23
#define CHECK_SETDEFPUBSCR	24
#define CHECK_USETEMPLATE	25
#define CHECK_USEAUTOCASE	26
#define CHECK_NUM		27


/******
 * Definizione struttura per i tasti
 ******/

struct MyKey
{
	ULONG			 Qual,
				 Code;
	struct MyProgram	*Prg;
	struct MyKey		*NextKey;
};



/******
 * Definizione struttura evento (per UNDO)
 ******/

struct Event
{
	UBYTE		 Type;
	UBYTE		 Arg1;
	char		*Arg2;
	ULONG		 Arg3,
			 Arg4,
			 Arg5,
			 Arg6,
			 Arg7;
	LONG		 Col,
			 Line;
};



/******
 * Struttura per i folder
 ******/

#define MAXFOLDNAME	49

struct Folder
{
	char		 Name[MAXFOLDNAME+1];
	long		 Num;
	struct Line	*Line;
	long		 Col;
	struct Line	*Start,
			*End;

	struct Folder	*NextFolder;
};



/******
 * Struttura per i marker
 ******/

struct Marker
{
	struct Line	*Line;
	long		 Col;
};



/******
 * Struttura per i comandi arexx (INTERNI/ESTERNI),relativi template e valori di default
 ******/

struct ArexxExtCmds
{
	UBYTE			 External;
	char			*Name;
	char			*Template;
	void			*Defaults[MAXREXXARGS];
	LONG ASM		(*CommFunc)( RG(a0) struct CommandData *);
	struct ArexxExtCmds	*NextCmd;
};



/******
 * Definizione struttura finestre
 ******/

struct PEDWindow
{
	struct Window	*Window;	/* Puntatore alla finestra reale */

	struct Gadget	*GadgetScrollX,	/* Puntatori ai gadget di scorrimento */
			*GadgetScrollY,
			*GadgetUpArrow,
			*GadgetDownArrow,
			*GadgetLeftArrow,
			*GadgetRightArrow;

	struct Image	*ImageUpArrow,	/* Immagini delle quattro frecce */
			*ImageDownArrow,
			*ImageLeftArrow,
			*ImageRightArrow;

	int		 OldSx,		/* Posizione precedente allo scroll */
			 OldSy,
			 Sx,		/* Posizione schermo attuale */
			 Sy;

	struct Line	*FirstLine,	/* Prima linea testo */
			*LastLine;	/* Ultima linea testo */

	LONG		 MaxCol,	/* copia di prefs.MaxCol */
			 NumCol,	/* # colonne nella finestra */
			 MaxLine,	/* # di linee nel testo */
			 NumLine;	/* # di linee nella finestra */

	ULONG		 Size;		/* # bytes occupati dal testo (e' necessario aggiungere n.linee) */

	WORD		 Left,		/* Variabili temporanee. Mantengono */
			 Top,		/* le dimensioni e posizione */
			 Width,		/* della finestra mentre lo schermo */
			 Height;	/* viene chiuso e poi riaperto */

	WORD		 ZoomInfo[4];	/* Info per lo zoom della finestra */

	char		 FileName[200];	/* Nome (+Path) del file contenuto nella finestra */
	char		 Title[200];	/* Titolo finestra (nomefile o "Untitled" ecc */

	LONG		 CursorCol;
	LONG		 CursorLine;
	LONG		 LastCursorCol;	/* Ultima colonna in cui abbiamo scritto qualcosa */

	struct Line	*Line;		/* puntatore alla linea del cursore */
	UWORD		 RealNumCol;	/* # colonne REALI (strlen()) */
	UWORD		 LogicNumCol;	/* # colonne LOGICO (NumCol()) */

	/* Dati di comodo */
	UWORD		 FWidth,
			 FHeight,
			 FBase,
			 WOffsetX,
			 WOffsetY;
	struct RastPort *RP;

	/* Variabili relative alla situazione attuale */
	UBYTE		 Ins;

	UBYTE		 MLine,		/* TRUE includi tutta la linea iniziale e finale */
			 Block;		/* Sto marcando qualcosa ? 0=NO,1=BLOCCO,2=BLOCCO COL. */
	LONG		 BlockLine,	/* Inizio blocco marcato */
			 BlockCol;

	ULONG		 Changes;	/* Cambi dall'ultimo salvataggio */

	/* Coda circolare per l'UNDO */

	struct Event	 *Events;
	LONG		 LastEvent;
	ULONG		 NumEvent,
			 NumRedo,
			 UndoLevels;

	/* Cut linee */

	char		*CuttedLine;

	/* Marcatori */

	struct Marker	Marker[MAXMARKER];

	/* Fold */

	struct Folder	*Folder;
	int		 LinesFolded;
	int		 NumFolds;

	/* TRUE=Finestra chiusa */

	UBYTE		 Frozen;

	/* TRUE=Finestra lockata (dall'handler) */

	ULONG		 Locked;

	/* Contatore autosave (in secondi) */

	ULONG		 AutoSave;

	/* Dati utili SOLO durante il load dal PED_DISK */

	struct Line	*OldLine;
	UBYTE		*LineBuffer,
			*Pun,
			 InsLine,
			 Warn;

	/* Dati utili per i template (ricordano gli ultimi key inseriti) */

	UBYTE			 OldKeys[MAXTEMPLATE];
	UBYTE			 PunOldKeys;

	/* Ultima modifica */

	struct DateStamp	DateStamp;

	/* LINK */

	struct PEDWindow	*NextWindow;

	/* Dati utente */

	ULONG			 UserData[16];
};



/******
 * Definizione struttura linea
 ******/

struct Line
{
	char		*Buffer;
	struct Line	*PrevLine,
			*NextLine;
	struct Folder	*Folder;
};



/******
 * Definizione struttura preferenze
 ******/

#define MAXCOMMENT	9
#define MAXFONTNAM	49
#define MAXCOMMENT2	79
#define MAXSTARTUPMACRO	99
#define MAXREFNAME	99
#define MAXFOLDFUNCNAME	29

struct Prefs
{
	ULONG		ScreenMode;		/* Display ID per lo schermo pubblico */
	UWORD		ScreenWidth;		/* Larghezza schermo */
	UWORD		ScreenHeight;		/* Altezza schermo */
	UWORD		ScreenOverscanType;	/* Overscan */
	UBYTE		ScreenDepth;		/* N.ro colori */
	UBYTE		ScreenAutoScroll;	/* Autoscroll SI/NO */

	UWORD		ScreenColors[MAXCOLOR];	/* Primi MAXCOLOR colori */

	char		ScreenFontName[MAXFONTNAM+1];/* Font per disegnare il testo dello schermo*/
	UWORD		ScreenFontSize;		/* Dim. Font per disegnare i testo dello sc. */

	char		MenuFontName[MAXFONTNAM+1];/* Font per disegnare i menu' */
	UWORD		MenuFontSize;		/* Dim. Font per disegnare i menu' */

	char		TextFontName[MAXFONTNAM+1];/* Font per disegnare il testo */
	UWORD		TextFontSize;		/* Dim. Font per disegnare il testo */

	char		GadgetFontName[MAXFONTNAM+1];/* Font per disegnare i gadgets */
	UWORD		GadgetFontSize;		/* Dim. Font per disegnare i gadgets */

	UWORD		MaxCol;			/* Colonne massime */

	UBYTE		AltStep;		/* Salto righe CURSOR+ALT */

	UBYTE		Tab;			/* Dimensione tab. in caratteri */

	UBYTE		LeftBorder;		/* Bordi Scroll */
	UBYTE		RightBorder;
	UBYTE		TopBorder;
	UBYTE		BottomBorder;

	UBYTE		UseKeyWordColors;	/* Abilita uso colori X per keywords */
	UBYTE		KeyAlpha[256];		/* Tabella relativa */
	char		**KeyWord;		/* puntatore ad array di keyword */
	int		NumKeyWord;		/* # keyword nell'array */
	char		GestColorOff,		/* caratteri on/off gestione colore */
			GestColorOn;

	UBYTE		UseCommentColors;	/* Abilita uso colori X i commmenti */
	UWORD		CheckColorLines;	/* MAX # di linee da controlare per ottenere
							il colore dei commenti 0=TUTTE */
	UBYTE		Comment1Pen,
			Comment2Pen;		/* penne commenti */
	char		Comment1On[MAXCOMMENT+1],
			Comment1Off[MAXCOMMENT+1],
			Comment2On[MAXCOMMENT+1];

	UBYTE		BackPen;		/* penna sfondo schermo */
	UBYTE		PaperPen;		/* penna carta */
	UBYTE		DefTextPen;		/* penna testo normale */
	UBYTE		PaperBlockPen;		/* penna carta blocchi */
	UWORD		ScreenPens[13];		/* Penne schermo (v39=12 penne + 1 ~0 ) */

	UBYTE		Clipboard;		/* Clipboard standard */

	UBYTE		EraseRight;		/* Cancella a destra prima di un CR ? */

	UBYTE		ShowCursorPos;		/* Mostra linea/col nel titolo */
	UBYTE		ShowPathName;		/* Mostra path sul titolo */

	UBYTE		AutoArrangeVer;		/* Arrangia le finestre ad ogni apertura (VER) */
	UBYTE		AutoArrangeHor;		/* Arrangia le finestre ad ogni apertura (HOR) */
	UBYTE		Favourite;		/* Favorisci quella attiva */

	UBYTE		UseSetDefPubScr;	/* Setta come schermo pubblico di default */

	UBYTE		UseClock;		/* Crea l'orologio */

	UBYTE		Backup;			/* Crea backup al salvataggio */
	UBYTE		Save;			/* Tipo di salvataggio sicuro */
	UBYTE		SaveRefs;		/* Salva anche i riferimenti ? */

	UBYTE		UnfoldOnGotoMarker;	/* Unfold al goto-marker */
	UBYTE		UnfoldOnGotoLine;	/* Unfold alla ricerca linea */
	UBYTE		UnfoldOnGotoByte;	/* Unfold alla ricerca byte */
	UBYTE		UnfoldOnGotoScan;	/* Unfold per gli scan */

	UBYTE		BlankBorders;		/* Bordi neri ? */

	UBYTE		AutoFold;		/* Fold-all al caricamento */

	char		SearchFoldFileName[MAXFOLDFUNCNAME+1];	/* File funzione cerca-FOLD */

	LONG		DefBits;		/* Bits per i file */
	char		DefComment[MAXCOMMENT2+1];/* Commento-default */
	UBYTE		CreateIcons;		/* Crea icone */

	UBYTE		UseKeyWordInd;		/* Abilita indentazione per keywords */
	char		**KeyWordInd;		/* puntatore ad array di keyword */
	int		NumKeyWordInd;		/* # keyword nell'array */
	UWORD		IndJump;		/* Colonne per livello d'indentazione (pref. =TAB) */

	UBYTE		SeparateItems;		/* Separa gli item con stringa="" */
	struct MyMenu	*Menu;			/* Menu */
	char		HelpFile[MAXFILENAME+1];/* Nome file guida */

	struct MyKey	*Key;			/* Tasti */

	UBYTE		UseAppIcon;		/* Usa app-icon all'iconify */

	UBYTE		UseMenuItem;		/* Usa menu-item all'iconify */

	UBYTE		UseAutoSave;		/* Attiva auto-save */
	UWORD		MinAutoSave;		/* Minuti tra gli auto-save */

	UBYTE		AutoReLoad;		/* Ricarica ultimi file ? */

	UBYTE		MouseBlanker;		/* Attiva mouse-blanker */

	UBYTE		FastLoad;		/* Attiva Fast-loading */

	char		**APIClients;		/* puntatore ad array di nomi eseguibili (clienti) */
	int		NumAPIClients;		/* # puntatori nell'array */

	char		StartUpMacro[MAXSTARTUPMACRO];/* Macro eseguita all'inizio */
	char		PreIconifyMacro[MAXSTARTUPMACRO];/* Macro eseguita prima di iconificarsi */
	char		PostIconifyMacro[MAXSTARTUPMACRO];/* Macro eseguita dopo una deiconificazione */

	struct MyScan	*Scan;			/* Puntatore alla lista di scan disponibili */

      struct MyTemplate	*Template;		/* Puntatore alla lista di template */
	UBYTE		UseTemplate;		/* Attiva i template */

	UBYTE		UseRightGadget;		/* Crea il gadget di scorrimento V */
	UBYTE		UseBottomGadget;	/* Crea il gadget di scorrimento H */

	LONG		LeftExtraSpace,		/* Spazio libero extra per altre finestre */
			RightExtraSpace,
			TopExtraSpace,
			BottomExtraSpace;

	char		**DictWord;		/* puntatore ad array di parole (dizionario) */
	int		NumDictWord;		/* # puntatori nell'array */
	UBYTE		UseAutoCase;		/* Abilita l'autocase */

	UBYTE		CheckPar;		/* Controlla () */

	UBYTE		FastMode;		/* TRUE= FAAAAAASSSSSSTTTTTTT!!!!1!! */

	char		**Ref;			/* Riferimenti */
	int		NumRef;			/* # di riferimenti */
	UBYTE		RAMReference;		/* TRUE=tieni in RAM */
	UBYTE		ReferenceCase;		/* Ricerca CASE-SENSITIVE ? */
	char		ReferenceFile[MAXREFNAME+1];/* nome file */

	char		**HuntPath;		/* Path ricerca files */
	int		NumHuntPath;		/* # di paths */

	UBYTE		UseRTSysReq;		/* Usa ReqTools system req. */
	UBYTE		UseRTFileReq;		/* Usa ReqTools file req. */
	UBYTE		UseRTFontReq;		/* Usa ReqTools font req. */
	UBYTE		UseRTScreenReq;		/* Usa ReqTools screen req. */

	char		**ProjectFiles;		/* Files nel progetto */
	int		NumProjectFiles;	/* # di files */

	UWORD		Left,			/* Margini sinistro e destro */
			Right;			/* per il layoutblock */

	char		XpkLib[5];		/* Libreria XPK scelta */
	UBYTE		XpkMode;		/* Metodo 0..100 */
	int		XpkPos;			/* Posizione nella lista */
	char		XpkPassWord[52];	/* PassWord XPK */

        ULONG		UndoLevels;		/* Livelli di UNDO (min.1) */
};



