# Gtk: Writing a CellRenderer in Ruby

**URL:** <https://www.ruby-forum.com/t/gtk-writing-a-cellrenderer-in-ruby/72432>\
**Category:** Ruby\
**Created:** [October 17, 2006, 7:11am UTC](https://www.ruby-forum.com/t/gtk-writing-a-cellrenderer-in-ruby/72432 "2006-10-17T07:11:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Clifford\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/49beb7/32.png) [@Clifford\_H](https://www.ruby-forum.com/u/Clifford_H)\
**Post date:** [October 17, 2006, 7:11am UTC](https://www.ruby-forum.com/t/gtk-writing-a-cellrenderer-in-ruby/72432/1 "2006-10-17T07:11:08Z")

</div>

(Repost from the ruby-gnome2 developers list, where there seems  
to be no-one home)

Folk,

I’ve subclassed the CellRendererPixbuf to make a renderer that  
has a “state” property that displays an appropriate icon for each  
state, and I want to add mouse button event handlers to it.

Problem is, there doesn’t seem to be any way to get the TreeView  
object which is the necessary Widget. In the C API, the \_GtkTreeView  
object (in gtktreeview.h), there’s a “parent” member, but no supported  
way of accessing it. What am I missing? Is there some standard Gtk  
method or property for getting the parent of a Gtk::Object? If not,  
why not?

The code, such as it is, is below. The images for are  
simply loaded from \<state.png\>.

Clifford H…

module Gtk  
class CellRendererIconSet \< CellRendererPixbuf  
type\_register  
install\_property(GLib::Param::String.new(  
“state”,  
“state”,  
“The state of an IconSet”,  
“”,  
GLib::Param::READABLE|GLib::Param::WRITABLE))

```
    def initialize(*args)
        super()
        @state = nil
        @states = {}
        if (args.size > 0)
            states = args[0]
            if (states.is_a? Hash)
                @states = states
            elsif (states.is_a? Array)
                states.each{|s| @states[s] = nil }
            end
            # More handling needed here...
        end
        # and here...
    end

    attr_reader :state

    def state=(s)
        throw "CellRendererIconSet: no such state #{s.inspect} in

```

#{@states}" if !@states.include?(s)  
@state = s  
self.pixbuf=(icon)  
end

```
    def icon(s = nil)
        s ||= @state
        puts "loading icon for #{s}" if (!@states[s])
	# Needs a directory path from which to load here:
        @states[s] ||= Gdk::Pixbuf.new(s.to_s.downcase+".png")
    end
end

```

end

I want to move something like the following code into the class:

```
    treeview.add_events(Gdk::Event::BUTTON_PRESS_MASK)
    treeview.signal_connect("button_press_event") { |w, e|
        path, column, cell_x, cell_y = treeview.get_path_at_pos(e.x,

```

e.y)

```
        puts "button #{e.button} at (#{e.x}, #{e.y}) row #{path},

```

column #{column}, at (#{cell\_x}, #{cell\_y})"

```
        next if column != self
	on_click(path, e.button, e.state)
    }

```

… so I can then write (or similar with signals):

```
def on_click(path, button, state)
      next if e.button != 3

        # Get current state and calculate next state:
        storeRow = store[path]
        state = storeRow[Column::IconState]
        newstate = States[(States.index(state)+1) % States.size]
        storeRow[Column::IconState] = newstate
	true
  end
```

---

<div class="post-metadata">

**Author:** ![Clifford\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/49beb7/32.png) [@Clifford\_H](https://www.ruby-forum.com/u/Clifford_H)\
**Post date:** [October 20, 2006, 2:26am UTC](https://www.ruby-forum.com/t/gtk-writing-a-cellrenderer-in-ruby/72432/2 "2006-10-20T02:26:15Z")

</div>

Can it really be true that no-one else has written a CellRenderer for  
Ruby/GTK?

Here’s an updated description of the problem, with code that works.  
Comments identify the two problem areas I’m trying to get answers to.

My earlier code contained this line:  
tree.signal\_connect(“button\_press\_event”) { |w, e|

The problem is that inside CellRendererIconSet, it’s not possible  
to get the “tree” object in order to call signal\_connect in the  
first place. I have to pass the tree widget to the new renderer  
in a separate call, as in the following revised version. It looks  
pretty clean, but the extra “set\_tree” call is bad - it should  
be possible to do this in the constructor without having to pass  
an additional argument (which isn’t what the attached code does).

It seems to me that when the CellRenderer is associated with a  
Column, it should receive a callback telling it the column object  
and the tree object, so it can attach to the tree’s widget then.

The further problem is that when an event does occur, I can find  
out what column it’s on, but can’t disregard it if it occurred on  
another column, because I can’t tell what column the renderer is  
on. That logic must go in the “click” handler, which is bad.

It’s annoying that this is _almost nice_, but I can’t see how to  
properly finish it.

module Gtk  
class CellRendererIconSet \< CellRendererPixbuf  
type\_register  
install\_property(GLib::Param::String.new(  
“state”,  
“state”,  
“The state of an IconSet”,  
“”,  
GLib::Param::READABLE|GLib::Param::WRITABLE))

```
    def initialize(*args)
        super()
        @state = nil
        @states = {}
        if (args.size > 0)
            states = args[0]
            if (states.is_a? Hash)
                @states = states
            elsif (states.is_a? Array)
                states.each{|s| @states[s] = nil }
            end
        end
    end

# Register events for this Renderer:
    signal_new("button_press_event", GLib::Signal::RUN_FIRST, nil, 

```

nil,  
Gdk::EventButton, Gtk::TreePath, Gtk::TreeViewColumn,  
Integer, Integer)

```
    signal_new("button_release_event", GLib::Signal::RUN_FIRST, nil, 

```

nil,  
Gdk::EventButton, Gtk::TreePath, Gtk::TreeViewColumn,  
Integer, Integer)

```
    signal_new("click", GLib::Signal::RUN_FIRST, nil, nil,
            Gdk::EventButton, Gtk::TreePath, Gtk::TreeViewColumn,
            Integer, Integer)

    def signal_do_button_press_event(event, path, column, cell_x, 

```

cell\_y)  
# puts “press event #{event.inspect}, path #{path.inspect}”  
end

```
    def signal_do_button_release_event(event, path, column, cell_x, 

```

cell\_y)  
# puts “release event #{event.inspect}, path  
#{path.inspect}”  
end

