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