-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtsms_gui_style.c
168 lines (141 loc) · 7.29 KB
/
tsms_gui_style.c
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
#include "tsms_gui.h"
#include "tsms_gui_style.h"
TSMS_STYLE TSMS_STYLE_DEFAULT;
TSMS_STYLE TSMS_STYLE_DEFAULT_BUTTON;
TSMS_STYLE TSMS_STYLE_DEFAULT_TEXT;
TSMS_STYLE TSMS_STYLE_DEFAULT_CONTAINER;
TSMS_STYLE TSMS_STYLE_DEFAULT_ROW;
TSMS_STYLE TSMS_STYLE_DEFAULT_COLUMN;
TSMS_STYLE TSMS_STYLE_DEFAULT_LABEL;
TSMS_RESULT TSMS_GUI_STYLE_init() {
TSMS_STYLE_DEFAULT = (TSMS_STYLE) {{0, 0, 0, 0, &TSMS_COLOR_WHITE}, {0, 0, 0, 0, &TSMS_COLOR_WHITE},{0, 0, 0, 0, &TSMS_COLOR_WHITE},
&TSMS_COLOR_WHITE, &TSMS_COLOR_WHITE,&TSMS_COLOR_WHITE, TSMS_STYLE_AUTO,TSMS_STYLE_AUTO,
TSMS_STYLE_AUTO, TSMS_STYLE_AUTO, TSMS_STYLE_AUTO,TSMS_STYLE_AUTO,
{TSMS_FONT_TYPE_ARRAY, 1, TSMS_FONT_ARRAY_DEFAULT_FONT, &TSMS_COLOR_BLACK},
TSMS_STYLE_POSITION_RELATIVE, 0, 0, 0, TSMS_STYLE_DISPLAY_BLOCK
};
TSMS_STYLE_DEFAULT_BUTTON = (TSMS_STYLE) {
{0, 2, 0, 2, &TSMS_COLOR_WHITE}, {2, 2, 2, 2, &TSMS_COLOR_BLACK}, {7, 7, 7, 7, &TSMS_COLOR_WHITE},
&TSMS_COLOR_WHITE, &TSMS_COLOR_GRAY, &TSMS_COLOR_WHITE, TSMS_STYLE_AUTO, TSMS_STYLE_AUTO,
TSMS_STYLE_INHERIT, TSMS_STYLE_INHERIT, TSMS_STYLE_INHERIT,TSMS_STYLE_INHERIT,
{TSMS_STYLE_FONT_TYPE_INHERIT, TSMS_STYLE_FONT_SIZE_INHERIT, TSMS_STYLE_FONT_INHERIT,TSMS_STYLE_COLOR_INHERT},
TSMS_STYLE_POSITION_RELATIVE, TSMS_STYLE_INHERIT, 0, 0,TSMS_STYLE_DISPLAY_BLOCK
};
TSMS_STYLE_DEFAULT_TEXT = (TSMS_STYLE) {
{0, 0, 0, 0, &TSMS_COLOR_WHITE}, {0, 0, 0, 0, &TSMS_COLOR_WHITE}, {0, 0, 0, 0, &TSMS_COLOR_WHITE},
TSMS_STYLE_COLOR_INHERT, &TSMS_COLOR_WHITE, &TSMS_COLOR_WHITE, TSMS_STYLE_AUTO, TSMS_STYLE_AUTO,
TSMS_STYLE_INHERIT, TSMS_STYLE_INHERIT,TSMS_STYLE_INHERIT, TSMS_STYLE_INHERIT,
{TSMS_STYLE_FONT_TYPE_INHERIT, TSMS_STYLE_FONT_SIZE_INHERIT, TSMS_STYLE_FONT_INHERIT,TSMS_STYLE_COLOR_INHERT},
TSMS_STYLE_POSITION_RELATIVE, TSMS_STYLE_INHERIT, 0, 0,TSMS_STYLE_DISPLAY_BLOCK
};
TSMS_STYLE_DEFAULT_CONTAINER = (TSMS_STYLE) {
{0, 0, 0, 0, &TSMS_COLOR_WHITE}, {0, 0, 0, 0, &TSMS_COLOR_WHITE}, {0, 0, 0, 0, &TSMS_COLOR_WHITE},
&TSMS_COLOR_WHITE, &TSMS_COLOR_WHITE, &TSMS_COLOR_WHITE, TSMS_STYLE_AUTO, TSMS_STYLE_AUTO,
TSMS_STYLE_INHERIT, TSMS_STYLE_INHERIT,TSMS_STYLE_INHERIT, TSMS_STYLE_INHERIT,
{TSMS_STYLE_FONT_TYPE_INHERIT, TSMS_STYLE_FONT_SIZE_INHERIT, TSMS_STYLE_FONT_INHERIT,TSMS_STYLE_COLOR_INHERT},
TSMS_STYLE_POSITION_RELATIVE, TSMS_STYLE_INHERIT, 0, 0,TSMS_STYLE_DISPLAY_BLOCK
};
TSMS_STYLE_DEFAULT_ROW = TSMS_STYLE_DEFAULT_CONTAINER;
TSMS_STYLE_DEFAULT_COLUMN = TSMS_STYLE_DEFAULT_CONTAINER;
TSMS_STYLE_DEFAULT_LABEL = (TSMS_STYLE) {
{0, 2, 0, 2, &TSMS_COLOR_WHITE}, {0, 0, 0, 0, &TSMS_COLOR_WHITE},{7, 7, 7, 7, &TSMS_COLOR_WHITE},
&TSMS_COLOR_WHITE, &TSMS_COLOR_WHITE, &TSMS_COLOR_WHITE, TSMS_STYLE_AUTO, TSMS_STYLE_AUTO,
TSMS_STYLE_INHERIT, TSMS_STYLE_INHERIT, TSMS_STYLE_INHERIT,TSMS_STYLE_INHERIT,
{TSMS_STYLE_FONT_TYPE_INHERIT, TSMS_STYLE_FONT_SIZE_INHERIT, TSMS_STYLE_FONT_INHERIT,TSMS_STYLE_COLOR_INHERT},
TSMS_STYLE_POSITION_RELATIVE, TSMS_STYLE_INHERIT, 0, 0,TSMS_STYLE_DISPLAY_BLOCK
};
return TSMS_SUCCESS;
}
uint16_t TSMS_STYLE_Y_ATTACHMENT(TSMS_STYLE style) {
return style.margin.top + style.padding.top + style.border.top + style.margin.bottom + style.padding.bottom +
style.border.bottom;
}
uint16_t TSMS_STYLE_X_ATTACHMENT(TSMS_STYLE style) {
return style.margin.left + style.padding.left + style.border.left + style.margin.right + style.padding.right +
style.border.right;
}
uint16_t TSMS_STYLE_getBoxWidth(TSMS_STYLE style, uint16_t width, uint16_t parentWidth) {
if (style.width != TSMS_STYLE_AUTO)
width = style.width;
if (style.minWidth != TSMS_STYLE_AUTO && width < style.minWidth)
width = style.minWidth;
if (style.maxWidth != TSMS_STYLE_AUTO && width > style.maxWidth)
width = style.maxWidth;
if (width + TSMS_STYLE_X_ATTACHMENT(style) > parentWidth)
width = parentWidth - TSMS_STYLE_X_ATTACHMENT(style);
return width;
}
uint16_t TSMS_STYLE_getBoxHeight(TSMS_STYLE style, uint16_t height, uint16_t parentHeight) {
if (style.height != TSMS_STYLE_AUTO)
height = style.height;
if (style.minHeight != TSMS_STYLE_AUTO && height < style.minHeight)
height = style.minHeight;
if (style.maxHeight != TSMS_STYLE_AUTO && height > style.maxHeight)
height = style.maxHeight;
if (height + TSMS_STYLE_Y_ATTACHMENT(style) > parentHeight)
height = parentHeight - TSMS_STYLE_Y_ATTACHMENT(style);
return height;
}
uint16_t TSMS_STYLE_leftOffset(TSMS_STYLE style) {
return style.margin.left + style.padding.left + style.border.left;
}
uint16_t TSMS_STYLE_topOffset(TSMS_STYLE style) {
return style.margin.top + style.padding.top + style.border.top;
}
bool TSMS_STYLE_equals(TSMS_STYLE style1, TSMS_STYLE style2) {
uint8_t *style1Pointer = (uint8_t *) &style1;
uint8_t *style2Pointer = (uint8_t *) &style2;
return memcmp(style1Pointer, style2Pointer, sizeof(TSMS_STYLE)) == 0;
}
TSMS_STYLE TSMS_STYLE_getStyle(pGuiElement element) {
if (element->parent == TSMS_NULL)
return element->computedStyle = TSMS_MUTABLE_STYLE_get(element->style);
TSMS_STYLE parentStyle = element->parent->computedStyle;
TSMS_STYLE style = TSMS_MUTABLE_STYLE_get(element->style);
if (style.minWidth == TSMS_STYLE_INHERIT)
style.minWidth = parentStyle.minWidth;
if (style.minHeight == TSMS_STYLE_INHERIT)
style.minHeight = parentStyle.minHeight;
if (style.maxWidth == TSMS_STYLE_INHERIT)
style.maxWidth = parentStyle.maxWidth;
if (style.maxHeight == TSMS_STYLE_INHERIT)
style.maxHeight = parentStyle.maxHeight;
if (style.font.type == TSMS_STYLE_FONT_TYPE_INHERIT)
style.font = parentStyle.font;
if (style.font.size == TSMS_STYLE_FONT_SIZE_INHERIT)
style.font.size = parentStyle.font.size;
if (style.font.font == TSMS_STYLE_FONT_INHERIT)
style.font.font = parentStyle.font.font;
if (style.font.color == TSMS_STYLE_COLOR_INHERT)
style.font.color = parentStyle.font.color;
if (style.position == TSMS_STYLE_POSITION_INHERIT)
style.position = parentStyle.position;
if (style.backgroundColor == TSMS_STYLE_COLOR_INHERT)
style.backgroundColor = parentStyle.backgroundColor;
if (style.zIndex == TSMS_STYLE_INHERIT || style.zIndex < parentStyle.zIndex || style.position == TSMS_STYLE_POSITION_RELATIVE)
style.zIndex = parentStyle.zIndex;
if (parentStyle.display == TSMS_STYLE_DISPLAY_NONE)
style.display = TSMS_STYLE_DISPLAY_NONE;
return element->computedStyle = style;
}
bool TSMS_STYLE_equalsFont(TSMS_STYLE_FONT font1, TSMS_STYLE_FONT font2) {
uint8_t *font1Pointer = (uint8_t *) &font1;
uint8_t *font2Pointer = (uint8_t *) &font2;
return memcmp(font1Pointer, font2Pointer, sizeof(TSMS_STYLE_FONT)) == 0;
}
uint16_t TSMS_STYLE_elementWidth(TSMS_STYLE style, uint16_t parentWidth) {
if (style.width == TSMS_STYLE_AUTO) {
uint16_t maxWidth =
style.maxWidth == TSMS_STYLE_AUTO ? parentWidth - TSMS_STYLE_X_ATTACHMENT(style) : style.maxWidth;
return min(maxWidth, parentWidth - TSMS_STYLE_X_ATTACHMENT(style));
}
return min(style.width, parentWidth - TSMS_STYLE_X_ATTACHMENT(style));
}
uint16_t TSMS_STYLE_elementHeight(TSMS_STYLE style, uint16_t parentHeight) {
if (style.height == TSMS_STYLE_AUTO) {
uint16_t maxHeight =
style.maxHeight == TSMS_STYLE_AUTO ? parentHeight - TSMS_STYLE_Y_ATTACHMENT(style) : style.maxHeight;
return min(maxHeight, parentHeight - TSMS_STYLE_Y_ATTACHMENT(style));
}
return min(style.height, parentHeight - TSMS_STYLE_Y_ATTACHMENT(style));
}