[gtk+/search-entry: 3/3] inspector: Use some of the new GtkSearchEntry API



commit 46fd9fa4ef97265b28c5db51cbb431f45990473d
Author: Matthias Clasen <mclasen redhat com>
Date:   Mon Nov 24 22:44:44 2014 -0500

    inspector: Use some of the new GtkSearchEntry API
    
    Avoid repetitive, manual event handling in various places
    in GtkInspector.

 gtk/inspector/object-tree.c      |    9 +++
 gtk/inspector/object-tree.ui     |    3 +
 gtk/inspector/prop-list.c        |  103 +-----------------------------------
 gtk/inspector/prop-list.ui       |    1 +
 gtk/inspector/style-prop-list.c  |  106 ++------------------------------------
 gtk/inspector/style-prop-list.ui |    2 +
 6 files changed, 22 insertions(+), 202 deletions(-)
---
diff --git a/gtk/inspector/object-tree.c b/gtk/inspector/object-tree.c
index f8f1d2a..6c48193 100644
--- a/gtk/inspector/object-tree.c
+++ b/gtk/inspector/object-tree.c
@@ -408,6 +408,14 @@ previous_match (GtkButton              *button,
 }
 
 static void
+stop_search (GtkWidget              *entry,
+             GtkInspectorObjectTree *wt)
+{
+  gtk_entry_set_text (GTK_ENTRY (wt->priv->search_entry), "");
+  gtk_search_bar_set_search_mode (GTK_SEARCH_BAR (wt->priv->search_bar), FALSE);
+}
+
+static void
 gtk_inspector_object_tree_init (GtkInspectorObjectTree *wt)
 {
   guint signal_id;
@@ -492,6 +500,7 @@ gtk_inspector_object_tree_class_init (GtkInspectorObjectTreeClass *klass)
   gtk_widget_class_bind_template_callback (widget_class, on_search_changed);
   gtk_widget_class_bind_template_callback (widget_class, next_match);
   gtk_widget_class_bind_template_callback (widget_class, previous_match);
+  gtk_widget_class_bind_template_callback (widget_class, stop_search);
 }
 
 typedef struct
diff --git a/gtk/inspector/object-tree.ui b/gtk/inspector/object-tree.ui
index 397fb3c..2b93eb0 100644
--- a/gtk/inspector/object-tree.ui
+++ b/gtk/inspector/object-tree.ui
@@ -30,6 +30,9 @@
                 <property name="visible">True</property>
                 <property name="max-width-chars">40</property>
                 <signal name="search-changed" handler="on_search_changed"/>
+                <signal name="next-match" handler="next_match"/>
+                <signal name="previous-match" handler="previous_match"/>
+                <signal name="stop-search" handler="stop_search"/>
               </object>
             </child>
             <child>
diff --git a/gtk/inspector/prop-list.c b/gtk/inspector/prop-list.c
index 771ea9c..3c26426 100644
--- a/gtk/inspector/prop-list.c
+++ b/gtk/inspector/prop-list.c
@@ -81,116 +81,19 @@ search_close_clicked (GtkWidget            *button,
 }
 
 static gboolean
