gtkfontbutton.c 44.7 KB
Newer Older
1
/* 
Cody Russell's avatar
Cody Russell committed
2
 * GTK - The GIMP Toolkit
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * 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
Javier Jardón's avatar
Javier Jardón committed
19
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
20 21 22 23 24 25 26 27
 */
/*
 * 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/. 
 */

28
#include "config.h"
29 30 31 32

#include "gtkfontbutton.h"

#include "gtkmain.h"
Matthias Clasen's avatar
Matthias Clasen committed
33
#include "gtkbox.h"
34
#include "gtklabel.h"
35
#include "gtkfontchooser.h"
36
#include "gtkfontchooserdialog.h"
37 38
#include "gtkimage.h"
#include "gtkmarshalers.h"
39
#include "gtkseparator.h"
40
#include "gtkprivate.h"
41
#include "gtkintl.h"
42
#include "gtkcssprovider.h"
43 44 45

#include <string.h>
#include <stdio.h>
46
#include "gtkfontchooserutils.h"
47

Matthias Clasen's avatar
Matthias Clasen committed
48

49 50
/**
 * SECTION:gtkfontbutton
51
 * @Short_description: A button to launch a font chooser dialog
52
 * @Title: GtkFontButton
53
 * @See_also: #GtkFontChooserDialog, #GtkColorButton.
54 55
 *
 * The #GtkFontButton is a button which displays the currently selected
56
 * font an allows to open a font chooser dialog to change the font.
57
 * It is suitable widget for selecting a font in a preference dialog.
58 59 60 61
 *
 * # CSS nodes
 *
 * GtkFontButton has a single CSS node with name button and style class .font.
62 63 64
 */


65
struct _GtkFontButtonPrivate
66 67
{
  gchar         *title;
68

69
  gchar         *fontname;
70

71 72 73 74
  guint         use_font : 1;
  guint         use_size : 1;
  guint         show_style : 1;
  guint         show_size : 1;
75
  guint         show_preview_entry : 1;
76

77 78 79
  GtkWidget     *font_dialog;
  GtkWidget     *font_label;
  GtkWidget     *size_label;
80
  GtkWidget     *font_size_box;
81

82 83 84
  PangoFontDescription *font_desc;
  PangoFontFamily      *font_family;
  PangoFontFace        *font_face;
85
  PangoFontMap         *font_map;
86
  gint                  font_size;
87
  char                 *font_features;
88
  PangoLanguage        *language;
89 90 91 92
  gchar                *preview_text;
  GtkFontFilterFunc     font_filter;
  gpointer              font_filter_data;
  GDestroyNotify        font_filter_data_destroy;
93
  GtkCssProvider       *provider;
94 95

  GtkFontChooserLevel   level;
96 97 98
};

/* Signals */
99
enum
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
{
  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_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 */
130 131
static void response_cb                             (GtkDialog         *dialog,
                                                     gint               response_id,
132 133 134 135 136 137 138 139
                                                     gpointer           data);
static void dialog_destroy                          (GtkWidget         *widget,
                                                     gpointer           data);

/* Auxiliary functions */
static void gtk_font_button_label_use_font          (GtkFontButton     *gfs);
static void gtk_font_button_update_font_info        (GtkFontButton     *gfs);

140 141
static void        font_button_set_font_name (GtkFontButton *button,
                                              const char    *fontname);
142 143 144 145
static void        gtk_font_button_set_level     (GtkFontButton       *font_button,
                                                  GtkFontChooserLevel  level);
static void        gtk_font_button_set_language  (GtkFontButton *button,
                                                  const char    *language);
146

147 148
static guint font_button_signals[LAST_SIGNAL] = { 0 };

149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
static void
clear_font_data (GtkFontButton *font_button)
{
  GtkFontButtonPrivate *priv = font_button->priv;

  if (priv->font_family)
    g_object_unref (priv->font_family);
  priv->font_family = NULL;

  if (priv->font_face)
    g_object_unref (priv->font_face);
  priv->font_face = NULL;

  if (priv->font_desc)
    pango_font_description_free (priv->font_desc);
  priv->font_desc = NULL;

  g_free (priv->fontname);
  priv->fontname = NULL;
168 169 170

  g_free (priv->font_features);
  priv->font_features = NULL;
171 172
}

173 174 175 176 177 178 179 180 181 182 183 184
static void
clear_font_filter_data (GtkFontButton *font_button)
{
  GtkFontButtonPrivate *priv = font_button->priv;

  if (priv->font_filter_data_destroy)
    priv->font_filter_data_destroy (priv->font_filter_data);
  priv->font_filter = NULL;
  priv->font_filter_data = NULL;
  priv->font_filter_data_destroy = NULL;
}

185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
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_data (GtkFontButton *font_button)
{
  GtkFontButtonPrivate *priv = font_button->priv;
  PangoFontFamily **families;
  PangoFontFace **faces;
  gint n_families, n_faces, i;
  const gchar *family;

  g_assert (priv->font_desc != NULL);

  priv->fontname = pango_font_description_to_string (priv->font_desc);

  family = pango_font_description_get_family (priv->font_desc);
  if (family == NULL)
    return;

  n_families = 0;
  families = NULL;
  pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (font_button)),
                               &families, &n_families);
  n_faces = 0;
  faces = NULL;
  for (i = 0; i < n_families; i++)
    {
      const gchar *name = pango_font_family_get_name (families[i]);

      if (!g_ascii_strcasecmp (name, family))
        {
          priv->font_family = g_object_ref (families[i]);

          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, priv->font_desc))
        {
          priv->font_face = g_object_ref (faces[i]);

          pango_font_description_free (tmp_desc);
          break;
        }
      else
        pango_font_description_free (tmp_desc);
    }

  g_free (faces);
}

