Initial Commit
[tilda-gobject.git] / tilda-window.c
1 #include "tilda-window.h"
2
3 static GObjectClass *parent_class = NULL;
4
5 enum tilda_window_properties {
6         TILDA_WINDOW_NUMBER = 1,
7 };
8
9 static void
10 tilda_window_instance_init (GTypeInstance *instance,
11                                                         gpointer       g_class)
12 {
13         TildaWindow *self = (TildaWindow *) instance;
14         self->dispose_has_run = FALSE;
15
16         /* Initialize all properties */
17         self->number = 0;
18 }
19
20 static void
21 tilda_window_set_property (GObject      *object,
22                                                    guint         property_id,
23                                                    const GValue *value,
24                                                    GParamSpec   *pspec)
25 {
26         TildaWindow *self = (TildaWindow *) object;
27
28         switch (property_id) {
29
30                 case TILDA_WINDOW_NUMBER:
31                         self->number = g_value_get_int (value);
32                         g_print ("window number: %d\n", self->number);
33                         break;
34
35                 default:
36                         /* We don't have this property */
37                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
38                         break;
39         }
40 }
41
42 static void
43 tilda_window_get_property (GObject    *object,
44                                                    guint       property_id,
45                                                    GValue     *value,
46                                                    GParamSpec *pspec)
47 {
48         TildaWindow *self = (TildaWindow *) object;
49
50         switch (property_id) {
51
52                 case TILDA_WINDOW_NUMBER:
53                         g_value_set_int (value, self->number);
54                         break;
55
56                 default:
57                         /* We don't have this property */
58                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
59                         break;
60         }
61 }
62
63 static GObject *
64 tilda_window_constructor (GType                  type,
65                                                   guint                  n_construct_properties,
66                                                   GObjectConstructParam *construct_properties)
67 {
68         GObject *obj;
69
70         /* Invoke parent constructor */
71         TildaWindowClass *klass;
72         klass = TILDA_WINDOW_CLASS (g_type_class_peek (TILDA_TYPE_WINDOW));
73         obj = parent_class->constructor (type,
74                                                                          n_construct_properties,
75                                                                          construct_properties);
76
77         /* Do other stuff here. The object is ready to go now, and all
78          * ctor properties have been set.
79          *
80          * TODO: This is the place to do DBus-init */
81
82         return obj;
83 }
84
85 static void
86 tilda_window_dispose (GObject *obj)
87 {
88         TildaWindow *self = (TildaWindow *) obj;
89
90         /* We don't want to run dispose twice, so just return immediately */
91         if (self->dispose_has_run)
92                 return;
93
94         /*
95          * In dispose, you are supposed to free all types referenced from this
96          * object which might themselves hold a reference to self. Generally,
97          * the most simple solution is to unref all members on which you own a
98          * reference.
99          */
100
101         /* Chain up to the parent class */
102         G_OBJECT_CLASS (parent_class)->dispose (obj);
103 }
104
105 static void
106 tilda_window_finalize (GObject *obj)
107 {
108         TildaWindow *self = (TildaWindow *) obj;
109
110         /*
111          * Here, complete the object's destruction.
112          * You might not need to do much...
113          */
114         // TODO: g_free() any primitives here
115
116
117         /* Chain up to the parent class */
118         G_OBJECT_CLASS (parent_class)->finalize (obj);
119 }
120
121 static void
122 tilda_window_class_init (gpointer g_class,
123                                                  gpointer g_class_data)
124 {
125         GObjectClass *gobject_class = G_OBJECT_CLASS (g_class);
126         TildaWindowClass *klass = TILDA_WINDOW_CLASS (g_class);
127         GParamSpec *pspec;
128
129         /* Hook our functions to this type */
130         gobject_class->set_property = tilda_window_set_property;
131         gobject_class->get_property = tilda_window_get_property;
132         gobject_class->dispose = tilda_window_dispose;
133         gobject_class->finalize = tilda_window_finalize;
134         gobject_class->constructor = tilda_window_constructor;
135
136         parent_class = g_type_class_peek_parent (klass);
137
138         /* Install all of the properties */
139         pspec = g_param_spec_int ("number",
140                                                           "Window number",
141                                                           "Set window's number",
142                                                           0,            // min value
143                                                           INT_MAX,      // max value
144                                                           0,            // def value
145                                                           G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
146
147         g_object_class_install_property (gobject_class,
148                                                                          TILDA_WINDOW_NUMBER,
149                                                                          pspec);
150
151         /* TODO: more properties */
152 }
153
154 GType
155 tilda_window_get_type (void)
156 {
157         static GType type = 0;
158
159         if (type == 0)
160         {
161                 static const GTypeInfo info = {
162                         sizeof (TildaWindowClass),
163                         NULL,   /* base_init */
164                         NULL,   /* base_finalize */
165                         tilda_window_class_init,        /* class_init */
166                         NULL,   /* class_finalize */
167                         NULL,   /* class_data */
168                         sizeof (TildaWindow),
169                         0,              /* n_preallocs */
170                         tilda_window_instance_init,     /* instance_init */
171                 };
172
173                 type = g_type_register_static (G_TYPE_OBJECT,
174                                                                            "TildaWindowType",
175                                                                            &info,
176                                                                            0);
177         }
178
179         return type;
180 }
181
182
183 int main (int argc, char *argv[])
184 {
185         GObject *tw;
186         gint test_number = INT_MIN;
187
188         /* Initialize the GObject type system */
189         g_type_init ();
190
191         tw = g_object_new (TILDA_TYPE_WINDOW, "number", 10, NULL);
192         g_object_get (G_OBJECT (tw), "number", &test_number, NULL);
193         g_assert (test_number == 10);
194
195         g_object_unref (G_OBJECT (tw));
196
197         tw = g_object_new (TILDA_TYPE_WINDOW, "number", 22, NULL);
198         g_object_get (G_OBJECT (tw), "number", &test_number, NULL);
199         g_assert (test_number == 22);
200
201         g_object_unref (G_OBJECT (tw));
202
203         return 0;
204 }
205
206 /* vim: set ts=4 sts=4 sw=4 noet tw=112: */
207