[Controller] Disallow multiple TildaWindows to bind to the same key
authorIra W. Snyder <devel@irasnyder.com>
Wed, 23 Jan 2008 22:36:51 +0000 (14:36 -0800)
committerIra W. Snyder <devel@irasnyder.com>
Wed, 23 Jan 2008 22:36:51 +0000 (14:36 -0800)
Previously, it was possible to have two TildaWindows be simultaneously pulled
down via the same key binding. This has the potential to be very cool, but also
very confusing. For now, completely disable this ability. In the future, it
will be re-enabled via a hidden option.

tilda-controller.c
tilda-controller.h
tilda-window.c

index cf0c23c..613bedb 100644 (file)
@@ -110,6 +110,36 @@ tilda_controller_remove_window (TildaController *self, gint window_number)
        return FALSE;
 }
 
+/**
+ * Check if a key is used in one of our windows.
+ *
+ * This is needed because the tomboy_keybinder_bind() function allows
+ * more than one callback to be registered for the same key.
+ */
+gboolean
+tilda_controller_global_key_in_use (const TildaController *self, const gchar *keystr)
+{
+       gint i;
+       TildaWindow *tw;
+
+       guint key1, key2;
+       GdkModifierType mod1, mod2;
+
+       gtk_accelerator_parse (keystr, &key1, &mod1);
+
+       for (i=0; i<self->windows->len; ++i)
+       {
+               tw = g_ptr_array_index (self->windows, i);
+               gtk_accelerator_parse (tw->key, &key2, &mod2);
+
+               if (key1 == key2 && mod1 == mod2)
+                       return TRUE;
+       }
+
+       /* No identical keys found, we're ok */
+       return FALSE;
+}
+
 gboolean
 tilda_controller_quit (TildaController *self, GError **error)
 {
index 44e7a2a..6645436 100644 (file)
@@ -34,6 +34,7 @@ gboolean tilda_controller_quit (TildaController *self, GError **error);
 
 /* API */
 gboolean tilda_controller_remove_window (TildaController *self, gint window_number);
+gboolean tilda_controller_global_key_in_use (const TildaController *self, const gchar *keystr);
 
 #endif /* TILDA_CONTROLLER_H */
 
index ed45af3..dcef0f5 100644 (file)
@@ -298,6 +298,12 @@ tilda_window_try_to_bind_key (TildaWindow *self, const gchar *new_key)
        if (new_key == NULL || strcmp("", new_key) == 0)
                return FALSE;
 
+       /* Check that no other windows are using the key */
+       // FIXME: there should be a hidden option to disable this. Maybe some people want
+       // to have logs in two Tildas, and just show them with one key. Crazy...
+       if (tilda_controller_global_key_in_use(TILDA_CONTROLLER(self->controller), new_key))
+               return FALSE;
+
        /* Unbind if we were set */
        if (self->key)
                tomboy_keybinder_unbind (self->key, tilda_window_keybinding_cb);