ObjectivelyMVC 0.1.0
Object oriented MVC framework for OpenGL, SDL2 and GNU C
CollectionItemView.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 "CollectionItemView.h"
27
28#define _Class _CollectionItemView
29
30#pragma mark - Object
31
35static void dealloc(Object *self) {
36
38
39 release(this->imageView);
40 release(this->selectionOverlay);
41 release(this->text);
42
43 super(Object, self, dealloc);
44}
45
46#pragma mark - View
47
51static _Bool matchesSelector(const View *self, const SimpleSelector *simpleSelector) {
52
53 assert(simpleSelector);
54
55 const CollectionItemView *this = (CollectionItemView *) self;
56 const char *pattern = simpleSelector->pattern;
57
58 switch (simpleSelector->type) {
60 if (strcmp("selected", pattern) == 0) {
61 return this->isSelected;
62 }
63 break;
64 default:
65 break;
66 }
67
68 return super(View, self, matchesSelector, simpleSelector);
69}
70
71#pragma mark - CollectionItemView
72
77static CollectionItemView *initWithFrame(CollectionItemView *self, const SDL_Rect *frame) {
78
79 self = (CollectionItemView *) super(View, self, initWithFrame, frame);
80 if (self) {
81 self->imageView = $(alloc(ImageView), initWithFrame, frame);
82 assert(self->imageView);
83
85
86 $((View *) self, addSubview, (View *) self->imageView);
87
88 self->text = $(alloc(Text), initWithText, NULL, NULL);
89 assert(self->text);
90
91 self->text->view.alignment = ViewAlignmentMiddleCenter;
92
93 $((View *) self, addSubview, (View *) self->text);
94
95 self->selectionOverlay = $(alloc(View), initWithFrame, NULL);
96 assert(self->selectionOverlay);
97
98 $(self->selectionOverlay, addClassName, "selectionOverlay");
99
100 self->selectionOverlay->autoresizingMask = ViewAutoresizingFill;
101
102 $((View *) self, addSubview, self->selectionOverlay);
103
104 self->view.clipsSubviews = true;
105 }
106
107 return self;
108}
109
114static void setSelected(CollectionItemView *self, _Bool isSelected) {
115 self->isSelected = isSelected;
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)->matchesSelector = matchesSelector;
128
129 ((CollectionItemViewInterface *) clazz->interface)->initWithFrame = initWithFrame;
130 ((CollectionItemViewInterface *) clazz->interface)->setSelected = setSelected;
131}
132
138 static Class *clazz;
139 static Once once;
140
141 do_once(&once, {
142 clazz = _initialize(&(const ClassDef) {
143 .name = "CollectionItemView",
144 .superclass = _View(),
145 .instanceSize = sizeof(CollectionItemView),
146 .interfaceOffset = offsetof(CollectionItemView, interface),
147 .interfaceSize = sizeof(CollectionItemViewInterface),
149 });
150 });
151
152 return clazz;
153}
154
155#undef _Class
156
static void dealloc(Object *self)
static void initialize(Class *clazz)
The CollectionItemView type.
@ SimpleSelectorTypePseudo
@ ViewAutoresizingFill
Definition: View.h:89
@ ViewAlignmentMiddleCenter
Definition: View.h:72
Box * initWithFrame(Box *self, const SDL_Rect *frame)
Initializes this Box with the given frame.
Definition: Box.c:92
CollectionViewItems are a visual representation of an item within a CollectionView.
Class * _CollectionItemView(void)
The CollectionItemView archetype.
void setSelected(CollectionItemView *self, _Bool isSelected)
Sets the selected state of this item.
ImageView * imageView
The ImageView.
_Bool isSelected
True when this item is selected, false otherwise.
_Bool isSelected(const Control *self)
Definition: Control.c:325
ImageViews render an Image in the context of a View hierarchy.
Definition: ImageView.h:43
View view
The superclass.
Definition: ImageView.h:48
Label * initWithText(Label *self, const char *text, Font *font)
Initializes this Label with the given text and Font.
Definition: Label.c:96
The SimpleSelector type.
SimpleSelectorType type
The SimpleSelectorType.
char * pattern
The pattern, as provided by the user.
Text rendered with TrueType fonts.
Definition: Text.h:41
Views are the fundamental building blocks of ObjectivelyMVC user interfaces.
Definition: View.h:133
_Bool clipsSubviews
If true, subviews will be clipped to this View's frame.
Definition: View.h:180
ViewAlignment alignment
The alignment.
Definition: View.h:149
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
int autoresizingMask
The ViewAutoresizing bitmask.
Definition: View.h:154
_Bool matchesSelector(const View *self, const SimpleSelector *simpleSelector)