glade-editor-property.c 117 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
21
22
23
24
25
26
27
28
29
30
 *
 * Authors:
 *   Tristan Van Berkom <tvb@gnome.org>
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <string.h>
#include <glib/gi18n-lib.h>
#include <gtk/gtk.h>
31
#include <gdk/gdkkeysyms.h>
32
33
34
35
36
37
38
39

#include "glade.h"
#include "glade-widget.h"
#include "glade-editor-property.h"
#include "glade-property.h"
#include "glade-command.h"
#include "glade-project.h"
#include "glade-builtins.h"
40
#include "glade-marshallers.h"
41
42
43
44

enum {
	PROP_0,
	PROP_PROPERTY_CLASS,
45
46
	PROP_USE_COMMAND,
	PROP_SHOW_INFO
47
48
};

49
50
51
52
enum {
	GTK_DOC_SEARCH,
	LAST_SIGNAL
};
53
54
55
56

static GtkTableClass             *table_class;
static GladeEditorPropertyClass  *editor_property_class;

57
static guint                      glade_editor_property_signals[LAST_SIGNAL] = { 0 };
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

#define GLADE_PROPERTY_TABLE_ROW_SPACING 2
#define FLAGS_COLUMN_SETTING             0
#define FLAGS_COLUMN_SYMBOL              1

/*******************************************************************************
                Boiler plate macros (inspired from glade-command.c)
 *******************************************************************************/
#define MAKE_TYPE(func, type, parent)			\
GType							\
func ## _get_type (void)				\
{							\
	static GType cmd_type = 0;			\
							\
	if (!cmd_type)					\
	{						\
		static const GTypeInfo info =		\
		{					\
			sizeof (type ## Class),		\
			(GBaseInitFunc) NULL,		\
			(GBaseFinalizeFunc) NULL,	\
			(GClassInitFunc) func ## _class_init,	\
			(GClassFinalizeFunc) NULL,	\
			NULL,				\
			sizeof (type),			\
			0,				\
			(GInstanceInitFunc) NULL	\
		};					\
							\
		cmd_type = g_type_register_static (parent, #type, &info, 0);	\
	}						\
							\
	return cmd_type;				\
}							\


#define GLADE_MAKE_EPROP(type, func)					\
static void								\
func ## _finalize (GObject *object);					\
static void								\
func ## _load (GladeEditorProperty *me, GladeProperty *property);	\
static GtkWidget *							\
func ## _create_input (GladeEditorProperty *me);			\
static void								\
func ## _class_init (gpointer parent_tmp, gpointer notused)		\
{									\
	GladeEditorPropertyClass *parent = parent_tmp;			\
	GObjectClass* object_class;					\
	object_class = G_OBJECT_CLASS (parent);				\
	parent->load =  func ## _load;					\
	parent->create_input =  func ## _create_input;			\
	object_class->finalize = func ## _finalize;			\
}									\
typedef struct {							\
	GladeEditorPropertyClass cmd;					\
} type ## Class;							\
static MAKE_TYPE(func, type, GLADE_TYPE_EDITOR_PROPERTY)




/*******************************************************************************
                               GladeEditorPropertyClass
 *******************************************************************************/
122

Tristan Van Berkom's avatar
Updated    
Tristan Van Berkom committed
123
124
125
126
/* declare this forwardly for the finalize routine */
static void glade_editor_property_load_common (GladeEditorProperty *eprop, 
					       GladeProperty       *property);

127
128
129
130
131
132
133
134
135
136
137
/* For use in editor implementations
 */
static void
glade_editor_property_commit (GladeEditorProperty *eprop,
			      GValue              *value)
{
	g_return_if_fail (GLADE_IS_EDITOR_PROPERTY (eprop));

	if (eprop->use_command == FALSE)
		glade_property_set_value (eprop->property, value);
	else
138
		glade_command_set_property_value (eprop->property, value);
139
140
141
142

	/* If the value was denied by a verify function, we'll have to
	 * reload the real value.
	 */
143
	if (g_param_values_cmp (eprop->property->klass->pspec,
144
145
146
147
148
149
				eprop->property->value, value) != 0)
		GLADE_EDITOR_PROPERTY_GET_CLASS (eprop)->load (eprop, eprop->property);
}



150
151
152
153
154
155
156
157
158
static void
glade_editor_property_tooltip_cb (GladeProperty       *property,
				  const gchar         *tooltip,
				  GladeEditorProperty *eprop)
{
	glade_util_widget_set_tooltip (eprop->input, tooltip);
	glade_util_widget_set_tooltip (eprop->eventbox, tooltip);
}

159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
static void
glade_eprop_label_style_update_cb (GtkWidget           *label,
				GtkStyle            *prev,
				GladeEditorProperty *eprop)
{
	if (eprop->insensitive_colour)
		gdk_color_free (eprop->insensitive_colour);
	if (eprop->normal_colour)
		gdk_color_free (eprop->normal_colour);

	eprop->insensitive_colour =
		gdk_color_copy (&(label->style->fg[GTK_STATE_INSENSITIVE]));
	eprop->normal_colour      =
		gdk_color_copy (&(label->style->fg[GTK_STATE_NORMAL]));
}

175
176
177
178
179
180
181
static void
glade_editor_property_sensitivity_cb (GladeProperty       *property,
				      GParamSpec          *pspec,
				      GladeEditorProperty *eprop)
{
	gboolean sensitive = glade_property_get_sensitive (eprop->property);

182
183
	g_signal_handlers_block_by_func
		(eprop->item_label, glade_eprop_label_style_update_cb, eprop);
184
185
186
	gtk_widget_modify_fg 
		(GTK_WIDGET (eprop->item_label), 
		 GTK_STATE_NORMAL, 
187
188
189
		 sensitive ? eprop->normal_colour : eprop->insensitive_colour);
	g_signal_handlers_unblock_by_func
		(eprop->item_label, glade_eprop_label_style_update_cb, eprop);
190
191
192

	if (sensitive == FALSE)
		gtk_widget_set_sensitive (eprop->input, FALSE);
193
	else if (glade_property_get_enabled (property) != FALSE)
194
195
196
197
		gtk_widget_set_sensitive (eprop->input, TRUE);

	if (eprop->check)
		gtk_widget_set_sensitive (eprop->check, sensitive);
198
199
}

200
void
201
glade_editor_property_value_changed_cb (GladeProperty       *property,
202
					GValue              *old_value,
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
					GValue              *value,
					GladeEditorProperty *eprop)
{
	g_assert (eprop->property == property);
	glade_editor_property_load (eprop, eprop->property);
}

static void
glade_editor_property_enabled_cb (GladeProperty       *property,
				  GParamSpec          *pspec,
				  GladeEditorProperty *eprop)
{
	gboolean enabled;
	g_assert (eprop->property == property);

218
	if (eprop->klass->optional)
219
220
	{
		enabled = glade_property_get_enabled (property);
221
222
223
224
225
226
227

		/* sensitive = enabled &&   */
		if (enabled == FALSE)
			gtk_widget_set_sensitive (eprop->input, FALSE);
		else if (glade_property_get_sensitive (property))
			gtk_widget_set_sensitive (eprop->input, TRUE);

228
229
230
231
232
233
234
235
236
237
238
239
		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (eprop->check), enabled);
	}
}

static void
glade_editor_property_enabled_toggled_cb (GtkWidget           *check,
					  GladeEditorProperty *eprop)
{
	glade_property_set_enabled (eprop->property, 
				    gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (check)));
}

