glade-gtk.c 330 KB
Newer Older
1
2
3
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2001 Ximian, Inc.
4
 * Copyright (C) 2004 - 2008 Tristan Van Berkom, Juan Pablo Ugarte et al.
5
6
7
8
9
10
11
12
13
14
15
16
17
 *
 * 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
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
20
21
 *
 * Authors:
 *   Chema Celorio <chema@celorio.com>
22
23
 *   Tristan Van Berkom <tvb@gnome.org>
 *   Juan Pablo Ugarte <juanpablougarte@gmail.com>
24
25
 */

26
#include <config.h>
27

28
#include "glade-gtk.h"
29
#include "fixed-bg.xpm"
30
#include "glade-accels.h"
31
#include "glade-attributes.h"
32
#include "glade-column-types.h"
33
#include "glade-model-data.h"
34
#include "glade-icon-sources.h"
35
#include "glade-button-editor.h"
36
#include "glade-tool-button-editor.h"
37
#include "glade-image-editor.h"
38
#include "glade-image-item-editor.h"
39
#include "glade-icon-factory-editor.h"
40
#include "glade-store-editor.h"
41
#include "glade-label-editor.h"
42
#include "glade-cell-renderer-editor.h"
43
#include "glade-treeview-editor.h"
44
#include "glade-entry-editor.h"
45
#include "glade-activatable-editor.h"
46

47
48
#include <gladeui/glade-editor-property.h>
#include <gladeui/glade-base-editor.h>
49
#include <gladeui/glade-xml-utils.h>
50

51

52
#include <gtk/gtk.h>
53
#include <glib/gi18n-lib.h>
54
55
#include <string.h>
#include <stdlib.h>
56

57
/* --------------------------------- Constants ------------------------------ */
58
59
60
#define FIXED_DEFAULT_CHILD_WIDTH  100
#define FIXED_DEFAULT_CHILD_HEIGHT 60

61
62
#define MNEMONIC_INSENSITIVE_MSG   _("This property does not apply unless Use Underline is set.")
#define NOT_SELECTED_MSG           _("Property not selected")
63
#define RESPID_INSENSITIVE_MSG     _("This property is only for use in dialog action buttons")
64
#define ACTION_APPEARANCE_MSG      _("This property is set to be controled by an Action")
65
/* -------------------------------- ParamSpecs ------------------------------ */
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
/*
GtkImageMenuItem GnomeUI "stock_item" property special case:
	
"stock_item" property is added by glade2 gnome support and makes reference to
GNOMEUIINFO_MENU_* macros. This set-function maps these properties to 
existing non deprecated gtk ones.
*/
typedef enum {
	GNOMEUIINFO_MENU_NONE,
	/* The 'File' menu */
	GNOMEUIINFO_MENU_NEW_ITEM,
	GNOMEUIINFO_MENU_NEW_SUBTREE,
	GNOMEUIINFO_MENU_OPEN_ITEM,
	GNOMEUIINFO_MENU_SAVE_ITEM,
	GNOMEUIINFO_MENU_SAVE_AS_ITEM,
	GNOMEUIINFO_MENU_REVERT_ITEM,
	GNOMEUIINFO_MENU_PRINT_ITEM,
	GNOMEUIINFO_MENU_PRINT_SETUP_ITEM,
	GNOMEUIINFO_MENU_CLOSE_ITEM,
	GNOMEUIINFO_MENU_EXIT_ITEM,
	GNOMEUIINFO_MENU_QUIT_ITEM,
	/* The "Edit" menu */
	GNOMEUIINFO_MENU_CUT_ITEM,
	GNOMEUIINFO_MENU_COPY_ITEM,
	GNOMEUIINFO_MENU_PASTE_ITEM,
	GNOMEUIINFO_MENU_SELECT_ALL_ITEM,
	GNOMEUIINFO_MENU_CLEAR_ITEM,
	GNOMEUIINFO_MENU_UNDO_ITEM,
	GNOMEUIINFO_MENU_REDO_ITEM,
	GNOMEUIINFO_MENU_FIND_ITEM,
	GNOMEUIINFO_MENU_FIND_AGAIN_ITEM,
	GNOMEUIINFO_MENU_REPLACE_ITEM,
	GNOMEUIINFO_MENU_PROPERTIES_ITEM,
	/* The Settings menu */
	GNOMEUIINFO_MENU_PREFERENCES_ITEM,
	/* The Windows menu */
	GNOMEUIINFO_MENU_NEW_WINDOW_ITEM,
	GNOMEUIINFO_MENU_CLOSE_WINDOW_ITEM,
	/* And the "Help" menu */
	GNOMEUIINFO_MENU_ABOUT_ITEM,
	/* The "Game" menu */
	GNOMEUIINFO_MENU_NEW_GAME_ITEM,
	GNOMEUIINFO_MENU_PAUSE_GAME_ITEM,
	GNOMEUIINFO_MENU_RESTART_GAME_ITEM,
	GNOMEUIINFO_MENU_UNDO_MOVE_ITEM,
	GNOMEUIINFO_MENU_REDO_MOVE_ITEM,
	GNOMEUIINFO_MENU_HINT_ITEM,
	GNOMEUIINFO_MENU_SCORES_ITEM,
	GNOMEUIINFO_MENU_END_GAME_ITEM,
	/* Some standard menus */
	GNOMEUIINFO_MENU_FILE_TREE,
	GNOMEUIINFO_MENU_EDIT_TREE,
	GNOMEUIINFO_MENU_VIEW_TREE,
	GNOMEUIINFO_MENU_SETTINGS_TREE,
	GNOMEUIINFO_MENU_FILES_TREE,
	GNOMEUIINFO_MENU_WINDOWS_TREE,
	GNOMEUIINFO_MENU_HELP_TREE,
	GNOMEUIINFO_MENU_GAME_TREE
} GladeGtkGnomeUIInfoEnum;

