ObjectivelyMVC 0.1.0
Object oriented MVC framework for OpenGL, SDL2 and GNU C
Label.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 "Label.h"
27
28#define _Class _Label
29
30#pragma mark - Object
31
35static void dealloc(Object *self) {
36
37 Label *this = (Label *) self;
38
39 release(this->text);
40
41 super(Object, self, dealloc);
42}
43
47static String *description(const Object *self) {
48
49 View *this = (View *) self;
50
51 String *classNames = $((Object *) this->classNames, description);
52 String *description = str("%s@%p \"%s\" %s [%d, %d, %d, %d]",
53 this->identifier ?: classnameof(self),
54 self,
55 ((Label *) self)->text->text,
56 classNames->chars,
57 this->frame.x, this->frame.y, this->frame.w, this->frame.h);
58
59 release(classNames);
60 return description;
61}
62
63#pragma mark - View
64
68static void awakeWithDictionary(View *self, const Dictionary *dictionary) {
69
70 super(View, self, awakeWithDictionary, dictionary);
71
72 Label *this = (Label *) self;
73
74 const Inlet inlets[] = MakeInlets(
75 MakeInlet("text", InletTypeView, &this->text, NULL)
76 );
77
78 $(self, bind, inlets, dictionary);
79
80 self->needsLayout = true;
81}
82
86static View *init(View *self) {
87 return (View *) $((Label *) self, initWithText, NULL, NULL);
88}
89
90#pragma mark - Label
91
96static Label *initWithText(Label *self, const char *text, Font *font) {
97
98 self = (Label *) super(View, self, initWithFrame, NULL);
99 if (self) {
100
101 self->text = $(alloc(Text), initWithText, text, font);
102 assert(self->text);
103
104 $((View *) self, addSubview, (View *) self->text);
105 }
106
107 return self;
108}
109
110#pragma mark - Class lifecycle
111
115static void initialize(Class *clazz) {
116
117 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
118 ((ObjectInterface *) clazz->interface)->description = description;
119
120 ((ViewInterface *) clazz->interface)->awakeWithDictionary = awakeWithDictionary;
121 ((ViewInterface *) clazz->interface)->init = init;
122
123 ((LabelInterface *) clazz->interface)->initWithText = initWithText;
124}
125
130Class *_Label(void) {
131 static Class *clazz;
132 static Once once;
133
134 do_once(&once, {
135 clazz = _initialize(&(const ClassDef) {
136 .name = "Label",
137 .superclass = _View(),
138 .instanceSize = sizeof(Label),
139 .interfaceOffset = offsetof(Label, interface),
140 .interfaceSize = sizeof(LabelInterface),
142 });
143 });
144
145 return clazz;
146}
147
148#undef _Class
static String * description(const Object *self)
Definition: Label.c:47
static void dealloc(Object *self)
Definition: Label.c:35
static void initialize(Class *clazz)
Definition: Label.c:115
The Label type.
@ InletTypeView
Definition: View+JSON.h:139
#define MakeInlets(...)
Creates a null-termianted array of Inlets.
Definition: View+JSON.h:221
#define MakeInlet(name, type, dest, data)
Creates an Inlet with the specified parameters.
Definition: View+JSON.h:216
Box * initWithFrame(Box *self, const SDL_Rect *frame)
Initializes this Box with the given frame.
Definition: Box.c:92
CollectionView * init(CollectionView *self, const SDL_Rect *frame)
Initializes this CollectionView with the specified frame and style.
TrueType fonts.
Definition: Font.h:63
Inlets enable inbound data binding of View attributes through JSON.
Definition: View+JSON.h:155
Labels provide a configurable container for Text.
Definition: Label.h:40
Text * text
The Label Text.
Definition: Label.h:56
Label * initWithText(Label *self, const char *text, Font *font)
Initializes this Label with the given text and Font.
Definition: Label.c:96
Class * _Label(void)
The Label archetype.
Definition: Label.c:130
Text rendered with TrueType fonts.
Definition: Text.h:41
Views are the fundamental building blocks of ObjectivelyMVC user interfaces.
Definition: View.h:133
_Bool bind(View *self, const Inlet *inlets, const Dictionary *dictionary)
Performs data binding for the Inlets described in dictionary.
_Bool needsLayout
If true, this View will layout its subviews before it is drawn.
Definition: View.h:221
Class * _View(void)
The View archetype.
Definition: View.c:1769
void addSubview(View *self, View *subview)
Adds a subview to this view, to be drawn above its siblings.
Definition: PageView.c:35
void awakeWithDictionary(View *self, const Dictionary *dictionary)
Wakes this View with the specified Dictionary.
Definition: Box.c:50