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 =======================================================================================================================
11 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
12 ''' SF_UI
13 ''' =====
14 ''' Singleton class module for the identification and the manipulation of the
15 ''' different windows composing the whole LibreOffice application:
16 ''' - Windows selection
17 ''' - Windows moving and resizing
18 ''' - Statusbar settings
19 ''' - Creation of new windows
20 ''' - Access to the underlying
"documents
"
22 ''' WindowName: how to designate a window. It can be either
23 ''' a full FileName given in the notation indicated by the current value of SF_FileSystem.FileNaming
24 ''' or the last component of the full FileName or even only its BaseName
25 ''' or the title of the window
26 ''' or, for new documents, something like
"Untitled
1"
27 ''' or one of the special windows
"BASICIDE
" and
"WELCOMESCREEN
"
28 ''' The window search is case-sensitive
30 ''' Service invocation example:
31 ''' Dim ui As Variant
32 ''' ui = CreateScriptService(
"UI
")
34 ''' Detailed user documentation:
35 ''' https://help.libreoffice.org/latest/en-US/text/sbasic/shared/
03/sf_ui.html?DbPAR=BASIC
37 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
39 REM ================================================================== EXCEPTIONS
41 Const DOCUMENTERROR =
"DOCUMENTERROR
" ' Requested document was not found
42 Const DOCUMENTCREATIONERROR =
"DOCUMENTCREATIONERROR
" ' Incoherent arguments, new document could not be created
43 Const DOCUMENTOPENERROR =
"DOCUMENTOPENERROR
" ' Document could not be opened, check the arguments
44 Const BASEDOCUMENTOPENERROR =
"BASEDOCUMENTOPENERROR
" ' Id. for Base document
45 Const UNKNOWNFILEERROR =
"UNKNOWNFILEERROR
" ' Calc datasource does not exist
47 REM ============================================================= PRIVATE MEMBERS
50 Component As Object
' com.sun.star.lang.XComponent
51 Frame As Object
' com.sun.star.comp.framework.Frame
52 WindowName As String
' Object Name
53 WindowTitle As String
' Only mean to identify new documents
54 WindowFileName As String
' URL of file name
55 DocumentType As String
' Writer, Calc, ...
56 ParentName As String
' Identifier of the parent Base file when Window is a subcomponent
59 ' The progress/status bar of the active window
60 'Private oStatusBar As Object
' com.sun.star.task.XStatusIndicator
62 REM ============================================================ MODULE CONSTANTS
64 ' Special windows
65 Const BASICIDE =
"BASICIDE
"
66 Const WELCOMESCREEN =
"WELCOMESCREEN
"
68 ' Document types (only if not
1 of the special windows)
69 Const BASEDOCUMENT =
"Base
"
70 Const CALCDOCUMENT =
"Calc
"
71 Const DRAWDOCUMENT =
"Draw
"
72 Const IMPRESSDOCUMENT =
"Impress
"
73 Const MATHDOCUMENT =
"Math
"
74 Const WRITERDOCUMENT =
"Writer
"
76 ' Window subtypes
77 Const TABLEDATA =
"TableData
"
78 Const QUERYDATA =
"QueryData
"
79 Const SQLDATA =
"SqlData
"
80 Const BASEREPORT =
"BaseReport
"
81 Const BASEDIAGRAM =
"BaseDiagram
"
83 ' Macro execution modes
84 Const cstMACROEXECNORMAL =
0 ' Default, execution depends on user configuration and choice
85 Const cstMACROEXECNEVER =
1 ' Macros are not executed
86 Const cstMACROEXECALWAYS =
2 ' Macros are always executed
88 REM ===================================================== CONSTRUCTOR/DESTRUCTOR
90 REM -----------------------------------------------------------------------------
91 Public Function Dispose() As Variant
93 End Function
' ScriptForge.SF_UI Explicit destructor
95 REM ================================================================== PROPERTIES
97 REM -----------------------------------------------------------------------------
98 Public Function ActiveWindow() As String
99 ''' Returns a valid WindowName for the currently active window
100 ''' When
"" is returned, the window could not be identified
102 Dim vWindow As Window
' A component
103 Dim oComp As Object
' com.sun.star.lang.XComponent
105 Set oComp = StarDesktop.CurrentComponent
106 If Not IsNull(oComp) Then
107 vWindow = SF_UI._IdentifyWindow(oComp)
109 If Len(.WindowFileName)
> 0 Then
110 ActiveWindow = SF_FileSystem._ConvertFromUrl(.WindowFileName)
111 ElseIf Len(.WindowName)
> 0 Then
112 ActiveWindow = .WindowName
113 ElseIf Len(.WindowTitle)
> 0 Then
114 ActiveWindow = .WindowTitle
116 ActiveWindow =
""
121 End Function
' ScriptForge.SF_UI.ActiveWindow
123 REM -----------------------------------------------------------------------------
124 Property Get Height() As Long
125 ''' Returns the height of the active window
126 Dim oPosSize As Object
' com.sun.star.awt.Rectangle
127 Set oPosSize = SF_UI._PosSize()
128 If Not IsNull(oPosSize) Then Height = oPosSize.Height Else Height = -
1
129 End Property
' ScriptForge.SF_UI.Height
131 REM -----------------------------------------------------------------------------
132 Property Get MACROEXECALWAYS As Integer
133 ''' Macros are always executed
134 MACROEXECALWAYS = cstMACROEXECALWAYS
135 End Property
' ScriptForge.SF_UI.MACROEXECALWAYS
137 REM -----------------------------------------------------------------------------
138 Property Get MACROEXECNEVER As Integer
139 ''' Macros are not executed
140 MACROEXECNEVER = cstMACROEXECNEVER
141 End Property
' ScriptForge.SF_UI.MACROEXECNEVER
143 REM -----------------------------------------------------------------------------
144 Property Get MACROEXECNORMAL As Integer
145 ''' Default, execution depends on user configuration and choice
146 MACROEXECNORMAL = cstMACROEXECNORMAL
147 End Property
' ScriptForge.SF_UI.MACROEXECNORMAL
149 REM -----------------------------------------------------------------------------
150 Property Get ObjectType As String
151 ''' Only to enable object representation
152 ObjectType =
"SF_UI
"
153 End Property
' ScriptForge.SF_UI.ObjectType
155 REM -----------------------------------------------------------------------------
156 Property Get ServiceName As String
157 ''' Internal use
158 ServiceName =
"ScriptForge.UI
"
159 End Property
' ScriptForge.SF_UI.ServiceName
161 REM -----------------------------------------------------------------------------
162 Property Get Width() As Long
163 ''' Returns the width of the active window
164 Dim oPosSize As Object
' com.sun.star.awt.Rectangle
165 Set oPosSize = SF_UI._PosSize()
166 If Not IsNull(oPosSize) Then Width = oPosSize.Width Else Width = -
1
167 End Property
' ScriptForge.SF_UI.Width
169 REM -----------------------------------------------------------------------------
170 Property Get X() As Long
171 ''' Returns the X coordinate of the active window
172 Dim oPosSize As Object
' com.sun.star.awt.Rectangle
173 Set oPosSize = SF_UI._PosSize()
174 If Not IsNull(oPosSize) Then X = oPosSize.X Else X = -
1
175 End Property
' ScriptForge.SF_UI.X
177 REM -----------------------------------------------------------------------------
178 Property Get Y() As Long
179 ''' Returns the Y coordinate of the active window
180 Dim oPosSize As Object
' com.sun.star.awt.Rectangle
181 Set oPosSize = SF_UI._PosSize()
182 If Not IsNull(oPosSize) Then Y = oPosSize.Y Else Y = -
1
183 End Property
' ScriptForge.SF_UI.Y
185 REM ===================================================================== METHODS
187 REM -----------------------------------------------------------------------------
188 Public Function Activate(Optional ByVal WindowName As Variant) As Boolean
189 ''' Make the specified window active
190 ''' Args:
191 ''' WindowName: see definitions
192 ''' Returns:
193 ''' True if the given window is found and can be activated
194 ''' There is no change in the actual user interface if no window matches the selection
195 ''' Examples:
196 ''' ui.Activate(
"C:\Me\My file.odt
")
198 Dim bActivate As Boolean
' Return value
199 Dim oEnum As Object
' com.sun.star.container.XEnumeration
200 Dim oComp As Object
' com.sun.star.lang.XComponent
201 Dim vWindow As Window
' A single component
202 Dim oContainer As Object
' com.sun.star.awt.XWindow
203 Const cstThisSub =
"UI.Activate
"
204 Const cstSubArgs =
"WindowName
"
206 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
210 If IsMissing(WindowName) Or IsEmpty(WindowName) Then WindowName =
""
211 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
212 If Not SF_Utils._ValidateFile(WindowName,
"WindowName
") Then GoTo Finally
216 Set oEnum = StarDesktop.Components().createEnumeration
217 Do While oEnum.hasMoreElements
218 Set oComp = oEnum.nextElement
219 vWindow = SF_UI._IdentifyWindow(oComp)
221 ' Does the current window match the arguments ?
222 If (Len(.WindowFileName)
> 0 And .WindowFileName = SF_FileSystem._ConvertToUrl(WindowName)) _
223 Or (Len(.WindowName)
> 0 And .WindowName = WindowName) _
224 Or (Len(.WindowTitle)
> 0 And .WindowTitle = WindowName) Then
225 Set oContainer = vWindow.Frame.ContainerWindow
227 If .isVisible() = False Then .setVisible(True)
230 .toFront()
' Force window change in Linux
231 Wait
1 ' Bypass desynchro issue in Linux
241 SF_Utils._ExitFunction(cstThisSub)
245 End Function
' ScriptForge.SF_UI.Activate
247 REM -----------------------------------------------------------------------------
248 Public Function CreateBaseDocument(Optional ByVal FileName As Variant _
249 , Optional ByVal EmbeddedDatabase As Variant _
250 , Optional ByVal RegistrationName As Variant _
251 , Optional ByVal CalcFileName As Variant _
253 ''' Create a new LibreOffice Base document embedding an empty database of the given type
254 ''' Args:
255 ''' FileName: Identifies the file to create. It must follow the SF_FileSystem.FileNaming notation
256 ''' If the file already exists, it is overwritten without warning
257 ''' EmbeddedDatabase: either
"HSQLDB
" (default) or
"FIREBIRD
" or
"CALC
"
258 ''' RegistrationName: the name used to store the new database in the databases register
259 ''' If
"" (default), no registration takes place
260 ''' If the name already exists it is overwritten without warning
261 ''' CalcFileName: only when EmbedddedDatabase =
"CALC
", the name of the file containing the tables as Calc sheets
262 ''' The name of the file must be given in SF_FileSystem.FileNaming notation
263 ''' The file must exist
264 ''' Returns:
265 ''' A SFDocuments.SF_Document object or one of its subclasses
266 ''' Exceptions
267 ''' UNKNOWNFILEERROR Calc datasource does not exist
268 ''' Examples:
269 ''' Dim myBase As Object, myCalcBase As Object
270 ''' Set myBase = ui.CreateBaseDocument(
"C:\Databases\MyBaseFile.odb
",
"FIREBIRD
")
271 ''' Set myCalcBase = ui.CreateBaseDocument(
"C:\Databases\MyCalcBaseFile.odb
",
"CALC
", ,
"C:\Databases\MyCalcFile.ods
")
273 Dim oCreate As Variant
' Return value
274 Dim oDBContext As Object
' com.sun.star.sdb.DatabaseContext
275 Dim oDatabase As Object
' com.sun.star.comp.dba.ODatabaseSource
276 Dim oComp As Object
' Loaded component com.sun.star.lang.XComponent
277 Dim sFileName As String
' Alias of FileName
278 Dim FSO As Object
' Alias for FileSystem service
279 Const cstDocType =
"private:factory/s
"
280 Const cstThisSub =
"UI.CreateBaseDocument
"
281 Const cstSubArgs =
"FileName, [EmbeddedDatabase=
""HSQLDB
""|
""FIREBIRD
""|
""CALC
""], [RegistrationName=
""""], [CalcFileName]
"
283 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
284 Set oCreate = Nothing
285 Set FSO = CreateScriptService(
"FileSystem
")
288 If IsMissing(EmbeddedDatabase) Or IsEmpty(EmbeddedDatabase) Then EmbeddedDatabase =
"HSQLDB
"
289 If IsMissing(RegistrationName) Or IsEmpty(RegistrationName) Then RegistrationName =
""
290 If IsMissing(CalcFileName) Or IsEmpty(CalcFileName) Then CalcFileName =
""
291 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
292 If Not SF_Utils._ValidateFile(FileName,
"FileName
") Then GoTo Finally
293 If Not SF_Utils._Validate(EmbeddedDatabase,
"EmbeddedDatabase
", V_STRING, Array(
"CALC
",
"HSQLDB
",
"FIREBIRD
")) Then GoTo Finally
294 If Not SF_Utils._Validate(RegistrationName,
"RegistrationName
", V_STRING) Then GoTo Finally
295 If UCase(EmbeddedDatabase) =
"CALC
" Then
296 If Not SF_Utils._ValidateFile(CalcFileName,
"CalcFileName
") Then GoTo Finally
297 If Not FSO.FileExists(CalcFileName) Then GoTo CatchNotExists
302 Set oDBContext = SF_Utils._GetUNOService(
"DatabaseContext
")
304 Set oDatabase = .createInstance()
305 ' Build the url link to the database
306 Select Case UCase(EmbeddedDatabase)
307 Case
"HSQLDB
",
"FIREBIRD
"
308 oDatabase.URL =
"sdbc:embedded:
" & LCase(EmbeddedDatabase)
309 Case
"CALC
"
310 oDatabase.URL =
"sdbc:calc:
" & FSO._ConvertToUrl(CalcFileName)
312 ' Create empty Base document
313 sFileName = FSO._ConvertToUrl(FileName)
314 ' An existing file is overwritten without warning
315 If FSO.FileExists(FileName) Then FSO.DeleteFile(FileName)
316 If FSO.FileExists(FileName
& ".lck
") Then FSO.DeleteFile(FileName
& ".lck
")
317 oDatabase.DatabaseDocument.storeAsURL(sFileName, Array(SF_Utils._MakePropertyValue(
"Overwrite
", True)))
318 ' Register database if requested
319 If Len(RegistrationName)
> 0 Then
320 If .hasRegisteredDatabase(RegistrationName) Then
321 .changeDatabaseLocation(RegistrationName, sFileName)
323 .registerDatabaseLocation(RegistrationName, sFileName)
328 Set oCreate = OpenBaseDocument(FileName)
331 Set CreateBaseDocument = oCreate
332 SF_Utils._ExitFunction(cstThisSub)
337 SF_Exception.RaiseFatal(UNKNOWNFILEERROR,
"CalcFileName
", CalcFileName)
339 End Function
' ScriptForge.SF_UI.CreateBaseDocument
341 REM -----------------------------------------------------------------------------
342 Public Function CreateDocument(Optional ByVal DocumentType As Variant _
343 , Optional ByVal TemplateFile As Variant _
344 , Optional ByVal Hidden As Variant _
346 ''' Create a new LibreOffice document of a given type or based on a given template
347 ''' Args:
348 ''' DocumentType:
"Calc
",
"Writer
", etc. If absent, a TemplateFile must be given
349 ''' TemplateFile: the full FileName of the template to build the new document on
350 ''' If the file does not exist, the argument is ignored
351 ''' The
"FileSystem
" service provides the TemplatesFolder and UserTemplatesFolder
352 ''' properties to help to build the argument
353 ''' Hidden: if True, open in the background (default = False)
354 ''' To use with caution: activation or closure can only happen programmatically
355 ''' Returns:
356 ''' A SFDocuments.SF_Document object or one of its subclasses
357 ''' Exceptions:
358 ''' DOCUMENTCREATIONERROR Wrong arguments
359 ''' Examples:
360 ''' Dim myDoc1 As Object, myDoc2 As Object, FSO As Object
361 ''' Set myDoc1 = ui.CreateDocument(
"Calc
")
362 ''' Set FSO = CreateScriptService(
"FileSystem
")
363 ''' Set myDoc2 = ui.CreateDocument(, FSO.BuildPath(FSO.TemplatesFolder,
"personal/CV.ott
"))
365 Dim oCreate As Variant
' Return value
366 Dim vProperties As Variant
' Array of com.sun.star.beans.PropertyValue
367 Dim bTemplateExists As Boolean
' True if TemplateFile is valid
368 Dim sNew As String
' File url
369 Dim oComp As Object
' Loaded component com.sun.star.lang.XComponent
370 Const cstDocType =
"private:factory/s
"
371 Const cstThisSub =
"UI.CreateDocument
"
372 Const cstSubArgs =
"[DocumentType=
""""], [TemplateFile=
""""], [Hidden=False]
"
374 '>>> If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
375 Set oCreate = Nothing
378 If IsMissing(DocumentType) Or IsEmpty(DocumentType) Then DocumentType =
""
379 If IsMissing(TemplateFile) Or IsEmpty(TemplateFile) Then TemplateFile =
""
380 If IsMissing(Hidden) Or IsEmpty(Hidden) Then Hidden = False
382 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
383 If Not SF_Utils._Validate(DocumentType,
"DocumentType
", V_STRING _
384 , Array(
"", BASEDOCUMENT, CALCDOCUMENT, DRAWDOCUMENT _
385 , IMPRESSDOCUMENT, MATHDOCUMENT, WRITERDOCUMENT)) Then GoTo Finally
386 If Not SF_Utils._ValidateFile(TemplateFile,
"TemplateFile
", , True) Then GoTo Finally
387 If Not SF_Utils._Validate(Hidden,
"Hidden
", V_BOOLEAN) Then GoTo Finally
390 If Len(DocumentType) + Len(TemplateFile) =
0 Then GoTo CatchError
391 If Len(TemplateFile)
> 0 Then bTemplateExists = SF_FileSystem.FileExists(TemplateFile) Else bTemplateExists = False
392 If Len(DocumentType) =
0 Then
393 If Not bTemplateExists Then GoTo CatchError
397 If bTemplateExists Then sNew = SF_FileSystem._ConvertToUrl(TemplateFile) Else sNew = cstDocType
& LCase(DocumentType)
398 vProperties = Array( _
399 SF_Utils._MakePropertyValue(
"AsTemplate
", bTemplateExists) _
400 , SF_Utils._MakePropertyValue(
"Hidden
", Hidden) _
402 Set oComp = StarDesktop.loadComponentFromURL(sNew,
"_blank
",
0, vProperties)
403 If Not IsNull(oComp) Then Set oCreate = CreateScriptService(
"SFDocuments.Document
", oComp)
406 Set CreateDocument = oCreate
407 SF_Utils._ExitFunction(cstThisSub)
412 SF_Exception.RaiseFatal(DOCUMENTCREATIONERROR,
"DocumentType
", DocumentType,
"TemplateFile
", TemplateFile)
414 End Function
' ScriptForge.SF_UI.CreateDocument
416 REM -----------------------------------------------------------------------------
417 Public Function Documents() As Variant
418 ''' Returns the list of the currently open documents. Special windows are ignored.
419 ''' Returns:
420 ''' A zero-based
1D array of filenames (in SF_FileSystem.FileNaming notation)
421 ''' or of window titles for unsaved documents
422 ''' Examples:
423 ''' Dim vDocs As Variant, sDoc As String
424 ''' vDocs = ui.Documents()
425 ''' For each sDoc In vDocs
426 ''' ...
428 Dim vDocuments As Variant
' Return value
429 Dim oEnum As Object
' com.sun.star.container.XEnumeration
430 Dim oComp As Object
' com.sun.star.lang.XComponent
431 Dim vWindow As Window
' A single component
432 Const cstThisSub =
"UI.Documents
"
433 Const cstSubArgs =
""
435 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
439 SF_Utils._EnterFunction(cstThisSub, cstSubArgs)
442 Set oEnum = StarDesktop.Components().createEnumeration
443 Do While oEnum.hasMoreElements
444 Set oComp = oEnum.nextElement
445 vWindow = SF_UI._IdentifyWindow(oComp)
447 If Len(.WindowFileName)
> 0 Then
448 vDocuments = SF_Array.Append(vDocuments, SF_FileSystem._ConvertFromUrl(.WindowFileName))
449 ElseIf Len(.WindowTitle)
> 0 Then
450 vDocuments = SF_Array.Append(vDocuments, .WindowTitle)
456 Documents = vDocuments
457 SF_Utils._ExitFunction(cstThisSub)
461 End Function
' ScriptForge.SF_UI.Documents
463 REM -----------------------------------------------------------------------------
464 Public Function GetDocument(Optional ByVal WindowName As Variant) As Variant
465 ''' Returns a SFDocuments.Document object referring to the active window or the given window
466 ''' Args:
467 ''' WindowName: when a string, see definitions. If absent the active window is considered.
468 ''' when an object, must be a UNO object of types
469 ''' com.sun.star.lang.XComponent or com.sun.star.comp.dba.ODatabaseDocument
470 ''' Exceptions:
471 ''' DOCUMENTERROR The targeted window could not be found
472 ''' Examples:
473 ''' Dim oDoc As Object
474 ''' Set oDoc = ui.GetDocument
' or Set oDoc = ui.GetDocument(ThisComponent)
475 ''' oDoc.Save()
477 Dim oDocument As Object
' Return value
478 Const cstThisSub =
"UI.GetDocument
"
479 Const cstSubArgs =
"[WindowName]
"
481 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
482 Set oDocument = Nothing
485 If IsMissing(WindowName) Or IsEmpty(WindowName) Then WindowName =
""
486 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
487 If Not SF_Utils._Validate(WindowName,
"WindowName
", Array(V_STRING, V_OBJECT)) Then GoTo Finally
488 If VarType(WindowName) = V_STRING Then
489 If Not SF_Utils._ValidateFile(WindowName,
"WindowName
", , True) Then GoTo Finally
494 Set oDocument = SF_Services.CreateScriptService(
"SFDocuments.Document
", WindowName)
495 If IsNull(oDocument) Then GoTo CatchDeliver
498 Set GetDocument = oDocument
499 SF_Utils._ExitFunction(cstThisSub)
504 SF_Exception.RaiseFatal(DOCUMENTERROR,
"WindowName
", WindowName)
506 End Function
' ScriptForge.SF_UI.GetDocument
508 REM -----------------------------------------------------------------------------
509 Public Function GetProperty(Optional ByVal PropertyName As Variant) As Variant
510 ''' Return the actual value of the given property
511 ''' Args:
512 ''' PropertyName: the name of the property as a string
513 ''' Returns:
514 ''' The actual value of the property
515 ''' Exceptions
516 ''' ARGUMENTERROR The property does not exist
518 Const cstThisSub =
"UI.GetProperty
"
519 Const cstSubArgs =
"PropertyName
"
521 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
525 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
526 If Not SF_Utils._Validate(PropertyName,
"PropertyName
", V_STRING, Properties()) Then GoTo Catch
530 Select Case UCase(PropertyName)
531 Case
"ACTIVEWINDOW
" : GetProperty = ActiveWindow()
532 Case
"HEIGHT
" : GetProperty = SF_UI.Height
533 Case
"WIDTH
" : GetProperty = SF_UI.Width
534 Case
"X
" : GetProperty = SF_UI.X
535 Case
"Y
" : GetProperty = SF_UI.Y
541 SF_Utils._ExitFunction(cstThisSub)
545 End Function
' ScriptForge.SF_UI.GetProperty
547 REM -----------------------------------------------------------------------------
548 Public Sub Maximize(Optional ByVal WindowName As Variant)
549 ''' Maximizes the active window or the given window
550 ''' Args:
551 ''' WindowName: see definitions. If absent the active window is considered
552 ''' Examples:
553 ''' ui.Maximize
554 ''' ...
556 Dim oEnum As Object
' com.sun.star.container.XEnumeration
557 Dim oComp As Object
' com.sun.star.lang.XComponent
558 Dim vWindow As Window
' A single component
559 Dim oContainer As Object
' com.sun.star.awt.XWindow
560 Dim bFound As Boolean
' True if window found
561 Const cstThisSub =
"UI.Maximize
"
562 Const cstSubArgs =
"[WindowName]
"
564 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
567 If IsMissing(WindowName) Or IsEmpty(WindowName) Then WindowName =
""
568 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
569 If Not SF_Utils._ValidateFile(WindowName,
"WindowName
", , True) Then GoTo Finally
574 If Len(WindowName)
> 0 Then
575 Set oEnum = StarDesktop.Components().createEnumeration
576 Do While oEnum.hasMoreElements And Not bFound
577 Set oComp = oEnum.nextElement
578 vWindow = SF_UI._IdentifyWindow(oComp)
580 ' Does the current window match the arguments ?
581 If (Len(.WindowFileName)
> 0 And .WindowFileName = SF_FileSystem.ConvertToUrl(WindowName)) _
582 Or (Len(.WindowName)
> 0 And .WindowName = WindowName) _
583 Or (Len(.WindowTitle)
> 0 And .WindowTitle = WindowName) Then bFound = True
587 vWindow = SF_UI._IdentifyWindow(StarDesktop.CurrentComponent)
592 Set oContainer = vWindow.Frame.ContainerWindow
593 oContainer.IsMaximized = True
597 SF_Utils._ExitFunction(cstThisSub)
601 End Sub
' ScriptForge.SF_UI.Maximize
603 REM -----------------------------------------------------------------------------
604 Public Sub Minimize(Optional ByVal WindowName As Variant)
605 ''' Minimizes the current window or the given window
606 ''' Args:
607 ''' WindowName: see definitions. If absent the current window is considered
608 ''' Examples:
609 ''' ui.Minimize(
"myFile.ods
")
610 ''' ...
612 Dim oEnum As Object
' com.sun.star.container.XEnumeration
613 Dim oComp As Object
' com.sun.star.lang.XComponent
614 Dim vWindow As Window
' A single component
615 Dim oContainer As Object
' com.sun.star.awt.XWindow
616 Dim bFound As Boolean
' True if window found
617 Const cstThisSub =
"UI.Minimize
"
618 Const cstSubArgs =
"[WindowName]
"
620 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
623 If IsMissing(WindowName) Or IsEmpty(WindowName) Then WindowName =
""
624 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
625 If Not SF_Utils._ValidateFile(WindowName,
"WindowName
", , True) Then GoTo Finally
630 If Len(WindowName)
> 0 Then
631 Set oEnum = StarDesktop.Components().createEnumeration
632 Do While oEnum.hasMoreElements And Not bFound
633 Set oComp = oEnum.nextElement
634 vWindow = SF_UI._IdentifyWindow(oComp)
636 ' Does the current window match the arguments ?
637 If (Len(.WindowFileName)
> 0 And .WindowFileName = SF_FileSystem.ConvertToUrl(WindowName)) _
638 Or (Len(.WindowName)
> 0 And .WindowName = WindowName) _
639 Or (Len(.WindowTitle)
> 0 And .WindowTitle = WindowName) Then bFound = True
643 vWindow = SF_UI._IdentifyWindow(StarDesktop.CurrentComponent)
648 Set oContainer = vWindow.Frame.ContainerWindow
649 oContainer.IsMinimized = True
653 SF_Utils._ExitFunction(cstThisSub)
657 End Sub
' ScriptForge.SF_UI.Minimize
659 REM -----------------------------------------------------------------------------
660 Public Function Methods() As Variant
661 ''' Return the list of public methods of the UI service as an array
663 Methods = Array(
"Activate
" _
664 ,
"CreateBaseDocument
" _
665 ,
"CreateDocument
" _
666 ,
"Documents
" _
667 ,
"GetDocument
" _
668 ,
"Maximize
" _
669 ,
"Minimize
" _
670 ,
"OpenBaseDocument
" _
671 ,
"OpenDocument
" _
672 ,
"Resize
" _
673 ,
"RunCommand
" _
674 ,
"SetStatusbar
" _
675 ,
"ShowProgressBar
" _
676 ,
"WindowExists
" _
679 End Function
' ScriptForge.SF_UI.Methods
681 REM -----------------------------------------------------------------------------
682 Public Function OpenBaseDocument(Optional ByVal FileName As Variant _
683 , Optional ByVal RegistrationName As Variant _
684 , Optional ByVal MacroExecution As Variant _
686 ''' Open an existing LibreOffice Base document and return a SFDocuments.Document object
687 ''' Args:
688 ''' FileName: Identifies the file to open. It must follow the SF_FileSystem.FileNaming notation
689 ''' RegistrationName: the name of a registered database
690 ''' It is ignored if FileName
<> ""
691 ''' MacroExecution: one of the MACROEXECxxx constants
692 ''' Returns:
693 ''' A SFDocuments.SF_Base object
694 ''' Null if the opening failed, including when due to a user decision
695 ''' Exceptions:
696 ''' BASEDOCUMENTOPENERROR Wrong arguments
697 ''' Examples:
698 ''' Dim mBasec As Object, FSO As Object
699 ''' Set myBase = ui.OpenBaseDocument(
"C:\Temp\myDB.odb
", MacroExecution := ui.MACROEXECNEVER)
701 Dim oOpen As Variant
' Return value
702 Dim vProperties As Variant
' Array of com.sun.star.beans.PropertyValue
703 Dim oDBContext As Object
' com.sun.star.sdb.DatabaseContext
704 Dim oComp As Object
' Loaded component com.sun.star.lang.XComponent
705 Dim sFile As String
' Alias for FileName
706 Dim iMacro As Integer
' Alias for MacroExecution
707 Const cstThisSub =
"UI.OpenBaseDocument
"
708 Const cstSubArgs =
"[FileName=
""""], [RegistrationName=
""""], [MacroExecution=
0|
1|
2]
"
710 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
714 If IsMissing(FileName) Or IsEmpty(FileName) Then FileName =
""
715 If IsMissing(RegistrationName) Or IsEmpty(RegistrationName) Then RegistrationName =
""
716 If IsMissing(MacroExecution) Or IsEmpty(MacroExecution) Then MacroExecution = MACROEXECNORMAL
718 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
719 If Not SF_Utils._ValidateFile(FileName,
"FileName
", , True) Then GoTo Finally
720 If Not SF_Utils._Validate(RegistrationName,
"RegistrationName
", V_STRING) Then GoTo Finally
721 If Not SF_Utils._Validate(MacroExecution,
"MacroExecution
", V_NUMERIC _
722 , Array(MACROEXECNORMAL, MACROEXECNEVER, MACROEXECALWAYS)) Then GoTo Finally
725 ' Check the existence of FileName
726 If Len(FileName) =
0 Then
' FileName has precedence over RegistrationName
727 If Len(RegistrationName) =
0 Then GoTo CatchError
728 Set oDBContext = SF_Utils._GetUNOService(
"DatabaseContext
")
729 If Not oDBContext.hasRegisteredDatabase(RegistrationName) Then GoTo CatchError
730 FileName = SF_FileSystem._ConvertFromUrl(oDBContext.getDatabaseLocation(RegistrationName))
732 If Not SF_FileSystem.FileExists(FileName) Then GoTo CatchError
735 With com.sun.star.document.MacroExecMode
736 Select Case MacroExecution
737 Case
0 : iMacro = .USE_CONFIG
738 Case
1 : iMacro = .NEVER_EXECUTE
739 Case
2 : iMacro = .ALWAYS_EXECUTE_NO_WARN
743 vProperties = Array(SF_Utils._MakePropertyValue(
"MacroExecutionMode
", iMacro))
745 sFile = SF_FileSystem._ConvertToUrl(FileName)
746 Set oComp = StarDesktop.loadComponentFromURL(sFile,
"_blank
",
0, vProperties)
747 If Not IsNull(oComp) Then Set oOpen = CreateScriptService(
"SFDocuments.Document
", oComp)
750 Set OpenBaseDocument = oOpen
751 SF_Utils._ExitFunction(cstThisSub)
756 SF_Exception.RaiseFatal(BASEDOCUMENTOPENERROR,
"FileName
", FileName,
"RegistrationName
", RegistrationName)
758 End Function
' ScriptForge.SF_UI.OpenBaseDocument
760 REM -----------------------------------------------------------------------------
761 Public Function OpenDocument(Optional ByVal FileName As Variant _
762 , Optional ByVal Password As Variant _
763 , Optional ByVal ReadOnly As Variant _
764 , Optional ByVal Hidden As Variant _
765 , Optional ByVal MacroExecution As Variant _
766 , Optional ByVal FilterName As Variant _
767 , Optional ByVal FilterOptions As Variant _
769 ''' Open an existing LibreOffice document with the given options
770 ''' Args:
771 ''' FileName: Identifies the file to open. It must follow the SF_FileSystem.FileNaming notation
772 ''' Password: To use when the document is protected
773 ''' If wrong or absent while the document is protected, the user will be prompted to enter a password
774 ''' ReadOnly: Default = False
775 ''' Hidden: if True, open in the background (default = False)
776 ''' To use with caution: activation or closure can only happen programmatically
777 ''' MacroExecution: one of the MACROEXECxxx constants
778 ''' FilterName: the name of a filter that should be used for loading the document
779 ''' If present, the filter must exist
780 ''' FilterOptions: an optional string of options associated with the filter
781 ''' Returns:
782 ''' A SFDocuments.SF_Document object or one of its subclasses
783 ''' Null if the opening failed, including when due to a user decision
784 ''' Exceptions:
785 ''' DOCUMENTOPENERROR Wrong arguments
786 ''' Examples:
787 ''' Dim myDoc As Object, FSO As Object
788 ''' Set myDoc = ui.OpenDocument(
"C:\Temp\myFile.odt
", MacroExecution := ui.MACROEXECNEVER)
790 Dim oOpen As Variant
' Return value
791 Dim oFilterFactory As Object
' com.sun.star.document.FilterFactory
792 Dim vProperties As Variant
' Array of com.sun.star.beans.PropertyValue
793 Dim oComp As Object
' Loaded component com.sun.star.lang.XComponent
794 Dim sFile As String
' Alias for FileName
795 Dim iMacro As Integer
' Alias for MacroExecution
796 Const cstThisSub =
"UI.OpenDocument
"
797 Const cstSubArgs =
"FileName, [Password=
""""], [ReadOnly=False], [Hidden=False], [MacroExecution=
0|
1|
2], [FilterName=
""""], [FilterOptions=
""""]
"
799 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
803 If IsMissing(Password) Or IsEmpty(Password) Then Password =
""
804 If IsMissing(ReadOnly) Or IsEmpty(ReadOnly) Then ReadOnly = False
805 If IsMissing(Hidden) Or IsEmpty(Hidden) Then Hidden = False
806 If IsMissing(MacroExecution) Or IsEmpty(MacroExecution) Then MacroExecution = MACROEXECNORMAL
807 If IsMissing(FilterName) Or IsEmpty(FilterName) Then FilterName =
""
808 If IsMissing(FilterOptions) Or IsEmpty(FilterOptions) Then FilterOptions =
""
810 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
811 If Not SF_Utils._ValidateFile(FileName,
"FileName
") Then GoTo Finally
812 If Not SF_Utils._Validate(Password,
"Password
", V_STRING) Then GoTo Finally
813 If Not SF_Utils._Validate(ReadOnly,
"ReadOnly
", V_BOOLEAN) Then GoTo Finally
814 If Not SF_Utils._Validate(Hidden,
"Hidden
", V_BOOLEAN) Then GoTo Finally
815 If Not SF_Utils._Validate(MacroExecution,
"MacroExecution
", V_NUMERIC _
816 , Array(MACROEXECNORMAL, MACROEXECNEVER, MACROEXECALWAYS)) Then GoTo Finally
817 If Not SF_Utils._Validate(FilterName,
"FilterName
", V_STRING) Then GoTo Finally
818 If Not SF_Utils._Validate(FilterOptions,
"FilterOptions
", V_STRING) Then GoTo Finally
821 ' Check the existence of FileName and FilterName
822 If Not SF_FileSystem.FileExists(FileName) Then GoTo CatchError
823 If Len(FilterName)
> 0 Then
824 Set oFilterFactory = SF_Utils._GetUNOService(
"FilterFactory
")
825 If Not oFilterFactory.hasByName(FilterName) Then GoTo CatchError
829 With com.sun.star.document.MacroExecMode
830 Select Case MacroExecution
831 Case
0 : iMacro = .USE_CONFIG
832 Case
1 : iMacro = .NEVER_EXECUTE
833 Case
2 : iMacro = .ALWAYS_EXECUTE_NO_WARN
837 vProperties = Array( _
838 SF_Utils._MakePropertyValue(
"ReadOnly
", ReadOnly) _
839 , SF_Utils._MakePropertyValue(
"Hidden
", Hidden) _
840 , SF_Utils._MakePropertyValue(
"MacroExecutionMode
", iMacro) _
841 , SF_Utils._MakePropertyValue(
"FilterName
", FilterName) _
842 , SF_Utils._MakePropertyValue(
"FilterOptions
", FilterOptions) _
844 If Len(Password)
> 0 Then
' Password is to add only if
<> "" !?
845 vProperties = SF_Array.Append(vProperties, SF_Utils._MakePropertyValue(
"Password
", Password))
848 sFile = SF_FileSystem._ConvertToUrl(FileName)
849 Set oComp = StarDesktop.loadComponentFromURL(sFile,
"_blank
",
0, vProperties)
850 If Not IsNull(oComp) Then Set oOpen = CreateScriptService(
"SFDocuments.Document
", oComp)
853 Set OpenDocument = oOpen
854 SF_Utils._ExitFunction(cstThisSub)
859 SF_Exception.RaiseFatal(DOCUMENTOPENERROR,
"FileName
", FileName,
"Password
", Password,
"FilterName
", FilterName)
861 End Function
' ScriptForge.SF_UI.OpenDocument
863 REM -----------------------------------------------------------------------------
864 Public Function Properties() As Variant
865 ''' Return the list or properties of the Timer class as an array
867 Properties = Array( _
868 "ActiveWindow
" _
869 ,
"Height
" _
870 ,
"Width
" _
875 End Function
' ScriptForge.SF_UI.Properties
877 REM -----------------------------------------------------------------------------
878 Public Sub Resize(Optional ByVal Left As Variant _
879 , Optional ByVal Top As Variant _
880 , Optional ByVal Width As Variant _
881 , Optional ByVal Height As Variant _
883 ''' Resizes and/or moves the active window. Negative arguments are ignored.
884 ''' If the window was minimized or without arguments, it is restored
885 ''' Args:
886 ''' Left, Top: Distances from top and left edges of the screen
887 ''' Width, Height: Dimensions of the window
888 ''' Examples:
889 ''' ui.Resize(
10,,
500)
' Top and Height are unchanged
890 ''' ...
892 Dim vWindow As Window
' A single component
893 Dim oContainer As Object
' com.sun.star.awt.XWindow
894 Dim iPosSize As Integer
' Computes which of the
4 arguments should be considered
895 Const cstThisSub =
"UI.Resize
"
896 Const cstSubArgs =
"[Left], [Top], [Width], [Height]
"
898 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
901 If IsMissing(Left) Or IsEmpty(Left) Then Left = -
1
902 If IsMissing(Top) Or IsEmpty(Top) Then Top = -
1
903 If IsMissing(Width) Or IsEmpty(Width) Then Width = -
1
904 If IsMissing(Height) Or IsEmpty(Height) Then Height = -
1
905 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
906 If Not SF_Utils._Validate(Left,
"Left
", V_NUMERIC) Then GoTo Finally
907 If Not SF_Utils._Validate(Top,
"Top
", V_NUMERIC) Then GoTo Finally
908 If Not SF_Utils._Validate(Width,
"Width
", V_NUMERIC) Then GoTo Finally
909 If Not SF_Utils._Validate(Height,
"Height
", V_NUMERIC) Then GoTo Finally
913 vWindow = SF_UI._IdentifyWindow(StarDesktop.CurrentComponent)
914 If Not IsNull(vWindow.Frame) Then
915 Set oContainer = vWindow.Frame.ContainerWindow
917 If Left
>=
0 Then iPosSize = iPosSize + com.sun.star.awt.PosSize.X
918 If Top
>=
0 Then iPosSize = iPosSize + com.sun.star.awt.PosSize.Y
919 If Width
> 0 Then iPosSize = iPosSize + com.sun.star.awt.PosSize.WIDTH
920 If Height
> 0 Then iPosSize = iPosSize + com.sun.star.awt.PosSize.HEIGHT
924 .setPosSize(Left, Top, Width, Height, iPosSize)
929 SF_Utils._ExitFunction(cstThisSub)
933 End Sub
' ScriptForge.SF_UI.Resize
935 REM -----------------------------------------------------------------------------
936 Public Sub RunCommand(Optional ByVal Command As Variant _
937 , ParamArray Args As Variant _
939 ''' Run on the current window the given menu command. The command is executed with or without arguments
940 ''' A few typical commands:
941 ''' About, Delete, Edit, Undo, Copy, Paste, ...
942 ''' Dozens can be found on next page: https://wiki.documentfoundation.org/Development/DispatchCommands
943 ''' Args:
944 ''' Command: Case-sensitive. The command itself is not checked.
945 ''' If the command does not contain the
".uno:
" prefix, it is added.
946 ''' If nothing happens, then the command is probably wrong
947 ''' Args: Pairs of arguments name (string), value (any)
948 ''' Returns:
949 ''' Examples:
950 ''' ui.RunCommand(
"BasicIDEAppear
", _
951 ''' "Document
",
"LibreOffice Macros
& Dialogs
", _
952 ''' "LibName
",
"ScriptForge
", _
953 ''' "Name
",
"SF_Session
", _
954 ''' "Line
",
600)
956 Dim oDispatch
' com.sun.star.frame.DispatchHelper
957 Dim vProps As Variant
' Array of PropertyValues
958 Dim vValue As Variant
' A single value argument
959 Dim sCommand As String
' Alias of Command
961 Const cstPrefix =
".uno:
"
963 Const cstThisSub =
"UI.RunCommand
"
964 Const cstSubArgs =
"Command, [arg0Name, arg0Value], [arg1Name, arg1Value], ...
"
966 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
969 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
970 If Not SF_Utils._Validate(Command,
"Command
", V_STRING) Then GoTo Finally
971 If Not SF_Utils._ValidateArray(Args,
"Args
",
1) Then GoTo Finally
972 For i =
0 To UBound(Args) -
1 Step
2
973 If Not SF_Utils._Validate(Args(i),
"Arg
" & CStr(i/
2)
& "Name
", V_STRING) Then GoTo Finally
978 ' Build array of property values
980 For i =
0 To UBound(Args) -
1 Step
2
981 If IsEmpty(Args(i +
1)) Then vValue = Null Else vValue = Args(i +
1)
982 vProps = SF_Array.Append(vProps, SF_Utils._MakePropertyValue(Args(i), vValue))
984 Set oDispatch = SF_Utils._GetUNOService(
"DispatchHelper
")
985 If SF_String.StartsWith(Command, cstPrefix) Then sCommand = Command Else sCommand = cstPrefix
& Command
986 oDispatch.executeDispatch(StarDesktop.ActiveFrame, sCommand,
"",
0, vProps)
989 SF_Utils._ExitFunction(cstThisSub)
993 End Sub
' ScriptForge.SF_UI.RunCommand
995 REM -----------------------------------------------------------------------------
996 Public Function SetProperty(Optional ByVal PropertyName As Variant _
997 , Optional ByRef Value As Variant _
999 ''' Set a new value to the given property
1000 ''' Args:
1001 ''' PropertyName: the name of the property as a string
1002 ''' Value: its new value
1003 ''' Exceptions
1004 ''' ARGUMENTERROR The property does not exist
1006 Const cstThisSub =
"UI.SetProperty
"
1007 Const cstSubArgs =
"PropertyName, Value
"
1009 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
1013 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
1014 If Not SF_Utils._Validate(PropertyName,
"PropertyName
", V_STRING, Properties()) Then GoTo Catch
1018 Select Case UCase(PropertyName)
1023 SF_Utils._ExitFunction(cstThisSub)
1027 End Function
' ScriptForge.SF_UI.SetProperty
1029 REM -----------------------------------------------------------------------------
1030 Public Sub SetStatusbar(Optional ByVal Text As Variant _
1031 , Optional ByVal Percentage As Variant _
1033 ''' Display a text and a progressbar in the status bar of the active window
1034 ''' Any subsequent calls in the same macro run refer to the same status bar of the same window,
1035 ''' even if the window is not active anymore
1036 ''' A call without arguments resets the status bar to its normal state.
1037 ''' Args:
1038 ''' Text: the optional text to be displayed before the progress bar
1039 ''' Percentage: the optional degree of progress between
0 and
100
1040 ''' Examples:
1041 ''' Dim i As Integer
1042 ''' For i =
0 To
100
1043 ''' ui.SetStatusbar(
"Progress ...
", i)
1044 ''' Wait
50
1045 ''' Next i
1046 ''' ui.SetStatusbar
1049 Dim oControl As Object
1050 Dim oStatusbar As Object
1051 Const cstThisSub =
"UI.SetStatusbar
"
1052 Const cstSubArgs =
"[Text], [Percentage]
"
1054 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
1057 If IsMissing(Text) Or IsEmpty(Text) Then Text =
""
1058 If IsMissing(Percentage) Or IsEmpty(Percentage) Then Percentage = -
1
1059 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
1060 If Not SF_Utils._Validate(Text,
"Text
", V_STRING) Then GoTo Finally
1061 If Not SF_Utils._Validate(Percentage,
"Percentage
", V_NUMERIC) Then GoTo Finally
1065 Set oStatusbar = _SF_.Statusbar
1067 If IsNull(oStatusbar) Then
' Initial call
1068 Set oComp = StarDesktop.CurrentComponent
1069 If Not IsNull(oComp) Then
1070 Set oControl = Nothing
1071 If SF_Session.HasUnoProperty(oComp,
"CurrentController
") Then Set oControl = oComp.CurrentController
1072 If Not IsNull(oControl) Then
1073 If SF_Session.HasUnoMethod(oControl,
"getStatusIndicator
") Then oStatusbar = oControl.getStatusIndicator()
1076 If Not IsNull(oStatusbar) Then
1077 .start(
"",
100)
1080 If Not IsNull(oStatusbar) Then
1081 If Len(Text) =
0 And Percentage = -
1 Then
1083 Set oStatusbar = Nothing
1085 If Len(Text)
> 0 Then .setText(Text)
1086 If Percentage
>=
0 And Percentage
<=
100 Then .setValue(Percentage)
1092 Set _SF_.Statusbar = oStatusbar
1093 SF_Utils._ExitFunction(cstThisSub)
1097 End Sub
' ScriptForge.SF_UI.SetStatusbar
1099 REM -----------------------------------------------------------------------------
1100 Public Sub ShowProgressBar(Optional Title As Variant _
1101 , Optional ByVal Text As Variant _
1102 , Optional ByVal Percentage As Variant _
1103 , Optional ByRef _Context As Variant _
1105 ''' Display a non-modal dialog box. Specify its title, an explicatory text and the progress on a progressbar
1106 ''' A call without arguments erases the progress bar dialog.
1107 ''' The box will anyway vanish at the end of the macro run.
1108 ''' Args:
1109 ''' Title: the title appearing on top of the dialog box (Default =
"ScriptForge
")
1110 ''' Text: the optional text to be displayed above the progress bar (default = zero-length string)
1111 ''' Percentage: the degree of progress between
0 and
100. Default =
0
1112 ''' _Context: from Python, the XComponentXontext (FOR INTERNAL USE ONLY)
1113 ''' Examples:
1114 ''' Dim i As Integer
1115 ''' For i =
0 To
100
1116 ''' ui.ShowProgressBar(,
"Progress ...
" & i
& "/
100", i)
1117 ''' Wait
50
1118 ''' Next i
1119 ''' ui.ShowProgressBar
1121 Dim bFirstCall As Boolean
' True at first invocation of method
1122 Dim oDialog As Object
' SFDialogs.Dialog object
1123 Dim oFixedText As Object
' SFDialogs.DialogControl object
1124 Dim oProgressBar As Object
' SFDialogs.DialogControl object
1125 Dim sTitle As String
' Alias of Title
1126 Const cstThisSub =
"UI.ShowProgressBar
"
1127 Const cstSubArgs =
"[Title], [Text], [Percentage]
"
1129 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
1132 If IsMissing(Title) Or IsEmpty(Title) Then Title =
""
1133 If IsMissing(Text) Or IsEmpty(Text) Then Text =
""
1134 If IsMissing(Percentage) Or IsEmpty(Percentage) Then Percentage = -
1
1135 If IsMissing(_Context) Or IsEmpty(_Context) Then _Context = Nothing
1136 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
1137 If Not SF_Utils._Validate(Title,
"Title
", V_STRING) Then GoTo Finally
1138 If Not SF_Utils._Validate(Text,
"Text
", V_STRING) Then GoTo Finally
1139 If Not SF_Utils._Validate(Percentage,
"Percentage
", V_NUMERIC) Then GoTo Finally
1144 Set oDialog = .ProgressBarDialog
1145 Set oFixedText = .ProgressBarText
1146 Set oProgressBar = .ProgressBarBar
1149 bFirstCall = ( IsNull(oDialog) )
1150 If Not bFirstCall Then bFirstCall = Not ._IsStillAlive(False)
' False to not raise an error
1151 If bFirstCall Then Set oDialog = CreateScriptService(
"SFDialogs.Dialog
",
"GlobalScope
",
"ScriptForge
",
"dlgProgress
", _Context)
1153 If Not IsNull(oDialog) Then
1154 If Len(Title) =
0 And Len(Text) =
0 And Percentage = -
1 Then
1155 Set oDialog = .Dispose()
1157 .Caption = Iif(Len(Title)
> 0, Title,
"ScriptForge
")
1159 Set oFixedText = .Controls(
"ProgressText
")
1160 Set oProgressBar = .Controls(
"ProgressBar
")
1161 .Execute(Modal := False)
1163 If Len(Text)
> 0 Then oFixedText.Caption = Text
1164 oProgressBar.Value = Iif(Percentage
>=
0 And Percentage
<=
100, Percentage,
0)
1171 Set .ProgressBarDialog = oDialog
1172 Set .ProgressBarText = oFixedText
1173 Set .ProgressBarBar = oProgressBar
1175 SF_Utils._ExitFunction(cstThisSub)
1179 End Sub
' ScriptForge.SF_UI.ShowProgressBar
1181 REM -----------------------------------------------------------------------------
1182 Public Function WindowExists(Optional ByVal WindowName As Variant) As Boolean
1183 ''' Returns True if the specified window exists
1184 ''' Args:
1185 ''' WindowName: see definitions
1186 ''' Returns:
1187 ''' True if the given window is found
1188 ''' Examples:
1189 ''' ui.WindowExists(
"C:\Me\My file.odt
")
1191 Dim bWindowExists As Boolean
' Return value
1192 Dim oEnum As Object
' com.sun.star.container.XEnumeration
1193 Dim oComp As Object
' com.sun.star.lang.XComponent
1194 Dim vWindow As Window
' A single component
1195 Dim oContainer As Object
' com.sun.star.awt.XWindow
1196 Const cstThisSub =
"UI.WindowExists
"
1197 Const cstSubArgs =
"WindowName
"
1199 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
1200 bWindowExists = False
1203 If IsMissing(WindowName) Or IsEmpty(WindowName) Then WindowName =
""
1204 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
1205 If Not SF_Utils._ValidateFile(WindowName,
"WindowName
") Then GoTo Finally
1209 Set oEnum = StarDesktop.Components().createEnumeration
1210 Do While oEnum.hasMoreElements
1211 Set oComp = oEnum.nextElement
1212 vWindow = SF_UI._IdentifyWindow(oComp)
1214 ' Does the current window match the arguments ?
1215 If (Len(.WindowFileName)
> 0 And .WindowFileName = SF_FileSystem.ConvertToUrl(WindowName)) _
1216 Or (Len(.WindowName)
> 0 And .WindowName = WindowName) _
1217 Or (Len(.WindowTitle)
> 0 And .WindowTitle = WindowName) Then
1218 bWindowExists = True
1225 WindowExists = bWindowExists
1226 SF_Utils._ExitFunction(cstThisSub)
1230 End Function
' ScriptForge.SF_UI.WindowExists
1232 REM =========================================================== PRIVATE FUNCTIONS
1234 REM -----------------------------------------------------------------------------
1235 Public Sub _CloseProgressBar(Optional ByRef poEvent As Object)
1236 ''' Triggered by the Close button in the dlgProgress dialog
1237 ''' to simply close the dialog
1239 ShowProgressBar()
' Without arguments =
> close the dialog
1241 End Sub
' ScriptForge.SF_UI._CloseProgressBar
1243 REM -----------------------------------------------------------------------------
1244 Public Function _IdentifyWindow(ByRef poComponent As Object) As Object
1245 ''' Return a Window object (definition on top of module) based on component given as argument
1246 ''' Is a shortcut to explore the most relevant properties or objects bound to a UNO component
1248 Dim oWindow As Window
' Return value
1249 Dim sImplementation As String
' Component
's implementationname
1250 Dim sIdentifier As String
' Component
's identifier
1251 Dim vSelection As Variant
' Array of poCOmponent.Selection property values
1252 Dim iCommandType As Integer
' Datasheet type
1253 Dim FSO As Object
' Alias for SF_FileSystem
1255 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
1256 Set _IdentifyWindow = Nothing
1257 sImplementation =
"" : sIdentifier =
""
1259 Set FSO = SF_FileSystem
1261 Set .Frame = Nothing
1262 Set .Component = Nothing
1263 .WindowName =
""
1264 .WindowTitle =
""
1265 .WindowFileName =
""
1266 .DocumentType =
""
1267 .ParentName =
""
1268 If IsNull(poComponent) Then GoTo Finally
1269 If SF_Session.HasUnoProperty(poComponent,
"ImplementationName
") Then sImplementation = poComponent.ImplementationName
1270 If SF_Session.HasUnoProperty(poComponent,
"Identifier
") Then sIdentifier = poComponent.Identifier
1271 Set .Component = poComponent
1272 Select Case sImplementation
1273 Case
"com.sun.star.comp.basic.BasicIDE
"
1274 .WindowName = BASICIDE
1275 Case
"com.sun.star.comp.dba.ODatabaseDocument
" ' No identifier
1276 .WindowFileName = SF_Utils._GetPropertyValue(poComponent.Args,
"URL
")
1277 If Len(.WindowFileName)
> 0 Then .WindowName = FSO.GetName(FSO._ConvertFromUrl(.WindowFileName))
1278 .DocumentType = BASEDOCUMENT
1279 Case
"org.openoffice.comp.dbu.ODatasourceBrowser
" ' Base datasheet (table, query or sql in read mode
1280 Set .Frame = poComponent.Frame
1281 If Not IsEmpty(poComponent.Selection) Then
' Empty for (F4) DatasourceBrowser !!
1282 vSelection = poComponent.Selection
1283 .WindowName = SF_Utils._GetPropertyValue(vSelection,
"Command
")
1284 iCommandType = SF_Utils._GetPropertyValue(vSelection,
"CommandType
")
1285 Select Case iCommandType
1286 Case com.sun.star.sdb.CommandType.TABLE : .DocumentType = TABLEDATA
1287 Case com.sun.star.sdb.CommandType.QUERY : .DocumentType = QUERYDATA
1288 Case com.sun.star.sdb.CommandType.COMMAND : .DocumentType = SQLDATA
1290 .ParentName = SF_Utils._GetPropertyValue(vSelection,
"DataSourceName
")
1291 .WindowTitle = .WindowName
1293 Case
"org.openoffice.comp.dbu.OTableDesign
",
"org.openoffice.comp.dbu.OQueryDesign
" ' Table or Query in Edit mode
1294 Case
"org.openoffice.comp.dbu.ORelationDesign
"
1295 Case
"com.sun.star.comp.sfx2.BackingComp
" ' Welcome screen
1296 Set .Frame = poComponent.Frame
1297 .WindowName = WELCOMESCREEN
1299 If Len(sIdentifier)
> 0 Then
1300 ' Do not use URL : it contains the TemplateFile when new documents are created from a template
1301 .WindowFileName = poComponent.Location
1302 If Len(.WindowFileName)
> 0 Then .WindowName = FSO.GetName(FSO._ConvertFromUrl(.WindowFileName))
1303 If SF_Session.HasUnoProperty(poComponent,
"Title
") Then .WindowTitle = poComponent.Title
1304 Select Case sIdentifier
1305 Case
"com.sun.star.sdb.FormDesign
" ' Form
1306 Case
"com.sun.star.sdb.TextReportDesign
" ' Report
1307 Case
"com.sun.star.text.TextDocument
" ' Writer
1308 .DocumentType = WRITERDOCUMENT
1309 Case
"com.sun.star.sheet.SpreadsheetDocument
" ' Calc
1310 .DocumentType = CALCDOCUMENT
1311 Case
"com.sun.star.presentation.PresentationDocument
" ' Impress
1312 .DocumentType = IMPRESSDOCUMENT
1313 Case
"com.sun.star.drawing.DrawingDocument
" ' Draw
1314 .DocumentType = DRAWDOCUMENT
1315 Case
"com.sun.star.formula.FormulaProperties
" ' Math
1316 .DocumentType = MATHDOCUMENT
1321 If IsNull(.Frame) Then
1322 If Not IsNull(poComponent.CurrentController) Then Set .Frame = poComponent.CurrentController.Frame
1327 Set _IdentifyWindow = oWindow
1331 End Function
' ScriptForge.SF_UI._IdentifyWindow
1333 REM -----------------------------------------------------------------------------
1334 Public Function _PosSize() As Object
1335 ''' Returns the PosSize structure of the active window
1337 Dim vWindow As Window
' A single component
1338 Dim oContainer As Object
' com.sun.star.awt.XWindow
1339 Dim oPosSize As Object
' com.sun.star.awt.Rectangle
1341 Set oPosSize = Nothing
1344 vWindow = SF_UI._IdentifyWindow(StarDesktop.CurrentComponent)
1345 If Not IsNull(vWindow.Frame) Then
1346 Set oContainer = vWindow.Frame.ContainerWindow
1347 Set oPosSize = oContainer.getPosSize()
1351 Set _PosSize = oPosSize
1353 End Function
' ScriptForge.SF_UI._PosSize
1355 REM -----------------------------------------------------------------------------
1356 Private Function _Repr() As String
1357 ''' Convert the UI instance to a readable string, typically for debugging purposes (DebugPrint ...)
1358 ''' Args:
1359 ''' Return:
1360 ''' "[UI]
"
1362 _Repr =
"[UI]
"
1364 End Function
' ScriptForge.SF_UI._Repr
1366 REM ============================================ END OF SCRIPTFORGE.SF_UI