static GType
glade_gtk_gnome_ui_info_get_type (void)
{
	static GType etype = 0;
	if (etype == 0) {
		static const GEnumValue values[] = {
			{ GNOMEUIINFO_MENU_NONE, "GNOMEUIINFO_MENU_NONE", NULL},
			/* The 'File' menu */
			{ GNOMEUIINFO_MENU_NEW_ITEM, "GNOMEUIINFO_MENU_NEW_ITEM", "gtk-new"},
			{ GNOMEUIINFO_MENU_OPEN_ITEM, "GNOMEUIINFO_MENU_OPEN_ITEM", "gtk-open"},
			{ GNOMEUIINFO_MENU_SAVE_ITEM, "GNOMEUIINFO_MENU_SAVE_ITEM", "gtk-save"},
			{ GNOMEUIINFO_MENU_SAVE_AS_ITEM, "GNOMEUIINFO_MENU_SAVE_AS_ITEM", "gtk-save-as"},
			{ GNOMEUIINFO_MENU_REVERT_ITEM, "GNOMEUIINFO_MENU_REVERT_ITEM", "gtk-revert-to-saved"},
			{ GNOMEUIINFO_MENU_PRINT_ITEM, "GNOMEUIINFO_MENU_PRINT_ITEM", "gtk-print"},
			{ GNOMEUIINFO_MENU_PRINT_SETUP_ITEM, "GNOMEUIINFO_MENU_PRINT_SETUP_ITEM", NULL},
			{ GNOMEUIINFO_MENU_CLOSE_ITEM, "GNOMEUIINFO_MENU_CLOSE_ITEM", "gtk-close"},
			{ GNOMEUIINFO_MENU_EXIT_ITEM, "GNOMEUIINFO_MENU_EXIT_ITEM", "gtk-quit"},
			{ GNOMEUIINFO_MENU_QUIT_ITEM, "GNOMEUIINFO_MENU_QUIT_ITEM", "gtk-quit"},
			/* The "Edit" menu */
			{ GNOMEUIINFO_MENU_CUT_ITEM, "GNOMEUIINFO_MENU_CUT_ITEM", "gtk-cut"},
			{ GNOMEUIINFO_MENU_COPY_ITEM, "GNOMEUIINFO_MENU_COPY_ITEM", "gtk-copy"},
			{ GNOMEUIINFO_MENU_PASTE_ITEM, "GNOMEUIINFO_MENU_PASTE_ITEM", "gtk-paste"},
			{ GNOMEUIINFO_MENU_SELECT_ALL_ITEM, "GNOMEUIINFO_MENU_SELECT_ALL_ITEM", NULL},
			{ GNOMEUIINFO_MENU_CLEAR_ITEM, "GNOMEUIINFO_MENU_CLEAR_ITEM", "gtk-clear"},
			{ GNOMEUIINFO_MENU_UNDO_ITEM, "GNOMEUIINFO_MENU_UNDO_ITEM", "gtk-undo"},
			{ GNOMEUIINFO_MENU_REDO_ITEM, "GNOMEUIINFO_MENU_REDO_ITEM", "gtk-redo"},
			{ GNOMEUIINFO_MENU_FIND_ITEM, "GNOMEUIINFO_MENU_FIND_ITEM", "gtk-find"},
			{ GNOMEUIINFO_MENU_FIND_AGAIN_ITEM, "GNOMEUIINFO_MENU_FIND_AGAIN_ITEM", NULL},
			{ GNOMEUIINFO_MENU_REPLACE_ITEM, "GNOMEUIINFO_MENU_REPLACE_ITEM", "gtk-find-and-replace"},
			{ GNOMEUIINFO_MENU_PROPERTIES_ITEM, "GNOMEUIINFO_MENU_PROPERTIES_ITEM", "gtk-properties"},
			/* The Settings menu */
			{ GNOMEUIINFO_MENU_PREFERENCES_ITEM, "GNOMEUIINFO_MENU_PREFERENCES_ITEM", "gtk-preferences"},
			/* The Windows menu */
			{ GNOMEUIINFO_MENU_NEW_WINDOW_ITEM, "GNOMEUIINFO_MENU_NEW_WINDOW_ITEM", NULL},
			{ GNOMEUIINFO_MENU_CLOSE_WINDOW_ITEM, "GNOMEUIINFO_MENU_CLOSE_WINDOW_ITEM", NULL},
			/* And the "Help" menu */
			{ GNOMEUIINFO_MENU_ABOUT_ITEM, "GNOMEUIINFO_MENU_ABOUT_ITEM", "gtk-about"},
			/* The "Game" menu */
			{ GNOMEUIINFO_MENU_NEW_GAME_ITEM, "GNOMEUIINFO_MENU_NEW_GAME_ITEM", NULL},
			{ GNOMEUIINFO_MENU_PAUSE_GAME_ITEM, "GNOMEUIINFO_MENU_PAUSE_GAME_ITEM", NULL},
			{ GNOMEUIINFO_MENU_RESTART_GAME_ITEM, "GNOMEUIINFO_MENU_RESTART_GAME_ITEM", NULL},
			{ GNOMEUIINFO_MENU_UNDO_MOVE_ITEM, "GNOMEUIINFO_MENU_UNDO_MOVE_ITEM", NULL},
			{ GNOMEUIINFO_MENU_REDO_MOVE_ITEM, "GNOMEUIINFO_MENU_REDO_MOVE_ITEM", NULL},
			{ GNOMEUIINFO_MENU_HINT_ITEM, "GNOMEUIINFO_MENU_HINT_ITEM", NULL},
			{ GNOMEUIINFO_MENU_SCORES_ITEM, "GNOMEUIINFO_MENU_SCORES_ITEM", NULL},
			{ GNOMEUIINFO_MENU_END_GAME_ITEM, "GNOMEUIINFO_MENU_END_GAME_ITEM", NULL},
			/* Some standard menus */
			{ GNOMEUIINFO_MENU_FILE_TREE, "GNOMEUIINFO_MENU_FILE_TREE", NULL},
			{ GNOMEUIINFO_MENU_EDIT_TREE, "GNOMEUIINFO_MENU_EDIT_TREE", NULL},
			{ GNOMEUIINFO_MENU_VIEW_TREE, "GNOMEUIINFO_MENU_VIEW_TREE", NULL},
			{ GNOMEUIINFO_MENU_SETTINGS_TREE, "GNOMEUIINFO_MENU_SETTINGS_TREE", NULL},
			{ GNOMEUIINFO_MENU_FILES_TREE, "GNOMEUIINFO_MENU_FILES_TREE", NULL},
			{ GNOMEUIINFO_MENU_WINDOWS_TREE, "GNOMEUIINFO_MENU_WINDOWS_TREE", NULL},
			{ GNOMEUIINFO_MENU_HELP_TREE, "GNOMEUIINFO_MENU_HELP_TREE", NULL},
			{ GNOMEUIINFO_MENU_GAME_TREE, "GNOMEUIINFO_MENU_GAME_TREE", NULL},
			{ 0, NULL, NULL }
		};
		etype = g_enum_register_static ("GladeGtkGnomeUIInfo", values);
	}
	return etype;
}

188
GParamSpec *
189
190
191
192
193
194
195
196
glade_gtk_gnome_ui_info_spec (void)
{
	return g_param_spec_enum ("gnomeuiinfo", _("GnomeUIInfo"), 
				  _("Choose the GnomeUIInfo stock item"),
				  glade_gtk_gnome_ui_info_get_type (),
				  0, G_PARAM_READWRITE);
}

197
/* Fake GtkImage::icon-size since its an int pspec in the image */
198
GParamSpec *
199
gladegtk_icon_size_spec (void)
200
{
201
202
203
204
205
	return g_param_spec_enum ("icon-size", _("Icon Size"), 
				  _("Symbolic size to use for stock icon, icon set or named icon"),
				  GTK_TYPE_ICON_SIZE,
				  GTK_ICON_SIZE_BUTTON, 
				  G_PARAM_READWRITE);
206
207
}

208
209
210
/* This function does absolutely nothing
 * (and is for use in overriding post_create functions).
 */
211
void
212
empty (GObject *container, GladeCreateReason reason)
213
214
215
{
}

216
217
218
219
220
221
/* This function is used to stop default handlers  */
static void
glade_gtk_stop_emission_POINTER (gpointer instance, gpointer dummy, gpointer data)
{
	g_signal_stop_emission (instance, GPOINTER_TO_UINT (data) , 0);
}
222

223
224
225
226
227
228
229
230
231


/* Initialize needed pspec types from here */
void
glade_gtk_init (const gchar *name)
{

}

232
/* ----------------------------- GtkWidget ------------------------------ */
233
234
235
236
237
gboolean
glade_gtk_widget_depends (GladeWidgetAdaptor *adaptor,
			  GladeWidget        *widget,
			  GladeWidget        *another)
{
238
239
240
	if (GTK_IS_ICON_FACTORY (another->object) ||
	    GTK_IS_ACTION       (another->object) ||
	    GTK_IS_ACTION_GROUP (another->object))
241
242
243
244
245
		return TRUE; 

	return GWA_GET_CLASS (G_TYPE_OBJECT)->depends (adaptor, widget, another);
}

246
247
248
249
250
251
#define GLADE_TAG_A11Y_A11Y         "accessibility"
#define GLADE_TAG_A11Y_ACTION_NAME  "action_name" /* We should make -/_ synonymous */
#define GLADE_TAG_A11Y_DESC         "description"
#define GLADE_TAG_A11Y_TARGET       "target"
#define GLADE_TAG_A11Y_TYPE         "type"

252
253
#define GLADE_TAG_A11Y_INTERNAL_NAME         "accessible"

