#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dos/dos.h>
#include <dos/datetime.h>
#include <libraries/gadtools.h>
#include <workbench/startup.h>
#include <exec/types.h>
#include <exec/memory.h>
#include <graphics/gfxbase.h>
#include <intuition/intuitionbase.h>
#include <intuition/intuition.h>
#include <intuition/gadgetclass.h>
#include <intuition/classusr.h>
#include <intuition/icclass.h>
#include <datatypes/datatypes.h>
#include <datatypes/datatypesclass.h>
#include <clib/intuition_protos.h>
#include <clib/gadtools_protos.h>
#include <clib/wb_protos.h>
#include <clib/datatypes_protos.h>
#include <clib/utility_protos.h>
#include <clib/asl_protos.h>
#include <clib/diskfont_protos.h>
#include <clib/icon_protos.h>
#include <clib/exec_protos.h>
#include <clib/graphics_protos.h>
#include <clib/locale_protos.h>
#include <clib/dos_protos.h>
#include <libraries/locale.h>

#include "list.h"
#include "strutture_define.h"
#include "amytree_func.h"


struct TextFont *diskfont;

struct Window *w=NULL,*vis=NULL, *cer=NULL;
struct IntuiText itxt;
struct IntuiMessage *imsg;
struct AppWindow *apw=NULL;
struct AppMessage *amsg;
struct MsgPort *port=NULL;
struct WBArg *argptr;
struct Library *WorkbenchBase,*DataTypesBase,*AslBase,*IconBase, *LocaleBase;
struct Catalog *catalog=NULL;
struct IntuitionBase *IntuitionBase=NULL;
struct GfxBase *GfxBase;
struct Library *GadToolsBase=NULL;
struct Library *UtilityBase=NULL;
struct Screen *PubScreen;
struct Menu *MyMenu;
struct MenuItem *MItem;
struct TagItem *tstate,*tags,*tag;
struct FileRequester *FileReqLoad;
struct FileRequester *FileReqSave;
struct FileRequester *FileGenTree;
struct FileRequester *FileVis;
struct FontRequester *FontReq;

char *MyFL[4]={"Select","Tree Fonts","Gadgets Fonts",NULL};

struct TagItem FTags[]={
            ASL_Hail,(ULONG)"AmyTree Fonts",
            ASLFO_MinHeight, 8,
            ASLFO_MaxHeight, 8,
            ASL_FuncFlags, FONF_DRAWMODE,
            ASL_ModeList, (ULONG)MyFL,
            TAG_DONE, NULL
       };

struct TagItem MyTagsVis[]={
   ASLFR_TitleText, (ULONG)"Choose program",
   TAG_DONE
   };
struct TagItem MyTagsLoad[]={
   ASLFR_DoPatterns, TRUE,
   ASLFR_TitleText, (ULONG)"Load Tree",
   ASLFR_InitialPattern, (ULONG)"#?.tree",
   TAG_DONE
   };      
struct TagItem MyTagsSave[]={
   ASLFR_DoSaveMode, TRUE,
   ASLFR_RejectIcons, TRUE,
   ASLFR_TitleText, (ULONG)"Save Tree",
   TAG_DONE
   };      
struct TagItem MyTagsGenTree[]={
   ASLFR_TitleText, (ULONG)"Select ROOT Tree",
//   ASLFR_InitialDrawer, (ULONG)"SYS:", 
   ASLFR_DrawersOnly, TRUE,
   TAG_DONE
   };      


struct NewGadget NewGad, CerGad;
struct Gadget *MyGadList, *PrevGad, *Num_Files, *Num_Dirs, *View_String,
              *ViewButton, *ScrollerVert, *ScrollerOriz, *ApriButton, *ChiudiButton,
              *InfoButton, *CercaButton, *CerGadList, *CerStr, *CerPrev,
              *ShowButton,*UserButton1,*UserButton2,*filelist, 
              *Rec_Lev,*PlayButton,*on_off,*OpenBranch, *SizeGadget,
              *newtree_gad,*dimdir_gad,*clone_gad;

struct Gadget *UserButton5=NULL,*UserButton6=NULL,*UserButton7=NULL,*UserButton8=NULL,
              *UserButton3=NULL,*UserButton4=NULL, *SysButton=NULL, *WorkButton=NULL, *CDButton=NULL,
              *IconButton=NULL;

struct Remember *rem_key=NULL;
struct List lista_files, lista_exts;

Object *dto=NULL,*pvo=NULL,*pho=NULL;

struct TagItem VertMapping[]= {
	{PGA_Top,	DTA_TopVert},
	{PGA_Visible,	DTA_VisibleVert},
	{PGA_Total,	DTA_TotalVert},
	{TAG_DONE,	NULL}
};

struct TagItem HorizMapping[]= {
	{PGA_Top,	DTA_TopHoriz},
	{PGA_Visible,	DTA_VisibleHoriz},
	{PGA_Total,	DTA_TotalHoriz},
	{TAG_DONE,	NULL}
};

struct TextAttr tfont;
struct TextAttr gfont;

struct TextAttr xen8 = { ( STRPTR )"xen.font",
                           8,
                           0x00, 
                           0x41
                       };

typedef struct{ STRPTR description;
                STRPTR type; } desc_type;

struct EasyStruct FATAL_ERROR= { sizeof(struct EasyStruct),
                          0,
                          NULL,
                          "ERROR: %s",
                          "OK" };

