glade-signal.c 4.21 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * 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
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 19 20
 *
 * Authors:
 *   Chema Celorio <chema@celorio.com>
21
 *   Paolo Borelli <pborelli@katamail.com>
22 23
 */

Christian Persch's avatar
Christian Persch committed
24 25
#include "config.h"

26 27
#include <string.h>

Christian Persch's avatar
Christian Persch committed
28 29
#include <glib/gi18n-lib.h>

30 31
#include "glade.h"
#include "glade-signal.h"
32 33
#include "glade-xml-utils.h"

34 35 36 37 38 39 40 41 42 43 44
/**
 * glade_signal_new:
 * @name: a name for the signal
 * @handler: a handler function for the signal
 * @after: #gboolean indicating whether this handler should be called after
 *         the default handler
 *
 * Creates a new #GladeSignal with the given parameters.
 *
 * Returns: the new #GladeSignal
 */
45 46 47 48 49
GladeSignal *glade_signal_new (const gchar *name,
			       const gchar *handler,
			       const gchar *userdata,
			       gboolean     lookup,
			       gboolean     after)
50 51 52
{
	GladeSignal *signal = g_new0 (GladeSignal, 1);

53 54 55 56 57
	signal->name     = g_strdup (name);
	signal->handler  = g_strdup (handler);
	signal->userdata = userdata ? g_strdup (userdata) : NULL;
	signal->lookup   = lookup;
	signal->after    = after;
58 59 60 61

	return signal;
}

62 63 64 65 66 67
/**
 * glade_signal_free:
 * @signal: a #GladeSignal
 *
 * Frees @signal and its associated memory.
 */
68 69 70
void
glade_signal_free (GladeSignal *signal)
{
71 72
	g_return_if_fail (GLADE_IS_SIGNAL (signal));

73 74
	g_free (signal->name);
	g_free (signal->handler);
75
	if (signal->userdata) g_free (signal->userdata);
76 77
	g_free (signal);
}
78

79 80 81 82 83 84 85
/**
 * glade_signal_equal:
 * @sig1: a #GladeSignal
 * @sig2: a #GladeSignal
 *
 * Returns: %TRUE if @sig1 and @sig2 have identical attributes, %FALSE otherwise
 */
86
gboolean
87
glade_signal_equal (GladeSignal *sig1, GladeSignal *sig2)
88 89 90 91
{
	gboolean ret = FALSE;
	g_return_val_if_fail (GLADE_IS_SIGNAL (sig1), FALSE);
	g_return_val_if_fail (GLADE_IS_SIGNAL (sig2), FALSE);
92 93 94 95 96 97 98 99 100
	
	if (!strcmp (sig1->name, sig2->name)        &&
	    !strcmp (sig1->handler, sig2->handler)  &&
	    sig1->after  == sig2->after             &&
	    sig1->lookup == sig2->lookup)
	{
		if ((sig1->userdata == NULL && sig2->userdata == NULL) ||
		    (sig1->userdata != NULL && sig2->userdata != NULL  &&
		     !strcmp (sig1->userdata, sig2->userdata)))
101
			ret = TRUE;
102
	}
103

104 105 106
	return ret;
}

107 108 109 110 111 112
/**
 * glade_signal_clone:
 * @signal: a #GladeSignal
 *
 * Returns: a new #GladeSignal with the same attributes as @signal
 */
113
GladeSignal *
114
glade_signal_clone (const GladeSignal *signal)
115
{
116 117 118 119 120
	return glade_signal_new (signal->name,
				 signal->handler,
				 signal->userdata,
				 signal->lookup,
				 signal->after);
121 122
}

123 124
/**
 * glade_signal_write:
125 126 127
 * @info:
 * @signal:
 * @interface:
128
 *
129
 * Returns: TRUE if succeed
130
 */
131 132 133
gboolean
glade_signal_write (GladeSignalInfo *info, GladeSignal *signal,
		     GladeInterface *interface)
134
{
135
	info->name    = glade_xml_alloc_string(interface, signal->name);
136
	glade_util_replace (info->name, '-', '_');
137
	info->handler = glade_xml_alloc_string(interface, signal->handler);
138 139
	info->object  =
		signal->userdata ?
140
		glade_xml_alloc_string(interface, signal->userdata) : NULL;
141 142 143
	info->after   = signal->after;
	info->lookup  = signal->lookup;
	return TRUE;
144
}
145

146 147
/*
 * Returns a new GladeSignal with the attributes defined in node
148
 */
149
GladeSignal *glade_signal_new_from_signal_info (GladeSignalInfo *info)
150 151 152
{
	GladeSignal *signal;

153
	g_return_val_if_fail (info != NULL, NULL);
154 155

	signal = g_new0 (GladeSignal, 1);
156
	signal->name     = g_strdup (info->name);
157
	glade_util_replace (signal->name, '_', '-');
158 159 160
	signal->handler  = g_strdup (info->handler);
	signal->after    = info->after;
	signal->userdata = g_strdup (info->object);
161

162
	if (!signal->name)
163 164 165 166
		return NULL;

	return signal;
}