gtkfontchooserdialog.c 8.22 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* GTK - The GIMP Toolkit
 * Copyright (C) 2011 Alberto Ruiz <aruiz@gnome.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 17 18 19 20 21 22 23 24 25 26 27
 */

#include "config.h"

#include <stdlib.h>
#include <glib/gprintf.h>
#include <string.h>

#include <atk/atk.h>

#include "gtkfontchooserdialog.h"
#include "gtkfontchooser.h"
28
#include "gtkfontchooserwidget.h"
29
#include "gtkfontchooserwidgetprivate.h"
30 31
#include "gtkfontchooserutils.h"
#include "gtkbox.h"
32
#include "deprecated/gtkstock.h"
33 34 35 36 37
#include "gtkintl.h"
#include "gtkaccessible.h"
#include "gtkbuildable.h"
#include "gtkprivate.h"
#include "gtkwidget.h"
38 39
#include "gtksettings.h"
#include "gtkdialogprivate.h"
40 41 42 43 44 45 46 47 48 49

struct _GtkFontChooserDialogPrivate
{
  GtkWidget *fontchooser;

  GtkWidget *select_button;
  GtkWidget *cancel_button;
};

/**
50 51
 * SECTION:gtkfontchooserdialog
 * @Short_description: A dialog for selecting fonts
52 53 54
 * @Title: GtkFontChooserDialog
 * @See_also: #GtkFontChooser, #GtkDialog
 *
55
 * The #GtkFontChooserDialog widget is a dialog for selecting a font.
Matthias Clasen's avatar
Matthias Clasen committed
56
 * It implements the #GtkFontChooser interface.
57
 *
58
 * # GtkFontChooserDialog as GtkBuildable
59 60
 *
 * The GtkFontChooserDialog implementation of the #GtkBuildable
61 62
 * interface exposes the buttons with the names “select_button”
 * and “cancel_button”.
63 64
 *
 * Since: 3.2
65 66
 */

67 68
static void     gtk_font_chooser_dialog_buildable_interface_init     (GtkBuildableIface *iface);
static GObject *gtk_font_chooser_dialog_buildable_get_internal_child (GtkBuildable *buildable,
69 70 71
                                                                      GtkBuilder   *builder,
                                                                      const gchar  *childname);

72
G_DEFINE_TYPE_WITH_CODE (GtkFontChooserDialog, gtk_font_chooser_dialog, GTK_TYPE_DIALOG,
73
                         G_ADD_PRIVATE (GtkFontChooserDialog)
74 75
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_FONT_CHOOSER,
                                                _gtk_font_chooser_delegate_iface_init)
76
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
77
                                                gtk_font_chooser_dialog_buildable_interface_init))
78 79 80

static GtkBuildableIface *parent_buildable_iface;

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
static void
gtk_font_chooser_dialog_set_property (GObject      *object,
                                      guint         prop_id,
                                      const GValue *value,
                                      GParamSpec   *pspec)
{
  GtkFontChooserDialog *dialog = GTK_FONT_CHOOSER_DIALOG (object);
  GtkFontChooserDialogPrivate *priv = dialog->priv;

  switch (prop_id)
    {
    default:
      g_object_set_property (G_OBJECT (priv->fontchooser), pspec->name, value);
      break;
    }
}

static void
gtk_font_chooser_dialog_get_property (GObject      *object,
                                      guint         prop_id,
                                      GValue       *value,
                                      GParamSpec   *pspec)
{
  GtkFontChooserDialog *dialog = GTK_FONT_CHOOSER_DIALOG (object);
  GtkFontChooserDialogPrivate *priv = dialog->priv;

  switch (prop_id)
    {
    default:
      g_object_get_property (G_OBJECT (priv->fontchooser), pspec->name, value);
      break;
    }
}

115 116 117 118 119 120 121 122 123 124
static void
font_activated_cb (GtkFontChooser *fontchooser,
                   const gchar    *fontname,
                   gpointer        user_data)
{
  GtkDialog *dialog = user_data;

  gtk_dialog_response (dialog, GTK_RESPONSE_OK);
}

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
static gboolean
gtk_font_chooser_dialog_key_press_event (GtkWidget   *dialog,
                                         GdkEventKey *event)
{
  GtkFontChooserDialog *fdialog = GTK_FONT_CHOOSER_DIALOG (dialog);
  gboolean handled = FALSE;

  handled = GTK_WIDGET_CLASS (gtk_font_chooser_dialog_parent_class)->key_press_event (dialog, event);

  if (!handled)
    handled = gtk_font_chooser_widget_handle_event (fdialog->priv->fontchooser, event);

  return handled;
}

