gimpfontselectbutton.c 14.4 KB
Newer Older
1 2 3
/* LIBGIMP - The GIMP Library
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
 *
4
 * gimpfontselectbutton.c
5 6
 * Copyright (C) 2003  Sven Neumann  <sven@gimp.org>
 *
7
 * This library is free software: you can redistribute it and/or
8 9
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
10
 * version 3 of the License, or (at your option) any later version.
11 12 13 14 15 16 17
 *
 * 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
18
 * License along with this library.  If not, see
19
 * <https://www.gnu.org/licenses/>.
20 21 22 23
 */

#include "config.h"

24
#include <gegl.h>
25 26 27 28
#include <gtk/gtk.h>

#include "libgimpwidgets/gimpwidgets.h"

29
#include "gimp.h"
30 31 32

#include "gimpuitypes.h"
#include "gimpfontselectbutton.h"
33
#include "gimpuimarshal.h"
34

35 36
#include "libgimp-intl.h"

37

38 39 40 41 42 43 44 45 46
/**
 * SECTION: gimpfontselectbutton
 * @title: GimpFontSelectButton
 * @short_description: A button which pops up a font selection dialog.
 *
 * A button which pops up a font selection dialog.
 **/


47 48 49 50 51
#define GIMP_FONT_SELECT_BUTTON_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GIMP_TYPE_FONT_SELECT_BUTTON, GimpFontSelectButtonPrivate))

typedef struct _GimpFontSelectButtonPrivate GimpFontSelectButtonPrivate;

struct _GimpFontSelectButtonPrivate
52
{
53
  gchar       *title;
54

55
  gchar       *font_name;      /* local copy */
56

57 58
  GtkWidget   *inside;
  GtkWidget   *label;
59
};
60

61 62 63 64 65 66 67 68 69 70 71 72 73
enum
{
  FONT_SET,
  LAST_SIGNAL
};

enum
{
  PROP_0,
  PROP_TITLE,
  PROP_FONT_NAME
};

74

75
/*  local function prototypes  */
76

77 78
static void   gimp_font_select_button_finalize     (GObject      *object);

79 80 81 82 83 84 85 86 87
static void   gimp_font_select_button_set_property (GObject      *object,
                                                    guint         property_id,
                                                    const GValue *value,
                                                    GParamSpec   *pspec);
static void   gimp_font_select_button_get_property (GObject      *object,
                                                    guint         property_id,
                                                    GValue       *value,
                                                    GParamSpec   *pspec);

88
static void   gimp_font_select_button_clicked  (GimpFontSelectButton *button);
89 90 91 92

static void   gimp_font_select_button_callback (const gchar *font_name,
                                                gboolean     dialog_closing,
                                                gpointer     user_data);
93

94
static void   gimp_font_select_drag_data_received (GimpFontSelectButton *button,
95 96 97 98 99 100
                                                   GdkDragContext       *context,
                                                   gint                  x,
                                                   gint                  y,
                                                   GtkSelectionData     *selection,
                                                   guint                 info,
                                                   guint                 time);
101

102
static GtkWidget * gimp_font_select_button_create_inside (GimpFontSelectButton *button);
103

104 105 106

static const GtkTargetEntry target = { "application/x-gimp-font-name", 0 };

107 108 109
static guint font_button_signals[LAST_SIGNAL] = { 0 };


110 111
G_DEFINE_TYPE (GimpFontSelectButton, gimp_font_select_button,
               GIMP_TYPE_SELECT_BUTTON)
112

113 114 115 116

