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