ObjectivelyMVC
Object oriented MVC framework for SDL3 and GNU C
Loading...
Searching...
No Matches
CollectionItemView.c
Go to the documentation of this file.
1/*
2 * ObjectivelyMVC: Object oriented MVC framework for SDL3 and 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
84 $((View *) self, addSubview, (View *) self->imageView);
85
86 self->text = $(alloc(Text), initWithText, NULL, NULL);
87 assert(self->text);
88
89 $((View *) self, addSubview, (View *) self->text);
90
91 self->selectionOverlay = $(alloc(View), initWithFrame, NULL);
92 assert(self->selectionOverlay);
93
94 $(self->selectionOverlay, addClassName, "selectionOverlay");
95
96 $((View *) self, addSubview, self->selectionOverlay);
97
98 }
99
100 return self;
101}
102
107static void setSelected(CollectionItemView *self, bool isSelected) {
108 self->isSelected = isSelected;
109}
110
111#pragma mark - Class lifecycle
112
116static void initialize(Class *clazz) {
117
118 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
119
120 ((ViewInterface *) clazz->interface)->matchesSelector = matchesSelector;
121
122 ((CollectionItemViewInterface *) clazz->interface)->initWithFrame = initWithFrame;
123 ((CollectionItemViewInterface *) clazz->interface)->setSelected = setSelected;
124}
125
131 static Class *clazz;
132 static Once once;
133
134 do_once(&once, {
135 clazz = _initialize(&(const ClassDef) {
136 .name = "CollectionItemView",
137 .superclass = _View(),
138 .instanceSize = sizeof(CollectionItemView),
139 .interfaceSize = sizeof(CollectionItemViewInterface),
140 .initialize = initialize,
141 });
142 });
143
144 return clazz;
145}
146
147#undef _Class
148
static void setSelected(CollectionItemView *self, bool isSelected)
Class * _CollectionItemView(void)
static bool matchesSelector(const View *self, const SimpleSelector *simpleSelector)
static void dealloc(Object *self)
static void initialize(Class *clazz)
static CollectionItemView * initWithFrame(CollectionItemView *self, const SDL_Rect *frame)
The CollectionItemView type.
static bool isSelected(const Control *self)
Definition Control.c:358
static Label * initWithText(Label *self, const char *text, Font *font)
Definition Label.c:97
static void addSubview(View *self, View *subview)
Definition PageView.c:49
@ SimpleSelectorTypePseudo
static void addClassName(View *self, const char *className)
Definition View.c:183
Class * _View(void)
Definition View.c:2336
CollectionViewItems are a visual representation of an item within a CollectionView.
bool isSelected
True when this item is selected, false otherwise.
void setSelected(CollectionItemView *self, bool isSelected)
Sets the selected state of this item.
ImageView * imageView
The ImageView.
ImageViews render an Image in the context of a View hierarchy.
Definition ImageView.h:45
The SimpleSelector type.
SimpleSelectorType type
The SimpleSelectorType.
char * pattern
The pattern, as provided by the user.
Text rendered with TrueType fonts.
Definition Text.h:131
Views are the fundamental building blocks of ObjectivelyMVC user interfaces.
Definition View.h:225