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"
65 /* We don't enable the font and style entries because they don't add
66 * much in terms of visible effect and have a weird effect on keynav.
67 * the Windows font selector has entries similarly positioned but they
68 * act in conjunction with the associated lists to form a single focus
71 #undef INCLUDE_FONT_ENTRIES
73 /* This is the default text shown in the preview entry, though the user
74 can set it. Remember that some fonts only have capital letters. */
75 #define PREVIEW_TEXT N_("abcdefghijk ABCDEFGHIJK")
77 #define DEFAULT_FONT_NAME "Sans 10"
79 /* This is the initial and maximum height of the preview entry (it expands
80 when large font sizes are selected). Initial height is also the minimum. */
81 #define INITIAL_PREVIEW_HEIGHT 44
82 #define MAX_PREVIEW_HEIGHT 300
84 /* These are the sizes of the font, style & size lists. */
85 #define FONT_LIST_HEIGHT 136
86 #define FONT_LIST_WIDTH 190
87 #define FONT_STYLE_LIST_WIDTH 170
88 #define FONT_SIZE_LIST_WIDTH 60
90 /* These are what we use as the standard font sizes, for the size list.
92 static const guint16 font_sizes[] = {
93 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, 22, 24, 26, 28,
94 32, 36, 40, 48, 56, 64, 72
119 static void gtk_font_selection_set_property (GObject *object,
123 static void gtk_font_selection_get_property (GObject *object,
127 static void gtk_font_selection_finalize (GObject *object);
128 static void gtk_font_selection_screen_changed (GtkWidget *widget,
129 GdkScreen *previous_screen);
130 static void gtk_font_selection_style_set (GtkWidget *widget,
131 GtkStyle *prev_style);
133 /* These are the callbacks & related functions. */
134 static void gtk_font_selection_select_font (GtkTreeSelection *selection,
136 static void gtk_font_selection_show_available_fonts (GtkFontSelection *fs);
138 static void gtk_font_selection_show_available_styles (GtkFontSelection *fs);
139 static void gtk_font_selection_select_best_style (GtkFontSelection *fs,
141 static void gtk_font_selection_select_style (GtkTreeSelection *selection,
144 static void gtk_font_selection_select_best_size (GtkFontSelection *fs);
145 static void gtk_font_selection_show_available_sizes (GtkFontSelection *fs,
146 gboolean first_time);
147 static void gtk_font_selection_size_activate (GtkWidget *w,
149 static gboolean gtk_font_selection_size_focus_out (GtkWidget *w,
150 GdkEventFocus *event,
152 static void gtk_font_selection_select_size (GtkTreeSelection *selection,
155 static void gtk_font_selection_scroll_on_map (GtkWidget *w,
158 static void gtk_font_selection_preview_changed (GtkWidget *entry,
159 GtkFontSelection *fontsel);
160 static void gtk_font_selection_scroll_to_selection (GtkFontSelection *fontsel);
163 /* Misc. utility functions. */
164 static void gtk_font_selection_load_font (GtkFontSelection *fs);
165 static void gtk_font_selection_update_preview (GtkFontSelection *fs);
167 static GdkFont* gtk_font_selection_get_font_internal (GtkFontSelection *fontsel);
168 static PangoFontDescription *gtk_font_selection_get_font_description (GtkFontSelection *fontsel);
169 static gboolean gtk_font_selection_select_font_desc (GtkFontSelection *fontsel,
170 PangoFontDescription *new_desc,
171 PangoFontFamily **pfamily,
172 PangoFontFace **pface);
173 static void gtk_font_selection_reload_fonts (GtkFontSelection *fontsel);
174 static void gtk_font_selection_ref_family (GtkFontSelection *fontsel,
175 PangoFontFamily *family);
176 static void gtk_font_selection_ref_face (GtkFontSelection *fontsel,
177 PangoFontFace *face);
179 G_DEFINE_TYPE (GtkFontSelection, gtk_font_selection, GTK_TYPE_VBOX)
182 gtk_font_selection_class_init (GtkFontSelectionClass *klass)
184 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
185 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
187 gobject_class->set_property = gtk_font_selection_set_property;
188 gobject_class->get_property = gtk_font_selection_get_property;
190 widget_class->screen_changed = gtk_font_selection_screen_changed;
191 widget_class->style_set = gtk_font_selection_style_set;
193 g_object_class_install_property (gobject_class,
195 g_param_spec_string ("font-name",
197 P_("The string that represents this font"),
199 GTK_PARAM_READWRITE));
200 g_object_class_install_property (gobject_class,
202 g_param_spec_boxed ("font",
204 P_("The GdkFont that is currently selected"),
206 GTK_PARAM_READABLE));
207 g_object_class_install_property (gobject_class,
209 g_param_spec_string ("preview-text",
211 P_("The text to display in order to demonstrate the selected font"),
213 GTK_PARAM_READWRITE));
214 gobject_class->finalize = gtk_font_selection_finalize;
218 gtk_font_selection_set_property (GObject *object,
223 GtkFontSelection *fontsel;
225 fontsel = GTK_FONT_SELECTION (object);
230 gtk_font_selection_set_font_name (fontsel, g_value_get_string (value));
232 case PROP_PREVIEW_TEXT:
233 gtk_font_selection_set_preview_text (fontsel, g_value_get_string (value));
236 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
241 static void gtk_font_selection_get_property (GObject *object,
246 GtkFontSelection *fontsel;
248 fontsel = GTK_FONT_SELECTION (object);
253 g_value_take_string (value, gtk_font_selection_get_font_name (fontsel));
256 g_value_set_boxed (value, gtk_font_selection_get_font_internal (fontsel));
258 case PROP_PREVIEW_TEXT:
259 g_value_set_string (value, gtk_font_selection_get_preview_text (fontsel));
262 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
267 /* Handles key press events on the lists, so that we can trap Enter to
268 * activate the default button on our own.
271 list_row_activated (GtkWidget *widget)
275 window = GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (widget)));
276 if (!GTK_WIDGET_TOPLEVEL (window))
280 && widget != window->default_widget
281 && !(widget == window->focus_widget &&
282 (!window->default_widget || !GTK_WIDGET_SENSITIVE (window->default_widget))))
284 gtk_window_activate_default (window);
291 gtk_font_selection_init (GtkFontSelection *fontsel)
293 GtkWidget *scrolled_win;
295 GtkWidget *table, *label;
296 GtkWidget *font_label, *style_label;
299 GtkTreeViewColumn *column;
300 GList *focus_chain = NULL;
303 gtk_widget_push_composite_child ();
305 gtk_box_set_spacing (GTK_BOX (fontsel), 12);
306 fontsel->size = 12 * PANGO_SCALE;
308 /* Create the table of font, style & size. */
309 table = gtk_table_new (3, 3, FALSE);
310 gtk_widget_show (table);
311 gtk_table_set_row_spacings (GTK_TABLE (table), 6);
312 gtk_table_set_col_spacings (GTK_TABLE (table), 12);
313 gtk_box_pack_start (GTK_BOX (fontsel), table, TRUE, TRUE, 0);
315 #ifdef INCLUDE_FONT_ENTRIES
316 fontsel->font_entry = gtk_entry_new ();
317 gtk_editable_set_editable (GTK_EDITABLE (fontsel->font_entry), FALSE);
318 gtk_widget_set_size_request (fontsel->font_entry, 20, -1);
319 gtk_widget_show (fontsel->font_entry);
320 gtk_table_attach (GTK_TABLE (table), fontsel->font_entry, 0, 1, 1, 2,
323 fontsel->font_style_entry = gtk_entry_new ();
324 gtk_editable_set_editable (GTK_EDITABLE (fontsel->font_style_entry), FALSE);
325 gtk_widget_set_size_request (fontsel->font_style_entry, 20, -1);
326 gtk_widget_show (fontsel->font_style_entry);
327 gtk_table_attach (GTK_TABLE (table), fontsel->font_style_entry, 1, 2, 1, 2,
329 #endif /* INCLUDE_FONT_ENTRIES */
331 fontsel->size_entry = gtk_entry_new ();
332 gtk_widget_set_size_request (fontsel->size_entry, 20, -1);
333 gtk_widget_show (fontsel->size_entry);
334 gtk_table_attach (GTK_TABLE (table), fontsel->size_entry, 2, 3, 1, 2,
336 g_signal_connect (fontsel->size_entry, "activate",
337 G_CALLBACK (gtk_font_selection_size_activate),
339 g_signal_connect_after (fontsel->size_entry, "focus-out-event",
340 G_CALLBACK (gtk_font_selection_size_focus_out),
343 font_label = gtk_label_new_with_mnemonic (_("_Family:"));
344 gtk_misc_set_alignment (GTK_MISC (font_label), 0.0, 0.5);
345 gtk_widget_show (font_label);
346 gtk_table_attach (GTK_TABLE (table), font_label, 0, 1, 0, 1,
349 style_label = gtk_label_new_with_mnemonic (_("_Style:"));
350 gtk_misc_set_alignment (GTK_MISC (style_label), 0.0, 0.5);
351 gtk_widget_show (style_label);
352 gtk_table_attach (GTK_TABLE (table), style_label, 1, 2, 0, 1,
355 label = gtk_label_new_with_mnemonic (_("Si_ze:"));
356 gtk_label_set_mnemonic_widget (GTK_LABEL (label),
357 fontsel->size_entry);
358 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
359 gtk_widget_show (label);
360 gtk_table_attach (GTK_TABLE (table), label, 2, 3, 0, 1,
364 /* Create the lists */
366 model = gtk_list_store_new (2,
367 G_TYPE_OBJECT, /* FAMILY_COLUMN */
368 G_TYPE_STRING); /* FAMILY_NAME_COLUMN */
369 fontsel->family_list = gtk_tree_view_new_with_model (GTK_TREE_MODEL (model));
370 g_object_unref (model);
372 g_signal_connect (fontsel->family_list, "row-activated",
373 G_CALLBACK (list_row_activated), fontsel);
375 column = gtk_tree_view_column_new_with_attributes ("Family",
376 gtk_cell_renderer_text_new (),
377 "text", FAMILY_NAME_COLUMN,
379 gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
380 gtk_tree_view_append_column (GTK_TREE_VIEW (fontsel->family_list), column);
382 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (fontsel->family_list), FALSE);
383 gtk_tree_selection_set_mode (gtk_tree_view_get_selection (GTK_TREE_VIEW (fontsel->family_list)),
384 GTK_SELECTION_BROWSE);
386 gtk_label_set_mnemonic_widget (GTK_LABEL (font_label), fontsel->family_list);
388 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
389 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win), GTK_SHADOW_IN);
390 gtk_widget_set_size_request (scrolled_win,
391 FONT_LIST_WIDTH, FONT_LIST_HEIGHT);
392 gtk_container_add (GTK_CONTAINER (scrolled_win), fontsel->family_list);
393 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
394 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
395 gtk_widget_show (fontsel->family_list);
396 gtk_widget_show (scrolled_win);
398 gtk_table_attach (GTK_TABLE (table), scrolled_win, 0, 1, 1, 3,
399 GTK_EXPAND | GTK_FILL,
400 GTK_EXPAND | GTK_FILL, 0, 0);
401 focus_chain = g_list_append (focus_chain, scrolled_win);
403 model = gtk_list_store_new (2,
404 G_TYPE_OBJECT, /* FACE_COLUMN */
405 G_TYPE_STRING); /* FACE_NAME_COLUMN */
406 fontsel->face_list = gtk_tree_view_new_with_model (GTK_TREE_MODEL (model));
407 g_object_unref (model);
408 g_signal_connect (fontsel->face_list, "row-activated",
409 G_CALLBACK (list_row_activated), fontsel);
411 gtk_label_set_mnemonic_widget (GTK_LABEL (style_label), fontsel->face_list);
413 column = gtk_tree_view_column_new_with_attributes ("Face",
414 gtk_cell_renderer_text_new (),
415 "text", FACE_NAME_COLUMN,
417 gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
418 gtk_tree_view_append_column (GTK_TREE_VIEW (fontsel->face_list), column);
420 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (fontsel->face_list), FALSE);
421 gtk_tree_selection_set_mode (gtk_tree_view_get_selection (GTK_TREE_VIEW (fontsel->face_list)),
422 GTK_SELECTION_BROWSE);
424 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
425 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win), GTK_SHADOW_IN);
426 gtk_widget_set_size_request (scrolled_win,
427 FONT_STYLE_LIST_WIDTH, FONT_LIST_HEIGHT);
428 gtk_container_add (GTK_CONTAINER (scrolled_win), fontsel->face_list);
429 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
430 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
431 gtk_widget_show (fontsel->face_list);
432 gtk_widget_show (scrolled_win);
433 gtk_table_attach (GTK_TABLE (table), scrolled_win, 1, 2, 1, 3,
434 GTK_EXPAND | GTK_FILL,
435 GTK_EXPAND | GTK_FILL, 0, 0);
436 focus_chain = g_list_append (focus_chain, scrolled_win);
438 focus_chain = g_list_append (focus_chain, fontsel->size_entry);
440 model = gtk_list_store_new (1, G_TYPE_INT);
441 fontsel->size_list = gtk_tree_view_new_with_model (GTK_TREE_MODEL (model));
442 g_object_unref (model);
443 g_signal_connect (fontsel->size_list, "row-activated",
444 G_CALLBACK (list_row_activated), fontsel);
446 column = gtk_tree_view_column_new_with_attributes ("Size",
447 gtk_cell_renderer_text_new (),
450 gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
451 gtk_tree_view_append_column (GTK_TREE_VIEW (fontsel->size_list), column);
453 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (fontsel->size_list), FALSE);
454 gtk_tree_selection_set_mode (gtk_tree_view_get_selection (GTK_TREE_VIEW (fontsel->size_list)),
455 GTK_SELECTION_BROWSE);
457 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
458 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win), GTK_SHADOW_IN);
459 gtk_container_add (GTK_CONTAINER (scrolled_win), fontsel->size_list);
460 gtk_widget_set_size_request (scrolled_win, -1, FONT_LIST_HEIGHT);
461 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
462 GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
463 gtk_widget_show (fontsel->size_list);
464 gtk_widget_show (scrolled_win);
465 gtk_table_attach (GTK_TABLE (table), scrolled_win, 2, 3, 2, 3,
466 GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
467 focus_chain = g_list_append (focus_chain, scrolled_win);
469 gtk_container_set_focus_chain (GTK_CONTAINER (table), focus_chain);
470 g_list_free (focus_chain);
472 /* Insert the fonts. */
473 g_signal_connect (gtk_tree_view_get_selection (GTK_TREE_VIEW (fontsel->family_list)), "changed",
474 G_CALLBACK (gtk_font_selection_select_font), fontsel);
476 g_signal_connect_after (fontsel->family_list, "map",
477 G_CALLBACK (gtk_font_selection_scroll_on_map),
480 g_signal_connect (gtk_tree_view_get_selection (GTK_TREE_VIEW (fontsel->face_list)), "changed",
481 G_CALLBACK (gtk_font_selection_select_style), fontsel);
483 g_signal_connect (gtk_tree_view_get_selection (GTK_TREE_VIEW (fontsel->size_list)), "changed",
484 G_CALLBACK (gtk_font_selection_select_size), fontsel);
485 atk_obj = gtk_widget_get_accessible (fontsel->size_list);
486 if (GTK_IS_ACCESSIBLE (atk_obj))
488 /* Accessibility support is enabled.
489 * Make the label ATK_RELATON_LABEL_FOR for the size list as well.
491 AtkObject *atk_label;
492 AtkRelationSet *relation_set;
493 AtkRelation *relation;
494 AtkObject *obj_array[1];
496 atk_label = gtk_widget_get_accessible (label);
497 relation_set = atk_object_ref_relation_set (atk_obj);
498 relation = atk_relation_set_get_relation_by_type (relation_set, ATK_RELATION_LABELLED_BY);
501 atk_relation_add_target (relation, atk_label);
505 obj_array[0] = atk_label;
506 relation = atk_relation_new (obj_array, 1, ATK_RELATION_LABELLED_BY);
507 atk_relation_set_add (relation_set, relation);
509 g_object_unref (relation_set);
511 relation_set = atk_object_ref_relation_set (atk_label);
512 relation = atk_relation_set_get_relation_by_type (relation_set, ATK_RELATION_LABEL_FOR);
515 atk_relation_add_target (relation, atk_obj);
519 obj_array[0] = atk_obj;
520 relation = atk_relation_new (obj_array, 1, ATK_RELATION_LABEL_FOR);
521 atk_relation_set_add (relation_set, relation);
523 g_object_unref (relation_set);
527 vbox = gtk_vbox_new (FALSE, 6);
528 gtk_widget_show (vbox);
529 gtk_box_pack_start (GTK_BOX (fontsel), vbox, FALSE, TRUE, 0);
531 /* create the text entry widget */
532 label = gtk_label_new_with_mnemonic (_("_Preview:"));
533 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
534 gtk_widget_show (label);
535 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, TRUE, 0);
537 text_box = gtk_hbox_new (FALSE, 0);
538 gtk_widget_show (text_box);
539 gtk_box_pack_start (GTK_BOX (vbox), text_box, FALSE, TRUE, 0);
541 fontsel->preview_entry = gtk_entry_new ();
542 gtk_label_set_mnemonic_widget (GTK_LABEL (label), fontsel->preview_entry);
543 gtk_entry_set_text (GTK_ENTRY (fontsel->preview_entry), _(PREVIEW_TEXT));
545 gtk_widget_show (fontsel->preview_entry);
546 g_signal_connect (fontsel->preview_entry, "changed",
547 G_CALLBACK (gtk_font_selection_preview_changed), fontsel);
548 gtk_widget_set_size_request (fontsel->preview_entry,
549 -1, INITIAL_PREVIEW_HEIGHT);
550 gtk_box_pack_start (GTK_BOX (text_box), fontsel->preview_entry,
552 gtk_widget_pop_composite_child();
556 * gtk_font_selection_new:
558 * Creates a new #GtkFontSelection.
560 * Return value: a n ew #GtkFontSelection
563 gtk_font_selection_new (void)
565 GtkFontSelection *fontsel;
567 fontsel = g_object_new (GTK_TYPE_FONT_SELECTION, NULL);
569 return GTK_WIDGET (fontsel);
573 gtk_font_selection_finalize (GObject *object)
575 GtkFontSelection *fontsel;
577 g_return_if_fail (GTK_IS_FONT_SELECTION (object));
579 fontsel = GTK_FONT_SELECTION (object);
582 gdk_font_unref (fontsel->font);
584 gtk_font_selection_ref_family (fontsel, NULL);
585 gtk_font_selection_ref_face (fontsel, NULL);
587 G_OBJECT_CLASS (gtk_font_selection_parent_class)->finalize (object);
591 gtk_font_selection_ref_family (GtkFontSelection *fontsel,
592 PangoFontFamily *family)
595 family = g_object_ref (family);
597 g_object_unref (fontsel->family);
598 fontsel->family = family;
601 static void gtk_font_selection_ref_face (GtkFontSelection *fontsel,
605 face = g_object_ref (face);
607 g_object_unref (fontsel->face);
608 fontsel->face = face;
612 gtk_font_selection_reload_fonts (GtkFontSelection *fontsel)
614 if (gtk_widget_has_screen (GTK_WIDGET (fontsel)))
616 PangoFontDescription *desc;
617 desc = gtk_font_selection_get_font_description (fontsel);
619 gtk_font_selection_show_available_fonts (fontsel);
620 gtk_font_selection_show_available_sizes (fontsel, TRUE);
621 gtk_font_selection_show_available_styles (fontsel);
623 gtk_font_selection_select_font_desc (fontsel, desc, NULL, NULL);
624 gtk_font_selection_scroll_to_selection (fontsel);
626 pango_font_description_free (desc);
631 gtk_font_selection_screen_changed (GtkWidget *widget,
632 GdkScreen *previous_screen)
634 gtk_font_selection_reload_fonts (GTK_FONT_SELECTION (widget));
638 gtk_font_selection_style_set (GtkWidget *widget,
639 GtkStyle *prev_style)
641 /* Maybe fonts where installed or removed... */
642 gtk_font_selection_reload_fonts (GTK_FONT_SELECTION (widget));
646 gtk_font_selection_preview_changed (GtkWidget *entry,
647 GtkFontSelection *fontsel)
649 g_object_notify (G_OBJECT (fontsel), "preview-text");
653 scroll_to_selection (GtkTreeView *tree_view)
655 GtkTreeSelection *selection = gtk_tree_view_get_selection (tree_view);
659 if (gtk_tree_selection_get_selected (selection, &model, &iter))
661 GtkTreePath *path = gtk_tree_model_get_path (model, &iter);
662 gtk_tree_view_scroll_to_cell (tree_view, path, NULL, TRUE, 0.5, 0.5);
663 gtk_tree_path_free (path);
668 set_cursor_to_iter (GtkTreeView *view,
671 GtkTreeModel *model = gtk_tree_view_get_model (view);
672 GtkTreePath *path = gtk_tree_model_get_path (model, iter);
674 gtk_tree_view_set_cursor (view, path, NULL, FALSE);
676 gtk_tree_path_free (path);
680 gtk_font_selection_scroll_to_selection (GtkFontSelection *fontsel)
682 /* Try to scroll the font family list to the selected item */
683 scroll_to_selection (GTK_TREE_VIEW (fontsel->family_list));
685 /* Try to scroll the font family list to the selected item */
686 scroll_to_selection (GTK_TREE_VIEW (fontsel->face_list));
688 /* Try to scroll the font family list to the selected item */
689 scroll_to_selection (GTK_TREE_VIEW (fontsel->size_list));
690 /* This is called when the list is mapped. Here we scroll to the current
691 font if necessary. */
695 gtk_font_selection_scroll_on_map (GtkWidget *widget,
698 gtk_font_selection_scroll_to_selection (GTK_FONT_SELECTION (data));
701 /* This is called when a family is selected in the list. */
703 gtk_font_selection_select_font (GtkTreeSelection *selection,
706 GtkFontSelection *fontsel;
709 #ifdef INCLUDE_FONT_ENTRIES
710 const gchar *family_name;
713 fontsel = GTK_FONT_SELECTION (data);
715 if (gtk_tree_selection_get_selected (selection, &model, &iter))
717 PangoFontFamily *family;
719 gtk_tree_model_get (model, &iter, FAMILY_COLUMN, &family, -1);
720 if (fontsel->family != family)
722 gtk_font_selection_ref_family (fontsel, family);
724 #ifdef INCLUDE_FONT_ENTRIES
725 family_name = pango_font_family_get_name (fontsel->family);
726 gtk_entry_set_text (GTK_ENTRY (fontsel->font_entry), family_name);
729 gtk_font_selection_show_available_styles (fontsel);
730 gtk_font_selection_select_best_style (fontsel, TRUE);
733 g_object_unref (family);
738 cmp_families (const void *a, const void *b)
740 const char *a_name = pango_font_family_get_name (*(PangoFontFamily **)a);
741 const char *b_name = pango_font_family_get_name (*(PangoFontFamily **)b);
743 return g_utf8_collate (a_name, b_name);
747 gtk_font_selection_show_available_fonts (GtkFontSelection *fontsel)
750 PangoFontFamily **families;
751 PangoFontFamily *match_family = NULL;
753 GtkTreeIter match_row;
755 model = GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (fontsel->family_list)));
757 pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (fontsel)),
758 &families, &n_families);
759 qsort (families, n_families, sizeof (PangoFontFamily *), cmp_families);
761 gtk_list_store_clear (model);
763 for (i=0; i<n_families; i++)
765 const gchar *name = pango_font_family_get_name (families[i]);
768 gtk_list_store_append (model, &iter);
769 gtk_list_store_set (model, &iter,
770 FAMILY_COLUMN, families[i],
771 FAMILY_NAME_COLUMN, name,
774 if (i == 0 || !g_ascii_strcasecmp (name, "sans"))
776 match_family = families[i];
781 gtk_font_selection_ref_family (fontsel, match_family);
784 set_cursor_to_iter (GTK_TREE_VIEW (fontsel->family_list), &match_row);
785 #ifdef INCLUDE_FONT_ENTRIES
786 gtk_entry_set_text (GTK_ENTRY (fontsel->font_entry),
787 pango_font_family_get_name (match_family));
788 #endif /* INCLUDE_FONT_ENTRIES */
795 compare_font_descriptions (const PangoFontDescription *a, const PangoFontDescription *b)
797 int val = strcmp (pango_font_description_get_family (a), pango_font_description_get_family (b));
801 if (pango_font_description_get_weight (a) != pango_font_description_get_weight (b))
802 return pango_font_description_get_weight (a) - pango_font_description_get_weight (b);
804 if (pango_font_description_get_style (a) != pango_font_description_get_style (b))
805 return pango_font_description_get_style (a) - pango_font_description_get_style (b);
807 if (pango_font_description_get_stretch (a) != pango_font_description_get_stretch (b))
808 return pango_font_description_get_stretch (a) - pango_font_description_get_stretch (b);
810 if (pango_font_description_get_variant (a) != pango_font_description_get_variant (b))
811 return pango_font_description_get_variant (a) - pango_font_description_get_variant (b);
817 faces_sort_func (const void *a, const void *b)
819 PangoFontDescription *desc_a = pango_font_face_describe (*(PangoFontFace **)a);
820 PangoFontDescription *desc_b = pango_font_face_describe (*(PangoFontFace **)b);
822 int ord = compare_font_descriptions (desc_a, desc_b);
824 pango_font_description_free (desc_a);
825 pango_font_description_free (desc_b);
831 font_description_style_equal (const PangoFontDescription *a,
832 const PangoFontDescription *b)
834 return (pango_font_description_get_weight (a) == pango_font_description_get_weight (b) &&
835 pango_font_description_get_style (a) == pango_font_description_get_style (b) &&
836 pango_font_description_get_stretch (a) == pango_font_description_get_stretch (b) &&
837 pango_font_description_get_variant (a) == pango_font_description_get_variant (b));
840 /* This fills the font style list with all the possible style combinations
841 for the current font family. */
843 gtk_font_selection_show_available_styles (GtkFontSelection *fontsel)
846 PangoFontFace **faces;
847 PangoFontDescription *old_desc;
849 GtkTreeIter match_row;
850 PangoFontFace *match_face = NULL;
852 model = GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (fontsel->face_list)));
855 old_desc = pango_font_face_describe (fontsel->face);
859 pango_font_family_list_faces (fontsel->family, &faces, &n_faces);
860 qsort (faces, n_faces, sizeof (PangoFontFace *), faces_sort_func);
862 gtk_list_store_clear (model);
864 for (i=0; i < n_faces; i++)
867 const gchar *str = pango_font_face_get_face_name (faces[i]);
869 gtk_list_store_append (model, &iter);
870 gtk_list_store_set (model, &iter,
871 FACE_COLUMN, faces[i],
872 FACE_NAME_COLUMN, str,
878 match_face = faces[i];
882 PangoFontDescription *tmp_desc = pango_font_face_describe (faces[i]);
884 if (font_description_style_equal (tmp_desc, old_desc))
887 match_face = faces[i];
890 pango_font_description_free (tmp_desc);
895 pango_font_description_free (old_desc);
897 gtk_font_selection_ref_face (fontsel, match_face);
900 #ifdef INCLUDE_FONT_ENTRIES
901 const gchar *str = pango_font_face_get_face_name (fontsel->face);
903 gtk_entry_set_text (GTK_ENTRY (fontsel->font_style_entry), str);
905 set_cursor_to_iter (GTK_TREE_VIEW (fontsel->face_list), &match_row);
911 /* This selects a style when the user selects a font. It just uses the first
912 available style at present. I was thinking of trying to maintain the
913 selected style, e.g. bold italic, when the user selects different fonts.
914 However, the interface is so easy to use now I'm not sure it's worth it.
915 Note: This will load a font. */
917 gtk_font_selection_select_best_style (GtkFontSelection *fontsel,
923 model = gtk_tree_view_get_model (GTK_TREE_VIEW (fontsel->face_list));
925 if (gtk_tree_model_get_iter_first (model, &iter))
927 set_cursor_to_iter (GTK_TREE_VIEW (fontsel->face_list), &iter);
928 scroll_to_selection (GTK_TREE_VIEW (fontsel->face_list));
931 gtk_font_selection_show_available_sizes (fontsel, FALSE);
932 gtk_font_selection_select_best_size (fontsel);
936 /* This is called when a style is selected in the list. */
938 gtk_font_selection_select_style (GtkTreeSelection *selection,
941 GtkFontSelection *fontsel = GTK_FONT_SELECTION (data);
945 if (gtk_tree_selection_get_selected (selection, &model, &iter))
949 gtk_tree_model_get (model, &iter, FACE_COLUMN, &face, -1);
950 gtk_font_selection_ref_face (fontsel, face);
951 g_object_unref (face);
954 gtk_font_selection_show_available_sizes (fontsel, FALSE);
955 gtk_font_selection_select_best_size (fontsel);
959 gtk_font_selection_show_available_sizes (GtkFontSelection *fontsel,
967 model = GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (fontsel->size_list)));
969 /* Insert the standard font sizes */
972 gtk_list_store_clear (model);
974 for (i = 0; i < G_N_ELEMENTS (font_sizes); i++)
978 gtk_list_store_append (model, &iter);
979 gtk_list_store_set (model, &iter, SIZE_COLUMN, font_sizes[i], -1);
981 if (font_sizes[i] * PANGO_SCALE == fontsel->size)
982 set_cursor_to_iter (GTK_TREE_VIEW (fontsel->size_list), &iter);
988 gboolean found = FALSE;
990 gtk_tree_model_get_iter_first (GTK_TREE_MODEL (model), &iter);
991 for (i = 0; i < G_N_ELEMENTS (font_sizes) && !found; i++)
993 if (font_sizes[i] * PANGO_SCALE == fontsel->size)
995 set_cursor_to_iter (GTK_TREE_VIEW (fontsel->size_list), &iter);
999 gtk_tree_model_iter_next (GTK_TREE_MODEL (model), &iter);
1004 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (fontsel->size_list));
1005 gtk_tree_selection_unselect_all (selection);
1009 /* Set the entry to the new size, rounding to 1 digit,
1010 * trimming of trailing 0's and a trailing period
1012 g_snprintf (buffer, sizeof (buffer), "%.1f", fontsel->size / (1.0 * PANGO_SCALE));
1013 if (strchr (buffer, '.'))
1015 p = buffer + strlen (buffer) - 1;
1023 /* Compare, to avoid moving the cursor unecessarily */
1024 if (strcmp (gtk_entry_get_text (GTK_ENTRY (fontsel->size_entry)), buffer) != 0)
1025 gtk_entry_set_text (GTK_ENTRY (fontsel->size_entry), buffer);
1029 gtk_font_selection_select_best_size (GtkFontSelection *fontsel)
1031 gtk_font_selection_load_font (fontsel);
1035 gtk_font_selection_set_size (GtkFontSelection *fontsel,
1038 if (fontsel->size != new_size)
1040 fontsel->size = new_size;
1042 gtk_font_selection_show_available_sizes (fontsel, FALSE);
1043 gtk_font_selection_load_font (fontsel);
1047 /* If the user hits return in the font size entry, we change to the new font
1050 gtk_font_selection_size_activate (GtkWidget *w,
1053 GtkFontSelection *fontsel;
1057 fontsel = GTK_FONT_SELECTION (data);
1059 text = gtk_entry_get_text (GTK_ENTRY (fontsel->size_entry));
1060 new_size = MAX (0.1, atof (text) * PANGO_SCALE + 0.5);
1062 if (fontsel->size != new_size)
1063 gtk_font_selection_set_size (fontsel, new_size);
1065 list_row_activated (w);
1069 gtk_font_selection_size_focus_out (GtkWidget *w,
1070 GdkEventFocus *event,
1073 GtkFontSelection *fontsel;
1077 fontsel = GTK_FONT_SELECTION (data);
1079 text = gtk_entry_get_text (GTK_ENTRY (fontsel->size_entry));
1080 new_size = MAX (0.1, atof (text) * PANGO_SCALE + 0.5);
1082 gtk_font_selection_set_size (fontsel, new_size);
1087 /* This is called when a size is selected in the list. */
1089 gtk_font_selection_select_size (GtkTreeSelection *selection,
1092 GtkFontSelection *fontsel;
1093 GtkTreeModel *model;
1097 fontsel = GTK_FONT_SELECTION (data);
1099 if (gtk_tree_selection_get_selected (selection, &model, &iter))
1101 gtk_tree_model_get (model, &iter, SIZE_COLUMN, &new_size, -1);
1102 gtk_font_selection_set_size (fontsel, new_size * PANGO_SCALE);
1107 gtk_font_selection_load_font (GtkFontSelection *fontsel)
1110 gdk_font_unref (fontsel->font);
1111 fontsel->font = NULL;
1113 gtk_font_selection_update_preview (fontsel);
1116 static PangoFontDescription *
1117 gtk_font_selection_get_font_description (GtkFontSelection *fontsel)
1119 PangoFontDescription *font_desc;
1123 font_desc = pango_font_face_describe (fontsel->face);
1124 pango_font_description_set_size (font_desc, fontsel->size);
1127 font_desc = pango_font_description_from_string (DEFAULT_FONT_NAME);
1132 /* This sets the font in the preview entry to the selected font, and tries to
1133 make sure that the preview entry is a reasonable size, i.e. so that the
1134 text can be seen with a bit of space to spare. But it tries to avoid
1135 resizing the entry every time the font changes.
1136 This also used to shrink the preview if the font size was decreased, but
1137 that made it awkward if the user wanted to resize the window themself. */
1139 gtk_font_selection_update_preview (GtkFontSelection *fontsel)
1141 GtkRcStyle *rc_style;
1143 GtkRequisition old_requisition;
1144 GtkWidget *preview_entry = fontsel->preview_entry;
1147 gtk_widget_get_child_requisition (preview_entry, &old_requisition);
1149 rc_style = gtk_rc_style_new ();
1150 rc_style->font_desc = gtk_font_selection_get_font_description (fontsel);
1152 gtk_widget_modify_style (preview_entry, rc_style);
1153 g_object_unref (rc_style);
1155 gtk_widget_size_request (preview_entry, NULL);
1157 /* We don't ever want to be over MAX_PREVIEW_HEIGHT pixels high. */
1158 new_height = CLAMP (preview_entry->requisition.height, INITIAL_PREVIEW_HEIGHT, MAX_PREVIEW_HEIGHT);
1160 if (new_height > old_requisition.height || new_height < old_requisition.height - 30)
1161 gtk_widget_set_size_request (preview_entry, -1, new_height);
1163 /* This sets the preview text, if it hasn't been set already. */
1164 text = gtk_entry_get_text (GTK_ENTRY (preview_entry));
1165 if (strlen (text) == 0)
1166 gtk_entry_set_text (GTK_ENTRY (preview_entry), _(PREVIEW_TEXT));
1167 gtk_editable_set_position (GTK_EDITABLE (preview_entry), 0);
1171 gtk_font_selection_get_font_internal (GtkFontSelection *fontsel)
1175 PangoFontDescription *font_desc = gtk_font_selection_get_font_description (fontsel);
1176 fontsel->font = gdk_font_from_description_for_display (gtk_widget_get_display (GTK_WIDGET (fontsel)), font_desc);
1177 pango_font_description_free (font_desc);
1180 return fontsel->font;
1184 /*****************************************************************************
1185 * These functions are the main public interface for getting/setting the font.
1186 *****************************************************************************/
1189 * gtk_font_selection_get_family_list:
1190 * @fontsel: a #GtkFontSelection
1192 * This returns the #GtkTreeView that lists font families, for
1193 * example, 'Sans', 'Serif', etc.
1195 * Return value: A #GtkWidget that is part of @fontsel
1200 gtk_font_selection_get_family_list (GtkFontSelection *fontsel)
1202 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1204 return fontsel->family_list;
1208 * gtk_font_selection_get_face_list:
1209 * @fontsel: a #GtkFontSelection
1211 * This returns the #GtkTreeView which lists all styles available for
1212 * the selected font. For example, 'Regular', 'Bold', etc.
1214 * Return value: A #GtkWidget that is part of @fontsel
1219 gtk_font_selection_get_face_list (GtkFontSelection *fontsel)
1221 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1223 return fontsel->face_list;
1227 * gtk_font_selection_get_size_entry:
1228 * @fontsel: a #GtkFontSelection
1230 * This returns the #GtkEntry used to allow the user to edit the font
1231 * number manually instead of selecting it from the list of font sizes.
1233 * Return value: A #GtkWidget that is part of @fontsel
1238 gtk_font_selection_get_size_entry (GtkFontSelection *fontsel)
1240 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1242 return fontsel->size_entry;
1246 * gtk_font_selection_get_size_list:
1247 * @fontsel: a #GtkFontSelection
1249 * This returns the #GtkTreeeView used to list font sizes.
1251 * Return value: A #GtkWidget that is part of @fontsel
1256 gtk_font_selection_get_size_list (GtkFontSelection *fontsel)
1258 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1260 return fontsel->size_list;
1264 * gtk_font_selection_get_preview_entry:
1265 * @fontsel: a #GtkFontSelection
1267 * This returns the #GtkEntry used to display the font as a preview.
1269 * Return value: A #GtkWidget that is part of @fontsel
1274 gtk_font_selection_get_preview_entry (GtkFontSelection *fontsel)
1276 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1278 return fontsel->preview_entry;
1282 * gtk_font_selection_get_family:
1283 * @fontsel: a #GtkFontSelection
1285 * Gets the #PangoFontFamily representing the selected font family.
1287 * Return value: A #PangoFontFamily representing the selected font
1288 * family. Font families are a collection of font faces. The
1289 * returned object is owned by @fontsel and must not be modified
1295 gtk_font_selection_get_family (GtkFontSelection *fontsel)
1297 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1299 return fontsel->family;
1303 * gtk_font_selection_get_face:
1304 * @fontsel: a #GtkFontSelection
1306 * Gets the #PangoFontFace representing the selected font group
1307 * details (i.e. family, slant, weight, width, etc).
1309 * Return value: A #PangoFontFace representing the selected font
1310 * group details. The returned object is owned by @fontsel and
1311 * must not be modified or freed.
1316 gtk_font_selection_get_face (GtkFontSelection *fontsel)
1318 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1320 return fontsel->face;
1324 * gtk_font_selection_get_size:
1325 * @fontsel: a #GtkFontSelection
1327 * The selected font size.
1329 * Return value: A n integer representing the selected font size,
1330 * or -1 if no font size is selected.
1335 gtk_font_selection_get_size (GtkFontSelection *fontsel)
1337 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), -1);
1339 return fontsel->size;
1343 * gtk_font_selection_get_font:
1344 * @fontsel: a #GtkFontSelection
1346 * Gets the currently-selected font.
1348 * Return value: A #GdkFont.
1350 * Deprecated: 2.0: Use gtk_font_selection_get_font_name() instead.
1353 gtk_font_selection_get_font (GtkFontSelection *fontsel)
1355 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1357 return gtk_font_selection_get_font_internal (fontsel);
1361 * gtk_font_selection_get_font_name:
1362 * @fontsel: a #GtkFontSelection
1364 * Gets the currently-selected font name.
1366 * Note that this can be a different string than what you set with
1367 * gtk_font_selection_set_font_name(), as the font selection widget may
1368 * normalize font names and thus return a string with a different structure.
1369 * For example, "Helvetica Italic Bold 12" could be normalized to
1370 * "Helvetica Bold Italic 12". Use pango_font_description_equal()
1371 * if you want to compare two font descriptions.
1373 * Return value: A string with the name of the current font, or %NULL if
1374 * no font is selected. You must free this string with g_free().
1377 gtk_font_selection_get_font_name (GtkFontSelection *fontsel)
1381 PangoFontDescription *font_desc = gtk_font_selection_get_font_description (fontsel);
1382 result = pango_font_description_to_string (font_desc);
1383 pango_font_description_free (font_desc);
1388 /* This selects the appropriate list rows.
1389 First we check the fontname is valid and try to find the font family
1390 - i.e. the name in the main list. If we can't find that, then just return.
1391 Next we try to set each of the properties according to the fontname.
1392 Finally we select the font family & style in the lists. */
1394 gtk_font_selection_select_font_desc (GtkFontSelection *fontsel,
1395 PangoFontDescription *new_desc,
1396 PangoFontFamily **pfamily,
1397 PangoFontFace **pface)
1399 PangoFontFamily *new_family = NULL;
1400 PangoFontFace *new_face = NULL;
1401 PangoFontFace *fallback_face = NULL;
1402 GtkTreeModel *model;
1404 GtkTreeIter match_iter;
1406 const gchar *new_family_name;
1408 new_family_name = pango_font_description_get_family (new_desc);
1410 if (!new_family_name)
1413 /* Check to make sure that this is in the list of allowed fonts
1415 model = gtk_tree_view_get_model (GTK_TREE_VIEW (fontsel->family_list));
1416 for (valid = gtk_tree_model_get_iter_first (model, &iter);
1418 valid = gtk_tree_model_iter_next (model, &iter))
1420 PangoFontFamily *family;
1422 gtk_tree_model_get (model, &iter, FAMILY_COLUMN, &family, -1);
1424 if (g_ascii_strcasecmp (pango_font_family_get_name (family),
1425 new_family_name) == 0)
1426 new_family = g_object_ref (family);
1428 g_object_unref (family);
1438 *pfamily = new_family;
1440 g_object_unref (new_family);
1441 set_cursor_to_iter (GTK_TREE_VIEW (fontsel->family_list), &iter);
1442 gtk_font_selection_show_available_styles (fontsel);
1444 model = gtk_tree_view_get_model (GTK_TREE_VIEW (fontsel->face_list));
1445 for (valid = gtk_tree_model_get_iter_first (model, &iter);
1447 valid = gtk_tree_model_iter_next (model, &iter))
1449 PangoFontFace *face;
1450 PangoFontDescription *tmp_desc;
1452 gtk_tree_model_get (model, &iter, FACE_COLUMN, &face, -1);
1453 tmp_desc = pango_font_face_describe (face);
1455 if (font_description_style_equal (tmp_desc, new_desc))
1456 new_face = g_object_ref (face);
1460 fallback_face = g_object_ref (face);
1464 pango_font_description_free (tmp_desc);
1465 g_object_unref (face);
1475 new_face = fallback_face;
1476 else if (fallback_face)
1477 g_object_unref (fallback_face);
1482 g_object_unref (new_face);
1483 set_cursor_to_iter (GTK_TREE_VIEW (fontsel->face_list), &match_iter);
1485 gtk_font_selection_set_size (fontsel, pango_font_description_get_size (new_desc));
1491 /* This sets the current font, then selecting the appropriate list rows. */
1494 * gtk_font_selection_set_font_name:
1495 * @fontsel: a #GtkFontSelection
1496 * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
1498 * Sets the currently-selected font.
1500 * Note that the @fontsel needs to know the screen in which it will appear
1501 * for this to work; this can be guaranteed by simply making sure that the
1502 * @fontsel is inserted in a toplevel window before you call this function.
1504 * Return value: %TRUE if the font could be set successfully; %FALSE if no
1505 * such font exists or if the @fontsel doesn't belong to a particular
1509 gtk_font_selection_set_font_name (GtkFontSelection *fontsel,
1510 const gchar *fontname)
1512 PangoFontFamily *family = NULL;
1513 PangoFontFace *face = NULL;
1514 PangoFontDescription *new_desc;
1516 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), FALSE);
1518 if (!gtk_widget_has_screen (GTK_WIDGET (fontsel)))
1521 new_desc = pango_font_description_from_string (fontname);
1523 if (gtk_font_selection_select_font_desc (fontsel, new_desc, &family, &face))
1525 gtk_font_selection_ref_family (fontsel, family);
1527 g_object_unref (family);
1529 gtk_font_selection_ref_face (fontsel, face);
1531 g_object_unref (face);
1534 pango_font_description_free (new_desc);
1536 g_object_freeze_notify (G_OBJECT (fontsel));
1537 g_object_notify (G_OBJECT (fontsel), "font-name");
1538 g_object_notify (G_OBJECT (fontsel), "font");
1539 g_object_thaw_notify (G_OBJECT (fontsel));
1545 * gtk_font_selection_get_preview_text:
1546 * @fontsel: a #GtkFontSelection
1548 * Gets the text displayed in the preview area.
1550 * Return value: the text displayed in the preview area.
1551 * This string is owned by the widget and should not be
1554 G_CONST_RETURN gchar*
1555 gtk_font_selection_get_preview_text (GtkFontSelection *fontsel)
1557 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1559 return gtk_entry_get_text (GTK_ENTRY (fontsel->preview_entry));
1564 * gtk_font_selection_set_preview_text:
1565 * @fontsel: a #GtkFontSelection
1566 * @text: the text to display in the preview area
1568 * Sets the text displayed in the preview area.
1569 * The @text is used to show how the selected font looks.
1572 gtk_font_selection_set_preview_text (GtkFontSelection *fontsel,
1575 g_return_if_fail (GTK_IS_FONT_SELECTION (fontsel));
1576 g_return_if_fail (text != NULL);
1578 gtk_entry_set_text (GTK_ENTRY (fontsel->preview_entry), text);
1581 /*****************************************************************************
1582 * GtkFontSelectionDialog
1583 *****************************************************************************/
1585 static void gtk_font_selection_dialog_buildable_interface_init (GtkBuildableIface *iface);
1586 static GObject * gtk_font_selection_dialog_buildable_get_internal_child (GtkBuildable *buildable,
1587 GtkBuilder *builder,
1588 const gchar *childname);
1590 G_DEFINE_TYPE_WITH_CODE (GtkFontSelectionDialog, gtk_font_selection_dialog,
1592 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
1593 gtk_font_selection_dialog_buildable_interface_init))
1595 static GtkBuildableIface *parent_buildable_iface;
1598 gtk_font_selection_dialog_class_init (GtkFontSelectionDialogClass *klass)
1603 gtk_font_selection_dialog_init (GtkFontSelectionDialog *fontseldiag)
1605 GtkDialog *dialog = GTK_DIALOG (fontseldiag);
1607 gtk_dialog_set_has_separator (dialog, FALSE);
1608 gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
1609 gtk_box_set_spacing (GTK_BOX (dialog->vbox), 2); /* 2 * 5 + 2 = 12 */
1610 gtk_container_set_border_width (GTK_CONTAINER (dialog->action_area), 5);
1611 gtk_box_set_spacing (GTK_BOX (dialog->action_area), 6);
1613 gtk_widget_push_composite_child ();
1615 gtk_window_set_resizable (GTK_WINDOW (fontseldiag), TRUE);
1617 fontseldiag->main_vbox = dialog->vbox;
1619 fontseldiag->fontsel = gtk_font_selection_new ();
1620 gtk_container_set_border_width (GTK_CONTAINER (fontseldiag->fontsel), 5);
1621 gtk_widget_show (fontseldiag->fontsel);
1622 gtk_box_pack_start (GTK_BOX (fontseldiag->main_vbox),
1623 fontseldiag->fontsel, TRUE, TRUE, 0);
1625 /* Create the action area */
1626 fontseldiag->action_area = dialog->action_area;
1628 fontseldiag->cancel_button = gtk_dialog_add_button (dialog,
1630 GTK_RESPONSE_CANCEL);
1632 fontseldiag->apply_button = gtk_dialog_add_button (dialog,
1634 GTK_RESPONSE_APPLY);
1635 gtk_widget_hide (fontseldiag->apply_button);
1637 fontseldiag->ok_button = gtk_dialog_add_button (dialog,
1640 gtk_widget_grab_default (fontseldiag->ok_button);
1642 gtk_dialog_set_alternative_button_order (GTK_DIALOG (fontseldiag),
1645 GTK_RESPONSE_CANCEL,
1648 gtk_window_set_title (GTK_WINDOW (fontseldiag),
1649 _("Font Selection"));
1651 gtk_widget_pop_composite_child ();
1653 _gtk_dialog_set_ignore_separator (dialog, TRUE);
1657 * gtk_font_selection_dialog_new:
1658 * @title: the title of the dialog window
1660 * Creates a new #GtkFontSelectionDialog.
1662 * Return value: a new #GtkFontSelectionDialog
1665 gtk_font_selection_dialog_new (const gchar *title)
1667 GtkFontSelectionDialog *fontseldiag;
1669 fontseldiag = g_object_new (GTK_TYPE_FONT_SELECTION_DIALOG, NULL);
1672 gtk_window_set_title (GTK_WINDOW (fontseldiag), title);
1674 return GTK_WIDGET (fontseldiag);
1678 * gtk_font_selection_dialog_get_ok_button:
1679 * @fsd: a #GtkFontSelectionDialog
1681 * Gets the 'OK' button.
1683 * Return value: the #GtkWidget used in the dialog for the 'OK' button.
1688 gtk_font_selection_dialog_get_ok_button (GtkFontSelectionDialog *fsd)
1690 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1692 return fsd->ok_button;
1696 * gtk_font_selection_dialog_get_apply_button:
1697 * @fsd: a #GtkFontSelectionDialog
1699 * Obtains a button. The button doesn't have any function.
1701 * Return value: a #GtkWidget
1705 * Deprecated: 2.16: Don't use this function.
1708 gtk_font_selection_dialog_get_apply_button (GtkFontSelectionDialog *fsd)
1710 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1712 return fsd->apply_button;
1716 * gtk_font_selection_dialog_get_cancel_button:
1717 * @fsd: a #GtkFontSelectionDialog
1719 * Gets the 'Cancel' button.
1721 * Return value: the #GtkWidget used in the dialog for the 'Cancel' button.
1726 gtk_font_selection_dialog_get_cancel_button (GtkFontSelectionDialog *fsd)
1728 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1730 return fsd->cancel_button;
1734 gtk_font_selection_dialog_buildable_interface_init (GtkBuildableIface *iface)
1736 parent_buildable_iface = g_type_interface_peek_parent (iface);
1737 iface->get_internal_child = gtk_font_selection_dialog_buildable_get_internal_child;
1741 gtk_font_selection_dialog_buildable_get_internal_child (GtkBuildable *buildable,
1742 GtkBuilder *builder,
1743 const gchar *childname)
1745 if (strcmp(childname, "ok_button") == 0)
1746 return G_OBJECT (GTK_FONT_SELECTION_DIALOG(buildable)->ok_button);
1747 else if (strcmp(childname, "cancel_button") == 0)
1748 return G_OBJECT (GTK_FONT_SELECTION_DIALOG (buildable)->cancel_button);
1749 else if (strcmp(childname, "apply_button") == 0)
1750 return G_OBJECT (GTK_FONT_SELECTION_DIALOG(buildable)->apply_button);
1751 else if (strcmp(childname, "font_selection") == 0)
1752 return G_OBJECT (GTK_FONT_SELECTION_DIALOG(buildable)->fontsel);
1754 return parent_buildable_iface->get_internal_child (buildable, builder, childname);
1758 * gtk_font_selection_dialog_get_font_name:
1759 * @fsd: a #GtkFontSelectionDialog
1761 * Gets the currently-selected font name.
1763 * Note that this can be a different string than what you set with
1764 * gtk_font_selection_dialog_set_font_name(), as the font selection widget
1765 * may normalize font names and thus return a string with a different
1766 * structure. For example, "Helvetica Italic Bold 12" could be normalized
1767 * to "Helvetica Bold Italic 12". Use pango_font_description_equal()
1768 * if you want to compare two font descriptions.
1770 * Return value: A string with the name of the current font, or %NULL if no
1771 * font is selected. You must free this string with g_free().
1774 gtk_font_selection_dialog_get_font_name (GtkFontSelectionDialog *fsd)
1776 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1778 return gtk_font_selection_get_font_name (GTK_FONT_SELECTION (fsd->fontsel));
1782 * gtk_font_selection_dialog_get_font:
1783 * @fsd: a #GtkFontSelectionDialog
1785 * Gets the currently-selected font.
1787 * Return value: the #GdkFont from the #GtkFontSelection for the
1788 * currently selected font in the dialog, or %NULL if no font is selected
1790 * Deprecated: 2.0: Use gtk_font_selection_dialog_get_font_name() instead.
1793 gtk_font_selection_dialog_get_font (GtkFontSelectionDialog *fsd)
1795 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1797 return gtk_font_selection_get_font_internal (GTK_FONT_SELECTION (fsd->fontsel));
1801 * gtk_font_selection_dialog_set_font_name:
1802 * @fsd: a #GtkFontSelectionDialog
1803 * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
1805 * Sets the currently selected font.
1807 * Return value: %TRUE if the font selected in @fsd is now the
1808 * @fontname specified, %FALSE otherwise.
1811 gtk_font_selection_dialog_set_font_name (GtkFontSelectionDialog *fsd,
1812 const gchar *fontname)
1814 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), FALSE);
1815 g_return_val_if_fail (fontname, FALSE);
1817 return gtk_font_selection_set_font_name (GTK_FONT_SELECTION (fsd->fontsel), fontname);
1821 * gtk_font_selection_dialog_get_preview_text:
1822 * @fsd: a #GtkFontSelectionDialog
1824 * Gets the text displayed in the preview area.
1826 * Return value: the text displayed in the preview area.
1827 * This string is owned by the widget and should not be
1830 G_CONST_RETURN gchar*
1831 gtk_font_selection_dialog_get_preview_text (GtkFontSelectionDialog *fsd)
1833 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1835 return gtk_font_selection_get_preview_text (GTK_FONT_SELECTION (fsd->fontsel));
1839 * gtk_font_selection_dialog_set_preview_text:
1840 * @fsd: a #GtkFontSelectionDialog
1841 * @text: the text to display in the preview area
1843 * Sets the text displayed in the preview area.
1846 gtk_font_selection_dialog_set_preview_text (GtkFontSelectionDialog *fsd,
1849 g_return_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd));
1850 g_return_if_fail (text != NULL);
1852 gtk_font_selection_set_preview_text (GTK_FONT_SELECTION (fsd->fontsel), text);
1855 #define __GTK_FONTSEL_C__
1856 #include "gtkaliasdef.c"