Cell background of a liststore treeview?

Hi,

I need a treeview expert to set a background color of a particular
cell of a liststore,
here the code i extracted from my project.
The goal is to set only the cell corresponding to severity “high” to
background color red, as the data are dynamically added to the
liststore using the append method which is using the same cell
renderer all the column is set to red ;(

Any idea ?

thanks,
david

#######################################################################################

#!/usr/bin/ruby

require ‘gtk2’
require ‘gnomecanvas2’

Gtk.init
w = Gtk::Window.new

ls = Gtk::ListStore.new(Integer, String, Integer, String, String)
treeview = Gtk::TreeView.new(ls)
w.add treeview

column1 = Gtk::TreeViewColumn.new(“Selected”,
Gtk::CellRendererToggle.new, {:active => 0})
column2 = Gtk::TreeViewColumn.new(“Protocol”,
Gtk::CellRendererText.new, {:text => 1})
column3 = Gtk::TreeViewColumn.new(“Port”, Gtk::CellRendererText.new,
{:text => 2})
column4 = Gtk::TreeViewColumn.new(“Service”,
Gtk::CellRendererText.new, {:text => 3})
column5 = Gtk::TreeViewColumn.new(“Severity”,
Gtk::CellRendererText.new, {:text => 4})

treeview.append_column(column1)
treeview.append_column(column2)
treeview.append_column(column3)
treeview.append_column(column4)
treeview.append_column(column5)
treeview.show

array = [[1, “tcp”,80,“http”,“high”], [1, “udp”,53,“dns”,“medium”]]
array.each {|cb, proto, port, service, sev|
iter = treeview.model.append
iter[0] = cb
iter[1] = proto
iter[2] = port
iter[3] = service
iter[4] = sev
if sev == “high”
sev_column = treeview.get_column(4)
cells = sev_column.cell_renderers
puts cells
if cells.size > 0
sev_column.cell_renderers[0].set_cell_background_gdk(Gdk::Color.new(64515,
0, 0))
end
end
}

w.show_all
Gtk.main

Hi:

The method you need to use is:

Gtk::TreeViewColumn#set_cell_data_func

http://ruby-gnome2.sourceforge.jp/hiki.cgi?Gtk%3A%3ATreeViewColumn#set_cell_data_func

However, there’s a much easier way to accomplish this using visualruby.
Using visualruby’s listview object, you can load any type of object into
a listview, then define a method called “visual_attributes” to define
how you want the object to appear:

class Server
def initialize(cb, proto, port)
@cb = cb
@proto = proto
@port = port
end

def visual_attributes
hash = { :text => @cb.to_s }
hash[:background] = (@port < 0) ? “red” : “white”
return hash
end
end

Later you create a listview like this:

@list_view = VR::Listview.new(:cb => String, :serv => Server)

The Server objects will all appear with the text set to @cb,
and the background will be red for any port that’s less than zero.

For more look at the Listview Tutorials on visualruby.net.

Yours,
Eric C

On Sun, Nov 25, 2012 at 12:39 AM, Eric C.
[email protected] wrote:

Using visualruby’s listview object, you can load any type of object into
def visual_attributes
The Server objects will all appear with the text set to @cb,
and the background will be red for any port that’s less than zero.

For more look at the Listview Tutorials on visualruby.net.

Thx Eric, i will have a look and try.

regards,
david

Hi David:

Look at the example program, “active_record2” it has cells with
different colors in the same column.

On Sun, Nov 25, 2012 at 3:14 AM, Eric C.
[email protected] wrote:

Hi David:

Look at the example program, “active_record2” it has cells with
different colors in the same column.

Thx Eric, i made it works.

regards,
david

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs