summaryrefslogtreecommitdiff
path: root/src/glade-property.c
blob: dc813981ece76c67e27da8a2c02d0a19f641792f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
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
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2001 Ximian, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 *
 * Authors:
 *   Chema Celorio <chema@celorio.com>
 */

#include <stdlib.h> /* for atoi and atof */
#include <string.h>

#include "glade.h"
#include "glade-choice.h"
#include "glade-widget.h"
#include "glade-property.h"
#include "glade-property-class.h"
#include "glade-parameter.h"
#include "glade-widget-class.h"


static void glade_property_class_init (GladePropertyObjectClass * klass);
static void glade_property_init (GladeProperty *property);
static void glade_property_destroy (GtkObject *object);

enum
{
	CHANGED,
	LAST_SIGNAL
};

static guint glade_property_signals[LAST_SIGNAL] = {0};
static GtkObjectClass *parent_class = NULL;

guint
glade_property_get_type (void)
{
	static guint type = 0;
  
	if (!type)
	{
		GtkTypeInfo info =
		{
			"GladeProperty",
			sizeof (GladeProperty),
			sizeof (GladePropertyObjectClass),
			(GtkClassInitFunc) glade_property_class_init,
			(GtkObjectInitFunc) glade_property_init,
			/* reserved_1 */ NULL,
			/* reserved_2 */ NULL,
			(GtkClassInitFunc) NULL,
		};
		
		type = gtk_type_unique (gtk_object_get_type (),
					&info);
	}
	
	return type;
}

static void
glade_property_class_init (GladePropertyObjectClass * klass)
{
	GtkObjectClass *object_class;

	object_class = (GtkObjectClass *) klass;

	parent_class = gtk_type_class (gtk_object_get_type ());

	glade_property_signals[CHANGED] =
		gtk_signal_new ("changed",
				GTK_RUN_LAST,
				GTK_CLASS_TYPE (object_class),
				GTK_SIGNAL_OFFSET (GladePropertyObjectClass, changed),
				gtk_marshal_VOID__VOID,
				GTK_TYPE_NONE, 0);
	
	klass->changed = NULL;

	object_class->destroy = glade_property_destroy;
}


static void
glade_property_init (GladeProperty *property)
{

	property->class = NULL;
	property->value = g_new0 (GValue, 1);
	property->enabled = TRUE;
	property->child = NULL;
}

static void
glade_property_destroy (GtkObject *object)
{
	GladeProperty *property;

	property = GLADE_PROPERTY (object);
}

static GladeProperty *
glade_property_new (void)
{
	GladeProperty *property;

	property = GLADE_PROPERTY (gtk_type_new (glade_property_get_type ()));

	return property;
}


/* We are recursing so add the prototype. Don't you love C ? */
static GList * glade_property_list_new_from_list (GList *list, GladeWidget *widget);

GladeProperty *
glade_property_new_from_class (GladePropertyClass *class, GladeWidget *widget)
{
	GladeProperty *property;
	property = glade_property_new ();
	
	property->class = class;

	if (class->type == GLADE_PROPERTY_TYPE_OBJECT) {
		property->child = glade_widget_new_from_class (class->child,
							       widget);
		return property;
	}
	
	/* Create an empty default if the class does not specify a default value */
	if (!class->def) {
		property->value = glade_property_class_make_gvalue_from_string (class, "");
		return property;
	}
			
	switch (class->type) {
	case GLADE_PROPERTY_TYPE_DOUBLE:
	case GLADE_PROPERTY_TYPE_INTEGER:
	case GLADE_PROPERTY_TYPE_FLOAT:
		property->enabled = class->optional_default;
		/* Fall thru */
	case GLADE_PROPERTY_TYPE_ENUM:
	case GLADE_PROPERTY_TYPE_BOOLEAN:
	case GLADE_PROPERTY_TYPE_STRING:
		g_value_init (property->value, class->def->g_type);
		g_value_copy (class->def, property->value);
		break;
	case GLADE_PROPERTY_TYPE_OTHER_WIDGETS:
#if 0	
		value = g_strdup ("");
#endif	
		break;
	case GLADE_PROPERTY_TYPE_OBJECT:
		break;
	case GLADE_PROPERTY_TYPE_ERROR:
		g_warning ("Invalid Glade property type (%d)\n", class->type);
		break;
	}

	return property;
}

