ObjectivelyMVC 0.1.0
Object oriented MVC framework for OpenGL, SDL2 and GNU C
TabViewItem.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#include <string.h>
26
27#include "TabViewItem.h"
28
29#define _Class _TabViewItem
30
31#pragma mark - Object
32
36static void dealloc(Object *self) {
37
38 TabViewItem *this = (TabViewItem *) self;
39
40 free(this->identifier);
41
42 release(this->label);
43 release(this->view);
44
45 super(Object, self, dealloc);
46}
47
48#pragma mark - TabViewItem
49
54static TabViewItem *initWithIdentifier(TabViewItem *self, const char *identifier) {
55
56 self = (TabViewItem *) super(Object, self, init);
57 if (self) {
58 self->identifier = strdup(identifier);
59 assert(self->identifier);
60
61 self->label = $(alloc(Label), initWithText, self->identifier, NULL);
62 assert(self->label);
63
64 self->view = $(alloc(View), initWithFrame, NULL);
65 assert(self->view);
66
67 $(self, setState, TabStateDefault);
68 }
69
70 return self;
71}
72
78
79 self = $(self, initWithIdentifier, view->identifier ?: classnameof(self));
80 if (self) {
81 release(self->view);
82 self->view = retain(view);
83 }
84
85 return self;
86}
87
92static void setState(TabViewItem *self, int state) {
93
94 self->state = state;
95
96 if (self->state & TabStateSelected) {
97 $((View *) self->label, addClassName, "selected");
98 } else {
99 $((View *) self->label, removeClassName, "selected");
100 }
101}
102
103#pragma mark - Class lifecycle
104
108static void initialize(Class *clazz) {
109
110 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
111
112 ((TabViewItemInterface *) clazz->interface)->initWithIdentifier = initWithIdentifier;
113 ((TabViewItemInterface *) clazz->interface)->initWithView = initWithView;
114 ((TabViewItemInterface *) clazz->interface)->setState = setState;
115}
116
121Class *_TabViewItem(void) {
122 static Class *clazz;
123 static Once once;
124
125 do_once(&once, {
126 clazz = _initialize(&(const ClassDef) {
127 .name = "TabViewItem",
128 .superclass = _Object(),
129 .instanceSize = sizeof(TabViewItem),
130 .interfaceOffset = offsetof(TabViewItem, interface),
131 .interfaceSize = sizeof(TabViewItemInterface),
133 });
134 });
135
136 return clazz;
137}
138
139#undef _Class
static void dealloc(Object *self)
Definition: TabViewItem.c:36
static void initialize(Class *clazz)
Definition: TabViewItem.c:108
TabViewItems embed Views in a TabView.
@ TabStateSelected
Definition: TabViewItem.h:41
@ TabStateDefault
Definition: TabViewItem.h:40
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.
Labels provide a configurable container for Text.
Definition: Label.h:40
Label * initWithText(Label *self, const char *text, Font *font)
Initializes this Label with the given text and Font.
Definition: Label.c:96
TabViewItems embed Views in a TabView.
Definition: TabViewItem.h:51
setState(TabViewItem *self, int state)
Sets this TabViewItem's state, which may alter its appearance.
Definition: TabViewItem.c:92
int state
The bit mask of TabState.
Definition: TabViewItem.h:77
TabViewItem * initWithView(TabViewItem *self, View *view)
Initializes this TabViewItem with the specified View.
Definition: TabViewItem.c:77
View * view
The View this TabViewItem embeds.
Definition: TabViewItem.h:82
Class * _TabViewItem(void)
The TabViewItem archetype.
Definition: TabViewItem.c:121
Label * label
The Label used to select this tab.
Definition: TabViewItem.h:72
char * identifier
The identifier.
Definition: TabViewItem.h:67
TableColumn * initWithIdentifier(TableColumn *self, const char *identifier)
Initializes this TableColumn with the given identifier.
Definition: TableColumn.c:56
Views are the fundamental building blocks of ObjectivelyMVC user interfaces.
Definition: View.h:133
void addClassName(View *self, const char *className)
Adds the given class name to this View.
Definition: View.c:120
char * identifier
An optional identifier.
Definition: View.h:201
void removeClassName(View *self, const char *className)
Removes the given class name to this View.
Definition: View.c:1158