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