Advertisement
Guest User

Untitled

a guest
Feb 25th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.24 KB | None | 0 0
  1. --[[
  2. Basic & Common Forms and Dialogs
  3. a collection of common dialogs and forms for use in programs.
  4. These dialogs can be used even whether or not your program otherwise
  5. uses the gml library.
  6. --]]
  7. local gml=require("gml")
  8. local shell=require("shell")
  9. local filesystem=require("filesystem")
  10.  
  11. local gmlDialogs={VERSION="1.0"}
  12.  
  13.  
  14. function gmlDialogs.filePicker(mode,curDir,name,extension)
  15. checkArg(1,mode,"string")
  16. checkArg(2,path,"nil","string")
  17. checkArg(3,name,"nil","string")
  18. checkArg(4,extensions,"nil","string")
  19.  
  20. curDir=curDir or "/"
  21. if not filesystem.exists(curDir) or not filesystem.isDirectory(curDir) then
  22. error("invalid path arg to filePicker",2)
  23. end
  24.  
  25. name=name or ""
  26.  
  27. if mode=="load" then
  28. mode=false
  29. elseif mode~="save" then
  30. error("Invalid mode arg to gml.filePicker, must be \"save\" or \"load\"",2)
  31. end
  32.  
  33. local result=nil
  34.  
  35. local gui=gml.create("center","center",50,16)
  36.  
  37. gui:addLabel(1,1,14,"Common Folders")
  38. local commonDirList=gui:addListBox(1,2,16,11,{"/","/usr/","/tmp/","/lib/","/bin/"})
  39.  
  40. local contentsLabel=gui:addLabel(18,1,31,"contents of /")
  41. local directoryList=gui:addListBox(17,2,32,11,{})
  42.  
  43. local messageLabel=gui:addLabel(1,-1,30,"")
  44. messageLabel.class="error"
  45.  
  46. local filename=gui:addTextField(22,-2,26,name)
  47. local fileLabel=gui:addLabel(17,-2,5,"File:")
  48. gui:addButton(-2,-1,8,1,mode and "Save" or "Open",function()
  49. --require an actual filename
  50. local ext=filename.text
  51. local t=curDir..filename.text
  52. ext=ext:match("%.([^/]*)$")
  53. local failed
  54. local function fail(msg)
  55. messageLabel.text=msg
  56. messageLabel:draw()
  57. failed=true
  58. end
  59.  
  60. if mode then
  61. --saving
  62. if filesystem.get(curDir).isReadOnly() then
  63. fail("Directory is read-only")
  64. elseif extension then
  65. if ext then
  66. --has an extension, is it the right one?
  67. if ext~=extension then
  68. fail("Invalid extension, use "..extension)
  69. end
  70. else
  71. t=t.."."..extension
  72. end
  73. end
  74. else
  75. --loading
  76. if not filesystem.exists(t) then
  77. fail("That file doesn't exist")
  78. elseif extension then
  79. if ext and ext~=extension then
  80. fail("Invalid extension, use ."..extension)
  81. end
  82. end
  83. end
  84.  
  85. if not failed then
  86. result=t
  87. gui.close()
  88. end
  89. end)
  90.  
  91. gui:addButton(-11,-1,8,1,"Cancel",gui.close)
  92.  
  93. local function updateDirectoryList(dir)
  94. local list={}
  95. curDir=dir
  96. if dir~="/" then
  97. list[1]=".."
  98. end
  99. local nextDir=#list+1
  100. for file in filesystem.list(dir) do
  101. if filesystem.isDirectory(file) then
  102. if file:sub(-1)~="/" then
  103. file=file.."/"
  104. end
  105. table.insert(list,nextDir,file)
  106. nextDir=nextDir+1
  107. else
  108. table.insert(list,file)
  109. end
  110. end
  111. curDir=dir
  112. directoryList:updateList(list)
  113. contentsLabel.text="contents of "..curDir
  114. contentsLabel:draw()
  115. end
  116.  
  117. local function onDirSelect(lb,prevIndex,selIndex)
  118. updateDirectoryList(commonDirList:getSelected())
  119. end
  120.  
  121. commonDirList.onChange=onDirSelect
  122. local function onActivateItem()
  123. local selected=directoryList:getSelected()
  124. if selected==".." then
  125. selected=curDir:match("^(.*/)[^/]*/")
  126. else
  127. selected=curDir..selected
  128. end
  129. if filesystem.isDirectory(selected) then
  130. updateDirectoryList(selected)
  131. else
  132. filename.text=selected:match("([^/]*)$")
  133. gui:changeFocusTo(filename)
  134. end
  135. end
  136.  
  137. directoryList.onDoubleClick=onActivateItem
  138. directoryList.onEnter=onActivateItem
  139.  
  140. updateDirectoryList(curDir)
  141.  
  142. gui:run()
  143.  
  144. return result
  145. end
  146.  
  147.  
  148. function gmlDialogs.messageBox(message,buttons)
  149. checkArg(1,message,"string")
  150. checkArg(2,buttons,"table","nil")
  151.  
  152. local buttons=buttons or {"cancel","ok"}
  153. local choice
  154.  
  155. --do some figuring
  156. local lines={}
  157. message:gsub("([^\n]+)",function(line) lines[#lines+1]=line end)
  158. local i=1
  159. while i<=#lines do
  160. if #lines[i]>26 then
  161. local s,rs=lines[i],lines[i]:reverse()
  162. local pos=-26
  163. local prev=1
  164. while #s>prev+25 do
  165. local space=rs:find(" ",pos)
  166. if space then
  167. table.insert(lines,i,s:sub(prev,#s-space))
  168. prev=#s-space+2
  169. pos=-(#s-space+28)
  170. else
  171. table.insert(lines,i,s:sub(prev,prev+25))
  172. prev=prev+26
  173. pos=pos-26
  174. end
  175. i=i+1
  176. end
  177. lines[i]=s:sub(prev)
  178. end
  179. i=i+1
  180. end
  181.  
  182. local gui=gml.create("center","center",30,6+#lines)
  183.  
  184. local labels={}
  185. for i=1,#lines do
  186. labels[i]=gui:addLabel(2,1+i,26,lines[i])
  187. end
  188.  
  189. local buttonObjs={}
  190. --now the buttons
  191.  
  192. local xpos=2
  193. for i=1,#buttons do
  194. if type(buttons[i])~="string" then eror("messageBox must be passed an array of strings for buttons",2) end
  195. if i==#buttons then xpos=-2 end
  196. buttonObjs[i]=gui:addButton(xpos,-2,#buttons[i]+2,1,buttons[i],function() choice=buttons[i] gui.close() end)
  197. xpos=xpos+#buttons[i]+3
  198. end
  199.  
  200. gui:changeFocusTo(buttonObjs[#buttonObjs])
  201. gui:run()
  202.  
  203. return choice
  204. end
  205.  
  206.  
  207.  
  208. return gmlDialogs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement