[gtk/wip/otte/icontheme: 24/26] icontheme: Pass fallbacks as optional argument to lookup_icon()



commit 571021cbc1f8480585db3bbe452578c6b4f880d1
Author: Benjamin Otte <otte redhat com>
Date:   Tue Feb 4 03:53:22 2020 +0100

    icontheme: Pass fallbacks as optional argument to lookup_icon()
    
    This way, we can remove gtk_icon_theme_choose_icon() completely.

 demos/gtk-demo/clipboard.c           |  1 +
 demos/gtk-demo/textview.c            |  1 +
 demos/icon-browser/iconbrowserwin.c  |  2 +
 docs/reference/gtk/gtk4-sections.txt |  1 -
 gtk/gtkbuilder.c                     |  2 +-
 gtk/gtkcalendar.c                    |  1 +
 gtk/gtkcssimageicontheme.c           |  2 +
 gtk/gtkdragsource.c                  |  1 +
 gtk/gtkiconhelper.c                  |  1 +
 gtk/gtkicontheme.c                   | 98 +++++++++---------------------------
 gtk/gtkicontheme.h                   | 10 +---
 gtk/gtkmountoperation.c              |  1 +
 gtk/gtkwindow.c                      |  6 +--
 tests/testclipboard2.c               |  1 +
 tests/testdnd2.c                     |  2 +
 tests/testicontheme.c                |  4 +-
 testsuite/gtk/check-icon-names.c     |  2 +-
 testsuite/gtk/icontheme.c            |  6 +--
 18 files changed, 50 insertions(+), 92 deletions(-)
---
diff --git a/demos/gtk-demo/clipboard.c b/demos/gtk-demo/clipboard.c
index 2edd61c462..63c6519a9b 100644
--- a/demos/gtk-demo/clipboard.c
+++ b/demos/gtk-demo/clipboard.c
@@ -109,6 +109,7 @@ get_image_paintable (GtkImage *image)
       icon_theme = gtk_icon_theme_get_for_display (gtk_widget_get_display (GTK_WIDGET (image)));
       icon = gtk_icon_theme_lookup_icon (icon_theme,
                                          icon_name,
+                                         NULL,
                                          48, 1,
                                          gtk_widget_get_direction (GTK_WIDGET (image)),
                                          GTK_ICON_LOOKUP_GENERIC_FALLBACK);
diff --git a/demos/gtk-demo/textview.c b/demos/gtk-demo/textview.c
index cdc9e1b6d4..efe46e62be 100644
--- a/demos/gtk-demo/textview.c
+++ b/demos/gtk-demo/textview.c
@@ -137,6 +137,7 @@ insert_text (GtkTextView *view)
   icon_theme = gtk_icon_theme_get_for_display (gtk_widget_get_display (widget));
   icon = gtk_icon_theme_lookup_icon (icon_theme,
                                      "gtk3-demo",
+                                     NULL,
                                      32, 1,
                                      gtk_widget_get_direction (widget),
                                      GTK_ICON_LOOKUP_GENERIC_FALLBACK);
diff --git a/demos/icon-browser/iconbrowserwin.c b/demos/icon-browser/iconbrowserwin.c
index c133a3ee64..3a6e1a596a 100644
--- a/demos/icon-browser/iconbrowserwin.c
+++ b/demos/icon-browser/iconbrowserwin.c
@@ -369,6 +369,7 @@ get_image_paintable (GtkImage *image)
       icon_theme = gtk_icon_theme_get_for_display (gtk_widget_get_display (GTK_WIDGET (image)));
       icon = gtk_icon_theme_lookup_icon (icon_theme,
                                          icon_name,
+                                         NULL,
                                          size, 1,
                                          gtk_widget_get_direction (GTK_WIDGET (image)),
                                          GTK_ICON_LOOKUP_FORCE_SIZE | GTK_ICON_LOOKUP_GENERIC_FALLBACK);