240
241
242
243
static void
glade_editor_property_info_clicked_cb (GtkWidget           *info,
				       GladeEditorProperty *eprop)
{
244
245
	GladeWidgetAdaptor *adaptor;
	gchar              *search, *book;
246

247
248
	adaptor = glade_widget_adaptor_from_pclass (eprop->klass);
	search  = g_strdup_printf ("The %s property", eprop->klass->id);
249
250

	g_object_get (adaptor, "book", &book, NULL);
251
252
253

	g_signal_emit (G_OBJECT (eprop),
		       glade_editor_property_signals[GTK_DOC_SEARCH],
254
		       0, book,
255
		       g_type_name (eprop->klass->pspec->owner_type), search);
256

257
	g_free (book);
258
259
260
261
262
263
	g_free (search);
}

static GtkWidget *
glade_editor_property_create_info_button (GladeEditorProperty *eprop)
{
Tristan Van Berkom's avatar
Tristan Van Berkom committed
264
265
	GtkWidget *image;
	GtkWidget *button;
266
267

	button = gtk_button_new ();
Tristan Van Berkom's avatar
Tristan Van Berkom committed
268
269

	image = glade_util_get_devhelp_icon (GTK_ICON_SIZE_MENU);
270
	gtk_widget_show (image);
Tristan Van Berkom's avatar
Tristan Van Berkom committed
271

272
273
	gtk_container_add (GTK_CONTAINER (button), image);

Tristan Van Berkom's avatar
Tristan Van Berkom committed
274
	glade_util_widget_set_tooltip (button, _("View GTK+ documentation for this property"));
275
276
277
278

	return button;
}

279
280
281
282
283
284
285
286
287
static GObject *
glade_editor_property_constructor (GType                  type,
				   guint                  n_construct_properties,
				   GObjectConstructParam *construct_properties)
{
	GObject             *obj;
	GladeEditorProperty *eprop;
	gchar               *text;

288
	/* Invoke parent constructor (eprop->klass should be resolved by this point) . */
289
290
291
292
293
	obj = G_OBJECT_CLASS (table_class)->constructor
		(type, n_construct_properties, construct_properties);
	
	eprop = GLADE_EDITOR_PROPERTY (obj);

294
	/* Create label (make label visible but not the eventbox) */
295
	text = g_strdup_printf ("%s:", eprop->klass->name);
296
297
298
	eprop->item_label = gtk_label_new (text);
	eprop->eventbox   = gtk_event_box_new ();
	g_free (text);
299
	gtk_widget_show (eprop->item_label);
300

301
302
303
304
305
	/* Deal with label colours */
	glade_eprop_label_style_update_cb (eprop->item_label, NULL, eprop);
	g_signal_connect (G_OBJECT (eprop->item_label), "style-set",
			  G_CALLBACK (glade_eprop_label_style_update_cb), eprop);
	
306
307
308
	/* keep our own reference */
	g_object_ref (G_OBJECT (eprop->eventbox));

Tristan Van Berkom's avatar
Tristan Van Berkom committed
309
	gtk_misc_set_alignment (GTK_MISC (eprop->item_label), 1.0, 0.5);
310
311
312
313
	gtk_container_add (GTK_CONTAINER (eprop->eventbox), eprop->item_label);

	/* Create hbox and possibly check button
	 */
314
	if (eprop->klass->optional)
315
316
	{
		eprop->check = gtk_check_button_new ();
Tristan Van Berkom's avatar
Tristan Van Berkom committed
317
		gtk_widget_show (eprop->check);
318
319
320
321
322
		gtk_box_pack_start (GTK_BOX (eprop), eprop->check, FALSE, FALSE, 0);
		g_signal_connect (G_OBJECT (eprop->check), "toggled", 
				  G_CALLBACK (glade_editor_property_enabled_toggled_cb), eprop);

	}
323
324

	/* Create the class specific input widget and add it */
325
	eprop->input = GLADE_EDITOR_PROPERTY_GET_CLASS (eprop)->create_input (eprop);
326
	gtk_widget_show (eprop->input);
327
328
	gtk_box_pack_start (GTK_BOX (eprop), eprop->input, TRUE, TRUE, 0);

329
330
331
332
333
334
335
336
	/* Create the informational button and add it */
	eprop->info = glade_editor_property_create_info_button (eprop);
	g_signal_connect (G_OBJECT (eprop->info), "clicked", 
			  G_CALLBACK (glade_editor_property_info_clicked_cb), eprop);


	gtk_box_pack_start (GTK_BOX (eprop), eprop->info, FALSE, FALSE, 2);

337
338
339
340
341
342
343
344
345
	return obj;
}

static void
glade_editor_property_finalize (GObject *object)
{
	GladeEditorProperty *eprop = GLADE_EDITOR_PROPERTY (object);

	/* detatch from loaded property */
Tristan Van Berkom's avatar
Updated    
Tristan Van Berkom committed
346
	glade_editor_property_load_common (eprop, NULL);
347

348
349
350
351
352
	if (eprop->insensitive_colour)
		gdk_color_free (eprop->insensitive_colour);
	if (eprop->normal_colour)
		gdk_color_free (eprop->normal_colour);
	
353
354
355
356
357
358
359
360
361
362
363
364
365
366
	G_OBJECT_CLASS (table_class)->finalize (object);
}