struct EasyStruct infofile= { sizeof(struct EasyStruct),
                             0,
                             "File info",
                             "Name        : %s\nSize byte   : %ld\nNum. Blocks : %ld\nDATE        : %s\nTime        : %s\nComment     : %s\nDescription : %s\nType        : %s",
                             "OK" };


/*
	Data for "push_Up" Image
*/


UWORD __chip chiusaData[10] =
{
	/* Plane 0 */
	0xFF80,0x8080,0xBE80,0x8080,0xFF80,
	/* Plane 1 */
	0x0000,0x7F00,0x4100,0x7F00,0x0000
};

struct Image chiusa =
{
	0, 0,		/* LeftEdge, TopEdge */
	9, 5, 2,	/* Width, Height, Depth */
	chiusaData,	/* ImageData */
	0x0003, 0x0000,	/* PlanePick, PlaneOnOff */
	NULL		/* NextImage */
};

UWORD __chip apertaData[10] =
{
	/* Plane 0 */
	0xFF80,0x8880,0xBE80,0x8880,0xFF80,
	/* Plane 1 */
	0x0000,0x7700,0x4100,0x7700,0x0000
};

struct Image aperta =
{
	0, 0,		/* LeftEdge, TopEdge */
	9, 5, 2,	/* Width, Height, Depth */
	apertaData,	/* ImageData */
	0x0003, 0x0000,	/* PlanePick, PlaneOnOff */
	NULL		/* NextImage */
};

UWORD __chip vuotaData[10] =
{
	/* Plane 0 */
	0xFF80,0xD580,0xAA80,0xD580,0xFF80,
	/* Plane 1 */
	0x0000,0x2A00,0x5500,0x2A00,0x0000
};

struct Image vuota =
{
	0, 0,		/* LeftEdge, TopEdge */
	9, 5, 2,	/* Width, Height, Depth */
	vuotaData,	/* ImageData */
	0x0003, 0x0000,	/* PlanePick, PlaneOnOff */
	NULL		/* NextImage */
};

UWORD __chip freData[4] =
{
	/* Plane 0 */
	0x3F80,0x1F00,0x0E00,0x0400
};


struct Image down =
{
	0, 0,		/* LeftEdge, TopEdge */
	10, 4, 2,	/* Width, Height, Depth */
	freData,	/* ImageData */
	0x0001, 0x0000,	/* PlanePick, PlaneOnOff */
	NULL		/* NextImage */
};


UWORD __chip fvData[9] =
{
	/* Plane 0 */
	0x0000,0x2000,0x3000,0x3800,0x3C00,0x3800,0x3000,0x2000,
	0x0000
};

struct Image right =
{
	0, 0,		/* LeftEdge, TopEdge */
	7, 9, 2,	/* Width, Height, Depth */
	fvData,	/* ImageData */
	0x0001, 0x0000,	/* PlanePick, PlaneOnOff */
	NULL		/* NextImage */
};


/*
	Data for "left" Image
*/


UWORD __chip leftData[7] =
{

	0x1000,0x3000,0x7000,0xF000,0x7000,0x3000,0x1000
};

struct Image left =
{
	0, 0,		
	4, 7, 2,	
	leftData,	
	0x0001, 0x0000,	
	NULL		
};

/*
	Data for "up" Image
*/


UWORD __chip upData[4] =
{

	0x1000,0x3800,0x7C00,0xFE00
};

struct Image up =
{
	0, 0,		
	7, 4, 2,	
	upData,	
	0x0001, 0x0000,	
	NULL		
};


/* ---- PROTOTIPI FUNZIONI ALIB ---- */

void NewList( struct List *list );

void print_text(struct RastPort *, struct IntuiText *, char *, int, int, int, int, LONG);
void stampa_wintree(node *,char,node *);
void agg_figlio(node *, node *);
int  del_tree(node *);
void lista(STRPTR);
void crea_gadget(void);
void Num_dirs(short int);
void agg_curskey(node *);
void ERRORE(STRPTR);
desc_type info_file(STRPTR);
void viewfile(STRPTR);
void cerca_node_1(STRPTR, node *);
void max_liv(node *);
void info_window(STRPTR);
node * load_tree(STRPTR);
node * load_tree_new(STRPTR);
void init_wintree(node *, UWORD, UWORD, node *);
void gen_coo(node *);
int  apri_dir(STRPTR, node *, LONG);
int  chiudi_dir(node *);
void agg_scroller(UWORD, node *, node *);
void agg_button(node *);
void show(node *);
STRPTR leftcar(STRPTR,int);
void free_list(void);
STRPTR _fgets(STRPTR buf , int n, FILE *fp);
void   play(node *);
void   leggi_prefs(void);
BOOL   Myreq(STRPTR mes, int x, int y);
void   esegui(STRPTR, short s);
void   clean_up(void);
UBYTE  esp(BPTR lock);
BYTE   espandi_tutto(node *);
int    chiudi_tutto(node *root);
int    crea_listafiles(node *dire);
void   agg_listview(node *dire);
void   listga(char lof, node *);
void   agg_listga(BOOL stato);
void   Ordina_lista(struct List *lista);
void   CloseFont(struct TextFont *); 
void   InfoWindow(void);
void   OrdTree(node *nodo);
STRPTR GetString(struct LocaleInfo *li, LONG stringNum);
void FlushIBuffer(node *root);