~ubuntu-branches/ubuntu/jaunty/gimp/jaunty-security

« back to all changes in this revision

Viewing changes to libgimp/gimpfontselectbutton.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2007-05-02 16:33:03 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070502163303-bvzhjzbpw8qglc4y
Tags: 2.3.16-1ubuntu1
* Resynchronized with Debian, remaining Ubuntu changes:
  - debian/rules: i18n magic.
* debian/control.in:
  - Maintainer: Ubuntu Core Developers <ubuntu-devel@lists.ubuntu.com>
* debian/patches/02_help-message.patch,
  debian/patches/03_gimp.desktop.in.in.patch,
  debian/patches/10_dont_show_wizard.patch: updated.
* debian/patches/04_composite-signedness.patch,
  debian/patches/05_add-letter-spacing.patch: dropped, used upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* LIBGIMP - The GIMP Library
 
2
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
 
3
 *
 
4
 * gimpfontselectbutton.c
 
5
 * Copyright (C) 2003  Sven Neumann  <sven@gimp.org>
 
6
 *
 
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.
 
11
 *
 
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.
 
16
 *
 
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.
 
21
 */
 
22
 
 
23
#include "config.h"
 
24
 
 
25
#include <gtk/gtk.h>
 
26
 
 
27
#include "libgimpwidgets/gimpwidgets.h"
 
28
 
 
29
#include "gimp.h"
 
30
 
 
31
#include "gimpuitypes.h"
 
32
#include "gimpfontselectbutton.h"
 
33
#include "gimpuimarshal.h"
 
34
 
 
35
#include "libgimp-intl.h"
 
36
 
 
37
 
 
38
#define GIMP_FONT_SELECT_BUTTON_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GIMP_TYPE_FONT_SELECT_BUTTON, GimpFontSelectButtonPrivate))
 
39
 
 
40
typedef struct _GimpFontSelectButtonPrivate GimpFontSelectButtonPrivate;
 
41
 
 
42
struct _GimpFontSelectButtonPrivate
 
43
{
 
44
  gchar       *title;
 
45
 
 
46
  gchar       *font_name;      /* local copy */
 
47
 
 
48
  GtkWidget   *inside;
 
49
  GtkWidget   *label;
 
50
};
 
51
 
 
52
enum
 
53
{
 
54
  FONT_SET,
 
55
  LAST_SIGNAL
 
56
};
 
57
 
 
58
enum
 
59
{
 
60
  PROP_0,
 
61
  PROP_TITLE,
 
62
  PROP_FONT_NAME
 
63
};
 
64
 
 
65
 
 
66
/*  local function prototypes  */
 
67
 
 
68
static void   gimp_font_select_button_finalize     (GObject      *object);
 
69
 
 
70
static void   gimp_font_select_button_set_property (GObject      *object,
 
71
                                                    guint         property_id,
 
72
                                                    const GValue *value,
 
73
                                                    GParamSpec   *pspec);
 
74
static void   gimp_font_select_button_get_property (GObject      *object,
 
75
                                                    guint         property_id,
 
76
                                                    GValue       *value,
 
77
                                                    GParamSpec   *pspec);
 
78
 
 
79
static void   gimp_font_select_button_clicked  (GimpFontSelectButton *button);
 
80
 
 
81
static void   gimp_font_select_button_callback (const gchar *font_name,
 
82
                                                gboolean     dialog_closing,
 
83
                                                gpointer     user_data);
 
84
 
 
85
static void   gimp_font_select_drag_data_received (GimpFontSelectButton *button,
 
86
                                                   GdkDragContext       *context,
 
87
                                                   gint                  x,
 
88
                                                   gint                  y,
 
89
                                                   GtkSelectionData     *selection,
 
90
                                                   guint                 info,
 
91
                                                   guint                 time);
 
92
 
 
93
static GtkWidget * gimp_font_select_button_create_inside (GimpFontSelectButton *button);
 
94
 
 
95
 
 
96
static const GtkTargetEntry target = { "application/x-gimp-font-name", 0 };
 
97
 
 
98
static guint font_button_signals[LAST_SIGNAL] = { 0 };
 
99
 
 
100
 
 
101
G_DEFINE_TYPE (GimpFontSelectButton, gimp_font_select_button,
 
102
               GIMP_TYPE_SELECT_BUTTON)
 
103
 
 
104
 
 
105
static void
 
106
gimp_font_select_button_class_init (GimpFontSelectButtonClass *klass)
 
