Build with DEBUG defined
[tilda-gobject.git] / tilda-window.c
index fdf2376..7cff702 100644 (file)
@@ -1,11 +1,47 @@
+#include "tilda.h"
 #include "tilda-window.h"
+#include "tilda-window-dbus-glue.h"
+
+static gint
+tilda_window_find_next_free_terminal_number (TildaWindow *tw)
+{
+       gint i, j;
+       gboolean found;
+
+       for (i=0; i<INT_MAX; ++i)
+       {
+               found = FALSE;
+
+               for (j=0; j<tw->terms->len; ++j)
+               {
+                       TildaTerminal *tt = g_ptr_array_index (tw->terms, j);
+
+                       if (tt->number == i)
+                       {
+                               found = TRUE;
+                               break;
+                       }
+               }
+
+               if (!found)
+                       return i;
+       }
+
+       return 0;
+}
 
 static gboolean
 tilda_window_add_term (TildaWindow *tw)
 {
-       // FIXME: this is totally bad, but it's a good hack for feasability
-       static gint mynumber = 0;
-       TildaTerminal *tt = g_object_new (TILDA_TYPE_TERMINAL, "number", mynumber++, NULL);
+       gint number;
+       TildaTerminal *tt;
+
+       number = tilda_window_find_next_free_terminal_number (tw);
+       tt = g_object_new (TILDA_TYPE_TERMINAL,
+                                          "number", number,
+                                          "window-number", tw->number,
+                                          "parent-window", tw,
+                                          NULL);
        g_ptr_array_add (tw->terms, tt);
 
        GtkWidget *label = gtk_label_new ("Tilda");
@@ -19,22 +55,93 @@ tilda_window_add_term (TildaWindow *tw)
        return TRUE;
 }
 
-static gboolean
-tilda_window_remove_term (TildaWindow *tw, int number)
+/**
+ * Remove the TildaTerminal with the given number from the given
+ * TildaWindow.
+ *
+ * Return: TRUE on success, FALSE otherwise.
+ */
+gboolean
+tilda_window_remove_term (TildaWindow *tw, gint terminal_number)
 {
-       int i;
+       gint i;
 
        for (i=0; i<tw->terms->len; ++i)
        {
                TildaTerminal *tt = g_ptr_array_index (tw->terms, i);
 
-               if (tt->number == number)
-                       g_print ("Need to remove window %d terminal %d\n", tw->number, tt->number);
+               if (tt->number == terminal_number)
+               {
+                       gint notebook_index = gtk_notebook_page_num (GTK_NOTEBOOK(tw->notebook), tt->hbox);
+
+                       /* Make sure the index was valid */
+                       if (notebook_index == -1)
+                       {
+                               g_printerr ("DEBUG ERROR: Bad Notebook Tab\n");
+                               return FALSE;
+                       }
+
+                       /* Actually remove the terminal */
+                       gtk_notebook_remove_page (GTK_NOTEBOOK (tw->notebook), notebook_index);
+
+                       /* We should hide the tabs if there is only one tab left */
+                       if (gtk_notebook_get_n_pages (GTK_NOTEBOOK (tw->notebook)) == 1)
+                               gtk_notebook_set_show_tabs (GTK_NOTEBOOK (tw->notebook), FALSE);
+
+#if 0
+                       // FIXME FIXME FIXME: need to actually do the stuff below
+                       /* With no pages left, it's time to leave the program */
+                       if (gtk_notebook_get_n_pages (GTK_NOTEBOOK (tw->notebook)) < 1)
+                               gtk_main_quit ();
+#endif
+
+                       /* Remove the term from our lists, then free it */
+                       g_ptr_array_remove_fast (tw->terms, tt);
+                       g_object_unref (G_OBJECT(tt));
+
+                       /* Leave the loop, we're done */
+                       break;
+               }
        }
 
        return TRUE;
 }
 
