]> Pileus Git - ~andy/gtk/blob - gtk/gtkfontchooserdialog.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / gtk / gtkfontchooserdialog.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2011 Alberto Ruiz <aruiz@gnome.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include "config.h"
19
20 #include <stdlib.h>
21 #include <glib/gprintf.h>
22 #include <string.h>
23
24 #include <atk/atk.h>
25
26 #include "gtkfontchooserdialog.h"
27 #include "gtkfontchooser.h"
28 #include "gtkfontchooserwidget.h"
29 #include "gtkfontchooserutils.h"
30 #include "gtkbox.h"
31 #include "gtkstock.h"
32 #include "gtkintl.h"
33 #include "gtkaccessible.h"
34 #include "gtkbuildable.h"
35 #include "gtkprivate.h"
36 #include "gtkwidget.h"
37
38 struct _GtkFontChooserDialogPrivate
39 {
40   GtkWidget *fontchooser;
41
42   GtkWidget *select_button;
43   GtkWidget *cancel_button;
44 };
45
46 /**
47  * SECTION:gtkfontchooserdialog
48  * @Short_description: A dialog for selecting fonts
49  * @Title: GtkFontChooserDialog
50  * @See_also: #GtkFontChooser, #GtkDialog
51  *
52  * The #GtkFontChooserDialog widget is a dialog for selecting a font.
53  * It implements the #GtkFontChooser interface.
54  *
55  * <refsect2 id="GtkFontChooserDialog-BUILDER-UI">
56  * <title>GtkFontChooserDialog as GtkBuildable</title>
57  * The GtkFontChooserDialog implementation of the GtkBuildable interface
58  * exposes the buttons with the names
59  * "select_button" and "cancel_button.
60  * </refsect2>
61  *
62  * Since: 3.2
63  */
64
65 static void     gtk_font_chooser_dialog_buildable_interface_init     (GtkBuildableIface *iface);
66 static GObject *gtk_font_chooser_dialog_buildable_get_internal_child (GtkBuildable *buildable,
67                                                                       GtkBuilder   *builder,
68                                                                       const gchar  *childname);
69
70 G_DEFINE_TYPE_WITH_CODE (GtkFontChooserDialog, gtk_font_chooser_dialog, GTK_TYPE_DIALOG,
71                          G_IMPLEMENT_INTERFACE (GTK_TYPE_FONT_CHOOSER,
72                                                 _gtk_font_chooser_delegate_iface_init)
73                          G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
74                                                 gtk_font_chooser_dialog_buildable_interface_init))
75
76 static GtkBuildableIface *parent_buildable_iface;
77
78 static void
79 gtk_font_chooser_dialog_set_property (GObject      *object,
80                                       guint         prop_id,
81                                       const GValue *value,
82                                       GParamSpec   *pspec)
83 {
84   GtkFontChooserDialog *dialog = GTK_FONT_CHOOSER_DIALOG (object);
85   GtkFontChooserDialogPrivate *priv = dialog->priv;
86
87   switch (prop_id)
88     {
89     default:
90       g_object_set_property (G_OBJECT (priv->fontchooser), pspec->name, value);
91       break;
92     }
93 }
94
95 static void
96 gtk_font_chooser_dialog_get_property (GObject      *object,
97                                       guint         prop_id,
98                                       GValue       *value,
99                                       GParamSpec   *pspec)
100 {
101   GtkFontChooserDialog *dialog = GTK_FONT_CHOOSER_DIALOG (object);
102   GtkFontChooserDialogPrivate *priv = dialog->priv;
103
104   switch (prop_id)
105     {
106     default:
107       g_object_get_property (G_OBJECT (priv->fontchooser), pspec->name, value);
108       break;
109     }
110 }
111
112 static void
113 gtk_font_chooser_dialog_class_init (GtkFontChooserDialogClass *klass)
114 {
115   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
116
117   gobject_class->get_property = gtk_font_chooser_dialog_get_property;
118   gobject_class->set_property = gtk_font_chooser_dialog_set_property;
119
120   _gtk_font_chooser_install_properties (gobject_class);
121
122   g_type_class_add_private (klass, sizeof (GtkFontChooserDialogPrivate));
123 }
124
125 static void
126 font_activated_cb (GtkFontChooser *fontchooser,
127                    const gchar    *fontname,
128                    gpointer        user_data)
129 {
130   GtkDialog *dialog = user_data;
131
132   gtk_dialog_response (dialog, GTK_RESPONSE_OK);
133 }
134
135 static void
136 gtk_font_chooser_dialog_init (GtkFontChooserDialog *fontchooserdiag)
137 {
138   GtkFontChooserDialogPrivate *priv;
139   GtkDialog *dialog = GTK_DIALOG (fontchooserdiag);
140   GtkWidget *action_area, *content_area;
141
142   fontchooserdiag->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontchooserdiag,
143                                                        GTK_TYPE_FONT_CHOOSER_DIALOG,
144                                                        GtkFontChooserDialogPrivate);
145   priv = fontchooserdiag->priv;
146
147   content_area = gtk_dialog_get_content_area (dialog);
148   action_area = gtk_dialog_get_action_area (dialog);
149
150   gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
151   gtk_box_set_spacing (GTK_BOX (content_area), 2); /* 2 * 5 + 2 = 12 */
152   gtk_container_set_border_width (GTK_CONTAINER (action_area), 5);
153   gtk_box_set_spacing (GTK_BOX (action_area), 6);
154
155   gtk_widget_push_composite_child ();
156
157   gtk_window_set_resizable (GTK_WINDOW (fontchooserdiag), TRUE);
158
159   /* Create the content area */
160   priv->fontchooser = gtk_font_chooser_widget_new ();
161   gtk_container_set_border_width (GTK_CONTAINER (priv->fontchooser), 5);
162   gtk_widget_show (priv->fontchooser);
163   gtk_box_pack_start (GTK_BOX (content_area),
164                       priv->fontchooser, TRUE, TRUE, 0);
165
166   g_signal_connect (priv->fontchooser, "font-activated",
167                     G_CALLBACK (font_activated_cb), dialog);
168
169   /* Create the action area */
170   priv->cancel_button = gtk_dialog_add_button (dialog,
171                                                GTK_STOCK_CANCEL,
172                                                GTK_RESPONSE_CANCEL);
173   priv->select_button = gtk_dialog_add_button (dialog,
174                                                _("_Select"),
175                                                GTK_RESPONSE_OK);
176   gtk_widget_grab_default (priv->select_button);
177
178   gtk_dialog_set_alternative_button_order (GTK_DIALOG (fontchooserdiag),
179                                            GTK_RESPONSE_OK,
180                                            GTK_RESPONSE_CANCEL,
181                                            -1);
182
183   gtk_window_set_title (GTK_WINDOW (fontchooserdiag), _("Font Selection"));
184
185   gtk_widget_pop_composite_child ();
186
187   _gtk_font_chooser_set_delegate (GTK_FONT_CHOOSER (fontchooserdiag),
188                                   GTK_FONT_CHOOSER (priv->fontchooser));
189 }
190
191 /**
192  * gtk_font_chooser_dialog_new:
193  * @title: (allow-none): Title of the dialog, or %NULL
194  * @parent: (allow-none): Transient parent of the dialog, or %NULL
195  *
196  * Creates a new #GtkFontChooserDialog.
197  *
198  * Return value: a new #GtkFontChooserDialog
199  *
200  * Since: 3.2
201  */
202 GtkWidget*
203 gtk_font_chooser_dialog_new (const gchar *title,
204                              GtkWindow   *parent)
205 {
206   GtkFontChooserDialog *dialog;
207
208   dialog = g_object_new (GTK_TYPE_FONT_CHOOSER_DIALOG,
209                          "title", title,
210                          "transient-for", parent,
211                          NULL);
212
213   return GTK_WIDGET (dialog);
214 }
215
216 static void
217 gtk_font_chooser_dialog_buildable_interface_init (GtkBuildableIface *iface)
218 {
219   parent_buildable_iface = g_type_interface_peek_parent (iface);
220   iface->get_internal_child = gtk_font_chooser_dialog_buildable_get_internal_child;
221 }
222
223 static GObject *
224 gtk_font_chooser_dialog_buildable_get_internal_child (GtkBuildable *buildable,
225                                                       GtkBuilder   *builder,
226                                                       const gchar  *childname)
227 {
228   GtkFontChooserDialogPrivate *priv;
229
230   priv = GTK_FONT_CHOOSER_DIALOG (buildable)->priv;
231
232   if (g_strcmp0 (childname, "select_button") == 0)
233     return G_OBJECT (priv->select_button);
234   else if (g_strcmp0 (childname, "cancel_button") == 0)
235     return G_OBJECT (priv->cancel_button);
236
237   return parent_buildable_iface->get_internal_child (buildable, builder, childname);
238 }