]> Pileus Git - ~andy/gtk/commitdiff
Allow setting Pango attributes on entries
authorMatthias Clasen <mclasen@redhat.com>
Fri, 31 Aug 2012 18:46:07 +0000 (14:46 -0400)
committerMatthias Clasen <mclasen@redhat.com>
Fri, 31 Aug 2012 19:09:55 +0000 (15:09 -0400)
This is useful to adjust the weight or scale of the text.

docs/reference/gtk/gtk3-sections.txt
gtk/gtk.symbols
gtk/gtkentry.c
gtk/gtkentry.h

index 97c8a89f9d6e17c8ff4bc45d71821c82bce565c9..6649d9f927d29cb5b349751a243899278b69dd9e 100644 (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
index 370923ae7a1511b79b88d65b3ab3df020d1c5aff..614f52891592a4488d2ad1762af37e0cdefe1b9e 100644 (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
index a2a55857642506a467f195dd84a0718a0cf243bf..c3a50f1aa5f83fa84a1053ed79808e79275d37d2 100644 (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;
+}
+
index 86efd7e098d96534b32aa0d649397a9ce097555f..338c8d279bd9ea67b81f864affeef0c523f231e7 100644 (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