ObjectivelyMVC
Object oriented MVC framework for SDL3 and GNU C
Loading...
Searching...
No Matches
ScrollHandle.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 "ScrollHandle.h"
25
26#define _Class _ScrollHandle
27
28#pragma mark - View
29
33static View *init(View *self) {
34 return (View *) $((ScrollHandle *) self, initWithFrame, NULL);
35}
36
37#pragma mark - Control
38
42static bool captureEvent(Control *self, const SDL_Event *event) {
43
44 ScrollHandle *this = (ScrollHandle *) self;
45
46 if (event->type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
47 if ($((View *) self, didReceiveEvent, event)) {
48 self->state |= ControlStateHighlighted;
49 return true;
50 }
51 } else if (event->type == SDL_EVENT_MOUSE_MOTION) {
52 if (self->state & ControlStateHighlighted) {
53 if (this->delegate.didDrag) {
54 this->delegate.didDrag(this, event->motion.yrel);
55 }
56 return true;
57 }
58 } else if (event->type == SDL_EVENT_MOUSE_BUTTON_UP) {
59 if (self->state & ControlStateHighlighted) {
60 self->state &= ~ControlStateHighlighted;
61 return true;
62 }
63 }
64
65 return super(Control, self, captureEvent, event);
66}
67
68#pragma mark - ScrollHandle
69
74static ScrollHandle *initWithFrame(ScrollHandle *self, const SDL_Rect *frame) {
75
76 self = (ScrollHandle *) super(Control, self, initWithFrame, frame);
77 if (self) {
78 $((View *) self, addClassName, "handle");
79 }
80
81 return self;
82}
83
84#pragma mark - Class lifecycle
85
89static void initialize(Class *clazz) {
90
91 ((ViewInterface *) clazz->interface)->init = init;
92
93 ((ControlInterface *) clazz->interface)->captureEvent = captureEvent;
94
95 ((ScrollHandleInterface *) clazz->interface)->initWithFrame = initWithFrame;
96}
97
102Class *_ScrollHandle(void) {
103 static Class *clazz;
104 static Once once;
105
106 do_once(&once, {
107 clazz = _initialize(&(const ClassDef) {
108 .name = "ScrollHandle",
109 .superclass = _Control(),
110 .instanceSize = sizeof(ScrollHandle),
111 .interfaceOffset = offsetof(ScrollHandle, interface),
112 .interfaceSize = sizeof(ScrollHandleInterface),
114 });
115 });
116
117 return clazz;
118}
119
120#undef _Class
Class * _Control(void)
Definition Control.c:391
@ ControlStateHighlighted
Definition Control.h:67
static View * init(View *self)
static bool captureEvent(Control *self, const SDL_Event *event)
static ScrollHandle * initWithFrame(ScrollHandle *self, const SDL_Rect *frame)
static void initialize(Class *clazz)
Class * _ScrollHandle(void)
The draggable handle of a ScrollBar.
static void addClassName(View *self, const char *className)
Definition View.c:159
static bool didReceiveEvent(const View *self, const SDL_Event *event)
Definition View.c:657
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
A draggable scrollbar handle.
Views are the fundamental building blocks of ObjectivelyMVC user interfaces.
Definition View.h:134
View * init(View *self)
Initializes this View.
Definition Box.c:67
View * initWithFrame(View *self, const SDL_Rect *frame)
Initializes this View with the specified frame.
Definition View.c:978