ObjectivelyMVC 0.1.0
Object oriented MVC framework for OpenGL, SDL2 and GNU C
Macros | Functions
Style.c File Reference
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <Objectively.h>
#include "Style.h"

Go to the source code of this file.

Macros

#define _Class   _Style
 

Functions

Class * _Style (void)
 
static void addAttribute (Style *self, const char *attr, ident value)
 
static void addAttributes (Style *self, const Dictionary *attributes)
 
static void addBoolAttribute (Style *self, const char *attr, _Bool value)
 
static void addCharactersAttribute (Style *self, const char *attr, const char *value)
 
static void addColorAttribute (Style *self, const char *attr, const SDL_Color *color)
 
static void addDoubleAttribute (Style *self, const char *attr, double value)
 
static void addEnumAttribute (Style *self, const char *attr, const EnumName *names, int value)
 
static void addFloatAttribute (Style *self, const char *attr, float value)
 
static void addIntegerAttribute (Style *self, const char *attr, int value)
 
static void addPointAttribute (Style *self, const char *attr, const SDL_Point *value)
 
static void addRectangleAttribute (Style *self, const char *attr, const SDL_Rect *value)
 
static void addSelector (Style *self, Selector *selector)
 
static void addSizeAttribute (Style *self, const char *attr, const SDL_Size *value)
 
static ident attributeValue (const Style *self, const char *attr)
 
static void dealloc (Object *self)
 
static String * description (const Object *self)
 
static int hash (const Object *self)
 
static void initialize (Class *clazz)
 
static StyleinitWithAttributes (Style *self, const Dictionary *attributes)
 
static StyleinitWithRules (Style *self, const char *rules)
 
static _Bool isComputedEqual (const Style *self, const Style *other)
 
static _Bool isEqual (const Object *self, const Object *other)
 
static Array * parse (const char *css)
 
static ident parseValue (String *string)
 
static void removeAllAttributes (Style *self)
 
static void removeAttribute (Style *self, const char *attr)
 

Macro Definition Documentation

◆ _Class

#define _Class   _Style

Definition at line 32 of file Style.c.

Function Documentation

◆ _Style()

Class * _Style ( void  )

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

◆ addAttribute()

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

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()

static void addAttributes ( Style self,
const Dictionary *  attributes 
)
static

Definition at line 119 of file Style.c.

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

◆ addBoolAttribute()

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

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()

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

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()

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

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()

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

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()

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

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()

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

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()

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

Definition at line 207 of file Style.c.

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

◆ addPointAttribute()

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

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()

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

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()

static void addSelector ( Style self,
Selector selector 
)
static

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()

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

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()

static ident attributeValue ( const Style self,
const char *  attr 
)
static

Definition at line 284 of file Style.c.

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

◆ dealloc()

static void dealloc ( Object *  self)
static
See also
Object::dealloc(Object *)

Definition at line 39 of file Style.c.

39 {
40
41 Style *this = (Style *) self;
42
43 release(this->attributes);
44 release(this->selectors);
45
46 super(Object, self, dealloc);
47}
static void dealloc(Object *self)
Definition: Style.c:39

◆ description()

static String * description ( const Object *  self)
static
See also
Object::description(const Object *)

Definition at line 52 of file Style.c.

52 {
53
54 const Style *this = (Style *) self;
55
56 return $((Array *) this->selectors, componentsJoinedByCharacters, ", ");
57}

◆ hash()

static int hash ( const Object *  self)
static
See also
Object::hash(const Object *)

Definition at line 62 of file Style.c.

62 {
63
64 Style *this = (Style *) self;
65
66 int hash = HASH_SEED;
67
68 hash = HashForObject(hash, this->attributes);
69 hash = HashForObject(hash, this->selectors);
70
71 return hash;
72}
static int hash(const Object *self)
Definition: Style.c:62

◆ initialize()

static void initialize ( Class *  clazz)
static
See also
Class::initialize(Class *)

Definition at line 504 of file Style.c.

