ObjectivelyMVC
Object oriented MVC framework for SDL3 and GNU C
Loading...
Searching...
No Matches
TabViewController.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 "TabViewController.h"
27
28#define _Class _TabViewController
29
30#pragma mark - Object
31
35static void dealloc(Object *self) {
36
37 TabViewController *this = (TabViewController *) self;
38
39 release(this->tabView);
40
41 super(Object, self, dealloc);
42}
43
44#pragma mark - ViewController
45
49static void loadView(ViewController *self) {
50
51 super(ViewController, self, loadView);
52
54
55 TabViewController *this = (TabViewController *) self;
56
57 this->tabView = $(alloc(TabView), initWithFrame, NULL);
58 assert(this->tabView);
59
60 $(self->view, addSubview, (View *) this->tabView);
61}
62
66static void addChildViewController(ViewController *self, ViewController *childViewController) {
67
68 super(ViewController, self, addChildViewController, childViewController);
69
70 TabViewController *this = (TabViewController *) self;
71
72 TabViewItem *tab = $(alloc(TabViewItem), initWithView, childViewController->view);
73
74 $(this->tabView, addTab, tab);
75
76 release(tab);
77}
78
82static void removeChildViewController(ViewController *self, ViewController *childViewController) {
83
84 super(ViewController, self, removeChildViewController, childViewController);
85
86 TabViewController *this = (TabViewController *) self;
87
88 TabViewItem *tab = $(this, tabForViewController, childViewController);
89
90 $(this->tabView, removeTab, tab);
91}
92
93#pragma mark - TabViewController
94
100 return (TabViewController *) super(ViewController, self, init);
101}
102
106static bool tabForViewController_predicate(const ident obj, ident data) {
107 return ((TabViewItem *) obj)->view == data;
108}
109
114static TabViewItem *tabForViewController(const TabViewController *self, const ViewController *viewController) {
115
116 assert(viewController);
117
118 if (viewController->view) {
119 return $((Array *) self->tabView->tabs, find, tabForViewController_predicate, (ident) viewController->view);
120 }
121
122 return NULL;
123}
124
128static bool viewControllerForTab_predicate(const ident obj, ident data) {
129 return ((ViewController *) obj)->view == data;
130}
131
137
138 assert(tab);
139
140 if (tab->view) {
141 return $((Array *) self->viewController.childViewControllers, find, viewControllerForTab_predicate, tab->view);
142 }
143
144 return NULL;
145}
146
147#pragma mark - Class lifecycle
148
152static void initialize(Class *clazz) {
153
154 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
155
156 ((ViewControllerInterface *) clazz->interface)->loadView = loadView;
157 ((ViewControllerInterface *) clazz->interface)->addChildViewController = addChildViewController;
158 ((ViewControllerInterface *) clazz->interface)->removeChildViewController = removeChildViewController;
159
160 ((TabViewControllerInterface *) clazz->interface)->init = init;
161 ((TabViewControllerInterface *) clazz->interface)->tabForViewController = tabForViewController;
162 ((TabViewControllerInterface *) clazz->interface)->viewControllerForTab = viewControllerForTab;
163}
164
169Class *_TabViewController(void) {
170 static Class *clazz;
171 static Once once;
172
173 do_once(&once, {
174 clazz = _initialize(&(const ClassDef) {
175 .name = "TabViewController",
176 .superclass = _ViewController(),
177 .instanceSize = sizeof(TabViewController),
178 .interfaceOffset = offsetof(TabViewController, interface),
179 .interfaceSize = sizeof(TabViewControllerInterface),
181 });
182 });
183
184 return clazz;
185}
186
187#undef _Class
static Box * initWithFrame(Box *self, const SDL_Rect *frame)
Definition Box.c:92
static void addSubview(View *self, View *subview)
Definition PageView.c:49
static void addTab(TabView *self, TabViewItem *tab)
Definition TabView.c:133
static void removeTab(TabView *self, TabViewItem *tab)
Definition TabView.c:183
static void addChildViewController(ViewController *self, ViewController *childViewController)
static bool viewControllerForTab_predicate(const ident obj, ident data)
Predicate for viewControllerForTab.
static void loadView(ViewController *self)
static TabViewController * init(TabViewController *self)
static void removeChildViewController(ViewController *self, ViewController *childViewController)
static bool tabForViewController_predicate(const ident obj, ident data)
Predicate for tabViewItemFor.
static ViewController * viewControllerForTab(const TabViewController *self, const TabViewItem *tab)
static void dealloc(Object *self)
static TabViewItem * tabForViewController(const TabViewController *self, const ViewController *viewController)
static void initialize(Class *clazz)
Class * _TabViewController(void)
TabViewControllers arrange and manage their child ViewControllers in a tab view interface.
static TabViewItem * initWithView(TabViewItem *self, View *view)
Definition TabViewItem.c:78
@ ViewAutoresizingContain
Definition View.h:92
Class * _ViewController(void)
TabViewControllers arrange and manage their child ViewControllers in a tab view interface.
ViewController viewController
The superclass.
TabView * tabView
The TabView.
TabViewItem * tabForViewController(const TabViewController *, const ViewController *)
Returns the TabViewItem for the specified child ViewController, or NULL.
TabViews allow for the display of multiple pages of information within a single view.
Definition TabView.h:79
Array * tabs
The TabViewItems.
Definition TabView.h:115
TabViewItems embed Views in a TabView.
Definition TabViewItem.h:51
View * view
The View this TabViewItem embeds.
Definition TabViewItem.h:82
A ViewController manages a View and its descendants.
Array * childViewControllers
The child view controllers.
View * view
The main view.
Views are the fundamental building blocks of ObjectivelyMVC user interfaces.
Definition View.h:134
int autoresizingMask
The ViewAutoresizing bitmask.
Definition View.h:155