[gtk/wip/otte/for-master: 1/2] stringfilter: Make the constructor take an expression



commit fb7e62744e8ebaee897d9e6dad392be4341e6f56
Author: Benjamin Otte <otte redhat com>
Date:   Sun Jul 12 07:02:04 2020 +0200

    stringfilter: Make the constructor take an expression
    
    An expression is critically important for a string filter and people
    should be made aware of it when constructing the filter.

 demos/gtk-demo/listview_settings.c         |  6 +---
 demos/gtk-demo/listview_words.c            |  6 +---
 gtk/gtkdropdown.c                          |  3 +-
 gtk/gtkprintunixdialog.c                   | 13 ++++-----
 gtk/gtkstringfilter.c                      | 16 +++++++++--
 gtk/gtkstringfilter.h                      |  8 +++---
 gtk/inspector/prop-list.c                  |  2 +-
 testsuite/gtk/expression.c                 | 46 +++++++++++++++---------------
 testsuite/gtk/filter.c                     | 32 ++++++++-------------
 testsuite/gtk/filterlistmodel-exhaustive.c | 10 ++-----
 10 files changed, 64 insertions(+), 78 deletions(-)
---
diff --git a/demos/gtk-demo/listview_settings.c b/demos/gtk-demo/listview_settings.c
index 3c0de54d9f..7dc55ada2f 100644
--- a/demos/gtk-demo/listview_settings.c
+++ b/demos/gtk-demo/listview_settings.c
@@ -215,7 +215,6 @@ transform_settings_to_keys (GBinding     *binding,
   GtkFilterListModel *filter_model;
   GtkFilter *filter;
   GtkNoSelection *selection_model;
-  GtkExpression *expression;
   char **keys;
   guint i;
 
@@ -246,11 +245,8 @@ transform_settings_to_keys (GBinding     *binding,
                                         gtk_column_view_get_sorter (GTK_COLUMN_VIEW (data)));
   g_object_unref (store);
 
-  expression = gtk_property_expression_new (SETTINGS_TYPE_KEY, NULL, "name");
-  filter = gtk_string_filter_new ();
-  gtk_string_filter_set_expression (GTK_STRING_FILTER (filter), expression);
+  filter = gtk_string_filter_new (gtk_property_expression_new (SETTINGS_TYPE_KEY, NULL, "name"));
   filter_model = gtk_filter_list_model_new (G_LIST_MODEL (sort_model), filter);
-  gtk_expression_unref (expression);
   g_object_unref (sort_model);
 
   g_set_object (&current_filter, filter);
diff --git a/demos/gtk-demo/listview_words.c b/demos/gtk-demo/listview_words.c
index be556696cf..95664ed279 100644
--- a/demos/gtk-demo/listview_words.c
+++ b/demos/gtk-demo/listview_words.c
@@ -160,7 +160,6 @@ do_listview_words (GtkWidget *do_widget)
       GtkNoSelection *selection;
       GtkStringList *stringlist;
       GtkFilter *filter;
-      GtkExpression *expression;
       GFile *file;
 
       file = g_file_new_for_path ("/usr/share/dict/words");
@@ -177,10 +176,7 @@ do_listview_words (GtkWidget *do_widget)
           g_strfreev (words);
         }
 
-      filter = gtk_string_filter_new ();
-      expression = gtk_property_expression_new (GTK_TYPE_STRING_OBJECT, NULL, "string");
-      gtk_string_filter_set_expression (GTK_STRING_FILTER (filter), expression);
-      gtk_expression_unref (expression);
+      filter = gtk_string_filter_new (gtk_property_expression_new (GTK_TYPE_STRING_OBJECT, NULL, "string"));
       filter_model = gtk_filter_list_model_new (G_LIST_MODEL (stringlist), filter);
       gtk_filter_list_model_set_incremental (filter_model, TRUE);
 
