On 01/29/2017 12:34 PM, Erdem wrote:
I'd like to change color of each cell according to cell name.
Here is what I've done so far.
import gtk.Main; import gtk.MainWindow; import gtk.Box; import gtk.TreeView; import gtk.TreeViewColumn; import gtk.CellRendererText; import gtk.ListStore; import gtk.TreeModel; import gtk.TreeIter; import gtk.CellRenderer; import std.stdio; struct Color { string name; string description; } class MyWindow: MainWindow { this() { super("TreeView example"); setSizeRequest(500, 200); Color[] colors; colors ~= Color("Green", "This should be green"); colors ~= Color("Red", "This should be red"); colors ~= Color("Blue", "This should be blue"); auto tree = new TreeView(); auto dataModel = new ListStore([GType.STRING, GType.STRING]); auto nameColumn = new TreeViewColumn(); nameColumn.setTitle("Color"); auto colorNameCell = new CellRendererText(); nameColumn.packStart(colorNameCell, true); tree.appendColumn(nameColumn); auto descriptionColumn = new TreeViewColumn(); descriptionColumn.setTitle("Description"); auto colorDescriptionCell = new CellRendererText(); descriptionColumn.packStart(colorDescriptionCell, true); tree.appendColumn(descriptionColumn); auto iter = dataModel.createIter(); foreach (color; colors) { dataModel.setValue(iter, 0, color.name); dataModel.setValue(iter, 1, color.description); dataModel.append(iter); } nameColumn.setCellDataFunc(colorNameCell, cast (GtkTreeCellDataFunc) &renderColor, null, null); tree.setModel(dataModel); add(tree); showAll(); } static extern(C) void renderColor(GtkTreeViewColumn * treeColumn, GtkCellRenderer * cell, GtkTreeModel * gtkModel, GtkTreeIter * iter, void * data) { auto model = new TreeModel(gtkModel, false); auto iterator = new TreeIter(iter, false); auto cellRenderer = new CellRenderer(cell, false); string colorName = model.getValue(iterator, 0).dupString(); writeln(colorName); // cellRenderer.foreground = "red"; } } void main(string[] args) { Main.init(args); auto mywindow = new MyWindow(); Main.run(); }
For example if colorName starts with "r" I'd like to change this cell color to red. If it starts with "g" I'd like to change cell color to green.
I was expecting to find a method similiar to this but
there isn't.cellRenderer.foreground = "red";
You can set the background color and other properties with the setProperty
function.
cellRenderer.setProperty("cell-background", "red");