static void
glade_editor_property_set_property (GObject      *object,
				    guint         prop_id,
				    const GValue *value,
				    GParamSpec   *pspec)
{
	GladeEditorProperty *eprop = GLADE_EDITOR_PROPERTY (object);

	switch (prop_id)
	{
	case PROP_PROPERTY_CLASS:
367
		eprop->klass = g_value_get_pointer (value);
368
369
370
371
		break;
	case PROP_USE_COMMAND:
		eprop->use_command = g_value_get_boolean (value);
		break;
372
373
374
375
376
377
	case PROP_SHOW_INFO:
		if (g_value_get_boolean (value))
			glade_editor_property_show_info (eprop);
		else
			glade_editor_property_hide_info (eprop);
		break;
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
glade_editor_property_get_property (GObject    *object,
				    guint       prop_id,
				    GValue     *value,
				    GParamSpec *pspec)
{
	GladeEditorProperty *eprop = GLADE_EDITOR_PROPERTY (object);

	switch (prop_id)
	{
	case PROP_PROPERTY_CLASS:
395
		g_value_set_pointer (value, eprop->klass);
396
397
398
399
		break;
	case PROP_USE_COMMAND:
		g_value_set_boolean (value, eprop->use_command);
		break;
400
401
	case PROP_SHOW_INFO:
		g_value_set_boolean (value, eprop->show_info);
402
403
404
405
406
407
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

408
409
410
411
412
413
414
415
416
417
418
419
420
static void
glade_eprop_property_finalized (GladeEditorProperty *eprop,
				GladeProperty       *where_property_was)
{
	eprop->tooltip_id   = 0;
	eprop->sensitive_id = 0;
	eprop->changed_id   = 0;
	eprop->enabled_id   = 0;
	eprop->property     = NULL;

	glade_editor_property_load (eprop, NULL);
}

421
422
423
424
static void
glade_editor_property_load_common (GladeEditorProperty *eprop, 
				   GladeProperty       *property)
{
425
426
427
428
429
430
431
432
433
434
435
436
437
	/* Hide properties that are removed for some particular widgets. 
	 */
	if (property)
	{
		gtk_widget_show (GTK_WIDGET (eprop));
		gtk_widget_show (eprop->eventbox);
	}
	else
	{
		gtk_widget_hide (GTK_WIDGET (eprop));
		gtk_widget_hide (eprop->eventbox);
	}

438
439
440
441
	/* disconnect anything from previously loaded property */
	if (eprop->property != property && eprop->property != NULL) 
	{
		if (eprop->tooltip_id > 0)
442
			g_signal_handler_disconnect (eprop->property,
443
444
445
446
447
						     eprop->tooltip_id);
		if (eprop->sensitive_id > 0)
			g_signal_handler_disconnect (eprop->property, 
						     eprop->sensitive_id);
		if (eprop->changed_id > 0)
448
			g_signal_handler_disconnect (eprop->property,
449
450
451
452
453
454
455
456
457
						     eprop->changed_id);
		if (eprop->enabled_id > 0)
			g_signal_handler_disconnect (eprop->property, 
						     eprop->enabled_id);

		eprop->tooltip_id   = 0;
		eprop->sensitive_id = 0;
		eprop->changed_id   = 0;
		eprop->enabled_id   = 0;
458
459
460
461
462
463

		/* Unref it here */
		g_object_weak_unref (G_OBJECT (eprop->property),
				     (GWeakNotify)glade_eprop_property_finalized,
				     eprop);

464
465
466
467
468
469

		/* For a reason I cant quite tell yet, this is the only
		 * safe way to nullify the property member of the eprop
		 * without leeking signal connections to properties :-/
		 */
		if (property == NULL) 
470
		{
471
			eprop->property = NULL;
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
	}

	/* Connect new stuff, deal with tooltip
	 */
	if (eprop->property != property && property != NULL)
	{
		eprop->property = property;

		eprop->tooltip_id = 
			g_signal_connect (G_OBJECT (eprop->property),
					  "tooltip-changed", 
					  G_CALLBACK (glade_editor_property_tooltip_cb),
					  eprop);
		eprop->sensitive_id =
			g_signal_connect (G_OBJECT (eprop->property),
					  "notify::sensitive", 
					  G_CALLBACK (glade_editor_property_sensitivity_cb),
					  eprop);
		eprop->changed_id =
			g_signal_connect (G_OBJECT (eprop->property),
					  "value-changed", 
					  G_CALLBACK (glade_editor_property_value_changed_cb),
					  eprop);
		eprop->enabled_id =
			g_signal_connect (G_OBJECT (eprop->property),
					  "notify::enabled", 
					  G_CALLBACK (glade_editor_property_enabled_cb),
					  eprop);
501
502
503
504
505
506
507

		/* In query dialogs when the user hits cancel, 
		 * these babies go away (so better stay protected).
		 */
		g_object_weak_ref (G_OBJECT (eprop->property),
				   (GWeakNotify)glade_eprop_property_finalized,
				   eprop);
508

509
510
511
512
513
514
515
516
		/* Load initial tooltips
		 */
		glade_editor_property_tooltip_cb
			(property, glade_property_get_tooltip (property), eprop);
		
		/* Load initial enabled state
		 */
		glade_editor_property_enabled_cb (property, NULL, eprop);
517
		
518
519
520
		/* Load initial sensitive state.
		 */
		glade_editor_property_sensitivity_cb (property, NULL, eprop);
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
	}
}

static void
glade_editor_property_init (GladeEditorProperty *eprop)
{

}

static void
glade_editor_property_class_init (GladeEditorPropertyClass *eprop_class)
{
	GObjectClass       *object_class;
	g_return_if_fail (eprop_class != NULL);

	/* Both parent classes assigned here.
	 */
	editor_property_class = eprop_class;
	table_class           = g_type_class_peek_parent (eprop_class);
	object_class          = G_OBJECT_CLASS (eprop_class);

	/* GObjectClass */
	object_class->constructor  = glade_editor_property_constructor;
	object_class->finalize     = glade_editor_property_finalize;
	object_class->get_property = glade_editor_property_get_property;
	object_class->set_property = glade_editor_property_set_property;

	/* Class methods */
	eprop_class->load          = glade_editor_property_load_common;
	eprop_class->create_input  = NULL;

552
553
554
555
556
557
558
559
560
	/**
	 * GladeEditorProperty::gtk-doc-search:
	 * @gladeeditor: the #GladeEditorProperty which received the signal.
	 * @arg1: the (#gchar *) book to search or %NULL
	 * @arg2: the (#gchar *) page to search or %NULL
	 * @arg3: the (#gchar *) search string or %NULL
	 *
	 * Emitted when the editor property requests that a doc-search be performed.
	 */
561
562
563
564
565
566
567
568
569
570
571
	glade_editor_property_signals[GTK_DOC_SEARCH] =
		g_signal_new ("gtk-doc-search",
			      G_TYPE_FROM_CLASS (object_class),
			      G_SIGNAL_RUN_LAST,
			      G_STRUCT_OFFSET (GladeEditorPropertyClass,
					       gtk_doc_search),
			      NULL, NULL,
			      glade_marshal_VOID__STRING_STRING_STRING,
			      G_TYPE_NONE, 3, 
			      G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);

572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
	/* Properties */
	g_object_class_install_property 
		(object_class, PROP_PROPERTY_CLASS,
		 g_param_spec_pointer 
		 ("property-class", _("Property Class"), 
		  _("The GladePropertyClass this GladeEditorProperty was created for"),
		  G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

	g_object_class_install_property 
		(object_class, PROP_USE_COMMAND,
		 g_param_spec_boolean 
		 ("use-command", _("Use Command"), 
		  _("Whether we should use the command API for the undo/redo stack"),
		  FALSE, G_PARAM_READWRITE));

587
588
589
590
591
592
	g_object_class_install_property 
		(object_class, PROP_SHOW_INFO,
		 g_param_spec_boolean 
		 ("show-info", _("Show Info"), 
		  _("Whether we should show an informational button"),
		  FALSE, G_PARAM_READWRITE));
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
}


GType
glade_editor_property_get_type (void)
{
	static GType property_type = 0;
	
	if (!property_type)
	{
		static const GTypeInfo property_info = 
		{
			sizeof (GladeEditorPropertyClass),
			(GBaseInitFunc) NULL,
			(GBaseFinalizeFunc) NULL,
			(GClassInitFunc) glade_editor_property_class_init,
			(GClassFinalizeFunc) NULL,
			NULL,           /* class_data */
			sizeof (GladeEditorProperty),
			0,              /* n_preallocs */
			(GInstanceInitFunc) glade_editor_property_init,
		};
		property_type = 
			g_type_register_static (GTK_TYPE_HBOX,
						"GladeEditorProperty",
						&property_info, 0);
	}
	return property_type;
}

/*******************************************************************************
                        GladeEditorPropertyNumericClass
 *******************************************************************************/
typedef struct {
	GladeEditorProperty  parent_instance;

	GtkWidget           *spin;
} GladeEPropNumeric;

GLADE_MAKE_EPROP (GladeEPropNumeric, glade_eprop_numeric)
#define GLADE_TYPE_EPROP_NUMERIC            (glade_eprop_numeric_get_type())
#define GLADE_EPROP_NUMERIC(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), GLADE_TYPE_EPROP_NUMERIC, GladeEPropNumeric))
#define GLADE_EPROP_NUMERIC_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GLADE_TYPE_EPROP_NUMERIC, GladeEPropNumericClass))
#define GLADE_IS_EPROP_NUMERIC(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GLADE_TYPE_EPROP_NUMERIC))
#define GLADE_IS_EPROP_NUMERIC_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GLADE_TYPE_EPROP_NUMERIC))
#define GLADE_EPROP_NUMERIC_GET_CLASS(o)    (G_TYPE_INSTANCE_GET_CLASS ((o), GLADE_EPROP_NUMERIC, GladeEPropNumericClass))

static void
glade_eprop_numeric_finalize (GObject *object)
{
	/* Chain up */
	G_OBJECT_CLASS (editor_property_class)->finalize (object);
}

static void
glade_eprop_numeric_load (GladeEditorProperty *eprop, GladeProperty *property)
{
	gfloat val = 0.0F;
	GladeEPropNumeric *eprop_numeric = GLADE_EPROP_NUMERIC (eprop);

	/* Chain up first */
	editor_property_class->load (eprop, property);

	if (property)
	{
658
		if (G_IS_PARAM_SPEC_INT(eprop->klass->pspec))
659
			val = (gfloat) g_value_get_int (property->value);
660
		else if (G_IS_PARAM_SPEC_UINT(eprop->klass->pspec))
661
			val = (gfloat) g_value_get_uint (property->value);		
662
		else if (G_IS_PARAM_SPEC_LONG(eprop->klass->pspec))
663
			val = (gfloat) g_value_get_long (property->value);		
664
		else if (G_IS_PARAM_SPEC_ULONG(eprop->klass->pspec))
665
			val = (gfloat) g_value_get_ulong (property->value);		
666
		else if (G_IS_PARAM_SPEC_INT64(eprop->klass->pspec))
667
			val = (gfloat) g_value_get_int64 (property->value);		
668
		else if (G_IS_PARAM_SPEC_UINT64(eprop->klass->pspec))
669
			val = (gfloat) g_value_get_uint64 (property->value);		
670
		else if (G_IS_PARAM_SPEC_DOUBLE(eprop->klass->pspec))
671
			val = (gfloat) g_value_get_double (property->value);
672
		else if (G_IS_PARAM_SPEC_FLOAT(eprop->klass->pspec))
673
674
675
			val = g_value_get_float (property->value);
		else
			g_warning ("Unsupported type %s\n",
676
				   g_type_name(G_PARAM_SPEC_TYPE (eprop->klass->pspec)));
677
678
679
680
681
682
683
684
685
686
687
688
689
		gtk_spin_button_set_value (GTK_SPIN_BUTTON (eprop_numeric->spin), val);
	}
}


static void
glade_eprop_numeric_changed (GtkWidget *spin,
			     GladeEditorProperty *eprop)
{
	GValue val = { 0, };

	if (eprop->loading) return;

690
	g_value_init (&val, eprop->klass->pspec->value_type);
691
	
692
	if (G_IS_PARAM_SPEC_INT(eprop->klass->pspec))
693
694
		g_value_set_int (&val, gtk_spin_button_get_value_as_int
				 (GTK_SPIN_BUTTON (spin)));
695
	else if (G_IS_PARAM_SPEC_UINT(eprop->klass->pspec))
696
697
		g_value_set_uint (&val, gtk_spin_button_get_value_as_int
				  (GTK_SPIN_BUTTON (spin)));
698
	else if (G_IS_PARAM_SPEC_LONG(eprop->klass->pspec))
699
700
		g_value_set_long (&val, (glong)gtk_spin_button_get_value_as_int
				  (GTK_SPIN_BUTTON (spin)));
701
	else if (G_IS_PARAM_SPEC_ULONG(eprop->klass->pspec))
702
703
		g_value_set_ulong (&val, (gulong)gtk_spin_button_get_value_as_int
				   (GTK_SPIN_BUTTON (spin)));
704
	else if (G_IS_PARAM_SPEC_INT64(eprop->klass->pspec))
705
706
		g_value_set_int64 (&val, (gint64)gtk_spin_button_get_value_as_int
				  (GTK_SPIN_BUTTON (spin)));
707
	else if (G_IS_PARAM_SPEC_UINT64(eprop->klass->pspec))
708
709
		g_value_set_uint64 (&val, (guint64)gtk_spin_button_get_value_as_int
				   (GTK_SPIN_BUTTON (spin)));
710
	else if (G_IS_PARAM_SPEC_FLOAT(eprop->klass->pspec))
711
712
		g_value_set_float (&val, (gfloat) gtk_spin_button_get_value
				   (GTK_SPIN_BUTTON (spin)));
713
	else if (G_IS_PARAM_SPEC_DOUBLE(eprop->klass->pspec))
714
715
716
717
		g_value_set_double (&val, gtk_spin_button_get_value
				    (GTK_SPIN_BUTTON (spin)));
	else
		g_warning ("Unsupported type %s\n",
718
			   g_type_name(G_PARAM_SPEC_TYPE (eprop->klass->pspec)));
719

720
	glade_editor_property_commit (eprop, &val);
721
722
723
724
725
726
727
728
729
	g_value_unset (&val);
}

static GtkWidget *
glade_eprop_numeric_create_input (GladeEditorProperty *eprop)
{
	GladeEPropNumeric *eprop_numeric = GLADE_EPROP_NUMERIC (eprop);
	GtkAdjustment     *adjustment;

730
	adjustment          = glade_property_class_make_adjustment (eprop->klass);
731
	eprop_numeric->spin = gtk_spin_button_new (adjustment, 4,
732
733
						   G_IS_PARAM_SPEC_FLOAT (eprop->klass->pspec) ||
						   G_IS_PARAM_SPEC_DOUBLE (eprop->klass->pspec)
734
						   ? 2 : 0);
735
736
	gtk_widget_show (eprop_numeric->spin);
	
737
738
739
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
773
774
775
776
777
778
779
780
781
	g_signal_connect (G_OBJECT (eprop_numeric->spin), "value_changed",
			  G_CALLBACK (glade_eprop_numeric_changed),
			  eprop);

	return eprop_numeric->spin;
}

/*******************************************************************************
                        GladeEditorPropertyEnumClass
 *******************************************************************************/
typedef struct {
	GladeEditorProperty  parent_instance;

	GtkWidget           *option_menu;
} GladeEPropEnum;

GLADE_MAKE_EPROP (GladeEPropEnum, glade_eprop_enum)
#define GLADE_TYPE_EPROP_ENUM            (glade_eprop_enum_get_type())
#define GLADE_EPROP_ENUM(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), GLADE_TYPE_EPROP_ENUM, GladeEPropEnum))
#define GLADE_EPROP_ENUM_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GLADE_TYPE_EPROP_ENUM, GladeEPropEnumClass))
#define GLADE_IS_EPROP_ENUM(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GLADE_TYPE_EPROP_ENUM))
#define GLADE_IS_EPROP_ENUM_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GLADE_TYPE_EPROP_ENUM))
#define GLADE_EPROP_ENUM_GET_CLASS(o)    (G_TYPE_INSTANCE_GET_CLASS ((o), GLADE_EPROP_ENUM, GladeEPropEnumClass))


static void
glade_eprop_enum_finalize (GObject *object)
{
	/* Chain up */
	G_OBJECT_CLASS (editor_property_class)->finalize (object);
}

static void
glade_eprop_enum_load (GladeEditorProperty *eprop, GladeProperty *property)
{
	GladeEPropEnum     *eprop_enum = GLADE_EPROP_ENUM (eprop);
	GEnumClass         *eclass;
	guint               i;
	gint                value;

	/* Chain up first */
	editor_property_class->load (eprop, property);

	if (property)
	{
782
		eclass = g_type_class_ref (eprop->klass->pspec->value_type);
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
		value  = g_value_get_enum (property->value);
		
		for (i = 0; i < eclass->n_values; i++)
			if (eclass->values[i].value == value)
				break;
		
		gtk_option_menu_set_history (GTK_OPTION_MENU (eprop_enum->option_menu),
					     i < eclass->n_values ? i : 0);
		g_type_class_unref (eclass);
	}
}

static void
glade_eprop_enum_changed (GtkWidget           *menu_item,
			  GladeEditorProperty *eprop)
{
	gint   ival;
	GValue val = { 0, };
	GladeProperty *property;

	if (eprop->loading) return;

	ival = GPOINTER_TO_INT(g_object_get_data (G_OBJECT (menu_item), GLADE_ENUM_DATA_TAG));

	property = eprop->property;

809
	g_value_init (&val, eprop->klass->pspec->value_type);
810
811
	g_value_set_enum (&val, ival);

812
	glade_editor_property_commit (eprop, &val);
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
	g_value_unset (&val);
}

static GtkWidget *
glade_editor_create_input_enum_item (GladeEditorProperty *eprop,
				     const gchar         *name,
				     gint                 value)
{
	GtkWidget *menu_item;

	menu_item = gtk_menu_item_new_with_label (name);

	g_signal_connect (G_OBJECT (menu_item), "activate",
			  G_CALLBACK (glade_eprop_enum_changed),
			  eprop);

	g_object_set_data (G_OBJECT (menu_item), GLADE_ENUM_DATA_TAG, GINT_TO_POINTER(value));

	return menu_item;
}

static GtkWidget *
glade_editor_create_input_stock_item (GladeEditorProperty *eprop,
				      const gchar         *id,
				      gint                 value)
{
	GtkWidget *menu_item = gtk_image_menu_item_new_from_stock (id, NULL);

	g_signal_connect (G_OBJECT (menu_item), "activate",
			  G_CALLBACK (glade_eprop_enum_changed),
			  eprop);

	g_object_set_data (G_OBJECT (menu_item), GLADE_ENUM_DATA_TAG, GINT_TO_POINTER(value));

	return menu_item;
}

static GtkWidget *
glade_eprop_enum_create_input (GladeEditorProperty *eprop)
{
	GladeEPropEnum      *eprop_enum = GLADE_EPROP_ENUM (eprop);
	GtkWidget           *menu_item, *menu;
855
	GladePropertyClass  *klass;
856
857
858
859
	GEnumClass          *eclass;
	gboolean             stock;
	guint                i;
	
860
861
862
863
	klass  = eprop->klass;
	eclass = g_type_class_ref (klass->pspec->value_type);
	stock  = (klass->pspec->value_type == GLADE_TYPE_STOCK) ||
		(klass->pspec->value_type == GLADE_TYPE_STOCK_IMAGE);
864
865
866
867
868

	menu = gtk_menu_new ();

	for (i = 0; i < eclass->n_values; i++)
	{
869
		const gchar *value_name = 
870
			glade_property_class_get_displayable_value
871
				(klass, eclass->values[i].value);
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
		if (value_name == NULL) value_name = eclass->values[i].value_name;
		
		if (stock && strcmp (eclass->values[i].value_nick, "glade-none"))
			menu_item = glade_editor_create_input_stock_item
				(eprop, 
				 eclass->values[i].value_nick, 
				 eclass->values[i].value);
		else
			menu_item = glade_editor_create_input_enum_item
				(eprop, value_name, eclass->values[i].value);

		gtk_widget_show (menu_item);
		gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
	}

	eprop_enum->option_menu = gtk_option_menu_new ();
	gtk_option_menu_set_menu (GTK_OPTION_MENU (eprop_enum->option_menu), menu);

890
891
	gtk_widget_show_all (eprop_enum->option_menu);

892
893
894
895
896
897
898
899
900
901
902
	g_type_class_unref (eclass);

	return eprop_enum->option_menu;
}

/*******************************************************************************
                        GladeEditorPropertyFlagsClass
 *******************************************************************************/
typedef struct {
	GladeEditorProperty  parent_instance;

903
	GtkTreeModel	*model;
Tristan Van Berkom's avatar
Updated    
Tristan Van Berkom committed
904
	GtkWidget       *entry;
905
906
907
908
909
910
911
912
913
914
915
916
917
} GladeEPropFlags;

GLADE_MAKE_EPROP (GladeEPropFlags, glade_eprop_flags)
#define GLADE_TYPE_EPROP_FLAGS            (glade_eprop_flags_get_type())
#define GLADE_EPROP_FLAGS(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), GLADE_TYPE_EPROP_FLAGS, GladeEPropFlags))
#define GLADE_EPROP_FLAGS_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GLADE_TYPE_EPROP_FLAGS, GladeEPropFlagsClass))
#define GLADE_IS_EPROP_FLAGS(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GLADE_TYPE_EPROP_FLAGS))
#define GLADE_IS_EPROP_FLAGS_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GLADE_TYPE_EPROP_FLAGS))
#define GLADE_EPROP_FLAGS_GET_CLASS(o)    (G_TYPE_INSTANCE_GET_CLASS ((o), GLADE_EPROP_FLAGS, GladeEPropFlagsClass))

static void
glade_eprop_flags_finalize (GObject *object)
{
918
919
920
921
	GladeEPropFlags *eprop_flags = GLADE_EPROP_FLAGS(object);

	g_object_unref (G_OBJECT (eprop_flags->model));

922
923
924
925
926
927
928
929
	/* Chain up */
	G_OBJECT_CLASS (editor_property_class)->finalize (object);
}

static void
glade_eprop_flags_load (GladeEditorProperty *eprop, GladeProperty *property)
{
	GladeEPropFlags *eprop_flags = GLADE_EPROP_FLAGS (eprop);
930
	GFlagsClass *klass;
931
	guint flag_num, value;
Tristan Van Berkom's avatar
Updated    
Tristan Van Berkom committed
932
	GString *string = g_string_new (NULL);
933
934
935
936

	/* Chain up first */
	editor_property_class->load (eprop, property);
	
Tristan Van Berkom's avatar
Updated    
Tristan Van Berkom committed
937
938
	gtk_list_store_clear(GTK_LIST_STORE(eprop_flags->model));

939
940
	if (property)
	{
941
		/* Populate the model with the flags. */
942
		klass = g_type_class_ref (G_VALUE_TYPE (property->value));
943
944
945
		value = g_value_get_flags (property->value);
			
		/* Step through each of the flags in the class. */
946
		for (flag_num = 0; flag_num < klass->n_values; flag_num++) {
947
948
949
			GtkTreeIter iter;
			guint mask;
			gboolean setting;
950
			const gchar *value_name;
951
			
952
			mask = klass->values[flag_num].value;
953
954
955
			setting = ((value & mask) == mask) ? TRUE : FALSE;
			
			value_name = glade_property_class_get_displayable_value
956
				(eprop->klass, klass->values[flag_num].value);
957

958
			if (value_name == NULL) value_name = klass->values[flag_num].value_name;
959
			
Tristan Van Berkom's avatar
Updated    
Tristan Van Berkom committed
960
961
962
963
			/* Setup string for property label */
			if (setting)
			{
				if (string->len > 0)
964
					g_string_append (string, " | ");
Tristan Van Berkom's avatar
Updated    
Tristan Van Berkom committed
965
966
				g_string_append (string, value_name);
			}
967
968
969
970
971
972
973
974
975
			
			/* Add a row to represent the flag. */
			gtk_list_store_append (GTK_LIST_STORE(eprop_flags->model), &iter);
			gtk_list_store_set (GTK_LIST_STORE(eprop_flags->model), &iter,
					    FLAGS_COLUMN_SETTING,
					    setting,
					    FLAGS_COLUMN_SYMBOL,
					    value_name,
					    -1);
Tristan Van Berkom's avatar
Updated    
Tristan Van Berkom committed
976

977
		}
978
		g_type_class_unref(klass);	
979
	}
Tristan Van Berkom's avatar
Updated    
Tristan Van Berkom committed
980

Tristan Van Berkom's avatar
Tristan Van Berkom committed
981
982
983
	gtk_entry_set_text (GTK_ENTRY (eprop_flags->entry), string->str);

	g_string_free (string, TRUE);
984
985
}

986

987
static void
988
989
990
flag_toggled_direct (GtkCellRendererToggle *cell,
		     gchar                 *path_string,
		     GladeEditorProperty   *eprop)
991
992
{
	GtkTreeIter iter;
993
994
995
996
997
	guint new_value;
	gboolean selected;
	guint value;
	value = new_value =0;
	gint flag_num=0;
998
	GFlagsClass *klass;
999

Tristan Van Berkom's avatar
Updated    
Tristan Van Berkom committed
1000
	GladeEPropFlags *eprop_flags = GLADE_EPROP_FLAGS(eprop);
1001

1002
1003
	if (!eprop->property)
		return ;
1004

1005
	klass = g_type_class_ref (G_VALUE_TYPE (eprop->property->value));
1006
1007
1008
1009
1010
1011
	value = g_value_get_flags (eprop->property->value);

	gtk_tree_model_get_iter_from_string (eprop_flags->model, &iter, path_string);

	gtk_tree_model_get (eprop_flags->model, &iter,
			    FLAGS_COLUMN_SETTING, &selected,
1012
1013
			    -1);

1014
	selected = selected ? FALSE : TRUE;
1015

1016
1017
	gtk_list_store_set (GTK_LIST_STORE (eprop_flags->model), &iter,
			    FLAGS_COLUMN_SETTING, selected,
1018
			    -1);
1019
1020
1021
1022
1023
1024
1025


	gtk_tree_model_get_iter_first (GTK_TREE_MODEL (eprop_flags->model), &iter);

	/* Step through each of the flags in the class, checking if
	   the corresponding toggle in the dialog is selected, If it
	   is, OR the flags' mask with the new value. */
1026
	for (flag_num = 0; flag_num < klass->n_values; flag_num++) {
1027
1028
1029
1030
1031
1032
1033
		gboolean setting;

		gtk_tree_model_get (GTK_TREE_MODEL (eprop_flags->model), &iter,
				    FLAGS_COLUMN_SETTING, &setting,
				    -1);

		if (setting)
1034
			new_value |= klass->values[flag_num].value;
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047

		gtk_tree_model_iter_next (GTK_TREE_MODEL (eprop_flags->model),
					  &iter);
	}

	/* If the new_value is different from the old value, we need
	   to update the property. */
	if (new_value != value) {
		GValue val = { 0, };

		g_value_init (&val, G_VALUE_TYPE (eprop->property->value));
		g_value_set_flags (&val, new_value);

1048
		glade_editor_property_commit (eprop, &val);
1049
1050
1051
1052
1053
		g_value_unset (&val);
	}



1054
1055
}

1056
1057
static GtkWidget*
glade_eprop_flags_create_treeview(GladeEditorProperty *eprop)
1058
1059
1060
1061
1062
{
	GtkWidget *scrolled_window;
	GtkWidget *tree_view;
	GtkTreeViewColumn *column;
	GtkCellRenderer *renderer;
1063
1064
1065
1066
	GladeEPropFlags *eprop_flags=GLADE_EPROP_FLAGS(eprop);
	if (!eprop_flags->model)
		eprop_flags->model = GTK_TREE_MODEL(gtk_list_store_new (2, G_TYPE_BOOLEAN, 
									G_TYPE_STRING));
1067
1068
1069
1070
1071
1072


	scrolled_window = gtk_scrolled_window_new (NULL, NULL);
	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
					GTK_POLICY_AUTOMATIC,
					GTK_POLICY_AUTOMATIC);
1073
1074
	gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_window), 
					     GTK_SHADOW_IN);
1075
1076
	gtk_widget_show (scrolled_window);

1077
	
1078

1079
	tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (eprop_flags->model));
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
	gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (tree_view), FALSE);
	gtk_widget_show (tree_view);
	gtk_container_add (GTK_CONTAINER (scrolled_window), tree_view);

	column = gtk_tree_view_column_new ();

	renderer = gtk_cell_renderer_toggle_new ();
	gtk_tree_view_column_pack_start (column, renderer, FALSE);
	gtk_tree_view_column_set_attributes (column, renderer,
					     "active", FLAGS_COLUMN_SETTING,
					     NULL);

	g_signal_connect (renderer, "toggled",
1093
1094
			  G_CALLBACK (flag_toggled_direct), eprop);

1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105

	renderer = gtk_cell_renderer_text_new ();
	gtk_tree_view_column_pack_start (column, renderer, TRUE);
	gtk_tree_view_column_set_attributes (column, renderer,
					     "text", FLAGS_COLUMN_SYMBOL,
					     NULL);

	gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), column);



1106
1107
	return scrolled_window;
}
1108

