ObjectivelyMVC
Object oriented MVC framework for SDL3 and GNU C
Loading...
Searching...
No Matches
Style Struct Reference

The Style type. More...

#include <Style.h>

Inheritance diagram for Style:

Public Member Functions

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

Data Fields

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

Protected Attributes

StyleInterface * interface [0]
 The interface type.
 

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 676 of file Style.c.

676 {
677 static Class *clazz;
678 static Once once;
679
680 do_once(&once, {
681 clazz = _initialize(&(const ClassDef) {
682 .name = "Style",
683 .superclass = _Object(),
684 .instanceSize = sizeof(Style),
685 .interfaceSize = sizeof(StyleInterface),
686 .initialize = initialize,
687 });
688 });
689
690 return clazz;
691}
static void initialize(Class *clazz)
Definition AtlasImage.c:74
The Style type.
Definition Style.h:43

◆ 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 196 of file Style.c.

196 {
197
198 assert(attr);
199 assert(value);
200
201 char *key = strtrim(attr);
202 assert(key);
203
204 setAttribute(self, key, value);
205
206 free(key);
207}
static void setAttribute(Style *self, const char *key, ident value)
Stores value under key, expanding it into longhands if key is a shorthand.
Definition Style.c:184

◆ 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 221 of file Style.c.

221 {
222
223 assert(attributes);
224
225 $(attributes, enumerateObjectsAndKeys, addAttributes_enumerate, self);
226}
static void addAttributes_enumerate(const Dictionary *dictionary, ident obj, ident key, ident data)
DictionaryEnumerator for addAttributes.
Definition Style.c:212
Dictionary * attributes
Definition Style.h:59

◆ 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 232 of file Style.c.

232 {
233 $(self, addAttribute, attr, $$(Boole, valueof, value));
234}
void addAttribute(Style *self, const char *attr, ident value)
Adds or replaces the given attribute with value.
Definition Style.c:196

◆ 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 240 of file Style.c.

240 {
241
242 String *string = $$(String, stringWithCharacters, value);
243
244 $(self, addAttribute, attr, string);
245
246 release(string);
247}

◆ 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 253 of file Style.c.

253 {
254
255 Number *r = $$(Number, numberWithValue, color->r);
256 Number *g = $$(Number, numberWithValue, color->g);
257 Number *b = $$(Number, numberWithValue, color->b);
258 Number *a = $$(Number, numberWithValue, color->a);
259
260 Array *array = $$(Array, arrayWithObjects, r, g, b, a, NULL);
261
262 $(self, addAttribute, attr, array);
263
264 release(array);
265 release(r);
266 release(g);
267 release(b);
268 release(a);
269}

◆ 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 275 of file Style.c.

275 {
276
277 Number *number = $$(Number, numberWithValue, value);
278
279 $(self, addAttribute, attr, number);
280
281 release(number);
282}

◆ 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 288 of file Style.c.

288 {
289
290 String *string = nameof(names, value);
291
292 $(self, addAttribute, attr, string);
293
294 release(string);
295}

◆ 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 301 of file Style.c.

301 {
302 $(self, addDoubleAttribute, attr, value);
303}
void addDoubleAttribute(Style *self, const char *attr, double value)
Adds or replaces the given attribute with value.
Definition Style.c:275

◆ 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 309 of file Style.c.

309 {
310 $(self, addDoubleAttribute, attr, value);
311}

◆ 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 317 of file Style.c.

317 {
318
319 Number *x = $$(Number, numberWithValue, value->x);
320 Number *y = $$(Number, numberWithValue, value->y);
321
322 Array *array = $$(Array, arrayWithObjects, x, y, NULL);
323
324 $(self, addAttribute, attr, array);
325
326 release(array);
327 release(x);
328 release(y);
329}

◆ 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 335 of file Style.c.

335 {
336
337 Number *x = $$(Number, numberWithValue, value->x);
338 Number *y = $$(Number, numberWithValue, value->y);
339 Number *w = $$(Number, numberWithValue, value->w);
340 Number *h = $$(Number, numberWithValue, value->h);
341
342 Array *array = $$(Array, arrayWithObjects, x, y, w, h, NULL);
343
344 $(self, addAttribute, attr, array);
345
346 release(array);
347 release(x);
348 release(y);
349 release(w);
350 release(h);
351}

