Allow setting Pango attributes on entries

This is useful to adjust the weight or scale of the text.
This commit is contained in:
Matthias Clasen 2012-08-31 14:46:07 -04:00
parent 2458f4e594
commit 1ac2982265
4 changed files with 95 additions and 7 deletions

View File

@ -1046,6 +1046,8 @@ gtk_entry_get_layout
gtk_entry_get_layout_offsets
gtk_entry_layout_index_to_text_index
gtk_entry_text_index_to_layout_index
gtk_entry_set_attributes
gtk_entry_get_attributes
gtk_entry_get_max_length
gtk_entry_get_visibility
gtk_entry_set_completion

View File

@ -890,6 +890,7 @@ gtk_entry_completion_set_popup_single_match
gtk_entry_completion_set_text_column
gtk_entry_get_activates_default
gtk_entry_get_alignment
gtk_entry_get_attributes
gtk_entry_get_buffer
gtk_entry_get_completion
gtk_entry_get_current_icon_drag_source
@ -932,6 +933,7 @@ gtk_entry_progress_pulse
gtk_entry_reset_im_context
gtk_entry_set_activates_default
gtk_entry_set_alignment
gtk_entry_set_attributes
gtk_entry_set_buffer
gtk_entry_set_completion
gtk_entry_set_cursor_hadjustment

View File

@ -147,6 +147,7 @@ struct _GtkEntryPrivate
GdkWindow *text_area;
PangoLayout *cached_layout;
PangoAttrList *attrs;
gchar *im_module;
@ -307,7 +308,8 @@ enum {
PROP_PLACEHOLDER_TEXT,
PROP_COMPLETION,
PROP_INPUT_PURPOSE,
PROP_INPUT_HINTS
PROP_INPUT_HINTS,
PROP_ATTRIBUTES
};
static guint signals[LAST_SIGNAL] = { 0 };
@ -1375,6 +1377,22 @@ gtk_entry_class_init (GtkEntryClass *class)
GTK_INPUT_HINT_NONE,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
/**
* GtkEntry:attributes:
*
* A list of Pango attributes to apply to the text of the entry.
*
* This is mainly useful to change the size or weight of the text.
*
* Since: 3.6
*/
g_object_class_install_property (gobject_class,
PROP_ATTRIBUTES,
g_param_spec_boxed ("attributes",
P_("Attributes"),
P_("A list of style attributes to apply to the text of the label"),
PANGO_TYPE_ATTR_LIST,
GTK_PARAM_READWRITE));
/**
* GtkEntry:icon-prelight:
@ -2187,6 +2205,10 @@ gtk_entry_set_property (GObject *object,
gtk_entry_set_input_hints (entry, g_value_get_flags (value));
break;
case PROP_ATTRIBUTES:
gtk_entry_set_attributes (entry, g_value_get_boxed (value));
break;
case PROP_SCROLL_OFFSET:
case PROP_CURSOR_POSITION:
default:
@ -2419,6 +2441,10 @@ gtk_entry_get_property (GObject *object,
g_value_set_flags (value, gtk_entry_get_input_hints (entry));
break;
case PROP_ATTRIBUTES:
g_value_set_boxed (value, priv->attrs);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
@ -5606,9 +5632,9 @@ gtk_entry_create_layout (GtkEntry *entry,
{
GtkEntryPrivate *priv = entry->priv;
GtkWidget *widget = GTK_WIDGET (entry);
PangoLayout *layout = gtk_widget_create_pango_layout (widget, NULL);
PangoAttrList *tmp_attrs = pango_attr_list_new ();
gboolean placeholder_layout = show_placeholder_text (entry);
PangoLayout *layout;
PangoAttrList *tmp_attrs;
gboolean placeholder_layout;
gchar *preedit_string = NULL;
gint preedit_length = 0;
@ -5617,8 +5643,13 @@ gtk_entry_create_layout (GtkEntry *entry,
gchar *display;
guint n_bytes;
layout = gtk_widget_create_pango_layout (widget, NULL);
pango_layout_set_single_paragraph_mode (layout, TRUE);
tmp_attrs = priv->attrs ? pango_attr_list_ref (priv->attrs)
: pango_attr_list_new ();
placeholder_layout = show_placeholder_text (entry);
display = placeholder_layout ? g_strdup (priv->placeholder_text) : _gtk_entry_get_display_text (entry, 0, -1);
n_bytes = strlen (display);
@ -5646,12 +5677,9 @@ gtk_entry_create_layout (GtkEntry *entry,
gint cursor_index = g_utf8_offset_to_pointer (display, priv->current_pos) - display;
g_string_insert (tmp_string, cursor_index, preedit_string);
pango_layout_set_text (layout, tmp_string->str, tmp_string->len);
pango_attr_list_splice (tmp_attrs, preedit_attrs,
cursor_index, preedit_length);
g_string_free (tmp_string, TRUE);
}
else
@ -9893,3 +9921,54 @@ gtk_entry_get_input_hints (GtkEntry *entry)
return hints;
}
/**
* gtk_entry_set_attributes:
* @entry: a #GtkEntry
* @attrs: a #PangoAttrList
*
* Sets a #PangoAttrList; the attributes in the list are applied to the
* entry text.
*
* Since: 3.6
*/
void
gtk_entry_set_attributes (GtkEntry *entry,
PangoAttrList *attrs)
{
GtkEntryPrivate *priv = entry->priv;
g_return_if_fail (GTK_IS_ENTRY (entry));
if (attrs)
pango_attr_list_ref (attrs);
if (priv->attrs)
pango_attr_list_unref (priv->attrs);
priv->attrs = attrs;
g_object_notify (G_OBJECT (entry), "attributes");
gtk_entry_recompute (entry);
gtk_widget_queue_resize (GTK_WIDGET (entry));
}
/**
* gtk_entry_get_attributes:
* @entry: a #GtkEntry
*
* Gets the attribute list that was set on the entry using
* gtk_entry_set_attributes(), if any.
*
* Return value: (transfer none): the attribute list, or %NULL
* if none was set.
*
* Since: 3.6
*/
PangoAttrList *
gtk_entry_get_attributes (GtkEntry *entry)
{
g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
return entry->priv->attrs;
}

View File

@ -289,6 +289,11 @@ void gtk_entry_set_input_hints (GtkEntry
GDK_AVAILABLE_IN_3_6
GtkInputHints gtk_entry_get_input_hints (GtkEntry *entry);
GDK_AVAILABLE_IN_3_6
void gtk_entry_set_attributes (GtkEntry *entry,
PangoAttrList *attrs);
GDK_AVAILABLE_IN_3_6
PangoAttrList *gtk_entry_get_attributes (GtkEntry *entry);
G_END_DECLS