Monkeypatch to CellRendererObject doesn't work

I want a CellRendererObject to be able to take pango text as an
alternative to to_s, if my cell class implements to_pango().

I put this in my program (at the top of my “main window” file):

class CellRendererObject < Gtk::CellRendererText
def render_object(iter) # MONKEY-PATCH!!!
obj = iter[@model_col]
if obj.respond_to?(:to_pango)
self.set_markup(obj.to_pango)
return
end
self.text = obj.respond_to?(:to_s) ? obj.to_s : “Define to_s!”
if obj.respond_to? @visual_attributes_method
return unless attrib = obj.send(@visual_attributes_method)
attrib.each_pair do | key, val |
self.send( key.to_s + “=”, val)
end
end
end
end

I use breakpoints and verify that it’s being executed after
CellRendererObject.rb is loaded.

But when it’s time to display the tree, the render_object() in
CellRendererObject.rb is called, and my version is not.

Am I doing the monkeypatch wrong somehow? Is there something unusual
about VR’s control flow?

Thanks…

I think what you’re trying to do is really easy in visualruby.

When you initialize a listview, just make one of the columns your own
type of object:

@lv = VR::ListView.new(:name => String, :mything => MyClass)

Then define your MyClass:

class MyClass < SomePangoTypeClass # I don’t know about this class

def to_s()
return self.to_pango()
end

end

Whatever the to_s method returns will be shown on the ListView.

Tutorial here:

http://visualruby.net/site/ListView%20Tutorial.html

Is that what you’re trying to do?

Eric

If you mean
http://visualruby.net/site/ListView%20Tutorial.html#cell_appearance

then that is not powerful enough for what I’m trying to do. I don’t want
to just set the text and set the cell’s visual attributes. I want to
change attributes within the string - have parts of it highlighted red
and green, for example.

The CellRendererObject puts the result of to_s() into self.text= . I
need it to go into self.set_markup() . Otherwise, inline won’t be
parsed.

If I edit the CellRendererObject.rb file in my local install, plugging
in the code I put in my initial post, then it works just fine. The text
shows up with red and green stripes just like I wanted.

I just need to know how to monkeypatch it so I don’t have to edit the
file in everyone’s install. Can you spot what I’m doing wrong?

Thanks,
Chris

Yea, you’re right it doesn’t do that. That would be cool if you could
make that work. Let me know if you get it to work.

Eric

The file I’m interested in is
C:\Ruby193\lib\ruby\gems\1.9.1\gems\vrlib-1.0.16\lib\treeview\columns\CellRendererObject.rb

  • I think that’s a VR file, not a Gtk file. The CellRendererObject does
    inherit from Gtk::CellRendererText. But the file is in vrlib.

The method I’m trying to replace is the method that calls my object’s
to_s, and passes its text into the text= method. I want to pass pango
text into the set_markup method. I found that method by putting a
breakpoint in to_s and looking at the top of the call stack.

At any rate, I’m trying to monkeypatch, not subclass. I don’t directly
get access to the CellRendererObject, do I? I mean, I don’t see how I
can subclass anything, because with one exception, my code never
allocates anything except my own classes, which don’t inherit from
anything.

I’m calling add_row on the VR::TreeView to add rows to my tree view. If
there’s something I can subclass to pass pango text into set_markup, let
me know, but I doubt there is.

In which case I really do need to monkey-patch the
vrlib/…/CellRendererObject.rb file, and it’s not working.

Chris

I found the problem. I hadn’t noticed that the
class CellRendererObject < Gtk::CellRendererText

is defined inside of
module VR

So if I include the following in my code, then I can use Pango markup
inside VR ListViews and TreeViews.

module VR
class CellRendererObject < Gtk::CellRendererText
def render_object(iter) # MONKEY-PATCH!!!
obj = iter[@model_col]
if obj.respond_to?(:to_pango)
self.set_markup(obj.to_pango)
return
end
self.text = obj.respond_to?(:to_s) ? obj.to_s : “Define to_s!”
if obj.respond_to? @visual_attributes_method
return unless attrib = obj.send(@visual_attributes_method)
attrib.each_pair do | key, val |
self.send( key.to_s + “=”, val)
end
end
end
end
end

Shouldn’t you be subclassing VR::CellRendererObject not
Gtk::CellRendererObject?

Eric

I have a question:

What markup language is used in this statement?

self.set_markup(obj.to_pango)

Is it like html? Where can I read about the markup tags etc?

It seems to me that setting the text attribute is obsolete. Your way of
doing it looks better to me. Why not just make everything the markup
language and eliminate text? After all the text would still render the
same anyway. Right?

Eric

Cool. I didn’t know that you could play with text like that in a cell.

Pango will treat some characters as special, like <. People who don’t
want the inline formatting would not want to have to gsub(’<’,’<) on
everything. (There may be other problematic characters as well.) The way
I did it, if they don’t implement to_pango it still works the old way.

I also did a hack to pass in a format-specifier variable, so the same
object can display two different ways in two different ListViews - if
you’re interested. I could have subclassed instead, I guess.

Nevermind I found it. I’ll work on getting it into visualruby.