Gimp/libgimp/gimpfontselectbutton.c
Michael Natterer 9638102418 Introduce a global ID space for GimpData/GimpResource objects
Much like for images and items. Change the PDB to transmit IDs
instead of names for brush, pattern etc. and refactor a whole
lot of libgimp code to deal with it.

	modified:   libgimp/gimpplugin-private.h
2023-05-31 16:12:04 +02:00

227 lines
6.5 KiB
C

/* LIBGIMP - The GIMP Library
* Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
*
* gimpfontselectbutton.c
* Copyright (C) 2003 Sven Neumann <sven@gimp.org>
*
* 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 3 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 Lesser General Public
* License along with this library. If not, see
* <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <gegl.h>
#include <gtk/gtk.h>
#include "libgimpwidgets/gimpwidgets.h"
#include "gimp.h"
#include "gimpuitypes.h"
#include "gimpfontselectbutton.h"
#include "gimpuimarshal.h"
#include "libgimp-intl.h"
/**
* SECTION: gimpfontselectbutton
* @title: GimpFontSelectButton
* @short_description: A button which pops up a font selection dialog.
*
* A button which pops up a font selection dialog.
**/
struct _GimpFontSelectButton
{
GimpResourceSelectButton parent_instance;
GtkWidget *font_name_label;
GtkWidget *drag_region_widget;
GtkWidget *button;
};
static void gimp_font_select_button_draw_interior (GimpResourceSelectButton *self);
static GtkWidget *gimp_font_select_button_create_interior (GimpFontSelectButton *self);
static const GtkTargetEntry drag_target = { "application/x-gimp-font-name", 0, 0 };
G_DEFINE_FINAL_TYPE (GimpFontSelectButton,
gimp_font_select_button,
GIMP_TYPE_RESOURCE_SELECT_BUTTON)
static void
gimp_font_select_button_class_init (GimpFontSelectButtonClass *klass)
{
GimpResourceSelectButtonClass *superclass = GIMP_RESOURCE_SELECT_BUTTON_CLASS (klass);
superclass->draw_interior = gimp_font_select_button_draw_interior;
superclass->resource_type = GIMP_TYPE_FONT;
}
static void
gimp_font_select_button_init (GimpFontSelectButton *self)
{
GtkWidget *interior;
interior = gimp_font_select_button_create_interior (self);
gimp_resource_select_button_embed_interior (GIMP_RESOURCE_SELECT_BUTTON (self), interior);
gimp_resource_select_button_set_drag_target (GIMP_RESOURCE_SELECT_BUTTON (self),
self->drag_region_widget,
&drag_target);
gimp_resource_select_button_set_clickable (GIMP_RESOURCE_SELECT_BUTTON (self),
self->button);
}
/**
* gimp_font_select_button_new:
* @title: (nullable): Title of the dialog to use or %NULL to use the default title.
* @resource: (nullable): Initial font.
*
* Creates a new #GtkWidget that lets a user choose a font.
* You can put this widget in a plug-in dialog.
*
* When font is NULL, initial choice is from context.
*
* Returns: A #GtkWidget that you can use in your UI.
*
* Since: 2.4
*/
GtkWidget *
gimp_font_select_button_new (const gchar *title,
GimpResource *resource)
{
GtkWidget *self;
g_return_val_if_fail (resource == NULL || GIMP_IS_FONT (resource), NULL);
if (resource == NULL)
resource = GIMP_RESOURCE (gimp_context_get_font ());
if (title)
self = g_object_new (GIMP_TYPE_FONT_SELECT_BUTTON,
"title", title,
"resource", resource,
NULL);
else
self = g_object_new (GIMP_TYPE_FONT_SELECT_BUTTON,
"resource", resource,
NULL);
gimp_font_select_button_draw_interior (GIMP_RESOURCE_SELECT_BUTTON (self));
return self;
}
/**
* gimp_font_select_button_get_font:
* @self: A #GimpFontSelectButton
*
* Gets the currently selected font.
*
* Returns: (transfer none): an internal copy of the font which must not be freed.
*
* Since: 2.4
*/
GimpFont *
gimp_font_select_button_get_font (GimpFontSelectButton *self)
{
g_return_val_if_fail (GIMP_IS_FONT_SELECT_BUTTON (self), NULL);
return (GimpFont *) gimp_resource_select_button_get_resource ((GimpResourceSelectButton*) self);
}
/**
* gimp_font_select_button_set_font:
* @self: A #GimpFontSelectButton
* @font: Font to set.
*
* Sets the currently selected font.
* Usually you should not call this; the user is in charge.
* Changes the selection in both the button and it's popup chooser.
*
* Since: 2.4
*/
void
gimp_font_select_button_set_font (GimpFontSelectButton *self,
GimpFont *font)
{
g_return_if_fail (GIMP_IS_FONT_SELECT_BUTTON (self));
gimp_resource_select_button_set_resource (GIMP_RESOURCE_SELECT_BUTTON (self), GIMP_RESOURCE (font));
}
/* private functions */
/* This is NOT an implementation of virtual function.
*
* Create a widget that is the interior of a button.
* Super creates the button, self creates interior.
* Button is-a container and self calls super to add interior to the container.
*
* Special: an hbox containing a general icon for a font and
* a label that is the name of the font family and style.
* FUTURE: label styled in the current font family and style.
*/
static GtkWidget*
gimp_font_select_button_create_interior (GimpFontSelectButton *self)
{
GtkWidget *button;
GtkWidget *hbox;
GtkWidget *image;
GtkWidget *label;
gchar *font_name = "unknown";
button = gtk_button_new ();
hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 4);
gtk_container_add (GTK_CONTAINER (button), hbox);
image = gtk_image_new_from_icon_name (GIMP_ICON_FONT,
GTK_ICON_SIZE_BUTTON);
gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
label = gtk_label_new (font_name);
gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 4);
self->font_name_label = label; /* Save label for redraw. */
self->drag_region_widget = hbox;
self->button = button;
return button;
}
static void
gimp_font_select_button_draw_interior (GimpResourceSelectButton *self)
{
GimpFontSelectButton *font_select= GIMP_FONT_SELECT_BUTTON (self);
GimpResource *resource;
gchar *name = NULL;
resource = gimp_resource_select_button_get_resource (self);
if (resource)
name = gimp_resource_get_name (resource);
gtk_label_set_text (GTK_LABEL (font_select->font_name_label), name);
}