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 // Keep the expanded menu on-screen. The options are only un-hidden (and the menu
86 // sized to its full height) just above, so this is the earliest point the true
87 // height is known -- stateDidChange sees a collapsed, all-hidden menu. The menu
88 // opens downward from the control and is parented to the window root (its frame
89 // is window space); if it runs past the bottom edge, shift it up, clamped to the
90 // top. Idempotent: once its bottom sits at the window edge, re-running is a no-op.
91 View *menu = (View *) this->stackView;
92 if ($(control, isHighlighted) && menu->window) {
93
94 int windowHeight;
95 SDL_GetWindowSize(menu->window, NULL, &windowHeight);
96
97 const int bottom = menu->frame.y + menu->frame.h;
98 if (bottom > windowHeight) {
99 menu->frame.y -= (bottom - windowHeight);
100 if (menu->frame.y < 0) {
101 menu->frame.y = 0;
102 }
103 }
104 }
105 }
106
107 super(View, self, layoutSubviews);
108}
109
113static SDL_Size sizeThatFits(const View *self) {
114
115 const Select *this = (Select *) self;
116
117 SDL_Size size = super(View, self, sizeThatFits);
118
119 const Array *options = (Array *) this->options;
120 for (size_t i = 0; i < options->count; i++) {
121
122 const View *option = $(options, objectAtIndex, i);
123 const SDL_Size optionSize = $(option, sizeThatFits);
124
125 size.w = max(size.w, optionSize.w + self->padding.left + self->padding.right);
126 size.h = max(size.h, optionSize.h + self->padding.top + self->padding.bottom);
127 }
128
129 return size;
130}
131
132#pragma mark - Control
133
137static bool captureEvent(Control *self, const SDL_Event *event) {
138
139 Select *this = (Select *) self;
140
141 const Array *options = (Array *) this->options;
142
143 if (event->type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
144 if (self->state & ControlStateHighlighted) {
145 $((View *) self, becomeTouchResponder);
146 return true;
147 }
148 }
149
150 if (event->type == SDL_EVENT_MOUSE_BUTTON_UP) {
151 if (self->state & ControlStateHighlighted) {
152 self->state &= ~ControlStateHighlighted;
153
154 for (size_t i = 0; i < options->count; i++) {
155
156 Option *option = $(options, objectAtIndex, i);
157 if ($((View *) option, didReceiveEvent, event)) {
158 $(this, selectOption, option);
159 if (this->delegate.didSelectOption) {
160 this->delegate.didSelectOption(this, option);
161 }
162 return true;
163 }
164 }
165 } else {
167 return true;
168 }
169 }
170
171 if (event->type == SDL_EVENT_KEY_DOWN) {
172
173 switch (event->key.key) {
174
175 case SDLK_ESCAPE:
176 self->state &= ~ControlStateHighlighted;
177 return true;
178
179 case SDLK_RETURN:
180 case SDLK_KP_ENTER:
182 return true;
183
184 case SDLK_UP:
185 case SDLK_DOWN: {
186 Option *option = $(this, selectedOption);
187 if (option) {
188
189 ssize_t index = $(options, indexOfObject, option);
190 if (event->key.key == SDLK_UP) {
191 index = (index - 1 + options->count) % options->count;
192 } else {
193 index = (index + 1) % options->count;
194 }
195
196 option = $(options, objectAtIndex, index);
197
198 $(this, selectOption, option);
199 if (this->delegate.didSelectOption) {
200 this->delegate.didSelectOption(this, option);
201 }
202 }
203 return true;
204 }
205 }
206 }
207
208 return super(Control, self, captureEvent, event);
209}
210
214static void stateDidChange(Control *self) {
215
216 Select *this = (Select *) self;
217
218 if (self->selection == ControlSelectionSingle) {
219
220 View *stackView = (View *) this->stackView;
221
222 if ($(self, isHighlighted)) {
223
224 const SDL_Rect renderFrame = $((View *) self, renderFrame);
225
226 stackView->frame.x = renderFrame.x + self->view.padding.left;
227 stackView->frame.y = renderFrame.y + self->view.padding.top;
228
229 View *view = (View *) self;
230
231 while (view->superview) {
232 view = view->superview;
233 }
234
235 $(stackView, addClassName, "options");
236 $(view, addSubview, stackView);
237
238 stackView->nextResponder = (View *) self;
239
240 } else {
241
242 $(stackView, removeClassName, "options");
243 $((View *) self, addSubview, stackView);
244
245 stackView->nextResponder = NULL;
246 }
247
248 $(stackView, sizeToFit);
249 }
250
251 super(Control, self, stateDidChange);
252}
253
254#pragma mark - Select
255
259static void addOption_removeSubview(const Array *array, ident obj, ident data) {
260 $((View *) data, removeSubview, obj);
261}
262
266static void addOption_addSubview(const Array *array, ident obj, ident data) {
267 $((View *) data, addSubview, obj);
268}
269
274static void addOption(Select *self, const char *title, ident value) {
275
276 Option *option = $(alloc(Option), initWithTitle, title, value);
277 assert(option);
278
279 $(self->options, addObject, option);
280
281 $((Array *) self->options, enumerate, addOption_removeSubview, self->stackView);
282
283 if (self->comparator) {
284 $(self->options, sort, self->comparator);
285 }
286
287 $((Array *) self->options, enumerate, addOption_addSubview, self->stackView);
288
290 if ($(self, selectedOption) == NULL) {
291 $(option, setSelected, true);
292 }
293 }
294
295 option->view.nextResponder = (View *) self;
296
297 release(option);
298
299 self->control.view.needsLayout = true;
300}
301
306static Select *initWithFrame(Select *self, const SDL_Rect *frame) {
307
308 self = (Select *) super(Control, self, initWithFrame, frame);
309 if (self) {
310
312
313 self->options = $$(Array, arrayWithCapacity, 8);
314 assert(self->options);
315
316 self->stackView = $(alloc(StackView), initWithFrame, NULL);
317 assert(self->stackView);
318
319 $((View *) self, addSubview, (View *) self->stackView);
320 }
321
322 return self;
323}
324
328static bool optionWithValue_predicate(ident obj, ident data) {
329 return ((Option *) obj)->value == data;
330}
331
336static Option *optionWithValue(const Select *self, const ident value) {
337 return $((Array *) self->options, find, optionWithValue_predicate, value);
338}
339
343static void removeAllOptions_enumerate(const Array *array, ident obj, ident data) {
344 $((Select *) data, removeOption, obj);
345}
346
351static void removeAllOptions(Select *self) {
352 $((Array *) self->options, enumerate, removeAllOptions_enumerate, self);
353}
354
359static void removeOption(Select *self, Option *option) {
360
361 if ($((Array *) self->options, containsObject, option)) {
362
363 $(self->options, removeObject, option);
364 $((View *) self->stackView, removeSubview, (View *) option);
365
367 if ($(self, selectedOption) == NULL) {
368 Option *first_option = $((Array *) self->options, firstObject);
369 if (first_option) {
370 $(self, selectOption, first_option);
371 }
372 }
373 }
374
375 self->control.view.needsLayout = true;
376 }
377}
378
383static void removeOptionWithValue(Select *self, ident value) {
384
385 Option *option = (Option *) $(self, optionWithValue, value);
386 if (option) {
387 $(self, removeOption, option);
388 }
389}
390
394static void selectOption_enumerate(const Array *array, ident obj, ident data) {
395 $((Option *) obj, setSelected, false);
396}
397
402static void selectOption(Select *self, Option *option) {
403
405 $((Array *) self->options, enumerate, selectOption_enumerate, NULL);
406 }
407
408 if (option) {
409
410 if (option && option->isSelected == false) {
411 $(option, setSelected, true);
412 }
413 }
414
415 self->control.view.needsLayout = true;
416}
417
422static void selectOptionWithValue(Select *self, ident value) {
423
424 Option *option = $(self, optionWithValue, value);
425
426 $(self, selectOption, option);
427}
428
432static bool selectedOptions_predicate(ident obj, ident data) {
433 return ((Option *) obj)->isSelected;
434}
435
440static Option *selectedOption(const Select *self) {
441 return $((Array *) self->options, find, selectedOptions_predicate, NULL);
442}
443
448static Array *selectedOptions(const Select *self) {
449 return $((Array *) self->options, filteredArray, selectedOptions_predicate, NULL);
450}
451
452#pragma mark - Class lifecycle
453
457static void initialize(Class *clazz) {
458
459 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
460
461 ((ViewInterface *) clazz->interface)->init = init;
462 ((ViewInterface *) clazz->interface)->layoutSubviews = layoutSubviews;
463 ((ViewInterface *) clazz->interface)->sizeThatFits = sizeThatFits;
464
465 ((ControlInterface *) clazz->interface)->captureEvent = captureEvent;
466 ((ControlInterface *) clazz->interface)->stateDidChange = stateDidChange;
467
468 ((SelectInterface *) clazz->interface)->addOption = addOption;
469 ((SelectInterface *) clazz->interface)->initWithFrame = initWithFrame;
470 ((SelectInterface *) clazz->interface)->optionWithValue = optionWithValue;
471 ((SelectInterface *) clazz->interface)->removeAllOptions = removeAllOptions;
472 ((SelectInterface *) clazz->interface)->removeOption = removeOption;
473 ((SelectInterface *) clazz->interface)->removeOptionWithValue = removeOptionWithValue;
474 ((SelectInterface *) clazz->interface)->selectOption = selectOption;
475 ((SelectInterface *) clazz->interface)->selectOptionWithValue = selectOptionWithValue;
476 ((SelectInterface *) clazz->interface)->selectedOption = selectedOption;
477 ((SelectInterface *) clazz->interface)->selectedOptions = selectedOptions;
478}
479
484Class *_Select(void) {
485 static Class *clazz;
486 static Once once;
487
488 do_once(&once, {
489 clazz = _initialize(&(const ClassDef) {
490 .name = "Select",
491 .superclass = _Control(),
492 .instanceSize = sizeof(Select),
493 .interfaceOffset = offsetof(Select, interface),
494 .interfaceSize = sizeof(SelectInterface),
496 });
497 });
498
499 return clazz;
500}
501
502#undef _Class
503
static Button * initWithTitle(Button *self, const char *title)
Definition Button.c:175
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:359
static void selectOption(Select *self, Option *option)
Definition Select.c:402
static void removeAllOptions_enumerate(const Array *array, ident obj, ident data)
ArrayEnumerator for removeAllOptions.
Definition Select.c:343
static Option * selectedOption(const Select *self)
Definition Select.c:440
static void selectOptionWithValue(Select *self, ident value)
Definition Select.c:422
static void stateDidChange(Control *self)
Definition Select.c:214
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:394
static void addOption_removeSubview(const Array *array, ident obj, ident data)
ArrayEnumerator to remove Options from the stackView.
Definition Select.c:259
static void removeOptionWithValue(Select *self, ident value)
Definition Select.c:383
static SDL_Size sizeThatFits(const View *self)
Definition Select.c:113
static bool captureEvent(Control *self, const SDL_Event *event)
Definition Select.c:137
static void addOption(Select *self, const char *title, ident value)
Definition Select.c:274
static void removeAllOptions(Select *self)
Definition Select.c:351
static Array * selectedOptions(const Select *self)
Definition Select.c:448
static bool selectedOptions_predicate(ident obj, ident data)
Predicate for selectedOption and selectedOptions.
Definition Select.c:432
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:266
static Select * initWithFrame(Select *self, const SDL_Rect *frame)
Definition Select.c:306
static void initialize(Class *clazz)
Definition Select.c:457
static Option * optionWithValue(const Select *self, const ident value)
Definition Select.c:336
Class * _Select(void)
Definition Select.c:484
static void layoutSubviews(View *self)
Definition Select.c:61
static bool optionWithValue_predicate(ident obj, ident data)
Predicate function for optionWithValue.
Definition Select.c:328
A Control allowing users to select one or more Options.
static void removeClassName(View *self, const char *className)
Definition View.c:1333
static void addClassName(View *self, const char *className)
Definition View.c:159
static void layoutIfNeeded(View *self)
Definition View.c:1104
static void becomeTouchResponder(View *self)
Definition View.c:451
static bool didReceiveEvent(const View *self, const SDL_Event *event)
Definition View.c:657
static SDL_Rect renderFrame(const View *self)
Definition View.c:1444
static void enumerate(View *self, ViewEnumerator enumerator, ident data)
Definition View.c:738
static void sizeToFit(View *self)
Definition View.c:1756
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:336
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
SDL_Window * window
The window.
Definition View.h:277
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