254
255
256
#define GLADE_TAG_ATTRIBUTES        "attributes"
#define GLADE_TAG_ATTRIBUTE         "attribute"

257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#define GLADE_TAG_A11Y_LIBGLADE_RELATION     "atkrelation"
#define GLADE_TAG_A11Y_LIBGLADE_ACTION       "atkaction"
#define GLADE_TAG_A11Y_LIBGLADE_PROPERTY     "atkproperty"
#define GLADE_TAG_A11Y_GTKBUILDER_RELATION   "relation"
#define GLADE_TAG_A11Y_GTKBUILDER_ACTION     "action"
#define GLADE_TAG_A11Y_GTKBUILDER_PROPERTY   "property"

#define GLADE_TAG_A11Y_PROPERTY(type) \
	((type == GLADE_PROJECT_FORMAT_LIBGLADE) ? \
	 GLADE_TAG_A11Y_LIBGLADE_PROPERTY : GLADE_TAG_A11Y_GTKBUILDER_PROPERTY)

#define GLADE_TAG_A11Y_ACTION(type) \
	((type == GLADE_PROJECT_FORMAT_LIBGLADE) ? \
	 GLADE_TAG_A11Y_LIBGLADE_ACTION : GLADE_TAG_A11Y_GTKBUILDER_ACTION)

#define GLADE_TAG_A11Y_RELATION(type) \
	((type == GLADE_PROJECT_FORMAT_LIBGLADE) ? \
	 GLADE_TAG_A11Y_LIBGLADE_RELATION : GLADE_TAG_A11Y_GTKBUILDER_RELATION)


277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
static const gchar *atk_relations_list[] = {
	"controlled-by",
	"controller-for",
	"labelled-by",
	"label-for",
	"member-of",
	"node-child-of",
	"flows-to",
	"flows-from",
	"subwindow-of",
	"embeds",
	"embedded-by",
	"popup-for",
	"parent-window-of",
	"described-by",
	"description-for",
	NULL
};

static void
297
298
299
glade_gtk_read_accels (GladeWidget  *widget,
		       GladeXmlNode *node,
		       gboolean      require_signal)
300
301
302
303
304
305
306
307
308
309
310
311
312
{
	GladeProperty  *property;
	GladeXmlNode   *prop;
	GladeAccelInfo *ainfo;
	GValue         *value = NULL;
	GList          *accels = NULL;

	for (prop = glade_xml_node_get_children (node); 
	     prop; prop = glade_xml_node_next (prop))
	{
		if (!glade_xml_node_verify_silent (prop, GLADE_TAG_ACCEL))
			continue;

313
314
		if ((ainfo = glade_accel_read (prop, require_signal)) != NULL)
			accels = g_list_prepend (accels, ainfo);
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
	}

	if (accels)
	{
		value = g_new0 (GValue, 1);
		g_value_init (value, GLADE_TYPE_ACCEL_GLIST);
		g_value_take_boxed (value, accels);

		property = glade_widget_get_property (widget, "accelerator");
		glade_property_set_value (property, value);

		g_value_unset (value);
		g_free (value);
	}
}

static void
glade_gtk_parse_atk_props (GladeWidget  *widget,
			   GladeXmlNode *node)
{
335
	GladeProjectFormat fmt;
336
337
338
339
340
341
342
	GladeXmlNode  *prop;
	GladeProperty *property;
	GValue        *gvalue;
	gchar         *value, *name, *id, *comment;
	gint           translatable, has_context;
	gboolean       is_action;

343
344
	fmt = glade_project_get_format (widget->project);

345
346
347
	for (prop = glade_xml_node_get_children (node); 
	     prop; prop = glade_xml_node_next (prop))
	{
348
		if (glade_xml_node_verify_silent (prop, GLADE_TAG_A11Y_PROPERTY (fmt)))
349
			is_action = FALSE;
350
		else if (glade_xml_node_verify_silent (prop, GLADE_TAG_A11Y_ACTION (fmt)))
351
352
353
354
355
356
357
358
359
360
361
362
363
			is_action = TRUE;
		else 
			continue;

		if (!is_action && 
		    !(name = glade_xml_get_property_string_required
		      (prop, GLADE_XML_TAG_NAME, NULL)))
			continue;
		else if (is_action && 
			 !(name = glade_xml_get_property_string_required
			   (prop, GLADE_TAG_A11Y_ACTION_NAME, NULL)))
			continue;

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
		/* Make sure we are working with dashes and
		 * not underscores ... 
		 */
		id = glade_util_read_prop_name (name);
		g_free (name);

		/* We are namespacing the action properties internally
		 * just incase they clash (all property names must be
		 * unique...)
		 */
		if (is_action) 
		{
			name = g_strdup_printf ("atk-%s", id);
			g_free (id);
			id = name;
		}

		if ((property = glade_widget_get_property (widget, id)) != NULL)
		{
			/* Complex statement just getting the value here... */
			if ((!is_action && 
			     !(value = glade_xml_get_content (prop))) ||
			    (is_action &&
			     !(value = glade_xml_get_property_string_required
			       (prop, GLADE_TAG_A11Y_DESC, NULL))))
			{
				/* XXX should be a glade_xml_get_content_required()... */
				g_free (id);
				continue;
			}

			/* Set the parsed value on the property ... */
			gvalue = glade_property_class_make_gvalue_from_string
398
				(property->klass, value, widget->project, widget);
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
			glade_property_set_value (property, gvalue);
			g_value_unset (gvalue);
			g_free (gvalue);

			/* Deal with i18n... */
			translatable = glade_xml_get_property_boolean
				(prop, GLADE_TAG_TRANSLATABLE, FALSE);
			has_context = glade_xml_get_property_boolean
				(prop, GLADE_TAG_HAS_CONTEXT, FALSE);
			comment = glade_xml_get_property_string
				(prop, GLADE_TAG_COMMENT);

			glade_property_i18n_set_translatable
				(property, translatable);
			glade_property_i18n_set_has_context
				(property, has_context);
			glade_property_i18n_set_comment
				(property, comment);
			
			g_free (comment);
			g_free (value);
		}

		g_free (id);
	}
}

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
static void
glade_gtk_parse_atk_props_gtkbuilder (GladeWidget  *widget, 
				      GladeXmlNode *node)
{
	GladeXmlNode *child, *object_node;
	gchar        *internal;

	/* Search for internal "accessible" child and redirect parse from there */
	for (child = glade_xml_node_get_children (node); 
	     child; child = glade_xml_node_next (child))
	{
		if (glade_xml_node_verify_silent (child, GLADE_XML_TAG_CHILD))
		{
			if ((internal =
			     glade_xml_get_property_string (child, GLADE_XML_TAG_INTERNAL_CHILD)))
			{
				if (!strcmp (internal, GLADE_TAG_A11Y_INTERNAL_NAME) &&
				    (object_node = 
				     glade_xml_search_child_required 
				     (child, GLADE_XML_TAG_BUILDER_WIDGET)))
					glade_gtk_parse_atk_props (widget, object_node);

				g_free (internal);
			}
		}
	}
}

