tArgs={...}
if OneOS then
OneOS.ToolBarColour=colours.grey
OneOS.ToolBarTextColour=colours.white
end
local e,e=term.getSize()
local h=function(t,e)
local e=10^(e or 0)
return math.floor(t*e+.5)/e
end
UIColours={
Toolbar=colours.grey,
ToolbarText=colours.lightGrey,
ToolbarSelected=colours.lightBlue,
ControlText=colours.white,
ToolbarItemTitle=colours.black,
Background=colours.lightGrey,
MenuBackground=colours.white,
MenuText=colours.black,
MenuSeparatorText=colours.grey,
MenuDisabledText=colours.lightGrey,
Shadow=colours.grey,
TransparentBackgroundOne=colours.white,
TransparentBackgroundTwo=colours.lightGrey,
MenuBarActive=colours.white
}
local e=peripheral.getNames or function()
local t={}
for a,e in ipairs(rs.getSides())do
if peripheral.isPresent(e)then
table.insert(t,e)
local a=false
if not pcall(function()a=peripheral.call(e,'isWireless')end)then
a=true
end
if peripheral.getType(e)=="modem"and not a then
local e=peripheral.call(e,"getNamesRemote")
for a,e in ipairs(e)do
table.insert(t,e)
end
end
end
end
return t
end
OneOS.LoadAPI('System/API/Peripheral.lua')
TextLine={
Text="",
Alignment=AlignmentLeft,
Initialise=function(o,t,a)
local e={}
setmetatable(e,{__index=o})
e.Text=t
e.Alignment=a or AlignmentLeft
return e
end
}
local s=function(e)
return e:gsub('['..string.char(14)..'-'..string.char(29)..']','')
end
Printer = {
Name = nil,
PeripheralType = 'printer',
paperLevel = function(self)
return Peripheral.CallNamed(self.Name, 'getPaperLevel')
end,
newPage = function(self)
return Peripheral.CallNamed(self.Name, 'newPage')
end,
endPage = function(self)
return Peripheral.CallNamed(self.Name, 'endPage')
end,
pageWrite = function(self, text)
return Peripheral.CallNamed(self.Name, 'write', text)
end,
setPageTitle = function(self, title)
return Peripheral.CallNamed(self.Name, 'setPageTitle', title)
end,
inkLevel = function(self)
return Peripheral.CallNamed(self.Name, 'getInkLevel')
end,
getCursorPos = function(self)
return Peripheral.CallNamed(self.Name, 'getCursorPos')
end,
setCursorPos = function(self, x, y)
return Peripheral.CallNamed(self.Name, 'setCursorPos', x, y)
end,
pageSize = function(self)
return Peripheral.CallNamed(self.Name, 'getPageSize')
end,
Present = function()
if Peripheral.GetPeripheral(Printer.PeripheralType) == nil then
return false
else
return true
end
end,
PrintLines = function(self, lines, title, copies)
local pages = {}
local pageLines = {}
for i, line in ipairs(lines) do
table.insert(pageLines, TextLine:Initialise(s(line)))
if i % 25 == 0 then
table.insert(pages, pageLines)
pageLines = {}
end
end
if #pageLines ~= 0 then
table.insert(pages, pageLines)
end
return self:PrintPages(pages, title, copies)
end,
PrintPages = function(self, pages, title, copies)
copies = copies or 1
for c = 1, copies do
for p, page in ipairs(pages) do
if self:paperLevel() < #pages * copies then
return 'Add more paper to the printer'
end
if self:inkLevel() < #pages * copies then
return 'Add more ink to the printer'
end
self:newPage()
for i, line in ipairs(page) do
self:setCursorPos(1, i)
self:pageWrite(s(line.Text))
end
if title then
self:setPageTitle(title)
end
self:endPage()
end
end
end,
Initialise = function(self, name)
if Printer.Present() then --fix
local new = {}    -- the new instance
setmetatable( new, {__index = self} )
if name and Peripheral.PresentNamed(name) then
new.Name = name
else
new.Name = Peripheral.GetPeripheral(Printer.PeripheralType).Side
end
return new
end
end
}
Clipboard=OneOS.Clipboard
OneOS.LoadAPI('System/API/LegacyDrawing.lua')
local Drawing = LegacyDrawing
Current={
Document=nil,
TextInput=nil,
CursorPos={1,1},
CursorColour=colours.black,
Selection={8,36},
Window=nil,
Modified=false,
}
local c=false
function OrderSelection()
if Current.Selection then
if Current.Selection[1]<=Current.Selection[2]then
return Current.Selection
else
return{Current.Selection[2],Current.Selection[1]}
end
end
end
function s(e)
return e:gsub('['..string.char(14)..'-'..string.char(29)..']','')
end
function FindColours(e)
local t,e=e:gsub('['..string.char(14)..'-'..string.char(29)..']','')
return e
end
ColourFromCharacter=function(e)
local e=e:byte()-14
if e>16 then
return nil
else
return 2^e
end
end
CharacterFromColour=function(e)
return string.char(math.floor(math.log(e)/math.log(2))+14)
end
Events={}
Button={
X=1,
Y=1,
Width=0,
Height=0,
BackgroundColour=colours.lightGrey,
TextColour=colours.white,
ActiveBackgroundColour=colours.lightGrey,
Text="",
Parent=nil,
_Click=nil,
Toggle=nil,
AbsolutePosition=function(e)
return e.Parent:AbsolutePosition()
end,
Draw=function(e)
local t=e.BackgroundColour
local o=e.TextColour
if type(t)=='function'then
t=t()
end
if e.Toggle then
o=UIColours.MenuBarActive
t=e.ActiveBackgroundColour
end
local a=GetAbsolutePosition(e)
Drawing.DrawBlankArea(a.X,a.Y,e.Width,e.Height,t)
Drawing.DrawCharactersCenter(a.X,a.Y,e.Width,e.Height,e.Text,o,t)
end,
Initialise=function(d,l,c,u,t,h,n,r,a,i,o,s)
local e={}
setmetatable(e,{__index=d})
t=t or 1
e.Width=u or#a+2
e.Height=t
e.Y=c
e.X=l
e.Text=a or""
e.BackgroundColour=h or colours.lightGrey
e.TextColour=i or colours.white
e.ActiveBackgroundColour=s or colours.lightGrey
e.Parent=n
e._Click=r
e.Toggle=o
return e
end,
Click=function(e,o,a,t)
if e._Click then
if e:_Click(o,a,t,not e.Toggle)~=false and e.Toggle~=nil then
e.Toggle=not e.Toggle
Draw()
end
return true
else
return false
end
end
}
TextBox={
X=1,
Y=1,
Width=0,
Height=0,
BackgroundColour=colours.lightGrey,
TextColour=colours.black,
Parent=nil,
TextInput=nil,
Placeholder='',
AbsolutePosition=function(e)
return e.Parent:AbsolutePosition()
end,
Draw=function(e)
local t=GetAbsolutePosition(e)
Drawing.DrawBlankArea(t.X,t.Y,e.Width,e.Height,e.BackgroundColour)
local a=e.TextInput.Value
if#tostring(a)>(e.Width-2)then
a=a:sub(#a-(e.Width-3))
if Current.TextInput==e.TextInput then
Current.CursorPos={t.X+1+e.Width-2,t.Y}
end
else
if Current.TextInput==e.TextInput then
Current.CursorPos={t.X+1+e.TextInput.CursorPos,t.Y}
end
end
if#tostring(a)==0 then
Drawing.DrawCharacters(t.X+1,t.Y,e.Placeholder,colours.lightGrey,e.BackgroundColour)
else
Drawing.DrawCharacters(t.X+1,t.Y,a,e.TextColour,e.BackgroundColour)
end
term.setCursorBlink(true)
Current.CursorColour=e.TextColour
end,
Initialise=function(h,r,s,i,t,n,o,d,u,a,l)
local e={}
setmetatable(e,{__index=h})
t=t or 1
e.Width=i or#o+2
e.Height=t
e.Y=s
e.X=r
e.TextInput=TextInput:Initialise(o or'',function(e)
if a then
a(e)
end
Draw()
end,l)
e.BackgroundColour=d or colours.lightGrey
e.TextColour=u or colours.black
e.Parent=n
return e
end,
Click=function(e,t,t,t)
Current.Input=e.TextInput
e:Draw()
end
}
TextInput={
Value="",
Change=nil,
CursorPos=nil,
Numerical=false,
IsDocument=nil,
Initialise=function(n,t,i,o,a)
local e={}
setmetatable(e,{__index=n})
e.Value=tostring(t)
e.Change=i
e.CursorPos=#tostring(t)
e.Numerical=o
e.IsDocument=a or false
return e
end,
Insert=function(e,t)
if e.Numerical then
t=tostring(tonumber(t))
end
local a=OrderSelection()
if e.IsDocument and a then
e.Value=string.sub(e.Value,1,a[1]-1)..t..string.sub(e.Value,a[2]+2)
e.CursorPos=a[1]
Current.Selection=nil
else
local o,a=string.gsub(e.Value:sub(1,e.CursorPos),'\n','')
e.Value=string.sub(e.Value,1,e.CursorPos+a)..t..string.sub(e.Value,e.CursorPos+1+a)
e.CursorPos=e.CursorPos+1
end
e.Change(key)
end,
Extract=function(t,i)
local e=OrderSelection()
if t.IsDocument and e then
local o,a=string.gsub(t.Value:sub(e[1],e[2]),'\n','')
local o=string.sub(t.Value,e[1],e[2]+1+a)
if i then
t.Value=string.sub(t.Value,1,e[1]-1)..string.sub(t.Value,e[2]+2+a)
t.CursorPos=e[1]-1
Current.Selection=nil
end
return o
end
end,
Char=function(t,e)
if e=='nil'then
return
end
t:Insert(e)
end,
Key=function(e,t)
if t==keys.enter then
if e.IsDocument then
e.Value=string.sub(e.Value,1,e.CursorPos)..'\n'..string.sub(e.Value,e.CursorPos+1)
e.CursorPos=e.CursorPos+1
end
e.Change(t)
elseif t==keys.left then
if e.CursorPos>0 then
local a=FindColours(string.sub(e.Value,e.CursorPos,e.CursorPos))
e.CursorPos=e.CursorPos-1-a
e.Change(t)
end
elseif t==keys.right then
if e.CursorPos<string.len(e.Value)then
local a=FindColours(string.sub(e.Value,e.CursorPos+1,e.CursorPos+1))
e.CursorPos=e.CursorPos+1+a
e.Change(t)
end
elseif t==keys.backspace then
if e.IsDocument and Current.Selection then
e:Extract(true)
e.Change(t)
elseif e.CursorPos>0 then
local a=FindColours(string.sub(e.Value,e.CursorPos,e.CursorPos))
local i,o=string.gsub(e.Value:sub(1,e.CursorPos),'\n','')
e.Value=string.sub(e.Value,1,e.CursorPos-1-a+o)..string.sub(e.Value,e.CursorPos+1-a+o)
e.CursorPos=e.CursorPos-1-a
e.Change(t)
end
elseif t==keys.home then
e.CursorPos=0
e.Change(t)
elseif t==keys.delete then
if e.IsDocument and Current.Selection then
e:Extract(true)
e.Change(t)
elseif e.CursorPos<string.len(e.Value)then
e.Value=string.sub(e.Value,1,e.CursorPos)..string.sub(e.Value,e.CursorPos+2)
e.Change(t)
end
elseif t==keys["end"]then
e.CursorPos=string.len(e.Value)
e.Change(t)
elseif t==keys.up and e.IsDocument then
if Current.Document.CursorPos then
local a=Current.Document.Pages[Current.Document.CursorPos.Page]
e.CursorPos=a:GetCursorPosFromPoint(Current.Document.CursorPos.Collum+a.MarginX,Current.Document.CursorPos.Line-a.MarginY-1+Current.Document.ScrollBar.Scroll,true)
e.Change(t)
end
elseif t==keys.down and e.IsDocument then
if Current.Document.CursorPos then
local a=Current.Document.Pages[Current.Document.CursorPos.Page]
e.CursorPos=a:GetCursorPosFromPoint(Current.Document.CursorPos.Collum+a.MarginX,Current.Document.CursorPos.Line-a.MarginY+1+Current.Document.ScrollBar.Scroll,true)
e.Change(t)
end
end
end
}
Menu={
X=0,
Y=0,
Width=0,
Height=0,
Owner=nil,
Items={},
RemoveTop=false,
Draw=function(e)
Drawing.DrawBlankArea(e.X+1,e.Y+1,e.Width,e.Height,UIColours.Shadow)
if not e.RemoveTop then
Drawing.DrawBlankArea(e.X,e.Y,e.Width,e.Height,UIColours.MenuBackground)
for a,t in ipairs(e.Items)do
if t.Separator then
Drawing.DrawArea(e.X,e.Y+a,e.Width,1,'-',colours.grey,UIColours.MenuBackground)
else
local o=t.Colour or UIColours.MenuText
if(t.Enabled and type(t.Enabled)=='function'and t.Enabled()==false)or t.Enabled==false then
o=UIColours.MenuDisabledText
end
Drawing.DrawCharacters(e.X+1,e.Y+a,t.Title,o,UIColours.MenuBackground)
end
end
else
Drawing.DrawBlankArea(e.X,e.Y,e.Width,e.Height,UIColours.MenuBackground)
for a,t in ipairs(e.Items)do
if t.Separator then
Drawing.DrawArea(e.X,e.Y+a-1,e.Width,1,'-',colours.grey,UIColours.MenuBackground)
else
local o=t.Colour or UIColours.MenuText
if(t.Enabled and type(t.Enabled)=='function'and t.Enabled()==false)or t.Enabled==false then
o=UIColours.MenuDisabledText
end
Drawing.DrawCharacters(e.X+1,e.Y+a-1,t.Title,o,UIColours.MenuBackground)
Drawing.DrawCharacters(e.X-1+e.Width-#t.KeyName,e.Y+a-1,t.KeyName,o,UIColours.MenuBackground)
end
end
end
end,
NameForKey=function(t,e)
if e==keys.leftCtrl then
return'^'
elseif e==keys.tab then
return'Tab'
elseif e==keys.delete then
return'Delete'
elseif e==keys.n then
return'N'
elseif e==keys.a then
return'A'
elseif e==keys.s then
return'S'
elseif e==keys.o then
return'O'
elseif e==keys.z then
return'Z'
elseif e==keys.y then
return'Y'
elseif e==keys.c then
return'C'
elseif e==keys.x then
return'X'
elseif e==keys.v then
return'V'
elseif e==keys.r then
return'R'
elseif e==keys.l then
return'L'
elseif e==keys.t then
return'T'
elseif e==keys.h then
return'H'
elseif e==keys.e then
return'E'
elseif e==keys.p then
return'P'
elseif e==keys.f then
return'F'
elseif e==keys.m then
return'M'
elseif e==keys.q then
return'Q'
else
return'?'
end
end,
Initialise=function(i,a,o,t,n,s)
local e={}
setmetatable(e,{__index=i})
if not n then
return
end
local h={}
for e,a in ipairs(t)do
t[e].KeyName=''
if a.Keys then
for o,a in ipairs(a.Keys)do
t[e].KeyName=t[e].KeyName..i:NameForKey(a)
end
end
if t[e].KeyName~=''then
table.insert(h,t[e].KeyName)
end
end
local i=LongestString(h)
if i>0 then
i=i+2
end
e.Width=LongestString(t,'Title')+2+i
if e.Width<10 then
e.Width=10
end
e.Height=#t+2
e.RemoveTop=s or false
if s then
e.Height=e.Height-1
end
if o<1 then
o=1
end
if a<1 then
a=1
end
if o+e.Height>Drawing.Screen.Height+1 then
o=Drawing.Screen.Height-e.Height
end
if a+e.Width>Drawing.Screen.Width+1 then
a=Drawing.Screen.Width-e.Width
end
e.Y=o
e.X=a
e.Items=t
e.Owner=n
return e
end,
New=function(t,a,o,n,e,i)
if Current.Menu and Current.Menu.Owner==e then
Current.Menu=nil
return
end
local e=t:Initialise(a,o,n,e,i)
Current.Menu=e
return e
end,
Click=function(e,t,t,a)
local t=a-1
if e.RemoveTop then
t=a
end
if t>=1 and a<e.Height then
if not((e.Items[t].Enabled and type(e.Items[t].Enabled)=='function'and e.Items[t].Enabled()==false)or e.Items[t].Enabled==false)and e.Items[t].Click then
e.Items[t]:Click()
if Current.Menu.Owner and Current.Menu.Owner.Toggle then
Current.Menu.Owner.Toggle=false
end
Current.Menu=nil
e=nil
end
return true
end
end
}
MenuBar={
X=1,
Y=1,
Width=Drawing.Screen.Width,
Height=1,
MenuBarItems={},
AbsolutePosition=function(e)
return{X=e.X,Y=e.Y}
end,
Draw=function(e)
Drawing.DrawBlankArea(e.X,e.Y,e.Width,e.Height,colours.grey)
for t,e in ipairs(e.MenuBarItems)do
e:Draw()
end
end,
Initialise=function(t,a)
local e={}
setmetatable(e,{__index=t})
e.X=1
e.Y=1
e.MenuBarItems=a
return e
end,
AddToolbarItem=function(e,t)
table.insert(e.ToolbarItems,t)
e:CalculateToolbarItemPositions()
end,
CalculateToolbarItemPositions=function(t)
local e=1
for a,t in ipairs(t.ToolbarItems)do
t.Y=e
e=e+t.Height
end
end,
Click=function(e,a,t,o)
for o,e in ipairs(e.MenuBarItems)do
if e.X<=t and e.X+e.Width>t then
if e:Click(e,a,t-e.X+1,1)then
return true
end
end
end
return false
end
}
TextFormatPlainText=1
TextFormatInkText=2
Document={
X=1,
Y=1,
PageSize={Width=25,Height=21},
TextInput=nil,
Pages={},
Format=TextFormatPlainText,
Title='',
Path=nil,
ScrollBar=nil,
Lines={},
CursorPos=nil,
CalculateLineWrapping=function(e)
local a=e.PageSize.Width
local t=e.TextInput.Value
local e={''}
local o={}
for t,n in t:gmatch('(%S+)(%s*)')do
for e=1,math.ceil(#t/a)do
local i=''
if e==math.ceil(#t/a)then
i=n
end
table.insert(o,{t:sub(1+a*(e-1),a*e),i})
end
end
for o,t in ipairs(o)do
local o=t[1]
local t=t[2]
local i=e[#e]..o..t:gsub('\n','')
if#i>a then
table.insert(e,'')
end
if t:find('\n')then
e[#e]=e[#e]..o
t=t:gsub('\n',function()
table.insert(e,'')
return''
end)
else
e[#e]=e[#e]..o..t
end
end
return e
end,
CalculateCursorPos=function(e)
local t=0
Current.CursorPos=nil
for h,a in ipairs(e.Pages)do
a:Draw()
if not Current.CursorPos then
for i,n in ipairs(a.Lines)do
local o=e.TextInput.CursorPos-FindColours(e.TextInput.Value:sub(1,e.TextInput.CursorPos))
if t+#s(n.Text:gsub('\n',''))>=o then
Current.CursorPos={e.X+a.MarginX+(o-t),a.Y+1+i}
e.CursorPos={Page=h,Line=i,Collum=o-t-FindColours(e.TextInput.Value:sub(1,e.TextInput.CursorPos-1))}
break
end
t=t+#s(n.Text:gsub('\n',''))
end
end
end
end,
Draw=function(e)
e:CalculatePages()
e:CalculateCursorPos()
e.ScrollBar:Draw()
end,
CalculatePages=function(e)
e.Pages={}
local o=e:CalculateLineWrapping()
e.Lines=o
local t={}
local a=(3+e.PageSize.Height+2*Page.MarginY)
for o,i in ipairs(o)do
table.insert(t,TextLine:Initialise(i))
if o%e.PageSize.Height==0 then
table.insert(e.Pages,Page:Initialise(e,t,3-e.ScrollBar.Scroll+a*(#e.Pages)))
t={}
end
end
if#t~=0 then
table.insert(e.Pages,Page:Initialise(e,t,3-e.ScrollBar.Scroll+a*(#e.Pages)))
end
e.ScrollBar.MaxScroll=a*(#e.Pages)-Drawing.Screen.Height+1
end,
ScrollToCursor=function(e)
e:CalculateCursorPos()
if Current.CursorPos and
(Current.CursorPos[2]>Drawing.Screen.Height
or Current.CursorPos[2]<2)then
e.ScrollBar:DoScroll(Current.CursorPos[2]-Drawing.Screen.Height)
end
end,
SetSelectionColour=function(e,t)
local a=OrderSelection()
local i=e.TextInput:Extract(true)
local o=CharacterFromColour(t)
local t=''
if FindColours(e.TextInput.Value:sub(e.TextInput.CursorPos+1,e.TextInput.CursorPos+1))==0 then
for a=1,e.TextInput.CursorPos do
local e=e.TextInput.Value:sub(e.TextInput.CursorPos-a,e.TextInput.CursorPos-a)
if FindColours(e)==1 then
t=e
break
end
end
if t==''then
t=CharacterFromColour(colours.black)
end
end
e.TextInput:Insert(o..s(i)..t)
end,
Initialise=function(t,a,o,i)
local e={}
setmetatable(e,{__index=t})
e.Title=o or'New Document'
e.Path=i
e.X=(Drawing.Screen.Width-(e.PageSize.Width+2*(Page.MarginX)))/2
e.Y=2
e.TextInput=TextInput:Initialise(a,function()
e:ScrollToCursor()
Current.Modified=true
Draw()
end,false,true)
e.ScrollBar=ScrollBar:Initialise(Drawing.Screen.Width,e.Y,Drawing.Screen.Height-1,0,nil,nil,nil,function()end)
Current.TextInput=e.TextInput
Current.ScrollBar=e.ScrollBar
return e
end
}
ScrollBar={
X=1,
Y=1,
Width=1,
Height=1,
BackgroundColour=colours.grey,
BarColour=colours.lightBlue,
Parent=nil,
Change=nil,
Scroll=0,
MaxScroll=0,
ClickPoint=nil,
AbsolutePosition=function(e)
return e.Parent:AbsolutePosition()
end,
Draw=function(e)
local a=GetAbsolutePosition(e)
local t=e.Height-e.MaxScroll
if t<3 then
t=3
end
local o=(e.Scroll/e.MaxScroll)
Drawing.DrawBlankArea(a.X,a.Y,e.Width,e.Height,e.BackgroundColour)
Drawing.DrawBlankArea(a.X,a.Y+h(e.Height*o-t*o),e.Width,t,e.BarColour)
end,
Initialise=function(d,h,r,s,n,o,i,a,t)
local e={}
setmetatable(e,{__index=d})
e.Width=1
e.Height=s
e.Y=r
e.X=h
e.BackgroundColour=o or colours.grey
e.BarColour=i or colours.lightBlue
e.Parent=a
e.Change=t or function()end
e.MaxScroll=n
e.Scroll=0
return e
end,
DoScroll=function(e,t)
t=h(t)
if e.Scroll<0 or e.Scroll>e.MaxScroll then
return false
end
e.Scroll=e.Scroll+t
if e.Scroll<0 then
e.Scroll=0
elseif e.Scroll>e.MaxScroll then
e.Scroll=e.MaxScroll
end
e.Change()
return true
end,
Click=function(e,t,t,i,a)
local o=(e.Scroll/e.MaxScroll)
local t=(e.Height-e.MaxScroll)
if t<3 then
t=3
end
local t=(e.MaxScroll*(i+t*o)/e.Height)
if not a then
e.ClickPoint=e.Scroll-t+1
end
if e.Scroll-1~=t then
e:DoScroll(t-e.Scroll-1+e.ClickPoint)
end
return true
end
}
AlignmentLeft=1
AlignmentCentre=2
AlignmentRight=3
TextLine={
Text="",
Alignment=AlignmentLeft,
Initialise=function(o,a,t)
local e={}
setmetatable(e,{__index=o})
e.Text=a
e.Alignment=t or AlignmentLeft
return e
end
}
local i=1
Page={
X=1,
Y=1,
Width=1,
Height=1,
MarginX=3,
MarginY=2,
BackgroundColour=colours.white,
TextColour=colours.white,
ActiveBackgroundColour=colours.lightGrey,
Lines={},
Parent=nil,
AbsolutePosition=function(e)
return e.Parent:AbsolutePosition()
end,
Draw=function(e)
local t=GetAbsolutePosition(e)
if t.Y>Drawing.Screen.Height or t.Y+e.Height<1 then
return
end
Drawing.DrawBlankArea(t.X+e.Width,t.Y-1+1,1,e.Height,UIColours.Shadow)
Drawing.DrawBlankArea(t.X+1,t.Y-1+e.Height,e.Width,1,UIColours.Shadow)
Drawing.DrawBlankArea(t.X,t.Y-1,e.Width,e.Height,e.BackgroundColour)
local s=e.TextColour
if not Current.Selection then
for h,o in ipairs(e.Lines)do
local i=1
for a=1,#o.Text do
local n=ColourFromCharacter(o.Text:sub(a,a))
if n then
s=n
else
Drawing.WriteToBuffer(t.X+e.MarginX-1+i,t.Y-2+h+e.MarginY,o.Text:sub(a,a),s,e.BackgroundColour)
i=i+1
end
end
end
else
local h=OrderSelection()
local o=1
local s=e.TextColour
for d,n in ipairs(e.Lines)do
local i=1
for a=1,#n.Text do
local r=ColourFromCharacter(n.Text:sub(a,a))
if r then
s=r
else
local s=s
local r=colours.white
if o>=h[1]and o<=h[2]then
r=colours.lightBlue
s=colours.white
end
Drawing.WriteToBuffer(t.X+e.MarginX-1+i,t.Y-2+d+e.MarginY,n.Text:sub(a,a),s,r)
i=i+1
end
o=o+1
end
end
end
end,
Initialise=function(t,a,o,i)
local e={}
setmetatable(e,{__index=t})
e.Height=a.PageSize.Height+2*t.MarginY
e.Width=a.PageSize.Width+2*t.MarginX
e.X=1
e.Y=i or 1
e.Lines=o or{}
e.BackgroundColour=colours.white
e.TextColour=colours.black
e.Parent=a
e.ClickPos=1
return e
end,
GetCursorPosFromPoint=function(a,t,i,o)
local e=GetAbsolutePosition(a)
if o then
e={Y=0,X=0}
end
local i=i-e.Y+a.MarginY-a.Parent.ScrollBar.Scroll
local t=t-a.MarginX-e.X+1
local e=0
if i<=0 or t<=0 then
return 0
end
if i>#a.Lines then
for a,t in ipairs(a.Lines)do
e=e+#t.Text
end
return e
end
local o=0
for n,a in ipairs(a.Lines)do
if n==i then
if t>#a.Text then
t=#a.Text
else
t=t+FindColours(a.Text:sub(1,t))
end
e=e+t+2-n-o
break
else
o=FindColours(a.Text)
if o~=0 then
o=o
end
e=e+#a.Text+2-n+FindColours(a.Text)
end
end
return e-2
end,
Click=function(e,n,t,a,o)
local a=e:GetCursorPosFromPoint(t,a)
e.Parent.TextInput.CursorPos=a
if o==nil then
Current.Selection=nil
i=t
else
local a=a
if not Current.Selection then
local e=1
if i and i<t then
e=0
end
Current.Selection={a+e,a+1+e}
else
Current.Selection[2]=a+1
end
end
Draw()
return true
end
}
function GetAbsolutePosition(e)
local e=e
local t=0
local a=1
local o=1
while true do
a=a+e.X-1
o=o+e.Y-1
if not e.Parent then
return{X=a,Y=o}
end
e=e.Parent
if t>32 then
return{X=1,Y=1}
end
t=t+1
end
end
function Draw()
if not Current.Window then
Drawing.Clear(colours.lightGrey)
else
Drawing.DrawArea(1,2,Drawing.Screen.Width,Drawing.Screen.Height,'|',colours.black,colours.lightGrey)
end
if Current.Document then
Current.Document:Draw()
end
Current.MenuBar:Draw()
if Current.Window then
Current.Window:Draw()
end
if Current.Menu then
Current.Menu:Draw()
end
Drawing.DrawBuffer()
if Current.TextInput and Current.CursorPos and not Current.Menu and not(Current.Window and Current.Document and Current.TextInput==Current.Document.TextInput)and Current.CursorPos[2]>1 then
term.setCursorPos(Current.CursorPos[1],Current.CursorPos[2])
term.setCursorBlink(true)
term.setTextColour(Current.CursorColour)
else
term.setCursorBlink(false)
end
end
MainDraw=Draw
LongestString=function(e,t)
local a=0
for o=1,#e do
local e=e[o]
if t then
if e[t]then
e=e[t]
else
e=''
end
end
local e=string.len(e)
if e>a then
a=e
end
end
return a
end
function LoadMenuBar()
Current.MenuBar=MenuBar:Initialise({
Button:Initialise(1,1,nil,nil,colours.grey,Current.MenuBar,function(t,a,a,a,e)
if e then
Menu:New(1,2,{
{
Title="New...",
Click=function()
Current.Document=Document:Initialise('')
end,
Keys={
keys.leftCtrl,
keys.n
}
},
{
Title='Open...',
Click=function()
DisplayOpenDocumentWindow()
end,
Keys={
keys.leftCtrl,
keys.o
}
},
{
Separator=true
},
{
Title='Save...',
Click=function()
SaveDocument()
end,
Keys={
keys.leftCtrl,
keys.s
},
Enabled=function()
return true
end
},
{
Separator=true
},
{
Title='Print...',
Click=function()
PrintDocument()
end,
Keys={
keys.leftCtrl,
keys.p
},
Enabled=function()
return true
end
},
{
Separator=true
},
{
Title='Quit',
Click=function()
Close()
end
},
},t,true)
else
Current.Menu=nil
end
return true
end,'File',colours.lightGrey,false),
Button:Initialise(7,1,nil,nil,colours.grey,Current.MenuBar,function(t,e,e,e,e)
if not t.Toggle then
Menu:New(7,2,{
{
Title='Cut',
Click=function()
Clipboard.Cut(Current.Document.TextInput:Extract(true),'text')
end,
Keys={
keys.leftCtrl,
keys.x
},
Enabled=function()
return Current.Document~=nil and Current.Selection and Current.Selection[1]and Current.Selection[2]~=nil
end
},
{
Title='Copy',
Click=function()
Clipboard.Copy(Current.Document.TextInput:Extract(),'text')
end,
Keys={
keys.leftCtrl,
keys.c
},
Enabled=function()
return Current.Document~=nil and Current.Selection and Current.Selection[1]and Current.Selection[2]~=nil
end
},
{
Title='Paste',
Click=function()
local e=Clipboard.Paste()
Current.Document.TextInput:Insert(e)
Current.Document.TextInput.CursorPos=Current.Document.TextInput.CursorPos+#e-1
end,
Keys={
keys.leftCtrl,
keys.v
},
Enabled=function()
return Current.Document~=nil and(not Clipboard.isEmpty())and Clipboard.Type=='text'
end
},
{
Separator=true,
},
{
Title='Select All',
Click=function()
Current.Selection={1,#Current.Document.TextInput.Value:gsub('\n','')}
end,
Keys={
keys.leftCtrl,
keys.a
},
Enabled=function()
return Current.Document~=nil
end
}
},t,true)
else
Current.Menu=nil
end
return true
end,'Edit',colours.lightGrey,false)
})
end
function LoadMenuBar()
Current.MenuBar=MenuBar:Initialise({
Button:Initialise(1,1,nil,nil,colours.grey,Current.MenuBar,function(t,a,a,a,e)
if e then
Menu:New(1,2,{
{
Title="New...",
Click=function()
Current.Document=Document:Initialise('')
end,
Keys={
keys.leftCtrl,
keys.n
}
},
{
Title='Open...',
Click=function()
DisplayOpenDocumentWindow()
end,
Keys={
keys.leftCtrl,
keys.o
}
},
{
Separator=true
},
{
Title='Save...',
Click=function()
SaveDocument()
end,
Keys={
keys.leftCtrl,
keys.s
},
Enabled=function()
return Current.Document~=nil
end
},
{
Separator=true
},
{
Title='Print...',
Click=function()
PrintDocument()
end,
Keys={
keys.leftCtrl,
keys.p
},
Enabled=function()
return true
end
},
{
Separator=true
},
{
Title='Quit',
Click=function()
if Close()and OneOS then
OneOS.Close()
end
end,
Keys={
keys.leftCtrl,
keys.q
}
},
},t,true)
else
Current.Menu=nil
end
return true
end,'File',colours.lightGrey,false),
Button:Initialise(7,1,nil,nil,colours.grey,Current.MenuBar,function(e,t,t,t,t)
if not e.Toggle then
Menu:New(7,2,{
{
Title='Cut',
Click=function()
Clipboard.Cut(Current.Document.TextInput:Extract(true),'text')
end,
Keys={
keys.leftCtrl,
keys.x
},
Enabled=function()
return Current.Document~=nil and Current.Selection and Current.Selection[1]and Current.Selection[2]~=nil
end
},
{
Title='Copy',
Click=function()
Clipboard.Copy(Current.Document.TextInput:Extract(),'text')
end,
Keys={
keys.leftCtrl,
keys.c
},
Enabled=function()
return Current.Document~=nil and Current.Selection and Current.Selection[1]and Current.Selection[2]~=nil
end
},
{
Title='Paste',
Click=function()
local e=Clipboard.Paste()
Current.Document.TextInput:Insert(e)
Current.Document.TextInput.CursorPos=Current.Document.TextInput.CursorPos+#e-1
end,
Keys={
keys.leftCtrl,
keys.v
},
Enabled=function()
return Current.Document~=nil and(not Clipboard.isEmpty())and Clipboard.Type=='text'
end
},
{
Separator=true,
},
{
Title='Select All',
Click=function()
Current.Selection={1,#Current.Document.TextInput.Value:gsub('\n','')}
end,
Keys={
keys.leftCtrl,
keys.a
},
Enabled=function()
return Current.Document~=nil
end
}
},e,true)
else
Current.Menu=nil
end
return true
end,'Edit',colours.lightGrey,false),
Button:Initialise(13,1,nil,nil,colours.grey,Current.MenuBar,function(e,t,t,t,t)
if not e.Toggle then
Menu:New(13,2,{
{
Title='Red',
Click=function(e)
Current.Document:SetSelectionColour(e.Colour)
end,
Colour=colours.red,
Enabled=function()
return(Current.Document~=nil and Current.Selection~=nil and Current.Selection[1]~=nil and Current.Selection[2]~=nil)
end
},
{
Title='Orange',
Click=function(e)
Current.Document:SetSelectionColour(e.Colour)
end,
Colour=colours.orange,
Enabled=function()
return(Current.Document~=nil and Current.Selection~=nil and Current.Selection[1]~=nil and Current.Selection[2]~=nil)
end
},
{
Title='Yellow',
Click=function(e)
Current.Document:SetSelectionColour(e.Colour)
end,
Colour=colours.yellow,
Enabled=function()
return(Current.Document~=nil and Current.Selection~=nil and Current.Selection[1]~=nil and Current.Selection[2]~=nil)
end
},
{
Title='Pink',
Click=function(e)
Current.Document:SetSelectionColour(e.Colour)
end,
Colour=colours.pink,
Enabled=function()
return(Current.Document~=nil and Current.Selection~=nil and Current.Selection[1]~=nil and Current.Selection[2]~=nil)
end
},
{
Title='Magenta',
Click=function(e)
Current.Document:SetSelectionColour(e.Colour)
end,
Colour=colours.magenta,
Enabled=function()
return(Current.Document~=nil and Current.Selection~=nil and Current.Selection[1]~=nil and Current.Selection[2]~=nil)
end
},
{
Title='Purple',
Click=function(e)
Current.Document:SetSelectionColour(e.Colour)
end,
Colour=colours.purple,
Enabled=function()
return(Current.Document~=nil and Current.Selection~=nil and Current.Selection[1]~=nil and Current.Selection[2]~=nil)
end
},
{
Title='Light Blue',
Click=function(e)
Current.Document:SetSelectionColour(e.Colour)
end,
Colour=colours.lightBlue,
Enabled=function()
return(Current.Document~=nil and Current.Selection~=nil and Current.Selection[1]~=nil and Current.Selection[2]~=nil)
end
},
{
Title='Cyan',
Click=function(e)
Current.Document:SetSelectionColour(e.Colour)
end,
Colour=colours.cyan,
Enabled=function()
return(Current.Document~=nil and Current.Selection~=nil and Current.Selection[1]~=nil and Current.Selection[2]~=nil)
end
},
{
Title='Blue',
Click=function(e)
Current.Document:SetSelectionColour(e.Colour)
end,
Colour=colours.blue,
Enabled=function()
return(Current.Document~=nil and Current.Selection~=nil and Current.Selection[1]~=nil and Current.Selection[2]~=nil)
end
},
{
Title='Green',
Click=function(e)
Current.Document:SetSelectionColour(e.Colour)
end,
Colour=colours.green,
Enabled=function()
return(Current.Document~=nil and Current.Selection~=nil and Current.Selection[1]~=nil and Current.Selection[2]~=nil)
end
},
{
Title='Light Grey',
Click=function(e)
Current.Document:SetSelectionColour(e.Colour)
end,
Colour=colours.lightGrey,
Enabled=function()
return(Current.Document~=nil and Current.Selection~=nil and Current.Selection[1]~=nil and Current.Selection[2]~=nil)
end
},
{
Title='Grey',
Click=function(e)
Current.Document:SetSelectionColour(e.Colour)
end,
Colour=colours.grey,
Enabled=function()
return(Current.Document~=nil and Current.Selection~=nil and Current.Selection[1]~=nil and Current.Selection[2]~=nil)
end
},
{
Title='Black',
Click=function(e)
Current.Document:SetSelectionColour(e.Colour)
end,
Colour=colours.black,
Enabled=function()
return(Current.Document~=nil and Current.Selection~=nil and Current.Selection[1]~=nil and Current.Selection[2]~=nil)
end
},
{
Title='Brown',
Click=function(e)
Current.Document:SetSelectionColour(e.Colour)
end,
Colour=colours.brown,
Enabled=function()
return(Current.Document~=nil and Current.Selection~=nil and Current.Selection[1]~=nil and Current.Selection[2]~=nil)
end
}
},e,true)
else
Current.Menu=nil
end
return true
end,'Colour',colours.lightGrey,false)
})
end
function SplashScreen()
local e=colours.white
local a=colours.black
local t=colours.blue
local o=colours.lightBlue
local e={{e,e,e,a,e,e,e,a,e,e,e,},{e,e,e,a,e,e,e,a,e,e,e,},{e,e,e,a,t,t,t,a,e,e,e,},{e,a,a,t,t,t,t,t,a,a,e,},{a,t,t,o,o,t,t,t,t,t,a,},{a,t,o,o,t,t,t,t,t,t,a,},{a,t,o,o,t,t,t,t,t,t,a,},{a,t,t,t,t,t,t,t,t,t,a,},{e,a,a,a,a,a,a,a,a,a,e,},
["text"]={{" "," "," "," "," "," "," "," "," "," "," ",},{" "," "," "," "," "," "," "," "," "," "," ",},{" "," "," "," "," "," "," "," "," "," "," ",},{" "," "," "," "," "," "," "," "," "," "," ",},{" "," "," "," "," "," "," "," "," "," "," ",},{" "," "," "," ","I","n","k"," "," "," "," ",},{" "," "," "," "," "," "," "," "," "," "," ",},{" "," ","b","y"," ","o","e","e","d"," "," "},{" "," "," "," "," "," "," "," "," "," "," ",},},
["textcol"]={{e,e,e,e,e,e,e,e,e,e,e,},{e,e,e,e,e,e,e,e,e,e,e,},{e,e,e,e,e,e,e,e,e,e,e,},{e,e,e,e,e,e,e,e,e,e,e,},{e,e,e,e,e,e,e,e,e,e,e,},{e,e,e,e,e,e,e,e,e,e,e,},{e,e,e,e,e,e,e,e,e,e,e,},{o,o,o,o,o,o,o,o,o,o,o,},{e,e,e,e,e,e,e,e,e,e,e,},},}
Drawing.Clear(colours.white)
Drawing.DrawImage((Drawing.Screen.Width-11)/2,(Drawing.Screen.Height-9)/2,e,11,9)
Drawing.DrawBuffer()
Drawing.Clear(colours.black)
parallel.waitForAny(function()sleep(1)end,function()os.pullEvent('mouse_click')end)
end
function Initialise(e)
if OneOS then
fs=OneOS.FS
end
if not OneOS then
SplashScreen()
end
EventRegister('mouse_click',TryClick)
EventRegister('mouse_drag',function(a,o,t,e)TryClick(a,o,t,e,true)end)
EventRegister('mouse_scroll',Scroll)
EventRegister('key',HandleKey)
EventRegister('char',HandleKey)
EventRegister('timer',Timer)
EventRegister('terminate',function(e)if Close()then error("Terminated",0)end end)
LoadMenuBar()
if tArgs[1]then
if fs.exists(tArgs[1])then
OpenDocument(tArgs[1])
else
end
else
Current.Document=Document:Initialise('')
end
Draw()
EventHandler()
end
local t=false
controlPushedTimer=nil
closeWindowTimer=nil
function Timer(a,e)
if e==closeWindowTimer then
if Current.Window then
Current.Window:Close()
end
Draw()
elseif e==controlPushedTimer then
t=false
end
end
local o=false
function HandleKey(...)
local e={...}
local a=e[1]
local e=e[2]
if a=='key'and e==keys.leftCtrl or e==keys.rightCtrl or e==219 then
t=true
controlPushedTimer=os.startTimer(.5)
elseif t then
if a=='key'then
if CheckKeyboardShortcut(e)then
t=false
o=true
end
end
elseif o then
o=false
elseif Current.TextInput then
if a=='char'then
Current.TextInput:Char(e)
elseif a=='key'then
Current.TextInput:Key(e)
end
end
end
function CheckKeyboardShortcut(t)
local e={}
e[keys.n]=function()Current.Document=Document:Initialise('')end
e[keys.o]=function()DisplayOpenDocumentWindow()end
e[keys.s]=function()if Current.Document~=nil then SaveDocument()end end
e[keys.left]=function()if Current.TextInput then Current.TextInput:Key(keys.home)end end
e[keys.right]=function()if Current.TextInput then Current.TextInput:Key(keys["end"])end end
if Current.Document~=nil then
e[keys.s]=function()SaveDocument()end
e[keys.p]=function()PrintDocument()end
if Current.Selection and Current.Selection[1]and Current.Selection[2]~=nil then
e[keys.x]=function()Clipboard.Cut(Current.Document.TextInput:Extract(true),'text')end
e[keys.c]=function()Clipboard.Copy(Current.Document.TextInput:Extract(),'text')end
end
if(not Clipboard.isEmpty())and Clipboard.Type=='text'then
e[keys.v]=function()local e=Clipboard.Paste()
Current.Document.TextInput:Insert(e)
Current.Document.TextInput.CursorPos=Current.Document.TextInput.CursorPos+#e-1
end
end
e[keys.a]=function()Current.Selection={1,#Current.Document.TextInput.Value:gsub('\n','')}end
end
if e[t]then
e[t]()
Draw()
return true
else
return false
end
end
function CheckClick(e,a,t)
if e.X<=a and e.Y<=t and e.X+e.Width>a and e.Y+e.Height>t then
return true
end
end
function DoClick(e,n,a,o,i)
local t=GetAbsolutePosition(e)
t.Width=e.Width
t.Height=e.Height
if e and CheckClick(t,a,o)then
return e:Click(n,a-e.X+1,o-e.Y+1,i)
end
end
function TryClick(e,t,i,a,o)
if Current.Menu then
if DoClick(Current.Menu,t,i,a,o)then
Draw()
return
else
if Current.Menu.Owner and Current.Menu.Owner.Toggle then
Current.Menu.Owner.Toggle=false
end
Current.Menu=nil
Draw()
return
end
elseif Current.Window then
if DoClick(Current.Window,t,i,a,o)then
Draw()
return
else
Current.Window:Flash()
return
end
end
local e={}
table.insert(e,Current.MenuBar)
table.insert(e,Current.ScrollBar)
for a,t in ipairs(Current.Document.Pages)do
table.insert(e,t)
end
for n,e in ipairs(e)do
if DoClick(e,t,i,a,o)then
Draw()
return
end
end
Draw()
end
function Scroll(t,e,t,t)
if Current.Window and Current.Window.OpenButton then
Current.Document.Scroll=Current.Document.Scroll+e
if Current.Window.Scroll<0 then
Current.Window.Scroll=0
elseif Current.Window.Scroll>Current.Window.MaxScroll then
Current.Window.Scroll=Current.Window.MaxScroll
end
Draw()
elseif Current.ScrollBar then
if Current.ScrollBar:DoScroll(e*2)then
Draw()
end
end
end
function EventRegister(e,t)
if not Events[e]then
Events[e]={}
end
table.insert(Events[e],t)
end
function EventHandler()
while true do
local e,a,t,n,i=os.pullEventRaw()
if Events[e]then
for s,o in ipairs(Events[e])do
o(e,a,t,n,i)
end
end
end
end
local function o(e,t)
if not e then
return nil
elseif not string.find(fs.getName(e),'%.')then
if not t then
return fs.getName(e)
else
return''
end
else
local a=e
if e:sub(#e)=='/'then
a=e:sub(1,#e-1)
end
local e=a:gmatch('%.[0-9a-z]+$')()
if e then
e=e:sub(2)
else
return''
end
if t then
e='.'..e
end
return e:lower()
end
end
local o=function(e)
if e:sub(1,1)=='.'then
return e
end
local t=o(e)
if t==e then
return fs.getName(e)
end
return string.gsub(e,t,''):sub(1,-2)
end
local t=false
function PrintDocument()
if OneOS then
OneOS.LoadAPI('/System/API/Helpers.lua')
OneOS.LoadAPI('/System/API/Peripheral.lua')
OneOS.LoadAPI('/System/API/Printer.lua')
end
local e=function()
local e=PrintDocumentWindow:Initialise():Show()
end
if Peripheral.GetPeripheral('printer')==nil then
ButtonDialougeWindow:Initialise('No Printer Found','Please place a printer next to your computer. Ensure you also insert dye (left slot) and paper (top slots)','Ok',nil,function(e,t)
e:Close()
end):Show()
elseif not t and FindColours(Current.Document.TextInput.Value)~=0 then
ButtonDialougeWindow:Initialise('Important','Due to the way printers work, you can\'t print in more than one colour. The dye you use will be the colour of the text.','Ok',nil,function(a,o)
t=true
a:Close()
e()
end):Show()
else
e()
end
end
function SaveDocument()
local function a()
local e=fs.open(Current.Document.Path,'w')
if e then
if Current.Document.Format==TextFormatPlainText then
e.write(Current.Document.TextInput.Value)
else
local t={}
for o,a in ipairs(Current.Document.Pages)do
for a,e in ipairs(a.Lines)do
table.insert(t,e)
end
end
e.write(textutils.serialize(t))
end
Current.Modified=false
else
ButtonDialougeWindow:Initialise('Error','An error occured while saving the file, try again.','Ok',nil,function(e,t)
e:Close()
end):Show()
end
e.close()
end
if not Current.Document.Path then
SaveDocumentWindow:Initialise(function(o,t,e)
o:Close()
if t then
local t=''
if Current.Document.Format==TextFormatPlainText then
t='.txt'
elseif Current.Document.Format==TextFormatInkText then
t='.ink'
end
if e:sub(-4)~=t then
e=e..t
end
Current.Document.Path=e
Current.Document.Title=fs.getName(e)
a()
end
if Current.Document then
Current.TextInput=Current.Document.TextInput
end
end):Show()
else
a()
end
end
function DisplayOpenDocumentWindow()
OpenDocumentWindow:Initialise(function(a,t,e)
a:Close()
if t then
OpenDocument(e)
end
end):Show()
end
function OpenDocument(e)
Current.Selection=nil
local t=fs.open(e,'r')
if t then
Current.Document=Document:Initialise(t.readAll(),o(fs.getName(e)),e)
else
ButtonDialougeWindow:Initialise('Error','An error occured while opening the file, try again.','Ok',nil,function(e,t)
e:Close()
if Current.Document then
Current.TextInput=Current.Document.TextInput
end
end):Show()
end
t.close()
end
local a=function(e)
e='/'..e
local t=fs
if OneOS then
t=OneOS.FS
end
if t.isDir(e)then
e=e..'/'
end
e,n=e:gsub("//","/")
while n>0 do
e,n=e:gsub("//","/")
end
return e
end
OpenDocumentWindow={
X=1,
Y=1,
Width=0,
Height=0,
CursorPos=1,
Visible=true,
Return=nil,
OpenButton=nil,
PathTextBox=nil,
CurrentDirectory='/',
Scroll=0,
MaxScroll=0,
GoUpButton=nil,
SelectedFile='',
Files={},
Typed=false,
AbsolutePosition=function(e)
return{X=e.X,Y=e.Y}
end,
Draw=function(e)
if not e.Visible then
return
end
Drawing.DrawBlankArea(e.X+1,e.Y+1,e.Width,e.Height,colours.grey)
Drawing.DrawBlankArea(e.X,e.Y,e.Width,3,colours.lightGrey)
Drawing.DrawBlankArea(e.X,e.Y+1,e.Width,e.Height-6,colours.white)
Drawing.DrawCharactersCenter(e.X,e.Y,e.Width,1,e.Title,colours.black,colours.lightGrey)
Drawing.DrawBlankArea(e.X,e.Y+e.Height-5,e.Width,5,colours.lightGrey)
e:DrawFiles()
if(fs.exists(e.PathTextBox.TextInput.Value))or(e.SelectedFile and#e.SelectedFile>0 and fs.exists(e.CurrentDirectory..e.SelectedFile))then
e.OpenButton.TextColour=colours.black
else
e.OpenButton.TextColour=colours.lightGrey
end
e.PathTextBox:Draw()
e.OpenButton:Draw()
e.CancelButton:Draw()
e.GoUpButton:Draw()
end,
DrawFiles=function(e)
for a,t in ipairs(e.Files)do
if a>e.Scroll and a-e.Scroll<=11 then
if t==e.SelectedFile then
Drawing.DrawCharacters(e.X+1,e.Y+a-e.Scroll,t,colours.white,colours.lightBlue)
elseif string.find(t,'%.txt')or string.find(t,'%.text')or string.find(t,'%.ink')or fs.isDir(e.CurrentDirectory..t)then
Drawing.DrawCharacters(e.X+1,e.Y+a-e.Scroll,t,colours.black,colours.white)
else
Drawing.DrawCharacters(e.X+1,e.Y+a-e.Scroll,t,colours.grey,colours.white)
end
end
end
e.MaxScroll=#e.Files-11
if e.MaxScroll<0 then
e.MaxScroll=0
end
end,
Initialise=function(o,t)
local e={}
setmetatable(e,{__index=o})
e.Width=32
e.Height=17
e.Return=t
e.X=math.ceil((Drawing.Screen.Width-e.Width)/2)
e.Y=math.ceil((Drawing.Screen.Height-e.Height)/2)
e.Title='Open Document'
e.Visible=true
e.CurrentDirectory='/'
e.SelectedFile=nil
if OneOS and fs.exists('/Desktop/Documents/')then
e.CurrentDirectory='/Desktop/Documents/'
end
e.OpenButton=Button:Initialise(e.Width-6,e.Height-1,nil,nil,colours.white,e,function(o,i,i,i,i)
if fs.exists(e.PathTextBox.TextInput.Value)and o.TextColour==colours.black and not fs.isDir(e.PathTextBox.TextInput.Value)then
t(e,true,a(e.PathTextBox.TextInput.Value))
elseif e.SelectedFile and o.TextColour==colours.black and fs.isDir(e.CurrentDirectory..e.SelectedFile)then
e:GoToDirectory(e.CurrentDirectory..e.SelectedFile)
elseif e.SelectedFile and o.TextColour==colours.black then
t(e,true,a(e.CurrentDirectory..'/'..e.SelectedFile))
end
end,'Open',colours.black)
e.CancelButton=Button:Initialise(e.Width-15,e.Height-1,nil,nil,colours.white,e,function(a,a,a,a,a)
t(e,false)
end,'Cancel',colours.black)
e.GoUpButton=Button:Initialise(2,e.Height-1,nil,nil,colours.white,e,function(t,t,t,t,t)
local t=fs.getName(e.CurrentDirectory)
local t=e.CurrentDirectory:sub(1,#e.CurrentDirectory-#t-1)
e:GoToDirectory(t)
end,'Go Up',colours.black)
e.PathTextBox=TextBox:Initialise(2,e.Height-3,e.Width-2,1,e,e.CurrentDirectory,colours.white,colours.black)
e:GoToDirectory(e.CurrentDirectory)
return e
end,
Show=function(e)
Current.Window=e
return e
end,
Close=function(e)
Current.Input=nil
Current.Window=nil
e=nil
end,
GoToDirectory=function(e,t)
t=a(t)
e.CurrentDirectory=t
e.Scroll=0
e.SelectedFile=nil
e.Typed=false
e.PathTextBox.TextInput.Value=t
local t=fs
if OneOS then
t=OneOS.FS
end
e.Files=t.list(e.CurrentDirectory)
Draw()
end,
Flash=function(e)
e.Visible=false
Draw()
sleep(.15)
e.Visible=true
Draw()
sleep(.15)
e.Visible=false
Draw()
sleep(.15)
e.Visible=true
Draw()
end,
Click=function(e,s,i,t)
local n={e.OpenButton,e.CancelButton,e.PathTextBox,e.GoUpButton}
local o=false
for a,e in ipairs(n)do
if CheckClick(e,i,t)then
e:Click(s,i,t)
o=true
end
end
if not o then
if t<=12 then
local o=fs
if OneOS then
o=OneOS.FS
end
e.SelectedFile=o.list(e.CurrentDirectory)[t-1]
e.PathTextBox.TextInput.Value=a(e.CurrentDirectory..'/'..e.SelectedFile)
Draw()
end
end
return true
end
}
PrintDocumentWindow={
X=1,
Y=1,
Width=0,
Height=0,
CursorPos=1,
Visible=true,
Return=nil,
PrintButton=nil,
CopiesTextBox=nil,
Scroll=0,
MaxScroll=0,
PrinterSelectButton=nil,
Title='',
Status=0,
StatusText='',
SelectedPrinter=nil,
AbsolutePosition=function(e)
return{X=e.X,Y=e.Y}
end,
Draw=function(e)
if not e.Visible then
return
end
Drawing.DrawBlankArea(e.X+1,e.Y+1,e.Width,e.Height,colours.grey)
Drawing.DrawBlankArea(e.X,e.Y,e.Width,1,colours.lightGrey)
Drawing.DrawBlankArea(e.X,e.Y+1,e.Width,e.Height-1,colours.white)
Drawing.DrawCharactersCenter(e.X,e.Y,e.Width,1,e.Title,colours.black,colours.lightGrey)
e.PrinterSelectButton:Draw()
Drawing.DrawCharactersCenter(e.X,e.Y+e.PrinterSelectButton.Y-2,e.Width,1,'Printer',colours.black,colours.white)
Drawing.DrawCharacters(e.X+e.Width-3,e.Y+e.PrinterSelectButton.Y-1,'\\/',colours.black,colours.lightGrey)
Drawing.DrawCharacters(e.X+1,e.Y+e.CopiesTextBox.Y-1,'Copies',colours.black,colours.white)
local t=colours.grey
if e.Status==-1 then
t=colours.red
elseif e.Status==1 then
t=colours.green
end
Drawing.DrawCharacters(e.X+1,e.Y+e.CopiesTextBox.Y+1,e.StatusText,t,colours.white)
e.CopiesTextBox:Draw()
e.PrintButton:Draw()
e.CancelButton:Draw()
end,
Initialise=function(t)
local e={}
setmetatable(e,{__index=t})
e.Width=32
e.Height=11
e.Return=returnFunc
e.X=math.ceil((Drawing.Screen.Width-e.Width)/2)
e.Y=math.ceil((Drawing.Screen.Height-e.Height)/2)
e.Title='Print Document'
e.Visible=true
e.PrintButton=Button:Initialise(e.Width-7,e.Height-1,nil,nil,colours.lightGrey,e,function(t,t,t,t,t)
local a=true
if e.SelectedPrinter==nil then
local t=Peripheral.GetPeripheral('printer')
if t then
e.SelectedPrinter=t.Side
e.PrinterSelectButton.Text=t.Fullname
else
e.StatusText='No Connected Printer'
e.Status=-1
a=false
end
end
if a then
local t=Printer:Initialise(e.SelectedPrinter)
local t=t:PrintLines(Current.Document.Lines,Current.Document.Title,tonumber(e.CopiesTextBox.TextInput.Value))
if not t then
e.StatusText='Document Printed!'
e.Status=1
closeWindowTimer=os.startTimer(1)
else
e.StatusText=t
e.Status=-1
end
end
end,'Print',colours.black)
e.CancelButton=Button:Initialise(e.Width-15,e.Height-1,nil,nil,colours.lightGrey,e,function(t,t,t,t,t)
e:Close()
Draw()
end,'Close',colours.black)
e.PrinterSelectButton=Button:Initialise(2,4,e.Width-2,nil,colours.lightGrey,e,function(o,t,n,i,t)
local a={
{
Title="Automatic",
Click=function()
e.SelectedPrinter=nil
e.PrinterSelectButton.Text='Automatic'
end
},
{
Separator=true
}
}
for o,t in ipairs(Peripheral.GetPeripherals('printer'))do
table.insert(a,{
Title=t.Fullname,
Click=function(a)
e.SelectedPrinter=t.Side
e.PrinterSelectButton.Text=t.Fullname
end
})
end
Current.Menu=Menu:New(n,i+4,a,o,true)
end,'Automatic',colours.black)
e.CopiesTextBox=TextBox:Initialise(9,6,4,1,e,1,colours.lightGrey,colours.black,nil,true)
Current.TextInput=e.CopiesTextBox.TextInput
e.StatusText='Waiting...'
e.Status=0
return e
end,
Show=function(e)
Current.Window=e
return e
end,
Close=function(e)
Current.Input=nil
Current.Window=nil
e=nil
end,
Flash=function(e)
e.Visible=false
Draw()
sleep(.15)
e.Visible=true
Draw()
sleep(.15)
e.Visible=false
Draw()
sleep(.15)
e.Visible=true
Draw()
end,
Click=function(e,o,t,a)
local e={e.PrintButton,e.CancelButton,e.CopiesTextBox,e.PrinterSelectButton}
for i,e in ipairs(e)do
if CheckClick(e,t,a)then
e:Click(o,t,a)
end
end
return true
end
}
SaveDocumentWindow={
X=1,
Y=1,
Width=0,
Height=0,
CursorPos=1,
Visible=true,
Return=nil,
SaveButton=nil,
PathTextBox=nil,
CurrentDirectory='/',
Scroll=0,
MaxScroll=0,
ScrollBar=nil,
GoUpButton=nil,
Files={},
Typed=false,
AbsolutePosition=function(e)
return{X=e.X,Y=e.Y}
end,
Draw=function(e)
if not e.Visible then
return
end
Drawing.DrawBlankArea(e.X+1,e.Y+1,e.Width,e.Height,colours.grey)
Drawing.DrawBlankArea(e.X,e.Y,e.Width,3,colours.lightGrey)
Drawing.DrawBlankArea(e.X,e.Y+1,e.Width,e.Height-6,colours.white)
Drawing.DrawCharactersCenter(e.X,e.Y,e.Width,1,e.Title,colours.black,colours.lightGrey)
Drawing.DrawBlankArea(e.X,e.Y+e.Height-5,e.Width,5,colours.lightGrey)
Drawing.DrawCharacters(e.X+1,e.Y+e.Height-5,e.CurrentDirectory,colours.grey,colours.lightGrey)
e:DrawFiles()
if(e.PathTextBox.TextInput.Value)then
e.SaveButton.TextColour=colours.black
else
e.SaveButton.TextColour=colours.lightGrey
end
e.PathTextBox:Draw()
e.SaveButton:Draw()
e.CancelButton:Draw()
e.GoUpButton:Draw()
end,
DrawFiles=function(e)
for t,a in ipairs(e.Files)do
if t>e.Scroll and t-e.Scroll<=10 then
if a==e.SelectedFile then
Drawing.DrawCharacters(e.X+1,e.Y+t-e.Scroll,a,colours.white,colours.lightBlue)
elseif fs.isDir(e.CurrentDirectory..a)then
Drawing.DrawCharacters(e.X+1,e.Y+t-e.Scroll,a,colours.black,colours.white)
else
Drawing.DrawCharacters(e.X+1,e.Y+t-e.Scroll,a,colours.lightGrey,colours.white)
end
end
end
e.MaxScroll=#e.Files-11
if e.MaxScroll<0 then
e.MaxScroll=0
end
end,
Initialise=function(o,t)
local e={}
setmetatable(e,{__index=o})
e.Width=32
e.Height=16
e.Return=t
e.X=math.ceil((Drawing.Screen.Width-e.Width)/2)
e.Y=math.ceil((Drawing.Screen.Height-e.Height)/2)
e.Title='Save Document'
e.Visible=true
e.CurrentDirectory='/'
if OneOS and fs.exists('/Desktop/Documents/')then
e.CurrentDirectory='/Desktop/Documents/'
end
e.SaveButton=Button:Initialise(e.Width-6,e.Height-1,nil,nil,colours.white,e,function(o,i,i,i,i)
if o.TextColour==colours.black and not fs.isDir(e.CurrentDirectory..'/'..e.PathTextBox.TextInput.Value)then
t(e,true,a(e.CurrentDirectory..'/'..e.PathTextBox.TextInput.Value))
elseif e.SelectedFile and o.TextColour==colours.black and fs.isDir(e.CurrentDirectory..e.SelectedFile)then
e:GoToDirectory(e.CurrentDirectory..e.SelectedFile)
end
end,'Save',colours.black)
e.CancelButton=Button:Initialise(e.Width-15,e.Height-1,nil,nil,colours.white,e,function(a,a,a,a,a)
t(e,false)
end,'Cancel',colours.black)
e.GoUpButton=Button:Initialise(2,e.Height-1,nil,nil,colours.white,e,function(t,t,t,t,t)
local t=fs.getName(e.CurrentDirectory)
local t=e.CurrentDirectory:sub(1,#e.CurrentDirectory-#t-1)
e:GoToDirectory(t)
end,'Go Up',colours.black)
e.PathTextBox=TextBox:Initialise(2,e.Height-3,e.Width-2,1,e,'',colours.white,colours.black,function(t)
if t==keys.enter then
e.SaveButton:Click()
end
end)
e.PathTextBox.Placeholder='Document Name'
Current.TextInput=e.PathTextBox.TextInput
e:GoToDirectory(e.CurrentDirectory)
return e
end,
Show=function(e)
Current.Window=e
return e
end,
Close=function(e)
Current.Input=nil
Current.Window=nil
e=nil
end,
GoToDirectory=function(e,t)
t=a(t)
e.CurrentDirectory=t
e.Scroll=0
e.Typed=false
local t=fs
if OneOS then
t=OneOS.FS
end
e.Files=t.list(e.CurrentDirectory)
Draw()
end,
Flash=function(e)
e.Visible=false
Draw()
sleep(.15)
e.Visible=true
Draw()
sleep(.15)
e.Visible=false
Draw()
sleep(.15)
e.Visible=true
Draw()
end,
Click=function(e,n,o,t)
local i={e.SaveButton,e.CancelButton,e.PathTextBox,e.GoUpButton}
local a=false
for i,e in ipairs(i)do
if CheckClick(e,o,t)then
e:Click(n,o,t)
a=true
end
end
if not a then
if t<=11 then
local a=fs.list(e.CurrentDirectory)
if a[t-1]then
e:GoToDirectory(e.CurrentDirectory..a[t-1])
Draw()
end
end
end
return true
end
}
local i=function(t,i)
local e={''}
for a,t in t:gmatch('(%S+)(%s*)')do
local o=e[#e]..a..t:gsub('\n','')
if#o>i then
table.insert(e,'')
end
if t:find('\n')then
e[#e]=e[#e]..a
t=t:gsub('\n',function()
table.insert(e,'')
return''
end)
else
e[#e]=e[#e]..a..t
end
end
return e
end
ButtonDialougeWindow={
X=1,
Y=1,
Width=0,
Height=0,
CursorPos=1,
Visible=true,
CancelButton=nil,
OkButton=nil,
Lines={},
AbsolutePosition=function(e)
return{X=e.X,Y=e.Y}
end,
Draw=function(e)
if not e.Visible then
return
end
Drawing.DrawBlankArea(e.X+1,e.Y+1,e.Width,e.Height,colours.grey)
Drawing.DrawBlankArea(e.X,e.Y,e.Width,1,colours.lightGrey)
Drawing.DrawBlankArea(e.X,e.Y+1,e.Width,e.Height-1,colours.white)
Drawing.DrawCharactersCenter(e.X,e.Y,e.Width,1,e.Title,colours.black,colours.lightGrey)
for t,a in ipairs(e.Lines)do
Drawing.DrawCharacters(e.X+1,e.Y+1+t,a,colours.black,colours.white)
end
e.OkButton:Draw()
if e.CancelButton then
e.CancelButton:Draw()
end
end,
Initialise=function(h,s,n,t,a,o)
local e={}
setmetatable(e,{__index=h})
e.Width=28
e.Lines=i(n,e.Width-2)
e.Height=5+#e.Lines
e.Return=o
e.X=math.ceil((Drawing.Screen.Width-e.Width)/2)
e.Y=math.ceil((Drawing.Screen.Height-e.Height)/2)
e.Title=s
e.Visible=true
e.Visible=true
e.OkButton=Button:Initialise(e.Width-#t-2,e.Height-1,nil,1,nil,e,function()
o(e,true)
end,t)
if a then
e.CancelButton=Button:Initialise(e.Width-#t-2-1-#a-2,e.Height-1,nil,1,nil,e,function()
o(e,false)
end,a)
end
return e
end,
Show=function(e)
Current.Window=e
return e
end,
Close=function(e)
Current.Window=nil
e=nil
end,
Flash=function(e)
e.Visible=false
Draw()
sleep(.15)
e.Visible=true
Draw()
sleep(.15)
e.Visible=false
Draw()
sleep(.15)
e.Visible=true
Draw()
end,
Click=function(e,o,t,a)
local e={e.OkButton,e.CancelButton}
local i=false
for n,e in ipairs(e)do
if CheckClick(e,t,a)then
e:Click(o,t,a)
i=true
end
end
return true
end
}
TextDialougeWindow={
X=1,
Y=1,
Width=0,
Height=0,
CursorPos=1,
Visible=true,
CancelButton=nil,
OkButton=nil,
Lines={},
TextInput=nil,
AbsolutePosition=function(e)
return{X=e.X,Y=e.Y}
end,
Draw=function(e)
if not e.Visible then
return
end
Drawing.DrawBlankArea(e.X+1,e.Y+1,e.Width,e.Height,colours.grey)
Drawing.DrawBlankArea(e.X,e.Y,e.Width,1,colours.lightGrey)
Drawing.DrawBlankArea(e.X,e.Y+1,e.Width,e.Height-1,colours.white)
Drawing.DrawCharactersCenter(e.X,e.Y,e.Width,1,e.Title,colours.black,colours.lightGrey)
for t,a in ipairs(e.Lines)do
Drawing.DrawCharacters(e.X+1,e.Y+1+t,a,colours.black,colours.white)
end
Drawing.DrawBlankArea(e.X+1,e.Y+e.Height-4,e.Width-2,1,colours.lightGrey)
Drawing.DrawCharacters(e.X+2,e.Y+e.Height-4,e.TextInput.Value,colours.black,colours.lightGrey)
Current.CursorPos={e.X+2+e.TextInput.CursorPos,e.Y+e.Height-4}
Current.CursorColour=colours.black
e.OkButton:Draw()
if e.CancelButton then
e.CancelButton:Draw()
end
end,
Initialise=function(r,h,s,t,a,o,n)
local e={}
setmetatable(e,{__index=r})
e.Width=28
e.Lines=i(s,e.Width-2)
e.Height=7+#e.Lines
e.Return=o
e.X=math.ceil((Drawing.Screen.Width-e.Width)/2)
e.Y=math.ceil((Drawing.Screen.Height-e.Height)/2)
e.Title=h
e.Visible=true
e.Visible=true
e.OkButton=Button:Initialise(e.Width-#t-2,e.Height-1,nil,1,nil,e,function()
if#e.TextInput.Value>0 then
o(e,true,e.TextInput.Value)
end
end,t)
if a then
e.CancelButton=Button:Initialise(e.Width-#t-2-1-#a-2,e.Height-1,nil,1,nil,e,function()
o(e,false)
end,a)
end
e.TextInput=TextInput:Initialise('',function(t)
if t then
e.OkButton:Click()
end
Draw()
end,n)
Current.Input=e.TextInput
return e
end,
Show=function(e)
Current.Window=e
return e
end,
Close=function(e)
Current.Window=nil
Current.Input=nil
e=nil
end,
Flash=function(e)
e.Visible=false
Draw()
sleep(.15)
e.Visible=true
Draw()
sleep(.15)
e.Visible=false
Draw()
sleep(.15)
e.Visible=true
Draw()
end,
Click=function(a,o,e,t)
local a={a.OkButton,a.CancelButton}
local i=false
for n,a in ipairs(a)do
if CheckClick(a,e,t)then
a:Click(o,e,t)
i=true
end
end
return true
end
}
function PrintCentered(e,t)
local a,o=term.getSize()
x=math.ceil(math.ceil((a/2)-(#e/2)),0)+1
term.setCursorPos(x,t)
print(e)
end
function DoVanillaClose()
term.setBackgroundColour(colours.black)
term.setTextColour(colours.white)
term.clear()
term.setCursorPos(1,1)
PrintCentered("Thanks for using Ink!",(Drawing.Screen.Height/2)-1)
term.setTextColour(colours.lightGrey)
PrintCentered("Word Proccessor for ComputerCraft",(Drawing.Screen.Height/2))
term.setTextColour(colours.white)
PrintCentered("(c) oeed 2014",(Drawing.Screen.Height/2)+3)
term.setCursorPos(1,Drawing.Screen.Height)
error('',0)
end
function Close()
if c or not Current.Document or not Current.Modified then
if not OneOS then
DoVanillaClose()
end
return true
else
local e=ButtonDialougeWindow:Initialise('Quit Ink?','You have unsaved changes, do you want to quit anyway?','Quit','Cancel',function(t,e)
if e then
if OneOS then
OneOS.Close(true)
else
DoVanillaClose()
end
end
t:Close()
Draw()
end):Show()
os.queueEvent('mouse_click',1,e.X,e.Y)
return false
end
end
if OneOS then
OneOS.CanClose=function()
return Close()
end
end
Initialise()