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
41 super(Object, self, dealloc);
42}
43
44#pragma mark - View
45
49static void layoutSubviews(View *self) {
50
51 super(View, self, layoutSubviews);
52
53 ScrollView *this = (ScrollView *) self;
54
55 if (this->contentView) {
56 this->contentView->frame.x = this->contentOffset.x;
57 this->contentView->frame.y = this->contentOffset.y;
58 }
59}
60
61#pragma mark - Control
62
66static bool captureEvent(Control *self, const SDL_Event *event) {
67
68 ScrollView *this = (ScrollView *) self;
69
70 if (event->type == SDL_EVENT_MOUSE_MOTION && (event->motion.state & SDL_BUTTON_LMASK)) {
72
73 SDL_Point offset = this->contentOffset;
74
75 offset.x += event->motion.xrel;
76 offset.y += event->motion.yrel;
77
78 $(this, scrollToOffset, &offset);
79 return true;
80 } else if (event->type == SDL_EVENT_MOUSE_WHEEL) {
81 SDL_Point offset = this->contentOffset;
82
83 offset.x -= event->wheel.x * this->step;
84 offset.y += event->wheel.y * this->step;
85
86 $(this, scrollToOffset, &offset);
87 return true;
88 } else if (event->type == SDL_EVENT_MOUSE_BUTTON_UP && (event->button.button & SDL_BUTTON_LMASK)) {
89
90 if ($(self, isHighlighted)) {
91 self->state &= ~ControlStateHighlighted;
92 return true;
93 }
94 }
95
96 return super(Control, self, captureEvent, event);
97}
98
99#pragma mark - ScrollView
100
105static ScrollView *initWithFrame(ScrollView *self, const SDL_Rect *frame) {
106
107 self = (ScrollView *) super(Control, self, initWithFrame, frame);
108 if (self) {
110 }
111
112 return self;
113}
114
119static void scrollToOffset(ScrollView *self, const SDL_Point *offset) {
120
121 if (self->contentView) {
122 const SDL_Size contentSize = $(self->contentView, size);
123 const SDL_Rect bounds = $((View *) self, bounds);
124
125 if (contentSize.w > bounds.w) {
126 self->contentOffset.x = clamp(offset->x, -(contentSize.w - bounds.w), 0);
127 } else {
128 self->contentOffset.x = 0;
129 }
130
131 if (contentSize.h > bounds.h) {
132 self->contentOffset.y = clamp(offset->y, -(contentSize.h - bounds.h), 0);
133 } else {
134 self->contentOffset.y = 0;
135 }
136
137 } else {
138 self->contentOffset.x = self->contentOffset.y = 0;
139 }
140
141 self->control.view.needsLayout = true;
142}
143
148static void setContentView(ScrollView *self, View *contentView) {
149
150 if (contentView != self->contentView) {
151
152 if (self->contentView) {
153 $(self->contentView, removeClassName, "contentView");
154 $((View *) self, removeSubview, self->contentView);
155 self->contentView = release(self->contentView);
156 }
157
158 if (contentView) {
159 $(contentView, addClassName, "contentView");
160 $((View *) self, addSubview, contentView);
161 self->contentView = retain(contentView);
162 }
163
164 $(self, scrollToOffset, &MakePoint(0, 0));
165 }
166}
167
168#pragma mark - Class lifecycle
169
173static void initialize(Class *clazz) {
174
175 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
176
177 ((ViewInterface *) clazz->interface)->layoutSubviews = layoutSubviews;
178
179 ((ControlInterface *) clazz->interface)->captureEvent = captureEvent;
180
181 ((ScrollViewInterface *) clazz->interface)->initWithFrame = initWithFrame;
182 ((ScrollViewInterface *) clazz->interface)->scrollToOffset = scrollToOffset;
183 ((ScrollViewInterface *) clazz->interface)->setContentView = setContentView;
184}
185
190Class *_ScrollView(void) {
191 static Class *clazz;
192 static Once once;
193
194 do_once(&once, {
195 clazz = _initialize(&(const ClassDef) {
196 .name = "ScrollView",
197 .superclass = _Control(),
198 .instanceSize = sizeof(ScrollView),
199 .interfaceOffset = offsetof(ScrollView, interface),
200 .interfaceSize = sizeof(ScrollViewInterface),
202 });
203 });
204
205 return clazz;
206}
207
208#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
Class * _ScrollView(void)
Definition ScrollView.c:190
static bool captureEvent(Control *self, const SDL_Event *event)
Definition ScrollView.c:66
static ScrollView * initWithFrame(ScrollView *self, const SDL_Rect *frame)
Definition ScrollView.c:105
static void dealloc(Object *self)
Definition ScrollView.c:35
static void setContentView(ScrollView *self, View *contentView)
Definition ScrollView.c:148
static void initialize(Class *clazz)
Definition ScrollView.c:173
static void layoutSubviews(View *self)
Definition ScrollView.c:49
ScrollViews allow users to pan their internal contents.
#define DEFAULT_SCROLL_VIEW_STEP
Definition ScrollView.h:33
static void removeClassName(View *self, const char *className)
Definition View.c:1344
static void addClassName(View *self, const char *className)
Definition View.c:159
static SDL_Rect bounds(const View *self)
Definition View.c:492
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
ScrollViews allow users to pan their internal contents.
Definition ScrollView.h:62
static void scrollToOffset(ScrollView *self, const SDL_Point *offset)
Scrolls the content View to the specified offset.
Definition ScrollView.c:119
float step
The scroll step, in pixels.
Definition ScrollView.h:88
View * contentView
The content View.
Definition ScrollView.h:83
Control control
The superclass.
Definition ScrollView.h:67
SDL_Point contentOffset
The content View offset.
Definition ScrollView.h:78
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
SDL_Rect frame
The frame, relative to the superview.
Definition View.h:191