glade-widget-class.c 10.5 KB
Newer Older
Jose Maria Celorio's avatar
Jose Maria Celorio committed
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
/* -*- 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
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 *
 * Authors:
 *   Chema Celorio <chema@celorio.com>
 */

/* For g_file_exists */
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
27
#include <string.h>
Jose Maria Celorio's avatar
Jose Maria Celorio committed
28 29 30 31 32 33 34

#include <config.h>

#include "glade.h"
#include "glade-xml-utils.h"

#include <dirent.h>
Jose Maria Celorio's avatar
Jose Maria Celorio committed
35 36
#include <gmodule.h>
#include <ctype.h>
Jose Maria Celorio's avatar
Jose Maria Celorio committed
37 38 39 40 41 42 43

#include <gtk/gtkenums.h> /* This should go away. Chema */
#include <gdk-pixbuf/gdk-pixbuf.h>

#include "glade-placeholder.h"
#include "glade-property.h"
#include "glade-property-class.h"
Chema Celorio's avatar
Chema Celorio committed
44 45
#include "glade-project-window.h"
#include "glade-catalog.h"
Jose Maria Celorio's avatar
Jose Maria Celorio committed
46 47 48 49
#include "glade-choice.h"
#include "glade-parameter.h"
#include "glade-widget-class.h"

50

Jose Maria Celorio's avatar
Jose Maria Celorio committed
51 52 53 54 55
static gchar *
glade_widget_class_compose_get_type_func (GladeWidgetClass *class)
{
	gchar *retval;
	GString *tmp;
56
	gint i = 1, j;
Jose Maria Celorio's avatar
Jose Maria Celorio committed
57 58 59 60 61

	tmp = g_string_new (class->name);

	while (tmp->str[i]) {
		if (isupper (tmp->str[i])) {
62 63 64 65 66 67 68 69 70 71
			tmp = g_string_insert_c (tmp, i++, '_');

			j = 0;
			while (isupper (tmp->str[i++]))
				j++;

			if (j > 2) {
				g_string_insert_c (tmp, i-2, '_');
			}

Jose Maria Celorio's avatar
Jose Maria Celorio committed
72 73 74 75 76 77 78 79 80 81 82
			continue;
		}
		i++;
	}

	retval = g_strconcat (g_strdup (tmp->str), "_get_type", NULL);
	g_strdown (retval);
	g_string_free (tmp, TRUE);

	return retval;
}
83

Jose Maria Celorio's avatar
Jose Maria Celorio committed
84 85 86 87 88 89 90 91 92

static GladeWidgetClass *
glade_widget_class_new (void)
{
	GladeWidgetClass *widget;

	widget = g_new0 (GladeWidgetClass, 1);
	widget->flags = 0;
	widget->placeholder_replace = NULL;
Jose Maria Celorio's avatar
Jose Maria Celorio committed
93
	widget->type = 0;
Jose Maria Celorio's avatar
Jose Maria Celorio committed
94 95 96 97 98 99 100 101 102

	return widget;
}

static void
glade_widget_class_add_virtual_methods (GladeWidgetClass *class)
{
	g_return_if_fail (class->name != NULL);

103 104 105 106 107 108 109
	if (GLADE_WIDGET_CLASS_ADD_PLACEHOLDER(class)) {
		/* I don't love this. Make it better. Chema */
		if ((strcmp (class->name, "GtkVBox") == 0) ||
		    (strcmp (class->name, "GtkHBox") == 0))
			class->placeholder_replace = glade_placeholder_replace_box;
		if (strcmp (class->name, "GtkTable") == 0)
			class->placeholder_replace = glade_placeholder_replace_table;
Chema Celorio's avatar
Chema Celorio committed
110 111 112 113 114
		if (strcmp (class->name, "GtkNotebook") == 0)
			class->placeholder_replace = glade_placeholder_replace_notebook;
		if ((strcmp (class->name, "GtkWindow")    == 0) ||
		    (strcmp (class->name, "GtkFrame")     == 0) ||
		    (strcmp (class->name, "GtkHandleBox") == 0 ))
115 116 117 118 119 120
			class->placeholder_replace = glade_placeholder_replace_container;
		
		if (class->placeholder_replace == NULL)
			g_warning ("placeholder_replace has not been implemented for %s\n",
				   class->name);
	}
Jose Maria Celorio's avatar
Jose Maria Celorio committed
121 122
}

