- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
- */
-
-#include "config.h"
-
-#include <stdlib.h>
-#include <glib/gprintf.h>
-#include <string.h>
-
-#include <atk/atk.h>
-
-#include "gtkfontchooser.h"
-#include "gtkbutton.h"
-#include "gtkcellrenderertext.h"
-#include "gtkentry.h"
-#include "gtkframe.h"
-#include "gtkhbbox.h"
-#include "gtkhbox.h"
-#include "gtklabel.h"
-#include "gtkliststore.h"
-#include "gtkrc.h"
-#include "gtkstock.h"
-#include "gtktable.h"
-#include "gtktreeselection.h"
-#include "gtktreeview.h"
-#include "gtkvbox.h"
-#include "gtkscrolledwindow.h"
-#include "gtkintl.h"
-#include "gtkaccessible.h"
-#include "gtkbuildable.h"
-#include "gtkprivate.h"
-#include "gtkalignment.h"
-#include "gtkscale.h"
-#include "gtkbox.h"
-#include "gtkspinbutton.h"
-#include "gtkwidget.h"
-
-/**
- * SECTION:gtkfontchooser
- * @Short_description: A widget for selecting fonts
- * @Title: GtkFontChooser
- * @See_also: #GtkFontChooserDialog
- *
- * The #GtkFontChooser widget lists the available fonts, styles and sizes,
- * allowing the user to select a font.
- * It is used in the #GtkFontChooserDialog widget to provide a dialog box for
- * selecting fonts.
- *
- * To set the font which is initially selected, use
- * gtk_font_chooser_set_font_name().
- *
- * To get the selected font use gtk_font_chooser_get_font_name().
- *
- * To change the text which is shown in the preview area, use
- * gtk_font_chooser_set_preview_text().
- */
-
-
-struct _GtkFontChooserPrivate
-{
- GtkWidget *search_entry;
- GtkWidget *family_face_list;
- GtkListStore *model;
- GtkTreeModel *filter;
-
- GtkWidget *preview;
- GtkWidget *preview_scrolled_window;
- gchar *preview_text;
- gboolean show_preview_entry;
-
- GtkWidget *size_spin;
- GtkWidget *size_slider;
- gboolean ignore_slider;
-
- gint size;
- PangoFontFace *face;
- PangoFontFamily *family;
-
- /* Deprecated */
- GtkWidget *size_list;
- GtkWidget *font_list;
- GtkWidget *face_list;
-
- GtkListStore *_size_model;
- GtkListStore *_font_model;
- GtkListStore *_face_model;
-
- gboolean ignore_size;
- gboolean ignore_face;
- gboolean ignore_font;
-};
-
-
-struct _GtkFontChooserDialogPrivate
-{
- GtkWidget *fontchooser;
-
- GtkWidget *select_button;
- GtkWidget *cancel_button;
-
- /* Deprecated */
- GtkWidget *apply_button;
-};
-
-
-#define DEFAULT_FONT_NAME "Sans 10"
-#define MAX_FONT_SIZE 999
-
-/* This is the initial fixed height and the top padding of the preview entry */
-#define PREVIEW_HEIGHT 72
-#define PREVIEW_TOP_PADDING 6
-
-/* Widget default geometry */
-#define FONT_CHOOSER_WIDTH 540
-#define FONT_CHOOSER_HEIGHT 408
-
-/* These are the sizes of the font, style & size lists. */
-#define FONT_LIST_HEIGHT 136
-#define FONT_LIST_WIDTH 190
-#define FONT_STYLE_LIST_WIDTH 170
-#define FONT_SIZE_LIST_WIDTH 60
-
-#define ROW_FORMAT_STRING "<span weight=\"bold\" size=\"small\" foreground=\"%s\">%s</span>\n<span size=\"x-large\" font_desc=\"%s\">%s</span>"
-
-/* These are what we use as the standard font sizes, for the size list.
- */
-#define FONT_SIZES_LENGTH 14
-static const gint font_sizes[] = {
- 6, 8, 9, 10, 11, 12, 13, 14, 16, 20, 24, 36, 48, 72
-};
-
-enum {
- PROP_0,
- PROP_FONT_NAME,
- PROP_PREVIEW_TEXT,
- PROP_SHOW_PREVIEW_ENTRY
-};
-
-
-enum {
- FAMILY_COLUMN,
- FACE_COLUMN,
- PREVIEW_TEXT_COLUMN,
- PREVIEW_TITLE_COLUMN
-};
-
-static void gtk_font_chooser_set_property (GObject *object,
- guint prop_id,
- const GValue *value,
- GParamSpec *pspec);
-static void gtk_font_chooser_get_property (GObject *object,
- guint prop_id,
- GValue *value,
- GParamSpec *pspec);
-static void gtk_font_chooser_finalize (GObject *object);
-
-static void gtk_font_chooser_screen_changed (GtkWidget *widget,
- GdkScreen *previous_screen);
-static void gtk_font_chooser_style_updated (GtkWidget *widget);
-
-static void gtk_font_chooser_ref_family (GtkFontChooser *fontchooser,
- PangoFontFamily *family);
-static void gtk_font_chooser_ref_face (GtkFontChooser *fontchooser,
- PangoFontFace *face);
-
-static void gtk_font_chooser_bootstrap_fontlist (GtkFontChooser *fontchooser);
-
-/* Deprecated */
-static void update_font_list_selection (GtkFontChooser *fontchooser);
-static void update_size_list_selection (GtkFontChooser *fontchooser);
-static void update_face_model (GtkFontChooser *fontchooser,
- gboolean first);
-
-G_DEFINE_TYPE (GtkFontChooser, gtk_font_chooser, GTK_TYPE_VBOX)
-
-static void
-gtk_font_chooser_class_init (GtkFontChooserClass *klass)
-{
- GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
- GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
-
- widget_class->screen_changed = gtk_font_chooser_screen_changed;
- widget_class->style_updated = gtk_font_chooser_style_updated;
-
- gobject_class->finalize = gtk_font_chooser_finalize;
- gobject_class->set_property = gtk_font_chooser_set_property;
- gobject_class->get_property = gtk_font_chooser_get_property;
-
- g_object_class_install_property (gobject_class,
- PROP_FONT_NAME,
- g_param_spec_string ("font-name",
- P_("Font name"),
- P_("The string that represents this font"),
- DEFAULT_FONT_NAME,
- GTK_PARAM_READWRITE));
- g_object_class_install_property (gobject_class,
- PROP_PREVIEW_TEXT,
- g_param_spec_string ("preview-text",
- P_("Preview text"),
- P_("The text to display in order to demonstrate the selected font"),
- pango_language_get_sample_string (NULL),
- GTK_PARAM_READWRITE));
-
- g_object_class_install_property (gobject_class,
- PROP_SHOW_PREVIEW_ENTRY,
- g_param_spec_boolean ("show-preview-entry",
- P_("Show preview text entry"),
- P_("Whether the preview text entry is shown or not"),
- TRUE,
- GTK_PARAM_READWRITE));
-
- g_type_class_add_private (klass, sizeof (GtkFontChooserPrivate));
-}
-
-static void
-gtk_font_chooser_set_property (GObject *object,
- guint prop_id,
- const GValue *value,
- GParamSpec *pspec)
-{
- GtkFontChooser *fontchooser;
-
- fontchooser = GTK_FONT_CHOOSER (object);
-
- switch (prop_id)
- {
- case PROP_FONT_NAME:
- gtk_font_chooser_set_font_name (fontchooser, g_value_get_string (value));
- break;
- case PROP_PREVIEW_TEXT:
- gtk_font_chooser_set_preview_text (fontchooser, g_value_get_string (value));
- break;
- case PROP_SHOW_PREVIEW_ENTRY:
- gtk_font_chooser_set_show_preview_entry (fontchooser, g_value_get_boolean (value));
- default:
- G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
- break;
- }
-}
-
-static void
-gtk_font_chooser_get_property (GObject *object,
- guint prop_id,
- GValue *value,
- GParamSpec *pspec)
-{
- GtkFontChooser *fontchooser;
-
- fontchooser = GTK_FONT_CHOOSER (object);
-
- switch (prop_id)
- {
- case PROP_FONT_NAME:
- g_value_take_string (value, gtk_font_chooser_get_font_name (fontchooser));
- break;
- case PROP_PREVIEW_TEXT:
- g_value_set_string (value, gtk_font_chooser_get_preview_text (fontchooser));
- break;
- case PROP_SHOW_PREVIEW_ENTRY:
- g_value_set_boolean (value, gtk_font_chooser_get_show_preview_entry (fontchooser));
- default:
- G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
- break;
- }
-}
-
-void
-refilter_and_focus (GtkFontChooserPrivate *priv)
-{
- GtkTreeIter iter;
- GtkTreeView *treeview = GTK_TREE_VIEW (priv->family_face_list);
- GtkTreePath *path = gtk_tree_path_new ();
-
- gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter));
-
- if (!path)
- return;
-
- gtk_tree_view_get_cursor (treeview, &path, NULL);
-
- if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->filter), &iter, path))
- {
- gtk_tree_path_free (path);
- return;
- }
-
- gtk_tree_view_scroll_to_cell (treeview, path, NULL, TRUE, 0.5, 0.5);
- gtk_tree_path_free (path);
-}
-
-void
-deleted_text_cb (GtkEntryBuffer *buffer,
- guint position,
- guint n_chars,
- gpointer user_data)
-{
- GtkFontChooserPrivate *priv = (GtkFontChooserPrivate*)user_data;
- GtkWidget *entry = priv->search_entry;
-
- if (gtk_entry_buffer_get_length (buffer) == 0)
- {
- gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
- GTK_ENTRY_ICON_SECONDARY,
- GTK_STOCK_FIND);
- }
-
- gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter));
-}
-
-void
-inserted_text_cb (GtkEntryBuffer *buffer,
- guint position,
- gchar *chars,
- guint n_chars,
- gpointer user_data)
-{
- GtkFontChooserPrivate *priv = (GtkFontChooserPrivate*)user_data;
- GtkWidget *entry = priv->search_entry;
-
- if (g_strcmp0 (gtk_entry_get_icon_stock (GTK_ENTRY (entry), GTK_ENTRY_ICON_SECONDARY),
- GTK_STOCK_CLEAR))
- gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
- GTK_ENTRY_ICON_SECONDARY,
- GTK_STOCK_CLEAR);
-
-
- gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter));
-}
-
-void
-icon_press_cb (GtkEntry *entry,
- GtkEntryIconPosition pos,
- GdkEvent *event,
- gpointer user_data)
-{
- gtk_entry_buffer_delete_text (gtk_entry_get_buffer (entry), 0, -1);
-}
-
-void
-slider_change_cb (GtkAdjustment *adjustment, gpointer data)
-{
- GtkFontChooserPrivate *priv = (GtkFontChooserPrivate*)data;
-
- /* If we set the silder value manually, we ignore this callback */
- if (priv->ignore_slider)
- {
- priv->ignore_slider = FALSE;
- return;
- }
-
- gtk_adjustment_set_value (gtk_spin_button_get_adjustment( GTK_SPIN_BUTTON(priv->size_spin)),
- gtk_adjustment_get_value (adjustment));
-}
-
-void
-spin_change_cb (GtkAdjustment *adjustment, gpointer data)
-{
- PangoFontDescription *desc;
- GtkFontChooser *fontchooser = (GtkFontChooser*)data;
- GtkFontChooserPrivate *priv = fontchooser->priv;
-
- gdouble size = gtk_adjustment_get_value (adjustment);
-
- GtkAdjustment *slider_adj = gtk_range_get_adjustment (GTK_RANGE (priv->size_slider));
-
- /* We ignore the slider value change callback for both of this set_value call */
- priv->ignore_slider = TRUE;
- if (size < gtk_adjustment_get_lower (slider_adj))
- gtk_adjustment_set_value (slider_adj, gtk_adjustment_get_lower (slider_adj));
- else if (size > gtk_adjustment_get_upper (slider_adj))
- gtk_adjustment_set_value (slider_adj, gtk_adjustment_get_upper (slider_adj));
- else
- gtk_adjustment_set_value (slider_adj, size);
-
- priv->size = ((gint)gtk_adjustment_get_value (adjustment)) * PANGO_SCALE;
-
- desc = pango_context_get_font_description (gtk_widget_get_pango_context (priv->preview));
- pango_font_description_set_size (desc, priv->size);
- gtk_widget_override_font (priv->preview, desc);
-
-
- /* Deprecated */
- if (priv->size_list)
- {
- priv->ignore_size = TRUE;
- update_size_list_selection (fontchooser);
- }
-
- g_object_notify (G_OBJECT (fontchooser), "font-name");
-
- gtk_widget_queue_draw (priv->preview);
-}
-
-void
-set_range_marks (GtkFontChooserPrivate *priv,
- GtkWidget* size_slider,
- gint* sizes,
- gint length)
-{
- GtkAdjustment *adj;
- gint i;
- gdouble value;
-
- if (length<2)
- {
- sizes = (gint*)font_sizes;
- length = FONT_SIZES_LENGTH;
- }
-
- gtk_scale_clear_marks (GTK_SCALE (size_slider));
-
- adj = gtk_range_get_adjustment(GTK_RANGE (size_slider));
-
- gtk_adjustment_set_lower (adj, (gdouble) sizes[0]);
- gtk_adjustment_set_upper (adj, (gdouble) sizes[length-1]);
-
- value = gtk_adjustment_get_value (adj);
- if (value > (gdouble) sizes[length-1])
- {
- gtk_adjustment_set_value (adj, (gdouble) sizes[length-1]);
- priv->ignore_slider = TRUE;
- }
- else if (value < (gdouble) sizes[0])
- {
- gtk_adjustment_set_value (adj, (gdouble) sizes[0]);
- priv->ignore_slider = TRUE;
- }
-
- /* Deprecated: just populate the marks in the scale after removal */
- if (!priv->_size_model)
- {
- for (i=0; i<length; i++)
- gtk_scale_add_mark (GTK_SCALE (size_slider),
- (gdouble) sizes[i],
- GTK_POS_BOTTOM, NULL);
- }
- else
- {
- GString *size_str = g_string_new (NULL);
- gtk_list_store_clear (priv->_size_model);
-
- for (i=0; i<length; i++)
- {
- GtkTreeIter iter;
-
- g_string_printf (size_str, "%d", sizes[i]);
-
- gtk_scale_add_mark (GTK_SCALE (size_slider),
- (gdouble) sizes[i],
- GTK_POS_BOTTOM, NULL);
-
- gtk_list_store_append (priv->_size_model, &iter);
- gtk_list_store_set (priv->_size_model, &iter,
- 0, sizes[i],
- 1, size_str->str,
- -1);
- }
- g_string_free (size_str, TRUE);
- }
-}
-
-void
-cursor_changed_cb (GtkTreeView *treeview, gpointer data)
-{
- PangoFontFamily *family;
- PangoFontFace *face;
- PangoFontDescription *desc;
-
- gint *sizes;
- gint i, n_sizes;
-
- GtkTreeIter iter;
- GtkTreePath *path = gtk_tree_path_new ();
-
- GtkFontChooser *fontchooser = (GtkFontChooser*)data;
-
- gtk_tree_view_get_cursor (treeview, &path, NULL);
-
- if (!path)
- return;
-
- if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (fontchooser->priv->filter), &iter, path))
- {
- gtk_tree_path_free (path);
- return;
- }
-
-
- gtk_tree_model_get (GTK_TREE_MODEL (fontchooser->priv->filter), &iter,
- FACE_COLUMN, &face,
- FAMILY_COLUMN, &family,
- -1);
-
- gtk_tree_view_scroll_to_cell (treeview, path, NULL, TRUE, 0.5, 0.5);
-
- gtk_tree_path_free (path);
- path = NULL;
-
- if (!face || !family)
- {
- g_object_unref (face);
- g_object_unref (family);
- return;
- }
-
- desc = pango_font_face_describe (face);
- pango_font_description_set_size (desc, fontchooser->priv->size);
- gtk_widget_override_font (fontchooser->priv->preview, desc);
-
- pango_font_face_list_sizes (face, &sizes, &n_sizes);
- /* It seems not many fonts actually have a sane set of sizes */
- for (i=0; i<n_sizes; i++)
- sizes[i] = sizes[i] / PANGO_SCALE;
-
- set_range_marks (fontchooser->priv, fontchooser->priv->size_slider, sizes, n_sizes);
-
- gtk_font_chooser_ref_family (fontchooser, family);
- gtk_font_chooser_ref_face (fontchooser, face);
-
- /* Deprecated: Remove if clause after removal */
- if (fontchooser->priv->_font_model)
- update_font_list_selection (fontchooser);
-
- /* Free resources */
- g_object_unref ((gpointer)family);
- g_object_unref ((gpointer)face);
- pango_font_description_free(desc);
-
- g_object_notify (G_OBJECT (fontchooser), "font-name");
-}
-
-gboolean
-zoom_preview_cb (GtkWidget *scrolled_window, GdkEventScroll *event, gpointer data)
-{
- GtkFontChooserPrivate *priv = (GtkFontChooserPrivate*)data;
-
- GtkAdjustment *adj = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin));
-
- if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_RIGHT)
- gtk_adjustment_set_value (adj,
- gtk_adjustment_get_value (adj) +
- gtk_adjustment_get_step_increment (adj));
- else if (event->direction == GDK_SCROLL_DOWN || event->direction == GDK_SCROLL_LEFT)
- gtk_adjustment_set_value (adj,
- gtk_adjustment_get_value (adj) -
- gtk_adjustment_get_step_increment (adj));
- return TRUE;
-}
-
-static void
-gtk_font_chooser_init (GtkFontChooser *fontchooser)
-{
- GtkFontChooserPrivate *priv;
- PangoFontDescription *font_desc;
- GtkWidget *scrolled_win;
- GtkWidget *preview_and_size;
- GtkWidget *size_controls;
-
- fontchooser->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontchooser,
- GTK_TYPE_FONT_CHOOSER,
- GtkFontChooserPrivate);
-
- priv = fontchooser->priv;
-
- /* Deprecated: These members will not exist after removal */
- priv->size_list = NULL;
- priv->font_list = NULL;
- priv->face_list = NULL;
-
- priv->_size_model = NULL;
- priv->_font_model = NULL;
- priv->_face_model = NULL;
-
- priv->ignore_size = FALSE;
- priv->ignore_face = FALSE;
- priv->ignore_font = FALSE;
-
- /* Default preview string */
- priv->preview_text = g_strdup (pango_language_get_sample_string (NULL));
- priv->show_preview_entry = TRUE;
-
- /* Getting the default size */
- font_desc = pango_context_get_font_description (gtk_widget_get_pango_context (GTK_WIDGET (fontchooser)));
- priv->size = pango_font_description_get_size (font_desc);
- priv->face = NULL;
- priv->family = NULL;
-
- gtk_widget_push_composite_child ();
-
- /* Creating fundamental widgets for the private struct */
- priv->search_entry = gtk_entry_new ();
- priv->family_face_list = gtk_tree_view_new ();
- priv->preview = gtk_entry_new ();
- priv->size_slider = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL,
- (gdouble) font_sizes[0],
- (gdouble) font_sizes[FONT_SIZES_LENGTH - 1],
- 1.0);
-
- priv->size_spin = gtk_spin_button_new_with_range (0.0, (gdouble)(G_MAXINT / PANGO_SCALE), 1.0);
-
- /** Bootstrapping widget layout **/
- gtk_box_set_spacing (GTK_BOX (fontchooser), 6);
- gtk_box_pack_start (GTK_BOX (fontchooser), priv->search_entry, FALSE, TRUE, 0);
-
- /* Main font family/face view */
- scrolled_win = gtk_scrolled_window_new (NULL, NULL);
- gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
- GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
- gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win),
- GTK_SHADOW_ETCHED_IN);
- gtk_container_add (GTK_CONTAINER (scrolled_win), priv->family_face_list);
-
- /* Alignment for the preview and size controls */
- gtk_box_pack_start (GTK_BOX (fontchooser), scrolled_win, TRUE, TRUE, 0);
-
- preview_and_size = gtk_vbox_new (TRUE, 0);
- gtk_box_set_homogeneous (GTK_BOX (preview_and_size), FALSE);
- gtk_box_set_spacing (GTK_BOX (preview_and_size), 6);
-
- /* The preview entry needs a scrolled window to make sure we have a */
- scrolled_win = gtk_scrolled_window_new (NULL, NULL);
- priv->preview_scrolled_window = scrolled_win;
- gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
- GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
- gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win),
- GTK_SHADOW_ETCHED_IN);
- gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled_win),
- priv->preview);
- gtk_box_pack_start (GTK_BOX (preview_and_size), scrolled_win, FALSE, FALSE, 0);
-
- /* Setting the size requests for various widgets */
- gtk_widget_set_size_request (GTK_WIDGET (fontchooser), FONT_CHOOSER_WIDTH, FONT_CHOOSER_HEIGHT);
- gtk_widget_set_size_request (scrolled_win, -1, PREVIEW_HEIGHT);
- gtk_widget_set_size_request (priv->preview, -1, PREVIEW_HEIGHT - 6);
-
- /* Unset the frame on the preview entry */
- gtk_entry_set_has_frame (GTK_ENTRY (priv->preview), FALSE);
-
- /* Packing the slider and the spin in a hbox */
- size_controls = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
- gtk_scale_set_draw_value (GTK_SCALE (priv->size_slider), FALSE);
- gtk_box_set_spacing (GTK_BOX (size_controls), 6);
- gtk_box_pack_start (GTK_BOX (size_controls), priv->size_slider, TRUE, TRUE, 0);
- gtk_box_pack_start (GTK_BOX (size_controls), priv->size_spin, FALSE, TRUE, 0);
-
- gtk_widget_set_valign (priv->size_spin, GTK_ALIGN_START);
-
- gtk_box_pack_start (GTK_BOX (preview_and_size), size_controls, FALSE, FALSE, 0);
-
- gtk_box_pack_start (GTK_BOX (fontchooser), GTK_WIDGET(preview_and_size), FALSE, TRUE, 0);
-
- gtk_adjustment_set_value (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider)),
- (gdouble)(priv->size / PANGO_SCALE));
- gtk_adjustment_set_value (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin)),
- (gdouble)(priv->size / PANGO_SCALE));
-
- gtk_widget_show_all (GTK_WIDGET (fontchooser));
- gtk_widget_hide (GTK_WIDGET (fontchooser));
-
- /* Treeview column and model bootstrapping */
- gtk_font_chooser_bootstrap_fontlist (fontchooser);
-
- /* Set default preview text */
- gtk_entry_set_text (GTK_ENTRY (priv->preview),
- pango_language_get_sample_string (NULL));
-
- /* Set search icon and place holder text */
- gtk_entry_set_icon_from_stock (GTK_ENTRY (priv->search_entry),
- GTK_ENTRY_ICON_SECONDARY,
- GTK_STOCK_FIND);
- gtk_entry_set_placeholder_text (GTK_ENTRY (priv->search_entry), _("Search font name"));
-
- /** Callback connections **/
- /* Connect to callback for the live search text entry */
- g_signal_connect (G_OBJECT (gtk_entry_get_buffer (GTK_ENTRY (priv->search_entry))),
- "deleted-text", G_CALLBACK (deleted_text_cb), priv);
- g_signal_connect (G_OBJECT (gtk_entry_get_buffer (GTK_ENTRY (priv->search_entry))),
- "inserted-text", G_CALLBACK (inserted_text_cb), priv);
- g_signal_connect (G_OBJECT (priv->search_entry),
- "icon-press", G_CALLBACK (icon_press_cb), priv);
-
- /* Size controls callbacks */
- g_signal_connect (G_OBJECT (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider))),
- "value-changed", G_CALLBACK (slider_change_cb), priv);
- g_signal_connect (G_OBJECT (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin))),
- "value-changed", G_CALLBACK (spin_change_cb), fontchooser);
- priv->ignore_slider = FALSE;
-
- /* Font selection callback */
- g_signal_connect (G_OBJECT (priv->family_face_list), "cursor-changed",
- G_CALLBACK (cursor_changed_cb), fontchooser);
-
- /* Zoom on preview scroll*/
- g_signal_connect (G_OBJECT (scrolled_win), "scroll-event",
- G_CALLBACK (zoom_preview_cb), priv);
-
- g_signal_connect (G_OBJECT (priv->size_slider), "scroll-event",
- G_CALLBACK (zoom_preview_cb), priv);
-
- set_range_marks (priv, priv->size_slider, (gint*)font_sizes, FONT_SIZES_LENGTH);
-
- /* Set default focus */
- gtk_widget_pop_composite_child();
-}
-
-/**
- * gtk_font_chooser_new:
- *
- * Creates a new #GtkFontChooser.
- *
- * Return value: a new #GtkFontChooser
- */
-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, TRUE, 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 = FALSE;
- GtkFontChooserPrivate *priv = (GtkFontChooserPrivate*) data;
-
- const gchar *search_text = (const gchar*)gtk_entry_get_text (GTK_ENTRY (priv->search_entry));
- gchar *font_name;
- gchar *font_name_casefold;
- gchar *search_text_casefold;
-
- gtk_tree_model_get (model, iter,
- PREVIEW_TITLE_COLUMN, &font_name,
- -1);
-
- /* Covering some corner cases to speed up the result */
- if (font_name == NULL ||
- strlen (search_text) > strlen (font_name))
- {
- g_free (font_name);
- return FALSE;
- }
- if (strlen (search_text) == 0)
- {
- g_free (font_name);
- return TRUE;
- }
-
- font_name_casefold = g_utf8_casefold (font_name, -1);
- search_text_casefold = g_utf8_casefold (search_text, -1);
-
- if (g_strrstr (font_name_casefold, search_text_casefold))
- result = TRUE;
-
- g_free (search_text_casefold);
- g_free (font_name_casefold);
- g_free (font_name);
- 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);
-
- /* Deprecated: Remove if clause after removal */
- if (fontchooser->priv->size_list)
- {
- g_object_unref (fontchooser->priv->size_list);
- g_object_unref (fontchooser->priv->font_list);
- g_object_unref (fontchooser->priv->face_list);
- }
-
- 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;
-}
-
-/* Deprecated: All these functions aid deprecated functionality */
-static void
-populate_font_model (GtkFontChooser *fontchooser)
-{
- gint n_families, i;
- PangoFontFamily **families;
- GtkFontChooserPrivate *priv = fontchooser->priv;
- GtkTreePath *path;
-
- pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (fontchooser)),
- &families,
- &n_families);
-
- qsort (families, n_families, sizeof (PangoFontFamily *), cmp_families);
-
- gtk_list_store_clear (priv->_font_model);
-
- for (i=0; i<n_families; i++)
- {
- GtkTreeIter iter;
-
- gtk_list_store_append (priv->_font_model, &iter);
- gtk_list_store_set (priv->_font_model, &iter,
- 0, families[i],
- 1, pango_font_family_get_name (families[i]),
- -1);
- if (priv->family &&
- !strcmp (pango_font_family_get_name (families[i]),
- pango_font_family_get_name (priv->family)))
- {
- path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->_face_model),
- &iter);
- if (path)
- {
- GtkWidget *tv = gtk_bin_get_child (GTK_BIN (priv->font_list));
- gtk_tree_view_set_cursor (GTK_TREE_VIEW (tv),
- path,
- NULL,
- FALSE);
- gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (tv),
- path, NULL, TRUE, 0.5, 0.5);
- gtk_tree_path_free (path);
- }
- }
- }
-
- g_free (families);
-}
-
-static void
-update_font_list_selection (GtkFontChooser *fontchooser)
-{
- GtkTreeIter iter;
- gboolean valid;
- GtkFontChooserPrivate *priv = fontchooser->priv;
- gchar *family_name;
-
- valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->_font_model), &iter);
- while (valid)
- {
- GtkWidget *tv;
- GtkTreePath *path;
- gtk_tree_model_get (GTK_TREE_MODEL (priv->_font_model), &iter,
- 1, &family_name,
- -1);
- if (strcmp (family_name, pango_font_family_get_name (priv->family)))
- {
- gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->_font_model), &iter);
- g_free (family_name);
- continue;
- }
- path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->_font_model), &iter);
-
- if (!path)
- {
- g_free (family_name);
- break;
- }
-
- tv = gtk_bin_get_child (GTK_BIN (priv->font_list));
-
- priv->ignore_font = TRUE;
- gtk_tree_view_set_cursor (GTK_TREE_VIEW (tv), path, NULL, FALSE);
- gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (tv), path, NULL, TRUE, 0.5, 0.5);
-
- /* Free resources */
- gtk_tree_path_free (path);
- g_free (family_name);
- break;
- }
-
- update_face_model (fontchooser, FALSE);
-}
-
-static void
-update_face_model (GtkFontChooser *fontchooser, gboolean first)
-{
- GtkFontChooserPrivate *priv = fontchooser->priv;
- PangoFontFace **faces;
- int i, n_faces;
-
- pango_font_family_list_faces (priv->family, &faces, &n_faces);
-
- gtk_list_store_clear (priv->_face_model);
-
- for (i=0; i<n_faces; i++)
- {
- GtkTreeIter iter;
-
- gtk_list_store_append (priv->_face_model, &iter);
- gtk_list_store_set (priv->_face_model, &iter,
- 0, faces[i],
- 1, pango_font_face_get_face_name (faces[i]),
- -1);
-
- if ((!first && faces[i] == priv->face) ||
- (first && i == 0))
- {
- GtkTreePath *path;
- GtkWidget *tv;
-
- path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->_face_model), &iter);
- if (!path)
- continue;
-
- tv = gtk_bin_get_child (GTK_BIN (priv->face_list));
-
- if (!first)
- priv->ignore_face = TRUE;
-
- gtk_tree_view_set_cursor (GTK_TREE_VIEW (tv), path, NULL, FALSE);
-
- gtk_tree_path_free (path);
-
- if (first)
- gtk_font_chooser_ref_face (fontchooser, faces[i]);
- }
- }
-
- update_size_list_selection (fontchooser);
-
- g_object_notify (G_OBJECT (fontchooser), "font-name");
-}
-
-static void
-update_size_list_selection (GtkFontChooser *fontchooser)
-{
- GtkTreeIter iter;
- gboolean valid;
- GtkFontChooserPrivate *priv = fontchooser->priv;
- GtkWidget *tv = gtk_bin_get_child (GTK_BIN (priv->size_list));
-
- gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (tv)));
-
- valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->_size_model), &iter);
- while (valid)
- {
- gint size;
- gtk_tree_model_get (GTK_TREE_MODEL (priv->_size_model), &iter,
- 0, &size,
- -1);
-
- if (size * PANGO_SCALE == priv->size)
- {
- GtkTreePath *path;
-
-
- path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->_size_model), &iter);
- if (!path)
- break;
-
-
- priv->ignore_size = TRUE;
- gtk_tree_view_set_cursor (GTK_TREE_VIEW (tv), path, NULL, FALSE);
-
- gtk_tree_path_free (path);
- break;
- }
- valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->_size_model), &iter);
- }
-}
-
-
-static void
-select_family_and_face (GtkFontChooser *fontchooser)
-{
- GtkTreeIter iter;
- gboolean valid;
- GtkFontChooserPrivate *priv = fontchooser->priv;
- PangoFontFace *face;
- PangoFontFamily *family;
-
- gtk_entry_set_text (GTK_ENTRY (priv->search_entry), "");
-
- valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->filter), &iter);
- while (valid)
- {
- gtk_tree_model_get (GTK_TREE_MODEL (priv->filter), &iter,
- FACE_COLUMN, &face,
- FAMILY_COLUMN, &family,
- -1);
-
- if (face == priv->face && family == priv->family)
- {
- GtkTreePath *path;
-
- path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->filter), &iter);
- if (!path)
- {
- g_object_unref (face);
- g_object_unref (family);
- break;
- }
-
- priv->ignore_size = TRUE;
- gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->family_face_list), path, NULL, FALSE);
-
- gtk_tree_path_free (path);
- g_object_unref (face);
- g_object_unref (family);
- break;
- }
-
- g_object_unref (face);
- g_object_unref (family);
- valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->filter), &iter);
- }
-}
-
-
-static void
-family_list_cursor_changed_cb (GtkTreeView *treeview, gpointer data)
-{
- GtkWidget *tv;
- GtkTreeIter iter;
- GtkTreePath *path;
- GtkFontChooser *fontchooser = (GtkFontChooser*)data;
- PangoFontFamily *font;
-
- if (fontchooser->priv->ignore_font)
- {
- fontchooser->priv->ignore_font = FALSE;
- return;
- }
-
- tv = gtk_bin_get_child (GTK_BIN (fontchooser->priv->font_list));
- gtk_tree_view_get_cursor (GTK_TREE_VIEW (tv), &path, NULL);
-
- if (!path)
- return;
-
- gtk_tree_model_get_iter (GTK_TREE_MODEL (fontchooser->priv->_font_model),
- &iter,
- path);
-
- gtk_tree_model_get (GTK_TREE_MODEL (fontchooser->priv->_font_model), &iter,
- 0, &font,
- -1);
-
- gtk_font_chooser_ref_family (fontchooser, font);
- update_face_model (fontchooser, TRUE);
-
- fontchooser->priv->ignore_font = TRUE;
- select_family_and_face (fontchooser);
-
- gtk_tree_path_free (path);
- g_object_unref (font);
-}
-
-static void
-face_list_cursor_changed_cb (GtkTreeView *treeview, gpointer data)
-{
- GtkWidget *tv;
- GtkTreeIter iter;
- GtkTreePath *path;
- GtkFontChooser *fontchooser = (GtkFontChooser*)data;
- PangoFontFace *face;
-
- if (fontchooser->priv->ignore_face)
- {
- fontchooser->priv->ignore_face = FALSE;
- return;
- }
-
- tv = gtk_bin_get_child (GTK_BIN (fontchooser->priv->face_list));
- gtk_tree_view_get_cursor (GTK_TREE_VIEW (tv), &path, NULL);
-
- if (!path)
- return;
-
- gtk_tree_model_get_iter (GTK_TREE_MODEL (fontchooser->priv->_face_model),
- &iter,
- path);
-
- gtk_tree_model_get (GTK_TREE_MODEL (fontchooser->priv->_face_model), &iter,
- 0, &face,
- -1);