107
{
 
108
  GObjectClass          *object_class        = G_OBJECT_CLASS (klass);
 
109
  GimpSelectButtonClass *select_button_class = GIMP_SELECT_BUTTON_CLASS (klass);
 
110
 
 
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;
 
114
 
 
115
  select_button_class->select_destroy = gimp_font_select_destroy;
 
116
 
 
117
  klass->font_set = NULL;
 
118
 
 
119
  /**
 
120
   * GimpFontSelectButton:title:
 
121
   *
 
122
   * The title to be used for the font selection popup dialog.
 
123
   *
 
124
   * Since: GIMP 2.4
 
125
   */
 
126
  g_object_class_install_property (object_class, PROP_TITLE,
 
127
                                   g_param_spec_string ("title",
 
128
                                                        "Title",
 
129
                                                        "The title to be used for the font selection popup dialog",
 
130
                                                        _("Font Selection"),
 
131
                                                        GIMP_PARAM_READWRITE |
 
132
                                                        G_PARAM_CONSTRUCT_ONLY));
 
133
 
 
134
  /**
 
135
   * GimpFontSelectButton:font-name:
 
136
   *
 
137
   * The name of the currently selected font.
 
138
   *
 
139
   * Since: GIMP 2.4
 
140
   */
 
141
  g_object_class_install_property (object_class, PROP_FONT_NAME,
 
142
                                   g_param_spec_string ("font-name",
 
143
                                                        "Font name",
 
144
                                                        "The name of the currently selected font",
 
145
                                                        _("Sans"),
 
146
                                                        GIMP_PARAM_READWRITE));
 
147
 
 
148
  /**
 
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.
 
153
   *
 
154
   * The ::font-set signal is emitted when the user selects a font.
 
155
   *
 
156
   * Since: GIMP 2.4
 
157
   */
 
158
  font_button_signals[FONT_SET] =
 
159
    g_signal_new ("font-set",
 
160
                  G_TYPE_FROM_CLASS (klass),
 
161
                  G_SIGNAL_RUN_FIRST,
 
162
                  G_STRUCT_OFFSET (GimpFontSelectButtonClass, font_set),
 
163
                  NULL, NULL,
 
164
                  _gimpui_marshal_VOID__STRING_BOOLEAN,
 
165
                  G_TYPE_NONE, 2,
 
166
                  G_TYPE_STRING,
 
167
                  G_TYPE_BOOLEAN);
 
168
 
 
169
  g_type_class_add_private (object_class,
 
170
                            sizeof (GimpFontSelectButtonPrivate));
 
171
}
 
172
 
 
173
static void
 
174
gimp_font_select_button_init (GimpFontSelectButton *button)
 
175
{
 
176
  GimpFontSelectButtonPrivate *priv;
 
177
 
 
178
  priv = GIMP_FONT_SELECT_BUTTON_GET_PRIVATE (button);
 
179
 
 
180
  priv->font_name = NULL;
 
181
 
 
182
  priv->inside = gimp_font_select_button_create_inside (button);
 
183
  gtk_container_add (GTK_CONTAINER (button), priv->inside);
 
184
}
 
185
 
 
186
/**
 
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.
 
190
 *
 
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
 
193
 * plug-in dialog.
 
194
 *
 
195
 * Returns: A #GtkWidget that you can use in your UI.
 
196
 *
 
197
 * Since: GIMP 2.4
 
198
 */
 
199
GtkWidget *
 
200
gimp_font_select_button_new (const gchar *title,
 
201
                             const gchar *font_name)
 
202
{
 
203
  GtkWidget *button;
 
204
 
 
205
  if (title)
 
206
    button = g_object_new (GIMP_TYPE_FONT_SELECT_BUTTON,
 
207
                           "title",     title,
 
208
                           "font-name", font_name,
 
209
                           NULL);
 
210
  else
 
211
    button = g_object_new (GIMP_TYPE_FONT_SELECT_BUTTON,
 
212
                           "font-name", font_name,
 
213
                           NULL);
 
214
 
 
215
  return button;
 
216
}
 
217
 
 
218
/**
 
219
 * gimp_font_select_button_get_font:
 
220
 * @button: A #GimpFontSelectButton
 
221
 *
 
222
 * Retrieves the name of currently selected font.
 
223
 *
 
224
 * Returns: an internal copy of the font name which must not be freed.
 
225
 *
 
226
 * Since: GIMP 2.4
 
227
 */
 
228
const gchar *
 
229
gimp_font_select_button_get_font (GimpFontSelectButton *button)
 
