ObjectivelyMVC
Object oriented MVC framework for SDL3 and GNU C
Loading...
Searching...
No Matches
TabView.c File Reference
#include <assert.h>
#include <string.h>
#include <Objectively/String.h>
#include "TabView.h"

Go to the source code of this file.

Macros

#define _Class   _TabView
 

Functions

Class * _TabView (void)
 
static void addTab (TabView *self, TabViewItem *tab)
 
static void awakeWithDictionary (View *self, const Dictionary *dictionary)
 
static void awakeWithDictionary_tabs (const Array *array, ident obj, ident data)
 ArrayEnumerator for awaking TabViewItems.
 
static void dealloc (Object *self)
 
static Viewinit (View *self)
 
static void initialize (Class *clazz)
 
static TabViewinitWithFrame (TabView *self, const SDL_Rect *frame)
 
static void removeTab (TabView *self, TabViewItem *tab)
 
static void respondToEvent (View *self, const SDL_Event *event)
 
static void selectTab (TabView *self, TabViewItem *tab)
 
static void selectTab_enumerate (const Array *array, ident obj, ident data)
 ArrayEnumerator for selectTab.
 
static TabViewItemtabWithIdentifier (const TabView *self, const char *identifier)
 
static bool tabWithIdentifier_predicate (const ident obj, ident data)
 NULL-safe Predicate for tabWithIdentifier.
 

Macro Definition Documentation

◆ _Class

#define _Class   _TabView

Definition at line 31 of file TabView.c.

Function Documentation

◆ _TabView()

Class * _TabView ( void  )

Definition at line 307 of file TabView.c.

307 {
308 static Class *clazz;
309 static Once once;
310
311 do_once(&once, {
312 clazz = _initialize(&(const ClassDef) {
313 .name = "TabView",
314 .superclass = _StackView(),
315 .instanceSize = sizeof(TabView),
316 .interfaceOffset = offsetof(TabView, interface),
317 .interfaceSize = sizeof(TabViewInterface),
319 });
320 });
321
322 return clazz;
323}
Class * _StackView(void)
Definition StackView.c:296
static void initialize(Class *clazz)
Definition TabView.c:288
TabViews allow for the display of multiple pages of information within a single view.
Definition TabView.h:79

◆ addTab()

static void addTab ( TabView self,
TabViewItem tab 
)
static

Definition at line 137 of file TabView.c.

137 {
138
139 assert(tab);
140
141 $(self->tabs, addObject, tab);
142
143 $((View *) self->tabSelectionView, addSubview, (View *) tab->label);
144 $((View *) self->tabPageView, addSubview, tab->view);
145
146 if (self->delegate.didAddTab) {
147 self->delegate.didAddTab(self, tab);
148 }
149
150 if (self->selectedTab == NULL) {
151 $(self, selectTab, tab);
152 }
153}
static void addSubview(View *self, View *subview)
Definition PageView.c:49
static void selectTab(TabView *self, TabViewItem *tab)
Definition TabView.c:231
void(* didAddTab)(TabView *tabView, TabViewItem *tab)
Called when a tab is added.
Definition TabView.h:57
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
Array * tabs
The TabViewItems.
Definition TabView.h:115
TabViewDelegate delegate
The TabViewDelegate.
Definition TabView.h:95
View * view
The View this TabViewItem embeds.
Definition TabViewItem.h:83
Label * label
The Label used to select this tab.
Definition TabViewItem.h:73
Views are the fundamental building blocks of ObjectivelyMVC user interfaces.
Definition View.h:134

◆ awakeWithDictionary()

static void awakeWithDictionary ( View self,
const Dictionary *  dictionary 
)
static
See also
View::awakeWithDictionary(View *, const Dictionary *)

Definition at line 78 of file TabView.c.

78 {
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}
static void awakeWithDictionary(View *self, const Dictionary *dictionary)
Definition TabView.c:78
static void awakeWithDictionary_tabs(const Array *array, ident obj, ident data)
ArrayEnumerator for awaking TabViewItems.
Definition TabView.c:56
@ InletTypeView
Definition View+JSON.h:139
#define MakeInlets(...)
Creates a null-termianted array of Inlets.
Definition View+JSON.h:221
#define MakeInlet(name, type, dest, data)
Creates an Inlet with the specified parameters.
Definition View+JSON.h:216
static void enumerate(View *self, ViewEnumerator enumerator, ident data)
Definition View.c:738
Inlets enable inbound data binding of View attributes through JSON.
Definition View+JSON.h:155

