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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
|
/*
* Copyright (C) 2001 Ximian, Inc.
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Authors:
* Chema Celorio <chema@celorio.com>
* Paolo Borelli <pborelli@katamail.com>
*/
#include "config.h"
#include <string.h>
#include <glib/gi18n-lib.h>
#include "glade.h"
#include "glade-signal.h"
#include "glade-xml-utils.h"
struct _GladeSignalPrivate
{
const GladeSignalClass *class; /* Pointer to the signal class */
gchar *detail; /* Signal detail */
gchar *handler; /* Handler function eg "gtk_main_quit" */
gchar *userdata; /* User data signal handler argument */
gchar *support_warning;/* Message to inform the user about signals introduced in future versions */
guint8 after : 1; /* Connect after TRUE or FALSE */
guint8 swapped : 1; /* Connect swapped TRUE or FALSE (GtkBuilder only) */
};
enum {
PROP_0,
PROP_CLASS,
PROP_DETAIL,
PROP_HANDLER,
PROP_USERDATA,
PROP_SUPPORT_WARNING,
PROP_AFTER,
PROP_SWAPPED,
N_PROPERTIES
};
/* We need these defines because GladeSignalClass is another object type!
* So we use GladeSignalKlass as the class name for GladeSignal
*/
#define GladeSignalClass GladeSignalKlass
#define glade_signal_class_init glade_signal_klass_init
G_DEFINE_TYPE_WITH_PRIVATE (GladeSignal, glade_signal, G_TYPE_OBJECT)
#undef GladeSignalClass
#undef glade_signal_class_init
static GParamSpec *properties[N_PROPERTIES];
static void
glade_signal_finalize (GObject *object)
{
GladeSignal *signal = GLADE_SIGNAL (object);
g_free (signal->priv->detail);
g_free (signal->priv->handler);
g_free (signal->priv->userdata);
g_free (signal->priv->support_warning);
G_OBJECT_CLASS (glade_signal_parent_class)->finalize (object);
}
static void
glade_signal_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GladeSignal *signal = GLADE_SIGNAL (object);
switch (prop_id)
{
case PROP_CLASS:
g_value_set_pointer (value, (gpointer) signal->priv->class);
break;
case PROP_DETAIL:
g_value_set_string (value, signal->priv->detail);
break;
case PROP_HANDLER:
g_value_set_string (value, signal->priv->handler);
break;
case PROP_USERDATA:
g_value_set_string (value, signal->priv->userdata);
break;
case PROP_SUPPORT_WARNING:
g_value_set_string (value, signal->priv->support_warning);
break;
case PROP_AFTER:
g_value_set_boolean (value, signal->priv->after);
break;
case PROP_SWAPPED:
g_value_set_boolean (value, signal->priv->swapped);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
glade_signal_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GladeSignal *signal = GLADE_SIGNAL (object);
switch (prop_id)
{
case PROP_CLASS:
signal->priv->class = g_value_get_pointer (value);
break;
case PROP_DETAIL:
glade_signal_set_detail (signal, g_value_get_string (value));
break;
case PROP_HANDLER:
glade_signal_set_handler (signal, g_value_get_string (value));
break;
case PROP_USERDATA:
glade_signal_set_userdata (signal, g_value_get_string (value));
break;
case PROP_SUPPORT_WARNING:
glade_signal_set_support_warning (signal, g_value_get_string (value));
break;
case PROP_AFTER:
glade_signal_set_after (signal, g_value_get_boolean (value));
break;
case PROP_SWAPPED:
glade_signal_set_swapped (signal, g_value_get_boolean (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
glade_signal_init (GladeSignal *signal)
{
signal->priv = glade_signal_get_instance_private (signal);
}
static void
glade_signal_klass_init (GladeSignalKlass *klass)
{
GObjectClass *object_class;
object_class = G_OBJECT_CLASS (klass);
glade_signal_parent_class = g_type_class_peek_parent (klass);
object_class->set_property = glade_signal_set_property;
object_class->get_property = glade_signal_get_property;
object_class->finalize = glade_signal_finalize;
/* Properties */
properties[PROP_CLASS] =
g_param_spec_pointer ("class",
_("SignalClass"),
_("The signal class of this signal"),
G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
properties[PROP_DETAIL] =
g_param_spec_string ("detail",
_("Detail"),
_("The detail for this signal"),
NULL, G_PARAM_READWRITE);
properties[PROP_HANDLER] =
g_param_spec_string ("handler",
_("Handler"),
_("The handler for this signal"),
NULL, G_PARAM_READWRITE);
properties[PROP_USERDATA] =
g_param_spec_string ("userdata",
_("User Data"),
_("The user data for this signal"),
NULL, G_PARAM_READWRITE);
properties[PROP_SUPPORT_WARNING] =
g_param_spec_string ("support-warning",
_("Support Warning"),
_("The versioning support warning for this signal"),
NULL, G_PARAM_READWRITE);
properties[PROP_AFTER] =
g_param_spec_boolean ("after",
_("After"),
_("Whether this signal is run after default handlers"),
FALSE, G_PARAM_READWRITE);
properties[PROP_SWAPPED] =
g_param_spec_boolean ("swapped",
_("Swapped"),
_("Whether the user data is swapped with the instance for the handler"),
FALSE, G_PARAM_READWRITE);
/* Install all properties */
g_object_class_install_properties (object_class, N_PROPERTIES, properties);
}
/**
* glade_signal_new:
* @sig_class: a #GladeSignalClass
* @handler: a handler function for the signal
* @userdata: the userdata for this signal
* @after: whether this handler should be called after the default emission phase
* @swapped: whether the handler's user data should be swapped with the emitter instance.
*
* Creates a new #GladeSignal with the given parameters.
*
* Returns: the new #GladeSignal
*/
GladeSignal *
glade_signal_new (const GladeSignalClass *sig_class,
const gchar *handler,
const gchar *userdata,
gboolean after,
gboolean swapped)
{
g_return_val_if_fail (sig_class != NULL, NULL);
return GLADE_SIGNAL (g_object_new (GLADE_TYPE_SIGNAL,
"class", sig_class,
"handler", handler,
"userdata", userdata,
"after", after,
"swapped", swapped,
NULL));
}
/**
* glade_signal_equal:
* @sig1: a #GladeSignal
* @sig2: a #GladeSignal
*
* Returns: %TRUE if @sig1 and @sig2 have identical attributes, %FALSE otherwise
*/
gboolean
glade_signal_equal (const GladeSignal *sig1, const GladeSignal *sig2)
{
gboolean ret = FALSE;
g_return_val_if_fail (GLADE_IS_SIGNAL (sig1), FALSE);
g_return_val_if_fail (GLADE_IS_SIGNAL (sig2), FALSE);
/* Intentionally ignore support_warning */
if (!g_strcmp0 (glade_signal_get_name (sig1), glade_signal_get_name (sig2)) &&
!g_strcmp0 (sig1->priv->handler, sig2->priv->handler) &&
!g_strcmp0 (sig1->priv->detail, sig2->priv->detail) &&
sig1->priv->after == sig2->priv->after && sig1->priv->swapped == sig2->priv->swapped)
{
if ((sig1->priv->userdata == NULL && sig2->priv->userdata == NULL) ||
(sig1->priv->userdata != NULL && sig2->priv->userdata != NULL &&
!g_strcmp0 (sig1->priv->userdata, sig2->priv->userdata)))
ret = TRUE;
}
return ret;
}
/**
* glade_signal_clone:
* @signal: a #GladeSignal
*
* Returns: a new #GladeSignal with the same attributes as @signal
*/
GladeSignal *
glade_signal_clone (const GladeSignal *signal)
{
GladeSignal *dup;
g_return_val_if_fail (GLADE_IS_SIGNAL (signal), NULL);
dup = glade_signal_new (signal->priv->class,
signal->priv->handler,
signal->priv->userdata,
signal->priv->after,
signal->priv->swapped);
glade_signal_set_detail (dup, signal->priv->detail);
glade_signal_set_support_warning (dup, signal->priv->support_warning);
return dup;
}
/**
* glade_signal_write:
* @signal: The #GladeSignal
* @context: A #GladeXmlContext
* @node: A #GladeXmlNode
*
* Writes @signal to @node
*/
void
glade_signal_write (GladeSignal *signal,
GladeXmlContext *context,
GladeXmlNode *node)
{
GladeXmlNode *signal_node;
gchar *name;
/* Should assert GLADE_XML_TAG_WIDGET tag here, but no
* access to project, so not really seriosly needed
*/
if (signal->priv->detail)
name = g_strdup_printf ("%s::%s",
glade_signal_get_name (signal),
signal->priv->detail);
else
name = g_strdup (glade_signal_get_name (signal));
/* Now dump the node values... */
signal_node = glade_xml_node_new (context, GLADE_XML_TAG_SIGNAL);
glade_xml_node_append_child (node, signal_node);
glade_xml_node_set_property_string (signal_node, GLADE_XML_TAG_NAME, name);
glade_xml_node_set_property_string (signal_node, GLADE_XML_TAG_HANDLER,
signal->priv->handler);
if (signal->priv->userdata)
glade_xml_node_set_property_string (signal_node,
GLADE_XML_TAG_OBJECT, signal->priv->userdata);
if (signal->priv->after)
glade_xml_node_set_property_string (signal_node,
GLADE_XML_TAG_AFTER,
GLADE_XML_TAG_SIGNAL_TRUE);
/* Always serialize swapped regardless of format (libglade should not complain about this
* and we prefer to not lose data in conversions).
*/
glade_xml_node_set_property_string (signal_node,
GLADE_XML_TAG_SWAPPED,
signal->priv->swapped ?
GLADE_XML_TAG_SIGNAL_TRUE :
GLADE_XML_TAG_SIGNAL_FALSE);
g_free (name);
}
/**
* glade_signal_read:
* @node: The #GladeXmlNode to read
* @adaptor: The #GladeWidgetAdaptor for thw widget
*
* Reads and creates a ner #GladeSignal based on @node
*
* Returns: A newly created #GladeSignal
*/
GladeSignal *
glade_signal_read (GladeXmlNode *node, GladeWidgetAdaptor *adaptor)
{
GladeSignal *signal = NULL;
GladeSignalClass *signal_class;
gchar *name, *handler, *userdata, *detail;
g_return_val_if_fail (glade_xml_node_verify_silent
(node, GLADE_XML_TAG_SIGNAL), NULL);
if (!(name =
glade_xml_get_property_string_required (node, GLADE_XML_TAG_NAME,
NULL)))
return NULL;
glade_util_replace (name, '_', '-');
/* Search for a detail, and strip it from the signal name */
if ((detail = g_strstr_len (name, -1, "::"))) *detail = '\0';
if (!(handler =
glade_xml_get_property_string_required (node, GLADE_XML_TAG_HANDLER,
NULL)))
{
g_free (name);
return NULL;
}
userdata = glade_xml_get_property_string (node, GLADE_XML_TAG_OBJECT);
signal_class = glade_widget_adaptor_get_signal_class (adaptor, name);
if (signal_class)
{
signal = glade_signal_new (signal_class,
handler, userdata,
glade_xml_get_property_boolean (node, GLADE_XML_TAG_AFTER, FALSE),
glade_xml_get_property_boolean (node, GLADE_XML_TAG_SWAPPED,
userdata != NULL));
if (detail && detail[2]) glade_signal_set_detail (signal, &detail[2]);
}
else
{
/* XXX These errors should be collected and reported to the user */
g_warning ("No signal %s was found for class %s, skipping\n",
name, glade_widget_adaptor_get_name (adaptor));
}
g_free (name);
g_free (handler);
g_free (userdata);
return signal;
}
G_CONST_RETURN gchar *
glade_signal_get_name (const GladeSignal *signal)
{
g_return_val_if_fail (GLADE_IS_SIGNAL (signal), NULL);
return glade_signal_class_get_name (signal->priv->class);
}
G_CONST_RETURN GladeSignalClass *
glade_signal_get_class (const GladeSignal *signal)
{
return signal->priv->class;
}
void
glade_signal_set_detail (GladeSignal *signal, const gchar *detail)
{
g_return_if_fail (GLADE_IS_SIGNAL (signal));
if (glade_signal_class_get_flags (signal->priv->class) & G_SIGNAL_DETAILED &&
g_strcmp0 (signal->priv->detail, detail))
{
g_free (signal->priv->detail);
signal->priv->detail = (detail && g_utf8_strlen (detail, -1)) ? g_strdup (detail) : NULL;
g_object_notify_by_pspec (G_OBJECT (signal), properties[PROP_DETAIL]);
}
}
G_CONST_RETURN gchar *
glade_signal_get_detail (const GladeSignal *signal)
{
g_return_val_if_fail (GLADE_IS_SIGNAL (signal), NULL);
return signal->priv->detail;
}
void
glade_signal_set_handler (GladeSignal *signal, const gchar *handler)
{
g_return_if_fail (GLADE_IS_SIGNAL (signal));
if (g_strcmp0 (signal->priv->handler, handler))
{
g_free (signal->priv->handler);
signal->priv->handler =
handler ? g_strdup (handler) : NULL;
g_object_notify_by_pspec (G_OBJECT (signal), properties[PROP_HANDLER]);
}
}
G_CONST_RETURN gchar *
glade_signal_get_handler (const GladeSignal *signal)
{
g_return_val_if_fail (GLADE_IS_SIGNAL (signal), NULL);
return signal->priv->handler;
}
void
glade_signal_set_userdata (GladeSignal *signal, const gchar *userdata)
{
g_return_if_fail (GLADE_IS_SIGNAL (signal));
if (g_strcmp0 (signal->priv->userdata, userdata))
{
g_free (signal->priv->userdata);
signal->priv->userdata =
userdata ? g_strdup (userdata) : NULL;
g_object_notify_by_pspec (G_OBJECT (signal), properties[PROP_USERDATA]);
}
}
G_CONST_RETURN gchar *
glade_signal_get_userdata (const GladeSignal *signal)
{
g_return_val_if_fail (GLADE_IS_SIGNAL (signal), NULL);
return signal->priv->userdata;
}
void
glade_signal_set_after (GladeSignal *signal, gboolean after)
{
g_return_if_fail (GLADE_IS_SIGNAL (signal));
if (signal->priv->after != after)
{
signal->priv->after = after;
g_object_notify_by_pspec (G_OBJECT (signal), properties[PROP_AFTER]);
}
}
gboolean
glade_signal_get_after (const GladeSignal *signal)
{
g_return_val_if_fail (GLADE_IS_SIGNAL (signal), FALSE);
return signal->priv->after;
}
void
glade_signal_set_swapped (GladeSignal *signal, gboolean swapped)
{
g_return_if_fail (GLADE_IS_SIGNAL (signal));
if (signal->priv->swapped != swapped)
{
signal->priv->swapped = swapped;
g_object_notify_by_pspec (G_OBJECT (signal), properties[PROP_SWAPPED]);
}
}
gboolean
glade_signal_get_swapped (const GladeSignal *signal)
{
g_return_val_if_fail (GLADE_IS_SIGNAL (signal), FALSE);
return signal->priv->swapped;
}
void
glade_signal_set_support_warning (GladeSignal *signal,
const gchar *support_warning)
{
g_return_if_fail (GLADE_IS_SIGNAL (signal));
if (g_strcmp0 (signal->priv->support_warning, support_warning))
{
g_free (signal->priv->support_warning);
signal->priv->support_warning =
support_warning ? g_strdup (support_warning) : NULL;
g_object_notify_by_pspec (G_OBJECT (signal), properties[PROP_SUPPORT_WARNING]);
}
}
G_CONST_RETURN gchar *
glade_signal_get_support_warning (const GladeSignal *signal)
{
g_return_val_if_fail (GLADE_IS_SIGNAL (signal), NULL);
return signal->priv->support_warning;
}
|