5 void *(*ag_alloc)(size_t sz) = malloc;
6 void (*ag_free)(void *p) = free;
8 /* one day I may want to allow multiple contexts */
9 static struct ag_context defctx;
10 struct ag_context *ag_curctx_ = &defctx;
12 static unsigned int def_colors[] = {
13 0xf0f0f0, /* font/foreground */
14 0x444444, /* background */
15 0x555555, /* highlight */
16 0x333333, /* shadow */
17 0x403065, /* inactive frame */
18 0x54349c, /* inactive frame highlight */
19 0x221e2c, /* inactive frame shadow */
20 0x8032aa, /* active frame */
21 0xb14de8, /* active frame highlight */
22 0x3b2846, /* active frame shadow */
26 void *ag_zalloc(size_t sz)
28 void *p = ag_alloc(sz);
35 void ag_allocator(void *(*allocfunc)(size_t), void (*freefunc)(void *p))
37 ag_alloc = allocfunc ? allocfunc : malloc;
38 ag_free = freefunc ? freefunc : free;
42 int ag_init(int w, int h, struct ag_graphics *gfx)
45 if(!(ag->root = ag_alloc_widget(0))) {
49 ag_viewport(0, 0, w, h);
56 ag_free_tree(ag->root);
60 struct ag_theme *ag_load_theme(const char *path)
65 void ag_unload_theme(struct ag_theme *theme)
69 static void use_theme(ag_widget *w, struct ag_theme *theme)
74 w->use_theme(w, theme);
77 for(i=0; i<w->num_child; i++) {
78 use_theme(w->child[i], theme);
82 void ag_use_theme(struct ag_theme *theme)
85 use_theme(ag->root, theme);
88 void ag_viewport(int x, int y, int w, int h)
90 ag_rect(&ag->vp, x, y, w, h);
91 ag_move(ag->root, x, y);
92 ag_resize(ag->root, w, h);
95 void ag_graphics(struct ag_graphics *gfx)
98 memcpy(ag->colors, def_colors, sizeof ag->colors);
101 void ag_inp_key(int key, int press)
105 void ag_inp_mouse(int bn, int st, int x, int y)
109 void ag_inp_motion(int x, int y)
113 void ag_draw_tree(ag_widget *tree)
118 tree->draw(tree, &ag->gfx);
121 for(i=0; i<tree->num_child; i++) {
122 ag_draw_tree(tree->child[i]);
128 ag_draw_tree(ag->root);
131 void ag_gfx_color(int cidx)
133 int r = (ag->colors[cidx] >> 16) & 0xff;
134 int g = (ag->colors[cidx] >> 8) & 0xff;
135 int b = ag->colors[cidx] & 0xff;
136 ag->gfx.color(r, g, b);
139 void ag_gfx_fillrect(struct ag_rect *r)
144 void ag_gfx_fillrect4i(int x, int y, int w, int h)
147 ag_rect(&r, x, y, w, h);
148 ag->gfx.fillrect(&r);
151 void ag_gfx_frame(struct ag_rect *r, int style, int basecol)
153 if((style & FRM_NOFILL) == 0) {
154 ag_gfx_color(basecol);
157 ag_gfx_color(FRMSTYLE(style) == FRM_OUT ? basecol + 2 : basecol + 1);
158 ag_gfx_fillrect4i(r->x + 1, r->y + r->h - 1, r->w - 2, 1);
159 ag_gfx_fillrect4i(r->x + r->w - 1, r->y, 1, r->h);
160 ag_gfx_color(FRMSTYLE(style) == FRM_OUT ? basecol + 1 : basecol + 2);
161 ag_gfx_fillrect4i(r->x + 1, r->y, r->w - 2, 1);
162 ag_gfx_fillrect4i(r->x, r->y, 1, r->h);
165 void ag_gfx_line(int x0, int y0, int x1, int y1)
167 ag->gfx.line(x0, y0, x1, y1);
170 void ag_rect(struct ag_rect *r, int x, int y, int w, int h)
178 void ag_rect_union(struct ag_rect *a, struct ag_rect *b)
185 if(b->x < a->x) a->x = b->x;
186 if(b->y < a->y) a->y = b->y;
187 if(b->x + b->w > x1) x1 = b->x + b->w;
188 if(b->y + b->h > y1) y1 = b->y + b->h;
194 int ag_rect_overlap(struct ag_rect *a, struct ag_rect *b)
196 if(a->x > b->x + b->w) return 0;
197 if(b->x > a->x + a->w) return 0;
198 if(a->y > b->y + b->h) return 0;
199 if(b->y > a->x + a->h) return 0;