GtkLabel patch



This patch removes the pattern field from GtkLabel, and moves to a totally
PangoAttrList centric api. All functions are just different ways of
setting the attribute list. It needs a recent version of pango, since i
had to add a boxed type for PangoAttrList.

Included is an cleaned up and fixed version of lee's GParam patch.

Can I commit this?

/ Alex

Index: gtklabel.c
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtklabel.c,v
retrieving revision 1.74
diff -u -p -r1.74 gtklabel.c
--- gtklabel.c	2001/02/20 15:36:28	1.74
+++ gtklabel.c	2001/02/28 17:47:16
@@ -30,6 +30,7 @@
 #include "gtkclipboard.h"
 #include "gdk/gdki18n.h"
 #include <pango/pango.h>
+#include "gtkintl.h"

 struct _GtkLabelSelectionInfo
 {
@@ -39,21 +40,29 @@ struct _GtkLabelSelectionInfo
 };

 enum {
-  ARG_0,
-  ARG_LABEL,
-  ARG_PATTERN,
-  ARG_JUSTIFY,
-  ARG_WRAP
+  PROP_0,
+  PROP_TEXT,
+  PROP_ATTRIBUTES,
+  PROP_MARKUP,
+  PROP_MARKUP_ACCEL,
+  PROP_JUSTIFY,
+  PROP_PATTERN,
+  PROP_WRAP,
+  PROP_SELECTABLE
 };

 static void gtk_label_class_init        (GtkLabelClass    *klass);
 static void gtk_label_init              (GtkLabel         *label);
-static void gtk_label_set_arg           (GtkObject        *object,
-					 GtkArg           *arg,
-					 guint             arg_id);
-static void gtk_label_get_arg           (GtkObject        *object,
-					 GtkArg           *arg,
-					 guint             arg_id);
+static void gtk_label_set_property      (GObject          *object,
+					 guint             prop_id,
+					 const GValue     *value,
+					 GParamSpec       *pspec,
+					 const gchar      *trailer);
+static void gtk_label_get_property      (GObject          *object,
+					 guint             prop_id,
+					 GValue           *value,
+					 GParamSpec       *pspec,
+					 const gchar      *trailer);
 static void gtk_label_finalize          (GObject          *object);
 static void gtk_label_size_request      (GtkWidget        *widget,
 					 GtkRequisition   *requisition);
