]> Pileus Git - ~andy/gtk/blob - gtk/gtkfontchooser.c
GtkFontChooser: Added main treeview
[~andy/gtk] / gtk / gtkfontchooser.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * Massively updated for Pango by Owen Taylor, May 2000
5  * GtkFontSelection widget for Gtk+, by Damon Chaplin, May 1998.
6  * Based on the GnomeFontSelector widget, by Elliot Lee, but major changes.
7  * The GnomeFontSelector was derived from app/text_tool.c in the GIMP.
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  */
24
25 /*
26  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
27  * file for a list of people on the GTK+ Team.  See the ChangeLog
28  * files for a list of changes.  These files are distributed with
29  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
30  */
31
32 #include "config.h"
33
34 #include <stdlib.h>
35 #include <glib/gprintf.h>
36 #include <string.h>
37
38 #include <atk/atk.h>
39
40 #include "gtkfontsel.h"
41 #include "gtkbutton.h"
42 #include "gtkcellrenderertext.h"
43 #include "gtkentry.h"
44 #include "gtkframe.h"
45 #include "gtkhbbox.h"
46 #include "gtkhbox.h"
47 #include "gtklabel.h"
48 #include "gtkliststore.h"
49 #include "gtkrc.h"
50 #include "gtkstock.h"
51 #include "gtktable.h"
52 #include "gtktreeselection.h"
53 #include "gtktreeview.h"
54 #include "gtkvbox.h"
55 #include "gtkscrolledwindow.h"
56 #include "gtkintl.h"
57 #include "gtkaccessible.h"
58 #include "gtkbuildable.h"
59 #include "gtkprivate.h"
60
61
62 /**
63  * SECTION:gtkfontsel
64  * @Short_description: A widget for selecting fonts
65  * @Title: GtkFontSelection
66  * @See_also: #GtkFontSelectionDialog
67  *
68  * The #GtkFontSelection widget lists the available fonts, styles and sizes,
69  * allowing the user to select a font.
70  * It is used in the #GtkFontSelectionDialog widget to provide a dialog box for
71  * selecting fonts.
72  *
73  * To set the font which is initially selected, use
74  * gtk_font_selection_set_font_name().
75  *
76  * To get the selected font use gtk_font_selection_get_font_name().
77  *
78  * To change the text which is shown in the preview area, use
79  * gtk_font_selection_set_preview_text().
80  */
81
82
83 struct _GtkFontSelectionPrivate
84 {
85   GtkWidget *search_entry;
86   GtkWidget *family_face_list;
87   GtkWidget *size_slider;
88   GtkWidget *size_spin;
89
90   gint             size;
91   PangoFontFace   *face;
92   PangoFontFamily *family;
93 };
94
95
96 struct _GtkFontSelectionDialogPrivate
97 {
98   GtkWidget *fontsel;
99
100   GtkWidget *ok_button;
101   GtkWidget *apply_button;
102   GtkWidget *cancel_button;
103 };
104
105
106 /* We don't enable the font and style entries because they don't add
107  * much in terms of visible effect and have a weird effect on keynav.
108  * the Windows font selector has entries similarly positioned but they
109  * act in conjunction with the associated lists to form a single focus
110  * location.
111  */
112 #undef INCLUDE_FONT_ENTRIES
113
114 /* This is the default text shown in the preview entry, though the user
115    can set it. Remember that some fonts only have capital letters. */
116 #define PREVIEW_TEXT N_("abcdefghijk ABCDEFGHIJK")
117
118 #define DEFAULT_FONT_NAME "Sans 10"
119
120 /* This is the initial and maximum height of the preview entry (it expands
121    when large font sizes are selected). Initial height is also the minimum. */
122 #define INITIAL_PREVIEW_HEIGHT 44
123 #define MAX_PREVIEW_HEIGHT 300
124
125 /* These are the sizes of the font, style & size lists. */
126 #define FONT_LIST_HEIGHT        136
127 #define FONT_LIST_WIDTH         190
128 #define FONT_STYLE_LIST_WIDTH   170
129 #define FONT_SIZE_LIST_WIDTH    60
130
131 /* These are what we use as the standard font sizes, for the size list.
132  */
133 static const guint16 font_sizes[] = {
134   6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, 22, 24, 26, 28,
135   32, 36, 40, 48, 56, 64, 72
136 };
137
138 enum {
139    PROP_0,
140    PROP_FONT_NAME,
141    PROP_PREVIEW_TEXT
142 };
143
144
145 enum {
146   FAMILY_COLUMN,
147   FAMILY_NAME_COLUMN
148 };
149
150 enum {
151   FACE_COLUMN,
152   FACE_NAME_COLUMN
153 };
154
155 enum {
156   SIZE_COLUMN
157 };
158
159 static void    gtk_font_selection_set_property       (GObject         *object,
160                                                       guint            prop_id,
161                                                       const GValue    *value,
162                                                       GParamSpec      *pspec);
163 static void    gtk_font_selection_get_property       (GObject         *object,
164                                                       guint            prop_id,
165                                                       GValue          *value,
166                                                       GParamSpec      *pspec);
167 static void    gtk_font_selection_finalize           (GObject         *object);
168 static void    gtk_font_selection_screen_changed     (GtkWidget       *widget,
169                                                       GdkScreen       *previous_screen);
170 static void    gtk_font_selection_style_updated      (GtkWidget      *widget);
171
172 static void     gtk_font_selection_ref_family            (GtkFontSelection *fontsel,
173                                                           PangoFontFamily  *family);
174 static void     gtk_font_selection_ref_face              (GtkFontSelection *fontsel,
175                                                           PangoFontFace    *face);
176
177 G_DEFINE_TYPE (GtkFontSelection, gtk_font_selection, GTK_TYPE_VBOX)
178
179 static void
180 gtk_font_selection_class_init (GtkFontSelectionClass *klass)
181 {
182   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
183   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
184
185   gobject_class->finalize = gtk_font_selection_finalize;
186   gobject_class->set_property = gtk_font_selection_set_property;
187   gobject_class->get_property = gtk_font_selection_get_property;
188
189   widget_class->screen_changed = gtk_font_selection_screen_changed;
190   widget_class->style_updated = gtk_font_selection_style_updated;
191    
192   g_object_class_install_property (gobject_class,
193                                    PROP_FONT_NAME,
194                                    g_param_spec_string ("font-name",
195                                                         P_("Font name"),
196                                                         P_("The string that represents this font"),
197                                                         DEFAULT_FONT_NAME,
198                                                         GTK_PARAM_READWRITE));
199   g_object_class_install_property (gobject_class,
200                                    PROP_PREVIEW_TEXT,
201                                    g_param_spec_string ("preview-text",
202                                                         P_("Preview text"),
203                                                         P_("The text to display in order to demonstrate the selected font"),
204                                                         _(PREVIEW_TEXT),
205                                                         GTK_PARAM_READWRITE));
206
207   g_type_class_add_private (klass, sizeof (GtkFontSelectionPrivate));
208 }
209
210 static void 
211 gtk_font_selection_set_property (GObject         *object,
212                                  guint            prop_id,
213                                  const GValue    *value,
214                                  GParamSpec      *pspec)
215 {
216   GtkFontSelection *fontsel;
217
218   fontsel = GTK_FONT_SELECTION (object);
219
220   switch (prop_id)
221     {
222     case PROP_FONT_NAME:
223       gtk_font_selection_set_font_name (fontsel, g_value_get_string (value));
224       break;
225     case PROP_PREVIEW_TEXT:
226       gtk_font_selection_set_preview_text (fontsel, g_value_get_string (value));
227       break;
228     default:
229       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
230       break;
231     }
232 }
233
234 static void gtk_font_selection_get_property (GObject         *object,
235                                              guint            prop_id,
236                                              GValue          *value,
237                                              GParamSpec      *pspec)
238 {
239   GtkFontSelection *fontsel;
240
241   fontsel = GTK_FONT_SELECTION (object);
242
243   switch (prop_id)
244     {
245     case PROP_FONT_NAME:
246       g_value_take_string (value, gtk_font_selection_get_font_name (fontsel));
247       break;
248     case PROP_PREVIEW_TEXT:
249       g_value_set_string (value, gtk_font_selection_get_preview_text (fontsel));
250       break;
251     default:
252       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
253       break;
254     }
255 }
256
257 /* Handles key press events on the lists, so that we can trap Enter to
258  * activate the default button on our own.
259  */
260 static gboolean
261 list_row_activated (GtkWidget *widget)
262 {
263   GtkWidget *default_widget, *focus_widget;
264   GtkWindow *window;
265   
266   window = GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (widget)));
267   if (!gtk_widget_is_toplevel (GTK_WIDGET (window)))
268     window = NULL;
269
270   if (window)
271     {
272       default_widget = gtk_window_get_default_widget (window);
273       focus_widget = gtk_window_get_focus (window);
274
275       if (widget != default_widget &&
276           !(widget == focus_widget && (!default_widget || !gtk_widget_get_sensitive (default_widget))))
277         gtk_window_activate_default (window);
278     }
279
280   return TRUE;
281 }
282
283 static void
284 gtk_font_selection_init (GtkFontSelection *fontsel)
285 {
286   GtkFontSelectionPrivate *priv;
287   GtkWidget *scrolled_win;
288   GList *focus_chain = NULL;
289   AtkObject *atk_obj;
290
291   fontsel->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontsel,
292                                                GTK_TYPE_FONT_SELECTION,
293                                                GtkFontSelectionPrivate);
294   priv = fontsel->priv;
295   gtk_widget_push_composite_child ();
296
297   priv->search_entry = gtk_entry_new ();
298   priv->family_face_list = gtk_tree_view_new ();
299   priv->size_slider = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL,
300                                                 font_sizes[0],
301                                                 font_sizes[(sizeof (font_sizes) /
302                                                             sizeof (guint16)) - 1],
303                                                 1);
304   priv->size_spin = gtk_spin_button_new_with_range (font_sizes[0],
305                                                 font_sizes[(sizeof (font_sizes) /
306                                                             sizeof (guint16)) - 1],
307                                                 1);
308
309   scrolled_win = gtk_scrolled_window_new (NULL, NULL);
310   gtk_container_add (GTK_CONTAINER (scrolled_win), priv->family_face_list);
311
312   gtk_box_pack_start (GTK_BOX (fontsel), priv->search_entry, FALSE, TRUE, 0);
313   gtk_box_pack_start (GTK_BOX (fontsel), scrolled_win, TRUE, TRUE, 0);
314   
315
316   priv->size = 12 * PANGO_SCALE;
317   priv->face = NULL;
318   priv->family = NULL;
319
320   gtk_widget_show_all (GTK_WIDGET (fontsel));
321   gtk_widget_hide (GTK_WIDGET (fontsel));
322
323   gtk_widget_pop_composite_child();
324 }
325
326 /**
327  * gtk_font_selection_new:
328  *
329  * Creates a new #GtkFontSelection.
330  *
331  * Return value: a n ew #GtkFontSelection
332  */
333 GtkWidget *
334 gtk_font_selection_new (void)
335 {
336   GtkFontSelection *fontsel;
337   
338   fontsel = g_object_new (GTK_TYPE_FONT_SELECTION, NULL);
339   
340   return GTK_WIDGET (fontsel);
341 }
342
343 static void
344 gtk_font_selection_finalize (GObject *object)
345 {
346   GtkFontSelection *fontsel = GTK_FONT_SELECTION (object);
347
348   gtk_font_selection_ref_family (fontsel, NULL);
349   gtk_font_selection_ref_face (fontsel, NULL);
350
351   G_OBJECT_CLASS (gtk_font_selection_parent_class)->finalize (object);
352 }
353
354 static void
355 gtk_font_selection_screen_changed (GtkWidget *widget,
356                                   GdkScreen *previous_screen)
357 {
358   return;
359 }
360
361 static void
362 gtk_font_selection_style_updated (GtkWidget *widget)
363 {
364   GTK_WIDGET_CLASS (gtk_font_selection_parent_class)->style_updated (widget);
365
366   return;
367 }
368
369 static void
370 gtk_font_selection_ref_family (GtkFontSelection *fontsel,
371                               PangoFontFamily  *family)
372 {
373   GtkFontSelectionPrivate *priv = fontsel->priv;
374 #if 0
375   if (family)
376     family = g_object_ref (family);
377   if (priv->family)
378     g_object_unref (priv->family);
379   priv->family = family;
380 #endif
381 }
382
383 static void
384 gtk_font_selection_ref_face (GtkFontSelection *fontsel,
385                                         PangoFontFace    *face)
386 {
387   GtkFontSelectionPrivate *priv = fontsel->priv;
388 #if 0
389   if (face)
390     face = g_object_ref (face);
391   if (priv->face)
392     g_object_unref (priv->face);
393   priv->face = face;
394 #endif
395 }
396
397 /*****************************************************************************
398  * These functions are the main public interface for getting/setting the font.
399  *****************************************************************************/
400
401 /**
402  * gtk_font_selection_get_family_list:
403  * @fontsel: a #GtkFontSelection
404  *
405  * This returns the #GtkTreeView that lists font families, for
406  * example, 'Sans', 'Serif', etc.
407  *
408  * Return value: (transfer none): A #GtkWidget that is part of @fontsel
409  *
410  * Deprecated: 3.2
411  */
412 GtkWidget *
413 gtk_font_selection_get_family_list (GtkFontSelection *fontsel)
414 {
415   g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
416
417   return NULL;
418 }
419
420 /**
421  * gtk_font_selection_get_face_list:
422  * @fontsel: a #GtkFontSelection
423  *
424  * This returns the #GtkTreeView which lists all styles available for
425  * the selected font. For example, 'Regular', 'Bold', etc.
426  * 
427  * Return value: (transfer none): A #GtkWidget that is part of @fontsel
428  *
429  * Deprecated: 3.2
430  */
431 GtkWidget *
432 gtk_font_selection_get_face_list (GtkFontSelection *fontsel)
433 {
434   g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
435
436   return NULL;
437 }
438
439 /**
440  * gtk_font_selection_get_size_entry:
441  * @fontsel: a #GtkFontSelection
442  *
443  * This returns the #GtkEntry used to allow the user to edit the font
444  * number manually instead of selecting it from the list of font sizes.
445  *
446  * Return value: (transfer none): A #GtkWidget that is part of @fontsel
447  *
448  * Deprecated: 3.2
449  */
450 GtkWidget *
451 gtk_font_selection_get_size_entry (GtkFontSelection *fontsel)
452 {
453   g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
454
455   return NULL;
456 }
457
458 /**
459  * gtk_font_selection_get_size_list:
460  * @fontsel: a #GtkFontSelection
461  *
462  * This returns the #GtkTreeeView used to list font sizes.
463  *
464  * Return value: (transfer none): A #GtkWidget that is part of @fontsel
465  *
466  * Deprecated: 3.2
467  */
468 GtkWidget *
469 gtk_font_selection_get_size_list (GtkFontSelection *fontsel)
470 {
471   g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
472
473   return NULL;
474 }
475
476 /**
477  * gtk_font_selection_get_preview_entry:
478  * @fontsel: a #GtkFontSelection
479  *
480  * This returns the #GtkEntry used to display the font as a preview.
481  *
482  * Return value: (transfer none): A #GtkWidget that is part of @fontsel
483  *
484  * Deprecated: 3.2
485  */
486 GtkWidget *
487 gtk_font_selection_get_preview_entry (GtkFontSelection *fontsel)
488 {
489   g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
490
491   return NULL;
492 }
493
494 /**
495  * gtk_font_selection_get_family:
496  * @fontsel: a #GtkFontSelection
497  *
498  * Gets the #PangoFontFamily representing the selected font family.
499  *
500  * Return value: (transfer none): A #PangoFontFamily representing the
501  *     selected font family. Font families are a collection of font
502  *     faces. The returned object is owned by @fontsel and must not
503  *     be modified or freed.
504  *
505  * Since: 2.14
506  */
507 PangoFontFamily *
508 gtk_font_selection_get_family (GtkFontSelection *fontsel)
509 {
510   g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
511
512   return NULL;
513 }
514
515 /**
516  * gtk_font_selection_get_face:
517  * @fontsel: a #GtkFontSelection
518  *
519  * Gets the #PangoFontFace representing the selected font group
520  * details (i.e. family, slant, weight, width, etc).
521  *
522  * Return value: (transfer none): A #PangoFontFace representing the
523  *     selected font group details. The returned object is owned by
524  *     @fontsel and must not be modified or freed.
525  *
526  * Since: 2.14
527  */
528 PangoFontFace *
529 gtk_font_selection_get_face (GtkFontSelection *fontsel)
530 {
531   g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
532
533   return NULL;
534 }
535
536 /**
537  * gtk_font_selection_get_size:
538  * @fontsel: a #GtkFontSelection
539  *
540  * The selected font size.
541  *
542  * Return value: A n integer representing the selected font size,
543  *     or -1 if no font size is selected.
544  *
545  * Since: 2.14
546  **/
547 gint
548 gtk_font_selection_get_size (GtkFontSelection *fontsel)
549 {
550   g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), -1);
551
552   return NULL;
553 }
554
555 /**
556  * gtk_font_selection_get_font_name:
557  * @fontsel: a #GtkFontSelection
558  * 
559  * Gets the currently-selected font name. 
560  *
561  * Note that this can be a different string than what you set with 
562  * gtk_font_selection_set_font_name(), as the font selection widget may 
563  * normalize font names and thus return a string with a different structure. 
564  * For example, "Helvetica Italic Bold 12" could be normalized to 
565  * "Helvetica Bold Italic 12". Use pango_font_description_equal()
566  * if you want to compare two font descriptions.
567  * 
568  * Return value: A string with the name of the current font, or %NULL if 
569  *     no font is selected. You must free this string with g_free().
570  */
571 gchar *
572 gtk_font_selection_get_font_name (GtkFontSelection *fontsel)
573 {
574   return NULL;
575 }
576
577 /* This sets the current font, then selecting the appropriate list rows. */
578
579 /**
580  * gtk_font_selection_set_font_name:
581  * @fontsel: a #GtkFontSelection
582  * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
583  * 
584  * Sets the currently-selected font. 
585  *
586  * Note that the @fontsel needs to know the screen in which it will appear 
587  * for this to work; this can be guaranteed by simply making sure that the 
588  * @fontsel is inserted in a toplevel window before you call this function.
589  * 
590  * Return value: %TRUE if the font could be set successfully; %FALSE if no 
591  *     such font exists or if the @fontsel doesn't belong to a particular 
592  *     screen yet.
593  */
594 gboolean
595 gtk_font_selection_set_font_name (GtkFontSelection *fontsel,
596                                   const gchar      *fontname)
597 {
598   PangoFontFamily *family = NULL;
599   PangoFontFace *face = NULL;
600   PangoFontDescription *new_desc;
601   
602   g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), FALSE);
603
604   return TRUE;
605 }
606
607 /**
608  * gtk_font_selection_get_preview_text:
609  * @fontsel: a #GtkFontSelection
610  *
611  * Gets the text displayed in the preview area.
612  * 
613  * Return value: the text displayed in the preview area. 
614  *     This string is owned by the widget and should not be 
615  *     modified or freed 
616  */
617 G_CONST_RETURN gchar*
618 gtk_font_selection_get_preview_text (GtkFontSelection *fontsel)
619 {
620   return NULL;
621 }
622
623
624 /**
625  * gtk_font_selection_set_preview_text:
626  * @fontsel: a #GtkFontSelection
627  * @text: the text to display in the preview area 
628  *
629  * Sets the text displayed in the preview area.
630  * The @text is used to show how the selected font looks.
631  */
632 void
633 gtk_font_selection_set_preview_text  (GtkFontSelection *fontsel,
634                                       const gchar      *text)
635 {
636   GtkFontSelectionPrivate *priv;
637
638   g_return_if_fail (GTK_IS_FONT_SELECTION (fontsel));
639   g_return_if_fail (text != NULL);
640
641   priv = fontsel->priv;
642 }
643
644
645 /**
646  * SECTION:gtkfontseldlg
647  * @Short_description: A dialog box for selecting fonts
648  * @Title: GtkFontSelectionDialog
649  * @See_also: #GtkFontSelection, #GtkDialog
650  *
651  * The #GtkFontSelectionDialog widget is a dialog box for selecting a font.
652  *
653  * To set the font which is initially selected, use
654  * gtk_font_selection_dialog_set_font_name().
655  *
656  * To get the selected font use gtk_font_selection_dialog_get_font_name().
657  *
658  * To change the text which is shown in the preview area, use
659  * gtk_font_selection_dialog_set_preview_text().
660  *
661  * <refsect2 id="GtkFontSelectionDialog-BUILDER-UI">
662  * <title>GtkFontSelectionDialog as GtkBuildable</title>
663  * The GtkFontSelectionDialog implementation of the GtkBuildable interface
664  * exposes the embedded #GtkFontSelection as internal child with the
665  * name "font_selection". It also exposes the buttons with the names
666  * "ok_button", "cancel_button" and "apply_button".
667  * </refsect2>
668  */
669
670 static void gtk_font_selection_dialog_buildable_interface_init     (GtkBuildableIface *iface);
671 static GObject * gtk_font_selection_dialog_buildable_get_internal_child (GtkBuildable *buildable,
672                                                                           GtkBuilder   *builder,
673                                                                           const gchar  *childname);
674
675 G_DEFINE_TYPE_WITH_CODE (GtkFontSelectionDialog, gtk_font_selection_dialog,
676                          GTK_TYPE_DIALOG,
677                          G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
678                                                 gtk_font_selection_dialog_buildable_interface_init))
679
680 static GtkBuildableIface *parent_buildable_iface;
681
682 static void
683 gtk_font_selection_dialog_class_init (GtkFontSelectionDialogClass *klass)
684 {
685   g_type_class_add_private (klass, sizeof (GtkFontSelectionDialogPrivate));
686 }
687
688 static void
689 gtk_font_selection_dialog_init (GtkFontSelectionDialog *fontseldiag)
690 {
691   GtkFontSelectionDialogPrivate *priv;
692   GtkDialog *dialog = GTK_DIALOG (fontseldiag);
693   GtkWidget *action_area, *content_area;
694
695   fontseldiag->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontseldiag,
696                                                    GTK_TYPE_FONT_SELECTION_DIALOG,
697                                                    GtkFontSelectionDialogPrivate);
698   priv = fontseldiag->priv;
699
700   content_area = gtk_dialog_get_content_area (dialog);
701   action_area = gtk_dialog_get_action_area (dialog);
702
703   gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
704   gtk_box_set_spacing (GTK_BOX (content_area), 2); /* 2 * 5 + 2 = 12 */
705   gtk_container_set_border_width (GTK_CONTAINER (action_area), 5);
706   gtk_box_set_spacing (GTK_BOX (action_area), 6);
707
708   gtk_widget_push_composite_child ();
709
710   gtk_window_set_resizable (GTK_WINDOW (fontseldiag), TRUE);
711
712   /* Create the content area */
713   priv->fontsel = gtk_font_selection_new ();
714   gtk_container_set_border_width (GTK_CONTAINER (priv->fontsel), 5);
715   gtk_widget_show (priv->fontsel);
716   gtk_box_pack_start (GTK_BOX (content_area),
717                       priv->fontsel, TRUE, TRUE, 0);
718
719   /* Create the action area */
720   priv->cancel_button = gtk_dialog_add_button (dialog,
721                                                GTK_STOCK_CANCEL,
722                                                GTK_RESPONSE_CANCEL);
723
724   priv->apply_button = gtk_dialog_add_button (dialog,
725                                               GTK_STOCK_APPLY,
726                                               GTK_RESPONSE_APPLY);
727   gtk_widget_hide (priv->apply_button);
728
729   priv->ok_button = gtk_dialog_add_button (dialog,
730                                            GTK_STOCK_OK,
731                                            GTK_RESPONSE_OK);
732   gtk_widget_grab_default (priv->ok_button);
733
734   gtk_dialog_set_alternative_button_order (GTK_DIALOG (fontseldiag),
735                                            GTK_RESPONSE_OK,
736                                            GTK_RESPONSE_APPLY,
737                                            GTK_RESPONSE_CANCEL,
738                                            -1);
739
740   gtk_window_set_title (GTK_WINDOW (fontseldiag),
741                         _("Font Selection"));
742
743   gtk_widget_pop_composite_child ();
744 }
745
746 /**
747  * gtk_font_selection_dialog_new:
748  * @title: the title of the dialog window 
749  *
750  * Creates a new #GtkFontSelectionDialog.
751  *
752  * Return value: a new #GtkFontSelectionDialog
753  */
754 GtkWidget*
755 gtk_font_selection_dialog_new (const gchar *title)
756 {
757   GtkFontSelectionDialog *fontseldiag;
758   
759   fontseldiag = g_object_new (GTK_TYPE_FONT_SELECTION_DIALOG, NULL);
760
761   if (title)
762     gtk_window_set_title (GTK_WINDOW (fontseldiag), title);
763   
764   return GTK_WIDGET (fontseldiag);
765 }
766
767 /**
768  * gtk_font_selection_dialog_get_font_selection:
769  * @fsd: a #GtkFontSelectionDialog
770  *
771  * Retrieves the #GtkFontSelection widget embedded in the dialog.
772  *
773  * Returns: (transfer none): the embedded #GtkFontSelection
774  *
775  * Since: 2.22
776  **/
777 GtkWidget*
778 gtk_font_selection_dialog_get_font_selection (GtkFontSelectionDialog *fsd)
779 {
780   g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
781
782   return fsd->priv->fontsel;
783 }
784
785
786 /**
787  * gtk_font_selection_dialog_get_ok_button:
788  * @fsd: a #GtkFontSelectionDialog
789  *
790  * Gets the 'OK' button.
791  *
792  * Return value: (transfer none): the #GtkWidget used in the dialog
793  *     for the 'OK' button.
794  *
795  * Since: 2.14
796  */
797 GtkWidget *
798 gtk_font_selection_dialog_get_ok_button (GtkFontSelectionDialog *fsd)
799 {
800   g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
801
802   return fsd->priv->ok_button;
803 }
804
805 /**
806  * gtk_font_selection_dialog_get_cancel_button:
807  * @fsd: a #GtkFontSelectionDialog
808  *
809  * Gets the 'Cancel' button.
810  *
811  * Return value: (transfer none): the #GtkWidget used in the dialog
812  *     for the 'Cancel' button.
813  *
814  * Since: 2.14
815  */
816 GtkWidget *
817 gtk_font_selection_dialog_get_cancel_button (GtkFontSelectionDialog *fsd)
818 {
819   g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
820
821   return fsd->priv->cancel_button;
822 }
823
824 static void
825 gtk_font_selection_dialog_buildable_interface_init (GtkBuildableIface *iface)
826 {
827   parent_buildable_iface = g_type_interface_peek_parent (iface);
828   iface->get_internal_child = gtk_font_selection_dialog_buildable_get_internal_child;
829 }
830
831 static GObject *
832 gtk_font_selection_dialog_buildable_get_internal_child (GtkBuildable *buildable,
833                                                         GtkBuilder   *builder,
834                                                         const gchar  *childname)
835 {
836   GtkFontSelectionDialogPrivate *priv;
837
838   priv = GTK_FONT_SELECTION_DIALOG (buildable)->priv;
839
840   if (g_strcmp0 (childname, "ok_button") == 0)
841     return G_OBJECT (priv->ok_button);
842   else if (g_strcmp0 (childname, "cancel_button") == 0)
843     return G_OBJECT (priv->cancel_button);
844   else if (g_strcmp0 (childname, "apply_button") == 0)
845     return G_OBJECT (priv->apply_button);
846   else if (g_strcmp0 (childname, "font_selection") == 0)
847     return G_OBJECT (priv->fontsel);
848
849   return parent_buildable_iface->get_internal_child (buildable, builder, childname);
850 }
851
852 /**
853  * gtk_font_selection_dialog_get_font_name:
854  * @fsd: a #GtkFontSelectionDialog
855  * 
856  * Gets the currently-selected font name.
857  *
858  * Note that this can be a different string than what you set with 
859  * gtk_font_selection_dialog_set_font_name(), as the font selection widget
860  * may normalize font names and thus return a string with a different 
861  * structure. For example, "Helvetica Italic Bold 12" could be normalized 
862  * to "Helvetica Bold Italic 12".  Use pango_font_description_equal()
863  * if you want to compare two font descriptions.
864  * 
865  * Return value: A string with the name of the current font, or %NULL if no 
866  *     font is selected. You must free this string with g_free().
867  */
868 gchar*
869 gtk_font_selection_dialog_get_font_name (GtkFontSelectionDialog *fsd)
870 {
871   GtkFontSelectionDialogPrivate *priv;
872
873   g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
874
875   priv = fsd->priv;
876
877   return gtk_font_selection_get_font_name (GTK_FONT_SELECTION (priv->fontsel));
878 }
879
880 /**
881  * gtk_font_selection_dialog_set_font_name:
882  * @fsd: a #GtkFontSelectionDialog
883  * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
884  *
885  * Sets the currently selected font. 
886  * 
887  * Return value: %TRUE if the font selected in @fsd is now the
888  *     @fontname specified, %FALSE otherwise. 
889  */
890 gboolean
891 gtk_font_selection_dialog_set_font_name (GtkFontSelectionDialog *fsd,
892                                          const gchar            *fontname)
893 {
894   GtkFontSelectionDialogPrivate *priv;
895
896   g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), FALSE);
897   g_return_val_if_fail (fontname, FALSE);
898
899   priv = fsd->priv;
900
901   return gtk_font_selection_set_font_name (GTK_FONT_SELECTION (priv->fontsel), fontname);
902 }
903
904 /**
905  * gtk_font_selection_dialog_get_preview_text:
906  * @fsd: a #GtkFontSelectionDialog
907  *
908  * Gets the text displayed in the preview area.
909  * 
910  * Return value: the text displayed in the preview area. 
911  *     This string is owned by the widget and should not be 
912  *     modified or freed 
913  */
914 G_CONST_RETURN gchar*
915 gtk_font_selection_dialog_get_preview_text (GtkFontSelectionDialog *fsd)
916 {
917   GtkFontSelectionDialogPrivate *priv;
918
919   g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
920
921   priv = fsd->priv;
922
923   return gtk_font_selection_get_preview_text (GTK_FONT_SELECTION (priv->fontsel));
924 }
925
926 /**
927  * gtk_font_selection_dialog_set_preview_text:
928  * @fsd: a #GtkFontSelectionDialog
929  * @text: the text to display in the preview area
930  *
931  * Sets the text displayed in the preview area. 
932  */
933 void
934 gtk_font_selection_dialog_set_preview_text (GtkFontSelectionDialog *fsd,
935                                             const gchar            *text)
936 {
937   GtkFontSelectionDialogPrivate *priv;
938
939   g_return_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd));
940   g_return_if_fail (text != NULL);
941
942   priv = fsd->priv;
943
944   gtk_font_selection_set_preview_text (GTK_FONT_SELECTION (priv->fontsel), text);
945 }