◆ awakeWithDictionary_tabs()

static void awakeWithDictionary_tabs ( const Array *  array,
ident  obj,
ident  data 
)
static

ArrayEnumerator for awaking TabViewItems.

Definition at line 56 of file TabView.c.

56 {
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}
static void addTab(TabView *self, TabViewItem *tab)
Definition TabView.c:137
static TableColumn * initWithIdentifier(TableColumn *self, const char *identifier)
Definition TableColumn.c:56
TabViewItems embed Views in a TabView.
Definition TabViewItem.h:52

◆ dealloc()

static void dealloc ( Object *  self)
static
See also
Object::dealloc(Object *)

Definition at line 38 of file TabView.c.

38 {
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}
static void dealloc(Object *self)
Definition TabView.c:38

◆ init()

static View * init ( View self)
static
See also
View::init(View *)

Definition at line 100 of file TabView.c.

100 {
101 return (View *) $((TabView *) self, initWithFrame, NULL);
102}
static TabView * initWithFrame(TabView *self, const SDL_Rect *frame)
Definition TabView.c:159

◆ initialize()

static void initialize ( Class *  clazz)
static
See also
Class::initialize(Class *)

Definition at line 288 of file TabView.c.

288 {
289
290 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
291
292 ((ViewInterface *) clazz->interface)->awakeWithDictionary = awakeWithDictionary;
293 ((ViewInterface *) clazz->interface)->init = init;
294 ((ViewInterface *) clazz->interface)->respondToEvent = respondToEvent;
295
296 ((TabViewInterface *) clazz->interface)->addTab = addTab;
297 ((TabViewInterface *) clazz->interface)->initWithFrame = initWithFrame;
298 ((TabViewInterface *) clazz->interface)->removeTab = removeTab;
299 ((TabViewInterface *) clazz->interface)->selectTab = selectTab;
300 ((TabViewInterface *) clazz->interface)->tabWithIdentifier = tabWithIdentifier;
301}
static View * init(View *self)
Definition TabView.c:100
static void respondToEvent(View *self, const SDL_Event *event)
Definition TabView.c:107
static void removeTab(TabView *self, TabViewItem *tab)
Definition TabView.c:187
static TabViewItem * tabWithIdentifier(const TabView *self, const char *identifier)
Definition TabView.c:279
void removeTab(TabView *self, TabViewItem *tab)
Removes the specified TabViewItem from this TabView.
Definition TabView.c:187
void respondToEvent(View *self, const SDL_Event *event)
Responds to the specified event.
Definition Control.c:266

◆ initWithFrame()

static TabView * initWithFrame ( TabView self,
const SDL_Rect *  frame 
)
static

Definition at line 159 of file TabView.c.

159 {
160
161 self = (TabView *) super(StackView, self, initWithFrame, frame);
162 if (self) {
163
164 self->tabPageView = $(alloc(PageView), initWithFrame, NULL);
165 assert(self->tabPageView);
166
167 self->tabs = $$(Array, array);
168 assert(self->tabs);
169
170 self->tabSelectionView = $(alloc(StackView), initWithFrame, NULL);
171 assert(self->tabSelectionView);
172
173 $((View *) self->tabSelectionView, addClassName, "tabSelectionView");
174 $((View *) self, addSubview, (View *) self->tabSelectionView);
175
176 $((View *) self->tabPageView, addClassName, "tabPageView");
177 $((View *) self, addSubview, (View *) self->tabPageView);
178 }
179
180 return self;
181}
static void addClassName(View *self, const char *className)
Definition View.c:159
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

◆ removeTab()

static void removeTab ( TabView self,
TabViewItem tab 
)
static

Definition at line 187 of file TabView.c.

