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