SF_UI.xba 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
  3. <script:module xmlns:script="http://openoffice.org/2000/script" script:name="SF_UI" script:language="StarBasic" script:moduleType="normal">REM =======================================================================================================================
  4. REM === The ScriptForge library and its associated libraries are part of the LibreOffice project. ===
  5. REM === Full documentation is available on https://help.libreoffice.org/ ===
  6. REM =======================================================================================================================
  7. Option Compatible
  8. Option Explicit
  9. &apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;
  10. &apos;&apos;&apos; SF_UI
  11. &apos;&apos;&apos; =====
  12. &apos;&apos;&apos; Singleton class module for the identification and the manipulation of the
  13. &apos;&apos;&apos; different windows composing the whole LibreOffice application:
  14. &apos;&apos;&apos; - Windows selection
  15. &apos;&apos;&apos; - Windows moving and resizing
  16. &apos;&apos;&apos; - Statusbar settings
  17. &apos;&apos;&apos; - Creation of new windows
  18. &apos;&apos;&apos; - Access to the underlying &quot;documents&quot;
  19. &apos;&apos;&apos;
  20. &apos;&apos;&apos; WindowName: how to designate a window. It can be either
  21. &apos;&apos;&apos; a full FileName given in the notation indicated by the current value of SF_FileSystem.FileNaming
  22. &apos;&apos;&apos; or the last component of the full FileName or even only its BaseName
  23. &apos;&apos;&apos; or the title of the window
  24. &apos;&apos;&apos; or, for new documents, something like &quot;Untitled 1&quot;
  25. &apos;&apos;&apos; or one of the special windows &quot;BASICIDE&quot; and &quot;WELCOMESCREEN&quot;
  26. &apos;&apos;&apos; The window search is case-sensitive
  27. &apos;&apos;&apos;
  28. &apos;&apos;&apos; Service invocation example:
  29. &apos;&apos;&apos; Dim ui As Variant
  30. &apos;&apos;&apos; ui = CreateScriptService(&quot;UI&quot;)
  31. &apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;
  32. REM ================================================================== EXCEPTIONS
  33. Const DOCUMENTERROR = &quot;DOCUMENTERROR&quot; &apos; Requested document was not found
  34. Const DOCUMENTCREATIONERROR = &quot;DOCUMENTCREATIONERROR&quot; &apos; Incoherent arguments, new document could not be created
  35. Const DOCUMENTOPENERROR = &quot;DOCUMENTOPENERROR&quot; &apos; Document could not be opened, check the arguments
  36. Const BASEDOCUMENTOPENERROR = &quot;BASEDOCUMENTOPENERROR&quot; &apos; Id. for Base document
  37. REM ============================================================= PRIVATE MEMBERS
  38. Type Window
  39. Component As Object &apos; com.sun.star.lang.XComponent
  40. Frame As Object &apos; com.sun.star.comp.framework.Frame
  41. WindowName As String &apos; Object Name
  42. WindowTitle As String &apos; Only mean to identify new documents
  43. WindowFileName As String &apos; URL of file name
  44. DocumentType As String &apos; Writer, Calc, ...
  45. End Type
  46. &apos; The progress/status bar of the active window
  47. &apos;Private oStatusBar As Object &apos; com.sun.star.task.XStatusIndicator
  48. REM ============================================================ MODULE CONSTANTS
  49. &apos; Special windows
  50. Const BASICIDE = &quot;BASICIDE&quot;
  51. Const WELCOMESCREEN = &quot;WELCOMESCREEN&quot;
  52. &apos; Document types (only if not 1 of the special windows)
  53. Const BASEDOCUMENT = &quot;Base&quot;
  54. Const CALCDOCUMENT = &quot;Calc&quot;
  55. Const DRAWDOCUMENT = &quot;Draw&quot;
  56. Const IMPRESSDOCUMENT = &quot;Impress&quot;
  57. Const MATHDOCUMENT = &quot;Math&quot;
  58. Const WRITERDOCUMENT = &quot;Writer&quot;
  59. &apos; Window subtypes - Not supported yet
  60. Const BASETABLE = &quot;BASETABLE&quot;
  61. Const BASEQUERY = &quot;BASEQUERY&quot;
  62. Const BASEREPORT = &quot;BASEREPORT&quot;
  63. Const BASEDIAGRAM = &quot;BASEDIAGRAM&quot;
  64. &apos; Macro execution modes
  65. Const cstMACROEXECNORMAL = 0 &apos; Default, execution depends on user configuration and choice
  66. Const cstMACROEXECNEVER = 1 &apos; Macros are not executed
  67. Const cstMACROEXECALWAYS = 2 &apos; Macros are always executed
  68. REM ===================================================== CONSTRUCTOR/DESTRUCTOR
  69. REM -----------------------------------------------------------------------------
  70. Public Function Dispose() As Variant
  71. Set Dispose = Nothing
  72. End Function &apos; ScriptForge.SF_UI Explicit destructor
  73. REM ================================================================== PROPERTIES
  74. REM -----------------------------------------------------------------------------
  75. Public Function ActiveWindow() As String
  76. &apos;&apos;&apos; Returns a valid WindowName for the currently active window
  77. &apos;&apos;&apos; When &quot;&quot; is returned, the window could not be identified
  78. Dim vWindow As Window &apos; A component
  79. Dim oComp As Object &apos; com.sun.star.lang.XComponent
  80. Set oComp = StarDesktop.CurrentComponent
  81. If Not IsNull(oComp) Then
  82. vWindow = SF_UI._IdentifyWindow(oComp)
  83. With vWindow
  84. If Len(.WindowFileName) &gt; 0 Then
  85. ActiveWindow = SF_FileSystem._ConvertFromUrl(.WindowFileName)
  86. ElseIf Len(.WindowName) &gt; 0 Then
  87. ActiveWindow = .WindowName
  88. ElseIf Len(.WindowTitle) &gt; 0 Then
  89. ActiveWindow = .WindowTitle
  90. Else
  91. ActiveWindow = &quot;&quot;
  92. End If
  93. End With
  94. End If
  95. End Function &apos; ScriptForge.SF_UI.ActiveWindow
  96. REM -----------------------------------------------------------------------------
  97. Property Get MACROEXECALWAYS As Integer
  98. &apos;&apos;&apos; Macros are always executed
  99. MACROEXECALWAYS = cstMACROEXECALWAYS
  100. End Property &apos; ScriptForge.SF_UI.MACROEXECALWAYS
  101. REM -----------------------------------------------------------------------------
  102. Property Get MACROEXECNEVER As Integer
  103. &apos;&apos;&apos; Macros are not executed
  104. MACROEXECNEVER = cstMACROEXECNEVER
  105. End Property &apos; ScriptForge.SF_UI.MACROEXECNEVER
  106. REM -----------------------------------------------------------------------------
  107. Property Get MACROEXECNORMAL As Integer
  108. &apos;&apos;&apos; Default, execution depends on user configuration and choice
  109. MACROEXECNORMAL = cstMACROEXECNORMAL
  110. End Property &apos; ScriptForge.SF_UI.MACROEXECNORMAL
  111. REM -----------------------------------------------------------------------------
  112. Property Get ObjectType As String
  113. &apos;&apos;&apos; Only to enable object representation
  114. ObjectType = &quot;SF_UI&quot;
  115. End Property &apos; ScriptForge.SF_UI.ObjectType
  116. REM ===================================================================== METHODS
  117. REM -----------------------------------------------------------------------------
  118. Public Function Activate(Optional ByVal WindowName As Variant) As Boolean
  119. &apos;&apos;&apos; Make the specified window active
  120. &apos;&apos;&apos; Args:
  121. &apos;&apos;&apos; WindowName: see definitions
  122. &apos;&apos;&apos; Returns:
  123. &apos;&apos;&apos; True if the given window is found and can be activated
  124. &apos;&apos;&apos; There is no change in the actual user interface if no window matches the selection
  125. &apos;&apos;&apos; Examples:
  126. &apos;&apos;&apos; ui.Activate(&quot;C:\Me\My file.odt&quot;)
  127. Dim bActivate As Boolean &apos; Return value
  128. Dim oEnum As Object &apos; com.sun.star.container.XEnumeration
  129. Dim oComp As Object &apos; com.sun.star.lang.XComponent
  130. Dim vWindow As Window &apos; A single component
  131. Dim oContainer As Object &apos; com.sun.star.awt.XWindow
  132. Const cstThisSub = &quot;UI.Activate&quot;
  133. Const cstSubArgs = &quot;WindowName&quot;
  134. If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
  135. bActivate = False
  136. Check:
  137. If IsMissing(WindowName) Or IsEmpty(WindowName) Then WindowName = &quot;&quot;
  138. If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
  139. If Not SF_Utils._ValidateFile(WindowName, &quot;WindowName&quot;) Then GoTo Finally
  140. End If
  141. Try:
  142. Set oEnum = StarDesktop.Components().createEnumeration
  143. Do While oEnum.hasMoreElements
  144. Set oComp = oEnum.nextElement
  145. vWindow = SF_UI._IdentifyWindow(oComp)
  146. With vWindow
  147. &apos; Does the current window match the arguments ?
  148. If (Len(.WindowFileName) &gt; 0 And .WindowFileName = SF_FileSystem._ConvertToUrl(WindowName)) _
  149. Or (Len(.WindowName) &gt; 0 And .WindowName = WindowName) _
  150. Or (Len(.WindowTitle) &gt; 0 And .WindowTitle = WindowName) Then
  151. Set oContainer = vWindow.Frame.ContainerWindow
  152. With oContainer
  153. If .isVisible() = False Then .setVisible(True)
  154. .IsMinimized = False
  155. .setFocus()
  156. .toFront() &apos; Force window change in Linux
  157. Wait 1 &apos; Bypass desynchro issue in Linux
  158. End With
  159. bActivate = True
  160. Exit Do
  161. End If
  162. End With
  163. Loop
  164. Finally:
  165. Activate = bActivate
  166. SF_Utils._ExitFunction(cstThisSub)
  167. Exit Function
  168. Catch:
  169. GoTo Finally
  170. End Function &apos; ScriptForge.SF_UI.Activate
  171. REM -----------------------------------------------------------------------------
  172. Public Function CreateBaseDocument(Optional ByVal FileName As Variant _
  173. , Optional ByVal EmbeddedDatabase As Variant _
  174. , Optional ByVal RegistrationName As Variant _
  175. ) As Object
  176. &apos;&apos;&apos; Create a new LibreOffice Base document embedding an empty database of the given type
  177. &apos;&apos;&apos; Args:
  178. &apos;&apos;&apos; FileName: Identifies the file to create. It must follow the SF_FileSystem.FileNaming notation
  179. &apos;&apos;&apos; If the file already exists, it is overwritten without warning
  180. &apos;&apos;&apos; EmbeddedDatabase: either &quot;HSQLDB&quot; (default) or &quot;FIREBIRD&quot;
  181. &apos;&apos;&apos; RegistrationName: the name used to store the new database in the databases register
  182. &apos;&apos;&apos; If &quot;&quot; (default), no registration takes place
  183. &apos;&apos;&apos; If the name already exists it is overwritten without warning
  184. &apos;&apos;&apos; Returns:
  185. &apos;&apos;&apos; A SFDocuments.SF_Document object or one of its subclasses
  186. &apos;&apos;&apos; Examples:
  187. &apos;&apos;&apos; Dim myBase As Object
  188. &apos;&apos;&apos; Set myBase = ui.CreateBaseDocument(&quot;C:\Databases\MyBaseFile.odb&quot;, &quot;FIREBIRD&quot;)
  189. Dim oCreate As Variant &apos; Return value
  190. Dim oDBContext As Object &apos; com.sun.star.sdb.DatabaseContext
  191. Dim oDatabase As Object &apos; com.sun.star.comp.dba.ODatabaseSource
  192. Dim oComp As Object &apos; Loaded component com.sun.star.lang.XComponent
  193. Dim sFileName As String &apos; Alias of FileName
  194. Dim FSO As Object &apos; Alias for FileSystem service
  195. Const cstDocType = &quot;private:factory/s&quot;
  196. Const cstThisSub = &quot;UI.CreateBaseDocument&quot;
  197. Const cstSubArgs = &quot;FileName, [EmbeddedDatabase=&quot;&quot;HSQLDB&quot;&quot;|&quot;&quot;FIREBIRD&quot;&quot;], [RegistrationName=&quot;&quot;&quot;&quot;]&quot;
  198. If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
  199. Set oCreate = Nothing
  200. Check:
  201. If IsMissing(EmbeddedDatabase) Or IsEmpty(EmbeddedDatabase) Then EmbeddedDatabase = &quot;HSQLDB&quot;
  202. If IsMissing(RegistrationName) Or IsEmpty(RegistrationName) Then RegistrationName = &quot;&quot;
  203. If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
  204. If Not SF_Utils._ValidateFile(FileName, &quot;FileName&quot;) Then GoTo Finally
  205. If Not SF_Utils._Validate(EmbeddedDatabase, &quot;EmbeddedDatabase&quot;, V_STRING, Array(&quot;HSQLDB&quot;, &quot;FIREBIRD&quot;)) Then GoTo Finally
  206. If Not SF_Utils._Validate(RegistrationName, &quot;RegistrationName&quot;, V_STRING) Then GoTo Finally
  207. End If
  208. Try:
  209. Set oDBContext = SF_Utils._GetUNOService(&quot;DatabaseContext&quot;)
  210. With oDBContext
  211. Set oDatabase = .createInstance()
  212. oDatabase.URL = &quot;sdbc:embedded:&quot; &amp; LCase(EmbeddedDatabase)
  213. &apos; Create empty Base document
  214. Set FSO = CreateScriptService(&quot;FileSystem&quot;)
  215. sFileName = FSO._ConvertToUrl(FileName)
  216. &apos; An existing file is overwritten without warning
  217. If FSO.FileExists(FileName) Then FSO.DeleteFile(FileName)
  218. If FSO.FileExists(FileName &amp; &quot;.lck&quot;) Then FSO.DeleteFile(FileName &amp; &quot;.lck&quot;)
  219. oDatabase.DatabaseDocument.storeAsURL(sFileName, Array(SF_Utils._MakePropertyValue(&quot;Overwrite&quot;, True)))
  220. &apos; Register database if requested
  221. If Len(RegistrationName) &gt; 0 Then
  222. If .hasRegisteredDatabase(RegistrationName) Then
  223. .changeDatabaseLocation(RegistrationName, sFileName)
  224. Else
  225. .registerDatabaseLocation(RegistrationName, sFileName)
  226. End If
  227. End If
  228. End With
  229. Set oCreate = OpenBaseDocument(FileName)
  230. Finally:
  231. Set CreateBaseDocument = oCreate
  232. SF_Utils._ExitFunction(cstThisSub)
  233. Exit Function
  234. Catch:
  235. GoTo Finally
  236. End Function &apos; ScriptForge.SF_UI.CreateBaseDocument
  237. REM -----------------------------------------------------------------------------
  238. Public Function CreateDocument(Optional ByVal DocumentType As Variant _
  239. , Optional ByVal TemplateFile As Variant _
  240. , Optional ByVal Hidden As Variant _
  241. ) As Object
  242. &apos;&apos;&apos; Create a new LibreOffice document of a given type or based on a given template
  243. &apos;&apos;&apos; Args:
  244. &apos;&apos;&apos; DocumentType: &quot;Calc&quot;, &quot;Writer&quot;, etc. If absent, a TemplateFile must be given
  245. &apos;&apos;&apos; TemplateFile: the full FileName of the template to build the new document on
  246. &apos;&apos;&apos; If the file does not exist, the argument is ignored
  247. &apos;&apos;&apos; The &quot;FileSystem&quot; service provides the TemplatesFolder and UserTemplatesFolder
  248. &apos;&apos;&apos; properties to help to build the argument
  249. &apos;&apos;&apos; Hidden: if True, open in the background (default = False)
  250. &apos;&apos;&apos; To use with caution: activation or closure can only happen programmatically
  251. &apos;&apos;&apos; Returns:
  252. &apos;&apos;&apos; A SFDocuments.SF_Document object or one of its subclasses
  253. &apos;&apos;&apos; Exceptions:
  254. &apos;&apos;&apos; DOCUMENTCREATIONERROR Wrong arguments
  255. &apos;&apos;&apos; Examples:
  256. &apos;&apos;&apos; Dim myDoc1 As Object, myDoc2 As Object, FSO As Object
  257. &apos;&apos;&apos; Set myDoc1 = ui.CreateDocument(&quot;Calc&quot;)
  258. &apos;&apos;&apos; Set FSO = CreateScriptService(&quot;FileSystem&quot;)
  259. &apos;&apos;&apos; Set myDoc2 = ui.CreateDocument(, FSO.BuildPath(FSO.TemplatesFolder, &quot;personal/CV.ott&quot;))
  260. Dim oCreate As Variant &apos; Return value
  261. Dim vProperties As Variant &apos; Array of com.sun.star.beans.PropertyValue
  262. Dim bTemplateExists As Boolean &apos; True if TemplateFile is valid
  263. Dim sNew As String &apos; File url
  264. Dim oComp As Object &apos; Loaded component com.sun.star.lang.XComponent
  265. Const cstDocType = &quot;private:factory/s&quot;
  266. Const cstThisSub = &quot;UI.CreateDocument&quot;
  267. Const cstSubArgs = &quot;[DocumentType=&quot;&quot;&quot;&quot;], [TemplateFile=&quot;&quot;&quot;&quot;], [Hidden=False]&quot;
  268. &apos;&gt;&gt;&gt; If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
  269. Set oCreate = Nothing
  270. Check:
  271. If IsMissing(DocumentType) Or IsEmpty(DocumentType) Then DocumentType = &quot;&quot;
  272. If IsMissing(TemplateFile) Or IsEmpty(TemplateFile) Then TemplateFile = &quot;&quot;
  273. If IsMissing(Hidden) Or IsEmpty(Hidden) Then Hidden = False
  274. If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
  275. If Not SF_Utils._Validate(DocumentType, &quot;DocumentType&quot;, V_STRING _
  276. , Array(&quot;&quot;, BASEDOCUMENT, CALCDOCUMENT, DRAWDOCUMENT _
  277. , IMPRESSDOCUMENT, MATHDOCUMENT, WRITERDOCUMENT)) Then GoTo Finally
  278. If Not SF_Utils._ValidateFile(TemplateFile, &quot;TemplateFile&quot;, , True) Then GoTo Finally
  279. If Not SF_Utils._Validate(Hidden, &quot;Hidden&quot;, V_BOOLEAN) Then GoTo Finally
  280. End If
  281. If Len(DocumentType) + Len(TemplateFile) = 0 Then GoTo CatchError
  282. If Len(TemplateFile) &gt; 0 Then bTemplateExists = SF_FileSystem.FileExists(TemplateFile) Else bTemplateExists = False
  283. If Len(DocumentType) = 0 Then
  284. If Not bTemplateExists Then GoTo CatchError
  285. End If
  286. Try:
  287. If bTemplateExists Then sNew = SF_FileSystem._ConvertToUrl(TemplateFile) Else sNew = cstDocType &amp; LCase(DocumentType)
  288. vProperties = Array( _
  289. SF_Utils._MakePropertyValue(&quot;AsTemplate&quot;, bTemplateExists) _
  290. , SF_Utils._MakePropertyValue(&quot;Hidden&quot;, Hidden) _
  291. )
  292. Set oComp = StarDesktop.loadComponentFromURL(sNew, &quot;_blank&quot;, 0, vProperties)
  293. If Not IsNull(oComp) Then Set oCreate = CreateScriptService(&quot;SFDocuments.Document&quot;, oComp)
  294. Finally:
  295. Set CreateDocument = oCreate
  296. SF_Utils._ExitFunction(cstThisSub)
  297. Exit Function
  298. Catch:
  299. GoTo Finally
  300. CatchError:
  301. SF_Exception.RaiseFatal(DOCUMENTCREATIONERROR, &quot;DocumentType&quot;, DocumentType, &quot;TemplateFile&quot;, TemplateFile)
  302. GoTo Finally
  303. End Function &apos; ScriptForge.SF_UI.CreateDocument
  304. REM -----------------------------------------------------------------------------
  305. Public Function Documents() As Variant
  306. &apos;&apos;&apos; Returns the list of the currently open documents. Special windows are ignored.
  307. &apos;&apos;&apos; Returns:
  308. &apos;&apos;&apos; A zero-based 1D array of filenames (in SF_FileSystem.FileNaming notation)
  309. &apos;&apos;&apos; or of window titles for unsaved documents
  310. &apos;&apos;&apos; Examples:
  311. &apos;&apos;&apos; Dim vDocs As Variant, sDoc As String
  312. &apos;&apos;&apos; vDocs = ui.Documents()
  313. &apos;&apos;&apos; For each sDoc In vDocs
  314. &apos;&apos;&apos; ...
  315. Dim vDocuments As Variant &apos; Return value
  316. Dim oEnum As Object &apos; com.sun.star.container.XEnumeration
  317. Dim oComp As Object &apos; com.sun.star.lang.XComponent
  318. Dim vWindow As Window &apos; A single component
  319. Const cstThisSub = &quot;UI.Documents&quot;
  320. Const cstSubArgs = &quot;&quot;
  321. If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
  322. vDocuments = Array()
  323. Check:
  324. SF_Utils._EnterFunction(cstThisSub, cstSubArgs)
  325. Try:
  326. Set oEnum = StarDesktop.Components().createEnumeration
  327. Do While oEnum.hasMoreElements
  328. Set oComp = oEnum.nextElement
  329. vWindow = SF_UI._IdentifyWindow(oComp)
  330. With vWindow
  331. If Len(.WindowFileName) &gt; 0 Then
  332. vDocuments = SF_Array.Append(vDocuments, SF_FileSystem._ConvertFromUrl(.WindowFileName))
  333. ElseIf Len(.WindowTitle) &gt; 0 Then
  334. vDocuments = SF_Array.Append(vDocuments, .WindowTitle)
  335. End If
  336. End With
  337. Loop
  338. Finally:
  339. Documents = vDocuments
  340. SF_Utils._ExitFunction(cstThisSub)
  341. Exit Function
  342. Catch:
  343. GoTo Finally
  344. End Function &apos; ScriptForge.SF_UI.Documents
  345. REM -----------------------------------------------------------------------------
  346. Public Function GetDocument(Optional ByVal WindowName As Variant) As Variant
  347. &apos;&apos;&apos; Returns a SFDocuments.Document object referring to the active window or the given window
  348. &apos;&apos;&apos; Args:
  349. &apos;&apos;&apos; WindowName: see definitions. If absent the active window is considered
  350. &apos;&apos;&apos; Exceptions:
  351. &apos;&apos;&apos; DOCUMENTERROR The targeted window could not be found
  352. &apos;&apos;&apos; Examples:
  353. &apos;&apos;&apos; Dim oDoc As Object
  354. &apos;&apos;&apos; Set oDoc = ui.GetDocument
  355. &apos;&apos;&apos; oDoc.Save()
  356. Dim oDocument As Object &apos; Return value
  357. Const cstThisSub = &quot;UI.GetDocument&quot;
  358. Const cstSubArgs = &quot;[WindowName]&quot;
  359. If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
  360. Set oDocument = Nothing
  361. Check:
  362. If IsMissing(WindowName) Or IsEmpty(WindowName) Then WindowName = &quot;&quot;
  363. If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
  364. If Not SF_Utils._ValidateFile(WindowName, &quot;WindowName&quot;, , True) Then GoTo Finally
  365. End If
  366. Try:
  367. Set oDocument = SF_Services.CreateScriptService(&quot;SFDocuments.Document&quot;, WindowName)
  368. If IsNull(oDocument) Then GoTo CatchDeliver
  369. Finally:
  370. Set GetDocument = oDocument
  371. SF_Utils._ExitFunction(cstThisSub)
  372. Exit Function
  373. Catch:
  374. GoTo Finally
  375. CatchDeliver:
  376. SF_Exception.RaiseFatal(DOCUMENTERROR, &quot;WindowName&quot;, WindowName)
  377. GoTo Finally
  378. End Function &apos; ScriptForge.SF_UI.GetDocument
  379. REM -----------------------------------------------------------------------------
  380. Public Function GetProperty(Optional ByVal PropertyName As Variant) As Variant
  381. &apos;&apos;&apos; Return the actual value of the given property
  382. &apos;&apos;&apos; Args:
  383. &apos;&apos;&apos; PropertyName: the name of the property as a string
  384. &apos;&apos;&apos; Returns:
  385. &apos;&apos;&apos; The actual value of the property
  386. &apos;&apos;&apos; Exceptions
  387. &apos;&apos;&apos; ARGUMENTERROR The property does not exist
  388. Const cstThisSub = &quot;UI.GetProperty&quot;
  389. Const cstSubArgs = &quot;PropertyName&quot;
  390. If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
  391. GetProperty = Null
  392. Check:
  393. If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
  394. If Not SF_Utils._Validate(PropertyName, &quot;PropertyName&quot;, V_STRING, Properties()) Then GoTo Catch
  395. End If
  396. Try:
  397. Select Case UCase(PropertyName)
  398. Case &quot;ACTIVEWINDOW&quot; : GetProperty = ActiveWindow()
  399. Case Else
  400. End Select
  401. Finally:
  402. SF_Utils._ExitFunction(cstThisSub)
  403. Exit Function
  404. Catch:
  405. GoTo Finally
  406. End Function &apos; ScriptForge.SF_UI.GetProperty
  407. REM -----------------------------------------------------------------------------
  408. Public Sub Maximize(Optional ByVal WindowName As Variant)
  409. &apos;&apos;&apos; Maximizes the active window or the given window
  410. &apos;&apos;&apos; Args:
  411. &apos;&apos;&apos; WindowName: see definitions. If absent the active window is considered
  412. &apos;&apos;&apos; Examples:
  413. &apos;&apos;&apos; ui.Maximize
  414. &apos;&apos;&apos; ...
  415. Dim oEnum As Object &apos; com.sun.star.container.XEnumeration
  416. Dim oComp As Object &apos; com.sun.star.lang.XComponent
  417. Dim vWindow As Window &apos; A single component
  418. Dim oContainer As Object &apos; com.sun.star.awt.XWindow
  419. Dim bFound As Boolean &apos; True if window found
  420. Const cstThisSub = &quot;UI.Maximize&quot;
  421. Const cstSubArgs = &quot;[WindowName]&quot;
  422. If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
  423. Check:
  424. If IsMissing(WindowName) Or IsEmpty(WindowName) Then WindowName = &quot;&quot;
  425. If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
  426. If Not SF_Utils._ValidateFile(WindowName, &quot;WindowName&quot;, , True) Then GoTo Finally
  427. End If
  428. Try:
  429. bFound = False
  430. If Len(WindowName) &gt; 0 Then
  431. Set oEnum = StarDesktop.Components().createEnumeration
  432. Do While oEnum.hasMoreElements And Not bFound
  433. Set oComp = oEnum.nextElement
  434. vWindow = SF_UI._IdentifyWindow(oComp)
  435. With vWindow
  436. &apos; Does the current window match the arguments ?
  437. If (Len(.WindowFileName) &gt; 0 And .WindowFileName = SF_FileSystem.ConvertToUrl(WindowName)) _
  438. Or (Len(.WindowName) &gt; 0 And .WindowName = WindowName) _
  439. Or (Len(.WindowTitle) &gt; 0 And .WindowTitle = WindowName) Then bFound = True
  440. End With
  441. Loop
  442. Else
  443. vWindow = SF_UI._IdentifyWindow(StarDesktop.CurrentComponent)
  444. bFound = True
  445. End If
  446. If bFound Then
  447. Set oContainer = vWindow.Frame.ContainerWindow
  448. oContainer.IsMaximized = True
  449. End If
  450. Finally:
  451. SF_Utils._ExitFunction(cstThisSub)
  452. Exit Sub
  453. Catch:
  454. GoTo Finally
  455. End Sub &apos; ScriptForge.SF_UI.Maximize
  456. REM -----------------------------------------------------------------------------
  457. Public Sub Minimize(Optional ByVal WindowName As Variant)
  458. &apos;&apos;&apos; Minimizes the current window or the given window
  459. &apos;&apos;&apos; Args:
  460. &apos;&apos;&apos; WindowName: see definitions. If absent the current window is considered
  461. &apos;&apos;&apos; Examples:
  462. &apos;&apos;&apos; ui.Minimize(&quot;myFile.ods&quot;)
  463. &apos;&apos;&apos; ...
  464. Dim oEnum As Object &apos; com.sun.star.container.XEnumeration
  465. Dim oComp As Object &apos; com.sun.star.lang.XComponent
  466. Dim vWindow As Window &apos; A single component
  467. Dim oContainer As Object &apos; com.sun.star.awt.XWindow
  468. Dim bFound As Boolean &apos; True if window found
  469. Const cstThisSub = &quot;UI.Minimize&quot;
  470. Const cstSubArgs = &quot;[WindowName]&quot;
  471. If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
  472. Check:
  473. If IsMissing(WindowName) Or IsEmpty(WindowName) Then WindowName = &quot;&quot;
  474. If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
  475. If Not SF_Utils._ValidateFile(WindowName, &quot;WindowName&quot;, , True) Then GoTo Finally
  476. End If
  477. Try:
  478. bFound = False
  479. If Len(WindowName) &gt; 0 Then
  480. Set oEnum = StarDesktop.Components().createEnumeration
  481. Do While oEnum.hasMoreElements And Not bFound
  482. Set oComp = oEnum.nextElement
  483. vWindow = SF_UI._IdentifyWindow(oComp)
  484. With vWindow
  485. &apos; Does the current window match the arguments ?
  486. If (Len(.WindowFileName) &gt; 0 And .WindowFileName = SF_FileSystem.ConvertToUrl(WindowName)) _
  487. Or (Len(.WindowName) &gt; 0 And .WindowName = WindowName) _
  488. Or (Len(.WindowTitle) &gt; 0 And .WindowTitle = WindowName) Then bFound = True
  489. End With
  490. Loop
  491. Else
  492. vWindow = SF_UI._IdentifyWindow(StarDesktop.CurrentComponent)
  493. bFound = True
  494. End If
  495. If bFound Then
  496. Set oContainer = vWindow.Frame.ContainerWindow
  497. oContainer.IsMinimized = True
  498. End If
  499. Finally:
  500. SF_Utils._ExitFunction(cstThisSub)
  501. Exit Sub
  502. Catch:
  503. GoTo Finally
  504. End Sub &apos; ScriptForge.SF_UI.Minimize
  505. REM -----------------------------------------------------------------------------
  506. Public Function Methods() As Variant
  507. &apos;&apos;&apos; Return the list of public methods of the UI service as an array
  508. Methods = Array(&quot;Activate&quot; _
  509. , &quot;CreateBaseDocument&quot; _
  510. , &quot;CreateDocument&quot; _
  511. , &quot;Documents&quot; _
  512. , &quot;GetDocument&quot; _
  513. , &quot;Maximize&quot; _
  514. , &quot;Minimize&quot; _
  515. , &quot;OpenBaseDocument&quot; _
  516. , &quot;OpenDocument&quot; _
  517. , &quot;Resize&quot; _
  518. , &quot;SetStatusbar&quot; _
  519. , &quot;ShowProgressBar&quot; _
  520. , &quot;WindowExists&quot; _
  521. )
  522. End Function &apos; ScriptForge.SF_UI.Methods
  523. REM -----------------------------------------------------------------------------
  524. Public Function OpenBaseDocument(Optional ByVal FileName As Variant _
  525. , Optional ByVal RegistrationName As Variant _
  526. , Optional ByVal MacroExecution As Variant _
  527. ) As Object
  528. &apos;&apos;&apos; Open an existing LibreOffice Base document and return a SFDocuments.Document object
  529. &apos;&apos;&apos; Args:
  530. &apos;&apos;&apos; FileName: Identifies the file to open. It must follow the SF_FileSystem.FileNaming notation
  531. &apos;&apos;&apos; RegistrationName: the name of a registered database
  532. &apos;&apos;&apos; It is ignored if FileName &lt;&gt; &quot;&quot;
  533. &apos;&apos;&apos; MacroExecution: one of the MACROEXECxxx constants
  534. &apos;&apos;&apos; Returns:
  535. &apos;&apos;&apos; A SFDocuments.SF_Base object
  536. &apos;&apos;&apos; Null if the opening failed, including when due to a user decision
  537. &apos;&apos;&apos; Exceptions:
  538. &apos;&apos;&apos; BASEDOCUMENTOPENERROR Wrong arguments
  539. &apos;&apos;&apos; Examples:
  540. &apos;&apos;&apos; Dim mBasec As Object, FSO As Object
  541. &apos;&apos;&apos; Set myBase = ui.OpenBaseDocument(&quot;C:\Temp\myDB.odb&quot;, MacroExecution := ui.MACROEXECNEVER)
  542. Dim oOpen As Variant &apos; Return value
  543. Dim vProperties As Variant &apos; Array of com.sun.star.beans.PropertyValue
  544. Dim oDBContext As Object &apos; com.sun.star.sdb.DatabaseContext
  545. Dim oComp As Object &apos; Loaded component com.sun.star.lang.XComponent
  546. Dim sFile As String &apos; Alias for FileName
  547. Dim iMacro As Integer &apos; Alias for MacroExecution
  548. Const cstThisSub = &quot;UI.OpenBaseDocument&quot;
  549. Const cstSubArgs = &quot;[FileName=&quot;&quot;&quot;&quot;], [RegistrationName=&quot;&quot;&quot;&quot;], [MacroExecution=0|1|2]&quot;
  550. If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
  551. Set oOpen = Nothing
  552. Check:
  553. If IsMissing(FileName) Or IsEmpty(FileName) Then FileName = &quot;&quot;
  554. If IsMissing(RegistrationName) Or IsEmpty(RegistrationName) Then RegistrationName = &quot;&quot;
  555. If IsMissing(MacroExecution) Or IsEmpty(MacroExecution) Then MacroExecution = MACROEXECNORMAL
  556. If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
  557. If Not SF_Utils._ValidateFile(FileName, &quot;FileName&quot;, , True) Then GoTo Finally
  558. If Not SF_Utils._Validate(RegistrationName, &quot;RegistrationName&quot;, V_STRING) Then GoTo Finally
  559. If Not SF_Utils._Validate(MacroExecution, &quot;MacroExecution&quot;, V_NUMERIC _
  560. , Array(MACROEXECNORMAL, MACROEXECNEVER, MACROEXECALWAYS)) Then GoTo Finally
  561. End If
  562. &apos; Check the existence of FileName
  563. If Len(FileName) = 0 Then &apos; FileName has precedence over RegistrationName
  564. If Len(RegistrationName) = 0 Then GoTo CatchError
  565. Set oDBContext = SF_Utils._GetUNOService(&quot;DatabaseContext&quot;)
  566. If Not oDBContext.hasRegisteredDatabase(RegistrationName) Then GoTo CatchError
  567. FileName = SF_FileSystem._ConvertFromUrl(oDBContext.getDatabaseLocation(RegistrationName))
  568. End If
  569. If Not SF_FileSystem.FileExists(FileName) Then GoTo CatchError
  570. Try:
  571. With com.sun.star.document.MacroExecMode
  572. Select Case MacroExecution
  573. Case 0 : iMacro = .USE_CONFIG
  574. Case 1 : iMacro = .NEVER_EXECUTE
  575. Case 2 : iMacro = .ALWAYS_EXECUTE_NO_WARN
  576. End Select
  577. End With
  578. vProperties = Array(SF_Utils._MakePropertyValue(&quot;MacroExecutionMode&quot;, iMacro))
  579. sFile = SF_FileSystem._ConvertToUrl(FileName)
  580. Set oComp = StarDesktop.loadComponentFromURL(sFile, &quot;_blank&quot;, 0, vProperties)
  581. If Not IsNull(oComp) Then Set oOpen = CreateScriptService(&quot;SFDocuments.Document&quot;, oComp)
  582. Finally:
  583. Set OpenBaseDocument = oOpen
  584. SF_Utils._ExitFunction(cstThisSub)
  585. Exit Function
  586. Catch:
  587. GoTo Finally
  588. CatchError:
  589. SF_Exception.RaiseFatal(BASEDOCUMENTOPENERROR, &quot;FileName&quot;, FileName, &quot;RegistrationName&quot;, RegistrationName)
  590. GoTo Finally
  591. End Function &apos; ScriptForge.SF_UI.OpenBaseDocument
  592. REM -----------------------------------------------------------------------------
  593. Public Function OpenDocument(Optional ByVal FileName As Variant _
  594. , Optional ByVal Password As Variant _
  595. , Optional ByVal ReadOnly As Variant _
  596. , Optional ByVal Hidden As Variant _
  597. , Optional ByVal MacroExecution As Variant _
  598. , Optional ByVal FilterName As Variant _
  599. , Optional ByVal FilterOptions As Variant _
  600. ) As Object
  601. &apos;&apos;&apos; Open an existing LibreOffice document with the given options
  602. &apos;&apos;&apos; Args:
  603. &apos;&apos;&apos; FileName: Identifies the file to open. It must follow the SF_FileSystem.FileNaming notation
  604. &apos;&apos;&apos; Password: To use when the document is protected
  605. &apos;&apos;&apos; If wrong or absent while the document is protected, the user will be prompted to enter a password
  606. &apos;&apos;&apos; ReadOnly: Default = False
  607. &apos;&apos;&apos; Hidden: if True, open in the background (default = False)
  608. &apos;&apos;&apos; To use with caution: activation or closure can only happen programmatically
  609. &apos;&apos;&apos; MacroExecution: one of the MACROEXECxxx constants
  610. &apos;&apos;&apos; FilterName: the name of a filter that should be used for loading the document
  611. &apos;&apos;&apos; If present, the filter must exist
  612. &apos;&apos;&apos; FilterOptions: an optional string of options associated with the filter
  613. &apos;&apos;&apos; Returns:
  614. &apos;&apos;&apos; A SFDocuments.SF_Document object or one of its subclasses
  615. &apos;&apos;&apos; Null if the opening failed, including when due to a user decision
  616. &apos;&apos;&apos; Exceptions:
  617. &apos;&apos;&apos; DOCUMENTOPENERROR Wrong arguments
  618. &apos;&apos;&apos; Examples:
  619. &apos;&apos;&apos; Dim myDoc As Object, FSO As Object
  620. &apos;&apos;&apos; Set myDoc = ui.OpenDocument(&quot;C:\Temp\myFile.odt&quot;, MacroExecution := ui.MACROEXECNEVER)
  621. Dim oOpen As Variant &apos; Return value
  622. Dim oFilterFactory As Object &apos; com.sun.star.document.FilterFactory
  623. Dim vProperties As Variant &apos; Array of com.sun.star.beans.PropertyValue
  624. Dim oComp As Object &apos; Loaded component com.sun.star.lang.XComponent
  625. Dim sFile As String &apos; Alias for FileName
  626. Dim iMacro As Integer &apos; Alias for MacroExecution
  627. Const cstThisSub = &quot;UI.OpenDocument&quot;
  628. Const cstSubArgs = &quot;FileName, [Password=&quot;&quot;&quot;&quot;], [ReadOnly=False], [Hidden=False], [MacroExecution=0|1|2], [FilterName=&quot;&quot;&quot;&quot;], [FilterOptions=&quot;&quot;&quot;&quot;]&quot;
  629. If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
  630. Set oOpen = Nothing
  631. Check:
  632. If IsMissing(Password) Or IsEmpty(Password) Then Password = &quot;&quot;
  633. If IsMissing(ReadOnly) Or IsEmpty(ReadOnly) Then ReadOnly = False
  634. If IsMissing(Hidden) Or IsEmpty(Hidden) Then Hidden = False
  635. If IsMissing(MacroExecution) Or IsEmpty(MacroExecution) Then MacroExecution = MACROEXECNORMAL
  636. If IsMissing(FilterName) Or IsEmpty(FilterName) Then FilterName = &quot;&quot;
  637. If IsMissing(FilterOptions) Or IsEmpty(FilterOptions) Then FilterOptions = &quot;&quot;
  638. If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
  639. If Not SF_Utils._ValidateFile(FileName, &quot;FileName&quot;) Then GoTo Finally
  640. If Not SF_Utils._Validate(Password, &quot;Password&quot;, V_STRING) Then GoTo Finally
  641. If Not SF_Utils._Validate(ReadOnly, &quot;ReadOnly&quot;, V_BOOLEAN) Then GoTo Finally
  642. If Not SF_Utils._Validate(Hidden, &quot;Hidden&quot;, V_BOOLEAN) Then GoTo Finally
  643. If Not SF_Utils._Validate(MacroExecution, &quot;MacroExecution&quot;, V_NUMERIC _
  644. , Array(MACROEXECNORMAL, MACROEXECNEVER, MACROEXECALWAYS)) Then GoTo Finally
  645. If Not SF_Utils._Validate(FilterName, &quot;FilterName&quot;, V_STRING) Then GoTo Finally
  646. If Not SF_Utils._Validate(FilterOptions, &quot;FilterOptions&quot;, V_STRING) Then GoTo Finally
  647. End If
  648. &apos; Check the existence of FileName and FilterName
  649. If Not SF_FileSystem.FileExists(FileName) Then GoTo CatchError
  650. If Len(FilterName) &gt; 0 Then
  651. Set oFilterFactory = SF_Utils._GetUNOService(&quot;FilterFactory&quot;)
  652. If Not oFilterFactory.hasByName(FilterName) Then GoTo CatchError
  653. End If
  654. Try:
  655. With com.sun.star.document.MacroExecMode
  656. Select Case MacroExecution
  657. Case 0 : iMacro = .USE_CONFIG
  658. Case 1 : iMacro = .NEVER_EXECUTE
  659. Case 2 : iMacro = .ALWAYS_EXECUTE_NO_WARN
  660. End Select
  661. End With
  662. vProperties = Array( _
  663. SF_Utils._MakePropertyValue(&quot;ReadOnly&quot;, ReadOnly) _
  664. , SF_Utils._MakePropertyValue(&quot;Hidden&quot;, Hidden) _
  665. , SF_Utils._MakePropertyValue(&quot;MacroExecutionMode&quot;, iMacro) _
  666. , SF_Utils._MakePropertyValue(&quot;FilterName&quot;, FilterName) _
  667. , SF_Utils._MakePropertyValue(&quot;FilterOptions&quot;, FilterOptions) _
  668. )
  669. If Len(Password) &gt; 0 Then &apos; Password is to add only if &lt;&gt; &quot;&quot; !?
  670. vProperties = SF_Array.Append(vProperties, SF_Utils._MakePropertyValue(&quot;Password&quot;, Password))
  671. End If
  672. sFile = SF_FileSystem._ConvertToUrl(FileName)
  673. Set oComp = StarDesktop.loadComponentFromURL(sFile, &quot;_blank&quot;, 0, vProperties)
  674. If Not IsNull(oComp) Then Set oOpen = CreateScriptService(&quot;SFDocuments.Document&quot;, oComp)
  675. Finally:
  676. Set OpenDocument = oOpen
  677. SF_Utils._ExitFunction(cstThisSub)
  678. Exit Function
  679. Catch:
  680. GoTo Finally
  681. CatchError:
  682. SF_Exception.RaiseFatal(DOCUMENTOPENERROR, &quot;FileName&quot;, FileName, &quot;Password&quot;, Password, &quot;FilterName&quot;, FilterName)
  683. GoTo Finally
  684. End Function &apos; ScriptForge.SF_UI.OpenDocument
  685. REM -----------------------------------------------------------------------------
  686. Public Function Properties() As Variant
  687. &apos;&apos;&apos; Return the list or properties of the Timer class as an array
  688. Properties = Array( _
  689. &quot;ActiveWindow&quot; _
  690. )
  691. End Function &apos; ScriptForge.SF_UI.Properties
  692. REM -----------------------------------------------------------------------------
  693. Public Sub Resize(Optional ByVal Left As Variant _
  694. , Optional ByVal Top As Variant _
  695. , Optional ByVal Width As Variant _
  696. , Optional ByVal Height As Variant _
  697. )
  698. &apos;&apos;&apos; Resizes and/or moves the active window. Negative arguments are ignored.
  699. &apos;&apos;&apos; If the window was minimized or without arguments, it is restored
  700. &apos;&apos;&apos; Args:
  701. &apos;&apos;&apos; Left, Top: Distances from top and left edges of the screen
  702. &apos;&apos;&apos; Width, Height: Dimensions of the window
  703. &apos;&apos;&apos; Examples:
  704. &apos;&apos;&apos; ui.Resize(10,,500) &apos; Top and Height are unchanged
  705. &apos;&apos;&apos; ...
  706. Dim vWindow As Window &apos; A single component
  707. Dim oContainer As Object &apos; com.sun.star.awt.XWindow
  708. Dim iPosSize As Integer &apos; Computes which of the 4 arguments should be considered
  709. Const cstThisSub = &quot;UI.Resize&quot;
  710. Const cstSubArgs = &quot;[Left], [Top], [Width], [Height]&quot;
  711. If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
  712. Check:
  713. If IsMissing(Left) Or IsEmpty(Left) Then Left = -1
  714. If IsMissing(Top) Or IsEmpty(Top) Then Top = -1
  715. If IsMissing(Width) Or IsEmpty(Width) Then Width = -1
  716. If IsMissing(Height) Or IsEmpty(Height) Then Height = -1
  717. If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
  718. If Not SF_Utils._Validate(Left, &quot;Left&quot;, V_NUMERIC) Then GoTo Finally
  719. If Not SF_Utils._Validate(Top, &quot;Top&quot;, V_NUMERIC) Then GoTo Finally
  720. If Not SF_Utils._Validate(Width, &quot;Width&quot;, V_NUMERIC) Then GoTo Finally
  721. If Not SF_Utils._Validate(Height, &quot;Height&quot;, V_NUMERIC) Then GoTo Finally
  722. End If
  723. Try:
  724. vWindow = SF_UI._IdentifyWindow(StarDesktop.CurrentComponent)
  725. If Not IsNull(vWindow.Frame) Then
  726. Set oContainer = vWindow.Frame.ContainerWindow
  727. iPosSize = 0
  728. If Left &gt;= 0 Then iPosSize = iPosSize + com.sun.star.awt.PosSize.X
  729. If Top &gt;= 0 Then iPosSize = iPosSize + com.sun.star.awt.PosSize.Y
  730. If Width &gt; 0 Then iPosSize = iPosSize + com.sun.star.awt.PosSize.WIDTH
  731. If Height &gt; 0 Then iPosSize = iPosSize + com.sun.star.awt.PosSize.HEIGHT
  732. With oContainer
  733. .IsMaximized = False
  734. .IsMinimized = False
  735. .setPosSize(Left, Top, Width, Height, iPosSize)
  736. End With
  737. End If
  738. Finally:
  739. SF_Utils._ExitFunction(cstThisSub)
  740. Exit Sub
  741. Catch:
  742. GoTo Finally
  743. End Sub &apos; ScriptForge.SF_UI.Resize
  744. REM -----------------------------------------------------------------------------
  745. Public Function SetProperty(Optional ByVal PropertyName As Variant _
  746. , Optional ByRef Value As Variant _
  747. ) As Boolean
  748. &apos;&apos;&apos; Set a new value to the given property
  749. &apos;&apos;&apos; Args:
  750. &apos;&apos;&apos; PropertyName: the name of the property as a string
  751. &apos;&apos;&apos; Value: its new value
  752. &apos;&apos;&apos; Exceptions
  753. &apos;&apos;&apos; ARGUMENTERROR The property does not exist
  754. Const cstThisSub = &quot;UI.SetProperty&quot;
  755. Const cstSubArgs = &quot;PropertyName, Value&quot;
  756. If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
  757. SetProperty = False
  758. Check:
  759. If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
  760. If Not SF_Utils._Validate(PropertyName, &quot;PropertyName&quot;, V_STRING, Properties()) Then GoTo Catch
  761. End If
  762. Try:
  763. Select Case UCase(PropertyName)
  764. Case Else
  765. End Select
  766. Finally:
  767. SF_Utils._ExitFunction(cstThisSub)
  768. Exit Function
  769. Catch:
  770. GoTo Finally
  771. End Function &apos; ScriptForge.SF_UI.SetProperty
  772. REM -----------------------------------------------------------------------------
  773. Public Sub SetStatusbar(Optional ByVal Text As Variant _
  774. , Optional ByVal Percentage As Variant _
  775. )
  776. &apos;&apos;&apos; Display a text and a progressbar in the status bar of the active window
  777. &apos;&apos;&apos; Any subsequent calls in the same macro run refer to the same status bar of the same window,
  778. &apos;&apos;&apos; even if the window is not active anymore
  779. &apos;&apos;&apos; A call without arguments resets the status bar to its normal state.
  780. &apos;&apos;&apos; Args:
  781. &apos;&apos;&apos; Text: the optional text to be displayed before the progress bar
  782. &apos;&apos;&apos; Percentage: the optional degree of progress between 0 and 100
  783. &apos;&apos;&apos; Examples:
  784. &apos;&apos;&apos; Dim i As Integer
  785. &apos;&apos;&apos; For i = 0 To 100
  786. &apos;&apos;&apos; ui.SetStatusbar(&quot;Progress ...&quot;, i)
  787. &apos;&apos;&apos; Wait 50
  788. &apos;&apos;&apos; Next i
  789. &apos;&apos;&apos; ui.SetStatusbar
  790. Dim oComp As Object
  791. Dim oControl As Object
  792. Static oStatusbar As Object
  793. Const cstThisSub = &quot;UI.SetStatusbar&quot;
  794. Const cstSubArgs = &quot;[Text], [Percentage]&quot;
  795. If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
  796. Check:
  797. If IsMissing(Text) Or IsEmpty(Text) Then Text = &quot;&quot;
  798. If IsMissing(Percentage) Or IsEmpty(Percentage) Then Percentage = -1
  799. If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
  800. If Not SF_Utils._Validate(Text, &quot;Text&quot;, V_STRING) Then GoTo Finally
  801. If Not SF_Utils._Validate(Percentage, &quot;Percentage&quot;, V_NUMERIC) Then GoTo Finally
  802. End If
  803. Try:
  804. With oStatusbar
  805. If IsNull(oStatusbar) Then &apos; Initial call
  806. Set oComp = StarDesktop.CurrentComponent
  807. If Not IsNull(oComp) Then
  808. Set oControl = Nothing
  809. If SF_Session.HasUnoProperty(oComp, &quot;CurrentController&quot;) Then Set oControl = oComp.CurrentController
  810. If Not IsNull(oControl) Then
  811. If SF_Session.HasUnoMethod(oControl, &quot;getStatusIndicator&quot;) Then oStatusbar = oControl.getStatusIndicator()
  812. End If
  813. End If
  814. If Not IsNull(oStatusbar) Then
  815. .start(&quot;&quot;, 100)
  816. End If
  817. End If
  818. If Not IsNull(oStatusbar) Then
  819. If Len(Text) = 0 And Percentage = -1 Then
  820. .end()
  821. Else
  822. If Len(Text) &gt; 0 Then .setText(Text)
  823. If Percentage &gt;= 0 And Percentage &lt;= 100 Then .setValue(Percentage)
  824. End If
  825. End If
  826. End With
  827. Finally:
  828. SF_Utils._ExitFunction(cstThisSub)
  829. Exit Sub
  830. Catch:
  831. GoTo Finally
  832. End Sub &apos; ScriptForge.SF_UI.SetStatusbar
  833. REM -----------------------------------------------------------------------------
  834. Public Sub ShowProgressBar(Optional Title As Variant _
  835. , Optional ByVal Text As Variant _
  836. , Optional ByVal Percentage As Variant _
  837. )
  838. &apos;&apos;&apos; Display a non-modal dialog box. Specify its title, an explicatory text and the progress on a progressbar
  839. &apos;&apos;&apos; A call without arguments erases the progress bar dialog.
  840. &apos;&apos;&apos; The box will anyway vanish at the end of the macro run.
  841. &apos;&apos;&apos; Args:
  842. &apos;&apos;&apos; Title: the title appearing on top of the dialog box (Default = &quot;ScriptForge&quot;)
  843. &apos;&apos;&apos; Text: the optional text to be displayed above the progress bar (default = zero-length string)
  844. &apos;&apos;&apos; Percentage: the degree of progress between 0 and 100. Default = 0
  845. &apos;&apos;&apos; Examples:
  846. &apos;&apos;&apos; Dim i As Integer
  847. &apos;&apos;&apos; For i = 0 To 100
  848. &apos;&apos;&apos; ui.ShowProgressBar(, &quot;Progress ... &quot; &amp; i &amp; &quot;/100&quot;, i)
  849. &apos;&apos;&apos; Wait 50
  850. &apos;&apos;&apos; Next i
  851. &apos;&apos;&apos; ui.ShowProgressBar
  852. Dim bFirstCall As Boolean &apos; True at first invocation of method
  853. Static oDialog As Object &apos; SFDialogs.Dialog object
  854. Static oFixedText As Object &apos; SFDialogs.DialogControl object
  855. Static oProgressBar As Object &apos; SFDialogs.DialogControl object
  856. Dim sTitle As String &apos; Alias of Title
  857. Const cstThisSub = &quot;UI.ShowProgressBar&quot;
  858. Const cstSubArgs = &quot;[Title], [Text], [Percentage]&quot;
  859. If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
  860. Check:
  861. If IsMissing(Title) Or IsEmpty(Title) Then Title = &quot;&quot;
  862. If IsMissing(Text) Or IsEmpty(Text) Then Text = &quot;&quot;
  863. If IsMissing(Percentage) Or IsEmpty(Percentage) Then Percentage = -1
  864. If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
  865. If Not SF_Utils._Validate(Title, &quot;Title&quot;, V_STRING) Then GoTo Finally
  866. If Not SF_Utils._Validate(Text, &quot;Text&quot;, V_STRING) Then GoTo Finally
  867. If Not SF_Utils._Validate(Percentage, &quot;Percentage&quot;, V_NUMERIC) Then GoTo Finally
  868. End If
  869. Try:
  870. With oDialog
  871. bFirstCall = ( IsNull(oDialog) )
  872. If Not bFirstCall Then bFirstCall = Not ._IsStillAlive(False) &apos; False to not raise an error
  873. If bFirstCall Then Set oDialog = CreateScriptService(&quot;SFDialogs.Dialog&quot;, &quot;GlobalScope&quot;, &quot;ScriptForge&quot;, &quot;dlgProgress&quot;)
  874. If Not IsNull(oDialog) Then
  875. If Len(Title) = 0 And Len(Text) = 0 And Percentage = -1 Then
  876. Set oDialog = .Dispose()
  877. Else
  878. .Caption = Iif(Len(Title) &gt; 0, Title, &quot;ScriptForge&quot;)
  879. If bFirstCall Then
  880. Set oFixedText = .Controls(&quot;ProgressText&quot;)
  881. Set oProgressBar = .Controls(&quot;ProgressBar&quot;)
  882. .Controls(&quot;CloseButton&quot;).Caption = _SF_.Interface.GetText(&quot;CLOSEBUTTON&quot;)
  883. .Execute(Modal := False)
  884. End If
  885. If Len(Text) &gt; 0 Then oFixedText.Caption = Text
  886. oProgressBar.Value = Iif(Percentage &gt;= 0 And Percentage &lt;= 100, Percentage, 0)
  887. End If
  888. End If
  889. End With
  890. Finally:
  891. SF_Utils._ExitFunction(cstThisSub)
  892. Exit Sub
  893. Catch:
  894. GoTo Finally
  895. End Sub &apos; ScriptForge.SF_UI.ShowProgressBar
  896. REM -----------------------------------------------------------------------------
  897. Public Function WindowExists(Optional ByVal WindowName As Variant) As Boolean
  898. &apos;&apos;&apos; Returns True if the specified window exists
  899. &apos;&apos;&apos; Args:
  900. &apos;&apos;&apos; WindowName: see definitions
  901. &apos;&apos;&apos; Returns:
  902. &apos;&apos;&apos; True if the given window is found
  903. &apos;&apos;&apos; Examples:
  904. &apos;&apos;&apos; ui.WindowExists(&quot;C:\Me\My file.odt&quot;)
  905. Dim bWindowExists As Boolean &apos; Return value
  906. Dim oEnum As Object &apos; com.sun.star.container.XEnumeration
  907. Dim oComp As Object &apos; com.sun.star.lang.XComponent
  908. Dim vWindow As Window &apos; A single component
  909. Dim oContainer As Object &apos; com.sun.star.awt.XWindow
  910. Const cstThisSub = &quot;UI.WindowExists&quot;
  911. Const cstSubArgs = &quot;WindowName&quot;
  912. If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
  913. bWindowExists = False
  914. Check:
  915. If IsMissing(WindowName) Or IsEmpty(WindowName) Then WindowName = &quot;&quot;
  916. If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
  917. If Not SF_Utils._ValidateFile(WindowName, &quot;WindowName&quot;) Then GoTo Finally
  918. End If
  919. Try:
  920. Set oEnum = StarDesktop.Components().createEnumeration
  921. Do While oEnum.hasMoreElements
  922. Set oComp = oEnum.nextElement
  923. vWindow = SF_UI._IdentifyWindow(oComp)
  924. With vWindow
  925. &apos; Does the current window match the arguments ?
  926. If (Len(.WindowFileName) &gt; 0 And .WindowFileName = SF_FileSystem.ConvertToUrl(WindowName)) _
  927. Or (Len(.WindowName) &gt; 0 And .WindowName = WindowName) _
  928. Or (Len(.WindowTitle) &gt; 0 And .WindowTitle = WindowName) Then
  929. bWindowExists = True
  930. Exit Do
  931. End If
  932. End With
  933. Loop
  934. Finally:
  935. WindowExists = bWindowExists
  936. SF_Utils._ExitFunction(cstThisSub)
  937. Exit Function
  938. Catch:
  939. GoTo Finally
  940. End Function &apos; ScriptForge.SF_UI.WindowExists
  941. REM =========================================================== PRIVATE FUNCTIONS
  942. REM -----------------------------------------------------------------------------
  943. Public Sub _CloseProgressBar(Optional ByRef poEvent As Object)
  944. &apos;&apos;&apos; Triggered by the Close button in the dlgProgress dialog
  945. &apos;&apos;&apos; to simply close the dialog
  946. ShowProgressBar() &apos; Without arguments =&gt; close the dialog
  947. End Sub &apos; ScriptForge.SF_UI._CloseProgressBar
  948. REM -----------------------------------------------------------------------------
  949. Public Function _IdentifyWindow(ByRef poComponent As Object) As Object
  950. &apos;&apos;&apos; Return a Window object (definition on top of module) based on component given as argument
  951. &apos;&apos;&apos; Is a shortcut to explore the most relevant properties or objects bound to a UNO component
  952. Dim oWindow As Window &apos; Return value
  953. Dim sImplementation As String &apos; Component&apos;s implementationname
  954. Dim sIdentifier As String &apos; Component&apos;s identifier
  955. Dim vArg As Variant &apos; One single item of the Args UNO property
  956. Dim FSO As Object &apos; Alias for SF_FileSystem
  957. If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
  958. Set _IdentifyWindow = Nothing
  959. sImplementation = &quot;&quot; : sIdentifier = &quot;&quot;
  960. Set FSO = SF_FileSystem
  961. With oWindow
  962. Set .Frame = Nothing
  963. Set .Component = Nothing
  964. .WindowName = &quot;&quot;
  965. .WindowTitle = &quot;&quot;
  966. .WindowFileName = &quot;&quot;
  967. .DocumentType = &quot;&quot;
  968. If IsNull(poComponent) Then GoTo Finally
  969. If SF_Session.HasUnoProperty(poComponent, &quot;ImplementationName&quot;) Then sImplementation = poComponent.ImplementationName
  970. If SF_Session.HasUnoProperty(poComponent, &quot;Identifier&quot;) Then sIdentifier = poComponent.Identifier
  971. Set .Component = poComponent
  972. Select Case sImplementation
  973. Case &quot;com.sun.star.comp.basic.BasicIDE&quot;
  974. .WindowName = BASICIDE
  975. Case &quot;com.sun.star.comp.dba.ODatabaseDocument&quot; &apos; No identifier
  976. .WindowFileName = SF_Utils._GetPropertyValue(poComponent.Args, &quot;URL&quot;)
  977. If Len(.WindowFileName) &gt; 0 Then .WindowName = FSO.GetName(FSO._ConvertFromUrl(.WindowFileName))
  978. .DocumentType = BASEDOCUMENT
  979. Case &quot;org.openoffice.comp.dbu.ODatasourceBrowser&quot;
  980. Case &quot;org.openoffice.comp.dbu.OTableDesign&quot;, &quot;org.openoffice.comp.dbu.OQueryDesign&quot; &apos; Table or Query in Edit mode
  981. Case &quot;org.openoffice.comp.dbu.ORelationDesign&quot;
  982. Case &quot;com.sun.star.comp.sfx2.BackingComp&quot; &apos; Welcome screen
  983. Set .Frame = poComponent.Frame
  984. .WindowName = WELCOMESCREEN
  985. Case Else
  986. If Len(sIdentifier) &gt; 0 Then
  987. &apos; Do not use URL : it contains the TemplateFile when new documents are created from a template
  988. .WindowFileName = poComponent.Location
  989. If Len(.WindowFileName) &gt; 0 Then .WindowName = FSO.GetName(FSO._ConvertFromUrl(.WindowFileName))
  990. If SF_Session.HasUnoProperty(poComponent, &quot;Title&quot;) Then .WindowTitle = poComponent.Title
  991. Select Case sIdentifier
  992. Case &quot;com.sun.star.sdb.FormDesign&quot; &apos; Form
  993. Case &quot;com.sun.star.sdb.TextReportDesign&quot; &apos; Report
  994. Case &quot;com.sun.star.text.TextDocument&quot; &apos; Writer
  995. .DocumentType = WRITERDOCUMENT
  996. Case &quot;com.sun.star.sheet.SpreadsheetDocument&quot; &apos; Calc
  997. .DocumentType = CALCDOCUMENT
  998. Case &quot;com.sun.star.presentation.PresentationDocument&quot; &apos; Impress
  999. .DocumentType = IMPRESSDOCUMENT
  1000. Case &quot;com.sun.star.drawing.DrawingDocument&quot; &apos; Draw
  1001. .DocumentType = DRAWDOCUMENT
  1002. Case &quot;com.sun.star.formula.FormulaProperties&quot; &apos; Math
  1003. .DocumentType = MATHDOCUMENT
  1004. Case Else
  1005. End Select
  1006. End If
  1007. End Select
  1008. If IsNull(.Frame) Then
  1009. If Not IsNull(poComponent.CurrentController) Then Set .Frame = poComponent.CurrentController.Frame
  1010. End If
  1011. End With
  1012. Finally:
  1013. Set _IdentifyWindow = oWindow
  1014. Exit Function
  1015. Catch:
  1016. GoTo Finally
  1017. End Function &apos; ScriptForge.SF_UI._IdentifyWindow
  1018. REM -----------------------------------------------------------------------------
  1019. Private Function _Repr() As String
  1020. &apos;&apos;&apos; Convert the UI instance to a readable string, typically for debugging purposes (DebugPrint ...)
  1021. &apos;&apos;&apos; Args:
  1022. &apos;&apos;&apos; Return:
  1023. &apos;&apos;&apos; &quot;[UI]&quot;
  1024. _Repr = &quot;[UI]&quot;
  1025. End Function &apos; ScriptForge.SF_UI._Repr
  1026. REM ============================================ END OF SCRIPTFORGE.SF_UI
  1027. </script:module>