forked from nsf/bmpanel2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event-dispatchers.c
171 lines (156 loc) · 4.25 KB
/
event-dispatchers.c
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "gui.h"
static inline int point_in_rect(int px, int py, int x, int y, int w, int h)
{
return (px >= x &&
px < x + w &&
py >= y &&
py < y + h);
}
void disp_button_press_release(struct panel *p, XButtonEvent *e)
{
if (e->type == ButtonRelease && !p->dnd.taken_on) {
p->last_click_widget = 0;
p->last_click_x = 0;
p->last_click_y = 0;
p->last_button = 0;
}
size_t i;
for (i = 0; i < p->widgets_n; ++i) {
struct widget *w = &p->widgets[i];
if (point_in_rect(e->x, e->y, w->x, 0, w->width, p->height)) {
if (!p->dnd.taken_on) {
if (e->type == ButtonPress) {
p->last_click_widget = w;
p->last_click_x = e->x;
p->last_click_y = e->y;
p->last_button = e->button;
}
if (w->interface->button_click)
(*w->interface->button_click)(w, e);
} else {
if (e->type == ButtonRelease) {
p->dnd.dropped_on = w;
p->dnd.dropped_x = e->x;
p->dnd.dropped_y = e->y;
if (w->interface->dnd_drop)
(*w->interface->dnd_drop)(w, &p->dnd);
}
}
break;
}
}
if (e->type == ButtonRelease && p->dnd.taken_on) {
struct widget *w = p->dnd.taken_on;
if (w->interface->dnd_drop && p->dnd.taken_on != p->dnd.dropped_on)
(*w->interface->dnd_drop)(w, &p->dnd);
CLEAR_STRUCT(&p->dnd);
}
}
void disp_motion_notify(struct panel *p, XMotionEvent *e)
{
size_t i;
/* is there any widget under mouse at all? for example if mouse is on
top of separator, there is no widget under it */
int widget_under_mouse = 0;
/* motion events: enter, leave, motion */
for (i = 0; i < p->widgets_n; ++i) {
struct widget *w = &p->widgets[i];
if (point_in_rect(e->x, e->y, w->x, 0, w->width, p->height)) {
if (w == p->under_mouse) {
if (w->interface->mouse_motion)
(*w->interface->mouse_motion)(w, e);
} else {
if (p->under_mouse &&
p->under_mouse->interface->mouse_leave)
{
(*p->under_mouse->interface->
mouse_leave)(p->under_mouse);
}
p->under_mouse = w;
if (w->interface->mouse_enter)
(*w->interface->mouse_enter)(w);
}
widget_under_mouse = 1;
}
}
if (!widget_under_mouse) {
if (p->under_mouse && p->under_mouse->interface->mouse_leave)
(*p->under_mouse->interface->mouse_leave)(p->under_mouse);
p->under_mouse = 0;
}
/* drag'n'drop moving */
if (p->dnd.taken_on) {
p->dnd.cur_root_x = e->x_root;
p->dnd.cur_root_y = e->y_root;
p->dnd.cur_x = e->x;
p->dnd.cur_y = e->y;
struct widget *w = p->dnd.taken_on;
if (w->interface->dnd_drag)
(*w->interface->dnd_drag)(w, &p->dnd);
}
/* drag'n'drop detection */
if (p->last_click_widget && (abs(p->last_click_x - e->x) > p->drag_threshold
|| abs(p->last_click_y - e->y) > p->drag_threshold))
{
/* drag detected */
struct widget *w = p->last_click_widget;
p->dnd.taken_on = w;
p->dnd.taken_x = p->last_click_x;
p->dnd.taken_y = p->last_click_y;
p->dnd.cur_x = e->x;
p->dnd.cur_y = e->y;
p->dnd.cur_root_x = e->x_root;
p->dnd.cur_root_y = e->y_root;
p->dnd.button = p->last_button;
if (w->interface->dnd_start)
(*w->interface->dnd_start)(w, &p->dnd);
p->last_click_widget = 0;
p->last_click_x = 0;
p->last_click_y = 0;
p->last_button = 0;
}
}
void disp_enter_leave_notify(struct panel *p, XCrossingEvent *e)
{
if (e->type == LeaveNotify) {
if (p->under_mouse && p->under_mouse->interface->mouse_leave)
(*p->under_mouse->interface->mouse_leave)(p->under_mouse);
p->under_mouse = 0;
}
}
void disp_property_notify(struct panel *p, XPropertyEvent *e)
{
size_t i;
for (i = 0; i < p->widgets_n; ++i) {
struct widget *w = &p->widgets[i];
if (w->interface->prop_change)
(*w->interface->prop_change)(w, e);
}
}
void disp_client_msg(struct panel *p, XClientMessageEvent *e)
{
size_t i;
for (i = 0; i < p->widgets_n; ++i) {
struct widget *w = &p->widgets[i];
if (w->interface->client_msg)
(*w->interface->client_msg)(w, e);
}
}
void disp_win_destroy(struct panel *p, XDestroyWindowEvent *e)
{
size_t i;
for (i = 0; i < p->widgets_n; ++i) {
struct widget *w = &p->widgets[i];
if (w->interface->win_destroy)
(*w->interface->win_destroy)(w, e);
}
}
void disp_configure(struct panel *p, XConfigureEvent *e)
{
size_t i;
for (i = 0; i < p->widgets_n; ++i) {
struct widget *w = &p->widgets[i];
if (w->interface->configure)
(*w->interface->configure)(w, e);
}
}