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