forked from opencalc/opencalc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu.lua
318 lines (247 loc) · 6.56 KB
/
menu.lua
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
--[[
opencalc - an open source calcualtor
Copyright (C) 2011 Richard Titmuss <richard@opencalc.me>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program in the file COPYING; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
--]]
module(..., package.seeall)
Menu = {}
local MARGIN = 13
local BORDER = 17
local FONT_SIZE = 14
-- global menus
Menu.functionMenu = { }
Menu.appMenu = {
{ "Line Graph", todo = true },
{ "Pie Chart", todo = true },
{ "About", todo = true },
}
Menu.fileMenu = {
{ "New", todo = true },
{ "Delete", todo = true },
{ "Open", todo = true },
{ "Save", todo = true },
{ "Print", todo = true },
}
Menu.editMenu = {
{ "Bold", todo = true },
{ "Delete Row", todo = true },
{ "Delete Column", todo = true },
{ "Format", todo = true },
{ "Insert Row", todo = true },
{ "Insert Column", todo = true },
{ "Protect Cell", "protect", { "Lock", "Unlock" }, def = "Lock", todo = true },
{ "Name", todo = true },
{ "Find", todo = true },
{ "Find Next", todo = true },
}
--[[
item[1] is the name
item[2] is the value:
table: submenu
string: sheet pref, item.def is default value
function: get/set function
item[3] is the value type:
table: string selection
number: range is in item.min, item.max
cell: sheet cell [TODO]
range: sheet range [TODO]
function: custom input
--]]
function Menu:new(sheet, items)
obj = {
sheet = sheet,
items = items,
}
obj.menu = { {
items = obj.items,
filtered = obj.items,
selected = 1,
screentop = 1,
typeahead = "",
} }
setmetatable(obj, self)
self.__index = self
return obj
end
function Menu:draw(context, width, height)
context:save()
context:setSourceRGB(0, 0, 0)
context:rectangle(MARGIN, MARGIN,
width - MARGIN * 2, height - MARGIN * 2)
context:fill()
context:setSourceRGB(255, 255, 255)
context:rectangle(MARGIN, MARGIN,
width - MARGIN * 2, height - MARGIN * 2)
context:setLineWidth(1)
context:stroke()
context:selectFontFace("courier")
context:selectFontSize(FONT_SIZE)
local fe = context:fontExtents()
local y = MARGIN + fe.height - fe.descent
local x = BORDER
local menu = self.menu[1]
local items = menu.filtered
local selected = menu.selected
local visible_items = math.floor((height - MARGIN * 2) / fe.height)
if selected == 1 then
menu.screentop = 1
elseif selected == #items then
menu.screentop = math.max(1, #items - visible_items + 1)
elseif selected > menu.screentop + visible_items - 2 then
menu.screentop = menu.screentop +
(selected - (menu.screentop + visible_items - 2))
elseif selected < menu.screentop + 1 then
menu.screentop = menu.screentop -
(menu.screentop - selected + 1)
end
local pattern = "^$"
if #menu.typeahead > 0 then
pattern = "[" .. menu.typeahead .. string.upper(menu.typeahead) .. "]+"
end
for i = menu.screentop, menu.screentop + visible_items - 1 do
local item = items[i]
if not item then
break
end
local name, value = item[1], item[2]
if item.todo then
context:selectFontFace("courier", 1)
else
context:selectFontFace("courier")
end
context:setSourceRGB(255, 255, 255)
if selected == i then
context:rectangle(
MARGIN,
y - fe.height + fe.descent,
width - MARGIN * 2,
fe.height)
context:fill()
context:setSourceRGB(0, 0, 0)
end
context:moveTo(x, y)
context:showUnderlinedText(name, pattern)
if type(value) == "table" then
value = "=>"
elseif type(value) == "string" then
value = self.sheet:getProp(value, item.def)
elseif type(value) == "function" then
value = value(self.sheet)
end
local te = context:textExtents(value)
context:moveTo(width - BORDER - te.width, y)
context:showText(value)
y = y + fe.height
end
context:restore()
end
function Menu:filterItems(typeahead)
local filtered = {}
for i,item in ipairs(self.menu[1].items) do
local name = string.lower(item[1])
local match = true
for c in string.gmatch(typeahead, ".") do
if not string.find(name, c) then
match = false
break
end
end
if match then
table.insert(filtered, item)
end
end
return filtered
end
function Menu:promoteItem(item)
local menu = self.menu[1]
for i,mitem in ipairs(menu.items) do
if mitem == item then
table.remove(menu.items, i)
table.insert(menu.items, 1, item)
return true
end
end
return false
end
function Menu:event(event)
local menu = self.menu[1]
local items = menu.filtered
local selected = menu.selected
if event.type == "keypress" then
if event.key == "<move_up>" then
menu.selected = selected - 1
if menu.selected < 1 then
menu.selected = #items
end
elseif event.key == "<move_down>" then
menu.selected = selected + 1
if menu.selected > #items then
menu.selected = 1
end
elseif event.key == "<move_left>" then
if #self.menu == 1 then
return false
end
table.remove(self.menu, 1)
elseif event.key == "<move_right>" or
event.key == "=" then
local item = items[selected]
self:promoteItem(item)
local value = true
if type(item[3]) == "table" then
-- select next option
local last_value = self.sheet:getProp(item[2], item.def)
local options = item[3]
value = options[1]
for i=#options,1,-1 do
if options[i] == last_value then
break
end
value = options[i]
end
end
if type(item[2]) == "table" then
-- display sub-menu
table.insert(self.menu, 1, {
items = item[2],
filtered = item[2],
selected = 1,
screentop = 1,
typeahead = "",
})
elseif type(item[2]) == "function" then
-- custom function
return item[2](self.sheet, value)
elseif type(item[2]) == "string" then
self.sheet:setProp(item[2], value)
end
else
local str
if event.key == "<delete>" then
str = string.sub(menu.typeahead, 1, #menu.typeahead - 1)
else
str = menu.typeahead .. event.key
end
local filtered = self:filterItems(str)
if #filtered > 0 then
menu.filtered = filtered
menu.typeahead = str
if selected > #filtered then
menu.selected = #filtered
end
end
end
end
return true
end
return Menu