ObjectivelyMVC
Object oriented MVC framework for SDL3 and GNU C
Loading...
Searching...
No Matches
StackView.c File Reference
#include <assert.h>
#include "StackView.h"

Go to the source code of this file.

Macros

#define _Class   _StackView
 

Functions

Class * _StackView (void)
 
static void applyStyle (View *self, const Style *style)
 
static Viewinit (View *self)
 
static void initialize (Class *clazz)
 
static StackViewinitWithFrame (StackView *self, const SDL_Rect *frame)
 
static void layoutSubviews (View *self)
 
static SDL_Size sizeThatFits (const View *self)
 

Variables

const EnumName StackViewAxisNames []
 
const EnumName StackViewDistributionNames []
 

Macro Definition Documentation

◆ _Class

#define _Class   _StackView

Definition at line 39 of file StackView.c.

Function Documentation

◆ _StackView()

Class * _StackView ( void  )

Definition at line 287 of file StackView.c.

287 {
288 static Class *clazz;
289 static Once once;
290
291 do_once(&once, {
292 clazz = _initialize(&(const ClassDef) {
293 .name = "StackView",
294 .superclass = _View(),
295 .instanceSize = sizeof(StackView),
296 .interfaceOffset = offsetof(StackView, interface),
297 .interfaceSize = sizeof(StackViewInterface),
299 });
300 });
301
302 return clazz;
303}
static void initialize(Class *clazz)
Definition StackView.c:272
Class * _View(void)
Definition View.c:2067
StackViews are containers that manage the arrangement of their subviews.
Definition StackView.h:68

◆ applyStyle()

static void applyStyle ( View self,
const Style style 
)
static
See also
View::applyStyle(View *, const Style *)

Definition at line 46 of file StackView.c.

46 {
47
48 super(View, self, applyStyle, style);
49
50 StackView *this = (StackView *) self;
51
52 const Inlet inlets[] = MakeInlets(
53 MakeInlet("axis", InletTypeEnum, &this->axis, (ident) StackViewAxisNames),
54 MakeInlet("distribution", InletTypeEnum, &this->distribution, (ident) StackViewDistributionNames),
55 MakeInlet("spacing", InletTypeInteger, &this->spacing, NULL)
56 );
57
58 $(self, bind, inlets, (Dictionary *) style->attributes);
59}
const EnumName StackViewAxisNames[]
Definition StackView.c:28
static void applyStyle(View *self, const Style *style)
Definition StackView.c:46
const EnumName StackViewDistributionNames[]
Definition StackView.c:33
@ InletTypeEnum
Definition View+JSON.h:75
@ InletTypeInteger
Definition View+JSON.h:95
#define MakeInlets(...)
Creates a null-termianted array of Inlets.
Definition View+JSON.h:221
#define MakeInlet(name, type, dest, data)
Creates an Inlet with the specified parameters.
Definition View+JSON.h:216
Inlets enable inbound data binding of View attributes through JSON.
Definition View+JSON.h:155
Dictionary * attributes
Definition Style.h:59
Views are the fundamental building blocks of ObjectivelyMVC user interfaces.
Definition View.h:134

◆ init()

static View * init ( View self)
static
See also
View::init(View *)

Definition at line 64 of file StackView.c.

64 {
65 return (View *) $((StackView *) self, initWithFrame, NULL);
66}
static StackView * initWithFrame(StackView *self, const SDL_Rect *frame)
Definition StackView.c:257

◆ initialize()

static void initialize ( Class *  clazz)
static
See also
Class::initialize(Class *)

Definition at line 272 of file StackView.c.

272 {
273
274 ((ViewInterface *) clazz->interface)->applyStyle = applyStyle;
275 ((ViewInterface *) clazz->interface)->init = init;
276
277 ((ViewInterface *) clazz->interface)->layoutSubviews = layoutSubviews;
278 ((ViewInterface *) clazz->interface)->sizeThatFits = sizeThatFits;
279
280 ((StackViewInterface *) clazz->interface)->initWithFrame = initWithFrame;
281}
static View * init(View *self)
Definition StackView.c:64
static SDL_Size sizeThatFits(const View *self)
Definition StackView.c:191
static void layoutSubviews(View *self)
Definition StackView.c:71
layoutSubviews(View *self)
Performs layout for this View's immediate subviews.
Definition Box.c:74

◆ initWithFrame()

static StackView * initWithFrame ( StackView self,
const SDL_Rect *  frame 
)
static

Definition at line 257 of file StackView.c.

257 {
258
259 self = (StackView *) super(View, self, initWithFrame, frame);
260 if (self) {
262 }
263
264 return self;
265}
@ ViewAutoresizingContain
Definition View.h:92
View view
The superclass.
Definition StackView.h:73
int autoresizingMask
The ViewAutoresizing bitmask.
Definition View.h:155

◆ layoutSubviews()

static void layoutSubviews ( View self)
static
See also
View::layoutSubviews(View *)

Definition at line 71 of file StackView.c.

