icontheme: Pass fallbacks as optional argument to lookup_icon()
authorBenjamin Otte <otte@redhat.com>
Tue, 4 Feb 2020 02:53:22 +0000 (03:53 +0100)
committerAlexander Larsson <alexl@redhat.com>
Tue, 4 Feb 2020 15:51:54 +0000 (16:51 +0100)
This way, we can remove gtk_icon_theme_choose_icon() completely.

18 files changed:
demos/gtk-demo/clipboard.c
demos/gtk-demo/textview.c
demos/icon-browser/iconbrowserwin.c
docs/reference/gtk/gtk4-sections.txt
gtk/gtkbuilder.c
gtk/gtkcalendar.c
gtk/gtkcssimageicontheme.c
gtk/gtkdragsource.c
gtk/gtkiconhelper.c
gtk/gtkicontheme.c
gtk/gtkicontheme.h
gtk/gtkmountoperation.c
gtk/gtkwindow.c
tests/testclipboard2.c
tests/testdnd2.c
tests/testicontheme.c
testsuite/gtk/check-icon-names.c
testsuite/gtk/icontheme.c

index 2edd61c462132dbf4034719b7a125d4c669b6884..63c6519a9be19fada42d9d1d00e1c2b422a29bd7 100644 (file)
@@ -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);
index cdc9e1b6d4f5e49a2c2b7e2fe213373ad7b550db..efe46e62be1a7e951b28499cc0e611e186188902 100644 (file)
@@ -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);
index c133a3ee645d14be8adc991ce1237ac62909094d..3a6e1a596a2a134bf75605f523a31b765af685e6 100644 (file)
@@ -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);
index e57e2f6e95e2bcc944d79bac4594e61b29520387..61023e0186b29b686b5d9a4451489ff8136db9d3 100644 (file)
@@ -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
index 6ad3dc5c90fa734d055313aa63283742d297fe30..91da927da36a69152e9e411478517b4a017782e4 100644 (file)
@@ -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);
index 837daad39852b0a9a8cc5de6df3f76bb51dd70ee..1c01906f230a022af852b10bf021bf5a945f6677 100644 (file)
@@ -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,
index ee7db6f997849b62c66d6f20691973dde245f20a..1a7a1f686b9c8a0dfdc46c7ce9039a392d9cee16 100644 (file)
@@ -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);
index 87996430307aa4b98153df23387d8d96b705e43f..cda9754b54a3a6a515c70fb140667389285f6da8 100644 (file)
@@ -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),
index 8835a0ee6653e7e7dda043bb259745b0bce48786..08dafad5f8a1439f871896a55cd3644c4c724c9c 100644 (file)
@@ -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);
index f23572070d2da72014643222d1cc1f7c56772049..86a3c9f70f263027896e28b2f1412002195f75fd 100644 (file)
@@ -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;
     }
index 2a54f5bbf63618a7d4e1a8b61a990266e9d750b9..d0c360775aa5cb2c18de33f7307b9c1482d98e75 100644 (file)
@@ -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,
index 417ba0a7c14f67b2427b40edddf45150394a6bec..722415a6b85185f8616f5ec2dd0beed33164752e 100644 (file)
@@ -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);
index 2730181c7946a42debea28a3249edb948d639f87..0a030ade36889b4affd1969290fdab0d68477dfa 100644 (file)
@@ -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)
index e3fb9422e822fd462667af2835928b56deb65538..7f9f381712330bbe255e4cdaa170bd5199d0b6f6 100644 (file)
@@ -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);
index 9fd2d02ea76b6d71545654e618bb2490d810ad1e..da076e59ef13800b4e2d8806d0438b784ce54908 100644 (file)
@@ -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);
index f142e30220ac4d1e178befd3e18aed2e1c8b7793..8964bc687583ec783e7f2891db6c383e4c65c953 100644 (file)
@@ -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>");
 
index d608f76be1b439ce00957055d6c1f09785a44d65..eb0c2f409450ceb308d87656b48e055157eb8686 100644 (file)
@@ -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);
index 88e4afd4cfa186abbdfe418660b83be5f8f52d3d..1af4d72bf4ad1efac87407db0d95484fdbac25b0 100644 (file)
@@ -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);