454
455
456
457
static void
glade_gtk_parse_atk_relation (GladeProperty *property,
			      GladeXmlNode  *node)
{
458
	GladeProjectFormat fmt;
459
460
461
462
	GladeXmlNode *prop;
	gchar *type, *target, *id, *tmp;
	gchar *string = NULL;

463
464
	fmt = glade_project_get_format (property->widget->project);

465
466
467
	for (prop = glade_xml_node_get_children (node); 
	     prop; prop = glade_xml_node_next (prop))
	{
468
		if (!glade_xml_node_verify_silent (prop, GLADE_TAG_A11Y_RELATION (fmt)))
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
			continue;

		if (!(type = 
		     glade_xml_get_property_string_required
		     (prop, GLADE_TAG_A11Y_TYPE, NULL)))
			continue;

		if (!(target = 
		      glade_xml_get_property_string_required
		      (prop, GLADE_TAG_A11Y_TARGET, NULL)))
		{
			g_free (type);
			continue;
		}

		id = glade_util_read_prop_name (type);

		if (!strcmp (id, property->klass->id))
		{
			if (string == NULL)
				string = g_strdup (target);
			else
			{
				tmp = g_strdup_printf ("%s%s%s", string, 
						       GPC_OBJECT_DELIMITER, target);
				string = (g_free (string), tmp);
			}
			
		}

		g_free (id);
		g_free (type);
		g_free (target);
	}


	/* we must synchronize this directly after loading this project
	 * (i.e. lookup the actual objects after they've been parsed and
	 * are present). this is a feature of object and object list properties
	 * that needs a better api.
	 */
	if (string)
	{
		g_object_set_data_full (G_OBJECT (property), "glade-loaded-object", 
					g_strdup (string), g_free);
	}
}

static void
glade_gtk_widget_read_atk_props (GladeWidget  *widget,
				 GladeXmlNode *node)
{
	GladeXmlNode  *atk_node;
	GladeProperty *property;
	gint           i;

525
526
527
	if (glade_project_get_format (widget->project) == GLADE_PROJECT_FORMAT_GTKBUILDER)
		glade_gtk_parse_atk_props_gtkbuilder (widget, node);

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
	if ((atk_node = 
	     glade_xml_search_child (node, GLADE_TAG_A11Y_A11Y)) != NULL)
	{
		/* Properties & actions */
		glade_gtk_parse_atk_props (widget, atk_node);

		/* Relations */
		for (i = 0; atk_relations_list[i]; i++)
		{
			if ((property = 
			     glade_widget_get_property (widget, 
							atk_relations_list[i])))
				glade_gtk_parse_atk_relation (property, atk_node);
			else
				g_warning ("Couldnt find atk relation %s",
					   atk_relations_list[i]);
		}
	}
}

void
glade_gtk_widget_read_widget (GladeWidgetAdaptor *adaptor,
			      GladeWidget        *widget,
			      GladeXmlNode       *node)
{
553
554
	if (!glade_xml_node_verify 
	    (node, GLADE_XML_TAG_WIDGET (glade_project_get_format (widget->project))))
555
556
557
558
559
560
		return;

	/* First chain up and read in all the normal properties.. */
        GWA_GET_CLASS (G_TYPE_OBJECT)->read_widget (adaptor, widget, node);

	/* Read in accelerators */
561
	glade_gtk_read_accels (widget, node, TRUE);
562
563
564
565
566
567
568
569
570
571
572

	/* Read in atk props */
	glade_gtk_widget_read_atk_props (widget, node);

}

static void
glade_gtk_widget_write_atk_property (GladeProperty      *property,
				     GladeXmlContext    *context,
				     GladeXmlNode       *node)
{
573
	GladeProjectFormat fmt;
574
575
576
	GladeXmlNode  *prop_node;
	gchar         *value;
	
577
578
	fmt = glade_project_get_format (property->widget->project);

579
580
581
	glade_property_get (property, &value);
	if (value && value[0])
	{
582
		prop_node = glade_xml_node_new (context, GLADE_TAG_A11Y_PROPERTY (fmt));
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
		glade_xml_node_append_child (node, prop_node);

		glade_xml_node_set_property_string (prop_node, 
						    GLADE_TAG_NAME, 
						    property->klass->id);

		glade_xml_set_content (prop_node, value);

		if (property->i18n_translatable)
			glade_xml_node_set_property_string (prop_node, 
							    GLADE_TAG_TRANSLATABLE, 
							    GLADE_XML_TAG_I18N_TRUE);

		if (property->i18n_has_context)
			glade_xml_node_set_property_string (prop_node, 
							    GLADE_TAG_HAS_CONTEXT, 
							    GLADE_XML_TAG_I18N_TRUE);


		if (property->i18n_comment)
			glade_xml_node_set_property_string (prop_node, 
							    GLADE_TAG_COMMENT, 
							    property->i18n_comment);
	}
}

static void
610
611
612
glade_gtk_widget_write_atk_properties_libglade (GladeWidget        *widget,
						GladeXmlContext    *context,
						GladeXmlNode       *node)
613
614
615
616
617
618
619
620
621
622
{
	GladeProperty *name_prop, *desc_prop;

	name_prop = glade_widget_get_property (widget, "AtkObject::accessible-name");
	desc_prop = glade_widget_get_property (widget, "AtkObject::accessible-description");

	glade_gtk_widget_write_atk_property (name_prop, context, node);
	glade_gtk_widget_write_atk_property (desc_prop, context, node);
}

623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
static void
glade_gtk_widget_write_atk_properties_gtkbuilder (GladeWidget        *widget,
						  GladeXmlContext    *context,
						  GladeXmlNode       *node)
{
	GladeXmlNode  *child_node, *object_node;
	GladeProperty *name_prop, *desc_prop;

	name_prop = glade_widget_get_property (widget, "AtkObject::accessible-name");
	desc_prop = glade_widget_get_property (widget, "AtkObject::accessible-description");

	/* Create internal child here if any of these properties are non-null */
	if (!glade_property_default (name_prop) || 
	    !glade_property_default (desc_prop))
	{
638
639
		gchar *atkname = g_strdup_printf ("%s-atkobject", widget->name);

640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
		child_node = glade_xml_node_new (context, GLADE_XML_TAG_CHILD);
		glade_xml_node_append_child (node, child_node);

		glade_xml_node_set_property_string (child_node, 
						    GLADE_XML_TAG_INTERNAL_CHILD, 
						    GLADE_TAG_A11Y_INTERNAL_NAME);

		object_node = glade_xml_node_new (context, GLADE_XML_TAG_BUILDER_WIDGET);
		glade_xml_node_append_child (child_node, object_node);

		glade_xml_node_set_property_string (object_node, 
						    GLADE_XML_TAG_CLASS, 
						    "AtkObject");

		glade_xml_node_set_property_string (object_node, 
						    GLADE_XML_TAG_ID, 
656
						    atkname);
657
658
659
660
661
662
	
		if (!glade_property_default (name_prop))
			glade_gtk_widget_write_atk_property (name_prop, context, object_node);
		if (!glade_property_default (desc_prop))
			glade_gtk_widget_write_atk_property (desc_prop, context, object_node);

663
		g_free (atkname);
664
665
666
667
	}

}

668
669
670
671
672
static void
glade_gtk_widget_write_atk_relation (GladeProperty      *property,
				     GladeXmlContext    *context,
				     GladeXmlNode       *node)
{
673
	GladeProjectFormat fmt;
674
675
676
	GladeXmlNode *prop_node;
	gchar        *value, **split;
	gint          i;
677
678
	
	fmt = glade_project_get_format (property->widget->project);
679
680
681

	if ((value = glade_widget_adaptor_string_from_value
	     (GLADE_WIDGET_ADAPTOR (property->klass->handle),
682
	      property->klass, property->value, fmt)) != NULL)
683
684
685
686
687
	{
		if ((split = g_strsplit (value, GPC_OBJECT_DELIMITER, 0)) != NULL)
		{
			for (i = 0; split[i] != NULL; i++)
			{
688
689
				prop_node = glade_xml_node_new (context, 
								GLADE_TAG_A11Y_RELATION (fmt));
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
				glade_xml_node_append_child (node, prop_node);

				glade_xml_node_set_property_string (prop_node, 
								    GLADE_TAG_A11Y_TYPE, 
								    property->klass->id);
				glade_xml_node_set_property_string (prop_node, 
								    GLADE_TAG_A11Y_TARGET, 
								    split[i]);
			}
			g_strfreev (split);
		}
	}
}