@@ -119,24 +128,80 @@ gtk_label_get_type (void)
 static void
 gtk_label_class_init (GtkLabelClass *class)
 {
-  GObjectClass *gobject_class = G_OBJECT_CLASS (class);
+  GObjectClass *gobject_class;
   GtkObjectClass *object_class;
   GtkWidgetClass *widget_class;

+  gobject_class = (GObjectClass*) class;
   object_class = (GtkObjectClass*) class;
   widget_class = (GtkWidgetClass*) class;

   parent_class = gtk_type_class (GTK_TYPE_MISC);

-  gtk_object_add_arg_type ("GtkLabel::label", GTK_TYPE_STRING, GTK_ARG_READWRITE, ARG_LABEL);
-  gtk_object_add_arg_type ("GtkLabel::pattern", GTK_TYPE_STRING, GTK_ARG_READWRITE, ARG_PATTERN);
-  gtk_object_add_arg_type ("GtkLabel::justify", GTK_TYPE_JUSTIFICATION, GTK_ARG_READWRITE, ARG_JUSTIFY);
-  gtk_object_add_arg_type ("GtkLabel::wrap", GTK_TYPE_BOOL, GTK_ARG_READWRITE, ARG_WRAP);
-
+  gobject_class->set_property = gtk_label_set_property;
+  gobject_class->get_property = gtk_label_get_property;
+
+  g_object_class_install_property (G_OBJECT_CLASS(object_class),
+                                   PROP_TEXT,
+                                   g_param_spec_string ("text",
+                                                        _("Text"),
+                                                        _("The text that this label displays."),
+                                                        NULL,
+                                                        G_PARAM_READABLE | G_PARAM_WRITABLE));
+  g_object_class_install_property (gobject_class,
+				   PROP_ATTRIBUTES,
+				   g_param_spec_boxed ("attributes",
+						       _("Attributes"),
+						       _("Set the rendering attributes of this label."),
+						       PANGO_TYPE_ATTR_LIST,
+						       G_PARAM_READABLE | G_PARAM_WRITABLE));
+  g_object_class_install_property (gobject_class,
+                                   PROP_MARKUP,
+                                   g_param_spec_string ("markup",
+                                                        _("Markup"),
+                                                        _("Markup properties for this label."),
+                                                        NULL,
+                                                        G_PARAM_WRITABLE));
+  g_object_class_install_property (gobject_class,
+                                   PROP_MARKUP_ACCEL,
+                                   g_param_spec_string ("markup_accel",
+                                                        _("Accelerated Markup"),
+                                                        _("Markup properties where underscored characters are shown as keyboard shortcuts."),
+                                                        NULL,
+                                                        G_PARAM_WRITABLE));
+
+  g_object_class_install_property (gobject_class,
+				   PROP_JUSTIFY,
+                                   g_param_spec_enum ("justify",
+                                                      _("Justification"),
+                                                      _("How to align each line of text in the label"),
+						      GTK_TYPE_JUSTIFICATION,
+						      GTK_JUSTIFY_LEFT,
+                                                      G_PARAM_READABLE | G_PARAM_WRITABLE));
+
+  g_object_class_install_property (gobject_class,
+                                   PROP_PATTERN,
+                                   g_param_spec_string ("pattern",
+                                                        _("Pattern"),
+                                                        _("Pattern for label underlines"),
+                                                        NULL,
+                                                        G_PARAM_WRITABLE));
+
+  g_object_class_install_property (gobject_class,
+                                   PROP_WRAP,
+                                   g_param_spec_boolean ("wrap",
+                                                        _("Line wrap"),
+                                                        _("Whether words are wrapped at the end of a line."),
+                                                        TRUE,
+                                                        G_PARAM_READABLE | G_PARAM_WRITABLE));
+  g_object_class_install_property (gobject_class,
+                                   PROP_SELECTABLE,
+                                   g_param_spec_boolean ("selectable",
+                                                        _("Selectable"),
+                                                        _("Whether the label text can be selected with the mouse."),
+                                                        FALSE,
+                                                        G_PARAM_READABLE | G_PARAM_WRITABLE));
   gobject_class->finalize = gtk_label_finalize;
-
-  object_class->set_arg = gtk_label_set_arg;
-  object_class->get_arg = gtk_label_get_arg;

   widget_class->size_request = gtk_label_size_request;
   widget_class->size_allocate = gtk_label_size_allocate;
@@ -152,59 +217,82 @@ gtk_label_class_init (GtkLabelClass *cla
   widget_class->motion_notify_event = gtk_label_motion;
 }

-static void
-gtk_label_set_arg (GtkObject	  *object,
-		   GtkArg	  *arg,
-		   guint	   arg_id)
+static void
+gtk_label_set_property (GObject      *object,
+			guint         prop_id,
+			const GValue *value,
+			GParamSpec   *pspec,
+			const gchar  *trailer)
 {
   GtkLabel *label;

   label = GTK_LABEL (object);

-  switch (arg_id)
+  switch (prop_id)
     {
-    case ARG_LABEL:
-      gtk_label_set_text (label, GTK_VALUE_STRING (*arg));
+    case PROP_TEXT:
+      gtk_label_set_text (label, g_value_get_string (value));
       break;
-    case ARG_PATTERN:
-      gtk_label_set_pattern (label, GTK_VALUE_STRING (*arg));
+    case PROP_ATTRIBUTES:
+      {
+	PangoAttrList *attrs = g_value_get_as_pointer (value);
+	gtk_label_set_attributes (label, attrs);
+	break;
+      }
+    case PROP_MARKUP:
+      gtk_label_set_markup (label, g_value_get_string (value));
+      break;
+    case PROP_MARKUP_ACCEL:
+      gtk_label_set_markup_with_accel (label, g_value_get_string (value));
       break;
-    case ARG_JUSTIFY:
-      gtk_label_set_justify (label, GTK_VALUE_ENUM (*arg));
+    case PROP_JUSTIFY:
+      gtk_label_set_justify (label, g_value_get_enum (value));
       break;
-    case ARG_WRAP:
-      gtk_label_set_line_wrap (label, GTK_VALUE_BOOL (*arg));
+    case PROP_PATTERN:
+      gtk_label_set_pattern (label, g_value_get_string (value));
+      break;
+    case PROP_WRAP:
+      gtk_label_set_line_wrap (label, g_value_get_boolean (value));
+      break;
+    case PROP_SELECTABLE:
+      gtk_label_set_selectable (label, g_value_get_boolean (value));
       break;
     default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
     }
 }

-static void
-gtk_label_get_arg (GtkObject	  *object,
-		   GtkArg	  *arg,
-		   guint	   arg_id)
+static void
+gtk_label_get_property (GObject     *object,
+			guint        prop_id,
+			GValue      *value,
+			GParamSpec  *pspec,
+			const gchar *trailer)
 {
   GtkLabel *label;

   label = GTK_LABEL (object);

-  switch (arg_id)
+  switch (prop_id)
     {
-    case ARG_LABEL:
-      GTK_VALUE_STRING (*arg) = g_strdup (label->label);
+    case PROP_TEXT:
+      g_value_set_string (value, g_strdup (label->label));
       break;
-    case ARG_PATTERN:
-      GTK_VALUE_STRING (*arg) = g_strdup (label->pattern);
+    case PROP_ATTRIBUTES:
+      g_value_set_boxed (value, label->attrs);
       break;
-    case ARG_JUSTIFY:
-      GTK_VALUE_ENUM (*arg) = label->jtype;
+    case PROP_JUSTIFY:
+      g_value_set_enum (value, label->jtype);
       break;
-    case ARG_WRAP:
-      GTK_VALUE_BOOL (*arg) = label->wrap;
+    case PROP_WRAP:
+      g_value_set_boolean (value, label->wrap);
       break;
+    case PROP_SELECTABLE:
+      g_value_set_boolean (value, gtk_label_get_selectable (label));
+      break;
     default:
-      arg->type = GTK_TYPE_INVALID;
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
     }
 }
@@ -215,7 +303,6 @@ gtk_label_init (GtkLabel *label)
   GTK_WIDGET_SET_FLAGS (label, GTK_NO_WINDOW);

   label->label = NULL;
-  label->pattern = NULL;

   label->jtype = GTK_JUSTIFY_CENTER;
   label->wrap = FALSE;
@@ -251,6 +338,8 @@ gtk_label_set_text_internal (GtkLabel *l

   gtk_label_select_region_index (label, 0, 0);

+  g_object_notify (G_OBJECT (label), "text");
+
   gtk_widget_queue_resize (GTK_WIDGET (label));
 }

@@ -284,6 +373,7 @@ gtk_label_set_attributes (GtkLabel
     pango_attr_list_unref (label->attrs);

   label->attrs = attrs;
+  g_object_notify (G_OBJECT (label), "attributes");
 }

 static guint
@@ -386,15 +476,57 @@ gtk_label_get_text (GtkLabel *label)
   return label->label;
 }