1109
1110
1111
1112
1113
static void
glade_eprop_flags_show_dialog (GtkWidget           *button,
			       GladeEditorProperty *eprop)
{
	GtkWidget *dialog;
1114
1115
1116
1117
1118
1119
1120
1121
1122
	GtkWidget *view;
	GtkWidget *label;
	GtkWidget *vbox;

	dialog = gtk_dialog_new_with_buttons (_("Select Fields"),
					      GTK_WINDOW (gtk_widget_get_toplevel (button)),
					      GTK_DIALOG_MODAL,
					      GTK_STOCK_CLOSE,
					      GTK_RESPONSE_CLOSE,
1123
					      NULL);
1124
					      
1125
	gtk_window_set_default_size (GTK_WINDOW (dialog), 300, 400);
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
	gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
	gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_CLOSE);

	/* HIG spacings */
	gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
	gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dialog)->vbox), 2); /* 2 * 5 + 2 = 12 */
	gtk_container_set_border_width (GTK_CONTAINER (GTK_DIALOG (dialog)->action_area), 5);
	gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dialog)->action_area), 6);

	vbox = gtk_vbox_new (FALSE, 6);
	gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);
1137

1138
1139
1140
1141
1142
1143
1144
1145
1146
	view = glade_eprop_flags_create_treeview (eprop);
	
	label = gtk_label_new_with_mnemonic (_("_Select individual fields:"));
	gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
	gtk_label_set_mnemonic_widget (GTK_LABEL (label), gtk_bin_get_child (GTK_BIN (view)));

	gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);	
	gtk_box_pack_start (GTK_BOX (vbox), view, TRUE, TRUE, 0);
	gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), vbox, TRUE, TRUE, 0);
