ObjectivelyMVC 0.1.0
Object oriented MVC framework for OpenGL, SDL2 and GNU C
Public Member Functions | Data Fields | Protected Attributes
Style Struct Reference

The Style type. More...

#include <Style.h>

Inheritance diagram for Style:

Public Member Functions

Class * _Style (void)
 The Style archetype. More...
 
void addAttribute (Style *self, const char *attr, ident value)
 Adds or replaces the given attribute with value. More...
 
void addAttributes (Style *self, const Dictionary *attributes)
 Adds or replaces the attribtues in attributes to this Style. More...
 
void addBoolAttribute (Style *self, const char *attr, _Bool value)
 Adds or replaces the given attribute with value. More...
 
void addCharactersAttribute (Style *self, const char *attr, const char *value)
 Adds or replaces the given attribute with value. More...
 
void addColorAttribute (Style *self, const char *attr, const SDL_Color *value)
 Adds or replaces the given attribute with value. More...
 
void addDoubleAttribute (Style *self, const char *attr, double value)
 Adds or replaces the given attribute with value. More...
 
void addEnumAttribute (Style *self, const char *attr, const EnumName *names, int value)
 Adds or replaces the given attribute with value. More...
 
void addFloatAttribute (Style *self, const char *attr, float value)
 Adds or replaces the given attribute with value. More...
 
void addIntegerAttribute (Style *self, const char *attr, int value)
 Adds or replaces the given attribute with value. More...
 
void addPointAttribute (Style *self, const char *attr, const SDL_Point *value)
 Adds or replaces the given attribute with value. More...
 
void addRectangleAttribute (Style *self, const char *attr, const SDL_Rect *value)
 Adds or replaces the given attribute with value. More...
 
void addSelector (Style *self, Selector *selector)
 Adds the given Selector to this Style. More...
 
void addSizeAttribute (Style *self, const char *attr, const SDL_Size *value)
 Adds or replaces the given attribute with value. More...
 
ident attributeValue (const Style *self, const char *attr)
 
StyleinitWithAttributes (Style *self, const Dictionary *attributes)
 Initializes this Style with the given attributes. More...
 
StyleinitWithRules (Style *self, const char *rules)
 Initializes this Style with the given CSS selector rules. More...
 
_Bool isComputedEqual (const Style *self, const Style *other)
 Performs a fast, rule-based comparison of this Style to the given Style. More...
 
Array * parse (const char *css)
 Parses the null-terminated C string of CSS definitions into an Array of Styles. More...
 
void removeAllAttributes (Style *self)
 Removes all attributes from this Style. More...
 
void removeAttribute (Style *self, const char *attr)
 Removes the attribute with the given name. More...
 

Data Fields

Dictionary * attributes
 
Object object
 The superclass. More...
 
Array * selectors
 The Selectors. More...
 

Protected Attributes

StyleInterface * interface
 The interface. More...
 

Detailed Description

The Style type.

Definition at line 43 of file Style.h.

Member Function Documentation

◆ _Style()

Class * _Style ( void  )

The Style archetype.

Returns
The Style Class.

Definition at line 537 of file Style.c.

537 {
538 static Class *clazz;
539 static Once once;
540
541 do_once(&once, {
542 clazz = _initialize(&(const ClassDef) {
543 .name = "Style",
544 .superclass = _Object(),
545 .instanceSize = sizeof(Style),
546 .interfaceOffset = offsetof(Style, interface),
547 .interfaceSize = sizeof(StyleInterface),
549 });
550 });
551
552 return clazz;
553}
static void initialize(Class *clazz)
Definition: Style.c:504
The Style type.
Definition: Style.h:43
StyleInterface * interface
The interface.
Definition: Style.h:54

◆ addAttribute()

void addAttribute ( Style self,
const char *  attr,
ident  value 
)

Adds or replaces the given attribute with value.

Parameters
selfThe Style.
attrThe attribute name.
valueThe attribute value.
Remarks
The value must be an Object and not a primitive.

Definition at line 102 of file Style.c.

