-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathproperties.lua
309 lines (278 loc) · 11 KB
/
properties.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
-- 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.
-- properties.lua
-- Object for filling and retrieving values in a property page
-- Not to be used as a direct object, but methods should be inherited
-- Load necessary libraries
-- Get assemblies
luanet.load_assembly("System.Data")
-- Get constructors
local Button = luanet.import_type("System.Windows.Forms.Button")
local Form = luanet.import_type("System.Windows.Forms.Form")
local Grid = luanet.import_type("System.Windows.Forms.DataGridView")
local Panel = luanet.import_type("System.Windows.Forms.Panel")
local ComboCell = luanet.import_type("System.Windows.Forms.DataGridViewComboBoxCell")
local Cell = luanet.import_type("System.Windows.Forms.DataGridViewCell")
local Row = luanet.import_type("System.Windows.Forms.DataGridViewRow")
-- Get enumerations
local AutoSizeColumnsMode = luanet.import_type("System.Windows.Forms.DataGridViewAutoSizeColumnsMode")
local DockStyle = luanet.import_type("System.Windows.Forms.DockStyle") -- Get the enumeration
local RowHeaderModes = luanet.import_type("System.Windows.Forms.DataGridViewRowHeadersWidthSizeMode")
-- local variables
local properties = {}
local currentPropertyList
local cancelButton
local propertyForm
local grid
local okButton
local panel
local useCombo = true
-- forward declarations for local functions
local OKCB, ValidateCB
-- local functions
local function CancelCB(sender, args)
propertyForm.Visible = false -- Hide the form
properties.UpdatePropertyForm() -- Redraw to overwrite any changes
end
-- Use the convoluted method of getting the active page
-- instead of a call to mdiNoteBook.noteBookList.active,
-- since require("mdiNotebook") results in a circular reference
local function GetActivePage()
local mdiChild = mainForm.ActiveMdiChild
if not mdiChild or not mdiChild.Tag
or not mdiChild.Tag.pageList.active then
return nil
end
return mdiChild.Tag.pageList.active
end
-- Data grid cells with comboboxes throw an
-- odd exception which doesn't seem to matter
-- so I'll just capture and ignore for now
local function IgnoreErrorCB()
return
end
local function InitializeButtons()
-- Intialize the panel
panel = Panel()
propertyForm.Controls:Add(panel)
panel.Dock = DockStyle.Bottom
panel.Height = 40
-- Initialize buttons
okButton = Button()
okButton.Text = "OK"
okButton.Left = 25
okButton.Width = 80
okButton.Top = 10
--okButton.IsDefault = true
okButton.Click:Add(OKCB)
panel.Controls:Add(okButton)
cancelButton = Button()
cancelButton.Text = "Cancel"
cancelButton.Left = 130
cancelButton.Width = 80
cancelButton.Top = 10
cancelButton.Click:Add(CancelCB)
panel.Controls:Add(cancelButton)
propertyForm.AcceptButton = okButton
propertyForm.CancelButton = cancelButton
end
local function InitializeGrid()
grid = Grid()
propertyForm.Controls:Add(grid)
grid.Dock = DockStyle.Fill
grid.RowHeadersWidthSizeMode = RowHeaderModes.AutoSizeToAllHeaders
grid.ColumnHeadersVisible = false
grid.AllowUserToAddRows = false
grid.AutoGenerateColumns = false
grid.ShowCellToolTips = true
grid.AutoSizeColumnsMode = AutoSizeColumnsMode.Fill
grid.ColumnCount = 1
--grid.CellValidating:Add(ValidateCB)
grid.DataError:Add(IgnoreErrorCB)
end
-- This has a forward declaration
function OKCB(sender, args)
local page = GetActivePage()
if page then page:SetProperties() end
end
-- This has a forward declaration
function ValidateCB(sender, args)
local gridRow = grid.Rows[args.RowIndex]
local validValue = gridRow.Cells[0].Value
local propertyIndex = args.RowIndex + 1 -- Convert back to base 1
local property = currentPropertyList[propertyIndex]
if property.min or property.max then validValue = tonumber(validValue) end
if property.min then validValue = math.max(property.min, validValue) end
if property.max then validValue = math.min(property.max, validValue) end
property.value = validValue
-- I've validate the value, but I don't seem to be able to change the display
-- with the following line or any of the lines below it
gridRow.Cells[0].Value = tostring(validValue)
--grid:InvalidateCell(0,args.RowIndex)
--grid:RefreshEdit()
--grid:Invalidate()
end
function properties:AddProperty(args)
args = args or {}
local function trueFunction() return true end
args.modeTest = args.modeTest or trueFunction
table.insert(self.propertyList, args)
-- Duplicate the entries using the label as a key
-- This makes working back from the grid fairly simple
-- Keep the original integer indexing, because this controls
-- the order the properties are displayed
self.propertyList[args.label] = args
-- Create a grid row and populate here.
-- The just add rows to the grid on update
end
-- self here is the table that will inherit methods
-- from properties. Only inherit methods that don't
-- already exist, so that we have the concept of
-- overriding methods. This should be called with the
-- . notation, not the : notation
function properties.Inherit(self)
self.propertyList = {}
self.undoStack = {}
for key, value in pairs(properties) do
if type(value) == "function" and -- If the entry in the properties table is a function AND
not self[key] then -- the function does not exist in the inheriting table then
self[key] = value -- inherit the function
end
end
end
-- Pop the current properties from the undo stack
function properties:PopProperties(apply)
local pop = table.remove(self.undoStack)
if apply then
for key, value in pairs(pop) do
self[key] = value
end
end
end
-- Push the current properties to the undo stack
function properties:PushProperties()
local undo = {}
for index, property in ipairs(self.propertyList) do
undo[property.key] = self[property.key]
end
table.insert(self.undoStack, undo)
--print ("Push Called for Self ", self, " with size", #self.undoStack)
end
-- Copy values out of the grid into the object
function properties:SetProperties()
for i = 0, grid.RowCount - 1 do -- Convert to base-0 for C
local row = grid.Rows[i]
local cell = row.Cells[0]
local value = cell.Value
-- Don't use the Tag. It appears to cause memory
-- access issues for some reason
-- Instead I've set up keyed access to the propertyList based on the label
local property = self.propertyList[row.HeaderCell.Value]
if property.min or property.max then
-- Validate a numeric value
local newValue = tonumber(value)
if not newValue then
print (string.format("%s entry %s is not a number", row.HeaderCell.Value, value))
else
if property.min then newValue = math.max(newValue, property.min) end
if property.max then newValue = math.min(newValue, property.max) end
self[property.key] = newValue -- Try to convert to a number
end
elseif property.options then
-- Validate a string with limited options
local match = false
for index, option in ipairs(property.options) do
if option == value then
match = true
break
end
end
if not match then
print (string.format("%s entry %s is not a valid option", row.HeaderCell.Value, value))
else
self[property.key] = value
end
else
-- No validation required
self[property.key] = tonumber(value) or value -- Try to convert to a number
end
end
self:SetPropertiesFinalize()
end
-- This will most likely be overridden by
-- the inheriting object
function properties:SetPropertiesFinalize()
end
function properties.ShowForm()
propertyForm:Show()
end
function properties:Undo()
local size = #self.undoStack
if size <= 1 then return end -- Don't ever undo the last set of properties
self:PopProperties() -- Pop the current settings
self:PopProperties(true) -- Pop the prior settings and apply
self:SetPropertiesFinalize() -- This will push these settings again
end
-- Update the display of the property page
function properties:UpdatePropertyForm()
--if true then return end
grid.Rows:Clear()
-- This can either be called from the object (ie. page:UpdatePropertyForm())
-- or can be called directly (ie. properties.UpdatePropertyForm())
-- If called directly, use the currently active page
self = self or GetActivePage()
if not self then return end
propertyForm.Text = self:GetPropertyTitle() or "Page Properties"
-- Now fill the grid with the active properties
for index, property in ipairs(self.propertyList) do
if property.modeTest(self.mode) then
-- To include a combo box, you need to create a row
-- add the combo box, and then add it to the grid
-- This currently causes a whole bunch of problems, so
-- for now it's disabled
if useCombo then
local thisRow = Row()
if useCombo and property.options then
local comboCell = ComboCell() -- Create a DataGridViewComboBoxCell
for _, option in ipairs(property.options) do
comboCell.Items:Add(option)
end
thisRow.Cells:Add(comboCell) -- Add this cell to the row
end
-- If we just use thisRow, it's considered shared and has restricted access
-- Grabbing it below turns it into an unshared row
grid.Rows:Add(thisRow) -- Add the row to the grid
else
grid.Rows:Add()
end
local row = grid.Rows[grid.RowCount - 1]
row.HeaderCell.Value = tostring(property.label)
row.ReadOnly = property.readOnly
local cell = row.Cells[0]
cell.Value = tostring(self[property.key])
cell.ToolTipText = property.toolTip
-- Don't use the Tag. It appears to cause some corruption
-- in .NET memory
--cell.Tag = property -- Link back to the property
end
end
end
-- Code below here executes when require() is called
propertyForm = Form()
propertyForm.Text = "Page Properties"
propertyForm.ControlBox = false
propertyForm.TopMost = true
InitializeButtons()
InitializeGrid()
return properties