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/.
34 #include <glib/gprintf.h>
40 #include "gdk/gdkkeysyms.h"
42 #include "gtkfontsel.h"
44 #include "gtkbutton.h"
45 #include "gtkcellrenderertext.h"
51 #include "gtkliststore.h"
55 #include "gtktreeselection.h"
56 #include "gtktreeview.h"
58 #include "gtkscrolledwindow.h"
60 #include "gtkaccessible.h"
61 #include "gtkprivate.h"
62 #include "gtkbuildable.h"
64 /* We don't enable the font and style entries because they don't add
65 * much in terms of visible effect and have a weird effect on keynav.
66 * the Windows font selector has entries similarly positioned but they
67 * act in conjunction with the associated lists to form a single focus
70 #undef INCLUDE_FONT_ENTRIES
72 /* This is the default text shown in the preview entry, though the user
73 can set it. Remember that some fonts only have capital letters. */
74 #define PREVIEW_TEXT N_("abcdefghijk ABCDEFGHIJK")
76 #define DEFAULT_FONT_NAME "Sans 10"
78 /* This is the initial and maximum height of the preview entry (it expands
79 when large font sizes are selected). Initial height is also the minimum. */
80 #define INITIAL_PREVIEW_HEIGHT 44
81 #define MAX_PREVIEW_HEIGHT 300
83 /* These are the sizes of the font, style & size lists. */
84 #define FONT_LIST_HEIGHT 136
85 #define FONT_LIST_WIDTH 190
86 #define FONT_STYLE_LIST_WIDTH 170
87 #define FONT_SIZE_LIST_WIDTH 60
89 /* These are what we use as the standard font sizes, for the size list.
91 static const guint16 font_sizes[] = {
92 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, 22, 24, 26, 28,
93 32, 36, 40, 48, 56, 64, 72
117 static void gtk_font_selection_set_property (GObject *object,
121 static void gtk_font_selection_get_property (GObject *object,
125 static void gtk_font_selection_finalize (GObject *object);
126 static void gtk_font_selection_screen_changed (GtkWidget *widget,
127 GdkScreen *previous_screen);
128 static void gtk_font_selection_style_set (GtkWidget *widget,
129 GtkStyle *prev_style);
131 /* These are the callbacks & related functions. */
132 static void gtk_font_selection_select_font (GtkTreeSelection *selection,
134 static void gtk_font_selection_show_available_fonts (GtkFontSelection *fs);
136 static void gtk_font_selection_show_available_styles (GtkFontSelection *fs);
137 static void gtk_font_selection_select_best_style (GtkFontSelection *fs,
139 static void gtk_font_selection_select_style (GtkTreeSelection *selection,
142 static void gtk_font_selection_select_best_size (GtkFontSelection *fs);
143 static void gtk_font_selection_show_available_sizes (GtkFontSelection *fs,
144 gboolean first_time);
145 static void gtk_font_selection_size_activate (GtkWidget *w,
147 static gboolean gtk_font_selection_size_focus_out (GtkWidget *w,
148 GdkEventFocus *event,
150 static void gtk_font_selection_select_size (GtkTreeSelection *selection,
153 static void gtk_font_selection_scroll_on_map (GtkWidget *w,
156 static void gtk_font_selection_preview_changed (GtkWidget *entry,
157 GtkFontSelection *fontsel);
158 static void gtk_font_selection_scroll_to_selection (GtkFontSelection *fontsel);
161 /* Misc. utility functions. */
162 static void gtk_font_selection_load_font (GtkFontSelection *fs);
163 static void gtk_font_selection_update_preview (GtkFontSelection *fs);
165 static PangoFontDescription *gtk_font_selection_get_font_description (GtkFontSelection *fontsel);
166 static gboolean gtk_font_selection_select_font_desc (GtkFontSelection *fontsel,
167 PangoFontDescription *new_desc,
168 PangoFontFamily **pfamily,
169 PangoFontFace **pface);
170 static void gtk_font_selection_reload_fonts (GtkFontSelection *fontsel);
171 static void gtk_font_selection_ref_family (GtkFontSelection *fontsel,
172 PangoFontFamily *family);
173 static void gtk_font_selection_ref_face (GtkFontSelection *fontsel,
174 PangoFontFace *face);
176 G_DEFINE_TYPE (GtkFontSelection, gtk_font_selection, GTK_TYPE_VBOX)
179 gtk_font_selection_class_init (GtkFontSelectionClass *klass)
181 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
182 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
184 gobject_class->set_property = gtk_font_selection_set_property;
185 gobject_class->get_property = gtk_font_selection_get_property;
187 widget_class->screen_changed = gtk_font_selection_screen_changed;
188 widget_class->style_set = gtk_font_selection_style_set;
190 g_object_class_install_property (gobject_class,
192 g_param_spec_string ("font-name",
194 P_("The string that represents this font"),
196 GTK_PARAM_READWRITE));
197 g_object_class_install_property (gobject_class,
199 g_param_spec_string ("preview-text",
201 P_("The text to display in order to demonstrate the selected font"),
203 GTK_PARAM_READWRITE));
204 gobject_class->finalize = gtk_font_selection_finalize;
208 gtk_font_selection_set_property (GObject *object,
213 GtkFontSelection *fontsel;
215 fontsel = GTK_FONT_SELECTION (object);
220 gtk_font_selection_set_font_name (fontsel, g_value_get_string (value));
222 case PROP_PREVIEW_TEXT:
223 gtk_font_selection_set_preview_text (fontsel, g_value_get_string (value));
226 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
231 static void gtk_font_selection_get_property (GObject *object,
236 GtkFontSelection *fontsel;
238 fontsel = GTK_FONT_SELECTION (object);
243 g_value_take_string (value, gtk_font_selection_get_font_name (fontsel));
245 case PROP_PREVIEW_TEXT:
246 g_value_set_string (value, gtk_font_selection_get_preview_text (fontsel));
249 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
254 /* Handles key press events on the lists, so that we can trap Enter to
255 * activate the default button on our own.
258 list_row_activated (GtkWidget *widget)
262 window = GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (widget)));
263 if (!gtk_widget_is_toplevel (GTK_WIDGET (window)))
267 && widget != window->default_widget
268 && !(widget == window->focus_widget &&
269 (!window->default_widget || !gtk_widget_get_sensitive (window->default_widget))))
271 gtk_window_activate_default (window);
278 gtk_font_selection_init (GtkFontSelection *fontsel)
280 GtkWidget *scrolled_win;
282 GtkWidget *table, *label;
283 GtkWidget *font_label, *style_label;
286 GtkTreeViewColumn *column;
287 GList *focus_chain = NULL;
290 gtk_widget_push_composite_child ();
292 gtk_box_set_spacing (GTK_BOX (fontsel), 12);
293 fontsel->size = 12 * PANGO_SCALE;
295 /* Create the table of font, style & size. */
296 table = gtk_table_new (3, 3, FALSE);
297 gtk_widget_show (table);
298 gtk_table_set_row_spacings (GTK_TABLE (table), 6);
299 gtk_table_set_col_spacings (GTK_TABLE (table), 12);
300 gtk_box_pack_start (GTK_BOX (fontsel), table, TRUE, TRUE, 0);
302 #ifdef INCLUDE_FONT_ENTRIES
303 fontsel->font_entry = gtk_entry_new ();
304 gtk_editable_set_editable (GTK_EDITABLE (fontsel->font_entry), FALSE);
305 gtk_widget_set_size_request (fontsel->font_entry, 20, -1);
306 gtk_widget_show (fontsel->font_entry);
307 gtk_table_attach (GTK_TABLE (table), fontsel->font_entry, 0, 1, 1, 2,
310 fontsel->font_style_entry = gtk_entry_new ();
311 gtk_editable_set_editable (GTK_EDITABLE (fontsel->font_style_entry), FALSE);
312 gtk_widget_set_size_request (fontsel->font_style_entry, 20, -1);
313 gtk_widget_show (fontsel->font_style_entry);
314 gtk_table_attach (GTK_TABLE (table), fontsel->font_style_entry, 1, 2, 1, 2,
316 #endif /* INCLUDE_FONT_ENTRIES */
318 fontsel->size_entry = gtk_entry_new ();
319 gtk_widget_set_size_request (fontsel->size_entry, 20, -1);
320 gtk_widget_show (fontsel->size_entry);
321 gtk_table_attach (GTK_TABLE (table), fontsel->size_entry, 2, 3, 1, 2,
323 g_signal_connect (fontsel->size_entry, "activate",
324 G_CALLBACK (gtk_font_selection_size_activate),
326 g_signal_connect_after (fontsel->size_entry, "focus-out-event",
327 G_CALLBACK (gtk_font_selection_size_focus_out),
330 font_label = gtk_label_new_with_mnemonic (_("_Family:"));
331 gtk_misc_set_alignment (GTK_MISC (font_label), 0.0, 0.5);
332 gtk_widget_show (font_label);
333 gtk_table_attach (GTK_TABLE (table), font_label, 0, 1, 0, 1,
336 style_label = gtk_label_new_with_mnemonic (_("_Style:"));
337 gtk_misc_set_alignment (GTK_MISC (style_label), 0.0, 0.5);
338 gtk_widget_show (style_label);
339 gtk_table_attach (GTK_TABLE (table), style_label, 1, 2, 0, 1,
342 label = gtk_label_new_with_mnemonic (_("Si_ze:"));
343 gtk_label_set_mnemonic_widget (GTK_LABEL (label),
344 fontsel->size_entry);
345 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
346 gtk_widget_show (label);
347 gtk_table_attach (GTK_TABLE (table), label, 2, 3, 0, 1,
351 /* Create the lists */
353 model = gtk_list_store_new (2,
354 G_TYPE_OBJECT, /* FAMILY_COLUMN */
355 G_TYPE_STRING); /* FAMILY_NAME_COLUMN */
356 fontsel->family_list = gtk_tree_view_new_with_model (GTK_TREE_MODEL (model));
357 g_object_unref (model);
359 g_signal_connect (fontsel->family_list, "row-activated",
360 G_CALLBACK (list_row_activated), fontsel);
362 column = gtk_tree_view_column_new_with_attributes ("Family",
363 gtk_cell_renderer_text_new (),
364 "text", FAMILY_NAME_COLUMN,
366 gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
367 gtk_tree_view_append_column (GTK_TREE_VIEW (fontsel->family_list), column);
369 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (fontsel->family_list), FALSE);
370 gtk_tree_selection_set_mode (gtk_tree_view_get_selection (GTK_TREE_VIEW (fontsel->family_list)),
371 GTK_SELECTION_BROWSE);
373 gtk_label_set_mnemonic_widget (GTK_LABEL (font_label), fontsel->family_list);
375 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
376 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win), GTK_SHADOW_IN);
377 gtk_widget_set_size_request (scrolled_win,
378 FONT_LIST_WIDTH, FONT_LIST_HEIGHT);
379 gtk_container_add (GTK_CONTAINER (scrolled_win), fontsel->family_list);
380 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
381 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
382 gtk_widget_show (fontsel->family_list);
383 gtk_widget_show (scrolled_win);
385 gtk_table_attach (GTK_TABLE (table), scrolled_win, 0, 1, 1, 3,
386 GTK_EXPAND | GTK_FILL,
387 GTK_EXPAND | GTK_FILL, 0, 0);
388 focus_chain = g_list_append (focus_chain, scrolled_win);
390 model = gtk_list_store_new (2,
391 G_TYPE_OBJECT, /* FACE_COLUMN */
392 G_TYPE_STRING); /* FACE_NAME_COLUMN */
393 fontsel->face_list = gtk_tree_view_new_with_model (GTK_TREE_MODEL (model));
394 g_object_unref (model);
395 g_signal_connect (fontsel->face_list, "row-activated",
396 G_CALLBACK (list_row_activated), fontsel);
398 gtk_label_set_mnemonic_widget (GTK_LABEL (style_label), fontsel->face_list);
400 column = gtk_tree_view_column_new_with_attributes ("Face",
401 gtk_cell_renderer_text_new (),
402 "text", FACE_NAME_COLUMN,
404 gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
405 gtk_tree_view_append_column (GTK_TREE_VIEW (fontsel->face_list), column);
407 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (fontsel->face_list), FALSE);
408 gtk_tree_selection_set_mode (gtk_tree_view_get_selection (GTK_TREE_VIEW (fontsel->face_list)),
409 GTK_SELECTION_BROWSE);
411 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
412 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win), GTK_SHADOW_IN);
413 gtk_widget_set_size_request (scrolled_win,
414 FONT_STYLE_LIST_WIDTH, FONT_LIST_HEIGHT);
415 gtk_container_add (GTK_CONTAINER (scrolled_win), fontsel->face_list);
416 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
417 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
418 gtk_widget_show (fontsel->face_list);
419 gtk_widget_show (scrolled_win);
420 gtk_table_attach (GTK_TABLE (table), scrolled_win, 1, 2, 1, 3,
421 GTK_EXPAND | GTK_FILL,
422 GTK_EXPAND | GTK_FILL, 0, 0);
423 focus_chain = g_list_append (focus_chain, scrolled_win);
425 focus_chain = g_list_append (focus_chain, fontsel->size_entry);
427 model = gtk_list_store_new (1, G_TYPE_INT);
428 fontsel->size_list = gtk_tree_view_new_with_model (GTK_TREE_MODEL (model));
429 g_object_unref (model);
430 g_signal_connect (fontsel->size_list, "row-activated",
431 G_CALLBACK (list_row_activated), fontsel);
433 column = gtk_tree_view_column_new_with_attributes ("Size",
434 gtk_cell_renderer_text_new (),
437 gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
438 gtk_tree_view_append_column (GTK_TREE_VIEW (fontsel->size_list), column);
440 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (fontsel->size_list), FALSE);
441 gtk_tree_selection_set_mode (gtk_tree_view_get_selection (GTK_TREE_VIEW (fontsel->size_list)),
442 GTK_SELECTION_BROWSE);
444 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
445 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win), GTK_SHADOW_IN);
446 gtk_container_add (GTK_CONTAINER (scrolled_win), fontsel->size_list);
447 gtk_widget_set_size_request (scrolled_win, -1, FONT_LIST_HEIGHT);
448 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
449 GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
450 gtk_widget_show (fontsel->size_list);
451 gtk_widget_show (scrolled_win);
452 gtk_table_attach (GTK_TABLE (table), scrolled_win, 2, 3, 2, 3,
453 GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
454 focus_chain = g_list_append (focus_chain, scrolled_win);
456 gtk_container_set_focus_chain (GTK_CONTAINER (table), focus_chain);
457 g_list_free (focus_chain);
459 /* Insert the fonts. */
460 g_signal_connect (gtk_tree_view_get_selection (GTK_TREE_VIEW (fontsel->family_list)), "changed",
461 G_CALLBACK (gtk_font_selection_select_font), fontsel);
463 g_signal_connect_after (fontsel->family_list, "map",
464 G_CALLBACK (gtk_font_selection_scroll_on_map),
467 g_signal_connect (gtk_tree_view_get_selection (GTK_TREE_VIEW (fontsel->face_list)), "changed",
468 G_CALLBACK (gtk_font_selection_select_style), fontsel);
470 g_signal_connect (gtk_tree_view_get_selection (GTK_TREE_VIEW (fontsel->size_list)), "changed",
471 G_CALLBACK (gtk_font_selection_select_size), fontsel);
472 atk_obj = gtk_widget_get_accessible (fontsel->size_list);
473 if (GTK_IS_ACCESSIBLE (atk_obj))
475 /* Accessibility support is enabled.
476 * Make the label ATK_RELATON_LABEL_FOR for the size list as well.
478 AtkObject *atk_label;
479 AtkRelationSet *relation_set;
480 AtkRelation *relation;
481 AtkObject *obj_array[1];
483 atk_label = gtk_widget_get_accessible (label);
484 relation_set = atk_object_ref_relation_set (atk_obj);
485 relation = atk_relation_set_get_relation_by_type (relation_set, ATK_RELATION_LABELLED_BY);
488 atk_relation_add_target (relation, atk_label);
492 obj_array[0] = atk_label;
493 relation = atk_relation_new (obj_array, 1, ATK_RELATION_LABELLED_BY);
494 atk_relation_set_add (relation_set, relation);
496 g_object_unref (relation_set);
498 relation_set = atk_object_ref_relation_set (atk_label);
499 relation = atk_relation_set_get_relation_by_type (relation_set, ATK_RELATION_LABEL_FOR);
502 atk_relation_add_target (relation, atk_obj);
506 obj_array[0] = atk_obj;
507 relation = atk_relation_new (obj_array, 1, ATK_RELATION_LABEL_FOR);
508 atk_relation_set_add (relation_set, relation);
510 g_object_unref (relation_set);
514 vbox = gtk_vbox_new (FALSE, 6);
515 gtk_widget_show (vbox);
516 gtk_box_pack_start (GTK_BOX (fontsel), vbox, FALSE, TRUE, 0);
518 /* create the text entry widget */
519 label = gtk_label_new_with_mnemonic (_("_Preview:"));
520 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
521 gtk_widget_show (label);
522 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, TRUE, 0);
524 text_box = gtk_hbox_new (FALSE, 0);
525 gtk_widget_show (text_box);
526 gtk_box_pack_start (GTK_BOX (vbox), text_box, FALSE, TRUE, 0);
528 fontsel->preview_entry = gtk_entry_new ();
529 gtk_label_set_mnemonic_widget (GTK_LABEL (label), fontsel->preview_entry);
530 gtk_entry_set_text (GTK_ENTRY (fontsel->preview_entry), _(PREVIEW_TEXT));
532 gtk_widget_show (fontsel->preview_entry);
533 g_signal_connect (fontsel->preview_entry, "changed",
534 G_CALLBACK (gtk_font_selection_preview_changed), fontsel);
535 gtk_widget_set_size_request (fontsel->preview_entry,
536 -1, INITIAL_PREVIEW_HEIGHT);
537 gtk_box_pack_start (GTK_BOX (text_box), fontsel->preview_entry,
539 gtk_widget_pop_composite_child();
543 * gtk_font_selection_new:
545 * Creates a new #GtkFontSelection.
547 * Return value: a n ew #GtkFontSelection
550 gtk_font_selection_new (void)
552 GtkFontSelection *fontsel;
554 fontsel = g_object_new (GTK_TYPE_FONT_SELECTION, NULL);
556 return GTK_WIDGET (fontsel);
560 gtk_font_selection_finalize (GObject *object)
562 GtkFontSelection *fontsel;
564 g_return_if_fail (GTK_IS_FONT_SELECTION (object));
566 fontsel = GTK_FONT_SELECTION (object);
568 gtk_font_selection_ref_family (fontsel, NULL);
569 gtk_font_selection_ref_face (fontsel, NULL);
571 G_OBJECT_CLASS (gtk_font_selection_parent_class)->finalize (object);
575 gtk_font_selection_ref_family (GtkFontSelection *fontsel,
576 PangoFontFamily *family)
579 family = g_object_ref (family);
581 g_object_unref (fontsel->family);
582 fontsel->family = family;
585 static void gtk_font_selection_ref_face (GtkFontSelection *fontsel,
589 face = g_object_ref (face);
591 g_object_unref (fontsel->face);
592 fontsel->face = face;
596 gtk_font_selection_reload_fonts (GtkFontSelection *fontsel)
598 if (gtk_widget_has_screen (GTK_WIDGET (fontsel)))
600 PangoFontDescription *desc;
601 desc = gtk_font_selection_get_font_description (fontsel);
603 gtk_font_selection_show_available_fonts (fontsel);
604 gtk_font_selection_show_available_sizes (fontsel, TRUE);
605 gtk_font_selection_show_available_styles (fontsel);
607 gtk_font_selection_select_font_desc (fontsel, desc, NULL, NULL);
608 gtk_font_selection_scroll_to_selection (fontsel);
610 pango_font_description_free (desc);
615 gtk_font_selection_screen_changed (GtkWidget *widget,
616 GdkScreen *previous_screen)
618 gtk_font_selection_reload_fonts (GTK_FONT_SELECTION (widget));
622 gtk_font_selection_style_set (GtkWidget *widget,
623 GtkStyle *prev_style)
625 /* Maybe fonts where installed or removed... */
626 gtk_font_selection_reload_fonts (GTK_FONT_SELECTION (widget));
630 gtk_font_selection_preview_changed (GtkWidget *entry,
631 GtkFontSelection *fontsel)
633 g_object_notify (G_OBJECT (fontsel), "preview-text");
637 scroll_to_selection (GtkTreeView *tree_view)
639 GtkTreeSelection *selection = gtk_tree_view_get_selection (tree_view);
643 if (gtk_tree_selection_get_selected (selection, &model, &iter))
645 GtkTreePath *path = gtk_tree_model_get_path (model, &iter);
646 gtk_tree_view_scroll_to_cell (tree_view, path, NULL, TRUE, 0.5, 0.5);
647 gtk_tree_path_free (path);
652 set_cursor_to_iter (GtkTreeView *view,
655 GtkTreeModel *model = gtk_tree_view_get_model (view);
656 GtkTreePath *path = gtk_tree_model_get_path (model, iter);
658 gtk_tree_view_set_cursor (view, path, NULL, FALSE);
660 gtk_tree_path_free (path);
664 gtk_font_selection_scroll_to_selection (GtkFontSelection *fontsel)
666 /* Try to scroll the font family list to the selected item */
667 scroll_to_selection (GTK_TREE_VIEW (fontsel->family_list));
669 /* Try to scroll the font family list to the selected item */
670 scroll_to_selection (GTK_TREE_VIEW (fontsel->face_list));
672 /* Try to scroll the font family list to the selected item */
673 scroll_to_selection (GTK_TREE_VIEW (fontsel->size_list));
674 /* This is called when the list is mapped. Here we scroll to the current
675 font if necessary. */
679 gtk_font_selection_scroll_on_map (GtkWidget *widget,
682 gtk_font_selection_scroll_to_selection (GTK_FONT_SELECTION (data));
685 /* This is called when a family is selected in the list. */
687 gtk_font_selection_select_font (GtkTreeSelection *selection,
690 GtkFontSelection *fontsel;
693 #ifdef INCLUDE_FONT_ENTRIES
694 const gchar *family_name;
697 fontsel = GTK_FONT_SELECTION (data);
699 if (gtk_tree_selection_get_selected (selection, &model, &iter))
701 PangoFontFamily *family;
703 gtk_tree_model_get (model, &iter, FAMILY_COLUMN, &family, -1);
704 if (fontsel->family != family)
706 gtk_font_selection_ref_family (fontsel, family);
708 #ifdef INCLUDE_FONT_ENTRIES
709 family_name = pango_font_family_get_name (fontsel->family);
710 gtk_entry_set_text (GTK_ENTRY (fontsel->font_entry), family_name);
713 gtk_font_selection_show_available_styles (fontsel);
714 gtk_font_selection_select_best_style (fontsel, TRUE);
717 g_object_unref (family);
722 cmp_families (const void *a, const void *b)
724 const char *a_name = pango_font_family_get_name (*(PangoFontFamily **)a);
725 const char *b_name = pango_font_family_get_name (*(PangoFontFamily **)b);
727 return g_utf8_collate (a_name, b_name);
731 gtk_font_selection_show_available_fonts (GtkFontSelection *fontsel)
734 PangoFontFamily **families;
735 PangoFontFamily *match_family = NULL;
737 GtkTreeIter match_row;
739 model = GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (fontsel->family_list)));
741 pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (fontsel)),
742 &families, &n_families);
743 qsort (families, n_families, sizeof (PangoFontFamily *), cmp_families);
745 gtk_list_store_clear (model);
747 for (i=0; i<n_families; i++)
749 const gchar *name = pango_font_family_get_name (families[i]);
752 gtk_list_store_append (model, &iter);
753 gtk_list_store_set (model, &iter,
754 FAMILY_COLUMN, families[i],
755 FAMILY_NAME_COLUMN, name,
758 if (i == 0 || !g_ascii_strcasecmp (name, "sans"))
760 match_family = families[i];
765 gtk_font_selection_ref_family (fontsel, match_family);
768 set_cursor_to_iter (GTK_TREE_VIEW (fontsel->family_list), &match_row);
769 #ifdef INCLUDE_FONT_ENTRIES
770 gtk_entry_set_text (GTK_ENTRY (fontsel->font_entry),
771 pango_font_family_get_name (match_family));
772 #endif /* INCLUDE_FONT_ENTRIES */
779 compare_font_descriptions (const PangoFontDescription *a, const PangoFontDescription *b)
781 int val = strcmp (pango_font_description_get_family (a), pango_font_description_get_family (b));
785 if (pango_font_description_get_weight (a) != pango_font_description_get_weight (b))
786 return pango_font_description_get_weight (a) - pango_font_description_get_weight (b);
788 if (pango_font_description_get_style (a) != pango_font_description_get_style (b))
789 return pango_font_description_get_style (a) - pango_font_description_get_style (b);
791 if (pango_font_description_get_stretch (a) != pango_font_description_get_stretch (b))
792 return pango_font_description_get_stretch (a) - pango_font_description_get_stretch (b);
794 if (pango_font_description_get_variant (a) != pango_font_description_get_variant (b))
795 return pango_font_description_get_variant (a) - pango_font_description_get_variant (b);
801 faces_sort_func (const void *a, const void *b)
803 PangoFontDescription *desc_a = pango_font_face_describe (*(PangoFontFace **)a);
804 PangoFontDescription *desc_b = pango_font_face_describe (*(PangoFontFace **)b);
806 int ord = compare_font_descriptions (desc_a, desc_b);
808 pango_font_description_free (desc_a);
809 pango_font_description_free (desc_b);
815 font_description_style_equal (const PangoFontDescription *a,
816 const PangoFontDescription *b)
818 return (pango_font_description_get_weight (a) == pango_font_description_get_weight (b) &&
819 pango_font_description_get_style (a) == pango_font_description_get_style (b) &&
820 pango_font_description_get_stretch (a) == pango_font_description_get_stretch (b) &&
821 pango_font_description_get_variant (a) == pango_font_description_get_variant (b));
824 /* This fills the font style list with all the possible style combinations
825 for the current font family. */
827 gtk_font_selection_show_available_styles (GtkFontSelection *fontsel)
830 PangoFontFace **faces;
831 PangoFontDescription *old_desc;
833 GtkTreeIter match_row;
834 PangoFontFace *match_face = NULL;
836 model = GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (fontsel->face_list)));
839 old_desc = pango_font_face_describe (fontsel->face);
843 pango_font_family_list_faces (fontsel->family, &faces, &n_faces);
844 qsort (faces, n_faces, sizeof (PangoFontFace *), faces_sort_func);
846 gtk_list_store_clear (model);
848 for (i=0; i < n_faces; i++)
851 const gchar *str = pango_font_face_get_face_name (faces[i]);
853 gtk_list_store_append (model, &iter);
854 gtk_list_store_set (model, &iter,
855 FACE_COLUMN, faces[i],
856 FACE_NAME_COLUMN, str,
862 match_face = faces[i];
866 PangoFontDescription *tmp_desc = pango_font_face_describe (faces[i]);
868 if (font_description_style_equal (tmp_desc, old_desc))
871 match_face = faces[i];
874 pango_font_description_free (tmp_desc);
879 pango_font_description_free (old_desc);
881 gtk_font_selection_ref_face (fontsel, match_face);
884 #ifdef INCLUDE_FONT_ENTRIES
885 const gchar *str = pango_font_face_get_face_name (fontsel->face);
887 gtk_entry_set_text (GTK_ENTRY (fontsel->font_style_entry), str);
889 set_cursor_to_iter (GTK_TREE_VIEW (fontsel->face_list), &match_row);
895 /* This selects a style when the user selects a font. It just uses the first
896 available style at present. I was thinking of trying to maintain the
897 selected style, e.g. bold italic, when the user selects different fonts.
898 However, the interface is so easy to use now I'm not sure it's worth it.
899 Note: This will load a font. */
901 gtk_font_selection_select_best_style (GtkFontSelection *fontsel,
907 model = gtk_tree_view_get_model (GTK_TREE_VIEW (fontsel->face_list));
909 if (gtk_tree_model_get_iter_first (model, &iter))
911 set_cursor_to_iter (GTK_TREE_VIEW (fontsel->face_list), &iter);
912 scroll_to_selection (GTK_TREE_VIEW (fontsel->face_list));
915 gtk_font_selection_show_available_sizes (fontsel, FALSE);
916 gtk_font_selection_select_best_size (fontsel);
920 /* This is called when a style is selected in the list. */
922 gtk_font_selection_select_style (GtkTreeSelection *selection,
925 GtkFontSelection *fontsel = GTK_FONT_SELECTION (data);
929 if (gtk_tree_selection_get_selected (selection, &model, &iter))
933 gtk_tree_model_get (model, &iter, FACE_COLUMN, &face, -1);
934 gtk_font_selection_ref_face (fontsel, face);
935 g_object_unref (face);
938 gtk_font_selection_show_available_sizes (fontsel, FALSE);
939 gtk_font_selection_select_best_size (fontsel);
943 gtk_font_selection_show_available_sizes (GtkFontSelection *fontsel,
951 model = GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (fontsel->size_list)));
953 /* Insert the standard font sizes */
956 gtk_list_store_clear (model);
958 for (i = 0; i < G_N_ELEMENTS (font_sizes); i++)
962 gtk_list_store_append (model, &iter);
963 gtk_list_store_set (model, &iter, SIZE_COLUMN, font_sizes[i], -1);
965 if (font_sizes[i] * PANGO_SCALE == fontsel->size)
966 set_cursor_to_iter (GTK_TREE_VIEW (fontsel->size_list), &iter);
972 gboolean found = FALSE;
974 gtk_tree_model_get_iter_first (GTK_TREE_MODEL (model), &iter);
975 for (i = 0; i < G_N_ELEMENTS (font_sizes) && !found; i++)
977 if (font_sizes[i] * PANGO_SCALE == fontsel->size)
979 set_cursor_to_iter (GTK_TREE_VIEW (fontsel->size_list), &iter);
983 gtk_tree_model_iter_next (GTK_TREE_MODEL (model), &iter);
988 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (fontsel->size_list));
989 gtk_tree_selection_unselect_all (selection);
993 /* Set the entry to the new size, rounding to 1 digit,
994 * trimming of trailing 0's and a trailing period
996 g_snprintf (buffer, sizeof (buffer), "%.1f", fontsel->size / (1.0 * PANGO_SCALE));
997 if (strchr (buffer, '.'))
999 p = buffer + strlen (buffer) - 1;
1007 /* Compare, to avoid moving the cursor unecessarily */
1008 if (strcmp (gtk_entry_get_text (GTK_ENTRY (fontsel->size_entry)), buffer) != 0)
1009 gtk_entry_set_text (GTK_ENTRY (fontsel->size_entry), buffer);
1013 gtk_font_selection_select_best_size (GtkFontSelection *fontsel)
1015 gtk_font_selection_load_font (fontsel);
1019 gtk_font_selection_set_size (GtkFontSelection *fontsel,
1022 if (fontsel->size != new_size)
1024 fontsel->size = new_size;
1026 gtk_font_selection_show_available_sizes (fontsel, FALSE);
1027 gtk_font_selection_load_font (fontsel);
1031 /* If the user hits return in the font size entry, we change to the new font
1034 gtk_font_selection_size_activate (GtkWidget *w,
1037 GtkFontSelection *fontsel;
1041 fontsel = GTK_FONT_SELECTION (data);
1043 text = gtk_entry_get_text (GTK_ENTRY (fontsel->size_entry));
1044 new_size = MAX (0.1, atof (text) * PANGO_SCALE + 0.5);
1046 if (fontsel->size != new_size)
1047 gtk_font_selection_set_size (fontsel, new_size);
1049 list_row_activated (w);
1053 gtk_font_selection_size_focus_out (GtkWidget *w,
1054 GdkEventFocus *event,
1057 GtkFontSelection *fontsel;
1061 fontsel = GTK_FONT_SELECTION (data);
1063 text = gtk_entry_get_text (GTK_ENTRY (fontsel->size_entry));
1064 new_size = MAX (0.1, atof (text) * PANGO_SCALE + 0.5);
1066 gtk_font_selection_set_size (fontsel, new_size);
1071 /* This is called when a size is selected in the list. */
1073 gtk_font_selection_select_size (GtkTreeSelection *selection,
1076 GtkFontSelection *fontsel;
1077 GtkTreeModel *model;
1081 fontsel = GTK_FONT_SELECTION (data);
1083 if (gtk_tree_selection_get_selected (selection, &model, &iter))
1085 gtk_tree_model_get (model, &iter, SIZE_COLUMN, &new_size, -1);
1086 gtk_font_selection_set_size (fontsel, new_size * PANGO_SCALE);
1091 gtk_font_selection_load_font (GtkFontSelection *fontsel)
1093 gtk_font_selection_update_preview (fontsel);
1096 static PangoFontDescription *
1097 gtk_font_selection_get_font_description (GtkFontSelection *fontsel)
1099 PangoFontDescription *font_desc;
1103 font_desc = pango_font_face_describe (fontsel->face);
1104 pango_font_description_set_size (font_desc, fontsel->size);
1107 font_desc = pango_font_description_from_string (DEFAULT_FONT_NAME);
1112 /* This sets the font in the preview entry to the selected font, and tries to
1113 make sure that the preview entry is a reasonable size, i.e. so that the
1114 text can be seen with a bit of space to spare. But it tries to avoid
1115 resizing the entry every time the font changes.
1116 This also used to shrink the preview if the font size was decreased, but
1117 that made it awkward if the user wanted to resize the window themself. */
1119 gtk_font_selection_update_preview (GtkFontSelection *fontsel)
1121 GtkRcStyle *rc_style;
1123 GtkRequisition old_requisition;
1124 GtkWidget *preview_entry = fontsel->preview_entry;
1127 gtk_widget_get_child_requisition (preview_entry, &old_requisition);
1129 rc_style = gtk_rc_style_new ();
1130 rc_style->font_desc = gtk_font_selection_get_font_description (fontsel);
1132 gtk_widget_modify_style (preview_entry, rc_style);
1133 g_object_unref (rc_style);
1135 gtk_widget_size_request (preview_entry, NULL);
1137 /* We don't ever want to be over MAX_PREVIEW_HEIGHT pixels high. */
1138 new_height = CLAMP (preview_entry->requisition.height, INITIAL_PREVIEW_HEIGHT, MAX_PREVIEW_HEIGHT);
1140 if (new_height > old_requisition.height || new_height < old_requisition.height - 30)
1141 gtk_widget_set_size_request (preview_entry, -1, new_height);
1143 /* This sets the preview text, if it hasn't been set already. */
1144 text = gtk_entry_get_text (GTK_ENTRY (preview_entry));
1145 if (strlen (text) == 0)
1146 gtk_entry_set_text (GTK_ENTRY (preview_entry), _(PREVIEW_TEXT));
1147 gtk_editable_set_position (GTK_EDITABLE (preview_entry), 0);
1151 /*****************************************************************************
1152 * These functions are the main public interface for getting/setting the font.
1153 *****************************************************************************/
1156 * gtk_font_selection_get_family_list:
1157 * @fontsel: a #GtkFontSelection
1159 * This returns the #GtkTreeView that lists font families, for
1160 * example, 'Sans', 'Serif', etc.
1162 * Return value: A #GtkWidget that is part of @fontsel
1167 gtk_font_selection_get_family_list (GtkFontSelection *fontsel)
1169 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1171 return fontsel->family_list;
1175 * gtk_font_selection_get_face_list:
1176 * @fontsel: a #GtkFontSelection
1178 * This returns the #GtkTreeView which lists all styles available for
1179 * the selected font. For example, 'Regular', 'Bold', etc.
1181 * Return value: A #GtkWidget that is part of @fontsel
1186 gtk_font_selection_get_face_list (GtkFontSelection *fontsel)
1188 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1190 return fontsel->face_list;
1194 * gtk_font_selection_get_size_entry:
1195 * @fontsel: a #GtkFontSelection
1197 * This returns the #GtkEntry used to allow the user to edit the font
1198 * number manually instead of selecting it from the list of font sizes.
1200 * Return value: A #GtkWidget that is part of @fontsel
1205 gtk_font_selection_get_size_entry (GtkFontSelection *fontsel)
1207 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1209 return fontsel->size_entry;
1213 * gtk_font_selection_get_size_list:
1214 * @fontsel: a #GtkFontSelection
1216 * This returns the #GtkTreeeView used to list font sizes.
1218 * Return value: A #GtkWidget that is part of @fontsel
1223 gtk_font_selection_get_size_list (GtkFontSelection *fontsel)
1225 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1227 return fontsel->size_list;
1231 * gtk_font_selection_get_preview_entry:
1232 * @fontsel: a #GtkFontSelection
1234 * This returns the #GtkEntry used to display the font as a preview.
1236 * Return value: A #GtkWidget that is part of @fontsel
1241 gtk_font_selection_get_preview_entry (GtkFontSelection *fontsel)
1243 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1245 return fontsel->preview_entry;
1249 * gtk_font_selection_get_family:
1250 * @fontsel: a #GtkFontSelection
1252 * Gets the #PangoFontFamily representing the selected font family.
1254 * Return value: A #PangoFontFamily representing the selected font
1255 * family. Font families are a collection of font faces. The
1256 * returned object is owned by @fontsel and must not be modified
1262 gtk_font_selection_get_family (GtkFontSelection *fontsel)
1264 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1266 return fontsel->family;
1270 * gtk_font_selection_get_face:
1271 * @fontsel: a #GtkFontSelection
1273 * Gets the #PangoFontFace representing the selected font group
1274 * details (i.e. family, slant, weight, width, etc).
1276 * Return value: A #PangoFontFace representing the selected font
1277 * group details. The returned object is owned by @fontsel and
1278 * must not be modified or freed.
1283 gtk_font_selection_get_face (GtkFontSelection *fontsel)
1285 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1287 return fontsel->face;
1291 * gtk_font_selection_get_size:
1292 * @fontsel: a #GtkFontSelection
1294 * The selected font size.
1296 * Return value: A n integer representing the selected font size,
1297 * or -1 if no font size is selected.
1302 gtk_font_selection_get_size (GtkFontSelection *fontsel)
1304 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), -1);
1306 return fontsel->size;
1310 * gtk_font_selection_get_font_name:
1311 * @fontsel: a #GtkFontSelection
1313 * Gets the currently-selected font name.
1315 * Note that this can be a different string than what you set with
1316 * gtk_font_selection_set_font_name(), as the font selection widget may
1317 * normalize font names and thus return a string with a different structure.
1318 * For example, "Helvetica Italic Bold 12" could be normalized to
1319 * "Helvetica Bold Italic 12". Use pango_font_description_equal()
1320 * if you want to compare two font descriptions.
1322 * Return value: A string with the name of the current font, or %NULL if
1323 * no font is selected. You must free this string with g_free().
1326 gtk_font_selection_get_font_name (GtkFontSelection *fontsel)
1330 PangoFontDescription *font_desc = gtk_font_selection_get_font_description (fontsel);
1331 result = pango_font_description_to_string (font_desc);
1332 pango_font_description_free (font_desc);
1337 /* This selects the appropriate list rows.
1338 First we check the fontname is valid and try to find the font family
1339 - i.e. the name in the main list. If we can't find that, then just return.
1340 Next we try to set each of the properties according to the fontname.
1341 Finally we select the font family & style in the lists. */
1343 gtk_font_selection_select_font_desc (GtkFontSelection *fontsel,
1344 PangoFontDescription *new_desc,
1345 PangoFontFamily **pfamily,
1346 PangoFontFace **pface)
1348 PangoFontFamily *new_family = NULL;
1349 PangoFontFace *new_face = NULL;
1350 PangoFontFace *fallback_face = NULL;
1351 GtkTreeModel *model;
1353 GtkTreeIter match_iter;
1355 const gchar *new_family_name;
1357 new_family_name = pango_font_description_get_family (new_desc);
1359 if (!new_family_name)
1362 /* Check to make sure that this is in the list of allowed fonts
1364 model = gtk_tree_view_get_model (GTK_TREE_VIEW (fontsel->family_list));
1365 for (valid = gtk_tree_model_get_iter_first (model, &iter);
1367 valid = gtk_tree_model_iter_next (model, &iter))
1369 PangoFontFamily *family;
1371 gtk_tree_model_get (model, &iter, FAMILY_COLUMN, &family, -1);
1373 if (g_ascii_strcasecmp (pango_font_family_get_name (family),
1374 new_family_name) == 0)
1375 new_family = g_object_ref (family);
1377 g_object_unref (family);
1387 *pfamily = new_family;
1389 g_object_unref (new_family);
1390 set_cursor_to_iter (GTK_TREE_VIEW (fontsel->family_list), &iter);
1391 gtk_font_selection_show_available_styles (fontsel);
1393 model = gtk_tree_view_get_model (GTK_TREE_VIEW (fontsel->face_list));
1394 for (valid = gtk_tree_model_get_iter_first (model, &iter);
1396 valid = gtk_tree_model_iter_next (model, &iter))
1398 PangoFontFace *face;
1399 PangoFontDescription *tmp_desc;
1401 gtk_tree_model_get (model, &iter, FACE_COLUMN, &face, -1);
1402 tmp_desc = pango_font_face_describe (face);
1404 if (font_description_style_equal (tmp_desc, new_desc))
1405 new_face = g_object_ref (face);
1409 fallback_face = g_object_ref (face);
1413 pango_font_description_free (tmp_desc);
1414 g_object_unref (face);
1424 new_face = fallback_face;
1425 else if (fallback_face)
1426 g_object_unref (fallback_face);
1431 g_object_unref (new_face);
1432 set_cursor_to_iter (GTK_TREE_VIEW (fontsel->face_list), &match_iter);
1434 gtk_font_selection_set_size (fontsel, pango_font_description_get_size (new_desc));
1440 /* This sets the current font, then selecting the appropriate list rows. */
1443 * gtk_font_selection_set_font_name:
1444 * @fontsel: a #GtkFontSelection
1445 * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
1447 * Sets the currently-selected font.
1449 * Note that the @fontsel needs to know the screen in which it will appear
1450 * for this to work; this can be guaranteed by simply making sure that the
1451 * @fontsel is inserted in a toplevel window before you call this function.
1453 * Return value: %TRUE if the font could be set successfully; %FALSE if no
1454 * such font exists or if the @fontsel doesn't belong to a particular
1458 gtk_font_selection_set_font_name (GtkFontSelection *fontsel,
1459 const gchar *fontname)
1461 PangoFontFamily *family = NULL;
1462 PangoFontFace *face = NULL;
1463 PangoFontDescription *new_desc;
1465 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), FALSE);
1467 if (!gtk_widget_has_screen (GTK_WIDGET (fontsel)))
1470 new_desc = pango_font_description_from_string (fontname);
1472 if (gtk_font_selection_select_font_desc (fontsel, new_desc, &family, &face))
1474 gtk_font_selection_ref_family (fontsel, family);
1476 g_object_unref (family);
1478 gtk_font_selection_ref_face (fontsel, face);
1480 g_object_unref (face);
1483 pango_font_description_free (new_desc);
1485 g_object_freeze_notify (G_OBJECT (fontsel));
1486 g_object_notify (G_OBJECT (fontsel), "font-name");
1487 g_object_notify (G_OBJECT (fontsel), "font");
1488 g_object_thaw_notify (G_OBJECT (fontsel));
1494 * gtk_font_selection_get_preview_text:
1495 * @fontsel: a #GtkFontSelection
1497 * Gets the text displayed in the preview area.
1499 * Return value: the text displayed in the preview area.
1500 * This string is owned by the widget and should not be
1503 G_CONST_RETURN gchar*
1504 gtk_font_selection_get_preview_text (GtkFontSelection *fontsel)
1506 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1508 return gtk_entry_get_text (GTK_ENTRY (fontsel->preview_entry));
1513 * gtk_font_selection_set_preview_text:
1514 * @fontsel: a #GtkFontSelection
1515 * @text: the text to display in the preview area
1517 * Sets the text displayed in the preview area.
1518 * The @text is used to show how the selected font looks.
1521 gtk_font_selection_set_preview_text (GtkFontSelection *fontsel,
1524 g_return_if_fail (GTK_IS_FONT_SELECTION (fontsel));
1525 g_return_if_fail (text != NULL);
1527 gtk_entry_set_text (GTK_ENTRY (fontsel->preview_entry), text);
1530 /*****************************************************************************
1531 * GtkFontSelectionDialog
1532 *****************************************************************************/
1534 static void gtk_font_selection_dialog_buildable_interface_init (GtkBuildableIface *iface);
1535 static GObject * gtk_font_selection_dialog_buildable_get_internal_child (GtkBuildable *buildable,
1536 GtkBuilder *builder,
1537 const gchar *childname);
1539 G_DEFINE_TYPE_WITH_CODE (GtkFontSelectionDialog, gtk_font_selection_dialog,
1541 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
1542 gtk_font_selection_dialog_buildable_interface_init))
1544 static GtkBuildableIface *parent_buildable_iface;
1547 gtk_font_selection_dialog_class_init (GtkFontSelectionDialogClass *klass)
1552 gtk_font_selection_dialog_init (GtkFontSelectionDialog *fontseldiag)
1554 GtkDialog *dialog = GTK_DIALOG (fontseldiag);
1555 GtkWidget *action_area, *content_area;
1557 content_area = gtk_dialog_get_content_area (dialog);
1558 action_area = gtk_dialog_get_action_area (dialog);
1560 gtk_dialog_set_has_separator (dialog, FALSE);
1561 gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
1562 gtk_box_set_spacing (GTK_BOX (content_area), 2); /* 2 * 5 + 2 = 12 */
1563 gtk_container_set_border_width (GTK_CONTAINER (action_area), 5);
1564 gtk_box_set_spacing (GTK_BOX (action_area), 6);
1566 gtk_widget_push_composite_child ();
1568 gtk_window_set_resizable (GTK_WINDOW (fontseldiag), TRUE);
1570 fontseldiag->main_vbox = content_area;
1572 fontseldiag->fontsel = gtk_font_selection_new ();
1573 gtk_container_set_border_width (GTK_CONTAINER (fontseldiag->fontsel), 5);
1574 gtk_widget_show (fontseldiag->fontsel);
1575 gtk_box_pack_start (GTK_BOX (fontseldiag->main_vbox),
1576 fontseldiag->fontsel, TRUE, TRUE, 0);
1578 /* Create the action area */
1579 fontseldiag->action_area = action_area;
1581 fontseldiag->cancel_button = gtk_dialog_add_button (dialog,
1583 GTK_RESPONSE_CANCEL);
1585 fontseldiag->apply_button = gtk_dialog_add_button (dialog,
1587 GTK_RESPONSE_APPLY);
1588 gtk_widget_hide (fontseldiag->apply_button);
1590 fontseldiag->ok_button = gtk_dialog_add_button (dialog,
1593 gtk_widget_grab_default (fontseldiag->ok_button);
1595 gtk_dialog_set_alternative_button_order (GTK_DIALOG (fontseldiag),
1598 GTK_RESPONSE_CANCEL,
1601 gtk_window_set_title (GTK_WINDOW (fontseldiag),
1602 _("Font Selection"));
1604 gtk_widget_pop_composite_child ();
1606 _gtk_dialog_set_ignore_separator (dialog, TRUE);
1610 * gtk_font_selection_dialog_new:
1611 * @title: the title of the dialog window
1613 * Creates a new #GtkFontSelectionDialog.
1615 * Return value: a new #GtkFontSelectionDialog
1618 gtk_font_selection_dialog_new (const gchar *title)
1620 GtkFontSelectionDialog *fontseldiag;
1622 fontseldiag = g_object_new (GTK_TYPE_FONT_SELECTION_DIALOG, NULL);
1625 gtk_window_set_title (GTK_WINDOW (fontseldiag), title);
1627 return GTK_WIDGET (fontseldiag);
1631 * gtk_font_selection_dialog_get_font_selection:
1632 * @colorsel: a #GtkFontSelectionDialog
1634 * Retrieves the #GtkFontSelection widget embedded in the dialog.
1636 * Returns: the embedded #GtkFontSelection
1641 gtk_font_selection_dialog_get_font_selection (GtkFontSelectionDialog *fsd)
1643 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1645 return fsd->fontsel;
1650 * gtk_font_selection_dialog_get_ok_button:
1651 * @fsd: a #GtkFontSelectionDialog
1653 * Gets the 'OK' button.
1655 * Return value: the #GtkWidget used in the dialog for the 'OK' button.
1660 gtk_font_selection_dialog_get_ok_button (GtkFontSelectionDialog *fsd)
1662 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1664 return fsd->ok_button;
1668 * gtk_font_selection_dialog_get_cancel_button:
1669 * @fsd: a #GtkFontSelectionDialog
1671 * Gets the 'Cancel' button.
1673 * Return value: the #GtkWidget used in the dialog for the 'Cancel' button.
1678 gtk_font_selection_dialog_get_cancel_button (GtkFontSelectionDialog *fsd)
1680 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1682 return fsd->cancel_button;
1686 gtk_font_selection_dialog_buildable_interface_init (GtkBuildableIface *iface)
1688 parent_buildable_iface = g_type_interface_peek_parent (iface);
1689 iface->get_internal_child = gtk_font_selection_dialog_buildable_get_internal_child;
1693 gtk_font_selection_dialog_buildable_get_internal_child (GtkBuildable *buildable,
1694 GtkBuilder *builder,
1695 const gchar *childname)
1697 if (strcmp(childname, "ok_button") == 0)
1698 return G_OBJECT (GTK_FONT_SELECTION_DIALOG(buildable)->ok_button);
1699 else if (strcmp(childname, "cancel_button") == 0)
1700 return G_OBJECT (GTK_FONT_SELECTION_DIALOG (buildable)->cancel_button);
1701 else if (strcmp(childname, "apply_button") == 0)
1702 return G_OBJECT (GTK_FONT_SELECTION_DIALOG(buildable)->apply_button);
1703 else if (strcmp(childname, "font_selection") == 0)
1704 return G_OBJECT (GTK_FONT_SELECTION_DIALOG(buildable)->fontsel);
1706 return parent_buildable_iface->get_internal_child (buildable, builder, childname);
1710 * gtk_font_selection_dialog_get_font_name:
1711 * @fsd: a #GtkFontSelectionDialog
1713 * Gets the currently-selected font name.
1715 * Note that this can be a different string than what you set with
1716 * gtk_font_selection_dialog_set_font_name(), as the font selection widget
1717 * may normalize font names and thus return a string with a different
1718 * structure. For example, "Helvetica Italic Bold 12" could be normalized
1719 * to "Helvetica Bold Italic 12". Use pango_font_description_equal()
1720 * if you want to compare two font descriptions.
1722 * Return value: A string with the name of the current font, or %NULL if no
1723 * font is selected. You must free this string with g_free().
1726 gtk_font_selection_dialog_get_font_name (GtkFontSelectionDialog *fsd)
1728 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1730 return gtk_font_selection_get_font_name (GTK_FONT_SELECTION (fsd->fontsel));
1734 * gtk_font_selection_dialog_set_font_name:
1735 * @fsd: a #GtkFontSelectionDialog
1736 * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
1738 * Sets the currently selected font.
1740 * Return value: %TRUE if the font selected in @fsd is now the
1741 * @fontname specified, %FALSE otherwise.
1744 gtk_font_selection_dialog_set_font_name (GtkFontSelectionDialog *fsd,
1745 const gchar *fontname)
1747 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), FALSE);
1748 g_return_val_if_fail (fontname, FALSE);
1750 return gtk_font_selection_set_font_name (GTK_FONT_SELECTION (fsd->fontsel), fontname);
1754 * gtk_font_selection_dialog_get_preview_text:
1755 * @fsd: a #GtkFontSelectionDialog
1757 * Gets the text displayed in the preview area.
1759 * Return value: the text displayed in the preview area.
1760 * This string is owned by the widget and should not be
1763 G_CONST_RETURN gchar*
1764 gtk_font_selection_dialog_get_preview_text (GtkFontSelectionDialog *fsd)
1766 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1768 return gtk_font_selection_get_preview_text (GTK_FONT_SELECTION (fsd->fontsel));
1772 * gtk_font_selection_dialog_set_preview_text:
1773 * @fsd: a #GtkFontSelectionDialog
1774 * @text: the text to display in the preview area
1776 * Sets the text displayed in the preview area.
1779 gtk_font_selection_dialog_set_preview_text (GtkFontSelectionDialog *fsd,
1782 g_return_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd));
1783 g_return_if_fail (text != NULL);
1785 gtk_font_selection_set_preview_text (GTK_FONT_SELECTION (fsd->fontsel), text);