1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
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.
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/.
35 #include <glib/gprintf.h>
40 #undef GTK_DISABLE_DEPRECATED
41 #define GDK_DISABLE_DEPRECATION_WARNINGS
43 #include "gtkfontsel.h"
45 #include "gtkbutton.h"
46 #include "gtkcellrenderertext.h"
50 #include "gtkliststore.h"
53 #include "gtktreeselection.h"
54 #include "gtktreeview.h"
55 #include "gtkscrolledwindow.h"
57 #include "gtkaccessible.h"
58 #include "gtkbuildable.h"
59 #include "gtkorientable.h"
60 #include "gtkprivate.h"
65 * @Short_description: Deprecated widget for selecting fonts
66 * @Title: GtkFontSelection
67 * @See_also: #GtkFontSelectionDialog, #GtkFontChooser
69 * The #GtkFontSelection widget lists the available fonts, styles and sizes,
70 * allowing the user to select a font.
71 * It is used in the #GtkFontSelectionDialog widget to provide a dialog box for
74 * To set the font which is initially selected, use
75 * gtk_font_selection_set_font_name().
77 * To get the selected font use gtk_font_selection_get_font_name().
79 * To change the text which is shown in the preview area, use
80 * gtk_font_selection_set_preview_text().
82 * In GTK+ 3.2, #GtkFontSelection has been deprecated in favor of
87 struct _GtkFontSelectionPrivate
89 GtkWidget *font_entry; /* Used _get_family_entry() for consistency, -mr */
90 GtkWidget *font_style_entry; /* Used _get_face_entry() for consistency, -mr */
92 GtkWidget *size_entry;
93 GtkWidget *preview_entry;
95 GtkWidget *family_list;
99 PangoFontFamily *family; /* Current family */
100 PangoFontFace *face; /* Current face */
106 struct _GtkFontSelectionDialogPrivate
110 GtkWidget *ok_button;
111 GtkWidget *apply_button;
112 GtkWidget *cancel_button;
116 /* We don't enable the font and style entries because they don't add
117 * much in terms of visible effect and have a weird effect on keynav.
118 * the Windows font selector has entries similarly positioned but they
119 * act in conjunction with the associated lists to form a single focus
122 #undef INCLUDE_FONT_ENTRIES
124 /* This is the default text shown in the preview entry, though the user
125 can set it. Remember that some fonts only have capital letters. */
126 #define PREVIEW_TEXT N_("abcdefghijk ABCDEFGHIJK")
128 #define DEFAULT_FONT_NAME "Sans 10"
130 /* This is the initial and maximum height of the preview entry (it expands
131 when large font sizes are selected). Initial height is also the minimum. */
132 #define INITIAL_PREVIEW_HEIGHT 44
133 #define MAX_PREVIEW_HEIGHT 300
135 /* These are the sizes of the font, style & size lists. */
136 #define FONT_LIST_HEIGHT 136
137 #define FONT_LIST_WIDTH 190
138 #define FONT_STYLE_LIST_WIDTH 170
139 #define FONT_SIZE_LIST_WIDTH 60
141 /* These are what we use as the standard font sizes, for the size list.
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
169 static void gtk_font_selection_set_property (GObject *object,
173 static void gtk_font_selection_get_property (GObject *object,
177 static void gtk_font_selection_finalize (GObject *object);
178 static void gtk_font_selection_screen_changed (GtkWidget *widget,
179 GdkScreen *previous_screen);
180 static void gtk_font_selection_style_updated (GtkWidget *widget);
182 /* These are the callbacks & related functions. */
183 static void gtk_font_selection_select_font (GtkTreeSelection *selection,
185 static void gtk_font_selection_show_available_fonts (GtkFontSelection *fs);
187 static void gtk_font_selection_show_available_styles (GtkFontSelection *fs);
188 static void gtk_font_selection_select_best_style (GtkFontSelection *fs,
190 static void gtk_font_selection_select_style (GtkTreeSelection *selection,
193 static void gtk_font_selection_select_best_size (GtkFontSelection *fs);
194 static void gtk_font_selection_show_available_sizes (GtkFontSelection *fs,
195 gboolean first_time);
196 static void gtk_font_selection_size_activate (GtkWidget *w,
198 static gboolean gtk_font_selection_size_focus_out (GtkWidget *w,
199 GdkEventFocus *event,
201 static void gtk_font_selection_select_size (GtkTreeSelection *selection,
204 static void gtk_font_selection_scroll_on_map (GtkWidget *w,
207 static void gtk_font_selection_preview_changed (GtkWidget *entry,
208 GtkFontSelection *fontsel);
209 static void gtk_font_selection_scroll_to_selection (GtkFontSelection *fontsel);
212 /* Misc. utility functions. */
213 static void gtk_font_selection_load_font (GtkFontSelection *fs);
214 static void gtk_font_selection_update_preview (GtkFontSelection *fs);
216 static PangoFontDescription *gtk_font_selection_get_font_description (GtkFontSelection *fontsel);
217 static gboolean gtk_font_selection_select_font_desc (GtkFontSelection *fontsel,
218 PangoFontDescription *new_desc,
219 PangoFontFamily **pfamily,
220 PangoFontFace **pface);
221 static void gtk_font_selection_reload_fonts (GtkFontSelection *fontsel);
222 static void gtk_font_selection_ref_family (GtkFontSelection *fontsel,
223 PangoFontFamily *family);
224 static void gtk_font_selection_ref_face (GtkFontSelection *fontsel,
225 PangoFontFace *face);
227 G_DEFINE_TYPE (GtkFontSelection, gtk_font_selection, GTK_TYPE_BOX)
230 gtk_font_selection_class_init (GtkFontSelectionClass *klass)
232 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
233 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
235 gobject_class->finalize = gtk_font_selection_finalize;
236 gobject_class->set_property = gtk_font_selection_set_property;
237 gobject_class->get_property = gtk_font_selection_get_property;
239 widget_class->screen_changed = gtk_font_selection_screen_changed;
240 widget_class->style_updated = gtk_font_selection_style_updated;
242 g_object_class_install_property (gobject_class,
244 g_param_spec_string ("font-name",
246 P_("The string that represents this font"),
248 GTK_PARAM_READWRITE));
249 g_object_class_install_property (gobject_class,
251 g_param_spec_string ("preview-text",
253 P_("The text to display in order to demonstrate the selected font"),
255 GTK_PARAM_READWRITE));
257 g_type_class_add_private (klass, sizeof (GtkFontSelectionPrivate));
261 gtk_font_selection_set_property (GObject *object,
266 GtkFontSelection *fontsel;
268 fontsel = GTK_FONT_SELECTION (object);
273 gtk_font_selection_set_font_name (fontsel, g_value_get_string (value));
275 case PROP_PREVIEW_TEXT:
276 gtk_font_selection_set_preview_text (fontsel, g_value_get_string (value));
279 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
284 static void gtk_font_selection_get_property (GObject *object,
289 GtkFontSelection *fontsel;
291 fontsel = GTK_FONT_SELECTION (object);
296 g_value_take_string (value, gtk_font_selection_get_font_name (fontsel));
298 case PROP_PREVIEW_TEXT:
299 g_value_set_string (value, gtk_font_selection_get_preview_text (fontsel));
302 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
307 /* Handles key press events on the lists, so that we can trap Enter to
308 * activate the default button on our own.
311 list_row_activated (GtkWidget *widget)
313 GtkWidget *default_widget, *focus_widget;
316 window = GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (widget)));
317 if (!gtk_widget_is_toplevel (GTK_WIDGET (window)))
322 default_widget = gtk_window_get_default_widget (window);
323 focus_widget = gtk_window_get_focus (window);
325 if (widget != default_widget &&
326 !(widget == focus_widget && (!default_widget || !gtk_widget_get_sensitive (default_widget))))
327 gtk_window_activate_default (window);
334 gtk_font_selection_init (GtkFontSelection *fontsel)
336 GtkFontSelectionPrivate *priv;
337 GtkWidget *scrolled_win;
339 GtkWidget *table, *label;
340 GtkWidget *font_label, *style_label;
343 GtkTreeViewColumn *column;
344 GList *focus_chain = NULL;
347 fontsel->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontsel,
348 GTK_TYPE_FONT_SELECTION,
349 GtkFontSelectionPrivate);
350 priv = fontsel->priv;
352 gtk_orientable_set_orientation (GTK_ORIENTABLE (fontsel),
353 GTK_ORIENTATION_VERTICAL);
355 gtk_widget_push_composite_child ();
357 gtk_box_set_spacing (GTK_BOX (fontsel), 12);
358 priv->size = 12 * PANGO_SCALE;
360 /* Create the table of font, style & size. */
361 table = gtk_table_new (3, 3, FALSE);
362 gtk_widget_show (table);
363 gtk_table_set_row_spacings (GTK_TABLE (table), 6);
364 gtk_table_set_col_spacings (GTK_TABLE (table), 12);
365 gtk_box_pack_start (GTK_BOX (fontsel), table, TRUE, TRUE, 0);
367 #ifdef INCLUDE_FONT_ENTRIES
368 priv->font_entry = gtk_entry_new ();
369 gtk_editable_set_editable (GTK_EDITABLE (priv->font_entry), FALSE);
370 gtk_widget_set_size_request (priv->font_entry, 20, -1);
371 gtk_widget_show (priv->font_entry);
372 gtk_table_attach (GTK_TABLE (table), priv->font_entry, 0, 1, 1, 2,
375 priv->font_style_entry = gtk_entry_new ();
376 gtk_editable_set_editable (GTK_EDITABLE (priv->font_style_entry), FALSE);
377 gtk_widget_set_size_request (priv->font_style_entry, 20, -1);
378 gtk_widget_show (priv->font_style_entry);
379 gtk_table_attach (GTK_TABLE (table), priv->font_style_entry, 1, 2, 1, 2,
381 #endif /* INCLUDE_FONT_ENTRIES */
383 priv->size_entry = gtk_entry_new ();
384 gtk_widget_set_size_request (priv->size_entry, 20, -1);
385 gtk_widget_show (priv->size_entry);
386 gtk_table_attach (GTK_TABLE (table), priv->size_entry, 2, 3, 1, 2,
388 g_signal_connect (priv->size_entry, "activate",
389 G_CALLBACK (gtk_font_selection_size_activate),
391 g_signal_connect_after (priv->size_entry, "focus-out-event",
392 G_CALLBACK (gtk_font_selection_size_focus_out),
395 font_label = gtk_label_new_with_mnemonic (_("_Family:"));
396 gtk_widget_set_halign (font_label, GTK_ALIGN_START);
397 gtk_widget_set_valign (font_label, GTK_ALIGN_CENTER);
398 gtk_widget_show (font_label);
399 gtk_table_attach (GTK_TABLE (table), font_label, 0, 1, 0, 1,
402 style_label = gtk_label_new_with_mnemonic (_("_Style:"));
403 gtk_widget_set_halign (style_label, GTK_ALIGN_START);
404 gtk_widget_set_valign (style_label, GTK_ALIGN_CENTER);
405 gtk_widget_show (style_label);
406 gtk_table_attach (GTK_TABLE (table), style_label, 1, 2, 0, 1,
409 label = gtk_label_new_with_mnemonic (_("Si_ze:"));
410 gtk_label_set_mnemonic_widget (GTK_LABEL (label),
412 gtk_widget_set_halign (label, GTK_ALIGN_START);
413 gtk_widget_set_valign (label, GTK_ALIGN_CENTER);
414 gtk_widget_show (label);
415 gtk_table_attach (GTK_TABLE (table), label, 2, 3, 0, 1,
419 /* Create the lists */
421 model = gtk_list_store_new (2,
422 G_TYPE_OBJECT, /* FAMILY_COLUMN */
423 G_TYPE_STRING); /* FAMILY_NAME_COLUMN */
424 priv->family_list = gtk_tree_view_new_with_model (GTK_TREE_MODEL (model));
425 g_object_unref (model);
427 g_signal_connect (priv->family_list, "row-activated",
428 G_CALLBACK (list_row_activated), fontsel);
430 column = gtk_tree_view_column_new_with_attributes ("Family",
431 gtk_cell_renderer_text_new (),
432 "text", FAMILY_NAME_COLUMN,
434 gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
435 gtk_tree_view_append_column (GTK_TREE_VIEW (priv->family_list), column);
437 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->family_list), FALSE);
438 gtk_tree_selection_set_mode (gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->family_list)),
439 GTK_SELECTION_BROWSE);
441 gtk_label_set_mnemonic_widget (GTK_LABEL (font_label), priv->family_list);
443 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
444 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win), GTK_SHADOW_IN);
445 gtk_widget_set_size_request (scrolled_win,
446 FONT_LIST_WIDTH, FONT_LIST_HEIGHT);
447 gtk_container_add (GTK_CONTAINER (scrolled_win), priv->family_list);
448 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
449 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
450 gtk_widget_show (priv->family_list);
451 gtk_widget_show (scrolled_win);
453 gtk_table_attach (GTK_TABLE (table), scrolled_win, 0, 1, 1, 3,
454 GTK_EXPAND | GTK_FILL,
455 GTK_EXPAND | GTK_FILL, 0, 0);
456 focus_chain = g_list_append (focus_chain, scrolled_win);
458 model = gtk_list_store_new (2,
459 G_TYPE_OBJECT, /* FACE_COLUMN */
460 G_TYPE_STRING); /* FACE_NAME_COLUMN */
461 priv->face_list = gtk_tree_view_new_with_model (GTK_TREE_MODEL (model));
462 g_object_unref (model);
463 g_signal_connect (priv->face_list, "row-activated",
464 G_CALLBACK (list_row_activated), fontsel);
466 gtk_label_set_mnemonic_widget (GTK_LABEL (style_label), priv->face_list);
468 column = gtk_tree_view_column_new_with_attributes ("Face",
469 gtk_cell_renderer_text_new (),
470 "text", FACE_NAME_COLUMN,
472 gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
473 gtk_tree_view_append_column (GTK_TREE_VIEW (priv->face_list), column);
475 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->face_list), FALSE);
476 gtk_tree_selection_set_mode (gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->face_list)),
477 GTK_SELECTION_BROWSE);
479 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
480 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win), GTK_SHADOW_IN);
481 gtk_widget_set_size_request (scrolled_win,
482 FONT_STYLE_LIST_WIDTH, FONT_LIST_HEIGHT);
483 gtk_container_add (GTK_CONTAINER (scrolled_win), priv->face_list);
484 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
485 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
486 gtk_widget_show (priv->face_list);
487 gtk_widget_show (scrolled_win);
488 gtk_table_attach (GTK_TABLE (table), scrolled_win, 1, 2, 1, 3,
489 GTK_EXPAND | GTK_FILL,
490 GTK_EXPAND | GTK_FILL, 0, 0);
491 focus_chain = g_list_append (focus_chain, scrolled_win);
493 focus_chain = g_list_append (focus_chain, priv->size_entry);
495 model = gtk_list_store_new (1, G_TYPE_INT);
496 priv->size_list = gtk_tree_view_new_with_model (GTK_TREE_MODEL (model));
497 g_object_unref (model);
498 g_signal_connect (priv->size_list, "row-activated",
499 G_CALLBACK (list_row_activated), fontsel);
501 column = gtk_tree_view_column_new_with_attributes ("Size",
502 gtk_cell_renderer_text_new (),
505 gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
506 gtk_tree_view_append_column (GTK_TREE_VIEW (priv->size_list), column);
508 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->size_list), FALSE);
509 gtk_tree_selection_set_mode (gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->size_list)),
510 GTK_SELECTION_BROWSE);
512 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
513 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win), GTK_SHADOW_IN);
514 gtk_container_add (GTK_CONTAINER (scrolled_win), priv->size_list);
515 gtk_widget_set_size_request (scrolled_win, -1, FONT_LIST_HEIGHT);
516 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
517 GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
518 gtk_widget_show (priv->size_list);
519 gtk_widget_show (scrolled_win);
520 gtk_table_attach (GTK_TABLE (table), scrolled_win, 2, 3, 2, 3,
521 GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
522 focus_chain = g_list_append (focus_chain, scrolled_win);
524 gtk_container_set_focus_chain (GTK_CONTAINER (table), focus_chain);
525 g_list_free (focus_chain);
527 /* Insert the fonts. */
528 g_signal_connect (gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->family_list)), "changed",
529 G_CALLBACK (gtk_font_selection_select_font), fontsel);
531 g_signal_connect_after (priv->family_list, "map",
532 G_CALLBACK (gtk_font_selection_scroll_on_map),
535 g_signal_connect (gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->face_list)), "changed",
536 G_CALLBACK (gtk_font_selection_select_style), fontsel);
538 g_signal_connect (gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->size_list)), "changed",
539 G_CALLBACK (gtk_font_selection_select_size), fontsel);
540 atk_obj = gtk_widget_get_accessible (priv->size_list);
541 if (GTK_IS_ACCESSIBLE (atk_obj))
543 /* Accessibility support is enabled.
544 * Make the label ATK_RELATON_LABEL_FOR for the size list as well.
546 AtkObject *atk_label;
547 AtkRelationSet *relation_set;
548 AtkRelation *relation;
549 AtkObject *obj_array[1];
551 atk_label = gtk_widget_get_accessible (label);
552 relation_set = atk_object_ref_relation_set (atk_obj);
553 relation = atk_relation_set_get_relation_by_type (relation_set, ATK_RELATION_LABELLED_BY);
556 atk_relation_add_target (relation, atk_label);
560 obj_array[0] = atk_label;
561 relation = atk_relation_new (obj_array, 1, ATK_RELATION_LABELLED_BY);
562 atk_relation_set_add (relation_set, relation);
564 g_object_unref (relation_set);
566 relation_set = atk_object_ref_relation_set (atk_label);
567 relation = atk_relation_set_get_relation_by_type (relation_set, ATK_RELATION_LABEL_FOR);
570 atk_relation_add_target (relation, atk_obj);
574 obj_array[0] = atk_obj;
575 relation = atk_relation_new (obj_array, 1, ATK_RELATION_LABEL_FOR);
576 atk_relation_set_add (relation_set, relation);
578 g_object_unref (relation_set);
581 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
582 gtk_widget_show (vbox);
583 gtk_box_pack_start (GTK_BOX (fontsel), vbox, FALSE, TRUE, 0);
585 /* create the text entry widget */
586 label = gtk_label_new_with_mnemonic (_("_Preview:"));
587 gtk_widget_set_halign (label, GTK_ALIGN_START);
588 gtk_widget_set_valign (label, GTK_ALIGN_CENTER);
589 gtk_widget_show (label);
590 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, TRUE, 0);
592 text_box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
593 gtk_widget_show (text_box);
594 gtk_box_pack_start (GTK_BOX (vbox), text_box, FALSE, TRUE, 0);
596 priv->preview_entry = gtk_entry_new ();
597 gtk_label_set_mnemonic_widget (GTK_LABEL (label), priv->preview_entry);
598 gtk_entry_set_text (GTK_ENTRY (priv->preview_entry), _(PREVIEW_TEXT));
600 gtk_widget_show (priv->preview_entry);
601 g_signal_connect (priv->preview_entry, "changed",
602 G_CALLBACK (gtk_font_selection_preview_changed), fontsel);
603 gtk_widget_set_size_request (priv->preview_entry,
604 -1, INITIAL_PREVIEW_HEIGHT);
605 gtk_box_pack_start (GTK_BOX (text_box), priv->preview_entry,
607 gtk_widget_pop_composite_child();
611 * gtk_font_selection_new:
613 * Creates a new #GtkFontSelection.
615 * Return value: a n ew #GtkFontSelection
618 gtk_font_selection_new (void)
620 GtkFontSelection *fontsel;
622 fontsel = g_object_new (GTK_TYPE_FONT_SELECTION, NULL);
624 return GTK_WIDGET (fontsel);
628 gtk_font_selection_finalize (GObject *object)
630 GtkFontSelection *fontsel = GTK_FONT_SELECTION (object);
632 gtk_font_selection_ref_family (fontsel, NULL);
633 gtk_font_selection_ref_face (fontsel, NULL);
635 G_OBJECT_CLASS (gtk_font_selection_parent_class)->finalize (object);
639 gtk_font_selection_ref_family (GtkFontSelection *fontsel,
640 PangoFontFamily *family)
642 GtkFontSelectionPrivate *priv = fontsel->priv;
645 family = g_object_ref (family);
647 g_object_unref (priv->family);
648 priv->family = family;
651 static void gtk_font_selection_ref_face (GtkFontSelection *fontsel,
654 GtkFontSelectionPrivate *priv = fontsel->priv;
657 face = g_object_ref (face);
659 g_object_unref (priv->face);
664 gtk_font_selection_reload_fonts (GtkFontSelection *fontsel)
666 if (gtk_widget_has_screen (GTK_WIDGET (fontsel)))
668 PangoFontDescription *desc;
669 desc = gtk_font_selection_get_font_description (fontsel);
671 gtk_font_selection_show_available_fonts (fontsel);
672 gtk_font_selection_show_available_sizes (fontsel, TRUE);
673 gtk_font_selection_show_available_styles (fontsel);
675 gtk_font_selection_select_font_desc (fontsel, desc, NULL, NULL);
676 gtk_font_selection_scroll_to_selection (fontsel);
678 pango_font_description_free (desc);
683 gtk_font_selection_screen_changed (GtkWidget *widget,
684 GdkScreen *previous_screen)
686 gtk_font_selection_reload_fonts (GTK_FONT_SELECTION (widget));
690 gtk_font_selection_style_updated (GtkWidget *widget)
692 GTK_WIDGET_CLASS (gtk_font_selection_parent_class)->style_updated (widget);
694 /* Maybe fonts where installed or removed... */
695 gtk_font_selection_reload_fonts (GTK_FONT_SELECTION (widget));
699 gtk_font_selection_preview_changed (GtkWidget *entry,
700 GtkFontSelection *fontsel)
702 g_object_notify (G_OBJECT (fontsel), "preview-text");
706 scroll_to_selection (GtkTreeView *tree_view)
708 GtkTreeSelection *selection = gtk_tree_view_get_selection (tree_view);
712 if (gtk_tree_selection_get_selected (selection, &model, &iter))
714 GtkTreePath *path = gtk_tree_model_get_path (model, &iter);
715 gtk_tree_view_scroll_to_cell (tree_view, path, NULL, TRUE, 0.5, 0.5);
716 gtk_tree_path_free (path);
721 set_cursor_to_iter (GtkTreeView *view,
724 GtkTreeModel *model = gtk_tree_view_get_model (view);
725 GtkTreePath *path = gtk_tree_model_get_path (model, iter);
727 gtk_tree_view_set_cursor (view, path, NULL, FALSE);
729 gtk_tree_path_free (path);
733 gtk_font_selection_scroll_to_selection (GtkFontSelection *fontsel)
735 GtkFontSelectionPrivate *priv = fontsel->priv;
737 /* Try to scroll the font family list to the selected item */
738 scroll_to_selection (GTK_TREE_VIEW (priv->family_list));
740 /* Try to scroll the font family list to the selected item */
741 scroll_to_selection (GTK_TREE_VIEW (priv->face_list));
743 /* Try to scroll the font family list to the selected item */
744 scroll_to_selection (GTK_TREE_VIEW (priv->size_list));
745 /* This is called when the list is mapped. Here we scroll to the current
746 font if necessary. */
750 gtk_font_selection_scroll_on_map (GtkWidget *widget,
753 gtk_font_selection_scroll_to_selection (GTK_FONT_SELECTION (data));
756 /* This is called when a family is selected in the list. */
758 gtk_font_selection_select_font (GtkTreeSelection *selection,
761 GtkFontSelection *fontsel;
762 GtkFontSelectionPrivate *priv;
765 #ifdef INCLUDE_FONT_ENTRIES
766 const gchar *family_name;
769 fontsel = GTK_FONT_SELECTION (data);
770 priv = fontsel->priv;
772 if (gtk_tree_selection_get_selected (selection, &model, &iter))
774 PangoFontFamily *family;
776 gtk_tree_model_get (model, &iter, FAMILY_COLUMN, &family, -1);
777 if (priv->family != family)
779 gtk_font_selection_ref_family (fontsel, family);
781 #ifdef INCLUDE_FONT_ENTRIES
782 family_name = pango_font_family_get_name (priv->family);
783 gtk_entry_set_text (GTK_ENTRY (priv->font_entry), family_name);
786 gtk_font_selection_show_available_styles (fontsel);
787 gtk_font_selection_select_best_style (fontsel, TRUE);
790 g_object_unref (family);
795 cmp_families (const void *a, const void *b)
797 const char *a_name = pango_font_family_get_name (*(PangoFontFamily **)a);
798 const char *b_name = pango_font_family_get_name (*(PangoFontFamily **)b);
800 return g_utf8_collate (a_name, b_name);
804 gtk_font_selection_show_available_fonts (GtkFontSelection *fontsel)
806 GtkFontSelectionPrivate *priv = fontsel->priv;
808 PangoFontFamily **families;
809 PangoFontFamily *match_family = NULL;
811 GtkTreeIter match_row;
813 model = GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (priv->family_list)));
815 pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (fontsel)),
816 &families, &n_families);
817 qsort (families, n_families, sizeof (PangoFontFamily *), cmp_families);
819 gtk_list_store_clear (model);
821 for (i=0; i<n_families; i++)
823 const gchar *name = pango_font_family_get_name (families[i]);
826 gtk_list_store_append (model, &iter);
827 gtk_list_store_set (model, &iter,
828 FAMILY_COLUMN, families[i],
829 FAMILY_NAME_COLUMN, name,
832 if (i == 0 || !g_ascii_strcasecmp (name, "sans"))
834 match_family = families[i];
839 gtk_font_selection_ref_family (fontsel, match_family);
842 set_cursor_to_iter (GTK_TREE_VIEW (priv->family_list), &match_row);
843 #ifdef INCLUDE_FONT_ENTRIES
844 gtk_entry_set_text (GTK_ENTRY (priv->font_entry),
845 pango_font_family_get_name (match_family));
846 #endif /* INCLUDE_FONT_ENTRIES */
853 compare_font_descriptions (const PangoFontDescription *a, const PangoFontDescription *b)
855 int val = strcmp (pango_font_description_get_family (a), pango_font_description_get_family (b));
859 if (pango_font_description_get_weight (a) != pango_font_description_get_weight (b))
860 return pango_font_description_get_weight (a) - pango_font_description_get_weight (b);
862 if (pango_font_description_get_style (a) != pango_font_description_get_style (b))
863 return pango_font_description_get_style (a) - pango_font_description_get_style (b);
865 if (pango_font_description_get_stretch (a) != pango_font_description_get_stretch (b))
866 return pango_font_description_get_stretch (a) - pango_font_description_get_stretch (b);
868 if (pango_font_description_get_variant (a) != pango_font_description_get_variant (b))
869 return pango_font_description_get_variant (a) - pango_font_description_get_variant (b);
875 faces_sort_func (const void *a, const void *b)
877 PangoFontDescription *desc_a = pango_font_face_describe (*(PangoFontFace **)a);
878 PangoFontDescription *desc_b = pango_font_face_describe (*(PangoFontFace **)b);
880 int ord = compare_font_descriptions (desc_a, desc_b);
882 pango_font_description_free (desc_a);
883 pango_font_description_free (desc_b);
889 font_description_style_equal (const PangoFontDescription *a,
890 const PangoFontDescription *b)
892 return (pango_font_description_get_weight (a) == pango_font_description_get_weight (b) &&
893 pango_font_description_get_style (a) == pango_font_description_get_style (b) &&
894 pango_font_description_get_stretch (a) == pango_font_description_get_stretch (b) &&
895 pango_font_description_get_variant (a) == pango_font_description_get_variant (b));
898 /* This fills the font style list with all the possible style combinations
899 for the current font family. */
901 gtk_font_selection_show_available_styles (GtkFontSelection *fontsel)
903 GtkFontSelectionPrivate *priv = fontsel->priv;
905 PangoFontFace **faces;
906 PangoFontDescription *old_desc;
908 GtkTreeIter match_row;
909 PangoFontFace *match_face = NULL;
911 model = GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (priv->face_list)));
914 old_desc = pango_font_face_describe (priv->face);
918 pango_font_family_list_faces (priv->family, &faces, &n_faces);
919 qsort (faces, n_faces, sizeof (PangoFontFace *), faces_sort_func);
921 gtk_list_store_clear (model);
923 for (i=0; i < n_faces; i++)
926 const gchar *str = pango_font_face_get_face_name (faces[i]);
928 gtk_list_store_append (model, &iter);
929 gtk_list_store_set (model, &iter,
930 FACE_COLUMN, faces[i],
931 FACE_NAME_COLUMN, str,
937 match_face = faces[i];
941 PangoFontDescription *tmp_desc = pango_font_face_describe (faces[i]);
943 if (font_description_style_equal (tmp_desc, old_desc))
946 match_face = faces[i];
949 pango_font_description_free (tmp_desc);
954 pango_font_description_free (old_desc);
956 gtk_font_selection_ref_face (fontsel, match_face);
959 #ifdef INCLUDE_FONT_ENTRIES
960 const gchar *str = pango_font_face_get_face_name (priv->face);
962 gtk_entry_set_text (GTK_ENTRY (priv->font_style_entry), str);
964 set_cursor_to_iter (GTK_TREE_VIEW (priv->face_list), &match_row);
970 /* This selects a style when the user selects a font. It just uses the first
971 available style at present. I was thinking of trying to maintain the
972 selected style, e.g. bold italic, when the user selects different fonts.
973 However, the interface is so easy to use now I'm not sure it's worth it.
974 Note: This will load a font. */
976 gtk_font_selection_select_best_style (GtkFontSelection *fontsel,
979 GtkFontSelectionPrivate *priv = fontsel->priv;
983 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->face_list));
985 if (gtk_tree_model_get_iter_first (model, &iter))
987 set_cursor_to_iter (GTK_TREE_VIEW (priv->face_list), &iter);
988 scroll_to_selection (GTK_TREE_VIEW (priv->face_list));
991 gtk_font_selection_show_available_sizes (fontsel, FALSE);
992 gtk_font_selection_select_best_size (fontsel);
996 /* This is called when a style is selected in the list. */
998 gtk_font_selection_select_style (GtkTreeSelection *selection,
1001 GtkFontSelection *fontsel = GTK_FONT_SELECTION (data);
1002 GtkTreeModel *model;
1005 if (gtk_tree_selection_get_selected (selection, &model, &iter))
1007 PangoFontFace *face;
1009 gtk_tree_model_get (model, &iter, FACE_COLUMN, &face, -1);
1010 gtk_font_selection_ref_face (fontsel, face);
1011 g_object_unref (face);
1014 gtk_font_selection_show_available_sizes (fontsel, FALSE);
1015 gtk_font_selection_select_best_size (fontsel);
1019 gtk_font_selection_show_available_sizes (GtkFontSelection *fontsel,
1020 gboolean first_time)
1022 GtkFontSelectionPrivate *priv = fontsel->priv;
1024 GtkListStore *model;
1028 model = GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (priv->size_list)));
1030 /* Insert the standard font sizes */
1033 gtk_list_store_clear (model);
1035 for (i = 0; i < G_N_ELEMENTS (font_sizes); i++)
1039 gtk_list_store_append (model, &iter);
1040 gtk_list_store_set (model, &iter, SIZE_COLUMN, font_sizes[i], -1);
1042 if (font_sizes[i] * PANGO_SCALE == priv->size)
1043 set_cursor_to_iter (GTK_TREE_VIEW (priv->size_list), &iter);
1049 gboolean found = FALSE;
1051 gtk_tree_model_get_iter_first (GTK_TREE_MODEL (model), &iter);
1052 for (i = 0; i < G_N_ELEMENTS (font_sizes) && !found; i++)
1054 if (font_sizes[i] * PANGO_SCALE == priv->size)
1056 set_cursor_to_iter (GTK_TREE_VIEW (priv->size_list), &iter);
1060 gtk_tree_model_iter_next (GTK_TREE_MODEL (model), &iter);
1065 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->size_list));
1066 gtk_tree_selection_unselect_all (selection);
1070 /* Set the entry to the new size, rounding to 1 digit,
1071 * trimming of trailing 0's and a trailing period
1073 g_snprintf (buffer, sizeof (buffer), "%.1f", priv->size / (1.0 * PANGO_SCALE));
1074 if (strchr (buffer, '.'))
1076 p = buffer + strlen (buffer) - 1;
1084 /* Compare, to avoid moving the cursor unecessarily */
1085 if (strcmp (gtk_entry_get_text (GTK_ENTRY (priv->size_entry)), buffer) != 0)
1086 gtk_entry_set_text (GTK_ENTRY (priv->size_entry), buffer);
1090 gtk_font_selection_select_best_size (GtkFontSelection *fontsel)
1092 gtk_font_selection_load_font (fontsel);
1096 gtk_font_selection_set_size (GtkFontSelection *fontsel,
1099 GtkFontSelectionPrivate *priv = fontsel->priv;
1101 if (priv->size != new_size)
1103 priv->size = new_size;
1105 gtk_font_selection_show_available_sizes (fontsel, FALSE);
1106 gtk_font_selection_load_font (fontsel);
1110 /* If the user hits return in the font size entry, we change to the new font
1113 gtk_font_selection_size_activate (GtkWidget *w,
1116 GtkFontSelection *fontsel = GTK_FONT_SELECTION (data);
1117 GtkFontSelectionPrivate *priv = fontsel->priv;
1121 text = gtk_entry_get_text (GTK_ENTRY (priv->size_entry));
1122 new_size = MAX (0.1, atof (text) * PANGO_SCALE + 0.5);
1124 if (priv->size != new_size)
1125 gtk_font_selection_set_size (fontsel, new_size);
1127 list_row_activated (w);
1131 gtk_font_selection_size_focus_out (GtkWidget *w,
1132 GdkEventFocus *event,
1135 GtkFontSelection *fontsel = GTK_FONT_SELECTION (data);
1136 GtkFontSelectionPrivate *priv = fontsel->priv;
1140 text = gtk_entry_get_text (GTK_ENTRY (priv->size_entry));
1141 new_size = MAX (0.1, atof (text) * PANGO_SCALE + 0.5);
1143 gtk_font_selection_set_size (fontsel, new_size);
1148 /* This is called when a size is selected in the list. */
1150 gtk_font_selection_select_size (GtkTreeSelection *selection,
1153 GtkFontSelection *fontsel = GTK_FONT_SELECTION (data);
1154 GtkTreeModel *model;
1158 if (gtk_tree_selection_get_selected (selection, &model, &iter))
1160 gtk_tree_model_get (model, &iter, SIZE_COLUMN, &new_size, -1);
1161 gtk_font_selection_set_size (fontsel, new_size * PANGO_SCALE);
1166 gtk_font_selection_load_font (GtkFontSelection *fontsel)
1168 gtk_font_selection_update_preview (fontsel);
1171 static PangoFontDescription *
1172 gtk_font_selection_get_font_description (GtkFontSelection *fontsel)
1174 GtkFontSelectionPrivate *priv = fontsel->priv;
1175 PangoFontDescription *font_desc;
1179 font_desc = pango_font_face_describe (priv->face);
1180 pango_font_description_set_size (font_desc, priv->size);
1183 font_desc = pango_font_description_from_string (DEFAULT_FONT_NAME);
1188 /* This sets the font in the preview entry to the selected font,
1189 * and tries to make sure that the preview entry is a reasonable
1190 * size, i.e. so that the text can be seen with a bit of space to
1191 * spare. But it tries to avoid resizing the entry every time the
1192 * font changes. This also used to shrink the preview if the font
1193 * size was decreased, but that made it awkward if the user wanted
1194 * to resize the window themself.
1197 gtk_font_selection_update_preview (GtkFontSelection *fontsel)
1199 GtkFontSelectionPrivate *priv = fontsel->priv;
1201 GtkRequisition old_requisition, new_requisition;
1202 GtkWidget *preview_entry = priv->preview_entry;
1205 gtk_widget_get_preferred_size (preview_entry, &old_requisition, NULL);
1207 gtk_widget_override_font (preview_entry,
1208 gtk_font_selection_get_font_description (fontsel));
1210 gtk_widget_get_preferred_size (preview_entry, &new_requisition, NULL);
1212 /* We don't ever want to be over MAX_PREVIEW_HEIGHT pixels high. */
1213 new_height = CLAMP (new_requisition.height, INITIAL_PREVIEW_HEIGHT, MAX_PREVIEW_HEIGHT);
1215 if (new_height > old_requisition.height || new_height < old_requisition.height - 30)
1216 gtk_widget_set_size_request (preview_entry, -1, new_height);
1218 /* This sets the preview text, if it hasn't been set already. */
1219 text = gtk_entry_get_text (GTK_ENTRY (preview_entry));
1220 if (strlen (text) == 0)
1221 gtk_entry_set_text (GTK_ENTRY (preview_entry), _(PREVIEW_TEXT));
1222 gtk_editable_set_position (GTK_EDITABLE (preview_entry), 0);
1226 /*****************************************************************************
1227 * These functions are the main public interface for getting/setting the font.
1228 *****************************************************************************/
1231 * gtk_font_selection_get_family_list:
1232 * @fontsel: a #GtkFontSelection
1234 * This returns the #GtkTreeView that lists font families, for
1235 * example, 'Sans', 'Serif', etc.
1237 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
1241 * Deprecated: 3.2: Use #GtkFontChooser
1244 gtk_font_selection_get_family_list (GtkFontSelection *fontsel)
1246 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1248 return fontsel->priv->family_list;
1252 * gtk_font_selection_get_face_list:
1253 * @fontsel: a #GtkFontSelection
1255 * This returns the #GtkTreeView which lists all styles available for
1256 * the selected font. For example, 'Regular', 'Bold', etc.
1258 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
1262 * Deprecated: 3.2: Use #GtkFontChooser
1265 gtk_font_selection_get_face_list (GtkFontSelection *fontsel)
1267 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1269 return fontsel->priv->face_list;
1273 * gtk_font_selection_get_size_entry:
1274 * @fontsel: a #GtkFontSelection
1276 * This returns the #GtkEntry used to allow the user to edit the font
1277 * number manually instead of selecting it from the list of font sizes.
1279 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
1283 * Deprecated: 3.2: Use #GtkFontChooser
1286 gtk_font_selection_get_size_entry (GtkFontSelection *fontsel)
1288 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1290 return fontsel->priv->size_entry;
1294 * gtk_font_selection_get_size_list:
1295 * @fontsel: a #GtkFontSelection
1297 * This returns the #GtkTreeeView used to list font sizes.
1299 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
1303 * Deprecated: 3.2: Use #GtkFontChooser
1306 gtk_font_selection_get_size_list (GtkFontSelection *fontsel)
1308 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1310 return fontsel->priv->size_list;
1314 * gtk_font_selection_get_preview_entry:
1315 * @fontsel: a #GtkFontSelection
1317 * This returns the #GtkEntry used to display the font as a preview.
1319 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
1323 * Deprecated: 3.2: Use #GtkFontChooser
1326 gtk_font_selection_get_preview_entry (GtkFontSelection *fontsel)
1328 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1330 return fontsel->priv->preview_entry;
1334 * gtk_font_selection_get_family:
1335 * @fontsel: a #GtkFontSelection
1337 * Gets the #PangoFontFamily representing the selected font family.
1339 * Return value: (transfer none): A #PangoFontFamily representing the
1340 * selected font family. Font families are a collection of font
1341 * faces. The returned object is owned by @fontsel and must not
1342 * be modified or freed.
1346 * Deprecated: 3.2: Use #GtkFontChooser
1349 gtk_font_selection_get_family (GtkFontSelection *fontsel)
1351 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1353 return fontsel->priv->family;
1357 * gtk_font_selection_get_face:
1358 * @fontsel: a #GtkFontSelection
1360 * Gets the #PangoFontFace representing the selected font group
1361 * details (i.e. family, slant, weight, width, etc).
1363 * Return value: (transfer none): A #PangoFontFace representing the
1364 * selected font group details. The returned object is owned by
1365 * @fontsel and must not be modified or freed.
1369 * Deprecated: 3.2: Use #GtkFontChooser
1372 gtk_font_selection_get_face (GtkFontSelection *fontsel)
1374 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1376 return fontsel->priv->face;
1380 * gtk_font_selection_get_size:
1381 * @fontsel: a #GtkFontSelection
1383 * The selected font size.
1385 * Return value: A n integer representing the selected font size,
1386 * or -1 if no font size is selected.
1390 * Deprecated: 3.2: Use #GtkFontChooser
1393 gtk_font_selection_get_size (GtkFontSelection *fontsel)
1395 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), -1);
1397 return fontsel->priv->size;
1401 * gtk_font_selection_get_font_name:
1402 * @fontsel: a #GtkFontSelection
1404 * Gets the currently-selected font name.
1406 * Note that this can be a different string than what you set with
1407 * gtk_font_selection_set_font_name(), as the font selection widget may
1408 * normalize font names and thus return a string with a different structure.
1409 * For example, "Helvetica Italic Bold 12" could be normalized to
1410 * "Helvetica Bold Italic 12". Use pango_font_description_equal()
1411 * if you want to compare two font descriptions.
1413 * Return value: A string with the name of the current font, or %NULL if
1414 * no font is selected. You must free this string with g_free().
1416 * Deprecated: 3.2: Use #GtkFontChooser
1419 gtk_font_selection_get_font_name (GtkFontSelection *fontsel)
1423 PangoFontDescription *font_desc = gtk_font_selection_get_font_description (fontsel);
1424 result = pango_font_description_to_string (font_desc);
1425 pango_font_description_free (font_desc);
1430 /* This selects the appropriate list rows.
1431 First we check the fontname is valid and try to find the font family
1432 - i.e. the name in the main list. If we can't find that, then just return.
1433 Next we try to set each of the properties according to the fontname.
1434 Finally we select the font family & style in the lists. */
1436 gtk_font_selection_select_font_desc (GtkFontSelection *fontsel,
1437 PangoFontDescription *new_desc,
1438 PangoFontFamily **pfamily,
1439 PangoFontFace **pface)
1441 GtkFontSelectionPrivate *priv = fontsel->priv;
1442 PangoFontFamily *new_family = NULL;
1443 PangoFontFace *new_face = NULL;
1444 PangoFontFace *fallback_face = NULL;
1445 GtkTreeModel *model;
1447 GtkTreeIter match_iter;
1449 const gchar *new_family_name;
1451 new_family_name = pango_font_description_get_family (new_desc);
1453 if (!new_family_name)
1456 /* Check to make sure that this is in the list of allowed fonts
1458 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->family_list));
1459 for (valid = gtk_tree_model_get_iter_first (model, &iter);
1461 valid = gtk_tree_model_iter_next (model, &iter))
1463 PangoFontFamily *family;
1465 gtk_tree_model_get (model, &iter, FAMILY_COLUMN, &family, -1);
1467 if (g_ascii_strcasecmp (pango_font_family_get_name (family),
1468 new_family_name) == 0)
1469 new_family = g_object_ref (family);
1471 g_object_unref (family);
1481 *pfamily = new_family;
1483 g_object_unref (new_family);
1484 set_cursor_to_iter (GTK_TREE_VIEW (priv->family_list), &iter);
1485 gtk_font_selection_show_available_styles (fontsel);
1487 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->face_list));
1488 for (valid = gtk_tree_model_get_iter_first (model, &iter);
1490 valid = gtk_tree_model_iter_next (model, &iter))
1492 PangoFontFace *face;
1493 PangoFontDescription *tmp_desc;
1495 gtk_tree_model_get (model, &iter, FACE_COLUMN, &face, -1);
1496 tmp_desc = pango_font_face_describe (face);
1498 if (font_description_style_equal (tmp_desc, new_desc))
1499 new_face = g_object_ref (face);
1503 fallback_face = g_object_ref (face);
1507 pango_font_description_free (tmp_desc);
1508 g_object_unref (face);
1518 new_face = fallback_face;
1519 else if (fallback_face)
1520 g_object_unref (fallback_face);
1525 g_object_unref (new_face);
1526 set_cursor_to_iter (GTK_TREE_VIEW (priv->face_list), &match_iter);
1528 gtk_font_selection_set_size (fontsel, pango_font_description_get_size (new_desc));
1534 /* This sets the current font, then selecting the appropriate list rows. */
1537 * gtk_font_selection_set_font_name:
1538 * @fontsel: a #GtkFontSelection
1539 * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
1541 * Sets the currently-selected font.
1543 * Note that the @fontsel needs to know the screen in which it will appear
1544 * for this to work; this can be guaranteed by simply making sure that the
1545 * @fontsel is inserted in a toplevel window before you call this function.
1547 * Return value: %TRUE if the font could be set successfully; %FALSE if no
1548 * such font exists or if the @fontsel doesn't belong to a particular
1551 * Deprecated: 3.2: Use #GtkFontChooser
1554 gtk_font_selection_set_font_name (GtkFontSelection *fontsel,
1555 const gchar *fontname)
1557 PangoFontFamily *family = NULL;
1558 PangoFontFace *face = NULL;
1559 PangoFontDescription *new_desc;
1561 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), FALSE);
1563 if (!gtk_widget_has_screen (GTK_WIDGET (fontsel)))
1566 new_desc = pango_font_description_from_string (fontname);
1568 if (gtk_font_selection_select_font_desc (fontsel, new_desc, &family, &face))
1570 gtk_font_selection_ref_family (fontsel, family);
1572 g_object_unref (family);
1574 gtk_font_selection_ref_face (fontsel, face);
1576 g_object_unref (face);
1579 pango_font_description_free (new_desc);
1581 g_object_notify (G_OBJECT (fontsel), "font-name");
1587 * gtk_font_selection_get_preview_text:
1588 * @fontsel: a #GtkFontSelection
1590 * Gets the text displayed in the preview area.
1592 * Return value: the text displayed in the preview area.
1593 * This string is owned by the widget and should not be
1596 * Deprecated: 3.2: Use #GtkFontChooser
1599 gtk_font_selection_get_preview_text (GtkFontSelection *fontsel)
1601 GtkFontSelectionPrivate *priv;
1603 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1605 priv = fontsel->priv;
1607 return gtk_entry_get_text (GTK_ENTRY (priv->preview_entry));
1612 * gtk_font_selection_set_preview_text:
1613 * @fontsel: a #GtkFontSelection
1614 * @text: the text to display in the preview area
1616 * Sets the text displayed in the preview area.
1617 * The @text is used to show how the selected font looks.
1619 * Deprecated: 3.2: Use #GtkFontChooser
1622 gtk_font_selection_set_preview_text (GtkFontSelection *fontsel,
1625 GtkFontSelectionPrivate *priv;
1627 g_return_if_fail (GTK_IS_FONT_SELECTION (fontsel));
1628 g_return_if_fail (text != NULL);
1630 priv = fontsel->priv;
1632 gtk_entry_set_text (GTK_ENTRY (priv->preview_entry), text);
1637 * SECTION:gtkfontseldlg
1638 * @Short_description: A dialog box for selecting fonts
1639 * @Title: GtkFontSelectionDialog
1640 * @See_also: #GtkFontSelection, #GtkDialog, #GtkFontChooserDialog
1642 * The #GtkFontSelectionDialog widget is a dialog box for selecting a font.
1644 * To set the font which is initially selected, use
1645 * gtk_font_selection_dialog_set_font_name().
1647 * To get the selected font use gtk_font_selection_dialog_get_font_name().
1649 * To change the text which is shown in the preview area, use
1650 * gtk_font_selection_dialog_set_preview_text().
1652 * In GTK+ 3.2, #GtkFontSelectionDialog has been deprecated in favor of
1653 * #GtkFontChooserDialog.
1655 * <refsect2 id="GtkFontSelectionDialog-BUILDER-UI">
1656 * <title>GtkFontSelectionDialog as GtkBuildable</title>
1657 * The GtkFontSelectionDialog implementation of the GtkBuildable interface
1658 * exposes the embedded #GtkFontSelection as internal child with the
1659 * name "font_selection". It also exposes the buttons with the names
1660 * "ok_button", "cancel_button" and "apply_button".
1664 static void gtk_font_selection_dialog_buildable_interface_init (GtkBuildableIface *iface);
1665 static GObject * gtk_font_selection_dialog_buildable_get_internal_child (GtkBuildable *buildable,
1666 GtkBuilder *builder,
1667 const gchar *childname);
1669 G_DEFINE_TYPE_WITH_CODE (GtkFontSelectionDialog, gtk_font_selection_dialog,
1671 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
1672 gtk_font_selection_dialog_buildable_interface_init))
1674 static GtkBuildableIface *parent_buildable_iface;
1677 gtk_font_selection_dialog_class_init (GtkFontSelectionDialogClass *klass)
1679 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1681 gtk_widget_class_set_accessible_role (widget_class, ATK_ROLE_FONT_CHOOSER);
1683 g_type_class_add_private (klass, sizeof (GtkFontSelectionDialogPrivate));
1687 gtk_font_selection_dialog_init (GtkFontSelectionDialog *fontseldiag)
1689 GtkFontSelectionDialogPrivate *priv;
1690 GtkDialog *dialog = GTK_DIALOG (fontseldiag);
1691 GtkWidget *action_area, *content_area;
1693 fontseldiag->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontseldiag,
1694 GTK_TYPE_FONT_SELECTION_DIALOG,
1695 GtkFontSelectionDialogPrivate);
1696 priv = fontseldiag->priv;
1698 content_area = gtk_dialog_get_content_area (dialog);
1699 action_area = gtk_dialog_get_action_area (dialog);
1701 gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
1702 gtk_box_set_spacing (GTK_BOX (content_area), 2); /* 2 * 5 + 2 = 12 */
1703 gtk_container_set_border_width (GTK_CONTAINER (action_area), 5);
1704 gtk_box_set_spacing (GTK_BOX (action_area), 6);
1706 gtk_widget_push_composite_child ();
1708 gtk_window_set_resizable (GTK_WINDOW (fontseldiag), TRUE);
1710 /* Create the content area */
1711 priv->fontsel = gtk_font_selection_new ();
1712 gtk_container_set_border_width (GTK_CONTAINER (priv->fontsel), 5);
1713 gtk_widget_show (priv->fontsel);
1714 gtk_box_pack_start (GTK_BOX (content_area),
1715 priv->fontsel, TRUE, TRUE, 0);
1717 /* Create the action area */
1718 priv->cancel_button = gtk_dialog_add_button (dialog,
1720 GTK_RESPONSE_CANCEL);
1722 priv->apply_button = gtk_dialog_add_button (dialog,
1724 GTK_RESPONSE_APPLY);
1725 gtk_widget_hide (priv->apply_button);
1727 priv->ok_button = gtk_dialog_add_button (dialog,
1730 gtk_widget_grab_default (priv->ok_button);
1732 gtk_dialog_set_alternative_button_order (GTK_DIALOG (fontseldiag),
1735 GTK_RESPONSE_CANCEL,
1738 gtk_window_set_title (GTK_WINDOW (fontseldiag),
1739 _("Font Selection"));
1741 gtk_widget_pop_composite_child ();
1745 * gtk_font_selection_dialog_new:
1746 * @title: the title of the dialog window
1748 * Creates a new #GtkFontSelectionDialog.
1750 * Return value: a new #GtkFontSelectionDialog
1752 * Deprecated: 3.2: Use #GtkFontChooserDialog
1755 gtk_font_selection_dialog_new (const gchar *title)
1757 GtkFontSelectionDialog *fontseldiag;
1759 fontseldiag = g_object_new (GTK_TYPE_FONT_SELECTION_DIALOG, NULL);
1762 gtk_window_set_title (GTK_WINDOW (fontseldiag), title);
1764 return GTK_WIDGET (fontseldiag);
1768 * gtk_font_selection_dialog_get_font_selection:
1769 * @fsd: a #GtkFontSelectionDialog
1771 * Retrieves the #GtkFontSelection widget embedded in the dialog.
1773 * Returns: (transfer none): the embedded #GtkFontSelection
1777 * Deprecated: 3.2: Use #GtkFontChooserDialog
1780 gtk_font_selection_dialog_get_font_selection (GtkFontSelectionDialog *fsd)
1782 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1784 return fsd->priv->fontsel;
1789 * gtk_font_selection_dialog_get_ok_button:
1790 * @fsd: a #GtkFontSelectionDialog
1792 * Gets the 'OK' button.
1794 * Return value: (transfer none): the #GtkWidget used in the dialog
1795 * for the 'OK' button.
1799 * Deprecated: 3.2: Use #GtkFontChooserDialog
1802 gtk_font_selection_dialog_get_ok_button (GtkFontSelectionDialog *fsd)
1804 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1806 return fsd->priv->ok_button;
1810 * gtk_font_selection_dialog_get_cancel_button:
1811 * @fsd: a #GtkFontSelectionDialog
1813 * Gets the 'Cancel' button.
1815 * Return value: (transfer none): the #GtkWidget used in the dialog
1816 * for the 'Cancel' button.
1820 * Deprecated: 3.2: Use #GtkFontChooserDialog
1823 gtk_font_selection_dialog_get_cancel_button (GtkFontSelectionDialog *fsd)
1825 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1827 return fsd->priv->cancel_button;
1831 gtk_font_selection_dialog_buildable_interface_init (GtkBuildableIface *iface)
1833 parent_buildable_iface = g_type_interface_peek_parent (iface);
1834 iface->get_internal_child = gtk_font_selection_dialog_buildable_get_internal_child;
1838 gtk_font_selection_dialog_buildable_get_internal_child (GtkBuildable *buildable,
1839 GtkBuilder *builder,
1840 const gchar *childname)
1842 GtkFontSelectionDialogPrivate *priv;
1844 priv = GTK_FONT_SELECTION_DIALOG (buildable)->priv;
1846 if (g_strcmp0 (childname, "ok_button") == 0)
1847 return G_OBJECT (priv->ok_button);
1848 else if (g_strcmp0 (childname, "cancel_button") == 0)
1849 return G_OBJECT (priv->cancel_button);
1850 else if (g_strcmp0 (childname, "apply_button") == 0)
1851 return G_OBJECT (priv->apply_button);
1852 else if (g_strcmp0 (childname, "font_selection") == 0)
1853 return G_OBJECT (priv->fontsel);
1855 return parent_buildable_iface->get_internal_child (buildable, builder, childname);
1859 * gtk_font_selection_dialog_get_font_name:
1860 * @fsd: a #GtkFontSelectionDialog
1862 * Gets the currently-selected font name.
1864 * Note that this can be a different string than what you set with
1865 * gtk_font_selection_dialog_set_font_name(), as the font selection widget
1866 * may normalize font names and thus return a string with a different
1867 * structure. For example, "Helvetica Italic Bold 12" could be normalized
1868 * to "Helvetica Bold Italic 12". Use pango_font_description_equal()
1869 * if you want to compare two font descriptions.
1871 * Return value: A string with the name of the current font, or %NULL if no
1872 * font is selected. You must free this string with g_free().
1874 * Deprecated: 3.2: Use #GtkFontChooserDialog
1877 gtk_font_selection_dialog_get_font_name (GtkFontSelectionDialog *fsd)
1879 GtkFontSelectionDialogPrivate *priv;
1881 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1885 return gtk_font_selection_get_font_name (GTK_FONT_SELECTION (priv->fontsel));
1889 * gtk_font_selection_dialog_set_font_name:
1890 * @fsd: a #GtkFontSelectionDialog
1891 * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
1893 * Sets the currently selected font.
1895 * Return value: %TRUE if the font selected in @fsd is now the
1896 * @fontname specified, %FALSE otherwise.
1898 * Deprecated: 3.2: Use #GtkFontChooserDialog
1901 gtk_font_selection_dialog_set_font_name (GtkFontSelectionDialog *fsd,
1902 const gchar *fontname)
1904 GtkFontSelectionDialogPrivate *priv;
1906 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), FALSE);
1907 g_return_val_if_fail (fontname, FALSE);
1911 return gtk_font_selection_set_font_name (GTK_FONT_SELECTION (priv->fontsel), fontname);
1915 * gtk_font_selection_dialog_get_preview_text:
1916 * @fsd: a #GtkFontSelectionDialog
1918 * Gets the text displayed in the preview area.
1920 * Return value: the text displayed in the preview area.
1921 * This string is owned by the widget and should not be
1924 * Deprecated: 3.2: Use #GtkFontChooserDialog
1927 gtk_font_selection_dialog_get_preview_text (GtkFontSelectionDialog *fsd)
1929 GtkFontSelectionDialogPrivate *priv;
1931 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1935 return gtk_font_selection_get_preview_text (GTK_FONT_SELECTION (priv->fontsel));
1939 * gtk_font_selection_dialog_set_preview_text:
1940 * @fsd: a #GtkFontSelectionDialog
1941 * @text: the text to display in the preview area
1943 * Sets the text displayed in the preview area.
1945 * Deprecated: 3.2: Use #GtkFontChooserDialog
1948 gtk_font_selection_dialog_set_preview_text (GtkFontSelectionDialog *fsd,
1951 GtkFontSelectionDialogPrivate *priv;
1953 g_return_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd));
1954 g_return_if_fail (text != NULL);
1958 gtk_font_selection_set_preview_text (GTK_FONT_SELECTION (priv->fontsel), text);