static void
glade_gtk_widget_write_atk_relations (GladeWidget        *widget,
				      GladeXmlContext    *context,
				      GladeXmlNode       *node)
{
	GladeProperty *property;
	gint i;

	for (i = 0; atk_relations_list[i]; i++)
	{
		if ((property = 
		     glade_widget_get_property (widget, 
						atk_relations_list[i])))
			glade_gtk_widget_write_atk_relation (property, context, node);
		else
			g_warning ("Couldnt find atk relation %s on widget %s",
				   atk_relations_list[i], widget->name);
	}
}

static void
glade_gtk_widget_write_atk_action (GladeProperty      *property,
				   GladeXmlContext    *context,
				   GladeXmlNode       *node)
{
729
 	GladeProjectFormat fmt;
730
731
	GladeXmlNode *prop_node;
	gchar        *value = NULL;
732
733
	
	fmt = glade_project_get_format (property->widget->project);
734
735
736
737
738

	glade_property_get (property, &value);

	if (value && value[0])
	{
739
		prop_node = glade_xml_node_new (context, GLADE_TAG_A11Y_ACTION (fmt));
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
		glade_xml_node_append_child (node, prop_node);

		glade_xml_node_set_property_string (prop_node, 
						    GLADE_TAG_A11Y_ACTION_NAME, 
						    &property->klass->id[4]);
		glade_xml_node_set_property_string (prop_node, 
						    GLADE_TAG_A11Y_DESC, 
						    value);
	}
}

static void
glade_gtk_widget_write_atk_actions (GladeWidget        *widget,
				    GladeXmlContext    *context,
				    GladeXmlNode       *node)
{
	GladeProperty *property;

	if ((property = glade_widget_get_property (widget, "atk-click")) != NULL)
		glade_gtk_widget_write_atk_action (property, context, node);
	if ((property = glade_widget_get_property (widget, "atk-activate")) != NULL)
		glade_gtk_widget_write_atk_action (property, context, node);
	if ((property = glade_widget_get_property (widget, "atk-press")) != NULL)
		glade_gtk_widget_write_atk_action (property, context, node);
	if ((property = glade_widget_get_property (widget, "atk-release")) != NULL)
		glade_gtk_widget_write_atk_action (property, context, node);
}

static void
glade_gtk_widget_write_atk_props (GladeWidget        *widget,
				  GladeXmlContext    *context,
				  GladeXmlNode       *node)
{
773
774
775
776
 	GladeProjectFormat  fmt;
	GladeXmlNode       *atk_node;

	fmt = glade_project_get_format (widget->project);
777
778
779

	atk_node = glade_xml_node_new (context, GLADE_TAG_A11Y_A11Y);

780
781
782
	if (fmt == GLADE_PROJECT_FORMAT_LIBGLADE)
		glade_gtk_widget_write_atk_properties_libglade (widget, context, atk_node);

783
784
785
786
787
	glade_gtk_widget_write_atk_relations (widget, context, atk_node);
	glade_gtk_widget_write_atk_actions (widget, context, atk_node);

	if (!glade_xml_node_get_children (atk_node))
		glade_xml_node_delete (atk_node);
788
789
	else
		glade_xml_node_append_child (node, atk_node);
790
791
792

	if (fmt == GLADE_PROJECT_FORMAT_GTKBUILDER)
		glade_gtk_widget_write_atk_properties_gtkbuilder (widget, context, node);
793
794
795
}

static void
796
797
798
799
glade_gtk_write_accels (GladeWidget        *widget,
			GladeXmlContext    *context,
			GladeXmlNode       *node,
			gboolean            write_signal)
800
801
802
803
804
805
806
807
808
809
810
811
812
813
{
	GladeXmlNode  *accel_node;
	GladeProperty *property;
	GList         *list;

	/* Some child widgets may have disabled the property */
	if (!(property = glade_widget_get_property (widget, "accelerator")))
		return;

	for (list = g_value_get_boxed (property->value); 
	     list; list = list->next)
	{
		GladeAccelInfo *accel = list->data;

814
		accel_node = glade_accel_write (accel, context, write_signal);
815
816
817
818
819
820
821
822
823
824
		glade_xml_node_append_child (node, accel_node);
	}
}

void
glade_gtk_widget_write_widget (GladeWidgetAdaptor *adaptor,
			       GladeWidget        *widget,
			       GladeXmlContext    *context,
			       GladeXmlNode       *node)
{
825
826
827
828
829
 	GladeProjectFormat  fmt;

	fmt = glade_project_get_format (widget->project);

	if (!glade_xml_node_verify (node, GLADE_XML_TAG_WIDGET (fmt)))
830
831
832
833
834
		return;

	/* First chain up and read in all the normal properties.. */
        GWA_GET_CLASS (G_TYPE_OBJECT)->write_widget (adaptor, widget, context, node);

835

836
837
838
839
840
841
842
843
	/* in Libglade the order must be Properties, Atk, Signals, Accels. 
	 * in builder it doesnt matter so long as signals are after properties
	 * and before objects.
	 */
	if (fmt == GLADE_PROJECT_FORMAT_LIBGLADE)
	{
 		glade_gtk_widget_write_atk_props (widget, context, node);
		glade_widget_write_signals (widget, context, node);
844
		glade_gtk_write_accels (widget, context, node, TRUE);
845
846
	} else {
		/* The core takes care of signals in GtkBuilder format */
847
		glade_gtk_write_accels (widget, context, node, TRUE);		
848
849
 		glade_gtk_widget_write_atk_props (widget, context, node);
	}
850
851
852
853
854
855
856
857
858
859
860
}


GladeEditorProperty *
glade_gtk_widget_create_eprop (GladeWidgetAdaptor *adaptor,
			       GladePropertyClass *klass,
			       gboolean            use_command)
{
	GladeEditorProperty *eprop;

	/* chain up.. */
861
	if (klass->pspec->value_type == GLADE_TYPE_ACCEL_GLIST)
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
		eprop = g_object_new (GLADE_TYPE_EPROP_ACCEL,
				      "property-class", klass, 
				      "use-command", use_command,
				      NULL);
	else
		eprop = GWA_GET_CLASS 
			(G_TYPE_OBJECT)->create_eprop (adaptor, 
						       klass, 
						       use_command);
	return eprop;
}

gchar *
glade_gtk_widget_string_from_value (GladeWidgetAdaptor *adaptor,
				    GladePropertyClass *klass,
877
878
				    const GValue       *value,
				    GladeProjectFormat  fmt)
879
{
880
	if (klass->pspec->value_type == GLADE_TYPE_ACCEL_GLIST)
881
882
883
884
885
		return glade_accels_make_string (g_value_get_boxed (value));
	else
		return GWA_GET_CLASS 
			(G_TYPE_OBJECT)->string_from_value (adaptor, 
							    klass, 
886
887
							    value,
							    fmt);
888
889
}

890
891
892
893
894
895
896
static void
widget_parent_changed (GtkWidget          *widget,
		       GParamSpec         *pspec,
		       GladeWidgetAdaptor *adaptor)
{
	GladeWidget *gwidget = glade_widget_get_from_gobject (widget);

897
898
899
900
901
	/* this could get called for a stale instance of an object
	 * being rebuilt for a contruct-only property. */
	if (!gwidget)
		return;

902
	if (gwidget->parent && gwidget->parent->internal == NULL)
903
904
905
906
907
		glade_widget_set_action_sensitive (gwidget, "remove_parent", TRUE);
	else
		glade_widget_set_action_sensitive (gwidget, "remove_parent", FALSE);
}

908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926

static void
widget_format_changed (GladeProject *project, 
		       GParamSpec   *pspec,
		       GladeWidget  *gwidget)
{
	if (glade_project_get_format (project) == GLADE_PROJECT_FORMAT_LIBGLADE)
		glade_widget_set_action_sensitive (gwidget, "sizegroup_add", FALSE);
	else
		glade_widget_set_action_sensitive (gwidget, "sizegroup_add", TRUE);
}

