ModuleControls.xba 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
  3. <!--
  4. * This file is part of the LibreOffice project.
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. *
  10. * This file incorporates work covered by the following license notice:
  11. *
  12. * Licensed to the Apache Software Foundation (ASF) under one or more
  13. * contributor license agreements. See the NOTICE file distributed
  14. * with this work for additional information regarding copyright
  15. * ownership. The ASF licenses this file to you under the Apache
  16. * License, Version 2.0 (the "License"); you may not use this file
  17. * except in compliance with the License. You may obtain a copy of
  18. * the License at http://www.apache.org/licenses/LICENSE-2.0 .
  19. -->
  20. <script:module xmlns:script="http://openoffice.org/2000/script" script:name="ModuleControls" script:language="StarBasic">Option Explicit
  21. Public DlgOverwrite as Object
  22. Public Const SBOVERWRITEUNDEFINED as Integer = 0
  23. Public Const SBOVERWRITECANCEL as Integer = 2
  24. Public Const SBOVERWRITEQUERY as Integer = 7
  25. Public Const SBOVERWRITEALWAYS as Integer = 6
  26. Public Const SBOVERWRITENEVER as Integer = 8
  27. Public iGeneralOverwrite as Integer
  28. &apos; Accepts the name of a control and returns the respective control model as object
  29. &apos; The Container can either be a whole document or a specific sheet of a Calc-Document
  30. &apos; &apos;CName&apos; is the name of the Control
  31. Function getControlModel(oContainer as Object, CName as String)
  32. Dim aForm, oForms as Object
  33. Dim i as Integer
  34. oForms = oContainer.Drawpage.GetForms
  35. For i = 0 To oForms.Count-1
  36. aForm = oForms.GetbyIndex(i)
  37. If aForm.HasByName(CName) Then
  38. GetControlModel = aForm.GetbyName(CName)
  39. Exit Function
  40. End If
  41. Next i
  42. Msgbox(&quot;No Control with the name &apos;&quot; &amp; CName &amp; &quot;&apos; found&quot; , 16, GetProductName())
  43. End Function
  44. &apos; Gets the Shape of a Control( e. g. to reset the size or Position of the control
  45. &apos; Parameters:
  46. &apos; The &apos;oContainer&apos; is the Document or a specific sheet of a Calc - Document
  47. &apos; &apos;CName&apos; is the Name of the Control
  48. Function GetControlShape(oContainer as Object,CName as String)
  49. Dim i as integer
  50. Dim aShape as Object
  51. For i = 0 to oContainer.DrawPage.Count-1
  52. aShape = oContainer.DrawPage(i)
  53. If HasUnoInterfaces(aShape, &quot;com.sun.star.drawing.XControlShape&quot;) then
  54. If ashape.Control.Name = CName then
  55. GetControlShape = aShape
  56. exit Function
  57. End If
  58. End If
  59. Next
  60. End Function
  61. &apos; Returns the View of a Control
  62. &apos; Parameters:
  63. &apos; The &apos;oContainer&apos; is the Document or a specific sheet of a Calc - Document
  64. &apos; The &apos;oController&apos; is always directly attached to the Document
  65. &apos; &apos;CName&apos; is the Name of the Control
  66. Function getControlView(oContainer , oController as Object, CName as String) as Object
  67. Dim aForm, oForms, oControlModel as Object
  68. Dim i as Integer
  69. oForms = oContainer.DrawPage.Forms
  70. For i = 0 To oForms.Count-1
  71. aForm = oforms.GetbyIndex(i)
  72. If aForm.HasByName(CName) Then
  73. oControlModel = aForm.GetbyName(CName)
  74. GetControlView = oController.GetControl(oControlModel)
  75. Exit Function
  76. End If
  77. Next i
  78. Msgbox(&quot;No Control with the name &apos;&quot; &amp; CName &amp; &quot;&apos; found&quot; , 16, GetProductName())
  79. End Function
  80. &apos; Parameters:
  81. &apos; The &apos;oContainer&apos; is the Document or a specific sheet of a Calc - Document
  82. &apos; &apos;CName&apos; is the Name of the Control
  83. Function DisposeControl(oContainer as Object, CName as String) as Boolean
  84. Dim aControl as Object
  85. aControl = GetControlModel(oContainer,CName)
  86. If not IsNull(aControl) Then
  87. aControl.Dispose()
  88. DisposeControl = True
  89. Else
  90. DisposeControl = False
  91. End If
  92. End Function
  93. &apos; Returns a sequence of a group of controls like option buttons or checkboxes
  94. &apos; The &apos;oContainer&apos; is the Document or a specific sheet of a Calc - Document
  95. &apos; &apos;sGroupName&apos; is the Name of the Controlgroup
  96. Function GetControlGroupModel(oContainer as Object, sGroupName as String )
  97. Dim aForm, oForms As Object
  98. Dim aControlModel() As Object
  99. Dim i as integer
  100. oForms = oContainer.DrawPage.Forms
  101. For i = 0 To oForms.Count-1
  102. aForm = oForms(i)
  103. If aForm.HasbyName(sGroupName) Then
  104. aForm.GetGroupbyName(sGroupName,aControlModel)
  105. GetControlGroupModel = aControlModel
  106. Exit Function
  107. End If
  108. Next i
  109. Msgbox(&quot;No Controlgroup with the name &apos;&quot; &amp; sGroupName &amp; &quot;&apos; found&quot; , 16, GetProductName())
  110. End Function
  111. &apos; Returns the Referencevalue of a group of e.g. option buttons or check boxes
  112. &apos; &apos;oControlGroup&apos; is a sequence of the Control objects
  113. Function GetRefValue(oControlGroup() as Object)
  114. Dim i as Integer
  115. For i = 0 To Ubound(oControlGroup())
  116. &apos; oControlGroup(i).DefaultState = oControlGroup(i).State
  117. If oControlGroup(i).State Then
  118. GetRefValue = oControlGroup(i).RefValue
  119. exit Function
  120. End If
  121. Next
  122. GetRefValue() = -1
  123. End Function
  124. Function GetRefValueOfControlGroup(oContainer as Object, GroupName as String)
  125. Dim oOptGroup() as Object
  126. Dim iRef as Integer
  127. oOptGroup() = GetControlGroupModel(oContainer, GroupName)
  128. iRef = GetRefValue(oOptGroup())
  129. GetRefValueofControlGroup = iRef
  130. End Function
  131. Function GetOptionGroupValue(oContainer as Object, OptGroupName as String) as Boolean
  132. Dim oRulesOptions() as Object
  133. oRulesOptions() = GetControlGroupModel(oContainer, OptGroupName)
  134. GetOptionGroupValue = oRulesOptions(0).State
  135. End Function
  136. Function WriteOptValueToCell(oSheet as Object, OptGroupName as String, iCol as Integer, iRow as Integer) as Boolean
  137. Dim bOptValue as Boolean
  138. Dim oCell as Object
  139. bOptValue = GetOptionGroupValue(oSheet, OptGroupName)
  140. oCell = oSheet.GetCellByPosition(iCol, iRow)
  141. oCell.SetValue(ABS(CInt(bOptValue)))
  142. WriteOptValueToCell() = bOptValue
  143. End Function
  144. Function LoadDialog(Libname as String, DialogName as String, Optional oLibContainer)
  145. Dim oLib as Object
  146. Dim oLibDialog as Object
  147. Dim oRuntimeDialog as Object
  148. If IsMissing(oLibContainer ) then
  149. oLibContainer = DialogLibraries
  150. End If
  151. oLibContainer.LoadLibrary(LibName)
  152. oLib = oLibContainer.GetByName(Libname)
  153. oLibDialog = oLib.GetByName(DialogName)
  154. oRuntimeDialog = CreateUnoDialog(oLibDialog)
  155. LoadDialog() = oRuntimeDialog
  156. End Function
  157. Sub GetFolderName(oRefModel as Object)
  158. Dim oFolderDialog as Object
  159. Dim iAccept as Integer
  160. Dim sPath as String
  161. Dim InitPath as String
  162. Dim RefControlName as String
  163. Dim oUcb as object
  164. &apos;Note: The following services have to be called in the following order
  165. &apos; because otherwise Basic does not remove the FileDialog Service
  166. oFolderDialog = CreateUnoService(&quot;com.sun.star.ui.dialogs.FolderPicker&quot;)
  167. oUcb = createUnoService(&quot;com.sun.star.ucb.SimpleFileAccess&quot;)
  168. InitPath = ConvertToUrl(oRefModel.Text)
  169. If InitPath = &quot;&quot; Then
  170. InitPath = GetPathSettings(&quot;Work&quot;)
  171. End If
  172. If oUcb.Exists(InitPath) Then
  173. oFolderDialog.SetDisplayDirectory(InitPath)
  174. End If
  175. iAccept = oFolderDialog.Execute()
  176. If iAccept = 1 Then
  177. sPath = oFolderDialog.GetDirectory()
  178. If oUcb.Exists(sPath) Then
  179. oRefModel.Text = ConvertFromUrl(sPath)
  180. End If
  181. End If
  182. End Sub
  183. Sub GetFileName(oRefModel as Object, Filternames())
  184. Dim oFileDialog as Object
  185. Dim iAccept as Integer
  186. Dim sPath as String
  187. Dim InitPath as String
  188. Dim RefControlName as String
  189. Dim oUcb as object
  190. &apos;Dim ListAny(0)
  191. &apos;Note: The following services have to be called in the following order
  192. &apos; because otherwise Basic does not remove the FileDialog Service
  193. oFileDialog = CreateUnoService(&quot;com.sun.star.ui.dialogs.FilePicker&quot;)
  194. oUcb = createUnoService(&quot;com.sun.star.ucb.SimpleFileAccess&quot;)
  195. &apos;ListAny(0) = com.sun.star.ui.dialogs.TemplateDescription.FILEOPEN_SIMPLE
  196. &apos;oFileDialog.initialize(ListAny())
  197. AddFiltersToDialog(FilterNames(), oFileDialog)
  198. InitPath = ConvertToUrl(oRefModel.Text)
  199. If InitPath = &quot;&quot; Then
  200. InitPath = GetPathSettings(&quot;Work&quot;)
  201. End If
  202. If oUcb.Exists(InitPath) Then
  203. oFileDialog.SetDisplayDirectory(InitPath)
  204. End If
  205. iAccept = oFileDialog.Execute()
  206. If iAccept = 1 Then
  207. sPath = oFileDialog.Files(0)
  208. If oUcb.Exists(sPath) Then
  209. oRefModel.Text = ConvertFromUrl(sPath)
  210. End If
  211. End If
  212. oFileDialog.Dispose()
  213. End Sub
  214. Function StoreDocument(oDocument as Object, FilterNames() as String, DefaultName as String, DisplayDirectory as String, Optional iAddProcedure as Integer) as String
  215. Dim NoArgs() as New com.sun.star.beans.PropertyValue
  216. Dim oStoreProperties(0) as New com.sun.star.beans.PropertyValue
  217. Dim oStoreDialog as Object
  218. Dim iAccept as Integer
  219. Dim sPath as String
  220. Dim ListAny(0) as Long
  221. Dim UIFilterName as String
  222. Dim FilterName as String
  223. Dim FilterIndex as Integer
  224. ListAny(0) = com.sun.star.ui.dialogs.TemplateDescription.FILESAVE_AUTOEXTENSION_PASSWORD
  225. oStoreDialog = CreateUnoService(&quot;com.sun.star.ui.dialogs.FilePicker&quot;)
  226. oStoreDialog.Initialize(ListAny())
  227. AddFiltersToDialog(FilterNames(), oStoreDialog)
  228. oStoreDialog.SetDisplayDirectory(DisplayDirectory)
  229. oStoreDialog.SetDefaultName(DefaultName)
  230. oStoreDialog.setValue(com.sun.star.ui.dialogs.ExtendedFilePickerElementIds.CHECKBOX_AUTOEXTENSION,0, true)
  231. iAccept = oStoreDialog.Execute()
  232. If iAccept = 1 Then
  233. sPath = oStoreDialog.Files(0)
  234. UIFilterName = oStoreDialog.GetCurrentFilter()
  235. FilterIndex = IndexInArray(UIFilterName, FilterNames())
  236. FilterName = FilterNames(FilterIndex,2)
  237. If Not IsMissing(iAddProcedure) Then
  238. Select Case iAddProcedure
  239. Case 1
  240. CommitLastDocumentChanges(sPath)
  241. End Select
  242. End If
  243. On Local Error Goto NOSAVING
  244. If FilterName = &quot;&quot; Then
  245. &apos; Todo: Catch the case that a document that has to be overwritten is writeprotected (e.g. it is open)
  246. oDocument.StoreAsUrl(sPath, NoArgs())
  247. Else
  248. oStoreProperties(0).Name = &quot;FilterName&quot;
  249. oStoreProperties(0).Value = FilterName
  250. oDocument.StoreAsUrl(sPath, oStoreProperties())
  251. End If
  252. End If
  253. oStoreDialog.dispose()
  254. StoreDocument() = sPath
  255. Exit Function
  256. NOSAVING:
  257. If Err &lt;&gt; 0 Then
  258. &apos; Msgbox(&quot;Document cannot be saved under &apos;&quot; &amp; ConvertFromUrl(sPath) &amp; &quot;&apos;&quot;, 48, GetProductName())
  259. sPath = &quot;&quot;
  260. oStoreDialog.dispose()
  261. Resume NOERROR
  262. NOERROR:
  263. End If
  264. End Function
  265. Sub AddFiltersToDialog(FilterNames() as String, oDialog as Object)
  266. Dim i as Integer
  267. Dim MaxIndex as Integer
  268. Dim ViewFiltername as String
  269. Dim oProdNameAccess as Object
  270. Dim sProdName as String
  271. oProdNameAccess = GetRegistryKeyContent(&quot;org.openoffice.Setup/Product&quot;)
  272. sProdName = oProdNameAccess.getByName(&quot;ooName&quot;)
  273. MaxIndex = Ubound(FilterNames(), 1)
  274. For i = 0 To MaxIndex
  275. Filternames(i,0) = ReplaceString(Filternames(i,0), sProdName,&quot;%productname%&quot;)
  276. oDialog.AppendFilter(FilterNames(i,0), FilterNames(i,1))
  277. Next i
  278. oDialog.SetCurrentFilter(FilterNames(0,0))
  279. End Sub
  280. Sub SwitchMousePointer(oWindowPeer as Object, bDoEnable as Boolean)
  281. Dim oWindowPointer as Object
  282. oWindowPointer = CreateUnoService(&quot;com.sun.star.awt.Pointer&quot;)
  283. If bDoEnable Then
  284. oWindowPointer.SetType(com.sun.star.awt.SystemPointer.ARROW)
  285. Else
  286. oWindowPointer.SetType(com.sun.star.awt.SystemPointer.WAIT)
  287. End If
  288. oWindowPeer.SetPointer(oWindowPointer)
  289. End Sub
  290. Sub ShowOverwriteAllDialog(FilePath as String, sTitle as String)
  291. Dim QueryString as String
  292. Dim LocRetValue as Integer
  293. Dim lblYes as String
  294. Dim lblNo as String
  295. Dim lblYesToAll as String
  296. Dim lblCancel as String
  297. Dim OverwriteModel as Object
  298. If InitResources(GetProductName()) Then
  299. QueryString = GetResText(&quot;RID_COMMON_7&quot;)
  300. QueryString = ReplaceString(QueryString, ConvertFromUrl(FilePath), &quot;&lt;PATH&gt;&quot;)
  301. If Len(QueryString) &gt; 190 Then
  302. QueryString = DeleteStr(QueryString, &quot;.&lt;BR&gt;&quot;)
  303. End If
  304. QueryString = ReplaceString(QueryString, chr(13), &quot;&lt;BR&gt;&quot;)
  305. lblYes = GetResText(&quot;RID_COMMON_8&quot;)
  306. lblYesToAll = GetResText(&quot;RID_COMMON_9&quot;)
  307. lblNo = GetResText(&quot;RID_COMMON_10&quot;)
  308. lblCancel = GetResText(&quot;RID_COMMON_11&quot;)
  309. DlgOverwrite = LoadDialog(&quot;Tools&quot;, &quot;DlgOverwriteAll&quot;)
  310. DlgOverwrite.Title = sTitle
  311. OverwriteModel = DlgOverwrite.Model
  312. OverwriteModel.cmdYes.Label = lblYes
  313. OverwriteModel.cmdYesToAll.Label = lblYesToAll
  314. OverwriteModel.cmdNo.Label = lblNo
  315. OverwriteModel.cmdCancel.Label = lblCancel
  316. OverwriteModel.lblQueryforSave.Label = QueryString
  317. OverwriteModel.cmdNo.DefaultButton = True
  318. DlgOverwrite.GetControl(&quot;cmdNo&quot;).SetFocus()
  319. iGeneralOverwrite = 999
  320. LocRetValue = DlgOverwrite.execute()
  321. If iGeneralOverwrite = 999 Then
  322. iGeneralOverwrite = SBOVERWRITECANCEL
  323. End If
  324. DlgOverwrite.dispose()
  325. Else
  326. iGeneralOverwrite = SBOVERWRITECANCEL
  327. End If
  328. End Sub
  329. Sub SetOVERWRITEToQuery()
  330. iGeneralOverwrite = SBOVERWRITEQUERY
  331. DlgOverwrite.EndExecute()
  332. End Sub
  333. Sub SetOVERWRITEToAlways()
  334. iGeneralOverwrite = SBOVERWRITEALWAYS
  335. DlgOverwrite.EndExecute()
  336. End Sub
  337. Sub SetOVERWRITEToNever()
  338. iGeneralOverwrite = SBOVERWRITENEVER
  339. DlgOverwrite.EndExecute()
  340. End Sub
  341. </script:module>