Make TildaWindow determine number correctly
[tilda-gobject.git] / tilda.c
diff --git a/tilda.c b/tilda.c
index e1459dd..d6d60af 100644 (file)
--- a/tilda.c
+++ b/tilda.c
@@ -5,6 +5,7 @@
 #include "tilda-terminal.h"
 
 DBusGConnection *dbus_connection;
+GPtrArray *windows;
 
 static void
 tilda_initialize_dbus ()
@@ -41,6 +42,69 @@ tilda_initialize_dbus ()
        g_object_unref (driver_proxy);
 }
 
+static gint
+tilda_find_next_free_window_number ()
+{
+       gint i, j;
+       gboolean found;
+
+       for (i=0; i<INT_MAX; ++i)
+       {
+               found = FALSE;
+
+               for (j=0; j<windows->len; ++j)
+               {
+                       TildaWindow *tw = g_ptr_array_index (windows, j);
+
+                       if (tw->number == i)
+                       {
+                               found = TRUE;
+                               break;
+                       }
+               }
+
+               if (!found)
+                       return i;
+       }
+
+       return 0;
+}
+
+static TildaWindow *
+tilda_add_window ()
+{
+       TildaWindow *ret;
+       gint number;
+
+       number = tilda_find_next_free_window_number ();
+       ret = g_object_new (TILDA_TYPE_WINDOW, "number", number, NULL);
+
+       g_ptr_array_add (windows, ret);
+
+       g_print ("Adding window: 0x%x (number %d of %d)\n", ret, ret->number, windows->len);
+       return ret;
+}
+
+static void
+tilda_del_window (gint number)
+{
+       gint i;
+       TildaWindow *win;
+
+       for (i=0; i<windows->len; ++i)
+       {
+               win = g_ptr_array_index (windows, i);
+
+               if (win->number == number)
+               {
+                       g_print ("Deleting window 0x%x (number %d)\n", win, win->number);
+                       g_ptr_array_remove_index (windows, i);
+                       g_object_unref (G_OBJECT(win));
+                       break;
+               }
+       }
+}
+
 int main (int argc, char *argv[])
 {
        TildaWindow *tw;
@@ -51,16 +115,23 @@ int main (int argc, char *argv[])
        /* Start our connection to DBus */
        tilda_initialize_dbus ();
 
+       /* Initialize the array of windows */
+       windows = g_ptr_array_new ();
+
        /* Create a TildaWindow, run it, and exit when it does, basically.
         *
         * This is nothing like what the real main() will be, but it's
         * a good start for testing and integration of more of TildaWindow
         * and TildaTerminal. */
-       tw = g_object_new (TILDA_TYPE_WINDOW, "number", 0, NULL);
+       tw = tilda_add_window ();
+       tw = tilda_add_window ();
+       tw = tilda_add_window ();
 
-       gtk_main ();
+       tilda_del_window (1);
+
+       tw = tilda_add_window ();
 
-       g_object_unref (G_OBJECT (tw));
+       gtk_main ();
 
        return 0;
 }