static void
widget_project_changed (GladeWidget *gwidget, 
			GParamSpec  *pspec,
			gpointer     userdata)
{
	GladeProject
		*project = glade_widget_get_project (gwidget),
927
		*old_project = g_object_get_data (G_OBJECT (gwidget), "widget-project-ptr");
928
929
930
931
932
933
934
935
936
937
	
	if (old_project)
		g_signal_handlers_disconnect_by_func (G_OBJECT (old_project), 
						      G_CALLBACK (widget_format_changed),
						      gwidget);

	if (project)
		g_signal_connect (G_OBJECT (project), "notify::format", 
				  G_CALLBACK (widget_format_changed), gwidget);

938
	g_object_set_data (G_OBJECT (gwidget), "widget-project-ptr", project);
939
940
}

941
942
943
944
945
946
void
glade_gtk_widget_deep_post_create (GladeWidgetAdaptor *adaptor,
				   GObject            *widget, 
				   GladeCreateReason   reason)
{
	GladeWidget *gwidget = glade_widget_get_from_gobject (widget);
947
948
949
950
951

	/* Work around bug 472555 by resetting the default event mask,
	 * this way only user edits will be saved to the glade file. */
	if (reason == GLADE_CREATE_USER)
		glade_widget_property_reset (gwidget, "events");
952
953
954
	
	glade_widget_set_action_sensitive (gwidget, "remove_parent", FALSE);

955
	if (GTK_IS_WINDOW (widget) || gwidget->internal)
956
957
		glade_widget_set_action_sensitive (gwidget, "add_parent", FALSE);

958

959
960
961
962
963
	/* Watch parents/projects and set actions sensitive/insensitive */
	if (gwidget->internal == NULL)
		g_signal_connect (G_OBJECT (widget), "notify::parent",
				  G_CALLBACK (widget_parent_changed), adaptor);
	
964
965
966
967
	g_signal_connect (G_OBJECT (gwidget), "notify::project",
			  G_CALLBACK (widget_project_changed), NULL);

	widget_project_changed (gwidget, NULL, NULL);
968
969
970
}


971
void
972
973
974
975
glade_gtk_widget_set_property (GladeWidgetAdaptor *adaptor,
			       GObject            *object, 
			       const gchar        *id,
			       const GValue       *value)
976
{
977
        /* FIXME: is this still needed with the new gtk+ tooltips? */
978
979
	if (!strcmp (id, "tooltip"))
	{
980
981
982
983
                id = "tooltip-text";
        }

        GWA_GET_CLASS (G_TYPE_OBJECT)->set_property (adaptor, object, id, value);
984
985
}

986
void
987
988
989
990
glade_gtk_widget_get_property (GladeWidgetAdaptor *adaptor,
			       GObject            *object, 
			       const gchar        *id,
			       GValue             *value)
991
{
992
993
	if (!strcmp (id, "tooltip"))
	{
994
                id = "tooltip-text";
995
	}
996
997
	
        GWA_GET_CLASS (G_TYPE_OBJECT)->get_property (adaptor, object, id, value);
998
999
}

1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
static GList *
create_command_property_list (GladeWidget *gnew, GList *saved_props)
{
	GList *l, *command_properties = NULL;
	
	for (l = saved_props; l; l = l->next)
	{
		GladeProperty *property = l->data;
		GladeProperty *orig_prop = glade_widget_get_pack_property (gnew, property->klass->id);
		GCSetPropData *pdata = g_new0 (GCSetPropData, 1);

		pdata->property  = orig_prop;
		pdata->old_value = g_new0 (GValue, 1);
		pdata->new_value = g_new0 (GValue, 1);

		glade_property_get_value (orig_prop, pdata->old_value);
		glade_property_get_value (property, pdata->new_value);

		command_properties = g_list_prepend (command_properties, pdata);
	}
	return g_list_reverse (command_properties);
}


void
glade_gtk_widget_action_activate (GladeWidgetAdaptor *adaptor,
				  GObject *object,
				  const gchar *action_path)
{
	GladeWidget *gwidget = glade_widget_get_from_gobject (object), *gparent;
	GList       this_widget = { 0, }, that_widget = { 0, };
1031
	GtkWidget   *parent = gtk_widget_get_parent (GTK_WIDGET (object));
1032

1033
1034
1035
1036
	if (parent)
		gparent = glade_widget_get_from_gobject (parent);
	else
		gparent = NULL;
1037

1038
1039
1040
1041
1042
1043
1044
	if (strcmp (action_path, "edit_separate") == 0)
	{
		GtkWidget *dialog = 
			glade_editor_dialog_for_widget (gwidget);
		gtk_widget_show_all (dialog);
	}
	else if (strcmp (action_path, "remove_parent") == 0)
1045
	{
1046
		GladeWidget *new_gparent;
1047

1048
1049
		g_return_if_fail (gparent);
		new_gparent = gparent->parent;
1050
1051
1052
1053
1054
1055
		
		glade_command_push_group (_("Removing parent of %s"), 
					  gwidget->name);

		/* Remove "this" widget */
		this_widget.data = gwidget;
1056
		glade_command_cut (&this_widget);
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
		
		/* Delete the parent */
		that_widget.data = gparent;
		glade_command_delete (&that_widget);

		/* Add "this" widget to the new parent */
		glade_command_paste(&this_widget, new_gparent, NULL);
		
		glade_command_pop_group ();
	}
	else if (strncmp (action_path, "add_parent/", 11) == 0)
	{
		GType new_type = 0;
		
		if (strcmp (action_path + 11, "alignment") == 0)
			new_type = GTK_TYPE_ALIGNMENT;
		else if (strcmp (action_path + 11, "viewport") == 0)
			new_type = GTK_TYPE_VIEWPORT;
		else if (strcmp (action_path + 11, "eventbox") == 0)
			new_type = GTK_TYPE_EVENT_BOX;
		else if (strcmp (action_path + 11, "frame") == 0)
			new_type = GTK_TYPE_FRAME;
		else if (strcmp (action_path + 11, "aspect_frame") == 0)
			new_type = GTK_TYPE_ASPECT_FRAME;
		else if (strcmp (action_path + 11, "scrolled_window") == 0)
			new_type = GTK_TYPE_SCROLLED_WINDOW;
		else if (strcmp (action_path + 11, "expander") == 0)
			new_type = GTK_TYPE_EXPANDER;
		else if (strcmp (action_path + 11, "table") == 0)
			new_type = GTK_TYPE_TABLE;
		else if (strcmp (action_path + 11, "hbox") == 0)
			new_type = GTK_TYPE_HBOX;
		else if (strcmp (action_path + 11, "vbox") == 0)
			new_type = GTK_TYPE_VBOX;
		else if (strcmp (action_path + 11, "hpaned") == 0)
			new_type = GTK_TYPE_HPANED;
		else if (strcmp (action_path + 11, "vpaned") == 0)
			new_type = GTK_TYPE_VPANED;

		
		if (new_type)
		{
			GladeWidgetAdaptor *adaptor = glade_widget_adaptor_get_by_type (new_type);
			GList              *saved_props, *prop_cmds;
1101
			GladeProject       *project;
1102
1103
1104
1105
1106

			/* Dont add non-scrollable widgets to scrolled windows... */
			if (gparent &&
			    glade_util_check_and_warn_scrollable (gparent, adaptor, glade_app_get_window()))
				return;
1107
			
1108
			glade_command_push_group (_("Adding parent %s for %s"), 
1109
1110
1111
						  adaptor->title, gwidget->name);

			/* Record packing properties */
1112
			saved_props = glade_widget_dup_properties (gwidget, gwidget->packing_properties, FALSE, FALSE, FALSE);
1113
1114
1115
			
			/* Remove "this" widget */
			this_widget.data = gwidget;
1116
			glade_command_cut (&this_widget);
1117
1118
1119
1120
1121

			if (gparent)
				project = glade_widget_get_project (gparent);
			else
				project = glade_app_get_project ();
1122
			
1123
			/* Create new widget and put it where the placeholder was */
1124
1125
			if ((that_widget.data =
			     glade_command_create (adaptor, gparent, NULL, project)) != NULL)
1126
1127
			{
			
1128
1129
1130
1131
				/* Remove the alignment that we added in the frame's post_create... */
				if (new_type == GTK_TYPE_FRAME)
				{
					GObject     *frame = glade_widget_get_object (that_widget.data);
1132
					GladeWidget *galign = glade_widget_get_from_gobject (gtk_bin_get_child (GTK_BIN (frame)));
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
					GList        to_delete = { 0, };
					
					to_delete.data = galign;
					glade_command_delete (&to_delete);
				}
				
				/* Create heavy-duty glade-command properties stuff */
				prop_cmds = create_command_property_list (that_widget.data, saved_props);
				g_list_foreach (saved_props, (GFunc)g_object_unref, NULL);
				g_list_free (saved_props);
				
				/* Apply the properties in an undoable way */
				if (prop_cmds)
					glade_command_set_properties_list (glade_widget_get_project (gparent), prop_cmds);
				
				/* Add "this" widget to the new parent */
				glade_command_paste(&this_widget, GLADE_WIDGET (that_widget.data), NULL);
			}
			else
				/* Create parent was cancelled, paste back to parent */
				glade_command_paste(&this_widget, gparent, NULL);

1155
1156
1157
			glade_command_pop_group ();
		}
	}
Tristan Van Berkom's avatar
Tristan Van Berkom committed
1158
1159
1160
1161
	else if (strcmp (action_path, "sizegroup_add") == 0)
	{
		/* Ignore dummy */
	}
1162
1163
1164
1165
1166
1167
	else
		GWA_GET_CLASS (G_TYPE_OBJECT)->action_activate (adaptor,
								object,
								action_path);
}