230
{
 
231
  GimpFontSelectButtonPrivate *priv;
 
232
 
 
233
  g_return_val_if_fail (GIMP_IS_FONT_SELECT_BUTTON (button), NULL);
 
234
 
 
235
  priv = GIMP_FONT_SELECT_BUTTON_GET_PRIVATE (button);
 
236
  return priv->font_name;
 
237
}
 
238
 
 
239
/**
 
240
 * gimp_font_select_button_set_font:
 
241
 * @button: A #GimpFontSelectButton
 
242
 * @font_name: Font name to set; %NULL means no change.
 
243
 *
 
244
 * Sets the current font for the font select button.
 
245
 *
 
246
 * Since: GIMP 2.4
 
247
 */
 
248
void
 
249
gimp_font_select_button_set_font (GimpFontSelectButton *button,
 
250
                                  const gchar          *font_name)
 
251
{
 
252
  GimpSelectButton *select_button;
 
253
 
 
254
  g_return_if_fail (GIMP_IS_FONT_SELECT_BUTTON (button));
 
255
 
 
256
  select_button = GIMP_SELECT_BUTTON (button);
 
257
 
 
258
  if (select_button->temp_callback)
 
259
    gimp_fonts_set_popup (select_button->temp_callback, font_name);
 
260
  else
 
261
    gimp_font_select_button_callback (font_name, FALSE, button);
 
262
}
 
263
 
 
264
 
 
265
/*  private functions  */
 
266
 
 
267
static void
 
268
gimp_font_select_button_finalize (GObject *object)
 
269
{
 
270
  GimpFontSelectButtonPrivate *priv;
 
271
 
 
272
  priv = GIMP_FONT_SELECT_BUTTON_GET_PRIVATE (object);
 
273
 
 
274
  g_free (priv->font_name);
 
275
  priv->font_name = NULL;
 
276
 
 
277
  g_free (priv->title);
 
278
  priv->title = NULL;
 
279
 
 
280
  G_OBJECT_CLASS (gimp_font_select_button_parent_class)->finalize (object);
 
281
}
 
282
 
 
283
static void
 
284
gimp_font_select_button_set_property (GObject      *object,
 
285
                                      guint         property_id,
 
286
                                      const GValue *value,
 
287
                                      GParamSpec   *pspec)
 
288
{
 
289
  GimpFontSelectButton        *button = GIMP_FONT_SELECT_BUTTON (object);
 
290
  GimpFontSelectButtonPrivate *priv;
 
291
 
 
292
  priv = GIMP_FONT_SELECT_BUTTON_GET_PRIVATE (button);
 
293
 
 
294
  switch (property_id)
 
295
    {
 
296
    case PROP_TITLE:
 
297
      priv->title = g_value_dup_string (value);
 
298
      break;
 
299
    case PROP_FONT_NAME:
 
300
      gimp_font_select_button_set_font (button,
 
301
                                        g_value_get_string (value));
 
302
      break;
 
303
    default:
 
304
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 
305
      break;
 
306
    }
 
307
}
 
308
 
 
309
static void
 
310
gimp_font_select_button_get_property (GObject    *object,
 
311
                                      guint       property_id,
 
312
                                      GValue     *value,
 
313
                                      GParamSpec *pspec)
 
314
{
 
315
  GimpFontSelectButton        *button = GIMP_FONT_SELECT_BUTTON (object);
 
316
  GimpFontSelectButtonPrivate *priv;
 
317
 
 
318
  priv = GIMP_FONT_SELECT_BUTTON_GET_PRIVATE (button);
 
319
 
 
320
  switch (property_id)
 
321
    {
 
322
    case PROP_TITLE:
 
323
      g_value_set_string (value, priv->title);
 
324
      break;
 
325
    case PROP_FONT_NAME:
 
326
      g_value_set_string (value, priv->font_name);
 
327
      break;
 
328
    default:
 
329
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 
330
      break;
 
331
    }
 
332
}
 
333
 
 
334
static void
 
335
gimp_font_select_button_callback (const gchar *font_name,
 
336
                                  gboolean     dialog_closing,
 
337
                                  gpointer     user_data)
 
338
{
 
339
  GimpFontSelectButton        *button;
 
340
  GimpFontSelectButtonPrivate *priv;
 
341
  GimpSelectButton            *select_button;
 
342
 
 
343
  button = GIMP_FONT_SELECT_BUTTON (user_data);
 
344
 
 
345
  priv = GIMP_FONT_SELECT_BUTTON_GET_PRIVATE (button);
 
346
  select_button = GIMP_SELECT_BUTTON (button);
 
347
 
 
348
  g_free (priv->font_name);
 
349
  priv->font_name = g_strdup (font_name);
 
350
 
 
351
  gtk_label_set_text (GTK_LABEL (priv->label), font_name);
 
352
 
 
353
  if (dialog_closing)
 
354
    select_button->temp_callback = NULL;
 
355
 
 
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");
 
359
}
 
