ObjectivelyMVC
Object oriented MVC framework for SDL3 and GNU C
Loading...
Searching...
No Matches
Select.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 <Objectively/Array.h>
27
28#include "Select.h"
29
30#define _Class _Select
31
32#pragma mark - Object
33
37static void dealloc(Object *self) {
38
39 Select *this = (Select *) self;
40
41 memset(&this->delegate, 0, sizeof(this->delegate));
42
43 release(this->options);
44 release(this->stackView);
45
46 super(Object, self, dealloc);
47}
48
49#pragma mark - View
50
54static View *init(View *self) {
55 return (View *) $((Select *) self, initWithFrame, NULL);
56}
57
61static void layoutSubviews(View *self) {
62
63 Select *this = (Select *) self;
64
65 Control *control = (Control *) self;
66
67 if (control->selection == ControlSelectionSingle) {
68
69 const Array *options = (Array *) this->options;
70 for (size_t i = 0; i < options->count; i++) {
71
72 Option *option = $(options, objectAtIndex, i);
73 if (option->isSelected) {
74 option->view.hidden = false;
75 } else if ($(control, isHighlighted)) {
76 option->view.hidden = false;
77 } else {
78 option->view.hidden = true;
79 }
80 }
81
82 $((View *) this->stackView, sizeToFit);
83 $((View *) this->stackView, layoutIfNeeded);
84 }
85
86 super(View, self, layoutSubviews);
87}
88
92static SDL_Size sizeThatFits(const View *self) {
93
94 const Select *this = (Select *) self;
95
96 SDL_Size size = super(View, self, sizeThatFits);
97
98 const Array *options = (Array *) this->options;
99 for (size_t i = 0; i < options->count; i++) {
100
101 const View *option = $(options, objectAtIndex, i);
102 const SDL_Size optionSize = $(option, sizeThatFits);
103
104 size.w = max(size.w, optionSize.w + self->padding.left + self->padding.right);
105 size.h = max(size.h, optionSize.h + self->padding.top + self->padding.bottom);
106 }
107
108 return size;
109}
110
111#pragma mark - Control
112
116static bool captureEvent(Control *self, const SDL_Event *event) {
117
118 Select *this = (Select *) self;
119
120 const Array *options = (Array *) this->options;
121
122 if (event->type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
123 if (self->state & ControlStateHighlighted) {
124 $((View *) self, becomeTouchResponder);
125 return true;
126 }
127 }
128
129 if (event->type == SDL_EVENT_MOUSE_BUTTON_UP) {
130 if (self->state & ControlStateHighlighted) {
131 self->state &= ~ControlStateHighlighted;
132
133 for (size_t i = 0; i < options->count; i++) {
134
135 Option *option = $(options, objectAtIndex, i);
136 if ($((View *) option, didReceiveEvent, event)) {
137 $(this, selectOption, option);
138 if (this->delegate.didSelectOption) {
139 this->delegate.didSelectOption(this, option);
140 }
141 return true;
142 }
143 }
144 } else {
146 return true;
147 }
148 }
149
150 if (event->type == SDL_EVENT_KEY_DOWN) {
151
152 switch (event->key.key) {
153
154 case SDLK_ESCAPE:
155 self->state &= ~ControlStateHighlighted;
156 return true;
157
158 case SDLK_RETURN:
159 case SDLK_KP_ENTER:
161 return true;
162
163 case SDLK_UP:
164 case SDLK_DOWN: {
165 Option *option = $(this, selectedOption);
166 if (option) {
167
168 ssize_t index = $(options, indexOfObject, option);
169 if (event->key.key == SDLK_UP) {
170 index = (index - 1 + options->count) % options->count;
171 } else {
172 index = (index + 1) % options->count;
173 }
174
175 option = $(options, objectAtIndex, index);
176
177 $(this, selectOption, option);
178 if (this->delegate.didSelectOption) {
179 this->delegate.didSelectOption(this, option);
180 }
181 }
182 return true;
183 }
184 }
185 }
186
187 return super(Control, self, captureEvent, event);
188}
189
193static void stateDidChange(Control *self) {
194
195 Select *this = (Select *) self;
196
197 if (self->selection == ControlSelectionSingle) {
198
199 View *stackView = (View *) this->stackView;
200
201 if ($(self, isHighlighted)) {
202
203 const SDL_Rect renderFrame = $((View *) self, renderFrame);
204
205 stackView->frame.x = renderFrame.x + self->view.padding.left;
206 stackView->frame.y = renderFrame.y + self->view.padding.top;
207
208 View *view = (View *) self;
209
210 while (view->superview) {
211 view = view->superview;
212 }
213
214 $(stackView, addClassName, "options");
215 $(view, addSubview, stackView);
216
217 stackView->nextResponder = (View *) self;
218
219 } else {
220
221 $(stackView, removeClassName, "options");
222 $((View *) self, addSubview, stackView);
223
224 stackView->nextResponder = NULL;
225 }
226
227 $(stackView, sizeToFit);
228 }
229
230 super(Control, self, stateDidChange);
231}
232
233#pragma mark - Select
234
238static void addOption_removeSubview(const Array *array, ident obj, ident data) {
239 $((View *) data, removeSubview, obj);
240}
241
245static void addOption_addSubview(const Array *array, ident obj, ident data) {
246 $((View *) data, addSubview, obj);
247}
248
253static void addOption(Select *self, const char *title, ident value) {
254
255 Option *option = $(alloc(Option), initWithTitle, title, value);
256 assert(option);
257
258 $(self->options, addObject, option);
259
260 $((Array *) self->options, enumerate, addOption_removeSubview, self->stackView);
261
262 if (self->comparator) {
263 $(self->options, sort, self->comparator);
264 }
265
266 $((Array *) self->options, enumerate, addOption_addSubview, self->stackView);
267
269 if ($(self, selectedOption) == NULL) {
270 $(option, setSelected, true);
271 }
272 }
273
274 option->view.nextResponder = (View *) self;
275
276 release(option);
277
278 self->control.view.needsLayout = true;
279}
280
285static Select *initWithFrame(Select *self, const SDL_Rect *frame) {
286
287 self = (Select *) super(Control, self, initWithFrame, frame);
288 if (self) {
289
291
292 self->options = $$(Array, arrayWithCapacity, 8);
293 assert(self->options);
294
295 self->stackView = $(alloc(StackView), initWithFrame, NULL);
296 assert(self->stackView);
297
298 $((View *) self, addSubview, (View *) self->stackView);
299 }
300
301 return self;
302}
303
307static bool optionWithValue_predicate(ident obj, ident data) {
308 return ((Option *) obj)->value == data;
309}
310
315static Option *optionWithValue(const Select *self, const ident value) {
316 return $((Array *) self->options, find, optionWithValue_predicate, value);
317}
318
322static void removeAllOptions_enumerate(const Array *array, ident obj, ident data) {
323 $((Select *) data, removeOption, obj);
324}
325
330static void removeAllOptions(Select *self) {
331 $((Array *) self->options, enumerate, removeAllOptions_enumerate, self);
332}
333
338static void removeOption(Select *self, Option *option) {
339
340 if ($((Array *) self->options, containsObject, option)) {
341
342 $(self->options, removeObject, option);
343 $((View *) self->stackView, removeSubview, (View *) option);
344
346 if ($(self, selectedOption) == NULL) {
347 Option *first_option = $((Array *) self->options, firstObject);
348 if (first_option) {
349 $(self, selectOption, first_option);
350 }
351 }
352 }
353
354 self->control.view.needsLayout = true;
355 }
356}
357
362static void removeOptionWithValue(Select *self, ident value) {
363
364 Option *option = (Option *) $(self, optionWithValue, value);
365 if (option) {
366 $(self, removeOption, option);
367 }
368}
369
373static void selectOption_enumerate(const Array *array, ident obj, ident data) {
374 $((Option *) obj, setSelected, false);
375}
376
381static void selectOption(Select *self, Option *option) {
382
384 $((Array *) self->options, enumerate, selectOption_enumerate, NULL);
385 }
386
387 if (option) {
388
389 if (option && option->isSelected == false) {
390 $(option, setSelected, true);
391 }
392 }
393
394 self->control.view.needsLayout = true;
395}
396
401static void selectOptionWithValue(Select *self, ident value) {
402
403 Option *option = $(self, optionWithValue, value);
404
405 $(self, selectOption, option);
406}
407
411static bool selectedOptions_predicate(ident obj, ident data) {
412 return ((Option *) obj)->isSelected;
413}
414
419static Option *selectedOption(const Select *self) {
420 return $((Array *) self->options, find, selectedOptions_predicate, NULL);
421}
422
427static Array *selectedOptions(const Select *self) {
428 return $((Array *) self->options, filteredArray, selectedOptions_predicate, NULL);
429}
430
431#pragma mark - Class lifecycle
432
436static void initialize(Class *clazz) {
437
438 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
439
440 ((ViewInterface *) clazz->interface)->init = init;
441 ((ViewInterface *) clazz->interface)->layoutSubviews = layoutSubviews;
442 ((ViewInterface *) clazz->interface)->sizeThatFits = sizeThatFits;
443
444 ((ControlInterface *) clazz->interface)->captureEvent = captureEvent;
445 ((ControlInterface *) clazz->interface)->stateDidChange = stateDidChange;
446
447 ((SelectInterface *) clazz->interface)->addOption = addOption;
448 ((SelectInterface *) clazz->interface)->initWithFrame = initWithFrame;
449 ((SelectInterface *) clazz->interface)->optionWithValue = optionWithValue;
450 ((SelectInterface *) clazz->interface)->removeAllOptions = removeAllOptions;
451 ((SelectInterface *) clazz->interface)->removeOption = removeOption;
452 ((SelectInterface *) clazz->interface)->removeOptionWithValue = removeOptionWithValue;
453 ((SelectInterface *) clazz->interface)->selectOption = selectOption;
454 ((SelectInterface *) clazz->interface)->selectOptionWithValue = selectOptionWithValue;
455 ((SelectInterface *) clazz->interface)->selectedOption = selectedOption;
456 ((SelectInterface *) clazz->interface)->selectedOptions = selectedOptions;
457}
458
463Class *_Select(void) {
464 static Class *clazz;
465 static Once once;
466
467 do_once(&once, {
468 clazz = _initialize(&(const ClassDef) {
469 .name = "Select",
470 .superclass = _Control(),
471 .instanceSize = sizeof(Select),
472 .interfaceOffset = offsetof(Select, interface),
473 .interfaceSize = sizeof(SelectInterface),
475 });
476 });
477
478 return clazz;
479}
480
481#undef _Class
482
static Button * initWithTitle(Button *self, const char *title)
Definition Button.c:174
static void setSelected(CollectionItemView *self, bool isSelected)
Class * _Control(void)
Definition Control.c:391
static bool isHighlighted(const Control *self)
Definition Control.c:327
@ ControlSelectionSingle
Definition Control.h:56
@ ControlStateHighlighted
Definition Control.h:67
static SDL_Size size(const Image *self)
Definition Image.c:181
static void removeSubview(View *self, View *subview)
Definition PageView.c:72
static void addSubview(View *self, View *subview)
Definition PageView.c:49
static void removeOption(Select *self, Option *option)
Definition Select.c:338
static void selectOption(Select *self, Option *option)
Definition Select.c:381
static void removeAllOptions_enumerate(const Array *array, ident obj, ident data)
ArrayEnumerator for removeAllOptions.
Definition Select.c:322
static Option * selectedOption(const Select *self)
Definition Select.c:419
static void selectOptionWithValue(Select *self, ident value)
Definition Select.c:401
static void stateDidChange(Control *self)
Definition Select.c:193
static View * init(View *self)
Definition Select.c:54
static void selectOption_enumerate(const Array *array, ident obj, ident data)
ArrayEnumerator for enforcing ControlSelectionSingle.
Definition Select.c:373
static void addOption_removeSubview(const Array *array, ident obj, ident data)
ArrayEnumerator to remove Options from the stackView.
Definition Select.c:238
static void removeOptionWithValue(Select *self, ident value)
Definition Select.c:362
static SDL_Size sizeThatFits(const View *self)
Definition Select.c:92
static bool captureEvent(Control *self, const SDL_Event *event)
Definition Select.c:116
static void addOption(Select *self, const char *title, ident value)
Definition Select.c:253
static void removeAllOptions(Select *self)
Definition Select.c:330
static Array * selectedOptions(const Select *self)
Definition Select.c:427
static bool selectedOptions_predicate(ident obj, ident data)
Predicate for selectedOption and selectedOptions.
Definition Select.c:411
static void dealloc(Object *self)
Definition Select.c:37
static void addOption_addSubview(const Array *array, ident obj, ident data)
ArrayEnumerator to add Options to the stackView.
Definition Select.c:245
static Select * initWithFrame(Select *self, const SDL_Rect *frame)
Definition Select.c:285
static void initialize(Class *clazz)
Definition Select.c:436
static Option * optionWithValue(const Select *self, const ident value)
Definition Select.c:315
Class * _Select(void)
Definition Select.c:463
static void layoutSubviews(View *self)
Definition Select.c:61
static bool optionWithValue_predicate(ident obj, ident data)
Predicate function for optionWithValue.
Definition Select.c:307
A Control allowing users to select one or more Options.
static void removeClassName(View *self, const char *className)
Definition View.c:1344
static void addClassName(View *self, const char *className)
Definition View.c:159
static void layoutIfNeeded(View *self)
Definition View.c:1115
static void becomeTouchResponder(View *self)
Definition View.c:451
static bool didReceiveEvent(const View *self, const SDL_Event *event)
Definition View.c:668
static SDL_Rect renderFrame(const View *self)
Definition View.c:1455
static void enumerate(View *self, ViewEnumerator enumerator, ident data)
Definition View.c:749
static void sizeToFit(View *self)
Definition View.c:1767
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
ControlSelection selection
The ControlSelection.
Definition Control.h:109
Select Options.
Definition Option.h:41
bool isSelected
True if this Option is selected, false otherwise.
Definition Option.h:57
View view
The superclass.
Definition Option.h:46
A Control allowing users to select one or more Options.
Definition Select.h:63
Comparator comparator
An optional Comparator to sort Options.
Definition Select.h:79
Array * options
The Options.
Definition Select.h:89
StackView * stackView
The StackView for rendering the Options.
Definition Select.h:94
Option * optionWithValue(const Select *self, ident value)
Definition Select.c:315
Control control
The superclass.
Definition Select.h:68
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:134
View * superview
The super View.
Definition View.h:259
View * nextResponder
The next responder, or event handler, in the chain.
Definition View.h:229
bool needsLayout
If true, this View will layout its subviews before it is drawn.
Definition View.h:222
ViewPadding padding
The padding.
Definition View.h:234
layoutSubviews(View *self)
Performs layout for this View's immediate subviews.
Definition Box.c:74
SDL_Rect frame
The frame, relative to the superview.
Definition View.h:191
bool hidden
If true, this View is not drawn.
Definition View.h:196
int top
Definition View.h:101
int bottom
Definition View.h:101
int right
Definition View.h:101
int left
Definition View.h:101