Tristan Van Berkom's avatar
Tristan Van Berkom committed
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
static GList *list_sizegroups (GladeWidget *gwidget)
{
	GladeProject *project = glade_widget_get_project (gwidget);
	GList *groups = NULL;
	const GList *list;

	for (list = glade_project_get_objects (project); list; list = list->next)
	{
		GladeWidget *iter = glade_widget_get_from_gobject (list->data);
		if (GTK_IS_SIZE_GROUP (iter->object))
			groups = g_list_prepend (groups, iter);
	}
	return g_list_reverse (groups);
}

static void
glade_gtk_widget_add2group_cb (GtkMenuItem *item, GladeWidget *gwidget)
{
	GladeWidget *group = g_object_get_data (G_OBJECT (item), "glade-group-widget");
	GladeWidgetAdaptor *adaptor = glade_widget_adaptor_get_by_type (GTK_TYPE_SIZE_GROUP);
	GList *widget_list = NULL, *new_list;
	GladeProperty *property;

1191
1192
1193
1194
	if (group) 
		glade_command_push_group (_("Adding %s to Size Group %s"), gwidget->name, group->name);
	else
		glade_command_push_group (_("Adding %s to a new Size Group"), gwidget->name);
Tristan Van Berkom's avatar
Tristan Van Berkom committed
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266

	if (!group)
		/* Cant cancel a size group */
		group = glade_command_create (adaptor, NULL, NULL, glade_widget_get_project (gwidget));
 
	property = glade_widget_get_property (group, "widgets");
	glade_property_get (property, &widget_list);
	new_list = g_list_copy (widget_list);
	if (!g_list_find (widget_list, gwidget->object))
		new_list = g_list_append (new_list, gwidget->object);
	glade_command_set_property (property, new_list);

	g_list_free (new_list);
	
	glade_command_pop_group ();
}


GtkWidget *
glade_gtk_widget_action_submenu (GladeWidgetAdaptor *adaptor,
				 GObject *object,
				 const gchar *action_path)
{
	GladeWidget *gwidget = glade_widget_get_from_gobject (object);
	GList *groups, *list;

	if (strcmp (action_path, "sizegroup_add") == 0)
	{
		GtkWidget *menu = gtk_menu_new ();
		GtkWidget *separator, *item;
		GladeWidget *group;

		if ((groups = list_sizegroups (gwidget)) != NULL)
		{
			for (list = groups; list; list = list->next)
			{
				group = list->data;
				item = gtk_menu_item_new_with_label (group->name);

				g_object_set_data (G_OBJECT (item), "glade-group-widget", group);
				g_signal_connect (G_OBJECT (item), "activate",
						  G_CALLBACK (glade_gtk_widget_add2group_cb), gwidget);

				gtk_widget_show (item);
				gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
			}
			g_list_free (groups);

			separator = gtk_menu_item_new ();		
			gtk_menu_shell_append (GTK_MENU_SHELL (menu), separator);
			gtk_widget_show (separator);
		}
		
		/* Add trailing new... item */
		item = gtk_menu_item_new_with_label (_("New Size Group"));
		g_signal_connect (G_OBJECT (item), "activate",
				  G_CALLBACK (glade_gtk_widget_add2group_cb), gwidget);
		
		gtk_widget_show (item);
		gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);

		return menu;
	}
	else if (GWA_GET_CLASS (G_TYPE_OBJECT)->action_submenu)
		return GWA_GET_CLASS (G_TYPE_OBJECT)->action_submenu (adaptor,
								      object,
								      action_path);

	return NULL;
}


1267

1268
/* ----------------------------- GtkContainer ------------------------------ */
1269
void
1270
1271
1272
glade_gtk_container_post_create (GladeWidgetAdaptor *adaptor,
				 GObject            *container, 
				 GladeCreateReason   reason)
1273
{
1274
1275
	GList *children;
	g_return_if_fail (GTK_IS_CONTAINER (container));
1276

1277
	if (reason == GLADE_CREATE_USER)
1278
	{
1279
1280
1281
1282
		if ((children = gtk_container_get_children (GTK_CONTAINER (container))) == NULL)
			gtk_container_add (GTK_CONTAINER (container), glade_placeholder_new ());
		else
			g_list_free (children);
1283
	}
Jose Maria Celorio's avatar
Jose Maria Celorio committed
1284
1285
}

1286
void
1287
1288
1289
glade_gtk_container_replace_child (GladeWidgetAdaptor *adaptor,
				   GtkWidget          *container,
				   GtkWidget          *current,
1290
				   GtkWidget          *new_widget)
1291
{
1292
	GParamSpec **param_spec;
1293
	GladePropertyClass *pclass;
1294
1295
1296
	GValue *value;
	guint nproperties;
	guint i;
1297

1298
	if (gtk_widget_get_parent (current) != container)
1299
		return;
1300

1301
1302
1303
	param_spec = gtk_container_class_list_child_properties
		(G_OBJECT_GET_CLASS (container), &nproperties);
	value = g_malloc0 (sizeof(GValue) * nproperties);
1304

1305
1306
	for (i = 0; i < nproperties; i++) 
	{
1307
1308
1309
1310
		g_value_init (&value[i], param_spec[i]->value_type);
		gtk_container_child_get_property
			(GTK_CONTAINER (container), current, param_spec[i]->name, &value[i]);
	}
1311

1312
	gtk_container_remove (GTK_CONTAINER (container), current);
1313
	gtk_container_add (GTK_CONTAINER (container), new_widget);
1314

1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
	for (i = 0; i < nproperties; i++) 
	{
		/* If the added widget is a placeholder then we
		 * want to keep all the "tranfer-on-paste" properties
		 * as default so that it looks fresh (transfer-on-paste
		 * properties dont effect the position/slot inside a 
		 * contianer)
		 */
		if (GLADE_IS_PLACEHOLDER (new_widget))
		{
			pclass = glade_widget_adaptor_get_pack_property_class
				(adaptor, param_spec[i]->name);

			if (pclass && pclass->transfer_on_paste)
				continue;
		}

1332
		gtk_container_child_set_property
1333
			(GTK_CONTAINER (container), new_widget, param_spec[i]->name, &value[i]);
1334
1335
1336
1337
1338
1339
1340
1341
1342
	}
	
	for (i = 0; i < nproperties; i++)
		g_value_unset (&value[i]);

	g_free (param_spec);
	g_free (value);
}