1147

1148
1149
1150
1151
1152
	gtk_widget_show (label);
	gtk_widget_show (view);
	gtk_widget_show (vbox);			    
			    
	gtk_dialog_run (GTK_DIALOG (dialog));
1153
1154
1155
1156
1157
1158
1159
	gtk_widget_destroy (dialog);
}


static GtkWidget *
glade_eprop_flags_create_input (GladeEditorProperty *eprop)
{
Tristan Van Berkom's avatar
Updated    
Tristan Van Berkom committed
1160
1161
1162
1163
1164
1165
1166
	GtkWidget *vbox, *hbox, *button, *widget;
	GladeEPropFlags *eprop_flags = GLADE_EPROP_FLAGS(eprop);

	hbox   = gtk_hbox_new (FALSE, 0);
	vbox   = gtk_vbox_new (FALSE, 0);
	
	widget = glade_eprop_flags_create_treeview (eprop);
1167

Tristan Van Berkom's avatar
Updated    
Tristan Van Berkom committed
1168
1169
1170
1171
1172
1173
	eprop_flags->entry = gtk_entry_new ();
	gtk_entry_set_editable (GTK_ENTRY (eprop_flags->entry), FALSE);

	gtk_box_pack_start (GTK_BOX (vbox), eprop_flags->entry, FALSE, FALSE, 0);
	gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
	gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, TRUE, 0);