360
 
 
361
static void
 
362
gimp_font_select_button_clicked (GimpFontSelectButton *button)
 
363
{
 
364
  GimpFontSelectButtonPrivate *priv;
 
365
  GimpSelectButton            *select_button;
 
366
 
 
367
  priv = GIMP_FONT_SELECT_BUTTON_GET_PRIVATE (button);
 
368
  select_button = GIMP_SELECT_BUTTON (button);
 
369
 
 
370
  if (select_button->temp_callback)
 
371
    {
 
372
      /*  calling gimp_fonts_set_popup() raises the dialog  */
 
373
      gimp_fonts_set_popup (select_button->temp_callback,
 
374
                            priv->font_name);
 
375
    }
 
376
  else
 
377
    {
 
378
      select_button->temp_callback =
 
379
        gimp_font_select_new (priv->title, priv->font_name,
 
380
                              gimp_font_select_button_callback,
 
381
                              button);
 
382
    }
 
383
}
 
384
 
 
385
static void
 
386
gimp_font_select_drag_data_received (GimpFontSelectButton *button,
 
387
                                     GdkDragContext       *context,
 
388
                                     gint                  x,
 
389
                                     gint                  y,
 
390
                                     GtkSelectionData     *selection,
 
391
                                     guint                 info,
 
392
                                     guint                 time)
 
393
{
 
394
  gchar *str;
 
395
 
 
396
  if ((selection->format != 8) || (selection->length < 1))
 
397
    {
 
398
      g_warning ("Received invalid font data!");
 
399
      return;
 
400
    }
 
401
 
 
402
  str = g_strndup ((const gchar *) selection->data, selection->length);
 
403
 
 
404
  if (g_utf8_validate (str, -1, NULL))
 
405
    {
 
406
      gint     pid;
 
407
      gpointer unused;
 
408
      gint     name_offset = 0;
 
409
 
 
410
      if (sscanf (str, "%i:%p:%n", &pid, &unused, &name_offset) >= 2 &&
 
411
          pid == gimp_getpid () && name_offset > 0)
 
412
        {
 
413
          gchar *name = str + name_offset;
 
414
 
 
415
          gimp_font_select_button_set_font (button, name);
 
416
        }
 
417
    }
 
418
 
 
419
  g_free (str);
 
420
}
 
421
 
 
422
static GtkWidget *
 
423
gimp_font_select_button_create_inside (GimpFontSelectButton *font_button)
 
424
{
 
425
  GtkWidget                   *button;
 
426
  GtkWidget                   *hbox;
 
427
  GtkWidget                   *image;
 
428
  GimpFontSelectButtonPrivate *priv;
 
429
 
 
430
  priv = GIMP_FONT_SELECT_BUTTON_GET_PRIVATE (font_button);
 
431
 
 
432
  gtk_widget_push_composite_child ();
 
433
 
 
434
  button = gtk_button_new ();
 
435
 
 
436
  hbox = gtk_hbox_new (FALSE, 4);
 
437
  gtk_container_add (GTK_CONTAINER (button), hbox);
 
438
 
 
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);
 
441
 
 
442
  priv->label = gtk_label_new (priv->font_name);
 
443
  gtk_box_pack_start (GTK_BOX (hbox), priv->label, TRUE, TRUE, 4);
 
444
 
 
445
  gtk_widget_show_all (button);
 
446
 
 
447
  g_signal_connect_swapped (button, "clicked",
 
448
                            G_CALLBACK (gimp_font_select_button_clicked),
 
449
                            font_button);
 
450
 
 
451
  gtk_drag_dest_set (GTK_WIDGET (button),
 
452
                     GTK_DEST_DEFAULT_HIGHLIGHT |
 
453
                     GTK_DEST_DEFAULT_MOTION |
 
454
                     GTK_DEST_DEFAULT_DROP,
 
455
                     &target, 1,
 
456
                     GDK_ACTION_COPY);
 
457
 
 
458
  g_signal_connect_swapped (button, "drag-data-received",
 
459
                            G_CALLBACK (gimp_font_select_drag_data_received),
 
460
                            font_button);
 
461
 
 
462
  gtk_widget_pop_composite_child ();
 
463
 
 
464
  return button;
 
465
}