start by copying windtk and dropping dirty rects and indexed colors
[anigui] / src / window.c
1 #include "agimpl.h"
2
3 static void draw_win(ag_widget *w, struct ag_graphics *gfx);
4 static void use_theme(ag_widget *w, struct ag_theme *theme);
5
6 ag_widget *ag_window(ag_widget *par, const char *title, int style, int x, int y, int xsz, int ysz)
7 {
8         ag_widget *w;
9
10         if(!(w = ag_alloc_widget(par))) {
11                 return 0;
12         }
13         w->type = AG_TYPE_WINDOW;
14         ag_set_text(w, title);
15         ag_move(w, x, y);
16         ag_resize(w, xsz, ysz);
17         /* TODO: style */
18
19         use_theme(w, ag->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 ag_calc_window_rect(struct ag_rect *r, ag_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(ag_widget *w, struct ag_graphics *gfx)
38 {
39         struct ag_rect fr, tmprect;
40         int frmcol = COL_FFRM;//ag->focuswin == w ? COL_FFRM : COL_FRM;
41
42         ag_gfx_color(COL_BG);
43         ag_gfx_fillrect(&w->rect);
44
45         ag_calc_window_rect(&fr, w);
46
47         ag_gfx_frame(&fr, FRM_OUT | FRM_NOFILL, frmcol);        /* outer bevel */
48
49         /* draw main frame */
50         ag_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         ag_gfx_fillrect(&tmprect);      /* left bar */
60         tmprect.x += fr.w - FRMTHICK;
61         ag_gfx_fillrect(&tmprect);      /* right bar */
62
63         tmprect = fr;
64         tmprect.h = FRMTHICK;
65         tmprect.x += FRMTHICK;
66         tmprect.w -= FRMTHICK * 2;
67         ag_gfx_fillrect(&tmprect);      /* top bar */
68         tmprect.y += fr.h - FRMTHICK;
69         ag_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         ag_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         ag_gfx_frame(&fr, FRM_OUT, frmcol);
84 }
85
86 static void use_theme(ag_widget *w, struct ag_theme *theme)
87 {
88         w->draw = theme && theme->draw_window ? theme->draw_window : draw_win;
89 }