static void
gimp_font_select_button_class_init (GimpFontSelectButtonClass *klass)
{
117 118
  GObjectClass          *object_class        = G_OBJECT_CLASS (klass);
  GimpSelectButtonClass *select_button_class = GIMP_SELECT_BUTTON_CLASS (klass);
119

120 121 122
  object_class->finalize     = gimp_font_select_button_finalize;
  object_class->set_property = gimp_font_select_button_set_property;
  object_class->get_property = gimp_font_select_button_get_property;
123

124
  select_button_class->select_destroy = gimp_font_select_destroy;
125 126 127 128 129 130

  klass->font_set = NULL;

  /**
   * GimpFontSelectButton:title:
   *
131
   * The title to be used for the font selection popup dialog.
132
   *
133
   * Since: 2.4
134
   */
135 136 137 138
  g_object_class_install_property (object_class, PROP_TITLE,
                                   g_param_spec_string ("title",
                                                        "Title",
                                                        "The title to be used for the font selection popup dialog",
139
                                                        _("Font Selection"),
140
                                                        GIMP_PARAM_READWRITE |
141 142 143 144 145 146 147
                                                        G_PARAM_CONSTRUCT_ONLY));

  /**
   * GimpFontSelectButton:font-name:
   *
   * The name of the currently selected font.
   *
148
   * Since: 2.4
149
   */
150 151 152 153
  g_object_class_install_property (object_class, PROP_FONT_NAME,
                                   g_param_spec_string ("font-name",
                                                        "Font name",
                                                        "The name of the currently selected font",
154
                                                        "Sans-serif",
155
                                                        GIMP_PARAM_READWRITE));
156 157 158 159 160 161 162 163 164

  /**
   * GimpFontSelectButton::font-set:
   * @widget: the object which received the signal.
   * @font_name: the name of the currently selected font.
   * @dialog_closing: whether the dialog was closed or not.
   *
   * The ::font-set signal is emitted when the user selects a font.
   *
165
   * Since: 2.4
166 167 168 169 170 171 172 173 174 175 176 177
   */
  font_button_signals[FONT_SET] =
    g_signal_new ("font-set",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GimpFontSelectButtonClass, font_set),
                  NULL, NULL,
                  _gimpui_marshal_VOID__STRING_BOOLEAN,
                  G_TYPE_NONE, 2,
                  G_TYPE_STRING,
                  G_TYPE_BOOLEAN);

178 179
  g_type_class_add_private (object_class,
                            sizeof (GimpFontSelectButtonPrivate));
180 181 182
}

static void
183
gimp_font_select_button_init (GimpFontSelectButton *button)
184
{
185
  GimpFontSelectButtonPrivate *priv;
186 187

  priv = GIMP_FONT_SELECT_BUTTON_GET_PRIVATE (button);
188

189
  priv->font_name = NULL;
190

191 192
  priv->inside = gimp_font_select_button_create_inside (button);
  gtk_container_add (GTK_CONTAINER (button), priv->inside);
193
}
194 195

/**
196
 * gimp_font_select_button_new:
197
 * @title:     Title of the dialog to use or %NULL to use the default title.
198
 * @font_name: Initial font name.
199
 *
200 201
 * Creates a new #GtkWidget that completely controls the selection of
 * a font.  This widget is suitable for placement in a table in a
202 203
 * plug-in dialog.
 *
204
 * Returns: A #GtkWidget that you can use in your UI.
205
 *
206
 * Since: 2.4
207
 */
208
GtkWidget *
209 210
gimp_font_select_button_new (const gchar *title,
                             const gchar *font_name)
211
{
212
  GtkWidget *button;
213

214
  if (title)
215 216 217 218
    button = g_object_new (GIMP_TYPE_FONT_SELECT_BUTTON,
                           "title",     title,
                           "font-name", font_name,
                           NULL);
219
  else
220 221 222
    button = g_object_new (GIMP_TYPE_FONT_SELECT_BUTTON,
                           "font-name", font_name,
                           NULL);
223

224
  return button;
225 226 227
}

/**
228 229
 * gimp_font_select_button_get_font:
 * @button: A #GimpFontSelectButton
230 231 232 233
 *
 * Retrieves the name of currently selected font.
 *
 * Returns: an internal copy of the font name which must not be freed.
234
 *
235
 * Since: 2.4
236
 */
