shithub: libnate

ref: 228a7b0eeda3531eb0bf0efa2211e4532af69681
dir: /nate.h/

View raw version
#pragma lib "libnate.a"

typedef struct Nelem Nelem;
typedef struct Nelemfunctions Nelemfunctions;
typedef struct Nlist Nlist;


/* 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;
};

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

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

void nateinit(void);
void nateredraw(int all);

int natemouseevent(Mouse);

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

extern int nateborders;
extern int natetracehit;

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


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

struct Nelem {
	char *type;
	char *name;
	Rectangle r;
	Nelemfunctions* funcs;
};
struct Nelemfunctions {
	Rectangle (*calcsize)(Nelem*, Image*, Rectangle);
	void (*draw)(Nelem*, Image*);
	Nelem* (*checkhit)(Nelem*, Image*, Mouse);
	int (*hit)(Nelem*, Mouse);
	
	void (*free)(Nelem*);
	Nlist* (*getchildren)(Nelem*);
	
	char* (*getname)(Nelem*);
};

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

typedef struct Nlistelem Nlistelem;
struct Nlist {
	Nlistelem* first;
};
struct Nlistelem {
	Nelem* item;
	Nlistelem* next;
};

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 ncallcalcsize(Nelem*, Image*, Rectangle);
void ncalldraw(Nelem*, Image*);
Nelem* ncallcheckhit(Nelem*, Image*, Mouse);
int ncallhit(Nelem*, Mouse);
void ncallfree(Nelem*);
Nlist* ncallgetchildren(Nelem*);


/* 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_ACCESSOR(Type, Acc) Type* (*Acc)(void)
#define DECL_ACCESSOR_OneParam(Type, Acc, T1) Type* (*Acc)(T1)
#define DECL_ACCESSOR_TwoParams(Type, Acc, T1, T2) Type* (*Acc)(T1, T2)
#define DECL_ACCESSOR_ThreeParams(Type, Acc, T1, T2, T3) Type* (*Acc)(T1, T2, T3)