static GList *
glade_property_list_new_from_list (GList *list, GladeWidget *widget)
{
	GladePropertyClass *property_class;
	GladeProperty *property;
	GList *new_list = NULL;

	for (; list != NULL; list = list->next) {
		property_class = list->data;
		property = glade_property_new_from_class (property_class, widget);
		if (property == NULL)
			continue;

		property->widget = widget;

		new_list = g_list_prepend (new_list, property);
	}

	new_list = g_list_reverse (new_list);
	
	return new_list;
}


GList *
glade_property_list_new_from_widget_class (GladeWidgetClass *class,
					   GladeWidget *widget)
{
	GList *list = NULL;

	list = class->properties;

	return glade_property_list_new_from_list (list, widget);
}


GladeProperty *
glade_property_get_from_id (GList *settings_list, const gchar *id)
{
	GList *list;
	GladeProperty *property;

	g_return_val_if_fail (id != NULL, NULL);
	
	list = settings_list;
	for (; list != NULL; list = list->next) {
		property = list->data;
		g_return_val_if_fail (property, NULL);
		g_return_val_if_fail (property->class, NULL);
		g_return_val_if_fail (property->class->id, NULL);
		if (strcmp (property->class->id, id) == 0)
			return property;
	}

	return NULL;
}

static void
glade_property_emit_changed (GladeProperty *property)
{
	gtk_signal_emit (GTK_OBJECT (property),
			 glade_property_signals [CHANGED]);
}

void
glade_property_set_string (GladeProperty *property,
			   const gchar *text)
{
	g_return_if_fail (property != NULL);
	g_return_if_fail (property->value != NULL);
	g_return_if_fail (property->widget != NULL);
	g_return_if_fail (property->widget->widget != NULL);
	g_return_if_fail (text != NULL);

	if (!g_value_get_string (property->value) ||
	    (strcmp (text, g_value_get_string (property->value)) != 0))
		g_value_set_string (property->value, text);

	if (property->enabled) {
		property->loading = TRUE;
		if (property->class->set_function == NULL)
			g_object_set (G_OBJECT (property->widget->widget),
				      property->class->id,
				      g_value_get_string (property->value), NULL);
		else
			(*property->class->set_function) (G_OBJECT (property->widget->widget),
							  property->value);
		property->loading = FALSE;
	}

	glade_property_emit_changed (property);
}

void
glade_property_set_integer (GladeProperty *property, gint val)
{
	g_return_if_fail (property != NULL);
	g_return_if_fail (property->value != NULL);

	g_value_set_int (property->value, val);

	if (property->enabled) {
		property->loading = TRUE;
		if (property->class->set_function == NULL)
			gtk_object_set (GTK_OBJECT (property->widget->widget),
					property->class->id, val, NULL);
		else
			(*property->class->set_function) (G_OBJECT (property->widget->widget),
							  property->value);
		property->loading = FALSE;
	}

	glade_property_emit_changed (property);
}

void
glade_property_set_float (GladeProperty *property, gfloat val)
{
	g_return_if_fail (property != NULL);
	g_return_if_fail (property->value != NULL);

	g_value_set_float (property->value, val);

	if (property->enabled) {
		property->loading = TRUE;
		if (property->class->set_function == NULL)
			gtk_object_set (GTK_OBJECT (property->widget->widget),
					property->class->id, val, NULL);
		else
			(*property->class->set_function) (G_OBJECT (property->widget->widget),
							  property->value);
		property->loading = FALSE;
	}

	glade_property_emit_changed (property);
}

void
glade_property_set_double (GladeProperty *property, gdouble val)
{
	g_return_if_fail (property != NULL);
	g_return_if_fail (property->value != NULL);

	g_value_set_double (property->value, val);

	if (property->enabled) {
		property->loading = TRUE;
		if (property->class->set_function == NULL)
			gtk_object_set (GTK_OBJECT (property->widget->widget),
					property->class->id, val, NULL);
		else
			(*property->class->set_function) (G_OBJECT (property->widget->widget),
							  property->value);
		property->loading = FALSE;
	}

	glade_property_emit_changed (property);
#ifdef DEBUG
	if (GTK_IS_SPIN_BUTTON (property->widget->widget)) {
		g_debug ("It is spin button\n");
		g_debug ("The alignement is min :%g max:%g value%g\n",
			 GTK_SPIN_BUTTON (property->widget->widget)->adjustment->lower,
			 GTK_SPIN_BUTTON (property->widget->widget)->adjustment->upper,
			 GTK_SPIN_BUTTON (property->widget->widget)->adjustment->value);
		gtk_spin_button_set_value (GTK_SPIN_BUTTON (property->widget->widget),
					   333.22);
	}
#endif	
}

