ObjectivelyMVC 0.1.0
Object oriented MVC framework for OpenGL, SDL2 and GNU C
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
PageView.c
Go to the documentation of this file.
1/*
2 * ObjectivelyMVC: Object oriented MVC framework for OpenGL, SDL2 and GNU 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 - View
31
35static void addSubview(View *self, View *subview) {
36
37 super(View, self, addSubview, subview);
38
39 PageView *this = (PageView *) self;
40
41 subview->hidden = true;
42
43 if (this->currentPage == NULL) {
44 $(this, setCurrentPage, subview);
45 }
46}
47
51static View *init(View *self) {
52 return (View *) $((PageView *) self, initWithFrame, NULL);
53}
54
58static void removeSubview(View *self, View *subview) {
59
60 PageView *this = (PageView *) self;
61
62 retain(subview);
63
64 super(View, self, removeSubview, subview);
65
66 subview->hidden = false;
67
68 if (subview == this->currentPage) {
69 $(this, setCurrentPage, NULL);
70 }
71
72 release(subview);
73}
74
78static Array *visibleSubviews(const View *self) {
79 return $$(Array, arrayWithArray, (Array *) self->subviews);
80}
81
82#pragma mark - PageView
83
88static PageView *initWithFrame(PageView *self, const SDL_Rect *frame) {
89
90 self = (PageView *) super(View, self, initWithFrame, frame);
91 if (self) {
92 $((View *) self, addClassName, "container");
93 }
94
95 return self;
96}
97
101static void setCurrentPage_enumerate(const Array *array, ident obj, ident data) {
102
103 View *subview = obj;
104
105 if (subview == ((PageView *) data)->currentPage) {
106 subview->hidden = false;
107 } else {
108 subview->hidden = true;
109 }
110}
111
116static void setCurrentPage(PageView *self, View *currentPage) {
117
118 if (currentPage != self->currentPage) {
119
120 const Array *subviews = (Array *) self->view.subviews;
121
122 if (currentPage) {
123 self->currentPage = currentPage;
124 } else {
125 self->currentPage = $(subviews, firstObject);
126 }
127
128 $(subviews, enumerateObjects, setCurrentPage_enumerate, self);
129
130 if (self->currentPage) {
131
132 if (self->delegate.didSetCurrentPage) {
133 self->delegate.didSetCurrentPage(self);
134 }
135 }
136
137 self->view.needsLayout = true;
138 }
139}
140
141#pragma mark - Class lifecycle
142
146static void initialize(Class *clazz) {
147
148 ((ViewInterface *) clazz->interface)->addSubview = addSubview;
149 ((ViewInterface *) clazz->interface)->init = init;
150 ((ViewInterface *) clazz->interface)->removeSubview = removeSubview;
151 ((ViewInterface *) clazz->interface)->visibleSubviews = visibleSubviews;
152
153 ((PageViewInterface *) clazz->interface)->initWithFrame = initWithFrame;
154 ((PageViewInterface *) clazz->interface)->setCurrentPage = setCurrentPage;
155}
156
161Class *_PageView(void) {
162 static Class *clazz;
163 static Once once;
164
165 do_once(&once, {
166 clazz = _initialize(&(const ClassDef) {
167 .name = "PageView",
168 .superclass = _View(),
169 .instanceSize = sizeof(PageView),
170 .interfaceOffset = offsetof(PageView, interface),
171 .interfaceSize = sizeof(PageViewInterface),
173 });
174 });
175
176 return clazz;
177}
178
179#undef _Class
static void setCurrentPage_enumerate(const Array *array, ident obj, ident data)
ArrayEnumerator for setCurrentPage.
Definition: PageView.c:101
static void initialize(Class *clazz)
Definition: PageView.c:146
PageViews manage their subviews as pages in a book.
Box * initWithFrame(Box *self, const SDL_Rect *frame)
Initializes this Box with the given frame.
Definition: Box.c:92
CollectionView * init(CollectionView *self, const SDL_Rect *frame)
Initializes this CollectionView with the specified frame and style.
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
Class * _PageView(void)
The PageView archetype.
Definition: PageView.c:161
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:116
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:133
MutableArray * subviews
The immediate subviews.
Definition: View.h:252
_Bool needsLayout
If true, this View will layout its subviews before it is drawn.
Definition: View.h:221
_Bool hidden
If true, this View is not drawn.
Definition: View.h:195
Class * _View(void)
The View archetype.
Definition: View.c:1769
Array * visibleSubviews(const View *self)
Definition: PageView.c:78
void addSubview(View *self, View *subview)
Adds a subview to this view, to be drawn above its siblings.
Definition: PageView.c:35
void addClassName(View *self, const char *className)
Adds the given class name to this View.
Definition: View.c:120
void removeSubview(View *self, View *subview)
Removes the given subview from this View.
Definition: PageView.c:58