ObjectivelyMVC
Object oriented MVC framework for SDL3 and GNU C
Loading...
Searching...
No Matches
Option.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 "Option.h"
27
28#define _Class _Option
29
30#pragma mark - Object
31
35static void dealloc(Object *self) {
36
37 Option *this = (Option *) self;
38
39 release(this->title);
40
41 super(Object, self, dealloc);
42}
43
47static String *description(const Object *self) {
48
49 View *this = (View *) self;
50 const SDL_Rect bounds = $(this, bounds);
51
52 String *classNames = $((Object *) this->classNames, description);
53 String *description = str("%s@%p \"%s\" %s [%d, %d, %d, %d]",
54 this->identifier ?: classnameof(self),
55 self,
56 ((Option *) self)->title->text,
57 classNames->chars,
58 bounds.x, bounds.y, bounds.w, bounds.h);
59
60 release(classNames);
61 return description;
62}
63
64#pragma mark - View
65
69static bool matchesSelector(const View *self, const SimpleSelector *simpleSelector) {
70
71 assert(simpleSelector);
72
73 const Option *this = (Option *) self;
74
75 switch (simpleSelector->type) {
77 if (strcmp("selected", simpleSelector->pattern) == 0) {
78 return this->isSelected;
79 }
80 break;
81 default:
82 break;
83 }
84
85 return super(View, self, matchesSelector, simpleSelector);
86}
87
88#pragma mark - Option
89
94static Option *initWithTitle(Option *self, const char *title, ident value) {
95
96 self = (Option *) super(View, self, initWithFrame, NULL);
97 if (self) {
98
99 self->title = $(alloc(Text), initWithText, title, NULL);
100 assert(self->title);
101
102 self->value = value;
103
104 $((View *) self, addSubview, (View *) self->title);
105 }
106
107 return self;
108}
109
114static void setSelected(Option *self, bool isSelected) {
115
116 if (self->isSelected != isSelected) {
117 self->isSelected = isSelected;
118
119 $((View *) self, invalidateStyle);
120
121 self->view.needsLayout = true;
122 }
123}
124
125#pragma mark - Class lifecycle
126
130static void initialize(Class *clazz) {
131
132 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
133 ((ObjectInterface *) clazz->interface)->description = description;
134
135 ((ViewInterface *) clazz->interface)->matchesSelector = matchesSelector;
136
137 ((OptionInterface *) clazz->interface)->initWithTitle = initWithTitle;
138 ((OptionInterface *) clazz->interface)->setSelected = setSelected;
139}
140
145Class *_Option(void) {
146 static Class *clazz;
147 static Once once;
148
149 do_once(&once, {
150 clazz = _initialize(&(const ClassDef) {
151 .name = "Option",
152 .superclass = _View(),
153 .instanceSize = sizeof(Option),
154 .interfaceOffset = offsetof(Option, interface),
155 .interfaceSize = sizeof(OptionInterface),
157 });
158 });
159
160 return clazz;
161}
162
163#undef _Class
164
static Box * initWithFrame(Box *self, const SDL_Rect *frame)
Definition Box.c:92
static bool isSelected(const Control *self)
Definition Control.c:335
static Label * initWithText(Label *self, const char *text, Font *font)
Definition Label.c:97
static void setSelected(Option *self, bool isSelected)
Definition Option.c:114
Class * _Option(void)
Definition Option.c:145
static bool matchesSelector(const View *self, const SimpleSelector *simpleSelector)
Definition Option.c:69
static String * description(const Object *self)
Definition Option.c:47
static void dealloc(Object *self)
Definition Option.c:35
static void initialize(Class *clazz)
Definition Option.c:130
static Option * initWithTitle(Option *self, const char *title, ident value)
Definition Option.c:94
Select Options.
static void addSubview(View *self, View *subview)
Definition PageView.c:49
@ SimpleSelectorTypePseudo
Class * _View(void)
Definition View.c:2067
static void invalidateStyle(View *self)
Definition View.c:1033
static SDL_Rect bounds(const View *self)
Definition View.c:492
Select Options.
Definition Option.h:41
Text * title
The title.
Definition Option.h:62
void setSelected(Option *self, bool isSelected)
Sets this Option's selected state.
Definition Option.c:114
bool isSelected
True if this Option is selected, false otherwise.
Definition Option.h:57
ident value
The value.
Definition Option.h:67
The SimpleSelector type.
SimpleSelectorType type
The SimpleSelectorType.
char * pattern
The pattern, as provided by the user.
Text rendered with TrueType fonts.
Definition Text.h:69
Views are the fundamental building blocks of ObjectivelyMVC user interfaces.
Definition View.h:134
bool needsLayout
If true, this View will layout its subviews before it is drawn.
Definition View.h:222