]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkcolorchooserwidget.c
Re-fix opacity group handling
[~andy/gtk] / gtk / gtkcolorchooserwidget.c
index ca44737bf46f279021c425649efa2b51eca9c804..be954a26687421d4800fd072c85f3380afb7a534 100644 (file)
@@ -13,9 +13,7 @@
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  */
 
 #include "config.h"
@@ -267,9 +265,51 @@ gtk_color_chooser_widget_set_show_editor (GtkColorChooserWidget *cc,
 
 /* UI construction {{{1 */
 
+static guint
+scale_round (gdouble value, gdouble scale)
+{
+  value = floor (value * scale + 0.5);
+  value = MAX (value, 0);
+  value = MIN (value, scale);
+  return (guint)value;
+}
+
+static gchar *
+accessible_color_name (GdkRGBA *color)
+{
+  if (color->alpha < 1.0)
+    return g_strdup_printf (_("Red %d%%, Green %d%%, Blue %d%%, Alpha %d%%"),
+                            scale_round (color->red, 100),
+                            scale_round (color->green, 100),
+                            scale_round (color->blue, 100),
+                            scale_round (color->alpha, 100));
+  else
+    return g_strdup_printf (_("Red %d%%, Green %d%%, Blue %d%%"),
+                            scale_round (color->red, 100),
+                            scale_round (color->green, 100),
+                            scale_round (color->blue, 100));
+}
+
+static void
+remove_palette (GtkColorChooserWidget *cc)
+{
+  GList *children, *l;
+  GtkWidget *widget;
+
+  children = gtk_container_get_children (GTK_CONTAINER (cc->priv->palette));
+  for (l = children; l; l = l->next)
+    {
+      widget = l->data;
+      if (widget == cc->priv->custom_label || widget == cc->priv->custom)
+        continue;
+      gtk_container_remove (GTK_CONTAINER (cc->priv->palette), widget);
+    }
+  g_list_free (children);
+}
+
 static void
 add_palette (GtkColorChooserWidget  *cc,
-             gboolean                horizontal,
+             GtkOrientation          orientation,
              gint                    colors_per_line,
              gint                    n_colors,
              GdkRGBA                *colors,
@@ -282,6 +322,12 @@ add_palette (GtkColorChooserWidget  *cc,
   gint i;
   gint left, right;
 
+  if (colors == NULL)
+    {
+      remove_palette (cc);
+      return;
+    }
+
   grid = gtk_grid_new ();
   gtk_widget_set_margin_bottom (grid, 12);
   gtk_grid_set_row_spacing (GTK_GRID (grid), 2);
@@ -300,10 +346,21 @@ add_palette (GtkColorChooserWidget  *cc,
   for (i = 0; i < n_colors; i++)
     {
       p = gtk_color_swatch_new ();
+      atk_obj = gtk_widget_get_accessible (p);
       if (names)
         {
-          atk_obj = gtk_widget_get_accessible (p);
-          atk_object_set_description (atk_obj, C_("Color name", names[i]));
+          atk_object_set_description (atk_obj,
+                                      g_dpgettext2 (GETTEXT_PACKAGE, "Color name", names[i]));
+        }
+      else
+        {
+          gchar *text, *name;
+
+          name = accessible_color_name (&colors[i]);
+          text = g_strdup_printf (_("Color: %s"), name);
+          atk_object_set_description (atk_obj, text);
+          g_free (text);
+          g_free (name);
         }
       gtk_color_swatch_set_rgba (GTK_COLOR_SWATCH (p), &colors[i]);
       connect_swatch_signals (p, cc);
@@ -311,7 +368,7 @@ add_palette (GtkColorChooserWidget  *cc,
       line = i / colors_per_line;
       pos = i % colors_per_line;
 
-      if (horizontal)
+      if (orientation == GTK_ORIENTATION_HORIZONTAL)
         {
             if (pos == left)
               gtk_style_context_add_class (gtk_widget_get_style_context (p), GTK_STYLE_CLASS_LEFT);
@@ -337,22 +394,10 @@ add_palette (GtkColorChooserWidget  *cc,
 static void
 remove_default_palette (GtkColorChooserWidget *cc)
 {
-  GList *children, *l;
-  GtkWidget *widget;
-
   if (!cc->priv->has_default_palette)
     return;
 
-  children = gtk_container_get_children (GTK_CONTAINER (cc->priv->palette));
-  for (l = children; l; l = l->next)
-    {
-      widget = l->data;
-      if (widget == cc->priv->custom_label || widget == cc->priv->custom)
-        continue;
-      gtk_container_remove (GTK_CONTAINER (cc->priv->palette), widget);
-    }
-  g_list_free (children);
-
+  remove_palette (cc);
   cc->priv->has_default_palette = FALSE;
 }
 
@@ -428,25 +473,16 @@ add_default_palette (GtkColorChooserWidget *cc)
     for (j = 0; j < 3; j++)
       gdk_rgba_parse (&colors[i*3 + j], default_colors[i][j]);
 
-  add_palette (cc, FALSE, 3, 9*3, colors, color_names);
+  add_palette (cc, GTK_ORIENTATION_VERTICAL, 3, 9*3, colors, color_names);
 
   for (i = 0; i < 9; i++)
     gdk_rgba_parse (&colors[i], default_grays[i]);
 
-  add_palette (cc, TRUE, 9, 9, colors, gray_names);
+  add_palette (cc, GTK_ORIENTATION_HORIZONTAL, 9, 9, colors, gray_names);
 
   cc->priv->has_default_palette = TRUE;
 }
 
-static guint
-scale_round (gdouble value, gdouble scale)
-{
-  value = floor (value * scale + 0.5);
-  value = MAX (value, 0);
-  value = MIN (value, scale);
-  return (guint)value;
-}
-
 static void
 gtk_color_chooser_widget_init (GtkColorChooserWidget *cc)
 {
@@ -460,7 +496,7 @@ gtk_color_chooser_widget_init (GtkColorChooserWidget *cc)
   GVariantIter iter;
   gboolean selected;
   AtkObject *atk_obj;
-  gchar *text;
+  gchar *text, *name;
 
   cc->priv = G_TYPE_INSTANCE_GET_PRIVATE (cc, GTK_TYPE_COLOR_CHOOSER_WIDGET, GtkColorChooserWidgetPrivate);
 
@@ -482,6 +518,7 @@ gtk_color_chooser_widget_init (GtkColorChooserWidget *cc)
   gtk_box_pack_end (GTK_BOX (cc->priv->palette), label, FALSE, TRUE, 0);
 
   cc->priv->button = button = gtk_color_swatch_new ();
+  gtk_widget_set_name (button, "add-color-button");
   atk_obj = gtk_widget_get_accessible (button);
   atk_object_set_role (atk_obj, ATK_ROLE_PUSH_BUTTON);
   atk_object_set_description (atk_obj, _("Create custom color"));
@@ -489,8 +526,7 @@ gtk_color_chooser_widget_init (GtkColorChooserWidget *cc)
   gtk_color_swatch_set_icon (GTK_COLOR_SWATCH (button), "list-add-symbolic");
   gtk_container_add (GTK_CONTAINER (box), button);
 
-  cc->priv->settings = g_settings_new_with_path ("org.gtk.Settings.ColorChooser",
-                                                 "/org/gtk/settings/color-chooser/");
+  cc->priv->settings = g_settings_new ("org.gtk.Settings.ColorChooser");
   variant = g_settings_get_value (cc->priv->settings, "custom-colors");
   g_variant_iter_init (&iter, variant);
   i = 0;
@@ -502,13 +538,11 @@ gtk_color_chooser_widget_init (GtkColorChooserWidget *cc)
       gtk_color_swatch_set_rgba (GTK_COLOR_SWATCH (p), &color);
       gtk_color_swatch_set_can_drop (GTK_COLOR_SWATCH (p), TRUE);
       atk_obj = gtk_widget_get_accessible (p);
-      text = g_strdup_printf (_("Custom color %d: Red %d%%, Green %d%%, Blue %d%%, Alpha %d%%"), i,
-                              scale_round (color.red, 100),
-                              scale_round (color.green, 100),
-                              scale_round (color.blue, 100),
-                              scale_round (color.alpha, 100));
+      name = accessible_color_name (&color);
+      text = g_strdup_printf (_("Custom color %d: %s"), i, name);
       atk_object_set_description (atk_obj, text);
       g_free (text);
+      g_free (name);
       connect_custom_signals (p, cc);
       gtk_container_add (GTK_CONTAINER (box), p);
 
@@ -726,6 +760,7 @@ gtk_color_chooser_widget_set_rgba (GtkColorChooser *chooser,
             {
               select_swatch (cc, swatch);
               g_list_free (children);
+              g_list_free (palettes);
               return;
             }
         }
@@ -738,7 +773,7 @@ gtk_color_chooser_widget_set_rgba (GtkColorChooser *chooser,
 
 static void
 gtk_color_chooser_widget_add_palette (GtkColorChooser *chooser,
-                                      gboolean         horizontal,
+                                      GtkOrientation   orientation,
                                       gint             colors_per_line,
                                       gint             n_colors,
                                       GdkRGBA         *colors)
@@ -746,7 +781,7 @@ gtk_color_chooser_widget_add_palette (GtkColorChooser *chooser,
   GtkColorChooserWidget *cc = GTK_COLOR_CHOOSER_WIDGET (chooser);
 
   remove_default_palette (cc);
-  add_palette (cc, horizontal, colors_per_line, n_colors, colors, NULL);
+  add_palette (cc, orientation, colors_per_line, n_colors, colors, NULL);
 }
 
 static void