2 #include "tilda-window.h"
3 #include "tilda-window-dbus-glue.h"
6 tilda_window_find_next_free_terminal_number (TildaWindow *tw)
11 for (i=0; i<INT_MAX; ++i)
15 for (j=0; j<tw->terms->len; ++j)
17 TildaTerminal *tt = g_ptr_array_index (tw->terms, j);
34 tilda_window_add_term (TildaWindow *tw)
39 number = tilda_window_find_next_free_terminal_number (tw);
40 tt = g_object_new (TILDA_TYPE_TERMINAL,
42 "window-number", tw->number,
45 g_ptr_array_add (tw->terms, tt);
47 GtkWidget *label = gtk_label_new ("Tilda");
48 gint index = gtk_notebook_prepend_page (GTK_NOTEBOOK(tw->notebook), tt->hbox, label);
49 gtk_notebook_set_tab_label_packing (GTK_NOTEBOOK(tw->notebook), tt->hbox, TRUE, TRUE, GTK_PACK_END);
50 //gtk_notebook_set_current_page (GTK_NOTEBOOK(tw->notebook), index);
52 if (gtk_notebook_get_n_pages (GTK_NOTEBOOK(tw->notebook)) > 1)
53 gtk_notebook_set_show_tabs (GTK_NOTEBOOK(tw->notebook), TRUE);
59 * Remove the TildaTerminal with the given number from the given
62 * Return: TRUE on success, FALSE otherwise.
65 tilda_window_remove_term (TildaWindow *tw, gint terminal_number)
69 for (i=0; i<tw->terms->len; ++i)
71 TildaTerminal *tt = g_ptr_array_index (tw->terms, i);
73 if (tt->number == terminal_number)
75 gint notebook_index = gtk_notebook_page_num (GTK_NOTEBOOK(tw->notebook), tt->hbox);
77 /* Make sure the index was valid */
78 if (notebook_index == -1)
80 g_printerr ("DEBUG ERROR: Bad Notebook Tab\n");
84 /* Actually remove the terminal */
85 gtk_notebook_remove_page (GTK_NOTEBOOK (tw->notebook), notebook_index);
87 /* We should hide the tabs if there is only one tab left */
88 if (gtk_notebook_get_n_pages (GTK_NOTEBOOK (tw->notebook)) == 1)
89 gtk_notebook_set_show_tabs (GTK_NOTEBOOK (tw->notebook), FALSE);
92 // FIXME FIXME FIXME: need to actually do the stuff below
93 /* With no pages left, it's time to leave the program */
94 if (gtk_notebook_get_n_pages (GTK_NOTEBOOK (tw->notebook)) < 1)
98 /* Remove the term from our lists, then free it */
99 g_ptr_array_remove_fast (tw->terms, tt);
100 g_object_unref (G_OBJECT(tt));
102 /* Leave the loop, we're done */
111 tilda_window_dbus_register_object (TildaWindow *tw)
115 // Register this object with DBus
116 object_path = g_strdup_printf ("/net/sourceforge/Tilda/Window%d", tw->number);
117 dbus_g_connection_register_g_object (dbus_connection, object_path, G_OBJECT(tw));
118 g_free (object_path);
121 /*******************************************************************************
122 * ALL GOBJECT STUFF BELOW PLEASE
123 ******************************************************************************/
125 static GObjectClass *parent_class = NULL;
127 enum tilda_window_properties {
128 TILDA_WINDOW_NUMBER = 1,
132 tilda_window_instance_init (GTypeInstance *instance,
135 TildaWindow *self = (TildaWindow *) instance;
136 self->dispose_has_run = FALSE;
138 /* Initialize all properties */
139 self->window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
140 self->notebook = gtk_notebook_new ();
141 self->terms = g_ptr_array_new ();
143 /* Somewhat of a "poison" value, incase we don't set this */
144 self->number = 0xdeadbeef;
148 tilda_window_set_property (GObject *object,
153 TildaWindow *self = (TildaWindow *) object;
155 switch (property_id) {
157 case TILDA_WINDOW_NUMBER:
158 self->number = g_value_get_int (value);
159 g_print ("window number: %d\n", self->number);
163 /* We don't have this property */
164 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
170 tilda_window_get_property (GObject *object,
175 TildaWindow *self = (TildaWindow *) object;
177 switch (property_id) {
179 case TILDA_WINDOW_NUMBER:
180 g_value_set_int (value, self->number);
184 /* We don't have this property */
185 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
191 tilda_window_constructor (GType type,
192 guint n_construct_properties,
193 GObjectConstructParam *construct_properties)
198 /* Invoke parent constructor */
199 TildaWindowClass *klass;
200 klass = TILDA_WINDOW_CLASS (g_type_class_peek (TILDA_TYPE_WINDOW));
201 obj = parent_class->constructor (type,
202 n_construct_properties,
203 construct_properties);
205 /* Do other stuff here. The object is ready to go now, and all
206 * ctor properties have been set.
208 * TODO: This is the place to do DBus-init */
209 self = TILDA_WINDOW(obj);
211 /* Register this object with DBus */
212 tilda_window_dbus_register_object (self);
214 gtk_container_add (GTK_CONTAINER(self->window), self->notebook);
215 gtk_widget_show (self->notebook);
217 tilda_window_add_term (self);
218 tilda_window_add_term (self);
219 gtk_widget_show_all (self->window);
225 my_unref (gpointer data, gpointer user_data)
227 g_object_unref (G_OBJECT(data));
231 tilda_window_dispose (GObject *obj)
233 TildaWindow *self = (TildaWindow *) obj;
235 /* We don't want to run dispose twice, so just return immediately */
236 if (self->dispose_has_run)
240 * In dispose, you are supposed to free all types referenced from this
241 * object which might themselves hold a reference to self. Generally,
242 * the most simple solution is to unref all members on which you own a
245 * NOTE: See the following for how to deal with GtkObject-derived things:
246 * http://library.gnome.org/devel/gtk/unstable/GtkObject.html
248 g_ptr_array_foreach (self->terms, my_unref, NULL);
249 gtk_widget_destroy (self->window);
251 /* Chain up to the parent class */
252 G_OBJECT_CLASS (parent_class)->dispose (obj);
256 tilda_window_finalize (GObject *obj)
258 TildaWindow *self = (TildaWindow *) obj;
261 * Here, complete the object's destruction.
262 * You might not need to do much...
264 // TODO: g_free() any primitives here
265 g_ptr_array_free (self->terms, TRUE);
268 /* Chain up to the parent class */
269 G_OBJECT_CLASS (parent_class)->finalize (obj);
273 tilda_window_class_init (gpointer g_class,
274 gpointer g_class_data)
276 GObjectClass *gobject_class = G_OBJECT_CLASS (g_class);
277 TildaWindowClass *klass = TILDA_WINDOW_CLASS (g_class);
280 /* Hook our functions to this type */
281 gobject_class->set_property = tilda_window_set_property;
282 gobject_class->get_property = tilda_window_get_property;
283 gobject_class->dispose = tilda_window_dispose;
284 gobject_class->finalize = tilda_window_finalize;
285 gobject_class->constructor = tilda_window_constructor;
287 parent_class = g_type_class_peek_parent (klass);
289 /* Install all of the properties */
290 pspec = g_param_spec_int ("number",
292 "Set window's number",
294 INT_MAX, // max value
296 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
298 g_object_class_install_property (gobject_class,
302 /* TODO: more properties */
304 /* Hook the TildaWindow type into DBus */
305 dbus_g_object_type_install_info (tilda_window_get_type(), &dbus_glib_tilda_window_object_info);
309 tilda_window_get_type (void)
311 static GType type = 0;
315 static const GTypeInfo info = {
316 sizeof (TildaWindowClass),
317 NULL, /* base_init */
318 NULL, /* base_finalize */
319 tilda_window_class_init, /* class_init */
320 NULL, /* class_finalize */
321 NULL, /* class_data */
322 sizeof (TildaWindow),
324 tilda_window_instance_init, /* instance_init */
327 type = g_type_register_static (G_TYPE_OBJECT,
338 int main (int argc, char *argv[])
341 gint test_number = INT_MIN;
343 /* Initialize the GObject type system */
345 gtk_init (&argc, &argv);
347 tw = g_object_new (TILDA_TYPE_WINDOW, "number", 10, NULL);
348 g_object_get (G_OBJECT (tw), "number", &test_number, NULL);
349 g_assert (test_number == 10);
351 g_object_unref (G_OBJECT (tw));
353 tw = g_object_new (TILDA_TYPE_WINDOW, "number", 22, NULL);
354 g_object_get (G_OBJECT (tw), "number", &test_number, NULL);
355 g_assert (test_number == 22);
359 g_object_unref (G_OBJECT (tw));
366 /* vim: set ts=4 sts=4 sw=4 noet tw=112: */