]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkcolorbutton.c
a11y: Emit text-changed signals directly
[~andy/gtk] / gtk / gtkcolorbutton.c
index ecc808864aeb7ec023abf9cfd7d49401e42fd209..52c1ec52516f12f5dc1f973394ccd630c7ebb355 100644 (file)
@@ -14,9 +14,7 @@
  * Library General Public License for more details.
  *
  * You should have received a copy of the GNU Library General Public
- * License along with the Gnome Library; see the file COPYING.LIB.  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/>.
  */
 /* Color picker button for GNOME
  *
 
 #include "gtkbutton.h"
 #include "gtkmain.h"
-#include "gtkalignment.h"
 #include "gtkcolorchooser.h"
+#include "gtkcolorchooserprivate.h"
 #include "gtkcolorchooserdialog.h"
 #include "gtkdnd.h"
 #include "gtkdrawingarea.h"
-#include "gtkframe.h"
 #include "gtkmarshalers.h"
 #include "gtkprivate.h"
 #include "gtkintl.h"
@@ -63,7 +60,6 @@
 #define CHECK_DARK  (1.0 / 3.0)
 #define CHECK_LIGHT (2.0 / 3.0)
 
-
 struct _GtkColorButtonPrivate
 {
   GtkWidget *draw_area; /* Widget where we draw the color sample */
@@ -245,8 +241,7 @@ gtk_color_button_class_init (GtkColorButtonClass *klass)
    * @widget: the object which received the signal.
    *
    * The ::color-set signal is emitted when the user selects a color.
-   * When handling this signal, use gtk_color_button_get_color() and
-   * gtk_color_button_get_alpha() (or gtk_color_button_get_rgba()) to
+   * When handling this signal, use gtk_color_button_get_rgba() to
    * find out which color was just selected.
    *
    * Note that this signal is only emitted when the <emphasis>user</emphasis>
@@ -272,29 +267,6 @@ gtk_color_button_has_alpha (GtkColorButton *button)
   return button->priv->use_alpha && button->priv->rgba.alpha < 1;
 }
 