237
const gchar *
238
gimp_font_select_button_get_font (GimpFontSelectButton *button)
239
{
240
  GimpFontSelectButtonPrivate *priv;
241

242
  g_return_val_if_fail (GIMP_IS_FONT_SELECT_BUTTON (button), NULL);
243

244
  priv = GIMP_FONT_SELECT_BUTTON_GET_PRIVATE (button);
245
  return priv->font_name;
246
}
247

248
/**
249 250
 * gimp_font_select_button_set_font:
 * @button: A #GimpFontSelectButton
251 252 253 254
 * @font_name: Font name to set; %NULL means no change.
 *
 * Sets the current font for the font select button.
 *
255
 * Since: 2.4
256 257
 */
void
258 259
gimp_font_select_button_set_font (GimpFontSelectButton *button,
                                  const gchar          *font_name)
260
{
261
  GimpSelectButton *select_button;
262

263
  g_return_if_fail (GIMP_IS_FONT_SELECT_BUTTON (button));
264

265
  select_button = GIMP_SELECT_BUTTON (button);
266

267 268
  if (select_button->temp_callback)
    gimp_fonts_set_popup (select_button->temp_callback, font_name);
269
  else
270
    gimp_font_select_button_callback (font_name, FALSE, button);
271 272 273 274 275
}


/*  private functions  */

276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
static void
gimp_font_select_button_finalize (GObject *object)
{
  GimpFontSelectButtonPrivate *priv;

  priv = GIMP_FONT_SELECT_BUTTON_GET_PRIVATE (object);

  g_free (priv->font_name);
  priv->font_name = NULL;

  g_free (priv->title);
  priv->title = NULL;

  G_OBJECT_CLASS (gimp_font_select_button_parent_class)->finalize (object);
}

