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