-
Notifications
You must be signed in to change notification settings - Fork 2
/
mdiNotebook.lua
279 lines (241 loc) · 9.12 KB
/
mdiNotebook.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
-- 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.
-- mdiNotebook.lua
-- Container object for MDI dialog
-- OO structure from http://lua-users.org/wiki/ObjectOrientationTutorial
-- Load necessary libraries
local templates = require("templates")
local RawFile = require("LuaRawFile")
-- Get assemblies
-- Get constructors
local ContextMenuStrip = luanet.import_type("System.Windows.Forms.ContextMenuStrip")
local Form = luanet.import_type("System.Windows.Forms.Form")
local TabControl = luanet.import_type("System.Windows.Forms.TabControl")
local ToolStripMenuItem = luanet.import_type("System.Windows.Forms.ToolStripMenuItem")
-- Get enumerations
local DockStyle = luanet.import_type("System.Windows.Forms.DockStyle")
local MouseButtons = luanet.import_type("System.Windows.Forms.MouseButtons")
local TabAlignment = luanet.import_type("System.Windows.Forms.TabAlignment")
-- local variables
-- forward declarations for local functions
local noteBookActivated, noteBookClosed
-- local functions
local function DeleteActivePageCB(sender, args)
local self = sender.Tag -- Tag is the mdiNotebook
self:RemovePage(self.pageList.active)
end
-- Start of the mdiPage object
local mdiNoteBook = {}
mdiNoteBook.__index = mdiNoteBook
-- Table for controlling all displayed pages
local noteBookList = {} -- Contains all notebooks
noteBookList.active = false -- will contain the active notebook
-- The notebook list can be accessed either from any notebook
-- or it can be accessed directly from the results of the
-- require ("mdiNoteBook") return value
mdiNoteBook.noteBookList = noteBookList
setmetatable(mdiNoteBook, {
__call = function (cls, ...)
local self = setmetatable({}, cls)
self:_init(...)
return self
end,})
---Create a new object of the class
function mdiNoteBook:_init(args)
args = args or {}
self:CreateForm(args) -- Create the MDI Form
-- Add to the noteBookList
-- Do this after creating the form
table.insert(noteBookList, self)
noteBookList.active = self
-- Add page list
self.pageList = {}
self.pageList.active = false
if args.rawFile then -- if rawfile specified
self.rawFile = args.rawFile -- just use it
elseif args.fileName then -- otherwise if fileName specified
if not self:OpenFile(args.fileName) then -- try to open it
return nil
end
end
-- Add pages if function is provided
if args.AddPages then args.AddPages(self) end
self.form:Show()
end
-- Loop through the notebooks to find a match
-- and then set the active notebook
-- The sender is the MDI Form, so note the use of . instead of :
function mdiNoteBook.ActivatedCB(sender, args)
noteBookList.active = sender.Tag
end
function mdiNoteBook:AddPage(page)
self.tabControl.TabPages:Add(page.pageControl)
table.insert(self.pageList, page)
self.pageList.active = page
end
function mdiNoteBook:Close()
-- This will trigger the ClosedCB() below
self.form:Close() -- Close the form
return
end
-- The sender is the MDI Form, so note the use of . instead of :
function mdiNoteBook.ClosedCB(sender, args)
local thisNoteBook = sender.Tag -- Get the parent notebook for the form
-- Remove the notebook from the noteBookList
for index, noteBook in ipairs(noteBookList) do
if thisNoteBook == noteBook then
table.remove(noteBookList, index)
noteBookList.active = false
break
end
end
-- If no rawFile, then we are done
local rawFile = thisNoteBook.rawFile
if not rawFile then return end
-- If the raw file is used anywhere else, we are done
for index, noteBook in ipairs(noteBookList) do
if noteBook.rawFile == rawFile then return end
end
-- Finally, close the raw file
rawFile:Close()
return
end
function mdiNoteBook:CreateForm(args)
args = args or {}
local mdiForm = Form()
mdiForm.MdiParent = mainForm
mdiForm.Activated:Add(mdiNoteBook.ActivatedCB)
mdiForm.Closed:Add(mdiNoteBook.ClosedCB)
mdiForm.Text = self:GetFormTitle(args)
mdiForm.Height = args.height or 0.8 * mainForm.Height
mdiForm.Width = args.width or 0.8* mainForm.Width
mdiForm.Tag = self -- Set tag for referencing in callback
self.form = mdiForm
-- Create the Tab Control
local tabControl = TabControl()
self.tabControl = tabControl
tabControl.Parent = mdiForm
tabControl.Dock = DockStyle.Fill
tabControl.Alignment = TabAlignment.Right
tabControl.Tag = self
local menu = ContextMenuStrip() -- Get a ContextMenuStrip
tabControl.ContextMenuStrip = menu
local item = ToolStripMenuItem("Delete Active Page") -- Get a ToolStripMenuItem
item.Click:Add(DeleteActivePageCB) -- Add a callback
item.Tag = self -- Set tag to the page
menu.Items:Add(item) -- Add it to the menu
end
-- Dispose of all .NET objects
function mdiNoteBook:Dispose()
print ("Disposing pages")
for index, page in ipairs(self.pageList) do
page:Dispose()
end
end
function mdiNoteBook:GetFormTitle(args)
args = args or {}
local title
if args.title then -- Supplied title is most important
title = args.title
elseif args.fileName then -- then use file name
title = args.fileName
else -- otherwise use a generic name
title = "NoteBook"
end
-- Loop through and compare to current titles
local testCount = 1
while not titleOK do
local testTitle = title
local titleMatched = false
if testCount > 1 then testTitle = testTitle .. "_" .. tostring(testCount) end
for index, noteBook in ipairs(noteBookList) do
if testTitle == noteBook.form.Text then
titleMatched = true
break
end
end
if not titleMatched then return testTitle end
testCount = testCount + 1
end
end
function mdiNoteBook:GetUniquePageName(baseName)
local i = 1
while true do
local pageName = string.format("%s_%d", baseName, i)
local unique = true
for index, page in ipairs(self.pageList) do
if pageName == page.pageControl.Text then
unique = false
break
end
end
if unique then return pageName end
i = i + 1
end
end
function mdiNoteBook:OpenFile(fileName)
self.rawFile = RawFile.New(fileName)
if type(self.rawFile) ~= "userdata" then
print ("Unable to open " .. fileName)
print (self.rawFile)
return nil
end
self.rawFile:Open()
if not self.rawFile.IsOpen then
print ("Unable to open " .. self.rawFile)
return nil
end
self.fullFileName = fileName
-- Find a backslash followed by anthing other than a backslash,
-- right before the end of the string
local index = string.find(fileName, "\\[^\\]*$")
if index then
self.pathName = string.sub(fileName, 1, index)
self.fileName = string.sub(fileName, index + 1)
end
return true
end
function mdiNoteBook:RemovePage(page)
-- This likely leaks memory associated with this page
-- and associated .NET objects but it shouldn't happen
-- too frequently, so it should be relatively safe.
local activeIndex
for pageIndex, thisPage in ipairs(self.pageList) do
if thisPage == page then
activeIndex = pageIndex
break
end
end
table.remove(self.pageList, activeIndex)
self.tabControl.TabPages:Remove(page.pageControl)
end
-- The following are not meant to be part of the mdiNoteBook object
-- but are instead ways to access the active objects. Thus they
-- use "." notation instead of ":" notation, but can be called either way.
function mdiNoteBook.GetActiveNoteBook()
return noteBookList.active
end
function mdiNoteBook.GetActivePage()
local activeNotebook = mdiNoteBook.GetActiveNoteBook()
if not activeNotebook then return false end
return activeNotebook.pageList.active
end
function mdiNoteBook.GetActivePane()
local activePage = mdiNoteBook.GetActivePage()
if not activePage then return false end
-- Not all pages have panes
if not activePage.paneList then return false end
return activePage.paneList.active
end
return mdiNoteBook