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

Go to the source code of this file.

Macros

#define _Class   _ScrollView
 

Functions

Class * _ScrollView (void)
 
static void applyStyle (View *self, const Style *style)
 
static void awakeWithDictionary (View *self, const Dictionary *dictionary)
 
static bool captureEvent (Control *self, const SDL_Event *event)
 
static void dealloc (Object *self)
 
static View * init (View *self)
 
static void initialize (Class *clazz)
 
static ScrollView * initWithFrame (ScrollView *self, const SDL_Rect *frame)
 
static void layoutSubviews (View *self)
 
static void setContentView (ScrollView *self, View *contentView)
 
static void setScrollBarVisibility (ScrollView *self, ScrollBarVisibility visibility)
 

Macro Definition Documentation

◆ _Class

#define _Class   _ScrollView

Definition at line 28 of file ScrollView.c.

Function Documentation

◆ _ScrollView()

Class * _ScrollView ( void  )

Definition at line 292 of file ScrollView.c.

292 {
293 static Class *clazz;
294 static Once once;
295
296 do_once(&once, {
297 clazz = _initialize(&(const ClassDef) {
298 .name = "ScrollView",
299 .superclass = _Control(),
300 .instanceSize = sizeof(ScrollView),
301 .interfaceSize = sizeof(ScrollViewInterface),
302 .initialize = initialize,
303 });
304 });
305
306 return clazz;
307}
Class * _Control(void)
Definition Control.c:414
static void initialize(Class *clazz)
Definition ScrollView.c:271
ScrollViews allow users to pan their internal contents.
Definition ScrollView.h:61

◆ applyStyle()

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

Definition at line 75 of file ScrollView.c.

75 {
76
77 super(View, self, applyStyle, style);
78
79 ScrollView *this = (ScrollView *) self;
80
81 const Inlet inlets[] = MakeInlets(
82 MakeInlet("scrollbar", InletTypeEnum, &this->scrollBarVisibility, (ident) ScrollBarVisibilityNames)
83 );
84
85 $(self, bind, inlets, (Dictionary *) style->attributes);
86
87 $(self, setNeedsLayout);
88}
const EnumName ScrollBarVisibilityNames[]
Definition ScrollBar.c:31
static void applyStyle(View *self, const Style *style)
Definition ScrollView.c:75
@ InletTypeEnum
Definition View+JSON.h:75
#define MakeInlets(...)
Creates a null-termianted array of Inlets.
Definition View+JSON.h:215
#define MakeInlet(name, type, dest, data)
Creates an Inlet with the specified parameters.
Definition View+JSON.h:210
static void setNeedsLayout(View *self)
Definition View.c:1865
Inlets enable inbound data binding of View attributes through JSON.
Definition View+JSON.h:149
Dictionary * attributes
Definition Style.h:59
Views are the fundamental building blocks of ObjectivelyMVC user interfaces.
Definition View.h:225

◆ awakeWithDictionary()

static void awakeWithDictionary ( View *  self,
const Dictionary *  dictionary 
)
static
See also
View::awakeWithDictionary(View *, const Dictionary *)

Definition at line 50 of file ScrollView.c.

50 {
51
52 super(View, self, awakeWithDictionary, dictionary);
53
54 ScrollView *this = (ScrollView *) self;
55
56 View *contentView = NULL;
57
58 const Inlet inlets[] = MakeInlets(
59 MakeInlet("contentView", InletTypeView, &contentView, NULL),
60 MakeInlet("scrollbarVisibility", InletTypeEnum, &this->scrollBarVisibility, (ident) ScrollBarVisibilityNames),
61 MakeInlet("step", InletTypeFloat, &this->step, NULL)
62 );
63
64 $(self, bind, inlets, dictionary);
65
66 if (contentView) {
67 $(this, setContentView, contentView);
68 release(contentView);
69 }
70}
static void awakeWithDictionary(View *self, const Dictionary *dictionary)
Definition ScrollView.c:50
static void setContentView(ScrollView *self, View *contentView)
Definition ScrollView.c:231
@ InletTypeView
Definition View+JSON.h:133
@ InletTypeFloat
Definition View+JSON.h:80

