File: demo-treeview-editable.lua

package info (click to toggle)
lua-lgi 0.9.2-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,388 kB
  • sloc: ansic: 5,082; makefile: 169; sh: 31
file content (184 lines) | stat: -rw-r--r-- 4,232 bytes parent folder | download | duplicates (7)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
return function(parent, dir)

local lgi = require 'lgi'
local GObject = lgi.GObject
local Gtk = lgi.Gtk

local ItemColumn = {
   NUMBER = 1,
   PRODUCT = 2,
   YUMMY = 3,
}

local NumberColumn = {
   TEXT = 1,
   NUMBER = 2,
}

-- Fill store with initial items.
local item_store = Gtk.ListStore.new {
   [ItemColumn.NUMBER] = GObject.Type.INT,
   [ItemColumn.PRODUCT] = GObject.Type.STRING,
   [ItemColumn.YUMMY] = GObject.Type.INT,
}

for _, item in ipairs {
   {
      [ItemColumn.NUMBER] = 3,
      [ItemColumn.PRODUCT] = "bottles of coke",
      [ItemColumn.YUMMY] = 20,
   },
   {
      [ItemColumn.NUMBER] = 5,
      [ItemColumn.PRODUCT] = "packages of noodles",
      [ItemColumn.YUMMY] = 50,
   },
   {
      [ItemColumn.NUMBER] = 2,
      [ItemColumn.PRODUCT] = "packages of chocolate chip cookies",
      [ItemColumn.YUMMY] = 90,
   },
   {
      [ItemColumn.NUMBER] = 1,
      [ItemColumn.PRODUCT] = "can vanilla ice cream",
      [ItemColumn.YUMMY] = 60,
   },
   {
      [ItemColumn.NUMBER] = 6,
      [ItemColumn.PRODUCT] = "eggs",
      [ItemColumn.YUMMY] = 10,
   },
} do
   item_store:append(item)
end

-- Fill store with numbers.
local number_store = Gtk.ListStore.new {
   [NumberColumn.TEXT] = GObject.Type.INT,
   [NumberColumn.NUMBER] = GObject.Type.STRING,
}

for i = 1, 10 do
   number_store:append {
      [NumberColumn.TEXT] = i,
      [NumberColumn.NUMBER] = i,
   }
end

local window = Gtk.Window {
   title = "Shopping list",
   default_width = 320,
   default_height = 200,
   border_width = 5,
   Gtk.Box {
      orientation = 'VERTICAL',
      spacing = 5,
      Gtk.Label {
	 label = "Shopping list (you can edit the cells!)",
      },
      Gtk.ScrolledWindow {
	 shadow_type = 'ETCHED_IN',
	 expand = true,
	 Gtk.TreeView {
	    id = 'view',
	    model = item_store,
	    Gtk.TreeViewColumn {
	       title = "Number",
	       {
		  Gtk.CellRendererCombo {
		     id = 'number_renderer',
		     model = number_store,
		     text_column = NumberColumn.TEXT,
		     has_entry = false,
		     editable = true
		  },
		  { text = ItemColumn.NUMBER, }
	       },
	    },
	    Gtk.TreeViewColumn {
	       title = "Product",
	       {
		  Gtk.CellRendererText {
		     id = 'product_renderer',
		     editable = true,
		  },
		  { text = ItemColumn.PRODUCT, }
	       }
	    },
	    Gtk.TreeViewColumn {
	       title = "Yummy",
	       {
		  Gtk.CellRendererProgress {},
		  {
		     value = ItemColumn.YUMMY,
		  },
	       }
	    },
	 },
      },
      Gtk.Box {
	 orientation = 'HORIZONTAL',
	 spacing = 4,
	 homogeneous = true,
	 Gtk.Button {
	    id = 'add',
	    label = "Add item",
	 },
	 Gtk.Button {
	    id = 'remove',
	    label = "Remove item",
	 },
      },
   }
}

function window.child.number_renderer:on_edited(path_string, new_text)
   local path = Gtk.TreePath.new_from_string(path_string)
   item_store[path][ItemColumn.NUMBER] = new_text
end

function window.child.number_renderer:on_editing_started(editable, path)
   editable:set_row_separator_func(
      function(model, iter)
	 return model:get_path(iter):get_indices()[1] == 5
      end)
end

function window.child.product_renderer:on_edited(path_string, new_text)
   local path = Gtk.TreePath.new_from_string(path_string)
   item_store[path][ItemColumn.PRODUCT] = new_text
end

local selection = window.child.view:get_selection()
selection.mode = 'SINGLE'

function window.child.add:on_clicked()
   item_store:append {
      [ItemColumn.NUMBER] = 0,
      [ItemColumn.PRODUCT] = "Description here",
      [ItemColumn.YUMMY] = 50,
   }
end

function window.child.remove:on_clicked()
   local model, iter = selection:get_selected()
   if model and iter then
      model:remove(iter)
   end
end

window:show_all()
return window
end,

"Tree View/Editable Cells",

table.concat {
   [[This demo demonstrates the use of editable cells in a Gtk.TreeView. ]],
   [[If you're new to the Gtk.TreeView widgets and associates, look into ]],
   [[the Gtk.ListStore example first. It also shows how to use ]],
   [[the Gtk.CellRenderer::editing-started signal to do custom setup of ]],
   [[the editable widget.
The cell renderers used in this demo are Gtk.CellRendererText, ]],
   [[Gtk.CellRendererCombo and GtkCell.RendererProgress.]]
}