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