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