-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathgtk-tree-view.js
103 lines (70 loc) · 2.19 KB
/
gtk-tree-view.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
* gtk-tree-view.js
*/
const gi = require('../lib')
const Gtk = gi.require('Gtk', '3.0')
const GObject = gi.require('GObject')
gi.startLoop()
Gtk.init()
// Types: https://github.com/romgrk/node-gtk/blob/master/lib/overrides/GObject.js#L12-L35
// Functions: https://developer.gnome.org/gobject/stable/gobject-Standard-Parameter-and-Value-Types.html
const TYPE_FN = {
[GObject.TYPE_STRING]: GObject.Value.prototype.setString,
[GObject.TYPE_FLOAT]: GObject.Value.prototype.setFloat,
}
function appendRow(store, row, types) {
const iter = store.append()
for (let i = 0; i < row.length; i++) {
const item = row[i]
const type = types[i]
const typeFn = TYPE_FN[type]
const value = new GObject.Value()
value.init(type)
typeFn.call(value, item)
store.setValue(iter, i, value)
}
}
// Model
const types = [GObject.TYPE_STRING, GObject.TYPE_STRING, GObject.TYPE_FLOAT]
const books = [
['L\'étranger', 'Albert Camus', 10.76],
['L\'élégance du Hérisson', 'Muriel Barbery', 25.94],
['Le Vieux qui lisait des romans d\'amour', 'Luis Sepulveda', 10.76],
]
const store = new Gtk.ListStore()
store.setColumnTypes(types)
books.forEach(book => appendRow(store, book, types))
// View
const treeView = new Gtk.TreeView({ model: store })
{
const column = new Gtk.TreeViewColumn({ title: 'Title and Author' })
const title = new Gtk.CellRendererText()
const author = new Gtk.CellRendererText()
column.packStart(title, true)
column.packStart(author, true)
column.addAttribute(title, 'text', 0)
column.addAttribute(author, 'text', 1)
treeView.appendColumn(column)
}
{
const price = new Gtk.CellRendererText()
const column = new Gtk.TreeViewColumn({ title: 'Price ($CAN)' })
column.packStart(price, true)
column.addAttribute(price, 'text', 2)
treeView.appendColumn(column)
}
// configure main window
const window = new Gtk.Window({ type : Gtk.WindowType.TOPLEVEL })
window.setDefaultSize(500, 300)
window.setResizable(true)
window.add(treeView)
window.on('show', Gtk.main)
window.on('destroy', Gtk.mainQuit)
window.on('delete-event', () => false)
/*
* Main
*/
main()
function main() {
window.showAll()
}