250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
static gchar *
gtk_font_button_get_preview_text (GtkFontButton *font_button)
{
  GtkFontButtonPrivate *priv = font_button->priv;

  if (priv->font_dialog)
    return gtk_font_chooser_get_preview_text (GTK_FONT_CHOOSER (priv->font_dialog));

  return g_strdup (priv->preview_text);
}

static void
gtk_font_button_set_preview_text (GtkFontButton *font_button,
                                  const gchar   *preview_text)
{
  GtkFontButtonPrivate *priv = font_button->priv;

  if (priv->font_dialog)
    {
      gtk_font_chooser_set_preview_text (GTK_FONT_CHOOSER (priv->font_dialog),
                                         preview_text);
      return;
    }

  g_free (priv->preview_text);
  priv->preview_text = g_strdup (preview_text);
}


static gboolean
gtk_font_button_get_show_preview_entry (GtkFontButton *font_button)
{
  GtkFontButtonPrivate *priv = font_button->priv;

  if (priv->font_dialog)
    return gtk_font_chooser_get_show_preview_entry (GTK_FONT_CHOOSER (priv->font_dialog));

  return priv->show_preview_entry;
}

static void
gtk_font_button_set_show_preview_entry (GtkFontButton *font_button,
                                        gboolean       show)
{
  GtkFontButtonPrivate *priv = font_button->priv;

296 297 298 299 300 301 302 303 304
  show = show != FALSE;

  if (priv->show_preview_entry != show)
    {
      priv->show_preview_entry = show;
      if (priv->font_dialog)
        gtk_font_chooser_set_show_preview_entry (GTK_FONT_CHOOSER (priv->font_dialog), show);
      g_object_notify (G_OBJECT (font_button), "show-preview-entry");
    }
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
}

static PangoFontFamily *
gtk_font_button_font_chooser_get_font_family (GtkFontChooser *chooser)
{
  GtkFontButton *font_button = GTK_FONT_BUTTON (chooser);
  GtkFontButtonPrivate *priv = font_button->priv;

  return priv->font_family;
}

static PangoFontFace *
gtk_font_button_font_chooser_get_font_face (GtkFontChooser *chooser)
{
  GtkFontButton *font_button = GTK_FONT_BUTTON (chooser);
  GtkFontButtonPrivate *priv = font_button->priv;

  return priv->font_face;
}

static int
gtk_font_button_font_chooser_get_font_size (GtkFontChooser *chooser)
{
  GtkFontButton *font_button = GTK_FONT_BUTTON (chooser);
  GtkFontButtonPrivate *priv = font_button->priv;

  return priv->font_size;
}

static void
gtk_font_button_font_chooser_set_filter_func (GtkFontChooser    *chooser,
                                              GtkFontFilterFunc  filter_func,
                                              gpointer           filter_data,
                                              GDestroyNotify     data_destroy)
{
  GtkFontButton *font_button = GTK_FONT_BUTTON (chooser);
  GtkFontButtonPrivate *priv = font_button->priv;

  if (priv->font_dialog)
    {
      gtk_font_chooser_set_filter_func (GTK_FONT_CHOOSER (priv->font_dialog),
                                        filter_func,
                                        filter_data,
                                        data_destroy);
      return;
    }

  clear_font_filter_data (font_button);
  priv->font_filter = filter_func;
  priv->font_filter_data = filter_data;
  priv->font_filter_data_destroy = data_destroy;
}

358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
static void
gtk_font_button_take_font_desc (GtkFontButton        *font_button,
                                PangoFontDescription *font_desc)
{
  GtkFontButtonPrivate *priv = font_button->priv;
  GObject *object = G_OBJECT (font_button);

  if (priv->font_desc && font_desc &&
      pango_font_description_equal (priv->font_desc, font_desc))
    {
      pango_font_description_free (font_desc);
      return;
    }

  g_object_freeze_notify (object);

374 375
  clear_font_data (font_button);

376 377 378 379 380
  if (font_desc)
    priv->font_desc = font_desc; /* adopted */
  else
    priv->font_desc = pango_font_description_from_string (_("Sans 12"));

381 382
  if (pango_font_description_get_size_is_absolute (priv->font_desc))
    priv->font_size = pango_font_description_get_size (priv->font_desc);
383
  else
384 385
    priv->font_size = pango_font_description_get_size (priv->font_desc) / PANGO_SCALE;

386
  gtk_font_button_update_font_data (font_button);
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
  gtk_font_button_update_font_info (font_button);

  if (priv->font_dialog)
    gtk_font_chooser_set_font_desc (GTK_FONT_CHOOSER (priv->font_dialog),
                                    priv->font_desc);

  g_object_notify (G_OBJECT (font_button), "font");
  g_object_notify (G_OBJECT (font_button), "font-desc");
  g_object_notify (G_OBJECT (font_button), "font-name");

  g_object_thaw_notify (object);
}