diff --git a/gtk/gtkdropdown.c b/gtk/gtkdropdown.c
index f89fb11391..de1ba763cb 100644
--- a/gtk/gtkdropdown.c
+++ b/gtk/gtkdropdown.c
@@ -205,9 +205,8 @@ update_filter (GtkDropDown *self)
 
       if (self->expression)
         {
-          filter = gtk_string_filter_new ();
+          filter = gtk_string_filter_new (gtk_expression_ref (self->expression));
           gtk_string_filter_set_match_mode (GTK_STRING_FILTER (filter), GTK_STRING_FILTER_MATCH_MODE_PREFIX);
-          gtk_string_filter_set_expression (GTK_STRING_FILTER (filter), self->expression);
         }
       else
         filter = gtk_every_filter_new ();
diff --git a/gtk/gtkprintunixdialog.c b/gtk/gtkprintunixdialog.c
index dab4548b49..681dc04272 100644
--- a/gtk/gtkprintunixdialog.c
+++ b/gtk/gtkprintunixdialog.c
@@ -744,7 +744,6 @@ gtk_print_unix_dialog_init (GtkPrintUnixDialog *dialog)
   GtkSorter *sorter;
   GtkFilter *filter;
   GtkFilter *filter1;
-  GtkExpression *expression;
   GtkListItemFactory *factory;
   GListStore *store;
   GListModel *paper_size_list;
@@ -819,15 +818,13 @@ gtk_print_unix_dialog_init (GtkPrintUnixDialog *dialog)
 
   filter = gtk_every_filter_new ();
 
-  filter1 = gtk_string_filter_new ();
+  filter1 = gtk_string_filter_new (
+                gtk_cclosure_expression_new (G_TYPE_STRING,
+                                             NULL, 0, NULL,
+                                             G_CALLBACK (get_printer_key),
+                                             NULL, NULL));
   gtk_string_filter_set_match_mode (GTK_STRING_FILTER (filter1), GTK_STRING_FILTER_MATCH_MODE_SUBSTRING);
   gtk_string_filter_set_ignore_case (GTK_STRING_FILTER (filter1), TRUE);
-  expression = gtk_cclosure_expression_new (G_TYPE_STRING,
-                                            NULL, 0, NULL,
-                                            G_CALLBACK (get_printer_key),
-                                            NULL, NULL);
-  gtk_string_filter_set_expression (GTK_STRING_FILTER (filter1), expression);
-  gtk_expression_unref (expression);
   gtk_multi_filter_append (GTK_MULTI_FILTER (filter), filter1);
 
   filter1 = gtk_custom_filter_new (is_printer_active, dialog, NULL);
diff --git a/gtk/gtkstringfilter.c b/gtk/gtkstringfilter.c
index 3fa924e9c3..df45fe4f2a 100644
--- a/gtk/gtkstringfilter.c
+++ b/gtk/gtkstringfilter.c
@@ -302,7 +302,9 @@ gtk_string_filter_init (GtkStringFilter *self)
 }
 
 /**
- * gtk_string_filter_new:
+ * gtk_s tring_filter_new:
+ * @expression: (transfer full) (nullable): The expression to evaluate
+ *     or %NULL for none
  *
  * Creates a new string filter.
  *
@@ -312,9 +314,17 @@ gtk_string_filter_init (GtkStringFilter *self)
  * Returns: a new #GtkStringFilter
  **/
 GtkFilter *