292
static void
293 294 295 296 297
gimp_font_select_button_set_property (GObject      *object,
                                      guint         property_id,
                                      const GValue *value,
                                      GParamSpec   *pspec)
{
298
  GimpFontSelectButton        *button = GIMP_FONT_SELECT_BUTTON (object);
299 300
  GimpFontSelectButtonPrivate *priv;

301
  priv = GIMP_FONT_SELECT_BUTTON_GET_PRIVATE (button);
302 303 304 305

  switch (property_id)
    {
    case PROP_TITLE:
306
      priv->title = g_value_dup_string (value);
307 308
      break;
    case PROP_FONT_NAME:
309 310
      gimp_font_select_button_set_font (button,
                                        g_value_get_string (value));
311 312 313 314 315 316 317 318 319 320 321 322 323
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
gimp_font_select_button_get_property (GObject    *object,
                                      guint       property_id,
                                      GValue     *value,
                                      GParamSpec *pspec)
{
324
  GimpFontSelectButton        *button = GIMP_FONT_SELECT_BUTTON (object);
325 326
  GimpFontSelectButtonPrivate *priv;

327
  priv = GIMP_FONT_SELECT_BUTTON_GET_PRIVATE (button);
328 329 330 331

  switch (property_id)
    {
    case PROP_TITLE:
332
      g_value_set_string (value, priv->title);
333 334
      break;
    case PROP_FONT_NAME:
335
      g_value_set_string (value, priv->font_name);
336 337 338 339 340 341 342 343
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
344 345 346
gimp_font_select_button_callback (const gchar *font_name,
                                  gboolean     dialog_closing,
                                  gpointer     user_data)
347
{
348
  GimpFontSelectButton        *button;
349 350 351
  GimpFontSelectButtonPrivate *priv;
  GimpSelectButton            *select_button;

352
  button = GIMP_FONT_SELECT_BUTTON (user_data);
353

354 355
  priv = GIMP_FONT_SELECT_BUTTON_GET_PRIVATE (button);
  select_button = GIMP_SELECT_BUTTON (button);
356

357
  g_free (priv->font_name);
358
  priv->font_name = g_strdup (font_name);
359

360
  gtk_label_set_text (GTK_LABEL (priv->label), font_name);
361

362 363
  if (dialog_closing)
    select_button->temp_callback = NULL;
364

365
  g_signal_emit (button, font_button_signals[FONT_SET], 0,
366
                 font_name, dialog_closing);
367
  g_object_notify (G_OBJECT (button), "font-name");
368 369 370
}

static void
371
gimp_font_select_button_clicked (GimpFontSelectButton *button)
372
{
373 374
  GimpFontSelectButtonPrivate *priv;
  GimpSelectButton            *select_button;
375 376 377

  priv = GIMP_FONT_SELECT_BUTTON_GET_PRIVATE (button);
  select_button = GIMP_SELECT_BUTTON (button);
378

379
  if (select_button->temp_callback)
380
    {
381
      /*  calling gimp_fonts_set_popup() raises the dialog  */
382 383
      gimp_fonts_set_popup (select_button->temp_callback,
                            priv->font_name);
384 385 386
    }
  else
    {
387
      select_button->temp_callback =
388
        gimp_font_select_new (priv->title, priv->font_name,
389
                              gimp_font_select_button_callback,
390
                              button);
391 392
    }
}
393 394

static void
395
gimp_font_select_drag_data_received (GimpFontSelectButton *button,
396 397 398 399 400 401
                                     GdkDragContext       *context,
                                     gint                  x,
                                     gint                  y,
                                     GtkSelectionData     *selection,
                                     guint                 info,
                                     guint                 time)
402
{
403
  gint   length = gtk_selection_data_get_length (selection);
404
  gchar *str;
405

406
  if (gtk_selection_data_get_format (selection) != 8 || length < 1)
407
    {
408
      g_warning ("%s: received invalid font data", G_STRFUNC);
409 410 411
      return;
    }

412 413
  str = g_strndup ((const gchar *) gtk_selection_data_get_data (selection),
                   length);
414

415 416 417 418 419 420 421 422 423 424 425
  if (g_utf8_validate (str, -1, NULL))
    {
      gint     pid;
      gpointer unused;
      gint     name_offset = 0;

      if (sscanf (str, "%i:%p:%n", &pid, &unused, &name_offset) >= 2 &&
          pid == gimp_getpid () && name_offset > 0)
        {
          gchar *name = str + name_offset;

426
          gimp_font_select_button_set_font (button, name);
427 428
        }
    }
429

430
  g_free (str);
431
}
432 433

static GtkWidget *
434
gimp_font_select_button_create_inside (GimpFontSelectButton *font_button)
435
{
436 437 438 439 440 441
  GtkWidget                   *button;
  GtkWidget                   *hbox;
  GtkWidget                   *image;
  GimpFontSelectButtonPrivate *priv;

  priv = GIMP_FONT_SELECT_BUTTON_GET_PRIVATE (font_button);
442 443 444

  gtk_widget_push_composite_child ();

445 446
  button = gtk_button_new ();

447
  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 4);
448
  gtk_container_add (GTK_CONTAINER (button), hbox);
449

450 451
  image = gtk_image_new_from_icon_name (GIMP_ICON_FONT,
                                        GTK_ICON_SIZE_BUTTON);
452 453
  gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);

454 455
  priv->label = gtk_label_new (priv->font_name);
  gtk_box_pack_start (GTK_BOX (hbox), priv->label, TRUE, TRUE, 4);
456

457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
  gtk_widget_show_all (button);

  g_signal_connect_swapped (button, "clicked",
                            G_CALLBACK (gimp_font_select_button_clicked),
                            font_button);

  gtk_drag_dest_set (GTK_WIDGET (button),
                     GTK_DEST_DEFAULT_HIGHLIGHT |
                     GTK_DEST_DEFAULT_MOTION |
                     GTK_DEST_DEFAULT_DROP,
                     &target, 1,
                     GDK_ACTION_COPY);

  g_signal_connect_swapped (button, "drag-data-received",
                            G_CALLBACK (gimp_font_select_drag_data_received),
                            font_button);
473 474 475

  gtk_widget_pop_composite_child ();

476
  return button;
477
}