140 141 142
static void
gtk_font_chooser_dialog_class_init (GtkFontChooserDialogClass *klass)
{
143
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
144
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
145 146 147 148

  gobject_class->get_property = gtk_font_chooser_dialog_get_property;
  gobject_class->set_property = gtk_font_chooser_dialog_set_property;

149 150
  widget_class->key_press_event = gtk_font_chooser_dialog_key_press_event;

151 152
  _gtk_font_chooser_install_properties (gobject_class);

153 154 155
  /* Bind class to template
   */
  gtk_widget_class_set_template_from_resource (widget_class,
156
					       "/org/gtk/libgtk/ui/gtkfontchooserdialog.ui");
157

158
  gtk_widget_class_bind_template_child_private (widget_class, GtkFontChooserDialog, fontchooser);
159
  gtk_widget_class_bind_template_callback (widget_class, font_activated_cb);
160 161
}

162 163 164 165 166 167 168 169 170 171 172 173 174 175
static void
update_button (GtkFontChooserDialog *dialog)
{
  GtkFontChooserDialogPrivate *priv = dialog->priv;
  PangoFontDescription *desc;

  desc = gtk_font_chooser_get_font_desc (GTK_FONT_CHOOSER (priv->fontchooser));

  gtk_widget_set_sensitive (priv->select_button, desc != NULL);

  if (desc)
    pango_font_description_free (desc);
}

176 177 178 179 180
static void
gtk_font_chooser_dialog_init (GtkFontChooserDialog *fontchooserdiag)
{
  GtkFontChooserDialogPrivate *priv;

181
  fontchooserdiag->priv = gtk_font_chooser_dialog_get_instance_private (fontchooserdiag);
182 183
  priv = fontchooserdiag->priv;

184
  gtk_widget_init_template (GTK_WIDGET (fontchooserdiag));
185
  gtk_dialog_set_use_header_bar_from_setting (GTK_DIALOG (fontchooserdiag));
186

187
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
188
  gtk_dialog_set_alternative_button_order (GTK_DIALOG (fontchooserdiag),
189 190 191
                                           GTK_RESPONSE_OK,
                                           GTK_RESPONSE_CANCEL,
                                           -1);
192
G_GNUC_END_IGNORE_DEPRECATIONS
193

194 195 196
  priv->select_button = gtk_dialog_get_widget_for_response (GTK_DIALOG (fontchooserdiag), GTK_RESPONSE_OK);
  priv->cancel_button = gtk_dialog_get_widget_for_response (GTK_DIALOG (fontchooserdiag), GTK_RESPONSE_CANCEL);

197 198
  _gtk_font_chooser_set_delegate (GTK_FONT_CHOOSER (fontchooserdiag),
                                  GTK_FONT_CHOOSER (priv->fontchooser));
199 200 201 202

  g_signal_connect_swapped (priv->fontchooser, "notify::font-desc",
                            G_CALLBACK (update_button), fontchooserdiag);
  update_button (fontchooserdiag);
203 204
}

Matthias Clasen's avatar
Matthias Clasen committed
205 206
/**
 * gtk_font_chooser_dialog_new:
207
 * @title: (allow-none): Title of the dialog, or %NULL
Matthias Clasen's avatar
Matthias Clasen committed
208
 * @parent: (allow-none): Transient parent of the dialog, or %NULL
209 210 211
 *
 * Creates a new #GtkFontChooserDialog.
 *
212
 * Returns: a new #GtkFontChooserDialog
213 214
 *
 * Since: 3.2
215 216
 */
GtkWidget*
217 218
gtk_font_chooser_dialog_new (const gchar *title,
                             GtkWindow   *parent)
219
{
220 221
  GtkFontChooserDialog *dialog;

222 223 224 225
  dialog = g_object_new (GTK_TYPE_FONT_CHOOSER_DIALOG,
                         "title", title,
                         "transient-for", parent,
                         NULL);
226 227

  return GTK_WIDGET (dialog);
228 229 230 231 232 233 234 235 236 237 238
}

static void
gtk_font_chooser_dialog_buildable_interface_init (GtkBuildableIface *iface)
{
  parent_buildable_iface = g_type_interface_peek_parent (iface);
  iface->get_internal_child = gtk_font_chooser_dialog_buildable_get_internal_child;
}

static GObject *
gtk_font_chooser_dialog_buildable_get_internal_child (GtkBuildable *buildable,
239 240
                                                      GtkBuilder   *builder,
                                                      const gchar  *childname)
241 242 243 244 245 246 247 248 249 250 251 252
{
  GtkFontChooserDialogPrivate *priv;

  priv = GTK_FONT_CHOOSER_DIALOG (buildable)->priv;

  if (g_strcmp0 (childname, "select_button") == 0)
    return G_OBJECT (priv->select_button);
  else if (g_strcmp0 (childname, "cancel_button") == 0)
    return G_OBJECT (priv->cancel_button);

  return parent_buildable_iface->get_internal_child (buildable, builder, childname);
}