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"
|
|
|
|
|
2012-02-09 00:36:10 +00:00
|
|
|
#include "gtkcolorscaleprivate.h"
|
2012-02-04 05:02:49 +00:00
|
|
|
|
2012-02-14 17:16:56 +00:00
|
|
|
#include "gtkcolorchooserprivate.h"
|
2014-04-08 19:17:54 +00:00
|
|
|
#include "gtkgesturelongpress.h"
|
2020-05-18 15:31:42 +00:00
|
|
|
#include "gtkgestureclick.h"
|
2012-02-04 01:38:21 +00:00
|
|
|
#include "gtkcolorutils.h"
|
2012-02-02 06:16:00 +00:00
|
|
|
#include "gtkorientable.h"
|
2016-02-23 03:08:09 +00:00
|
|
|
#include "gtkrangeprivate.h"
|
2012-02-04 05:02:49 +00:00
|
|
|
#include "gtkprivate.h"
|
2012-02-02 05:58:26 +00:00
|
|
|
#include "gtkintl.h"
|
2016-12-15 09:09:41 +00:00
|
|
|
#include "gtksnapshot.h"
|
2020-03-19 02:36:31 +00:00
|
|
|
#include "gtkshortcutcontroller.h"
|
|
|
|
#include "gtkshortcuttrigger.h"
|
|
|
|
#include "gtkshortcutaction.h"
|
|
|
|
#include "gtkshortcut.h"
|
2012-02-02 05:58:26 +00:00
|
|
|
|
2012-02-14 19:02:43 +00:00
|
|
|
#include <math.h>
|
|
|
|
|
2020-09-19 18:26:48 +00:00
|
|
|
struct _GtkColorScale
|
2012-02-02 05:58:26 +00:00
|
|
|
{
|
2020-09-19 18:26:48 +00:00
|
|
|
GtkScale parent_instance;
|
|
|
|
|
2012-02-02 05:58:26 +00:00
|
|
|
GdkRGBA color;
|
|
|
|
GtkColorScaleType type;
|
2018-11-23 09:57:50 +00:00
|
|
|
GdkTexture *hue_texture;
|
2020-09-19 18:26:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
GtkScaleClass parent_class;
|
|
|
|
} GtkColorScaleClass;
|
2012-02-02 05:58:26 +00:00
|
|
|
|
2012-02-04 05:02:49 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_ZERO,
|
|
|
|
PROP_SCALE_TYPE
|
|
|
|
};
|
|
|
|
|
2014-04-08 19:17:54 +00:00
|
|
|
static void hold_action (GtkGestureLongPress *gesture,
|
2020-07-24 20:32:16 +00:00
|
|
|
double x,
|
|
|
|
double y,
|
2020-03-19 02:36:31 +00:00
|
|
|
GtkWidget *scale);
|
2014-04-08 19:17:54 +00:00
|
|
|
|
2020-05-18 15:31:42 +00:00
|
|
|
static void click_action (GtkGestureClick *gesture,
|
|
|
|
guint n_presses,
|
|
|
|
double x,
|
|
|
|
double y,
|
|
|
|
GtkWidget *scale);
|
|
|
|
|
2020-09-19 18:26:48 +00:00
|
|
|
G_DEFINE_TYPE (GtkColorScale, gtk_color_scale, GTK_TYPE_SCALE)
|
2012-02-02 05:58:26 +00:00
|
|
|
|
2016-03-09 16:27:01 +00:00
|
|
|
void
|
2016-12-15 09:09:41 +00:00
|
|
|
gtk_color_scale_snapshot_trough (GtkColorScale *scale,
|
|
|
|
GtkSnapshot *snapshot,
|
|
|
|
int width,
|
|
|
|
int height)
|
2012-02-02 05:58:26 +00:00
|
|
|
{
|
2016-12-15 09:09:41 +00:00
|
|
|
GtkWidget *widget = GTK_WIDGET (scale);
|
2012-02-02 05:58:26 +00:00
|
|
|
|
2016-03-09 16:27:01 +00:00
|
|
|
if (width <= 1 || height <= 1)
|
2012-02-02 05:58:26 +00:00
|
|
|
return;
|
|
|
|
|
2020-09-19 18:26:48 +00:00
|
|
|
if (scale->hue_texture &&
|
|
|
|
(width != gdk_texture_get_width (scale->hue_texture) ||
|
|
|
|
height != gdk_texture_get_height (scale->hue_texture)))
|
|
|
|
g_clear_object (&scale->hue_texture);
|
2018-11-23 09:57:50 +00:00
|
|
|
|
2020-09-19 18:26:48 +00:00
|
|
|
if (scale->type == GTK_COLOR_SCALE_HUE)
|
2012-02-02 05:58:26 +00:00
|
|
|
{
|
2020-09-19 18:26:48 +00:00
|
|
|
if (!scale->hue_texture)
|
2012-02-02 05:58:26 +00:00
|
|
|
{
|
2018-11-23 09:57:50 +00:00
|
|
|
GdkTexture *texture;
|
2020-07-24 13:54:49 +00:00
|
|
|
int stride;
|
2018-11-23 09:57:50 +00:00
|
|
|
GBytes *bytes;
|
|
|
|
guchar *data, *p;
|
2020-07-24 20:32:16 +00:00
|
|
|
double h;
|
2019-12-10 13:22:52 +00:00
|
|
|
float r, g, b;
|
2020-07-24 20:32:16 +00:00
|
|
|
double f;
|
2018-11-23 09:57:50 +00:00
|
|
|
int hue_x, hue_y;
|
|
|
|
|
|
|
|
stride = width * 3;
|
|
|
|
data = g_malloc (width * height * 3);
|
|
|
|
|
|
|
|
f = 1.0 / (height - 1);
|
|
|
|
for (hue_y = 0; hue_y < height; hue_y++)
|
2012-02-04 05:02:49 +00:00
|
|
|
{
|
2018-11-23 09:57:50 +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)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
2018-11-23 09:57:50 +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);
|
2020-09-19 18:26:48 +00:00
|
|
|
scale->hue_texture = texture;
|
2018-11-23 09:57:50 +00:00
|
|
|
}
|
2019-10-12 15:53:59 +00:00
|
|
|
|
|
|
|
gtk_snapshot_append_texture (snapshot,
|
2020-09-19 18:26:48 +00:00
|
|
|
scale->hue_texture,
|
2019-10-12 15:53:59 +00:00
|
|
|
&GRAPHENE_RECT_INIT(0, 0, width, height));
|
2012-02-02 05:58:26 +00:00
|
|
|
}
|
2020-09-19 18:26:48 +00:00
|
|
|
else if (scale->type == GTK_COLOR_SCALE_ALPHA)
|
2012-02-04 05:02:49 +00:00
|
|
|
{
|
2018-03-25 22:29:52 +00:00
|
|
|
graphene_point_t start, end;
|
2019-08-11 06:34:43 +00:00
|
|
|
const GdkRGBA *color;
|
2018-03-25 22:12:20 +00:00
|
|
|
|
|
|
|
if (gtk_orientable_get_orientation (GTK_ORIENTABLE (widget)) == GTK_ORIENTATION_HORIZONTAL &&
|
|
|
|
gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
|
|
|
|
{
|
2018-11-09 17:01:26 +00:00
|
|
|
graphene_point_init (&start, width, 0);
|
|
|
|
graphene_point_init (&end, 0, 0);
|
2018-03-25 22:29:52 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-11-09 17:01:26 +00:00
|
|
|
graphene_point_init (&start, 0, 0);
|
|
|
|
graphene_point_init (&end, width, 0);
|
2018-03-25 22:12:20 +00:00
|
|
|
}
|
|
|
|
|
2019-08-11 06:34:43 +00:00
|
|
|
_gtk_color_chooser_snapshot_checkered_pattern (snapshot, width, height);
|
2018-03-25 22:29:52 +00:00
|
|
|
|
2020-09-19 18:26:48 +00:00
|
|
|
color = &scale->color;
|
2018-06-18 12:28:19 +00:00
|
|
|
|
2018-03-25 22:29:52 +00:00
|
|
|
gtk_snapshot_append_linear_gradient (snapshot,
|
2018-11-09 17:01:26 +00:00
|
|
|
&GRAPHENE_RECT_INIT(0, 0, width, height),
|
2018-03-25 22:29:52 +00:00
|
|
|
&start,
|
|
|
|
&end,
|
|
|
|
(GskColorStop[2]) {
|
|
|
|
{ 0, { color->red, color->green, color->blue, 0 } },
|
|
|
|
{ 1, { color->red, color->green, color->blue, 1 } },
|
|
|
|
},
|
2018-04-24 01:17:23 +00:00
|
|
|
2);
|
2018-03-25 22:12:20 +00:00
|
|
|
}
|
2012-02-02 05:58:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_color_scale_init (GtkColorScale *scale)
|
|
|
|
{
|
2018-03-09 05:36:22 +00:00
|
|
|
GtkGesture *gesture;
|
2016-01-16 01:08:57 +00:00
|
|
|
|
2018-03-09 05:36:22 +00:00
|
|
|
gesture = gtk_gesture_long_press_new ();
|
|
|
|
g_signal_connect (gesture, "pressed",
|
2014-04-08 19:17:54 +00:00
|
|
|
G_CALLBACK (hold_action), scale);
|
2018-03-09 05:36:22 +00:00
|
|
|
gtk_event_controller_set_propagation_phase (GTK_EVENT_CONTROLLER (gesture),
|
2014-05-27 10:38:49 +00:00
|
|
|
GTK_PHASE_TARGET);
|
2018-03-09 05:36:22 +00:00
|
|
|
gtk_widget_add_controller (GTK_WIDGET (scale), GTK_EVENT_CONTROLLER (gesture));
|
2016-01-16 01:08:57 +00:00
|
|
|
|
2020-05-18 15:31:42 +00:00
|
|
|
gesture = gtk_gesture_click_new ();
|
|
|
|
gtk_gesture_single_set_button (GTK_GESTURE_SINGLE (gesture), GDK_BUTTON_SECONDARY);
|
|
|
|
g_signal_connect (gesture, "pressed",
|
|
|
|
G_CALLBACK (click_action), scale);
|
|
|
|
gtk_widget_add_controller (GTK_WIDGET (scale), GTK_EVENT_CONTROLLER (gesture));
|
|
|
|
|
2020-02-06 16:32:26 +00:00
|
|
|
gtk_widget_add_css_class (GTK_WIDGET (scale), "color");
|
2012-02-02 05:58:26 +00:00
|
|
|
}
|
|
|
|
|
2020-03-19 02:36:31 +00:00
|
|
|
static void
|
|
|
|
scale_constructed (GObject *object)
|
|
|
|
{
|
|
|
|
GtkColorScale *scale = GTK_COLOR_SCALE (object);
|
|
|
|
GtkEventController *controller;
|
|
|
|
GtkShortcutTrigger *trigger;
|
|
|
|
GtkShortcutAction *action;
|
|
|
|
GtkShortcut *shortcut;
|
|
|
|
|
|
|
|
controller = gtk_shortcut_controller_new ();
|
|
|
|
trigger = gtk_alternative_trigger_new (gtk_keyval_trigger_new (GDK_KEY_F10, GDK_SHIFT_MASK),
|
|
|
|
gtk_keyval_trigger_new (GDK_KEY_Menu, 0));
|
2020-03-20 15:19:45 +00:00
|
|
|
action = gtk_named_action_new ("color.edit");
|
2020-03-19 02:36:31 +00:00
|
|
|
shortcut = gtk_shortcut_new_with_arguments (trigger,
|
|
|
|
action,
|
|
|
|
"s",
|
2020-09-19 18:26:48 +00:00
|
|
|
scale->type == GTK_COLOR_SCALE_ALPHA
|
2020-03-19 02:36:31 +00:00
|
|
|
? "a" : "h");
|
|
|
|
gtk_shortcut_controller_add_shortcut (GTK_SHORTCUT_CONTROLLER (controller), shortcut);
|
|
|
|
gtk_widget_add_controller (GTK_WIDGET (scale), controller);
|
|
|
|
}
|
|
|
|
|
2012-02-02 05:58:26 +00:00
|
|
|
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:
|
2020-09-19 18:26:48 +00:00
|
|
|
g_value_set_int (value, scale->type);
|
2012-02-04 05:02:49 +00:00
|
|
|
break;
|
2020-09-19 18:26:48 +00:00
|
|
|
|
2012-02-04 05:02:49 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
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:
|
2020-09-19 18:26:48 +00:00
|
|
|
scale->type = (GtkColorScaleType) g_value_get_int (value);
|
2012-02-04 05:02:49 +00:00
|
|
|
break;
|
2020-09-19 18:26:48 +00:00
|
|
|
|
2012-02-04 05:02:49 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-04 16:22:44 +00:00
|
|
|
static void
|
2014-04-08 19:17:54 +00:00
|
|
|
hold_action (GtkGestureLongPress *gesture,
|
2020-07-24 20:32:16 +00:00
|
|
|
double x,
|
|
|
|
double y,
|
2020-03-19 02:36:31 +00:00
|
|
|
GtkWidget *scale)
|
2012-03-04 16:22:44 +00:00
|
|
|
{
|
2020-03-19 02:36:31 +00:00
|
|
|
gtk_widget_activate_action (scale,
|
|
|
|
"color.edit",
|
|
|
|
"s", gtk_widget_get_name (scale));
|
2012-03-04 16:22:44 +00:00
|
|
|
}
|
|
|
|
|
2020-05-18 15:31:42 +00:00
|
|
|
static void
|
|
|
|
click_action (GtkGestureClick *gesture,
|
|
|
|
guint n_presses,
|
|
|
|
double x,
|
|
|
|
double y,
|
|
|
|
GtkWidget *scale)
|
|
|
|
{
|
|
|
|
gtk_widget_activate_action (scale,
|
|
|
|
"color.edit",
|
|
|
|
"s", gtk_widget_get_name (scale));
|
|
|
|
}
|
|
|
|
|
2018-11-23 09:57:50 +00:00
|
|
|
static void
|
|
|
|
scale_finalize (GObject *object)
|
|
|
|
{
|
2020-09-19 18:26:48 +00:00
|
|
|
GtkColorScale *scale = GTK_COLOR_SCALE (object);
|
2018-11-23 09:57:50 +00:00
|
|
|
|
2020-09-19 18:26:48 +00:00
|
|
|
g_clear_object (&scale->hue_texture);
|
2018-11-23 09:57:50 +00:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (gtk_color_scale_parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
2012-02-04 05:02:49 +00:00
|
|
|
static void
|
|
|
|
gtk_color_scale_class_init (GtkColorScaleClass *class)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (class);
|
|
|
|
|
2020-03-19 02:36:31 +00:00
|
|
|
object_class->constructed = scale_constructed;
|
2018-11-23 09:57:50 +00:00
|
|
|
object_class->finalize = scale_finalize;
|
2012-02-04 05:02:49 +00:00
|
|
|
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)
|
|
|
|
{
|
2020-09-19 18:26:48 +00:00
|
|
|
scale->color = *color;
|
2019-01-16 18:20:53 +00:00
|
|
|
gtk_widget_queue_draw (gtk_range_get_trough_widget (GTK_RANGE (scale)));
|
2012-02-04 05:02:49 +00:00
|
|
|
}
|
|
|
|
|
2012-02-02 05:58:26 +00:00
|
|
|
GtkWidget *
|
2012-02-03 17:13:06 +00:00
|
|
|
gtk_color_scale_new (GtkAdjustment *adjustment,
|
|
|
|
GtkColorScaleType type)
|
2012-02-02 05:58:26 +00:00
|
|
|
{
|
2013-06-27 19:02:52 +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
|
|
|
}
|