gtk2/gtk/gtkcolorscale.c

293 lines
8.7 KiB
C
Raw Normal View History

2012-02-02 05:58:26 +00:00
/* GTK - The GIMP Toolkit
* Copyright (C) 2012 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
2012-02-27 13:01:10 +00:00
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
2012-02-02 05:58:26 +00:00
*/
#include "config.h"
#include "gtkcolorscaleprivate.h"
2012-02-04 05:02:49 +00:00
#include "gtkcolorchooserprivate.h"
#include "gtkgesturelongpress.h"
#include "gtkcolorutils.h"
#include "gtkorientable.h"
#include "gtkrangeprivate.h"
2012-02-02 05:58:26 +00:00
#include "gtkstylecontext.h"
2012-02-03 07:09:44 +00:00
#include "gtkaccessible.h"
2012-02-04 05:02:49 +00:00
#include "gtkprivate.h"
2012-02-02 05:58:26 +00:00
#include "gtkintl.h"
#include "gtksnapshot.h"
2012-02-02 05:58:26 +00:00
#include <math.h>
2012-02-02 05:58:26 +00:00
struct _GtkColorScalePrivate
{
GdkRGBA color;
GtkColorScaleType type;
GtkGesture *long_press_gesture;
2012-02-02 05:58:26 +00:00
};
2012-02-04 05:02:49 +00:00
enum
{
PROP_ZERO,
PROP_SCALE_TYPE
};
static void hold_action (GtkGestureLongPress *gesture,
gdouble x,
gdouble y,
GtkColorScale *scale);
G_DEFINE_TYPE_WITH_PRIVATE (GtkColorScale, gtk_color_scale, GTK_TYPE_SCALE)
2012-02-02 05:58:26 +00:00
void
gtk_color_scale_snapshot_trough (GtkColorScale *scale,
GtkSnapshot *snapshot,
int x,
int y,
int width,
int height)
2012-02-02 05:58:26 +00:00
{
GtkWidget *widget = GTK_WIDGET (scale);
2012-02-02 05:58:26 +00:00
if (width <= 1 || height <= 1)
2012-02-02 05:58:26 +00:00
return;
2012-02-04 05:02:49 +00:00
if (scale->priv->type == GTK_COLOR_SCALE_HUE)
2012-02-02 05:58:26 +00:00
{
GdkTexture *texture;
2012-02-04 05:02:49 +00:00
gint stride;
GBytes *bytes;
guchar *data, *p;
2012-02-04 05:02:49 +00:00
gdouble h;
gdouble r, g, b;
gdouble f;
int hue_x, hue_y;
2012-02-04 05:02:49 +00:00
stride = width * 3;
data = g_malloc (width * height * 3);
2012-02-04 05:02:49 +00:00
f = 1.0 / (height - 1);
for (hue_y = 0; hue_y < height; hue_y++)
2012-02-02 05:58:26 +00:00
{
h = CLAMP (hue_y * f, 0.0, 1.0);
p = data + hue_y * stride;
for (hue_x = 0; hue_x < stride; hue_x += 3)
2012-02-04 05:02:49 +00:00
{
gtk_hsv_to_rgb (h, 1, 1, &r, &g, &b);
p[hue_x + 0] = CLAMP (r * 255, 0, 255);
p[hue_x + 1] = CLAMP (g * 255, 0, 255);
p[hue_x + 2] = CLAMP (b * 255, 0, 255);
2012-02-04 05:02:49 +00:00
}
2012-02-02 05:58:26 +00:00
}
bytes = g_bytes_new_take (data, width * height * 3);
texture = gdk_memory_texture_new (width, height,
GDK_MEMORY_R8G8B8,
bytes,
stride);
g_bytes_unref (bytes);
gtk_snapshot_append_texture (snapshot,
texture,
&GRAPHENE_RECT_INIT(x, y, width, height),
"ColorScaleHue");
g_object_unref (texture);
2012-02-02 05:58:26 +00:00
}
2012-02-04 05:02:49 +00:00
else if (scale->priv->type == GTK_COLOR_SCALE_ALPHA)
{
cairo_t *cr;
graphene_point_t start, end;
cr = gtk_snapshot_append_cairo (snapshot,
&GRAPHENE_RECT_INIT(x, y, width, height),
"ColorScaleAlpha");
cairo_translate (cr, x, y);
if (gtk_orientable_get_orientation (GTK_ORIENTABLE (widget)) == GTK_ORIENTATION_HORIZONTAL &&
gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
{
cairo_translate (cr, width, 0);
cairo_scale (cr, -1, 1);
graphene_point_init (&start, x + width, y);
graphene_point_init (&end, x, y);
}
else
{
graphene_point_init (&start, x, y);
graphene_point_init (&end, x + width, y);
}
2012-02-04 05:02:49 +00:00
cairo_pattern_t *pattern;
cairo_matrix_t matrix;
GdkRGBA *color;
2012-02-02 05:58:26 +00:00
2012-02-04 05:02:49 +00:00
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);
2012-02-02 05:58:26 +00:00
pattern = _gtk_color_chooser_get_checkered_pattern ();
2012-02-04 05:02:49 +00:00
cairo_matrix_init_scale (&matrix, 0.125, 0.125);
cairo_pattern_set_matrix (pattern, &matrix);
cairo_mask (cr, pattern);
cairo_pattern_destroy (pattern);
2012-02-02 05:58:26 +00:00
cairo_destroy (cr);
color = &scale->priv->color;
gtk_snapshot_append_linear_gradient (snapshot,
&GRAPHENE_RECT_INIT(x, y, width, height),
&start,
&end,
(GskColorStop[2]) {
{ 0, { color->red, color->green, color->blue, 0 } },
{ 1, { color->red, color->green, color->blue, 1 } },
},
2,
"ColorAlphaGradient");
}
2012-02-02 05:58:26 +00:00
}
static void
gtk_color_scale_init (GtkColorScale *scale)
{
GtkStyleContext *context;
scale->priv = gtk_color_scale_get_instance_private (scale);
scale->priv->long_press_gesture = gtk_gesture_long_press_new (GTK_WIDGET (scale));
g_signal_connect (scale->priv->long_press_gesture, "pressed",
G_CALLBACK (hold_action), scale);
gtk_event_controller_set_propagation_phase (GTK_EVENT_CONTROLLER (scale->priv->long_press_gesture),
GTK_PHASE_TARGET);
context = gtk_widget_get_style_context (GTK_WIDGET (scale));
gtk_style_context_add_class (context, "color");
2012-02-02 05:58:26 +00:00
}
static void
scale_finalize (GObject *object)
{
GtkColorScale *scale = GTK_COLOR_SCALE (object);
g_clear_object (&scale->priv->long_press_gesture);
2012-02-02 05:58:26 +00:00
G_OBJECT_CLASS (gtk_color_scale_parent_class)->finalize (object);
}
static void
2012-02-04 05:02:49 +00:00
scale_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
2012-02-02 05:58:26 +00:00
{
2012-02-04 05:02:49 +00:00
GtkColorScale *scale = GTK_COLOR_SCALE (object);
2012-02-02 05:58:26 +00:00
2012-02-04 05:02:49 +00:00
switch (prop_id)
2012-02-02 05:58:26 +00:00
{
2012-02-04 05:02:49 +00:00
case PROP_SCALE_TYPE:
g_value_set_int (value, scale->priv->type);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
2012-02-02 05:58:26 +00:00
}
}
static void
2012-02-04 05:02:49 +00:00
scale_set_type (GtkColorScale *scale,
GtkColorScaleType type)
2012-02-02 05:58:26 +00:00
{
2012-02-03 07:09:44 +00:00
AtkObject *atk_obj;
2012-02-02 05:58:26 +00:00
scale->priv->type = type;
2012-02-03 07:09:44 +00:00
atk_obj = gtk_widget_get_accessible (GTK_WIDGET (scale));
if (GTK_IS_ACCESSIBLE (atk_obj))
{
if (type == GTK_COLOR_SCALE_HUE)
atk_object_set_name (atk_obj, C_("Color channel", "Hue"));
else if (type == GTK_COLOR_SCALE_ALPHA)
atk_object_set_name (atk_obj, C_("Color channel", "Alpha"));
atk_object_set_role (atk_obj, ATK_ROLE_COLOR_CHOOSER);
2012-02-03 07:09:44 +00:00
}
2012-02-02 05:58:26 +00:00
}
2012-02-04 05:02:49 +00:00
static void
scale_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GtkColorScale *scale = GTK_COLOR_SCALE (object);
switch (prop_id)
{
case PROP_SCALE_TYPE:
scale_set_type (scale, (GtkColorScaleType)g_value_get_int (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
hold_action (GtkGestureLongPress *gesture,
gdouble x,
gdouble y,
GtkColorScale *scale)
{
gboolean handled;
g_signal_emit_by_name (scale, "popup-menu", &handled);
}
2012-02-04 05:02:49 +00:00
static void
gtk_color_scale_class_init (GtkColorScaleClass *class)
{
GObjectClass *object_class = G_OBJECT_CLASS (class);
object_class->finalize = scale_finalize;
object_class->get_property = scale_get_property;
object_class->set_property = scale_set_property;
g_object_class_install_property (object_class, PROP_SCALE_TYPE,
g_param_spec_int ("scale-type", P_("Scale type"), P_("Scale type"),
0, 1, 0,
GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
}
void
gtk_color_scale_set_rgba (GtkColorScale *scale,
const GdkRGBA *color)
{
scale->priv->color = *color;
gtk_widget_queue_draw (GTK_WIDGET (scale));
}
2012-02-02 05:58:26 +00:00
GtkWidget *
gtk_color_scale_new (GtkAdjustment *adjustment,
GtkColorScaleType type)
2012-02-02 05:58:26 +00:00
{
return g_object_new (GTK_TYPE_COLOR_SCALE,
"adjustment", adjustment,
"draw-value", FALSE,
"scale-type", type,
NULL);
2012-02-02 05:58:26 +00:00
}