ObjectivelyMVC
Object oriented MVC framework for SDL3 and GNU C
Loading...
Searching...
No Matches
View.h
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#pragma once
25
26#include <Objectively/Data.h>
27#include <Objectively/Enum.h>
28#include <Objectively/Dictionary.h>
29#include <Objectively/Array.h>
30#include <Objectively/Set.h>
31#include <Objectively/Resource.h>
32
38
44#define ViewAlignmentMaskTop 0x1
45#define ViewAlignmentMaskMiddle 0x2
46#define ViewAlignmentMaskBottom 0x4
47#define ViewAlignmentMaskLeft 0x8
48#define ViewAlignmentMaskCenter 0x10
49#define ViewAlignmentMaskRight 0x20
50#define ViewAlignmentMaskInternal 0x100
51
52#define ViewAlignmentMaskVertical \
53 (ViewAlignmentMaskTop | ViewAlignmentMaskMiddle | ViewAlignmentMaskBottom)
54
55#define ViewAlignmentMaskHorizontal \
56 (ViewAlignmentMaskLeft | ViewAlignmentMaskCenter | ViewAlignmentMaskRight)
57
58#define ViewAlignmentMaskAny \
59 (ViewAlignmentMaskHorizontal | ViewAlignmentMaskVertical)
60
83
85
97
99
103#define ClampSize(size, min, max) \
104 clamp((size), (min), (max) ? (max) : INT32_MAX)
105
113
115
124
125
127
131typedef struct {
132 enum {
137
147
151 ViewConstraintUnspecified
152 } mode;
153
157 int value;
159
163#define MakeConstraint(mode, value) \
164 (ViewConstraint) { (mode), (value) }
165
169typedef struct {
170 int top, right, bottom, left;
172
176#define MakePadding(top, right, bottom, left) \
177 (ViewPadding) { (top), (right), (bottom), (left) }
178
182#define AddPadding(a, b) \
183 MakePadding(a.top + b.top, a.right + b.right, a.bottom + b.bottom, a.left + b.left)
184
191typedef struct {
192 int topLeft, topRight, bottomRight, bottomLeft;
194
198#define MakeCornerCut(topLeft, topRight, bottomRight, bottomLeft) \
199 (ViewCornerCut) { (topLeft), (topRight), (bottomRight), (bottomLeft) }
200
204#define CornerCutIsEmpty(cut) \
205 ((cut).topLeft == 0 && (cut).topRight == 0 && (cut).bottomRight == 0 && (cut).bottomLeft == 0)
206
214
215typedef struct ViewInterface ViewInterface;
216
225struct View {
226
230 Object object;
231
236 ViewInterface *interface[0];
237
243
249
255
262
270
275 SDL_Color borderColor;
276
282
288
294
300 struct {
301 SDL_Rect frame;
302 unsigned generation;
303 } clippingFrameCache;
304
310
315
323
327 SDL_Rect frame;
328
334
339 SDL_Size maxSize;
340
345 SDL_Size minSize;
346
354
362 bool needsApplyStyle;
363
370 bool needsApplyThemeSubviews;
371
378
385 bool needsLayoutSubviews;
386
393
399
409
415 struct {
416 SDL_Rect frame;
417 unsigned generation;
418 } renderFrameCache;
419
426
437
444
448 Array *subviews;
449
455
461
469
474 Array *warnings;
475
480 SDL_Window *window;
481};
482
486struct ViewInterface {
487
491 ObjectInterface parentInterface;
492
499 bool (*acceptsKeyResponder)(const View *self);
500
507 bool (*acceptsTouchResponder)(const View *self);
508
516 void (*addClassName)(View *self, const char *className);
517
526 void (*addSubview)(View *self, View *subview);
527
537 void (*addSubviewRelativeTo)(View *self, View *subview, View *other, ViewPosition position);
538
546 View *(*ancestorWithIdentifier)(const View *self, const char *identifier);
547
555 void (*applyStyle)(View *self, const Style *style);
556
564 void (*applyTheme)(View *self, const Theme *theme);
565
573 void (*applyThemeIfNeeded)(View *self, const Theme *theme);
574
582 void (*attachStylesheet)(View *self, SDL_Window *window);
583
592 void (*awakeWithCharacters)(View *self, const char *chars);
593
602 void (*awakeWithData)(View *self, const Data *data);
603
613 void (*awakeWithDictionary)(View *self, const Dictionary *dictionary);
614
622 void (*awakeWithResource)(View *self, const Resource *resource);
623
631 void (*awakeWithResourceName)(View *self, const char *name);
632
640 void (*becomeKeyResponder)(View *self);
641
649 void (*becomeTouchResponder)(View *self);
650
661 bool (*bind)(View *self, const Inlet *inlets, const Dictionary *dictionary);
662
669 SDL_Rect (*bounds)(const View *self);
670
678 void (*bringSubviewToFront)(View *self, View *subview);
679
687 void (*clearWarnings)(const View *self, WarningType level);
688
697 SDL_Rect (*clippingFrame)(const View *self);
698
706 bool (*containsPoint)(const View *self, const SDL_Point *point);
707
714 int (*depth)(const View *self);
715
723 View *(*descendantWithIdentifier)(const View *self, const char *identifier);
724
732 void (*detachStylesheet)(View *self, SDL_Window *window);
733
741 void (*didMoveToWindow)(View *self, SDL_Window *window);
742
750 bool (*didReceiveEvent)(const View *self, const SDL_Event *event);
751
762 void (*draw)(View *self, Renderer *renderer);
763
774 void (*emitViewEvent)(View *self, ViewEvent code, ident data);
775
784 void (*enumerate)(View *self, ViewEnumerator enumerator, ident data);
785
794 void (*enumerateAdjacent)(const View *self, ViewEnumerator enumerator, ident data);
795
804 void (*enumerateAncestors)(const View *self, ViewEnumerator enumerator, ident data);
805
814 void (*enumerateDescendants)(const View *self, ViewEnumerator enumerator, ident data);
815
825 void (*enumerateSelection)(View *self, const char *rule, ViewEnumerator enumerator, ident data);
826
835 void (*enumerateSiblings)(const View *self, ViewEnumerator enumerator, ident data);
836
845 void (*enumerateSubviews)(const View *self, ViewEnumerator enumerator, ident data);
846
855 void (*enumerateSuperview)(const View *self, ViewEnumerator enumerator, ident data);
856
866 void (*enumerateVisible)(View *self, ViewEnumerator enumerator, ident data);
867
875 bool (*hasClassName)(const View *self, const char *className);
876
883 bool (*hasOverflow)(const View *self);
884
895 View *(*hitTest)(const View *self, const SDL_Point *point);
896
907 View *(*init)(View *self);
908
918 View *(*initWithFrame)(View *self, const SDL_Rect *frame);
919
926 void (*invalidateStyle)(View *self);
927
934 bool (*isContainer)(const View *self);
935
943 bool (*isDescendantOfView)(const View *self, const View *view);
944
951 bool (*isKeyResponder)(const View *self);
952
959 bool (*isTouchResponder)(const View *self);
960
967 bool (*isVisible)(const View *self);
968
979 void (*layoutIfNeeded)(View *self);
980
991 void (*layoutSubviews)(View *self);
992
1004 void (*layoutWithConstraint)(View *self, ViewConstraint width, ViewConstraint height);
1005
1017 void (*layoutWithSize)(View *self, const SDL_Size *size);
1018
1026 bool (*matchesSelector)(const View *self, const SimpleSelector *simpleSelector);
1027
1035 void (*moveToWindow)(View *self, SDL_Window *window);
1036
1043 String *(*path)(const View *self);
1044
1051 void (*removeAllClassNames)(View *self);
1052
1059 void (*removeAllSubviews)(View *self);
1060
1068 void (*removeClassName)(View *self, const char *className);
1069
1076 void (*removeFromSuperview)(View *self);
1077
1085 void (*removeSubview)(View *self, View *subview);
1086
1096 void (*render)(View *self, Renderer *renderer);
1097
1105 void (*renderDeviceDidReset)(View *self);
1106
1114 void (*renderDeviceWillReset)(View *self);
1115
1122 SDL_Rect (*renderFrame)(const View *self);
1123
1132 void (*replaceSubview)(View *self, View *subview, View *replacement);
1133
1140 void (*resignKeyResponder)(View *self);
1141
1148 void (*resignTouchResponder)(View *self);
1149
1159 void (*resize)(View *self, const SDL_Size *size);
1160
1168 void (*resolve)(View *self, Outlet *outlets);
1169
1177 void (*respondToEvent)(View *self, const SDL_Event *event);
1178
1187 Set *(*select)(View *self, const char *rule);
1188
1197 View *(*selectFirst)(View *self, const char *rule);
1198
1209 void (*setVisibility)(View *self, ViewVisibility visibility);
1210
1220 void (*setNeedsApplyTheme)(View *self);
1221
1231 void (*setNeedsLayout)(View *self);
1232
1239 SDL_Size (*size)(const View *self);
1240
1248 SDL_Size (*sizeThatContains)(const View *self);
1249
1260 SDL_Size (*sizeThatFits)(const View *self);
1261
1268 SDL_Size (*sizeThatFills)(const View *self);
1269
1281 SDL_Size (*sizeThatSatisfies)(View *self, ViewConstraint width, ViewConstraint height);
1282
1290 void (*sizeToContain)(View *self);
1291
1299 void (*sizeToFill)(View *self);
1300
1308 void (*sizeToFit)(View *self);
1309
1319 void (*sizeToSatisfy)(View *self, ViewConstraint width, ViewConstraint height);
1320
1328 View *(*subviewWithIdentifier)(const View *self, const char *identifier);
1329
1346 void (*updateBindings)(View *self, ident data);
1347
1354 SDL_Rect (*viewport)(const View *self);
1355
1365 View *(*viewWithCharacters)(const char *chars, Outlet *outlets);
1366
1376 View *(*viewWithData)(const Data *data, Outlet *outlets);
1377
1388 View *(*viewWithDictionary)(const Dictionary *dictionary, Outlet *outlets);
1389
1400 View *(*viewWithResource)(const Resource *resource, Outlet *outlets);
1401
1412 View *(*viewWithResourceName)(const char *name, Outlet *outlets);
1413
1421 Array *(*visibleSubviews)(const View *self);
1422
1431 void (*warn)(View *self, WarningType level, const char *fmt, ...);
1432
1440 void (*willMoveToWindow)(View *self, SDL_Window *window);
1441};
1442
1449OBJECTIVELYMVC_EXPORT Class *_View(void);
1450
W3C Color constants.
static String * name(const Font *self)
Definition Font.c:303
Renderer extends Object with ObjectivelyMVC's UI rendering layer.
static Theme * theme(SDL_Window *window)
Definition Theme.c:295
The Theme type.
ViewEvent
View event relay codes.
Definition Types.h:61
void(* ViewEnumerator)(View *view, ident data)
A function type for View enumeration.
Definition Types.h:56
#define OBJECTIVELYMVC_EXPORT
Definition Types.h:40
JSON data binding for Views.
#define ViewAlignmentMaskInternal
Definition View.h:50
#define ViewAlignmentMaskCenter
Definition View.h:48
OBJECTIVELYMVC_EXPORT const EnumName ViewPointerEventsNames[]
Definition View.h:114
ViewPointerEvents
Whether a View is the result of a hit test.
Definition View.h:109
@ ViewPointerEventsNone
Definition View.h:111
@ ViewPointerEventsAuto
Definition View.h:110
OBJECTIVELYMVC_EXPORT Class * _View(void)
Definition View.c:2336
OBJECTIVELYMVC_EXPORT void MVC_InvalidateRenderFrames(void)
Invalidates every View's cached View::renderFrame and View::clippingFrame.
Definition View.c:46
#define ViewAlignmentMaskLeft
Definition View.h:47
#define ViewAlignmentMaskBottom
Definition View.h:46
#define ViewAlignmentMaskTop
Definition View.h:44
#define ViewAlignmentMaskRight
Definition View.h:49
#define ViewAlignmentMaskMiddle
Definition View.h:45
ViewVisibility
Visibility constants.
Definition View.h:119
@ ViewVisibilityUnspecified
Definition View.h:120
@ ViewVisibilityVisible
Definition View.h:121
@ ViewVisibilityHidden
Definition View.h:122
ViewPosition
Relative positioning of subviews within their superview.
Definition View.h:210
@ ViewPositionBefore
Definition View.h:211
@ ViewPositionAfter
Definition View.h:212
OBJECTIVELYMVC_EXPORT const EnumName ViewVisibilityNames[]
Definition View.h:126
OBJECTIVELYMVC_EXPORT const EnumName ViewAlignmentNames[]
Definition View.h:84
OBJECTIVELYMVC_EXPORT const EnumName ViewAutoresizingNames[]
Definition View.h:98
ViewAutoresizing
Auto-resizing constants, which are bitmasked.
Definition View.h:89
@ ViewAutoresizingNone
Definition View.h:90
@ ViewAutoresizingFill
Definition View.h:93
@ ViewAutoresizingContain
Definition View.h:95
@ ViewAutoresizingWidth
Definition View.h:91
@ ViewAutoresizingHeight
Definition View.h:92
@ ViewAutoresizingFit
Definition View.h:94
ViewAlignment
Alignment constants, used to align a View within its superview.
Definition View.h:64
@ ViewAlignmentBottomRight
Definition View.h:80
@ ViewAlignmentBottomLeft
Definition View.h:78
@ ViewAlignmentMiddleCenter
Definition View.h:76
@ ViewAlignmentInternal
Definition View.h:81
@ ViewAlignmentTopCenter
Definition View.h:73
@ ViewAlignmentTopRight
Definition View.h:74
@ ViewAlignmentMiddleRight
Definition View.h:77
@ ViewAlignmentTopLeft
Definition View.h:72
@ ViewAlignmentCenter
Definition View.h:70
@ ViewAlignmentLeft
Definition View.h:69
@ ViewAlignmentRight
Definition View.h:71
@ ViewAlignmentTop
Definition View.h:66
@ ViewAlignmentMiddleLeft
Definition View.h:75
@ ViewAlignmentBottom
Definition View.h:68
@ ViewAlignmentMiddle
Definition View.h:67
@ ViewAlignmentBottomCenter
Definition View.h:79
@ ViewAlignmentNone
Definition View.h:65
Warnings are accumulated on Views so that they may be reported by the DebugViewController.
WarningType
Warning types.
Definition Warning.h:33
Inlets enable inbound data binding of View attributes through JSON.
Definition View+JSON.h:149
Outlets enable outbound data binding of Views through JSON.
Definition View+JSON.h:194
Renderer extends Object with ObjectivelyMVC's UI rendering layer.
Definition Renderer.h:75
The SimpleSelector type.
The Style type.
Definition Style.h:43
Stylesheets are comprised of Selectors and Styles.
Definition Stylesheet.h:44
The Theme type.
Definition Theme.h:54
A constraint offered by an ancestor View to a descendant during View::sizeThatSatisfies.
Definition View.h:131
int value
The constraint value; a size in pixels.
Definition View.h:157
@ ViewConstraintMax
The offered value is an upper bound; the View may resolve to any size up to it.
Definition View.h:146
@ ViewConstraintEqual
The offered value is final; the View must resolve to exactly this size.
Definition View.h:136
A ViewController manages a View and its descendants.
The horizontal inset of each of a View's corners.
Definition View.h:191
int bottomLeft
Definition View.h:192
Views are the fundamental building blocks of ObjectivelyMVC user interfaces.
Definition View.h:225
Stylesheet * stylesheet
An optional Stylesheet.
Definition View.h:443
bool clipsSubviews
If true, subviews will be clipped to this View's frame.
Definition View.h:309
View * superview
The super View.
Definition View.h:454
Array * subviews
The immediate subviews.
Definition View.h:448
unsigned generation
Definition View.h:302
ViewController * viewController
The ViewController.
Definition View.h:460
Style * style
The element-level Style of this View.
Definition View.h:425
ViewAlignment alignment
The alignment.
Definition View.h:242
SDL_Window * window
The window.
Definition View.h:480
bool needsApplyTheme
If true, this View will apply the Theme before it is drawn.
Definition View.h:353
ViewCornerCut cornerCut
The horizontal inset of each corner of the background and border.
Definition View.h:322
SDL_Color borderColor
The border color.
Definition View.h:275
int autoresizingMask
The ViewAutoresizing bitmask.
Definition View.h:248
View * nextResponder
The next responder, or event handler, in the chain.
Definition View.h:392
ViewPointerEvents pointerEvents
Whether this View is the result of a hit test; its subviews always are.
Definition View.h:408
bool needsLayout
If true, this View will layout its subviews before it is drawn.
Definition View.h:377
char * identifier
An optional identifier.
Definition View.h:333
Array * warnings
The Warnings this View generated.
Definition View.h:474
ViewPadding padding
The padding.
Definition View.h:398
int borderWidth
The border width.
Definition View.h:287
SDL_Size minSize
The minimum size this View may be resized to during layout.
Definition View.h:345
int backgroundGradientAngle
The angle of the background gradient, in degrees, clockwise from south: 0 (the default) fills downwar...
Definition View.h:261
Object object
The superclass.
Definition View.h:230
Style * computedStyle
The computed Style of this View.
Definition View.h:314
Set * classNames
The class names.
Definition View.h:293
ViewVisibility visibility
Whether this View is drawn, and contributes to its superview's size.
Definition View.h:468
int borderRadius
The corner radius applied to the background, border and bevel.
Definition View.h:281
SDL_Color backgroundGradientColor
The far color of the background gradient, which runs from View::backgroundColor.
Definition View.h:269
SDL_Size maxSize
The maximum size this View may be resized to during layout.
Definition View.h:339
SDL_Color backgroundColor
The background color.
Definition View.h:254
bool stretch
Whether this View takes a share of the space its StackView leaves along its axis.
Definition View.h:436
SDL_Rect frame
The frame, relative to the superview.
Definition View.h:301
ViewInterface * interface[0]
The interface type.
Definition View.h:236
Spacing applied to the inside of a View's frame.
Definition View.h:169
int bottom
Definition View.h:170