Hook TildaTerminal and TildaWindow together
[tilda-gobject.git] / tilda-window.c
1 #include "tilda-window.h"
2
3 static gboolean
4 tilda_window_add_term (TildaWindow *tw)
5 {
6         // FIXME: this is totally bad, but it's a good hack for feasability
7         static gint mynumber = 0;
8         TildaTerminal *tt = g_object_new (TILDA_TYPE_TERMINAL, "number", mynumber++, NULL);
9         g_ptr_array_add (tw->terms, tt);
10
11         GtkWidget *label = gtk_label_new ("Tilda");
12         gint index = gtk_notebook_prepend_page (GTK_NOTEBOOK(tw->notebook), tt->hbox, label);
13         gtk_notebook_set_tab_label_packing (GTK_NOTEBOOK(tw->notebook), tt->hbox, TRUE, TRUE, GTK_PACK_END);
14         //gtk_notebook_set_current_page (GTK_NOTEBOOK(tw->notebook), index);
15
16         if (gtk_notebook_get_n_pages (GTK_NOTEBOOK(tw->notebook)) > 1)
17                 gtk_notebook_set_show_tabs (GTK_NOTEBOOK(tw->notebook), TRUE);
18
19         return TRUE;
20 }
21
22 static gboolean
23 tilda_window_remove_term (TildaWindow *tw, int number)
24 {
25         int i;
26
27         for (i=0; i<tw->terms->len; ++i)
28         {
29                 TildaTerminal *tt = g_ptr_array_index (tw->terms, i);
30
31                 if (tt->number == number)
32                         g_print ("Need to remove window %d terminal %d\n", tw->number, tt->number);
33         }
34
35         return TRUE;
36 }
37
38 /*******************************************************************************
39  * ALL GOBJECT STUFF BELOW PLEASE
40  ******************************************************************************/
41
42 static GObjectClass *parent_class = NULL;
43
44 enum tilda_window_properties {
45         TILDA_WINDOW_NUMBER = 1,
46 };
47
48 static void
49 tilda_window_instance_init (GTypeInstance *instance,
50                                                         gpointer       g_class)
51 {
52         TildaWindow *self = (TildaWindow *) instance;
53         self->dispose_has_run = FALSE;
54
55         /* Initialize all properties */
56         self->window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
57         self->notebook = gtk_notebook_new ();
58         self->terms = g_ptr_array_new ();
59
60         /* Somewhat of a "poison" value, incase we don't set this */
61         self->number = 0xdeadbeef;
62 }
63
64 static void
65 tilda_window_set_property (GObject      *object,
66                                                    guint         property_id,
67                                                    const GValue *value,
68                                                    GParamSpec   *pspec)
69 {
70         TildaWindow *self = (TildaWindow *) object;
71
72         switch (property_id) {
73
74                 case TILDA_WINDOW_NUMBER:
75                         self->number = g_value_get_int (value);
76                         g_print ("window number: %d\n", self->number);
77                         break;
78
79                 default:
80                         /* We don't have this property */
81                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
82                         break;
83         }
84 }
85
86 static void
87 tilda_window_get_property (GObject    *object,
88                                                    guint       property_id,
89                                                    GValue     *value,
90                                                    GParamSpec *pspec)
91 {
92         TildaWindow *self = (TildaWindow *) object;
93
94         switch (property_id) {
95
96                 case TILDA_WINDOW_NUMBER:
97                         g_value_set_int (value, self->number);
98                         break;
99
100                 default:
101                         /* We don't have this property */
102                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
103                         break;
104         }
105 }
106
107 static GObject *
108 tilda_window_constructor (GType                  type,
109                                                   guint                  n_construct_properties,
110                                                   GObjectConstructParam *construct_properties)
111 {
112         GObject *obj;
113         TildaWindow *self;
114
115         /* Invoke parent constructor */
116         TildaWindowClass *klass;
117         klass = TILDA_WINDOW_CLASS (g_type_class_peek (TILDA_TYPE_WINDOW));
118         obj = parent_class->constructor (type,
119                                                                          n_construct_properties,
120                                                                          construct_properties);
121
122         /* Do other stuff here. The object is ready to go now, and all
123          * ctor properties have been set.
124          *
125          * TODO: This is the place to do DBus-init */
126         self = TILDA_WINDOW(obj);
127
128         gtk_container_add (GTK_CONTAINER(self->window), self->notebook);
129         gtk_widget_show (self->notebook);
130
131         tilda_window_add_term (self);
132         gtk_widget_show_all (self->window);
133
134         return obj;
135 }
136
137 static void
138 my_unref (gpointer data, gpointer user_data)
139 {
140         g_object_unref (G_OBJECT(data));
141 }
142
143 static void
144 tilda_window_dispose (GObject *obj)
145 {
146         TildaWindow *self = (TildaWindow *) obj;
147
148         /* We don't want to run dispose twice, so just return immediately */
149         if (self->dispose_has_run)
150                 return;
151
152         /*
153          * In dispose, you are supposed to free all types referenced from this
154          * object which might themselves hold a reference to self. Generally,
155          * the most simple solution is to unref all members on which you own a
156          * reference.
157          *
158          * NOTE: See the following for how to deal with GtkObject-derived things:
159          * http://library.gnome.org/devel/gtk/unstable/GtkObject.html
160          */
161         g_ptr_array_foreach (self->terms, my_unref, NULL);
162         gtk_widget_destroy (self->window);
163
164         /* Chain up to the parent class */
165         G_OBJECT_CLASS (parent_class)->dispose (obj);
166 }
167
168 static void
169 tilda_window_finalize (GObject *obj)
170 {
171         TildaWindow *self = (TildaWindow *) obj;
172
173         /*
174          * Here, complete the object's destruction.
175          * You might not need to do much...
176          */
177         // TODO: g_free() any primitives here
178         g_ptr_array_free (self->terms, TRUE);
179
180
181         /* Chain up to the parent class */
182         G_OBJECT_CLASS (parent_class)->finalize (obj);
183 }
184
185 static void
186 tilda_window_class_init (gpointer g_class,
187                                                  gpointer g_class_data)
188 {
189         GObjectClass *gobject_class = G_OBJECT_CLASS (g_class);
190         TildaWindowClass *klass = TILDA_WINDOW_CLASS (g_class);
191         GParamSpec *pspec;
192
193         /* Hook our functions to this type */
194         gobject_class->set_property = tilda_window_set_property;
195         gobject_class->get_property = tilda_window_get_property;
196         gobject_class->dispose = tilda_window_dispose;
197         gobject_class->finalize = tilda_window_finalize;
198         gobject_class->constructor = tilda_window_constructor;
199
200         parent_class = g_type_class_peek_parent (klass);
201
202         /* Install all of the properties */
203         pspec = g_param_spec_int ("number",
204                                                           "Window number",
205                                                           "Set window's number",
206                                                           0,            // min value
207                                                           INT_MAX,      // max value
208                                                           0,            // def value
209                                                           G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
210
211         g_object_class_install_property (gobject_class,
212                                                                          TILDA_WINDOW_NUMBER,
213                                                                          pspec);
214
215         /* TODO: more properties */
216 }
217
218 GType
219 tilda_window_get_type (void)
220 {
221         static GType type = 0;
222
223         if (type == 0)
224         {
225                 static const GTypeInfo info = {
226                         sizeof (TildaWindowClass),
227                         NULL,   /* base_init */
228                         NULL,   /* base_finalize */
229                         tilda_window_class_init,        /* class_init */
230                         NULL,   /* class_finalize */
231                         NULL,   /* class_data */
232                         sizeof (TildaWindow),
233                         0,              /* n_preallocs */
234                         tilda_window_instance_init,     /* instance_init */
235                 };
236
237                 type = g_type_register_static (G_TYPE_OBJECT,
238                                                                            "TildaWindowType",
239                                                                            &info,
240                                                                            0);
241         }
242
243         return type;
244 }
245
246 int main (int argc, char *argv[])
247 {
248         GObject *tw;
249         gint test_number = INT_MIN;
250
251         /* Initialize the GObject type system */
252         g_type_init ();
253         gtk_init (&argc, &argv);
254
255         tw = g_object_new (TILDA_TYPE_WINDOW, "number", 10, NULL);
256         g_object_get (G_OBJECT (tw), "number", &test_number, NULL);
257         g_assert (test_number == 10);
258
259         g_object_unref (G_OBJECT (tw));
260
261         tw = g_object_new (TILDA_TYPE_WINDOW, "number", 22, NULL);
262         g_object_get (G_OBJECT (tw), "number", &test_number, NULL);
263         g_assert (test_number == 22);
264
265         gtk_main ();
266
267         g_object_unref (G_OBJECT (tw));
268
269         return 0;
270 }
271
272 /* vim: set ts=4 sts=4 sw=4 noet tw=112: */