gimp/app/widgets/gimpcolorbar.c

336 lines
9.5 KiB
C
Raw Normal View History

/* GIMP - The GNU Image Manipulation Program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "config.h"
#include <string.h>
#include <gtk/gtk.h>
#include "libgimpmath/gimpmath.h"
#include "libgimpcolor/gimpcolor.h"
#include "libgimpconfig/gimpconfig.h"
#include "widgets-types.h"
#include "gimpcolorbar.h"
enum
{
PROP_0,
PROP_ORIENTATION,
PROP_COLOR,
PROP_CHANNEL
};
/* local function prototypes */
static void gimp_color_bar_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec);
static void gimp_color_bar_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec);
static gboolean gimp_color_bar_expose (GtkWidget *widget,
GdkEventExpose *event);
G_DEFINE_TYPE (GimpColorBar, gimp_color_bar, GTK_TYPE_EVENT_BOX)
#define parent_class gimp_color_bar_parent_class
static void
gimp_color_bar_class_init (GimpColorBarClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
GimpRGB white = { 1.0, 1.0, 1.0, 1.0 };
object_class->set_property = gimp_color_bar_set_property;
object_class->get_property = gimp_color_bar_get_property;
widget_class->expose_event = gimp_color_bar_expose;
g_object_class_install_property (object_class, PROP_ORIENTATION,
g_param_spec_enum ("orientation",
NULL, NULL,
GTK_TYPE_ORIENTATION,
GTK_ORIENTATION_HORIZONTAL,
GIMP_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property (object_class, PROP_COLOR,
gimp_param_spec_rgb ("color",
NULL, NULL,
added "has_alpha" to GimpParamSpecRGB. Made the GimpParamSpecRGB struct 2006-04-27 Sven Neumann <sven@gimp.org> * libgimpcolor/gimprgb.[ch]: added "has_alpha" to GimpParamSpecRGB. Made the GimpParamSpecRGB struct public. When validating a color, only look at the alpha channel if has_alpha is set. * libgimpconfig/gimpconfig-params.h: added "has_alpha" to GIMP_CONFIG_INSTALL_PROP_RGB macro definition. * libgimpconfig/gimpconfig-serialize.c: serialize color values as "(rgb r g b)" if the param-spec indicates that the alpha channel is meaningless. * app/config/gimpconfig-dump.c: take "has_alpha" into account when documenting color properties. * app/core/gimpcontext.c * app/core/gimpgrid.c * app/display/gimpdisplayoptions.c * app/text/gimptext.c * app/widgets/gimpaction.c * app/widgets/gimpcolorbar.c * libgimpwidgets/gimpcolorarea.c * libgimpwidgets/gimpcolorbutton.c: specify whether color properties have an alpha channel. * tools/pdbgen/app.pl: handle "has_alpha" for color paramaters. * tools/pdbgen/pdb/channel.pdb * tools/pdbgen/pdb/context.pdb * tools/pdbgen/pdb/grid.pdb * tools/pdbgen/pdb/image.pdb: set the "has_alpha" flag where appropriate. * app/pdb/gimp-pdb-compat.c (gimp_pdb_compat_param_spec): set "has_alpha" to TRUE for GIMP_PDB_COLOR. * app/pdb/channel_cmds.c * app/pdb/context_cmds.c * app/pdb/gradient_cmds.c * app/pdb/grid_cmds.c * app/pdb/image_cmds.c * app/pdb/palette_cmds.c * app/pdb/palettes_cmds.c * app/pdb/selection_tools_cmds.c: regenerated. * app/config/gimpdisplayconfig.c (gimp_display_config_class_init): removed unused code.
2006-04-27 23:19:59 +08:00
FALSE, &white,
GIMP_PARAM_WRITABLE |
G_PARAM_CONSTRUCT));
g_object_class_install_property (object_class, PROP_CHANNEL,
g_param_spec_enum ("histogram-channel",
NULL, NULL,
GIMP_TYPE_HISTOGRAM_CHANNEL,
GIMP_HISTOGRAM_VALUE,
GIMP_PARAM_WRITABLE));
}
static void
gimp_color_bar_init (GimpColorBar *bar)
{
gtk_event_box_set_visible_window (GTK_EVENT_BOX (bar), FALSE);
bar->orientation = GTK_ORIENTATION_HORIZONTAL;
}
static void
gimp_color_bar_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec)
{
GimpColorBar *bar = GIMP_COLOR_BAR (object);
switch (property_id)
{
case PROP_ORIENTATION:
bar->orientation = g_value_get_enum (value);
break;
case PROP_COLOR:
gimp_color_bar_set_color (bar, g_value_get_boxed (value));
break;
case PROP_CHANNEL:
gimp_color_bar_set_channel (bar, g_value_get_enum (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
gimp_color_bar_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec)
{
GimpColorBar *bar = GIMP_COLOR_BAR (object);
switch (property_id)
{
case PROP_ORIENTATION:
g_value_set_enum (value, bar->orientation);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static gboolean
gimp_color_bar_expose (GtkWidget *widget,
GdkEventExpose *event)
{
app/widgets/gimpactionview.c app/widgets/gimpblobeditor.c 2008-06-28 Michael Natterer <mitch@gimp.org> * app/widgets/gimpactionview.c * app/widgets/gimpblobeditor.c * app/widgets/gimpbrushfactoryview.c * app/widgets/gimpbrushselect.c * app/widgets/gimpcellrendererdashes.c * app/widgets/gimpcellrendererviewable.c * app/widgets/gimpcolorbar.c * app/widgets/gimpcoloreditor.c * app/widgets/gimpcolorframe.c * app/widgets/gimpcomponenteditor.c * app/widgets/gimpcontainerbox.c * app/widgets/gimpcontainergridview.c * app/widgets/gimpcontainerpopup.c * app/widgets/gimpcontainertreeview.c * app/widgets/gimpcurveview.c * app/widgets/gimpdasheditor.c * app/widgets/gimpdatafactoryview.c * app/widgets/gimpdock.c * app/widgets/gimpdockable.c * app/widgets/gimpdockseparator.c * app/widgets/gimpfgbgeditor.c * app/widgets/gimpfgbgview.c * app/widgets/gimpgradienteditor.c * app/widgets/gimphandlebar.c * app/widgets/gimphistogrambox.c * app/widgets/gimphistogramview.c * app/widgets/gimpitemtreeview.c * app/widgets/gimpmenudock.c * app/widgets/gimpmessagebox.c * app/widgets/gimppaletteview.c * app/widgets/gimpscalebutton.c * app/widgets/gimpsessioninfo-book.c * app/widgets/gimpsessioninfo-dock.c * app/widgets/gimpsettingseditor.c * app/widgets/gimpstrokeeditor.c * app/widgets/gimptemplateeditor.c * app/widgets/gimptemplateview.c * app/widgets/gimpthumbbox.c * app/widgets/gimptoolbox.c * app/widgets/gimptooloptionseditor.c * app/widgets/gimptoolview.c * app/widgets/gimpuimanager.c * app/widgets/gimpviewabledialog.c * app/widgets/gimpviewrenderervectors.c * app/widgets/gimpwidgets-utils.c: use accessors instead of accessing members of GTK+ widgets directly. svn path=/trunk/; revision=26008
2008-06-28 23:50:27 +08:00
GimpColorBar *bar = GIMP_COLOR_BAR (widget);
GtkStyle *style = gtk_widget_get_style (widget);
guchar *buf;
guchar *b;
gint x, y;
gint width, height;
gint i, j;
app/widgets/gimpactionview.c app/widgets/gimpblobeditor.c 2008-06-28 Michael Natterer <mitch@gimp.org> * app/widgets/gimpactionview.c * app/widgets/gimpblobeditor.c * app/widgets/gimpbrushfactoryview.c * app/widgets/gimpbrushselect.c * app/widgets/gimpcellrendererdashes.c * app/widgets/gimpcellrendererviewable.c * app/widgets/gimpcolorbar.c * app/widgets/gimpcoloreditor.c * app/widgets/gimpcolorframe.c * app/widgets/gimpcomponenteditor.c * app/widgets/gimpcontainerbox.c * app/widgets/gimpcontainergridview.c * app/widgets/gimpcontainerpopup.c * app/widgets/gimpcontainertreeview.c * app/widgets/gimpcurveview.c * app/widgets/gimpdasheditor.c * app/widgets/gimpdatafactoryview.c * app/widgets/gimpdock.c * app/widgets/gimpdockable.c * app/widgets/gimpdockseparator.c * app/widgets/gimpfgbgeditor.c * app/widgets/gimpfgbgview.c * app/widgets/gimpgradienteditor.c * app/widgets/gimphandlebar.c * app/widgets/gimphistogrambox.c * app/widgets/gimphistogramview.c * app/widgets/gimpitemtreeview.c * app/widgets/gimpmenudock.c * app/widgets/gimpmessagebox.c * app/widgets/gimppaletteview.c * app/widgets/gimpscalebutton.c * app/widgets/gimpsessioninfo-book.c * app/widgets/gimpsessioninfo-dock.c * app/widgets/gimpsettingseditor.c * app/widgets/gimpstrokeeditor.c * app/widgets/gimptemplateeditor.c * app/widgets/gimptemplateview.c * app/widgets/gimpthumbbox.c * app/widgets/gimptoolbox.c * app/widgets/gimptooloptionseditor.c * app/widgets/gimptoolview.c * app/widgets/gimpuimanager.c * app/widgets/gimpviewabledialog.c * app/widgets/gimpviewrenderervectors.c * app/widgets/gimpwidgets-utils.c: use accessors instead of accessing members of GTK+ widgets directly. svn path=/trunk/; revision=26008
2008-06-28 23:50:27 +08:00
x = y = gtk_container_get_border_width (GTK_CONTAINER (bar));
width = widget->allocation.width - 2 * x;
height = widget->allocation.height - 2 * y;
if (width < 1 || height < 1)
return TRUE;
buf = g_alloca (width * height * 3);
switch (bar->orientation)
{
case GTK_ORIENTATION_HORIZONTAL:
for (i = 0, b = buf; i < width; i++, b += 3)
{
const guchar *src = bar->buf + 3 * ((i * 256) / width);
b[0] = src[0];
b[1] = src[1];
b[2] = src[2];
}
for (i = 1; i < height; i++)
memcpy (buf + i * width * 3, buf, width * 3);
break;
case GTK_ORIENTATION_VERTICAL:
for (i = 0, b = buf; i < height; i++, b += 3 * width)
{
const guchar *src = bar->buf + 3 * (255 - ((i * 256) / height));
guchar *dest = b;
for (j = 0; j < width; j++, dest += 3)
{
dest[0] = src[0];
dest[1] = src[1];
dest[2] = src[2];
}
}
break;
}
app/widgets/gimpactionview.c app/widgets/gimpblobeditor.c 2008-06-28 Michael Natterer <mitch@gimp.org> * app/widgets/gimpactionview.c * app/widgets/gimpblobeditor.c * app/widgets/gimpbrushfactoryview.c * app/widgets/gimpbrushselect.c * app/widgets/gimpcellrendererdashes.c * app/widgets/gimpcellrendererviewable.c * app/widgets/gimpcolorbar.c * app/widgets/gimpcoloreditor.c * app/widgets/gimpcolorframe.c * app/widgets/gimpcomponenteditor.c * app/widgets/gimpcontainerbox.c * app/widgets/gimpcontainergridview.c * app/widgets/gimpcontainerpopup.c * app/widgets/gimpcontainertreeview.c * app/widgets/gimpcurveview.c * app/widgets/gimpdasheditor.c * app/widgets/gimpdatafactoryview.c * app/widgets/gimpdock.c * app/widgets/gimpdockable.c * app/widgets/gimpdockseparator.c * app/widgets/gimpfgbgeditor.c * app/widgets/gimpfgbgview.c * app/widgets/gimpgradienteditor.c * app/widgets/gimphandlebar.c * app/widgets/gimphistogrambox.c * app/widgets/gimphistogramview.c * app/widgets/gimpitemtreeview.c * app/widgets/gimpmenudock.c * app/widgets/gimpmessagebox.c * app/widgets/gimppaletteview.c * app/widgets/gimpscalebutton.c * app/widgets/gimpsessioninfo-book.c * app/widgets/gimpsessioninfo-dock.c * app/widgets/gimpsettingseditor.c * app/widgets/gimpstrokeeditor.c * app/widgets/gimptemplateeditor.c * app/widgets/gimptemplateview.c * app/widgets/gimpthumbbox.c * app/widgets/gimptoolbox.c * app/widgets/gimptooloptionseditor.c * app/widgets/gimptoolview.c * app/widgets/gimpuimanager.c * app/widgets/gimpviewabledialog.c * app/widgets/gimpviewrenderervectors.c * app/widgets/gimpwidgets-utils.c: use accessors instead of accessing members of GTK+ widgets directly. svn path=/trunk/; revision=26008
2008-06-28 23:50:27 +08:00
gdk_draw_rgb_image (widget->window, style->black_gc,
widget->allocation.x + x, widget->allocation.y + y,
width, height,
GDK_RGB_DITHER_NORMAL,
buf, 3 * width);
return TRUE;
}
/* public functions */
/**
* gimp_color_bar_new:
* @orientation: whether the bar should be oriented horizontally or
* vertically
*
* Creates a new #GimpColorBar widget.
*
* Return value: The new #GimpColorBar widget.
**/
GtkWidget *
gimp_color_bar_new (GtkOrientation orientation)
{
return g_object_new (GIMP_TYPE_COLOR_BAR,
"orientation", orientation,
NULL);
}
/**
* gimp_color_bar_set_color:
* @bar: a #GimpColorBar widget
* @color: a #GimpRGB color
*
* Makes the @bar display a gradient from black (on the left or the
* bottom), to the given @color (on the right or at the top).
**/
void
gimp_color_bar_set_color (GimpColorBar *bar,
const GimpRGB *color)
{
guchar *buf;
gint i;
g_return_if_fail (GIMP_IS_COLOR_BAR (bar));
g_return_if_fail (color != NULL);
for (i = 0, buf = bar->buf; i < 256; i++, buf += 3)
{
buf[0] = ROUND (color->r * (gdouble) i);
buf[1] = ROUND (color->g * (gdouble) i);
buf[2] = ROUND (color->b * (gdouble) i);
}
gtk_widget_queue_draw (GTK_WIDGET (bar));
}
/**
* gimp_color_bar_set_channel:
* @bar: a #GimpColorBar widget
* @channel: a #GimpHistogramChannel
*
* Convenience function that calls gimp_color_bar_set_color() with the
* color that matches the @channel.
**/
void
gimp_color_bar_set_channel (GimpColorBar *bar,
GimpHistogramChannel channel)
{
GimpRGB color = { 1.0, 1.0, 1.0, 1.0 };
g_return_if_fail (GIMP_IS_COLOR_BAR (bar));
switch (channel)
{
case GIMP_HISTOGRAM_VALUE:
case GIMP_HISTOGRAM_ALPHA:
case GIMP_HISTOGRAM_RGB:
gimp_rgb_set (&color, 1.0, 1.0, 1.0);
break;
case GIMP_HISTOGRAM_RED:
gimp_rgb_set (&color, 1.0, 0.0, 0.0);
break;
case GIMP_HISTOGRAM_GREEN:
gimp_rgb_set (&color, 0.0, 1.0, 0.0);
break;
case GIMP_HISTOGRAM_BLUE:
gimp_rgb_set (&color, 0.0, 0.0, 1.0);
break;
}
gimp_color_bar_set_color (bar, &color);
}
/**
* gimp_color_bar_set_buffers:
* @bar: a #GimpColorBar widget
* @red: an array of 256 values
* @green: an array of 256 values
* @blue: an array of 256 values
*
* This function gives full control over the colors displayed by the
* @bar widget. The 3 arrays can for example be taken from a #Levels
* or a #Curves struct.
**/
void
gimp_color_bar_set_buffers (GimpColorBar *bar,
const guchar *red,
const guchar *green,
const guchar *blue)
{
guchar *buf;
gint i;
g_return_if_fail (GIMP_IS_COLOR_BAR (bar));
g_return_if_fail (red != NULL);
g_return_if_fail (green != NULL);
g_return_if_fail (blue != NULL);
for (i = 0, buf = bar->buf; i < 256; i++, buf += 3)
{
buf[0] = red[i];
buf[1] = green[i];
buf[2] = blue[i];
}
gtk_widget_queue_draw (GTK_WIDGET (bar));
}