After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 527486 - GtkLabel should parse pango attributes
GtkLabel should parse pango attributes
Status: RESOLVED FIXED
Product: gtk+
Classification: Platform
Component: Class: GtkBuilder
unspecified
Other Linux
: Normal normal
: ---
Assigned To: GtkBuilder maintainers
GtkBuilder maintainers
Depends on:
Blocks: 97061
 
 
Reported: 2008-04-11 06:33 UTC by Tristan Van Berkom
Modified: 2008-04-16 08:33 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
pango attributes parsing in GtkLabel (13.21 KB, patch)
2008-04-11 06:40 UTC, Tristan Van Berkom
reviewed Details | Review
New patch with start and end properties implemented and fixed tests (15.19 KB, patch)
2008-04-11 16:09 UTC, Tristan Van Berkom
reviewed Details | Review

Description Tristan Van Berkom 2008-04-11 06:33:45 UTC
Attaching a first try patch for this.
Comment 1 Tristan Van Berkom 2008-04-11 06:40:54 UTC
Created attachment 109040 [details] [review]
pango attributes parsing in GtkLabel

As per ancient bug 97061, editing labels in glade would be much
more powerful if attributes were handled separately from label
text, its also inconvenient for translators to have pango markup
in their strings.

This patch parses PangoAttributes onto GtkLabel in the
following form:
  <object class="GtkLabel" name="foo_label">
    <pango>
      <attribute name="weight" value="PANGO_WEIGHT_BOLD"/>
    </pango>
  </object>

The patch also includes tests to buildertest.c.
Comment 2 Tristan Van Berkom 2008-04-11 07:30:06 UTC
Note, just got a better grip on how pango attributes work

TODO: implement byte ranges:
  <attribute name="weight" value="PANGO_WEIGHT_BOLD" start="0" end="6"/>

would span the entire "weight" text.
Comment 3 Johan (not receiving bugmail) Dahlin 2008-04-11 11:42:30 UTC
Comment on attachment 109040 [details] [review]
pango attributes parsing in GtkLabel

Thanks, this looks pretty good!
Just a couple of minor comments:

>Index: gtk/gtklabel.c

[..]
>+pango_start_element (GMarkupParseContext *context,
[..]
>+	      g_set_error (error,
>+			   GTK_BUILDER_ERROR,
>+			   GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
>+			   "%s:%d:%d '%s' is not a valid attribute of <%s>",
>+			   "<input>",
>+			   line_number, char_number, names[i], "attribute");
[..]
>+	  g_set_error (error,
>+		       GTK_BUILDER_ERROR,
>+		       GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
>+		       "%s:%d:%d <%s> requires attribute \"%s\"",
>+		       "<input>",
>+		       line_number, char_number, "attribute",
>+		       name ? "value" : "name");

These two errors are not tested, can they be?

>+gtk_label_buildable_custom_tag_start (GtkBuildable     *buildable,
[..]
>+  if (strcmp (tagname, "pango") == 0)

I think "attributes" would make more sense here, don't you?
This is a GtkLabel specific tag which provides something which only makes sense in a label.

>+test_pango_attributes (void)
>+{
[..]
>+  g_assert (found.weight);
>+  g_assert (found.foreground);
>+  g_assert (found.underline);
>+  g_assert (found.size);
>+  g_assert (found.language);
>+  g_assert (found.font_desc);

Freeing attributes manually should not be required by the callsite, it should be enough to unref the label.
Comment 4 Tristan Van Berkom 2008-04-11 16:09:54 UTC
Created attachment 109063 [details] [review]
New patch with start and end properties implemented and fixed tests

Ok Im refitting this patch a little bit... about the tests, well
the call really doesnt ever need to call gtk_label_get_attributes()
in practice, its the only way I figured how to test that they were
actually applied to the label.

Im kindof indifferent about the name, changing it to <attributes> for
this patch, I thought, like <accessibility> tag, it would add context
that the <attribute> tags were in fact <pango><attribute/></pango>s ;-)

So by the way this updated patch also includes and tests start/end
properties, so that you can set properties to portions of your text.

I imagine we may want to replicate this code in textbuffer or such,
maybe at that point we can make attribute_from_text () privately 
shared.
Comment 5 Johan (not receiving bugmail) Dahlin 2008-04-11 16:19:02 UTC
(In reply to comment #4)
> Created an attachment (id=109063) [edit]
> New patch with start and end properties implemented and fixed tests
> 
> Ok Im refitting this patch a little bit... about the tests, well
> the call really doesnt ever need to call gtk_label_get_attributes()
> in practice, its the only way I figured how to test that they were
> actually applied to the label.

Oh, I didn't look carefully enough. Forgot that.

> I imagine we may want to replicate this code in textbuffer or such,
> maybe at that point we can make attribute_from_text () privately 
> shared.

Sounds good.

I guess this can go in, but Mattias would appreciate if you got in some documentation, now when we have all other tags properly documented.

and I'd like to see parser/GError tests, to make sure that the error paths actually work, I tried quickly to do the same in another custom tag implementation without luck.

Comment 6 Tristan Van Berkom 2008-04-11 16:58:32 UTC
this patch included:

  g_assert (error);

in buildertest.c to assure that builder would error out
on faulty xml... is that good enough for a test ?

So then I take it I can commit this and move on to document
the added custom tags. I'll open other bugs for parsing
attributes onto other widgets.
Comment 7 Marc-Andre Lureau 2008-04-15 22:07:18 UTC
Hi Tristan!

Is is it only editable via UI file ATM?
I can't see any option in GtkLabel properties pane.

Thanks a lot :)
Comment 8 Tristan Van Berkom 2008-04-16 02:01:20 UTC
Yes I am going to do that somewhere abouts this week,
a dialog on the GtkLabel properties that allows you
to specify global attributes, or attributes that apply
to specific parts of the set label text.
Comment 9 Marc-Andre Lureau 2008-04-16 08:33:32 UTC
Thanks Tristan, you are doing a great work! ;)