-is_keynav_event (GdkEvent *event)
-{
-  GdkModifierType state = 0;
-  guint keyval;
-
-  if (!gdk_event_get_keyval (event, &keyval))
-    return FALSE;
-
-  gdk_event_get_state (event, &state);
-
-  if (keyval == GDK_KEY_Tab       || keyval == GDK_KEY_KP_Tab ||
-      keyval == GDK_KEY_Up        || keyval == GDK_KEY_KP_Up ||
-      keyval == GDK_KEY_Down      || keyval == GDK_KEY_KP_Down ||
-      keyval == GDK_KEY_Left      || keyval == GDK_KEY_KP_Left ||
-      keyval == GDK_KEY_Right     || keyval == GDK_KEY_KP_Right ||
-      keyval == GDK_KEY_Home      || keyval == GDK_KEY_KP_Home ||
-      keyval == GDK_KEY_End       || keyval == GDK_KEY_KP_End ||
-      keyval == GDK_KEY_Page_Up   || keyval == GDK_KEY_KP_Page_Up ||
-      keyval == GDK_KEY_Page_Down || keyval == GDK_KEY_KP_Page_Down ||
-      ((state & (GDK_CONTROL_MASK | GDK_MOD1_MASK)) != 0))
-        return TRUE;
-
-  /* Other navigation events should get automatically
-   * ignored as they will not change the content of the entry
-   */
-  return FALSE;
-}
-
-static void
-preedit_changed_cb (GtkEntry  *entry,
-                    GtkWidget *popup,
-                    gboolean  *preedit_changed)
-{
-  *preedit_changed = TRUE;
-}
-
-static gboolean
 key_press_event (GtkWidget            *window,
                  GdkEvent             *event,
                  GtkInspectorPropList *pl)
 {
-  gboolean handled;
-  gboolean preedit_changed;
-  guint preedit_change_id;
-  gboolean res;
-  gchar *old_text, *new_text;
-
   if (!gtk_widget_get_mapped (GTK_WIDGET (pl)))
     return GDK_EVENT_PROPAGATE;
 
-  if (is_keynav_event (event) ||
-      event->key.keyval == GDK_KEY_space ||
-      event->key.keyval == GDK_KEY_Menu)
-    return GDK_EVENT_PROPAGATE;
- 
-  if (event->key.keyval == GDK_KEY_Return ||
-      event->key.keyval == GDK_KEY_ISO_Enter ||
-      event->key.keyval == GDK_KEY_KP_Enter)
-    {
-      GtkTreeSelection *selection;
-      GtkTreeModel *model;
-      GtkTreeIter iter;
-      GtkTreePath *path;
-
-      selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (pl->priv->tree));
-      if (gtk_tree_selection_get_selected (selection, &model, &iter))
-        {
-          path = gtk_tree_model_get_path (model, &iter);
-          gtk_tree_view_row_activated (GTK_TREE_VIEW (pl->priv->tree),
-                                       path,
-                                       pl->priv->name_column);
-          gtk_tree_path_free (path);
-
-          return GDK_EVENT_STOP;
-        }
-      else
-        return GDK_EVENT_PROPAGATE;
-    }
-
-  if (event->key.keyval == GDK_KEY_Escape)
-    {
-      gtk_entry_set_text (GTK_ENTRY (pl->priv->search_entry), "");
-      gtk_stack_set_visible_child_name (GTK_STACK (pl->priv->search_stack), "title");
-      return GDK_EVENT_STOP;
-    }
-
-  if (!gtk_widget_get_realized (pl->priv->search_entry))
-    gtk_widget_realize (pl->priv->search_entry);
-
-  handled = FALSE;
-  preedit_changed = FALSE;
-  preedit_change_id = g_signal_connect (pl->priv->search_entry, "preedit-changed",
-                                        G_CALLBACK (preedit_changed_cb), &preedit_changed);
-
-  old_text = g_strdup (gtk_entry_get_text (GTK_ENTRY (pl->priv->search_entry)));
-  res = gtk_widget_event (pl->priv->search_entry, event);
-  new_text = g_strdup (gtk_entry_get_text (GTK_ENTRY (pl->priv->search_entry)));
-
-  g_signal_handler_disconnect (pl->priv->search_entry, preedit_change_id);
-
-  if ((res && g_strcmp0 (new_text, old_text) != 0) || preedit_changed)
+  if (gtk_search_entry_handle_event (GTK_SEARCH_ENTRY (pl->priv->search_entry), event))
     {
       gtk_stack_set_visible_child_name (GTK_STACK (pl->priv->search_stack), "search");
-      handled = TRUE;
+      return GDK_EVENT_STOP;
     }
