dirty redraw and SDL framebuffer example
[windtk] / src / window.c
1 #include "wtimpl.h"
2
3 static void draw_win(wt_widget *w, struct wt_graphics *gfx);
4 static void use_theme(wt_widget *w, struct wt_theme *theme);
5
6 wt_widget *wt_window(wt_widget *par, const char *title, int style, int x, int y, int xsz, int ysz)
7 {
8         wt_widget *w;
9
10         if(!(w = wt_alloc_widget(par))) {
11                 return 0;
12         }
13         w->type = WT_TYPE_WINDOW;
14         wt_set_text(w, title);
15         wt_move(w, x, y);
16         wt_resize(w, xsz, ysz);
17         /* TODO: style */
18
19         use_theme(w, wt->theme);
20         return w;
21 }
22
23 #define FRMTHICK        1
24 #define FRMBEVEL        1
25 #define FRMOFFS         (FRMTHICK + FRMBEVEL * 2)
26 #define TBARTHICK       12      /* TODO: base it on font metrics */
27 #define TBAROFFS        (TBARTHICK + FRMBEVEL * 2)
28
29 void calc_window_rect(struct wt_rect *r, wt_widget *w)
30 {
31         r->x = w->rect.x - FRMOFFS;
32         r->y = w->rect.y - TBAROFFS - FRMOFFS;
33         r->w = w->rect.w + FRMOFFS * 2;
34         r->h = w->rect.h + TBAROFFS + FRMOFFS * 2;
35 }
36
37 static void draw_win(wt_widget *w, struct wt_graphics *gfx)
38 {
39         struct wt_rect fr, tmprect;
40         int frmcol = COL_FFRM;//wt->focuswin == w ? COL_FFRM : COL_FRM;
41
42         wt_gfx_color(COL_BG);
43         wt_gfx_fillrect(&w->rect);
44
45         calc_window_rect(&fr, w);
46
47         wt_gfx_frame(&fr, FRM_OUT | FRM_NOFILL, frmcol);        /* outer bevel */
48
49         /* draw main frame */
50         wt_gfx_color(frmcol);
51
52         fr.x += FRMBEVEL;
53         fr.y += FRMBEVEL;
54         fr.w -= FRMBEVEL * 2;
55         fr.h -= FRMBEVEL * 2;
56
57         tmprect = fr;
58         tmprect.w = FRMTHICK;
59         wt_gfx_fillrect(&tmprect);      /* left bar */
60         tmprect.x += fr.w - FRMTHICK;
61         wt_gfx_fillrect(&tmprect);      /* right bar */
62
63         tmprect = fr;
64         tmprect.h = FRMTHICK;
65         tmprect.x += FRMTHICK;
66         tmprect.w -= FRMTHICK * 2;
67         wt_gfx_fillrect(&tmprect);      /* top bar */
68         tmprect.y += fr.h - FRMTHICK;
69         wt_gfx_fillrect(&tmprect);      /* bottom bar */
70
71         /* inner bevel */
72         fr.x += FRMTHICK;
73         fr.y += FRMTHICK;
74         fr.w -= FRMTHICK * 2;
75         fr.h -= FRMTHICK * 2;
76         wt_gfx_frame(&fr, FRM_IN | FRM_NOFILL, frmcol);
77
78         /* titlebar */
79         fr.x = w->rect.x;
80         fr.w = w->rect.w;
81         fr.y += FRMBEVEL;
82         fr.h = TBARTHICK + FRMBEVEL * 2;
83         wt_gfx_frame(&fr, FRM_OUT, frmcol);
84 }
85
86 static void use_theme(wt_widget *w, struct wt_theme *theme)
87 {
88         w->draw = theme && theme->draw_window ? theme->draw_window : draw_win;
89 }