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, see <http://www.gnu.org/licenses/>.
24 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
25 * file for a list of people on the GTK+ Team. See the ChangeLog
26 * files for a list of changes. These files are distributed with
27 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
32 #define GDK_DISABLE_DEPRECATION_WARNINGS
35 #include <glib/gprintf.h>
40 #include "gtkbutton.h"
41 #include "gtkcellrenderertext.h"
45 #include "gtkliststore.h"
48 #include "gtktreeselection.h"
49 #include "gtktreeview.h"
50 #include "gtkscrolledwindow.h"
52 #include "gtkaccessible.h"
53 #include "gtkbuildable.h"
54 #include "gtkorientable.h"
55 #include "gtkprivate.h"
57 #define GDK_DISABLE_DEPRECATION_WARNINGS
59 #include "gtkfontsel.h"
63 * @Short_description: Deprecated widget for selecting fonts
64 * @Title: GtkFontSelection
65 * @See_also: #GtkFontSelectionDialog, #GtkFontChooser
67 * The #GtkFontSelection widget lists the available fonts, styles and sizes,
68 * allowing the user to select a font.
69 * It is used in the #GtkFontSelectionDialog widget to provide a dialog box for
72 * To set the font which is initially selected, use
73 * gtk_font_selection_set_font_name().
75 * To get the selected font use gtk_font_selection_get_font_name().
77 * To change the text which is shown in the preview area, use
78 * gtk_font_selection_set_preview_text().
80 * In GTK+ 3.2, #GtkFontSelection has been deprecated in favor of
85 struct _GtkFontSelectionPrivate
87 GtkWidget *font_entry; /* Used _get_family_entry() for consistency, -mr */
88 GtkWidget *font_style_entry; /* Used _get_face_entry() for consistency, -mr */
90 GtkWidget *size_entry;
91 GtkWidget *preview_entry;
93 GtkWidget *family_list;
97 PangoFontFamily *family; /* Current family */
98 PangoFontFace *face; /* Current face */
104 struct _GtkFontSelectionDialogPrivate
108 GtkWidget *ok_button;
109 GtkWidget *apply_button;
110 GtkWidget *cancel_button;
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
120 #undef INCLUDE_FONT_ENTRIES
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")
126 #define DEFAULT_FONT_NAME "Sans 10"
128 /* This is the initial and maximum height of the preview entry (it expands
129 when large font sizes are selected). Initial height is also the minimum. */
130 #define INITIAL_PREVIEW_HEIGHT 44
131 #define MAX_PREVIEW_HEIGHT 300
133 /* These are the sizes of the font, style & size lists. */
134 #define FONT_LIST_HEIGHT 136
135 #define FONT_LIST_WIDTH 190
136 #define FONT_STYLE_LIST_WIDTH 170
137 #define FONT_SIZE_LIST_WIDTH 60
139 /* These are what we use as the standard font sizes, for the size list.
141 static const guint16 font_sizes[] = {
142 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, 22, 24, 26, 28,
143 32, 36, 40, 48, 56, 64, 72
167 static void gtk_font_selection_set_property (GObject *object,
171 static void gtk_font_selection_get_property (GObject *object,
175 static void gtk_font_selection_finalize (GObject *object);
176 static void gtk_font_selection_screen_changed (GtkWidget *widget,
177 GdkScreen *previous_screen);
178 static void gtk_font_selection_style_updated (GtkWidget *widget);
180 /* These are the callbacks & related functions. */
181 static void gtk_font_selection_select_font (GtkTreeSelection *selection,
183 static void gtk_font_selection_show_available_fonts (GtkFontSelection *fs);
185 static void gtk_font_selection_show_available_styles (GtkFontSelection *fs);
186 static void gtk_font_selection_select_best_style (GtkFontSelection *fs,
188 static void gtk_font_selection_select_style (GtkTreeSelection *selection,
191 static void gtk_font_selection_select_best_size (GtkFontSelection *fs);
192 static void gtk_font_selection_show_available_sizes (GtkFontSelection *fs,
193 gboolean first_time);
194 static void gtk_font_selection_size_activate (GtkWidget *w,
196 static gboolean gtk_font_selection_size_focus_out (GtkWidget *w,
197 GdkEventFocus *event,
199 static void gtk_font_selection_select_size (GtkTreeSelection *selection,
202 static void gtk_font_selection_scroll_on_map (GtkWidget *w,
205 static void gtk_font_selection_preview_changed (GtkWidget *entry,
206 GtkFontSelection *fontsel);
207 static void gtk_font_selection_scroll_to_selection (GtkFontSelection *fontsel);
210 /* Misc. utility functions. */
211 static void gtk_font_selection_load_font (GtkFontSelection *fs);
212 static void gtk_font_selection_update_preview (GtkFontSelection *fs);
214 static PangoFontDescription *gtk_font_selection_get_font_description (GtkFontSelection *fontsel);
215 static gboolean gtk_font_selection_select_font_desc (GtkFontSelection *fontsel,
216 PangoFontDescription *new_desc,
217 PangoFontFamily **pfamily,
218 PangoFontFace **pface);
219 static void gtk_font_selection_reload_fonts (GtkFontSelection *fontsel);
220 static void gtk_font_selection_ref_family (GtkFontSelection *fontsel,
221 PangoFontFamily *family);
222 static void gtk_font_selection_ref_face (GtkFontSelection *fontsel,
223 PangoFontFace *face);
225 G_DEFINE_TYPE (GtkFontSelection, gtk_font_selection, GTK_TYPE_BOX)
228 gtk_font_selection_class_init (GtkFontSelectionClass *klass)
230 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
231 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
233 gobject_class->finalize = gtk_font_selection_finalize;
234 gobject_class->set_property = gtk_font_selection_set_property;
235 gobject_class->get_property = gtk_font_selection_get_property;
237 widget_class->screen_changed = gtk_font_selection_screen_changed;
238 widget_class->style_updated = gtk_font_selection_style_updated;
240 g_object_class_install_property (gobject_class,
242 g_param_spec_string ("font-name",
244 P_("The string that represents this font"),
246 GTK_PARAM_READWRITE));
247 g_object_class_install_property (gobject_class,
249 g_param_spec_string ("preview-text",
251 P_("The text to display in order to demonstrate the selected font"),
253 GTK_PARAM_READWRITE));
255 g_type_class_add_private (klass, sizeof (GtkFontSelectionPrivate));
259 gtk_font_selection_set_property (GObject *object,
264 GtkFontSelection *fontsel;
266 fontsel = GTK_FONT_SELECTION (object);
271 gtk_font_selection_set_font_name (fontsel, g_value_get_string (value));
273 case PROP_PREVIEW_TEXT:
274 gtk_font_selection_set_preview_text (fontsel, g_value_get_string (value));
277 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
282 static void gtk_font_selection_get_property (GObject *object,
287 GtkFontSelection *fontsel;
289 fontsel = GTK_FONT_SELECTION (object);
294 g_value_take_string (value, gtk_font_selection_get_font_name (fontsel));
296 case PROP_PREVIEW_TEXT:
297 g_value_set_string (value, gtk_font_selection_get_preview_text (fontsel));
300 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
305 /* Handles key press events on the lists, so that we can trap Enter to
306 * activate the default button on our own.
309 list_row_activated (GtkWidget *widget)
311 GtkWidget *default_widget, *focus_widget;
314 window = GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (widget)));
315 if (!gtk_widget_is_toplevel (GTK_WIDGET (window)))
320 default_widget = gtk_window_get_default_widget (window);
321 focus_widget = gtk_window_get_focus (window);
323 if (widget != default_widget &&
324 !(widget == focus_widget && (!default_widget || !gtk_widget_get_sensitive (default_widget))))
325 gtk_window_activate_default (window);
332 gtk_font_selection_init (GtkFontSelection *fontsel)
334 GtkFontSelectionPrivate *priv;
335 GtkWidget *scrolled_win;
337 GtkWidget *table, *label;
338 GtkWidget *font_label, *style_label;
341 GtkTreeViewColumn *column;
342 GList *focus_chain = NULL;
345 fontsel->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontsel,
346 GTK_TYPE_FONT_SELECTION,
347 GtkFontSelectionPrivate);
348 priv = fontsel->priv;
350 gtk_orientable_set_orientation (GTK_ORIENTABLE (fontsel),
351 GTK_ORIENTATION_VERTICAL);
353 gtk_widget_push_composite_child ();
355 gtk_box_set_spacing (GTK_BOX (fontsel), 12);
356 priv->size = 12 * PANGO_SCALE;
358 /* Create the table of font, style & size. */
359 table = gtk_table_new (3, 3, FALSE);
360 gtk_widget_show (table);
361 gtk_table_set_row_spacings (GTK_TABLE (table), 6);
362 gtk_table_set_col_spacings (GTK_TABLE (table), 12);
363 gtk_box_pack_start (GTK_BOX (fontsel), table, TRUE, TRUE, 0);
365 #ifdef INCLUDE_FONT_ENTRIES
366 priv->font_entry = gtk_entry_new ();
367 gtk_editable_set_editable (GTK_EDITABLE (priv->font_entry), FALSE);
368 gtk_widget_set_size_request (priv->font_entry, 20, -1);
369 gtk_widget_show (priv->font_entry);
370 gtk_table_attach (GTK_TABLE (table), priv->font_entry, 0, 1, 1, 2,
373 priv->font_style_entry = gtk_entry_new ();
374 gtk_editable_set_editable (GTK_EDITABLE (priv->font_style_entry), FALSE);
375 gtk_widget_set_size_request (priv->font_style_entry, 20, -1);
376 gtk_widget_show (priv->font_style_entry);
377 gtk_table_attach (GTK_TABLE (table), priv->font_style_entry, 1, 2, 1, 2,
379 #endif /* INCLUDE_FONT_ENTRIES */
381 priv->size_entry = gtk_entry_new ();
382 gtk_widget_set_size_request (priv->size_entry, 20, -1);
383 gtk_widget_show (priv->size_entry);
384 gtk_table_attach (GTK_TABLE (table), priv->size_entry, 2, 3, 1, 2,
386 g_signal_connect (priv->size_entry, "activate",
387 G_CALLBACK (gtk_font_selection_size_activate),
389 g_signal_connect_after (priv->size_entry, "focus-out-event",
390 G_CALLBACK (gtk_font_selection_size_focus_out),
393 font_label = gtk_label_new_with_mnemonic (_("_Family:"));
394 gtk_widget_set_halign (font_label, GTK_ALIGN_START);
395 gtk_widget_set_valign (font_label, GTK_ALIGN_CENTER);
396 gtk_widget_show (font_label);
397 gtk_table_attach (GTK_TABLE (table), font_label, 0, 1, 0, 1,
400 style_label = gtk_label_new_with_mnemonic (_("_Style:"));
401 gtk_widget_set_halign (style_label, GTK_ALIGN_START);
402 gtk_widget_set_valign (style_label, GTK_ALIGN_CENTER);
403 gtk_widget_show (style_label);
404 gtk_table_attach (GTK_TABLE (table), style_label, 1, 2, 0, 1,
407 label = gtk_label_new_with_mnemonic (_("Si_ze:"));
408 gtk_label_set_mnemonic_widget (GTK_LABEL (label),
410 gtk_widget_set_halign (label, GTK_ALIGN_START);
411 gtk_widget_set_valign (label, GTK_ALIGN_CENTER);
412 gtk_widget_show (label);
413 gtk_table_attach (GTK_TABLE (table), label, 2, 3, 0, 1,
417 /* Create the lists */
419 model = gtk_list_store_new (2,
420 G_TYPE_OBJECT, /* FAMILY_COLUMN */
421 G_TYPE_STRING); /* FAMILY_NAME_COLUMN */
422 priv->family_list = gtk_tree_view_new_with_model (GTK_TREE_MODEL (model));
423 g_object_unref (model);
425 g_signal_connect (priv->family_list, "row-activated",
426 G_CALLBACK (list_row_activated), fontsel);
428 column = gtk_tree_view_column_new_with_attributes ("Family",
429 gtk_cell_renderer_text_new (),
430 "text", FAMILY_NAME_COLUMN,
432 gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
433 gtk_tree_view_append_column (GTK_TREE_VIEW (priv->family_list), column);
435 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->family_list), FALSE);
436 gtk_tree_selection_set_mode (gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->family_list)),
437 GTK_SELECTION_BROWSE);
439 gtk_label_set_mnemonic_widget (GTK_LABEL (font_label), priv->family_list);
441 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
442 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win), GTK_SHADOW_IN);
443 gtk_widget_set_size_request (scrolled_win,
444 FONT_LIST_WIDTH, FONT_LIST_HEIGHT);
445 gtk_container_add (GTK_CONTAINER (scrolled_win), priv->family_list);
446 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
447 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
448 gtk_widget_show (priv->family_list);
449 gtk_widget_show (scrolled_win);
451 gtk_table_attach (GTK_TABLE (table), scrolled_win, 0, 1, 1, 3,
452 GTK_EXPAND | GTK_FILL,
453 GTK_EXPAND | GTK_FILL, 0, 0);
454 focus_chain = g_list_append (focus_chain, scrolled_win);
456 model = gtk_list_store_new (2,
457 G_TYPE_OBJECT, /* FACE_COLUMN */
458 G_TYPE_STRING); /* FACE_NAME_COLUMN */
459 priv->face_list = gtk_tree_view_new_with_model (GTK_TREE_MODEL (model));
460 g_object_unref (model);
461 g_signal_connect (priv->face_list, "row-activated",
462 G_CALLBACK (list_row_activated), fontsel);
464 gtk_label_set_mnemonic_widget (GTK_LABEL (style_label), priv->face_list);
466 column = gtk_tree_view_column_new_with_attributes ("Face",
467 gtk_cell_renderer_text_new (),
468 "text", FACE_NAME_COLUMN,
470 gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
471 gtk_tree_view_append_column (GTK_TREE_VIEW (priv->face_list), column);
473 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->face_list), FALSE);
474 gtk_tree_selection_set_mode (gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->face_list)),
475 GTK_SELECTION_BROWSE);
477 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
478 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win), GTK_SHADOW_IN);
479 gtk_widget_set_size_request (scrolled_win,
480 FONT_STYLE_LIST_WIDTH, FONT_LIST_HEIGHT);
481 gtk_container_add (GTK_CONTAINER (scrolled_win), priv->face_list);
482 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
483 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
484 gtk_widget_show (priv->face_list);
485 gtk_widget_show (scrolled_win);
486 gtk_table_attach (GTK_TABLE (table), scrolled_win, 1, 2, 1, 3,
487 GTK_EXPAND | GTK_FILL,
488 GTK_EXPAND | GTK_FILL, 0, 0);
489 focus_chain = g_list_append (focus_chain, scrolled_win);
491 focus_chain = g_list_append (focus_chain, priv->size_entry);
493 model = gtk_list_store_new (1, G_TYPE_INT);
494 priv->size_list = gtk_tree_view_new_with_model (GTK_TREE_MODEL (model));
495 g_object_unref (model);
496 g_signal_connect (priv->size_list, "row-activated",
497 G_CALLBACK (list_row_activated), fontsel);
499 column = gtk_tree_view_column_new_with_attributes ("Size",
500 gtk_cell_renderer_text_new (),
503 gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
504 gtk_tree_view_append_column (GTK_TREE_VIEW (priv->size_list), column);
506 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->size_list), FALSE);
507 gtk_tree_selection_set_mode (gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->size_list)),
508 GTK_SELECTION_BROWSE);
510 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
511 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win), GTK_SHADOW_IN);
512 gtk_container_add (GTK_CONTAINER (scrolled_win), priv->size_list);
513 gtk_widget_set_size_request (scrolled_win, -1, FONT_LIST_HEIGHT);
514 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
515 GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
516 gtk_widget_show (priv->size_list);
517 gtk_widget_show (scrolled_win);
518 gtk_table_attach (GTK_TABLE (table), scrolled_win, 2, 3, 2, 3,
519 GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
520 focus_chain = g_list_append (focus_chain, scrolled_win);
522 gtk_container_set_focus_chain (GTK_CONTAINER (table), focus_chain);
523 g_list_free (focus_chain);
525 /* Insert the fonts. */
526 g_signal_connect (gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->family_list)), "changed",
527 G_CALLBACK (gtk_font_selection_select_font), fontsel);
529 g_signal_connect_after (priv->family_list, "map",
530 G_CALLBACK (gtk_font_selection_scroll_on_map),
533 g_signal_connect (gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->face_list)), "changed",
534 G_CALLBACK (gtk_font_selection_select_style), fontsel);
536 g_signal_connect (gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->size_list)), "changed",
537 G_CALLBACK (gtk_font_selection_select_size), fontsel);
538 atk_obj = gtk_widget_get_accessible (priv->size_list);
539 if (GTK_IS_ACCESSIBLE (atk_obj))
541 /* Accessibility support is enabled.
542 * Make the label ATK_RELATON_LABEL_FOR for the size list as well.
544 AtkObject *atk_label;
545 AtkRelationSet *relation_set;
546 AtkRelation *relation;
547 AtkObject *obj_array[1];
549 atk_label = gtk_widget_get_accessible (label);
550 relation_set = atk_object_ref_relation_set (atk_obj);
551 relation = atk_relation_set_get_relation_by_type (relation_set, ATK_RELATION_LABELLED_BY);
554 atk_relation_add_target (relation, atk_label);
558 obj_array[0] = atk_label;
559 relation = atk_relation_new (obj_array, 1, ATK_RELATION_LABELLED_BY);
560 atk_relation_set_add (relation_set, relation);
562 g_object_unref (relation_set);
564 relation_set = atk_object_ref_relation_set (atk_label);
565 relation = atk_relation_set_get_relation_by_type (relation_set, ATK_RELATION_LABEL_FOR);
568 atk_relation_add_target (relation, atk_obj);
572 obj_array[0] = atk_obj;
573 relation = atk_relation_new (obj_array, 1, ATK_RELATION_LABEL_FOR);
574 atk_relation_set_add (relation_set, relation);
576 g_object_unref (relation_set);
579 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
580 gtk_widget_show (vbox);
581 gtk_box_pack_start (GTK_BOX (fontsel), vbox, FALSE, TRUE, 0);
583 /* create the text entry widget */
584 label = gtk_label_new_with_mnemonic (_("_Preview:"));
585 gtk_widget_set_halign (label, GTK_ALIGN_START);
586 gtk_widget_set_valign (label, GTK_ALIGN_CENTER);
587 gtk_widget_show (label);
588 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, TRUE, 0);
590 text_box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
591 gtk_widget_show (text_box);
592 gtk_box_pack_start (GTK_BOX (vbox), text_box, FALSE, TRUE, 0);
594 priv->preview_entry = gtk_entry_new ();
595 gtk_label_set_mnemonic_widget (GTK_LABEL (label), priv->preview_entry);
596 gtk_entry_set_text (GTK_ENTRY (priv->preview_entry), _(PREVIEW_TEXT));
598 gtk_widget_show (priv->preview_entry);
599 g_signal_connect (priv->preview_entry, "changed",
600 G_CALLBACK (gtk_font_selection_preview_changed), fontsel);
601 gtk_widget_set_size_request (priv->preview_entry,
602 -1, INITIAL_PREVIEW_HEIGHT);
603 gtk_box_pack_start (GTK_BOX (text_box), priv->preview_entry,
605 gtk_widget_pop_composite_child();
609 * gtk_font_selection_new:
611 * Creates a new #GtkFontSelection.
613 * Return value: a new #GtkFontSelection
615 * Deprecated: 3.2: Use #GtkFontChooserWidget instead
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_insert_with_values (model, &iter, -1,
827 FAMILY_COLUMN, families[i],
828 FAMILY_NAME_COLUMN, name,
831 if (i == 0 || !g_ascii_strcasecmp (name, "sans"))
833 match_family = families[i];
838 gtk_font_selection_ref_family (fontsel, match_family);
841 set_cursor_to_iter (GTK_TREE_VIEW (priv->family_list), &match_row);
842 #ifdef INCLUDE_FONT_ENTRIES
843 gtk_entry_set_text (GTK_ENTRY (priv->font_entry),
844 pango_font_family_get_name (match_family));
845 #endif /* INCLUDE_FONT_ENTRIES */
852 compare_font_descriptions (const PangoFontDescription *a, const PangoFontDescription *b)
854 int val = strcmp (pango_font_description_get_family (a), pango_font_description_get_family (b));
858 if (pango_font_description_get_weight (a) != pango_font_description_get_weight (b))
859 return pango_font_description_get_weight (a) - pango_font_description_get_weight (b);
861 if (pango_font_description_get_style (a) != pango_font_description_get_style (b))
862 return pango_font_description_get_style (a) - pango_font_description_get_style (b);
864 if (pango_font_description_get_stretch (a) != pango_font_description_get_stretch (b))
865 return pango_font_description_get_stretch (a) - pango_font_description_get_stretch (b);
867 if (pango_font_description_get_variant (a) != pango_font_description_get_variant (b))
868 return pango_font_description_get_variant (a) - pango_font_description_get_variant (b);
874 faces_sort_func (const void *a, const void *b)
876 PangoFontDescription *desc_a = pango_font_face_describe (*(PangoFontFace **)a);
877 PangoFontDescription *desc_b = pango_font_face_describe (*(PangoFontFace **)b);
879 int ord = compare_font_descriptions (desc_a, desc_b);
881 pango_font_description_free (desc_a);
882 pango_font_description_free (desc_b);
888 font_description_style_equal (const PangoFontDescription *a,
889 const PangoFontDescription *b)
891 return (pango_font_description_get_weight (a) == pango_font_description_get_weight (b) &&
892 pango_font_description_get_style (a) == pango_font_description_get_style (b) &&
893 pango_font_description_get_stretch (a) == pango_font_description_get_stretch (b) &&
894 pango_font_description_get_variant (a) == pango_font_description_get_variant (b));
897 /* This fills the font style list with all the possible style combinations
898 for the current font family. */
900 gtk_font_selection_show_available_styles (GtkFontSelection *fontsel)
902 GtkFontSelectionPrivate *priv = fontsel->priv;
904 PangoFontFace **faces;
905 PangoFontDescription *old_desc;
907 GtkTreeIter match_row;
908 PangoFontFace *match_face = NULL;
910 model = GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (priv->face_list)));
913 old_desc = pango_font_face_describe (priv->face);
917 pango_font_family_list_faces (priv->family, &faces, &n_faces);
918 qsort (faces, n_faces, sizeof (PangoFontFace *), faces_sort_func);
920 gtk_list_store_clear (model);
922 for (i=0; i < n_faces; i++)
925 const gchar *str = pango_font_face_get_face_name (faces[i]);
927 gtk_list_store_insert_with_values (model, &iter, -1,
928 FACE_COLUMN, faces[i],
929 FACE_NAME_COLUMN, str,
935 match_face = faces[i];
939 PangoFontDescription *tmp_desc = pango_font_face_describe (faces[i]);
941 if (font_description_style_equal (tmp_desc, old_desc))
944 match_face = faces[i];
947 pango_font_description_free (tmp_desc);
952 pango_font_description_free (old_desc);
954 gtk_font_selection_ref_face (fontsel, match_face);
957 #ifdef INCLUDE_FONT_ENTRIES
958 const gchar *str = pango_font_face_get_face_name (priv->face);
960 gtk_entry_set_text (GTK_ENTRY (priv->font_style_entry), str);
962 set_cursor_to_iter (GTK_TREE_VIEW (priv->face_list), &match_row);
968 /* This selects a style when the user selects a font. It just uses the first
969 available style at present. I was thinking of trying to maintain the
970 selected style, e.g. bold italic, when the user selects different fonts.
971 However, the interface is so easy to use now I'm not sure it's worth it.
972 Note: This will load a font. */
974 gtk_font_selection_select_best_style (GtkFontSelection *fontsel,
977 GtkFontSelectionPrivate *priv = fontsel->priv;
981 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->face_list));
983 if (gtk_tree_model_get_iter_first (model, &iter))
985 set_cursor_to_iter (GTK_TREE_VIEW (priv->face_list), &iter);
986 scroll_to_selection (GTK_TREE_VIEW (priv->face_list));
989 gtk_font_selection_show_available_sizes (fontsel, FALSE);
990 gtk_font_selection_select_best_size (fontsel);
994 /* This is called when a style is selected in the list. */
996 gtk_font_selection_select_style (GtkTreeSelection *selection,
999 GtkFontSelection *fontsel = GTK_FONT_SELECTION (data);
1000 GtkTreeModel *model;
1003 if (gtk_tree_selection_get_selected (selection, &model, &iter))
1005 PangoFontFace *face;
1007 gtk_tree_model_get (model, &iter, FACE_COLUMN, &face, -1);
1008 gtk_font_selection_ref_face (fontsel, face);
1009 g_object_unref (face);
1012 gtk_font_selection_show_available_sizes (fontsel, FALSE);
1013 gtk_font_selection_select_best_size (fontsel);
1017 gtk_font_selection_show_available_sizes (GtkFontSelection *fontsel,
1018 gboolean first_time)
1020 GtkFontSelectionPrivate *priv = fontsel->priv;
1022 GtkListStore *model;
1026 model = GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (priv->size_list)));
1028 /* Insert the standard font sizes */
1031 gtk_list_store_clear (model);
1033 for (i = 0; i < G_N_ELEMENTS (font_sizes); i++)
1037 gtk_list_store_insert_with_values (model, &iter, -1,
1038 SIZE_COLUMN, font_sizes[i], -1);
1040 if (font_sizes[i] * PANGO_SCALE == priv->size)
1041 set_cursor_to_iter (GTK_TREE_VIEW (priv->size_list), &iter);
1047 gboolean found = FALSE;
1049 gtk_tree_model_get_iter_first (GTK_TREE_MODEL (model), &iter);
1050 for (i = 0; i < G_N_ELEMENTS (font_sizes) && !found; i++)
1052 if (font_sizes[i] * PANGO_SCALE == priv->size)
1054 set_cursor_to_iter (GTK_TREE_VIEW (priv->size_list), &iter);
1058 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (model), &iter))
1064 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->size_list));
1065 gtk_tree_selection_unselect_all (selection);
1069 /* Set the entry to the new size, rounding to 1 digit,
1070 * trimming of trailing 0's and a trailing period
1072 g_snprintf (buffer, sizeof (buffer), "%.1f", priv->size / (1.0 * PANGO_SCALE));
1073 if (strchr (buffer, '.'))
1075 p = buffer + strlen (buffer) - 1;
1083 /* Compare, to avoid moving the cursor unecessarily */
1084 if (strcmp (gtk_entry_get_text (GTK_ENTRY (priv->size_entry)), buffer) != 0)
1085 gtk_entry_set_text (GTK_ENTRY (priv->size_entry), buffer);
1089 gtk_font_selection_select_best_size (GtkFontSelection *fontsel)
1091 gtk_font_selection_load_font (fontsel);
1095 gtk_font_selection_set_size (GtkFontSelection *fontsel,
1098 GtkFontSelectionPrivate *priv = fontsel->priv;
1100 if (priv->size != new_size)
1102 priv->size = new_size;
1104 gtk_font_selection_show_available_sizes (fontsel, FALSE);
1105 gtk_font_selection_load_font (fontsel);
1109 /* If the user hits return in the font size entry, we change to the new font
1112 gtk_font_selection_size_activate (GtkWidget *w,
1115 GtkFontSelection *fontsel = GTK_FONT_SELECTION (data);
1116 GtkFontSelectionPrivate *priv = fontsel->priv;
1120 text = gtk_entry_get_text (GTK_ENTRY (priv->size_entry));
1121 new_size = MAX (0.1, atof (text) * PANGO_SCALE + 0.5);
1123 if (priv->size != new_size)
1124 gtk_font_selection_set_size (fontsel, new_size);
1126 list_row_activated (w);
1130 gtk_font_selection_size_focus_out (GtkWidget *w,
1131 GdkEventFocus *event,
1134 GtkFontSelection *fontsel = GTK_FONT_SELECTION (data);
1135 GtkFontSelectionPrivate *priv = fontsel->priv;
1139 text = gtk_entry_get_text (GTK_ENTRY (priv->size_entry));
1140 new_size = MAX (0.1, atof (text) * PANGO_SCALE + 0.5);
1142 gtk_font_selection_set_size (fontsel, new_size);
1147 /* This is called when a size is selected in the list. */
1149 gtk_font_selection_select_size (GtkTreeSelection *selection,
1152 GtkFontSelection *fontsel = GTK_FONT_SELECTION (data);
1153 GtkTreeModel *model;
1157 if (gtk_tree_selection_get_selected (selection, &model, &iter))
1159 gtk_tree_model_get (model, &iter, SIZE_COLUMN, &new_size, -1);
1160 gtk_font_selection_set_size (fontsel, new_size * PANGO_SCALE);
1165 gtk_font_selection_load_font (GtkFontSelection *fontsel)
1167 gtk_font_selection_update_preview (fontsel);
1170 static PangoFontDescription *
1171 gtk_font_selection_get_font_description (GtkFontSelection *fontsel)
1173 GtkFontSelectionPrivate *priv = fontsel->priv;
1174 PangoFontDescription *font_desc;
1178 font_desc = pango_font_face_describe (priv->face);
1179 pango_font_description_set_size (font_desc, priv->size);
1182 font_desc = pango_font_description_from_string (DEFAULT_FONT_NAME);
1187 /* This sets the font in the preview entry to the selected font.
1190 gtk_font_selection_update_preview (GtkFontSelection *fontsel)
1192 GtkFontSelectionPrivate *priv = fontsel->priv;
1193 GtkWidget *preview_entry = priv->preview_entry;
1196 gtk_widget_override_font (preview_entry,
1197 gtk_font_selection_get_font_description (fontsel));
1199 /* This sets the preview text, if it hasn't been set already. */
1200 text = gtk_entry_get_text (GTK_ENTRY (preview_entry));
1201 if (strlen (text) == 0)
1202 gtk_entry_set_text (GTK_ENTRY (preview_entry), _(PREVIEW_TEXT));
1203 gtk_editable_set_position (GTK_EDITABLE (preview_entry), 0);
1207 /*****************************************************************************
1208 * These functions are the main public interface for getting/setting the font.
1209 *****************************************************************************/
1212 * gtk_font_selection_get_family_list:
1213 * @fontsel: a #GtkFontSelection
1215 * This returns the #GtkTreeView that lists font families, for
1216 * example, 'Sans', 'Serif', etc.
1218 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
1222 * Deprecated: 3.2: Use #GtkFontChooser
1225 gtk_font_selection_get_family_list (GtkFontSelection *fontsel)
1227 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1229 return fontsel->priv->family_list;
1233 * gtk_font_selection_get_face_list:
1234 * @fontsel: a #GtkFontSelection
1236 * This returns the #GtkTreeView which lists all styles available for
1237 * the selected font. For example, 'Regular', 'Bold', etc.
1239 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
1243 * Deprecated: 3.2: Use #GtkFontChooser
1246 gtk_font_selection_get_face_list (GtkFontSelection *fontsel)
1248 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1250 return fontsel->priv->face_list;
1254 * gtk_font_selection_get_size_entry:
1255 * @fontsel: a #GtkFontSelection
1257 * This returns the #GtkEntry used to allow the user to edit the font
1258 * number manually instead of selecting it from the list of font sizes.
1260 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
1264 * Deprecated: 3.2: Use #GtkFontChooser
1267 gtk_font_selection_get_size_entry (GtkFontSelection *fontsel)
1269 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1271 return fontsel->priv->size_entry;
1275 * gtk_font_selection_get_size_list:
1276 * @fontsel: a #GtkFontSelection
1278 * This returns the #GtkTreeView used to list font sizes.
1280 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
1284 * Deprecated: 3.2: Use #GtkFontChooser
1287 gtk_font_selection_get_size_list (GtkFontSelection *fontsel)
1289 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1291 return fontsel->priv->size_list;
1295 * gtk_font_selection_get_preview_entry:
1296 * @fontsel: a #GtkFontSelection
1298 * This returns the #GtkEntry used to display the font as a preview.
1300 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
1304 * Deprecated: 3.2: Use #GtkFontChooser
1307 gtk_font_selection_get_preview_entry (GtkFontSelection *fontsel)
1309 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1311 return fontsel->priv->preview_entry;
1315 * gtk_font_selection_get_family:
1316 * @fontsel: a #GtkFontSelection
1318 * Gets the #PangoFontFamily representing the selected font family.
1320 * Return value: (transfer none): A #PangoFontFamily representing the
1321 * selected font family. Font families are a collection of font
1322 * faces. The returned object is owned by @fontsel and must not
1323 * be modified or freed.
1327 * Deprecated: 3.2: Use #GtkFontChooser
1330 gtk_font_selection_get_family (GtkFontSelection *fontsel)
1332 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1334 return fontsel->priv->family;
1338 * gtk_font_selection_get_face:
1339 * @fontsel: a #GtkFontSelection
1341 * Gets the #PangoFontFace representing the selected font group
1342 * details (i.e. family, slant, weight, width, etc).
1344 * Return value: (transfer none): A #PangoFontFace representing the
1345 * selected font group details. The returned object is owned by
1346 * @fontsel and must not be modified or freed.
1350 * Deprecated: 3.2: Use #GtkFontChooser
1353 gtk_font_selection_get_face (GtkFontSelection *fontsel)
1355 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1357 return fontsel->priv->face;
1361 * gtk_font_selection_get_size:
1362 * @fontsel: a #GtkFontSelection
1364 * The selected font size.
1366 * Return value: A n integer representing the selected font size,
1367 * or -1 if no font size is selected.
1371 * Deprecated: 3.2: Use #GtkFontChooser
1374 gtk_font_selection_get_size (GtkFontSelection *fontsel)
1376 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), -1);
1378 return fontsel->priv->size;
1382 * gtk_font_selection_get_font_name:
1383 * @fontsel: a #GtkFontSelection
1385 * Gets the currently-selected font name.
1387 * Note that this can be a different string than what you set with
1388 * gtk_font_selection_set_font_name(), as the font selection widget may
1389 * normalize font names and thus return a string with a different structure.
1390 * For example, "Helvetica Italic Bold 12" could be normalized to
1391 * "Helvetica Bold Italic 12". Use pango_font_description_equal()
1392 * if you want to compare two font descriptions.
1394 * Return value: A string with the name of the current font, or %NULL if
1395 * no font is selected. You must free this string with g_free().
1397 * Deprecated: 3.2: Use #GtkFontChooser
1400 gtk_font_selection_get_font_name (GtkFontSelection *fontsel)
1404 PangoFontDescription *font_desc = gtk_font_selection_get_font_description (fontsel);
1405 result = pango_font_description_to_string (font_desc);
1406 pango_font_description_free (font_desc);
1411 /* This selects the appropriate list rows.
1412 First we check the fontname is valid and try to find the font family
1413 - i.e. the name in the main list. If we can't find that, then just return.
1414 Next we try to set each of the properties according to the fontname.
1415 Finally we select the font family & style in the lists. */
1417 gtk_font_selection_select_font_desc (GtkFontSelection *fontsel,
1418 PangoFontDescription *new_desc,
1419 PangoFontFamily **pfamily,
1420 PangoFontFace **pface)
1422 GtkFontSelectionPrivate *priv = fontsel->priv;
1423 PangoFontFamily *new_family = NULL;
1424 PangoFontFace *new_face = NULL;
1425 PangoFontFace *fallback_face = NULL;
1426 GtkTreeModel *model;
1428 GtkTreeIter match_iter;
1430 const gchar *new_family_name;
1432 new_family_name = pango_font_description_get_family (new_desc);
1434 if (!new_family_name)
1437 /* Check to make sure that this is in the list of allowed fonts
1439 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->family_list));
1440 for (valid = gtk_tree_model_get_iter_first (model, &iter);
1442 valid = gtk_tree_model_iter_next (model, &iter))
1444 PangoFontFamily *family;
1446 gtk_tree_model_get (model, &iter, FAMILY_COLUMN, &family, -1);
1448 if (g_ascii_strcasecmp (pango_font_family_get_name (family),
1449 new_family_name) == 0)
1450 new_family = g_object_ref (family);
1452 g_object_unref (family);
1462 *pfamily = new_family;
1464 g_object_unref (new_family);
1465 set_cursor_to_iter (GTK_TREE_VIEW (priv->family_list), &iter);
1466 gtk_font_selection_show_available_styles (fontsel);
1468 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->face_list));
1469 for (valid = gtk_tree_model_get_iter_first (model, &iter);
1471 valid = gtk_tree_model_iter_next (model, &iter))
1473 PangoFontFace *face;
1474 PangoFontDescription *tmp_desc;
1476 gtk_tree_model_get (model, &iter, FACE_COLUMN, &face, -1);
1477 tmp_desc = pango_font_face_describe (face);
1479 if (font_description_style_equal (tmp_desc, new_desc))
1480 new_face = g_object_ref (face);
1484 fallback_face = g_object_ref (face);
1488 pango_font_description_free (tmp_desc);
1489 g_object_unref (face);
1499 new_face = fallback_face;
1500 else if (fallback_face)
1501 g_object_unref (fallback_face);
1506 g_object_unref (new_face);
1507 set_cursor_to_iter (GTK_TREE_VIEW (priv->face_list), &match_iter);
1509 gtk_font_selection_set_size (fontsel, pango_font_description_get_size (new_desc));
1515 /* This sets the current font, then selecting the appropriate list rows. */
1518 * gtk_font_selection_set_font_name:
1519 * @fontsel: a #GtkFontSelection
1520 * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
1522 * Sets the currently-selected font.
1524 * Note that the @fontsel needs to know the screen in which it will appear
1525 * for this to work; this can be guaranteed by simply making sure that the
1526 * @fontsel is inserted in a toplevel window before you call this function.
1528 * Return value: %TRUE if the font could be set successfully; %FALSE if no
1529 * such font exists or if the @fontsel doesn't belong to a particular
1532 * Deprecated: 3.2: Use #GtkFontChooser
1535 gtk_font_selection_set_font_name (GtkFontSelection *fontsel,
1536 const gchar *fontname)
1538 PangoFontFamily *family = NULL;
1539 PangoFontFace *face = NULL;
1540 PangoFontDescription *new_desc;
1542 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), FALSE);
1544 if (!gtk_widget_has_screen (GTK_WIDGET (fontsel)))
1547 new_desc = pango_font_description_from_string (fontname);
1549 if (gtk_font_selection_select_font_desc (fontsel, new_desc, &family, &face))
1551 gtk_font_selection_ref_family (fontsel, family);
1553 g_object_unref (family);
1555 gtk_font_selection_ref_face (fontsel, face);
1557 g_object_unref (face);
1560 pango_font_description_free (new_desc);
1562 g_object_notify (G_OBJECT (fontsel), "font-name");
1568 * gtk_font_selection_get_preview_text:
1569 * @fontsel: a #GtkFontSelection
1571 * Gets the text displayed in the preview area.
1573 * Return value: the text displayed in the preview area.
1574 * This string is owned by the widget and should not be
1577 * Deprecated: 3.2: Use #GtkFontChooser
1580 gtk_font_selection_get_preview_text (GtkFontSelection *fontsel)
1582 GtkFontSelectionPrivate *priv;
1584 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1586 priv = fontsel->priv;
1588 return gtk_entry_get_text (GTK_ENTRY (priv->preview_entry));
1593 * gtk_font_selection_set_preview_text:
1594 * @fontsel: a #GtkFontSelection
1595 * @text: the text to display in the preview area
1597 * Sets the text displayed in the preview area.
1598 * The @text is used to show how the selected font looks.
1600 * Deprecated: 3.2: Use #GtkFontChooser
1603 gtk_font_selection_set_preview_text (GtkFontSelection *fontsel,
1606 GtkFontSelectionPrivate *priv;
1608 g_return_if_fail (GTK_IS_FONT_SELECTION (fontsel));
1609 g_return_if_fail (text != NULL);
1611 priv = fontsel->priv;
1613 gtk_entry_set_text (GTK_ENTRY (priv->preview_entry), text);
1618 * SECTION:gtkfontseldlg
1619 * @Short_description: Deprecated dialog box for selecting fonts
1620 * @Title: GtkFontSelectionDialog
1621 * @See_also: #GtkFontSelection, #GtkDialog, #GtkFontChooserDialog
1623 * The #GtkFontSelectionDialog widget is a dialog box for selecting a font.
1625 * To set the font which is initially selected, use
1626 * gtk_font_selection_dialog_set_font_name().
1628 * To get the selected font use gtk_font_selection_dialog_get_font_name().
1630 * To change the text which is shown in the preview area, use
1631 * gtk_font_selection_dialog_set_preview_text().
1633 * In GTK+ 3.2, #GtkFontSelectionDialog has been deprecated in favor of
1634 * #GtkFontChooserDialog.
1636 * <refsect2 id="GtkFontSelectionDialog-BUILDER-UI">
1637 * <title>GtkFontSelectionDialog as GtkBuildable</title>
1638 * The GtkFontSelectionDialog implementation of the GtkBuildable interface
1639 * exposes the embedded #GtkFontSelection as internal child with the
1640 * name "font_selection". It also exposes the buttons with the names
1641 * "ok_button", "cancel_button" and "apply_button".
1645 static void gtk_font_selection_dialog_buildable_interface_init (GtkBuildableIface *iface);
1646 static GObject * gtk_font_selection_dialog_buildable_get_internal_child (GtkBuildable *buildable,
1647 GtkBuilder *builder,
1648 const gchar *childname);
1650 G_DEFINE_TYPE_WITH_CODE (GtkFontSelectionDialog, gtk_font_selection_dialog,
1652 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
1653 gtk_font_selection_dialog_buildable_interface_init))
1655 static GtkBuildableIface *parent_buildable_iface;
1658 gtk_font_selection_dialog_class_init (GtkFontSelectionDialogClass *klass)
1660 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1662 gtk_widget_class_set_accessible_role (widget_class, ATK_ROLE_FONT_CHOOSER);
1664 g_type_class_add_private (klass, sizeof (GtkFontSelectionDialogPrivate));
1668 gtk_font_selection_dialog_init (GtkFontSelectionDialog *fontseldiag)
1670 GtkFontSelectionDialogPrivate *priv;
1671 GtkDialog *dialog = GTK_DIALOG (fontseldiag);
1672 GtkWidget *action_area, *content_area;
1674 fontseldiag->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontseldiag,
1675 GTK_TYPE_FONT_SELECTION_DIALOG,
1676 GtkFontSelectionDialogPrivate);
1677 priv = fontseldiag->priv;
1679 content_area = gtk_dialog_get_content_area (dialog);
1680 action_area = gtk_dialog_get_action_area (dialog);
1682 gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
1683 gtk_box_set_spacing (GTK_BOX (content_area), 2); /* 2 * 5 + 2 = 12 */
1684 gtk_container_set_border_width (GTK_CONTAINER (action_area), 5);
1685 gtk_box_set_spacing (GTK_BOX (action_area), 6);
1687 gtk_widget_push_composite_child ();
1689 gtk_window_set_resizable (GTK_WINDOW (fontseldiag), TRUE);
1691 /* Create the content area */
1692 priv->fontsel = gtk_font_selection_new ();
1693 gtk_container_set_border_width (GTK_CONTAINER (priv->fontsel), 5);
1694 gtk_widget_show (priv->fontsel);
1695 gtk_box_pack_start (GTK_BOX (content_area),
1696 priv->fontsel, TRUE, TRUE, 0);
1698 /* Create the action area */
1699 priv->cancel_button = gtk_dialog_add_button (dialog,
1701 GTK_RESPONSE_CANCEL);
1703 priv->apply_button = gtk_dialog_add_button (dialog,
1705 GTK_RESPONSE_APPLY);
1706 gtk_widget_hide (priv->apply_button);
1708 priv->ok_button = gtk_dialog_add_button (dialog,
1711 gtk_widget_grab_default (priv->ok_button);
1713 gtk_dialog_set_alternative_button_order (GTK_DIALOG (fontseldiag),
1716 GTK_RESPONSE_CANCEL,
1719 gtk_window_set_title (GTK_WINDOW (fontseldiag),
1720 _("Font Selection"));
1722 gtk_widget_pop_composite_child ();
1726 * gtk_font_selection_dialog_new:
1727 * @title: the title of the dialog window
1729 * Creates a new #GtkFontSelectionDialog.
1731 * Return value: a new #GtkFontSelectionDialog
1733 * Deprecated: 3.2: Use #GtkFontChooserDialog
1736 gtk_font_selection_dialog_new (const gchar *title)
1738 GtkFontSelectionDialog *fontseldiag;
1740 fontseldiag = g_object_new (GTK_TYPE_FONT_SELECTION_DIALOG, NULL);
1743 gtk_window_set_title (GTK_WINDOW (fontseldiag), title);
1745 return GTK_WIDGET (fontseldiag);
1749 * gtk_font_selection_dialog_get_font_selection:
1750 * @fsd: a #GtkFontSelectionDialog
1752 * Retrieves the #GtkFontSelection widget embedded in the dialog.
1754 * Returns: (transfer none): the embedded #GtkFontSelection
1758 * Deprecated: 3.2: Use #GtkFontChooserDialog
1761 gtk_font_selection_dialog_get_font_selection (GtkFontSelectionDialog *fsd)
1763 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1765 return fsd->priv->fontsel;
1770 * gtk_font_selection_dialog_get_ok_button:
1771 * @fsd: a #GtkFontSelectionDialog
1773 * Gets the 'OK' button.
1775 * Return value: (transfer none): the #GtkWidget used in the dialog
1776 * for the 'OK' button.
1780 * Deprecated: 3.2: Use #GtkFontChooserDialog
1783 gtk_font_selection_dialog_get_ok_button (GtkFontSelectionDialog *fsd)
1785 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1787 return fsd->priv->ok_button;
1791 * gtk_font_selection_dialog_get_cancel_button:
1792 * @fsd: a #GtkFontSelectionDialog
1794 * Gets the 'Cancel' button.
1796 * Return value: (transfer none): the #GtkWidget used in the dialog
1797 * for the 'Cancel' button.
1801 * Deprecated: 3.2: Use #GtkFontChooserDialog
1804 gtk_font_selection_dialog_get_cancel_button (GtkFontSelectionDialog *fsd)
1806 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1808 return fsd->priv->cancel_button;
1812 gtk_font_selection_dialog_buildable_interface_init (GtkBuildableIface *iface)
1814 parent_buildable_iface = g_type_interface_peek_parent (iface);
1815 iface->get_internal_child = gtk_font_selection_dialog_buildable_get_internal_child;
1819 gtk_font_selection_dialog_buildable_get_internal_child (GtkBuildable *buildable,
1820 GtkBuilder *builder,
1821 const gchar *childname)
1823 GtkFontSelectionDialogPrivate *priv;
1825 priv = GTK_FONT_SELECTION_DIALOG (buildable)->priv;
1827 if (g_strcmp0 (childname, "ok_button") == 0)
1828 return G_OBJECT (priv->ok_button);
1829 else if (g_strcmp0 (childname, "cancel_button") == 0)
1830 return G_OBJECT (priv->cancel_button);
1831 else if (g_strcmp0 (childname, "apply_button") == 0)
1832 return G_OBJECT (priv->apply_button);
1833 else if (g_strcmp0 (childname, "font_selection") == 0)
1834 return G_OBJECT (priv->fontsel);
1836 return parent_buildable_iface->get_internal_child (buildable, builder, childname);
1840 * gtk_font_selection_dialog_get_font_name:
1841 * @fsd: a #GtkFontSelectionDialog
1843 * Gets the currently-selected font name.
1845 * Note that this can be a different string than what you set with
1846 * gtk_font_selection_dialog_set_font_name(), as the font selection widget
1847 * may normalize font names and thus return a string with a different
1848 * structure. For example, "Helvetica Italic Bold 12" could be normalized
1849 * to "Helvetica Bold Italic 12". Use pango_font_description_equal()
1850 * if you want to compare two font descriptions.
1852 * Return value: A string with the name of the current font, or %NULL if no
1853 * font is selected. You must free this string with g_free().
1855 * Deprecated: 3.2: Use #GtkFontChooserDialog
1858 gtk_font_selection_dialog_get_font_name (GtkFontSelectionDialog *fsd)
1860 GtkFontSelectionDialogPrivate *priv;
1862 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1866 return gtk_font_selection_get_font_name (GTK_FONT_SELECTION (priv->fontsel));
1870 * gtk_font_selection_dialog_set_font_name:
1871 * @fsd: a #GtkFontSelectionDialog
1872 * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
1874 * Sets the currently selected font.
1876 * Return value: %TRUE if the font selected in @fsd is now the
1877 * @fontname specified, %FALSE otherwise.
1879 * Deprecated: 3.2: Use #GtkFontChooserDialog
1882 gtk_font_selection_dialog_set_font_name (GtkFontSelectionDialog *fsd,
1883 const gchar *fontname)
1885 GtkFontSelectionDialogPrivate *priv;
1887 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), FALSE);
1888 g_return_val_if_fail (fontname, FALSE);
1892 return gtk_font_selection_set_font_name (GTK_FONT_SELECTION (priv->fontsel), fontname);
1896 * gtk_font_selection_dialog_get_preview_text:
1897 * @fsd: a #GtkFontSelectionDialog
1899 * Gets the text displayed in the preview area.
1901 * Return value: the text displayed in the preview area.
1902 * This string is owned by the widget and should not be
1905 * Deprecated: 3.2: Use #GtkFontChooserDialog
1908 gtk_font_selection_dialog_get_preview_text (GtkFontSelectionDialog *fsd)
1910 GtkFontSelectionDialogPrivate *priv;
1912 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1916 return gtk_font_selection_get_preview_text (GTK_FONT_SELECTION (priv->fontsel));
1920 * gtk_font_selection_dialog_set_preview_text:
1921 * @fsd: a #GtkFontSelectionDialog
1922 * @text: the text to display in the preview area
1924 * Sets the text displayed in the preview area.
1926 * Deprecated: 3.2: Use #GtkFontChooserDialog
1929 gtk_font_selection_dialog_set_preview_text (GtkFontSelectionDialog *fsd,
1932 GtkFontSelectionDialogPrivate *priv;
1934 g_return_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd));
1935 g_return_if_fail (text != NULL);
1939 gtk_font_selection_set_preview_text (GTK_FONT_SELECTION (priv->fontsel), text);