1343
void
1344
1345
1346
glade_gtk_container_add_child (GladeWidgetAdaptor *adaptor,
			       GtkWidget          *container,
			       GtkWidget          *child)
1347
{
1348
1349
	GtkWidget *container_child = gtk_bin_get_child (GTK_BIN (container));

1350
1351
	/* Get a placeholder out of the way before adding the child if its a GtkBin
	 */
1352
1353
1354
	if (GTK_IS_BIN (container) && container_child != NULL &&
	    GLADE_IS_PLACEHOLDER (container_child))
		gtk_container_remove (GTK_CONTAINER (container), container_child);
1355
1356
1357
1358

	gtk_container_add (GTK_CONTAINER (container), child);
}

1359
void
1360
1361
1362
glade_gtk_container_remove_child (GladeWidgetAdaptor *adaptor,
				  GtkWidget          *container,
				  GtkWidget          *child)
1363
{
1364
	GList *children;
1365
1366
1367
1368
	gtk_container_remove (GTK_CONTAINER (container), child);

	/* If this is the last one, add a placeholder by default.
	 */
1369
1370
	if ((children = gtk_container_get_children (GTK_CONTAINER (container))) == NULL)
	{
1371
		gtk_container_add (GTK_CONTAINER (container), glade_placeholder_new ());
1372
1373
1374
	}
	else
		g_list_free (children);
1375
1376
}

1377
void
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
glade_gtk_container_set_child_property (GladeWidgetAdaptor *adaptor,
					GObject            *container,
					GObject            *child,
					const gchar        *property_name,
					const GValue       *value)
{
	gtk_container_child_set_property (GTK_CONTAINER (container),
					  GTK_WIDGET (child),
					  property_name, value);
}

1389
void
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
glade_gtk_container_get_child_property (GladeWidgetAdaptor *adaptor,
					GObject            *container,
					GObject            *child,
					const gchar        *property_name,
					GValue             *value)
{
	gtk_container_child_get_property (GTK_CONTAINER (container),
					  GTK_WIDGET (child),
					  property_name, value);
}


1402
GList *
1403
1404
1405
1406
1407
1408
glade_gtk_container_get_children (GladeWidgetAdaptor  *adaptor,
				  GtkContainer        *container)
{
	return glade_util_container_get_all_children (container);
}

1409
1410
1411
1412
1413
1414
1415
GladeEditable *
glade_gtk_container_create_editable (GladeWidgetAdaptor  *adaptor,
				       GladeEditorPageType  type)
{
	return GWA_GET_CLASS (GTK_TYPE_CONTAINER)->create_editable (adaptor, type);;
}

1416
/* ----------------------------- GtkBox ------------------------------ */
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
typedef struct {
	GtkWidget *widget;
	gint       position;
} GladeGtkBoxChild;

static GList *glade_gtk_box_original_positions = NULL;

static gboolean
glade_gtk_box_configure_child (GladeFixed   *fixed,
			       GladeWidget  *child,
			       GdkRectangle *rect,
			       GtkWidget    *box)
{
1430
1431
	GList         *list, *children;
	GtkWidget     *bchild;
1432
1433
1434
	GtkAllocation  allocation, bchild_allocation;
	gint           point, trans_point, span,
		iter_span, position, old_position,
1435
		offset, orig_offset;
1436
	gboolean       found = FALSE;
1437

1438
	gtk_widget_get_allocation (GTK_WIDGET (child->object), &allocation);
1439
	if (GTK_IS_HBOX (box) || GTK_IS_HBUTTON_BOX (box))
1440
	{
1441
		point       = fixed->mouse_x;
1442
		span        = allocation.width;
1443
1444
		offset      = rect->x;
		orig_offset = fixed->child_x_origin;
1445
1446
1447
	}
	else
	{
1448
		point       = fixed->mouse_y;
1449
		span        = allocation.height;
1450
1451
		offset      = rect->y;
		orig_offset = fixed->child_y_origin;
1452
1453
	}

1454
1455
1456
	glade_widget_pack_property_get
		(child, "position", &old_position);

1457
1458
1459
	children = gtk_container_get_children (GTK_CONTAINER (box));

	for (list = children; list; list = list->next)
1460
1461
1462
	{
		bchild = list->data;

1463
		if (bchild == GTK_WIDGET (child->object))
1464
1465
			continue;

1466
1467
1468
1469
1470
		/* Find the widget in the box where the center of
		 * this rectangle fits... and set the position to that
		 * position.
		 */

1471
		gtk_widget_get_allocation (GTK_WIDGET (bchild), &bchild_allocation);
1472
		if (GTK_IS_HBOX (box) || GTK_IS_HBUTTON_BOX (box))
1473
1474
		{
			gtk_widget_translate_coordinates 
1475
				(GTK_WIDGET (box), bchild,
1476
1477
				 point, 0, &trans_point, NULL);

1478
			iter_span   = bchild_allocation.width;
1479
1480
1481
1482
		}
		else
		{
			gtk_widget_translate_coordinates 
1483
				(GTK_WIDGET (box), bchild,
1484
				 0, point, NULL, &trans_point);
1485
			iter_span = bchild_allocation.height;
1486
1487
1488
1489
		}

#if 0
		gtk_container_child_get (GTK_CONTAINER (box),
1490
					 bchild,
1491
					 "position", &position, NULL);
1492
		g_print ("widget: %p pos %d, point %d, trans_point %d, iter_span %d\n", 
1493
			 bchild, position, point, trans_point, iter_span);
1494
1495
#endif

1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
		if (iter_span <= span)
		{
			found = trans_point >= 0 && trans_point < iter_span;
		}
		else
		{
			if (offset > orig_offset)
				found = trans_point >= iter_span - span && 
					trans_point < iter_span;
			else if (offset < orig_offset)
				found = trans_point >= 0 &&
					trans_point < span;
		}
1509
1510
1511
1512

		if (found)
		{
			gtk_container_child_get (GTK_CONTAINER (box),
1513
						 bchild,
1514
1515
						 "position", &position, NULL);

1516
1517
1518
1519
1520
1521
#if 0
			g_print ("setting position of %s from %d to %d, "
				 "(point %d iter_span %d)\n", 
				 child->name, old_position, position, trans_point, iter_span);
#endif

1522
1523
1524
1525
1526
1527
1528
			glade_widget_pack_property_set
				(child, "position", position);
							
			break;
		}

	}
1529
1530
1531

	g_list_free (children);

1532
1533
1534
1535
1536
1537
1538
1539
	return TRUE;
}

static gboolean
glade_gtk_box_configure_begin (GladeFixed  *fixed,
			       GladeWidget *child,
			       GtkWidget   *box)
{
1540
1541
	GList       *list, *children;
	GtkWidget   *bchild;
1542
1543
1544

	g_assert (glade_gtk_box_original_positions == NULL);

1545
1546
1547
	children = gtk_container_get_children (GTK_CONTAINER (box));

	for (list = children; list; list = list->next)
1548
1549
1550
1551
1552
	{
		GladeGtkBoxChild *gbchild;
		GladeWidget      *gchild;
				
		bchild = list->data;