- * gtk_font_chooser_new:
- *
- * Creates a new #GtkFontChooser.
- *
- * Return value: a new #GtkFontChooser
- *
- * Since: 3.2
- */
-GtkWidget *
-gtk_font_chooser_new (void)
-{
- GtkFontChooser *fontchooser;
-
- fontchooser = g_object_new (GTK_TYPE_FONT_CHOOSER, NULL);
-
- return GTK_WIDGET (fontchooser);
-}
-
-static int
-cmp_families (const void *a, const void *b)
-{
- const char *a_name = pango_font_family_get_name (*(PangoFontFamily **)a);
- const char *b_name = pango_font_family_get_name (*(PangoFontFamily **)b);
-
- return g_utf8_collate (a_name, b_name);
-}
-
-static void
-populate_list (GtkFontChooser *fontchooser, GtkTreeView* treeview, GtkListStore* model)
-{
- GtkStyleContext *style_context;
- GdkRGBA g_color;
- PangoColor p_color;
- gchar *color_string;
- PangoFontDescription *default_font;
-
- GtkTreeIter match_row;
- GtkTreePath *path;
-
- gint n_families, i;
- PangoFontFamily **families;
-
- GString *tmp = g_string_new (NULL);
- GString *family_and_face = g_string_new (NULL);
-
- pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (treeview)),
- &families,
- &n_families);
-
- qsort (families, n_families, sizeof (PangoFontFamily *), cmp_families);
-
- gtk_list_store_clear (model);
-
- /* Get row header font color */
- style_context = gtk_widget_get_style_context (GTK_WIDGET (treeview));
- gtk_style_context_get_color (style_context,
- GTK_STATE_FLAG_NORMAL | GTK_STATE_FLAG_INSENSITIVE,
- &g_color);
-
- p_color.red = (guint16)((gdouble)G_MAXUINT16 * g_color.red);
- p_color.green = (guint16)((gdouble)G_MAXUINT16 * g_color.green);
- p_color.blue = (guint16)((gdouble)G_MAXUINT16 * g_color.blue);
- color_string = pango_color_to_string (&p_color);
-
- /* Get theme font */
- default_font = (PangoFontDescription*) gtk_style_context_get_font (style_context,
- GTK_STATE_NORMAL);
-
- /* Iterate over families and faces */
- for (i=0; i<n_families; i++)
- {
- GtkTreeIter iter;
- PangoFontFace **faces;
-
- int j, n_faces;
- const gchar *fam_name = pango_font_family_get_name (families[i]);
-
- pango_font_family_list_faces (families[i], &faces, &n_faces);
-
- for (j=0; j<n_faces; j++)
- {
- PangoFontDescription *pango_desc = pango_font_face_describe (faces[j]);
- const gchar *face_name = pango_font_face_get_face_name (faces[j]);
- gchar *font_desc = pango_font_description_to_string (pango_desc);
-
- /* foreground_color, family_name, face_name, desc, sample string */
- g_string_printf (family_and_face, "%s %s",
- fam_name,
- face_name);
-
- g_string_printf (tmp, ROW_FORMAT_STRING,
- color_string,
- family_and_face->str,
- font_desc,
- fontchooser->priv->preview_text);
-
- gtk_list_store_append (model, &iter);
- gtk_list_store_set (model, &iter,
- FAMILY_COLUMN, families[i],
- FACE_COLUMN, faces[j],
- PREVIEW_TITLE_COLUMN, family_and_face->str,
- PREVIEW_TEXT_COLUMN, tmp->str,
- -1);
-
- /* Select the first font or the default font/face from the style context */
- if ((i == 0 && j == 0) ||
- (!strcmp (fam_name, pango_font_description_get_family (default_font)) && j == 0))
- match_row = iter;
-
- pango_font_description_free(pango_desc);
- g_free (font_desc);
- }
-
- g_free (faces);
- }
-
- path = gtk_tree_model_get_path (GTK_TREE_MODEL (model), &match_row);
-
- if (path)
- {
- gtk_tree_view_set_cursor (treeview, path, NULL, FALSE);
- gtk_tree_view_scroll_to_cell (treeview, path, NULL, FALSE, 0.5, 0.5);
- gtk_tree_path_free(path);
- }
-
- g_string_free (family_and_face, TRUE);
- g_string_free (tmp, TRUE);
- g_free (color_string);
- g_free (families);
-}
-
-gboolean
-visible_func (GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
-{
- gboolean result = TRUE;
- GtkFontChooserPrivate *priv = (GtkFontChooserPrivate*) data;
-
- const gchar *search_text = (const gchar*)gtk_entry_get_text (GTK_ENTRY (priv->search_entry));
- gchar *font_name;
- gchar *term;
- gchar **split_terms;
- gint n_terms = 0;
-
- /* If there's no filter string we show the item */
- if (strlen (search_text) == 0)
- return TRUE;
-
- gtk_tree_model_get (model, iter,
- PREVIEW_TITLE_COLUMN, &font_name,
- -1);
-
- if (font_name == NULL)
- return FALSE;
-
- split_terms = g_strsplit (search_text, " ", 0);
- term = split_terms[0];
-
- while (term && result)
- {
- gchar* font_name_casefold = g_utf8_casefold (font_name, -1);
- gchar* term_casefold = g_utf8_casefold (term, -1);
-
- if (g_strrstr (font_name_casefold, term_casefold))
- result = result && TRUE;
- else
- result = FALSE;
-
- n_terms++;
- term = split_terms[n_terms];
-
- g_free (term_casefold);
- g_free (font_name_casefold);
- }
-
- g_free (font_name);
- g_strfreev (split_terms);
-
- return result;
-}
-
-static void
-gtk_font_chooser_bootstrap_fontlist (GtkFontChooser* fontchooser)
-{
- GtkTreeView *treeview = GTK_TREE_VIEW (fontchooser->priv->family_face_list);
- GtkCellRenderer *cell;
- GtkTreeViewColumn *col;
-
- fontchooser->priv->model = gtk_list_store_new (4,
- PANGO_TYPE_FONT_FAMILY,
- PANGO_TYPE_FONT_FACE,
- G_TYPE_STRING,
- G_TYPE_STRING);
-
- fontchooser->priv->filter = gtk_tree_model_filter_new (GTK_TREE_MODEL (fontchooser->priv->model),
- NULL);
- g_object_unref (fontchooser->priv->model);
-
- gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (fontchooser->priv->filter),
- visible_func,
- (gpointer)fontchooser->priv,
- NULL);
-
- gtk_tree_view_set_model (treeview, GTK_TREE_MODEL (fontchooser->priv->filter));
- g_object_unref (fontchooser->priv->filter);
-
- gtk_tree_view_set_rules_hint (treeview, TRUE);
- gtk_tree_view_set_headers_visible (treeview, FALSE);
-
- cell = gtk_cell_renderer_text_new ();
- col = gtk_tree_view_column_new_with_attributes ("Family",
- cell,
- "markup", PREVIEW_TEXT_COLUMN,
- NULL);
-
- g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
-
- gtk_tree_view_append_column (treeview, col);
-
- populate_list (fontchooser, treeview, fontchooser->priv->model);
-}
-
-
-static void
-gtk_font_chooser_finalize (GObject *object)
-{
- GtkFontChooser *fontchooser = GTK_FONT_CHOOSER (object);
-
- gtk_font_chooser_ref_family (fontchooser, NULL);
- gtk_font_chooser_ref_face (fontchooser, NULL);
-
- G_OBJECT_CLASS (gtk_font_chooser_parent_class)->finalize (object);
-}
-
-
-static void
-gtk_font_chooser_screen_changed (GtkWidget *widget,
- GdkScreen *previous_screen)
-{
- GtkFontChooser *fontchooser = GTK_FONT_CHOOSER (widget);
-
- populate_list (fontchooser,
- GTK_TREE_VIEW (fontchooser->priv->family_face_list),
- fontchooser->priv->model);
- return;
-}
-
-static void
-gtk_font_chooser_style_updated (GtkWidget *widget)
-{
- GtkFontChooser *fontchooser = GTK_FONT_CHOOSER (widget);
-
- GTK_WIDGET_CLASS (gtk_font_chooser_parent_class)->style_updated (widget);
-
- populate_list (fontchooser,
- GTK_TREE_VIEW (fontchooser->priv->family_face_list),
- fontchooser->priv->model);
- return;
-}
-
-static void
-gtk_font_chooser_ref_family (GtkFontChooser *fontchooser,
- PangoFontFamily *family)
-{
- GtkFontChooserPrivate *priv = fontchooser->priv;
-
- if (family)
- family = g_object_ref (family);
- if (priv->family)
- g_object_unref (priv->family);
- priv->family = family;
-}
-
-static void
-gtk_font_chooser_ref_face (GtkFontChooser *fontchooser,
- PangoFontFace *face)
-{
- GtkFontChooserPrivate *priv = fontchooser->priv;
-
- if (face)
- face = g_object_ref (face);
- if (priv->face)
- g_object_unref (priv->face);
- priv->face = face;
-}
-
-
-/*****************************************************************************
- * These functions are the main public interface for getting/setting the font.
- *****************************************************************************/
-
-/**
- * gtk_font_chooser_get_family: