Use pango markup instead of attributes

Attributes don't work from introspection, so this blocks porting to gtk3.

In MessageColumn, admit that multiple highlighters don't actually work.
This commit is contained in:
René Stadler 2012-09-23 17:22:53 +02:00 committed by Stefan Sauer
parent 634f17ed7d
commit 66ed3bb258
3 changed files with 40 additions and 31 deletions

View file

@ -24,6 +24,7 @@ def _ (s):
import logging import logging
import glib
import gtk import gtk
from GstDebugViewer import Common, Data from GstDebugViewer import Common, Data
@ -328,30 +329,38 @@ class MessageColumn (TextColumn):
def get_row_data_func (self): def get_row_data_func (self):
from pango import AttrList, AttrBackground, AttrForeground
highlighters = self.highlighters highlighters = self.highlighters
id_ = self.id id_ = self.id
# FIXME: This should be none; need to investigate
# `cellrenderertext.props.attributes = None' failure (param conversion
# error like `treeview.props.model = None').
no_attrs = AttrList ()
def message_data_func (props, row): def message_data_func (props, row):
props.text = row[id_] msg = row[id_]
if not highlighters: if not highlighters:
props.attributes = no_attrs props.text = msg
for highlighter in highlighters.values (): return
ranges = highlighter (row)
if not ranges: if len (highlighters) > 1:
props.attributes = no_attrs raise NotImplementedError ("FIXME: Support more than one...")
else:
attrlist = AttrList () highlighter = highlighters.values ()[0]
for start, end in ranges: ranges = highlighter (row)
attrlist.insert (AttrBackground (0, 0, 65535, start, end)) if not ranges:
attrlist.insert (AttrForeground (65535, 65535, 65535, start, end)) props.text = msg
props.attributes = attrlist else:
tags = []
prev_end = 0
end = None
for start, end in ranges:
if prev_end < start:
tags.append (glib.markup_escape_text (msg[prev_end:start]))
msg_escape = glib.markup_escape_text (msg[start:end])
tags.append ("<span foreground=\'#FFFFFF\'"
" background=\'#0000FF\'>%s</span>" % (msg_escape,))
prev_end = end
if end is not None:
tags.append (glib.markup_escape_text (msg[end:]))
props.markup = "".join (tags)
return message_data_func return message_data_func

View file

@ -28,7 +28,7 @@ import os.path
from bisect import bisect_right, bisect_left from bisect import bisect_right, bisect_left
import logging import logging
import pango import glib
import gobject import gobject
import gtk import gtk
@ -846,11 +846,11 @@ class Window (object):
bar.props.message_type = gtk.MESSAGE_ERROR bar.props.message_type = gtk.MESSAGE_ERROR
box = bar.get_content_area () box = bar.get_content_area ()
attrs = pango.AttrList () markup = "<b>%s</b> %s" % (glib.markup_escape_text (message1),
attrs.insert (pango.AttrWeight (pango.WEIGHT_BOLD, 0, len (message1))) glib.markup_escape_text (message2),)
label = gtk.Label () label = gtk.Label ()
label.props.label = "%s %s" % (message1, message2) label.props.use_markup = True
label.props.attributes = attrs label.props.label = markup
label.props.selectable = True label.props.selectable = True
box.pack_start (label, False, False, 0) box.pack_start (label, False, False, 0)

View file

@ -24,7 +24,7 @@ import logging
from GstDebugViewer import Common, Data, GUI from GstDebugViewer import Common, Data, GUI
from GstDebugViewer.Plugins import * from GstDebugViewer.Plugins import *
import pango import glib
import gtk import gtk
class SearchOperation (object): class SearchOperation (object):
@ -152,11 +152,9 @@ class FindBarWidget (gtk.HBox):
next_action.connect_proxy (next_button) next_action.connect_proxy (next_button)
self.pack_start (next_button, False, False, 0) self.pack_start (next_button, False, False, 0)
self.status_label = gtk.Label ("") self.status_label = gtk.Label ()
self.status_label.props.xalign = 0. self.status_label.props.xalign = 0.
attrs = pango.AttrList () self.status_label.props.use_markup = True
attrs.insert (pango.AttrWeight (pango.WEIGHT_BOLD, 0, -1))
self.status_label.props.attributes = attrs
self.pack_start (self.status_label, False, False, 6) self.pack_start (self.status_label, False, False, 6)
self.__compute_status_size () self.__compute_status_size ()
self.status_label.connect ("notify::style", self.__handle_notify_style) self.status_label.connect ("notify::style", self.__handle_notify_style)
@ -166,7 +164,7 @@ class FindBarWidget (gtk.HBox):
def __compute_status_size (self): def __compute_status_size (self):
label = self.status_label label = self.status_label
old_text = label.props.label old_markup = label.props.label
label.set_size_request (-1, -1) label.set_size_request (-1, -1)
max_width = 0 max_width = 0
try: try:
@ -176,7 +174,7 @@ class FindBarWidget (gtk.HBox):
max_width = max (max_width, width) max_width = max (max_width, width)
label.set_size_request (max_width, -1) label.set_size_request (max_width, -1)
finally: finally:
label.props.label = old_text label.props.label = old_markup
def __handle_notify_style (self, *a, **kw): def __handle_notify_style (self, *a, **kw):
@ -184,7 +182,9 @@ class FindBarWidget (gtk.HBox):
def __set_status (self, text): def __set_status (self, text):
self.status_label.props.label = text markup = "<b>%s</b>" % (glib.markup_escape_text (text),)
self.status_label.props.label = markup
def status_no_match_found (self): def status_no_match_found (self):