ObjectivelyMVC
Object oriented MVC framework for SDL3 and GNU C
Loading...
Searching...
No Matches
Checkbox.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 "Checkbox.h"
27
28#include "../Assets/check.png.h"
29
30#define _Class _Checkbox
31
32static Image *_check;
33
34#pragma mark - Object
35
39static void dealloc(Object *self) {
40
41 Checkbox *this = (Checkbox *) self;
42
43 memset(&this->delegate, 0, sizeof(this->delegate));
44
45 release(this->box);
46 release(this->check);
47
48 super(Object, self, dealloc);
49}
50
51#pragma mark - View
52
56static void awakeWithDictionary(View *self, const Dictionary *dictionary) {
57
58 super(View, self, awakeWithDictionary, dictionary);
59
60 Checkbox *this = (Checkbox *) self;
61
62 const Inlet inlets[] = MakeInlets(
63 MakeInlet("check", InletTypeView, &this->check, NULL)
64 );
65
66 $(self, bind, inlets, dictionary);
67}
68
72static View *init(View *self) {
73 return (View *) $((Checkbox *) self, initWithFrame, NULL);
74}
75
76#pragma mark - Control
77
81static bool captureEvent(Control *self, const SDL_Event *event) {
82
83 Checkbox *this = (Checkbox *) self;
84
85 View *view = (View *) self;
86
87 const View *box = (View *) this->box;
88
89 switch (event->type) {
90 case SDL_EVENT_MOUSE_BUTTON_DOWN:
91 if ($(box, didReceiveEvent, event)) {
93 return true;
94 }
95 break;
96 case SDL_EVENT_MOUSE_BUTTON_UP:
97 if ($(box, didReceiveEvent, event)) {
99 self->state &= ~ControlStateHighlighted;
100 if (this->delegate.didToggle) {
101 this->delegate.didToggle(this);
102 }
103 $(view, emitViewEvent, ViewEventChange, NULL);
104 return true;
105 }
106 break;
107 case SDL_EVENT_KEY_DOWN:
108 switch (event->key.key) {
109 case SDLK_SPACE:
110 case SDLK_KP_SPACE:
111 case SDLK_RETURN:
112 case SDLK_KP_ENTER:
114 self->state &= ~ControlStateHighlighted;
115 if (this->delegate.didToggle) {
116 this->delegate.didToggle(this);
117 }
118 $(view, emitViewEvent, ViewEventChange, NULL);
119 return true;
120 }
121 }
122
123 return super(Control, self, captureEvent, event);
124}
125
126#pragma mark - Checkbox
127
132static Checkbox *initWithFrame(Checkbox *self, const SDL_Rect *frame) {
133
134 self = (Checkbox *) super(Control, self, initWithFrame, frame);
135 if (self) {
136
137 self->box = $(alloc(Control), initWithFrame, frame);
138 assert(self->box);
139
141
142 self->check = $(alloc(ImageView), initWithImage, _check);
143 assert(self->check);
144
146
147 $((View *) self->box, addSubview, (View *) self->check);
148 $((View *) self, addSubview, (View *) self->box);
149 }
150
151 return self;
152}
153
154#pragma mark - Class lifecycle
155
159static void initialize(Class *clazz) {
160
161 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
162
163 ((ViewInterface *) clazz->interface)->awakeWithDictionary = awakeWithDictionary;
164 ((ViewInterface *) clazz->interface)->init = init;
165
166 ((ControlInterface *) clazz->interface)->captureEvent = captureEvent;
167
168 ((CheckboxInterface *) clazz->interface)->initWithFrame = initWithFrame;
169
170 _check = $(alloc(Image), initWithBytes, check_png, check_png_len - 1);
171}
172
176static void destroy(Class *clazz) {
177 release(_check);
178}
179
184Class *_Checkbox(void) {
185 static Class *clazz;
186 static Once once;
187
188 do_once(&once, {
189 clazz = _initialize(&(const ClassDef) {
190 .name = "Checkbox",
191 .superclass = _Control(),
192 .instanceSize = sizeof(Checkbox),
193 .interfaceOffset = offsetof(Checkbox, interface),
194 .interfaceSize = sizeof(CheckboxInterface),
196 .destroy = destroy,
197 });
198 });
199
200 return clazz;
201}
202
203#undef _Class
static Button * initWithImage(Button *self, Image *image)
Definition Button.c:161
static void destroy(Class *clazz)
Definition Checkbox.c:176
static View * init(View *self)
Definition Checkbox.c:72
static void awakeWithDictionary(View *self, const Dictionary *dictionary)
Definition Checkbox.c:56
static bool captureEvent(Control *self, const SDL_Event *event)
Definition Checkbox.c:81
static Checkbox * initWithFrame(Checkbox *self, const SDL_Rect *frame)
Definition Checkbox.c:132
static void dealloc(Object *self)
Definition Checkbox.c:39
static Image * _check
Definition Checkbox.c:32
static void initialize(Class *clazz)
Definition Checkbox.c:159
Class * _Checkbox(void)
Definition Checkbox.c:184
Checkboxes are toggle Controls that respond to click events.
Class * _Control(void)
Definition Control.c:391
@ ControlStateHighlighted
Definition Control.h:67
@ ControlStateSelected
Definition Control.h:68
static Image * initWithBytes(Image *self, const uint8_t *bytes, size_t length)
Definition Image.c:88
static void addSubview(View *self, View *subview)
Definition PageView.c:49
@ ViewEventChange
A Control's input value has changed.
Definition Types.h:110
@ 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
static bool didReceiveEvent(const View *self, const SDL_Event *event)
Definition View.c:668
static void emitViewEvent(View *self, ViewEvent code, ident data)
Definition View.c:736
@ ViewAutoresizingFill
Definition View.h:90
@ ViewAlignmentMiddleCenter
Definition View.h:73
Checkboxes are toggle Controls that respond to click events.
Definition Checkbox.h:67
ImageView * check
The check.
Definition Checkbox.h:93
Control * box
The box.
Definition Checkbox.h:88
Controls are Views which capture and respond to events.
Definition Control.h:83
unsigned int state
The bit mask of ControlState.
Definition Control.h:104
View view
The superclass.
Definition Control.h:88
Image loading.
Definition Image.h:38
ImageViews render an Image in the context of a View hierarchy.
Definition ImageView.h:45
View view
The superclass.
Definition ImageView.h:50
Inlets enable inbound data binding of View attributes through JSON.
Definition View+JSON.h:155
Views are the fundamental building blocks of ObjectivelyMVC user interfaces.
Definition View.h:134
ViewAlignment alignment
The alignment.
Definition View.h:150
int autoresizingMask
The ViewAutoresizing bitmask.
Definition View.h:155
void awakeWithDictionary(View *, const Dictionary *)
Wakes this View with the specified Dictionary.
Definition Box.c:50
View * initWithFrame(View *self, const SDL_Rect *frame)
Initializes this View with the specified frame.
Definition View.c:989