+static PangoAttrList *
+gtk_label_pattern_to_attrs (GtkLabel      *label,
+			    const gchar   *pattern)
+{
+  const char *start;
+  const char *p = label->label;
+  const char *q = pattern;
+  PangoAttrList *attrs;
+
+  attrs = pango_attr_list_new ();
+
+  while (1)
+    {
+      while (*p && *q && *q != '_')
+	{
+	  p = g_utf8_next_char (p);
+	  q++;
+	}
+      start = p;
+      while (*p && *q && *q == '_')
+	{
+	  p = g_utf8_next_char (p);
+	  q++;
+	}
+
+      if (p > start)
+	{
+	  PangoAttribute *attr = pango_attr_underline_new (PANGO_UNDERLINE_LOW);
+	  attr->start_index = start - label->label;
+	  attr->end_index = p - label->label;
+
+	  pango_attr_list_insert (attrs, attr);
+	}
+      else
+	break;
+    }
+
+  return attrs;
+}
+
 void
 gtk_label_set_pattern (GtkLabel	   *label,
 		       const gchar *pattern)
 {
+  PangoAttrList *attrs;
   g_return_if_fail (GTK_IS_LABEL (label));

-  g_free (label->pattern);
-  label->pattern = g_strdup (pattern);
+  attrs = gtk_label_pattern_to_attrs (label, pattern);

+  gtk_label_set_attributes (label, attrs);
+
   gtk_widget_queue_resize (GTK_WIDGET (label));
 }

