ObjectivelyMVC 0.1.0
Object oriented MVC framework for OpenGL, SDL2 and GNU C
Macros | Functions
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 192 of file ScrollView.c.

192 {
193 static Class *clazz;
194 static Once once;
195
196 do_once(&once, {
197 clazz = _initialize(&(const ClassDef) {
198 .name = "ScrollView",
199 .superclass = _Control(),
200 .instanceSize = sizeof(ScrollView),
201 .interfaceOffset = offsetof(ScrollView, interface),
202 .interfaceSize = sizeof(ScrollViewInterface),
204 });
205 });
206
207 return clazz;
208}
static void initialize(Class *clazz)
Definition: ScrollView.c:175
Class * _Control(void)
The Control archetype.
Definition: Control.c:379
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 68 of file ScrollView.c.

68 {
69
70 ScrollView *this = (ScrollView *) self;
71
72 if (event->type == SDL_MOUSEMOTION && (event->motion.state & SDL_BUTTON_LMASK)) {
74
75 SDL_Point offset = this->contentOffset;
76
77 offset.x += event->motion.xrel;
78 offset.y += event->motion.yrel;
79
80 $(this, scrollToOffset, &offset);
81 return true;
82 } else if (event->type == SDL_MOUSEWHEEL) {
83 SDL_Point offset = this->contentOffset;
84
85 offset.x -= event->wheel.x * this->step;
86 offset.y += event->wheel.y * this->step;
87
88 $(this, scrollToOffset, &offset);
89 return true;
90 } else if (event->type == SDL_MOUSEBUTTONUP && (event->button.button & SDL_BUTTON_LMASK)) {
91
92 if ($(self, isHighlighted)) {
93 self->state &= ~ControlStateHighlighted;
94 return true;
95 }
96 }
97
98 return super(Control, self, captureEvent, event);
99}
@ ControlStateHighlighted
Definition: Control.h:68
Controls are Views which capture events and dispatch Actions.
Definition: Control.h:83
_Bool captureEvent(Control *self, const SDL_Event *event)
Captures a given event, potentially altering the state of this Control.
Definition: Button.c:76
unsigned int state
The bit mask of ControlState.
Definition: Control.h:110
_Bool isHighlighted(const Control *self)
Definition: Control.c:317
static void scrollToOffset(ScrollView *self, const SDL_Point *offset)
Scrolls the content View to the specified offset.
Definition: ScrollView.c:121

◆ 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 175 of file ScrollView.c.

175 {
176
177 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
178
179 ((ViewInterface *) clazz->interface)->layoutSubviews = layoutSubviews;
180
181 ((ControlInterface *) clazz->interface)->captureEvent = captureEvent;
182
183 ((ScrollViewInterface *) clazz->interface)->initWithFrame = initWithFrame;
184 ((ScrollViewInterface *) clazz->interface)->scrollToOffset = scrollToOffset;
185 ((ScrollViewInterface *) clazz->interface)->setContentView = setContentView;
186}
Box * initWithFrame(Box *self, const SDL_Rect *frame)
Initializes this Box with the given frame.
Definition: Box.c:92
void setContentView(ScrollView *self, View *contentView)
Sets the content view of this ScrollView.
Definition: ScrollView.c:150
layoutSubviews(View *self)
Performs layout for this View's immediate subviews.
Definition: Box.c:74

◆ initWithFrame()

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

Definition at line 107 of file ScrollView.c.

107 {
108
109 self = (ScrollView *) super(Control, self, initWithFrame, frame);
110 if (self) {
112 }
113
114 return self;
115}
#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 $(this->contentView, sizeToContain);
60 }
61}
Views are the fundamental building blocks of ObjectivelyMVC user interfaces.
Definition: View.h:133
SDL_Rect frame
The frame, relative to the superview.
Definition: View.h:190
void sizeToContain(View *self)
Resizes this View to contain its subviews.
Definition: View.c:1485

◆ setContentView()

static void setContentView ( ScrollView self,
View contentView 
)
static

Definition at line 150 of file ScrollView.c.

150 {
151
152 if (contentView != self->contentView) {
153
154 if (self->contentView) {
155 $(self->contentView, removeClassName, "contentView");
156 $((View *) self, removeSubview, self->contentView);
157 self->contentView = release(self->contentView);
158 }
159
160 if (contentView) {
161 $(contentView, addClassName, "contentView");
162 $((View *) self, addSubview, contentView);
163 self->contentView = retain(contentView);
164 }
165
166 $(self, scrollToOffset, &MakePoint(0, 0));
167 }
168}
#define MakePoint(x, y)
Creates an SDL_Point with the given coordinates.
Definition: Types.h:69
View * contentView
The content View.
Definition: ScrollView.h:83
void addSubview(View *self, View *subview)
Adds a subview to this view, to be drawn above its siblings.
Definition: PageView.c:35
void addClassName(View *self, const char *className)
Adds the given class name to this View.
Definition: View.c:120
void removeClassName(View *self, const char *className)
Removes the given class name to this View.
Definition: View.c:1158
void removeSubview(View *self, View *subview)
Removes the given subview from this View.
Definition: PageView.c:58