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