@@ -416,6 +548,7 @@ gtk_label_set_justify (GtkLabel        *
 	  label->layout = NULL;
 	}

+      g_object_notify (G_OBJECT (label), "justify");
       gtk_widget_queue_resize (GTK_WIDGET (label));
     }
 }
@@ -431,7 +564,8 @@ gtk_label_set_line_wrap (GtkLabel *label
   if (label->wrap != wrap)
     {
       label->wrap = wrap;
-
+      g_object_notify (G_OBJECT (label), "wrap");
+
       gtk_widget_queue_resize (GTK_WIDGET (label));
     }
 }
@@ -457,7 +591,6 @@ gtk_label_finalize (GObject *object)
   label = GTK_LABEL (object);

   g_free (label->label);
-  g_free (label->pattern);

   if (label->layout)
     g_object_unref (G_OBJECT (label->layout));
@@ -471,44 +604,6 @@ gtk_label_finalize (GObject *object)
 }

 static void
-gtk_label_pattern_to_attrs (GtkLabel      *label,
-                            PangoAttrList *attrs)
-{
-  if (label->pattern)
-    {
-      const char *start;
-      const char *p = label->label;
-      const char *q = label->pattern;
-
-      while (1)
-	{
-	  while (*p && *q && *q != '_')
-	    {
-	      p = g_utf8_next_char (p);
-	      q++;
-	    }
-	  start = p;
-	  while (*p && *q && *q == '_')
-	    {
-	      p = g_utf8_next_char (p);
-	      q++;
-	    }
-
-	  if (p > start)
-	    {
-	      PangoAttribute *attr = pango_attr_underline_new (PANGO_UNDERLINE_LOW);
-	      attr->start_index = start - label->label;
-	      attr->end_index = p - label->label;
-
-	      pango_attr_list_insert (attrs, attr);
-	    }
-	  else
-	    break;
-	}
-    }
-}
-
-static void
 gtk_label_clear_layout (GtkLabel *label)
 {
   if (label->layout)
@@ -556,27 +651,12 @@ gtk_label_ensure_layout (GtkLabel *label
   if (!label->layout)
     {
       PangoAlignment align = PANGO_ALIGN_LEFT; /* Quiet gcc */
-      PangoAttrList *attrs = NULL;

       label->layout = gtk_widget_create_pango_layout (widget, label->label);

-      /* FIXME move to a model where the pattern isn't stored
-       * permanently, and just modifies or creates the AttrList
-       */
       if (label->attrs)
-	attrs = pango_attr_list_copy (label->attrs);
-      else if (label->pattern)
-        attrs = pango_attr_list_new ();
-
-      if (label->pattern)
-	gtk_label_pattern_to_attrs (label, attrs);
+	pango_layout_set_attributes (label->layout, label->attrs);

-      if (attrs)
-	{
-	  pango_layout_set_attributes (label->layout, attrs);
-	  pango_attr_list_unref (attrs);
-	}
-
       switch (label->jtype)
 	{
 	case GTK_JUSTIFY_LEFT:
@@ -1310,7 +1390,10 @@ gtk_label_set_selectable (GtkLabel *labe
         }
     }
   if (setting != old_setting)
-    gtk_widget_queue_draw (GTK_WIDGET (label));
+    {
+       g_object_notify (G_OBJECT (label), "selectable");
+       gtk_widget_queue_draw (GTK_WIDGET (label));
+    }
 }

 gboolean
Index: gtklabel.h
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtklabel.h,v
retrieving revision 1.24
diff -u -p -r1.24 gtklabel.h
--- gtklabel.h	2001/02/17 00:11:02	1.24
+++ gtklabel.h	2001/02/28 17:47:16
@@ -54,9 +54,7 @@ struct _GtkLabel
   GtkMisc misc;

   /*< private >*/
-
   gchar    *label;
-  gchar    *pattern;

   guint   jtype : 2;
   guint   wrap : 1;





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