```
    def signal_do_click(event, path, column, cell_x, cell_y)
        # puts "click event #{event.inspect}, path #{path.inspect}"
    end

    def tree=(tree)
        tree.add_events(Gdk::Event::BUTTON_PRESS_MASK|Gdk::Event::BUTTON_RELEASE_MASK)
        armed_column = nil
        tree.signal_connect("button_press_event") { |w, e|
            path, column, cell_x, cell_y = tree.get_path_at_pos(e.x, 

```

e.y)  
#puts “press #{e.button} at (#{e.x}, #{e.y}) row  
#{path}, column #{column}, at (#{cell\_x}, #{cell\_y})”

```
            armed_column = column
            signal_emit("button_press_event", e, path, column, 

```

cell\_x, cell\_y)  
}  
tree.signal\_connect(“button\_release\_event”) { |w, e|  
path, column, cell\_x, cell\_y = tree.get\_path\_at\_pos(e.x,  
e.y)  
#puts “release #{e.button} at (#{e.x}, #{e.y}) row  
#{path}, column #{column}, at (#{cell\_x}, #{cell\_y})”  
cell\_x ||= -1 # Can’t be null  
cell\_y ||= -1  
signal\_emit(“button\_release\_event”, e, path, column,  
cell\_x, cell\_y)

```
            if (column == armed_column)
                signal_emit("click", e, path, column, cell_x, 

```

cell\_y)  
end  
armed\_column = nil  
}  
end

```
    def state
        @state
    end

    def state=(s)
        throw "CellRendererIconSet: no such state #{s.inspect} in 

```

#{@states}" if !@states.include?(s)  
@state = s  
self.pixbuf=(icon)  
end

```
    def icon(s = nil)
        s ||= @state
        puts "loading icon for #{s}" if (!@states[s])
        @states[s] ||= Gdk::Pixbuf.new(s.to_s.downcase+".png")
    end
end

```

end

Then in the tree construction code, I put:

```
    tree.append_column(
        @iconColumn =
        Gtk::TreeViewColumn.new(
            "", # Heading
            r = Gtk::CellRendererIconSet.new(States),
            :state => Column::ProjectState # data
        )
    )
    r.tree = tree # This call should be unnecessary!

```

And finally, in order to handle the click event, I add:

```
    r.signal_connect("click") { |r, e, path, column, cell_x, cell_y|
        next if column != @iconColumn	# This should be unnecessary!
        next if e.button != 3 # ignore if not right-click

    # Handle the click by choosing the next icon in the States array:
        storeRow = store[path]
        state = storeRow[Column::ProjectState]
        newstate = States[(States.index(state)+1) % States.size]
        puts state+" -> "+newstate
        storeRow[Column::ProjectState] = newstate
    }

```

Does that make it clearer?

Clifford H…
