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) {
75 } else if ($(control, isHighlighted)) {
77 } else {
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 $((View *) self, setNeedsLayout);
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
346static void removeAllOptions(Select *self) {
347
348 Option *option;
349 while ((option = $((Array *) self->options, lastObject))) {
350 $(self, removeOption, option);
351 }
352}
353
358static void removeOption(Select *self, Option *option) {
359
360 if ($((Array *) self->options, containsObject, option)) {
361
362 $(self->options, removeObject, option);
363 $((View *) self->stackView, removeSubview, (View *) option);
364
366 if ($(self, selectedOption) == NULL) {
367 Option *first_option = $((Array *) self->options, firstObject);
368 if (first_option) {
369 $(self, selectOption, first_option);
370 }
371 }
372 }
373
374 $((View *) self, setNeedsLayout);
375 }
376}
377
382static void removeOptionWithValue(Select *self, ident value) {
383
384 Option *option = (Option *) $(self, optionWithValue, value);
385 if (option) {
386 $(self, removeOption, option);
387 }
388}
389
393static void selectOption_enumerate(const Array *array, ident obj, ident data) {
394 $((Option *) obj, setSelected, false);
395}
396
401static void selectOption(Select *self, Option *option) {
402
404 $((Array *) self->options, enumerate, selectOption_enumerate, NULL);
405 }
406
407 if (option) {
408
409 if (option && option->isSelected == false) {
410 $(option, setSelected, true);
411 }
412 }
413
414 $((View *) self, setNeedsLayout);
415}
416
421static void selectOptionWithValue(Select *self, ident value) {
422
423 Option *option = $(self, optionWithValue, value);
424
425 $(self, selectOption, option);
426}
427
431static bool selectedOptions_predicate(ident obj, ident data) {
432 return ((Option *) obj)->isSelected;
433}
434
439static Option *selectedOption(const Select *self) {
440 return $((Array *) self->options, find, selectedOptions_predicate, NULL);
441}
442
447static Array *selectedOptions(const Select *self) {
448 return $((Array *) self->options, filteredArray, selectedOptions_predicate, NULL);
449}
450
451#pragma mark - Class lifecycle
452
456static void initialize(Class *clazz) {
457
458 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
459
460 ((ViewInterface *) clazz->interface)->init = init;
461 ((ViewInterface *) clazz->interface)->layoutSubviews = layoutSubviews;
462 ((ViewInterface *) clazz->interface)->sizeThatFits = sizeThatFits;
463
464 ((ControlInterface *) clazz->interface)->captureEvent = captureEvent;
465 ((ControlInterface *) clazz->interface)->stateDidChange = stateDidChange;
466
467 ((SelectInterface *) clazz->interface)->addOption = addOption;
468 ((SelectInterface *) clazz->interface)->initWithFrame = initWithFrame;
469 ((SelectInterface *) clazz->interface)->optionWithValue = optionWithValue;
470 ((SelectInterface *) clazz->interface)->removeAllOptions = removeAllOptions;
471 ((SelectInterface *) clazz->interface)->removeOption = removeOption;
472 ((SelectInterface *) clazz->interface)->removeOptionWithValue = removeOptionWithValue;
473 ((SelectInterface *) clazz->interface)->selectOption = selectOption;
474 ((SelectInterface *) clazz->interface)->selectOptionWithValue = selectOptionWithValue;
475 ((SelectInterface *) clazz->interface)->selectedOption = selectedOption;
476 ((SelectInterface *) clazz->interface)->selectedOptions = selectedOptions;
477}
478
483Class *_Select(void) {
484 static Class *clazz;
485 static Once once;
486
487 do_once(&once, {
488 clazz = _initialize(&(const ClassDef) {
489 .name = "Select",
490 .superclass = _Control(),
491 .instanceSize = sizeof(Select),
492 .interfaceSize = sizeof(SelectInterface),
493 .initialize = initialize,
494 });
495 });
496
497 return clazz;
498}
499
500#undef _Class
501
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:414
static bool isHighlighted(const Control *self)
Definition Control.c:350
@ ControlSelectionSingle
Definition Control.h:56
@ ControlStateHighlighted
Definition Control.h:67
static SDL_Size size(const Image *self)
Definition Image.c:416
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:358
static void selectOption(Select *self, Option *option)
Definition Select.c:401
static Option * selectedOption(const Select *self)
Definition Select.c:439
static void selectOptionWithValue(Select *self, ident value)
Definition Select.c:421
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:393
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:382
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:346
static Array * selectedOptions(const Select *self)
Definition Select.c:447
static bool selectedOptions_predicate(ident obj, ident data)
Predicate for selectedOption and selectedOptions.
Definition Select.c:431
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:456
static Option * optionWithValue(const Select *self, const ident value)
Definition Select.c:336
Class * _Select(void)
Definition Select.c:483
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:1445
static void addClassName(View *self, const char *className)
Definition View.c:183
static void layoutIfNeeded(View *self)
Definition View.c:1174
static void becomeTouchResponder(View *self)
Definition View.c:511
static bool didReceiveEvent(const View *self, const SDL_Event *event)
Definition View.c:726
static void setNeedsLayout(View *self)
Definition View.c:1865
static void setVisibility(View *self, ViewVisibility visibility)
Definition View.c:1834
static SDL_Rect renderFrame(const View *self)
Definition View.c:1629
static void enumerate(View *self, ViewEnumerator enumerator, ident data)
Definition View.c:807
static void sizeToFit(View *self)
Definition View.c:2018
@ ViewVisibilityVisible
Definition View.h:121
@ ViewVisibilityHidden
Definition View.h:122
Controls are Views which capture and respond to events.
Definition Control.h:83
unsigned int state
The bit mask of ControlState.
Definition Control.h:117
ControlSelection selection
The ControlSelection.
Definition Control.h:123
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:225
View * superview
The super View.
Definition View.h:454
SDL_Window * window
The window.
Definition View.h:480
View * nextResponder
The next responder, or event handler, in the chain.
Definition View.h:392
ViewPadding padding
The padding.
Definition View.h:398
layoutSubviews(View *self)
Arranges this View's immediate subviews within its (already final) frame.
Definition Box.c:74
SDL_Rect frame
The frame, relative to the superview.
Definition View.h:301
int top
Definition View.h:170
int bottom
Definition View.h:170
int right
Definition View.h:170
int left
Definition View.h:170