]> Pileus Git - ~andy/gtk/blob - gtk/gtkfontchooser.c
GtkFontChooser: Live search implemented
[~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 #include "gtkalignment.h"
61 #include "gtkscale.h"
62 #include "gtkbox.h"
63 #include "gtkspinbutton.h"
64
65
66 /**
67  * SECTION:gtkfontsel
68  * @Short_description: A widget for selecting fonts
69  * @Title: GtkFontSelection
70  * @See_also: #GtkFontSelectionDialog
71  *
72  * The #GtkFontSelection widget lists the available fonts, styles and sizes,
73  * allowing the user to select a font.
74  * It is used in the #GtkFontSelectionDialog widget to provide a dialog box for
75  * selecting fonts.
76  *
77  * To set the font which is initially selected, use
78  * gtk_font_selection_set_font_name().
79  *
80  * To get the selected font use gtk_font_selection_get_font_name().
81  *
82  * To change the text which is shown in the preview area, use
83  * gtk_font_selection_set_preview_text().
84  */
85
86
87 struct _GtkFontSelectionPrivate
88 {
89   GtkWidget *search_entry;
90   GtkWidget *family_face_list;
91   GtkWidget *size_slider;
92   GtkWidget *size_spin;
93   GtkWidget *preview;
94
95   GtkListStore *model;  
96   GtkTreeModel *filter;
97
98   gint             size;
99   PangoFontFace   *face;
100   PangoFontFamily *family;
101 };
102
103
104 struct _GtkFontSelectionDialogPrivate
105 {
106   GtkWidget *fontsel;
107
108   GtkWidget *ok_button;
109   GtkWidget *apply_button;
110   GtkWidget *cancel_button;
111 };
112
113
114 /* We don't enable the font and style entries because they don't add
115  * much in terms of visible effect and have a weird effect on keynav.
116  * the Windows font selector has entries similarly positioned but they
117  * act in conjunction with the associated lists to form a single focus
118  * location.
119  */
120 #undef INCLUDE_FONT_ENTRIES
121
122 /* This is the default text shown in the preview entry, though the user
123    can set it. Remember that some fonts only have capital letters. */
124 #define PREVIEW_TEXT N_("abcdefghijk ABCDEFGHIJK")
125
126 #define DEFAULT_FONT_NAME "Sans 10"
127
128 /* This is the initial fixed height and the top padding of the preview entry */
129 #define PREVIEW_HEIGHT 84
130 #define PREVIEW_TOP_PADDING 6
131
132 /* These are the sizes of the font, style & size lists. */
133 #define FONT_LIST_HEIGHT        136
134 #define FONT_LIST_WIDTH         190
135 #define FONT_STYLE_LIST_WIDTH   170
136 #define FONT_SIZE_LIST_WIDTH    60
137
138 #define ROW_FORMAT_STRING "<span size=\"small\" foreground=\"%s\">%s <i>%s</i></span>\n<span size=\"large\" font_desc=\"%s\">%s</span>"
139
140 /* These are what we use as the standard font sizes, for the size list.
141  */
142 #define FONT_SIZES_LENGTH 25
143 static const guint16 font_sizes[] = {
144   6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, 22, 24, 26, 28,
145   32, 36, 40, 48, 56, 64, 72
146 };
147
148 enum {
149    PROP_0,
150    PROP_FONT_NAME,
151    PROP_PREVIEW_TEXT
152 };
153
154
155 enum {
156   FAMILY_COLUMN,
157   FACE_COLUMN,
158   FAMILY_NAME_COLUMN,
159   TEXT_COLUMN
160 };
161
162 static void    gtk_font_selection_set_property       (GObject         *object,
163                                                       guint            prop_id,
164                                                       const GValue    *value,
165                                                       GParamSpec      *pspec);
166 static void    gtk_font_selection_get_property       (GObject         *object,
167                                                       guint            prop_id,
168                                                       GValue          *value,
169                                                       GParamSpec      *pspec);
170 static void    gtk_font_selection_finalize           (GObject         *object);
171 static void    gtk_font_selection_screen_changed     (GtkWidget       *widget,
172                                                       GdkScreen       *previous_screen);
173 static void    gtk_font_selection_style_updated      (GtkWidget      *widget);
174
175 static void     gtk_font_selection_ref_family            (GtkFontSelection *fontsel,
176                                                           PangoFontFamily  *family);
177 static void     gtk_font_selection_ref_face              (GtkFontSelection *fontsel,
178                                                           PangoFontFace    *face);
179 static void gtk_font_selection_bootstrap_fontlist (GtkFontSelection *fontsel);
180
181 G_DEFINE_TYPE (GtkFontSelection, gtk_font_selection, GTK_TYPE_VBOX)
182
183 static void
184 gtk_font_selection_class_init (GtkFontSelectionClass *klass)
185 {
186   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
187   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
188
189   gobject_class->finalize = gtk_font_selection_finalize;
190   gobject_class->set_property = gtk_font_selection_set_property;
191   gobject_class->get_property = gtk_font_selection_get_property;
192
193   widget_class->screen_changed = gtk_font_selection_screen_changed;
194   widget_class->style_updated = gtk_font_selection_style_updated;
195    
196   g_object_class_install_property (gobject_class,
197                                    PROP_FONT_NAME,
198                                    g_param_spec_string ("font-name",
199                                                         P_("Font name"),
200                                                         P_("The string that represents this font"),
201                                                         DEFAULT_FONT_NAME,
202                                                         GTK_PARAM_READWRITE));
203   g_object_class_install_property (gobject_class,
204                                    PROP_PREVIEW_TEXT,
205                                    g_param_spec_string ("preview-text",
206                                                         P_("Preview text"),
207                                                         P_("The text to display in order to demonstrate the selected font"),
208                                                         _(PREVIEW_TEXT),
209                                                         GTK_PARAM_READWRITE));
210
211   g_type_class_add_private (klass, sizeof (GtkFontSelectionPrivate));
212 }
213
214 static void 
215 gtk_font_selection_set_property (GObject         *object,
216                                  guint            prop_id,
217                                  const GValue    *value,
218                                  GParamSpec      *pspec)
219 {
220   GtkFontSelection *fontsel;
221
222   fontsel = GTK_FONT_SELECTION (object);
223
224   switch (prop_id)
225     {
226     case PROP_FONT_NAME:
227       gtk_font_selection_set_font_name (fontsel, g_value_get_string (value));
228       break;
229     case PROP_PREVIEW_TEXT:
230       gtk_font_selection_set_preview_text (fontsel, g_value_get_string (value));
231       break;
232     default:
233       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
234       break;
235     }
236 }
237
238 static void gtk_font_selection_get_property (GObject         *object,
239                                              guint            prop_id,
240                                              GValue          *value,
241                                              GParamSpec      *pspec)
242 {
243   GtkFontSelection *fontsel;
244
245   fontsel = GTK_FONT_SELECTION (object);
246
247   switch (prop_id)
248     {
249     case PROP_FONT_NAME:
250       g_value_take_string (value, gtk_font_selection_get_font_name (fontsel));
251       break;
252     case PROP_PREVIEW_TEXT:
253       g_value_set_string (value, gtk_font_selection_get_preview_text (fontsel));
254       break;
255     default:
256       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
257       break;
258     }
259 }
260
261 void
262 deleted_text_cb (GtkEntryBuffer *buffer,
263                  guint           position,
264                  guint           n_chars,
265                  gpointer        user_data)
266 {
267   GtkFontSelectionPrivate *priv  = (GtkFontSelectionPrivate*)user_data;
268   GtkWidget               *entry = priv->search_entry;
269   
270   gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter));
271 }
272
273 void
274 inserted_text_cb (GtkEntryBuffer *buffer,
275                   guint           position,
276                   gchar          *chars,
277                   guint           n_chars,
278                   gpointer        user_data) 
279 {
280   GtkFontSelectionPrivate *priv  = (GtkFontSelectionPrivate*)user_data;
281   GtkWidget               *entry = priv->search_entry;
282   
283   gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter));
284 }
285
286 static void
287 gtk_font_selection_init (GtkFontSelection *fontsel)
288 {
289   GtkFontSelectionPrivate *priv;
290   PangoFontDescription    *font_desc;
291   GtkWidget               *scrolled_win;
292   GtkWidget               *alignment;
293   GtkWidget               *preview_and_size;
294   GtkWidget               *size_controls;
295 #if 0
296   GList                   *focus_chain = NULL;
297   AtkObject *atk_obj;
298 #endif
299
300   fontsel->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontsel,
301                                                GTK_TYPE_FONT_SELECTION,
302                                                GtkFontSelectionPrivate);
303   priv = fontsel->priv;
304   gtk_widget_push_composite_child ();
305
306   /* Creating fundamental widgets for the private struct */
307   priv->search_entry = gtk_entry_new ();
308   priv->family_face_list = gtk_tree_view_new ();
309   priv->preview = gtk_entry_new ();
310   priv->size_slider = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL,
311                                                 (gdouble) font_sizes[0],
312                                                 (gdouble) font_sizes[FONT_SIZES_LENGTH - 1],
313                                                 1.0);
314
315   priv->size_spin = gtk_spin_button_new (NULL, 1.0, 0);
316
317
318   /** Bootstrapping widget layout **/
319   /* Main font family/face view */
320   scrolled_win = gtk_scrolled_window_new (NULL, NULL);
321   gtk_container_add (GTK_CONTAINER (scrolled_win), priv->family_face_list);
322
323   /* Alignment for the preview and size controls */
324   alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
325   gtk_alignment_set_padding (GTK_ALIGNMENT (alignment),
326                              PREVIEW_TOP_PADDING, 0, 0, 0);
327
328   preview_and_size = gtk_vbox_new (TRUE, 0);
329   gtk_box_set_homogeneous (GTK_BOX (preview_and_size), FALSE);
330   gtk_box_pack_start (GTK_BOX (preview_and_size), priv->preview, FALSE, TRUE, 0);
331   gtk_widget_set_size_request (priv->preview, -1, PREVIEW_HEIGHT);
332
333   /* Packing the slider and the spin in a hbox */
334   size_controls = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
335   gtk_scale_set_draw_value (GTK_SCALE (priv->size_slider), FALSE);
336   gtk_box_pack_start (GTK_BOX (size_controls), priv->size_slider, TRUE, TRUE, 0);
337   gtk_box_pack_start (GTK_BOX (size_controls), priv->size_spin, FALSE, TRUE, 0);
338
339   gtk_box_pack_start (GTK_BOX (preview_and_size), size_controls, FALSE, FALSE, 0);
340   gtk_container_add (GTK_CONTAINER (alignment), preview_and_size);
341
342   /* Packing everything in the selection */
343   gtk_box_pack_start (GTK_BOX (fontsel), priv->search_entry, FALSE, TRUE, 0);
344   gtk_box_pack_start (GTK_BOX (fontsel), scrolled_win, TRUE, TRUE, 0);
345   gtk_box_pack_start (GTK_BOX (fontsel), GTK_WIDGET(alignment), FALSE, TRUE, 0);
346
347   /* Getting the default size */
348   font_desc  = pango_context_get_font_description (gtk_widget_get_pango_context (GTK_WIDGET (fontsel)));
349   priv->size = pango_font_description_get_size (font_desc);
350   priv->face = NULL;
351   priv->family = NULL;
352
353   gtk_widget_show_all (GTK_WIDGET (fontsel));
354   gtk_widget_hide (GTK_WIDGET (fontsel));
355
356   /* Treeview column and model bootstrapping */
357   gtk_font_selection_bootstrap_fontlist (fontsel);
358   
359   /* Set default preview text */
360   gtk_entry_set_text (GTK_ENTRY (priv->preview),
361                       pango_language_get_sample_string (NULL));
362   
363   /* Set search icon and place holder text */
364   gtk_entry_set_icon_from_stock (GTK_ENTRY (priv->search_entry),
365                                  GTK_ENTRY_ICON_SECONDARY,
366                                  GTK_STOCK_FIND);
367   gtk_entry_set_placeholder_text (GTK_ENTRY (priv->search_entry), N_("Search font name"));
368   
369   /** Callback connections **/
370   /* Connect to callback for the live search text entry */
371   g_signal_connect (G_OBJECT (gtk_entry_get_buffer (GTK_ENTRY (priv->search_entry))),
372                     "deleted-text", G_CALLBACK (deleted_text_cb), (gpointer)priv);
373   g_signal_connect (G_OBJECT (gtk_entry_get_buffer (GTK_ENTRY (priv->search_entry))),
374                     "inserted-text", G_CALLBACK (inserted_text_cb), (gpointer)priv);
375
376   gtk_widget_pop_composite_child();
377 }
378
379 /**
380  * gtk_font_selection_new:
381  *
382  * Creates a new #GtkFontSelection.
383  *
384  * Return value: a n ew #GtkFontSelection
385  */
386 GtkWidget *
387 gtk_font_selection_new (void)
388 {
389   GtkFontSelection *fontsel;
390   
391   fontsel = g_object_new (GTK_TYPE_FONT_SELECTION, NULL);
392   
393   return GTK_WIDGET (fontsel);
394 }
395
396 static int
397 cmp_families (const void *a, const void *b)
398 {
399   const char *a_name = pango_font_family_get_name (*(PangoFontFamily **)a);
400   const char *b_name = pango_font_family_get_name (*(PangoFontFamily **)b);
401
402   return g_utf8_collate (a_name, b_name);
403 }
404
405 static void
406 set_cursor_to_iter (GtkTreeView *view,
407                     GtkTreeIter *iter)
408 {
409   GtkTreeModel *model = gtk_tree_view_get_model (view);
410   GtkTreePath *path = gtk_tree_model_get_path (model, iter);
411
412   gtk_tree_view_set_cursor (view, path, NULL, FALSE);
413
414   gtk_tree_path_free (path);
415 }
416
417 static void 
418 populate_list (GtkTreeView* treeview, GtkListStore* model)
419 {
420   GtkStyleContext *style_context;
421   GdkRGBA          g_color;
422   PangoColor       p_color;
423   gchar            *color_string;
424
425   GtkTreeIter   match_row;
426
427   gint n_families, i;  
428   PangoFontFamily **families;
429
430   GString     *tmp = g_string_new (NULL);
431
432   pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (treeview)),
433                                &families,
434                                &n_families);
435
436   qsort (families, n_families, sizeof (PangoFontFamily *), cmp_families);
437
438   gtk_list_store_clear (model);
439
440   /* Get row header font color */
441   style_context = gtk_widget_get_style_context (GTK_WIDGET (treeview));
442   gtk_style_context_get_color (style_context,
443                                GTK_STATE_FLAG_NORMAL |GTK_STATE_FLAG_INSENSITIVE,
444                                &g_color);
445
446   p_color.red   = (guint16)((gdouble)G_MAXUINT16 * g_color.red);
447   p_color.green = (guint16)((gdouble)G_MAXUINT16 * g_color.green);
448   p_color.blue  = (guint16)((gdouble)G_MAXUINT16 * g_color.blue);
449   color_string  = pango_color_to_string (&p_color);
450
451   /* Iterate over families and faces */
452   for (i=0; i<n_families; i++)
453     {
454       GtkTreeIter     iter;
455       PangoFontFace **faces;
456       int             j, n_faces;
457       const gchar    *fam_name = pango_font_family_get_name (families[i]);
458
459       pango_font_family_list_faces (families[i], &faces, &n_faces);
460       
461       for (j=0; j<n_faces; j++)
462         {
463           PangoFontDescription *pango_desc = pango_font_face_describe (faces[j]);
464           const gchar *face_name = pango_font_face_get_face_name (faces[j]);
465           gchar       *font_desc = pango_font_description_to_string (pango_desc);
466           
467           /* foreground_color, family_name, face_name, desc, sample string */
468           g_string_printf (tmp, ROW_FORMAT_STRING,
469                                 color_string,
470                                 fam_name,
471                                 face_name,
472                                 font_desc,
473                                 PREVIEW_TEXT);
474
475
476           gtk_list_store_append (model, &iter);
477           gtk_list_store_set (model, &iter,
478                               FAMILY_COLUMN, families[i],
479                               FACE_COLUMN, faces[j],
480                               FAMILY_NAME_COLUMN, fam_name,
481                               TEXT_COLUMN, tmp->str,
482                               -1);
483
484           if ((i == 0 && j == 0) ||
485               (!g_ascii_strcasecmp (face_name, "sans") && j == 0))
486             {
487               match_row = iter;
488             }
489
490           pango_font_description_free(pango_desc);
491           g_free (font_desc);
492         }
493
494       g_free (faces);
495     }
496
497   set_cursor_to_iter (treeview, &match_row);
498
499   g_string_free (tmp, TRUE);
500   g_free (color_string);
501   g_free (families);
502 }
503
504 gboolean
505 visible_func (GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
506 {
507   gboolean result = FALSE;
508   GtkFontSelectionPrivate *priv = (GtkFontSelectionPrivate*) data;
509
510   const gchar *search_text = (const gchar*)gtk_entry_get_text (GTK_ENTRY (priv->search_entry));
511   gchar       *font_name;
512   gchar       *font_name_casefold;
513   gchar       *search_text_casefold;
514
515   gtk_tree_model_get (model, iter,
516                       FAMILY_NAME_COLUMN, &font_name,
517                       -1);
518
519   if (font_name == NULL)
520     {
521       g_free (font_name);
522       return FALSE;
523     }
524   
525   font_name_casefold = g_utf8_casefold (font_name, -1);
526   search_text_casefold = g_utf8_casefold (search_text, -1);
527   
528   if (g_strrstr (font_name_casefold, search_text_casefold))
529     result = TRUE;
530
531   g_free (search_text_casefold);
532   g_free (font_name_casefold);
533   g_free (font_name);
534   return result;
535 }
536
537 static void
538 gtk_font_selection_bootstrap_fontlist (GtkFontSelection* fontsel)
539 {
540   GtkTreeView       *treeview = GTK_TREE_VIEW (fontsel->priv->family_face_list);
541   GtkTreeViewColumn *col;
542
543   fontsel->priv->model = gtk_list_store_new (4,
544                                              PANGO_TYPE_FONT_FAMILY,
545                                              PANGO_TYPE_FONT_FACE,
546                                              G_TYPE_STRING,
547                                              G_TYPE_STRING);
548
549   fontsel->priv->filter = gtk_tree_model_filter_new (GTK_TREE_MODEL (fontsel->priv->model),
550                                                      NULL);
551   gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (fontsel->priv->filter),
552                                           visible_func,
553                                           (gpointer)fontsel->priv,
554                                           NULL);
555                                           
556
557   gtk_tree_view_set_model (treeview, GTK_TREE_MODEL (fontsel->priv->filter));
558   
559   gtk_tree_view_set_rules_hint      (treeview, TRUE);
560   gtk_tree_view_set_headers_visible (treeview, FALSE);
561
562   col = gtk_tree_view_column_new_with_attributes ("Family",
563                                                    gtk_cell_renderer_text_new (),
564                                                    "markup", TEXT_COLUMN,
565                                                    NULL);
566   gtk_tree_view_append_column (treeview, col);
567
568   populate_list (treeview, fontsel->priv->model);
569 }
570
571
572 static void
573 gtk_font_selection_finalize (GObject *object)
574 {
575   GtkFontSelection *fontsel = GTK_FONT_SELECTION (object);
576
577   gtk_font_selection_ref_family (fontsel, NULL);
578   gtk_font_selection_ref_face (fontsel, NULL);
579
580   G_OBJECT_CLASS (gtk_font_selection_parent_class)->finalize (object);
581 }
582
583 static void
584 gtk_font_selection_screen_changed (GtkWidget *widget,
585                                   GdkScreen *previous_screen)
586 {
587   return;
588 }
589
590 static void
591 gtk_font_selection_style_updated (GtkWidget *widget)
592 {
593   GTK_WIDGET_CLASS (gtk_font_selection_parent_class)->style_updated (widget);
594
595   return;
596 }
597
598 static void
599 gtk_font_selection_ref_family (GtkFontSelection *fontsel,
600                                PangoFontFamily  *family)
601 {
602   GtkFontSelectionPrivate *priv = fontsel->priv;
603
604   if (family)
605     family = g_object_ref (family);
606   if (priv->family)
607     g_object_unref (priv->family);
608   priv->family = family;
609 }
610
611 static void
612 gtk_font_selection_ref_face (GtkFontSelection *fontsel,
613                              PangoFontFace    *face)
614 {
615   GtkFontSelectionPrivate *priv = fontsel->priv;
616
617   if (face)
618     face = g_object_ref (face);
619   if (priv->face)
620     g_object_unref (priv->face);
621   priv->face = face;
622 }
623
624 /*****************************************************************************
625  * These functions are the main public interface for getting/setting the font.
626  *****************************************************************************/
627
628 /**
629  * gtk_font_selection_get_family_list:
630  * @fontsel: a #GtkFontSelection
631  *
632  * This returns the #GtkTreeView that lists font families, for
633  * example, 'Sans', 'Serif', etc.
634  *
635  * Return value: (transfer none): A #GtkWidget that is part of @fontsel
636  *
637  * Deprecated: 3.2
638  */
639 GtkWidget *
640 gtk_font_selection_get_family_list (GtkFontSelection *fontsel)
641 {
642   g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
643
644   return NULL;
645 }
646
647 /**
648  * gtk_font_selection_get_face_list:
649  * @fontsel: a #GtkFontSelection
650  *
651  * This returns the #GtkTreeView which lists all styles available for
652  * the selected font. For example, 'Regular', 'Bold', etc.
653  * 
654  * Return value: (transfer none): A #GtkWidget that is part of @fontsel
655  *
656  * Deprecated: 3.2
657  */
658 GtkWidget *
659 gtk_font_selection_get_face_list (GtkFontSelection *fontsel)
660 {
661   g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
662
663   return NULL;
664 }
665
666 /**
667  * gtk_font_selection_get_size_entry:
668  * @fontsel: a #GtkFontSelection
669  *
670  * This returns the #GtkEntry used to allow the user to edit the font
671  * number manually instead of selecting it from the list of font sizes.
672  *
673  * Return value: (transfer none): A #GtkWidget that is part of @fontsel
674  *
675  * Deprecated: 3.2
676  */
677 GtkWidget *
678 gtk_font_selection_get_size_entry (GtkFontSelection *fontsel)
679 {
680   g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
681
682   return NULL;
683 }
684
685 /**
686  * gtk_font_selection_get_size_list:
687  * @fontsel: a #GtkFontSelection
688  *
689  * This returns the #GtkTreeeView used to list font sizes.
690  *
691  * Return value: (transfer none): A #GtkWidget that is part of @fontsel
692  *
693  * Deprecated: 3.2
694  */
695 GtkWidget *
696 gtk_font_selection_get_size_list (GtkFontSelection *fontsel)
697 {
698   g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
699
700   return NULL;
701 }
702
703 /**
704  * gtk_font_selection_get_preview_entry:
705  * @fontsel: a #GtkFontSelection
706  *
707  * This returns the #GtkEntry used to display the font as a preview.
708  *
709  * Return value: (transfer none): A #GtkWidget that is part of @fontsel
710  *
711  * Deprecated: 3.2
712  */
713 GtkWidget *
714 gtk_font_selection_get_preview_entry (GtkFontSelection *fontsel)
715 {
716   g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
717
718   return NULL;
719 }
720
721 /**
722  * gtk_font_selection_get_family:
723  * @fontsel: a #GtkFontSelection
724  *
725  * Gets the #PangoFontFamily representing the selected font family.
726  *
727  * Return value: (transfer none): A #PangoFontFamily representing the
728  *     selected font family. Font families are a collection of font
729  *     faces. The returned object is owned by @fontsel and must not
730  *     be modified or freed.
731  *
732  * Since: 2.14
733  */
734 PangoFontFamily *
735 gtk_font_selection_get_family (GtkFontSelection *fontsel)
736 {
737   g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
738
739   return NULL;
740 }
741
742 /**
743  * gtk_font_selection_get_face:
744  * @fontsel: a #GtkFontSelection
745  *
746  * Gets the #PangoFontFace representing the selected font group
747  * details (i.e. family, slant, weight, width, etc).
748  *
749  * Return value: (transfer none): A #PangoFontFace representing the
750  *     selected font group details. The returned object is owned by
751  *     @fontsel and must not be modified or freed.
752  *
753  * Since: 2.14
754  */
755 PangoFontFace *
756 gtk_font_selection_get_face (GtkFontSelection *fontsel)
757 {
758   g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
759
760   return NULL;
761 }
762
763 /**
764  * gtk_font_selection_get_size:
765  * @fontsel: a #GtkFontSelection
766  *
767  * The selected font size.
768  *
769  * Return value: A n integer representing the selected font size,
770  *     or -1 if no font size is selected.
771  *
772  * Since: 2.14
773  **/
774 gint
775 gtk_font_selection_get_size (GtkFontSelection *fontsel)
776 {
777   g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), -1);
778
779   return fontsel->priv->size;
780 }
781
782 /**
783  * gtk_font_selection_get_font_name:
784  * @fontsel: a #GtkFontSelection
785  * 
786  * Gets the currently-selected font name. 
787  *
788  * Note that this can be a different string than what you set with 
789  * gtk_font_selection_set_font_name(), as the font selection widget may 
790  * normalize font names and thus return a string with a different structure. 
791  * For example, "Helvetica Italic Bold 12" could be normalized to 
792  * "Helvetica Bold Italic 12". Use pango_font_description_equal()
793  * if you want to compare two font descriptions.
794  * 
795  * Return value: A string with the name of the current font, or %NULL if 
796  *     no font is selected. You must free this string with g_free().
797  */
798 gchar *
799 gtk_font_selection_get_font_name (GtkFontSelection *fontsel)
800 {
801   return NULL;
802 }
803
804 /* This sets the current font, then selecting the appropriate list rows. */
805
806 /**
807  * gtk_font_selection_set_font_name:
808  * @fontsel: a #GtkFontSelection
809  * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
810  * 
811  * Sets the currently-selected font. 
812  *
813  * Note that the @fontsel needs to know the screen in which it will appear 
814  * for this to work; this can be guaranteed by simply making sure that the 
815  * @fontsel is inserted in a toplevel window before you call this function.
816  * 
817  * Return value: %TRUE if the font could be set successfully; %FALSE if no 
818  *     such font exists or if the @fontsel doesn't belong to a particular 
819  *     screen yet.
820  */
821 gboolean
822 gtk_font_selection_set_font_name (GtkFontSelection *fontsel,
823                                   const gchar      *fontname)
824 {
825 #if 0
826   PangoFontFamily *family = NULL;
827   PangoFontFace *face = NULL;
828   PangoFontDescription *new_desc;
829 #endif
830
831   g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), FALSE);
832
833   return TRUE;
834 }
835
836 /**
837  * gtk_font_selection_get_preview_text:
838  * @fontsel: a #GtkFontSelection
839  *
840  * Gets the text displayed in the preview area.
841  * 
842  * Return value: the text displayed in the preview area. 
843  *     This string is owned by the widget and should not be 
844  *     modified or freed 
845  */
846 G_CONST_RETURN gchar*
847 gtk_font_selection_get_preview_text (GtkFontSelection *fontsel)
848 {
849   return NULL;
850 }
851
852
853 /**
854  * gtk_font_selection_set_preview_text:
855  * @fontsel: a #GtkFontSelection
856  * @text: the text to display in the preview area 
857  *
858  * Sets the text displayed in the preview area.
859  * The @text is used to show how the selected font looks.
860  */
861 void
862 gtk_font_selection_set_preview_text  (GtkFontSelection *fontsel,
863                                       const gchar      *text)
864 {
865 #if 0
866   GtkFontSelectionPrivate *priv;
867
868   g_return_if_fail (GTK_IS_FONT_SELECTION (fontsel));
869   g_return_if_fail (text != NULL);
870
871   priv = fontsel->priv;
872 #endif
873 }
874
875
876 /**
877  * SECTION:gtkfontseldlg
878  * @Short_description: A dialog box for selecting fonts
879  * @Title: GtkFontSelectionDialog
880  * @See_also: #GtkFontSelection, #GtkDialog
881  *
882  * The #GtkFontSelectionDialog widget is a dialog box for selecting a font.
883  *
884  * To set the font which is initially selected, use
885  * gtk_font_selection_dialog_set_font_name().
886  *
887  * To get the selected font use gtk_font_selection_dialog_get_font_name().
888  *
889  * To change the text which is shown in the preview area, use
890  * gtk_font_selection_dialog_set_preview_text().
891  *
892  * <refsect2 id="GtkFontSelectionDialog-BUILDER-UI">
893  * <title>GtkFontSelectionDialog as GtkBuildable</title>
894  * The GtkFontSelectionDialog implementation of the GtkBuildable interface
895  * exposes the embedded #GtkFontSelection as internal child with the
896  * name "font_selection". It also exposes the buttons with the names
897  * "ok_button", "cancel_button" and "apply_button".
898  * </refsect2>
899  */
900
901 static void gtk_font_selection_dialog_buildable_interface_init     (GtkBuildableIface *iface);
902 static GObject * gtk_font_selection_dialog_buildable_get_internal_child (GtkBuildable *buildable,
903                                                                           GtkBuilder   *builder,
904                                                                           const gchar  *childname);
905
906 G_DEFINE_TYPE_WITH_CODE (GtkFontSelectionDialog, gtk_font_selection_dialog,
907                          GTK_TYPE_DIALOG,
908                          G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
909                                                 gtk_font_selection_dialog_buildable_interface_init))
910
911 static GtkBuildableIface *parent_buildable_iface;
912
913 static void
914 gtk_font_selection_dialog_class_init (GtkFontSelectionDialogClass *klass)
915 {
916   g_type_class_add_private (klass, sizeof (GtkFontSelectionDialogPrivate));
917 }
918
919 static void
920 gtk_font_selection_dialog_init (GtkFontSelectionDialog *fontseldiag)
921 {
922   GtkFontSelectionDialogPrivate *priv;
923   GtkDialog *dialog = GTK_DIALOG (fontseldiag);
924   GtkWidget *action_area, *content_area;
925
926   fontseldiag->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontseldiag,
927                                                    GTK_TYPE_FONT_SELECTION_DIALOG,
928                                                    GtkFontSelectionDialogPrivate);
929   priv = fontseldiag->priv;
930
931   content_area = gtk_dialog_get_content_area (dialog);
932   action_area = gtk_dialog_get_action_area (dialog);
933
934   gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
935   gtk_box_set_spacing (GTK_BOX (content_area), 2); /* 2 * 5 + 2 = 12 */
936   gtk_container_set_border_width (GTK_CONTAINER (action_area), 5);
937   gtk_box_set_spacing (GTK_BOX (action_area), 6);
938
939   gtk_widget_push_composite_child ();
940
941   gtk_window_set_resizable (GTK_WINDOW (fontseldiag), TRUE);
942
943   /* Create the content area */
944   priv->fontsel = gtk_font_selection_new ();
945   gtk_container_set_border_width (GTK_CONTAINER (priv->fontsel), 5);
946   gtk_widget_show (priv->fontsel);
947   gtk_box_pack_start (GTK_BOX (content_area),
948                       priv->fontsel, TRUE, TRUE, 0);
949
950   /* Create the action area */
951   priv->cancel_button = gtk_dialog_add_button (dialog,
952                                                GTK_STOCK_CANCEL,
953                                                GTK_RESPONSE_CANCEL);
954
955   priv->apply_button = gtk_dialog_add_button (dialog,
956                                               GTK_STOCK_APPLY,
957                                               GTK_RESPONSE_APPLY);
958   gtk_widget_hide (priv->apply_button);
959
960   priv->ok_button = gtk_dialog_add_button (dialog,
961                                            GTK_STOCK_OK,
962                                            GTK_RESPONSE_OK);
963   gtk_widget_grab_default (priv->ok_button);
964
965   gtk_dialog_set_alternative_button_order (GTK_DIALOG (fontseldiag),
966                                            GTK_RESPONSE_OK,
967                                            GTK_RESPONSE_APPLY,
968                                            GTK_RESPONSE_CANCEL,
969                                            -1);
970
971   gtk_window_set_title (GTK_WINDOW (fontseldiag),
972                         _("Font Selection"));
973
974   gtk_widget_pop_composite_child ();
975 }
976
977 /**
978  * gtk_font_selection_dialog_new:
979  * @title: the title of the dialog window 
980  *
981  * Creates a new #GtkFontSelectionDialog.
982  *
983  * Return value: a new #GtkFontSelectionDialog
984  */
985 GtkWidget*
986 gtk_font_selection_dialog_new (const gchar *title)
987 {
988   GtkFontSelectionDialog *fontseldiag;
989   
990   fontseldiag = g_object_new (GTK_TYPE_FONT_SELECTION_DIALOG, NULL);
991
992   if (title)
993     gtk_window_set_title (GTK_WINDOW (fontseldiag), title);
994   
995   return GTK_WIDGET (fontseldiag);
996 }
997
998 /**
999  * gtk_font_selection_dialog_get_font_selection:
1000  * @fsd: a #GtkFontSelectionDialog
1001  *
1002  * Retrieves the #GtkFontSelection widget embedded in the dialog.
1003  *
1004  * Returns: (transfer none): the embedded #GtkFontSelection
1005  *
1006  * Since: 2.22
1007  **/
1008 GtkWidget*
1009 gtk_font_selection_dialog_get_font_selection (GtkFontSelectionDialog *fsd)
1010 {
1011   g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1012
1013   return fsd->priv->fontsel;
1014 }
1015
1016
1017 /**
1018  * gtk_font_selection_dialog_get_ok_button:
1019  * @fsd: a #GtkFontSelectionDialog
1020  *
1021  * Gets the 'OK' button.
1022  *
1023  * Return value: (transfer none): the #GtkWidget used in the dialog
1024  *     for the 'OK' button.
1025  *
1026  * Since: 2.14
1027  */
1028 GtkWidget *
1029 gtk_font_selection_dialog_get_ok_button (GtkFontSelectionDialog *fsd)
1030 {
1031   g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1032
1033   return fsd->priv->ok_button;
1034 }
1035
1036 /**
1037  * gtk_font_selection_dialog_get_cancel_button:
1038  * @fsd: a #GtkFontSelectionDialog
1039  *
1040  * Gets the 'Cancel' button.
1041  *
1042  * Return value: (transfer none): the #GtkWidget used in the dialog
1043  *     for the 'Cancel' button.
1044  *
1045  * Since: 2.14
1046  */
1047 GtkWidget *
1048 gtk_font_selection_dialog_get_cancel_button (GtkFontSelectionDialog *fsd)
1049 {
1050   g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1051
1052   return fsd->priv->cancel_button;
1053 }
1054
1055 static void
1056 gtk_font_selection_dialog_buildable_interface_init (GtkBuildableIface *iface)
1057 {
1058   parent_buildable_iface = g_type_interface_peek_parent (iface);
1059   iface->get_internal_child = gtk_font_selection_dialog_buildable_get_internal_child;
1060 }
1061
1062 static GObject *
1063 gtk_font_selection_dialog_buildable_get_internal_child (GtkBuildable *buildable,
1064                                                         GtkBuilder   *builder,
1065                                                         const gchar  *childname)
1066 {
1067   GtkFontSelectionDialogPrivate *priv;
1068
1069   priv = GTK_FONT_SELECTION_DIALOG (buildable)->priv;
1070
1071   if (g_strcmp0 (childname, "ok_button") == 0)
1072     return G_OBJECT (priv->ok_button);
1073   else if (g_strcmp0 (childname, "cancel_button") == 0)
1074     return G_OBJECT (priv->cancel_button);
1075   else if (g_strcmp0 (childname, "apply_button") == 0)
1076     return G_OBJECT (priv->apply_button);
1077   else if (g_strcmp0 (childname, "font_selection") == 0)
1078     return G_OBJECT (priv->fontsel);
1079
1080   return parent_buildable_iface->get_internal_child (buildable, builder, childname);
1081 }
1082
1083 /**
1084  * gtk_font_selection_dialog_get_font_name:
1085  * @fsd: a #GtkFontSelectionDialog
1086  * 
1087  * Gets the currently-selected font name.
1088  *
1089  * Note that this can be a different string than what you set with 
1090  * gtk_font_selection_dialog_set_font_name(), as the font selection widget
1091  * may normalize font names and thus return a string with a different 
1092  * structure. For example, "Helvetica Italic Bold 12" could be normalized 
1093  * to "Helvetica Bold Italic 12".  Use pango_font_description_equal()
1094  * if you want to compare two font descriptions.
1095  * 
1096  * Return value: A string with the name of the current font, or %NULL if no 
1097  *     font is selected. You must free this string with g_free().
1098  */
1099 gchar*
1100 gtk_font_selection_dialog_get_font_name (GtkFontSelectionDialog *fsd)
1101 {
1102   GtkFontSelectionDialogPrivate *priv;
1103
1104   g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1105
1106   priv = fsd->priv;
1107
1108   return gtk_font_selection_get_font_name (GTK_FONT_SELECTION (priv->fontsel));
1109 }
1110
1111 /**
1112  * gtk_font_selection_dialog_set_font_name:
1113  * @fsd: a #GtkFontSelectionDialog
1114  * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
1115  *
1116  * Sets the currently selected font. 
1117  * 
1118  * Return value: %TRUE if the font selected in @fsd is now the
1119  *     @fontname specified, %FALSE otherwise. 
1120  */
1121 gboolean
1122 gtk_font_selection_dialog_set_font_name (GtkFontSelectionDialog *fsd,
1123                                          const gchar            *fontname)
1124 {
1125   GtkFontSelectionDialogPrivate *priv;
1126
1127   g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), FALSE);
1128   g_return_val_if_fail (fontname, FALSE);
1129
1130   priv = fsd->priv;
1131
1132   return gtk_font_selection_set_font_name (GTK_FONT_SELECTION (priv->fontsel), fontname);
1133 }
1134
1135 /**
1136  * gtk_font_selection_dialog_get_preview_text:
1137  * @fsd: a #GtkFontSelectionDialog
1138  *
1139  * Gets the text displayed in the preview area.
1140  * 
1141  * Return value: the text displayed in the preview area. 
1142  *     This string is owned by the widget and should not be 
1143  *     modified or freed 
1144  */
1145 G_CONST_RETURN gchar*
1146 gtk_font_selection_dialog_get_preview_text (GtkFontSelectionDialog *fsd)
1147 {
1148   GtkFontSelectionDialogPrivate *priv;
1149
1150   g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1151
1152   priv = fsd->priv;
1153
1154   return gtk_font_selection_get_preview_text (GTK_FONT_SELECTION (priv->fontsel));
1155 }
1156
1157 /**
1158  * gtk_font_selection_dialog_set_preview_text:
1159  * @fsd: a #GtkFontSelectionDialog
1160  * @text: the text to display in the preview area
1161  *
1162  * Sets the text displayed in the preview area. 
1163  */
1164 void
1165 gtk_font_selection_dialog_set_preview_text (GtkFontSelectionDialog *fsd,
1166                                             const gchar            *text)
1167 {
1168   GtkFontSelectionDialogPrivate *priv;
1169
1170   g_return_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd));
1171   g_return_if_fail (text != NULL);
1172
1173   priv = fsd->priv;
1174
1175   gtk_font_selection_set_preview_text (GTK_FONT_SELECTION (priv->fontsel), text);
1176 }