ObjectivelyMVC
Object oriented MVC framework for SDL3 and GNU C
Loading...
Searching...
No Matches
ScrollView.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
26#include "ScrollView.h"
27
28#define _Class _ScrollView
29
30#pragma mark - Object
31
35static void dealloc(Object *self) {
36
37 ScrollView *this = (ScrollView *) self;
38
39 release(this->contentView);
40 release(this->scrollBar);
41
42 super(Object, self, dealloc);
43}
44
45#pragma mark - View
46
50static void awakeWithDictionary(View *self, const Dictionary *dictionary) {
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}
71
75static void applyStyle(View *self, const Style *style) {
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}
89
93static View *init(View *self) {
94 return (View *) $((ScrollView *) self, initWithFrame, NULL);
95}
96
100static void layoutSubviews(View *self) {
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}
134
135#pragma mark - Control
136
140static bool captureEvent(Control *self, const SDL_Event *event) {
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}
172
173#pragma mark - ScrollView
174
179static ScrollView *initWithFrame(ScrollView *self, const SDL_Rect *frame) {
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}
196
201static void scrollToOffset(ScrollView *self, const SDL_Point *offset) {
202
203 if (self->contentView) {
204 const SDL_Size contentSize = $(self->contentView, size);
205 const SDL_Rect bounds = $((View *) self, bounds);
206
207 if (contentSize.w > bounds.w) {
208 self->contentOffset.x = clamp(offset->x, -(contentSize.w - bounds.w), 0);
209 } else {
210 self->contentOffset.x = 0;
211 }
212
213 if (contentSize.h > bounds.h) {
214 self->contentOffset.y = clamp(offset->y, -(contentSize.h - bounds.h), 0);
215 } else {
216 self->contentOffset.y = 0;
217 }
218
219 } else {
220 self->contentOffset.x = self->contentOffset.y = 0;
221 }
222
223 $((View *) self, setNeedsLayout);
224 $((View *) self->scrollBar, setNeedsLayout);
225}
226
231static void setContentView(ScrollView *self, View *contentView) {
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}
252
258
259 View *this = (View *) self;
260
261 $(this->style, addEnumAttribute, "scrollbar", ScrollBarVisibilityNames, visibility);
262
263 $(this, invalidateStyle);
264}
265
266#pragma mark - Class lifecycle
267
271static void initialize(Class *clazz) {
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}
287
292Class *_ScrollView(void) {
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}
308
309#undef _Class
Class * _Control(void)
Definition Control.c:414
static bool isHighlighted(const Control *self)
Definition Control.c:350
@ ControlStateHighlighted
Definition Control.h:67
static SDL_Size size(const Image *self)
Definition Image.c:416
static void removeSubview(View *self, View *subview)
Definition PageView.c:72
static void addSubview(View *self, View *subview)
Definition PageView.c:49
static SDL_Size contentSize(const Panel *self)
Definition Panel.c:201
static ScrollBar * initWithScrollView(ScrollBar *self, ScrollView *scrollView)
Definition ScrollBar.c:173
const EnumName ScrollBarVisibilityNames[]
Definition ScrollBar.c:31
ScrollBarVisibility
Governs whether a ScrollView's ScrollBar is shown.
Definition ScrollBar.h:67
@ ScrollBarHide
Definition ScrollBar.h:68
@ ScrollBarShow
Definition ScrollBar.h:69
static void setScrollBarVisibility(ScrollView *self, ScrollBarVisibility visibility)
Definition ScrollView.c:257
static View * init(View *self)
Definition ScrollView.c:93
static void awakeWithDictionary(View *self, const Dictionary *dictionary)
Definition ScrollView.c:50
Class * _ScrollView(void)
Definition ScrollView.c:292
static void applyStyle(View *self, const Style *style)
Definition ScrollView.c:75
static bool captureEvent(Control *self, const SDL_Event *event)
Definition ScrollView.c:140
static ScrollView * initWithFrame(ScrollView *self, const SDL_Rect *frame)
Definition ScrollView.c:179
static void dealloc(Object *self)
Definition ScrollView.c:35
static void setContentView(ScrollView *self, View *contentView)
Definition ScrollView.c:231
static void initialize(Class *clazz)
Definition ScrollView.c:271
static void layoutSubviews(View *self)
Definition ScrollView.c:100
ScrollViews allow users to pan their internal contents.
static void addEnumAttribute(Style *self, const char *attr, const EnumName *names, int value)
Definition Style.c:288
@ InletTypeEnum
Definition View+JSON.h:75
@ InletTypeView
Definition View+JSON.h:133
@ InletTypeFloat
Definition View+JSON.h:80
#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 removeClassName(View *self, const char *className)
Definition View.c:1445
static void addClassName(View *self, const char *className)
Definition View.c:183
static void invalidateStyle(View *self)
Definition View.c:1092
static void setNeedsLayout(View *self)
Definition View.c:1865
static void setVisibility(View *self, ViewVisibility visibility)
Definition View.c:1834
static SDL_Rect bounds(const View *self)
Definition View.c:552
@ ViewVisibilityVisible
Definition View.h:121
@ ViewVisibilityHidden
Definition View.h:122
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
Inlets enable inbound data binding of View attributes through JSON.
Definition View+JSON.h:149
A draggable scrollbar, owned by a ScrollView.
Definition ScrollBar.h:85
ScrollViews allow users to pan their internal contents.
Definition ScrollView.h:61
static void scrollToOffset(ScrollView *self, const SDL_Point *offset)
Scrolls the content View to the specified offset.
Definition ScrollView.c:201
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
View * contentView
The content View.
Definition ScrollView.h:84
SDL_Point contentOffset
The content View offset.
Definition ScrollView.h:77
The Style type.
Definition Style.h:43
Dictionary * attributes
Definition Style.h:59
Views are the fundamental building blocks of ObjectivelyMVC user interfaces.
Definition View.h:225
layoutSubviews(View *self)
Arranges this View's immediate subviews within its (already final) frame.
Definition Box.c:74