-
-  g_free (old_text);
-  g_free (new_text);
-
- return handled ? GDK_EVENT_STOP : GDK_EVENT_PROPAGATE;
+  return GDK_EVENT_PROPAGATE;
 }
 
 static void
diff --git a/gtk/inspector/prop-list.ui b/gtk/inspector/prop-list.ui
index ae7e6ec..6d67a73 100644
--- a/gtk/inspector/prop-list.ui
+++ b/gtk/inspector/prop-list.ui
@@ -29,6 +29,7 @@
               <object class="GtkSearchEntry" id="search_entry">
                 <property name="visible">True</property>
                 <property name="max-width-chars">40</property>
+                <signal name="stop-search" handler="search_close_clicked"/>
               </object>
             </child>
             <child>
diff --git a/gtk/inspector/style-prop-list.c b/gtk/inspector/style-prop-list.c
index 536661a..fe31dfb 100644
--- a/gtk/inspector/style-prop-list.c
+++ b/gtk/inspector/style-prop-list.c
@@ -68,116 +68,19 @@ search_close_clicked (GtkWidget                 *button,
 }
 
 static gboolean
-is_keynav_event (GdkEvent *event)
-{
-  GdkModifierType state = 0;
-  guint keyval;
-
-  if (!gdk_event_get_keyval (event, &keyval))
-    return FALSE;
-
-  gdk_event_get_state (event, &state);
-
-  if (keyval == GDK_KEY_Tab       || keyval == GDK_KEY_KP_Tab ||
-      keyval == GDK_KEY_Up        || keyval == GDK_KEY_KP_Up ||
-      keyval == GDK_KEY_Down      || keyval == GDK_KEY_KP_Down ||
-      keyval == GDK_KEY_Left      || keyval == GDK_KEY_KP_Left ||
-      keyval == GDK_KEY_Right     || keyval == GDK_KEY_KP_Right ||
-      keyval == GDK_KEY_Home      || keyval == GDK_KEY_KP_Home ||
-      keyval == GDK_KEY_End       || keyval == GDK_KEY_KP_End ||
-      keyval == GDK_KEY_Page_Up   || keyval == GDK_KEY_KP_Page_Up ||
-      keyval == GDK_KEY_Page_Down || keyval == GDK_KEY_KP_Page_Down ||
-      ((state & (GDK_CONTROL_MASK | GDK_MOD1_MASK)) != 0))
-        return TRUE;
-
-  /* Other navigation events should get automatically
-   * ignored as they will not change the content of the entry
-   */
-  return FALSE;
-}
-
-static void
-preedit_changed_cb (GtkEntry  *entry,
-                    GtkWidget *popup,
-                    gboolean  *preedit_changed)
-{
-  *preedit_changed = TRUE;
-}
-
-static gboolean
 key_press_event (GtkWidget                 *window,
                  GdkEvent                  *event,
                  GtkInspectorStylePropList *pl)
 {
-  gboolean handled;
-  gboolean preedit_changed;
-  guint preedit_change_id;
-  gboolean res;
-  gchar *old_text, *new_text;
-
   if (!gtk_widget_get_mapped (GTK_WIDGET (pl)))
     return GDK_EVENT_PROPAGATE;
 
-  if (is_keynav_event (event) ||
-      event->key.keyval == GDK_KEY_space ||
-      event->key.keyval == GDK_KEY_Menu)
-    return GDK_EVENT_PROPAGATE;
-
-  if (event->key.keyval == GDK_KEY_Return ||
-      event->key.keyval == GDK_KEY_ISO_Enter ||
-      event->key.keyval == GDK_KEY_KP_Enter)
-    {
-      GtkTreeSelection *selection;
-      GtkTreeModel *model;
-      GtkTreeIter iter;
-      GtkTreePath *path;
-
-      selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (pl->priv->tree));
-      if (gtk_tree_selection_get_selected (selection, &model, &iter))
-        {
-          path = gtk_tree_model_get_path (model, &iter);
-          gtk_tree_view_row_activated (GTK_TREE_VIEW (pl->priv->tree),
-                                       path,
-                                       pl->priv->name_column);
-          gtk_tree_path_free (path);
-
-          return GDK_EVENT_STOP;
-        }
-      else
-        return GDK_EVENT_PROPAGATE;
-    }
-
-  if (event->key.keyval == GDK_KEY_Escape)
-    {
-      gtk_entry_set_text (GTK_ENTRY (pl->priv->search_entry), "");
-      gtk_stack_set_visible_child_name (GTK_STACK (pl->priv->search_stack), "title");
-      return GDK_EVENT_STOP;
-    }
-
-  if (!gtk_widget_get_realized (pl->priv->search_entry))
-    gtk_widget_realize (pl->priv->search_entry);
-
-  handled = FALSE;
-  preedit_changed = FALSE;
-  preedit_change_id = g_signal_connect (pl->priv->search_entry, "preedit-changed",
-                                        G_CALLBACK (preedit_changed_cb), &preedit_changed);
-
-  old_text = g_strdup (gtk_entry_get_text (GTK_ENTRY (pl->priv->search_entry)));
-  res = gtk_widget_event (pl->priv->search_entry, event);
-  new_text = g_strdup (gtk_entry_get_text (GTK_ENTRY (pl->priv->search_entry)));
-
-  g_signal_handler_disconnect (pl->priv->search_entry, preedit_change_id);
-
-  if ((res && g_strcmp0 (new_text, old_text) != 0) || preedit_changed)
+  if (gtk_search_entry_handle_event (GTK_SEARCH_ENTRY (pl->priv->search_entry), event))
     {
       gtk_stack_set_visible_child_name (GTK_STACK (pl->priv->search_stack), "search");
-      handled = TRUE;
+      return GDK_EVENT_STOP;
     }