123 124 125 126 127 128 129 130 131 132
GList * 
glade_widget_class_list_signals (GladeWidgetClass *class) 
{
	GList *signals;
	GType type;
	guint count;
	guint *sig_ids;
	guint num_signals;
	GladeWidgetClassSignal *cur;

133
	g_return_val_if_fail (class->type != 0, NULL);
134

135 136
	signals = NULL;
	type = class->type;
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
	while (g_type_is_a (type, GTK_TYPE_OBJECT)) { 
		if (G_TYPE_IS_INSTANTIATABLE (type) || G_TYPE_IS_INTERFACE (type)) {
			sig_ids = g_signal_list_ids (type, &num_signals);

			for (count = 0; count < num_signals; count++) {
				cur = g_new0 (GladeWidgetClassSignal, 1);
				cur->name = (gchar *) g_signal_name (sig_ids[count]);
				cur->type = (gchar *) g_type_name (type);

				signals = g_list_append (signals, (GladeWidgetClassSignal *) cur);
			}
		}

		type = g_type_parent (type);
	}

	return signals;
}

Jose Maria Celorio's avatar
Jose Maria Celorio committed
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
static gboolean
glade_widget_class_set_type (GladeWidgetClass *class, const gchar *init_function_name)
{
	static GModule *allsymbols;
	guint (*get_type) ();
	GType type;

	class->type = 0;

	g_return_val_if_fail (GLADE_IS_WIDGET_CLASS (class), FALSE);
	g_return_val_if_fail (init_function_name != NULL, FALSE);
	
	if (!allsymbols)
		allsymbols = g_module_open (NULL, 0);
	
	if (!g_module_symbol (allsymbols, init_function_name,
			      (gpointer) &get_type)) {
		g_warning (_("We could not find the symbol \"%s\" while trying to load \"%s\""),
			   init_function_name, class->name);
		return FALSE;
	}

	g_assert (get_type);
	type = get_type ();

	if (type == 0) {
		g_warning(_("Could not get the type from \"%s\" while trying to load \"%s\""), class->name);
		return FALSE;
	}

186 187
/* Disabled for GtkAdjustment, but i'd like to check for this somehow */
#if 0	
Jose Maria Celorio's avatar
Jose Maria Celorio committed
188 189 190 191 192
	if (!g_type_is_a (type, gtk_widget_get_type ())) {
		g_warning (_("The loaded type is not a GtkWidget, while trying to load \"%s\""),
			   class->name);
		return FALSE;
	}
193
#endif
Jose Maria Celorio's avatar
Jose Maria Celorio committed
194 195 196 197 198 199

	class->type = type;
	
	return TRUE;
}
		
