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 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}
63
67static void applyStyle(View *self, const Style *style) {
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}
81
85static void layoutSubviews(View *self) {
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}
122
123#pragma mark - Control
124
128static bool captureEvent(Control *self, const SDL_Event *event) {
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}
160
161#pragma mark - ScrollView
162
167static ScrollView *initWithFrame(ScrollView *self, const SDL_Rect *frame) {
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;
180 scrollBar->hidden = true;
181
182 $((View *) self, addSubview, scrollBar);
183 }
184
185 return self;
186}
187
192static void scrollToOffset(ScrollView *self, const SDL_Point *offset) {
193
194 if (self->contentView) {
195 const SDL_Size contentSize = $(self->contentView, size);
196 const SDL_Rect bounds = $((View *) self, bounds);
197
198 if (contentSize.w > bounds.w) {
199 self->contentOffset.x = clamp(offset->x, -(contentSize.w - bounds.w), 0);
200 } else {
201 self->contentOffset.x = 0;
202 }
203
204 if (contentSize.h > bounds.h) {
205 self->contentOffset.y = clamp(offset->y, -(contentSize.h - bounds.h), 0);
206 } else {
207 self->contentOffset.y = 0;
208 }
209
210 } else {
211 self->contentOffset.x = self->contentOffset.y = 0;
212 }
213
214 self->control.view.needsLayout = true;
215 ((View *) self->scrollBar)->needsLayout = true;
216}
217
222static void setContentView(ScrollView *self, View *contentView) {
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}
243
249
250 self->scrollBarVisibility = visibility;
251 self->control.view.needsLayout = true;
252}
253
254#pragma mark - Class lifecycle
255
259static void initialize(Class *clazz) {
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}
274
279Class *_ScrollView(void) {
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}
296
297#undef _Class
Class * _Control(void)
Definition Control.c:391
static bool isHighlighted(const Control *self)
Definition Control.c:327
@ ControlStateHighlighted
Definition Control.h:67
static SDL_Size size(const Image *self)
Definition Image.c:181
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:195
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
@ ScrollBarAuto
Definition ScrollBar.h:70
@ ScrollBarShow
Definition ScrollBar.h:69
static void setScrollBarVisibility(ScrollView *self, ScrollBarVisibility visibility)
Definition ScrollView.c:248
static void awakeWithDictionary(View *self, const Dictionary *dictionary)
Definition ScrollView.c:50
Class * _ScrollView(void)
Definition ScrollView.c:279
static void applyStyle(View *self, const Style *style)
Definition ScrollView.c:67
static bool captureEvent(Control *self, const SDL_Event *event)
Definition ScrollView.c:128
static ScrollView * initWithFrame(ScrollView *self, const SDL_Rect *frame)
Definition ScrollView.c:167
static void dealloc(Object *self)
Definition ScrollView.c:35
static void setContentView(ScrollView *self, View *contentView)
Definition ScrollView.c:222
static void initialize(Class *clazz)
Definition ScrollView.c:259
static void layoutSubviews(View *self)
Definition ScrollView.c:85
ScrollViews allow users to pan their internal contents.
@ InletTypeEnum
Definition View+JSON.h:75
@ InletTypeFloat
Definition View+JSON.h:80
#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
static void removeClassName(View *self, const char *className)
Definition View.c:1333
static void addClassName(View *self, const char *className)
Definition View.c:159
static SDL_Rect bounds(const View *self)
Definition View.c:492
@ ViewAutoresizingHeight
Definition View.h:89
@ ViewAlignmentRight
Definition View.h:68
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
View view
The superclass.
Definition Control.h:88
Inlets enable inbound data binding of View attributes through JSON.
Definition View+JSON.h:155
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:192
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
View * contentView
The content View.
Definition ScrollView.h:82
Control control
The superclass.
Definition ScrollView.h:66
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:134
ViewAlignment alignment
The alignment.
Definition View.h:150
int autoresizingMask
The ViewAutoresizing bitmask.
Definition View.h:155
bool needsLayout
If true, this View will layout its subviews before it is drawn.
Definition View.h:222
bool hidden
If true, this View is not drawn.
Definition View.h:196