Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/libccc/0.0.4/demo/herzi-enum-model.c
blob: 4d4114c17d4fb3ee9a66d149e66e5981b4b21b6b (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
/* This file is part of libccc
 *
 * AUTHORS
 *     Sven Herzberg  <herzi@gnome-de.org>
 *
 * Copyright (C) 2006  Sven Herzberg <herzi@gnome-de.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser 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
 */

#include "herzi-enum-model.h"

#include "glib-helpers.h"

struct _HerziEnumModel {
	GtkListStore base_instance;
	GType type;
};

GtkTreeModel*
herzi_enum_model_new(GType enum_type) {
	g_return_val_if_fail(g_type_is_a(enum_type, G_TYPE_ENUM), NULL);

	return g_object_new(HERZI_TYPE_ENUM_MODEL, "type", enum_type, NULL);
}

GType
herzi_enum_model_get_gtype(HerziEnumModel const* self) {
	g_return_val_if_fail(HERZI_IS_ENUM_MODEL(self), G_TYPE_INVALID);

	return self->type;
}

/* GType stuff */
G_DEFINE_TYPE(HerziEnumModel, herzi_enum_model, GTK_TYPE_LIST_STORE);

enum {
	PROP_0,
	PROP_TYPE
};

static void
herzi_enum_model_init(HerziEnumModel* self) {
	GType columns[HERZI_ENUM_MODEL_N_COLUMNS];
	columns[HERZI_ENUM_MODEL_COL_VALUE] = G_TYPE_INT;
	columns[HERZI_ENUM_MODEL_COL_NAME]  = G_TYPE_STRING;
	columns[HERZI_ENUM_MODEL_COL_NICK]  = G_TYPE_STRING;

	gtk_list_store_set_column_types(GTK_LIST_STORE(self), HERZI_ENUM_MODEL_N_COLUMNS, columns);
}

static GObject*
hem_constructor(GType type, guint n_properties, GObjectConstructParam* properties) {
	// FIXME: check the type first
	GObject* object = G_OBJECT_CLASS(herzi_enum_model_parent_class)->constructor(type, n_properties, properties);
	HerziEnumModel* self = HERZI_ENUM_MODEL(object);
	GEnumClass* klass = g_type_class_ref(self->type);
	guint i;

	for(i = 0; i < klass->n_values; i++) {
		GtkTreeIter iter;
		gtk_list_store_append(GTK_LIST_STORE(self), &iter);
		gtk_list_store_set(GTK_LIST_STORE(self), &iter,
				   HERZI_ENUM_MODEL_COL_VALUE, klass->values[i].value,
				   HERZI_ENUM_MODEL_COL_NAME,  klass->values[i].value_name,
				   HERZI_ENUM_MODEL_COL_NICK,  klass->values[i].value_nick,
				   -1);
	}

	g_type_class_unref(klass);

	return object;
}

static void
hem_get_property(GObject* object, guint prop_id, GValue* value, GParamSpec* pspec) {
	HerziEnumModel* self = HERZI_ENUM_MODEL(object);

	switch(prop_id) {
	case PROP_TYPE:
		g_value_set_gtype(value, herzi_enum_model_get_gtype(self));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
		break;
	}
}

static void
hem_set_property(GObject* object, guint prop_id, GValue const* value, GParamSpec* pspec) {
	HerziEnumModel* self = HERZI_ENUM_MODEL(object);

	switch(prop_id) {
	case PROP_TYPE:
		self->type = g_value_get_gtype(value);
		g_object_notify(object, "type");
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
		break;
	}
}

static void
herzi_enum_model_class_init(HerziEnumModelClass* self_class) {
	GObjectClass* go_class;

	/* GObjectClass */
	go_class = G_OBJECT_CLASS(self_class);
	go_class->constructor  = hem_constructor;
	go_class->get_property = hem_get_property;
	go_class->set_property = hem_set_property;

	g_object_class_install_property(go_class,
					PROP_TYPE,
					g_param_spec_gtype("type",
							   "Enum GType",
							   "Blurb",
							   G_TYPE_ENUM,
							   G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
}