200
GladeWidgetClass *
Chema Celorio's avatar
Chema Celorio committed
201
glade_widget_class_new_from_node (GladeXmlNode *node)
Jose Maria Celorio's avatar
Jose Maria Celorio committed
202 203
{
	GladeWidgetClass *class;
Chema Celorio's avatar
Chema Celorio committed
204
	GladeXmlNode *child;
Jose Maria Celorio's avatar
Jose Maria Celorio committed
205 206
	gchar *init_function_name;

Jose Maria Celorio's avatar
Jose Maria Celorio committed
207 208
	if (!glade_xml_node_verify (node, GLADE_TAG_GLADE_WIDGET_CLASS))
		return NULL;
209

Jose Maria Celorio's avatar
Jose Maria Celorio committed
210 211 212 213 214 215 216 217
	child = glade_xml_search_child_required (node, GLADE_TAG_PROPERTIES);
	if (child == NULL)
		return FALSE;

	class = glade_widget_class_new ();

	class->name         = glade_xml_get_value_string_required (node, GLADE_TAG_NAME, NULL);
	class->generic_name = glade_xml_get_value_string_required (node, GLADE_TAG_GENERIC_NAME, NULL);
218
	init_function_name  = glade_xml_get_value_string (node, GLADE_TAG_GET_TYPE_FUNCTION);
219 220 221 222 223 224

	if (!init_function_name) {
		init_function_name = glade_widget_class_compose_get_type_func (class);
		if (!init_function_name)
			return FALSE;
	}
Jose Maria Celorio's avatar
Jose Maria Celorio committed
225 226 227 228 229

	if (!glade_widget_class_set_type (class, init_function_name))
		return NULL;
	g_free (init_function_name);

230
	class->properties   = glade_property_class_list_new_from_node (child, class);
231
	class->signals      = glade_widget_class_list_signals (class);
Jose Maria Celorio's avatar
Jose Maria Celorio committed
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

	if (!class->name ||
	    !class->generic_name) {
		g_warning ("Invalid XML file. Widget Class %s\n", class->name);
		return NULL;
	}
	
	/* Get the flags */
	if (glade_xml_get_boolean (node, GLADE_TAG_TOPLEVEL))
		GLADE_WIDGET_CLASS_SET_FLAGS (class, GLADE_TOPLEVEL);
	else
		GLADE_WIDGET_CLASS_UNSET_FLAGS (class, GLADE_TOPLEVEL);
	if (glade_xml_get_boolean (node, GLADE_TAG_PLACEHOLDER))
		GLADE_WIDGET_CLASS_SET_FLAGS (class, GLADE_ADD_PLACEHOLDER);
	else
		GLADE_WIDGET_CLASS_UNSET_FLAGS (class, GLADE_ADD_PLACEHOLDER);

	glade_widget_class_add_virtual_methods (class);
	
	return class;
}

static gboolean
glade_widget_class_create_pixmap (GladeWidgetClass *class)
{
	struct stat s;
	GtkWidget *widget;
	gchar *full_path;
Jose Maria Celorio's avatar
Jose Maria Celorio committed
260 261

	g_return_val_if_fail (GLADE_IS_WIDGET_CLASS (class), FALSE);
Jose Maria Celorio's avatar
Jose Maria Celorio committed
262 263 264
	
	widget = gtk_button_new ();

265
	full_path = g_strdup_printf (PIXMAPS_DIR "/%s.xpm", class->generic_name);
Jose Maria Celorio's avatar
Jose Maria Celorio committed
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
	if (stat (full_path, &s) != 0) {
		g_warning ("Could not create a the \"%s\" GladeWidgetClass because \"%s\" does not exist",
			   class->name, full_path);
		return FALSE;
	}

	class->pixbuf = gdk_pixbuf_new_from_file (full_path, NULL);

	class->pixmap = gdk_pixmap_colormap_create_from_xpm (NULL, gtk_widget_get_colormap (GTK_WIDGET (widget)),
							     &class->mask, NULL, full_path);

	gtk_widget_unref (widget);
	g_free (full_path);
	
	return TRUE;
}

GladeWidgetClass *
glade_widget_class_new_from_name (const gchar *name)
{
Chema Celorio's avatar
Chema Celorio committed
286
	GladeXmlContext *context;
Jose Maria Celorio's avatar
Jose Maria Celorio committed
287
	GladeWidgetClass *class;
Jose Maria Celorio's avatar
Jose Maria Celorio committed
288 289 290 291
	gchar *file_name;

	file_name = g_strconcat (WIDGETS_DIR, "/", name, ".xml", NULL);
	
Chema Celorio's avatar
Chema Celorio committed
292
	context = glade_xml_context_new_from_path (file_name, NULL, GLADE_TAG_GLADE_WIDGET_CLASS);
Jose Maria Celorio's avatar
Jose Maria Celorio committed
293 294
	if (context == NULL)
		return NULL;
Chema Celorio's avatar
Chema Celorio committed
295 296
	class = glade_widget_class_new_from_node (glade_xml_context_get_root (context));
	glade_xml_context_free (context);
Jose Maria Celorio's avatar
Jose Maria Celorio committed
297

Jose Maria Celorio's avatar
Jose Maria Celorio committed
298
	if (!glade_widget_class_create_pixmap (class))
Jose Maria Celorio's avatar
Jose Maria Celorio committed
299 300 301 302
		return NULL;

	g_free (file_name);
	
Jose Maria Celorio's avatar
Jose Maria Celorio committed
303
	return class;
Jose Maria Celorio's avatar
Jose Maria Celorio committed
304 305 306 307 308 309 310 311
}