187 {
188
189 assert(tab);
190
191 if (self->delegate.willRemoveTab) {
192 self->delegate.willRemoveTab(self, tab);
193 }
194
195 retain(tab);
196
197 $(self->tabs, removeObject, tab);
198
199 $((View *) self->tabSelectionView, removeSubview, (View *) tab->label);
200 $((View *) self->tabPageView, removeSubview, tab->view);
201
202 if (self->selectedTab == tab) {
203 $(self, selectTab, NULL);
204 }
205
206 release(tab);
207}
static void removeSubview(View *self, View *subview)
Definition PageView.c:72
void(* willRemoveTab)(TabView *tabView, TabViewItem *tab)
Called when a tab is to be removed.
Definition TabView.h:71

◆ respondToEvent()

static void respondToEvent ( View self,
const SDL_Event *  event 
)
static
See also
View::respondToEvent(View *, const SDL_Event *)

Definition at line 107 of file TabView.c.

107 {
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 }
125 }
126 }
127
128 super(View, self, respondToEvent, event);
129}
@ TabStateDisabled
Definition TabViewItem.h:42
static bool didReceiveEvent(const View *self, const SDL_Event *event)
Definition View.c:657
int state
The bit mask of TabState.
Definition TabViewItem.h:78

◆ selectTab()

static void selectTab ( TabView self,
TabViewItem tab 
)
static

Definition at line 231 of file TabView.c.

231 {
232
233 if (self->selectedTab != tab) {
234
235 const Array *tabs = (Array *) self->tabs;
236
237 if (tab) {
238 self->selectedTab = tab;
239 } else {
240 self->selectedTab = $(tabs, firstObject);
241 }
242
243 $(tabs, enumerate, selectTab_enumerate, self);
244
245 if (self->selectedTab) {
246 $(self->tabPageView, setCurrentPage, self->selectedTab->view);
247
248 if (self->delegate.didSelectTab) {
249 self->delegate.didSelectTab(self, self->selectedTab);
250 }
251 }
252
253 self->stackView.view.needsLayout = true;
254 }
255}
static void setCurrentPage(PageView *self, View *currentPage)
Definition PageView.c:130
static void selectTab_enumerate(const Array *array, ident obj, ident data)
ArrayEnumerator for selectTab.
Definition TabView.c:212
View view
The superclass.
Definition StackView.h:73
void(* didSelectTab)(TabView *tabView, TabViewItem *tab)
Called when a tab is selected.
Definition TabView.h:64
StackView stackView
The superclass.
Definition TabView.h:84
bool needsLayout
If true, this View will layout its subviews before it is drawn.
Definition View.h:222

◆ selectTab_enumerate()

static void selectTab_enumerate ( const Array *  array,
ident  obj,
ident  data 
)
static

ArrayEnumerator for selectTab.

Definition at line 212 of file TabView.c.

212 {
213
214 TabViewItem *tab = obj;
215
216 int state = tab->state;
217
218 if (tab == ((TabView *) data)->selectedTab) {
219 state |= TabStateSelected;
220 } else {
221 state &= ~TabStateSelected;
222 }
223
224 $(tab, setState, state);
225}
static void setState(TabViewItem *self, int state)
Definition TabViewItem.c:93
@ TabStateSelected
Definition TabViewItem.h:41

◆ tabWithIdentifier()

static TabViewItem * tabWithIdentifier ( const TabView self,
const char *  identifier 
)
static

Definition at line 279 of file TabView.c.

279 {
280 return $((Array *) self->tabs, find, tabWithIdentifier_predicate, (ident) identifier);
281}
static bool tabWithIdentifier_predicate(const ident obj, ident data)
NULL-safe Predicate for tabWithIdentifier.
Definition TabView.c:260

◆ tabWithIdentifier_predicate()

static bool tabWithIdentifier_predicate ( const ident  obj,
ident  data 
)
static

NULL-safe Predicate for tabWithIdentifier.

Definition at line 260 of file TabView.c.

260 {
261
262 const TabViewItem *tab = obj;
263
264 if (tab->identifier) {
265 if (data) {
266 return !strcmp(tab->identifier, data);
267 } else {
268 return false;
269 }
270 } else {
271 return data == NULL;
272 }
273}
char * identifier
The identifier.
Definition TabViewItem.h:68