Make TildaWindow properly generate terminal numbers
authorIra W. Snyder <devel@irasnyder.com>
Sat, 12 Jan 2008 05:42:04 +0000 (21:42 -0800)
committerIra W. Snyder <devel@irasnyder.com>
Sat, 12 Jan 2008 05:42:04 +0000 (21:42 -0800)
This removes the ugly hack of using a static variable to give
unique terminal numbers, and instead determines the correct
terminal number at runtime.

tilda-window.c

index adbe624..0a55adf 100644 (file)
@@ -2,16 +2,46 @@
 #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++,
-                                                                         "window-number", tw->number,
-                                                                         "parent-window", tw,
-                                                                         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");
@@ -35,7 +65,11 @@ tilda_window_remove_term (TildaWindow *tw, int number)
                TildaTerminal *tt = g_ptr_array_index (tw->terms, i);
 
                if (tt->number == number)
+               {
+                       // FIXME: Find it in the notebook. Remove that notebook page.
+                       // FIXME: Check if we need to exit. Etc.
                        g_print ("Need to remove window %d terminal %d\n", tw->number, tt->number);
+               }
        }
 
        return TRUE;
@@ -148,6 +182,7 @@ tilda_window_constructor (GType                  type,
        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);