+/**
+ * This sets up the given TildaWindow for the capability of real
+ * transparency, if the X server is capable of it. */
+static void
+tilda_window_setup_real_transparency (TildaWindow *self)
+{
+       GdkScreen *screen;
+       GdkColormap *colormap;
+
+       screen = gtk_widget_get_screen (GTK_WIDGET(self->window));
+       colormap = gdk_screen_get_rgba_colormap (screen);
+
+       /* If possible, set the RGBA colormap so VTE can use real alpha
+        * channels for transparency. */
+       if (colormap != NULL && gdk_screen_is_composited (screen))
+       {
+               gtk_widget_set_colormap (GTK_WIDGET(self->window), colormap);
+               self->have_real_transparency = TRUE;
+               return;
+       }
+
+       self->have_real_transparency = FALSE;
+}
+
+static void
+tilda_window_dbus_register_object (TildaWindow *tw)
+{
+       gchar *object_path;
+
+       // Register this object with DBus
+       object_path = g_strdup_printf ("/net/sourceforge/Tilda/Window%d", tw->number);
+       dbus_g_connection_register_g_object (dbus_connection, object_path, G_OBJECT(tw));
+       g_free (object_path);
+}
+
 /*******************************************************************************
  * ALL GOBJECT STUFF BELOW PLEASE
  ******************************************************************************/
@@ -43,6 +150,8 @@ static GObjectClass *parent_class = NULL;
 
 enum tilda_window_properties {
        TILDA_WINDOW_NUMBER = 1,
+
+       TILDA_WINDOW_HAVE_REAL_TRANSPARENCY,
 };
 
 static void
@@ -76,6 +185,11 @@ tilda_window_set_property (GObject      *object,
                        g_print ("window number: %d\n", self->number);
                        break;
 
+               case TILDA_WINDOW_HAVE_REAL_TRANSPARENCY:
+                       self->have_real_transparency = g_value_get_boolean (value);
+                       g_print ("window have real transp: %d\n", self->have_real_transparency);
+                       break;
+
                default:
                        /* We don't have this property */
                        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
@@ -97,6 +211,10 @@ tilda_window_get_property (GObject    *object,
                        g_value_set_int (value, self->number);
                        break;
 
+               case TILDA_WINDOW_HAVE_REAL_TRANSPARENCY:
+                       g_value_set_boolean (value, self->have_real_transparency);
+                       break;
+
                default:
                        /* We don't have this property */
                        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
@@ -125,9 +243,16 @@ tilda_window_constructor (GType                  type,
         * TODO: This is the place to do DBus-init */
        self = TILDA_WINDOW(obj);
 
+       /* Register this object with DBus */
+       tilda_window_dbus_register_object (self);
+
+       /* Try to set up real transparency */
+       tilda_window_setup_real_transparency (self);
+
        gtk_container_add (GTK_CONTAINER(self->window), self->notebook);
        gtk_widget_show (self->notebook);
 
+       tilda_window_add_term (self);
        tilda_window_add_term (self);
        gtk_widget_show_all (self->window);
 
@@ -212,7 +337,17 @@ tilda_window_class_init (gpointer g_class,
                                                                         TILDA_WINDOW_NUMBER,
                                                                         pspec);
 
+       pspec = g_param_spec_boolean ("have-real-transparency",
+                                                                 NULL, NULL, FALSE, G_PARAM_READABLE);
+
+       g_object_class_install_property (gobject_class,
+                                                                        TILDA_WINDOW_HAVE_REAL_TRANSPARENCY,
+                                                                        pspec);
+
        /* TODO: more properties */
+
+       /* Hook the TildaWindow type into DBus */
+       dbus_g_object_type_install_info (tilda_window_get_type(), &dbus_glib_tilda_window_object_info);
 }
 
 GType
@@ -243,6 +378,8 @@ tilda_window_get_type (void)
        return type;
 }
 
+#if 0
+
 int main (int argc, char *argv[])
 {
        GObject *tw;
@@ -269,4 +406,6 @@ int main (int argc, char *argv[])
        return 0;
 }
 
+#endif
+
 /* vim: set ts=4 sts=4 sw=4 noet tw=112: */