/
penge-clickable-label.c
411 lines (331 loc) · 11.6 KB
/
penge-clickable-label.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
* Copyright (C) 2010 Intel Corporation.
*
* Author: Rob Bradford <rob@linux.intel.com>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU Lesser General Public License,
* version 2.1, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
* more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
/* Strongly inspired by Emmanuele Bassi's 'tweet' */
#include "penge-clickable-label.h"
/* for pointer cursor support */
#include <clutter/x11/clutter-x11.h>
#include <X11/Xlib.h>
#include <X11/cursorfont.h>
G_DEFINE_TYPE (PengeClickableLabel, penge_clickable_label, MX_TYPE_LABEL)
#define GET_PRIVATE_REAL(o) \
(G_TYPE_INSTANCE_GET_PRIVATE ((o), PENGE_TYPE_CLICKABLE_LABEL, PengeClickableLabelPrivate))
#define GET_PRIVATE(o) ((PengeClickableLabel *)o)->priv
struct _PengeClickableLabelPrivate {
GRegex *url_regex;
GArray *matches;
};
static const char tweet_url_label_regex[] = "\\b https?://[\\S]+?(?=\\.?(\\s|$))";
typedef struct
{
gint start, end;
} _UrlLabelMatch;
enum
{
URL_CLICKED,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL] = { 0, };
static void
penge_clickable_label_get_property (GObject *object, guint property_id,
GValue *value, GParamSpec *pspec)
{
switch (property_id) {
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
}
static void
penge_clickable_label_set_property (GObject *object, guint property_id,
const GValue *value, GParamSpec *pspec)
{
switch (property_id) {
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
}
static void
penge_clickable_label_dispose (GObject *object)
{
PengeClickableLabelPrivate *priv = GET_PRIVATE (object);
if (priv->url_regex)
{
g_regex_unref (priv->url_regex);
priv->url_regex = NULL;
}
G_OBJECT_CLASS (penge_clickable_label_parent_class)->dispose (object);
}
static void
penge_clickable_label_finalize (GObject *object)
{
PengeClickableLabelPrivate *priv = GET_PRIVATE (object);
g_array_free (priv->matches, TRUE);
G_OBJECT_CLASS (penge_clickable_label_parent_class)->finalize (object);
}
static void
_update_attributes_from_matches (PengeClickableLabel *label,
_UrlLabelMatch *match_in)
{
PengeClickableLabelPrivate *priv = GET_PRIVATE (label);
PangoAttrList *attrs;
PangoAttribute *attr;
ClutterActor *text;
const gchar *str;
gint i = 0;
text = mx_label_get_clutter_text (MX_LABEL (label));
str = clutter_text_get_text (CLUTTER_TEXT (text));
attrs = pango_attr_list_new ();
for (i = 0; i < priv->matches->len; i++)
{
_UrlLabelMatch *match = &g_array_index (priv->matches,
_UrlLabelMatch,
i);
if (match == match_in)
{
attr = pango_attr_foreground_new ((0xac * 0xffff) / 0xff,
(0x30 * 0xffff) / 0xff,
(0xe0 * 0xffff) / 0xff);
} else {
attr = pango_attr_foreground_new ((0x30 * 0xffff) / 0xff,
(0xac * 0xffff) / 0xff,
(0xe0 * 0xffff) / 0xff);
}
attr->start_index = match->start;
attr->end_index = match->end;
pango_attr_list_change (attrs, attr);
}
clutter_text_set_attributes (CLUTTER_TEXT (text), attrs);
pango_attr_list_unref (attrs);
}
static void
_text_changed_notify_cb (ClutterText *text,
GParamSpec *pspec,
PengeClickableLabel *label)
{
PengeClickableLabelPrivate *priv = GET_PRIVATE (label);
/* Clear any existing matches */
g_array_set_size (priv->matches, 0);
if (priv->url_regex)
{
GMatchInfo *match_info;
const gchar *str;
str = clutter_text_get_text (text);
/* Find each URL and keep track of its location */
g_regex_match (priv->url_regex, str, 0, &match_info);
while (g_match_info_matches (match_info))
{
_UrlLabelMatch match;
if (g_match_info_fetch_pos (match_info, 0, &match.start, &match.end))
g_array_append_val (priv->matches, match);
g_match_info_next (match_info, NULL);
}
g_match_info_free (match_info);
}
/* If there is at least one URL then make sure the actor is reactive
so we can detect when the cursor moves over it */
if (priv->matches->len > 0)
{
clutter_actor_set_reactive (CLUTTER_ACTOR (label), TRUE);
clutter_actor_set_reactive (CLUTTER_ACTOR (text), TRUE);
}
_update_attributes_from_matches (label, NULL);
}
static gboolean
_button_release_event_cb (ClutterActor *actor,
ClutterEvent *event,
PengeClickableLabel *label)
{
PengeClickableLabelPrivate *priv = GET_PRIVATE (label);
ClutterButtonEvent *bevent = (ClutterButtonEvent *)event;
gfloat layout_x, layout_y;
PangoLayout *layout;
const gchar *str;
gint i = 0, index = 0;
clutter_actor_transform_stage_point (actor,
bevent->x,
bevent->y,
&layout_x,
&layout_y);
layout = clutter_text_get_layout (CLUTTER_TEXT (actor));
str = clutter_text_get_text (CLUTTER_TEXT (actor));
if (pango_layout_xy_to_index (layout,
layout_x * PANGO_SCALE,
layout_y * PANGO_SCALE,
&index,
NULL))
{
/* Check whether that byte index is covered by any of the
URL matches */
for (i = 0; i < priv->matches->len; i++)
{
_UrlLabelMatch *match;
match = &g_array_index (priv->matches, _UrlLabelMatch, i);
if (index >= match->start && index < match->end)
{
gchar *url_text = g_strndup (str + match->start,
match->end - match->start);
g_signal_emit (label, signals[URL_CLICKED], 0, url_text);
g_free (url_text);
return TRUE;
}
}
}
return FALSE;
}
static gboolean
_motion_event_cb (ClutterActor *actor,
ClutterEvent *event,
PengeClickableLabel *label)
{
PengeClickableLabelPrivate *priv = GET_PRIVATE (label);
ClutterButtonEvent *bevent = (ClutterButtonEvent *)event;
gfloat layout_x, layout_y;
PangoLayout *layout;
const gchar *str;
gint i = 0, index = 0;
Display *dpy;
ClutterActor *stage;
Window win;
static Cursor hand = None;
dpy = clutter_x11_get_default_display ();
stage = clutter_actor_get_stage (actor);
win = clutter_x11_get_stage_window (CLUTTER_STAGE (stage));
if (hand == None)
hand = XCreateFontCursor (dpy, XC_hand2);
clutter_actor_transform_stage_point (actor,
bevent->x,
bevent->y,
&layout_x,
&layout_y);
layout = clutter_text_get_layout (CLUTTER_TEXT (actor));
str = clutter_text_get_text (CLUTTER_TEXT (actor));
if (pango_layout_xy_to_index (layout,
layout_x * PANGO_SCALE,
layout_y * PANGO_SCALE,
&index,
NULL))
{
/* Check whether that byte index is covered by any of the
URL matches */
for (i = 0; i < priv->matches->len; i++)
{
_UrlLabelMatch *match;
match = &g_array_index (priv->matches, _UrlLabelMatch, i);
if (index >= match->start && index < match->end)
{
XDefineCursor (dpy, win, hand);
_update_attributes_from_matches (label, match);
return FALSE;
}
}
}
XUndefineCursor (dpy, win);
_update_attributes_from_matches (label, NULL);
return FALSE;
}
static gboolean
_leave_event_cb (ClutterActor *actor,
ClutterEvent *event,
PengeClickableLabel *label)
{
Display *dpy;
ClutterActor *stage;
Window win;
dpy = clutter_x11_get_default_display ();
stage = clutter_actor_get_stage (actor);
win = clutter_x11_get_stage_window (CLUTTER_STAGE (stage));
/* Remove cursor */
XUndefineCursor (dpy, win);
_update_attributes_from_matches (label, NULL);
return FALSE;
}
static void
penge_clickable_label_constructed (GObject *object)
{
ClutterActor *text;
text = mx_label_get_clutter_text (MX_LABEL (object));
g_signal_connect (text,
"notify::text",
(GCallback)_text_changed_notify_cb,
object);
g_object_notify (G_OBJECT (text), "text");
g_signal_connect (text,
"button-release-event",
(GCallback)_button_release_event_cb,
object);
g_signal_connect (text,
"motion-event",
(GCallback)_motion_event_cb,
object);
g_signal_connect (text,
"leave-event",
(GCallback)_leave_event_cb,
object);
if (G_OBJECT_CLASS (penge_clickable_label_parent_class)->constructed)
G_OBJECT_CLASS (penge_clickable_label_parent_class)->constructed (object);
}
static void
penge_clickable_label_class_init (PengeClickableLabelClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
g_type_class_add_private (klass, sizeof (PengeClickableLabelPrivate));
object_class->get_property = penge_clickable_label_get_property;
object_class->set_property = penge_clickable_label_set_property;
object_class->dispose = penge_clickable_label_dispose;
object_class->finalize = penge_clickable_label_finalize;
object_class->constructed = penge_clickable_label_constructed;
signals[URL_CLICKED] = g_signal_new ("url-clicked",
PENGE_TYPE_CLICKABLE_LABEL,
G_SIGNAL_RUN_FIRST,
0,
0,
NULL,
g_cclosure_marshal_VOID__STRING,
G_TYPE_NONE,
1,
G_TYPE_STRING);
}
static void
penge_clickable_label_init (PengeClickableLabel *self)
{
PengeClickableLabelPrivate *priv = GET_PRIVATE_REAL (self);
GError *error = NULL;
self->priv = priv;
priv->url_regex = g_regex_new (tweet_url_label_regex,
G_REGEX_CASELESS
| G_REGEX_EXTENDED
| G_REGEX_NO_AUTO_CAPTURE,
0, &error);
if (priv->url_regex == NULL)
{
g_critical ("Compilation of URL matching regex failed: %s",
error->message);
g_clear_error (&error);
}
priv->matches = g_array_new (FALSE, FALSE, sizeof (_UrlLabelMatch));
}
ClutterActor *
penge_clickable_label_new (const gchar *text)
{
if (!text)
text = "";
return g_object_new (PENGE_TYPE_CLICKABLE_LABEL,
"text", text,
NULL);
}