1174
1175
1176
1177

	button = gtk_button_new_with_label ("...");
	gtk_box_pack_start (GTK_BOX (hbox), button,  FALSE, FALSE, 0);

1178
1179
	gtk_widget_show_all (hbox);

1180
1181
1182
	g_signal_connect (G_OBJECT (button), "clicked",
			  G_CALLBACK (glade_eprop_flags_show_dialog),
			  eprop);
1183

1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
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
	return hbox;
}

/*******************************************************************************
                        GladeEditorPropertyColorClass
 *******************************************************************************/
typedef struct {
	GladeEditorProperty  parent_instance;

	GtkWidget           *cbutton;
	GtkWidget           *entry;
} GladeEPropColor;

GLADE_MAKE_EPROP (GladeEPropColor, glade_eprop_color)
#define GLADE_TYPE_EPROP_COLOR            (glade_eprop_color_get_type())
#define GLADE_EPROP_COLOR(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), GLADE_TYPE_EPROP_COLOR, GladeEPropColor))
#define GLADE_EPROP_COLOR_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GLADE_TYPE_EPROP_COLOR, GladeEPropColorClass))
#define GLADE_IS_EPROP_COLOR(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GLADE_TYPE_EPROP_COLOR))
#define GLADE_IS_EPROP_COLOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GLADE_TYPE_EPROP_COLOR))
#define GLADE_EPROP_COLOR_GET_CLASS(o)    (G_TYPE_INSTANCE_GET_CLASS ((o), GLADE_EPROP_COLOR, GladeEPropColorClass))


