[gtk+/wip/css-icons] GtkIconTheme: Support looking up directional variants



commit b24f6fa928ca9e17943fc29c46ea300d1467b00a
Author: Matthias Clasen <mclasen redhat com>
Date:   Sun May 11 23:54:55 2014 -0400

    GtkIconTheme: Support looking up directional variants
    
    Add two new icon lookup flags, GTK_ICON_LOOKUP_DIR_LTR and _RTL,
    which tell GtkIconTheme to look for icon variants which have a
    -ltr or -rtl suffix. GtkIconHelper adds these lookup flags when
    looking up icons.
    
    Note that due to the way this overlaps with symbolic icon lookup,
    directional variants of symbolic icons must be called
    -symbolic-rtl, not -rtl-symbolic.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=729980

 gtk/gtkicontheme.c    |   82 +++++++++++++++++++++++++++++++++++++++++--------
 gtk/gtkicontheme.h    |    8 ++++-
 gtk/gtkstylecontext.c |   17 ++++++++--
 3 files changed, 90 insertions(+), 17 deletions(-)
---
diff --git a/gtk/gtkicontheme.c b/gtk/gtkicontheme.c
index d875a2a..1ad6953 100644
--- a/gtk/gtkicontheme.c
+++ b/gtk/gtkicontheme.c
@@ -1649,7 +1649,9 @@ real_choose_icon (GtkIconTheme       *icon_theme,
    * to show up instead.
    */
   if (icon_names[0] &&
-      g_str_has_suffix (icon_names[0], "-symbolic"))
+      (g_str_has_suffix (icon_names[0], "-symbolic") ||
+       g_str_has_suffix (icon_names[0], "-symbolic-ltr") ||
+       g_str_has_suffix (icon_names[0], "-symbolic-rtl")))
     {
       for (l = priv->themes; l; l = l->next)
         {
@@ -1805,6 +1807,14 @@ choose_icon (GtkIconTheme       *icon_theme,
   GtkIconInfo *icon_info;
   gchar **new_names;
   guint i;
+  const gchar *dir_suffix;
+
+  if (flags & GTK_ICON_LOOKUP_DIR_LTR)
+    dir_suffix = "-ltr";
+  else if (flags & GTK_ICON_LOOKUP_DIR_RTL)
+    dir_suffix = "-rtl";
+  else
+    dir_suffix = NULL;
 
   for (i = 0; icon_names[i]; i++)
     {
@@ -1816,20 +1826,31 @@ choose_icon (GtkIconTheme       *icon_theme,
 
   if ((flags & GTK_ICON_LOOKUP_FORCE_REGULAR) && has_symbolic)
     {
-      new_names = g_new0 (gchar *, i + 1);
+      if (dir_suffix)
+        new_names = g_new0 (gchar *, 2*i + 1);
+      else
+        new_names = g_new0 (gchar *, i + 1);
       for (i = 0; icon_names[i]; i++)
         {
+          gchar *tmp;
           if (g_str_has_suffix (icon_names[i], "-symbolic"))
-            new_names[i] = g_strndup (icon_names[i], strlen (icon_names[i]) - strlen ("-symbolic"));
+            tmp = g_strndup (icon_names[i], strlen (icon_names[i]) - strlen ("-symbolic"));
           else
-            new_names[i] = g_strdup (icon_names[i]);
+            tmp = g_strdup (icon_names[i]);
+          if (dir_suffix)
+            {
+              new_names[2*i] = g_strconcat (tmp, dir_suffix, NULL);
+              new_names[2*i+1] = tmp;
+            }
+          else
+            new_names[i] = tmp;
         }
 
       icon_info = real_choose_icon (icon_theme,
                                     (const gchar **) new_names,
                                     size,
                                     scale,
-                                    flags & ~(GTK_ICON_LOOKUP_FORCE_REGULAR | 
GTK_ICON_LOOKUP_FORCE_SYMBOLIC));
+                                    flags & ~(GTK_ICON_LOOKUP_FORCE_REGULAR | GTK_ICON_LOOKUP_FORCE_SYMBOLIC 
| GTK_ICON_LOOKUP_DIR_LTR | GTK_ICON_LOOKUP_DIR_RTL));
 
       g_strfreev (new_names);
 
@@ -1838,20 +1859,52 @@ choose_icon (GtkIconTheme       *icon_theme,
     }
   else if ((flags & GTK_ICON_LOOKUP_FORCE_SYMBOLIC) && has_regular)
     {
-      new_names = g_new0 (gchar *, i + 1);
+      if (dir_suffix)
+        new_names = g_new0 (gchar *, 2*i + 1);
+      else
+        new_names = g_new0 (gchar *, i + 1);
       for (i = 0; icon_names[i]; i++)
         {
-          if (!g_str_has_suffix (icon_names[i], "-symbolic"))
-            new_names[i] = g_strconcat (icon_names[i], "-symbolic", NULL);
+          gchar *tmp;
+          if (g_str_has_suffix (icon_names[i], "-symbolic"))
+            tmp = g_strdup (icon_names[i]);
+          else
+            tmp = g_strconcat (icon_names[i], "-symbolic", NULL);
+          if (dir_suffix)
+            {
+              new_names[2*i] = g_strconcat (tmp, dir_suffix, NULL);
+              new_names[2*i+1] = tmp;
+            }
           else
-            new_names[i] = g_strdup (icon_names[i]);
+            new_names[i] = tmp;
+        }
+
+      icon_info = real_choose_icon (icon_theme,
+                                    (const gchar **) new_names,
+                                    size,
+                                    scale,
+                                    flags & ~(GTK_ICON_LOOKUP_FORCE_REGULAR | GTK_ICON_LOOKUP_FORCE_SYMBOLIC 
| GTK_ICON_LOOKUP_DIR_LTR | GTK_ICON_LOOKUP_DIR_RTL));
+
+      g_strfreev (new_names);
+
+      if (icon_info)
+        return icon_info;
+    }
+
+  if (dir_suffix)
+    {
+      new_names = g_new0 (gchar *, 2*i + 1);
+      for (i = 0; icon_names[i]; i++)
+        {
+          new_names[2*i] = g_strconcat (icon_names[i], dir_suffix, NULL);
+          new_names[2*i + 1] = g_strdup (icon_names[i]);
         }
 
       icon_info = real_choose_icon (icon_theme,
                                     (const gchar **) new_names,
                                     size,
                                     scale,
-                                    flags & ~(GTK_ICON_LOOKUP_FORCE_REGULAR | 
GTK_ICON_LOOKUP_FORCE_SYMBOLIC));
+                                    flags & ~(GTK_ICON_LOOKUP_FORCE_REGULAR | GTK_ICON_LOOKUP_FORCE_SYMBOLIC 
| GTK_ICON_LOOKUP_DIR_LTR | GTK_ICON_LOOKUP_DIR_RTL));
 
       g_strfreev (new_names);
 
@@ -1863,7 +1916,7 @@ choose_icon (GtkIconTheme       *icon_theme,
                                 icon_names,
                                 size,
                                 scale,
-                                flags & ~(GTK_ICON_LOOKUP_FORCE_REGULAR | GTK_ICON_LOOKUP_FORCE_SYMBOLIC));
+                                flags & ~(GTK_ICON_LOOKUP_FORCE_REGULAR | GTK_ICON_LOOKUP_FORCE_SYMBOLIC | 
GTK_ICON_LOOKUP_DIR_LTR | GTK_ICON_LOOKUP_DIR_RTL));
 
   return icon_info;
 }
@@ -1951,7 +2004,7 @@ gtk_icon_theme_lookup_icon_for_scale (GtkIconTheme       *icon_theme,
 
       is_symbolic = g_str_has_suffix (icon_name, "-symbolic");
       if (is_symbolic)
-        nonsymbolic_icon_name = g_strndup (icon_name, strlen (icon_name) - 9);
+        nonsymbolic_icon_name = g_strndup (icon_name, strlen (icon_name) - strlen ("-symbolic"));
       else
         nonsymbolic_icon_name = g_strdup (icon_name);
  
@@ -3639,7 +3692,10 @@ gtk_icon_info_is_symbolic (GtkIconInfo *icon_info)
   if (icon_info->icon_file)
     icon_uri = g_file_get_uri (icon_info->icon_file);
 
-  is_symbolic = (icon_uri != NULL) && (g_str_has_suffix (icon_uri, "-symbolic.svg"));
+  is_symbolic = (icon_uri != NULL) &&
+                 (g_str_has_suffix (icon_uri, "-symbolic.svg") ||
+                  g_str_has_suffix (icon_uri, "-symbolic-ltr.svg") ||
+                  g_str_has_suffix (icon_uri, "-symbolic-rtl.svg"));
   g_free (icon_uri);
 
   return is_symbolic;
diff --git a/gtk/gtkicontheme.h b/gtk/gtkicontheme.h
index c11cfab..662545b 100644
--- a/gtk/gtkicontheme.h
+++ b/gtk/gtkicontheme.h
@@ -117,6 +117,10 @@ struct _GtkIconThemeClass
  *   when symbolic icon names are given. Since 3.14.
  * @GTK_ICON_LOOKUP_FORCE_SYMBOLIC: Try to always load symbolic icons, even
  *   when regular icon names are given. Since 3.14.
+ * @GTK_ICON_LOOKUP_DIR_LTR: Try to load a variant of the icon for left-to-right
+ *   text direction. Since 3.14.
+ * @GTK_ICON_LOOKUP_DIR_RTL: Try to load a variant of the icon for right-to-left
+ *   text direction. Since 3.14.
  * 
  * Used to specify options for gtk_icon_theme_lookup_icon()
  */
@@ -128,7 +132,9 @@ typedef enum
   GTK_ICON_LOOKUP_GENERIC_FALLBACK = 1 << 3,
   GTK_ICON_LOOKUP_FORCE_SIZE       = 1 << 4,
   GTK_ICON_LOOKUP_FORCE_REGULAR    = 1 << 5,
-  GTK_ICON_LOOKUP_FORCE_SYMBOLIC   = 1 << 6
+  GTK_ICON_LOOKUP_FORCE_SYMBOLIC   = 1 << 6,
+  GTK_ICON_LOOKUP_DIR_LTR          = 1 << 7,
+  GTK_ICON_LOOKUP_DIR_RTL          = 1 << 8
 } GtkIconLookupFlags;
 
 /**
diff --git a/gtk/gtkstylecontext.c b/gtk/gtkstylecontext.c
index c9b1812..ebdc243 100644
--- a/gtk/gtkstylecontext.c
+++ b/gtk/gtkstylecontext.c
@@ -4682,6 +4682,7 @@ GtkIconLookupFlags
 _gtk_style_context_get_icon_lookup_flags (GtkStyleContext *context)
 {
   GtkCssIconStyle icon_style;
+  GtkIconLookupFlags flags;
 
   g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), 0);
 
@@ -4690,15 +4691,25 @@ _gtk_style_context_get_icon_lookup_flags (GtkStyleContext *context)
   switch (icon_style)
     {
     case GTK_CSS_ICON_STYLE_REGULAR:
-      return GTK_ICON_LOOKUP_FORCE_REGULAR;
+      flags = GTK_ICON_LOOKUP_FORCE_REGULAR;
+      break;
     case GTK_CSS_ICON_STYLE_SYMBOLIC:
-      return GTK_ICON_LOOKUP_FORCE_SYMBOLIC;
+      flags = GTK_ICON_LOOKUP_FORCE_SYMBOLIC;
+      break;
     case GTK_CSS_ICON_STYLE_REQUESTED:
-      return 0;
+      flags = 0;
+      break;
     default:
       g_assert_not_reached ();
       return 0;
     }
+
+  if (context->priv->info->state_flags & GTK_STATE_FLAG_DIR_LTR)
+    flags |= GTK_ICON_LOOKUP_DIR_LTR;
+  else if (context->priv->info->state_flags & GTK_STATE_FLAG_DIR_RTL)
+    flags |= GTK_ICON_LOOKUP_DIR_RTL;
+
+  return flags;
 }
 
 static AtkAttributeSet *


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