void
glade_property_set_boolean (GladeProperty *property, gboolean val)
{
	g_return_if_fail (property != NULL);
	g_return_if_fail (property->value != NULL);
	g_return_if_fail (property->widget != NULL);
	g_return_if_fail (property->widget->widget != NULL);

	g_value_set_boolean (property->value, val);

	if (property->enabled) {
		property->loading = TRUE;
		if (property->class->set_function == NULL) {
			if (GTK_IS_TABLE (property->widget->widget)) {
				g_print ("Is table \n");
				gtk_widget_queue_resize (GTK_WIDGET (property->widget->widget));
#if 0	
				gtk_table_set_homogeneous (property->widget->widget, val);
#endif	
			} else {
				gtk_object_set (GTK_OBJECT (property->widget->widget),
						property->class->id,
						val,
						NULL);
			}
		}
		else
			(*property->class->set_function) (G_OBJECT (property->widget->widget),
							  property->value);
		property->loading = FALSE;
	}

	glade_property_emit_changed (property);
}

void
glade_property_set_enum (GladeProperty *property, GladeChoice *choice)
{
	g_return_if_fail (property != NULL);
	g_return_if_fail (property->value != NULL);
	g_return_if_fail (choice != NULL);


	g_value_set_enum (property->value, choice->value);

	property->loading = TRUE;
	if (property->class->set_function == NULL)
		gtk_object_set (GTK_OBJECT (property->widget->widget),
				property->class->id, choice->value, NULL);
	else
		(*property->class->set_function) (G_OBJECT (property->widget->widget),
						  property->value);
	property->loading = FALSE;

	glade_property_emit_changed (property);
}
	
void
glade_property_set (GladeProperty *property, GValue *value)
{
	switch (property->class->type) {
	case GLADE_PROPERTY_TYPE_BOOLEAN:
		glade_property_set_boolean (property,
					    g_value_get_boolean (value));
		break;
	case GLADE_PROPERTY_TYPE_FLOAT:
		g_print ("1 %s \n", property->class->name);
		glade_property_set_float (property,
					  g_value_get_float (value));
		g_print ("End:1\n");
		break;
	case GLADE_PROPERTY_TYPE_INTEGER:
		glade_property_set_integer (property,
					    g_value_get_int (value));
		break;
	case GLADE_PROPERTY_TYPE_DOUBLE:
		glade_property_set_double (property,
					   g_value_get_double (value));
		break;
	case GLADE_PROPERTY_TYPE_STRING:
		glade_property_set_string (property,
					   g_value_get_string (value));
		break;
	case GLADE_PROPERTY_TYPE_ENUM:
		{
			GladeChoice *choice = NULL;
			GList * list = NULL;

			list = property->class->choices;
			for (; list != NULL; list = list->next) {
				choice = list->data;
				if (choice->value == g_value_get_enum (value))
					break;
			}
			if (list == NULL)
				g_warning ("Cant find the GladePropertyChoice "
					   "for the #%d\n", g_value_get_int (value));
			else
				glade_property_set_enum (property, choice);
		}
		break;
	case GLADE_PROPERTY_TYPE_OBJECT:
		glade_implement_me ();
		g_print ("Set adjustment ->%d<-\n", GPOINTER_TO_INT (property->child));
#if 1	
		g_print ("Set directly \n");
#if 0
		glade_widget_set_default_options_real (property->child, packing);
#endif	
		gtk_spin_button_set_adjustment (GTK_SPIN_BUTTON (property->widget->widget),
						GTK_ADJUSTMENT (property->child));
#else		
		gtk_object_set (GTK_OBJECT (property->widget->widget),
				property->class->id,
				property->child, NULL);
#endif		
		g_print ("Adjustment has been set\n");
		break;
	default:
		g_warning ("Implement set default for this type [%s]\n", property->class->name);
		break;
	}
	
}