-
-  g_free (old_text);
-  g_free (new_text);
-
-  return handled ? GDK_EVENT_STOP : GDK_EVENT_PROPAGATE;
+  return GDK_EVENT_PROPAGATE;
 }
 
 static void
@@ -210,8 +113,6 @@ gtk_inspector_style_prop_list_init (GtkInspectorStylePropList *pl)
                                                 NULL,
                                                 (GDestroyNotify) gtk_tree_iter_free);
 
-  g_signal_connect (pl, "hierarchy-changed", G_CALLBACK (hierarchy_changed), NULL);
-
   for (i = 0; i < _gtk_css_style_property_get_n_properties (); i++)
     {
       GtkCssStyleProperty *prop;
@@ -284,6 +185,7 @@ gtk_inspector_style_prop_list_class_init (GtkInspectorStylePropListClass *klass)
   gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorStylePropList, object_title);
   gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorStylePropList, name_column);
   gtk_widget_class_bind_template_callback (widget_class, search_close_clicked);
+  gtk_widget_class_bind_template_callback (widget_class, hierarchy_changed);
 }
 
 static gchar *
diff --git a/gtk/inspector/style-prop-list.ui b/gtk/inspector/style-prop-list.ui
index 5105cbf..1ba5099 100644
--- a/gtk/inspector/style-prop-list.ui
+++ b/gtk/inspector/style-prop-list.ui
@@ -11,6 +11,7 @@
   </object>
   <template class="GtkInspectorStylePropList" parent="GtkBox">
     <property name="orientation">vertical</property>
+    <signal name="hierarchy-changed" handler="hierarchy_changed"/>
     <child>
       <object class="GtkStack" id="search_stack">
         <property name="visible">True</property>
@@ -27,6 +28,7 @@
               <object class="GtkSearchEntry" id="search_entry">
                 <property name="visible">True</property>
                 <property name="max-width-chars">40</property>
+                <signal name="stop-search" handler="search_close_clicked"/>
               </object>
             </child>
             <child>


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