◆ captureEvent()

static bool captureEvent ( Control *  self,
const SDL_Event *  event 
)
static
See also
Control::captureEvent(Control *, const SDL_Event *)

Definition at line 140 of file ScrollView.c.

140 {
141
142 ScrollView *this = (ScrollView *) self;
143
144 if (event->type == SDL_EVENT_MOUSE_MOTION && (event->motion.state & SDL_BUTTON_LMASK)) {
146
147 SDL_Point offset = this->contentOffset;
148
149 offset.x += event->motion.xrel;
150 offset.y += event->motion.yrel;
151
152 $(this, scrollToOffset, &offset);
153 return true;
154 } else if (event->type == SDL_EVENT_MOUSE_WHEEL) {
155 SDL_Point offset = this->contentOffset;
156
157 offset.x -= event->wheel.x * this->step;
158 offset.y += event->wheel.y * this->step;
159
160 $(this, scrollToOffset, &offset);
161 return true;
162 } else if (event->type == SDL_EVENT_MOUSE_BUTTON_UP && (event->button.button & SDL_BUTTON_LMASK)) {
163
164 if ($(self, isHighlighted)) {
165 self->state &= ~ControlStateHighlighted;
166 return true;
167 }
168 }
169
170 return super(Control, self, captureEvent, event);
171}
static bool isHighlighted(const Control *self)
Definition Control.c:350
@ ControlStateHighlighted
Definition Control.h:67
static bool captureEvent(Control *self, const SDL_Event *event)
Definition ScrollView.c:140
Controls are Views which capture and respond to events.
Definition Control.h:83
unsigned int state
The bit mask of ControlState.
Definition Control.h:117

◆ dealloc()

static void dealloc ( Object *  self)
static
See also
Object::dealloc(Object *)

Definition at line 35 of file ScrollView.c.

35 {
36
37 ScrollView *this = (ScrollView *) self;
38
39 release(this->contentView);
40 release(this->scrollBar);
41
42 super(Object, self, dealloc);
43}
static void dealloc(Object *self)
Definition ScrollView.c:35

◆ init()

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

Definition at line 93 of file ScrollView.c.

93 {
94 return (View *) $((ScrollView *) self, initWithFrame, NULL);
95}
static ScrollView * initWithFrame(ScrollView *self, const SDL_Rect *frame)
Definition ScrollView.c:179

◆ initialize()

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

Definition at line 271 of file ScrollView.c.

271 {
272
273 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
274
275 ((ViewInterface *) clazz->interface)->applyStyle = applyStyle;
276 ((ViewInterface *) clazz->interface)->awakeWithDictionary = awakeWithDictionary;
277 ((ViewInterface *) clazz->interface)->init = init;
278 ((ViewInterface *) clazz->interface)->layoutSubviews = layoutSubviews;
279
280 ((ControlInterface *) clazz->interface)->captureEvent = captureEvent;
281
282 ((ScrollViewInterface *) clazz->interface)->initWithFrame = initWithFrame;
283 ((ScrollViewInterface *) clazz->interface)->scrollToOffset = scrollToOffset;
284 ((ScrollViewInterface *) clazz->interface)->setContentView = setContentView;
285 ((ScrollViewInterface *) clazz->interface)->setScrollBarVisibility = setScrollBarVisibility;
286}
static void setScrollBarVisibility(ScrollView *self, ScrollBarVisibility visibility)
Definition ScrollView.c:257
static View * init(View *self)
Definition ScrollView.c:93
static void layoutSubviews(View *self)
Definition ScrollView.c:100
static void scrollToOffset(ScrollView *self, const SDL_Point *offset)
Scrolls the content View to the specified offset.
Definition ScrollView.c:201
layoutSubviews(View *self)
Arranges this View's immediate subviews within its (already final) frame.
Definition Box.c:74

◆ initWithFrame()

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

Definition at line 179 of file ScrollView.c.

