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 void initialize (Class *clazz)
 
static ScrollViewinitWithFrame (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 279 of file ScrollView.c.

279 {
280 static Class *clazz;
281 static Once once;
282
283 do_once(&once, {
284 clazz = _initialize(&(const ClassDef) {
285 .name = "ScrollView",
286 .superclass = _Control(),
287 .instanceSize = sizeof(ScrollView),
288 .interfaceOffset = offsetof(ScrollView, interface),
289 .interfaceSize = sizeof(ScrollViewInterface),
291 });
292 });
293
294 return clazz;
295}
Class * _Control(void)
Definition Control.c:391
static void initialize(Class *clazz)
Definition ScrollView.c:259
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 67 of file ScrollView.c.

67 {
68
69 super(View, self, applyStyle, style);
70
71 ScrollView *this = (ScrollView *) self;
72
73 const Inlet inlets[] = MakeInlets(
74 MakeInlet("scrollbar", InletTypeEnum, &this->scrollBarVisibility, (ident) ScrollBarVisibilityNames)
75 );
76
77 $(self, bind, inlets, (Dictionary *) style->attributes);
78
79 self->needsLayout = true;
80}
const EnumName ScrollBarVisibilityNames[]
Definition ScrollBar.c:31
static void applyStyle(View *self, const Style *style)
Definition ScrollView.c:67
@ InletTypeEnum
Definition View+JSON.h:75
#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
bool needsLayout
If true, this View will layout its subviews before it is drawn.
Definition View.h:222

◆ 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 const Inlet inlets[] = MakeInlets(
57 MakeInlet("scrollbarVisibility", InletTypeEnum, &this->scrollBarVisibility, (ident) ScrollBarVisibilityNames),
58 MakeInlet("step", InletTypeFloat, &this->step, NULL)
59 );
60
61 $(self, bind, inlets, dictionary);
62}
static void awakeWithDictionary(View *self, const Dictionary *dictionary)
Definition ScrollView.c:50
@ 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 128 of file ScrollView.c.

128 {
129
130 ScrollView *this = (ScrollView *) self;
131
132 if (event->type == SDL_EVENT_MOUSE_MOTION && (event->motion.state & SDL_BUTTON_LMASK)) {
134
135 SDL_Point offset = this->contentOffset;
136
137 offset.x += event->motion.xrel;
138 offset.y += event->motion.yrel;
139
140 $(this, scrollToOffset, &offset);
141 return true;
142 } else if (event->type == SDL_EVENT_MOUSE_WHEEL) {
143 SDL_Point offset = this->contentOffset;
144
145 offset.x -= event->wheel.x * this->step;
146 offset.y += event->wheel.y * this->step;
147
148 $(this, scrollToOffset, &offset);
149 return true;
150 } else if (event->type == SDL_EVENT_MOUSE_BUTTON_UP && (event->button.button & SDL_BUTTON_LMASK)) {
151
152 if ($(self, isHighlighted)) {
153 self->state &= ~ControlStateHighlighted;
154 return true;
155 }
156 }
157
158 return super(Control, self, captureEvent, event);
159}
static bool isHighlighted(const Control *self)
Definition Control.c:327
@ ControlStateHighlighted
Definition Control.h:67
static bool captureEvent(Control *self, const SDL_Event *event)
Definition ScrollView.c:128
Controls are Views which capture and respond to events.
Definition Control.h:83
unsigned int state
The bit mask of ControlState.
Definition Control.h:104

◆ 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

◆ initialize()

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

Definition at line 259 of file ScrollView.c.

259 {
260
261 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
262
263 ((ViewInterface *) clazz->interface)->applyStyle = applyStyle;
264 ((ViewInterface *) clazz->interface)->awakeWithDictionary = awakeWithDictionary;
265 ((ViewInterface *) clazz->interface)->layoutSubviews = layoutSubviews;
266
267 ((ControlInterface *) clazz->interface)->captureEvent = captureEvent;
268
269 ((ScrollViewInterface *) clazz->interface)->initWithFrame = initWithFrame;
270 ((ScrollViewInterface *) clazz->interface)->scrollToOffset = scrollToOffset;
271 ((ScrollViewInterface *) clazz->interface)->setContentView = setContentView;
272 ((ScrollViewInterface *) clazz->interface)->setScrollBarVisibility = setScrollBarVisibility;
273}
static void setScrollBarVisibility(ScrollView *self, ScrollBarVisibility visibility)
Definition ScrollView.c:248
static ScrollView * initWithFrame(ScrollView *self, const SDL_Rect *frame)
Definition ScrollView.c:167
static void setContentView(ScrollView *self, View *contentView)
Definition ScrollView.c:222
static void layoutSubviews(View *self)
Definition ScrollView.c:85
static void scrollToOffset(ScrollView *self, const SDL_Point *offset)
Scrolls the content View to the specified offset.
Definition ScrollView.c:192