static const PangoFontDescription *
gtk_font_button_get_font_desc (GtkFontButton *font_button)
{
  return font_button->priv->font_desc;
}

406
static void
407 408
gtk_font_button_font_chooser_set_font_map (GtkFontChooser *chooser,
                                           PangoFontMap   *font_map)
409
{
410 411
  GtkFontButton *font_button = GTK_FONT_BUTTON (chooser);

412 413 414 415 416 417 418 419 420 421 422 423
  if (g_set_object (&font_button->priv->font_map, font_map))
    {
      PangoContext *context;

      if (!font_map)
        font_map = pango_cairo_font_map_get_default ();

      context = gtk_widget_get_pango_context (font_button->priv->font_label);
      pango_context_set_font_map (context, font_map);
    }
}

424 425 426 427 428 429 430 431
static PangoFontMap *
gtk_font_button_font_chooser_get_font_map (GtkFontChooser *chooser)
{
  GtkFontButton *font_button = GTK_FONT_BUTTON (chooser);

  return font_button->priv->font_map;
}

432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
static void
gtk_font_button_font_chooser_notify (GObject    *object,
                                     GParamSpec *pspec,
                                     gpointer    user_data)
{
  /* We do not forward the notification of the "font" property to the dialog! */
  if (pspec->name == I_("preview-text") ||
      pspec->name == I_("show-preview-entry"))
    g_object_notify_by_pspec (user_data, pspec);
}

static void
gtk_font_button_font_chooser_iface_init (GtkFontChooserIface *iface)
{
  iface->get_font_family = gtk_font_button_font_chooser_get_font_family;
  iface->get_font_face = gtk_font_button_font_chooser_get_font_face;
  iface->get_font_size = gtk_font_button_font_chooser_get_font_size;
  iface->set_filter_func = gtk_font_button_font_chooser_set_filter_func;
450 451
  iface->set_font_map = gtk_font_button_font_chooser_set_font_map;
  iface->get_font_map = gtk_font_button_font_chooser_get_font_map;
452 453 454
}

G_DEFINE_TYPE_WITH_CODE (GtkFontButton, gtk_font_button, GTK_TYPE_BUTTON,
455
                         G_ADD_PRIVATE (GtkFontButton)
456 457
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_FONT_CHOOSER,
                                                gtk_font_button_font_chooser_iface_init))
458 459 460 461 462

