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 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)
 

Macro Definition Documentation

◆ _Class

#define _Class   _ScrollView

Definition at line 28 of file ScrollView.c.

Function Documentation

◆ _ScrollView()

Class * _ScrollView ( void  )

Definition at line 190 of file ScrollView.c.

190 {
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}
Class * _Control(void)
Definition Control.c:391
static void initialize(Class *clazz)
Definition ScrollView.c:173
ScrollViews allow users to pan their internal contents.
Definition ScrollView.h:62

◆ captureEvent()

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

Definition at line 66 of file ScrollView.c.

66 {
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}
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:66
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
41 super(Object, self, dealloc);
42}
static void dealloc(Object *self)
Definition ScrollView.c:35

◆ initialize()

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

Definition at line 173 of file ScrollView.c.

173 {
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}
static ScrollView * initWithFrame(ScrollView *self, const SDL_Rect *frame)
Definition ScrollView.c:105
static void setContentView(ScrollView *self, View *contentView)
Definition ScrollView.c:148
static void layoutSubviews(View *self)
Definition ScrollView.c:49
static void scrollToOffset(ScrollView *self, const SDL_Point *offset)
Scrolls the content View to the specified offset.
Definition ScrollView.c:119

◆ initWithFrame()

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

Definition at line 105 of file ScrollView.c.

105 {
106
107 self = (ScrollView *) super(Control, self, initWithFrame, frame);
108 if (self) {
110 }
111
112 return self;
113}
#define DEFAULT_SCROLL_VIEW_STEP
Definition ScrollView.h:33
float step
The scroll step, in pixels.
Definition ScrollView.h:88

◆ layoutSubviews()

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

Definition at line 49 of file ScrollView.c.

49 {
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}
Views are the fundamental building blocks of ObjectivelyMVC user interfaces.
Definition View.h:134
SDL_Rect frame
The frame, relative to the superview.
Definition View.h:191

◆ setContentView()

static void setContentView ( ScrollView self,
View contentView 
)
static

Definition at line 148 of file ScrollView.c.

148 {
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}
static void removeSubview(View *self, View *subview)
Definition PageView.c:72
static void addSubview(View *self, View *subview)
Definition PageView.c:49
static void removeClassName(View *self, const char *className)
Definition View.c:1344
static void addClassName(View *self, const char *className)
Definition View.c:159
View * contentView
The content View.
Definition ScrollView.h:83