102 {
103
104 assert(attr);
105 assert(value);
106
107 char *key = strtrim(attr);
108 assert(key);
109
110 $((MutableDictionary *) self->attributes, setObjectForKeyPath, value, key);
111
112 free(key);
113}
Dictionary * attributes
Definition: Style.h:59

◆ addAttributes()

void addAttributes ( Style self,
const Dictionary *  attributes 
)

Adds or replaces the attribtues in attributes to this Style.

Parameters
selfThe Style.
attributesThe attributes.

Definition at line 119 of file Style.c.

119 {
120
121 assert(attributes);
122
123 $((MutableDictionary *) self->attributes, addEntriesFromDictionary, attributes);
124}

◆ addBoolAttribute()

void addBoolAttribute ( Style self,
const char *  attr,
_Bool  value 
)

Adds or replaces the given attribute with value.

Parameters
selfThe Style.
attrThe attribute name.
valueThe attribute value.

Definition at line 130 of file Style.c.

130 {
131 $(self, addAttribute, attr, $$(Boole, valueof, value));
132}
void addAttribute(Style *self, const char *attr, ident value)
Adds or replaces the given attribute with value.
Definition: Style.c:102

◆ addCharactersAttribute()

void addCharactersAttribute ( Style self,
const char *  attr,
const char *  value 
)

Adds or replaces the given attribute with value.

Parameters
selfThe Style.
attrThe attribute name.
valueThe attribute value.

Definition at line 138 of file Style.c.

138 {
139
140 String *string = $$(String, stringWithCharacters, value);
141
142 $(self, addAttribute, attr, string);
143
144 release(string);
145}

◆ addColorAttribute()

void addColorAttribute ( Style self,
const char *  attr,
const SDL_Color *  value 
)

Adds or replaces the given attribute with value.

Parameters
selfThe Style.
attrThe attribute name.
valueThe attribute value.

Definition at line 151 of file Style.c.

151 {
152
153 Number *r = $$(Number, numberWithValue, color->r);
154 Number *g = $$(Number, numberWithValue, color->g);
155 Number *b = $$(Number, numberWithValue, color->b);
156 Number *a = $$(Number, numberWithValue, color->a);
157
158 Array *array = $$(Array, arrayWithObjects, r, g, b, a, NULL);
159
160 $(self, addAttribute, attr, array);
161
162 release(array);
163 release(r);
164 release(g);
165 release(b);
166 release(a);
167}

◆ addDoubleAttribute()

void addDoubleAttribute ( Style self,
const char *  attr,
double  value 
)

Adds or replaces the given attribute with value.

Parameters
selfThe Style.
attrThe attribute name.
valueThe attribute value.

Definition at line 173 of file Style.c.

173 {
174
175 Number *number = $$(Number, numberWithValue, value);
176
177 $(self, addAttribute, attr, number);
178
179 release(number);
180}

◆ addEnumAttribute()

void addEnumAttribute ( Style self,
const char *  attr,
const EnumName *  names,
int  value 
)

Adds or replaces the given attribute with value.

Parameters
selfThe Style.
attrThe attribute name.
namesThe EnumNames from which to derrive the attribute value.
valueThe attribute value.

Definition at line 186 of file Style.c.

186 {
187
188 String *string = nameof(names, value);
189
190 $(self, addAttribute, attr, string);
191
192 release(string);
193}

◆ addFloatAttribute()

void addFloatAttribute ( Style self,
const char *  attr,
float  value 
)

Adds or replaces the given attribute with value.

Parameters
selfThe Style.
attrThe attribute name.
valueThe attribute value.

Definition at line 199 of file Style.c.

199 {
200 $(self, addDoubleAttribute, attr, value);
201}
void addDoubleAttribute(Style *self, const char *attr, double value)
Adds or replaces the given attribute with value.
Definition: Style.c:173

◆ addIntegerAttribute()

void addIntegerAttribute ( Style self,
const char *  attr,
int  value 
)

Adds or replaces the given attribute with value.

Parameters
selfThe Style.
attrThe attribute name.
valueThe attribute value.