-gtk_string_filter_new (void)
+gtk_string_filter_new (GtkExpression *expression)
 {
-  return g_object_new (GTK_TYPE_STRING_FILTER, NULL);
+  GtkFilter *result;
+
+  result = g_object_new (GTK_TYPE_STRING_FILTER,
+                         "expression", expression,
+                         NULL);
+
+  g_clear_pointer (&expression, gtk_expression_unref);
+
+  return result;
 }
 
 /**
diff --git a/gtk/gtkstringfilter.h b/gtk/gtkstringfilter.h
index 2c5e866bac..fece96e7ae 100644
--- a/gtk/gtkstringfilter.h
+++ b/gtk/gtkstringfilter.h
@@ -51,7 +51,7 @@ GDK_AVAILABLE_IN_ALL
 G_DECLARE_FINAL_TYPE (GtkStringFilter, gtk_string_filter, GTK, STRING_FILTER, GtkFilter)
 
 GDK_AVAILABLE_IN_ALL
-GtkFilter *             gtk_string_filter_new                   (void);
+GtkFilter *             gtk_string_filter_new                   (GtkExpression          *exporession);
 
 GDK_AVAILABLE_IN_ALL
 const char *            gtk_string_filter_get_search            (GtkStringFilter        *self);
@@ -69,10 +69,10 @@ GDK_AVAILABLE_IN_ALL
 void                    gtk_string_filter_set_ignore_case       (GtkStringFilter        *self,
                                                                  gboolean                ignore_case);
 GDK_AVAILABLE_IN_ALL
-GtkStringFilterMatchMode gtk_string_filter_get_match_mode   (GtkStringFilter          *self);
+GtkStringFilterMatchMode gtk_string_filter_get_match_mode       (GtkStringFilter        *self);
 GDK_AVAILABLE_IN_ALL
-void                     gtk_string_filter_set_match_mode   (GtkStringFilter          *self,
-                                                             GtkStringFilterMatchMode  mode);
+void                     gtk_string_filter_set_match_mode       (GtkStringFilter        *self,
+                                                                 GtkStringFilterMatchMode mode);
 
 
 
diff --git a/gtk/inspector/prop-list.c b/gtk/inspector/prop-list.c
index 44a3e8ce0b..b30fe7a740 100644
--- a/gtk/inspector/prop-list.c
+++ b/gtk/inspector/prop-list.c
@@ -113,7 +113,7 @@ gtk_inspector_prop_list_init (GtkInspectorPropList *pl)
 
   pl->priv = gtk_inspector_prop_list_get_instance_private (pl);
   gtk_widget_init_template (GTK_WIDGET (pl));
-  pl->priv->filter = gtk_string_filter_new ();
+  pl->priv->filter = gtk_string_filter_new (NULL);
   gtk_string_filter_set_match_mode (GTK_STRING_FILTER (pl->priv->filter), 
GTK_STRING_FILTER_MATCH_MODE_SUBSTRING);
 
   sorter = gtk_string_sorter_new (gtk_cclosure_expression_new (G_TYPE_STRING, NULL,
diff --git a/testsuite/gtk/expression.c b/testsuite/gtk/expression.c
index 08b6e4d97d..d5a8a07802 100644
--- a/testsuite/gtk/expression.c
+++ b/testsuite/gtk/expression.c
@@ -38,7 +38,7 @@ test_property (void)
   GtkStringFilter *filter;
   guint counter = 0;
 
-  filter = GTK_STRING_FILTER (gtk_string_filter_new ());
+  filter = GTK_STRING_FILTER (gtk_string_filter_new (NULL));
   expr = gtk_property_expression_new (GTK_TYPE_STRING_FILTER, NULL, "search");
   watch = gtk_expression_watch (expr, filter, inc_counter, &counter, NULL);
 
@@ -83,7 +83,7 @@ test_closure (void)
   GtkStringFilter *filter;
   guint counter = 0;
 
-  filter = GTK_STRING_FILTER (gtk_string_filter_new ());
+  filter = GTK_STRING_FILTER (gtk_string_filter_new (NULL));
   pexpr[0] = gtk_property_expression_new (GTK_TYPE_STRING_FILTER, NULL, "search");
   pexpr[1] = gtk_property_expression_new (GTK_TYPE_STRING_FILTER, NULL, "ignore-case");
   pexpr[2] = gtk_property_expression_new (GTK_TYPE_STRING_FILTER, NULL, "match-mode");
@@ -156,7 +156,7 @@ test_object (void)
   GValue value = G_VALUE_INIT;
   gboolean res;
 
-  obj = G_OBJECT (gtk_string_filter_new ());
+  obj = G_OBJECT (gtk_string_filter_new (NULL));
 
   expr = gtk_object_expression_new (obj);
   g_assert_true (!gtk_expression_is_static (expr));
@@ -200,7 +200,7 @@ test_nested (void)
   GtkExpressionWatch *watch;
   guint counter = 0;
 
-  filter = gtk_string_filter_new ();
+  filter = gtk_string_filter_new (NULL);
   gtk_string_filter_set_search (GTK_STRING_FILTER (filter), "word");
   list = G_LIST_MODEL (g_list_store_new (G_TYPE_OBJECT));
   filtered = gtk_filter_list_model_new (list, filter);
@@ -231,7 +231,7 @@ test_nested (void)
   g_assert_cmpint (counter, ==, 0);
 
   g_clear_object (&filter);
-  filter = gtk_string_filter_new ();
+  filter = gtk_string_filter_new (NULL);
   gtk_string_filter_set_search (GTK_STRING_FILTER (filter), "salad");
   gtk_filter_list_model_set_filter (filtered, filter);
   g_assert_cmpint (counter, ==, 1);
@@ -289,7 +289,7 @@ test_nested_this_destroyed (void)
   GtkExpressionWatch *watch;
   guint counter = 0;
 
-  filter = gtk_string_filter_new ();
+  filter = gtk_string_filter_new (NULL);
   gtk_string_filter_set_search (GTK_STRING_FILTER (filter), "word");
   list = G_LIST_MODEL (g_list_store_new (G_TYPE_OBJECT));
   filtered = gtk_filter_list_model_new (list, filter);
@@ -308,7 +308,7 @@ test_nested_this_destroyed (void)
   g_clear_object (&filter);
   g_assert_cmpint (counter, ==, 0);
 
-  filter = gtk_string_filter_new ();
+  filter = gtk_string_filter_new (NULL);
   gtk_string_filter_set_search (GTK_STRING_FILTER (filter), "salad");
   gtk_filter_list_model_set_filter (filtered, filter);
   g_assert_cmpint (counter, ==, 1);
@@ -373,10 +373,10 @@ test_this (void)
 
   expr = gtk_property_expression_new (GTK_TYPE_STRING_FILTER, NULL, "search");
 
-  filter = gtk_string_filter_new ();
+  filter = gtk_string_filter_new (NULL);
   gtk_string_filter_set_search (GTK_STRING_FILTER (filter), "word");
 
-  filter2 = gtk_string_filter_new ();
+  filter2 = gtk_string_filter_new (NULL);
   gtk_string_filter_set_search (GTK_STRING_FILTER (filter2), "sausage");
 
   res = gtk_expression_evaluate (expr, filter, &value);
@@ -429,11 +429,11 @@ test_bind (void)
 
   expr = gtk_property_expression_new (GTK_TYPE_STRING_FILTER, NULL, "search");
 
-  target = gtk_string_filter_new ();
+  target = gtk_string_filter_new (NULL);
   gtk_string_filter_set_search (GTK_STRING_FILTER (target), "word");
   g_assert_cmpstr (gtk_string_filter_get_search (GTK_STRING_FILTER (target)), ==, "word");
 
-  source = gtk_string_filter_new ();
+  source = gtk_string_filter_new (NULL);
   gtk_string_filter_set_search (GTK_STRING_FILTER (source), "sausage");
 
   watch = gtk_expression_bind (expr, target, "search", source);
@@ -468,7 +468,7 @@ test_bind_self (void)
                                       NULL,
                                       "ignore-case");
 
-  filter = gtk_string_filter_new ();
+  filter = gtk_string_filter_new (NULL);
   gtk_string_filter_set_search (GTK_STRING_FILTER (filter), "word");
   g_assert_cmpstr (gtk_string_filter_get_search (GTK_STRING_FILTER (filter)), ==, "word");
 
@@ -491,7 +491,7 @@ test_bind_child (void)
                                       NULL,
                                       "filter");
 
-  filter = gtk_string_filter_new ();
+  filter = gtk_string_filter_new (NULL);
   child = gtk_filter_list_model_new (NULL, NULL);
   gtk_filter_list_model_set_filter (child, filter);
   target = gtk_filter_list_model_new (G_LIST_MODEL (child), NULL);
@@ -501,7 +501,7 @@ test_bind_child (void)
   gtk_expression_bind (expr, target, "filter", child);
   g_assert_true (gtk_filter_list_model_get_filter (child) == gtk_filter_list_model_get_filter (target));
 
-  filter = gtk_string_filter_new ();
+  filter = gtk_string_filter_new (NULL);
   gtk_filter_list_model_set_filter (child, filter);
   g_assert_true (filter == gtk_filter_list_model_get_filter (target));
   g_assert_true (gtk_filter_list_model_get_filter (child) == gtk_filter_list_model_get_filter (target));
@@ -524,7 +524,7 @@ test_nested_bind (void)
   gboolean res;
   GValue value = G_VALUE_INIT;
 
-  filter2 = gtk_string_filter_new ();
+  filter2 = gtk_string_filter_new (NULL);
   gtk_string_filter_set_search (GTK_STRING_FILTER (filter2), "sausage");
 
   list = G_LIST_MODEL (g_list_store_new (G_TYPE_OBJECT));
@@ -535,7 +535,7 @@ test_nested_bind (void)
                                              "filter");
   expr = gtk_property_expression_new (GTK_TYPE_STRING_FILTER, gtk_expression_ref (filter_expr), "search");
 
-  filter = gtk_string_filter_new ();
+  filter = gtk_string_filter_new (NULL);
   gtk_string_filter_set_search (GTK_STRING_FILTER (filter), "word");
   g_assert_cmpstr (gtk_string_filter_get_search (GTK_STRING_FILTER (filter)), ==, "word");
 
@@ -544,7 +544,7 @@ test_nested_bind (void)
   gtk_string_filter_set_search (GTK_STRING_FILTER (filter2), "sausage");
   g_assert_cmpstr (gtk_string_filter_get_search (GTK_STRING_FILTER (filter)), ==, "sausage");
 
-  filter3 = gtk_string_filter_new ();
+  filter3 = gtk_string_filter_new (NULL);
   gtk_string_filter_set_search (GTK_STRING_FILTER (filter3), "banana");
   gtk_filter_list_model_set_filter (filtered, filter3);
 
@@ -599,8 +599,8 @@ test_double_bind (void)
   GtkExpression *filter_expr;
   GtkExpression *params[2];
 
-  filter1 = GTK_STRING_FILTER (gtk_string_filter_new ());
-  filter2 = GTK_STRING_FILTER (gtk_string_filter_new ());
+  filter1 = GTK_STRING_FILTER (gtk_string_filter_new (NULL));
+  filter2 = GTK_STRING_FILTER (gtk_string_filter_new (NULL));
 
   filter_expr = gtk_object_expression_new (G_OBJECT (filter1));
 
@@ -640,9 +640,9 @@ test_binds (void)
   GtkExpression *filter2_expr;
   GtkExpression *params[2];
 
-  filter1 = GTK_STRING_FILTER (gtk_string_filter_new ());
-  filter2 = GTK_STRING_FILTER (gtk_string_filter_new ());
-  filter3 = GTK_STRING_FILTER (gtk_string_filter_new ());
+  filter1 = GTK_STRING_FILTER (gtk_string_filter_new (NULL));
+  filter2 = GTK_STRING_FILTER (gtk_string_filter_new (NULL));
+  filter3 = GTK_STRING_FILTER (gtk_string_filter_new (NULL));
 
   filter1_expr = gtk_object_expression_new (G_OBJECT (filter1));
   filter2_expr = gtk_object_expression_new (G_OBJECT (filter2));
@@ -693,7 +693,7 @@ test_bind_object (void)
   GtkFilterListModel *model;
   GtkExpression *expr;
 
-  filter = gtk_string_filter_new ();
+  filter = gtk_string_filter_new (NULL);
   store = g_list_store_new (G_TYPE_OBJECT);
   model = gtk_filter_list_model_new (G_LIST_MODEL (store), NULL);
 
diff --git a/testsuite/gtk/filter.c b/testsuite/gtk/filter.c
index 5c5fd707c7..09b2ca346d 100644
--- a/testsuite/gtk/filter.c
+++ b/testsuite/gtk/filter.c
@@ -249,16 +249,13 @@ test_string_simple (void)
 {
   GtkFilterListModel *model;
   GtkFilter *filter;
-  GtkExpression *expr;
 
-  expr = gtk_cclosure_expression_new (G_TYPE_STRING,
-                                      NULL,
-                                      0, NULL,
-                                      G_CALLBACK (get_string),
-                                      NULL, NULL);
-
-  filter = gtk_string_filter_new ();
-  gtk_string_filter_set_expression (GTK_STRING_FILTER (filter), expr);
+  filter = gtk_string_filter_new (
+               gtk_cclosure_expression_new (G_TYPE_STRING,
+                                            NULL,
+                                            0, NULL,
+                                            G_CALLBACK (get_string),
+                                            NULL, NULL));
 
   model = new_model (20, filter);
   assert_model (model, "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20");
@@ -268,7 +265,6 @@ test_string_simple (void)
 
   g_object_unref (model);
   g_object_unref (filter);
-  gtk_expression_unref (expr);
 }
 
 static void
@@ -276,16 +272,13 @@ test_string_properties (void)
 {
   GtkFilterListModel *model;
   GtkFilter *filter;
-  GtkExpression *expr;
-
-  expr = gtk_cclosure_expression_new (G_TYPE_STRING,
-                                      NULL,
-                                      0, NULL,
-                                      G_CALLBACK (get_spelled_out),
-                                      NULL, NULL);
 
-  filter = gtk_string_filter_new ();
-  gtk_string_filter_set_expression (GTK_STRING_FILTER (filter), expr);
+  filter = gtk_string_filter_new (
+               gtk_cclosure_expression_new (G_TYPE_STRING,
+                                            NULL,
+                                            0, NULL,
+                                            G_CALLBACK (get_spelled_out),
+                                            NULL, NULL));
 
   model = new_model (1000, filter);
   gtk_string_filter_set_search (GTK_STRING_FILTER (filter), "thirte");
@@ -311,7 +304,6 @@ test_string_properties (void)
 
   g_object_unref (model);
   g_object_unref (filter);
-  gtk_expression_unref (expr);
 }
 
 static void
diff --git a/testsuite/gtk/filterlistmodel-exhaustive.c b/testsuite/gtk/filterlistmodel-exhaustive.c
index 4bf54c1d57..76d2a9dd4c 100644
--- a/testsuite/gtk/filterlistmodel-exhaustive.c
+++ b/testsuite/gtk/filterlistmodel-exhaustive.c
@@ -206,17 +206,16 @@ static GtkFilter *
 create_filter (gsize id)
 {
   GtkFilter *filter;
-  GtkExpression *expr;
 
   switch (id)
   {
     case 0:
       /* GTK_FILTER_MATCH_ALL */
-      return gtk_string_filter_new ();
+      return gtk_string_filter_new (NULL);
 
     case 1:
       /* GTK_FILTER_MATCH_NONE */
-      filter = gtk_string_filter_new ();
+      filter = gtk_string_filter_new (NULL);
       gtk_string_filter_set_search (GTK_STRING_FILTER (filter), "does not matter, because no expression");
       return filter;
 
@@ -224,10 +223,7 @@ create_filter (gsize id)
     case 3:
     case 4:
       /* match all As, Bs and nothing */
-      filter = gtk_string_filter_new ();
-      expr = gtk_property_expression_new (GTK_TYPE_STRING_OBJECT, NULL, "string");
-      gtk_string_filter_set_expression (GTK_STRING_FILTER (filter), expr);
-      gtk_expression_unref (expr);
+      filter = gtk_string_filter_new (gtk_property_expression_new (GTK_TYPE_STRING_OBJECT, NULL, "string"));
       if (id == 2)
         gtk_string_filter_set_search (GTK_STRING_FILTER (filter), "A");
       else if (id == 3)


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