static void
gtk_font_button_class_init (GtkFontButtonClass *klass)
{
  GObjectClass *gobject_class;
463
  GtkWidgetClass *widget_class;
464 465 466
  GtkButtonClass *button_class;
  
  gobject_class = (GObjectClass *) klass;
467
  widget_class = (GtkWidgetClass *) klass;
468 469 470 471 472 473 474 475 476 477
  button_class = (GtkButtonClass *) 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;

478 479
  _gtk_font_chooser_install_properties (gobject_class);

480 481 482
  /**
   * GtkFontButton:title:
   * 
483
   * The title of the font chooser dialog.
484 485 486 487 488 489
   *
   * Since: 2.4
   */
  g_object_class_install_property (gobject_class,
                                   PROP_TITLE,
                                   g_param_spec_string ("title",
490
                                                        P_("Title"),
491
                                                        P_("The title of the font chooser dialog"),
492
                                                        _("Pick a Font"),
493
                                                        GTK_PARAM_READWRITE));
494 495 496 497 498 499 500

  /**
   * GtkFontButton:font-name:
   * 
   * The name of the currently selected font.
   *
   * Since: 2.4
501 502
   *
   * Deprecated: 3.22: Use the #GtkFontChooser::font property instead
503 504 505
   */
  g_object_class_install_property (gobject_class,
                                   PROP_FONT_NAME,
506
                                   g_param_spec_string ("font-name",
507 508
                                                        P_("Font name"),
                                                        P_("The name of the selected font"),
509
                                                        _("Sans 12"),
510
                                                        GTK_PARAM_READWRITE | G_PARAM_DEPRECATED));
511 512 513 514

  /**
   * GtkFontButton:use-font:
   * 
Matthias Clasen's avatar
Matthias Clasen committed
515 516
   * If this property is set to %TRUE, the label will be drawn 
   * in the selected font.
517 518 519 520 521
   *
   * Since: 2.4
   */
  g_object_class_install_property (gobject_class,
                                   PROP_USE_FONT,
522
                                   g_param_spec_boolean ("use-font",
523 524
                                                         P_("Use font in label"),
                                                         P_("Whether the label is drawn in the selected font"),
525
                                                         FALSE,
526
                                                         GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
527 528 529 530

  /**
   * GtkFontButton:use-size:
   * 
Matthias Clasen's avatar
Matthias Clasen committed
531 532
   * If this property is set to %TRUE, the label will be drawn 
   * with the selected font size.
533 534 535 536 537
   *
   * Since: 2.4
   */
  g_object_class_install_property (gobject_class,
                                   PROP_USE_SIZE,
538
                                   g_param_spec_boolean ("use-size",
539 540
                                                         P_("Use size in label"),
                                                         P_("Whether the label is drawn with the selected font size"),
541
                                                         FALSE,
542
                                                         GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
543 544 545 546

  /**
   * GtkFontButton:show-style:
   * 
Matthias Clasen's avatar
Matthias Clasen committed
547 548 549
   * If this property is set to %TRUE, the name of the selected font style 
   * will be shown in the label. For a more WYSIWYG way to show the selected 
   * style, see the ::use-font property. 
550 551 552 553
   *
   * Since: 2.4
   */
  g_object_class_install_property (gobject_class,
554
                                   PROP_SHOW_STYLE,
555
                                   g_param_spec_boolean ("show-style",
556 557
                                                         P_("Show style"),
                                                         P_("Whether the selected font style is shown in the label"),
558
                                                         TRUE,
559
                                                         GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
560 561 562
  /**
   * GtkFontButton:show-size:
   * 
Matthias Clasen's avatar
Matthias Clasen committed
563 564 565
   * If this property is set to %TRUE, the selected font size will be shown 
   * in the label. For a more WYSIWYG way to show the selected size, see the 
   * ::use-size property. 
566 567 568 569 570
   *
   * Since: 2.4
   */
  g_object_class_install_property (gobject_class,
                                   PROP_SHOW_SIZE,
571
                                   g_param_spec_boolean ("show-size",
572 573
                                                         P_("Show size"),
                                                         P_("Whether selected font size is shown in the label"),
574
                                                         TRUE,
575
                                                         GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
576 577 578 579 580

  /**
   * GtkFontButton::font-set:
   * @widget: the object which received the signal.
   * 
581
   * The ::font-set signal is emitted when the user selects a font. 
582
   * When handling this signal, use gtk_font_chooser_get_font()
583 584
   * to find out which font was just selected.
   *
585
   * Note that this signal is only emitted when the user
586
   * changes the font. If you need to react to programmatic font changes
587
   * as well, use the notify::font signal.
588 589 590
   *
   * Since: 2.4
   */
Matthias Clasen's avatar
Matthias Clasen committed
591
  font_button_signals[FONT_SET] = g_signal_new (I_("font-set"),
592 593 594 595
                                                G_TYPE_FROM_CLASS (gobject_class),
                                                G_SIGNAL_RUN_FIRST,
                                                G_STRUCT_OFFSET (GtkFontButtonClass, font_set),
                                                NULL, NULL,
596
                                                NULL,
597
                                                G_TYPE_NONE, 0);
598 599 600

  /* Bind class to template
   */
601
  gtk_widget_class_set_template_from_resource (widget_class, "/org/gtk/libgtk/ui/gtkfontbutton.ui");
602 603 604
  gtk_widget_class_bind_template_child_private (widget_class, GtkFontButton, font_label);
  gtk_widget_class_bind_template_child_private (widget_class, GtkFontButton, size_label);
  gtk_widget_class_bind_template_child_private (widget_class, GtkFontButton, font_size_box);
605 606

  gtk_widget_class_set_css_name (widget_class, "button");
607 608 609 610 611
}

static void
gtk_font_button_init (GtkFontButton *font_button)
{
612 613
  GtkStyleContext *context;

614
  font_button->priv = gtk_font_button_get_instance_private (font_button);
615 616 617 618 619 620

  /* Initialize fields */
  font_button->priv->use_font = FALSE;
  font_button->priv->use_size = FALSE;
  font_button->priv->show_style = TRUE;
  font_button->priv->show_size = TRUE;
621
  font_button->priv->show_preview_entry = TRUE;
622
  font_button->priv->font_dialog = NULL;
623 624 625
  font_button->priv->font_family = NULL;
  font_button->priv->font_face = NULL;
  font_button->priv->font_size = -1;
626
  font_button->priv->title = g_strdup (_("Pick a Font"));
627 628 629
  font_button->priv->level = GTK_FONT_CHOOSER_LEVEL_FAMILY |
                             GTK_FONT_CHOOSER_LEVEL_STYLE |
                             GTK_FONT_CHOOSER_LEVEL_SIZE;
630
  font_button->priv->language = pango_language_get_default ();
631

632
  gtk_widget_init_template (GTK_WIDGET (font_button));
633

634
  gtk_font_button_take_font_desc (font_button, NULL);
635 636 637

  context = gtk_widget_get_style_context (GTK_WIDGET (font_button));
  gtk_style_context_add_class (context, "font");
638 639 640 641 642 643
}

static void
gtk_font_button_finalize (GObject *object)
{
  GtkFontButton *font_button = GTK_FONT_BUTTON (object);
644
  GtkFontButtonPrivate *priv = font_button->priv;
645

646 647
  if (priv->font_dialog != NULL) 
    gtk_widget_destroy (priv->font_dialog);
648

649
  g_free (priv->title);
650

651
  clear_font_data (font_button);
652 653
  clear_font_filter_data (font_button);

654 655 656
  g_free (priv->preview_text);

  g_clear_object (&priv->provider);
657

Matthias Clasen's avatar
Matthias Clasen committed
658
  G_OBJECT_CLASS (gtk_font_button_parent_class)->finalize (object);
659 660 661 662 663 664 665 666 667
}

static void
gtk_font_button_set_property (GObject      *object,
                              guint         param_id,
                              const GValue *value,
                              GParamSpec   *pspec)
{
  GtkFontButton *font_button = GTK_FONT_BUTTON (object);
668

669 670
  switch (param_id) 
    {
671 672 673 674 675 676
    case GTK_FONT_CHOOSER_PROP_PREVIEW_TEXT:
      gtk_font_button_set_preview_text (font_button, g_value_get_string (value));
      break;
    case GTK_FONT_CHOOSER_PROP_SHOW_PREVIEW_ENTRY:
      gtk_font_button_set_show_preview_entry (font_button, g_value_get_boolean (value));
      break;
677 678 679
    case PROP_TITLE:
      gtk_font_button_set_title (font_button, g_value_get_string (value));
      break;
680 681 682
    case GTK_FONT_CHOOSER_PROP_FONT_DESC:
      gtk_font_button_take_font_desc (font_button, g_value_dup_boxed (value));
      break;
683 684 685
    case GTK_FONT_CHOOSER_PROP_LANGUAGE:
      gtk_font_button_set_language (font_button, g_value_get_string (value));
      break;
686 687 688
    case GTK_FONT_CHOOSER_PROP_LEVEL:
      gtk_font_button_set_level (font_button, g_value_get_flags (value));
      break;
689
    case GTK_FONT_CHOOSER_PROP_FONT:
690
    case PROP_FONT_NAME:
691
      font_button_set_font_name (font_button, g_value_get_string (value));
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
      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);
718
  GtkFontButtonPrivate *priv = font_button->priv;
719 720 721
  
  switch (param_id) 
    {
722 723 724 725 726 727
    case GTK_FONT_CHOOSER_PROP_PREVIEW_TEXT:
      g_value_set_string (value, gtk_font_button_get_preview_text (font_button));
      break;
    case GTK_FONT_CHOOSER_PROP_SHOW_PREVIEW_ENTRY:
      g_value_set_boolean (value, gtk_font_button_get_show_preview_entry (font_button));
      break;
728 729 730
    case PROP_TITLE:
      g_value_set_string (value, gtk_font_button_get_title (font_button));
      break;
731 732 733
    case GTK_FONT_CHOOSER_PROP_FONT_DESC:
      g_value_set_boxed (value, gtk_font_button_get_font_desc (font_button));
      break;
734 735 736 737
    case GTK_FONT_CHOOSER_PROP_FONT_FEATURES:
      g_value_set_string (value, priv->font_features);
      break;
    case GTK_FONT_CHOOSER_PROP_LANGUAGE:
738
      g_value_set_string (value, pango_language_to_string (priv->language));
739
      break;
740 741 742
    case GTK_FONT_CHOOSER_PROP_LEVEL:
      g_value_set_flags (value, priv->level);
      break;
743
    case GTK_FONT_CHOOSER_PROP_FONT:
744
    case PROP_FONT_NAME:
745
      g_value_set_string (value, font_button->priv->fontname);
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
      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;
    }
763
}
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778


/**
 * 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);
779
}
780 781 782

/**
 * gtk_font_button_new_with_font:
783
 * @fontname: Name of font to display in font chooser dialog
784 785 786 787 788 789 790 791 792 793
 *
 * 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)
{
794
  return g_object_new (GTK_TYPE_FONT_BUTTON, "font", fontname, NULL);
795 796 797 798 799
} 

/**
 * gtk_font_button_set_title:
 * @font_button: a #GtkFontButton
800
 * @title: a string containing the font chooser dialog title
801
 *
802
 * Sets the title for the font chooser dialog.  
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
 *
 * 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
 *
828
 * Retrieves the title of the font chooser dialog.
829 830 831 832 833
 *
 * Returns: an internal copy of the title string which must not be freed.
 *
 * Since: 2.4
 */
834
const gchar*
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880
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;

881
      gtk_font_button_label_use_font (font_button);
882
 
883
      g_object_notify (G_OBJECT (font_button), "use-font");
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
    }
} 


/**
 * 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,
917
                              gboolean       use_size)
918 919 920 921 922 923 924 925
{
  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;

926
      gtk_font_button_label_use_font (font_button);
927

928
      g_object_notify (G_OBJECT (font_button), "use-size");
929 930 931 932 933 934 935 936 937
    }
} 

/**
 * gtk_font_button_get_show_style:
 * @font_button: a #GtkFontButton
 * 
 * Returns whether the name of the font style will be shown in the label.
 * 
938
 * Returns: whether the font style will be shown in the label.
939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
 *
 * 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);
  
972
      g_object_notify (G_OBJECT (font_button), "show-style");
973 974 975 976 977 978 979 980 981 982
    }
} 


/**
 * gtk_font_button_get_show_size:
 * @font_button: a #GtkFontButton
 * 
 * Returns whether the font size will be shown in the label.
 * 
983
 * Returns: whether the font size will be shown in the label.
984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
 *
 * 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;

1016 1017 1018 1019
      if (font_button->priv->show_size)
	gtk_widget_show (font_button->priv->font_size_box);
      else
	gtk_widget_hide (font_button->priv->font_size_box);
1020 1021 1022
      
      gtk_font_button_update_font_info (font_button);

1023
      g_object_notify (G_OBJECT (font_button), "show-size");
1024 1025 1026 1027 1028 1029 1030 1031
    }
} 


/**
 * gtk_font_button_get_font_name:
 * @font_button: a #GtkFontButton
 *
1032 1033 1034
 * Retrieves the name of the currently selected font. This name includes
 * style and size information as well. If you want to render something
 * with the font, use this string with pango_font_description_from_string() .
1035
 * If you’re interested in peeking certain values (family name,
1036 1037
 * style, size, weight) just query these properties from the
 * #PangoFontDescription object.
1038 1039 1040 1041
 *
 * Returns: an internal copy of the font name which must not be freed.
 *
 * Since: 2.4
1042
 * Deprecated: 3.22: Use gtk_font_chooser_get_font() instead
1043
 */
1044
const gchar *
1045 1046 1047
gtk_font_button_get_font_name (GtkFontButton *font_button)
{
  g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), NULL);
1048

1049 1050 1051
  return font_button->priv->fontname;
}

1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
static void
font_button_set_font_name (GtkFontButton *font_button,
                           const char    *fontname)
{
  PangoFontDescription *font_desc;

  font_desc = pango_font_description_from_string (fontname);
  gtk_font_button_take_font_desc (font_button, font_desc);
}

1062 1063 1064
/**
 * gtk_font_button_set_font_name:
 * @font_button: a #GtkFontButton
1065
 * @fontname: Name of font to display in font chooser dialog
1066 1067 1068
 *
 * Sets or updates the currently-displayed font in font picker dialog.
 *
1069
 * Returns: %TRUE
1070 1071
 *
 * Since: 2.4
1072
 * Deprecated: 3.22: Use gtk_font_chooser_set_font() instead
1073 1074 1075 1076 1077 1078 1079 1080
 */
gboolean 
gtk_font_button_set_font_name (GtkFontButton *font_button,
                               const gchar    *fontname)
{
  g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
  g_return_val_if_fail (fontname != NULL, FALSE);

1081
  font_button_set_font_name (font_button, fontname);
1082

1083
  return TRUE;
1084 1085 1086 1087 1088
}

static void
gtk_font_button_clicked (GtkButton *button)
{
1089 1090
  GtkFontChooser *font_dialog;
  GtkFontButton  *font_button = GTK_FONT_BUTTON (button);
1091
  GtkFontButtonPrivate *priv = font_button->priv;
1092 1093 1094 1095 1096 1097
  
  if (!font_button->priv->font_dialog) 
    {
      GtkWidget *parent;
      
      parent = gtk_widget_get_toplevel (GTK_WIDGET (font_button));
1098 1099

      priv->font_dialog = gtk_font_chooser_dialog_new (priv->title, NULL);
1100
      font_dialog = GTK_FONT_CHOOSER (font_button->priv->font_dialog);
1101

1102 1103
      if (priv->font_map)
        gtk_font_chooser_set_font_map (font_dialog, priv->font_map);
1104
      gtk_font_chooser_set_show_preview_entry (font_dialog, priv->show_preview_entry);
1105
      gtk_font_chooser_set_level (GTK_FONT_CHOOSER (font_dialog), priv->level);
1106 1107
      gtk_font_chooser_set_language (GTK_FONT_CHOOSER (font_dialog), pango_language_to_string 
(priv->language));
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126

      if (priv->preview_text)
        {
          gtk_font_chooser_set_preview_text (font_dialog, priv->preview_text);
          g_free (priv->preview_text);
          priv->preview_text = NULL;
        }

      if (priv->font_filter)
        {
          gtk_font_chooser_set_filter_func (font_dialog,
                                            priv->font_filter,
                                            priv->font_filter_data,
                                            priv->font_filter_data_destroy);
          priv->font_filter = NULL;
          priv->font_filter_data = NULL;
          priv->font_filter_data_destroy = NULL;
        }

1127
      if (gtk_widget_is_toplevel (parent) && GTK_IS_WINDOW (parent))
1128 1129
        {
          if (GTK_WINDOW (parent) != gtk_window_get_transient_for (GTK_WINDOW (font_dialog)))
1130 1131 1132 1133 1134 1135
            gtk_window_set_transient_for (GTK_WINDOW (font_dialog), GTK_WINDOW (parent));

          gtk_window_set_modal (GTK_WINDOW (font_dialog),
                                gtk_window_get_modal (GTK_WINDOW (parent)));
        }

1136 1137 1138
      g_signal_connect (font_dialog, "notify",
                        G_CALLBACK (gtk_font_button_font_chooser_notify), button);

1139 1140 1141
      g_signal_connect (font_dialog, "response",
                        G_CALLBACK (response_cb), font_button);

1142 1143
      g_signal_connect (font_dialog, "destroy",
                        G_CALLBACK (dialog_destroy), font_button);
1144 1145 1146

      g_signal_connect (font_dialog, "delete-event",
                        G_CALLBACK (gtk_widget_hide_on_delete), NULL);
1147 1148
    }
  
1149
  if (!gtk_widget_get_visible (font_button->priv->font_dialog))
1150
    {
1151
      font_dialog = GTK_FONT_CHOOSER (font_button->priv->font_dialog);
1152
      gtk_font_chooser_set_font_desc (font_dialog, font_button->priv->font_desc);
1153 1154
    } 

1155
  G_GNUC_BEGIN_IGNORE_DEPRECATIONS
1156
  gtk_window_present (GTK_WINDOW (font_button->priv->font_dialog));
1157
  G_GNUC_END_IGNORE_DEPRECATIONS
1158 1159
}

1160

1161
static void
1162 1163 1164
response_cb (GtkDialog *dialog,
             gint       response_id,
             gpointer   data)
1165 1166
{
  GtkFontButton *font_button = GTK_FONT_BUTTON (data);
1167 1168
  GtkFontButtonPrivate *priv = font_button->priv;
  GtkFontChooser *font_chooser;
1169
  GObject *object;
1170

1171
  gtk_widget_hide (font_button->priv->font_dialog);
1172 1173 1174 1175

  if (response_id != GTK_RESPONSE_OK)
    return;

1176
  font_chooser = GTK_FONT_CHOOSER (priv->font_dialog);
1177 1178 1179 1180
  object = G_OBJECT (font_chooser);

  g_object_freeze_notify (object);

1181
  clear_font_data (font_button);
1182

1183 1184 1185
  priv->font_desc = gtk_font_chooser_get_font_desc (font_chooser);
  if (priv->font_desc)
    priv->fontname = pango_font_description_to_string (priv->font_desc);
1186
  priv->font_family = gtk_font_chooser_get_font_family (font_chooser);
1187 1188
  if (priv->font_family)
    g_object_ref (priv->font_family);
1189
  priv->font_face = gtk_font_chooser_get_font_face (font_chooser);
1190 1191
  if (priv->font_face)
    g_object_ref (priv->font_face);
1192
  priv->font_size = gtk_font_chooser_get_font_size (font_chooser);
1193
  g_free (priv->font_features);
1194 1195
  priv->font_features = gtk_font_chooser_get_font_features (font_chooser);
  priv->language = pango_language_from_string (gtk_font_chooser_get_language (font_chooser));
1196

1197 1198 1199
  /* Set label font */
  gtk_font_button_update_font_info (font_button);

1200
  g_object_notify (G_OBJECT (font_button), "font");
1201
  g_object_notify (G_OBJECT (font_button), "font-desc");
1202
  g_object_notify (G_OBJECT (font_button), "font-name");
1203
  g_object_notify (G_OBJECT (font_button), "font-features");
1204 1205 1206

  g_object_thaw_notify (object);

1207 1208 1209 1210 1211 1212
  /* Emit font_set signal */
  g_signal_emit (font_button, font_button_signals[FONT_SET], 0);
}

static void
dialog_destroy (GtkWidget *widget,
1213
                gpointer   data)
1214 1215 1216 1217 1218 1219 1220
{
  GtkFontButton *font_button = GTK_FONT_BUTTON (data);
    
  /* Dialog will get destroyed so reference is not valid now */
  font_button->priv->font_dialog = NULL;
} 

1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
static gchar *
pango_font_description_to_css (PangoFontDescription *desc)
{
  GString *s;
  PangoFontMask set;

  s = g_string_new ("* { ");

  set = pango_font_description_get_set_fields (desc);
  if (set & PANGO_FONT_MASK_FAMILY)
    {
      g_string_append (s, "font-family: ");
      g_string_append (s, pango_font_description_get_family (desc));
      g_string_append (s, "; ");
    }
  if (set & PANGO_FONT_MASK_STYLE)
    {
      switch (pango_font_description_get_style (desc))
        {
        case PANGO_STYLE_NORMAL:
          g_string_append (s, "font-style: normal; ");
          break;
        case PANGO_STYLE_OBLIQUE:
          g_string_append (s, "font-style: oblique; ");
          break;
        case PANGO_STYLE_ITALIC:
          g_string_append (s, "font-style: italic; ");
          break;
        }
    }
  if (set & PANGO_FONT_MASK_VARIANT)
    {
      switch (pango_font_description_get_variant (desc))
        {
        case PANGO_VARIANT_NORMAL:
          g_string_append (s, "font-variant: normal; ");
          break;
        case PANGO_VARIANT_SMALL_CAPS:
          g_string_append (s, "font-variant: small-caps; ");
          break;
        }
    }
  if (set & PANGO_FONT_MASK_WEIGHT)
    {
      switch (pango_font_description_get_weight (desc))
        {
        case PANGO_WEIGHT_THIN:
          g_string_append (s, "font-weight: 100; ");
          break;
        case PANGO_WEIGHT_ULTRALIGHT:
          g_string_append (s, "font-weight: 200; ");
          break;
        case PANGO_WEIGHT_LIGHT:
        case PANGO_WEIGHT_SEMILIGHT:
          g_string_append (s, "font-weight: 300; ");
          break;
        case PANGO_WEIGHT_BOOK:
        case PANGO_WEIGHT_NORMAL:
          g_string_append (s, "font-weight: 400; ");
          break;
        case PANGO_WEIGHT_MEDIUM:
          g_string_append (s, "font-weight: 500; ");
          break;
        case PANGO_WEIGHT_SEMIBOLD:
          g_string_append (s, "font-weight: 600; ");
          break;
        case PANGO_WEIGHT_BOLD:
          g_string_append (s, "font-weight: 700; ");
          break;
        case PANGO_WEIGHT_ULTRABOLD:
          g_string_append (s, "font-weight: 800; ");
          break;
        case PANGO_WEIGHT_HEAVY:
        case PANGO_WEIGHT_ULTRAHEAVY:
          g_string_append (s, "font-weight: 900; ");
          break;
        }
    }
  if (set & PANGO_FONT_MASK_STRETCH)
    {
      switch (pango_font_description_get_stretch (desc))
        {
        case PANGO_STRETCH_ULTRA_CONDENSED:
          g_string_append (s, "font-stretch: ultra-condensed; ");
          break;
        case PANGO_STRETCH_EXTRA_CONDENSED:
          g_string_append (s, "font-stretch: extra-condensed; ");
          break;
        case PANGO_STRETCH_CONDENSED:
          g_string_append (s, "font-stretch: condensed; ");
          break;
        case PANGO_STRETCH_SEMI_CONDENSED:
          g_string_append (s, "font-stretch: semi-condensed; ");
          break;
        case PANGO_STRETCH_NORMAL:
          g_string_append (s, "font-stretch: normal; ");
          break;
        case PANGO_STRETCH_SEMI_EXPANDED:
          g_string_append (s, "font-stretch: semi-expanded; ");
          break;
        case PANGO_STRETCH_EXPANDED:
          g_string_append (s, "font-stretch: expanded; ");
          break;
        case PANGO_STRETCH_EXTRA_EXPANDED:
          g_string_append (s, "font-stretch: extra-expanded; ");
          break;
        case PANGO_STRETCH_ULTRA_EXPANDED:
          g_string_append (s, "font-stretch: ultra-expanded; ");
          break;
        }
    }
  if (set & PANGO_FONT_MASK_SIZE)
    {
      g_string_append_printf (s, "font-size: %dpt", pango_font_description_get_size (desc) / PANGO_SCALE);
    }

  g_string_append (s, "}");

  return g_string_free (s, FALSE);
}

1342 1343 1344
static void
gtk_font_button_label_use_font (GtkFontButton *font_button)
{
1345 1346
  GtkFontButtonPrivate *priv = font_button->priv;
  GtkStyleContext *context;
1347

1348
  context = gtk_widget_get_style_context (priv->font_label);
1349

1350 1351 1352 1353 1354 1355 1356
  if (!priv->use_font)
    {
      if (priv->provider)
        {
          gtk_style_context_remove_provider (context, GTK_STYLE_PROVIDER (priv->provider));
          g_clear_object (&priv->provider);
        }
1357 1358
    }
  else
1359 1360
    {
      PangoFontDescription *desc;
1361
      gchar *data;
1362 1363 1364 1365 1366 1367 1368 1369 1370 1371

      if (!priv->provider)
        {
          priv->provider = gtk_css_provider_new ();
          gtk_style_context_add_provider (context,
                                          GTK_STYLE_PROVIDER (priv->provider),
                                          GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
        }

      desc = pango_font_description_copy (priv->font_desc);
1372

1373 1374 1375
      if (!priv->use_size)
        pango_font_description_unset_fields (desc, PANGO_FONT_MASK_SIZE);

1376
      data = pango_font_description_to_css (desc);
1377
      gtk_css_provider_load_from_data (priv->provider, data, -1, NULL);
1378

1379 1380 1381
      g_free (data);
      pango_font_description_free (desc);
    }
1382 1383 1384 1385 1386
}

static void
gtk_font_button_update_font_info (GtkFontButton *font_button)
{
1387
  GtkFontButtonPrivate *priv = font_button->priv;
1388 1389
  const gchar *fam_name;
  const gchar *face_name;
1390
  gchar *family_style;
1391

1392 1393 1394
  if (priv->font_family)
    fam_name = pango_font_family_get_name (priv->font_family);
  else
1395
    fam_name = C_("font", "None");
1396 1397 1398 1399
  if (priv->font_face)
    face_name = pango_font_face_get_face_name (priv->font_face);
  else
    face_name = "";
1400

1401
  if (priv->show_style)
1402
    family_style = g_strconcat (fam_name, " ", face_name, NULL);
1403
  else
1404
    family_style = g_strdup (fam_name);
1405

1406 1407 1408 1409 1410
  gtk_label_set_text (GTK_LABEL (font_button->priv->font_label), family_style);
  g_free (family_style);

  if (font_button->priv->show_size) 
    {
1411
      /* mirror Pango, which doesn't translate this either */
1412
      gchar *size = g_strdup_printf ("%2.4g%s",
1413 1414
                                     pango_font_description_get_size (priv->font_desc) / (double)PANGO_SCALE,
                                     pango_font_description_get_size_is_absolute (priv->font_desc) ? "px" : "");
1415 1416 1417 1418 1419 1420 1421 1422
      
      gtk_label_set_text (GTK_LABEL (font_button->priv->size_label), size);
      
      g_free (size);
    }

  gtk_font_button_label_use_font (font_button);
} 
1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445

static void
gtk_font_button_set_level (GtkFontButton       *button,
                           GtkFontChooserLevel  level)
{
  GtkFontButtonPrivate *priv = button->priv;

  if (priv->level == level)
    return;

  priv->level = level;

  if (priv->font_dialog)
    g_object_set (priv->font_dialog, "level", level, NULL);

  g_object_set (button,
                "show-size", (level & GTK_FONT_CHOOSER_LEVEL_SIZE) != 0,
                "show-style", (level & GTK_FONT_CHOOSER_LEVEL_STYLE) != 0,
                NULL);

  g_object_notify (G_OBJECT (button), "level");
}

1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458
static void
gtk_font_button_set_language (GtkFontButton *button,
                              const char    *language)
{
  GtkFontButtonPrivate *priv = button->priv;

  priv->language = pango_language_from_string (language);

  if (priv->font_dialog)
    gtk_font_chooser_set_language (GTK_FONT_CHOOSER (priv->font_dialog), language);

  g_object_notify (G_OBJECT (button), "language");
}