font.c 2.9 KB
Newer Older
Victor Ananjesky's avatar
Victor Ananjesky committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * This file is part of YAD.
 *
 * YAD is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * YAD 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with YAD. If not, see <http://www.gnu.org/licenses/>.
 *
17
 * Copyright (C) 2008-2019, Victor Ananjevsky <ananasik@gmail.com>
Victor Ananjesky's avatar
Victor Ananjesky committed
18 19 20 21 22 23
 */

#include "yad.h"

static GtkWidget *font;

24 25 26 27 28 29 30
static void
font_activated_cb (GtkFontChooser *w, gchar *fn, gpointer d)
{
  if (options.plug == -1)
    yad_exit (options.data.def_resp);
}

Victor Ananjesky's avatar
Victor Ananjesky committed
31 32 33
static void
realize_cb (GtkWidget * w, gpointer d)
{
34
  gtk_font_chooser_set_font (GTK_FONT_CHOOSER (w), options.common_data.font);
Victor Ananjesky's avatar
Victor Ananjesky committed
35 36 37 38 39 40 41
}

GtkWidget *
font_create_widget (GtkWidget * dlg)
{
  GtkWidget *w;

42
  w = font = gtk_font_chooser_widget_new ();
Victor Ananjesky's avatar
Victor Ananjesky committed
43 44 45
  gtk_widget_set_name (w, "yad-font-widget");

  if (options.font_data.preview)
46
    gtk_font_chooser_set_preview_text (GTK_FONT_CHOOSER (w), options.font_data.preview);
Victor Ananjesky's avatar
Victor Ananjesky committed
47

48 49
  g_signal_connect (G_OBJECT (w), "font-activated", G_CALLBACK (font_activated_cb), NULL);

Victor Ananjesky's avatar
Victor Ananjesky committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
  /* font must be set after widget inserted in toplevel */
  if (options.common_data.font)
    g_signal_connect_after (G_OBJECT (w), "realize", G_CALLBACK (realize_cb), NULL);

  return w;
}

void
font_print_result (void)
{
  if (options.font_data.separate_output)
    {
      PangoFontFace *face;
      PangoFontFamily *family;
      gint size;

66 67 68
      face = gtk_font_chooser_get_font_face (GTK_FONT_CHOOSER (font));
      family = gtk_font_chooser_get_font_family (GTK_FONT_CHOOSER (font));
      size = gtk_font_chooser_get_font_size (GTK_FONT_CHOOSER (font));
Victor Ananjesky's avatar
Victor Ananjesky committed
69 70 71 72 73 74 75

      if (options.common_data.quoted_output)
        {
          gchar *q1 = g_shell_quote (pango_font_family_get_name (family));
          gchar *q2 = g_shell_quote (pango_font_face_get_face_name (face));

          g_printf ("%s%s%s%s%d\n", q1, options.common_data.separator, q2,
76
                    options.common_data.separator, size / PANGO_SCALE);
Victor Ananjesky's avatar
Victor Ananjesky committed
77 78 79 80 81 82 83

          g_free (q1);
          g_free (q2);
        }
      else
        {
          g_printf ("%s%s%s%s%d\n", pango_font_family_get_name (family), options.common_data.separator,
84
                    pango_font_face_get_face_name (face), options.common_data.separator, size / PANGO_SCALE);
Victor Ananjesky's avatar
Victor Ananjesky committed
85 86 87 88
        }
    }
  else
    {
89
      gchar *fn = gtk_font_chooser_get_font (GTK_FONT_CHOOSER (font));
Victor Ananjesky's avatar
Victor Ananjesky committed
90 91 92 93 94 95 96 97 98 99 100 101 102

      if (options.common_data.quoted_output)
        {
          gchar *buf = g_shell_quote (fn);
          g_printf ("%s\n", buf);
          g_free (buf);
        }
      else
        g_printf ("%s\n", fn);

      g_free (fn);
    }
}