[gtksourceview/wip/completion-next-cycle: 2/2] Hide CompletionInfo when focus-out-event on the attached-to widget



commit dd59d71d38b75b062d09ef7dc666ba833204bb73
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Wed Apr 3 18:24:36 2013 +0200

    Hide CompletionInfo when focus-out-event on the attached-to widget
    
    The application just needs to attach the calltip window to the
    GtkTextView widget, so when the text view looses focus, the calltip is
    hidden.

 gtksourceview/gtksourcecompletioninfo.c |   65 ++++++++++++++++++++++++++++--
 1 files changed, 60 insertions(+), 5 deletions(-)
---
diff --git a/gtksourceview/gtksourcecompletioninfo.c b/gtksourceview/gtksourcecompletioninfo.c
index f7e8bd8..7a50917 100644
--- a/gtksourceview/gtksourcecompletioninfo.c
+++ b/gtksourceview/gtksourcecompletioninfo.c
@@ -48,11 +48,12 @@
  * </example>
  *
  * If the calltip is displayed on top of a certain widget, say a #GtkTextView,
- * you should hide it when the #GtkWidget::focus-out-event signal is emitted by
- * the #GtkTextView. You may also be interested by the
- * #GtkTextBuffer:cursor-position property (when its value is modified). If you
- * use the #GtkSourceCompletionInfo through the #GtkSourceCompletion machinery,
- * you don't need to worry about this.
+ * you should attach the calltip window to the #GtkTextView with
+ * gtk_window_set_attached_to().  By doing this, the calltip will be hidden when
+ * the #GtkWidget::focus-out-event signal is emitted by the #GtkTextView. You
+ * may also be interested by the #GtkTextBuffer:cursor-position property (when
+ * its value is modified). If you use the #GtkSourceCompletionInfo through the
+ * #GtkSourceCompletion machinery, you don't need to worry about this.
  */
 
 #include <gtksourceview/gtksourcecompletioninfo.h>
@@ -62,6 +63,9 @@
 struct _GtkSourceCompletionInfoPrivate
 {
        guint idle_resize;
+
+       GtkWidget *attached_to;
+       gulong focus_out_event_handler;
 };
 
 /* Signals */
@@ -184,11 +188,55 @@ gtk_source_completion_info_get_preferred_height (GtkWidget *widget,
 
 /* Init, dispose, finalize, ... */
 
+static gboolean
+focus_out_event_cb (GtkSourceCompletionInfo *info)
+{
+       gtk_widget_hide (GTK_WIDGET (info));
+       return FALSE;
+}
+
+static void
+update_attached_to (GtkSourceCompletionInfo *info)
+{
+       GtkWidget *attached_to = gtk_window_get_attached_to (GTK_WINDOW (info));
+
+       if (info->priv->focus_out_event_handler != 0 &&
+           info->priv->attached_to != NULL)
+       {
+               g_signal_handler_disconnect (info->priv->attached_to,
+                                            info->priv->focus_out_event_handler);
+
+               info->priv->focus_out_event_handler = 0;
+       }
+
+       info->priv->attached_to = attached_to;
+
+       if (attached_to == NULL)
+       {
+               return;
+       }
+
+       g_object_add_weak_pointer (G_OBJECT (attached_to),
+                                  (gpointer) &info->priv->attached_to);
+
+       info->priv->focus_out_event_handler = g_signal_connect_swapped (attached_to,
+                                                                       "focus-out-event",
+                                                                       G_CALLBACK (focus_out_event_cb),
+                                                                       info);
+}
+
 static void
 gtk_source_completion_info_init (GtkSourceCompletionInfo *info)
 {
        info->priv = GTK_SOURCE_COMPLETION_INFO_GET_PRIVATE (info);
 
+       g_signal_connect (info,
+                         "notify::attached-to",
+                         G_CALLBACK (update_attached_to),
+                         NULL);
+
+       update_attached_to (info);
+
        /* Tooltip style */
        gtk_window_set_title (GTK_WINDOW (info), _("Completion Info"));
        gtk_widget_set_name (GTK_WIDGET (info), "gtk-tooltip");
@@ -209,6 +257,13 @@ gtk_source_completion_info_finalize (GObject *object)
                g_source_remove (info->priv->idle_resize);
        }
 
+       if (info->priv->focus_out_event_handler != 0 &&
+           info->priv->attached_to != NULL)
+       {
+               g_signal_handler_disconnect (info->priv->attached_to,
+                                            info->priv->focus_out_event_handler);
+       }
+
        G_OBJECT_CLASS (gtk_source_completion_info_parent_class)->finalize (object);
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]