Commit 4226551f authored by Matthias Clasen's avatar Matthias Clasen

Move color scales into separate widget

parent bdb8931b
......@@ -416,6 +416,7 @@ gtk_private_h_sources = \
gtkcolorswatch.h \
gtkcoloreditor.h \
gtkcolorplane.h \
gtkcolorscale.h \
gtkcolorchooserprivate.h \
gtkcontainerprivate.h \
gtkcsscomputedvaluesprivate.h \
......@@ -600,7 +601,8 @@ gtk_base_c_sources = \
gtkcolorsel.c \
gtkcolorseldialog.c \
gtkcoloreditor.c \
gtkcolorplane.c \
gtkcolorplane.c \
gtkcolorscale.c \
gtkcolorswatch.c \
gtkcolorchooser.c \
gtkcolorchooserwidget.c \
......
......@@ -18,7 +18,6 @@
*/
/* TODO
* - move out custom sliders
* - pop-up entries
*/
......@@ -27,9 +26,10 @@
#include "gtkcolorchooserprivate.h"
#include "gtkcoloreditor.h"
#include "gtkcolorplane.h"
#include "gtkcolorscale.h"
#include "gtkcolorswatch.h"
#include "gtkgrid.h"
#include "gtkscale.h"
#include "gtkorientable.h"
#include "gtkaspectframe.h"
#include "gtkdrawingarea.h"
#include "gtkentry.h"
......@@ -50,8 +50,6 @@ struct _GtkColorEditorPrivate
GtkAdjustment *h_adj;
GtkAdjustment *a_adj;
cairo_surface_t *h_surface;
cairo_surface_t *a_surface;
GdkRGBA color;
gdouble h, s, v;
......@@ -146,12 +144,7 @@ h_changed (GtkAdjustment *adj,
gtk_color_plane_set_h (GTK_COLOR_PLANE (editor->priv->sv_plane), editor->priv->h);
update_entry (editor);
gtk_color_swatch_set_color (GTK_COLOR_SWATCH (editor->priv->swatch), &editor->priv->color);
gtk_widget_queue_draw (editor->priv->a_slider);
if (editor->priv->a_surface)
{
cairo_surface_destroy (editor->priv->a_surface);
editor->priv->a_surface = NULL;
}
gtk_color_scale_set_color (GTK_COLOR_SCALE (editor->priv->a_slider), &editor->priv->color);
g_object_notify (G_OBJECT (editor), "color");
}
......@@ -167,12 +160,7 @@ sv_changed (GtkColorPlane *plane,
&editor->priv->color.blue);
update_entry (editor);
gtk_color_swatch_set_color (GTK_COLOR_SWATCH (editor->priv->swatch), &editor->priv->color);
gtk_widget_queue_draw (editor->priv->a_slider);
if (editor->priv->a_surface)
{
cairo_surface_destroy (editor->priv->a_surface);
editor->priv->a_surface = NULL;
}
gtk_color_scale_set_color (GTK_COLOR_SCALE (editor->priv->a_slider), &editor->priv->color);
g_object_notify (G_OBJECT (editor), "color");
}
......@@ -185,193 +173,11 @@ a_changed (GtkAdjustment *adj,
g_object_notify (G_OBJECT (editor), "color");
}
static cairo_pattern_t *
get_checkered_pattern (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;
}
static cairo_surface_t *
create_h_surface (GtkWidget *widget)
{
cairo_t *cr;
cairo_surface_t *surface;
gint width, height, stride;
cairo_surface_t *tmp;
guint red, green, blue;
guint32 *data, *p;
gdouble h;
gdouble r, g, b;
gdouble f;
gint x, y;
if (!gtk_widget_get_realized (widget))
return NULL;
width = gtk_widget_get_allocated_width (widget);
height = gtk_widget_get_allocated_height (widget);
surface = gdk_window_create_similar_surface (gtk_widget_get_window (widget),
CAIRO_CONTENT_COLOR,
width, height);
if (width == 1 || height == 1)
return surface;
stride = cairo_format_stride_for_width (CAIRO_FORMAT_RGB24, width);
data = g_malloc (height * stride);
f = 1.0 / (height - 1);
for (y = 0; y < height; y++)
{
h = CLAMP (y * f, 0.0, 1.0);
p = data + y * (stride / 4);
for (x = 0; x < width; x++)
{
gtk_hsv_to_rgb (h, 1, 1, &r, &g, &b);
red = CLAMP (r * 255, 0, 255);
green = CLAMP (g * 255, 0, 255);
blue = CLAMP (b * 255, 0, 255);
p[x] = (red << 16) | (green << 8) | blue;
}
}
tmp = cairo_image_surface_create_for_data ((guchar *)data, CAIRO_FORMAT_RGB24,
width, height, stride);
cr = cairo_create (surface);
cairo_set_source_surface (cr, tmp, 0, 0);
cairo_paint (cr);
cairo_destroy (cr);
cairo_surface_destroy (tmp);
g_free (data);
return surface;
}
static gboolean
h_draw (GtkWidget *widget,
cairo_t *cr,
GtkColorEditor *editor)
{
cairo_surface_t *surface;
gint width, height;
width = gtk_widget_get_allocated_width (widget);
height = gtk_widget_get_allocated_height (widget);
if (!editor->priv->h_surface)
editor->priv->h_surface = create_h_surface (widget);
surface = editor->priv->h_surface;
cairo_save (cr);
cairo_rectangle (cr, 1, 1, width - 2, height - 2);
cairo_clip (cr);
cairo_set_source_surface (cr, surface, 0, 0);
cairo_paint (cr);
cairo_restore (cr);
return FALSE;
}
static cairo_surface_t *
create_a_surface (GtkWidget *widget,
GdkRGBA *color)
{
cairo_t *cr;
cairo_surface_t *surface;
cairo_pattern_t *pattern;
cairo_matrix_t matrix;
gint width, height;
if (!gtk_widget_get_realized (widget))
return NULL;
width = gtk_widget_get_allocated_width (widget);
height = gtk_widget_get_allocated_height (widget);
surface = gdk_window_create_similar_surface (gtk_widget_get_window (widget),
CAIRO_CONTENT_COLOR,
width, height);
if (width == 1 || height == 1)
return surface;
cr = cairo_create (surface);
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);
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 = cairo_pattern_create_linear (0, 0, width, 0);
cairo_pattern_add_color_stop_rgba (pattern, 0, color->red, color->green, color->blue, 0);
cairo_pattern_add_color_stop_rgba (pattern, width, color->red, color->green, color->blue, 1);
cairo_set_source (cr, pattern);
cairo_paint (cr);
cairo_pattern_destroy (pattern);
cairo_destroy (cr);
return surface;
}
static gboolean
a_draw (GtkWidget *widget,
cairo_t *cr,
GtkColorEditor *editor)
{
cairo_surface_t *surface;
gint width, height;
width = gtk_widget_get_allocated_width (widget);
height = gtk_widget_get_allocated_height (widget);
if (!editor->priv->a_surface)
editor->priv->a_surface = create_a_surface (widget, &editor->priv->color);
surface = editor->priv->a_surface;
cairo_save (cr);
cairo_rectangle (cr, 1, 1, width - 2, height - 2);
cairo_clip (cr);
cairo_set_source_surface (cr, surface, 0, 0);
cairo_paint (cr);
cairo_restore (cr);
return FALSE;
}
static void
gtk_color_editor_init (GtkColorEditor *editor)
{
GtkWidget *grid;
GtkAdjustment *adj;
GtkWidget *slider;
editor->priv = G_TYPE_INSTANCE_GET_PRIVATE (editor,
GTK_TYPE_COLOR_EDITOR,
......@@ -396,13 +202,13 @@ gtk_color_editor_init (GtkColorEditor *editor)
g_signal_connect (editor->priv->entry, "focus-out-event",
G_CALLBACK (entry_focus_out), editor);
adj = gtk_adjustment_new (0, 0, 1, 0.01, 0.1, 0);
g_signal_connect (adj, "value-changed", G_CALLBACK (h_changed), editor);
editor->priv->h_slider = gtk_scale_new (GTK_ORIENTATION_VERTICAL, adj);
editor->priv->h_adj = adj;
g_signal_connect (editor->priv->h_slider, "draw", G_CALLBACK (h_draw), editor);
editor->priv->h_slider = slider = gtk_color_scale_new ();
gtk_orientable_set_orientation (GTK_ORIENTABLE (slider), GTK_ORIENTATION_VERTICAL);
gtk_color_scale_set_type (GTK_COLOR_SCALE (slider), GTK_COLOR_SCALE_HUE);
gtk_style_context_add_class (gtk_widget_get_style_context (slider), GTK_STYLE_CLASS_SCALE_HAS_MARKS_BELOW);
editor->priv->h_adj = gtk_range_get_adjustment (GTK_RANGE (slider));
g_signal_connect (editor->priv->h_adj, "value-changed", G_CALLBACK (h_changed), editor);
gtk_scale_set_draw_value (GTK_SCALE (editor->priv->h_slider), FALSE);
editor->priv->sv_plane = gtk_color_plane_new ();
gtk_widget_set_size_request (editor->priv->sv_plane, 300, 300);
gtk_widget_set_hexpand (editor->priv->sv_plane, TRUE);
......@@ -410,12 +216,12 @@ gtk_color_editor_init (GtkColorEditor *editor)
g_signal_connect (editor->priv->sv_plane, "changed", G_CALLBACK (sv_changed), editor);
adj = gtk_adjustment_new (1, 0, 1, 0.01, 0.1, 0);
editor->priv->a_slider = gtk_scale_new (GTK_ORIENTATION_HORIZONTAL, adj);
g_signal_connect (adj, "value-changed", G_CALLBACK (a_changed), editor);
gtk_scale_set_draw_value (GTK_SCALE (editor->priv->a_slider), FALSE);
editor->priv->a_adj = adj;
g_signal_connect (editor->priv->a_slider, "draw", G_CALLBACK (a_draw), editor);
editor->priv->a_slider = slider = gtk_color_scale_new ();
gtk_orientable_set_orientation (GTK_ORIENTABLE (slider), GTK_ORIENTATION_HORIZONTAL);
gtk_color_scale_set_type (GTK_COLOR_SCALE (slider), GTK_COLOR_SCALE_ALPHA);
gtk_style_context_add_class (gtk_widget_get_style_context (slider), GTK_STYLE_CLASS_SCALE_HAS_MARKS_ABOVE);
editor->priv->a_adj = gtk_range_get_adjustment (GTK_RANGE (slider));
g_signal_connect (editor->priv->a_adj, "value-changed", G_CALLBACK (a_changed), editor);
gtk_grid_attach (GTK_GRID (grid), editor->priv->swatch, 1, 0, 1, 1);
gtk_grid_attach (GTK_GRID (grid), editor->priv->entry, 2, 0, 1, 1);
......@@ -495,13 +301,6 @@ gtk_color_editor_set_property (GObject *object,
static void
gtk_color_editor_finalize (GObject *object)
{
GtkColorEditor *editor = GTK_COLOR_EDITOR (object);
if (editor->priv->h_surface)
cairo_surface_destroy (editor->priv->h_surface);
if (editor->priv->a_surface)
cairo_surface_destroy (editor->priv->a_surface);
G_OBJECT_CLASS (gtk_color_editor_parent_class)->finalize (object);
}
......@@ -554,13 +353,9 @@ gtk_color_editor_set_color (GtkColorChooser *chooser,
gtk_adjustment_set_value (editor->priv->h_adj, h);
gtk_adjustment_set_value (editor->priv->a_adj, color->alpha);
gtk_color_scale_set_color (GTK_COLOR_SCALE (editor->priv->a_slider), &editor->priv->color);
update_entry (editor);
if (editor->priv->a_surface)
{
cairo_surface_destroy (editor->priv->a_surface);
editor->priv->a_surface = NULL;
}
gtk_widget_queue_draw (GTK_WIDGET (editor));
g_object_notify (G_OBJECT (editor), "color");
......
/* 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
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "config.h"
#include "gtkcolorscale.h"
#include "gtkhsv.h"
#include "gtkstylecontext.h"
#include "gtkintl.h"
struct _GtkColorScalePrivate
{
cairo_surface_t *surface;
gint width, height;
GdkRGBA color;
GtkColorScaleType type;
};
G_DEFINE_TYPE (GtkColorScale, gtk_color_scale, GTK_TYPE_SCALE)
static cairo_pattern_t *
get_checkered_pattern (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;
}
static void
create_h_surface (GtkColorScale *scale)
{
GtkWidget *widget = GTK_WIDGET (scale);
cairo_t *cr;
cairo_surface_t *surface;
gint width, height, stride;
cairo_surface_t *tmp;
guint red, green, blue;
guint32 *data, *p;
gdouble h;
gdouble r, g, b;
gdouble f;
gint x, y;
if (!gtk_widget_get_realized (widget))
return;
width = gtk_widget_get_allocated_width (widget);
height = gtk_widget_get_allocated_height (widget);
if (width != scale->priv->width ||
height != scale->priv->height)
{
surface = gdk_window_create_similar_surface (gtk_widget_get_window (widget),
CAIRO_CONTENT_COLOR,
width, height);
if (scale->priv->surface)
cairo_surface_destroy (scale->priv->surface);
scale->priv->surface = surface;
scale->priv->width = width;
scale->priv->height= height;
}
else
surface = scale->priv->surface;
if (width == 1 || height == 1)
return;
stride = cairo_format_stride_for_width (CAIRO_FORMAT_RGB24, width);
data = g_malloc (height * stride);
f = 1.0 / (height - 1);
for (y = 0; y < height; y++)
{
h = CLAMP (y * f, 0.0, 1.0);
p = data + y * (stride / 4);
for (x = 0; x < width; x++)
{
gtk_hsv_to_rgb (h, 1, 1, &r, &g, &b);
red = CLAMP (r * 255, 0, 255);
green = CLAMP (g * 255, 0, 255);
blue = CLAMP (b * 255, 0, 255);
p[x] = (red << 16) | (green << 8) | blue;
}
}
tmp = cairo_image_surface_create_for_data ((guchar *)data, CAIRO_FORMAT_RGB24,
width, height, stride);
cr = cairo_create (surface);
cairo_set_source_surface (cr, tmp, 0, 0);
cairo_paint (cr);
cairo_destroy (cr);
cairo_surface_destroy (tmp);
g_free (data);
}
static void
create_a_surface (GtkColorScale *scale)
{
GtkWidget *widget = GTK_WIDGET (scale);
cairo_t *cr;
cairo_surface_t *surface;
cairo_pattern_t *pattern;
cairo_matrix_t matrix;
GdkRGBA *color;
gint width, height;
if (!gtk_widget_get_realized (widget))
return;
width = gtk_widget_get_allocated_width (widget);
height = gtk_widget_get_allocated_height (widget);
if (!scale->priv->surface ||
width != scale->priv->width ||
height != scale->priv->height)
{
surface = gdk_window_create_similar_surface (gtk_widget_get_window (widget),
CAIRO_CONTENT_COLOR,
width, height);
if (scale->priv->surface)
cairo_surface_destroy (scale->priv->surface);
scale->priv->surface = surface;
scale->priv->width = width;
scale->priv->height = height;
}
else
return;
if (width == 1 || height == 1)
return;
cr = cairo_create (surface);
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);
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);
color = &scale->priv->color;
pattern = cairo_pattern_create_linear (0, 0, width, 0);
cairo_pattern_add_color_stop_rgba (pattern, 0, color->red, color->green, color->blue, 0);
cairo_pattern_add_color_stop_rgba (pattern, width, color->red, color->green, color->blue, 1);
cairo_set_source (cr, pattern);
cairo_paint (cr);
cairo_pattern_destroy (pattern);
cairo_destroy (cr);
}
static void
create_surface (GtkColorScale *scale)
{
switch (scale->priv->type)
{
case GTK_COLOR_SCALE_HUE:
create_h_surface (scale);
break;
case GTK_COLOR_SCALE_ALPHA:
create_a_surface (scale);
break;
}
}
static gboolean
scale_draw (GtkWidget *widget,
cairo_t *cr)
{
GtkColorScale *scale = GTK_COLOR_SCALE (widget);
gint width, height;
width = gtk_widget_get_allocated_width (widget);
height = gtk_widget_get_allocated_height (widget);
create_surface (scale);
cairo_save (cr);
cairo_rectangle (cr, 1, 1, width - 2, height - 2);
cairo_clip (cr);
cairo_set_source_surface (cr, scale->priv->surface, 0, 0);
cairo_paint (cr);
cairo_restore (cr);
GTK_WIDGET_CLASS (gtk_color_scale_parent_class)->draw (widget, cr);
return FALSE;
}
static void
gtk_color_scale_init (GtkColorScale *scale)
{
scale->priv = G_TYPE_INSTANCE_GET_PRIVATE (scale,
GTK_TYPE_COLOR_SCALE,
GtkColorScalePrivate);
scale->priv->type = GTK_COLOR_SCALE_HUE;
}
static void
scale_finalize (GObject *object)
{
GtkColorScale *scale = GTK_COLOR_SCALE (object);
if (scale->priv->surface)
cairo_surface_destroy (scale->priv->surface);
G_OBJECT_CLASS (gtk_color_scale_parent_class)->finalize (object);
}
static void
gtk_color_scale_class_init (GtkColorScaleClass *class)
{
GObjectClass *object_class = G_OBJECT_CLASS (class);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
object_class->finalize = scale_finalize;
widget_class->draw = scale_draw;
g_type_class_add_private (class, sizeof (GtkColorScalePrivate));
}
void
gtk_color_scale_set_color (GtkColorScale *scale,
const GdkRGBA *color)
{
scale->priv->color.red = color->red;
scale->priv->color.green = color->green;
scale->priv->color.blue = color->blue;
scale->priv->color.alpha = color->alpha;
if (scale->priv->surface)
{
cairo_surface_destroy (scale->priv->surface);
scale->priv->surface = NULL;
}
create_surface (scale);
gtk_widget_queue_draw (GTK_WIDGET (scale));
}
void
gtk_color_scale_set_type (GtkColorScale *scale,
GtkColorScaleType type)
{
scale->priv->type = type;
cairo_surface_destroy (scale->priv->surface);
scale->priv->surface = NULL;
create_surface (scale);
gtk_widget_queue_draw (GTK_WIDGET (scale));
}
GtkWidget *
gtk_color_scale_new (void)
{
return (GtkWidget *) g_object_new (GTK_TYPE_COLOR_SCALE,
"adjustment", gtk_adjustment_new (0, 0, 1, 0.01, 0.1, 0),
"draw-value", FALSE,
NULL);
}
/* 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
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#if !defined (__GTK_H_INSIDE__) && !defined (GTK_COMPILATION)
#error "Only <gtk/gtk.h> can be included directly."
#endif
#ifndef __GTK_COLOR_SCALE_H__
#define __GTK_COLOR_SCALE_H__
#include <gtk/gtkscale.h>
G_BEGIN_DECLS
#define GTK_TYPE_COLOR_SCALE (gtk_color_scale_get_type ())
#define GTK_COLOR_SCALE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_COLOR_SCALE, GtkColorScale))
#define GTK_COLOR_SCALE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_COLOR_SCALE, GtkColorScaleClass))
#define GTK_IS_COLOR_SCALE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_COLOR_SCALE))
#define GTK_IS_COLOR_SCALE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_COLOR_SCALE))
#define GTK_COLOR_SCALE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_COLOR_SCALE, GtkColorScaleClass))
typedef struct _GtkColorScale GtkColorScale;
typedef struct _GtkColorScaleClass GtkColorScaleClass;
typedef struct _GtkColorScalePrivate GtkColorScalePrivate;
struct _GtkColorScale
{
GtkScale parent_instance;
GtkColorScalePrivate *priv;
};
struct _GtkColorScaleClass
{
GtkScaleClass parent_class;
/* Padding for future expansion */
void (*_gtk_reserved1) (void);
void (*_gtk_reserved2) (void);
void (*_gtk_reserved3) (void);
void (*_gtk_reserved4) (void);
};
typedef enum
{
GTK_COLOR_SCALE_HUE,
GTK_COLOR_SCALE_ALPHA
} GtkColorScaleType;
GType gtk_color_scale_get_type (void) G_GNUC_CONST;
GtkWidget * gtk_color_scale_new (void);
void gtk_color_scale_set_type (GtkColorScale *scale,
GtkColorScaleType type);
void gtk_color_scale_set_color (GtkColorScale *scale,
const GdkRGBA *color);
G_END_DECLS
#endif /* __GTK_COLOR_SCALE_H__ */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment