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 Type _Toolbar
' Proto-toolbar object. Passed to the
"Toolbar
" service, a full ScriptForge Toolbar object will be returned
60 Component As Object
' com.sun.star.lang.XComponent
61 ResourceURL As String
' Toolbar internal name
62 UIName As String
' Toolbar external name, may be
""
63 UIConfigurationManager As Object
' com.sun.star.ui.XUIConfigurationManager
64 ElementsInfoIndex As Long
' Index of the toolbar in the getElementsInfo(
0) array
65 Storage As Long
' One of the toolbar location constants
68 ' The progress/status bar of the active window
69 'Private oStatusBar As Object
' com.sun.star.task.XStatusIndicator
71 REM ============================================================ MODULE CONSTANTS
73 ' Special windows
74 Const BASICIDE =
"BASICIDE
"
75 Const WELCOMESCREEN =
"WELCOMESCREEN
"
77 ' Document types (only if not
1 of the special windows)
78 Const BASEDOCUMENT =
"Base
"
79 Const CALCDOCUMENT =
"Calc
"
80 Const DRAWDOCUMENT =
"Draw
"
81 Const FORMDOCUMENT =
"FormDocument
"
82 Const IMPRESSDOCUMENT =
"Impress
"
83 Const MATHDOCUMENT =
"Math
"
84 Const WRITERDOCUMENT =
"Writer
"
86 ' Window subtypes
87 Const TABLEDATA =
"TableData
"
88 Const QUERYDATA =
"QueryData
"
89 Const SQLDATA =
"SqlData
"
90 Const BASEREPORT =
"BaseReport
"
91 Const BASEDIAGRAM =
"BaseDiagram
"
93 ' Macro execution modes
94 Const cstMACROEXECNORMAL =
0 ' Default, execution depends on user configuration and choice
95 Const cstMACROEXECNEVER =
1 ' Macros are not executed
96 Const cstMACROEXECALWAYS =
2 ' Macros are always executed
98 ' Toolbar locations
99 Const cstBUILTINTOOLBAR =
0 ' Standard toolbar
100 Const cstCUSTOMTOOLBAR =
1 ' Toolbar added by user and stored in the LibreOffice application
101 Const cstCUSTOMDOCTOOLBAR =
2 ' Toolbar added by user solely for a single document
103 REM ===================================================== CONSTRUCTOR/DESTRUCTOR
105 REM -----------------------------------------------------------------------------
106 Public Function Dispose() As Variant
107 Set Dispose = Nothing
108 End Function
' ScriptForge.SF_UI Explicit destructor
110 REM ================================================================== PROPERTIES
112 REM -----------------------------------------------------------------------------
113 Public Function ActiveWindow() As String
114 ''' Returns a valid WindowName for the currently active window
115 ''' When
"" is returned, the window could not be identified
117 Dim vWindow As Window
' A component
118 Dim oComp As Object
' com.sun.star.lang.XComponent
120 Set oComp = StarDesktop.CurrentComponent
121 If Not IsNull(oComp) Then
122 vWindow = SF_UI._IdentifyWindow(oComp)
124 If Len(.WindowFileName)
> 0 Then
125 ActiveWindow = SF_FileSystem._ConvertFromUrl(.WindowFileName)
126 ElseIf Len(.WindowName)
> 0 Then
127 ActiveWindow = .WindowName
128 ElseIf Len(.WindowTitle)
> 0 Then
129 ActiveWindow = .WindowTitle
131 ActiveWindow =
""
136 End Function
' ScriptForge.SF_UI.ActiveWindow
138 REM -----------------------------------------------------------------------------
139 Property Get Height() As Long
140 ''' Returns the height of the active window
141 Dim oPosSize As Object
' com.sun.star.awt.Rectangle
142 Set oPosSize = SF_UI._PosSize()
143 If Not IsNull(oPosSize) Then Height = oPosSize.Height Else Height = -
1
144 End Property
' ScriptForge.SF_UI.Height
146 REM -----------------------------------------------------------------------------
147 Property Get MACROEXECALWAYS As Integer
148 ''' Macros are always executed
149 MACROEXECALWAYS = cstMACROEXECALWAYS
150 End Property
' ScriptForge.SF_UI.MACROEXECALWAYS
152 REM -----------------------------------------------------------------------------
153 Property Get MACROEXECNEVER As Integer
154 ''' Macros are not executed
155 MACROEXECNEVER = cstMACROEXECNEVER
156 End Property
' ScriptForge.SF_UI.MACROEXECNEVER
158 REM -----------------------------------------------------------------------------
159 Property Get MACROEXECNORMAL As Integer
160 ''' Default, execution depends on user configuration and choice
161 MACROEXECNORMAL = cstMACROEXECNORMAL
162 End Property
' ScriptForge.SF_UI.MACROEXECNORMAL
164 REM -----------------------------------------------------------------------------
165 Property Get ObjectType As String
166 ''' Only to enable object representation
167 ObjectType =
"SF_UI
"
168 End Property
' ScriptForge.SF_UI.ObjectType
170 REM -----------------------------------------------------------------------------
171 Property Get ServiceName As String
172 ''' Internal use
173 ServiceName =
"ScriptForge.UI
"
174 End Property
' ScriptForge.SF_UI.ServiceName
176 REM -----------------------------------------------------------------------------
177 Property Get Width() As Long
178 ''' Returns the width of the active window
179 Dim oPosSize As Object
' com.sun.star.awt.Rectangle
180 Set oPosSize = SF_UI._PosSize()
181 If Not IsNull(oPosSize) Then Width = oPosSize.Width Else Width = -
1
182 End Property
' ScriptForge.SF_UI.Width
184 REM -----------------------------------------------------------------------------
185 Property Get X() As Long
186 ''' Returns the X coordinate of the active window
187 Dim oPosSize As Object
' com.sun.star.awt.Rectangle
188 Set oPosSize = SF_UI._PosSize()
189 If Not IsNull(oPosSize) Then X = oPosSize.X Else X = -
1
190 End Property
' ScriptForge.SF_UI.X
192 REM -----------------------------------------------------------------------------
193 Property Get Y() As Long
194 ''' Returns the Y coordinate of the active window
195 Dim oPosSize As Object
' com.sun.star.awt.Rectangle
196 Set oPosSize = SF_UI._PosSize()
197 If Not IsNull(oPosSize) Then Y = oPosSize.Y Else Y = -
1
198 End Property
' ScriptForge.SF_UI.Y
200 REM ===================================================================== METHODS
202 REM -----------------------------------------------------------------------------
203 Public Function Activate(Optional ByVal WindowName As Variant) As Boolean
204 ''' Make the specified window active
205 ''' Args:
206 ''' WindowName: see definitions
207 ''' Returns:
208 ''' True if the given window is found and can be activated
209 ''' There is no change in the actual user interface if no window matches the selection
210 ''' Examples:
211 ''' ui.Activate(
"C:\Me\My file.odt
")
213 Dim bActivate As Boolean
' Return value
214 Dim oEnum As Object
' com.sun.star.container.XEnumeration
215 Dim oComp As Object
' com.sun.star.lang.XComponent
216 Dim vWindow As Window
' A single component
217 Dim oContainer As Object
' com.sun.star.awt.XWindow
218 Const cstThisSub =
"UI.Activate
"
219 Const cstSubArgs =
"WindowName
"
221 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
225 If IsMissing(WindowName) Or IsEmpty(WindowName) Then WindowName =
""
226 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
227 If Not SF_Utils._ValidateFile(WindowName,
"WindowName
") Then GoTo Finally
231 Set oEnum = StarDesktop.Components().createEnumeration
232 Do While oEnum.hasMoreElements
233 Set oComp = oEnum.nextElement
234 vWindow = SF_UI._IdentifyWindow(oComp)
236 ' Does the current window match the arguments ?
237 If (Len(.WindowFileName)
> 0 And .WindowFileName = SF_FileSystem._ConvertToUrl(WindowName)) _
238 Or (Len(.WindowName)
> 0 And .WindowName = WindowName) _
239 Or (Len(.WindowTitle)
> 0 And .WindowTitle = WindowName) Then
240 Set oContainer = vWindow.Frame.ContainerWindow
242 If .isVisible() = False Then .setVisible(True)
245 .toFront()
' Force window change in Linux
246 Wait
1 ' Bypass desynchro issue in Linux
256 SF_Utils._ExitFunction(cstThisSub)
260 End Function
' ScriptForge.SF_UI.Activate
262 REM -----------------------------------------------------------------------------
263 Public Function CreateBaseDocument(Optional ByVal FileName As Variant _
264 , Optional ByVal EmbeddedDatabase As Variant _
265 , Optional ByVal RegistrationName As Variant _
266 , Optional ByVal CalcFileName As Variant _
268 ''' Create a new LibreOffice Base document embedding an empty database of the given type
269 ''' Args:
270 ''' FileName: Identifies the file to create. It must follow the SF_FileSystem.FileNaming notation
271 ''' If the file already exists, it is overwritten without warning
272 ''' EmbeddedDatabase: either
"HSQLDB
" (default) or
"FIREBIRD
" or
"CALC
"
273 ''' RegistrationName: the name used to store the new database in the databases register
274 ''' If
"" (default), no registration takes place
275 ''' If the name already exists it is overwritten without warning
276 ''' CalcFileName: only when EmbedddedDatabase =
"CALC
", the name of the file containing the tables as Calc sheets
277 ''' The name of the file must be given in SF_FileSystem.FileNaming notation
278 ''' The file must exist
279 ''' Returns:
280 ''' A SFDocuments.SF_Document object or one of its subclasses
281 ''' Exceptions
282 ''' UNKNOWNFILEERROR Calc datasource does not exist
283 ''' Examples:
284 ''' Dim myBase As Object, myCalcBase As Object
285 ''' Set myBase = ui.CreateBaseDocument(
"C:\Databases\MyBaseFile.odb
",
"FIREBIRD
")
286 ''' Set myCalcBase = ui.CreateBaseDocument(
"C:\Databases\MyCalcBaseFile.odb
",
"CALC
", ,
"C:\Databases\MyCalcFile.ods
")
288 Dim oCreate As Variant
' Return value
289 Dim oDBContext As Object
' com.sun.star.sdb.DatabaseContext
290 Dim oDatabase As Object
' com.sun.star.comp.dba.ODatabaseSource
291 Dim oComp As Object
' Loaded component com.sun.star.lang.XComponent
292 Dim sFileName As String
' Alias of FileName
293 Dim FSO As Object
' Alias for FileSystem service
294 Const cstDocType =
"private:factory/s
"
295 Const cstThisSub =
"UI.CreateBaseDocument
"
296 Const cstSubArgs =
"FileName, [EmbeddedDatabase=
""HSQLDB
""|
""FIREBIRD
""|
""CALC
""], [RegistrationName=
""""], [CalcFileName]
"
298 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
299 Set oCreate = Nothing
300 Set FSO = CreateScriptService(
"FileSystem
")
303 If IsMissing(EmbeddedDatabase) Or IsEmpty(EmbeddedDatabase) Then EmbeddedDatabase =
"HSQLDB
"
304 If IsMissing(RegistrationName) Or IsEmpty(RegistrationName) Then RegistrationName =
""
305 If IsMissing(CalcFileName) Or IsEmpty(CalcFileName) Then CalcFileName =
""
306 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
307 If Not SF_Utils._ValidateFile(FileName,
"FileName
") Then GoTo Finally
308 If Not SF_Utils._Validate(EmbeddedDatabase,
"EmbeddedDatabase
", V_STRING, Array(
"CALC
",
"HSQLDB
",
"FIREBIRD
")) Then GoTo Finally
309 If Not SF_Utils._Validate(RegistrationName,
"RegistrationName
", V_STRING) Then GoTo Finally
310 If UCase(EmbeddedDatabase) =
"CALC
" Then
311 If Not SF_Utils._ValidateFile(CalcFileName,
"CalcFileName
") Then GoTo Finally
312 If Not FSO.FileExists(CalcFileName) Then GoTo CatchNotExists
317 Set oDBContext = SF_Utils._GetUNOService(
"DatabaseContext
")
319 Set oDatabase = .createInstance()
320 ' Build the url link to the database
321 Select Case UCase(EmbeddedDatabase)
322 Case
"HSQLDB
",
"FIREBIRD
"
323 oDatabase.URL =
"sdbc:embedded:
" & LCase(EmbeddedDatabase)
324 Case
"CALC
"
325 oDatabase.URL =
"sdbc:calc:
" & FSO._ConvertToUrl(CalcFileName)
327 ' Create empty Base document
328 sFileName = FSO._ConvertToUrl(FileName)
329 ' An existing file is overwritten without warning
330 If FSO.FileExists(FileName) Then FSO.DeleteFile(FileName)
331 If FSO.FileExists(FileName
& ".lck
") Then FSO.DeleteFile(FileName
& ".lck
")
332 oDatabase.DatabaseDocument.storeAsURL(sFileName, Array(SF_Utils._MakePropertyValue(
"Overwrite
", True)))
333 ' Register database if requested
334 If Len(RegistrationName)
> 0 Then
335 If .hasRegisteredDatabase(RegistrationName) Then
336 .changeDatabaseLocation(RegistrationName, sFileName)
338 .registerDatabaseLocation(RegistrationName, sFileName)
343 Set oCreate = OpenBaseDocument(FileName)
346 Set CreateBaseDocument = oCreate
347 SF_Utils._ExitFunction(cstThisSub)
352 SF_Exception.RaiseFatal(UNKNOWNFILEERROR,
"CalcFileName
", CalcFileName)
354 End Function
' ScriptForge.SF_UI.CreateBaseDocument
356 REM -----------------------------------------------------------------------------
357 Public Function CreateDocument(Optional ByVal DocumentType As Variant _
358 , Optional ByVal TemplateFile As Variant _
359 , Optional ByVal Hidden As Variant _
361 ''' Create a new LibreOffice document of a given type or based on a given template
362 ''' Args:
363 ''' DocumentType:
"Calc
",
"Writer
", etc. If absent, a TemplateFile must be given
364 ''' TemplateFile: the full FileName of the template to build the new document on
365 ''' If the file does not exist, the argument is ignored
366 ''' The
"FileSystem
" service provides the TemplatesFolder and UserTemplatesFolder
367 ''' properties to help to build the argument
368 ''' Hidden: if True, open in the background (default = False)
369 ''' To use with caution: activation or closure can only happen programmatically
370 ''' Returns:
371 ''' A SFDocuments.SF_Document object or one of its subclasses
372 ''' Exceptions:
373 ''' DOCUMENTCREATIONERROR Wrong arguments
374 ''' Examples:
375 ''' Dim myDoc1 As Object, myDoc2 As Object, FSO As Object
376 ''' Set myDoc1 = ui.CreateDocument(
"Calc
")
377 ''' Set FSO = CreateScriptService(
"FileSystem
")
378 ''' Set myDoc2 = ui.CreateDocument(, FSO.BuildPath(FSO.TemplatesFolder,
"personal/CV.ott
"))
380 Dim oCreate As Variant
' Return value
381 Dim vProperties As Variant
' Array of com.sun.star.beans.PropertyValue
382 Dim bTemplateExists As Boolean
' True if TemplateFile is valid
383 Dim sNew As String
' File url
384 Dim oComp As Object
' Loaded component com.sun.star.lang.XComponent
385 Const cstDocType =
"private:factory/s
"
386 Const cstThisSub =
"UI.CreateDocument
"
387 Const cstSubArgs =
"[DocumentType=
""""], [TemplateFile=
""""], [Hidden=False]
"
389 '>>> If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
390 Set oCreate = Nothing
393 If IsMissing(DocumentType) Or IsEmpty(DocumentType) Then DocumentType =
""
394 If IsMissing(TemplateFile) Or IsEmpty(TemplateFile) Then TemplateFile =
""
395 If IsMissing(Hidden) Or IsEmpty(Hidden) Then Hidden = False
397 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
398 If Not SF_Utils._Validate(DocumentType,
"DocumentType
", V_STRING _
399 , Array(
"", BASEDOCUMENT, CALCDOCUMENT, DRAWDOCUMENT _
400 , IMPRESSDOCUMENT, MATHDOCUMENT, WRITERDOCUMENT)) Then GoTo Finally
401 If Not SF_Utils._ValidateFile(TemplateFile,
"TemplateFile
", , True) Then GoTo Finally
402 If Not SF_Utils._Validate(Hidden,
"Hidden
", V_BOOLEAN) Then GoTo Finally
405 If Len(DocumentType) + Len(TemplateFile) =
0 Then GoTo CatchError
406 If Len(TemplateFile)
> 0 Then bTemplateExists = SF_FileSystem.FileExists(TemplateFile) Else bTemplateExists = False
407 If Len(DocumentType) =
0 Then
408 If Not bTemplateExists Then GoTo CatchError
412 If bTemplateExists Then sNew = SF_FileSystem._ConvertToUrl(TemplateFile) Else sNew = cstDocType
& LCase(DocumentType)
413 vProperties = Array( _
414 SF_Utils._MakePropertyValue(
"AsTemplate
", bTemplateExists) _
415 , SF_Utils._MakePropertyValue(
"Hidden
", Hidden) _
417 Set oComp = StarDesktop.loadComponentFromURL(sNew,
"_blank
",
0, vProperties)
418 If Not IsNull(oComp) Then Set oCreate = CreateScriptService(
"SFDocuments.Document
", oComp)
421 Set CreateDocument = oCreate
422 SF_Utils._ExitFunction(cstThisSub)
427 SF_Exception.RaiseFatal(DOCUMENTCREATIONERROR,
"DocumentType
", DocumentType,
"TemplateFile
", TemplateFile)
429 End Function
' ScriptForge.SF_UI.CreateDocument
431 REM -----------------------------------------------------------------------------
432 Public Function Documents() As Variant
433 ''' Returns the list of the currently open documents. Special windows are ignored.
434 ''' Returns:
435 ''' A zero-based
1D array of filenames (in SF_FileSystem.FileNaming notation)
436 ''' or of window titles for unsaved documents
437 ''' Examples:
438 ''' Dim vDocs As Variant, sDoc As String
439 ''' vDocs = ui.Documents()
440 ''' For each sDoc In vDocs
441 ''' ...
443 Dim vDocuments As Variant
' Return value
444 Dim oEnum As Object
' com.sun.star.container.XEnumeration
445 Dim oComp As Object
' com.sun.star.lang.XComponent
446 Dim vWindow As Window
' A single component
447 Const cstThisSub =
"UI.Documents
"
448 Const cstSubArgs =
""
450 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
454 SF_Utils._EnterFunction(cstThisSub, cstSubArgs)
457 Set oEnum = StarDesktop.Components().createEnumeration
458 Do While oEnum.hasMoreElements
459 Set oComp = oEnum.nextElement
460 vWindow = SF_UI._IdentifyWindow(oComp)
462 If Len(.WindowFileName)
> 0 Then
463 vDocuments = SF_Array.Append(vDocuments, SF_FileSystem._ConvertFromUrl(.WindowFileName))
464 ElseIf Len(.WindowTitle)
> 0 Then
465 vDocuments = SF_Array.Append(vDocuments, .WindowTitle)
471 Documents = vDocuments
472 SF_Utils._ExitFunction(cstThisSub)
476 End Function
' ScriptForge.SF_UI.Documents
478 REM -----------------------------------------------------------------------------
479 Public Function GetDocument(Optional ByVal WindowName As Variant) As Variant
480 ''' Returns a SFDocuments.Document object referring to the active window or the given window
481 ''' Args:
482 ''' WindowName: when a string, see definitions. If absent the active window is considered.
483 ''' when an object, must be a UNO object of types
484 ''' com.sun.star.lang.XComponent or com.sun.star.comp.dba.ODatabaseDocument
485 ''' Exceptions:
486 ''' DOCUMENTERROR The targeted window could not be found
487 ''' Examples:
488 ''' Dim oDoc As Object
489 ''' Set oDoc = ui.GetDocument
' or Set oDoc = ui.GetDocument(ThisComponent)
490 ''' oDoc.Save()
492 Dim oDocument As Object
' Return value
493 Const cstThisSub =
"UI.GetDocument
"
494 Const cstSubArgs =
"[WindowName]
"
496 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
497 Set oDocument = Nothing
500 If IsMissing(WindowName) Or IsEmpty(WindowName) Then WindowName =
""
501 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
502 If Not SF_Utils._Validate(WindowName,
"WindowName
", Array(V_STRING, V_OBJECT)) Then GoTo Finally
503 If VarType(WindowName) = V_STRING Then
504 If Not SF_Utils._ValidateFile(WindowName,
"WindowName
", , True) Then GoTo Finally
509 Set oDocument = SF_Services.CreateScriptService(
"SFDocuments.Document
", WindowName)
510 If IsNull(oDocument) Then GoTo CatchDeliver
513 Set GetDocument = oDocument
514 SF_Utils._ExitFunction(cstThisSub)
519 SF_Exception.RaiseFatal(DOCUMENTERROR,
"WindowName
", WindowName)
521 End Function
' ScriptForge.SF_UI.GetDocument
523 REM -----------------------------------------------------------------------------
524 Public Function GetProperty(Optional ByVal PropertyName As Variant) As Variant
525 ''' Return the actual value of the given property
526 ''' Args:
527 ''' PropertyName: the name of the property as a string
528 ''' Returns:
529 ''' The actual value of the property
530 ''' Exceptions
531 ''' ARGUMENTERROR The property does not exist
533 Const cstThisSub =
"UI.GetProperty
"
534 Const cstSubArgs =
"PropertyName
"
536 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
540 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
541 If Not SF_Utils._Validate(PropertyName,
"PropertyName
", V_STRING, Properties()) Then GoTo Catch
545 Select Case UCase(PropertyName)
546 Case
"ACTIVEWINDOW
" : GetProperty = ActiveWindow()
547 Case
"HEIGHT
" : GetProperty = SF_UI.Height
548 Case
"WIDTH
" : GetProperty = SF_UI.Width
549 Case
"X
" : GetProperty = SF_UI.X
550 Case
"Y
" : GetProperty = SF_UI.Y
556 SF_Utils._ExitFunction(cstThisSub)
560 End Function
' ScriptForge.SF_UI.GetProperty
562 REM -----------------------------------------------------------------------------
563 Public Sub Maximize(Optional ByVal WindowName As Variant)
564 ''' Maximizes the active window or the given window
565 ''' Args:
566 ''' WindowName: see definitions. If absent the active window is considered
567 ''' Examples:
568 ''' ui.Maximize
569 ''' ...
571 Dim oEnum As Object
' com.sun.star.container.XEnumeration
572 Dim oComp As Object
' com.sun.star.lang.XComponent
573 Dim vWindow As Window
' A single component
574 Dim oContainer As Object
' com.sun.star.awt.XWindow
575 Dim bFound As Boolean
' True if window found
576 Const cstThisSub =
"UI.Maximize
"
577 Const cstSubArgs =
"[WindowName]
"
579 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
582 If IsMissing(WindowName) Or IsEmpty(WindowName) Then WindowName =
""
583 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
584 If Not SF_Utils._ValidateFile(WindowName,
"WindowName
", , True) Then GoTo Finally
589 If Len(WindowName)
> 0 Then
590 Set oEnum = StarDesktop.Components().createEnumeration
591 Do While oEnum.hasMoreElements And Not bFound
592 Set oComp = oEnum.nextElement
593 vWindow = SF_UI._IdentifyWindow(oComp)
595 ' Does the current window match the arguments ?
596 If (Len(.WindowFileName)
> 0 And .WindowFileName = SF_FileSystem.ConvertToUrl(WindowName)) _
597 Or (Len(.WindowName)
> 0 And .WindowName = WindowName) _
598 Or (Len(.WindowTitle)
> 0 And .WindowTitle = WindowName) Then bFound = True
602 vWindow = SF_UI._IdentifyWindow(StarDesktop.CurrentComponent)
607 Set oContainer = vWindow.Frame.ContainerWindow
608 oContainer.IsMaximized = True
612 SF_Utils._ExitFunction(cstThisSub)
616 End Sub
' ScriptForge.SF_UI.Maximize
618 REM -----------------------------------------------------------------------------
619 Public Sub Minimize(Optional ByVal WindowName As Variant)
620 ''' Minimizes the current window or the given window
621 ''' Args:
622 ''' WindowName: see definitions. If absent the current window is considered
623 ''' Examples:
624 ''' ui.Minimize(
"myFile.ods
")
625 ''' ...
627 Dim oEnum As Object
' com.sun.star.container.XEnumeration
628 Dim oComp As Object
' com.sun.star.lang.XComponent
629 Dim vWindow As Window
' A single component
630 Dim oContainer As Object
' com.sun.star.awt.XWindow
631 Dim bFound As Boolean
' True if window found
632 Const cstThisSub =
"UI.Minimize
"
633 Const cstSubArgs =
"[WindowName]
"
635 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
638 If IsMissing(WindowName) Or IsEmpty(WindowName) Then WindowName =
""
639 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
640 If Not SF_Utils._ValidateFile(WindowName,
"WindowName
", , True) Then GoTo Finally
645 If Len(WindowName)
> 0 Then
646 Set oEnum = StarDesktop.Components().createEnumeration
647 Do While oEnum.hasMoreElements And Not bFound
648 Set oComp = oEnum.nextElement
649 vWindow = SF_UI._IdentifyWindow(oComp)
651 ' Does the current window match the arguments ?
652 If (Len(.WindowFileName)
> 0 And .WindowFileName = SF_FileSystem.ConvertToUrl(WindowName)) _
653 Or (Len(.WindowName)
> 0 And .WindowName = WindowName) _
654 Or (Len(.WindowTitle)
> 0 And .WindowTitle = WindowName) Then bFound = True
658 vWindow = SF_UI._IdentifyWindow(StarDesktop.CurrentComponent)
663 Set oContainer = vWindow.Frame.ContainerWindow
664 oContainer.IsMinimized = True
668 SF_Utils._ExitFunction(cstThisSub)
672 End Sub
' ScriptForge.SF_UI.Minimize
674 REM -----------------------------------------------------------------------------
675 Public Function Methods() As Variant
676 ''' Return the list of public methods of the UI service as an array
678 Methods = Array(
"Activate
" _
679 ,
"CreateBaseDocument
" _
680 ,
"CreateDocument
" _
681 ,
"Documents
" _
682 ,
"GetDocument
" _
683 ,
"Maximize
" _
684 ,
"Minimize
" _
685 ,
"OpenBaseDocument
" _
686 ,
"OpenDocument
" _
687 ,
"Resize
" _
688 ,
"RunCommand
" _
689 ,
"SetStatusbar
" _
690 ,
"ShowProgressBar
" _
691 ,
"WindowExists
" _
694 End Function
' ScriptForge.SF_UI.Methods
696 REM -----------------------------------------------------------------------------
697 Public Function OpenBaseDocument(Optional ByVal FileName As Variant _
698 , Optional ByVal RegistrationName As Variant _
699 , Optional ByVal MacroExecution As Variant _
701 ''' Open an existing LibreOffice Base document and return a SFDocuments.Document object
702 ''' Args:
703 ''' FileName: Identifies the file to open. It must follow the SF_FileSystem.FileNaming notation
704 ''' RegistrationName: the name of a registered database
705 ''' It is ignored if FileName
<> ""
706 ''' MacroExecution: one of the MACROEXECxxx constants
707 ''' Returns:
708 ''' A SFDocuments.SF_Base object
709 ''' Null if the opening failed, including when due to a user decision
710 ''' Exceptions:
711 ''' BASEDOCUMENTOPENERROR Wrong arguments
712 ''' Examples:
713 ''' Dim mBasec As Object, FSO As Object
714 ''' Set myBase = ui.OpenBaseDocument(
"C:\Temp\myDB.odb
", MacroExecution := ui.MACROEXECNEVER)
716 Dim oOpen As Variant
' Return value
717 Dim vProperties As Variant
' Array of com.sun.star.beans.PropertyValue
718 Dim oDBContext As Object
' com.sun.star.sdb.DatabaseContext
719 Dim oComp As Object
' Loaded component com.sun.star.lang.XComponent
720 Dim sFile As String
' Alias for FileName
721 Dim iMacro As Integer
' Alias for MacroExecution
722 Const cstThisSub =
"UI.OpenBaseDocument
"
723 Const cstSubArgs =
"[FileName=
""""], [RegistrationName=
""""], [MacroExecution=
0|
1|
2]
"
725 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
729 If IsMissing(FileName) Or IsEmpty(FileName) Then FileName =
""
730 If IsMissing(RegistrationName) Or IsEmpty(RegistrationName) Then RegistrationName =
""
731 If IsMissing(MacroExecution) Or IsEmpty(MacroExecution) Then MacroExecution = MACROEXECNORMAL
733 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
734 If Not SF_Utils._ValidateFile(FileName,
"FileName
", , True) Then GoTo Finally
735 If Not SF_Utils._Validate(RegistrationName,
"RegistrationName
", V_STRING) Then GoTo Finally
736 If Not SF_Utils._Validate(MacroExecution,
"MacroExecution
", V_NUMERIC _
737 , Array(MACROEXECNORMAL, MACROEXECNEVER, MACROEXECALWAYS)) Then GoTo Finally
740 ' Check the existence of FileName
741 If Len(FileName) =
0 Then
' FileName has precedence over RegistrationName
742 If Len(RegistrationName) =
0 Then GoTo CatchError
743 Set oDBContext = SF_Utils._GetUNOService(
"DatabaseContext
")
744 If Not oDBContext.hasRegisteredDatabase(RegistrationName) Then GoTo CatchError
745 FileName = SF_FileSystem._ConvertFromUrl(oDBContext.getDatabaseLocation(RegistrationName))
747 If Not SF_FileSystem.FileExists(FileName) Then GoTo CatchError
750 With com.sun.star.document.MacroExecMode
751 Select Case MacroExecution
752 Case
0 : iMacro = .USE_CONFIG
753 Case
1 : iMacro = .NEVER_EXECUTE
754 Case
2 : iMacro = .ALWAYS_EXECUTE_NO_WARN
758 vProperties = Array(SF_Utils._MakePropertyValue(
"MacroExecutionMode
", iMacro))
760 sFile = SF_FileSystem._ConvertToUrl(FileName)
761 Set oComp = StarDesktop.loadComponentFromURL(sFile,
"_blank
",
0, vProperties)
762 If Not IsNull(oComp) Then Set oOpen = CreateScriptService(
"SFDocuments.Document
", oComp)
765 Set OpenBaseDocument = oOpen
766 SF_Utils._ExitFunction(cstThisSub)
771 SF_Exception.RaiseFatal(BASEDOCUMENTOPENERROR,
"FileName
", FileName,
"RegistrationName
", RegistrationName)
773 End Function
' ScriptForge.SF_UI.OpenBaseDocument
775 REM -----------------------------------------------------------------------------
776 Public Function OpenDocument(Optional ByVal FileName As Variant _
777 , Optional ByVal Password As Variant _
778 , Optional ByVal ReadOnly As Variant _
779 , Optional ByVal Hidden As Variant _
780 , Optional ByVal MacroExecution As Variant _
781 , Optional ByVal FilterName As Variant _
782 , Optional ByVal FilterOptions As Variant _
784 ''' Open an existing LibreOffice document with the given options
785 ''' Args:
786 ''' FileName: Identifies the file to open. It must follow the SF_FileSystem.FileNaming notation
787 ''' Password: To use when the document is protected
788 ''' If wrong or absent while the document is protected, the user will be prompted to enter a password
789 ''' ReadOnly: Default = False
790 ''' Hidden: if True, open in the background (default = False)
791 ''' To use with caution: activation or closure can only happen programmatically
792 ''' MacroExecution: one of the MACROEXECxxx constants
793 ''' FilterName: the name of a filter that should be used for loading the document
794 ''' If present, the filter must exist
795 ''' FilterOptions: an optional string of options associated with the filter
796 ''' Returns:
797 ''' A SFDocuments.SF_Document object or one of its subclasses
798 ''' Null if the opening failed, including when due to a user decision
799 ''' Exceptions:
800 ''' DOCUMENTOPENERROR Wrong arguments
801 ''' Examples:
802 ''' Dim myDoc As Object, FSO As Object
803 ''' Set myDoc = ui.OpenDocument(
"C:\Temp\myFile.odt
", MacroExecution := ui.MACROEXECNEVER)
805 Dim oOpen As Variant
' Return value
806 Dim oFilterFactory As Object
' com.sun.star.document.FilterFactory
807 Dim vProperties As Variant
' Array of com.sun.star.beans.PropertyValue
808 Dim oComp As Object
' Loaded component com.sun.star.lang.XComponent
809 Dim sFile As String
' Alias for FileName
810 Dim iMacro As Integer
' Alias for MacroExecution
811 Const cstThisSub =
"UI.OpenDocument
"
812 Const cstSubArgs =
"FileName, [Password=
""""], [ReadOnly=False], [Hidden=False], [MacroExecution=
0|
1|
2], [FilterName=
""""], [FilterOptions=
""""]
"
814 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
818 If IsMissing(Password) Or IsEmpty(Password) Then Password =
""
819 If IsMissing(ReadOnly) Or IsEmpty(ReadOnly) Then ReadOnly = False
820 If IsMissing(Hidden) Or IsEmpty(Hidden) Then Hidden = False
821 If IsMissing(MacroExecution) Or IsEmpty(MacroExecution) Then MacroExecution = MACROEXECNORMAL
822 If IsMissing(FilterName) Or IsEmpty(FilterName) Then FilterName =
""
823 If IsMissing(FilterOptions) Or IsEmpty(FilterOptions) Then FilterOptions =
""
825 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
826 If Not SF_Utils._ValidateFile(FileName,
"FileName
") Then GoTo Finally
827 If Not SF_Utils._Validate(Password,
"Password
", V_STRING) Then GoTo Finally
828 If Not SF_Utils._Validate(ReadOnly,
"ReadOnly
", V_BOOLEAN) Then GoTo Finally
829 If Not SF_Utils._Validate(Hidden,
"Hidden
", V_BOOLEAN) Then GoTo Finally
830 If Not SF_Utils._Validate(MacroExecution,
"MacroExecution
", V_NUMERIC _
831 , Array(MACROEXECNORMAL, MACROEXECNEVER, MACROEXECALWAYS)) Then GoTo Finally
832 If Not SF_Utils._Validate(FilterName,
"FilterName
", V_STRING) Then GoTo Finally
833 If Not SF_Utils._Validate(FilterOptions,
"FilterOptions
", V_STRING) Then GoTo Finally
836 ' Check the existence of FileName and FilterName
837 If Not SF_FileSystem.FileExists(FileName) Then GoTo CatchError
838 If Len(FilterName)
> 0 Then
839 Set oFilterFactory = SF_Utils._GetUNOService(
"FilterFactory
")
840 If Not oFilterFactory.hasByName(FilterName) Then GoTo CatchError
844 With com.sun.star.document.MacroExecMode
845 Select Case MacroExecution
846 Case
0 : iMacro = .USE_CONFIG
847 Case
1 : iMacro = .NEVER_EXECUTE
848 Case
2 : iMacro = .ALWAYS_EXECUTE_NO_WARN
852 vProperties = Array( _
853 SF_Utils._MakePropertyValue(
"ReadOnly
", ReadOnly) _
854 , SF_Utils._MakePropertyValue(
"Hidden
", Hidden) _
855 , SF_Utils._MakePropertyValue(
"MacroExecutionMode
", iMacro) _
856 , SF_Utils._MakePropertyValue(
"FilterName
", FilterName) _
857 , SF_Utils._MakePropertyValue(
"FilterOptions
", FilterOptions) _
859 If Len(Password)
> 0 Then
' Password is to add only if
<> "" !?
860 vProperties = SF_Array.Append(vProperties, SF_Utils._MakePropertyValue(
"Password
", Password))
863 sFile = SF_FileSystem._ConvertToUrl(FileName)
864 Set oComp = StarDesktop.loadComponentFromURL(sFile,
"_blank
",
0, vProperties)
865 If Not IsNull(oComp) Then Set oOpen = CreateScriptService(
"SFDocuments.Document
", oComp)
868 Set OpenDocument = oOpen
869 SF_Utils._ExitFunction(cstThisSub)
874 SF_Exception.RaiseFatal(DOCUMENTOPENERROR,
"FileName
", FileName,
"Password
", Password,
"FilterName
", FilterName)
876 End Function
' ScriptForge.SF_UI.OpenDocument
878 REM -----------------------------------------------------------------------------
879 Public Function Properties() As Variant
880 ''' Return the list or properties of the Timer class as an array
882 Properties = Array( _
883 "ActiveWindow
" _
884 ,
"Height
" _
885 ,
"Width
" _
890 End Function
' ScriptForge.SF_UI.Properties
892 REM -----------------------------------------------------------------------------
893 Public Sub Resize(Optional ByVal Left As Variant _
894 , Optional ByVal Top As Variant _
895 , Optional ByVal Width As Variant _
896 , Optional ByVal Height As Variant _
898 ''' Resizes and/or moves the active window. Negative arguments are ignored.
899 ''' If the window was minimized or without arguments, it is restored
900 ''' Args:
901 ''' Left, Top: Distances from top and left edges of the screen
902 ''' Width, Height: Dimensions of the window
903 ''' Examples:
904 ''' ui.Resize(
10,,
500)
' Top and Height are unchanged
905 ''' ...
907 Dim vWindow As Window
' A single component
908 Dim oContainer As Object
' com.sun.star.awt.XWindow
909 Dim iPosSize As Integer
' Computes which of the
4 arguments should be considered
910 Const cstThisSub =
"UI.Resize
"
911 Const cstSubArgs =
"[Left], [Top], [Width], [Height]
"
913 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
916 If IsMissing(Left) Or IsEmpty(Left) Then Left = -
1
917 If IsMissing(Top) Or IsEmpty(Top) Then Top = -
1
918 If IsMissing(Width) Or IsEmpty(Width) Then Width = -
1
919 If IsMissing(Height) Or IsEmpty(Height) Then Height = -
1
920 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
921 If Not SF_Utils._Validate(Left,
"Left
", V_NUMERIC) Then GoTo Finally
922 If Not SF_Utils._Validate(Top,
"Top
", V_NUMERIC) Then GoTo Finally
923 If Not SF_Utils._Validate(Width,
"Width
", V_NUMERIC) Then GoTo Finally
924 If Not SF_Utils._Validate(Height,
"Height
", V_NUMERIC) Then GoTo Finally
928 vWindow = SF_UI._IdentifyWindow(StarDesktop.CurrentComponent)
929 If Not IsNull(vWindow.Frame) Then
930 Set oContainer = vWindow.Frame.ContainerWindow
932 If Left
>=
0 Then iPosSize = iPosSize + com.sun.star.awt.PosSize.X
933 If Top
>=
0 Then iPosSize = iPosSize + com.sun.star.awt.PosSize.Y
934 If Width
> 0 Then iPosSize = iPosSize + com.sun.star.awt.PosSize.WIDTH
935 If Height
> 0 Then iPosSize = iPosSize + com.sun.star.awt.PosSize.HEIGHT
939 .setPosSize(Left, Top, Width, Height, iPosSize)
944 SF_Utils._ExitFunction(cstThisSub)
948 End Sub
' ScriptForge.SF_UI.Resize
950 REM -----------------------------------------------------------------------------
951 Public Sub RunCommand(Optional ByVal Command As Variant _
952 , ParamArray Args As Variant _
954 ''' Run on the current window the given menu command. The command is executed with or without arguments
955 ''' A few typical commands:
956 ''' About, Delete, Edit, Undo, Copy, Paste, ...
957 ''' Dozens can be found on next page: https://wiki.documentfoundation.org/Development/DispatchCommands
958 ''' Args:
959 ''' Command: Case-sensitive. The command itself is not checked.
960 ''' If the command does not contain the
".uno:
" prefix, it is added.
961 ''' If nothing happens, then the command is probably wrong
962 ''' Args: Pairs of arguments name (string), value (any)
963 ''' Returns:
964 ''' Examples:
965 ''' ui.RunCommand(
"BasicIDEAppear
", _
966 ''' "Document
",
"LibreOffice Macros
& Dialogs
", _
967 ''' "LibName
",
"ScriptForge
", _
968 ''' "Name
",
"SF_Session
", _
969 ''' "Line
",
600)
971 Dim oDispatch
' com.sun.star.frame.DispatchHelper
972 Dim vProps As Variant
' Array of PropertyValues
973 Dim vValue As Variant
' A single value argument
974 Dim sCommand As String
' Alias of Command
976 Const cstPrefix =
".uno:
"
978 Const cstThisSub =
"UI.RunCommand
"
979 Const cstSubArgs =
"Command, [arg0Name, arg0Value], [arg1Name, arg1Value], ...
"
981 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
984 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
985 If Not SF_Utils._Validate(Command,
"Command
", V_STRING) Then GoTo Finally
986 If Not SF_Utils._ValidateArray(Args,
"Args
",
1) Then GoTo Finally
987 For i =
0 To UBound(Args) -
1 Step
2
988 If Not SF_Utils._Validate(Args(i),
"Arg
" & CStr(i/
2)
& "Name
", V_STRING) Then GoTo Finally
993 ' Build array of property values
995 For i =
0 To UBound(Args) -
1 Step
2
996 If IsEmpty(Args(i +
1)) Then vValue = Null Else vValue = Args(i +
1)
997 vProps = SF_Array.Append(vProps, SF_Utils._MakePropertyValue(Args(i), vValue))
999 Set oDispatch = SF_Utils._GetUNOService(
"DispatchHelper
")
1000 If SF_String.StartsWith(Command, cstPrefix) Then sCommand = Command Else sCommand = cstPrefix
& Command
1001 oDispatch.executeDispatch(StarDesktop.ActiveFrame, sCommand,
"",
0, vProps)
1004 SF_Utils._ExitFunction(cstThisSub)
1008 End Sub
' ScriptForge.SF_UI.RunCommand
1010 REM -----------------------------------------------------------------------------
1011 Public Function SetProperty(Optional ByVal PropertyName As Variant _
1012 , Optional ByRef Value As Variant _
1014 ''' Set a new value to the given property
1015 ''' Args:
1016 ''' PropertyName: the name of the property as a string
1017 ''' Value: its new value
1018 ''' Exceptions
1019 ''' ARGUMENTERROR The property does not exist
1021 Const cstThisSub =
"UI.SetProperty
"
1022 Const cstSubArgs =
"PropertyName, Value
"
1024 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
1028 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
1029 If Not SF_Utils._Validate(PropertyName,
"PropertyName
", V_STRING, Properties()) Then GoTo Catch
1033 Select Case UCase(PropertyName)
1038 SF_Utils._ExitFunction(cstThisSub)
1042 End Function
' ScriptForge.SF_UI.SetProperty
1044 REM -----------------------------------------------------------------------------
1045 Public Sub SetStatusbar(Optional ByVal Text As Variant _
1046 , Optional ByVal Percentage As Variant _
1048 ''' Display a text and a progressbar in the status bar of the active window
1049 ''' Any subsequent calls in the same macro run refer to the same status bar of the same window,
1050 ''' even if the window is not active anymore
1051 ''' A call without arguments resets the status bar to its normal state.
1052 ''' Args:
1053 ''' Text: the optional text to be displayed before the progress bar
1054 ''' Percentage: the optional degree of progress between
0 and
100
1055 ''' Examples:
1056 ''' Dim i As Integer
1057 ''' For i =
0 To
100
1058 ''' ui.SetStatusbar(
"Progress ...
", i)
1059 ''' Wait
50
1060 ''' Next i
1061 ''' ui.SetStatusbar
1064 Dim oControl As Object
1065 Dim oStatusbar As Object
1066 Const cstThisSub =
"UI.SetStatusbar
"
1067 Const cstSubArgs =
"[Text], [Percentage]
"
1069 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
1072 If IsMissing(Text) Or IsEmpty(Text) Then Text =
""
1073 If IsMissing(Percentage) Or IsEmpty(Percentage) Then Percentage = -
1
1074 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
1075 If Not SF_Utils._Validate(Text,
"Text
", V_STRING) Then GoTo Finally
1076 If Not SF_Utils._Validate(Percentage,
"Percentage
", V_NUMERIC) Then GoTo Finally
1080 Set oStatusbar = _SF_.Statusbar
1081 If IsNull(oStatusbar) Then
' Initial call
1082 Set oComp = StarDesktop.CurrentComponent
1083 If Not IsNull(oComp) Then
1084 Set oControl = Nothing
1085 If SF_Session.HasUnoProperty(oComp,
"CurrentController
") Then Set oControl = oComp.CurrentController
1086 If Not IsNull(oControl) Then
1087 If SF_Session.HasUnoMethod(oControl,
"getStatusIndicator
") Then oStatusbar = oControl.getStatusIndicator()
1090 If Not IsNull(oStatusbar) Then oStatusBar.start(
"",
100)
1092 If Not IsNull(oStatusbar) Then
1094 If Len(Text) =
0 And Percentage = -
1 Then
1096 Set oStatusbar = Nothing
1098 If Len(Text)
> 0 Then .setText(Text)
1099 If Percentage
>=
0 And Percentage
<=
100 Then .setValue(Percentage)
1105 Set _SF_.Statusbar = oStatusbar
1106 SF_Utils._ExitFunction(cstThisSub)
1110 End Sub
' ScriptForge.SF_UI.SetStatusbar
1112 REM -----------------------------------------------------------------------------
1113 Public Sub ShowProgressBar(Optional Title As Variant _
1114 , Optional ByVal Text As Variant _
1115 , Optional ByVal Percentage As Variant _
1116 , Optional ByRef _Context As Variant _
1118 ''' Display a non-modal dialog box. Specify its title, an explicatory text and the progress on a progressbar
1119 ''' A call without arguments erases the progress bar dialog.
1120 ''' The box will anyway vanish at the end of the macro run.
1121 ''' Args:
1122 ''' Title: the title appearing on top of the dialog box (Default =
"ScriptForge
")
1123 ''' Text: the optional text to be displayed above the progress bar (default = zero-length string)
1124 ''' Percentage: the degree of progress between
0 and
100. Default =
0
1125 ''' _Context: from Python, the XComponentXontext (FOR INTERNAL USE ONLY)
1126 ''' Examples:
1127 ''' Dim i As Integer
1128 ''' For i =
0 To
100
1129 ''' ui.ShowProgressBar(,
"Progress ...
" & i
& "/
100", i)
1130 ''' Wait
50
1131 ''' Next i
1132 ''' ui.ShowProgressBar
1134 Dim bFirstCall As Boolean
' True at first invocation of method
1135 Dim oDialog As Object
' SFDialogs.Dialog object
1136 Dim oFixedText As Object
' SFDialogs.DialogControl object
1137 Dim oProgressBar As Object
' SFDialogs.DialogControl object
1138 Dim sTitle As String
' Alias of Title
1139 Const cstThisSub =
"UI.ShowProgressBar
"
1140 Const cstSubArgs =
"[Title], [Text], [Percentage]
"
1142 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
1145 If IsMissing(Title) Or IsEmpty(Title) Then Title =
""
1146 If IsMissing(Text) Or IsEmpty(Text) Then Text =
""
1147 If IsMissing(Percentage) Or IsEmpty(Percentage) Then Percentage = -
1
1148 If IsMissing(_Context) Or IsEmpty(_Context) Then _Context = Nothing
1149 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
1150 If Not SF_Utils._Validate(Title,
"Title
", V_STRING) Then GoTo Finally
1151 If Not SF_Utils._Validate(Text,
"Text
", V_STRING) Then GoTo Finally
1152 If Not SF_Utils._Validate(Percentage,
"Percentage
", V_NUMERIC) Then GoTo Finally
1157 Set oDialog = .ProgressBarDialog
1158 Set oFixedText = .ProgressBarText
1159 Set oProgressBar = .ProgressBarBar
1162 bFirstCall = ( IsNull(oDialog) )
1163 If Not bFirstCall Then bFirstCall = Not oDialog._IsStillAlive(False)
' False to not raise an error
1164 If bFirstCall Then Set oDialog = CreateScriptService(
"SFDialogs.Dialog
",
"GlobalScope
",
"ScriptForge
",
"dlgProgress
", _Context)
1166 If Not IsNull(oDialog) Then
1167 If Len(Title) =
0 And Len(Text) =
0 And Percentage = -
1 Then
1168 Set oDialog = .Dispose()
1170 .Caption = Iif(Len(Title)
> 0, Title,
"ScriptForge
")
1172 Set oFixedText = .Controls(
"ProgressText
")
1173 Set oProgressBar = .Controls(
"ProgressBar
")
1174 .Execute(Modal := False)
1176 If Len(Text)
> 0 Then oFixedText.Caption = Text
1177 oProgressBar.Value = Iif(Percentage
>=
0 And Percentage
<=
100, Percentage,
0)
1184 Set .ProgressBarDialog = oDialog
1185 Set .ProgressBarText = oFixedText
1186 Set .ProgressBarBar = oProgressBar
1188 SF_Utils._ExitFunction(cstThisSub)
1192 End Sub
' ScriptForge.SF_UI.ShowProgressBar
1194 REM -----------------------------------------------------------------------------
1195 Public Function WindowExists(Optional ByVal WindowName As Variant) As Boolean
1196 ''' Returns True if the specified window exists
1197 ''' Args:
1198 ''' WindowName: see definitions
1199 ''' Returns:
1200 ''' True if the given window is found
1201 ''' Examples:
1202 ''' ui.WindowExists(
"C:\Me\My file.odt
")
1204 Dim bWindowExists As Boolean
' Return value
1205 Dim oEnum As Object
' com.sun.star.container.XEnumeration
1206 Dim oComp As Object
' com.sun.star.lang.XComponent
1207 Dim vWindow As Window
' A single component
1208 Dim oContainer As Object
' com.sun.star.awt.XWindow
1209 Const cstThisSub =
"UI.WindowExists
"
1210 Const cstSubArgs =
"WindowName
"
1212 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
1213 bWindowExists = False
1216 If IsMissing(WindowName) Or IsEmpty(WindowName) Then WindowName =
""
1217 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
1218 If Not SF_Utils._ValidateFile(WindowName,
"WindowName
") Then GoTo Finally
1222 Set oEnum = StarDesktop.Components().createEnumeration
1223 Do While oEnum.hasMoreElements
1224 Set oComp = oEnum.nextElement
1225 vWindow = SF_UI._IdentifyWindow(oComp)
1227 ' Does the current window match the arguments ?
1228 If (Len(.WindowFileName)
> 0 And .WindowFileName = SF_FileSystem.ConvertToUrl(WindowName)) _
1229 Or (Len(.WindowName)
> 0 And .WindowName = WindowName) _
1230 Or (Len(.WindowTitle)
> 0 And .WindowTitle = WindowName) Then
1231 bWindowExists = True
1238 WindowExists = bWindowExists
1239 SF_Utils._ExitFunction(cstThisSub)
1243 End Function
' ScriptForge.SF_UI.WindowExists
1245 REM =========================================================== PRIVATE FUNCTIONS
1247 REM -----------------------------------------------------------------------------
1248 Public Sub _CloseProgressBar(Optional ByRef poEvent As Object)
1249 ''' Triggered by the Close button in the dlgProgress dialog
1250 ''' to simply close the dialog
1252 ShowProgressBar()
' Without arguments =
> close the dialog
1254 End Sub
' ScriptForge.SF_UI._CloseProgressBar
1256 REM -----------------------------------------------------------------------------
1257 Function _GetConfigurationManager(ByRef poComponent) As String
1258 ''' Derives the argument to be passed to a configuration manager
1259 ''' (managing the user interface elements like menus, toolbars, ...)
1260 ''' from the nature of the component
1261 ''' Args:
1262 ''' poComponent: any component in desktop, typically a document but not only
1264 Dim sConfigurationManager As String
' Return value
1265 Dim oWindow As Object
' Window type
1268 ' On Local Error GoTo Catch
1269 If IsNull(poComponent) Then GoTo Catch
1272 Set oWindow = _IdentifyWindow(poComponent)
1274 ' Derive the name of the UI configuration manager from the component type
1276 Select Case .WindowName
1277 Case BASICIDE : sConfigurationManager =
"com.sun.star.script.BasicIDE
"
1278 Case WELCOMESCREEN : sConfigurationManager =
"com.sun.star.frame.StartModule
"
1280 Select Case .DocumentType
1281 Case BASEDOCUMENT : sConfigurationManager =
"com.sun.star.sdb.OfficeDatabaseDocument
"
1282 Case CALCDOCUMENT : sConfigurationManager =
"com.sun.star.sheet.SpreadsheetDocument
"
1283 Case DRAWDOCUMENT : sConfigurationManager =
"com.sun.star.drawing.DrawingDocument
"
1284 Case FORMDOCUMENT : sConfigurationManager =
"com.sun.star.sdb.FormDesign
"
1285 Case IMPRESSDOCUMENT : sConfigurationManager =
"com.sun.star.presentation.PresentationDocument
"
1286 Case MATHDOCUMENT : sConfigurationManager =
"com.sun.star.formula.FormulaProperties
"
1287 Case WRITERDOCUMENT : sConfigurationManager =
"com.sun.star.text.TextDocument
"
1288 Case TABLEDATA, QUERYDATA, SQLDATA
1289 sConfigurationManager =
"com.sun.star.sdb.DataSourceBrowser
"
1290 Case Else : sConfigurationManager =
""
1296 _GetConfigurationManager = sConfigurationManager
1299 On Local Error GoTo
0
1301 End Function
' ScriptForge.SF_UI._GetConfigurationManager
1303 REM -----------------------------------------------------------------------------
1304 Public Function _IdentifyWindow(ByRef poComponent As Object) As Object
1305 ''' Return a Window object (definition on top of module) based on component given as argument
1306 ''' Is a shortcut to explore the most relevant properties or objects bound to a UNO component
1308 Dim oWindow As Window
' Return value
1309 Dim sImplementation As String
' Component
's implementationname
1310 Dim sIdentifier As String
' Component
's identifier
1311 Dim vSelection As Variant
' Array of poCOmponent.Selection property values
1312 Dim iCommandType As Integer
' Datasheet type
1313 Dim FSO As Object
' Alias for SF_FileSystem
1315 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
1316 Set _IdentifyWindow = Nothing
1317 sImplementation =
"" : sIdentifier =
""
1319 Set FSO = SF_FileSystem
1321 Set .Frame = Nothing
1322 Set .Component = Nothing
1323 .WindowName =
""
1324 .WindowTitle =
""
1325 .WindowFileName =
""
1326 .DocumentType =
""
1327 .ParentName =
""
1328 If IsNull(poComponent) Then GoTo Finally
1329 If SF_Session.HasUnoProperty(poComponent,
"ImplementationName
") Then sImplementation = poComponent.ImplementationName
1330 If SF_Session.HasUnoProperty(poComponent,
"Identifier
") Then sIdentifier = poComponent.Identifier
1331 Set .Component = poComponent
1332 Select Case sImplementation
1333 Case
"com.sun.star.comp.basic.BasicIDE
"
1334 .WindowName = BASICIDE
1335 Case
"com.sun.star.comp.dba.ODatabaseDocument
" ' No identifier
1336 .WindowFileName = SF_Utils._GetPropertyValue(poComponent.Args,
"URL
")
1337 If Len(.WindowFileName)
> 0 Then .WindowName = FSO.GetName(FSO._ConvertFromUrl(.WindowFileName))
1338 .DocumentType = BASEDOCUMENT
1339 Case
"org.openoffice.comp.dbu.ODatasourceBrowser
" ' Base datasheet (table, query or sql) in read mode
1340 Set .Frame = poComponent.Frame
1341 If Not IsEmpty(poComponent.Selection) Then
' Empty for (F4) DatasourceBrowser !!
1342 vSelection = poComponent.Selection
1343 .WindowName = SF_Utils._GetPropertyValue(vSelection,
"Command
")
1344 iCommandType = SF_Utils._GetPropertyValue(vSelection,
"CommandType
")
1345 Select Case iCommandType
1346 Case com.sun.star.sdb.CommandType.TABLE : .DocumentType = TABLEDATA
1347 Case com.sun.star.sdb.CommandType.QUERY : .DocumentType = QUERYDATA
1348 Case com.sun.star.sdb.CommandType.COMMAND : .DocumentType = SQLDATA
1350 .ParentName = SF_Utils._GetPropertyValue(vSelection,
"DataSourceName
")
1351 .WindowTitle = .WindowName
1353 Case
"org.openoffice.comp.dbu.OTableDesign
",
"org.openoffice.comp.dbu.OQueryDesign
" ' Table or Query in Edit mode
1354 Case
"org.openoffice.comp.dbu.ORelationDesign
"
1355 Case
"com.sun.star.comp.sfx2.BackingComp
" ' Welcome screen
1356 Set .Frame = poComponent.Frame
1357 .WindowName = WELCOMESCREEN
1359 If Len(sIdentifier)
> 0 Then
1360 ' Do not use URL : it contains the TemplateFile when new documents are created from a template
1361 .WindowFileName = poComponent.Location
1362 If Len(.WindowFileName)
> 0 Then .WindowName = FSO.GetName(FSO._ConvertFromUrl(.WindowFileName))
1363 If SF_Session.HasUnoProperty(poComponent,
"Title
") Then .WindowTitle = poComponent.Title
1364 Select Case sIdentifier
1365 Case
"com.sun.star.sdb.FormDesign
" ' Form
1366 .DocumentType = FORMDOCUMENT
1367 Case
"com.sun.star.sdb.TextReportDesign
" ' Report
1368 Case
"com.sun.star.text.TextDocument
" ' Writer
1369 .DocumentType = WRITERDOCUMENT
1370 Case
"com.sun.star.sheet.SpreadsheetDocument
" ' Calc
1371 .DocumentType = CALCDOCUMENT
1372 Case
"com.sun.star.presentation.PresentationDocument
" ' Impress
1373 .DocumentType = IMPRESSDOCUMENT
1374 Case
"com.sun.star.drawing.DrawingDocument
" ' Draw
1375 .DocumentType = DRAWDOCUMENT
1376 Case
"com.sun.star.formula.FormulaProperties
" ' Math
1377 .DocumentType = MATHDOCUMENT
1382 If IsNull(.Frame) Then
1383 If Not IsNull(poComponent.CurrentController) Then Set .Frame = poComponent.CurrentController.Frame
1388 Set _IdentifyWindow = oWindow
1392 End Function
' ScriptForge.SF_UI._IdentifyWindow
1394 REM -----------------------------------------------------------------------------
1395 Public Function _ListToolbars(ByRef poComponent As Object) As Object
1396 ''' Returns a SF_Dictionary object containing a list of all available
1397 ''' toolbars in the given component
1398 ''' A toolbar may be located:
1399 ''' - builtin in the LibreOffice configuration, but dependent on the component type
1400 ''' - added by the user and stored in the LibreOffice configuration of the user
1401 ''' - added by the user and stored in the component/document itself
1402 ''' The output dictionary has as
1403 ''' key: the UIName of the toolbar when not blank, otherwise the last component of its ResourceURL
1404 ''' item: a _Toolbar object (see top of module)
1405 ''' Menubar, statusbar and popup menus are ignored.
1406 ''' Args:
1407 ''' poComponent: any component in desktop, typically a document but not only
1409 Dim oToolbarsDict As Object
' Return value
1410 Dim oConfigMgr As Object
' com.sun.star.ui.ModuleUIConfigurationManagerSupplier
1411 Dim sConfigurationManager As String
' Derived from the component
's type
1412 Dim oUIConfigMgr As Object
' com.sun.star.comp.framework.ModuleUIConfigurationManager
1413 Dim vCommandBars As Variant
' Array of bars in component
1414 Dim vCommandBar As Variant
' Array of PropertyValue about a single bar
1415 Dim oToolbar As Object
' Toolbar description as a _Toolbar object
1416 Dim sResourceURL As String
' Toolbar internal name as
"private:resource/toolbar/...
"
1417 Dim sUIName As String
' Toolbar external name, may be zero-length string
1418 Dim sBarName As String
' External bar name: either UIName or last component of resource URL
1421 Const cstCUSTOM =
"custom_
"
1424 ' On Local Error GoTo Catch
1425 If IsNull(poComponent) Then GoTo Catch
1428 Set oToolbarsDict = CreateScriptService(
"Dictionary
", True)
' with case-sensitive comparison of keys
1430 ' 1. Collect all builtin and custom toolbars stored in the LibreOffice configuration files
1432 ' Derive the name of the UI configuration manager from the component type
1433 sConfigurationManager = _GetConfigurationManager(poComponent)
1435 Set oConfigMgr = SF_Utils._GetUNOService(
"ModuleUIConfigurationManagerSupplier
")
1436 Set oUIConfigMgr = oConfigMgr.getUIConfigurationManager(sConfigurationManager)
1437 vCommandBars = oUIConfigMgr.getUIElementsInfo(com.sun.star.ui.UIElementType.TOOLBAR)
1439 ' Ignore statusbar, menubar and popup menus. Store toolbars in dictionary
1440 For i =
0 To UBound(vCommandBars)
1441 vCommandBar = vCommandBars(i)
1442 sResourceURL = SF_Utils._GetPropertyValue(vCommandBar,
"ResourceURL
")
1443 sUIName = SF_Utils._GetPropertyValue(vCommandBar,
"UIName
")
1444 If Len(sUIName)
> 0 Then sBarName = sUIName Else sBarName = Split(sResourceURL,
"/
")(
2)
1445 ' Store a new entry in the returned dictionary
1446 If Not oToolbarsDict.Exists(sBarName) Then
1447 Set oToolbar = New _Toolbar
1449 Set .Component = poComponent
1450 .ResourceURL = sResourceURL
1452 Set .UIConfigurationManager = oUIConfigMgr
1453 .ElementsInfoIndex = i
1454 ' Distinguish builtin and custom toolbars stored in the application
1455 If SF_String.StartsWith(sBarName, cstCUSTOM, CaseSensitive := True) Then
1456 .Storage = cstCUSTOMTOOLBAR
1457 sBarName = Mid(sBarName, Len(cstCUSTOM) +
1)
1459 .Storage = cstBUILTINTOOLBAR
1462 oToolbarsDict.Add(sBarName, oToolbar)
1466 ' 2. Collect all toolbars stored in the current component/document
1468 ' Some components (e.g. datasheets) cannot contain own toolbars
1469 If SF_Session.HasUnoMethod(poComponent,
"getUIConfigurationManager
") Then
1470 Set oUIConfigMgr = poComponent.getUIConfigurationManager
1471 vCommandBars = oUIConfigMgr.getUIElementsInfo(com.sun.star.ui.UIElementType.TOOLBAR)
1472 For i =
0 To UBound(vCommandBars)
1473 vCommandBar = vCommandBars(i)
1474 sResourceURL = SF_Utils._GetPropertyValue(vCommandBar,
"ResourceURL
")
1475 sUIName = SF_Utils._GetPropertyValue(vCommandBar,
"UIName
")
1476 If Len(sUIName)
> 0 Then sBarName = sUIName Else sBarName = Split(sResourceURL,
"/
")(
2)
1477 ' Store a new entry in the returned dictionary
1478 If Not oToolbarsDict.Exists(sBarName) Then
1479 Set oToolbar = New _Toolbar
1481 Set .Component = poComponent
1482 .ResourceURL = sResourceURL
1484 Set .UIConfigurationManager = oUIConfigMgr
1485 .ElementsInfoIndex = i
1486 .Storage = cstCUSTOMDOCTOOLBAR
1488 oToolbarsDict.Add(sBarName, oToolbar)
1494 Set _ListToolbars = oToolbarsDict
1497 Set oToolbarsDict = Nothing
1499 End Function
' ScriptForge.SF_UI._ListToolbars
1501 REM -----------------------------------------------------------------------------
1502 Public Function _PosSize() As Object
1503 ''' Returns the PosSize structure of the active window
1505 Dim vWindow As Window
' A single component
1506 Dim oContainer As Object
' com.sun.star.awt.XWindow
1507 Dim oPosSize As Object
' com.sun.star.awt.Rectangle
1509 Set oPosSize = Nothing
1512 vWindow = SF_UI._IdentifyWindow(StarDesktop.CurrentComponent)
1513 If Not IsNull(vWindow.Frame) Then
1514 Set oContainer = vWindow.Frame.ContainerWindow
1515 Set oPosSize = oContainer.getPosSize()
1519 Set _PosSize = oPosSize
1521 End Function
' ScriptForge.SF_UI._PosSize
1523 REM -----------------------------------------------------------------------------
1524 Private Function _Repr() As String
1525 ''' Convert the UI instance to a readable string, typically for debugging purposes (DebugPrint ...)
1526 ''' Args:
1527 ''' Return:
1528 ''' "[UI]
"
1530 _Repr =
"[UI]
"
1532 End Function
' ScriptForge.SF_UI._Repr
1534 REM ============================================ END OF SCRIPTFORGE.SF_UI