ObjectivelyMVC
Object oriented MVC framework for SDL3 and GNU C
Loading...
Searching...
No Matches
PageView.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 "PageView.h"
27
28#define _Class _PageView
29
30#pragma mark - Object
31
35static void dealloc(Object *self) {
36
37 PageView *this = (PageView *) self;
38
39 memset(&this->delegate, 0, sizeof(this->delegate));
40
41 super(Object, self, dealloc);
42}
43
44#pragma mark - View
45
49static void addSubview(View *self, View *subview) {
50
51 super(View, self, addSubview, subview);
52
53 PageView *this = (PageView *) self;
54
55 subview->hidden = true;
56
57 if (this->currentPage == NULL) {
58 $(this, setCurrentPage, subview);
59 }
60}
61
65static View *init(View *self) {
66 return (View *) $((PageView *) self, initWithFrame, NULL);
67}
68
72static void removeSubview(View *self, View *subview) {
73
74 PageView *this = (PageView *) self;
75
76 retain(subview);
77
78 super(View, self, removeSubview, subview);
79
80 subview->hidden = false;
81
82 if (subview == this->currentPage) {
83 $(this, setCurrentPage, NULL);
84 }
85
86 release(subview);
87}
88
92static Array *visibleSubviews(const View *self) {
93 return $$(Array, arrayWithArray, (Array *) self->subviews);
94}
95
96#pragma mark - PageView
97
102static PageView *initWithFrame(PageView *self, const SDL_Rect *frame) {
103
104 self = (PageView *) super(View, self, initWithFrame, frame);
105 if (self) {
106 $((View *) self, addClassName, "container");
107 }
108
109 return self;
110}
111
115static void setCurrentPage_enumerate(const Array *array, ident obj, ident data) {
116
117 View *subview = obj;
118
119 if (subview == ((PageView *) data)->currentPage) {
120 subview->hidden = false;
121 } else {
122 subview->hidden = true;
123 }
124}
125
130static void setCurrentPage(PageView *self, View *currentPage) {
131
132 if (currentPage != self->currentPage) {
133
134 const Array *subviews = (Array *) self->view.subviews;
135
136 if (currentPage) {
137 self->currentPage = currentPage;
138 } else {
139 self->currentPage = $(subviews, firstObject);
140 }
141
142 $(subviews, enumerate, setCurrentPage_enumerate, self);
143
144 if (self->currentPage) {
145
146 if (self->delegate.didSetCurrentPage) {
147 self->delegate.didSetCurrentPage(self);
148 }
149 }
150
151 self->view.needsLayout = true;
152 }
153}
154
155#pragma mark - Class lifecycle
156
160static void initialize(Class *clazz) {
161
162 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
163
164 ((ViewInterface *) clazz->interface)->addSubview = addSubview;
165 ((ViewInterface *) clazz->interface)->init = init;
166 ((ViewInterface *) clazz->interface)->removeSubview = removeSubview;
167 ((ViewInterface *) clazz->interface)->visibleSubviews = visibleSubviews;
168
169 ((PageViewInterface *) clazz->interface)->initWithFrame = initWithFrame;
170 ((PageViewInterface *) clazz->interface)->setCurrentPage = setCurrentPage;
171}
172
177Class *_PageView(void) {
178 static Class *clazz;
179 static Once once;
180
181 do_once(&once, {
182 clazz = _initialize(&(const ClassDef) {
183 .name = "PageView",
184 .superclass = _View(),
185 .instanceSize = sizeof(PageView),
186 .interfaceOffset = offsetof(PageView, interface),
187 .interfaceSize = sizeof(PageViewInterface),
189 });
190 });
191
192 return clazz;
193}
194
195#undef _Class
Class * _PageView(void)
Definition PageView.c:177
static View * init(View *self)
Definition PageView.c:65
static void setCurrentPage(PageView *self, View *currentPage)
Definition PageView.c:130
static void setCurrentPage_enumerate(const Array *array, ident obj, ident data)
ArrayEnumerator for setCurrentPage.
Definition PageView.c:115
static void removeSubview(View *self, View *subview)
Definition PageView.c:72
static Array * visibleSubviews(const View *self)
Definition PageView.c:92
static void dealloc(Object *self)
Definition PageView.c:35
static void addSubview(View *self, View *subview)
Definition PageView.c:49
static void initialize(Class *clazz)
Definition PageView.c:160
static PageView * initWithFrame(PageView *self, const SDL_Rect *frame)
Definition PageView.c:102
PageViews manage their subviews as pages in a book.
static void addClassName(View *self, const char *className)
Definition View.c:159
Class * _View(void)
Definition View.c:2067
static void enumerate(View *self, ViewEnumerator enumerator, ident data)
Definition View.c:749
void(* didSetCurrentPage)(PageView *pageView)
Called when the current page is set.
Definition PageView.h:52
PageViews manage their subviews as pages in a book.
Definition PageView.h:60
View view
The superclass.
Definition PageView.h:65
void setCurrentPage(PageView *self, View *currentPage)
Presents the specified subview as the current page of this PageView.
Definition PageView.c:130
View * currentPage
The index of the current page.
Definition PageView.h:76
PageViewDelegate delegate
The delegate.
Definition PageView.h:81
Views are the fundamental building blocks of ObjectivelyMVC user interfaces.
Definition View.h:134
Array * subviews
The immediate subviews.
Definition View.h:253
bool needsLayout
If true, this View will layout its subviews before it is drawn.
Definition View.h:222
void removeSubview(View *self, View *subview)
Removes the given subview from this View.
Definition PageView.c:72
bool hidden
If true, this View is not drawn.
Definition View.h:196