ObjectivelyMVC 0.1.0
Object oriented MVC framework for OpenGL, SDL2 and GNU C
Theme.c
Go to the documentation of this file.
1/*
2 * ObjectivelyMVC: Object oriented MVC framework for OpenGL, SDL2 and GNU C.
3 * Copyright (C) 2014 Jay Dolan <jay@jaydolan.com>
4 *
5 * This software is provided 'as-is', without any express or implied
6 * warranty. In no event will the authors be held liable for any damages
7 * arising from the use of this software.
8 *
9 * Permission is granted to anyone to use this software for any purpose,
10 * including commercial applications, and to alter it and redistribute it
11 * freely, subject to the following restrictions:
12 *
13 * 1. The origin of this software must not be misrepresented; you must not
14 * claim that you wrote the original software. If you use this software
15 * in a product, an acknowledgment in the product documentation would be
16 * appreciated but is not required.
17 *
18 * 2. Altered source versions must be plainly marked as such, and must not be
19 * misrepresented as being the original software.
20 *
21 * 3. This notice may not be removed or altered from any source distribution.
22 */
23
24#include <assert.h>
25
26#include <Objectively/JSONSerialization.h>
27
28#include "Log.h"
29#include "Theme.h"
30#include "View.h"
31#include "WindowController.h"
32
33#define _Class _Theme
34
35#pragma mark - Object
36
40static void dealloc(Object *self) {
41
42 Theme *this = (Theme *) self;
43
44 release(this->stylesheets);
45
46 super(Object, self, dealloc);
47}
48
49#pragma mark - Theme
50
55static void addStylesheet(Theme *self, Stylesheet *stylesheet) {
56 $((MutableArray *) self->stylesheets, addObject, stylesheet);
57}
58
62static ident computeStyle_reduce(const ident obj, ident accumulator, ident data) {
63
64 const Stylesheet *stylesheet = obj;
65
66 const View *view = data;
67
68 const Array *selectors = (Array *) stylesheet->selectors;
69 for (size_t i = 0; i < selectors->count; i++) {
70
71 Selector *selector = $(selectors, objectAtIndex, i);
72 if ($(selector, matchesView, view)) {
73
74 assert(selector->style);
75
76 if (MVC_LogEnabled(SDL_LOG_PRIORITY_VERBOSE)) {
77
78 String *this = $((Object *) selector, description);
79 String *that = $((Object *) view, description);
80
81 MVC_LogVerbose("%s -> %s\n", this->chars, that->chars);
82
83 release(this);
84 release(that);
85 }
86
87 if ($(selector->style, attributeValue, "debug")) {
88 SDL_TriggerBreakpoint();
89 }
90
91 $((Style *) accumulator, addSelector, selector);
92 $((Style *) accumulator, addAttributes, selector->style->attributes);
93 }
94 }
95
96 return accumulator;
97}
98
103static Style *computeStyle(const Theme *self, const View *view) {
104
105 assert(view);
106
107 Style *style = $(alloc(Style), initWithAttributes, NULL);
108 assert(style);
109
110 return $((Array *) self->stylesheets, reduce, computeStyle_reduce, style, (ident) view);
111}
112
117static Theme *init(Theme *self) {
118
119 self = (Theme *) super(Object, self, init);
120 if (self) {
121
122 self->stylesheets = $$(MutableArray, arrayWithCapacity, 8);
123 assert(self->stylesheets);
124
126 }
127
128 return self;
129}
130
135static void removeStylesheet(Theme *self, Stylesheet *stylesheet) {
136 $((MutableArray *) self->stylesheets, removeObject, stylesheet);
137}
138
143static Theme *theme(SDL_Window *window) {
144
145 assert(window);
146
148 if (windowController) {
149 return windowController->theme;
150 }
151
152 return NULL;
153}
154
155#pragma mark - Class lifecycle
156
160static void initialize(Class *clazz) {
161
162 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
163
164 ((ThemeInterface *) clazz->interface)->addStylesheet = addStylesheet;
165 ((ThemeInterface *) clazz->interface)->computeStyle = computeStyle;
166 ((ThemeInterface *) clazz->interface)->init = init;
167 ((ThemeInterface *) clazz->interface)->removeStylesheet = removeStylesheet;
168 ((ThemeInterface *) clazz->interface)->theme = theme;
169}
170
175Class *_Theme(void) {
176 static Class *clazz;
177 static Once once;
178
179 do_once(&once, {
180 clazz = _initialize(&(const ClassDef) {
181 .name = "Theme",
182 .superclass = _Object(),
183 .instanceSize = sizeof(Theme),
184 .interfaceOffset = offsetof(Theme, interface),
185 .interfaceSize = sizeof(ThemeInterface),
187 });
188 });
189
190 return clazz;
191}
192
193#undef _Class
static String * description(const Object *self)
Definition: Label.c:47
View logging facilities via SDL_Log.
#define MVC_LogVerbose(fmt,...)
Definition: Log.h:43
#define MVC_LogEnabled(priority)
Definition: Log.h:37
static ident computeStyle_reduce(const ident obj, ident accumulator, ident data)
Reducer for computeStyle.
Definition: Theme.c:62
static void dealloc(Object *self)
Definition: Theme.c:40
static void initialize(Class *clazz)
Definition: Theme.c:160
The Theme type.
Views are the fundamental building blocks of ObjectivelyMVC user interfaces.
A WindowController manages a ViewController and its descendants within an SDL_Window.
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
Style * style
The Style.
Definition: Selector.h:82
_Bool matchesView(const Selector *self, View *view)
The Style type.
Definition: Style.h:43
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
Dictionary * attributes
Definition: Style.h:59
void addAttributes(Style *self, const Dictionary *attributes)
Adds or replaces the attribtues in attributes to this Style.
Definition: Style.c:119
ident attributeValue(const Style *self, const char *attr)
Definition: Style.c:284
Stylesheets are comprised of Selectors and Styles.
Definition: Stylesheet.h:44
Stylesheet * defaultStylesheet(void)
Definition: Stylesheet.c:95
Array * selectors
The Selectors, ordered by specificity.
Definition: Stylesheet.h:60
The Theme type.
Definition: Theme.h:51
Style * computeStyle(const Theme *self, View *view)
MutableArray * stylesheets
The Stylesheets, in order of priority.
Definition: Theme.h:67
void removeStylesheet(Theme *self, Stylesheet *stylesheet)
Removes the given Stylesheet from this Theme.
Definition: Theme.c:135
void addStylesheet(Theme *self, Stylesheet *stylesheet)
Adds the specified Stylesheet to this Theme.
Definition: Theme.c:55
Theme * theme(SDL_Window *window)
Definition: Theme.c:143
Class * _Theme(void)
The Theme archetype.
Definition: Theme.c:175
Views are the fundamental building blocks of ObjectivelyMVC user interfaces.
Definition: View.h:133
A WindowController manages a ViewController and its descendants within an SDL_Window.
Theme * theme
The Theme.
WindowController * windowController(SDL_Window *window)