◆ addSelector()

void addSelector ( Style *  self,
Selector *  selector 
)

Adds the given Selector to this Style.

Parameters
selfThe Style.
selectorThe Selector.

Definition at line 357 of file Style.c.

357 {
358
359 assert(selector);
360
361 $((Array *) self->selectors, addObject, selector);
362}
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 368 of file Style.c.

368 {
369
370 Number *w = $$(Number, numberWithValue, value->w);
371 Number *h = $$(Number, numberWithValue, value->h);
372
373 Array *array = $$(Array, arrayWithObjects, w, h, NULL);
374
375 $(self, addAttribute, attr, array);
376
377 release(array);
378 release(w);
379 release(h);
380}

◆ attributeValue()

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

Definition at line 386 of file Style.c.

386 {
387 return $((Dictionary *) self->attributes, objectForKeyPath, attr);
388}

◆ 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 394 of file Style.c.

394 {
395
396 self = $(self, initWithRules, NULL);
397 if (self) {
398 if (attributes) {
399 $(self, addAttributes, attributes);
400 }
401 }
402
403 return self;
404}
Style * initWithRules(Style *self, const char *rules)
Initializes this Style with the given CSS selector rules.
Definition Style.c:410
void addAttributes(Style *self, const Dictionary *attributes)
Adds or replaces the attribtues in attributes to this Style.
Definition Style.c:221

◆ 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 410 of file Style.c.

410 {
411
412 self = (Style *) super(Object, self, init);
413 if (self) {
414
415 self->selectors = $$(Selector, parse, rules);
416 assert(self->selectors);
417
418 for (size_t i = 0; i < self->selectors->count; i++) {
419 ((Selector *) self->selectors->elements[i])->style = self;
420 }
421
422 self->attributes = (Dictionary *) $$(Dictionary, dictionaryWithCapacity, 4);
423 assert(self->attributes);
424 }
425
426 return self;
427}
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:291

◆ 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 433 of file Style.c.

433 {
434
435 assert(other);
436
437 if (self->selectors->count == other->selectors->count) {
438
439 for (size_t i = 0; i < self->selectors->count; i++) {
440
441 const Selector *this = $(self->selectors, objectAtIndex, i);
442 const Selector *that = $(other->selectors, objectAtIndex, i);
443
444 if (strcmp(this->rule, that->rule)) {
445 return false;
446 }
447 }
448
449 return true;
450 }
451
452 return false;
453}
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 291 of file Selector.c.

291 {
292
293 Array *selectors = $$(Array, arrayWithCapacity, 4);
294 assert(selectors);
295
296 if (rules) {
297
298 const char *c = rules;
299 while (*c) {
300 const size_t size = strcspn(c, ",");
301 if (size) {
302 char *rule = calloc(1, size + 1);
303 assert(rule);
304
305 strncpy(rule, c, size);
306
307 Selector *selector = $(alloc(Selector), initWithRule, rule);
308 assert(selector);
309
310 $(selectors, addObject, selector);
311
312 release(selector);
313 free(rule);
314 }
315 c += size;
316 c += strspn(c, ", \t\n");
317 }
318 }
319
320 return (Array *) selectors;
321}
static SDL_Size size(const Image *self)
Definition Image.c:416
static Selector * initWithRule(Selector *self, const char *rule)
Definition Selector.c:190

◆ removeAllAttributes()

void removeAllAttributes ( Style *  self)

Removes all attributes from this Style.

Parameters
selfThe Style.

Definition at line 634 of file Style.c.

634 {
635 $((Dictionary *) self->attributes, removeAllObjects);
636}

◆ removeAttribute()

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

Removes the attribute with the given name.

Parameters
selfThe Style.
attrThe attribute name.

Definition at line 626 of file Style.c.

626 {
627 $((Dictionary *) self->attributes, removeObjectForKeyPath, attr);
628}

Field Documentation

◆ attributes

Dictionary* Style::attributes

The attributes.

Definition at line 59 of file Style.h.

◆ interface

StyleInterface* Style::interface[0]
protected

The interface type.

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: