summaryrefslogtreecommitdiff
path: root/props.c
blob: c36144d15bddb7cc76a9d34891a9223877959355 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <ctype.h>
#include <fcall.h>
#include <thread.h>
#include <9p.h>

#include "guifs.h"

#define Eparse "could not parse property"

int
allspace(char *r)
{
	while(*r){
		if(!isspace(*r))
			return 0;
		r++;
	}
	return 1;
}

PropVal
defbackground(void)
{
	PropVal v;
	v.colour = mkcolour(DWhite);
	return v;
}

PropVal
defspacing(void)
{
	PropVal v;
	v.spacing = emalloc(sizeof(Spacing));
	return v;
}

PropVal
deforientation(void)
{
	PropVal v;
	v.orientation = Horizontal;
	return v;
}

PropVal
defbordercolour(void)
{
	PropVal v;
	v.colour = mkcolour(DRed);
	return v;
}

char *
printcolour(PropVal p)
{
	int bufsize = 64;
	char *buf = emalloc(bufsize);
	snprint(buf, bufsize, "%08ulX\n", p.colour->code);
	return buf;
}

char *
printspacing(PropVal p)
{
	int bufsize = 256;
	char *buf = emalloc(bufsize);
	snprint(buf, bufsize, "%d %d %d %d\n", p.spacing->up, p.spacing->right, p.spacing->down, p.spacing->left);
	return buf;
}

char *
printorientation(PropVal p)
{
	char *str;
	switch(p.orientation){
	case Horizontal:
		str = estrdup9p("horizontal\n");
		break;
	case Vertical:
		str = estrdup9p("vertical\n");
		break;
	default:
		str = estrdup9p("???\n");
		break;
	}
	return str;
}

char *
parsecolour(char *str, PropVal *p)
{
	char *r;
	ulong c = strtoul(str, &r, 16);
	if((r - str) != 8 || !allspace(r))
		return Eparse;
	(*p).colour = mkcolour(c);
	return nil;
}

char *
parsespacing(char *str, PropVal *p)
{
	USED(p);
	char *fields[5];
	int spacings[4];

	int n = getfields(str, fields, nelem(fields), 0, " ");
	if(!(n == 4 || n == 2 || n == 1))
		return Eparse;

	for(int i = 0; i < n; i++){
		char *r;
		spacings[i] = strtol(fields[i], &r, 10);
		if(!allspace(r))
			return Eparse;
	}

	Spacing *s = emalloc(sizeof(Spacing));
	switch(n){
	case 1:
		s->up = s->down = s->left = s->right = spacings[0];
		break;
	case 2:
		s->up = s->down = spacings[0];
		s->left = s->right = spacings[1];
		break;
	case 4:
		s->up = spacings[0];
		s->right = spacings[1];
		s->down = spacings[2];
		s->left = spacings[3];
		break;
	}
	(*p).spacing = s;

	return nil;
}

char *
parseorientation(char *str, PropVal *p)
{
	if(strncmp(str, "horizontal", 10) == 0 && allspace(str+10))
		(*p).orientation = Horizontal;
	else if(strncmp(str, "vertical", 8) == 0 && allspace(str+8))
		(*p).orientation = Vertical;
	else
		return Eparse;
	return nil;
}

PropVal
getprop(GuiElement *g, int tag)
{
	PropVal *v = nil;
	rlock(&g->lock);
	for(int i = 0; i < g->nprops && v == nil; i++)
		if(g->props[i].tag == tag)
			v = &g->props[i].val;
	runlock(&g->lock);

	if(v == nil)
		sysfatal("invalid prop for this gui element");
	else
		return *v;
}

void
setprop(GuiElement *g, int tag, PropVal val)
{
	wlock(&g->lock);
	/* TODO: free old propval */
	for(int i = 0; i < g->nprops; i++)
		if(g->props[i].tag == tag)
			g->props[i].val = val;
	wunlock(&g->lock);
	updategui(0);
}

PropSpec propspecs[Pmax] = {
	[Pbackground] = {"background",	defbackground,	printcolour,	parsecolour},
	[Pborder] = {"border", 	defspacing,	printspacing,	parsespacing},
	[Pmargin] = {"margin", 	defspacing,	printspacing,	parsespacing},
	[Ppadding] = {"padding", defspacing,	printspacing,	parsespacing},
	[Porientation] = {"orientation",	deforientation,	printorientation,	parseorientation},
	[Pbordercolour] = {"bordercolour",	defbordercolour,	printcolour,	parsecolour},
};

int baseprops[nbaseprops] = {Pborder, Pmargin, Ppadding, Pbordercolour};