const gchar *
glade_property_get_string (GladeProperty *property)
{
	g_return_val_if_fail (property != NULL, NULL);
	g_return_val_if_fail (property->value != NULL, NULL);

	return g_value_get_string (property->value);
}
	
gint
glade_property_get_integer (GladeProperty *property)
{
	g_return_val_if_fail (property != NULL, 0);
	g_return_val_if_fail (property->value != NULL, 0);

	return g_value_get_int (property->value);	
}

gfloat
glade_property_get_float (GladeProperty *property)
{
	gfloat resp;
	g_return_val_if_fail (property != NULL, 0.0);
	g_return_val_if_fail (property->value != NULL, 0.0);

	resp = g_value_get_float (property->value);

	return resp;
}
	
gdouble
glade_property_get_double (GladeProperty *property)
{
	g_return_val_if_fail (property != NULL, 0.0);
	g_return_val_if_fail (property->value != NULL, 0.0);
	
	return g_value_get_double (property->value);
}

gboolean
glade_property_get_boolean (GladeProperty *property)
{
	g_return_val_if_fail (property != NULL, FALSE);
	g_return_val_if_fail (property->value != NULL, FALSE);

	return g_value_get_boolean (property->value);
}	
	
GladeChoice *
glade_property_get_enum (GladeProperty *property)
{
	GladeChoice *choice = NULL;
	GList *list;
	gint value;
	
	g_return_val_if_fail (property != NULL, NULL);
	g_return_val_if_fail (property->value != NULL, NULL);

	value = g_value_get_enum (property->value);
	list = property->class->choices;
	for (; list != NULL; list = list->next) {
		choice = list->data;
		if (choice->value == value)
			break;
	}
	if (list == NULL)
		g_warning ("Cant find the GladePropertyChoice selected\n");

	return choice;
}

void
glade_property_query_result_set_int (GladePropertyQueryResult *result,
				     const gchar *key,
				     gint value)
{
	g_return_if_fail (result != NULL);
	g_return_if_fail (key != NULL);

	g_hash_table_insert (result->hash, (gchar *)key,
			     GINT_TO_POINTER (value));

}

void
glade_property_query_result_get_int (GladePropertyQueryResult *result,
				     const gchar *key,
				     gint *return_value)
{
	g_return_if_fail (result != NULL);
	g_return_if_fail (key != NULL);

	*return_value = GPOINTER_TO_INT (g_hash_table_lookup (result->hash, key));
}

GladePropertyQueryResult *
glade_property_query_result_new (void)
{
	GladePropertyQueryResult *result;

	result = g_new0 (GladePropertyQueryResult, 1);
	result->hash = g_hash_table_new (g_str_hash, g_str_equal);

	return result;
}

void
glade_property_query_result_destroy (GladePropertyQueryResult *result)
{
	g_return_if_fail (result != NULL);

	g_hash_table_destroy (result->hash);
	result->hash = NULL;
	
	g_free (result);
}

GladeXmlNode *
glade_property_write (GladeXmlContext *context, GladeProperty *property)
{
	GladeXmlNode *node;
	gchar *temp;

	if (!property->enabled)
		return NULL;

	node = glade_xml_node_new (context, GLADE_XML_TAG_PROPERTY);
	glade_xml_node_set_property_string (node, GLADE_XML_TAG_NAME, property->class->id);
	temp = glade_property_class_make_string_from_gvalue (property->class,
							     property->value);
	glade_xml_set_content (node, temp);
	g_free (temp);
	
	return node;
}
	
void
glade_property_free (GladeProperty *property)
{
	property->class = NULL;
	property->widget = NULL;
	if (property->value)
		g_free (property->value);
	property->value = NULL;

	if (property->child)
		g_warning ("Implmenet free property->child\n");

	g_free (property);
}


void
glade_property_get_from_widget (GladeProperty *property)
{
	gboolean bool = FALSE;
	
	g_value_reset (property->value);

	if (property->class->get_function)
		(*property->class->get_function) (G_OBJECT (property->widget->widget), property->value);
	else {
		switch (property->class->type) {
		case GLADE_PROPERTY_TYPE_BOOLEAN:
			gtk_object_get (GTK_OBJECT (property->widget->widget),
					property->class->id,
					&bool,
					NULL);
			g_value_set_boolean (property->value, bool);
			break;
		default:
			glade_implement_me ();
			break;
		}
	}
}