ObjectivelyMVC 0.1.0
Object oriented MVC framework for OpenGL, SDL2 and GNU C
Box.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 "Box.h"
27
28#define _Class _Box
29
30#pragma mark - Object
31
35static void dealloc(Object *self) {
36
37 Box *this = (Box *) self;
38
39 release(this->contentView);
40 release(this->label);
41
42 super(Object, self, dealloc);
43}
44
45#pragma mark - View
46
50static void awakeWithDictionary(View *self, const Dictionary *dictionary) {
51
52 super(View, self, awakeWithDictionary, dictionary);
53
54 Box *this = (Box *) self;
55
56 const Inlet inlets[] = MakeInlets(
57 MakeInlet("contentView", InletTypeView, &this->contentView, NULL),
58 MakeInlet("label", InletTypeView, &this->label, NULL)
59 );
60
61 $(self, bind, inlets, dictionary);
62}
63
67static View *init(View *self) {
68 return (View *) $((Box *) self, initWithFrame, NULL);
69}
70
74static void layoutSubviews(View *self) {
75
76 super(View, self, layoutSubviews);
77
78 View *label = (View *) ((Box *) self)->label;
79 if (label->hidden == false) {
80
81 const SDL_Size size = $(label, sizeThatContains);
82 label->frame.y = -size.h * 0.5;
83 }
84}
85
86#pragma mark - Box
87
92static Box *initWithFrame(Box *self, const SDL_Rect *frame) {
93
94 self = (Box *) super(View, self, initWithFrame, frame);
95 if (self) {
96
97 self->contentView = $(alloc(StackView), initWithFrame, NULL);
98 assert(self->contentView);
99
100 $((View *) self->contentView, addClassName, "contentView");
101 $((View *) self->contentView, addClassName, "container");
102
103 $((View *) self, addSubview, (View *) self->contentView);
104
105 self->label = $(alloc(Label), initWithText, NULL, NULL);
106 assert(self->label);
107
108 View *label = (View *) self->label;
109
111
112 $((View *) self, addSubview, (View *) self->label);
113 }
114
115 return self;
116}
117
118#pragma mark - Class lifecycle
119
123static void initialize(Class *clazz) {
124
125 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
126
127 ((ViewInterface *) clazz->interface)->awakeWithDictionary = awakeWithDictionary;
128 ((ViewInterface *) clazz->interface)->init = init;
129 ((ViewInterface *) clazz->interface)->layoutSubviews = layoutSubviews;
130
131 ((BoxInterface *) clazz->interface)->initWithFrame = initWithFrame;
132}
133
138Class *_Box(void) {
139 static Class *clazz;
140 static Once once;
141
142 do_once(&once, {
143 clazz = _initialize(&(const ClassDef) {
144 .name = "Box",
145 .superclass = _View(),
146 .instanceSize = sizeof(Box),
147 .interfaceOffset = offsetof(Box, interface),
148 .interfaceSize = sizeof(BoxInterface),
150 });
151 });
152
153 return clazz;
154}
155
156#undef _Class
static void dealloc(Object *self)
Definition: Box.c:35
static void initialize(Class *clazz)
Definition: Box.c:123
A container View with a positioned Label.
@ 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
@ ViewAlignmentInternal
Definition: View.h:77
A container View with a positioned Label.
Definition: Box.h:44
StackView * contentView
The internal container.
Definition: Box.h:60
Class * _Box(void)
The Box archetype.
Definition: Box.c:138
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.
SDL_Size size(const Image *self)
Definition: Image.c:181
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
Label * initWithText(Label *self, const char *text, Font *font)
Initializes this Label with the given text and Font.
Definition: Label.c:96
The SDL_Size type.
Definition: Types.h:62
int h
Definition: Types.h:63
StackViews are containers that manage the arrangement of their subviews.
Definition: StackView.h:68
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.
ViewAlignment alignment
The alignment.
Definition: View.h:149
_Bool hidden
If true, this View is not drawn.
Definition: View.h:195
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 addClassName(View *self, const char *className)
Adds the given class name to this View.
Definition: View.c:120
void awakeWithDictionary(View *self, const Dictionary *dictionary)
Wakes this View with the specified Dictionary.
Definition: Box.c:50
layoutSubviews(View *self)
Performs layout for this View's immediate subviews.
Definition: Box.c:74
SDL_Size sizeThatContains(const View *self)
Definition: View.c:1423
SDL_Rect frame
The frame, relative to the superview.
Definition: View.h:190