ObjectivelyMVC
Object oriented MVC framework for SDL3 and GNU C
Loading...
Searching...
No Matches
TabView.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#include <string.h>
26
27#include <Objectively/String.h>
28
29#include "TabView.h"
30
31#define _Class _TabView
32
33#pragma mark - Object
34
38static void dealloc(Object *self) {
39
40 TabView *this = (TabView *) self;
41
42 memset(&this->delegate, 0, sizeof(this->delegate));
43
44 release(this->tabPageView);
45 release(this->tabSelectionView);
46 release(this->tabs);
47
48 super(Object, self, dealloc);
49}
50
51#pragma mark - View
52
56static void awakeWithDictionary_tabs(const Array *array, ident obj, ident data) {
57
58 const String *identifier = $((Dictionary *) obj, objectForKeyPath, "identifier");
59 assert(identifier);
60
61 TabViewItem *tab = $(alloc(TabViewItem), initWithIdentifier, identifier->chars);
62 assert(tab);
63
64 const Inlet inlets[] = MakeInlets(
65 MakeInlet("label", InletTypeView, &tab->label, NULL),
66 MakeInlet("view", InletTypeView, &tab->view, NULL)
67 );
68
69 $((View *) data, bind, inlets, obj);
70 $((TabView *) data, addTab, tab);
71
72 release(tab);
73}
74
78static void awakeWithDictionary(View *self, const Dictionary *dictionary) {
79
80 super(View, self, awakeWithDictionary, dictionary);
81
82 TabView *this = (TabView *) self;
83
84 const Inlet inlets[] = MakeInlets(
85 MakeInlet("tabPageView", InletTypeView, &this->tabPageView, NULL),
86 MakeInlet("tabSelectionView", InletTypeView, &this->tabSelectionView, NULL)
87 );
88
89 $(self, bind, inlets, dictionary);
90
91 const Array *tabs = $(dictionary, objectForKeyPath, "tabs");
92 if (tabs) {
93 $(tabs, enumerate, awakeWithDictionary_tabs, self);
94 }
95}
96
100static View *init(View *self) {
101 return (View *) $((TabView *) self, initWithFrame, NULL);
102}
103
107static bool respondToEvent(View *self, const SDL_Event *event) {
108
109 if (event->type == SDL_EVENT_MOUSE_BUTTON_UP) {
110
111 TabView *this = (TabView *) self;
112
113 const Array *tabs = (Array *) this->tabs;
114 for (size_t i = 0; i < tabs->count; i++) {
115
116 TabViewItem *tab = $(tabs, objectAtIndex, i);
117
118 if (tab->state & TabStateDisabled) {
119 continue;
120 }
121
122 if ($((View *) tab->label, didReceiveEvent, event)) {
123 $(this, selectTab, tab);
124 $(self, resignTouchResponder);
126 if (event->button.clicks) {
127 $(self, emitViewEvent, ViewEventClick, NULL);
128 }
129 return true;
130 }
131 }
132 }
133
134 return super(View, self, respondToEvent, event);
135}
136
137#pragma mark - TabView
138
143static void addTab(TabView *self, TabViewItem *tab) {
144
145 assert(tab);
146
147 $(self->tabs, addObject, tab);
148
149 $((View *) self->tabSelectionView, addSubview, (View *) tab->label);
150 $((View *) self->tabPageView, addSubview, tab->view);
151
152 if (self->delegate.didAddTab) {
153 self->delegate.didAddTab(self, tab);
154 }
155
156 if (self->selectedTab == NULL) {
157 $(self, selectTab, tab);
158 }
159}
160
165static TabView *initWithFrame(TabView *self, const SDL_Rect *frame) {
166
167 self = (TabView *) super(StackView, self, initWithFrame, frame);
168 if (self) {
169
170 self->tabPageView = $(alloc(PageView), initWithFrame, NULL);
171 assert(self->tabPageView);
172
173 self->tabs = $$(Array, array);
174 assert(self->tabs);
175
176 self->tabSelectionView = $(alloc(StackView), initWithFrame, NULL);
177 assert(self->tabSelectionView);
178
179 $((View *) self->tabSelectionView, addClassName, "tabSelectionView");
180 $((View *) self, addSubview, (View *) self->tabSelectionView);
181
182 $((View *) self->tabPageView, addClassName, "tabPageView");
183 $((View *) self, addSubview, (View *) self->tabPageView);
184 }
185
186 return self;
187}
188
193static void removeTab(TabView *self, TabViewItem *tab) {
194
195 assert(tab);
196
197 if (self->delegate.willRemoveTab) {
198 self->delegate.willRemoveTab(self, tab);
199 }
200
201 retain(tab);
202
203 $(self->tabs, removeObject, tab);
204
205 $((View *) self->tabSelectionView, removeSubview, (View *) tab->label);
206 $((View *) self->tabPageView, removeSubview, tab->view);
207
208 if (self->selectedTab == tab) {
209 $(self, selectTab, NULL);
210 }
211
212 release(tab);
213}
214
218static void selectTab_enumerate(const Array *array, ident obj, ident data) {
219
220 TabViewItem *tab = obj;
221
222 int state = tab->state;
223
224 if (tab == ((TabView *) data)->selectedTab) {
225 state |= TabStateSelected;
226 } else {
227 state &= ~TabStateSelected;
228 }
229
230 $(tab, setState, state);
231}
232
237static void selectTab(TabView *self, TabViewItem *tab) {
238
239 if (self->selectedTab != tab) {
240
241 const Array *tabs = (Array *) self->tabs;
242
243 if (tab) {
244 self->selectedTab = tab;
245 } else {
246 self->selectedTab = $(tabs, firstObject);
247 }
248
250
251 if (self->selectedTab) {
252 $(self->tabPageView, setCurrentPage, self->selectedTab->view);
253
254 if (self->delegate.didSelectTab) {
255 self->delegate.didSelectTab(self, self->selectedTab);
256 }
257 }
258
259 $((View *) self, setNeedsLayout);
260 }
261}
262
266static bool tabWithIdentifier_predicate(const ident obj, ident data) {
267
268 const TabViewItem *tab = obj;
269
270 if (tab->identifier) {
271 if (data) {
272 return !strcmp(tab->identifier, data);
273 } else {
274 return false;
275 }
276 } else {
277 return data == NULL;
278 }
279}
280
285static TabViewItem *tabWithIdentifier(const TabView *self, const char *identifier) {
286 return $((Array *) self->tabs, find, tabWithIdentifier_predicate, (ident) identifier);
287}
288
289#pragma mark - Class lifecycle
290
294static void initialize(Class *clazz) {
295
296 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
297
298 ((ViewInterface *) clazz->interface)->awakeWithDictionary = awakeWithDictionary;
299 ((ViewInterface *) clazz->interface)->init = init;
300 ((ViewInterface *) clazz->interface)->respondToEvent = respondToEvent;
301
302 ((TabViewInterface *) clazz->interface)->addTab = addTab;
303 ((TabViewInterface *) clazz->interface)->initWithFrame = initWithFrame;
304 ((TabViewInterface *) clazz->interface)->removeTab = removeTab;
305 ((TabViewInterface *) clazz->interface)->selectTab = selectTab;
306 ((TabViewInterface *) clazz->interface)->tabWithIdentifier = tabWithIdentifier;
307}
308
313Class *_TabView(void) {
314 static Class *clazz;
315 static Once once;
316
317 do_once(&once, {
318 clazz = _initialize(&(const ClassDef) {
319 .name = "TabView",
320 .superclass = _StackView(),
321 .instanceSize = sizeof(TabView),
322 .interfaceSize = sizeof(TabViewInterface),
323 .initialize = initialize,
324 });
325 });
326
327 return clazz;
328}
329
330#undef _Class
static void initialize(Class *clazz)
Definition AtlasImage.c:74
static View * init(View *self)
Definition Box.c:67
static void awakeWithDictionary(View *self, const Dictionary *dictionary)
Definition Box.c:50
static void dealloc(Object *self)
Definition Box.c:35
static Box * initWithFrame(Box *self, const SDL_Rect *frame)
Definition Box.c:92
static bool respondToEvent(View *self, const SDL_Event *event)
Definition Control.c:283
static void setCurrentPage(PageView *self, View *currentPage)
Definition PageView.c:130
Class * _StackView(void)
Definition StackView.c:358
static void addTab(TabView *self, TabViewItem *tab)
Definition TabView.c:143
Class * _TabView(void)
Definition TabView.c:313
static TabView * initWithFrame(TabView *self, const SDL_Rect *frame)
Definition TabView.c:165
static View * init(View *self)
Definition TabView.c:100
static void awakeWithDictionary(View *self, const Dictionary *dictionary)
Definition TabView.c:78
static bool respondToEvent(View *self, const SDL_Event *event)
Definition TabView.c:107
static void awakeWithDictionary_tabs(const Array *array, ident obj, ident data)
ArrayEnumerator for awaking TabViewItems.
Definition TabView.c:56
static void removeTab(TabView *self, TabViewItem *tab)
Definition TabView.c:193
static void selectTab_enumerate(const Array *array, ident obj, ident data)
ArrayEnumerator for selectTab.
Definition TabView.c:218
static void selectTab(TabView *self, TabViewItem *tab)
Definition TabView.c:237
static bool tabWithIdentifier_predicate(const ident obj, ident data)
NULL-safe Predicate for tabWithIdentifier.
Definition TabView.c:266
static void dealloc(Object *self)
Definition TabView.c:38
static TabViewItem * tabWithIdentifier(const TabView *self, const char *identifier)
Definition TabView.c:285
static void initialize(Class *clazz)
Definition TabView.c:294
Tabbed pages within a single View.
static void setState(TabViewItem *self, int state)
Definition TabViewItem.c:93
@ TabStateSelected
Definition TabViewItem.h:41
@ TabStateDisabled
Definition TabViewItem.h:42
static TableColumn * initWithIdentifier(TableColumn *self, const char *identifier)
Definition TableColumn.c:56
@ ViewEventMouseButtonUp
A View has received a mouse button up event.
Definition Types.h:85
@ ViewEventClick
A Control received one or more click events.
Definition Types.h:115
@ InletTypeView
Definition View+JSON.h:133
#define MakeInlets(...)
Creates a null-termianted array of Inlets.
Definition View+JSON.h:215
#define MakeInlet(name, type, dest, data)
Creates an Inlet with the specified parameters.
Definition View+JSON.h:210
static void resignTouchResponder(View *self)
Definition View.c:1692
static bool didReceiveEvent(const View *self, const SDL_Event *event)
Definition View.c:726
static void enumerate(View *self, ViewEnumerator enumerator, ident data)
Definition View.c:807
static void emitViewEvent(View *self, ViewEvent code, ident data)
Definition View.c:794
Inlets enable inbound data binding of View attributes through JSON.
Definition View+JSON.h:149
PageViews manage their subviews as pages in a book.
Definition PageView.h:60
StackViews are containers that manage the arrangement of their subviews.
Definition StackView.h:68
void(* didAddTab)(TabView *tabView, TabViewItem *tab)
Called when a tab is added.
Definition TabView.h:57
void(* didSelectTab)(TabView *tabView, TabViewItem *tab)
Called when a tab is selected.
Definition TabView.h:64
void(* willRemoveTab)(TabView *tabView, TabViewItem *tab)
Called when a tab is to be removed.
Definition TabView.h:71
TabViews allow for the display of multiple pages of information within a single view.
Definition TabView.h:79
PageView * tabPageView
The tab content container.
Definition TabView.h:105
TabViewItem * selectedTab
The selected TabViewItem.
Definition TabView.h:100
StackView * tabSelectionView
The tab selection container.
Definition TabView.h:110
TabView * initWithFrame(TabView *self, const SDL_Rect *frame)
Initializes this TabView with the specified frame.
Definition TabView.c:165
Array * tabs
The TabViewItems.
Definition TabView.h:115
TabViewDelegate delegate
The TabViewDelegate.
Definition TabView.h:95
void selectTab(TabView *self, TabViewItem *tab)
Selects the specified TabViewItem.
Definition TabView.c:237
TabViewItems embed Views in a TabView.
Definition TabViewItem.h:52
int state
The bit mask of TabState.
Definition TabViewItem.h:78
View * view
The View this TabViewItem embeds.
Definition TabViewItem.h:83
Label * label
The Label used to select this tab.
Definition TabViewItem.h:73
char * identifier
The identifier.
Definition TabViewItem.h:68
Views are the fundamental building blocks of ObjectivelyMVC user interfaces.
Definition View.h:225
void setNeedsLayout(View *self)
Marks this View as needing layout before it is next drawn.
Definition View.c:1867
void addSubview(View *self, View *subview)
Adds a subview to this view, to be drawn above its siblings.
Definition PageView.c:49
void addClassName(View *self, const char *className)
Adds the given class name to this View.
Definition View.c:183
char * identifier
An optional identifier.
Definition View.h:333
void enumerate(const View *self, ViewEnumerator enumerator, ident data)
SDL_Rect frame
The frame, relative to the superview.
Definition View.h:301
void removeSubview(View *self, View *subview)
Removes the given subview from this View.
Definition PageView.c:72