repair brokeness provided by a foolish Makefile
[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 struct boundstuff {
110         Rect r;
111         Strut s;
112 };
113
114 static void decor_bound(gpointer data, gpointer user_data)
115 {
116         struct boundstuff *bs = user_data;
117         struct decor *decor = data;
118         struct box *box = &decor->geometry.box;
119         struct vector *a = &decor->space.anchor;
120         Rect newrect, temprect;
121         double x1, y1, x2, y2;
122
123         x1 = expression_eval(&a->x);
124         y1 = expression_eval(&a->y);
125         x2 = x1;
126         y2 = y1;
127         x1 += expression_eval(&box->start.x);
128         y1 += expression_eval(&box->start.y);
129         x2 += expression_eval(&box->end.x);
130         y2 += expression_eval(&box->end.y);
131         RECT_SET(temprect, x1, y1, x2-x1, y2-y2);
132         RECT_ADD(newrect, temprect, bs->r);
133         bs->r = newrect;
134
135         if (decor->children)
136                 g_slist_foreach(decor->children, decor_bound, bs);
137 }
138
139 void obtheme_calc_bound(struct theme *thm, char *name, Rect *r, Strut *s)
140 {
141         struct style *sty;
142         struct boundstuff bs;
143
144         RECT_SET(bs.r, 0, 0 ,0 ,0);
145         sty = g_hash_table_lookup(thm->styles, name);
146         assert(sty);
147         g_slist_foreach(sty->tree, decor_bound, &bs);
148         *r = bs.r;
149 }
150
151 void obtheme_decorate_window(struct theme *thm, char *name)
152 {
153         struct style *sty;
154         sty = g_hash_table_lookup(thm->styles, name);
155         g_slist_foreach(sty->tree, decor_render, NULL);
156 }