initial commit
[windtk] / src / windtk.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include "wtimpl.h"
4
5 void *(*wt_alloc)(size_t sz) = malloc;
6 void (*wt_free)(void *p) = free;
7
8 /* one day I may want to allow multiple contexts */
9 static struct wt_context defctx;
10 struct wt_context *wt_curctx_ = &defctx;
11
12 static unsigned int def_colors[] = {
13         0xf0f0f0,       /* font/foreground */
14         0x333333,       /* background */
15         0x444444,       /* highlight */
16         0x222222,       /* 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 */
23         0
24 };
25
26 void *wt_zalloc(size_t sz)
27 {
28         void *p = wt_alloc(sz);
29         if(p) {
30                 memset(p, 0, sz);
31         }
32         return p;
33 }
34
35 void wt_allocator(void *(*allocfunc)(size_t), void (*freefunc)(void *p))
36 {
37         wt_alloc = allocfunc ? allocfunc : malloc;
38         wt_free = freefunc ? freefunc : free;
39 }
40
41
42 int wt_init(int w, int h, struct wt_graphics *gfx)
43 {
44         if(!(wt->root = wt_alloc_widget(0))) {
45                 return -1;
46         }
47
48         wt_viewport(0, 0, w, h);
49         wt_graphics(gfx);
50         return 0;
51 }
52
53 void wt_destroy(void)
54 {
55         wt_free_tree(wt->root);
56         wt->root = 0;
57 }
58
59 void wt_viewport(int x, int y, int w, int h)
60 {
61         wt_setrect(&wt->vp, x, y, w, h);
62         wt_move(wt->root, x, y);
63         wt_resize(wt->root, w, h);
64 }
65
66 void wt_graphics(struct wt_graphics *gfx)
67 {
68         int i, r, g, b;
69         wt->gfx = *gfx;
70
71         for(i=0; i<NUM_COLORS; i++) {
72                 r = def_colors[i] >> 16;
73                 g = (def_colors[i] >> 8) & 0xff;
74                 b = def_colors[i] & 0xff;
75                 if((wt->colors[i] = gfx->newcolor(r, g, b)) == -1) {
76                         wt->colors[i] = i ? wt->colors[i - 1] : 0;
77                 }
78         }
79 }
80
81 void wt_inp_key(int key, int press)
82 {
83 }
84
85 void wt_inp_mouse(int bn, int st, int x, int y)
86 {
87 }
88
89 void wt_inp_motion(int x, int y)
90 {
91 }
92
93 void wt_gfx_color(int cidx)
94 {
95         wt->gfx.color(wt->colors[cidx]);
96 }
97
98 void wt_gfx_fillrect(struct wt_rect *r)
99 {
100         wt->gfx.fillrect(r);
101 }
102
103 void wt_gfx_frame(struct wt_rect *r, int style, int basecol)
104 {
105         wt_gfx_color(basecol);
106         wt_gfx_fillrect(r);
107         wt_gfx_color(style == FRM_OUT ? basecol + 2 : basecol + 1);
108         wt_gfx_line(r->x, r->y + r->height, r->x + r->width - 1, r->y + r->height);
109         wt_gfx_line(r->x + r->width, r->y + 1, r->x + r->width, r->y + r->height);
110         wt_gfx_color(style == FRM_OUT ? basecol + 1 : basecol + 2);
111         wt_gfx_line(r->x, r->y, r->x + r->width, r->y);
112         wt_gfx_line(r->x, r->y + 1, r->x, r->y + r->height - 1);
113 }
114
115 void wt_gfx_line(int x0, int y0, int x1, int y1)
116 {
117         wt->gfx.line(x0, y0, x1, y1);
118 }
119
120 void wt_setrect(struct wt_rect *r, int x, int y, int w, int h)
121 {
122         r->x = x;
123         r->y = y;
124         r->width = w;
125         r->height = h;
126 }