dirty redraw and SDL framebuffer example
[windtk] / src / windtk.h
1 #ifndef WINDTK_H_
2 #define WINDTK_H_
3
4 enum {
5         WT_TYPE_WIDGET,
6         WT_TYPE_WINDOW,
7         WT_TYPE_LABEL,
8         WT_TYPE_BUTTON,
9         WT_TYPE_CHECKBOX,
10         WT_TYPE_TEXTFIELD
11 };
12
13 enum {
14         WT_KEY_UP = 0x100,
15         WT_KEY_DOWN,
16         WT_KEY_LEFT,
17         WT_KEY_RIGHT,
18         WT_KEY_HOME,
19         WT_KEY_END,
20         WT_KEY_PGUP,
21         WT_KEY_PGDN
22 };
23
24 enum {
25         WT_WS_DEFAULT,
26         WT_WS_NOFRM
27 };
28
29 enum {
30         WT_CB_FOCUS,
31         WT_CB_UNFOCUS,
32         WT_CB_MODIFY,
33         WT_CB_CLICK,
34         WT_CB_MBUTTON,
35         WT_CB_MMOTION,
36
37         WT_NUM_CALLBACKS
38 };
39
40 typedef struct wt_widget wt_widget;
41 typedef void (*wt_callback_func)(wt_widget*, void*);
42
43 struct wt_image {
44         int width, height;
45         int bpp;
46         int pitch;
47         void *pixels;
48         void *udata;
49 };
50
51 struct wt_rect {
52         int x, y, w, h;
53 };
54
55 /* graphics flags */
56 enum {
57         WT_GFX_RGB              = 1,    /* RGB mode, calls newcolor to set current, not to allocate */
58         WT_GFX_NODIRTY  = 2             /* don't use dirty rects, always redraw completely */
59 };
60
61 struct wt_graphics {
62         unsigned int flags;
63
64         int (*newcolor)(int r, int g, int b);
65         void (*color)(int c);
66
67         void (*fillrect)(struct wt_rect *rect);
68         void (*line)(int x0, int y0, int x1, int y1);
69
70         int (*newimage)(struct wt_image *img);
71         void (*blit)(int img, int x, int y, int w, int h);
72
73         void (*text)(int font, const char *s, int x, int y);
74         void (*textbox)(int font, const char *s, int x, int y, struct wt_rect *boxret);
75         int (*lineheight)(int font);
76         int (*baseline)(int font);
77 };
78
79 typedef void (*wt_draw_func)(wt_widget*, struct wt_graphics*);
80
81 struct wt_theme {
82         char *name;
83         void *so;
84         wt_draw_func draw_window;
85         wt_draw_func draw_label;
86         wt_draw_func draw_button;
87         wt_draw_func draw_checkbox;
88         wt_draw_func draw_textfield;
89
90         struct wt_theme *next;
91 };
92
93 void wt_allocator(void *(*allocfunc)(size_t), void (*freefunc)(void*));
94
95 int wt_init(int w, int h, struct wt_graphics *gfx);
96 void wt_destroy(void);
97
98 struct wt_theme *wt_load_theme(const char *path);       /* load dynamically, where applicable */
99 void wt_unload_theme(struct wt_theme *theme);
100 void wt_use_theme(struct wt_theme *theme);
101
102 void wt_viewport(int x, int y, int w, int h);
103 void wt_graphics(struct wt_graphics *gfx);
104
105 void wt_inp_key(int key, int press);
106 void wt_inp_mouse(int bn, int st, int x, int y);
107 void wt_inp_motion(int x, int y);
108
109 void wt_draw(void);
110
111 /* screen regions updated by the last wt_draw */
112 int wt_num_upd(void);
113 struct wt_rect *wt_upd_rect(int idx);
114 void wt_add_upd_rect(struct wt_rect *r);
115
116 wt_widget *wt_alloc_widget(wt_widget *par);
117 void wt_free_widget(wt_widget *w);
118 void wt_free_tree(wt_widget *tree);
119
120 void wt_dirty_widget(wt_widget *w);             /* propagates to children */
121
122 wt_widget *wt_window(wt_widget *par, const char *title, int style, int x, int y, int width, int height);
123 wt_widget *wt_label(wt_widget *par, const char *text, int x, int y);
124 wt_widget *wt_button(wt_widget *par, const char *text, int x, int y, int width, int height);
125 wt_widget *wt_button_cb(wt_widget *par, const char *text, int x, int y, int width,
126                 int height,     wt_callback_func cbclick, void *cls);
127 wt_widget *wt_checkbox(wt_widget *par, const char *text, int chk, int x, int y, int width, int height);
128 wt_widget *wt_checkbox_cb(wt_widget *par, const char *text, int chk, int x, int y,
129                 int width, int height, wt_callback_func cbtoggle, void *cls);
130 wt_widget *wt_textfield(wt_widget *par, const char *text, int x, int y, int width, int height);
131
132 int wt_type(wt_widget *w);
133
134 int wt_set_text(wt_widget *w, const char *text);
135 const char *wt_text(wt_widget *w);
136
137 int wt_add_child(wt_widget *w, wt_widget *c);
138 int wt_remove_child(wt_widget *w, wt_widget *c);
139 wt_widget *wt_parent(wt_widget *w);                     /* parent widget */
140 wt_widget *wt_widget_window(wt_widget *w);      /* first ancestor of type window */
141 int wt_child_count(wt_widget *w);                       /* number of children */
142 wt_widget *wt_child(wt_widget *w, int idx);     /* get child idx */
143
144 void wt_move(wt_widget *w, int x, int y);
145 void wt_resize(wt_widget *w, int x, int y);
146 int *wt_position(wt_widget *w, int *xret, int *yret);
147 int *wt_size(wt_widget *w, int *xret, int *yret);
148
149 int wt_hittest(wt_widget *w, int x, int y);
150 wt_widget *wt_widget_at(int x, int y);
151
152 void wt_layout(wt_widget *w, int layout);
153 void wt_padding(wt_widget *w, int pad);
154 /* calculates layout of child widgets and updates dimensions */
155 void wt_relayout(wt_widget *w);
156
157 void wt_focus(wt_widget *w);
158 void wt_unfocus(wt_widget *w);
159 int wt_isfocused(wt_widget *w);
160
161 void wt_hover(wt_widget *w);
162 void wt_unhover(wt_widget *w);
163 int wt_ishover(wt_widget *w);
164
165 void wt_enable(wt_widget *w);
166 void wt_disable(wt_widget *w);
167 int wt_isenabled(wt_widget *w);
168
169 void wt_callback(wt_widget *w, int type, wt_callback_func func, void *cls);
170
171 void wt_rect(struct wt_rect *r, int x, int y, int w, int h);
172 void wt_rect_union(struct wt_rect *a, struct wt_rect *b);
173 int wt_rect_overlap(struct wt_rect *a, struct wt_rect *b);
174
175 #endif  /* WINDTK_H_ */