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