const gchar *
glade_widget_class_get_name (GladeWidgetClass *widget)
{
	return widget->name;
}

Jose Maria Celorio's avatar
Jose Maria Celorio committed
312 313 314 315 316 317
GType
glade_widget_class_get_type (GladeWidgetClass *widget)
{
	return widget->type;
}

Jose Maria Celorio's avatar
Jose Maria Celorio committed
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
/**
 * glade_widget_class_has_queries:
 * @class: 
 * 
 * A way to know if this class has a property that requires us to query the
 * user before creating the widget.
 * 
 * Return Value: TRUE if the GladeWidgetClass requires a query to the user
 *               before creationg.
 **/
gboolean
glade_widget_class_has_queries (GladeWidgetClass *class)
{
	GladePropertyClass *property_class;
	GList *list;

	list = class->properties;
	for (; list != NULL; list = list->next) {
		property_class = list->data;
		if (property_class->query != NULL)
			return TRUE;
	}

	return FALSE;
}
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


/* ParamSpec stuff */
static void
glade_widget_class_get_specs (GladeWidgetClass *class, GParamSpec ***specs, gint *n_specs)
{
	GObjectClass *object_class;
	GType type;

	type = glade_widget_class_get_type (class);
	g_type_class_ref (type); /* hmm */
	 /* We count on the fact we have an instance, or else we'd have
	  * touse g_type_class_ref ();
	  */
	object_class = g_type_class_peek (type);
	if (object_class == NULL) {
		g_warning ("Class peek failed\n");
		*specs = NULL;
		*n_specs = 0;
		return;
	}

	*specs = g_object_class_list_properties (object_class, n_specs);
}

GParamSpec *
glade_widget_class_find_spec (GladeWidgetClass *class, const gchar *name)
{
	GParamSpec **specs = NULL;
	GParamSpec *spec;
	gint n_specs = 0;
	gint i;

	glade_widget_class_get_specs (class, &specs, &n_specs);

	for (i = 0; i < n_specs; i++) {
		spec = specs[i];

		if (!spec || !spec->name) {
			g_warning ("Spec does not have a valid name, or invalid spec");
			return NULL;
		}

		if (strcmp (spec->name, name) == 0)
			return spec;
	}

	return NULL;
}
			
void
glade_widget_class_dump_param_specs (GladeWidgetClass *class)
{
	GParamSpec **specs = NULL;
	GParamSpec *spec;
	gint n_specs = 0;
	gint i;

	glade_widget_class_get_specs (class, &specs, &n_specs);

	g_print ("Dumping ParamSpec for %s\n", class->name);
	
	for (i = 0; i < n_specs; i++) {
		spec = specs[i];
		g_print ("%02d - %s\n", i, spec->name); 
	}
}
Chema Celorio's avatar
Chema Celorio committed
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


GladeWidgetClass *
glade_widget_class_get_by_name (const gchar *name)
{
	GladeProjectWindow *gpw;
	GladeWidgetClass *class;
	GList *list;
	
	g_return_val_if_fail (name != NULL, NULL);

	gpw = glade_project_window_get ();
	list = gpw->catalog->widgets;
	for (; list != NULL; list = list->next) {
		class = list->data;
		g_return_val_if_fail (class->name != NULL, NULL);
		if (class->name == NULL)
			return NULL;
		if (strcmp (class->name, name) == 0)
			return class;
	}

	g_warning ("Class not found by name %s\n", name);
	
	return NULL;
}