Markup tags don't seem to work at the beginning of a TreeViewColumn string
(I reported this error on Stack Overflow, and it was recommended that I report the error here. Here's the link to the original post: https://stackoverflow.com/questions/53986019/markup-tags-dont-seem-to-work-at-the-beginning-of-a-treeviewcolumn-string)
I want to be able to use Pango markup in my TreeViewColumn (coding in Python), but markup that starts at the very beginning of the string appears to be ignored, unless I pre-pend a zero-width space before the string and markup.
I noticed that using Pango markup in my TreeViewColumn text worked fine when it was not at the beginning of the text, like making the suffix of a word gray:
'week<span foreground="gray">ly</span>'
But I wanted a bit more contrast, so I decided to make the rest of the string bold, like this:
'<b>week</b><span foreground="gray">ly</span>'
With this string in the TreeViewColumn, the bold doesn't show up as bold, but just as normal. But I knew that the bold tags worked, because I could mark the suffix in bold (instead of gray), and that worked fine. It seemed like it was having problems with markup at the beginning of the string, so I tried pre-pending a zero-width space before the string:
'\u200B<b>week</b><span foreground="gray">ly</span>'
Then the bold and the gray text both appear fine.
Here are a few code snippets, first for creating the ListStore and TreeView:
# create an empty ListStore for words and frequencies
self.WordListStore = Gtk.ListStore(str, int)
# create a TreeView to view the word/freq list and add to ScrolledWindow
self.WordTreeView = Gtk.TreeView(self.WordListStore)
sw.add(self.WordTreeView)
sw.show()
main_hb.pack_start(sw, True, True, 5)
# add the columns to the TreeView, use of markup=0 allows us to put tags like <b> in the entries
column = Gtk.TreeViewColumn("Word", myGlobalRenderer.vernRendererText, markup=0)
column.set_sort_column_id(0)
self.WordTreeView.append_column(column)
column = Gtk.TreeViewColumn("Frequency", myGlobalRenderer.numRendererText, text=1)
column.set_sort_column_id(1)
self.WordTreeView.append_column(column)
And then here is a code snippet for how I populate the ListStore:
# start by clearing the list
listStore.clear()
prefMatch = re.compile('^<b>(' + '|'.join(a[:-1] for a in prefixes) + ')')
suffMatch = re.compile('(' + '|'.join(a[1:] for a in suffixes) + ')</b>$')
# add each of the words and its count
for word in self.words:
marked_word = '<b>' + word + '</b>'
marked_word = prefMatch.sub('<span foreground="gray">\\1</span><b>', marked_word)
marked_word = suffMatch.sub('</b><span foreground="gray">\\1</span>', marked_word)
# put zero-width space in front, or markup doesn't appear
marked_word = '\u200B' + marked_word
listStore.append([marked_word, self.words[word]])
# start with descending count order
# (but user can sort by clicking column headers)
listStore.set_sort_column_id(1, Gtk.SortType.DESCENDING)
I haven't given the details of the variables, but the critical detail is that words that are marked as bold (or have any sort of markup at the beginning of the string) don't display using that initial markup. But if I add the '\u200B' at the beginning of the string, then the markup appears fine.
Is this a bug with Pango markup? I would expect markup at the beginning of the string to appear correctly, just like markup found elsewhere in the string, but that doesn't seem to be the case.