Add color and font picker widgets.

This commit is contained in:
Matthias Clasen 2003-07-01 19:52:07 +00:00
parent 7f5968aa07
commit b0b5e9f90f
10 changed files with 2182 additions and 5 deletions

View File

@ -1,6 +1,13 @@
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.

View File

@ -1,6 +1,13 @@
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.

View File

@ -1,6 +1,13 @@
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.

View File

@ -1,6 +1,13 @@
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.

View File

@ -1,6 +1,13 @@
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.

View File

@ -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 \

994
gtk/gtkcolorbutton.c Normal file
View File

@ -0,0 +1,994 @@
/*
* 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,
GdkDragContext *context,
GtkSelectionData *selection_data,
guint info,
guint time,
GtkColorButton *color_button)
{
guint16 dropped[4];
dropped[0] = color_button->priv->color.red;
dropped[1] = color_button->priv->color.green;
dropped[2] = color_button->priv->color.blue;
dropped[3] = color_button->priv->alpha;
gtk_selection_data_set (selection_data, selection_data->target,
16, (guchar *)dropped, 8);
}
static void
gtk_color_button_init (GtkColorButton *color_button)
{
GtkWidget *alignment;
GtkWidget *frame;
PangoLayout *layout;
PangoRectangle rect;
/* Create the widgets */
color_button->priv = GTK_COLOR_BUTTON_GET_PRIVATE (color_button);
gtk_widget_push_composite_child ();
alignment = gtk_alignment_new (0.5, 0.5, 0.5, 1.0);
gtk_container_set_border_width (GTK_CONTAINER (alignment), 1);
gtk_container_add (GTK_CONTAINER (color_button), alignment);
gtk_widget_show (alignment);
frame = gtk_frame_new (NULL);
gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_ETCHED_OUT);
gtk_container_add (GTK_CONTAINER (alignment), frame);
gtk_widget_show (frame);
color_button->priv->drawing_area = gtk_drawing_area_new ();
layout = gtk_widget_create_pango_layout (GTK_WIDGET (color_button), "Black");
pango_layout_get_pixel_extents (layout, NULL, &rect);
gtk_widget_set_size_request (color_button->priv->drawing_area, rect.width - 2, rect.height - 2);
g_signal_connect (color_button->priv->drawing_area, "expose_event",
G_CALLBACK (expose_event), color_button);
gtk_container_add (GTK_CONTAINER (frame), color_button->priv->drawing_area);
gtk_widget_show (color_button->priv->drawing_area);
color_button->priv->title = g_strdup (_("Pick a color")); /* default title */
/* Create the buffer for the image so that we can create an image.
* Also create the picker's pixmap.
*/
color_button->priv->pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, rect.width, rect.height);
color_button->priv->gc = NULL;
/* Start with opaque black, alpha disabled */
color_button->priv->color.red = 0;
color_button->priv->color.green = 0;
color_button->priv->color.blue = 0;
color_button->priv->alpha = 65535;
color_button->priv->use_alpha = FALSE;
gtk_drag_dest_set (GTK_WIDGET (color_button),
GTK_DEST_DEFAULT_MOTION |
GTK_DEST_DEFAULT_HIGHLIGHT |
GTK_DEST_DEFAULT_DROP,
drop_types, 1, GDK_ACTION_COPY);
gtk_drag_source_set (GTK_WIDGET(color_button),
GDK_BUTTON1_MASK|GDK_BUTTON3_MASK,
drop_types, 1,
GDK_ACTION_COPY);
g_signal_connect (color_button, "drag_begin",
G_CALLBACK (gtk_color_button_drag_begin), color_button);
g_signal_connect (color_button, "drag_data_received",
G_CALLBACK (gtk_color_button_drag_data_received), color_button);
g_signal_connect (color_button, "drag_data_get",
G_CALLBACK (gtk_color_button_drag_data_get), color_button);
gtk_widget_pop_composite_child ();
}
static void
gtk_color_button_finalize (GObject *object)
{
GtkColorButton *color_button = GTK_COLOR_BUTTON (object);
if (color_button->priv->gc != NULL)
g_object_unref (G_OBJECT (color_button->priv->gc));
color_button->priv->gc = NULL;
if (color_button->priv->cs_dialog != NULL)
gtk_widget_destroy (color_button->priv->cs_dialog);
color_button->priv->cs_dialog = NULL;
if (color_button->priv->pixbuf != NULL)
g_object_unref (color_button->priv->pixbuf);
color_button->priv->pixbuf = NULL;
g_free (color_button->priv->title);
color_button->priv->title = NULL;
G_OBJECT_CLASS (parent_class)->finalize (object);
}
/**
* gtk_color_button_new:
*
* Creates a new color button. This returns a widget in the form of
* a small button containing a swatch representing the current selected
* color. When the button is clicked, a color-selection dialog will open,
* allowing the user to select a color. The swatch will be updated to reflect
* the new color when the user finishes.
*
* Returns: a new color button.
*
* Since: 2.4
*/
GtkWidget *
gtk_color_button_new (void)
{
return g_object_new (GTK_TYPE_COLOR_BUTTON, NULL);
}
/**
* gtk_color_button_new_with_color:
* @color: A #GdkColor to set the current color with.
*
* Creates a new color button.
*
* Returns: a new color button.
*
* Since: 2.4
*/
GtkWidget *
gtk_color_button_new_with_color (GdkColor *color)
{
return g_object_new (GTK_TYPE_COLOR_BUTTON, "color", color, NULL);
}
static void
dialog_ok_clicked (GtkWidget *widget,
gpointer data)
{
GtkColorButton *color_button = GTK_COLOR_BUTTON (data);
GtkColorSelection *color_selection;
color_selection = GTK_COLOR_SELECTION (GTK_COLOR_SELECTION_DIALOG (color_button->priv->cs_dialog)->colorsel);
gtk_color_selection_get_current_color (color_selection, &color_button->priv->color);
color_button->priv->alpha = gtk_color_selection_get_current_alpha (color_selection);
if (color_button->priv->pixbuf != NULL)
g_object_unref (color_button->priv->pixbuf);
color_button->priv->pixbuf = NULL;
gtk_widget_hide (color_button->priv->cs_dialog);
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 gboolean
dialog_destroy (GtkWidget *widget,
gpointer data)
{
GtkColorButton *color_button = GTK_COLOR_BUTTON (data);
color_button->priv->cs_dialog = NULL;
return FALSE;
}
static void
dialog_cancel_clicked (GtkWidget *widget,
gpointer data)
{
GtkColorButton *color_button = GTK_COLOR_BUTTON (data);
gtk_widget_hide (color_button->priv->cs_dialog);
}
static void
gtk_color_button_clicked (GtkButton *button)
{
GtkColorButton *color_button = GTK_COLOR_BUTTON (button);
GtkColorSelectionDialog *color_dialog;
/* if dialog already exists, make sure it's shown and raised */
if (!color_button->priv->cs_dialog)
{
/* Create the dialog and connects its buttons */
GtkWidget *parent;
parent = gtk_widget_get_toplevel (GTK_WIDGET (color_button));
color_button->priv->cs_dialog = gtk_color_selection_dialog_new (color_button->priv->title);
color_dialog = GTK_COLOR_SELECTION_DIALOG (color_button->priv->cs_dialog);
if (parent)
gtk_window_set_transient_for (GTK_WINDOW (color_dialog),
GTK_WINDOW (parent));
g_signal_connect (color_dialog->ok_button, "clicked",
G_CALLBACK (dialog_ok_clicked), color_button);
g_signal_connect (color_dialog->cancel_button, "clicked",
G_CALLBACK (dialog_cancel_clicked), color_button);
g_signal_connect (color_dialog, "destroy",
G_CALLBACK (dialog_destroy), color_button);
/* If there is a grabbed window, set new dialog as modal */
if (gtk_grab_get_current ())
gtk_window_set_modal (GTK_WINDOW (color_button->priv->cs_dialog),TRUE);
}
color_dialog = GTK_COLOR_SELECTION_DIALOG (color_button->priv->cs_dialog);
gtk_color_selection_set_has_opacity_control (GTK_COLOR_SELECTION (color_dialog->colorsel),
color_button->priv->use_alpha);
gtk_color_selection_set_previous_color (GTK_COLOR_SELECTION (color_dialog->colorsel),
&color_button->priv->color);
gtk_color_selection_set_previous_alpha (GTK_COLOR_SELECTION (color_dialog->colorsel),
color_button->priv->alpha);
gtk_color_selection_set_current_color (GTK_COLOR_SELECTION (color_dialog->colorsel),
&color_button->priv->color);
gtk_color_selection_set_current_alpha (GTK_COLOR_SELECTION (color_dialog->colorsel),
color_button->priv->alpha);
gtk_window_present (GTK_WINDOW (color_button->priv->cs_dialog));
}
/**
* gtk_color_button_set_color:
* @color_button: a #GtkColorButton.
* @color: A #GdkColor to set the current color with.
*
* Sets the current color to be @color.
*
* Since: 2.4
**/
void
gtk_color_button_set_color (GtkColorButton *color_button,
GdkColor *color)
{
g_return_if_fail (GTK_IS_COLOR_BUTTON (color_button));
color_button->priv->color.red = color->red;
color_button->priv->color.green = color->green;
color_button->priv->color.blue = color->blue;
g_object_notify (G_OBJECT (color_button), "color");
}
/**
* gtk_color_button_set_alpha:
* @color_button: a #GtkColorButton.
* @alpha: an integer between 0 and 65535.
*
* Sets the current opacity to be @alpha.
*
* Since: 2.4
**/
void
gtk_color_button_set_alpha (GtkColorButton *color_button,
guint16 alpha)
{
g_return_if_fail (GTK_IS_COLOR_BUTTON (color_button));
color_button->priv->alpha = alpha;
g_object_notify (G_OBJECT (color_button), "alpha");
}
/**
* gtk_color_button_get_color:
* @color_button: a #GtkColorButton.
* @color: a #GdkColor to fill in with the current color.
*
* Sets @color to be the current color in the #GtkColorButton widget.
*
* Since: 2.4
**/
void
gtk_color_button_get_color (GtkColorButton *color_button,
GdkColor *color)
{
g_return_if_fail (GTK_IS_COLOR_BUTTON (color_button));
color->red = color_button->priv->color.red;
color->green = color_button->priv->color.green;
color->blue = color_button->priv->color.blue;
}
/**
* gtk_color_button_get_alpha:
* @color_button: a #GtkColorButton.
*
* Returns the current alpha value.
*
* Return value: an integer between 0 and 65535.
*
* Since: 2.4
**/
guint16
gtk_color_button_get_alpha (GtkColorButton *color_button)
{
g_return_val_if_fail (GTK_IS_COLOR_BUTTON (color_button), 0);
return color_button->priv->alpha;
}
/**
* gtk_color_button_set_use_alpha:
* @color_button: a #GtkColorButton.
* @use_alpha: %TRUE if color button should use alpha channel, %FALSE if not.
*
* Sets whether or not the color button should use the alpha channel.
*
* Since: 2.4
*/
void
gtk_color_button_set_use_alpha (GtkColorButton *color_button,
gboolean use_alpha)
{
g_return_if_fail (GTK_IS_COLOR_BUTTON (color_button));
use_alpha = (use_alpha != FALSE);
if (color_button->priv->use_alpha != use_alpha)
{
color_button->priv->use_alpha = use_alpha;
render (color_button);
gtk_widget_queue_draw (color_button->priv->drawing_area);
g_object_notify (G_OBJECT (color_button), "use_alpha");
}
}
/**
* gtk_color_button_get_use_alpha:
* @color_button: a #GtkColorButton.
*
* Does the color selection dialog use the alpha channel?
*
* Returns: %TRUE if the color sample uses alpha channel, %FALSE if not.
*
* Since: 2.4
*/
gboolean
gtk_color_button_get_use_alpha (GtkColorButton *color_button)
{
g_return_val_if_fail (GTK_IS_COLOR_BUTTON (color_button), FALSE);
return color_button->priv->use_alpha;
}
/**
* gtk_color_button_set_title:
* @color_button: a #GtkColorButton
* @title: String containing new window title.
*
* Sets the title for the color selection dialog.
*
* Since: 2.4
*/
void
gtk_color_button_set_title (GtkColorButton *color_button,
const gchar *title)
{
gchar *old_title;
g_return_if_fail (GTK_IS_COLOR_BUTTON (color_button));
old_title = color_button->priv->title;
color_button->priv->title = g_strdup (title);
g_free (old_title);
if (color_button->priv->cs_dialog)
gtk_window_set_title (GTK_WINDOW (color_button->priv->cs_dialog),
color_button->priv->title);
g_object_notify (G_OBJECT (color_button), "title");
}
/**
* gtk_color_button_get_title:
* @color_button: a #GtkColorButton
*
* Gets the title of the color selection dialog.
*
* Returns: An internal string, do not free the return value
*
* Since: 2.4
*/
G_CONST_RETURN gchar *
gtk_color_button_get_title (GtkColorButton *color_button)
{
g_return_val_if_fail (GTK_IS_COLOR_BUTTON (color_button), NULL);
return color_button->priv->title;
}
static void
gtk_color_button_set_property (GObject *object,
guint param_id,
const GValue *value,
GParamSpec *pspec)
{
GtkColorButton *color_button = GTK_COLOR_BUTTON (object);
switch (param_id)
{
case PROP_USE_ALPHA:
gtk_color_button_set_use_alpha (color_button, g_value_get_boolean (value));
break;
case PROP_TITLE:
gtk_color_button_set_title (color_button, g_value_get_string (value));
break;
case PROP_COLOR:
gtk_color_button_set_color (color_button, g_value_get_boxed (value));
break;
case PROP_ALPHA:
gtk_color_button_set_alpha (color_button, g_value_get_uint (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
break;
}
}
static void
gtk_color_button_get_property (GObject *object,
guint param_id,
GValue *value,
GParamSpec *pspec)
{
GtkColorButton *color_button = GTK_COLOR_BUTTON (object);
GdkColor color;
switch (param_id)
{
case PROP_USE_ALPHA:
g_value_set_boolean (value, gtk_color_button_get_use_alpha (color_button));
break;
case PROP_TITLE:
g_value_set_string (value, gtk_color_button_get_title (color_button));
break;
case PROP_COLOR:
gtk_color_button_get_color (color_button, &color);
g_value_set_boxed (value, &color);
break;
case PROP_ALPHA:
g_value_set_uint (value, gtk_color_button_get_alpha (color_button));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
break;
}
}

103
gtk/gtkcolorbutton.h Normal file
View File

@ -0,0 +1,103 @@
/*
* 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/.
*/
#ifndef __GTK_COLOR_BUTTON_H__
#define __GTK_COLOR_BUTTON_H__
#include <gtk/gtkbutton.h>
G_BEGIN_DECLS
/* The GtkColorSelectionButton widget is a simple color picker in a button.
* The button displays a sample of the currently selected color. When
* the user clicks on the button, a color selection dialog pops up.
* The color picker emits the "color_set" signal when the color is set.
*/
#define GTK_TYPE_COLOR_BUTTON (gtk_color_button_get_type ())
#define GTK_COLOR_BUTTON(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_COLOR_BUTTON, GtkColorButton))
#define GTK_COLOR_BUTTON_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_COLOR_BUTTON, GtkColorButtonClass))
#define GTK_IS_COLOR_BUTTON(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_COLOR_BUTTON))
#define GTK_IS_COLOR_BUTTON_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_COLOR_BUTTON))
#define GTK_COLOR_BUTTON_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_COLOR_BUTTON, GtkColorButtonClass))
#define GTK_COLOR_BUTTON_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_COLOR_BUTTON, GtkColorButtonPrivate))
typedef struct _GtkColorButton GtkColorButton;
typedef struct _GtkColorButtonClass GtkColorButtonClass;
typedef struct _GtkColorButtonPrivate GtkColorButtonPrivate;
struct _GtkColorButton {
GtkButton button;
/*< private >*/
GtkColorButtonPrivate *priv;
};
struct _GtkColorButtonClass {
GtkButtonClass parent_class;
void (* color_set) (GtkColorButton *cp);
/* Padding for future expansion */
void (*_gtk_reserved1) (void);
void (*_gtk_reserved2) (void);
void (*_gtk_reserved3) (void);
void (*_gtk_reserved4) (void);
};
GType gtk_color_button_get_type (void) G_GNUC_CONST;
GtkWidget *gtk_color_button_new (void);
GtkWidget *gtk_color_button_new_with_color (GdkColor *color);
void gtk_color_button_set_color (GtkColorButton *color_button,
GdkColor *color);
void gtk_color_button_set_alpha (GtkColorButton *color_button,
guint16 alpha);
void gtk_color_button_get_color (GtkColorButton *color_button,
GdkColor *color);
guint16 gtk_color_button_get_alpha (GtkColorButton *color_button);
void gtk_color_button_set_use_alpha (GtkColorButton *color_button,
gboolean use_alpha);
gboolean gtk_color_button_get_use_alpha (GtkColorButton *color_button);
void gtk_color_button_set_title (GtkColorButton *color_button,
const gchar *title);
G_CONST_RETURN gchar *gtk_color_button_get_title (GtkColorButton *color_button);
G_END_DECLS
#endif /* __GTK_COLOR_BUTTON_H__ */

939
gtk/gtkfontbutton.c Normal file
View File

@ -0,0 +1,939 @@
/*
* GTK - The GIMP Toolkit
* Copyright (C) 1998 David Abilleira Freijeiro <odaf@nexo.es>
* All rights reserved.
*
* Based on gnome-color-picker by Federico Mena <federico@nuclecu.unam.mx>
*
* 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 this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Cambridge, MA 02139, USA.
*/
/*
* 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 "gtkfontbutton.h"
#include "gtksignal.h"
#include "gtkmain.h"
#include "gtkalignment.h"
#include "gtkhbox.h"
#include "gtklabel.h"
#include "gtkvseparator.h"
#include "gtkfontsel.h"
#include "gtkimage.h"
#include "gtkmarshalers.h"
#include "gtkintl.h"
#include <string.h>
#include <stdio.h>
struct _GtkFontButtonPrivate
{
gchar *title;
gchar *fontname;
guint use_font : 1;
guint use_size : 1;
guint show_style : 1;
guint show_size : 1;
GtkWidget *font_dialog;
GtkWidget *inside;
GtkWidget *font_label;
GtkWidget *size_label;
};
/* Signals */
enum
{
FONT_SET,
LAST_SIGNAL
};
enum
{
PROP_0,
PROP_TITLE,
PROP_FONT_NAME,
PROP_USE_FONT,
PROP_USE_SIZE,
PROP_SHOW_STYLE,
PROP_SHOW_SIZE
};
/* Prototypes */
static void gtk_font_button_init (GtkFontButton *font_button);
static void gtk_font_button_class_init (GtkFontButtonClass *klass);
static void gtk_font_button_finalize (GObject *object);
static void gtk_font_button_get_property (GObject *object,
guint param_id,
GValue *value,
GParamSpec *pspec);
static void gtk_font_button_set_property (GObject *object,
guint param_id,
const GValue *value,
GParamSpec *pspec);
static void gtk_font_button_clicked (GtkButton *button);
/* Dialog response functions */
static void dialog_ok_clicked (GtkWidget *widget,
gpointer data);
static void dialog_cancel_clicked (GtkWidget *widget,
gpointer data);
static void dialog_destroy (GtkWidget *widget,
gpointer data);
/* Auxiliary functions */
static GtkWidget *gtk_font_button_create_inside (GtkFontButton *gfs);
static void gtk_font_button_label_use_font (GtkFontButton *gfs);
static void gtk_font_button_update_font_info (GtkFontButton *gfs);
static gpointer parent_class = NULL;
static guint font_button_signals[LAST_SIGNAL] = { 0 };
GType
gtk_font_button_get_type (void)
{
static GType font_button_type = 0;
if (!font_button_type)
{
static const GTypeInfo font_button_info =
{
sizeof (GtkFontButtonClass),
NULL, /* base_init */
NULL, /* base_finalize */
(GClassInitFunc) gtk_font_button_class_init,
NULL, /* class_finalize */
NULL, /* class_data */
sizeof (GtkFontButton),
0, /* n_preallocs */
(GInstanceInitFunc) gtk_font_button_init,
};
font_button_type =
g_type_register_static (GTK_TYPE_BUTTON, "GtkFontButton",
&font_button_info, 0);
}
return font_button_type;
}
static void
gtk_font_button_class_init (GtkFontButtonClass *klass)
{
GtkObjectClass *object_class;
GObjectClass *gobject_class;
GtkButtonClass *button_class;
object_class = (GtkObjectClass *) klass;
gobject_class = (GObjectClass *) klass;
button_class = (GtkButtonClass *) klass;
parent_class = g_type_class_peek_parent (klass);
gobject_class->finalize = gtk_font_button_finalize;
gobject_class->set_property = gtk_font_button_set_property;
gobject_class->get_property = gtk_font_button_get_property;
button_class->clicked = gtk_font_button_clicked;
klass->font_set = NULL;
/**
* GtkFontButton:title:
*
* The title of the font selection dialog.
*
* Since: 2.4
*/
g_object_class_install_property (gobject_class,
PROP_TITLE,
g_param_spec_string ("title",
_("Title"),
_("The title of the font selection dialog"),
_("Pick a font"),
(G_PARAM_READABLE |
G_PARAM_WRITABLE)));
/**
* GtkFontButton:font-name:
*
* The name of the currently selected font.
*
* Since: 2.4
*/
g_object_class_install_property (gobject_class,
PROP_FONT_NAME,
g_param_spec_string ("font_name",
_("Font name"),
_("The name of the selected font"),
_("Sans 12"),
(G_PARAM_READABLE |
G_PARAM_WRITABLE)));
/**
* GtkFontButton:use-font:
*
* If this property is set to %TRUE, the label will be drawn in the selected font.
*
* Since: 2.4
*/
g_object_class_install_property (gobject_class,
PROP_USE_FONT,
g_param_spec_boolean ("use_font",
_("Use font in label"),
_("Whether the label is drawn in the selected font"),
FALSE,
(G_PARAM_READABLE |
G_PARAM_WRITABLE)));
/**
* GtkFontButton:use-size:
*
* If this property is set to %TRUE, the label will be drawn with the selected font size.
*
* Since: 2.4
*/
g_object_class_install_property (gobject_class,
PROP_USE_SIZE,
g_param_spec_boolean ("use_size",
_("Use size in label"),
_("Whether the label is drawn with the selected font size"),
FALSE,
(G_PARAM_READABLE |
G_PARAM_WRITABLE)));
/**
* GtkFontButton:show-style:
*
* If this property is set to %TRUE, the name of the selected font style will be shown in the label. For
* a more WYSIWIG way to show the selected style, see the ::use-font property.
*
* Since: 2.4
*/
g_object_class_install_property (gobject_class,
PROP_SHOW_SIZE,
g_param_spec_boolean ("show_style",
_("Show style"),
_("Whether the selected font style is shown in the label"),
TRUE,
(G_PARAM_READABLE |
G_PARAM_WRITABLE)));
/**
* GtkFontButton:show-size:
*
* If this property is set to %TRUE, the selected font size will be shown in the label. For
* a more WYSIWIG way to show the selected size, see the ::use-size property.
*
* Since: 2.4
*/
g_object_class_install_property (gobject_class,
PROP_SHOW_SIZE,
g_param_spec_boolean ("show_size",
_("Show size"),
_("Whether selected font size is shown in the label"),
TRUE,
(G_PARAM_READABLE |
G_PARAM_WRITABLE)));
/**
* GtkFontButton::font-set:
* @widget: the object which received the signal.
*
* The ::font-set signal is emitted when the user selects a font. When handling this signal,
* use gtk_font_button_get_font_name() to find out which font was just selected.
*
* Since: 2.4
*/
font_button_signals[FONT_SET] = g_signal_new ("font_set",
G_TYPE_FROM_CLASS (gobject_class),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (GtkFontButtonClass, font_set),
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
g_type_class_add_private (gobject_class, sizeof (GtkFontButtonPrivate));
}
static void
gtk_font_button_init (GtkFontButton *font_button)
{
font_button->priv = GTK_FONT_BUTTON_GET_PRIVATE (font_button);
/* Initialize fields */
font_button->priv->fontname = g_strdup (_("Sans 12"));
font_button->priv->use_font = FALSE;
font_button->priv->use_size = FALSE;
font_button->priv->show_style = TRUE;
font_button->priv->show_size = TRUE;
font_button->priv->font_dialog = NULL;
font_button->priv->title = g_strdup (_("Pick a Font"));
font_button->priv->inside = gtk_font_button_create_inside (font_button);
gtk_container_add (GTK_CONTAINER (font_button), font_button->priv->inside);
gtk_font_button_update_font_info (font_button);
}
static void
gtk_font_button_finalize (GObject *object)
{
GtkFontButton *font_button = GTK_FONT_BUTTON (object);
if (font_button->priv->font_dialog != NULL)
gtk_widget_destroy (font_button->priv->font_dialog);
font_button->priv->font_dialog = NULL;
g_free (font_button->priv->fontname);
font_button->priv->fontname = NULL;
g_free (font_button->priv->title);
font_button->priv->title = NULL;
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static void
gtk_font_button_set_property (GObject *object,
guint param_id,
const GValue *value,
GParamSpec *pspec)
{
GtkFontButton *font_button = GTK_FONT_BUTTON (object);
switch (param_id)
{
case PROP_TITLE:
gtk_font_button_set_title (font_button, g_value_get_string (value));
break;
case PROP_FONT_NAME:
gtk_font_button_set_font_name (font_button, g_value_get_string (value));
break;
case PROP_USE_FONT:
gtk_font_button_set_use_font (font_button, g_value_get_boolean (value));
break;
case PROP_USE_SIZE:
gtk_font_button_set_use_size (font_button, g_value_get_boolean (value));
break;
case PROP_SHOW_STYLE:
gtk_font_button_set_show_style (font_button, g_value_get_boolean (value));
break;
case PROP_SHOW_SIZE:
gtk_font_button_set_show_size (font_button, g_value_get_boolean (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
break;
}
}
static void
gtk_font_button_get_property (GObject *object,
guint param_id,
GValue *value,
GParamSpec *pspec)
{
GtkFontButton *font_button = GTK_FONT_BUTTON (object);
switch (param_id)
{
case PROP_TITLE:
g_value_set_string (value, gtk_font_button_get_title (font_button));
break;
case PROP_FONT_NAME:
g_value_set_string (value, gtk_font_button_get_font_name (font_button));
break;
case PROP_USE_FONT:
g_value_set_boolean (value, gtk_font_button_get_use_font (font_button));
break;
case PROP_USE_SIZE:
g_value_set_boolean (value, gtk_font_button_get_use_size (font_button));
break;
case PROP_SHOW_STYLE:
g_value_set_boolean (value, gtk_font_button_get_show_style (font_button));
break;
case PROP_SHOW_SIZE:
g_value_set_boolean (value, gtk_font_button_get_show_size (font_button));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
break;
}
}
/**
* gtk_font_button_new:
*
* Creates a new font picker widget.
*
* Returns: a new font picker widget.
*
* Since: 2.4
*/
GtkWidget *
gtk_font_button_new (void)
{
return g_object_new (GTK_TYPE_FONT_BUTTON, NULL);
}
/**
* gtk_font_button_new_with_font:
* @fontname: Name of font to display in font selection dialog
*
* Creates a new font picker widget.
*
* Returns: a new font picker widget.
*
* Since: 2.4
*/
GtkWidget *
gtk_font_button_new_with_font (const gchar *fontname)
{
return g_object_new (GTK_TYPE_FONT_BUTTON, "font_name", fontname, NULL);
}
/**
* gtk_font_button_set_title:
* @font_button: a #GtkFontButton
* @title: a string containing the font selection dialog title
*
* Sets the title for the font selection dialog.
*
* Since: 2.4
*/
void
gtk_font_button_set_title (GtkFontButton *font_button,
const gchar *title)
{
gchar *old_title;
g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
old_title = font_button->priv->title;
font_button->priv->title = g_strdup (title);
g_free (old_title);
if (font_button->priv->font_dialog)
gtk_window_set_title (GTK_WINDOW (font_button->priv->font_dialog),
font_button->priv->title);
g_object_notify (G_OBJECT (font_button), "title");
}
/**
* gtk_font_button_get_title:
* @font_button: a #GtkFontButton
*
* Retrieves the title of the font selection dialog.
*
* Returns: an internal copy of the title string which must not be freed.
*
* Since: 2.4
*/
G_CONST_RETURN gchar*
gtk_font_button_get_title (GtkFontButton *font_button)
{
g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), NULL);
return font_button->priv->title;
}
/**
* gtk_font_button_get_use_font:
* @font_button: a #GtkFontButton
*
* Returns whether the selected font is used in the label.
*
* Returns: whether the selected font is used in the label.
*
* Since: 2.4
*/
gboolean
gtk_font_button_get_use_font (GtkFontButton *font_button)
{
g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
return font_button->priv->use_font;
}
/**
* gtk_font_button_set_use_font:
* @font_button: a #GtkFontButton
* @use_font: If %TRUE, font name will be written using font chosen.
*
* If @use_font is %TRUE, the font name will be written using the selected font.
*
* Since: 2.4
*/
void
gtk_font_button_set_use_font (GtkFontButton *font_button,
gboolean use_font)
{
g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
use_font = (use_font != FALSE);
if (font_button->priv->use_font != use_font)
{
font_button->priv->use_font = use_font;
if (use_font)
gtk_font_button_label_use_font (font_button);
else
gtk_widget_set_style (font_button->priv->font_label, NULL);
g_object_notify (G_OBJECT (font_button), "use_font");
}
}
/**
* gtk_font_button_get_use_size:
* @font_button: a #GtkFontButton
*
* Returns whether the selected size is used in the label.
*
* Returns: whether the selected size is used in the label.
*
* Since: 2.4
*/
gboolean
gtk_font_button_get_use_size (GtkFontButton *font_button)
{
g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
return font_button->priv->use_size;
}
/**
* gtk_font_button_set_use_size:
* @font_button: a #GtkFontButton
* @use_size: If %TRUE, font name will be written using the selected size.
*
* If @use_size is %TRUE, the font name will be written using the selected size.
*
* Since: 2.4
*/
void
gtk_font_button_set_use_size (GtkFontButton *font_button,
gboolean use_size)
{
g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
use_size = (use_size != FALSE);
if (font_button->priv->use_size != use_size)
{
font_button->priv->use_size = use_size;
if (font_button->priv->use_font)
gtk_font_button_label_use_font (font_button);
g_object_notify (G_OBJECT (font_button), "use_size");
}
}
/**
* gtk_font_button_get_show_style:
* @font_button: a #GtkFontButton
*
* Returns whether the name of the font style will be shown in the label.
*
* Return value: whether the font style will be shown in the label.
*
* Since: 2.4
**/
gboolean
gtk_font_button_get_show_style (GtkFontButton *font_button)
{
g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
return font_button->priv->show_style;
}
/**
* gtk_font_button_set_show_style:
* @font_button: a #GtkFontButton
* @show_style: %TRUE if font style should be displayed in label.
*
* If @show_style is %TRUE, the font style will be displayed along with name of the selected font.
*
* Since: 2.4
*/
void
gtk_font_button_set_show_style (GtkFontButton *font_button,
gboolean show_style)
{
g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
show_style = (show_style != FALSE);
if (font_button->priv->show_style != show_style)
{
font_button->priv->show_style = show_style;
gtk_font_button_update_font_info (font_button);
g_object_notify (G_OBJECT (font_button), "show_style");
}
}
/**
* gtk_font_button_get_show_size:
* @font_button: a #GtkFontButton
*
* Returns whether the font size will be shown in the label.
*
* Return value: whether the font size will be shown in the label.
*
* Since: 2.4
**/
gboolean
gtk_font_button_get_show_size (GtkFontButton *font_button)
{
g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
return font_button->priv->show_size;
}
/**
* gtk_font_button_set_show_size:
* @font_button: a #GtkFontButton
* @show_size: %TRUE if font size should be displayed in dialog.
*
* If @show_size is %TRUE, the font size will be displayed along with the name of the selected font.
*
* Since: 2.4
*/
void
gtk_font_button_set_show_size (GtkFontButton *font_button,
gboolean show_size)
{
g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
show_size = (show_size != FALSE);
if (font_button->priv->show_size != show_size)
{
font_button->priv->show_size = show_size;
gtk_container_remove (GTK_CONTAINER (font_button), font_button->priv->inside);
font_button->priv->inside = gtk_font_button_create_inside (font_button);
gtk_container_add (GTK_CONTAINER (font_button), font_button->priv->inside);
gtk_font_button_update_font_info (font_button);
g_object_notify (G_OBJECT (font_button), "show_size");
}
}
/**
* gtk_font_button_get_font_name:
* @font_button: a #GtkFontButton
*
* Retrieves the name of the currently selected font.
*
* Returns: an internal copy of the font name which must not be freed.
*
* Since: 2.4
*/
G_CONST_RETURN gchar *
gtk_font_button_get_font_name (GtkFontButton *font_button)
{
g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), NULL);
return font_button->priv->fontname;
}
/**
* gtk_font_button_set_font_name:
* @font_button: a #GtkFontButton
* @fontname: Name of font to display in font selection dialog
*
* Sets or updates the currently-displayed font in font picker dialog.
*
* Returns: Return value of gtk_font_selection_dialog_set_font_name() if the
* font selection dialog exists, otherwise %FALSE.
*
* Since: 2.4
*/
gboolean
gtk_font_button_set_font_name (GtkFontButton *font_button,
const gchar *fontname)
{
gboolean result;
gchar *old_fontname;
g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
g_return_val_if_fail (fontname != NULL, FALSE);
if (g_ascii_strcasecmp (font_button->priv->fontname, fontname))
{
old_fontname = font_button->priv->fontname;
font_button->priv->fontname = g_strdup (fontname);
g_free (old_fontname);
}
gtk_font_button_update_font_info (font_button);
if (font_button->priv->font_dialog)
result = gtk_font_selection_dialog_set_font_name (GTK_FONT_SELECTION_DIALOG (font_button->priv->font_dialog),
font_button->priv->fontname);
else
result = FALSE;
g_object_notify (G_OBJECT (font_button), "font_name");
return result;
}
static void
gtk_font_button_clicked (GtkButton *button)
{
GtkFontSelectionDialog *font_dialog;
GtkFontButton *font_button = GTK_FONT_BUTTON (button);
if (!font_button->priv->font_dialog)
{
GtkWidget *parent;
parent = gtk_widget_get_toplevel (GTK_WIDGET (font_button));
font_button->priv->font_dialog = gtk_font_selection_dialog_new (font_button->priv->title);
font_dialog = GTK_FONT_SELECTION_DIALOG (font_button->priv->font_dialog);
if (parent)
gtk_window_set_transient_for (GTK_WINDOW (font_dialog), GTK_WINDOW (parent));
/* If there is a grabbed window, set new dialog as modal */
if (gtk_grab_get_current ())
gtk_window_set_modal (GTK_WINDOW (font_dialog), TRUE);
g_signal_connect (font_dialog->ok_button, "clicked",
G_CALLBACK (dialog_ok_clicked), font_button);
g_signal_connect (font_dialog->cancel_button, "clicked",
G_CALLBACK (dialog_cancel_clicked), font_button);
g_signal_connect (font_dialog, "destroy",
G_CALLBACK (dialog_destroy), font_button);
}
if (!GTK_WIDGET_VISIBLE (font_button->priv->font_dialog))
{
font_dialog = GTK_FONT_SELECTION_DIALOG (font_button->priv->font_dialog);
gtk_font_selection_dialog_set_font_name (font_dialog, font_button->priv->fontname);
}
gtk_window_present (GTK_WINDOW (font_button->priv->font_dialog));
}
static void
dialog_ok_clicked (GtkWidget *widget,
gpointer data)
{
GtkFontButton *font_button = GTK_FONT_BUTTON (data);
gtk_widget_hide (font_button->priv->font_dialog);
g_free (font_button->priv->fontname);
font_button->priv->fontname = gtk_font_selection_dialog_get_font_name (GTK_FONT_SELECTION_DIALOG (font_button->priv->font_dialog));
/* Set label font */
gtk_font_button_update_font_info (font_button);
g_object_notify (G_OBJECT (font_button), "font_name");
/* Emit font_set signal */
g_signal_emit (font_button, font_button_signals[FONT_SET], 0);
}
static void
dialog_cancel_clicked (GtkWidget *widget,
gpointer data)
{
GtkFontButton *font_button = GTK_FONT_BUTTON (data);
gtk_widget_hide (font_button->priv->font_dialog);
}
static void
dialog_destroy (GtkWidget *widget,
gpointer data)
{
GtkFontButton *font_button = GTK_FONT_BUTTON (data);
/* Dialog will get destroyed so reference is not valid now */
font_button->priv->font_dialog = NULL;
}
static GtkWidget *
gtk_font_button_create_inside (GtkFontButton *font_button)
{
GtkWidget *widget;
gtk_widget_push_composite_child ();
widget = gtk_hbox_new (FALSE, 0);
font_button->priv->font_label = gtk_label_new (_("Font"));
gtk_label_set_justify (GTK_LABEL (font_button->priv->font_label), GTK_JUSTIFY_LEFT);
gtk_box_pack_start (GTK_BOX (widget), font_button->priv->font_label, TRUE, TRUE, 5);
if (font_button->priv->show_size)
{
gtk_box_pack_start (GTK_BOX (widget), gtk_vseparator_new (), FALSE, FALSE, 0);
font_button->priv->size_label = gtk_label_new ("14");
gtk_box_pack_start (GTK_BOX (widget), font_button->priv->size_label, FALSE, FALSE, 5);
}
gtk_widget_show_all (widget);
gtk_widget_pop_composite_child ();
return widget;
}
static void
gtk_font_button_label_use_font (GtkFontButton *font_button)
{
PangoFontDescription *desc;
if (!font_button->priv->use_font)
return;
desc = pango_font_description_from_string (font_button->priv->fontname);
if (!font_button->priv->use_size)
pango_font_description_unset_fields (desc, PANGO_FONT_MASK_SIZE);
gtk_widget_modify_font (font_button->priv->font_label, desc);
}
static gboolean
font_description_style_equal (const PangoFontDescription *a,
const PangoFontDescription *b)
{
return (pango_font_description_get_weight (a) == pango_font_description_get_weight (b) &&
pango_font_description_get_style (a) == pango_font_description_get_style (b) &&
pango_font_description_get_stretch (a) == pango_font_description_get_stretch (b) &&
pango_font_description_get_variant (a) == pango_font_description_get_variant (b));
}
static void
gtk_font_button_update_font_info (GtkFontButton *font_button)
{
PangoFontDescription *desc;
const gchar *family;
gchar *style;
gchar *family_style;
desc = pango_font_description_from_string (font_button->priv->fontname);
family = pango_font_description_get_family (desc);
#if 0
/* This gives the wrong names, e.g. Italic when the font selection
* dialog displayed Oblique.
*/
pango_font_description_unset_fields (desc, PANGO_FONT_MASK_FAMILY | PANGO_FONT_MASK_SIZE);
style = pango_font_description_to_string (desc);
gtk_label_set_text (GTK_LABEL (font_button->priv->style_label), style);
#endif
style = NULL;
if (font_button->priv->show_style)
{
PangoFontFamily **families;
PangoFontFace **faces;
gint n_families, n_faces, i;
n_families = 0;
n_faces = 0;
pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (font_button)),
&families, &n_families);
for (i = 0; i < n_families; i++)
{
const gchar *name = pango_font_family_get_name (families[i]);
if (!g_ascii_strcasecmp (name, family))
{
pango_font_family_list_faces (families[i], &faces, &n_faces);
break;
}
}
g_free (families);
for (i = 0; i < n_faces; i++)
{
PangoFontDescription *tmp_desc = pango_font_face_describe (faces[i]);
if (font_description_style_equal (tmp_desc, desc))
{
style = g_strdup (pango_font_face_get_face_name (faces[i]));
pango_font_description_free (tmp_desc);
break;
}
else
pango_font_description_free (tmp_desc);
}
g_free (faces);
}
if (style == NULL || !g_ascii_strcasecmp (style, "Regular"))
family_style = g_strdup (family);
else
family_style = g_strdup_printf ("%s %s", family, style);
gtk_label_set_text (GTK_LABEL (font_button->priv->font_label), family_style);
g_free (style);
g_free (family_style);
if (font_button->priv->show_size)
{
gchar *size = g_strdup_printf ("%d",
pango_font_description_get_size (desc) / PANGO_SCALE);
gtk_label_set_text (GTK_LABEL (font_button->priv->size_label), size);
g_free (size);
}
gtk_font_button_label_use_font (font_button);
pango_font_description_free (desc);
}

101
gtk/gtkfontbutton.h Normal file
View File

@ -0,0 +1,101 @@
/* GTK - The GIMP Toolkit
* Copyright (C) 1998 David Abilleira Freijeiro <odaf@nexo.es>
* All rights reserved
* Based on gnome-color-picker by Federico Mena <federico@nuclecu.unam.mx>
*
* 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 this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Cambridge, MA 02139, USA.
*/
/*
* 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/.
*/
#ifndef __GTK_FONT_BUTTON_H__
#define __GTK_FONT_BUTTON_H__
#include <gtk/gtkbutton.h>
G_BEGIN_DECLS
/* GtkFontButton is a button widget that allow user to select a font.
*/
#define GTK_TYPE_FONT_BUTTON (gtk_font_button_get_type ())
#define GTK_FONT_BUTTON(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_FONT_BUTTON, GtkFontButton))
#define GTK_FONT_BUTTON_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FONT_BUTTON, GtkFontButtonClass))
#define GTK_IS_FONT_BUTTON(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_FONT_BUTTON))
#define GTK_IS_FONT_BUTTON_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FONT_BUTTON))
#define GTK_FONT_BUTTON_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FONT_BUTTON, GtkFontButtonClass))
#define GTK_FONT_BUTTON_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_FONT_BUTTON, GtkFontButtonPrivate))
typedef struct _GtkFontButton GtkFontButton;
typedef struct _GtkFontButtonClass GtkFontButtonClass;
typedef struct _GtkFontButtonPrivate GtkFontButtonPrivate;
struct _GtkFontButton {
GtkButton button;
/*< private >*/
GtkFontButtonPrivate *priv;
};
struct _GtkFontButtonClass {
GtkButtonClass parent_class;
/* font_set signal is emitted when font is chosen */
void (* font_set) (GtkFontButton *gfp);
/* Padding for future expansion */
void (*_gtk_reserved1) (void);
void (*_gtk_reserved2) (void);
void (*_gtk_reserved3) (void);
void (*_gtk_reserved4) (void);
};
GType gtk_font_button_get_type (void) G_GNUC_CONST;
GtkWidget *gtk_font_button_new (void);
GtkWidget *gtk_font_button_new_with_font (const gchar *fontname);
G_CONST_RETURN gchar *gtk_font_button_get_title (GtkFontButton *font_button);
void gtk_font_button_set_title (GtkFontButton *font_button,
const gchar *title);
gboolean gtk_font_button_get_use_font (GtkFontButton *font_button);
void gtk_font_button_set_use_font (GtkFontButton *font_button,
gboolean use_font);
gboolean gtk_font_button_get_use_size (GtkFontButton *font_button);
void gtk_font_button_set_use_size (GtkFontButton *font_button,
gboolean use_size);
G_CONST_RETURN gchar* gtk_font_button_get_font_name (GtkFontButton *font_button);
gboolean gtk_font_button_set_font_name (GtkFontButton *font_button,
const gchar *fontname);
gboolean gtk_font_button_get_show_style (GtkFontButton *font_button);
void gtk_font_button_set_show_style (GtkFontButton *font_button,
gboolean show_style);
gboolean gtk_font_button_get_show_size (GtkFontButton *font_button);
void gtk_font_button_set_show_size (GtkFontButton *font_button,
gboolean show_size);
G_END_DECLS
#endif /* __GTK_FONT_BUTTON_H__ */