summaryrefslogtreecommitdiff
path: root/gui-aoi.c
blob: f44db302040c63d559909adc6c952f33c152c350 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
 * gui-aoi.c - GUI: areas of interest
 *
 * Written 2016 by Werner Almesberger
 * Copyright 2016 by Werner Almesberger
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

/*
 * Resources:
 *
 * http://zetcode.com/gfx/cairo/cairobackends/
 * https://developer.gnome.org/gtk3/stable/gtk-migrating-2-to-3.html
 */

#include <stddef.h>

#include "util.h"
#include "gui-aoi.h"


static const struct aoi *hovering = NULL;


const struct aoi *aoi_add(struct aoi **aois, const struct aoi *aoi)
{
	struct aoi *new;

	new = alloc_type(struct aoi);
	*new = *aoi;
	new->next = *aois;
	*aois = new;

	return new;
}


bool aoi_hover(const struct aoi *aois, int x, int y)
{
	const struct aoi *aoi;

	if (hovering) {
		if (x >= hovering->x && x < hovering->x + hovering->w &&
		    y >= hovering->y && y < hovering->y + hovering->h)
			return 1;
		hovering->hover(hovering->user, 0);
		hovering = NULL;
	}

	for (aoi = aois; aoi; aoi = aoi->next)
		if (x >= aoi->x && x < aoi->x + aoi->w &&
		    y >= aoi->y && y < aoi->y + aoi->h)
			break;
	if (aoi && aoi->hover && aoi->hover(aoi->user, 1)) {
		hovering = aoi;
		return 1;
	}
	return 0;
}


bool aoi_click(const struct aoi *aois, int x, int y)
{
	const struct aoi *aoi;

	if (hovering) {
		hovering->hover(hovering->user, 0);
		hovering = NULL;
	}
	for (aoi = aois; aoi; aoi = aoi->next)
		if (x >= aoi->x && x < aoi->x + aoi->w &&
		    y >= aoi->y && y < aoi->y + aoi->h)
			break;
	if (aoi && aoi->click) {
		aoi->click(aoi->user);
		return 1;
	}
	return 0;
}


void aoi_remove(struct aoi **aois, const struct aoi *aoi)
{
	if (hovering == aoi)
		aoi->hover(aoi->user, 0);
	while (*aois != aoi)
		aois = &(*aois)->next;
	*aois = aoi->next;
	free((void *) aoi);
}