* 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.
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <atk/atk.h>
#include "gtkfontchooserwidget.h"
-#include "gtkfontchooser.h"
-#include "gtkfontchooserutils.h"
+
+#include "gtkadjustment.h"
+#include "gtkbuildable.h"
+#include "gtkbox.h"
#include "gtkcellrenderertext.h"
#include "gtkentry.h"
-#include "gtkframe.h"
-#include "gtkbbox.h"
-#include "gtkbox.h"
+#include "gtksearchentry.h"
+#include "gtkgrid.h"
+#include "gtkfontchooser.h"
+#include "gtkfontchooserutils.h"
+#include "gtkintl.h"
#include "gtklabel.h"
#include "gtkliststore.h"
-#include "gtkstock.h"
-#include "gtktextview.h"
-#include "gtktreeselection.h"
-#include "gtktreeview.h"
-#include "gtkscrolledwindow.h"
-#include "gtkintl.h"
-#include "gtkaccessible.h"
-#include "gtkbuildable.h"
+#include "gtknotebook.h"
#include "gtkprivate.h"
#include "gtkscale.h"
+#include "gtkscrolledwindow.h"
#include "gtkspinbutton.h"
-#include "gtknotebook.h"
+#include "gtktextview.h"
+#include "gtktreeselection.h"
+#include "gtktreeview.h"
#include "gtkwidget.h"
-#include "gtkgrid.h"
/**
- * SECTION:gtkfontchooser
+ * SECTION:gtkfontchooserwidget
* @Short_description: A widget for selecting fonts
* @Title: GtkFontChooserWidget
* @See_also: #GtkFontChooserDialog
{
GtkWidget *search_entry;
GtkWidget *family_face_list;
+ GtkCellRenderer *family_face_cell;
GtkWidget *list_scrolled_window;
GtkWidget *empty_list;
GtkWidget *list_notebook;
#define FONT_STYLE_LIST_WIDTH 170
#define FONT_SIZE_LIST_WIDTH 60
-#define ROW_FORMAT_STRING "<span weight=\"bold\" size=\"small\">%s</span>\n<span size=\"x-large\" font_desc=\"%s\">%s</span>"
-
#define NO_FONT_MATCHED_SEARCH N_("No fonts matched your search. You can revise your search and try again.")
enum {
}
static void
-text_changed_cb (GtkEntry *entry,
- GParamSpec *pspec,
- GtkFontChooserWidget *fc)
+gtk_font_chooser_widget_refilter_font_list (GtkFontChooserWidget *fontchooser)
{
- GtkFontChooserWidgetPrivate *priv = fc->priv;
- const gchar *text;
-
- text = gtk_entry_get_text (entry);
-
- if (text == NULL || text[0] == '\0')
- {
- GIcon *icon;
-
- icon = g_themed_icon_new_with_default_fallbacks ("edit-find-symbolic");
- g_object_set (G_OBJECT (priv->search_entry),
- "secondary-icon-gicon", icon,
- "secondary-icon-activatable", FALSE,
- "secondary-icon-sensitive", FALSE,
- NULL);
- g_object_unref (icon);
- }
- else
- {
- if (!gtk_entry_get_icon_activatable (GTK_ENTRY (priv->search_entry), GTK_ENTRY_ICON_SECONDARY))
- {
- GIcon *icon;
-
- icon = g_themed_icon_new_with_default_fallbacks ("edit-clear-symbolic");
- g_object_set (G_OBJECT (priv->search_entry),
- "secondary-icon-gicon", icon,
- "secondary-icon-activatable", TRUE,
- "secondary-icon-sensitive", TRUE,
- NULL);
- g_object_unref (icon);
- }
- }
-
- gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
+ gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (fontchooser->priv->filter_model));
+ gtk_font_chooser_widget_ensure_selection (fontchooser);
}
static void
-icon_press_cb (GtkEntry *entry,
- GtkEntryIconPosition pos,
- GdkEvent *event,
- gpointer user_data)
+text_changed_cb (GtkEntry *entry,
+ GParamSpec *pspec,
+ GtkFontChooserWidget *fc)
{
- gtk_entry_set_text (entry, "");
+ gtk_font_chooser_widget_refilter_font_list (fc);
}
static void
static void
gtk_font_chooser_widget_init (GtkFontChooserWidget *fontchooser)
{
- GIcon *icon;
GtkFontChooserWidgetPrivate *priv;
GtkWidget *scrolled_win;
GtkWidget *grid;
gtk_widget_push_composite_child ();
/* Creating fundamental widgets for the private struct */
- priv->search_entry = gtk_entry_new ();
+ priv->search_entry = gtk_search_entry_new ();
priv->family_face_list = gtk_tree_view_new ();
+ gtk_tree_view_set_enable_search (GTK_TREE_VIEW (priv->family_face_list), FALSE);
priv->preview = gtk_entry_new ();
priv->size_slider = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL,
0.0,
gtk_entry_set_text (GTK_ENTRY (priv->preview),
pango_language_get_sample_string (NULL));
- /* Set search icon and place holder text */
- icon = g_themed_icon_new_with_default_fallbacks ("edit-find-symbolic");
- g_object_set (G_OBJECT (priv->search_entry),
- "secondary-icon-gicon", icon,
- "secondary-icon-activatable", FALSE,
- "secondary-icon-sensitive", FALSE,
- NULL);
- g_object_unref (icon);
-
gtk_entry_set_placeholder_text (GTK_ENTRY (priv->search_entry), _("Search font name"));
- /** Callback connections **/
+ /* Callback connections */
g_signal_connect (priv->search_entry, "notify::text",
G_CALLBACK (text_changed_cb), fontchooser);
- g_signal_connect (priv->search_entry,
- "icon-press", G_CALLBACK (icon_press_cb), NULL);
g_signal_connect (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider)),
"value-changed", G_CALLBACK (size_change_cb), fontchooser);
return result;
}
+/* in pango units */
+static int
+gtk_font_chooser_widget_get_preview_text_height (GtkFontChooserWidget *fontchooser)
+{
+ GtkWidget *treeview = fontchooser->priv->family_face_list;
+ double dpi, font_size;
+
+ dpi = gdk_screen_get_resolution (gtk_widget_get_screen (treeview));
+ gtk_style_context_get (gtk_widget_get_style_context (treeview),
+ gtk_widget_get_state_flags (treeview),
+ "font-size", &font_size,
+ NULL);
+
+ return (dpi < 0.0 ? 96.0 : dpi) / 72.0 * PANGO_SCALE_X_LARGE * font_size * PANGO_SCALE;
+}
+
+static PangoAttrList *
+gtk_font_chooser_widget_get_preview_attributes (GtkFontChooserWidget *fontchooser,
+ const PangoFontDescription *font_desc,
+ gsize first_line_len)
+{
+ PangoAttribute *attribute;
+ PangoAttrList *attrs;
+
+ attrs = pango_attr_list_new ();
+
+ attribute = pango_attr_weight_new (PANGO_WEIGHT_BOLD);
+ attribute->end_index = first_line_len;
+ pango_attr_list_insert (attrs, attribute);
+
+ attribute = pango_attr_scale_new (PANGO_SCALE_SMALL);
+ attribute->end_index = first_line_len;
+ pango_attr_list_insert (attrs, attribute);
+
+ if (font_desc)
+ {
+ attribute = pango_attr_font_desc_new (font_desc);
+ attribute->start_index = first_line_len;
+ pango_attr_list_insert (attrs, attribute);
+ }
+
+ attribute = pango_attr_fallback_new (FALSE);
+ attribute->start_index = first_line_len;
+ pango_attr_list_insert (attrs, attribute);
+
+ attribute = pango_attr_size_new_absolute (gtk_font_chooser_widget_get_preview_text_height (fontchooser));
+ attribute->start_index = first_line_len;
+ pango_attr_list_insert (attrs, attribute);
+
+ return attrs;
+}
+
static void
gtk_font_chooser_widget_cell_data_func (GtkTreeViewColumn *column,
GtkCellRenderer *cell,
{
GtkFontChooserWidget *fontchooser = user_data;
PangoFontDescription *font_desc;
- char *to_string, *markup;
+ PangoAttrList *attrs;
+ char *to_string, *text;
+ gsize first_line_len;
font_desc = tree_model_get_font_description (tree_model, iter);
to_string = pango_font_description_to_string (font_desc);
- markup = g_markup_printf_escaped (ROW_FORMAT_STRING,
- to_string,
- to_string,
- fontchooser->priv->preview_text);
+ text = g_strconcat (to_string, "\n", fontchooser->priv->preview_text, NULL);
+ first_line_len = strlen (to_string) + 1;
+
+ attrs = gtk_font_chooser_widget_get_preview_attributes (fontchooser,
+ font_desc,
+ first_line_len);
g_object_set (cell,
- "markup", markup,
+ "attributes", attrs,
+ "text", text,
NULL);
pango_font_description_free (font_desc);
+ pango_attr_list_unref (attrs);
g_free (to_string);
- g_free (markup);
+ g_free (text);
+}
+
+static void
+gtk_font_chooser_widget_set_cell_size (GtkFontChooserWidget *fontchooser)
+{
+ GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
+ PangoAttrList *attrs;
+ GtkRequisition size;
+
+ gtk_cell_renderer_set_fixed_size (priv->family_face_cell, -1, -1);
+
+ attrs = gtk_font_chooser_widget_get_preview_attributes (fontchooser,
+ NULL,
+ 1);
+
+ g_object_set (priv->family_face_cell,
+ "attributes", attrs,
+ "text", "x\nx",
+ NULL);
+
+ pango_attr_list_unref (attrs);
+
+ gtk_cell_renderer_get_preferred_size (priv->family_face_cell,
+ priv->family_face_list,
+ &size,
+ NULL);
+ gtk_cell_renderer_set_fixed_size (priv->family_face_cell, size.width, size.height);
}
static void
{
GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
GtkTreeView *treeview = GTK_TREE_VIEW (priv->family_face_list);
- GtkCellRenderer *cell;
GtkTreeViewColumn *col;
+ g_signal_connect_data (priv->family_face_list,
+ "style-updated",
+ G_CALLBACK (gtk_font_chooser_widget_set_cell_size),
+ fontchooser,
+ NULL,
+ G_CONNECT_AFTER | G_CONNECT_SWAPPED);
+
priv->model = GTK_TREE_MODEL (gtk_list_store_new (4,
PANGO_TYPE_FONT_FAMILY,
PANGO_TYPE_FONT_FACE,
gtk_tree_view_set_rules_hint (treeview, TRUE);
gtk_tree_view_set_headers_visible (treeview, FALSE);
+ gtk_tree_view_set_fixed_height_mode (treeview, TRUE);
- cell = gtk_cell_renderer_text_new ();
- g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
+ priv->family_face_cell = gtk_cell_renderer_text_new ();
+ g_object_set (priv->family_face_cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
col = gtk_tree_view_column_new ();
gtk_tree_view_column_set_title (col, _("Font Family"));
- gtk_tree_view_column_pack_start (col, cell, TRUE);
+ gtk_tree_view_column_set_sizing (col, GTK_TREE_VIEW_COLUMN_FIXED);
+ gtk_tree_view_column_pack_start (col, priv->family_face_cell, TRUE);
gtk_tree_view_column_set_cell_data_func (col,
- cell,
+ priv->family_face_cell,
gtk_font_chooser_widget_cell_data_func,
fontchooser,
NULL);
gtk_tree_view_append_column (treeview, col);
gtk_font_chooser_widget_load_fonts (fontchooser);
+
+ gtk_font_chooser_widget_set_cell_size (fontchooser);
}
static void
&filter_iter,
&priv->font_iter))
{
+ GtkTreePath *path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->filter_model),
+ &filter_iter);
+
gtk_tree_selection_select_iter (selection, &filter_iter);
+ gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (priv->family_face_list),
+ path, NULL, FALSE, 0.0, 0.0);
+ gtk_tree_path_free (path);
}
else
{
priv->filter_data = data;
priv->filter_data_destroy = destroy;
- gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
+ gtk_font_chooser_widget_refilter_font_list (fontchooser);
}
static void