504 {
505
506 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
507 ((ObjectInterface *) clazz->interface)->description = description;
508 ((ObjectInterface *) clazz->interface)->hash = hash;
509 ((ObjectInterface *) clazz->interface)->isEqual = isEqual;
510
511 ((StyleInterface *) clazz->interface)->addAttribute = addAttribute;
512 ((StyleInterface *) clazz->interface)->addAttributes = addAttributes;
513 ((StyleInterface *) clazz->interface)->addBoolAttribute = addBoolAttribute;
514 ((StyleInterface *) clazz->interface)->addCharactersAttribute = addCharactersAttribute;
515 ((StyleInterface *) clazz->interface)->addColorAttribute = addColorAttribute;
516 ((StyleInterface *) clazz->interface)->addDoubleAttribute = addDoubleAttribute;
517 ((StyleInterface *) clazz->interface)->addEnumAttribute = addEnumAttribute;
518 ((StyleInterface *) clazz->interface)->addFloatAttribute = addFloatAttribute;
519 ((StyleInterface *) clazz->interface)->addIntegerAttribute = addIntegerAttribute;
520 ((StyleInterface *) clazz->interface)->addPointAttribute = addPointAttribute;
521 ((StyleInterface *) clazz->interface)->addRectangleAttribute = addRectangleAttribute;
522 ((StyleInterface *) clazz->interface)->addSelector = addSelector;
523 ((StyleInterface *) clazz->interface)->addSizeAttribute = addSizeAttribute;
524 ((StyleInterface *) clazz->interface)->attributeValue = attributeValue;
525 ((StyleInterface *) clazz->interface)->initWithAttributes = initWithAttributes;
526 ((StyleInterface *) clazz->interface)->initWithRules = initWithRules;
527 ((StyleInterface *) clazz->interface)->isComputedEqual = isComputedEqual;
528 ((StyleInterface *) clazz->interface)->parse = parse;
529 ((StyleInterface *) clazz->interface)->removeAllAttributes = removeAllAttributes;
530 ((StyleInterface *) clazz->interface)->removeAttribute = removeAttribute;
531}
static String * description(const Object *self)
Definition: Style.c:52
static _Bool isEqual(const Object *self, const Object *other)
Definition: Style.c:77
Array * parse(const char *rules)
Parses the null-terminated C string of Selector rules into an Array of Selectors.
Definition: Selector.c:274
void addSelector(Style *self, Selector *selector)
Adds the given Selector to this Style.
Definition: Style.c:255
Style * initWithAttributes(Style *self, const Dictionary *attributes)
Initializes this Style with the given attributes.
Definition: Style.c:292
_Bool isComputedEqual(const Style *self, const Style *other)
Performs a fast, rule-based comparison of this Style to the given Style.
Definition: Style.c:331
void addIntegerAttribute(Style *self, const char *attr, int value)
Adds or replaces the given attribute with value.
Definition: Style.c:207
void addSizeAttribute(Style *self, const char *attr, const SDL_Size *value)
Adds or replaces the given attribute with value.
Definition: Style.c:266
void removeAttribute(Style *self, const char *attr)
Removes the attribute with the given name.
Definition: Style.c:487
void addEnumAttribute(Style *self, const char *attr, const EnumName *names, int value)
Adds or replaces the given attribute with value.
Definition: Style.c:186
void addPointAttribute(Style *self, const char *attr, const SDL_Point *value)
Adds or replaces the given attribute with value.
Definition: Style.c:215
void addRectangleAttribute(Style *self, const char *attr, const SDL_Rect *value)
Adds or replaces the given attribute with value.
Definition: Style.c:233
Style * initWithRules(Style *self, const char *rules)
Initializes this Style with the given CSS selector rules.
Definition: Style.c:308
void removeAllAttributes(Style *self)
Removes all attributes from this Style.
Definition: Style.c:495
void addFloatAttribute(Style *self, const char *attr, float value)
Adds or replaces the given attribute with value.
Definition: Style.c:199
void addCharactersAttribute(Style *self, const char *attr, const char *value)
Adds or replaces the given attribute with value.
Definition: Style.c:138
void addAttributes(Style *self, const Dictionary *attributes)
Adds or replaces the attribtues in attributes to this Style.
Definition: Style.c:119
void addBoolAttribute(Style *self, const char *attr, _Bool value)
Adds or replaces the given attribute with value.
Definition: Style.c:130
ident attributeValue(const Style *self, const char *attr)
Definition: Style.c:284
void addColorAttribute(Style *self, const char *attr, const SDL_Color *value)
Adds or replaces the given attribute with value.
Definition: Style.c:151

◆ initWithAttributes()

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

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}

◆ initWithRules()

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

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}
CollectionView * init(CollectionView *self, const SDL_Rect *frame)
Initializes this CollectionView with the specified frame and style.
Selectors are comprised of one or more SelectorSequences.
Definition: Selector.h:49

◆ isComputedEqual()

