wt_draw
[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         wt->root = 0;
45         if(!(wt->root = wt_alloc_widget(0))) {
46                 return -1;
47         }
48
49         wt_viewport(0, 0, w, h);
50         wt_graphics(gfx);
51         return 0;
52 }
53
54 void wt_destroy(void)
55 {
56         wt_free_tree(wt->root);
57         wt->root = 0;
58 }
59
60 void wt_viewport(int x, int y, int w, int h)
61 {
62         wt_setrect(&wt->vp, x, y, w, h);
63         wt_move(wt->root, x, y);
64         wt_resize(wt->root, w, h);
65 }
66
67 void wt_graphics(struct wt_graphics *gfx)
68 {
69         int i, r, g, b;
70         wt->gfx = *gfx;
71
72         for(i=0; i<NUM_COLORS; i++) {
73                 r = def_colors[i] >> 16;
74                 g = (def_colors[i] >> 8) & 0xff;
75                 b = def_colors[i] & 0xff;
76                 if((wt->colors[i] = gfx->newcolor(r, g, b)) == -1) {
77                         wt->colors[i] = i ? wt->colors[i - 1] : 0;
78                 }
79         }
80 }
81
82 void wt_inp_key(int key, int press)
83 {
84 }
85
86 void wt_inp_mouse(int bn, int st, int x, int y)
87 {
88 }
89
90 void wt_inp_motion(int x, int y)
91 {
92 }
93
94 void wt_draw_tree(wt_widget *tree)
95 {
96         int i;
97
98         if(tree->draw) {
99                 tree->draw(tree);
100         }
101
102         for(i=0; i<tree->num_child; i++) {
103                 wt_draw_tree(tree->child[i]);
104         }
105 }
106
107 void wt_draw(void)
108 {
109         wt_draw_tree(wt->root);
110 }
111
112 void wt_gfx_color(int cidx)
113 {
114         wt->gfx.color(wt->colors[cidx]);
115 }
116
117 void wt_gfx_fillrect(struct wt_rect *r)
118 {
119         wt->gfx.fillrect(r);
120 }
121
122 void wt_gfx_frame(struct wt_rect *r, int style, int basecol)
123 {
124         wt_gfx_color(basecol);
125         wt_gfx_fillrect(r);
126         wt_gfx_color(style == FRM_OUT ? basecol + 2 : basecol + 1);
127         wt_gfx_line(r->x, r->y + r->height, r->x + r->width - 1, r->y + r->height);
128         wt_gfx_line(r->x + r->width, r->y + 1, r->x + r->width, r->y + r->height);
129         wt_gfx_color(style == FRM_OUT ? basecol + 1 : basecol + 2);
130         wt_gfx_line(r->x, r->y, r->x + r->width, r->y);
131         wt_gfx_line(r->x, r->y + 1, r->x, r->y + r->height - 1);
132 }
133
134 void wt_gfx_line(int x0, int y0, int x1, int y1)
135 {
136         wt->gfx.line(x0, y0, x1, y1);
137 }
138
139 void wt_setrect(struct wt_rect *r, int x, int y, int w, int h)
140 {
141         r->x = x;
142         r->y = y;
143         r->width = w;
144         r->height = h;
145 }