Commit b0b5e9f9 authored by Matthias Clasen's avatar Matthias Clasen
Browse files

Add color and font picker widgets.

parent 7f5968aa
2003-07-01 Matthias Clasen <maclas@gmx.de>
* gtk/gtk.h: Include gtkexpander.h
* gtk/Makefile.am (gtk_public_h_sources): Add gtkfontbutton.h, gtkcolorbutton.h.
(gtk_c_sources): Add gtkfontbutton.c, gtkcolorbutton.c.
* gtk/gtkfontbutton.[hc]: New files containing a font picker widget.
* gtk/gtkcolorbutton.[hc]: New files containing a color picker widget.
* gtk/gtk.h: Include gtkexpander.h, gtkfontbutton.h, gtkcolorbutton.h.
* gtk/gtkexpander.c: Small additions to the docs.
......
2003-07-01 Matthias Clasen <maclas@gmx.de>
* gtk/gtk.h: Include gtkexpander.h
* gtk/Makefile.am (gtk_public_h_sources): Add gtkfontbutton.h, gtkcolorbutton.h.
(gtk_c_sources): Add gtkfontbutton.c, gtkcolorbutton.c.
* gtk/gtkfontbutton.[hc]: New files containing a font picker widget.
* gtk/gtkcolorbutton.[hc]: New files containing a color picker widget.
* gtk/gtk.h: Include gtkexpander.h, gtkfontbutton.h, gtkcolorbutton.h.
* gtk/gtkexpander.c: Small additions to the docs.
......
2003-07-01 Matthias Clasen <maclas@gmx.de>
* gtk/gtk.h: Include gtkexpander.h
* gtk/Makefile.am (gtk_public_h_sources): Add gtkfontbutton.h, gtkcolorbutton.h.
(gtk_c_sources): Add gtkfontbutton.c, gtkcolorbutton.c.
* gtk/gtkfontbutton.[hc]: New files containing a font picker widget.
* gtk/gtkcolorbutton.[hc]: New files containing a color picker widget.
* gtk/gtk.h: Include gtkexpander.h, gtkfontbutton.h, gtkcolorbutton.h.
* gtk/gtkexpander.c: Small additions to the docs.
......
2003-07-01 Matthias Clasen <maclas@gmx.de>
* gtk/gtk.h: Include gtkexpander.h
* gtk/Makefile.am (gtk_public_h_sources): Add gtkfontbutton.h, gtkcolorbutton.h.
(gtk_c_sources): Add gtkfontbutton.c, gtkcolorbutton.c.
* gtk/gtkfontbutton.[hc]: New files containing a font picker widget.
* gtk/gtkcolorbutton.[hc]: New files containing a color picker widget.
* gtk/gtk.h: Include gtkexpander.h, gtkfontbutton.h, gtkcolorbutton.h.
* gtk/gtkexpander.c: Small additions to the docs.
......
2003-07-01 Matthias Clasen <maclas@gmx.de>
* gtk/gtk.h: Include gtkexpander.h
* gtk/Makefile.am (gtk_public_h_sources): Add gtkfontbutton.h, gtkcolorbutton.h.
(gtk_c_sources): Add gtkfontbutton.c, gtkcolorbutton.c.
* gtk/gtkfontbutton.[hc]: New files containing a font picker widget.
* gtk/gtkcolorbutton.[hc]: New files containing a color picker widget.
* gtk/gtk.h: Include gtkexpander.h, gtkfontbutton.h, gtkcolorbutton.h.
* gtk/gtkexpander.c: Small additions to the docs.
......
......@@ -109,6 +109,8 @@ gtk_public_h_sources = \
gtkcheckmenuitem.h \
gtkclipboard.h \
gtkclist.h \
gtkclipboard.h \
gtkcolorbutton.h \
gtkcolorsel.h \
gtkcolorseldialog.h \
gtkcombo.h \
......@@ -126,6 +128,7 @@ gtk_public_h_sources = \
gtkexpander.h \
gtkfilesel.h \
gtkfixed.h \
gtkfontbutton.h \
gtkfontsel.h \
gtkframe.h \
gtkgamma.h \
......@@ -282,6 +285,7 @@ gtk_c_sources = \
gtkseparatormenuitem.c \
gtkclipboard.c \
gtkclist.c \
gtkcolorbutton.c \
gtkcolorsel.c \
gtkcolorseldialog.c \
gtkcombo.c \
......@@ -297,6 +301,7 @@ gtk_c_sources = \
gtkexpander.c \
gtkfilesel.c \
gtkfixed.c \
gtkfontbutton.c \
gtkfontsel.c \
gtkframe.c \
gtkgamma.c \
......
/*
* GTK - The GIMP Toolkit
* Copyright (C) 1998, 1999 Red Hat, Inc.
* All rights reserved.
*
* This Library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with the Gnome Library; see the file COPYING.LIB. If not,
* write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/* Color picker button for GNOME
*
* Author: Federico Mena <federico@nuclecu.unam.mx>
*
* Modified by the GTK+ Team and others 2003. See the AUTHORS
* file for a list of people on the GTK+ Team. See the ChangeLog
* files for a list of changes. These files are distributed with
* GTK+ at ftp://ftp.gtk.org/pub/gtk/.
*/
#include <config.h>
#include "gtkcolorbutton.h"
#include "gdk/gdkkeysyms.h"
#include "gdk-pixbuf/gdk-pixbuf.h"
#include "gtkbutton.h"
#include "gtkmain.h"
#include "gtkalignment.h"
#include "gtkcolorsel.h"
#include "gtkcolorseldialog.h"
#include "gtkdnd.h"
#include "gtkdrawingarea.h"
#include "gtkframe.h"
#include "gtksignal.h"
#include "gtkmarshalers.h"
#include "gtkintl.h"
/* Size of checks and gray levels for alpha compositing checkerboard */
#define CHECK_SIZE 4
#define CHECK_DARK 21845 /* 65535 / 3 */
#define CHECK_LIGHT 43690
struct _GtkColorButtonPrivate
{
GdkPixbuf *pixbuf; /* Pixbuf for rendering sample */
GdkGC *gc; /* GC for drawing */
GtkWidget *drawing_area;/* Drawing area for color sample */
GtkWidget *cs_dialog; /* Color selection dialog */
gchar *title; /* Title for the color selection window */
GdkColor color;
guint16 alpha;
guint use_alpha : 1; /* Use alpha or not */
};
/* Properties */
enum
{
PROP_0,
PROP_USE_ALPHA,
PROP_TITLE,
PROP_COLOR,
PROP_ALPHA
};
/* Signals */
enum
{
COLOR_SET,
LAST_SIGNAL
};
static void gtk_color_button_class_init (GtkColorButtonClass *klass);
static void gtk_color_button_init (GtkColorButton *color_button);
/* gobject signals */
static void gtk_color_button_finalize (GObject *object);
static void gtk_color_button_set_property (GObject *object,
guint param_id,
const GValue *value,
GParamSpec *pspec);
static void gtk_color_button_get_property (GObject *object,
guint param_id,
GValue *value,
GParamSpec *pspec);
/* gtkwidget signals */
static void gtk_color_button_realize (GtkWidget *widget);
static void gtk_color_button_state_changed (GtkWidget *widget,
GtkStateType previous_state);
static void gtk_color_button_style_set (GtkWidget *widget,
GtkStyle *previous_style);
/* gtkbutton signals */
static void gtk_color_button_clicked (GtkButton *button);
/* source side drag signals */
static void gtk_color_button_drag_begin (GtkWidget *widget,
GdkDragContext *context,
gpointer data);
static void gtk_color_button_drag_data_get (GtkWidget *widget,
GdkDragContext *context,
GtkSelectionData *selection_data,
guint info,
guint time,
GtkColorButton *color_button);
/* target side drag signals */
static void gtk_color_button_drag_data_received (GtkWidget *widget,
GdkDragContext *context,
gint x,
gint y,
GtkSelectionData *selection_data,
guint info,
guint32 time,
GtkColorButton *color_button);
static gpointer parent_class = NULL;
static guint color_button_signals[LAST_SIGNAL] = { 0 };
static GtkTargetEntry drop_types[] = { { "application/x-color", 0, 0 } };
GType
gtk_color_button_get_type (void)
{
static GType color_button_type = 0;
if (!color_button_type)
{
static const GTypeInfo color_button_info =
{
sizeof (GtkColorButtonClass),
NULL, /* base_init */
NULL, /* base_finalize */
(GClassInitFunc) gtk_color_button_class_init,
NULL, /* class_finalize */
NULL, /* class_data */
sizeof (GtkColorButton),
0, /* n_preallocs */
(GInstanceInitFunc) gtk_color_button_init,
};
color_button_type =
g_type_register_static (GTK_TYPE_BUTTON, "GtkColorButton",
&color_button_info, 0);
}
return color_button_type;
}
static void
gtk_color_button_class_init (GtkColorButtonClass *klass)
{
GObjectClass *gobject_class;
GtkObjectClass *object_class;
GtkWidgetClass *widget_class;
GtkButtonClass *button_class;
gobject_class = G_OBJECT_CLASS (klass);
object_class = GTK_OBJECT_CLASS (klass);
widget_class = GTK_WIDGET_CLASS (klass);
button_class = GTK_BUTTON_CLASS (klass);
parent_class = g_type_class_peek_parent (klass);
gobject_class->get_property = gtk_color_button_get_property;
gobject_class->set_property = gtk_color_button_set_property;
gobject_class->finalize = gtk_color_button_finalize;
widget_class->state_changed = gtk_color_button_state_changed;
widget_class->realize = gtk_color_button_realize;
widget_class->style_set = gtk_color_button_style_set;
button_class->clicked = gtk_color_button_clicked;
klass->color_set = NULL;
/**
* GtkColorButton:use-alpha:
*
* If this property is set to %TRUE, the color swatch on the button is rendered against a
* checkerboard background to show its opacity and the opacity slider is displayed in the
* color selection dialog.
*
* Since: 2.4
*/
g_object_class_install_property (gobject_class,
PROP_USE_ALPHA,
g_param_spec_boolean ("use_alpha", _("Use alpha"),
_("Whether or not to give the color an alpha value"),
FALSE,
(G_PARAM_READABLE | G_PARAM_WRITABLE)));
/**
* GtkColorButton:title:
*
* The title of the color selection dialog
*
* Since: 2.4
*/
g_object_class_install_property (gobject_class,
PROP_TITLE,
g_param_spec_string ("title",
_("Title"),
_("The title of the color selection dialog"),
_("Pick a color"),
(G_PARAM_READABLE | G_PARAM_WRITABLE)));
/**
* GtkColorButton:color:
*
* The selected color.
*
* Since: 2.4
*/
g_object_class_install_property (gobject_class,
PROP_COLOR,
g_param_spec_boxed ("color",
_("Current Color"),
_("The selected color"),
GDK_TYPE_COLOR,
G_PARAM_READABLE | G_PARAM_WRITABLE));
/**
* GtkColorButton:alpha:
*
* The selected opacity value (0 fully transparent, 65535 fully opaque).
*
* Since: 2.4
*/
g_object_class_install_property (gobject_class,
PROP_ALPHA,
g_param_spec_uint ("alpha",
_("Current Alpha"),
_("The selected opacity value (0 fully transparent, 65535 fully opaque)"),
0, 65535, 65535,
G_PARAM_READABLE | G_PARAM_WRITABLE));
/**
* GtkColorButton::color-set:
* @widget: the object which received the signal.
*
* The ::color-set signal is emitted when the user selects a color. When handling this signal,
* use gtk_color_button_get_color() and gtk_color_button_get_alpha() to find out which color
* was just selected.
*
* Since: 2.4
*/
color_button_signals[COLOR_SET] = g_signal_new ("color_set",
G_TYPE_FROM_CLASS (gobject_class),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (GtkColorButtonClass, color_set),
NULL, NULL,
_gtk_marshal_VOID__VOID,
G_TYPE_NONE, 0);
g_type_class_add_private (gobject_class, sizeof (GtkColorButtonPrivate));
}
static void
render (GtkColorButton *color_button)
{
gint dark_r, dark_g, dark_b;
gint light_r, light_g, light_b;
gint i, j, rowstride;
gint width, height;
gint c1[3], c2[3];
guchar *pixels;
guint8 insensitive_r = 0;
guint8 insensitive_g = 0;
guint8 insensitive_b = 0;
width = color_button->priv->drawing_area->allocation.width;
height = color_button->priv->drawing_area->allocation.height;
if (color_button->priv->pixbuf == NULL ||
gdk_pixbuf_get_width (color_button->priv->pixbuf) != width ||
gdk_pixbuf_get_height (color_button->priv->pixbuf) != height)
{
if (color_button->priv->pixbuf != NULL)
g_object_unref (color_button->priv->pixbuf);
color_button->priv->pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, width, height);
}
/* Compute dark and light check colors */
insensitive_r = GTK_WIDGET(color_button)->style->bg[GTK_STATE_INSENSITIVE].red >> 8;
insensitive_g = GTK_WIDGET(color_button)->style->bg[GTK_STATE_INSENSITIVE].green >> 8;
insensitive_b = GTK_WIDGET(color_button)->style->bg[GTK_STATE_INSENSITIVE].blue >> 8;
if (color_button->priv->use_alpha)
{
dark_r = ((CHECK_DARK << 16) + (color_button->priv->color.red - CHECK_DARK) * color_button->priv->alpha) >> 24;
dark_g = ((CHECK_DARK << 16) + (color_button->priv->color.green - CHECK_DARK) * color_button->priv->alpha) >> 24;
dark_b = ((CHECK_DARK << 16) + (color_button->priv->color.blue - CHECK_DARK) * color_button->priv->alpha) >> 24;
light_r = ((CHECK_LIGHT << 16) + (color_button->priv->color.red - CHECK_LIGHT) * color_button->priv->alpha) >> 24;
light_g = ((CHECK_LIGHT << 16) + (color_button->priv->color.green - CHECK_LIGHT) * color_button->priv->alpha) >> 24;
light_b = ((CHECK_LIGHT << 16) + (color_button->priv->color.blue - CHECK_LIGHT) * color_button->priv->alpha) >> 24;
}
else
{
dark_r = light_r = color_button->priv->color.red >> 8;
dark_g = light_g = color_button->priv->color.green >> 8;
dark_b = light_b = color_button->priv->color.blue >> 8;
}
/* Fill image buffer */
pixels = gdk_pixbuf_get_pixels (color_button->priv->pixbuf);
rowstride = gdk_pixbuf_get_rowstride (color_button->priv->pixbuf);
for (j = 0; j < height; j++)
{
if ((j / CHECK_SIZE) & 1)
{
c1[0] = dark_r;
c1[1] = dark_g;
c1[2] = dark_b;
c2[0] = light_r;
c2[1] = light_g;
c2[2] = light_b;
}
else
{
c1[0] = light_r;
c1[1] = light_g;
c1[2] = light_b;
c2[0] = dark_r;
c2[1] = dark_g;
c2[2] = dark_b;
}
for (i = 0; i < width; i++)
{
if (!GTK_WIDGET_SENSITIVE (GTK_WIDGET (color_button)) && (i+j)%2)
{
*(pixels + j * rowstride + i * 3) = insensitive_r;
*(pixels + j * rowstride + i * 3 + 1) = insensitive_g;
*(pixels + j * rowstride + i * 3 + 2) = insensitive_b;
}
else if ((i / CHECK_SIZE) & 1)
{
*(pixels + j * rowstride + i * 3) = c1[0];
*(pixels + j * rowstride + i * 3 + 1) = c1[1];
*(pixels + j * rowstride + i * 3 + 2) = c1[2];
}
else
{
*(pixels + j * rowstride + i * 3) = c2[0];
*(pixels + j * rowstride + i * 3 + 1) = c2[1];
*(pixels + j * rowstride + i * 3 + 2) = c2[2];
}
}
}
}
/* Handle exposure events for the color picker's drawing area */
static gint
expose_event (GtkWidget *widget,
GdkEventExpose *event,
gpointer data)
{
GtkColorButton *color_button = GTK_COLOR_BUTTON (data);
gint width = color_button->priv->drawing_area->allocation.width;
gint height = color_button->priv->drawing_area->allocation.height;
if (color_button->priv->pixbuf == NULL ||
width != gdk_pixbuf_get_width (color_button->priv->pixbuf) ||
height != gdk_pixbuf_get_height (color_button->priv->pixbuf))
render (color_button);
gdk_draw_pixbuf (widget->window,
color_button->priv->gc,
color_button->priv->pixbuf,
event->area.x,
event->area.y,
event->area.x,
event->area.y,
event->area.width,
event->area.height,
GDK_RGB_DITHER_MAX,
event->area.x,
event->area.y);
return FALSE;
}
static void
gtk_color_button_realize (GtkWidget *widget)
{
GtkColorButton *color_button = GTK_COLOR_BUTTON (widget);
GTK_WIDGET_CLASS (parent_class)->realize (widget);
if (color_button->priv->gc == NULL)
color_button->priv->gc = gdk_gc_new (widget->window);
render (color_button);
}
static void
gtk_color_button_style_set (GtkWidget *widget,
GtkStyle *previous_style)
{
GtkColorButton *color_button = GTK_COLOR_BUTTON (widget);
GTK_WIDGET_CLASS (parent_class)->style_set (widget, previous_style);
if (GTK_WIDGET_REALIZED (widget))
{
if (color_button->priv->pixbuf != NULL)
g_object_unref (color_button->priv->pixbuf);
color_button->priv->pixbuf = NULL;
}
}
static void
gtk_color_button_state_changed (GtkWidget *widget,
GtkStateType previous_state)
{
GtkColorButton *color_button = GTK_COLOR_BUTTON (widget);
if (widget->state == GTK_STATE_INSENSITIVE || previous_state == GTK_STATE_INSENSITIVE)
{
if (color_button->priv->pixbuf != NULL)
g_object_unref (color_button->priv->pixbuf);
color_button->priv->pixbuf = NULL;
}
}
static void
gtk_color_button_drag_data_received (GtkWidget *widget,
GdkDragContext *context,
gint x,
gint y,
GtkSelectionData *selection_data,
guint info,
guint32 time,
GtkColorButton *color_button)
{
guint16 *dropped;
if (selection_data->length < 0)
return;
if ((selection_data->format != 16) ||
(selection_data->length != 8))
{
g_warning (_("Received invalid color data\n"));
return;
}
dropped = (guint16 *)selection_data->data;
color_button->priv->color.red = dropped[0];
color_button->priv->color.green = dropped[1];
color_button->priv->color.blue = dropped[2];
color_button->priv->alpha = dropped[3];
if (color_button->priv->pixbuf != NULL)
g_object_unref (color_button->priv->pixbuf);
color_button->priv->pixbuf = NULL;
gtk_widget_queue_draw (color_button->priv->drawing_area);
g_signal_emit (color_button, color_button_signals[COLOR_SET], 0);
g_object_freeze_notify (G_OBJECT (color_button));
g_object_notify (G_OBJECT (color_button), "color");
g_object_notify (G_OBJECT (color_button), "alpha");
g_object_thaw_notify (G_OBJECT (color_button));
}
static void
set_color_icon (GdkDragContext *context,
GdkColor *color)
{
GdkPixbuf *pixbuf;
guint32 pixel;
pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE,
8, 48, 32);
pixel = ((color->red & 0xff00) << 16) |
((color->green & 0xff00) << 8) |
(color->blue & 0xff00);
gdk_pixbuf_fill (pixbuf, pixel);
gtk_drag_set_icon_pixbuf (context, pixbuf, -2, -2);
g_object_unref (pixbuf);
}
static void
gtk_color_button_drag_begin (GtkWidget *widget,
GdkDragContext *context,
gpointer data)
{
GtkColorButton *color_button = data;
set_color_icon (context, &color_button->priv->color);
}
static void
gtk_color_button_drag_data_get (GtkWidget *widget,