◆ initWithFrame()

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

Definition at line 167 of file ScrollView.c.

167 {
168
169 self = (ScrollView *) super(Control, self, initWithFrame, frame);
170 if (self) {
171 self->step = 12.f;
173
174 self->scrollBar = $(alloc(ScrollBar), initWithScrollView, self);
175 assert(self->scrollBar);
176
177 View *scrollBar = (View *) self->scrollBar;
178 scrollBar->alignment = ViewAlignmentRight;
179 scrollBar->autoresizingMask = ViewAutoresizingHeight;
180 scrollBar->hidden = true;
181
182 $((View *) self, addSubview, scrollBar);
183 }
184
185 return self;
186}
static void addSubview(View *self, View *subview)
Definition PageView.c:49
static ScrollBar * initWithScrollView(ScrollBar *self, ScrollView *scrollView)
Definition ScrollBar.c:173
@ ScrollBarAuto
Definition ScrollBar.h:70
@ ViewAutoresizingHeight
Definition View.h:89
@ ViewAlignmentRight
Definition View.h:68
A draggable scrollbar, owned by a ScrollView.
Definition ScrollBar.h:85
float step
The scroll step, in pixels.
Definition ScrollView.h:87
ScrollBarVisibility scrollBarVisibility
Governs whether the scrollbar is shown. Attribute scrollbar.
Definition ScrollView.h:100
ScrollBar * scrollBar
The scrollbar. Always present; shown per scrollBarVisibility (see the scrollbar style attribute / set...
Definition ScrollView.h:93

◆ layoutSubviews()

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

Definition at line 85 of file ScrollView.c.

85 {
86
87 ScrollView *this = (ScrollView *) self;
88
89 bool showScrollBar;
90 switch (this->scrollBarVisibility) {
91 case ScrollBarShow:
92 showScrollBar = true;
93 break;
94 case ScrollBarHide:
95 showScrollBar = false;
96 break;
97 default: {
98 if (this->contentView == NULL) {
99 showScrollBar = false;
100 } else {
101 const SDL_Size contentSize = $(this->contentView, size);
102 const SDL_Rect bounds = $(self, bounds);
103 showScrollBar = contentSize.h > bounds.h;
104 }
105 break;
106 }
107 }
108
109 ((View *) this->scrollBar)->hidden = !showScrollBar;
110
111 super(View, self, layoutSubviews);
112
113 if (this->contentView) {
114 this->contentView->frame.x = this->contentOffset.x;
115 this->contentView->frame.y = this->contentOffset.y;
116 }
117
118 if (showScrollBar) {
119 ((View *) this->scrollBar)->needsLayout = true;
120 }
121}
static SDL_Size size(const Image *self)
Definition Image.c:181
static SDL_Size contentSize(const Panel *self)
Definition Panel.c:195
@ ScrollBarHide
Definition ScrollBar.h:68
@ ScrollBarShow
Definition ScrollBar.h:69
static SDL_Rect bounds(const View *self)
Definition View.c:492

◆ setContentView()

static void setContentView ( ScrollView self,
View contentView 
)
static

Definition at line 222 of file ScrollView.c.

222 {
223
224 if (contentView != self->contentView) {
225
226 if (self->contentView) {
227 $(self->contentView, removeClassName, "contentView");
228 $((View *) self, removeSubview, self->contentView);
229 self->contentView = release(self->contentView);
230 }
231
232 if (contentView) {
233 $(contentView, addClassName, "contentView");
234 $((View *) self, addSubview, contentView);
235 self->contentView = retain(contentView);
236 }
237
238 $((View *) self, addSubview, (View *) self->scrollBar);
239
240 $(self, scrollToOffset, &MakePoint(0, 0));
241 }
242}
static void removeSubview(View *self, View *subview)
Definition PageView.c:72
static void removeClassName(View *self, const char *className)
Definition View.c:1333
static void addClassName(View *self, const char *className)
Definition View.c:159
View * contentView
The content View.
Definition ScrollView.h:82

◆ setScrollBarVisibility()

static void setScrollBarVisibility ( ScrollView self,
ScrollBarVisibility  visibility 
)
static

Definition at line 248 of file ScrollView.c.

248 {
249
250 self->scrollBarVisibility = visibility;
251 self->control.view.needsLayout = true;
252}
View view
The superclass.
Definition Control.h:88
Control control
The superclass.
Definition ScrollView.h:66