shithub: libnate

ref: 08aef1095811c8f436a469208b088002e8f4ed21
dir: /nate.h/

View raw version
#pragma lib "libnate.a"

typedef struct Nelem Nelem;
typedef struct Nslot Nslot;
typedef struct Nelemaccessors Nelemaccessors;
typedef struct Nelemfunctions Nelemfunctions;
typedef struct Nlist Nlist;
typedef struct Nlistelem Nlistelem;

/**************/
/* list types */
/**************/

struct Nlist {
	Nlistelem* first;
	int num;
};
struct Nlistelem {
	Nelem* item;
	Nlistelem* next;
};

/****************/
/* common types */
/****************/

typedef int (*OnclickHandler)(Mouse, Nelem*, void*);
typedef char* (*StringGetter)(void);

typedef struct Nmargin Nmargin;

struct Nmargin {
	int left;
	int top;
	int right;
	int bottom;
};

typedef enum {
	TOPLEFT,
	TOP,
	TOPRIGHT,
	LEFT,
	CENTER,
	RIGHT,
	BOTLEFT,
	BOTTOM,
	BOTRIGHT,
} Nalign;

#define NMargin(l, t, r, b) ((Nmargin){l, t, r, b})
#define NMargin2(x, y) ((Nmargin){x, y, x, y})

Rectangle insetmargin(Rectangle, Nmargin);
Rectangle extendmargin(Rectangle, Nmargin);

/******************/
/* user functions */
/******************/

typedef enum {
	DRAW = 1,
	RESIZE = 2,
} Dirtyflag;

void nateinit(void);
void nateredraw(Dirtyflag flags);
void ndebugprint(Nelem*, char*, ...);

int natemouseevent(Mouse);

#define NAssign(T, A, B) ((T*)(nassign((Nelem**)A, B)))
Nelemaccessors* nassign(Nelem**, Nelemaccessors*);

extern int nateborders;
extern int natetracehit;
extern int natetracedraw;
extern int natedebugfd;

struct {
	Image *red;
	Image *green;
	Image *blue;
} ncolor;


/**********************/
/* end user functions */
/**********************/

#define FILLX (1<<0)
#define FILLY (1<<1)

struct Nslot {
	Rectangle r;
	Nalign align;
	int fill;
};

Nslot NSlot(void);
Nslot NSlotx(Nalign, int);

struct Nelemaccessors {
	char *type;
};
struct Nelem {
	char *type;
	char *name;
	Nslot slot;
	Dirtyflag dirty;
	Nlist children;
	int nchildren; /* -1=inf, 0=none, n otherwise */
	Nelemaccessors *accs;
	Nelemfunctions *funcs;
};
struct Nelemfunctions {
	Rectangle (*calcrect)(Nelem*, Image*, Rectangle);
	Point (*desiredsize)(Nelem*, Image*);
	void (*draw)(Nelem*, Image*);
	Nelem* (*checkhit)(Nelem*, Image*, Mouse);
	int (*hit)(Nelem*, Mouse);
	
	void (*free)(Nelem*);
	Nlist* (*getchildren)(Nelem*);
	
	char* (*getname)(Nelem*);
};

/*******************/
/* nlist functions */
/*******************/

void linit(Nlist* list);
int  lhas(Nlist* list, Nelem* item);
void ladd(Nlist* list, Nelem* item);
void lins(Nlist* list, Nelem* item);
void ldel(Nlist* list, Nelem* item);
void linsert(Nlist* list, Nelem* before, Nelem* item);
void lforeach(Nlist* list, void (*f)(Nelem*, int, void*), void*);
void lfreelist(Nlist* list);

Nelem* lgetfirst(Nlist* list);
Nelem* lsetfirst(Nlist* list, Nelem* item);


/********************/
/* helper functions */
/********************/

// calls on Nelem
Rectangle ncallcalcrect(Nelem*, Image*, Rectangle);
Point ncalldesiredsize(Nelem*, Image*);
void ncalldraw(Nelem*, Image*);
Nelem* ncallcheckhit(Nelem*, Image*, Mouse);
int ncallhit(Nelem*, Mouse);
void ncallfree(Nelem*);
Nlist* ncallgetchildren(Nelem*);

void nsetdirty(Nelem*, Dirtyflag);


/***********************/
/* root set management */
/***********************/

// register Nelem as root
void nregister(Nelem*);
// deregister Nelem as root (free for collection)
void nderegister(Nelem*);
// true if Nelem is root
int nisroot(Nelem*);

// register root element
void nregroot(Nelem*);


/************************/
/* more syntactic sugar */
/************************/

#define DECL_SLOTFUNC(Acc) NACCS* (*Acc)(Nslot, Nelemaccessors*)
#define DECL_ACCESSOR(Acc) NACCS* (*Acc)(void)
#define DECL_ACCESSOR_OneParam(Acc, T1) NACCS* (*Acc)(T1)
#define DECL_ACCESSOR_TwoParams(Acc, T1, T2) NACCS* (*Acc)(T1, T2)
#define DECL_ACCESSOR_ThreeParams(Acc, T1, T2, T3) NACCS* (*Acc)(T1, T2, T3)