]> Pileus Git - ~andy/gtk/blob - gtk/gtkfontbutton.c
fontbutton: Properly update label after property changes
[~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       gtk_font_button_label_use_font (font_button);
798  
799       g_object_notify (G_OBJECT (font_button), "use-font");
800     }
801
802
803
804 /**
805  * gtk_font_button_get_use_size:
806  * @font_button: a #GtkFontButton
807  *
808  * Returns whether the selected size is used in the label.
809  *
810  * Returns: whether the selected size is used in the label.
811  *
812  * Since: 2.4
813  */
814 gboolean
815 gtk_font_button_get_use_size (GtkFontButton *font_button)
816 {
817   g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
818
819   return font_button->priv->use_size;
820
821
822 /**
823  * gtk_font_button_set_use_size:
824  * @font_button: a #GtkFontButton
825  * @use_size: If %TRUE, font name will be written using the selected size.
826  *
827  * If @use_size is %TRUE, the font name will be written using the selected size.
828  *
829  * Since: 2.4
830  */
831 void  
832 gtk_font_button_set_use_size (GtkFontButton *font_button,
833                               gboolean       use_size)
834 {
835   g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
836   
837   use_size = (use_size != FALSE);
838   if (font_button->priv->use_size != use_size) 
839     {
840       font_button->priv->use_size = use_size;
841
842       gtk_font_button_label_use_font (font_button);
843
844       g_object_notify (G_OBJECT (font_button), "use-size");
845     }
846
847
848 /**
849  * gtk_font_button_get_show_style:
850  * @font_button: a #GtkFontButton
851  * 
852  * Returns whether the name of the font style will be shown in the label.
853  * 
854  * Return value: whether the font style will be shown in the label.
855  *
856  * Since: 2.4
857  **/
858 gboolean 
859 gtk_font_button_get_show_style (GtkFontButton *font_button)
860 {
861   g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
862
863   return font_button->priv->show_style;
864 }
865
866 /**
867  * gtk_font_button_set_show_style:
868  * @font_button: a #GtkFontButton
869  * @show_style: %TRUE if font style should be displayed in label.
870  *
871  * If @show_style is %TRUE, the font style will be displayed along with name of the selected font.
872  *
873  * Since: 2.4
874  */
875 void
876 gtk_font_button_set_show_style (GtkFontButton *font_button,
877                                 gboolean       show_style)
878 {
879   g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
880   
881   show_style = (show_style != FALSE);
882   if (font_button->priv->show_style != show_style) 
883     {
884       font_button->priv->show_style = show_style;
885       
886       gtk_font_button_update_font_info (font_button);
887   
888       g_object_notify (G_OBJECT (font_button), "show-style");
889     }
890
891
892
893 /**
894  * gtk_font_button_get_show_size:
895  * @font_button: a #GtkFontButton
896  * 
897  * Returns whether the font size will be shown in the label.
898  * 
899  * Return value: whether the font size will be shown in the label.
900  *
901  * Since: 2.4
902  **/
903 gboolean 
904 gtk_font_button_get_show_size (GtkFontButton *font_button)
905 {
906   g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
907
908   return font_button->priv->show_size;
909 }
910
911 /**
912  * gtk_font_button_set_show_size:
913  * @font_button: a #GtkFontButton
914  * @show_size: %TRUE if font size should be displayed in dialog.
915  *
916  * If @show_size is %TRUE, the font size will be displayed along with the name of the selected font.
917  *
918  * Since: 2.4
919  */
920 void
921 gtk_font_button_set_show_size (GtkFontButton *font_button,
922                                gboolean       show_size)
923 {
924   g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
925   
926   show_size = (show_size != FALSE);
927
928   if (font_button->priv->show_size != show_size) 
929     {
930       font_button->priv->show_size = show_size;
931
932       gtk_container_remove (GTK_CONTAINER (font_button), font_button->priv->inside);
933       font_button->priv->inside = gtk_font_button_create_inside (font_button);
934       gtk_container_add (GTK_CONTAINER (font_button), font_button->priv->inside);
935       
936       gtk_font_button_update_font_info (font_button);
937
938       g_object_notify (G_OBJECT (font_button), "show-size");
939     }
940
941
942
943 /**
944  * gtk_font_button_get_font_name:
945  * @font_button: a #GtkFontButton
946  *
947  * Retrieves the name of the currently selected font. This name includes
948  * style and size information as well. If you want to render something
949  * with the font, use this string with pango_font_description_from_string() .
950  * If you're interested in peeking certain values (family name,
951  * style, size, weight) just query these properties from the
952  * #PangoFontDescription object.
953  *
954  * Returns: an internal copy of the font name which must not be freed.
955  *
956  * Since: 2.4
957  */
958 const gchar *
959 gtk_font_button_get_font_name (GtkFontButton *font_button)
960 {
961   g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), NULL);
962
963   return font_button->priv->fontname;
964 }
965
966 /**
967  * gtk_font_button_set_font_name:
968  * @font_button: a #GtkFontButton
969  * @fontname: Name of font to display in font chooser dialog
970  *
971  * Sets or updates the currently-displayed font in font picker dialog.
972  *
973  * Returns: %TRUE
974  *
975  * Since: 2.4
976  */
977 gboolean 
978 gtk_font_button_set_font_name (GtkFontButton *font_button,
979                                const gchar    *fontname)
980 {
981   PangoFontDescription *font_desc;
982
983   g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
984   g_return_val_if_fail (fontname != NULL, FALSE);
985
986   font_desc = pango_font_description_from_string (fontname);
987   gtk_font_button_take_font_desc (font_button, font_desc);
988
989   return TRUE;
990 }
991
992 static void
993 gtk_font_button_clicked (GtkButton *button)
994 {
995   GtkFontChooser *font_dialog;
996   GtkFontButton  *font_button = GTK_FONT_BUTTON (button);
997   GtkFontButtonPrivate *priv = font_button->priv;
998   
999   if (!font_button->priv->font_dialog) 
1000     {
1001       GtkWidget *parent;
1002       
1003       parent = gtk_widget_get_toplevel (GTK_WIDGET (font_button));
1004
1005       priv->font_dialog = gtk_font_chooser_dialog_new (priv->title, NULL);
1006       font_dialog = GTK_FONT_CHOOSER (font_button->priv->font_dialog);
1007
1008       gtk_font_chooser_set_show_preview_entry (font_dialog, priv->show_preview_entry);
1009
1010       if (priv->preview_text)
1011         {
1012           gtk_font_chooser_set_preview_text (font_dialog, priv->preview_text);
1013           g_free (priv->preview_text);
1014           priv->preview_text = NULL;
1015         }
1016
1017       if (priv->font_filter)
1018         {
1019           gtk_font_chooser_set_filter_func (font_dialog,
1020                                             priv->font_filter,
1021                                             priv->font_filter_data,
1022                                             priv->font_filter_data_destroy);
1023           priv->font_filter = NULL;
1024           priv->font_filter_data = NULL;
1025           priv->font_filter_data_destroy = NULL;
1026         }
1027
1028       if (gtk_widget_is_toplevel (parent) && GTK_IS_WINDOW (parent))
1029         {
1030           if (GTK_WINDOW (parent) != gtk_window_get_transient_for (GTK_WINDOW (font_dialog)))
1031             gtk_window_set_transient_for (GTK_WINDOW (font_dialog), GTK_WINDOW (parent));
1032
1033           gtk_window_set_modal (GTK_WINDOW (font_dialog),
1034                                 gtk_window_get_modal (GTK_WINDOW (parent)));
1035         }
1036
1037       g_signal_connect (font_dialog, "notify",
1038                         G_CALLBACK (gtk_font_button_font_chooser_notify), button);
1039
1040       g_signal_connect (font_dialog, "response",
1041                         G_CALLBACK (response_cb), font_button);
1042
1043       g_signal_connect (font_dialog, "destroy",
1044                         G_CALLBACK (dialog_destroy), font_button);
1045     }
1046   
1047   if (!gtk_widget_get_visible (font_button->priv->font_dialog))
1048     {
1049       font_dialog = GTK_FONT_CHOOSER (font_button->priv->font_dialog);
1050       gtk_font_chooser_set_font_desc (font_dialog, font_button->priv->font_desc);
1051     } 
1052
1053   gtk_window_present (GTK_WINDOW (font_button->priv->font_dialog));
1054 }
1055
1056
1057 static void
1058 response_cb (GtkDialog *dialog,
1059              gint       response_id,
1060              gpointer   data)
1061 {
1062   GtkFontButton *font_button = GTK_FONT_BUTTON (data);
1063   GtkFontButtonPrivate *priv = font_button->priv;
1064   GtkFontChooser *font_chooser;
1065   GObject *object;
1066
1067   gtk_widget_hide (font_button->priv->font_dialog);
1068
1069   if (response_id != GTK_RESPONSE_OK)
1070     return;
1071
1072   font_chooser = GTK_FONT_CHOOSER (priv->font_dialog);
1073   object = G_OBJECT (font_chooser);
1074
1075   g_object_freeze_notify (object);
1076
1077   clear_font_data (font_button);
1078
1079   priv->font_desc = gtk_font_chooser_get_font_desc (font_chooser);
1080   if (priv->font_desc)
1081     priv->fontname = pango_font_description_to_string (priv->font_desc);
1082   priv->font_family = gtk_font_chooser_get_font_family (font_chooser);
1083   if (priv->font_family)
1084     g_object_ref (priv->font_family);
1085   priv->font_face = gtk_font_chooser_get_font_face (font_chooser);
1086   if (priv->font_face)
1087     g_object_ref (priv->font_face);
1088   priv->font_size = gtk_font_chooser_get_font_size (font_chooser);
1089
1090   /* Set label font */
1091   gtk_font_button_update_font_info (font_button);
1092
1093   g_object_notify (G_OBJECT (font_button), "font");
1094   g_object_notify (G_OBJECT (font_button), "font-desc");
1095   g_object_notify (G_OBJECT (font_button), "font-name");
1096
1097   g_object_thaw_notify (object);
1098
1099   /* Emit font_set signal */
1100   g_signal_emit (font_button, font_button_signals[FONT_SET], 0);
1101 }
1102
1103 static void
1104 dialog_destroy (GtkWidget *widget,
1105                 gpointer   data)
1106 {
1107   GtkFontButton *font_button = GTK_FONT_BUTTON (data);
1108     
1109   /* Dialog will get destroyed so reference is not valid now */
1110   font_button->priv->font_dialog = NULL;
1111
1112
1113 static GtkWidget *
1114 gtk_font_button_create_inside (GtkFontButton *font_button)
1115 {
1116   GtkWidget *widget;
1117   
1118   gtk_widget_push_composite_child ();
1119
1120   widget = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
1121
1122   font_button->priv->font_label = gtk_label_new (_("Font"));
1123   
1124   gtk_label_set_justify (GTK_LABEL (font_button->priv->font_label), GTK_JUSTIFY_LEFT);
1125   gtk_box_pack_start (GTK_BOX (widget), font_button->priv->font_label, TRUE, TRUE, 5);
1126
1127   if (font_button->priv->show_size) 
1128     {
1129       gtk_box_pack_start (GTK_BOX (widget), gtk_separator_new (GTK_ORIENTATION_VERTICAL), FALSE, FALSE, 0);
1130       font_button->priv->size_label = gtk_label_new ("14");
1131       gtk_box_pack_start (GTK_BOX (widget), font_button->priv->size_label, FALSE, FALSE, 5);
1132     }
1133
1134   gtk_widget_show_all (widget);
1135
1136   gtk_widget_pop_composite_child ();
1137
1138   return widget;
1139
1140
1141 static void
1142 gtk_font_button_label_use_font (GtkFontButton *font_button)
1143 {
1144   PangoFontDescription *desc;
1145
1146   if (font_button->priv->use_font)
1147     {
1148       desc = pango_font_description_copy (font_button->priv->font_desc);
1149
1150       if (!font_button->priv->use_size)
1151         pango_font_description_unset_fields (desc, PANGO_FONT_MASK_SIZE);
1152     }
1153   else
1154     desc = NULL;
1155
1156   gtk_widget_override_font (font_button->priv->font_label, desc);
1157
1158   if (desc)
1159     pango_font_description_free (desc);
1160 }
1161
1162 static void
1163 gtk_font_button_update_font_info (GtkFontButton *font_button)
1164 {
1165   GtkFontButtonPrivate *priv = font_button->priv;
1166   gchar *family_style;
1167
1168   g_assert (priv->font_desc != NULL);
1169
1170   if (priv->show_style)
1171     {
1172       PangoFontDescription *desc = pango_font_description_copy_static (priv->font_desc);
1173
1174       pango_font_description_unset_fields (desc, PANGO_FONT_MASK_SIZE);
1175       family_style = pango_font_description_to_string (desc);
1176       pango_font_description_free (desc);
1177     }
1178   else
1179     family_style = g_strdup (pango_font_description_get_family (priv->font_desc));
1180
1181   gtk_label_set_text (GTK_LABEL (font_button->priv->font_label), family_style);
1182   g_free (family_style);
1183
1184   if (font_button->priv->show_size) 
1185     {
1186       gchar *size = g_strdup_printf ("%g",
1187                                      pango_font_description_get_size (priv->font_desc) / (double)PANGO_SCALE);
1188       
1189       gtk_label_set_text (GTK_LABEL (font_button->priv->size_label), size);
1190       
1191       g_free (size);
1192     }
1193
1194   gtk_font_button_label_use_font (font_button);
1195