71 {
72
73 super(View, self, layoutSubviews);
74
75 Array *subviews = $(self, visibleSubviews);
76 if (subviews->count) {
77
78 const SDL_Rect bounds = $(self, bounds);
79
80 StackView *this = (StackView *) self;
81
82 int availableSize, requestedSize = 0;
83 switch (this->axis) {
85 availableSize = bounds.h;
86 break;
88 availableSize = bounds.w;
89 break;
90 }
91
92 availableSize -= this->spacing * (subviews->count - 1);
93
94 for (size_t i = 0; i < subviews->count; i++) {
95
96 View *subview = $(subviews, objectAtIndex, i);
97
98 const SDL_Size subviewSize = $(subview, size);
99
100 switch (this->axis) {
102 requestedSize += subviewSize.h;
103 break;
105 requestedSize += subviewSize.w;
106 break;
107 }
108 }
109
110 int pos = 0;
111
112 const float scale = requestedSize ? availableSize / (float) requestedSize : 1.f;
113
114 for (size_t i = 0; i < subviews->count; i++) {
115
116 View *subview = $(subviews, objectAtIndex, i);
117
118 switch (this->axis) {
120 subview->frame.y = pos;
121 break;
123 subview->frame.x = pos;
124 break;
125 }
126
127 SDL_Size subviewSize = $(subview, size);
128
129 switch (this->axis) {
132 subviewSize.w = bounds.w;
133 }
134 break;
137 subviewSize.h = bounds.h;
138 }
139 break;
140 }
141
142 switch (this->distribution) {
144 break;
145
147 switch (this->axis) {
149 subviewSize.h *= scale;
150 break;
152 subviewSize.w *= scale;
153 break;
154 }
155 break;
156
158 switch (this->axis) {
160 subviewSize.h = availableSize / (float) subviews->count;
161 break;
163 subviewSize.w = availableSize / (float) subviews->count;
164 break;
165 }
166 break;
167 }
168
169 $(subview, resize, &subviewSize);
170 $(subview, layoutIfNeeded);
171
172 switch (this->axis) {
174 pos += subviewSize.h;
175 break;
177 pos += subviewSize.w;
178 break;
179 }
180
181 pos += this->spacing;
182 }
183 }
184
185 release(subviews);
186}
static SDL_Size size(const Image *self)
Definition Image.c:181
static Array * visibleSubviews(const View *self)
Definition PageView.c:92
@ StackViewDistributionDefault
Definition StackView.h:53
@ StackViewDistributionFillEqually
Definition StackView.h:55
@ StackViewDistributionFill
Definition StackView.h:54
@ StackViewAxisHorizontal
Definition StackView.h:44
@ StackViewAxisVertical
Definition StackView.h:43
static void resize(View *self, const SDL_Size *size)
Definition View.c:1527
static void layoutIfNeeded(View *self)
Definition View.c:1115
static SDL_Rect bounds(const View *self)
Definition View.c:492
@ ViewAutoresizingWidth
Definition View.h:88
@ ViewAutoresizingHeight
Definition View.h:89
SDL_Rect frame
The frame, relative to the superview.
Definition View.h:191

◆ sizeThatFits()

static SDL_Size sizeThatFits ( const View self)
static
See also
View::sizeThatFits(const View *)

Definition at line 191 of file StackView.c.

191 {
192
193 const StackView *this = (StackView *) self;
194
195 SDL_Size size = MakeSize(0, 0);
196
197 switch (this->axis) {
199 size.h = self->padding.top + self->padding.bottom;
200 break;
202 size.w = self->padding.left + self->padding.right;
203 break;
204 }
205
206 Array *subviews = $(self, visibleSubviews);
207 for (size_t i = 0; i < subviews->count; i++) {
208
209 const View *subview = $(subviews, objectAtIndex, i);
210
211 SDL_Size subviewSize;
213 subviewSize = $(subview, sizeThatContains);
214 } else if (subview->autoresizingMask & ViewAutoresizingFit) {
215 subviewSize = $(subview, sizeThatFits);
216 } else {
217 subviewSize = $(subview, size);
218 }
219
220 switch (this->axis) {
222 size.w = max(size.w, subviewSize.w);
223 size.h += subviewSize.h;
224 break;
226 size.w += subviewSize.w;
227 size.h = max(size.h, subviewSize.h);
228 break;
229 }
230 }
231
232 if (subviews->count) {
233 switch (this->axis) {
235 size.h += this->spacing * (subviews->count - 1);
236 break;
238 size.w += this->spacing * (subviews->count - 1);
239 break;
240 }
241 }
242
243 release(subviews);
244
245 size.w = clamp(size.w, self->minSize.w, self->maxSize.w);
246 size.h = clamp(size.h, self->minSize.h, self->maxSize.h);
247
248 return size;
249}
static SDL_Size sizeThatContains(const View *self)
Definition View.c:1659
@ ViewAutoresizingFit
Definition View.h:91
ViewPadding padding
The padding.
Definition View.h:234
SDL_Size minSize
The minimum size this View may be resized to during layout.
Definition View.h:212
SDL_Size maxSize
The maximum size this View may be resized to during layout.
Definition View.h:207
int top
Definition View.h:101
int bottom
Definition View.h:101
int right
Definition View.h:101
int left
Definition View.h:101

Variable Documentation

◆ StackViewAxisNames

const EnumName StackViewAxisNames[]
Initial value:
= MakeEnumNames(
MakeEnumAlias(StackViewAxisVertical, vertical),
MakeEnumAlias(StackViewAxisHorizontal, horizontal)
)

Definition at line 28 of file StackView.c.

◆ StackViewDistributionNames

const EnumName StackViewDistributionNames[]
Initial value:
= MakeEnumNames(
MakeEnumAlias(StackViewDistributionDefault, default),
MakeEnumAlias(StackViewDistributionFill, fill),
MakeEnumAlias(StackViewDistributionFillEqually, fill-equally)
)

Definition at line 33 of file StackView.c.