56734a015528b99111cf1a2ecf12d708ba986d6f
[manmower/obtheme.git] / obtheme.c
1 #include <assert.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <glib.h>
5 #include "obtheme.h"
6 #include "geom.h"
7
8 static double variable_lookup(struct variable *in)
9 {
10         if (strcmp("client", in->base) == 0) {
11                 if (strcmp("width", in->member) == 0) {
12                         return 640;
13                 } else
14                         return 480;
15         }
16         if (strcmp("font", in->base) == 0) {
17                 return 15.0;
18         }
19         return 0.0;
20 }
21
22 static double expression_eval(struct expression *in)
23 {
24         switch (in->op) {
25                 case OB_THEME_INV:
26                         return - expression_eval(in->a);
27                         break;
28                 case OB_THEME_MUL:
29                         return expression_eval(in->a) * expression_eval(in->b);
30                         break;
31                 case OB_THEME_DIV:
32                         return expression_eval(in->a) / expression_eval(in->b);
33                         break;
34                 case OB_THEME_ADD:
35                         return expression_eval(in->a) + expression_eval(in->b);
36                         break;
37                 case OB_THEME_SUB:
38                         return expression_eval(in->a) - expression_eval(in->b);
39                         break;
40                 case OB_THEME_EQL:
41                         if (in->v.base == NULL) {
42                                 return in->v.number;
43                         } else return variable_lookup(&in->v);
44                         break;
45                 default:
46                         assert(!!!"OH NOES!!!");
47         }
48         return 0;
49 }
50
51 static void decor_print(gpointer data, gpointer user_data)
52 {
53         struct decor *decor = data;
54         printf("    decor id %s\n", decor->name);
55 printf("   anchor.x = %f\n", expression_eval(&decor->space.anchor.x));
56         if (decor->texture.present) {
57                 if (decor->texture.internal)
58                         printf("    texture internal: %s\n", decor->texture.name);
59                 else
60                         printf("    texture file: %s\n", decor->texture.name);
61         }
62 //      printf("      anchor (%d %d %d)\n", decor->space.anchor.x, decor->space.anchor.y, decor->space.anchor.z);
63 //      printf("      up     (%d %d %d)\n", decor->space.up.x, decor->space.up.y, decor->space.up.z);
64         if (decor->children)
65                 g_slist_foreach(decor->children, decor_print, NULL);
66 }
67
68 static void style_print(gpointer key, gpointer value, gpointer user_data)
69 {
70         char *stylename = key;
71         struct style *style = value;
72
73         printf("  style %s\n", stylename);
74         g_slist_foreach(style->tree, decor_print, NULL);
75 }
76
77 static void theme_print(gpointer key, gpointer value, gpointer user_data)
78 {
79         char *name = key;
80         struct theme *thm = value;
81         printf("name = %s\n", name);
82         g_hash_table_foreach(thm->styles, style_print, NULL);
83 }
84
85 static void material_print(gpointer key, gpointer value, gpointer user_data)
86 {
87         char *name = key;
88         struct material *mat = value;
89         printf("name = %s\n", name);
90         printf(" opacity = %f\n", mat->opacity);
91 }
92
93 static void decor_render(gpointer data, gpointer user_data)
94 {
95         struct decor *decor = data;
96         struct box *box = &decor->geometry.box;
97         struct vector *a = &decor->space.anchor;
98         double x, y;
99
100         printf("%s:", decor->name);
101         x = expression_eval(&a->x);
102         y = expression_eval(&a->y);
103         printf("rectangle(%f %f) - (%f %f)\n", x + expression_eval(&box->start.x), y + expression_eval(&box->start.y),
104                                     x + expression_eval(&box->end.x), y + expression_eval(&box->end.y));
105         if (decor->children)
106                 g_slist_foreach(decor->children, decor_render, NULL);
107 }
108
109 static void decor_bound(gpointer data, gpointer user_data)
110 {
111         struct boundrect *br = user_data;
112         struct decor *decor = data;
113         struct box *box = &decor->geometry.box;
114         struct vector *a = &decor->space.anchor;
115         Rect newrect;
116         double x1, y1, x2, y2;
117
118         x1 = expression_eval(&a->x);
119         y1 = expression_eval(&a->y);
120         x2 = x1;
121         y2 = y1;
122         x1 += expression_eval(&box->start.x);
123         y1 += expression_eval(&box->start.y);
124         x2 += expression_eval(&box->end.x);
125         y2 += expression_eval(&box->end.y);
126         if (x1 < br->x1)
127                 br->x1 = x1;
128         if (x2 < br->x1)
129                 br->x1 = x2;
130         if (x1 > br->x2)
131                 br->x2 = x1;
132         if (x2 > br->x2)
133                 br->x2 = x2;
134
135         if (y1 < br->y1)
136                 br->y1 = y1;
137         if (y2 < br->y1)
138                 br->y1 = y2;
139         if (y1 > br->y2)
140                 br->y2 = y1;
141         if (y2 > br->y2)
142                 br->y2 = y2;
143         if (decor->children)
144                 g_slist_foreach(decor->children, decor_bound, br);
145 }
146
147 void obtheme_calc_bound(struct theme *thm, char *name, struct boundrect *br)
148 {
149         struct style *sty;
150
151         br->x1 = 0.0;
152         br->y1 = 0.0;
153         br->x2 = 0.0;
154         br->y2 = 0.0;
155         sty = g_hash_table_lookup(thm->styles, name);
156         assert(sty);
157         g_slist_foreach(sty->tree, decor_bound, br);
158 }
159
160 void obtheme_decorate_window(struct theme *thm, char *name)
161 {
162         struct style *sty;
163         sty = g_hash_table_lookup(thm->styles, name);
164         g_slist_foreach(sty->tree, decor_render, NULL);
165 }