Definition at line 207 of file Style.c.

207 {
208 $(self, addDoubleAttribute, attr, value);
209}

◆ addPointAttribute()

void addPointAttribute ( Style self,
const char *  attr,
const SDL_Point *  value 
)

Adds or replaces the given attribute with value.

Parameters
selfThe Style.
attrThe attribute name.
valueThe attribute value.

Definition at line 215 of file Style.c.

215 {
216
217 Number *x = $$(Number, numberWithValue, value->x);
218 Number *y = $$(Number, numberWithValue, value->y);
219
220 Array *array = $$(Array, arrayWithObjects, x, y, NULL);
221
222 $(self, addAttribute, attr, array);
223
224 release(array);
225 release(x);
226 release(y);
227}

◆ addRectangleAttribute()

void addRectangleAttribute ( Style self,
const char *  attr,
const SDL_Rect *  value 
)

Adds or replaces the given attribute with value.

Parameters
selfThe Style.
attrThe attribute name.
valueThe attribute value.

Definition at line 233 of file Style.c.

233 {
234
235 Number *x = $$(Number, numberWithValue, value->x);
236 Number *y = $$(Number, numberWithValue, value->y);
237 Number *w = $$(Number, numberWithValue, value->w);
238 Number *h = $$(Number, numberWithValue, value->h);
239
240 Array *array = $$(Array, arrayWithObjects, x, y, w, h, NULL);
241
242 $(self, addAttribute, attr, array);
243
244 release(array);
245 release(x);
246 release(y);
247 release(w);
248 release(h);
249}

◆ addSelector()

void addSelector ( Style self,
Selector selector 
)

Adds the given Selector to this Style.

Parameters
selfThe Style.
selectorThe Selector.

Definition at line 255 of file Style.c.

255 {
256
257 assert(selector);
258
259 $((MutableArray *) self->selectors, addObject, selector);
260}
Array * selectors
The Selectors.
Definition: Style.h:64

◆ addSizeAttribute()

void addSizeAttribute ( Style self,
const char *  attr,
const SDL_Size value 
)

Adds or replaces the given attribute with value.

Parameters
selfThe Style.
attrThe attribute name.
valueThe attribute value.

Definition at line 266 of file Style.c.

266 {
267
268 Number *w = $$(Number, numberWithValue, value->w);
269 Number *h = $$(Number, numberWithValue, value->h);
270
271 Array *array = $$(Array, arrayWithObjects, w, h, NULL);
272
273 $(self, addAttribute, attr, array);
274
275 release(array);
276 release(w);
277 release(h);
278}
int w
Definition: Types.h:63
int h
Definition: Types.h:63

◆ attributeValue()

ident attributeValue ( const Style self,
const char *  attr 
)
Parameters
selfThe Style.
attrThe attribute name.
Returns
The attribute value.

Definition at line 284 of file Style.c.

284 {
285 return $((Dictionary *) self->attributes, objectForKeyPath, attr);
286}

◆ initWithAttributes()

Style * initWithAttributes ( Style self,
const Dictionary *  attributes 
)

Initializes this Style with the given attributes.

Parameters
selfThe Style.
attributesThe attributes.
Returns
The initialized Style, or NULL on error.
See also
View::awakeWithDictionary(View *, const Dictionary *)

Definition at line 292 of file Style.c.

292 {
293
294 self = $(self, initWithRules, NULL);
295 if (self) {
296 if (attributes) {
297 $(self, addAttributes, attributes);
298 }
299 }
300
301 return self;
302}
Style * initWithRules(Style *self, const char *rules)
Initializes this Style with the given CSS selector rules.
Definition: Style.c:308
void addAttributes(Style *self, const Dictionary *attributes)
Adds or replaces the attribtues in attributes to this Style.
Definition: Style.c:119

◆ initWithRules()

Style * initWithRules ( Style self,
const char *  rules 
)

Initializes this Style with the given CSS selector rules.

Parameters
selfThe Style.
rulesA null-terminated C string of one or more Selector rules.
Returns
The initialized Style, or NULL on error.
See also
Selector::parse(const char *)

