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