[gtk+] cellrenderertext: Add placeholder-text property



commit a18f018d873b838c4b141bcac83e920fcf8b1bdf
Author: Carlos Garcia Campos <carlosgc gnome org>
Date:   Thu May 3 23:25:08 2012 -0400

    cellrenderertext: Add placeholder-text property
    
    It works similar to placeholder text in a GtkEntry, if the cell renderer
    is editable and it's empty, the placeholder text is displayed.
    https://bugzilla.gnome.org/show_bug.cgi?id=645514

 gtk/gtkcellrenderertext.c |   64 +++++++++++++++++++++++++++++++++++++++++---
 tests/testtreeedit.c      |    7 +++++
 2 files changed, 66 insertions(+), 5 deletions(-)
---
diff --git a/gtk/gtkcellrenderertext.c b/gtk/gtkcellrenderertext.c
index ca88fc7..e175625 100644
--- a/gtk/gtkcellrenderertext.c
+++ b/gtk/gtkcellrenderertext.c
@@ -107,7 +107,8 @@ enum {
   PROP_MAX_WIDTH_CHARS,
   PROP_WRAP_WIDTH,
   PROP_ALIGN,
-  
+  PROP_PLACEHOLDER_TEXT,
+
   /* Style args */
   PROP_BACKGROUND,
   PROP_FOREGROUND,
@@ -171,6 +172,7 @@ struct _GtkCellRendererTextPrivate
   PangoWrapMode         wrap_mode;
 
   gchar *text;
+  gchar *placeholder_text;
 
   gdouble font_scale;
 
@@ -619,7 +621,22 @@ gtk_cell_renderer_text_class_init (GtkCellRendererTextClass *class)
 						      PANGO_TYPE_ALIGNMENT,
 						      PANGO_ALIGN_LEFT,
 						      GTK_PARAM_READWRITE));
-  
+
+  /**
+   * GtkCellRendererText:placeholder-text:
+   *
+   * The text that will be displayed in the #GtkCellRenderer if
+   * #GtkCellRendererText:editable is %TRUE and the cell is empty.
+   *
+   * Since 3.6
+   */
+  g_object_class_install_property (object_class,
+                                   PROP_PLACEHOLDER_TEXT,
+                                   g_param_spec_string ("placeholder-text",
+                                                        P_("Placeholder text"),
+                                                        P_("Text rendered when an editable cell is empty"),
+                                                        NULL,
+                                                        GTK_PARAM_READWRITE));
 
 
   /* Style props are set or not */
@@ -726,6 +743,7 @@ gtk_cell_renderer_text_finalize (GObject *object)
   pango_font_description_free (priv->font);
 
   g_free (priv->text);
+  g_free (priv->placeholder_text);
 
   if (priv->extra_attrs)
     pango_attr_list_unref (priv->extra_attrs);
@@ -950,6 +968,10 @@ gtk_cell_renderer_text_get_property (GObject        *object,
       g_value_set_int (value, priv->max_width_chars);
       break;  
 
+    case PROP_PLACEHOLDER_TEXT:
+      g_value_set_string (value, priv->placeholder_text);
+      break;
+
     case PROP_BACKGROUND:
     case PROP_FOREGROUND:
     case PROP_MARKUP:
@@ -1510,7 +1532,12 @@ gtk_cell_renderer_text_set_property (GObject      *object,
     case PROP_ALIGN_SET:
       priv->align_set = g_value_get_boolean (value);
       break;
-      
+
+    case PROP_PLACEHOLDER_TEXT:
+      g_free (priv->placeholder_text);
+      priv->placeholder_text = g_value_dup_string (value);
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
       break;
@@ -1536,6 +1563,15 @@ gtk_cell_renderer_text_new (void)
   return g_object_new (GTK_TYPE_CELL_RENDERER_TEXT, NULL);
 }
 
+static inline gboolean
+show_placeholder_text (GtkCellRendererText *celltext)
+{
+  GtkCellRendererTextPrivate *priv = celltext->priv;
+
+  return priv->editable && priv->placeholder_text &&
+    (!priv->text || !priv->text[0]);
+}
+
 static void
 add_attr (PangoAttrList  *attr_list,
           PangoAttribute *attr)
@@ -1557,8 +1593,10 @@ get_layout (GtkCellRendererText *celltext,
   PangoLayout *layout;
   PangoUnderline uline;
   gint xpad;
+  gboolean placeholder_layout = show_placeholder_text (celltext);
 
-  layout = gtk_widget_create_pango_layout (widget, priv->text);
+  layout = gtk_widget_create_pango_layout (widget, placeholder_layout ?
+                                           priv->placeholder_text : priv->text);
 
   gtk_cell_renderer_get_padding (GTK_CELL_RENDERER (celltext), &xpad, NULL);
 
@@ -1569,7 +1607,7 @@ get_layout (GtkCellRendererText *celltext,
 
   pango_layout_set_single_paragraph_mode (layout, priv->single_paragraph);
 
-  if (cell_area)
+  if (!placeholder_layout && cell_area)
     {
       /* Add options that affect appearance but not size */
       
@@ -1594,6 +1632,22 @@ get_layout (GtkCellRendererText *celltext,
         add_attr (attr_list,
                   pango_attr_strikethrough_new (priv->strikethrough));
     }
+  else if (placeholder_layout)
+    {
+      PangoColor color;
+      GtkStyleContext *context;
+      GdkRGBA fg = { 0.5, 0.5, 0.5 };
+
+      context = gtk_widget_get_style_context (widget);
+      gtk_style_context_lookup_color (context, "placeholder_text_color", &fg);
+
+      color.red = CLAMP (fg.red * 65535. + 0.5, 0, 65535);
+      color.green = CLAMP (fg.green * 65535. + 0.5, 0, 65535);
+      color.blue = CLAMP (fg.blue * 65535. + 0.5, 0, 65535);
+
+      add_attr (attr_list,
+                pango_attr_foreground_new (color.red, color.green, color.blue));
+    }
 
   add_attr (attr_list, pango_attr_font_desc_new (priv->font));
 
diff --git a/tests/testtreeedit.c b/tests/testtreeedit.c
index 0fd7ecd..c094028 100644
--- a/tests/testtreeedit.c
+++ b/tests/testtreeedit.c
@@ -40,6 +40,7 @@ static ListEntry model_strings[] =
 {
   {"A simple string", TRUE, TRUE, 0 },
   {"Another string!", TRUE, TRUE, 10 },
+  {"", TRUE, TRUE, 0 },
   {"Guess what, a third string. This one can't be edited", FALSE, TRUE, 47 },
   {"And then a fourth string. Neither can this", FALSE, TRUE, 48 },
   {"Multiline\nFun!", TRUE, FALSE, 75 },
@@ -285,6 +286,9 @@ main (gint argc, gchar **argv)
   callback[1].renderer = renderer;
   g_signal_connect (renderer, "edited",
 		    G_CALLBACK (edited), tree_model);
+  g_object_set (renderer,
+                "placeholder-text", "Type here",
+                NULL);
 
   renderer = gtk_cell_renderer_text_new ();
   gtk_tree_view_column_pack_start (column, renderer, FALSE);
@@ -297,6 +301,9 @@ main (gint argc, gchar **argv)
   callback[2].renderer = renderer;
   g_signal_connect (renderer, "edited",
 		    G_CALLBACK (edited), tree_model);
+  g_object_set (renderer,
+                "placeholder-text", "Type here too",
+                NULL);
 
   renderer = gtk_cell_renderer_pixbuf_new ();
   g_object_set (renderer,



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]