Definition at line 308 of file Style.c.

308 {
309
310 self = (Style *) super(Object, self, init);
311 if (self) {
312
313 self->selectors = $$(Selector, parse, rules);
314 assert(self->selectors);
315
316 for (size_t i = 0; i < self->selectors->count; i++) {
317 ((Selector *) self->selectors->elements[i])->style = self;
318 }
319
320 self->attributes = (Dictionary *) $$(MutableDictionary, dictionaryWithCapacity, 4);
321 assert(self->attributes);
322 }
323
324 return self;
325}
static View * init(View *self)
Definition: Box.c:67
Selectors are comprised of one or more SelectorSequences.
Definition: Selector.h:49
Array * parse(const char *css)
Parses the null-terminated C string of CSS definitions into an Array of Styles.
Definition: Selector.c:274

◆ isComputedEqual()

_Bool isComputedEqual ( const Style self,
const Style other 
)

Performs a fast, rule-based comparison of this Style to the given Style.

Parameters
selfThe Style.
otherThe Style to test for computed equality.
Returns
True if the given Style is deemed equal, false otherwise.
Remarks
This method is optimized to quickly determine if two computed Styles should contain the same attributes based on their Selector rules. For true equality, use isEqual.

Definition at line 331 of file Style.c.

331 {
332
333 assert(other);
334
335 if (self->selectors->count == other->selectors->count) {
336
337 for (size_t i = 0; i < self->selectors->count; i++) {
338
339 const Selector *this = $(self->selectors, objectAtIndex, i);
340 const Selector *that = $(other->selectors, objectAtIndex, i);
341
342 if (strcmp(this->rule, that->rule)) {
343 return false;
344 }
345 }
346
347 return true;
348 }
349
350 return false;
351}
char * rule
The rule, as provided by the user.
Definition: Selector.h:70

◆ parse()

Array * parse ( const char *  css)

Parses the null-terminated C string of CSS definitions into an Array of Styles.

Parameters
cssThe CSS definitions.
Returns
An Array of Styles.

Definition at line 274 of file Selector.c.

274 {
275
276 MutableArray *selectors = $$(MutableArray, arrayWithCapacity, 4);
277 assert(selectors);
278
279 if (rules) {
280
281 const char *c = rules;
282 while (*c) {
283 const size_t size = strcspn(c, ",");
284 if (size) {
285 char *rule = calloc(1, size + 1);
286 assert(rule);
287
288 strncpy(rule, c, size);
289
290 Selector *selector = $(alloc(Selector), initWithRule, rule);
291 assert(selector);
292
293 $(selectors, addObject, selector);
294
295 release(selector);
296 free(rule);
297 }
298 c += size;
299 c += strspn(c, ", \t\n");
300 }
301 }
302
303 return (Array *) selectors;
304}
static SDL_Size size(const Image *self)
Definition: Image.c:181
static Selector * initWithRule(Selector *self, const char *rule)
Definition: Selector.c:188

◆ removeAllAttributes()

void removeAllAttributes ( Style self)

Removes all attributes from this Style.

Parameters
selfThe Style.

Definition at line 495 of file Style.c.

495 {
496 $((MutableDictionary *) self->attributes, removeAllObjects);
497}

◆ removeAttribute()

void removeAttribute ( Style self,
const char *  attr 
)

Removes the attribute with the given name.

Parameters
selfThe Style.
attrThe attribute name.

Definition at line 487 of file Style.c.

487 {
488 $((MutableDictionary *) self->attributes, removeObjectForKeyPath, attr);
489}

Field Documentation

◆ attributes

Dictionary* Style::attributes

The attributes.

Definition at line 59 of file Style.h.

◆ interface

StyleInterface* Style::interface
protected

The interface.

Definition at line 54 of file Style.h.

◆ object

Object Style::object

The superclass.

Definition at line 48 of file Style.h.

◆ selectors

Array* Style::selectors

The Selectors.

Definition at line 64 of file Style.h.


The documentation for this struct was generated from the following files: