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 * This sets up the given TildaWindow for the capability of real
112 * transparency, if the X server is capable of it. */
114 tilda_window_setup_real_transparency (TildaWindow *self)
117 GdkColormap *colormap;
119 screen = gtk_widget_get_screen (GTK_WIDGET(self->window));
120 colormap = gdk_screen_get_rgba_colormap (screen);
122 /* If possible, set the RGBA colormap so VTE can use real alpha
123 * channels for transparency. */
124 if (colormap != NULL && gdk_screen_is_composited (screen))
126 gtk_widget_set_colormap (GTK_WIDGET(self->window), colormap);
127 self->have_real_transparency = TRUE;
131 self->have_real_transparency = FALSE;
135 tilda_window_dbus_register_object (TildaWindow *tw)
139 // Register this object with DBus
140 object_path = g_strdup_printf ("/net/sourceforge/Tilda/Window%d", tw->number);
141 dbus_g_connection_register_g_object (dbus_connection, object_path, G_OBJECT(tw));
142 g_free (object_path);
145 /*******************************************************************************
146 * ALL GOBJECT STUFF BELOW PLEASE
147 ******************************************************************************/
149 static GObjectClass *parent_class = NULL;
151 enum tilda_window_properties {
152 TILDA_WINDOW_NUMBER = 1,
154 TILDA_WINDOW_HAVE_REAL_TRANSPARENCY,
158 tilda_window_instance_init (GTypeInstance *instance,
161 TildaWindow *self = (TildaWindow *) instance;
162 self->dispose_has_run = FALSE;
164 /* Initialize all properties */
165 self->window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
166 self->notebook = gtk_notebook_new ();
167 self->terms = g_ptr_array_new ();
169 /* Somewhat of a "poison" value, incase we don't set this */
170 self->number = 0xdeadbeef;
174 tilda_window_set_property (GObject *object,
179 TildaWindow *self = (TildaWindow *) object;
181 switch (property_id) {
183 case TILDA_WINDOW_NUMBER:
184 self->number = g_value_get_int (value);
185 g_print ("window number: %d\n", self->number);
188 case TILDA_WINDOW_HAVE_REAL_TRANSPARENCY:
189 self->have_real_transparency = g_value_get_boolean (value);
190 g_print ("window have real transp: %d\n", self->have_real_transparency);
194 /* We don't have this property */
195 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
201 tilda_window_get_property (GObject *object,
206 TildaWindow *self = (TildaWindow *) object;
208 switch (property_id) {
210 case TILDA_WINDOW_NUMBER:
211 g_value_set_int (value, self->number);
214 case TILDA_WINDOW_HAVE_REAL_TRANSPARENCY:
215 g_value_set_boolean (value, self->have_real_transparency);
219 /* We don't have this property */
220 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
226 tilda_window_constructor (GType type,
227 guint n_construct_properties,
228 GObjectConstructParam *construct_properties)
233 /* Invoke parent constructor */
234 TildaWindowClass *klass;
235 klass = TILDA_WINDOW_CLASS (g_type_class_peek (TILDA_TYPE_WINDOW));
236 obj = parent_class->constructor (type,
237 n_construct_properties,
238 construct_properties);
240 /* Do other stuff here. The object is ready to go now, and all
241 * ctor properties have been set.
243 * TODO: This is the place to do DBus-init */
244 self = TILDA_WINDOW(obj);
246 /* Register this object with DBus */
247 tilda_window_dbus_register_object (self);
249 /* Try to set up real transparency */
250 tilda_window_setup_real_transparency (self);
252 gtk_container_add (GTK_CONTAINER(self->window), self->notebook);
253 gtk_widget_show (self->notebook);
255 tilda_window_add_term (self);
256 tilda_window_add_term (self);
257 gtk_widget_show_all (self->window);
263 my_unref (gpointer data, gpointer user_data)
265 g_object_unref (G_OBJECT(data));
269 tilda_window_dispose (GObject *obj)
271 TildaWindow *self = (TildaWindow *) obj;
273 /* We don't want to run dispose twice, so just return immediately */
274 if (self->dispose_has_run)
278 * In dispose, you are supposed to free all types referenced from this
279 * object which might themselves hold a reference to self. Generally,
280 * the most simple solution is to unref all members on which you own a
283 * NOTE: See the following for how to deal with GtkObject-derived things:
284 * http://library.gnome.org/devel/gtk/unstable/GtkObject.html
286 g_ptr_array_foreach (self->terms, my_unref, NULL);
287 gtk_widget_destroy (self->window);
289 /* Chain up to the parent class */
290 G_OBJECT_CLASS (parent_class)->dispose (obj);
294 tilda_window_finalize (GObject *obj)
296 TildaWindow *self = (TildaWindow *) obj;
299 * Here, complete the object's destruction.
300 * You might not need to do much...
302 // TODO: g_free() any primitives here
303 g_ptr_array_free (self->terms, TRUE);
306 /* Chain up to the parent class */
307 G_OBJECT_CLASS (parent_class)->finalize (obj);
311 tilda_window_class_init (gpointer g_class,
312 gpointer g_class_data)
314 GObjectClass *gobject_class = G_OBJECT_CLASS (g_class);
315 TildaWindowClass *klass = TILDA_WINDOW_CLASS (g_class);
318 /* Hook our functions to this type */
319 gobject_class->set_property = tilda_window_set_property;
320 gobject_class->get_property = tilda_window_get_property;
321 gobject_class->dispose = tilda_window_dispose;
322 gobject_class->finalize = tilda_window_finalize;
323 gobject_class->constructor = tilda_window_constructor;
325 parent_class = g_type_class_peek_parent (klass);
327 /* Install all of the properties */
328 pspec = g_param_spec_int ("number",
330 "Set window's number",
332 INT_MAX, // max value
334 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
336 g_object_class_install_property (gobject_class,
340 pspec = g_param_spec_boolean ("have-real-transparency",
341 NULL, NULL, FALSE, G_PARAM_READABLE);
343 g_object_class_install_property (gobject_class,
344 TILDA_WINDOW_HAVE_REAL_TRANSPARENCY,
347 /* TODO: more properties */
349 /* Hook the TildaWindow type into DBus */
350 dbus_g_object_type_install_info (tilda_window_get_type(), &dbus_glib_tilda_window_object_info);
354 tilda_window_get_type (void)
356 static GType type = 0;
360 static const GTypeInfo info = {
361 sizeof (TildaWindowClass),
362 NULL, /* base_init */
363 NULL, /* base_finalize */
364 tilda_window_class_init, /* class_init */
365 NULL, /* class_finalize */
366 NULL, /* class_data */
367 sizeof (TildaWindow),
369 tilda_window_instance_init, /* instance_init */
372 type = g_type_register_static (G_TYPE_OBJECT,
383 int main (int argc, char *argv[])
386 gint test_number = INT_MIN;
388 /* Initialize the GObject type system */
390 gtk_init (&argc, &argv);
392 tw = g_object_new (TILDA_TYPE_WINDOW, "number", 10, NULL);
393 g_object_get (G_OBJECT (tw), "number", &test_number, NULL);
394 g_assert (test_number == 10);
396 g_object_unref (G_OBJECT (tw));
398 tw = g_object_new (TILDA_TYPE_WINDOW, "number", 22, NULL);
399 g_object_get (G_OBJECT (tw), "number", &test_number, NULL);
400 g_assert (test_number == 22);
404 g_object_unref (G_OBJECT (tw));
411 /* vim: set ts=4 sts=4 sw=4 noet tw=112: */