static _Bool isComputedEqual ( const Style self,
const Style other 
)
static

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

◆ isEqual()

static _Bool isEqual ( const Object *  self,
const Object *  other 
)
static
See also
Object::isEqual(const Object *, const Object *)

Definition at line 77 of file Style.c.

77 {
78
79 if (super(Object, self, isEqual, other)) {
80 return true;
81 }
82
83 if (other && $(other, isKindOfClass, _Style())) {
84
85 const Style *this = (Style *) self;
86 const Style *that = (Style *) other;
87
88 if ($((Object *) this->selectors, isEqual, (Object *) that->selectors)) {
89 return $((Object *) this->attributes, isEqual, (Object *) that->attributes);
90 }
91 }
92
93 return false;
94}
Class * _Style(void)
The Style archetype.
Definition: Style.c:537

◆ parse()

static Array * parse ( const char *  css)
static

Definition at line 420 of file Style.c.

420 {
421
422 MutableArray *styles = $$(MutableArray, array);
423 assert(styles);
424
425 if (css) {
426
427 StringReader *reader = $(alloc(StringReader), initWithCharacters, css);
428 assert(reader);
429
430 Style *style = NULL;
431 char *attr = NULL;
432
433 while (true) {
434
435 const Unicode *charset = style ? L"{:;}" : L"{}";
436 Unicode stop;
437
438 String *token = $(reader, readToken, charset, &stop);
439 if (token) {
440 switch (stop) {
441 case '{':
442 style = $(alloc(Style), initWithRules, token->chars);
443 assert(style);
444 break;
445 case ':':
446 if (style) {
447 attr = strtrim(token->chars);
448 assert(attr);
449 }
450 break;
451 case ';':
452 if (style && attr) {
453
454 ident value = parseValue(token);
455 $(style, addAttribute, attr, value);
456
457 free(attr);
458 attr = NULL;
459
460 release(value);
461 }
462 break;
463 case '}':
464 if (style) {
465 $(styles, addObject, style);
466 style = release(style);
467 }
468 break;
469 }
470
471 release(token);
472 } else {
473 break;
474 }
475 }
476
477 release(reader);
478 }
479
480 return (Array *) styles;
481}
static ident parseValue(String *string)
Definition: Style.c:356
Stylesheet * initWithCharacters(Stylesheet *self, const char *chars)
Initializes this Stylesheet with the given CSS definitions.
Definition: Stylesheet.c:124

◆ parseValue()

static ident parseValue ( String *  string)
static
Returns
The value Object parsed from the given String.

Definition at line 356 of file Style.c.

356 {
357
358 ident value = NULL;
359
360 assert(string);
361
362 String *trimmed = $(string, trimmedString);
363 assert(trimmed);
364
365 StringReader *reader = $(alloc(StringReader), initWithString, trimmed);
366 assert(reader);
367
368 const Unicode *charset = L", \n\t";
369 Unicode stop;
370
371 String *token = $(reader, readToken, charset, &stop);
372 if (token) {
373
374 NumberFormatter *formatter = $(alloc(NumberFormatter), initWithFormat, NULL);
375 assert(formatter);
376
377 Number *number = $(formatter, numberFromString, token);
378
379 if (stop == READER_EOF) {
380
381 if (number) {
382 value = retain(number);
383 } else if (strcmp("true", token->chars) == 0) {
384 value = $$(Boole, True);
385 } else if (strcmp("false", token->chars) == 0) {
386 value = $$(Boole, False);
387 } else {
388 value = $((Object *) token, copy);
389 }
390
391 } else if (number) {
392 value = $$(MutableArray, arrayWithCapacity, 4);
393
394 while (token) {
395
396 $((MutableArray *) value, addObject, parseValue(token));
397 release(token);
398
399 token = $(reader, readToken, charset, NULL);
400 }
401 } else {
402 value = retain(trimmed);
403 }
404
405 release(number);
406 release(formatter);
407 release(token);
408 }
409
410 release(reader);
411 release(trimmed);
412
413 return value;
414}
Stylesheet * initWithString(Stylesheet *self, const String *string)
Initializes this Stylesheet with the CSS definitions in string.
Definition: Stylesheet.c:195

◆ removeAllAttributes()

static void removeAllAttributes ( Style self)
static

Definition at line 495 of file Style.c.

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

◆ removeAttribute()

static void removeAttribute ( Style self,
const char *  attr 
)
static

Definition at line 487 of file Style.c.

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