]> Pileus Git - ~andy/gtk/blob - gtk/gtkfontbutton.c
Drop unneeded includes: gtkalignment.h
[~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 "gtkhbox.h"
35 #include "gtklabel.h"
36 #include "gtkfontsel.h"
37 #include "gtkimage.h"
38 #include "gtkmarshalers.h"
39 #include "gtkseparator.h"
40 #include "gtkprivate.h"
41 #include "gtkintl.h"
42
43 #include <string.h>
44 #include <stdio.h>
45
46
47 /**
48  * SECTION:gtkfontbutton
49  * @Short_description: A button to launch a font selection dialog
50  * @Title: GtkFontButton
51  * @See_also: #GtkFontSelectionDialog, #GtkColorButton.
52  *
53  * The #GtkFontButton is a button which displays the currently selected
54  * font an allows to open a font selection dialog to change the font.
55  * It is suitable widget for selecting a font in a preference dialog.
56  */
57
58
59 struct _GtkFontButtonPrivate 
60 {
61   gchar         *title;
62   
63   gchar         *fontname;
64   
65   guint         use_font : 1;
66   guint         use_size : 1;
67   guint         show_style : 1;
68   guint         show_size : 1;
69    
70   GtkWidget     *font_dialog;
71   GtkWidget     *inside;
72   GtkWidget     *font_label;
73   GtkWidget     *size_label;
74 };
75
76 /* Signals */
77 enum 
78 {
79   FONT_SET,
80   LAST_SIGNAL
81 };
82
83 enum 
84 {
85   PROP_0,
86   PROP_TITLE,
87   PROP_FONT_NAME,
88   PROP_USE_FONT,
89   PROP_USE_SIZE,
90   PROP_SHOW_STYLE,
91   PROP_SHOW_SIZE
92 };
93
94 /* Prototypes */
95 static void gtk_font_button_finalize               (GObject            *object);
96 static void gtk_font_button_get_property           (GObject            *object,
97                                                     guint               param_id,
98                                                     GValue             *value,
99                                                     GParamSpec         *pspec);
100 static void gtk_font_button_set_property           (GObject            *object,
101                                                     guint               param_id,
102                                                     const GValue       *value,
103                                                     GParamSpec         *pspec);
104
105 static void gtk_font_button_clicked                 (GtkButton         *button);
106
107 /* Dialog response functions */
108 static void dialog_ok_clicked                       (GtkWidget         *widget,
109                                                      gpointer           data);
110 static void dialog_cancel_clicked                   (GtkWidget         *widget,
111                                                      gpointer           data);
112 static void dialog_destroy                          (GtkWidget         *widget,
113                                                      gpointer           data);
114
115 /* Auxiliary functions */
116 static GtkWidget *gtk_font_button_create_inside     (GtkFontButton     *gfs);
117 static void gtk_font_button_label_use_font          (GtkFontButton     *gfs);
118 static void gtk_font_button_update_font_info        (GtkFontButton     *gfs);
119
120 static guint font_button_signals[LAST_SIGNAL] = { 0 };
121
122 G_DEFINE_TYPE (GtkFontButton, gtk_font_button, GTK_TYPE_BUTTON)
123
124 static void
125 gtk_font_button_class_init (GtkFontButtonClass *klass)
126 {
127   GObjectClass *gobject_class;
128   GtkButtonClass *button_class;
129   
130   gobject_class = (GObjectClass *) klass;
131   button_class = (GtkButtonClass *) klass;
132
133   gobject_class->finalize = gtk_font_button_finalize;
134   gobject_class->set_property = gtk_font_button_set_property;
135   gobject_class->get_property = gtk_font_button_get_property;
136   
137   button_class->clicked = gtk_font_button_clicked;
138   
139   klass->font_set = NULL;
140
141   /**
142    * GtkFontButton:title:
143    * 
144    * The title of the font selection dialog.
145    *
146    * Since: 2.4
147    */
148   g_object_class_install_property (gobject_class,
149                                    PROP_TITLE,
150                                    g_param_spec_string ("title",
151                                                         P_("Title"),
152                                                         P_("The title of the font selection dialog"),
153                                                         _("Pick a Font"),
154                                                         (GTK_PARAM_READABLE |
155                                                          GTK_PARAM_WRITABLE)));
156
157   /**
158    * GtkFontButton:font-name:
159    * 
160    * The name of the currently selected font.
161    *
162    * Since: 2.4
163    */
164   g_object_class_install_property (gobject_class,
165                                    PROP_FONT_NAME,
166                                    g_param_spec_string ("font-name",
167                                                         P_("Font name"),
168                                                         P_("The name of the selected font"),
169                                                         P_("Sans 12"),
170                                                         (GTK_PARAM_READABLE |
171                                                          GTK_PARAM_WRITABLE)));
172
173   /**
174    * GtkFontButton:use-font:
175    * 
176    * If this property is set to %TRUE, the label will be drawn 
177    * in the selected font.
178    *
179    * Since: 2.4
180    */
181   g_object_class_install_property (gobject_class,
182                                    PROP_USE_FONT,
183                                    g_param_spec_boolean ("use-font",
184                                                          P_("Use font in label"),
185                                                          P_("Whether the label is drawn in the selected font"),
186                                                          FALSE,
187                                                          GTK_PARAM_READWRITE));
188
189   /**
190    * GtkFontButton:use-size:
191    * 
192    * If this property is set to %TRUE, the label will be drawn 
193    * with the selected font size.
194    *
195    * Since: 2.4
196    */
197   g_object_class_install_property (gobject_class,
198                                    PROP_USE_SIZE,
199                                    g_param_spec_boolean ("use-size",
200                                                          P_("Use size in label"),
201                                                          P_("Whether the label is drawn with the selected font size"),
202                                                          FALSE,
203                                                          GTK_PARAM_READWRITE));
204
205   /**
206    * GtkFontButton:show-style:
207    * 
208    * If this property is set to %TRUE, the name of the selected font style 
209    * will be shown in the label. For a more WYSIWYG way to show the selected 
210    * style, see the ::use-font property. 
211    *
212    * Since: 2.4
213    */
214   g_object_class_install_property (gobject_class,
215                                    PROP_SHOW_STYLE,
216                                    g_param_spec_boolean ("show-style",
217                                                          P_("Show style"),
218                                                          P_("Whether the selected font style is shown in the label"),
219                                                          TRUE,
220                                                          GTK_PARAM_READWRITE));
221   /**
222    * GtkFontButton:show-size:
223    * 
224    * If this property is set to %TRUE, the selected font size will be shown 
225    * in the label. For a more WYSIWYG way to show the selected size, see the 
226    * ::use-size property. 
227    *
228    * Since: 2.4
229    */
230   g_object_class_install_property (gobject_class,
231                                    PROP_SHOW_SIZE,
232                                    g_param_spec_boolean ("show-size",
233                                                          P_("Show size"),
234                                                          P_("Whether selected font size is shown in the label"),
235                                                          TRUE,
236                                                          GTK_PARAM_READWRITE));
237
238   /**
239    * GtkFontButton::font-set:
240    * @widget: the object which received the signal.
241    * 
242    * The ::font-set signal is emitted when the user selects a font. 
243    * When handling this signal, use gtk_font_button_get_font_name() 
244    * to find out which font was just selected.
245    *
246    * Note that this signal is only emitted when the <emphasis>user</emphasis>
247    * changes the font. If you need to react to programmatic font changes
248    * as well, use the notify::font-name signal.
249    *
250    * Since: 2.4
251    */
252   font_button_signals[FONT_SET] = g_signal_new (I_("font-set"),
253                                                 G_TYPE_FROM_CLASS (gobject_class),
254                                                 G_SIGNAL_RUN_FIRST,
255                                                 G_STRUCT_OFFSET (GtkFontButtonClass, font_set),
256                                                 NULL, NULL,
257                                                 g_cclosure_marshal_VOID__VOID,
258                                                 G_TYPE_NONE, 0);
259   
260   g_type_class_add_private (gobject_class, sizeof (GtkFontButtonPrivate));
261 }
262
263 static void
264 gtk_font_button_init (GtkFontButton *font_button)
265 {
266   font_button->priv = G_TYPE_INSTANCE_GET_PRIVATE (font_button,
267                                                    GTK_TYPE_FONT_BUTTON,
268                                                    GtkFontButtonPrivate);
269
270   /* Initialize fields */
271   font_button->priv->fontname = g_strdup (_("Sans 12"));
272   font_button->priv->use_font = FALSE;
273   font_button->priv->use_size = FALSE;
274   font_button->priv->show_style = TRUE;
275   font_button->priv->show_size = TRUE;
276   font_button->priv->font_dialog = NULL;
277   font_button->priv->title = g_strdup (_("Pick a Font"));
278
279   font_button->priv->inside = gtk_font_button_create_inside (font_button);
280   gtk_container_add (GTK_CONTAINER (font_button), font_button->priv->inside);
281
282   gtk_font_button_update_font_info (font_button);  
283 }
284
285
286 static void
287 gtk_font_button_finalize (GObject *object)
288 {
289   GtkFontButton *font_button = GTK_FONT_BUTTON (object);
290
291   if (font_button->priv->font_dialog != NULL) 
292     gtk_widget_destroy (font_button->priv->font_dialog);
293   font_button->priv->font_dialog = NULL;
294
295   g_free (font_button->priv->fontname);
296   font_button->priv->fontname = NULL;
297   
298   g_free (font_button->priv->title);
299   font_button->priv->title = NULL;
300   
301   G_OBJECT_CLASS (gtk_font_button_parent_class)->finalize (object);
302 }
303
304 static void
305 gtk_font_button_set_property (GObject      *object,
306                               guint         param_id,
307                               const GValue *value,
308                               GParamSpec   *pspec)
309 {
310   GtkFontButton *font_button = GTK_FONT_BUTTON (object);
311   
312   switch (param_id) 
313     {
314     case PROP_TITLE:
315       gtk_font_button_set_title (font_button, g_value_get_string (value));
316       break;
317     case PROP_FONT_NAME:
318       gtk_font_button_set_font_name (font_button, g_value_get_string (value));
319       break;
320     case PROP_USE_FONT:
321       gtk_font_button_set_use_font (font_button, g_value_get_boolean (value));
322       break;
323     case PROP_USE_SIZE:
324       gtk_font_button_set_use_size (font_button, g_value_get_boolean (value));
325       break;
326     case PROP_SHOW_STYLE:
327       gtk_font_button_set_show_style (font_button, g_value_get_boolean (value));
328       break;
329     case PROP_SHOW_SIZE:
330       gtk_font_button_set_show_size (font_button, g_value_get_boolean (value));
331       break;
332     default:
333       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
334       break;
335   }
336 }
337
338 static void
339 gtk_font_button_get_property (GObject    *object,
340                               guint       param_id,
341                               GValue     *value,
342                               GParamSpec *pspec)
343 {
344   GtkFontButton *font_button = GTK_FONT_BUTTON (object);
345   
346   switch (param_id) 
347     {
348     case PROP_TITLE:
349       g_value_set_string (value, gtk_font_button_get_title (font_button));
350       break;
351     case PROP_FONT_NAME:
352       g_value_set_string (value, gtk_font_button_get_font_name (font_button));
353       break;
354     case PROP_USE_FONT:
355       g_value_set_boolean (value, gtk_font_button_get_use_font (font_button));
356       break;
357     case PROP_USE_SIZE:
358       g_value_set_boolean (value, gtk_font_button_get_use_size (font_button));
359       break;
360     case PROP_SHOW_STYLE:
361       g_value_set_boolean (value, gtk_font_button_get_show_style (font_button));
362       break;
363     case PROP_SHOW_SIZE:
364       g_value_set_boolean (value, gtk_font_button_get_show_size (font_button));
365       break;
366     default:
367       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
368       break;
369     }
370
371
372
373 /**
374  * gtk_font_button_new:
375  *
376  * Creates a new font picker widget.
377  *
378  * Returns: a new font picker widget.
379  *
380  * Since: 2.4
381  */
382 GtkWidget *
383 gtk_font_button_new (void)
384 {
385   return g_object_new (GTK_TYPE_FONT_BUTTON, NULL);
386
387
388 /**
389  * gtk_font_button_new_with_font:
390  * @fontname: Name of font to display in font selection dialog
391  *
392  * Creates a new font picker widget.
393  *
394  * Returns: a new font picker widget.
395  *
396  * Since: 2.4
397  */
398 GtkWidget *
399 gtk_font_button_new_with_font (const gchar *fontname)
400 {
401   return g_object_new (GTK_TYPE_FONT_BUTTON, "font-name", fontname, NULL);
402
403
404 /**
405  * gtk_font_button_set_title:
406  * @font_button: a #GtkFontButton
407  * @title: a string containing the font selection dialog title
408  *
409  * Sets the title for the font selection dialog.  
410  *
411  * Since: 2.4
412  */
413 void
414 gtk_font_button_set_title (GtkFontButton *font_button, 
415                            const gchar   *title)
416 {
417   gchar *old_title;
418   g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
419   
420   old_title = font_button->priv->title;
421   font_button->priv->title = g_strdup (title);
422   g_free (old_title);
423   
424   if (font_button->priv->font_dialog)
425     gtk_window_set_title (GTK_WINDOW (font_button->priv->font_dialog),
426                           font_button->priv->title);
427
428   g_object_notify (G_OBJECT (font_button), "title");
429
430
431 /**
432  * gtk_font_button_get_title:
433  * @font_button: a #GtkFontButton
434  *
435  * Retrieves the title of the font selection dialog.
436  *
437  * Returns: an internal copy of the title string which must not be freed.
438  *
439  * Since: 2.4
440  */
441 G_CONST_RETURN gchar*
442 gtk_font_button_get_title (GtkFontButton *font_button)
443 {
444   g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), NULL);
445
446   return font_button->priv->title;
447
448
449 /**
450  * gtk_font_button_get_use_font:
451  * @font_button: a #GtkFontButton
452  *
453  * Returns whether the selected font is used in the label.
454  *
455  * Returns: whether the selected font is used in the label.
456  *
457  * Since: 2.4
458  */
459 gboolean
460 gtk_font_button_get_use_font (GtkFontButton *font_button)
461 {
462   g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
463
464   return font_button->priv->use_font;
465
466
467 /**
468  * gtk_font_button_set_use_font:
469  * @font_button: a #GtkFontButton
470  * @use_font: If %TRUE, font name will be written using font chosen.
471  *
472  * If @use_font is %TRUE, the font name will be written using the selected font.  
473  *
474  * Since: 2.4
475  */
476 void  
477 gtk_font_button_set_use_font (GtkFontButton *font_button,
478                               gboolean       use_font)
479 {
480   g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
481   
482   use_font = (use_font != FALSE);
483   
484   if (font_button->priv->use_font != use_font) 
485     {
486       font_button->priv->use_font = use_font;
487
488       if (use_font)
489         gtk_font_button_label_use_font (font_button);
490       else
491         gtk_widget_set_style (font_button->priv->font_label, NULL);
492  
493      g_object_notify (G_OBJECT (font_button), "use-font");
494     }
495
496
497
498 /**
499  * gtk_font_button_get_use_size:
500  * @font_button: a #GtkFontButton
501  *
502  * Returns whether the selected size is used in the label.
503  *
504  * Returns: whether the selected size is used in the label.
505  *
506  * Since: 2.4
507  */
508 gboolean
509 gtk_font_button_get_use_size (GtkFontButton *font_button)
510 {
511   g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
512
513   return font_button->priv->use_size;
514
515
516 /**
517  * gtk_font_button_set_use_size:
518  * @font_button: a #GtkFontButton
519  * @use_size: If %TRUE, font name will be written using the selected size.
520  *
521  * If @use_size is %TRUE, the font name will be written using the selected size.
522  *
523  * Since: 2.4
524  */
525 void  
526 gtk_font_button_set_use_size (GtkFontButton *font_button,
527                               gboolean       use_size)
528 {
529   g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
530   
531   use_size = (use_size != FALSE);
532   if (font_button->priv->use_size != use_size) 
533     {
534       font_button->priv->use_size = use_size;
535
536       if (font_button->priv->use_font)
537         gtk_font_button_label_use_font (font_button);
538
539       g_object_notify (G_OBJECT (font_button), "use-size");
540     }
541
542
543 /**
544  * gtk_font_button_get_show_style:
545  * @font_button: a #GtkFontButton
546  * 
547  * Returns whether the name of the font style will be shown in the label.
548  * 
549  * Return value: whether the font style will be shown in the label.
550  *
551  * Since: 2.4
552  **/
553 gboolean 
554 gtk_font_button_get_show_style (GtkFontButton *font_button)
555 {
556   g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
557
558   return font_button->priv->show_style;
559 }
560
561 /**
562  * gtk_font_button_set_show_style:
563  * @font_button: a #GtkFontButton
564  * @show_style: %TRUE if font style should be displayed in label.
565  *
566  * If @show_style is %TRUE, the font style will be displayed along with name of the selected font.
567  *
568  * Since: 2.4
569  */
570 void
571 gtk_font_button_set_show_style (GtkFontButton *font_button,
572                                 gboolean       show_style)
573 {
574   g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
575   
576   show_style = (show_style != FALSE);
577   if (font_button->priv->show_style != show_style) 
578     {
579       font_button->priv->show_style = show_style;
580       
581       gtk_font_button_update_font_info (font_button);
582   
583       g_object_notify (G_OBJECT (font_button), "show-style");
584     }
585
586
587
588 /**
589  * gtk_font_button_get_show_size:
590  * @font_button: a #GtkFontButton
591  * 
592  * Returns whether the font size will be shown in the label.
593  * 
594  * Return value: whether the font size will be shown in the label.
595  *
596  * Since: 2.4
597  **/
598 gboolean 
599 gtk_font_button_get_show_size (GtkFontButton *font_button)
600 {
601   g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
602
603   return font_button->priv->show_size;
604 }
605
606 /**
607  * gtk_font_button_set_show_size:
608  * @font_button: a #GtkFontButton
609  * @show_size: %TRUE if font size should be displayed in dialog.
610  *
611  * If @show_size is %TRUE, the font size will be displayed along with the name of the selected font.
612  *
613  * Since: 2.4
614  */
615 void
616 gtk_font_button_set_show_size (GtkFontButton *font_button,
617                                gboolean       show_size)
618 {
619   g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
620   
621   show_size = (show_size != FALSE);
622
623   if (font_button->priv->show_size != show_size) 
624     {
625       font_button->priv->show_size = show_size;
626
627       gtk_container_remove (GTK_CONTAINER (font_button), font_button->priv->inside);
628       font_button->priv->inside = gtk_font_button_create_inside (font_button);
629       gtk_container_add (GTK_CONTAINER (font_button), font_button->priv->inside);
630       
631       gtk_font_button_update_font_info (font_button);
632
633       g_object_notify (G_OBJECT (font_button), "show-size");
634     }
635
636
637
638 /**
639  * gtk_font_button_get_font_name:
640  * @font_button: a #GtkFontButton
641  *
642  * Retrieves the name of the currently selected font. This name includes
643  * style and size information as well. If you want to render something
644  * with the font, use this string with pango_font_description_from_string() .
645  * If you're interested in peeking certain values (family name,
646  * style, size, weight) just query these properties from the
647  * #PangoFontDescription object.
648  *
649  * Returns: an internal copy of the font name which must not be freed.
650  *
651  * Since: 2.4
652  */
653 G_CONST_RETURN gchar *
654 gtk_font_button_get_font_name (GtkFontButton *font_button)
655 {
656   g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), NULL);
657   
658   return font_button->priv->fontname;
659 }
660
661 /**
662  * gtk_font_button_set_font_name:
663  * @font_button: a #GtkFontButton
664  * @fontname: Name of font to display in font selection dialog
665  *
666  * Sets or updates the currently-displayed font in font picker dialog.
667  *
668  * Returns: Return value of gtk_font_selection_dialog_set_font_name() if the
669  * font selection dialog exists, otherwise %FALSE.
670  *
671  * Since: 2.4
672  */
673 gboolean 
674 gtk_font_button_set_font_name (GtkFontButton *font_button,
675                                const gchar    *fontname)
676 {
677   gboolean result;
678   gchar *old_fontname;
679
680   g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
681   g_return_val_if_fail (fontname != NULL, FALSE);
682   
683   if (g_ascii_strcasecmp (font_button->priv->fontname, fontname)) 
684     {
685       old_fontname = font_button->priv->fontname;
686       font_button->priv->fontname = g_strdup (fontname);
687       g_free (old_fontname);
688     }
689   
690   gtk_font_button_update_font_info (font_button);
691   
692   if (font_button->priv->font_dialog)
693     result = gtk_font_selection_dialog_set_font_name (GTK_FONT_SELECTION_DIALOG (font_button->priv->font_dialog), 
694                                                       font_button->priv->fontname);
695   else
696     result = FALSE;
697
698   g_object_notify (G_OBJECT (font_button), "font-name");
699
700   return result;
701 }
702
703 static void
704 gtk_font_button_clicked (GtkButton *button)
705 {
706   GtkFontSelectionDialog *font_dialog;
707   GtkFontButton    *font_button = GTK_FONT_BUTTON (button);
708   
709   if (!font_button->priv->font_dialog) 
710     {
711       GtkWidget *parent;
712       
713       parent = gtk_widget_get_toplevel (GTK_WIDGET (font_button));
714       
715       font_button->priv->font_dialog = gtk_font_selection_dialog_new (font_button->priv->title);
716       
717       font_dialog = GTK_FONT_SELECTION_DIALOG (font_button->priv->font_dialog);
718       
719       if (gtk_widget_is_toplevel (parent) && GTK_IS_WINDOW (parent))
720         {
721           if (GTK_WINDOW (parent) != gtk_window_get_transient_for (GTK_WINDOW (font_dialog)))
722             gtk_window_set_transient_for (GTK_WINDOW (font_dialog), GTK_WINDOW (parent));
723                
724           gtk_window_set_modal (GTK_WINDOW (font_dialog),
725                                 gtk_window_get_modal (GTK_WINDOW (parent)));
726         }
727
728       g_signal_connect (gtk_font_selection_dialog_get_ok_button (font_dialog), "clicked",
729                         G_CALLBACK (dialog_ok_clicked), font_button);
730       g_signal_connect (gtk_font_selection_dialog_get_cancel_button (font_dialog), "clicked",
731                         G_CALLBACK (dialog_cancel_clicked), font_button);
732       g_signal_connect (font_dialog, "destroy",
733                         G_CALLBACK (dialog_destroy), font_button);
734     }
735   
736   if (!gtk_widget_get_visible (font_button->priv->font_dialog))
737     {
738       font_dialog = GTK_FONT_SELECTION_DIALOG (font_button->priv->font_dialog);
739       
740       gtk_font_selection_dialog_set_font_name (font_dialog, font_button->priv->fontname);
741       
742     } 
743
744   gtk_window_present (GTK_WINDOW (font_button->priv->font_dialog));
745 }
746
747 static void
748 dialog_ok_clicked (GtkWidget *widget,
749                    gpointer   data)
750 {
751   GtkFontButton *font_button = GTK_FONT_BUTTON (data);
752   
753   gtk_widget_hide (font_button->priv->font_dialog);
754   
755   g_free (font_button->priv->fontname);
756   font_button->priv->fontname = gtk_font_selection_dialog_get_font_name (GTK_FONT_SELECTION_DIALOG (font_button->priv->font_dialog));
757   
758   /* Set label font */
759   gtk_font_button_update_font_info (font_button);
760
761   g_object_notify (G_OBJECT (font_button), "font-name");
762   
763   /* Emit font_set signal */
764   g_signal_emit (font_button, font_button_signals[FONT_SET], 0);
765 }
766
767
768 static void
769 dialog_cancel_clicked (GtkWidget *widget,
770                        gpointer   data)
771 {
772   GtkFontButton *font_button = GTK_FONT_BUTTON (data);
773   
774   gtk_widget_hide (font_button->priv->font_dialog);  
775 }
776
777 static void
778 dialog_destroy (GtkWidget *widget,
779                 gpointer   data)
780 {
781   GtkFontButton *font_button = GTK_FONT_BUTTON (data);
782     
783   /* Dialog will get destroyed so reference is not valid now */
784   font_button->priv->font_dialog = NULL;
785
786
787 static GtkWidget *
788 gtk_font_button_create_inside (GtkFontButton *font_button)
789 {
790   GtkWidget *widget;
791   
792   gtk_widget_push_composite_child ();
793
794   widget = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
795
796   font_button->priv->font_label = gtk_label_new (_("Font"));
797   
798   gtk_label_set_justify (GTK_LABEL (font_button->priv->font_label), GTK_JUSTIFY_LEFT);
799   gtk_box_pack_start (GTK_BOX (widget), font_button->priv->font_label, TRUE, TRUE, 5);
800
801   if (font_button->priv->show_size) 
802     {
803       gtk_box_pack_start (GTK_BOX (widget), gtk_separator_new (GTK_ORIENTATION_VERTICAL), FALSE, FALSE, 0);
804       font_button->priv->size_label = gtk_label_new ("14");
805       gtk_box_pack_start (GTK_BOX (widget), font_button->priv->size_label, FALSE, FALSE, 5);
806     }
807
808   gtk_widget_show_all (widget);
809
810   gtk_widget_pop_composite_child ();
811
812   return widget;
813
814
815 static void
816 gtk_font_button_label_use_font (GtkFontButton *font_button)
817 {
818   PangoFontDescription *desc;
819
820   if (!font_button->priv->use_font)
821     return;
822
823   desc = pango_font_description_from_string (font_button->priv->fontname);
824   
825   if (!font_button->priv->use_size)
826     pango_font_description_unset_fields (desc, PANGO_FONT_MASK_SIZE);
827
828   gtk_widget_modify_font (font_button->priv->font_label, desc);
829
830   pango_font_description_free (desc);
831 }
832
833 static gboolean
834 font_description_style_equal (const PangoFontDescription *a,
835                               const PangoFontDescription *b)
836 {
837   return (pango_font_description_get_weight (a) == pango_font_description_get_weight (b) &&
838           pango_font_description_get_style (a) == pango_font_description_get_style (b) &&
839           pango_font_description_get_stretch (a) == pango_font_description_get_stretch (b) &&
840           pango_font_description_get_variant (a) == pango_font_description_get_variant (b));
841 }
842
843 static void
844 gtk_font_button_update_font_info (GtkFontButton *font_button)
845 {
846   PangoFontDescription *desc;
847   const gchar *family;
848   gchar *style;
849   gchar *family_style;
850   
851   desc = pango_font_description_from_string (font_button->priv->fontname);
852   family = pango_font_description_get_family (desc);
853   
854 #if 0
855   /* This gives the wrong names, e.g. Italic when the font selection
856    * dialog displayed Oblique.
857    */
858   pango_font_description_unset_fields (desc, PANGO_FONT_MASK_FAMILY | PANGO_FONT_MASK_SIZE);
859   style = pango_font_description_to_string (desc);
860   gtk_label_set_text (GTK_LABEL (font_button->priv->style_label), style);      
861 #endif
862
863   style = NULL;
864   if (font_button->priv->show_style && family) 
865     {
866       PangoFontFamily **families;
867       PangoFontFace **faces;
868       gint n_families, n_faces, i;
869
870       n_families = 0;
871       families = NULL;
872       pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (font_button)),
873                                    &families, &n_families);
874       n_faces = 0;
875       faces = NULL;
876       for (i = 0; i < n_families; i++) 
877         {
878           const gchar *name = pango_font_family_get_name (families[i]);
879           
880           if (!g_ascii_strcasecmp (name, family)) 
881             {
882               pango_font_family_list_faces (families[i], &faces, &n_faces);
883               break;
884             }
885         }
886       g_free (families);
887       
888       for (i = 0; i < n_faces; i++) 
889         {
890           PangoFontDescription *tmp_desc = pango_font_face_describe (faces[i]);
891           
892           if (font_description_style_equal (tmp_desc, desc)) 
893             {
894               style = g_strdup (pango_font_face_get_face_name (faces[i]));
895               pango_font_description_free (tmp_desc);
896               break;
897             }
898           else
899             pango_font_description_free (tmp_desc);
900         }
901       g_free (faces);
902     }
903
904   if (style == NULL || !g_ascii_strcasecmp (style, "Regular"))
905     family_style = g_strdup (family);
906   else
907     family_style = g_strdup_printf ("%s %s", family, style);
908   
909   gtk_label_set_text (GTK_LABEL (font_button->priv->font_label), family_style);
910   
911   g_free (style);
912   g_free (family_style);
913
914   if (font_button->priv->show_size) 
915     {
916       gchar *size = g_strdup_printf ("%g",
917                                      pango_font_description_get_size (desc) / (double)PANGO_SCALE);
918       
919       gtk_label_set_text (GTK_LABEL (font_button->priv->size_label), size);
920       
921       g_free (size);
922     }
923
924   gtk_font_button_label_use_font (font_button);
925   
926   pango_font_description_free (desc);
927