@@ -425,6 +426,7 @@ get_file (GValue   *value,
 
   info = gtk_icon_theme_lookup_icon (icon_theme,
                                      name,
+                                     NULL,
                                      32, 1,
                                      gtk_widget_get_direction (GTK_WIDGET (data)),
                                      0);
diff --git a/docs/reference/gtk/gtk4-sections.txt b/docs/reference/gtk/gtk4-sections.txt
index e57e2f6e95..61023e0186 100644
--- a/docs/reference/gtk/gtk4-sections.txt
+++ b/docs/reference/gtk/gtk4-sections.txt
@@ -5005,7 +5005,6 @@ gtk_icon_theme_add_resource_path
 gtk_icon_theme_set_custom_theme
 gtk_icon_theme_has_icon
 gtk_icon_theme_lookup_icon
-gtk_icon_theme_choose_icon
 gtk_icon_theme_choose_icon_async
 gtk_icon_theme_choose_icon_finish
 gtk_icon_theme_lookup_by_gicon
diff --git a/gtk/gtkbuilder.c b/gtk/gtkbuilder.c
index 6ad3dc5c90..91da927da3 100644
--- a/gtk/gtkbuilder.c
+++ b/gtk/gtkbuilder.c
@@ -2171,7 +2171,7 @@ gtk_builder_value_from_string_type (GtkBuilder   *builder,
               /* FIXME: this should be using the correct display */
               theme = gtk_icon_theme_get_for_display (gdk_display_get_default ());
 
-              icon = gtk_icon_theme_lookup_icon (theme, "image-missing", 16, 1,
+              icon = gtk_icon_theme_lookup_icon (theme, "image-missing", NULL, 16, 1,
                                                  GTK_TEXT_DIR_NONE,
                                                  0);
               texture = gtk_icon_download_texture (icon, NULL);
diff --git a/gtk/gtkcalendar.c b/gtk/gtkcalendar.c
index 837daad398..1c01906f23 100644
--- a/gtk/gtkcalendar.c
+++ b/gtk/gtkcalendar.c
@@ -2606,6 +2606,7 @@ gtk_calendar_drag_update (GtkGestureDrag *gesture,
   theme = gtk_icon_theme_get_for_display (gtk_widget_get_display (widget));
   icon = gtk_icon_theme_lookup_icon (theme,
                                      "text-x-generic",
+                                     NULL,
                                      32,
                                      1,
                                      GTK_TEXT_DIR_NONE,
diff --git a/gtk/gtkcssimageicontheme.c b/gtk/gtkcssimageicontheme.c
index ee7db6f997..1a7a1f686b 100644
--- a/gtk/gtkcssimageicontheme.c
+++ b/gtk/gtkcssimageicontheme.c
@@ -63,6 +63,7 @@ gtk_css_image_icon_theme_snapshot (GtkCssImage *image,
     {
       icon = gtk_icon_theme_lookup_icon (icon_theme->icon_theme,
                                          icon_theme->name,
+                                         NULL,
                                          size,
                                          icon_theme->scale,
                                          GTK_TEXT_DIR_NONE,
@@ -70,6 +71,7 @@ gtk_css_image_icon_theme_snapshot (GtkCssImage *image,
       if (icon == NULL)
         icon = gtk_icon_theme_lookup_icon (icon_theme->icon_theme,
                                            "image-missing",
+                                           NULL,
                                            size, icon_theme->scale,
                                            GTK_TEXT_DIR_NONE,
                                            0 | GTK_ICON_LOOKUP_GENERIC_FALLBACK);
diff --git a/gtk/gtkdragsource.c b/gtk/gtkdragsource.c
index 8799643030..cda9754b54 100644
--- a/gtk/gtkdragsource.c
+++ b/gtk/gtkdragsource.c
@@ -488,6 +488,7 @@ gtk_drag_source_drag_begin (GtkDragSource *source)
       theme = gtk_icon_theme_get_for_display (gtk_widget_get_display (widget));
       source->paintable = GDK_PAINTABLE(gtk_icon_theme_lookup_icon (theme,
                                                                     "text-x-generic",
+                                                                    NULL,
                                                                     32,
                                                                     1,
                                                                     gtk_widget_get_direction (widget),
diff --git a/gtk/gtkiconhelper.c b/gtk/gtkiconhelper.c
index 8835a0ee66..08dafad5f8 100644
--- a/gtk/gtkiconhelper.c
+++ b/gtk/gtkiconhelper.c
@@ -112,6 +112,7 @@ ensure_paintable_for_gicon (GtkIconHelper    *self,
   if (icon == NULL)
     icon = gtk_icon_theme_lookup_icon (icon_theme,
                                        "image-missing",
+                                       NULL,
                                        width, scale,
                                        dir,
                                        flags | GTK_ICON_LOOKUP_GENERIC_FALLBACK);
diff --git a/gtk/gtkicontheme.c b/gtk/gtkicontheme.c
index f23572070d..86a3c9f70f 100644
--- a/gtk/gtkicontheme.c
+++ b/gtk/gtkicontheme.c
@@ -2148,6 +2148,7 @@ choose_icon (GtkIconTheme      *self,
  * gtk_icon_theme_lookup_icon:
  * @self: a #GtkIconTheme
  * @icon_name: the name of the icon to lookup
+ * @fallbacks: (nullable) (array zero-terminated=1): 
  * @size: desired icon size.  The resulting icon may not be exactly this size.
  * @scale: the window scale this will be displayed on
  * @direction: text direction the icon will be displayed in
@@ -2167,20 +2168,20 @@ choose_icon (GtkIconTheme      *self,
  * passing the %GTK_ICON_LOOKUP_FORCE_SIZE flag, which causes the icon
  * to be scaled to the exact size.
  *
+ * If the available @icon_name is not available and @fallbacks are provided,
+ * they will be tried in order.
+ *
  * Note that you probably want to listen for icon theme changes and
  * update the icon. This is usually done by connecting to the
  * GtkWidget::style-updated signal.
  *
- * Often you want to look up multiple icons at the same time, as fallbacks.
- * For this use the gtk_icon_theme_choose_icon() method is a better option.
- *
  * Returns: (nullable) (transfer full): a #GtkIcon object
- *     containing information about the icon, or %NULL if the
- *     icon wasn’t found.
+ *     containing the icon, or %NULL if the icon wasn’t found.
  */
 GtkIcon *
 gtk_icon_theme_lookup_icon (GtkIconTheme       *self,
-                            const gchar        *icon_name,
+                            const char         *icon_name,
+                            const char         *fallbacks[],
                             gint                size,
                             gint                scale,
                             GtkTextDirection    direction,
@@ -2205,6 +2206,8 @@ gtk_icon_theme_lookup_icon (GtkIconTheme       *self,
       gboolean is_symbolic;
       int icon_name_len = strlen (icon_name);
 
+      g_warn_if_fail (fallbacks == NULL);
+
       is_symbolic = icon_name_is_symbolic (icon_name, icon_name_len);
       if (is_symbolic)
         nonsymbolic_icon_name = g_strndup (icon_name, icon_name_len - strlen ("-symbolic"));
@@ -2244,10 +2247,23 @@ gtk_icon_theme_lookup_icon (GtkIconTheme       *self,
           names = nonsymbolic_names;
         }
 
-      icon = choose_icon (self, (const gchar **) names, size, scale, direction, flags, FALSE, NULL);
+      icon = choose_icon (self, (const char **) names, size, scale, direction, flags, FALSE, NULL);
 
       g_strfreev (names);
     }
+  else if (fallbacks)
+    {
+      gsize n_fallbacks = g_strv_length ((char **) fallbacks);
+      const char **names = g_new (const char *, n_fallbacks + 2);
+
+      names[0] = icon_name;
+      memcpy (&names[1], fallbacks, sizeof (char *) * n_fallbacks);
+      names[n_fallbacks + 1] = NULL;
+
+      icon = choose_icon (self, names, size, scale, direction, flags, FALSE, NULL);
+
+      g_free (names);
+    }
   else
     {
       const gchar *names[2];
@@ -2263,70 +2279,6 @@ gtk_icon_theme_lookup_icon (GtkIconTheme       *self,
   return icon;
 }
 
-/**
- * gtk_icon_theme_choose_icon:
- * @self: a #GtkIconTheme
- * @icon_names: (array zero-terminated=1): %NULL-terminated
- *     array of icon names to lookup
- * @size: desired icon size. The resulting icon may not be exactly this size.
- * @scale: the window scale this will be displayed on
- * @direction: text direction the icon will be displayed in
- * @flags: flags modifying the behavior of the icon lookup
- *
- * Looks up a named icon for a desired size and window scale, returning a
- * #GtkIcon. The icon can then be rendered by using it as a #GdkPaintable,
- * or you can get information such as the filename and size. The pixels
- * of the texture can be access by using gtk_icon_download_texture().
- *
- * If @icon_names contains more than one name, this function
- * tries them all in the given order before falling back to
- * inherited icon themes.
- *
- * The icon icon size will be based on the requested @size, but may
- * not be exactly this size; an icon theme may have icons that differ
- * slightly from their nominal sizes, and in addition GTK+ will avoid
- * scaling icons that it considers sufficiently close to the requested
- * size or for which the source image would have to be scaled up too
- * far. (This maintains sharpness.). This behaviour can be changed by
- * passing the %GTK_ICON_LOOKUP_FORCE_SIZE flag, which causes the icon
- * to be scaled to the exact size.
- *
- * Note that you probably want to listen for icon theme changes and
- * update the icon. This is usually done by connecting to the
- * GtkWidget::style-updated signal.
- *
- * This function does not support the flag
- * GTK_ICON_LOOKUP_GENERIC_FALLBACK, that only works with the simpler
- * gtk_icon_theme_lookup_icon() method.
- *
- * Returns: (nullable) (transfer full): a #GtkIcon object
- *     containing information about the icon, or %NULL if the
- *     icon wasn’t found.
- */
-GtkIcon *
-gtk_icon_theme_choose_icon (GtkIconTheme       *self,
-                            const gchar        *icon_names[],
-                            gint                size,
-                            gint                scale,
-                            GtkTextDirection    direction,
-                            GtkIconLookupFlags  flags)
-{
-  GtkIcon *icon;
-
-  g_return_val_if_fail (GTK_IS_ICON_THEME (self), NULL);
-  g_return_val_if_fail (icon_names != NULL, NULL);
-  g_return_val_if_fail (scale >= 1, NULL);
-  g_warn_if_fail ((flags & GTK_ICON_LOOKUP_GENERIC_FALLBACK) == 0);
-
-  gtk_icon_theme_lock (self);
-
-  icon = choose_icon (self, icon_names, size, scale, direction, flags, FALSE, NULL);
-
-  gtk_icon_theme_unlock (self);
-
-  return icon;
-}
-
 /* Error quark */
 GQuark
 gtk_icon_theme_error_quark (void)
@@ -3972,8 +3924,8 @@ gtk_icon_theme_lookup_by_gicon (GtkIconTheme       *self,
     {
       const gchar **names;
 
-      names = (const gchar **)g_themed_icon_get_names (G_THEMED_ICON (gicon));
-      icon = gtk_icon_theme_choose_icon (self, names, size, scale, direction, flags);
+      names = (const gchar **) g_themed_icon_get_names (G_THEMED_ICON (gicon));
+      icon = gtk_icon_theme_lookup_icon (self, names[0], &names[1], size, scale, direction, flags);
 
       return icon;
     }
diff --git a/gtk/gtkicontheme.h b/gtk/gtkicontheme.h
index 2a54f5bbf6..d0c360775a 100644
--- a/gtk/gtkicontheme.h
+++ b/gtk/gtkicontheme.h
@@ -125,14 +125,8 @@ gint         *gtk_icon_theme_get_icon_sizes        (GtkIconTheme
                                                     const gchar                 *icon_name);
 GDK_AVAILABLE_IN_ALL
 GtkIcon *    gtk_icon_theme_lookup_icon            (GtkIconTheme                *self,
-                                                    const gchar                 *icon_name,
-                                                    gint                         size,
-                                                    gint                         scale,
-                                                    GtkTextDirection             direction,
-                                                    GtkIconLookupFlags           flags);
-GDK_AVAILABLE_IN_ALL
-GtkIcon *    gtk_icon_theme_choose_icon            (GtkIconTheme                *self,
-                                                    const gchar                 *icon_names[],
+                                                    const char                  *icon_name,
+                                                    const char                  *fallbacks[],
                                                     gint                         size,
                                                     gint                         scale,
                                                     GtkTextDirection             direction,
diff --git a/gtk/gtkmountoperation.c b/gtk/gtkmountoperation.c
index 417ba0a7c1..722415a6b8 100644
--- a/gtk/gtkmountoperation.c
+++ b/gtk/gtkmountoperation.c
@@ -1176,6 +1176,7 @@ add_pid_to_process_list_store (GtkMountOperation              *mount_operation,
                                            GTK_CSS_PROPERTY_ICON_THEME));
       icon = gtk_icon_theme_lookup_icon (theme,
                                          "application-x-executable",
+                                         NULL,
                                          24, 1,
                                          gtk_widget_get_direction (GTK_WIDGET 
(mount_operation->priv->dialog)),
                                          0);
diff --git a/gtk/gtkwindow.c b/gtk/gtkwindow.c
index 2730181c79..0a030ade36 100644
--- a/gtk/gtkwindow.c
+++ b/gtk/gtkwindow.c
@@ -4027,12 +4027,12 @@ icon_list_from_theme (GtkWindow   *window,
        * fixed size of 48.
        */
       if (sizes[i] == -1)
-        info = gtk_icon_theme_lookup_icon (icon_theme, name,
+        info = gtk_icon_theme_lookup_icon (icon_theme, name, NULL,
                                            48, priv->scale,
                                            gtk_widget_get_direction (GTK_WIDGET (window)),
                                            0);
       else
-        info = gtk_icon_theme_lookup_icon (icon_theme, name,
+        info = gtk_icon_theme_lookup_icon (icon_theme, name, NULL,
                                            sizes[i], priv->scale,
                                            gtk_widget_get_direction (GTK_WIDGET (window)),
                                            0);
@@ -4114,7 +4114,7 @@ gtk_window_get_icon_for_size (GtkWindow *window,
     return NULL;
 
   info = gtk_icon_theme_lookup_icon (gtk_icon_theme_get_for_display (gtk_widget_get_display (GTK_WIDGET 
(window))),
-                                     name, size, priv->scale,
+                                     name, NULL, size, priv->scale,
                                      gtk_widget_get_direction (GTK_WIDGET (window)),
                                      GTK_ICON_LOOKUP_FORCE_SIZE);
   if (info == NULL)
diff --git a/tests/testclipboard2.c b/tests/testclipboard2.c
index e3fb9422e8..7f9f381712 100644
--- a/tests/testclipboard2.c
+++ b/tests/testclipboard2.c
@@ -288,6 +288,7 @@ get_button_list (GdkClipboard *clipboard,
   g_value_init (&value, GDK_TYPE_PIXBUF);
   icon = gtk_icon_theme_lookup_icon (gtk_icon_theme_get_for_display (gdk_clipboard_get_display (clipboard)),
                                      "utilities-terminal",
+                                     NULL,
                                      48, 1,
                                      gtk_widget_get_direction (box),
                                      0);
diff --git a/tests/testdnd2.c b/tests/testdnd2.c
index 9fd2d02ea7..da076e59ef 100644
--- a/tests/testdnd2.c
+++ b/tests/testdnd2.c
@@ -27,6 +27,7 @@ get_image_texture (GtkImage *image,
       *out_size = width;
       icon = gtk_icon_theme_lookup_icon (icon_theme,
                                          icon_name,
+                                         NULL,
                                          width, 1,
                                          gtk_widget_get_direction (GTK_WIDGET (image)),
                                          GTK_ICON_LOOKUP_GENERIC_FALLBACK);
@@ -234,6 +235,7 @@ update_source_icon (GtkDragSource *source,
 
   icon = gtk_icon_theme_lookup_icon (gtk_icon_theme_get_for_display (gtk_widget_get_display (widget)),
                                      icon_name,
+                                     NULL,
                                      size, 1,
                                      gtk_widget_get_direction (widget),
                                      0);
diff --git a/tests/testicontheme.c b/tests/testicontheme.c
index f142e30220..8964bc6875 100644
--- a/tests/testicontheme.c
+++ b/tests/testicontheme.c
@@ -85,7 +85,7 @@ main (int argc, char *argv[])
       if (argc >= 6)
        scale = atoi (argv[5]);
 
-      icon = gtk_icon_theme_lookup_icon (icon_theme, argv[3], size, scale, direction, flags);
+      icon = gtk_icon_theme_lookup_icon (icon_theme, argv[3], NULL, size, scale, direction, flags);
       if (!icon)
         {
           g_print ("Icon '%s' not found\n", argv[3]);
@@ -127,7 +127,7 @@ main (int argc, char *argv[])
       if (argc >= 6)
        scale = atoi (argv[5]);
 
-      icon = gtk_icon_theme_lookup_icon (icon_theme, argv[3], size, scale, direction, flags);
+      icon = gtk_icon_theme_lookup_icon (icon_theme, argv[3], NULL, size, scale, direction, flags);
       g_print ("icon for %s at %dx%d@%dx is %s\n", argv[3], size, size, scale,
                icon ? gtk_icon_get_filename (icon) : "<none>");
 
diff --git a/testsuite/gtk/check-icon-names.c b/testsuite/gtk/check-icon-names.c
index d608f76be1..eb0c2f4094 100644
--- a/testsuite/gtk/check-icon-names.c
+++ b/testsuite/gtk/check-icon-names.c
@@ -80,7 +80,7 @@ test_icon_existence (gconstpointer icon_name)
    * icon theme.
    * The icon size is randomly chosen.
    */
-  info = gtk_icon_theme_lookup_icon (icon_theme, icon_name, 16, 1, GTK_TEXT_DIR_LTR, 0);
+  info = gtk_icon_theme_lookup_icon (icon_theme, icon_name, NULL, 16, 1, GTK_TEXT_DIR_LTR, 0);
   if (info == NULL)
     {
       g_test_message ("Failed to look up icon for \"%s\"", (char *) icon_name);
diff --git a/testsuite/gtk/icontheme.c b/testsuite/gtk/icontheme.c
index 88e4afd4cf..1af4d72bf4 100644
--- a/testsuite/gtk/icontheme.c
+++ b/testsuite/gtk/icontheme.c
@@ -58,7 +58,7 @@ assert_icon_lookup_size (const char         *icon_name,
 {
   GtkIcon *info;
 
-  info = gtk_icon_theme_lookup_icon (get_test_icontheme (FALSE), icon_name, size, 1, direction, flags);
+  info = gtk_icon_theme_lookup_icon (get_test_icontheme (FALSE), icon_name, NULL, size, 1, direction, flags);
   if (info == NULL)
     {
       g_error ("Could not look up an icon for \"%s\" with flags %s at size %d",
@@ -113,7 +113,7 @@ assert_icon_lookup_fails (const char         *icon_name,
 {
   GtkIcon *info;
 
-  info = gtk_icon_theme_lookup_icon (get_test_icontheme (FALSE), icon_name, size, 1, direction, flags);
+  info = gtk_icon_theme_lookup_icon (get_test_icontheme (FALSE), icon_name, NULL, size, 1, direction, flags);
 
   if (info != NULL)
     {
@@ -177,7 +177,7 @@ assert_lookup_order (const char         *icon_name,
 
   g_assert (lookups == NULL);
 
-  info = gtk_icon_theme_lookup_icon (get_test_icontheme (FALSE), icon_name, size, 1, direction, flags);
+  info = gtk_icon_theme_lookup_icon (get_test_icontheme (FALSE), icon_name, NULL, size, 1, direction, flags);
   if (info)
     g_object_unref (info);
   


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