[gnome-dictionary] gdict: Add GdictDefbox::selection-changed signal



commit b64bb1e1c37002ee51913de79714ca762a4a2112
Author: Juan R. GarcĂ­a Blanco <juanrgar gmail com>
Date:   Sat Mar 14 00:52:19 2015 +0100

    gdict: Add GdictDefbox::selection-changed signal
    
        * libgdict/gdict-defbox.h: Add selection_changed virtual method,
        and get_has_selection() method.
        * libgdict/gdict-defbox.c: Implement get_has_selection().
        Connect to GtkTextView:notify::has-selection signal, and
        forward as ::selection-changed.
        * See https://bugzilla.gnome.org/show_bug.cgi?id=334866

 libgdict/gdict-defbox.c |   45 +++++++++++++++++++++++++++++++++++++++++++++
 libgdict/gdict-defbox.h |   14 ++++++++------
 2 files changed, 53 insertions(+), 6 deletions(-)
---
diff --git a/libgdict/gdict-defbox.c b/libgdict/gdict-defbox.c
index 1f75b04..0b5694c 100644
--- a/libgdict/gdict-defbox.c
+++ b/libgdict/gdict-defbox.c
@@ -116,6 +116,7 @@ enum
   FIND_NEXT,
   FIND_PREVIOUS,
   LINK_CLICKED,
+  SELECTION_CHANGED,
   
   LAST_SIGNAL
 };
@@ -1494,6 +1495,15 @@ defbox_event_after_cb (GtkWidget   *text_view,
 }
 
 static void
+defbox_notify_has_selection (GtkTextBuffer *buffer,
+                             GParamSpec    *pspec,
+                             GdictDefbox   *defbox)
+{
+  GDICT_NOTE (DEFBOX, "text buffer has-selection notified");
+  g_signal_emit (defbox, gdict_defbox_signals[SELECTION_CHANGED], 0);
+}
+
+static void
 set_cursor_if_appropriate (GdictDefbox *defbox,
                            GtkTextView *text_view,
                            gint         x,
@@ -1626,6 +1636,9 @@ gdict_defbox_constructor (GType                  type,
   
   priv->buffer = gtk_text_buffer_new (NULL);
   gdict_defbox_init_tags (defbox);
+  g_signal_connect (priv->buffer, "notify::has-selection",
+                    G_CALLBACK (defbox_notify_has_selection),
+                    defbox);
   
   priv->text_view = gtk_text_view_new_with_buffer (priv->buffer);
   gtk_text_view_set_editable (GTK_TEXT_VIEW (priv->text_view), FALSE);
@@ -1820,6 +1833,14 @@ gdict_defbox_class_init (GdictDefboxClass *klass)
                   gdict_marshal_VOID__STRING,
                   G_TYPE_NONE, 1,
                   G_TYPE_STRING);
+  gdict_defbox_signals[SELECTION_CHANGED] =
+    g_signal_new ("selection-changed",
+                  G_OBJECT_CLASS_TYPE (gobject_class),
+                  G_SIGNAL_RUN_LAST,
+                  G_STRUCT_OFFSET (GdictDefboxClass, selection_changed),
+                  NULL, NULL,
+                  gdict_marshal_VOID__VOID,
+                  G_TYPE_NONE, 0);
   
   klass->show_find = gdict_defbox_real_show_find;
   klass->hide_find = gdict_defbox_real_hide_find;
@@ -2860,3 +2881,27 @@ gdict_defbox_get_selected_word (GdictDefbox *defbox)
       return retval;
     }
 }
+
+/**
+ * gdict_defbox_get_has_selection
+ * @defbox: a #GdictDefbox
+ *
+ * Retrieves whether there is text selected in @defbox or not.
+ *
+ * Return value: whether text is selected or not.
+ *
+ * Since: 0.12
+ */
+gboolean
+gdict_defbox_get_has_selection (GdictDefbox *defbox)
+{
+  GdictDefboxPrivate *priv;
+  GtkTextBuffer *buffer;
+
+  g_return_val_if_fail (GDICT_IS_DEFBOX (defbox), NULL);
+
+  priv = defbox->priv;
+  buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
+
+  return gtk_text_buffer_get_has_selection (buffer);
+}
diff --git a/libgdict/gdict-defbox.h b/libgdict/gdict-defbox.h
index 2217413..e839280 100644
--- a/libgdict/gdict-defbox.h
+++ b/libgdict/gdict-defbox.h
@@ -48,14 +48,15 @@ struct _GdictDefboxClass
   GtkBoxClass parent_class;
   
   /* these are all RUN_ACTION signals for key bindings */
-  void (*show_find)     (GdictDefbox *defbox);
-  void (*hide_find)     (GdictDefbox *defbox);
-  void (*find_previous) (GdictDefbox *defbox);
-  void (*find_next)     (GdictDefbox *defbox);
+  void (*show_find)         (GdictDefbox *defbox);
+  void (*hide_find)         (GdictDefbox *defbox);
+  void (*find_previous)     (GdictDefbox *defbox);
+  void (*find_next)         (GdictDefbox *defbox);
 
   /* signals */
-  void (*link_clicked)  (GdictDefbox *defbox,
-                         const gchar *link);
+  void (*link_clicked)      (GdictDefbox *defbox,
+                             const gchar *link);
+  void (*selection_changed) (GdictDefbox *defbox);
   
   /* padding for future expansion */
   void (*_gdict_defbox_1) (void);
@@ -95,6 +96,7 @@ void                  gdict_defbox_set_font_name      (GdictDefbox  *defbox,
                                                       const gchar  *font_name);
 const gchar *         gdict_defbox_get_font_name      (GdictDefbox  *defbox);
 gchar *               gdict_defbox_get_selected_word  (GdictDefbox  *defbox) G_GNUC_MALLOC;
+gboolean              gdict_defbox_get_has_selection  (GdictDefbox  *defbox);
 
 G_END_DECLS
 


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