static void
glade_eprop_color_finalize (GObject *object)
{
	/* Chain up */
	G_OBJECT_CLASS (editor_property_class)->finalize (object);
}

static void
glade_eprop_color_load (GladeEditorProperty *eprop, GladeProperty *property)
{
	GladeEPropColor *eprop_color = GLADE_EPROP_COLOR (eprop);
	GdkColor        *color;
	gchar           *text;

	/* Chain up first */
	editor_property_class->load (eprop, property);
	
	if (property)
	{
1225
		if ((text = glade_property_class_make_string_from_gvalue
1226
		     (eprop->klass, property->value)) != NULL)
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
		{
			gtk_entry_set_text (GTK_ENTRY (eprop_color->entry), text);
			g_free (text);
		}
		else
			gtk_entry_set_text (GTK_ENTRY (eprop_color->entry), "");
			
		if ((color = g_value_get_boxed (property->value)) != NULL)
			gtk_color_button_set_color (GTK_COLOR_BUTTON (eprop_color->cbutton),
						    color);
		else
		{
			GdkColor black = { 0, };

			/* Manually fill it with black for an NULL value.
			 */
			if (gdk_color_parse("Black", &black) &&
			    gdk_colormap_alloc_color(gtk_widget_get_default_colormap(),
						     &black, FALSE, TRUE))
				gtk_color_button_set_color
					(GTK_COLOR_BUTTON (eprop_color->cbutton),
					 &black);
		}
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
	}
}

static void
glade_eprop_color_changed (GtkWidget *button,
			   GladeEditorProperty *eprop)
{
	GdkColor color = { 0, };
	GValue   value = { 0, };

	if (eprop->loading) return;

	gtk_color_button_get_color (GTK_COLOR_BUTTON (button), &color);

	g_value_init (&value, GDK_TYPE_COLOR);
	g_value_set_boxed (&value, &color);

1267
	glade_editor_property_commit (eprop, &value);
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
	g_value_unset (&value);
}

static GtkWidget *
glade_eprop_color_create_input (GladeEditorProperty *eprop)
{
	GladeEPropColor *eprop_color = GLADE_EPROP_COLOR (eprop);
	GtkWidget       *hbox;

	hbox = gtk_hbox_new (FALSE, 0);

	eprop_color->entry = gtk_entry_new ();
	gtk_entry_set_editable (GTK_ENTRY (eprop_color->entry), FALSE);
	gtk_widget_show (eprop_color->entry);
	gtk_box_pack_start (GTK_BOX (hbox), eprop_color->entry, TRUE, TRUE, 0);

	eprop_color->cbutton = gtk_color_button_new ();
	gtk_widget_show (eprop_color->cbutton);
	gtk_box_pack_start (GTK_BOX (hbox), eprop_color->cbutton,  FALSE, FALSE, 0);

	g_signal_connect (G_OBJECT (eprop_color->cbutton), "color-set",
			  G_CALLBACK (glade_eprop_color_changed), 
			  eprop);

	return hbox;
}

/*******************************************************************************
                        GladeEditorPropertyTextClass
 *******************************************************************************/
typedef struct {
	GladeEditorProperty  parent_instance;

	GtkWidget           *text_entry;
} GladeEPropText;

GLADE_MAKE_EPROP (GladeEPropText, glade_eprop_text)
#define GLADE_TYPE_EPROP_TEXT            (glade_eprop_text_get_type())
#define GLADE_EPROP_TEXT(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), GLADE_TYPE_EPROP_TEXT, GladeEPropText))
#define GLADE_EPROP_TEXT_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GLADE_TYPE_EPROP_TEXT, GladeEPropTextClass))
#define GLADE_IS_EPROP_TEXT(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GLADE_TYPE_EPROP_TEXT))
#define GLADE_IS_EPROP_TEXT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GLADE_TYPE_EPROP_TEXT))
#define GLADE_EPROP_TEXT_GET_CLASS(o)    (G_TYPE_INSTANCE_GET_CLASS ((o), GLADE_EPROP_TEXT, GladeEPropTextClass))

