# Monkeypatch to CellRendererObject doesn't work

**URL:** <https://www.ruby-forum.com/t/monkeypatch-to-cellrendererobject-doesnt-work/240383>\
**Category:** Ruby-Gnome 2\
**Created:** [April 11, 2014, 12:17am UTC](https://www.ruby-forum.com/t/monkeypatch-to-cellrendererobject-doesnt-work/240383 "2014-04-11T00:17:54Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Detlef\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@Detlef\_R](https://www.ruby-forum.com/u/Detlef_R)\
**Post date:** [April 11, 2014, 12:17am UTC](https://www.ruby-forum.com/t/monkeypatch-to-cellrendererobject-doesnt-work/240383/1 "2014-04-11T00:17:54Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![mahadewa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@mahadewa](https://www.ruby-forum.com/u/mahadewa)\
**Post date:** [April 11, 2014, 12:45am UTC](https://www.ruby-forum.com/t/monkeypatch-to-cellrendererobject-doesnt-work/240383/2 "2014-04-11T00:45:05Z")

</div>

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](http://visualruby.net/site/ListView%20Tutorial.html)

Is that what you’re trying to do?

Eric

---

<div class="post-metadata">

**Author:** ![mahadewa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@mahadewa](https://www.ruby-forum.com/u/mahadewa)\
**Post date:** [April 11, 2014, 2:18am UTC](https://www.ruby-forum.com/t/monkeypatch-to-cellrendererobject-doesnt-work/240383/3 "2014-04-11T02:18:55Z")

</div>

If you mean  
[http://visualruby.net/site/ListView%20Tutorial.html#cell\_appearance](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.

---

<div class="post-metadata">

**Author:** ![mahadewa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@mahadewa](https://www.ruby-forum.com/u/mahadewa)\
**Post date:** [April 11, 2014, 3:33am UTC](https://www.ruby-forum.com/t/monkeypatch-to-cellrendererobject-doesnt-work/240383/4 "2014-04-11T03:33:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mahadewa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@mahadewa](https://www.ruby-forum.com/u/mahadewa)\
**Post date:** [April 11, 2014, 3:09am UTC](https://www.ruby-forum.com/t/monkeypatch-to-cellrendererobject-doesnt-work/240383/5 "2014-04-11T03:09:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mahadewa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@mahadewa](https://www.ruby-forum.com/u/mahadewa)\
**Post date:** [April 11, 2014, 7:51am UTC](https://www.ruby-forum.com/t/monkeypatch-to-cellrendererobject-doesnt-work/240383/6 "2014-04-11T07:51:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mahadewa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@mahadewa](https://www.ruby-forum.com/u/mahadewa)\
**Post date:** [April 14, 2014, 9:08pm UTC](https://www.ruby-forum.com/t/monkeypatch-to-cellrendererobject-doesnt-work/240383/7 "2014-04-14T21:08:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mahadewa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@mahadewa](https://www.ruby-forum.com/u/mahadewa)\
**Post date:** [April 11, 2014, 5:25am UTC](https://www.ruby-forum.com/t/monkeypatch-to-cellrendererobject-doesnt-work/240383/8 "2014-04-11T05:25:58Z")

</div>

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

Eric

---

<div class="post-metadata">

**Author:** ![mahadewa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@mahadewa](https://www.ruby-forum.com/u/mahadewa)\
**Post date:** [April 18, 2014, 8:55pm UTC](https://www.ruby-forum.com/t/monkeypatch-to-cellrendererobject-doesnt-work/240383/9 "2014-04-18T20:55:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mahadewa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@mahadewa](https://www.ruby-forum.com/u/mahadewa)\
**Post date:** [April 15, 2014, 3:01am UTC](https://www.ruby-forum.com/t/monkeypatch-to-cellrendererobject-doesnt-work/240383/10 "2014-04-15T03:01:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mahadewa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@mahadewa](https://www.ruby-forum.com/u/mahadewa)\
**Post date:** [April 18, 2014, 11:19pm UTC](https://www.ruby-forum.com/t/monkeypatch-to-cellrendererobject-doesnt-work/240383/11 "2014-04-18T23:19:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mahadewa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@mahadewa](https://www.ruby-forum.com/u/mahadewa)\
**Post date:** [April 18, 2014, 10:21pm UTC](https://www.ruby-forum.com/t/monkeypatch-to-cellrendererobject-doesnt-work/240383/12 "2014-04-18T22:21:01Z")

</div>

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