fix for css_colored_listview
This commit is contained in:
parent
8979188f4c
commit
b3d0a4b0fc
3 changed files with 3143 additions and 15 deletions
|
|
@ -1,9 +1,8 @@
|
|||
## gcc -Wall cell_color1.c -o cell_color1 `pkg-config --cflags --libs gtk+-3.0`
|
||||
## https://discourse.gnome.org/t/gtk-treeview-cell-color-change/1750/4
|
||||
## C. Eric Cashon
|
||||
|
||||
# nim c css_colored_listview.nim
|
||||
import gintro/[gtk, gobject]
|
||||
import gintro/[gtk, glib, gobject]
|
||||
import gintro/gdk except Window # there is a problem with gdk.Window -- we have to investigate!
|
||||
const # maybe we should use Nim's enum here?
|
||||
Id = 0
|
||||
|
|
@ -31,6 +30,26 @@ proc toBoolVal(b: bool): Value =
|
|||
discard init(result, gtype)
|
||||
setBoolean(result, b)
|
||||
|
||||
# we need the following two procs for now -- later we will not use that ugly cast...
|
||||
proc typeTest(o: gobject.Object; s: string): bool =
|
||||
let gt = g_type_from_name(s)
|
||||
return g_type_check_instance_is_a(cast[ptr TypeInstance00](o.impl), gt).toBool
|
||||
|
||||
proc listStore(o: gobject.Object): gtk.ListStore =
|
||||
assert(typeTest(o, "GtkListStore"))
|
||||
cast[gtk.ListStore](o)
|
||||
|
||||
proc updateRow(renderer: CellRendererText; path: cstring; newText: cstring; tree: TreeView) =
|
||||
var iter: TreeIter
|
||||
var value: Value
|
||||
let gtype = typeFromName("gchararray")
|
||||
discard init(value, gtype)
|
||||
let store = listStore(tree.getModel())
|
||||
value.setString(newText)
|
||||
let treePath = newTreePathFromString(path)
|
||||
discard store.getIter(iter, treePath)
|
||||
store.setValue(iter, 1, value)
|
||||
|
||||
# we use the old gtk style with init() as is used in the C original -- maybe better use modern app sytle
|
||||
proc main() =
|
||||
gtk.init()
|
||||
|
|
@ -41,8 +60,9 @@ proc main() =
|
|||
window.borderWidth = 20
|
||||
connect(window, "destroy", bye)
|
||||
var iter: TreeIter
|
||||
var h = [typeFromName("guint"), typeFromName("gchararray"), typeFromName("gchararray"), typeFromName("gchararray")]
|
||||
var store = newListStore(Columns, cast[pointer]( unsafeaddr h)) # this cast is ugly, we should fix it in the bindings soon.
|
||||
var h = [typeFromName("guint"), typeFromName("gchararray"), typeFromName("gchararray"),
|
||||
typeFromName("gchararray")]
|
||||
var store = newListStore(Columns, cast[pointer]( unsafeaddr h)) # cast is ugly, we should fix it in bindings.
|
||||
let progNames = ["Gedit", "Gimp", "Inkscape", "Firefox", "Calculator", "Devhelp"]
|
||||
for i, n in progNames:
|
||||
store.append(iter) # currently we have to use setValue() as there is no varargs proc as in C original
|
||||
|
|
@ -60,6 +80,7 @@ proc main() =
|
|||
setProperty(renderer1, "editable", toBoolVal(false))
|
||||
var renderer2 = newCellRendererText()
|
||||
setProperty(renderer2, "editable", toBoolVal(true))
|
||||
connect(renderer2, "edited", updateRow, tree)
|
||||
## Bind the Color column to the "cell-background" property.
|
||||
var column1 = newTreeViewColumn()
|
||||
column1.setTitle("ID")
|
||||
|
|
@ -73,16 +94,16 @@ proc main() =
|
|||
column1.addAttribute(renderer2, "text", Program)
|
||||
column1.addAttribute(renderer2, "cell-background", Color2)
|
||||
discard tree.appendColumn(column2)
|
||||
var grid = newGrid() # grid with only one occupied cell makes not much sense here -- but so we can add more widgets later
|
||||
var grid = newGrid() # only one occupied cell makes no sense -- but so we can add more widgets later
|
||||
grid.attach(tree, 0, 0, 1, 1)
|
||||
window.add(grid)
|
||||
const cssString = # note: big font selected intentionally
|
||||
"""treeview{background-color: rgba(0,255,255,1.0); font-size:30pt} treeview:selected{background-color: rgba(255,255,0,1.0);
|
||||
color: rgba(0,0,255,1.0);}"""
|
||||
"""treeview{background-color: rgba(0,255,255,1.0); font-size:30pt} treeview:selected{background-color:
|
||||
rgba(255,255,0,1.0); color: rgba(0,0,255,1.0);}"""
|
||||
var provider = newCssProvider()
|
||||
discard provider.loadFromData(cssString)
|
||||
addProviderForScreen(getDefaultScreen(), provider, STYLE_PROVIDER_PRIORITY_APPLICATION)
|
||||
window.showAll
|
||||
gtk.main()
|
||||
|
||||
main()
|
||||
main()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue