1
/* LIBGIMP - The GIMP Library
2
* Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
4
* gimpfontselectbutton.c
5
* Copyright (C) 2003 Sven Neumann <sven@gimp.org>
7
* This library is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU Lesser General Public
9
* License as published by the Free Software Foundation; either
10
* version 2 of the License, or (at your option) any later version.
12
* This library is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Library General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this library; if not, write to the
19
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20
* Boston, MA 02111-1307, USA.
27
#include "libgimpwidgets/gimpwidgets.h"
31
#include "gimpuitypes.h"
32
#include "gimpfontselectbutton.h"
33
#include "gimpuimarshal.h"
35
#include "libgimp-intl.h"
38
#define GIMP_FONT_SELECT_BUTTON_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GIMP_TYPE_FONT_SELECT_BUTTON, GimpFontSelectButtonPrivate))
40
typedef struct _GimpFontSelectButtonPrivate GimpFontSelectButtonPrivate;
42
struct _GimpFontSelectButtonPrivate
46
gchar *font_name; /* local copy */
66
/* local function prototypes */
68
static void gimp_font_select_button_finalize (GObject *object);
70
static void gimp_font_select_button_set_property (GObject *object,
74
static void gimp_font_select_button_get_property (GObject *object,
79
static void gimp_font_select_button_clicked (GimpFontSelectButton *button);
81
static void gimp_font_select_button_callback (const gchar *font_name,
82
gboolean dialog_closing,
85
static void gimp_font_select_drag_data_received (GimpFontSelectButton *button,
86
GdkDragContext *context,
89
GtkSelectionData *selection,
93
static GtkWidget * gimp_font_select_button_create_inside (GimpFontSelectButton *button);
96
static const GtkTargetEntry target = { "application/x-gimp-font-name", 0 };
98
static guint font_button_signals[LAST_SIGNAL] = { 0 };
101
G_DEFINE_TYPE (GimpFontSelectButton, gimp_font_select_button,
102
GIMP_TYPE_SELECT_BUTTON)
106
gimp_font_select_button_class_init (GimpFontSelectButtonClass *klass)
108
GObjectClass *object_class = G_OBJECT_CLASS (klass);
109
GimpSelectButtonClass *select_button_class = GIMP_SELECT_BUTTON_CLASS (klass);
111
object_class->finalize = gimp_font_select_button_finalize;
112
object_class->set_property = gimp_font_select_button_set_property;
113
object_class->get_property = gimp_font_select_button_get_property;
115
select_button_class->select_destroy = gimp_font_select_destroy;
117
klass->font_set = NULL;
120
* GimpFontSelectButton:title:
122
* The title to be used for the font selection popup dialog.
126
g_object_class_install_property (object_class, PROP_TITLE,
127
g_param_spec_string ("title",
129
"The title to be used for the font selection popup dialog",
131
GIMP_PARAM_READWRITE |
132
G_PARAM_CONSTRUCT_ONLY));
135
* GimpFontSelectButton:font-name:
137
* The name of the currently selected font.
141
g_object_class_install_property (object_class, PROP_FONT_NAME,
142
g_param_spec_string ("font-name",
144
"The name of the currently selected font",
146
GIMP_PARAM_READWRITE));
149
* GimpFontSelectButton::font-set:
150
* @widget: the object which received the signal.
151
* @font_name: the name of the currently selected font.
152
* @dialog_closing: whether the dialog was closed or not.
154
* The ::font-set signal is emitted when the user selects a font.
158
font_button_signals[FONT_SET] =
159
g_signal_new ("font-set",
160
G_TYPE_FROM_CLASS (klass),
162
G_STRUCT_OFFSET (GimpFontSelectButtonClass, font_set),
164
_gimpui_marshal_VOID__STRING_BOOLEAN,
169
g_type_class_add_private (object_class,
170
sizeof (GimpFontSelectButtonPrivate));
174
gimp_font_select_button_init (GimpFontSelectButton *button)
176
GimpFontSelectButtonPrivate *priv;
178
priv = GIMP_FONT_SELECT_BUTTON_GET_PRIVATE (button);
180
priv->font_name = NULL;
182
priv->inside = gimp_font_select_button_create_inside (button);
183
gtk_container_add (GTK_CONTAINER (button), priv->inside);
187
* gimp_font_select_button_new:
188
* @title: Title of the dialog to use or %NULL to use the default title.
189
* @font_name: Initial font name.
191
* Creates a new #GtkWidget that completely controls the selection of
192
* a font. This widget is suitable for placement in a table in a
195
* Returns: A #GtkWidget that you can use in your UI.
200
gimp_font_select_button_new (const gchar *title,
201
const gchar *font_name)
206
button = g_object_new (GIMP_TYPE_FONT_SELECT_BUTTON,
208
"font-name", font_name,
211
button = g_object_new (GIMP_TYPE_FONT_SELECT_BUTTON,
212
"font-name", font_name,
219
* gimp_font_select_button_get_font:
220
* @button: A #GimpFontSelectButton
222
* Retrieves the name of currently selected font.
224
* Returns: an internal copy of the font name which must not be freed.
229
gimp_font_select_button_get_font (GimpFontSelectButton *button)
231
GimpFontSelectButtonPrivate *priv;
233
g_return_val_if_fail (GIMP_IS_FONT_SELECT_BUTTON (button), NULL);
235
priv = GIMP_FONT_SELECT_BUTTON_GET_PRIVATE (button);
236
return priv->font_name;
240
* gimp_font_select_button_set_font:
241
* @button: A #GimpFontSelectButton
242
* @font_name: Font name to set; %NULL means no change.
244
* Sets the current font for the font select button.
249
gimp_font_select_button_set_font (GimpFontSelectButton *button,
250
const gchar *font_name)
252
GimpSelectButton *select_button;
254
g_return_if_fail (GIMP_IS_FONT_SELECT_BUTTON (button));
256
select_button = GIMP_SELECT_BUTTON (button);
258
if (select_button->temp_callback)
259
gimp_fonts_set_popup (select_button->temp_callback, font_name);
261
gimp_font_select_button_callback (font_name, FALSE, button);
265
/* private functions */
268
gimp_font_select_button_finalize (GObject *object)
270
GimpFontSelectButtonPrivate *priv;
272
priv = GIMP_FONT_SELECT_BUTTON_GET_PRIVATE (object);
274
g_free (priv->font_name);
275
priv->font_name = NULL;
277
g_free (priv->title);
280
G_OBJECT_CLASS (gimp_font_select_button_parent_class)->finalize (object);
284
gimp_font_select_button_set_property (GObject *object,
289
GimpFontSelectButton *button = GIMP_FONT_SELECT_BUTTON (object);
290
GimpFontSelectButtonPrivate *priv;
292
priv = GIMP_FONT_SELECT_BUTTON_GET_PRIVATE (button);
297
priv->title = g_value_dup_string (value);
300
gimp_font_select_button_set_font (button,
301
g_value_get_string (value));
304
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
310
gimp_font_select_button_get_property (GObject *object,
315
GimpFontSelectButton *button = GIMP_FONT_SELECT_BUTTON (object);
316
GimpFontSelectButtonPrivate *priv;
318
priv = GIMP_FONT_SELECT_BUTTON_GET_PRIVATE (button);
323
g_value_set_string (value, priv->title);
326
g_value_set_string (value, priv->font_name);
329
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
335
gimp_font_select_button_callback (const gchar *font_name,
336
gboolean dialog_closing,
339
GimpFontSelectButton *button;
340
GimpFontSelectButtonPrivate *priv;
341
GimpSelectButton *select_button;
343
button = GIMP_FONT_SELECT_BUTTON (user_data);
345
priv = GIMP_FONT_SELECT_BUTTON_GET_PRIVATE (button);
346
select_button = GIMP_SELECT_BUTTON (button);
348
g_free (priv->font_name);
349
priv->font_name = g_strdup (font_name);
351
gtk_label_set_text (GTK_LABEL (priv->label), font_name);
354
select_button->temp_callback = NULL;
356
g_signal_emit (button, font_button_signals[FONT_SET], 0,
357
font_name, dialog_closing);
358
g_object_notify (G_OBJECT (button), "font-name");
362
gimp_font_select_button_clicked (GimpFontSelectButton *button)
364
GimpFontSelectButtonPrivate *priv;
365
GimpSelectButton *select_button;
367
priv = GIMP_FONT_SELECT_BUTTON_GET_PRIVATE (button);
368
select_button = GIMP_SELECT_BUTTON (button);
370
if (select_button->temp_callback)
372
/* calling gimp_fonts_set_popup() raises the dialog */
373
gimp_fonts_set_popup (select_button->temp_callback,
378
select_button->temp_callback =
379
gimp_font_select_new (priv->title, priv->font_name,
380
gimp_font_select_button_callback,
386
gimp_font_select_drag_data_received (GimpFontSelectButton *button,
387
GdkDragContext *context,
390
GtkSelectionData *selection,
396
if ((selection->format != 8) || (selection->length < 1))
398
g_warning ("Received invalid font data!");
402
str = g_strndup ((const gchar *) selection->data, selection->length);
404
if (g_utf8_validate (str, -1, NULL))
408
gint name_offset = 0;
410
if (sscanf (str, "%i:%p:%n", &pid, &unused, &name_offset) >= 2 &&
411
pid == gimp_getpid () && name_offset > 0)
413
gchar *name = str + name_offset;
415
gimp_font_select_button_set_font (button, name);
423
gimp_font_select_button_create_inside (GimpFontSelectButton *font_button)
428
GimpFontSelectButtonPrivate *priv;
430
priv = GIMP_FONT_SELECT_BUTTON_GET_PRIVATE (font_button);
432
gtk_widget_push_composite_child ();
434
button = gtk_button_new ();
436
hbox = gtk_hbox_new (FALSE, 4);
437
gtk_container_add (GTK_CONTAINER (button), hbox);
439
image = gtk_image_new_from_stock (GIMP_STOCK_FONT, GTK_ICON_SIZE_BUTTON);
440
gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
442
priv->label = gtk_label_new (priv->font_name);
443
gtk_box_pack_start (GTK_BOX (hbox), priv->label, TRUE, TRUE, 4);
445
gtk_widget_show_all (button);
447
g_signal_connect_swapped (button, "clicked",
448
G_CALLBACK (gimp_font_select_button_clicked),
451
gtk_drag_dest_set (GTK_WIDGET (button),
452
GTK_DEST_DEFAULT_HIGHLIGHT |
453
GTK_DEST_DEFAULT_MOTION |
454
GTK_DEST_DEFAULT_DROP,
458
g_signal_connect_swapped (button, "drag-data-received",
459
G_CALLBACK (gimp_font_select_drag_data_received),
462
gtk_widget_pop_composite_child ();