static void
glade_eprop_text_finalize (GObject *object)
{
	/* Chain up */
	G_OBJECT_CLASS (editor_property_class)->finalize (object);
}

static void
glade_eprop_text_load (GladeEditorProperty *eprop, GladeProperty *property)
{
	GladeEPropText *eprop_text = GLADE_EPROP_TEXT (eprop);

	/* Chain up first */
	editor_property_class->load (eprop, property);
	
	if (property == NULL) return;

1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
	if (GTK_IS_ENTRY (eprop_text->text_entry))
	{
		GtkEntry *entry = GTK_ENTRY (eprop_text->text_entry);
		const gchar *text = g_value_get_string (property->value);

		gtk_entry_set_text (entry, text ? text : "");
		
	}
	else if (GTK_IS_TEXT_VIEW (eprop_text->text_entry))
	{
1339
1340
1341
1342
		GtkTextBuffer  *buffer;
			
		buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (eprop_text->text_entry));

1343
1344
		if (G_VALUE_HOLDS (property->value, G_TYPE_STRV) ||
		    G_VALUE_HOLDS (property->value, G_TYPE_VALUE_ARRAY))
1345
		{
1346
			gchar *text = glade_property_class_make_string_from_gvalue (
1347
						property->klass, property->value);
1348
			gtk_text_buffer_set_text (buffer, text ? text : "", -1);
1349
			g_free (text);
1350
1351
1352
		}
		else
		{
1353
			const gchar *text = g_value_get_string (property->value);
1354
			gtk_text_buffer_set_text (buffer, text ? text : "", -1);
1355
		}
1356
1357
1358
	}
	else
	{
1359
1360
1361
1362
1363
		g_warning ("BUG! Invalid Text Widget type.");
	}
}

static void
1364
glade_eprop_text_changed_common (GladeEditorProperty *eprop,
1365
1366
1367
				 const gchar *text,
				 gboolean use_command)
{
1368
	GValue  *val;
1369
	gchar  *prop_text;
1370

1371
1372
	if (eprop->property->klass->pspec->value_type == G_TYPE_STRV ||
	    eprop->property->klass->pspec->value_type == G_TYPE_VALUE_ARRAY)
1373
	{
1374
		val = glade_property_class_make_gvalue_from_string 
1375
			(eprop->property->klass, text, NULL);
1376
1377
1378
	} 
	else
	{
1379
1380
1381
		val = g_new0 (GValue, 1);
		
		g_value_init (val, G_TYPE_STRING);
1382
1383
1384
1385
1386
1387
1388
1389

		glade_property_get (eprop->property, &prop_text);

		/* Here we try not to modify the project state by not 
		 * modifying a null value for an unchanged property.
		 */
		if (prop_text == NULL &&
		    text && text[0] == '\0')
1390
			g_value_set_string (val, NULL);
1391
1392
		else if (text == NULL &&
			 prop_text && prop_text == '\0')
1393
			g_value_set_string (val, "");
1394
		else
1395
			g_value_set_string (val, text);
1396
1397
	}

1398
1399
1400
	glade_editor_property_commit (eprop, val);
	g_value_unset (val);
	g_free (val);
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
}

static void
glade_eprop_text_changed (GtkWidget           *entry,
			  GladeEditorProperty *eprop)
{
	gchar *text;

	if (eprop->loading) return;
	
	text = gtk_editable_get_chars (GTK_EDITABLE (entry), 0, -1);
1412
1413
	glade_eprop_text_changed_common (eprop, text, eprop->use_command);
	
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
	g_free (text);
}

static gboolean
glade_eprop_text_entry_focus_out (GtkWidget           *entry,
				  GdkEventFocus       *event,
				  GladeEditorProperty *eprop)
{
	glade_eprop_text_changed (entry, eprop);
	return FALSE;
}

static gboolean
glade_eprop_text_text_view_focus_out (GtkTextView         *view,
				      GdkEventFocus       *event,
				      GladeEditorProperty *eprop)
{
	gchar *text;
	GtkTextBuffer *buffer;
	GtkTextIter    start, end;
	
	if (eprop->loading) return FALSE;

	buffer = gtk_text_view_get_buffer (view);

1439
1440
	gtk_text_buffer_get_start_iter (buffer, &start);
	gtk_text_buffer_get_end_iter (buffer, &end);
1441
1442
1443

	text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);

1444
	glade_eprop_text_changed_common (eprop, text, eprop->use_command);
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457

	g_free (text);
	return FALSE;
}

static void
glade_eprop_text_show_i18n_dialog (GtkWidget           *entry,
				   GladeEditorProperty *eprop)
{
	GtkWidget     *dialog;
	GtkWidget     *vbox, *hbox;
	GtkWidget     *label;
	GtkWidget     *sw;
1458
	GtkWidget     *alignment;
1459
1460
1461
1462
1463
1464
	GtkWidget     *text_view, *comment_view;
	GtkTextBuffer *text_buffer, *comment_buffer;
	GtkWidget     *translatable_button, *context_button;
	const gchar   *text;
	gint           res;
	gchar         *str;
1465
	GParamSpec    *pspec;
1466

1467
1468
1469
	dialog = gtk_dialog_new_with_buttons (_("Edit Text"),
					      GTK_WINDOW (gtk_widget_get_toplevel (entry)),
					      GTK_DIALOG_MODAL,
1470
1471
1472
1473
1474
					      GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
					      GTK_STOCK_OK, GTK_RESPONSE_OK,
					      NULL);

	gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
	gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);

	gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
						 GTK_RESPONSE_OK,
						 GTK_RESPONSE_CANCEL);

	/* HIG spacings */
	gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
	gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dialog)->vbox), 2); /* 2 * 5 + 2 = 12 */
	gtk_container_set_border_width (GTK_CONTAINER (GTK_DIALOG (dialog)->action_area), 5);
	gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dialog)->action_area), 6);

1487
1488

	vbox = gtk_vbox_new (FALSE, 6);
1489
	gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
	gtk_widget_show (vbox);

	gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), vbox, TRUE, TRUE, 0);

	/* Text */
	label = gtk_label_new_with_mnemonic (_("_Text:"));
	gtk_widget_show (label);
	gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
	gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);

	sw = gtk_scrolled_window_new (NULL, NULL);
	gtk_widget_show (sw);
	gtk_box_pack_start (GTK_BOX (vbox), sw, TRUE, TRUE, 0);
	gtk_widget_set_size_request (sw, 400, 200);
	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
					GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
	gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw), GTK_SHADOW_IN);

	text_view = gtk_text_view_new ();
	gtk_widget_show (text_view);

	gtk_label_set_mnemonic_widget (GTK_LABEL (label), text_view);
		
	gtk_container_add (GTK_CONTAINER (sw), text_view);

	text_buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));

	text = g_value_get_string (eprop->property->value);
	if (text)
	{
		gtk_text_buffer_set_text (text_buffer,
					  text,
					  -1);
	}
	
	/* Translatable and context prefix. */
	hbox = gtk_hbox_new (FALSE, 12);
	gtk_widget_show (hbox);

	gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);

	translatable_button = gtk_check_button_new_with_mnemonic (_("T_ranslatable"));
	gtk_widget_show (translatable_button);
	gtk_box_pack_start (GTK_BOX (hbox), translatable_button, FALSE, FALSE</