-static cairo_pattern_t *
-gtk_color_button_get_checkered (void)
-{
-  /* need to respect pixman's stride being a multiple of 4 */
-  static unsigned char data[8] = { 0xFF, 0x00, 0x00, 0x00,
-                                   0x00, 0xFF, 0x00, 0x00 };
-  static cairo_surface_t *checkered = NULL;
-  cairo_pattern_t *pattern;
-
-  if (checkered == NULL)
-    {
-      checkered = cairo_image_surface_create_for_data (data,
-                                                       CAIRO_FORMAT_A8,
-                                                       2, 2, 4);
-    }
-
-  pattern = cairo_pattern_create_for_surface (checkered);
-  cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
-  cairo_pattern_set_filter (pattern, CAIRO_FILTER_NEAREST);
-
-  return pattern;
-}
-
 /* Handle exposure events for the color picker's drawing area */
 static gint
 gtk_color_button_draw_cb (GtkWidget *widget,
@@ -312,7 +284,7 @@ gtk_color_button_draw_cb (GtkWidget *widget,
       cairo_set_source_rgb (cr, CHECK_LIGHT, CHECK_LIGHT, CHECK_LIGHT);
       cairo_scale (cr, CHECK_SIZE, CHECK_SIZE);
 
-      checkered = gtk_color_button_get_checkered ();
+      checkered = _gtk_color_chooser_get_checkered_pattern ();
       cairo_mask (cr, checkered);
       cairo_pattern_destroy (checkered);
 
@@ -337,7 +309,7 @@ gtk_color_button_draw_cb (GtkWidget *widget,
       gtk_style_context_get_background_color (context, GTK_STATE_FLAG_INSENSITIVE, &color);
 
       gdk_cairo_set_source_rgba (cr, &color);
-      checkered = gtk_color_button_get_checkered ();
+      checkered = _gtk_color_chooser_get_checkered_pattern ();
       cairo_mask (cr, checkered);
       cairo_pattern_destroy (checkered);
     }
@@ -451,8 +423,6 @@ gtk_color_button_drag_data_get (GtkWidget        *widget,
 static void
 gtk_color_button_init (GtkColorButton *button)
 {
-  GtkWidget *alignment;
-  GtkWidget *frame;
   PangoLayout *layout;
   PangoRectangle rect;
 
@@ -463,27 +433,17 @@ gtk_color_button_init (GtkColorButton *button)
 
   gtk_widget_push_composite_child ();
 
-  alignment = gtk_alignment_new (0.5, 0.5, 0.5, 1.0);
-  gtk_container_set_border_width (GTK_CONTAINER (alignment), 1);
-  gtk_container_add (GTK_CONTAINER (button), alignment);
-  gtk_widget_show (alignment);
-
-  frame = gtk_frame_new (NULL);
-  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_ETCHED_OUT);
-  gtk_container_add (GTK_CONTAINER (alignment), frame);
-  gtk_widget_show (frame);
-
-  /* Just some widget we can hook to expose-event on */
-  button->priv->draw_area = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
-
+  button->priv->draw_area = gtk_drawing_area_new ();
   layout = gtk_widget_create_pango_layout (GTK_WIDGET (button), "Black");
   pango_layout_get_pixel_extents (layout, NULL, &rect);
   g_object_unref (layout);
 
-  gtk_widget_set_size_request (button->priv->draw_area, rect.width - 2, rect.height - 2);
+  gtk_widget_set_size_request (button->priv->draw_area, 
+                               rect.width, rect.height);
+
   g_signal_connect (button->priv->draw_area, "draw",
                     G_CALLBACK (gtk_color_button_draw_cb), button);
-  gtk_container_add (GTK_CONTAINER (frame), button->priv->draw_area);
+  gtk_container_add (GTK_CONTAINER (button), button->priv->draw_area);
   gtk_widget_show (button->priv->draw_area);
 
   button->priv->title = g_strdup (_("Pick a Color")); /* default title */
@@ -624,40 +584,49 @@ dialog_response (GtkDialog *dialog,
     }
 }
 
+/* Create the dialog and connects its buttons */
 static void
-gtk_color_button_clicked (GtkButton *b)
+ensure_dialog (GtkColorButton *button)
 {
-  GtkColorButton *button = GTK_COLOR_BUTTON (b);
-  GtkWidget *dialog;
+  GtkWidget *parent, *dialog;
 
-  /* if dialog already exists, make sure it's shown and raised */
-  if (!button->priv->cs_dialog)
-    {
-      /* Create the dialog and connects its buttons */
-      GtkWidget *parent;
+  if (button->priv->cs_dialog != NULL)
+    return;
 
-      parent = gtk_widget_get_toplevel (GTK_WIDGET (button));
+  parent = gtk_widget_get_toplevel (GTK_WIDGET (button));
 
-      button->priv->cs_dialog = dialog = gtk_color_chooser_dialog_new (button->priv->title, NULL);
+  button->priv->cs_dialog = dialog = gtk_color_chooser_dialog_new (button->priv->title, NULL);
 
-      if (gtk_widget_is_toplevel (parent) && GTK_IS_WINDOW (parent))
-        {
-          if (GTK_WINDOW (parent) != gtk_window_get_transient_for (GTK_WINDOW (dialog)))
-            gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (parent));
+  if (gtk_widget_is_toplevel (parent) && GTK_IS_WINDOW (parent))
+  {
+    if (GTK_WINDOW (parent) != gtk_window_get_transient_for (GTK_WINDOW (dialog)))
+      gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (parent));
 
-          gtk_window_set_modal (GTK_WINDOW (dialog),
-                                gtk_window_get_modal (GTK_WINDOW (parent)));
-        }
+    gtk_window_set_modal (GTK_WINDOW (dialog),
+                            gtk_window_get_modal (GTK_WINDOW (parent)));
+  }
 
-      g_signal_connect (dialog, "response",
-                        G_CALLBACK (dialog_response), button);
-      g_signal_connect (dialog, "destroy",
-                        G_CALLBACK (dialog_destroy), button);
-    }
+  g_signal_connect (dialog, "response",
+                    G_CALLBACK (dialog_response), button);
+  g_signal_connect (dialog, "destroy",
+                    G_CALLBACK (dialog_destroy), button);
+}
+
+
+static void
+gtk_color_button_clicked (GtkButton *b)
+{
+  GtkColorButton *button = GTK_COLOR_BUTTON (b);
+
+  /* if dialog already exists, make sure it's shown and raised */
+  ensure_dialog (button);
 
   gtk_color_chooser_set_use_alpha (GTK_COLOR_CHOOSER (button->priv->cs_dialog),
                                    button->priv->use_alpha);
 
+  gtk_color_chooser_set_rgba (GTK_COLOR_CHOOSER (button->priv->cs_dialog),
+                              &button->priv->rgba);
+
   gtk_window_present (GTK_WINDOW (button->priv->cs_dialog));
 }
 
@@ -758,7 +727,7 @@ gtk_color_button_get_alpha (GtkColorButton *button)
 }
 
 /**
- * gtk_color_button_set_rgba:
+ * gtk_color_button_set_rgba: (skip)
  * @button: a #GtkColorButton
  * @rgba: a #GdkRGBA to set the current color with
  *
@@ -784,7 +753,7 @@ gtk_color_button_set_rgba (GtkColorButton *button,
 }
 
 /**
- * gtk_color_button_get_rgba:
+ * gtk_color_button_get_rgba: (skip)
  * @button: a #GtkColorButton
  * @rgba: (out): a #GdkRGBA to fill in with the current color
  *
@@ -990,6 +959,21 @@ gtk_color_button_get_property (GObject    *object,
     }
 }
 
+static void
+gtk_color_button_add_palette (GtkColorChooser *chooser,
+                              GtkOrientation   orientation,
+                              gint             colors_per_line,
+                              gint             n_colors,
+                              GdkRGBA         *colors)
+{
+  GtkColorButton *button = GTK_COLOR_BUTTON (chooser);
+
+  ensure_dialog (button);
+
+  gtk_color_chooser_add_palette (GTK_COLOR_CHOOSER (button->priv->cs_dialog),
+                                   orientation, colors_per_line, n_colors, colors);
+}
+
 typedef void (* get_rgba) (GtkColorChooser *, GdkRGBA *);
 typedef void (* set_rgba) (GtkColorChooser *, const GdkRGBA *);
 
@@ -998,5 +982,6 @@ gtk_color_button_iface_init (GtkColorChooserInterface *iface)
 {
   iface->get_rgba = (get_rgba)gtk_color_button_get_rgba;
   iface->set_rgba = (set_rgba)gtk_color_button_set_rgba;
+  iface->add_palette = gtk_color_button_add_palette;
 }