179 {
180
181 self = (ScrollView *) super(Control, self, initWithFrame, frame);
182 if (self) {
183 self->step = 12.f;
184
185 self->scrollBar = $(alloc(ScrollBar), initWithScrollView, self);
186 assert(self->scrollBar);
187
188 View *scrollBar = (View *) self->scrollBar;
189 $(scrollBar, setVisibility, ViewVisibilityHidden);
190
191 $((View *) self, addSubview, scrollBar);
192 }
193
194 return self;
195}
static void addSubview(View *self, View *subview)
Definition PageView.c:49
static ScrollBar * initWithScrollView(ScrollBar *self, ScrollView *scrollView)
Definition ScrollBar.c:173
static void setVisibility(View *self, ViewVisibility visibility)
Definition View.c:1834
@ ViewVisibilityHidden
Definition View.h:122
A draggable scrollbar, owned by a ScrollView.
Definition ScrollBar.h:85
float step
The scroll step, in pixels.
Definition ScrollView.h:89
ScrollBar * scrollBar
The scrollbar. Always present; shown per scrollBarVisibility (see the scrollbar style attribute / set...
Definition ScrollView.h:95

◆ layoutSubviews()

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

Definition at line 100 of file ScrollView.c.

100 {
101
102 ScrollView *this = (ScrollView *) self;
103
104 bool scrollBarHidden;
105 switch (this->scrollBarVisibility) {
106 case ScrollBarShow:
107 scrollBarHidden = false;
108 break;
109 case ScrollBarHide:
110 scrollBarHidden = true;
111 break;
112 default: {
113 if (this->contentView == NULL) {
114 scrollBarHidden = true;
115 } else {
116 const SDL_Size contentSize = $(this->contentView, size);
117 const SDL_Rect bounds = $(self, bounds);
118 scrollBarHidden = contentSize.h <= bounds.h;
119 }
120 break;
121 }
122 }
123
124 $((View *) this->scrollBar, setVisibility,
125 scrollBarHidden ? ViewVisibilityHidden : ViewVisibilityVisible);
126
127 super(View, self, layoutSubviews);
128
129 if (this->contentView) {
130 this->contentView->frame.x = this->contentOffset.x;
131 this->contentView->frame.y = this->contentOffset.y;
132 }
133}
static SDL_Size size(const Image *self)
Definition Image.c:416
static SDL_Size contentSize(const Panel *self)
Definition Panel.c:201
@ ScrollBarHide
Definition ScrollBar.h:68
@ ScrollBarShow
Definition ScrollBar.h:69
static SDL_Rect bounds(const View *self)
Definition View.c:552
@ ViewVisibilityVisible
Definition View.h:121

◆ setContentView()

static void setContentView ( ScrollView *  self,
View *  contentView 
)
static

Definition at line 231 of file ScrollView.c.

231 {
232
233 if (contentView != self->contentView) {
234
235 if (self->contentView) {
236 $(self->contentView, removeClassName, "contentView");
237 $((View *) self, removeSubview, self->contentView);
238 self->contentView = release(self->contentView);
239 }
240
241 if (contentView) {
242 $(contentView, addClassName, "contentView");
243 $((View *) self, addSubview, contentView);
244 self->contentView = retain(contentView);
245 }
246
247 $((View *) self, addSubview, (View *) self->scrollBar);
248
249 $(self, scrollToOffset, &MakePoint(0, 0));
250 }
251}
static void removeSubview(View *self, View *subview)
Definition PageView.c:72
static void removeClassName(View *self, const char *className)
Definition View.c:1445
static void addClassName(View *self, const char *className)
Definition View.c:183
View * contentView
The content View.
Definition ScrollView.h:84

◆ setScrollBarVisibility()

static void setScrollBarVisibility ( ScrollView *  self,
ScrollBarVisibility  visibility 
)
static

Definition at line 257 of file ScrollView.c.

257 {
258
259 View *this = (View *) self;
260
261 $(this->style, addEnumAttribute, "scrollbar", ScrollBarVisibilityNames, visibility);
262
263 $(this, invalidateStyle);
264}
static void addEnumAttribute(Style *self, const char *attr, const EnumName *names, int value)
Definition Style.c:288
static void invalidateStyle(View *self)
Definition View.c:1092