-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmenu.lua
110 lines (94 loc) · 4.19 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
-- Copyright (c) 2016 Thermo Fisher Scientific
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
-- (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
-- merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished
-- to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
-- FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
-- CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-- menu.lua
-- Load necessary libraries
-- Get assemblies
-- Get constructors
local MainMenu = luanet.import_type("System.Windows.Forms.MainMenu")
local MenuItem = luanet.import_type("System.Windows.Forms.MenuItem")
local FolderBrowserDialog = luanet.import_type("System.Windows.Forms.FolderBrowserDialog")
-- Get enumerations
local DialogResult = luanet.import_type("System.Windows.Forms.DialogResult")
-- local variables
local menu = {}
local itemList = {} -- List of items on the menu
local callBackList = {} -- List of callbacks
menu.mainMenu = MainMenu() -- Create a new main menu
-- Forward declarations for local helper functions
-- local functions
local function runCallBack(sender)
local callBack = callBackList[sender.Tag]
if not callBack then
print ("No callback found for menuItem ", sender)
return
end
local status, err = xpcall(callBack, debug.traceback)
if not status then
print (err)
end
end
-- Global variables
function menu.AddMenu(args)
args = args or {}
if not args.name then
print ("menu.AddMenu({name = x, [label = y, callBack = z, parentName = xx, beforeName = yy]})")
return nil
end
args.label = args.label or args.name -- Use name for label if not specified
-- Confirm name is unique
for key, item in pairs(itemList) do
if key == args.name then
print ("menu.AddMenu(): name not unique")
return nil
end
end
-- Get parent
local parent
if args.parentName then
parent = itemList[args.parentName]
else
parent = menu.mainMenu
end
if not parent then
print ("No valid parent for name ", args.parentName)
return nil
end
-- Get index for insertion
local menuIndex
if args.beforeName then -- if requested to insert before a menu item
local before = itemList[args.beforeName] -- get the menu item based on name
menuIndex = parent.MenuItems:IndexOf(before) -- get the index of that item
if menuIndex == -1 then
print ("No valid before for name ", args.beforeName)
return nil
end
end
local menuItem = MenuItem(args.label) -- Get a new MenuItem
menuItem.Name = args.name -- Set it's name
if args.shortCut then menuItem.Shortcut = args.shortCut end -- Set shortcut if supplied
itemList[args.name] = menuItem -- Add item to our list for easy search
--if args.callBack then menuItem.Click:Add(args.callBack) end -- Set callback if supplied
if args.callBack then
menuItem.Click:Add(runCallBack) -- All menu items use same callback function
table.insert(callBackList, args.callBack) -- Add item to callback list
menuItem.Tag = #callBackList -- Add Tag so we can find proper callback
end
if menuIndex then
parent.MenuItems:Add(menuIndex, menuItem)
else
parent.MenuItems:Add(menuItem) -- Add the MenuItem to the parent
end
return menuItem
end
return menu