Make alpha optional

This commit is contained in:
Matthias Clasen 2012-01-31 02:05:37 -05:00
parent 8d1565df94
commit 8178578359
8 changed files with 178 additions and 32 deletions

View File

@ -46,6 +46,12 @@ gtk_color_chooser_default_init (GtkColorChooserInterface *iface)
GDK_TYPE_RGBA,
GTK_PARAM_READWRITE));
g_object_interface_install_property (iface,
g_param_spec_boolean ("show-alpha",
P_("Show alpha"),
P_("Whether alpha should be shown"),
TRUE,
GTK_PARAM_READWRITE));
/**
* GtkColorChooser::color-activated:
* @self: the object which received the signal
@ -108,3 +114,25 @@ _gtk_color_chooser_color_activated (GtkColorChooser *chooser,
g_signal_emit (chooser, signals[COLOR_ACTIVATED], 0, color);
}
gboolean
gtk_color_chooser_get_show_alpha (GtkColorChooser *chooser)
{
gboolean show_alpha;
g_return_val_if_fail (GTK_IS_COLOR_CHOOSER (chooser), TRUE);
g_object_get (chooser, "show-alpha", &show_alpha, NULL);
return show_alpha;
}
void
gtk_color_chooser_set_show_alpha (GtkColorChooser *chooser,
gboolean show_alpha)
{
g_return_if_fail (GTK_IS_COLOR_CHOOSER (chooser));
g_object_set (chooser, "show-alpha", show_alpha, NULL);
}

View File

@ -55,12 +55,15 @@ struct _GtkColorChooserInterface
gpointer padding[12];
};
GType gtk_color_chooser_get_type (void) G_GNUC_CONST;
GType gtk_color_chooser_get_type (void) G_GNUC_CONST;
void gtk_color_chooser_get_color (GtkColorChooser *chooser,
GdkRGBA *color);
void gtk_color_chooser_set_color (GtkColorChooser *chooser,
const GdkRGBA *color);
void gtk_color_chooser_get_color (GtkColorChooser *chooser,
GdkRGBA *color);
void gtk_color_chooser_set_color (GtkColorChooser *chooser,
const GdkRGBA *color);
gboolean gtk_color_chooser_get_show_alpha (GtkColorChooser *chooser);
void gtk_color_chooser_set_show_alpha (GtkColorChooser *chooser,
gboolean show_alpha);
G_END_DECLS

View File

@ -40,7 +40,8 @@ struct _GtkColorChooserDialogPrivate
enum
{
PROP_ZERO,
PROP_COLOR
PROP_COLOR,
PROP_SHOW_ALPHA
};
static void gtk_color_chooser_dialog_iface_init (GtkColorChooserInterface *iface);
@ -140,6 +141,7 @@ gtk_color_chooser_dialog_get_property (GObject *object,
GValue *value,
GParamSpec *pspec)
{
GtkColorChooserDialog *cd = GTK_COLOR_CHOOSER_DIALOG (object);
GtkColorChooser *cc = GTK_COLOR_CHOOSER (object);
switch (prop_id)
@ -152,6 +154,9 @@ gtk_color_chooser_dialog_get_property (GObject *object,
g_value_set_boxed (value, &color);
}
break;
case PROP_SHOW_ALPHA:
g_value_set_boolean (value, gtk_color_chooser_get_show_alpha (GTK_COLOR_CHOOSER (cd->priv->color_chooser)));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
@ -164,6 +169,7 @@ gtk_color_chooser_dialog_set_property (GObject *object,
const GValue *value,
GParamSpec *pspec)
{
GtkColorChooserDialog *cd = GTK_COLOR_CHOOSER_DIALOG (object);
GtkColorChooser *cc = GTK_COLOR_CHOOSER (object);
switch (prop_id)
@ -171,6 +177,9 @@ gtk_color_chooser_dialog_set_property (GObject *object,
case PROP_COLOR:
gtk_color_chooser_set_color (cc, g_value_get_boxed (value));
break;
case PROP_SHOW_ALPHA:
gtk_color_chooser_set_show_alpha (GTK_COLOR_CHOOSER (cd->priv->color_chooser), g_value_get_boolean (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
@ -189,6 +198,7 @@ gtk_color_chooser_dialog_class_init (GtkColorChooserDialogClass *class)
dialog_class->response = gtk_color_chooser_dialog_response;
g_object_class_override_property (object_class, PROP_COLOR, "color");
g_object_class_override_property (object_class, PROP_SHOW_ALPHA, "show-alpha");
g_type_class_add_private (class, sizeof (GtkColorChooserDialogPrivate));
}

View File

@ -42,6 +42,7 @@ struct _GtkColorChooserWidgetPrivate
GtkWidget *button;
GtkColorSwatch *current;
gboolean show_alpha;
GSettings *settings;
};
@ -49,7 +50,8 @@ struct _GtkColorChooserWidgetPrivate
enum
{
PROP_ZERO,
PROP_COLOR
PROP_COLOR,
PROP_SHOW_ALPHA
};
static void gtk_color_chooser_widget_iface_init (GtkColorChooserInterface *iface);
@ -330,6 +332,7 @@ gtk_color_chooser_widget_get_property (GObject *object,
GValue *value,
GParamSpec *pspec)
{
GtkColorChooserWidget *cw = GTK_COLOR_CHOOSER_WIDGET (object);
GtkColorChooser *cc = GTK_COLOR_CHOOSER (object);
switch (prop_id)
@ -341,13 +344,46 @@ gtk_color_chooser_widget_get_property (GObject *object,
gtk_color_chooser_get_color (cc, &color);
g_value_set_boxed (value, &color);
}
break;
break;
case PROP_SHOW_ALPHA:
g_value_set_boolean (value, cw->priv->show_alpha);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gtk_color_chooser_widget_set_show_alpha (GtkColorChooserWidget *cc,
gboolean show_alpha)
{
GtkWidget *grids[3];
gint i;
GList *children, *l;
GtkWidget *swatch;
cc->priv->show_alpha = show_alpha;
gtk_color_chooser_set_show_alpha (GTK_COLOR_CHOOSER (cc->priv->editor), show_alpha);
grids[0] = cc->priv->colors;
grids[1] = cc->priv->grays;
grids[2] = cc->priv->custom;
for (i = 0; i < 3; i++)
{
children = gtk_container_get_children (GTK_CONTAINER (grids[i]));
for (l = children; l; l = l->next)
{
swatch = l->data;
gtk_color_swatch_set_show_alpha (GTK_COLOR_SWATCH (swatch), show_alpha);
}
g_list_free (children);
}
gtk_widget_queue_draw (GTK_WIDGET (cc));
}
static void
gtk_color_chooser_widget_set_property (GObject *object,
guint prop_id,
@ -361,7 +397,11 @@ gtk_color_chooser_widget_set_property (GObject *object,
case PROP_COLOR:
gtk_color_chooser_set_color (GTK_COLOR_CHOOSER (cc),
g_value_get_boxed (value));
break;
break;
case PROP_SHOW_ALPHA:
gtk_color_chooser_widget_set_show_alpha (cc,
g_value_get_boolean (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
@ -388,6 +428,7 @@ gtk_color_chooser_widget_class_init (GtkColorChooserWidgetClass *class)
object_class->finalize = gtk_color_chooser_widget_finalize;
g_object_class_override_property (object_class, PROP_COLOR, "color");
g_object_class_override_property (object_class, PROP_SHOW_ALPHA, "show-alpha");
g_type_class_add_private (object_class, sizeof (GtkColorChooserWidgetPrivate));
}
@ -409,6 +450,9 @@ gtk_color_chooser_widget_get_color (GtkColorChooser *chooser,
color->blue = 1.0;
color->alpha = 1.0;
}
if (!cc->priv->show_alpha)
color->alpha = 1.0;
}
static void
@ -464,6 +508,8 @@ gtk_color_chooser_widget_set_color (GtkColorChooser *chooser,
{
swatch = l->data;
gtk_color_swatch_get_color (swatch, &c);
if (!cc->priv->show_alpha)
c.alpha = color->alpha;
if (gdk_rgba_equal (color, &c))
{
select_swatch (cc, swatch);

View File

@ -51,13 +51,15 @@ struct _GtkColorEditorPrivate
GtkAdjustment *a_adj;
GdkRGBA color;
gdouble h, s, v;
gboolean text_changed;
guint text_changed : 1;
guint show_alpha : 1;
};
enum
{
PROP_ZERO,
PROP_COLOR
PROP_COLOR,
PROP_SHOW_ALPHA
};
static void gtk_color_editor_iface_init (GtkColorChooserInterface *iface);
@ -188,21 +190,24 @@ get_checkered_pattern (void)
}
static gboolean
swatch_draw (GtkWidget *swatch,
swatch_draw (GtkWidget *widget,
cairo_t *cr,
GtkColorEditor *editor)
{
cairo_pattern_t *checkered;
cairo_set_source_rgb (cr, 0.33, 0.33, 0.33);
cairo_paint (cr);
if (editor->priv->show_alpha)
{
cairo_set_source_rgb (cr, 0.33, 0.33, 0.33);
cairo_paint (cr);
cairo_set_source_rgb (cr, 0.66, 0.66, 0.66);
cairo_scale (cr, 8, 8);
cairo_set_source_rgb (cr, 0.66, 0.66, 0.66);
cairo_scale (cr, 8, 8);
checkered = get_checkered_pattern ();
cairo_mask (cr, checkered);
cairo_pattern_destroy (checkered);
checkered = get_checkered_pattern ();
cairo_mask (cr, checkered);
cairo_pattern_destroy (checkered);
}
gdk_cairo_set_source_rgba (cr, &editor->priv->color);
cairo_paint (cr);
@ -219,6 +224,8 @@ gtk_color_editor_init (GtkColorEditor *editor)
editor->priv = G_TYPE_INSTANCE_GET_PRIVATE (editor,
GTK_TYPE_COLOR_EDITOR,
GtkColorEditorPrivate);
editor->priv->show_alpha = TRUE;
gtk_widget_push_composite_child ();
editor->priv->grid = grid = gtk_grid_new ();
@ -271,6 +278,7 @@ gtk_color_editor_get_property (GObject *object,
GValue *value,
GParamSpec *pspec)
{
GtkColorEditor *ce = GTK_COLOR_EDITOR (object);
GtkColorChooser *cc = GTK_COLOR_CHOOSER (object);
switch (prop_id)
@ -281,26 +289,48 @@ gtk_color_editor_get_property (GObject *object,
gtk_color_chooser_get_color (cc, &color);
g_value_set_boxed (value, &color);
}
break;
break;
case PROP_SHOW_ALPHA:
g_value_set_boolean (value, gtk_widget_get_visible (ce->priv->a_slider));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gtk_color_editor_set_show_alpha (GtkColorEditor *editor,
gboolean show_alpha)
{
if (editor->priv->show_alpha != show_alpha)
{
editor->priv->show_alpha = show_alpha;
if (show_alpha)
gtk_widget_show (editor->priv->a_slider);
else
gtk_widget_hide (editor->priv->a_slider);
}
}
static void
gtk_color_editor_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GtkColorEditor *ce = GTK_COLOR_EDITOR (object);
GtkColorChooser *cc = GTK_COLOR_CHOOSER (object);
switch (prop_id)
{
case PROP_COLOR:
gtk_color_chooser_set_color (cc, g_value_get_boxed (value));
break;
break;
case PROP_SHOW_ALPHA:
gtk_color_editor_set_show_alpha (ce, g_value_get_boolean (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
@ -316,6 +346,7 @@ gtk_color_editor_class_init (GtkColorEditorClass *class)
object_class->set_property = gtk_color_editor_set_property;
g_object_class_override_property (object_class, PROP_COLOR, "color");
g_object_class_override_property (object_class, PROP_SHOW_ALPHA, "show-alpha");
g_type_class_add_private (class, sizeof (GtkColorEditorPrivate));
}

View File

@ -41,6 +41,7 @@ struct _GtkColorSwatchPrivate
guint has_color : 1;
guint can_drop : 1;
guint contains_pointer : 1;
guint show_alpha : 1;
};
enum
@ -74,6 +75,7 @@ gtk_color_swatch_init (GtkColorSwatch *swatch)
| GDK_EXPOSURE_MASK
| GDK_ENTER_NOTIFY_MASK
| GDK_LEAVE_NOTIFY_MASK);
swatch->priv->show_alpha = TRUE;
}
static void
@ -137,24 +139,34 @@ swatch_draw (GtkWidget *widget,
_gtk_rounded_box_path (&box, cr);
cairo_clip_preserve (cr);
if (swatch->priv->has_color)
{
cairo_pattern_t *pattern;
cairo_matrix_t matrix;
cairo_set_source_rgb (cr, 0.33, 0.33, 0.33);
cairo_fill_preserve (cr);
if (swatch->priv->show_alpha)
{
cairo_set_source_rgb (cr, 0.33, 0.33, 0.33);
cairo_fill_preserve (cr);
cairo_set_source_rgb (cr, 0.66, 0.66, 0.66);
cairo_set_source_rgb (cr, 0.66, 0.66, 0.66);
pattern = get_checkered_pattern ();
cairo_matrix_init_scale (&matrix, 0.125, 0.125);
cairo_pattern_set_matrix (pattern, &matrix);
cairo_mask (cr, pattern);
cairo_pattern_destroy (pattern);
pattern = get_checkered_pattern ();
cairo_matrix_init_scale (&matrix, 0.125, 0.125);
cairo_pattern_set_matrix (pattern, &matrix);
cairo_clip_preserve (cr);
cairo_mask (cr, pattern);
cairo_pattern_destroy (pattern);
gdk_cairo_set_source_rgba (cr, &swatch->priv->color);
gdk_cairo_set_source_rgba (cr, &swatch->priv->color);
}
else
{
cairo_set_source_rgb (cr,
swatch->priv->color.red,
swatch->priv->color.green,
swatch->priv->color.blue);
}
cairo_fill_preserve (cr);
}
@ -693,4 +705,13 @@ gtk_color_swatch_set_can_drop (GtkColorSwatch *swatch,
swatch->priv->can_drop = can_drop;
}
void
gtk_color_swatch_set_show_alpha (GtkColorSwatch *swatch,
gboolean show_alpha)
{
swatch->priv->show_alpha = show_alpha;
gtk_widget_queue_draw (GTK_WIDGET (swatch));
}
/* vim:set foldmethod=marker: */

View File

@ -81,6 +81,8 @@ void gtk_color_swatch_set_can_drop (GtkColorSwatch *swatch,
gboolean can_drop);
void gtk_color_swatch_set_icon (GtkColorSwatch *swatch,
const gchar *icon);
void gtk_color_swatch_set_show_alpha (GtkColorSwatch *swatch,
gboolean show_alpha);
G_END_DECLS

View File

@ -38,6 +38,11 @@ main (int argc, char *argv[])
gtk_init (NULL, NULL);
dialog = gtk_color_chooser_dialog_new ("Select a color", NULL);
if (argc > 1 && strcmp (argv[1], "--no-alpha") == 0)
{
g_print ("turning alpha off\n");
gtk_color_chooser_set_show_alpha (GTK_COLOR_CHOOSER (dialog), FALSE);
}
g_signal_connect (dialog, "notify::color", G_CALLBACK (color_changed), NULL);
g_signal_connect (dialog, "response", G_CALLBACK (dialog_response), NULL);