]> Pileus Git - ~andy/gtk/blob - gtk/gtkfontbutton.c
fontbutton: Display the same text as elsewhere
[~andy/gtk] / gtk / gtkfontbutton.c
1 /* 
2  * GTK - The GIMP Toolkit
3  * Copyright (C) 1998 David Abilleira Freijeiro <odaf@nexo.es>
4  * All rights reserved.
5  *
6  * Based on gnome-color-picker by Federico Mena <federico@nuclecu.unam.mx>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Cambridge, MA 02139, USA.
21  */
22 /*
23  * Modified by the GTK+ Team and others 2003.  See the AUTHORS
24  * file for a list of people on the GTK+ Team.  See the ChangeLog
25  * files for a list of changes.  These files are distributed with
26  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
27  */
28
29 #include "config.h"
30
31 #include "gtkfontbutton.h"
32
33 #include "gtkmain.h"
34 #include "gtkbox.h"
35 #include "gtklabel.h"
36 #include "gtkfontchooser.h"
37 #include "gtkfontchooserdialog.h"
38 #include "gtkimage.h"
39 #include "gtkmarshalers.h"
40 #include "gtkseparator.h"
41 #include "gtkprivate.h"
42 #include "gtkintl.h"
43
44 #include <string.h>
45 #include <stdio.h>
46 #include "gtkfontchooserutils.h"
47
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 struct _GtkFontButtonPrivate 
62 {
63   gchar         *title;
64
65   gchar         *fontname;
66   
67   guint         use_font : 1;
68   guint         use_size : 1;
69   guint         show_style : 1;
70   guint         show_size : 1;
71   guint         show_preview_entry : 1;
72    
73   GtkWidget     *font_dialog;
74   GtkWidget     *inside;
75   GtkWidget     *font_label;
76   GtkWidget     *size_label;
77
78   PangoFontDescription *font_desc;
79   PangoFontFamily      *font_family;
80   PangoFontFace        *font_face;
81   gint                  font_size;
82   gchar                *preview_text;
83   GtkFontFilterFunc     font_filter;
84   gpointer              font_filter_data;
85   GDestroyNotify        font_filter_data_destroy;
86 };
87
88 /* Signals */
89 enum 
90 {
91   FONT_SET,
92   LAST_SIGNAL
93 };
94
95 enum 
96 {
97   PROP_0,
98   PROP_TITLE,
99   PROP_FONT_NAME,
100   PROP_USE_FONT,
101   PROP_USE_SIZE,
102   PROP_SHOW_STYLE,
103   PROP_SHOW_SIZE
104 };
105
106 /* Prototypes */
107 static void gtk_font_button_finalize               (GObject            *object);
108 static void gtk_font_button_get_property           (GObject            *object,
109                                                     guint               param_id,
110                                                     GValue             *value,
111                                                     GParamSpec         *pspec);
112 static void gtk_font_button_set_property           (GObject            *object,
113                                                     guint               param_id,
114                                                     const GValue       *value,
115                                                     GParamSpec         *pspec);
116
117 static void gtk_font_button_clicked                 (GtkButton         *button);
118
119 /* Dialog response functions */
120 static void response_cb                             (GtkDialog         *dialog,
121                                                      gint               response_id,
122                                                      gpointer           data);
123 static void dialog_destroy                          (GtkWidget         *widget,
124                                                      gpointer           data);
125
126 /* Auxiliary functions */
127 static GtkWidget *gtk_font_button_create_inside     (GtkFontButton     *gfs);
128 static void gtk_font_button_label_use_font          (GtkFontButton     *gfs);
129 static void gtk_font_button_update_font_info        (GtkFontButton     *gfs);
130
131 static guint font_button_signals[LAST_SIGNAL] = { 0 };
132
133 static void
134 clear_font_data (GtkFontButton *font_button)
135 {
136   GtkFontButtonPrivate *priv = font_button->priv;
137
138   if (priv->font_family)
139     g_object_unref (priv->font_family);
140   priv->font_family = NULL;
141
142   if (priv->font_face)
143     g_object_unref (priv->font_face);
144   priv->font_face = NULL;
145
146   if (priv->font_desc)
147     pango_font_description_free (priv->font_desc);
148   priv->font_desc = NULL;
149
150   g_free (priv->fontname);
151   priv->fontname = NULL;
152 }
153
154 static void
155 clear_font_filter_data (GtkFontButton *font_button)
156 {
157   GtkFontButtonPrivate *priv = font_button->priv;
158
159   if (priv->font_filter_data_destroy)
160     priv->font_filter_data_destroy (priv->font_filter_data);
161   priv->font_filter = NULL;
162   priv->font_filter_data = NULL;
163   priv->font_filter_data_destroy = NULL;
164 }
165
166 static gboolean
167 font_description_style_equal (const PangoFontDescription *a,
168                               const PangoFontDescription *b)
169 {
170   return (pango_font_description_get_weight (a) == pango_font_description_get_weight (b) &&
171           pango_font_description_get_style (a) == pango_font_description_get_style (b) &&
172           pango_font_description_get_stretch (a) == pango_font_description_get_stretch (b) &&
173           pango_font_description_get_variant (a) == pango_font_description_get_variant (b));
174 }
175
176 static void
177 gtk_font_button_update_font_data (GtkFontButton *font_button)
178 {
179   GtkFontButtonPrivate *priv = font_button->priv;
180   PangoFontFamily **families;
181   PangoFontFace **faces;
182   gint n_families, n_faces, i;
183   const gchar *family;
184
185   g_assert (priv->font_desc != NULL);
186
187   priv->fontname = pango_font_description_to_string (priv->font_desc);
188
189   family = pango_font_description_get_family (priv->font_desc);
190   if (family == NULL)
191     return;
192
193   n_families = 0;
194   families = NULL;
195   pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (font_button)),
196                                &families, &n_families);
197   n_faces = 0;
198   faces = NULL;
199   for (i = 0; i < n_families; i++)
200     {
201       const gchar *name = pango_font_family_get_name (families[i]);
202
203       if (!g_ascii_strcasecmp (name, family))
204         {
205           priv->font_family = g_object_ref (families[i]);
206
207           pango_font_family_list_faces (families[i], &faces, &n_faces);
208           break;
209         }
210     }
211   g_free (families);
212
213   for (i = 0; i < n_faces; i++)
214     {
215       PangoFontDescription *tmp_desc = pango_font_face_describe (faces[i]);
216
217       if (font_description_style_equal (tmp_desc, priv->font_desc))
218         {
219           priv->font_face = g_object_ref (faces[i]);
220
221           pango_font_description_free (tmp_desc);
222           break;
223         }
224       else
225         pango_font_description_free (tmp_desc);
226     }
227
228   g_free (faces);
229 }
230
231 static gchar *
232 gtk_font_button_get_preview_text (GtkFontButton *font_button)
233 {
234   GtkFontButtonPrivate *priv = font_button->priv;
235
236   if (priv->font_dialog)
237     return gtk_font_chooser_get_preview_text (GTK_FONT_CHOOSER (priv->font_dialog));
238
239   return g_strdup (priv->preview_text);
240 }
241
242 static void
243 gtk_font_button_set_preview_text (GtkFontButton *font_button,
244                                   const gchar   *preview_text)
245 {
246   GtkFontButtonPrivate *priv = font_button->priv;
247
248   if (priv->font_dialog)
249     {
250       gtk_font_chooser_set_preview_text (GTK_FONT_CHOOSER (priv->font_dialog),
251                                          preview_text);
252       return;
253     }
254
255   g_free (priv->preview_text);
256   priv->preview_text = g_strdup (preview_text);
257 }
258
259
260 static gboolean
261 gtk_font_button_get_show_preview_entry (GtkFontButton *font_button)
262 {
263   GtkFontButtonPrivate *priv = font_button->priv;
264
265   if (priv->font_dialog)
266     return gtk_font_chooser_get_show_preview_entry (GTK_FONT_CHOOSER (priv->font_dialog));
267
268   return priv->show_preview_entry;
269 }
270
271 static void
272 gtk_font_button_set_show_preview_entry (GtkFontButton *font_button,
273                                         gboolean       show)
274 {
275   GtkFontButtonPrivate *priv = font_button->priv;
276
277   if (priv->font_dialog)
278     return gtk_font_chooser_set_show_preview_entry (GTK_FONT_CHOOSER (priv->font_dialog), show);
279
280   priv->show_preview_entry = show != FALSE;
281 }
282
283 static PangoFontFamily *
284 gtk_font_button_font_chooser_get_font_family (GtkFontChooser *chooser)
285 {
286   GtkFontButton *font_button = GTK_FONT_BUTTON (chooser);
287   GtkFontButtonPrivate *priv = font_button->priv;
288
289   return priv->font_family;
290 }
291
292 static PangoFontFace *
293 gtk_font_button_font_chooser_get_font_face (GtkFontChooser *chooser)
294 {
295   GtkFontButton *font_button = GTK_FONT_BUTTON (chooser);
296   GtkFontButtonPrivate *priv = font_button->priv;
297
298   return priv->font_face;
299 }
300
301 static int
302 gtk_font_button_font_chooser_get_font_size (GtkFontChooser *chooser)
303 {
304   GtkFontButton *font_button = GTK_FONT_BUTTON (chooser);
305   GtkFontButtonPrivate *priv = font_button->priv;
306
307   return priv->font_size;
308 }
309
310 static void
311 gtk_font_button_font_chooser_set_filter_func (GtkFontChooser    *chooser,
312                                               GtkFontFilterFunc  filter_func,
313                                               gpointer           filter_data,
314                                               GDestroyNotify     data_destroy)
315 {
316   GtkFontButton *font_button = GTK_FONT_BUTTON (chooser);
317   GtkFontButtonPrivate *priv = font_button->priv;
318
319   if (priv->font_dialog)
320     {
321       gtk_font_chooser_set_filter_func (GTK_FONT_CHOOSER (priv->font_dialog),
322                                         filter_func,
323                                         filter_data,
324                                         data_destroy);
325       return;
326     }
327
328   clear_font_filter_data (font_button);
329   priv->font_filter = filter_func;
330   priv->font_filter_data = filter_data;
331   priv->font_filter_data_destroy = data_destroy;
332 }
333
334 static void
335 gtk_font_button_take_font_desc (GtkFontButton        *font_button,
336                                 PangoFontDescription *font_desc)
337 {
338   GtkFontButtonPrivate *priv = font_button->priv;
339   GObject *object = G_OBJECT (font_button);
340
341   if (priv->font_desc && font_desc &&
342       pango_font_description_equal (priv->font_desc, font_desc))
343     {
344       pango_font_description_free (font_desc);
345       return;
346     }
347
348   g_object_freeze_notify (object);
349
350   clear_font_data (font_button);
351
352   if (font_desc)
353     priv->font_desc = font_desc; /* adopted */
354   else
355     priv->font_desc = pango_font_description_from_string (_("Sans 12"));
356
357   if (pango_font_description_get_size_is_absolute (priv->font_desc))
358     priv->font_size = pango_font_description_get_size (priv->font_desc);
359   else 
360     priv->font_size = pango_font_description_get_size (priv->font_desc) / PANGO_SCALE;
361
362   gtk_font_button_update_font_data (font_button);
363   gtk_font_button_update_font_info (font_button);
364
365   if (priv->font_dialog)
366     gtk_font_chooser_set_font_desc (GTK_FONT_CHOOSER (priv->font_dialog),
367                                     priv->font_desc);
368
369   g_object_notify (G_OBJECT (font_button), "font");
370   g_object_notify (G_OBJECT (font_button), "font-desc");
371   g_object_notify (G_OBJECT (font_button), "font-name");
372
373   g_object_thaw_notify (object);
374 }
375
376 static const PangoFontDescription *
377 gtk_font_button_get_font_desc (GtkFontButton *font_button)
378 {
379   return font_button->priv->font_desc;
380 }
381
382 static void
383 gtk_font_button_font_chooser_notify (GObject    *object,
384                                      GParamSpec *pspec,
385                                      gpointer    user_data)
386 {
387   /* We do not forward the notification of the "font" property to the dialog! */
388   if (pspec->name == I_("preview-text") ||
389       pspec->name == I_("show-preview-entry"))
390     g_object_notify_by_pspec (user_data, pspec);
391 }
392
393 static void
394 gtk_font_button_font_chooser_iface_init (GtkFontChooserIface *iface)
395 {
396   iface->get_font_family = gtk_font_button_font_chooser_get_font_family;
397   iface->get_font_face = gtk_font_button_font_chooser_get_font_face;
398   iface->get_font_size = gtk_font_button_font_chooser_get_font_size;
399   iface->set_filter_func = gtk_font_button_font_chooser_set_filter_func;
400 }
401
402 G_DEFINE_TYPE_WITH_CODE (GtkFontButton, gtk_font_button, GTK_TYPE_BUTTON,
403                          G_IMPLEMENT_INTERFACE (GTK_TYPE_FONT_CHOOSER,
404                                                 gtk_font_button_font_chooser_iface_init))
405
406 static void
407 gtk_font_button_class_init (GtkFontButtonClass *klass)
408 {
409   GObjectClass *gobject_class;
410   GtkButtonClass *button_class;
411   
412   gobject_class = (GObjectClass *) klass;
413   button_class = (GtkButtonClass *) klass;
414
415   gobject_class->finalize = gtk_font_button_finalize;
416   gobject_class->set_property = gtk_font_button_set_property;
417   gobject_class->get_property = gtk_font_button_get_property;
418   
419   button_class->clicked = gtk_font_button_clicked;
420   
421   klass->font_set = NULL;
422
423   _gtk_font_chooser_install_properties (gobject_class);
424
425   /**
426    * GtkFontButton:title:
427    * 
428    * The title of the font chooser dialog.
429    *
430    * Since: 2.4
431    */
432   g_object_class_install_property (gobject_class,
433                                    PROP_TITLE,
434                                    g_param_spec_string ("title",
435                                                         P_("Title"),
436                                                         P_("The title of the font chooser dialog"),
437                                                         _("Pick a Font"),
438                                                         (GTK_PARAM_READABLE |
439                                                          GTK_PARAM_WRITABLE)));
440
441   /**
442    * GtkFontButton:font-name:
443    * 
444    * The name of the currently selected font.
445    *
446    * Since: 2.4
447    */
448   g_object_class_install_property (gobject_class,
449                                    PROP_FONT_NAME,
450                                    g_param_spec_string ("font-name",
451                                                         P_("Font name"),
452                                                         P_("The name of the selected font"),
453                                                         P_("Sans 12"),
454                                                         (GTK_PARAM_READABLE |
455                                                          GTK_PARAM_WRITABLE)));
456
457   /**
458    * GtkFontButton:use-font:
459    * 
460    * If this property is set to %TRUE, the label will be drawn 
461    * in the selected font.
462    *
463    * Since: 2.4
464    */
465   g_object_class_install_property (gobject_class,
466                                    PROP_USE_FONT,
467                                    g_param_spec_boolean ("use-font",
468                                                          P_("Use font in label"),
469                                                          P_("Whether the label is drawn in the selected font"),
470                                                          FALSE,
471                                                          GTK_PARAM_READWRITE));
472
473   /**
474    * GtkFontButton:use-size:
475    * 
476    * If this property is set to %TRUE, the label will be drawn 
477    * with the selected font size.
478    *
479    * Since: 2.4
480    */
481   g_object_class_install_property (gobject_class,
482                                    PROP_USE_SIZE,
483                                    g_param_spec_boolean ("use-size",
484                                                          P_("Use size in label"),
485                                                          P_("Whether the label is drawn with the selected font size"),
486                                                          FALSE,
487                                                          GTK_PARAM_READWRITE));
488
489   /**
490    * GtkFontButton:show-style:
491    * 
492    * If this property is set to %TRUE, the name of the selected font style 
493    * will be shown in the label. For a more WYSIWYG way to show the selected 
494    * style, see the ::use-font property. 
495    *
496    * Since: 2.4
497    */
498   g_object_class_install_property (gobject_class,
499                                    PROP_SHOW_STYLE,
500                                    g_param_spec_boolean ("show-style",
501                                                          P_("Show style"),
502                                                          P_("Whether the selected font style is shown in the label"),
503                                                          TRUE,
504                                                          GTK_PARAM_READWRITE));
505   /**
506    * GtkFontButton:show-size:
507    * 
508    * If this property is set to %TRUE, the selected font size will be shown 
509    * in the label. For a more WYSIWYG way to show the selected size, see the 
510    * ::use-size property. 
511    *
512    * Since: 2.4
513    */
514   g_object_class_install_property (gobject_class,
515                                    PROP_SHOW_SIZE,
516                                    g_param_spec_boolean ("show-size",
517                                                          P_("Show size"),
518                                                          P_("Whether selected font size is shown in the label"),
519                                                          TRUE,
520                                                          GTK_PARAM_READWRITE));
521
522   /**
523    * GtkFontButton::font-set:
524    * @widget: the object which received the signal.
525    * 
526    * The ::font-set signal is emitted when the user selects a font. 
527    * When handling this signal, use gtk_font_button_get_font_name() 
528    * to find out which font was just selected.
529    *
530    * Note that this signal is only emitted when the <emphasis>user</emphasis>
531    * changes the font. If you need to react to programmatic font changes
532    * as well, use the notify::font-name signal.
533    *
534    * Since: 2.4
535    */
536   font_button_signals[FONT_SET] = g_signal_new (I_("font-set"),
537                                                 G_TYPE_FROM_CLASS (gobject_class),
538                                                 G_SIGNAL_RUN_FIRST,
539                                                 G_STRUCT_OFFSET (GtkFontButtonClass, font_set),
540                                                 NULL, NULL,
541                                                 g_cclosure_marshal_VOID__VOID,
542                                                 G_TYPE_NONE, 0);
543   
544   g_type_class_add_private (gobject_class, sizeof (GtkFontButtonPrivate));
545 }
546
547 static void
548 gtk_font_button_init (GtkFontButton *font_button)
549 {
550   font_button->priv = G_TYPE_INSTANCE_GET_PRIVATE (font_button,
551                                                    GTK_TYPE_FONT_BUTTON,
552                                                    GtkFontButtonPrivate);
553
554   /* Initialize fields */
555   font_button->priv->use_font = FALSE;
556   font_button->priv->use_size = FALSE;
557   font_button->priv->show_style = TRUE;
558   font_button->priv->show_size = TRUE;
559   font_button->priv->show_preview_entry = FALSE;
560   font_button->priv->font_dialog = NULL;
561   font_button->priv->font_family = NULL;
562   font_button->priv->font_face = NULL;
563   font_button->priv->font_size = -1;
564   font_button->priv->title = g_strdup (_("Pick a Font"));
565
566   font_button->priv->inside = gtk_font_button_create_inside (font_button);
567   gtk_container_add (GTK_CONTAINER (font_button), font_button->priv->inside);
568
569   gtk_font_button_take_font_desc (font_button, NULL);
570 }
571
572 static void
573 gtk_font_button_finalize (GObject *object)
574 {
575   GtkFontButton *font_button = GTK_FONT_BUTTON (object);
576
577   if (font_button->priv->font_dialog != NULL) 
578     gtk_widget_destroy (font_button->priv->font_dialog);
579   font_button->priv->font_dialog = NULL;
580
581   g_free (font_button->priv->title);
582   font_button->priv->title = NULL;
583
584   clear_font_data (font_button);
585   clear_font_filter_data (font_button);
586
587   g_free (font_button->priv->preview_text);
588   font_button->priv->preview_text = NULL;
589
590   G_OBJECT_CLASS (gtk_font_button_parent_class)->finalize (object);
591 }
592
593 static void
594 gtk_font_button_set_property (GObject      *object,
595                               guint         param_id,
596                               const GValue *value,
597                               GParamSpec   *pspec)
598 {
599   GtkFontButton *font_button = GTK_FONT_BUTTON (object);
600
601   switch (param_id) 
602     {
603     case GTK_FONT_CHOOSER_PROP_PREVIEW_TEXT:
604       gtk_font_button_set_preview_text (font_button, g_value_get_string (value));
605       break;
606     case GTK_FONT_CHOOSER_PROP_SHOW_PREVIEW_ENTRY:
607       gtk_font_button_set_show_preview_entry (font_button, g_value_get_boolean (value));
608       break;
609     case PROP_TITLE:
610       gtk_font_button_set_title (font_button, g_value_get_string (value));
611       break;
612     case GTK_FONT_CHOOSER_PROP_FONT_DESC:
613       gtk_font_button_take_font_desc (font_button, g_value_dup_boxed (value));
614       break;
615     case GTK_FONT_CHOOSER_PROP_FONT:
616     case PROP_FONT_NAME:
617       gtk_font_button_set_font_name (font_button, g_value_get_string (value));
618       break;
619     case PROP_USE_FONT:
620       gtk_font_button_set_use_font (font_button, g_value_get_boolean (value));
621       break;
622     case PROP_USE_SIZE:
623       gtk_font_button_set_use_size (font_button, g_value_get_boolean (value));
624       break;
625     case PROP_SHOW_STYLE:
626       gtk_font_button_set_show_style (font_button, g_value_get_boolean (value));
627       break;
628     case PROP_SHOW_SIZE:
629       gtk_font_button_set_show_size (font_button, g_value_get_boolean (value));
630       break;
631     default:
632       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
633       break;
634   }
635 }
636
637 static void
638 gtk_font_button_get_property (GObject    *object,
639                               guint       param_id,
640                               GValue     *value,
641                               GParamSpec *pspec)
642 {
643   GtkFontButton *font_button = GTK_FONT_BUTTON (object);
644   
645   switch (param_id) 
646     {
647     case GTK_FONT_CHOOSER_PROP_PREVIEW_TEXT:
648       g_value_set_string (value, gtk_font_button_get_preview_text (font_button));
649       break;
650     case GTK_FONT_CHOOSER_PROP_SHOW_PREVIEW_ENTRY:
651       g_value_set_boolean (value, gtk_font_button_get_show_preview_entry (font_button));
652       break;
653     case PROP_TITLE:
654       g_value_set_string (value, gtk_font_button_get_title (font_button));
655       break;
656     case GTK_FONT_CHOOSER_PROP_FONT_DESC:
657       g_value_set_boxed (value, gtk_font_button_get_font_desc (font_button));
658       break;
659     case GTK_FONT_CHOOSER_PROP_FONT:
660     case PROP_FONT_NAME:
661       g_value_set_string (value, gtk_font_button_get_font_name (font_button));
662       break;
663     case PROP_USE_FONT:
664       g_value_set_boolean (value, gtk_font_button_get_use_font (font_button));
665       break;
666     case PROP_USE_SIZE:
667       g_value_set_boolean (value, gtk_font_button_get_use_size (font_button));
668       break;
669     case PROP_SHOW_STYLE:
670       g_value_set_boolean (value, gtk_font_button_get_show_style (font_button));
671       break;
672     case PROP_SHOW_SIZE:
673       g_value_set_boolean (value, gtk_font_button_get_show_size (font_button));
674       break;
675     default:
676       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
677       break;
678     }
679
680
681
682 /**
683  * gtk_font_button_new:
684  *
685  * Creates a new font picker widget.
686  *
687  * Returns: a new font picker widget.
688  *
689  * Since: 2.4
690  */
691 GtkWidget *
692 gtk_font_button_new (void)
693 {
694   return g_object_new (GTK_TYPE_FONT_BUTTON, NULL);
695
696
697 /**
698  * gtk_font_button_new_with_font:
699  * @fontname: Name of font to display in font chooser dialog
700  *
701  * Creates a new font picker widget.
702  *
703  * Returns: a new font picker widget.
704  *
705  * Since: 2.4
706  */
707 GtkWidget *
708 gtk_font_button_new_with_font (const gchar *fontname)
709 {
710   return g_object_new (GTK_TYPE_FONT_BUTTON, "font-name", fontname, NULL);
711
712
713 /**
714  * gtk_font_button_set_title:
715  * @font_button: a #GtkFontButton
716  * @title: a string containing the font chooser dialog title
717  *
718  * Sets the title for the font chooser dialog.  
719  *
720  * Since: 2.4
721  */
722 void
723 gtk_font_button_set_title (GtkFontButton *font_button, 
724                            const gchar   *title)
725 {
726   gchar *old_title;
727   g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
728   
729   old_title = font_button->priv->title;
730   font_button->priv->title = g_strdup (title);
731   g_free (old_title);
732   
733   if (font_button->priv->font_dialog)
734     gtk_window_set_title (GTK_WINDOW (font_button->priv->font_dialog),
735                           font_button->priv->title);
736
737   g_object_notify (G_OBJECT (font_button), "title");
738
739
740 /**
741  * gtk_font_button_get_title:
742  * @font_button: a #GtkFontButton
743  *
744  * Retrieves the title of the font chooser dialog.
745  *
746  * Returns: an internal copy of the title string which must not be freed.
747  *
748  * Since: 2.4
749  */
750 const gchar*
751 gtk_font_button_get_title (GtkFontButton *font_button)
752 {
753   g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), NULL);
754
755   return font_button->priv->title;
756
757
758 /**
759  * gtk_font_button_get_use_font:
760  * @font_button: a #GtkFontButton
761  *
762  * Returns whether the selected font is used in the label.
763  *
764  * Returns: whether the selected font is used in the label.
765  *
766  * Since: 2.4
767  */
768 gboolean
769 gtk_font_button_get_use_font (GtkFontButton *font_button)
770 {
771   g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
772
773   return font_button->priv->use_font;
774
775
776 /**
777  * gtk_font_button_set_use_font:
778  * @font_button: a #GtkFontButton
779  * @use_font: If %TRUE, font name will be written using font chosen.
780  *
781  * If @use_font is %TRUE, the font name will be written using the selected font.  
782  *
783  * Since: 2.4
784  */
785 void  
786 gtk_font_button_set_use_font (GtkFontButton *font_button,
787                               gboolean       use_font)
788 {
789   g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
790   
791   use_font = (use_font != FALSE);
792   
793   if (font_button->priv->use_font != use_font) 
794     {
795       font_button->priv->use_font = use_font;
796
797       if (use_font)
798         gtk_font_button_label_use_font (font_button);
799       else
800         gtk_widget_set_style (font_button->priv->font_label, NULL);
801  
802      g_object_notify (G_OBJECT (font_button), "use-font");
803     }
804
805
806
807 /**
808  * gtk_font_button_get_use_size:
809  * @font_button: a #GtkFontButton
810  *
811  * Returns whether the selected size is used in the label.
812  *
813  * Returns: whether the selected size is used in the label.
814  *
815  * Since: 2.4
816  */
817 gboolean
818 gtk_font_button_get_use_size (GtkFontButton *font_button)
819 {
820   g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
821
822   return font_button->priv->use_size;
823
824
825 /**
826  * gtk_font_button_set_use_size:
827  * @font_button: a #GtkFontButton
828  * @use_size: If %TRUE, font name will be written using the selected size.
829  *
830  * If @use_size is %TRUE, the font name will be written using the selected size.
831  *
832  * Since: 2.4
833  */
834 void  
835 gtk_font_button_set_use_size (GtkFontButton *font_button,
836                               gboolean       use_size)
837 {
838   g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
839   
840   use_size = (use_size != FALSE);
841   if (font_button->priv->use_size != use_size) 
842     {
843       font_button->priv->use_size = use_size;
844
845       if (font_button->priv->use_font)
846         gtk_font_button_label_use_font (font_button);
847
848       g_object_notify (G_OBJECT (font_button), "use-size");
849     }
850
851
852 /**
853  * gtk_font_button_get_show_style:
854  * @font_button: a #GtkFontButton
855  * 
856  * Returns whether the name of the font style will be shown in the label.
857  * 
858  * Return value: whether the font style will be shown in the label.
859  *
860  * Since: 2.4
861  **/
862 gboolean 
863 gtk_font_button_get_show_style (GtkFontButton *font_button)
864 {
865   g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
866
867   return font_button->priv->show_style;
868 }
869
870 /**
871  * gtk_font_button_set_show_style:
872  * @font_button: a #GtkFontButton
873  * @show_style: %TRUE if font style should be displayed in label.
874  *
875  * If @show_style is %TRUE, the font style will be displayed along with name of the selected font.
876  *
877  * Since: 2.4
878  */
879 void
880 gtk_font_button_set_show_style (GtkFontButton *font_button,
881                                 gboolean       show_style)
882 {
883   g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
884   
885   show_style = (show_style != FALSE);
886   if (font_button->priv->show_style != show_style) 
887     {
888       font_button->priv->show_style = show_style;
889       
890       gtk_font_button_update_font_info (font_button);
891   
892       g_object_notify (G_OBJECT (font_button), "show-style");
893     }
894
895
896
897 /**
898  * gtk_font_button_get_show_size:
899  * @font_button: a #GtkFontButton
900  * 
901  * Returns whether the font size will be shown in the label.
902  * 
903  * Return value: whether the font size will be shown in the label.
904  *
905  * Since: 2.4
906  **/
907 gboolean 
908 gtk_font_button_get_show_size (GtkFontButton *font_button)
909 {
910   g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
911
912   return font_button->priv->show_size;
913 }
914
915 /**
916  * gtk_font_button_set_show_size:
917  * @font_button: a #GtkFontButton
918  * @show_size: %TRUE if font size should be displayed in dialog.
919  *
920  * If @show_size is %TRUE, the font size will be displayed along with the name of the selected font.
921  *
922  * Since: 2.4
923  */
924 void
925 gtk_font_button_set_show_size (GtkFontButton *font_button,
926                                gboolean       show_size)
927 {
928   g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
929   
930   show_size = (show_size != FALSE);
931
932   if (font_button->priv->show_size != show_size) 
933     {
934       font_button->priv->show_size = show_size;
935
936       gtk_container_remove (GTK_CONTAINER (font_button), font_button->priv->inside);
937       font_button->priv->inside = gtk_font_button_create_inside (font_button);
938       gtk_container_add (GTK_CONTAINER (font_button), font_button->priv->inside);
939       
940       gtk_font_button_update_font_info (font_button);
941
942       g_object_notify (G_OBJECT (font_button), "show-size");
943     }
944
945
946
947 /**
948  * gtk_font_button_get_font_name:
949  * @font_button: a #GtkFontButton
950  *
951  * Retrieves the name of the currently selected font. This name includes
952  * style and size information as well. If you want to render something
953  * with the font, use this string with pango_font_description_from_string() .
954  * If you're interested in peeking certain values (family name,
955  * style, size, weight) just query these properties from the
956  * #PangoFontDescription object.
957  *
958  * Returns: an internal copy of the font name which must not be freed.
959  *
960  * Since: 2.4
961  */
962 const gchar *
963 gtk_font_button_get_font_name (GtkFontButton *font_button)
964 {
965   g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), NULL);
966
967   return font_button->priv->fontname;
968 }
969
970 /**
971  * gtk_font_button_set_font_name:
972  * @font_button: a #GtkFontButton
973  * @fontname: Name of font to display in font chooser dialog
974  *
975  * Sets or updates the currently-displayed font in font picker dialog.
976  *
977  * Returns: %TRUE
978  *
979  * Since: 2.4
980  */
981 gboolean 
982 gtk_font_button_set_font_name (GtkFontButton *font_button,
983                                const gchar    *fontname)
984 {
985   PangoFontDescription *font_desc;
986
987   g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
988   g_return_val_if_fail (fontname != NULL, FALSE);
989
990   font_desc = pango_font_description_from_string (fontname);
991   gtk_font_button_take_font_desc (font_button, font_desc);
992
993   return TRUE;
994 }
995
996 static void
997 gtk_font_button_clicked (GtkButton *button)
998 {
999   GtkFontChooser *font_dialog;
1000   GtkFontButton  *font_button = GTK_FONT_BUTTON (button);
1001   GtkFontButtonPrivate *priv = font_button->priv;
1002   
1003   if (!font_button->priv->font_dialog) 
1004     {
1005       GtkWidget *parent;
1006       
1007       parent = gtk_widget_get_toplevel (GTK_WIDGET (font_button));
1008
1009       priv->font_dialog = gtk_font_chooser_dialog_new (priv->title, NULL);
1010       font_dialog = GTK_FONT_CHOOSER (font_button->priv->font_dialog);
1011
1012       gtk_font_chooser_set_show_preview_entry (font_dialog, priv->show_preview_entry);
1013
1014       if (priv->preview_text)
1015         {
1016           gtk_font_chooser_set_preview_text (font_dialog, priv->preview_text);
1017           g_free (priv->preview_text);
1018           priv->preview_text = NULL;
1019         }
1020
1021       if (priv->font_filter)
1022         {
1023           gtk_font_chooser_set_filter_func (font_dialog,
1024                                             priv->font_filter,
1025                                             priv->font_filter_data,
1026                                             priv->font_filter_data_destroy);
1027           priv->font_filter = NULL;
1028           priv->font_filter_data = NULL;
1029           priv->font_filter_data_destroy = NULL;
1030         }
1031
1032       if (gtk_widget_is_toplevel (parent) && GTK_IS_WINDOW (parent))
1033         {
1034           if (GTK_WINDOW (parent) != gtk_window_get_transient_for (GTK_WINDOW (font_dialog)))
1035             gtk_window_set_transient_for (GTK_WINDOW (font_dialog), GTK_WINDOW (parent));
1036
1037           gtk_window_set_modal (GTK_WINDOW (font_dialog),
1038                                 gtk_window_get_modal (GTK_WINDOW (parent)));
1039         }
1040
1041       g_signal_connect (font_dialog, "notify",
1042                         G_CALLBACK (gtk_font_button_font_chooser_notify), button);
1043
1044       g_signal_connect (font_dialog, "response",
1045                         G_CALLBACK (response_cb), font_button);
1046
1047       g_signal_connect (font_dialog, "destroy",
1048                         G_CALLBACK (dialog_destroy), font_button);
1049     }
1050   
1051   if (!gtk_widget_get_visible (font_button->priv->font_dialog))
1052     {
1053       font_dialog = GTK_FONT_CHOOSER (font_button->priv->font_dialog);
1054       gtk_font_chooser_set_font_desc (font_dialog, font_button->priv->font_desc);
1055     } 
1056
1057   gtk_window_present (GTK_WINDOW (font_button->priv->font_dialog));
1058 }
1059
1060
1061 static void
1062 response_cb (GtkDialog *dialog,
1063              gint       response_id,
1064              gpointer   data)
1065 {
1066   GtkFontButton *font_button = GTK_FONT_BUTTON (data);
1067   GtkFontButtonPrivate *priv = font_button->priv;
1068   GtkFontChooser *font_chooser;
1069   GObject *object;
1070
1071   gtk_widget_hide (font_button->priv->font_dialog);
1072
1073   if (response_id != GTK_RESPONSE_OK)
1074     return;
1075
1076   font_chooser = GTK_FONT_CHOOSER (priv->font_dialog);
1077   object = G_OBJECT (font_chooser);
1078
1079   g_object_freeze_notify (object);
1080
1081   clear_font_data (font_button);
1082
1083   priv->font_desc = gtk_font_chooser_get_font_desc (font_chooser);
1084   if (priv->font_desc)
1085     priv->fontname = pango_font_description_to_string (priv->font_desc);
1086   priv->font_family = gtk_font_chooser_get_font_family (font_chooser);
1087   if (priv->font_family)
1088     g_object_ref (priv->font_family);
1089   priv->font_face = gtk_font_chooser_get_font_face (font_chooser);
1090   if (priv->font_face)
1091     g_object_ref (priv->font_face);
1092   priv->font_size = gtk_font_chooser_get_font_size (font_chooser);
1093
1094   /* Set label font */
1095   gtk_font_button_update_font_info (font_button);
1096
1097   g_object_notify (G_OBJECT (font_button), "font");
1098   g_object_notify (G_OBJECT (font_button), "font-desc");
1099   g_object_notify (G_OBJECT (font_button), "font-name");
1100
1101   g_object_thaw_notify (object);
1102
1103   /* Emit font_set signal */
1104   g_signal_emit (font_button, font_button_signals[FONT_SET], 0);
1105 }
1106
1107 static void
1108 dialog_destroy (GtkWidget *widget,
1109                 gpointer   data)
1110 {
1111   GtkFontButton *font_button = GTK_FONT_BUTTON (data);
1112     
1113   /* Dialog will get destroyed so reference is not valid now */
1114   font_button->priv->font_dialog = NULL;
1115
1116
1117 static GtkWidget *
1118 gtk_font_button_create_inside (GtkFontButton *font_button)
1119 {
1120   GtkWidget *widget;
1121   
1122   gtk_widget_push_composite_child ();
1123
1124   widget = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
1125
1126   font_button->priv->font_label = gtk_label_new (_("Font"));
1127   
1128   gtk_label_set_justify (GTK_LABEL (font_button->priv->font_label), GTK_JUSTIFY_LEFT);
1129   gtk_box_pack_start (GTK_BOX (widget), font_button->priv->font_label, TRUE, TRUE, 5);
1130
1131   if (font_button->priv->show_size) 
1132     {
1133       gtk_box_pack_start (GTK_BOX (widget), gtk_separator_new (GTK_ORIENTATION_VERTICAL), FALSE, FALSE, 0);
1134       font_button->priv->size_label = gtk_label_new ("14");
1135       gtk_box_pack_start (GTK_BOX (widget), font_button->priv->size_label, FALSE, FALSE, 5);
1136     }
1137
1138   gtk_widget_show_all (widget);
1139
1140   gtk_widget_pop_composite_child ();
1141
1142   return widget;
1143
1144
1145 static void
1146 gtk_font_button_label_use_font (GtkFontButton *font_button)
1147 {
1148   PangoFontDescription *desc;
1149
1150   if (!font_button->priv->use_font)
1151     return;
1152
1153   desc = pango_font_description_copy (font_button->priv->font_desc);
1154
1155   if (!font_button->priv->use_size)
1156     pango_font_description_unset_fields (desc, PANGO_FONT_MASK_SIZE);
1157
1158   gtk_widget_modify_font (font_button->priv->font_label, desc);
1159
1160   pango_font_description_free (desc);
1161 }
1162
1163 static void
1164 gtk_font_button_update_font_info (GtkFontButton *font_button)
1165 {
1166   GtkFontButtonPrivate *priv = font_button->priv;
1167   gchar *family_style;
1168
1169   g_assert (priv->font_desc != NULL);
1170
1171   if (priv->show_style)
1172     {
1173       PangoFontDescription *desc = pango_font_description_copy_static (priv->font_desc);
1174
1175       pango_font_description_unset_fields (desc, PANGO_FONT_MASK_SIZE);
1176       family_style = pango_font_description_to_string (desc);
1177       pango_font_description_free (desc);
1178     }
1179   else
1180     family_style = g_strdup (pango_font_description_get_family (priv->font_desc));
1181
1182   gtk_label_set_text (GTK_LABEL (font_button->priv->font_label), family_style);
1183   g_free (family_style);
1184
1185   if (font_button->priv->show_size) 
1186     {
1187       gchar *size = g_strdup_printf ("%g",
1188                                      pango_font_description_get_size (priv->font_desc) / (double)PANGO_SCALE);
1189       
1190       gtk_label_set_text (GTK_LABEL (font_button->priv->size_label), size);
1191       
1192       g_free (size);
1193     }
1194
1195   gtk_font_button_label_use_font (font_button);
1196