tdf#130857 qt weld: Implement QtInstanceWidget::get_text_height
[LibreOffice.git] / wizards / source / gimmicks / GetTexts.xba
blobaf93738fdfc759737a7e650c3086ab16fdfe0a20
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
3 <!--
4 * This file is part of the LibreOffice project.
6 * This Source Code Form is subject to the terms of the Mozilla Public
7 * License, v. 2.0. If a copy of the MPL was not distributed with this
8 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 * This file incorporates work covered by the following license notice:
12 * Licensed to the Apache Software Foundation (ASF) under one or more
13 * contributor license agreements. See the NOTICE file distributed
14 * with this work for additional information regarding copyright
15 * ownership. The ASF licenses this file to you under the Apache
16 * License, Version 2.0 (the "License"); you may not use this file
17 * except in compliance with the License. You may obtain a copy of
18 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 -->
20 <script:module xmlns:script="http://openoffice.org/2000/script" script:name="GetTexts" script:language="StarBasic">Option Explicit
21 &apos; Description:
22 &apos; This macro extracts the strings out of the currently active document and inserts them into a log document.
23 &apos; The aim of the macro is to provide the programmer an insight into the OpenOffice API.
24 &apos; It focuses on how document objects are accessed.
25 &apos; Therefore not only texts of the document body are retrieved but also texts of general
26 &apos; document objects like, annotations, charts and general document information.
28 Public oLogDocument, oLogText, oLogCursor, oLogHeaderStyle, oLogBodyTextStyle as Object
29 Public oDocument as Object
30 Public LogArray(1000) as String
31 Public LogIndex as Integer
32 Public oLocHeaderStyle as Object
34 Sub Main
35 Dim sDocType as String
36 Dim oHyperCursor as Object
37 Dim oCharStyles as Object
38 BasicLibraries.LoadLibrary(&quot;Tools&quot;)
39 On Local Error GoTo NODOCUMENT
40 oDocument = StarDesktop.ActiveFrame.Controller.Model
41 sDocType = GetDocumentType(oDocument)
42 NODOCUMENT:
43 If Err &lt;&gt; 0 Then
44 Msgbox(&quot;This macro extracts all data from the active Writer, Calc or Draw/Impress document.&quot; &amp; chr(13) &amp;_
45 &quot;To start this macro you have to activate a document first.&quot; , 16, GetProductName)
46 Exit Sub
47 End If
48 On Local Error Goto 0
50 &apos; Open a new document where all the texts are inserted
51 oLogDocument = CreateNewDocument(&quot;swriter&quot;)
52 If Not IsNull(oLogDocument) Then
53 oLogText = oLogDocument.Text
55 &apos; create and define the character styles of the log document
56 oCharStyles = oLogDocument.StyleFamilies.GetByName(&quot;CharacterStyles&quot;)
57 oLogHeaderStyle = oLogDocument.createInstance(&quot;com.sun.star.style.CharacterStyle&quot;)
58 oCharStyles.InsertbyName(&quot;Log Header&quot;, oLogHeaderStyle)
60 oLogHeaderStyle.charWeight = com.sun.star.awt.FontWeight.BOLD
61 oLogBodyTextStyle = oLogDocument.createInstance(&quot;com.sun.star.style.CharacterStyle&quot;)
62 oCharStyles.InsertbyName(&quot;Log Body&quot;, oLogBodyTextStyle)
64 &apos; Insert the title of the activated document as a hyperlink
65 oHyperCursor = oLogText.createTextCursor()
66 oHyperCursor.CharWeight = com.sun.star.awt.FontWeight.BOLD
67 oHyperCursor.gotoStart(False)
68 oHyperCursor.HyperLinkURL = oDocument.URL
69 oHyperCursor.HyperLinkTarget = oDocument.URL
70 If oDocument.DocumentProperties.Title &lt;&gt; &quot;&quot; Then
71 oHyperCursor.HyperlinkName = oDocument.DocumentProperties.Title
72 End If
73 oLogText.insertString(oHyperCursor, oDocument.DocumentProperties.Title, False)
74 oLogText.insertControlCharacter(oHyperCursor,com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK,False)
76 oLogCursor = oLogText.createTextCursor()
77 oLogCursor.GotoEnd(False)
78 &apos; &quot;Switch off&quot; the Hyperlink - Properties
79 oLogCursor.SetPropertyToDefault(&quot;HyperLinkURL&quot;)
80 oLogCursor.SetPropertyToDefault(&quot;HyperLinkTarget&quot;)
81 oLogCursor.SetPropertyToDefault(&quot;HyperLinkName&quot;)
82 LogIndex = 0
84 &apos; Get the Properties of the document
85 GetDocumentProps()
87 Select Case sDocType
88 Case &quot;swriter&quot;
89 GetWriterStrings()
90 Case &quot;scalc&quot;
91 GetCalcStrings()
92 Case &quot;sdraw&quot;, &quot;simpress&quot;
93 GetDrawStrings()
94 Case Else
95 Msgbox(&quot;This macro only works with a Writer, Calc or Draw/Impress document.&quot;, 16, GetProductName())
96 End Select
97 End If
98 End Sub
101 &apos; ***********************************************Calc documents**************************************************
103 Sub GetCalcStrings()
104 Dim i, n as integer
105 Dim oSheet as Object
106 Dim SheetName as String
107 Dim oSheets as Object
108 &apos; Create a sequence of all sheets within the document
109 oSheets = oDocument.Sheets
111 For i = 0 to osheets.Count - 1
112 oSheet = osheets.GetbyIndex(i)
113 SheetName = oSheet.Name
114 MakeLogHeadLine(&quot;Sheet No. &quot; &amp; i &amp; &quot; (&quot; &amp; SheetName &amp; &quot;)&quot; )
116 &apos; Check the &quot;body&quot; of the sheet
117 GetCellTexts(oSheet)
119 If oSheet.IsScenario then
120 MakeLogHeadLine(&quot;Scenario Comments from &quot; &amp; SheetName &amp; &quot;&apos;&quot;)
121 WriteStringtoLogFile(osheet.ScenarioComment)
122 End if
124 GetAnnotations(oSheet, &quot;Annotations from &apos;&quot; &amp; SheetName &amp; &quot;&apos;&quot;)
126 GetChartStrings(oSheet, &quot;Charts from &apos;&quot; &amp; SheetName &amp; &quot;&apos;&quot;)
128 GetControlStrings(oSheet.DrawPage, &quot;Controls from &apos;&quot; &amp; SheetName &amp; &quot;&apos;&quot;)
129 Next
131 &apos; Pictures
132 GetCalcGraphicNames()
134 GetNamedRanges()
135 End Sub
138 Sub GetCellTexts(oSheet as Object)
139 Dim BigRange, BigEnum, oCell as Object
140 BigRange = oDocument.CreateInstance(&quot;com.sun.star.sheet.SheetCellRanges&quot;)
141 BigRange.InsertbyName(&quot;&quot;,oSheet)
142 BigEnum = BigRange.GetCells.CreateEnumeration
143 While BigEnum.hasmoreElements
144 oCell = BigEnum.NextElement
145 If oCell.String &lt;&gt; &quot;&quot; And Val(oCell.String) = 0then
146 WriteStringtoLogFile(oCell.String)
147 End If
148 Wend
149 End Sub
152 Sub GetAnnotations(oSheet as Object, HeaderLine as String)
153 Dim oNotes as Object
154 Dim n as Integer
155 oNotes = oSheet.getAnnotations
156 If oNotes.hasElements() then
157 MakeLogHeadLine(HeaderLine)
158 For n = 0 to oNotes.Count-1
159 WriteStringtoLogFile(oNotes.GetbyIndex(n).String)
160 Next
161 End if
162 End Sub
165 Sub GetNamedRanges()
166 Dim i as integer
167 MakeLogHeadLine(&quot;Named Ranges&quot;)
168 For i = 0 To oDocument.NamedRanges.Count - 1
169 WriteStringtoLogFile(oDocument.NamedRanges.GetbyIndex(i).Name)
170 Next
171 End Sub
174 Sub GetCalcGraphicNames()
175 Dim n,m as integer
176 MakeLogHeadLine(&quot;Graphics&quot;)
177 For n = 0 To oDocument.Drawpages.count-1
178 For m = 0 To oDocument.Drawpages.GetbyIndex(n).Count - 1
179 WriteStringtoLogFile(oDocument.DrawPages.GetbyIndex(n).GetbyIndex(m).Text.String)
180 Next m
181 Next n
182 End Sub
185 &apos; ***********************************************Writer documents**************************************************
187 Sub GetParagraphTexts(oParaObject as Object, HeadLine as String)
188 Dim ParaEnum as Object
189 Dim oPara as Object
190 Dim oTextPortEnum as Object
191 Dim oTextPortion as Object
192 Dim i as integer
193 Dim oCellNames()
194 Dim oCell as Object
196 MakeLogHeadLine(HeadLine)
197 ParaEnum = oParaObject.Text.CreateEnumeration
199 While ParaEnum.HasMoreElements
200 oPara = ParaEnum.NextElement
202 &apos; Note: The enumeration ParaEnum lists all tables and paragraphs.
203 &apos; Therefore we have to find out what kind of object &quot;oPara&quot; actually is
204 If oPara.supportsService(&quot;com.sun.star.text.Paragraph&quot;) Then
205 &apos; &quot;oPara&quot; is a Paragraph
206 oTextPortEnum = oPara.createEnumeration
207 While oTextPortEnum.hasmoreElements
208 oTextPortion = oTextPortEnum.nextElement()
209 WriteStringToLogFile(oTextPortion.String)
210 Wend
211 Else
212 &apos; &quot;oPara&quot; is a table
213 oCellNames = oPara.CellNames
214 For i = 0 To Ubound(oCellNames())
215 If oCellNames(i) &lt;&gt; &quot;&quot; Then
216 oCell = oPara.getCellByName(oCellNames(i))
217 WriteStringToLogFile(oCell.String)
218 End If
219 Next
220 End If
221 Wend
222 End Sub
225 Sub GetChartStrings(oSheet as Object, HeaderLine as String)
226 Dim i as Integer
227 Dim aChartObject as Object
228 Dim aChartDiagram as Object
230 MakeLogHeadLine(HeaderLine)
232 For i = 0 to oSheet.Charts.Count-1
233 aChartObject = oSheet.Charts.GetByIndex(i).EmbeddedObject
234 If aChartObject.HasSubTitle then
235 WriteStringToLogFile(aChartObject.SubTitle.String)
236 End If
238 If aChartObject.HasMainTitle then
239 WriteStringToLogFile(aChartObject.Title.String)
240 End If
242 aChartDiagram = aChartObject.Diagram
244 If aChartDiagram.hasXAxisTitle Then
245 WriteStringToLogFile(aChartDiagram.XAxisTitle)
246 End If
248 If aChartDiagram.hasYAxisTitle Then
249 WriteStringToLogFile(aChartDiagram.YAxisTitle)
250 End If
252 If aChartDiagram.hasZAxisTitle Then
253 WriteStringToLogFile(aChartDiagram.ZAxisTitle)
254 End If
255 Next i
256 End Sub
259 Sub GetFrameTexts()
260 Dim i as integer
261 Dim oTextFrame as object
262 Dim oFrameEnum as Object
263 Dim oFramePort as Object
264 Dim oFrameTextEnum as Object
265 Dim oFrameTextPort as Object
267 MakeLogHeadLine(&quot;Text Frames&quot;)
268 For i = 0 to oDocument.TextFrames.Count-1
269 oTextFrame = oDocument.TextFrames.GetbyIndex(i)
270 WriteStringToLogFile(oTextFrame.Name)
272 &apos; Is the frame bound to the page?
273 If oTextFrame.AnchorType = com.sun.star.text.TextContentAnchorType.AT_PAGE Then
274 GetParagraphTexts(oTextFrame, &quot;Text Frame Contents&quot;)
275 End If
277 oFrameEnum = oTextFrame.CreateEnumeration
278 While oFrameEnum.HasMoreElements
279 oFramePort = oFrameEnum.NextElement
280 If oFramePort.supportsService(&quot;com.sun.star.text.Paragraph&quot;) then
281 oFrameTextEnum = oFramePort.createEnumeration
282 While oFrameTextEnum.HasMoreElements
283 oFrameTextPort = oFrameTextEnum.NextElement
284 If oFrameTextPort.SupportsService(&quot;com.sun.star.text.TextFrame&quot;) Then
285 WriteStringtoLogFile(oFrameTextPort.String)
286 End If
287 Wend
288 Else
289 WriteStringtoLogFile(oFramePort.Name)
290 End if
291 Wend
292 Next
293 End Sub
296 Sub GetTextFieldStrings()
297 Dim aTextField as Object
298 Dim i as integer
299 Dim CurElement as Object
300 MakeLogHeadLine(&quot;Text Fields&quot;)
301 aTextfield = oDocument.getTextfields.CreateEnumeration
302 While aTextField.hasmoreElements
303 CurElement = aTextField.NextElement
304 If CurElement.PropertySetInfo.hasPropertybyName(&quot;Content&quot;) Then
305 WriteStringtoLogFile(CurElement.Content)
306 ElseIf CurElement.PropertySetInfo.hasPropertybyName(&quot;PlaceHolder&quot;) Then
307 WriteStringtoLogFile(CurElement.PlaceHolder)
308 WriteStringtoLogFile(CurElement.Hint)
309 ElseIf Curelement.TextFieldMaster.PropertySetInfo.HasPropertybyName(&quot;Content&quot;) then
310 WriteStringtoLogFile(CurElement.TextFieldMaster.Content)
311 End If
312 Wend
313 End Sub
316 Sub GetLinkedFileNames()
317 Dim oDocSections as Object
318 Dim LinkedFileName as String
319 Dim i as Integer
320 If Right(oDocument.URL,3) = &quot;sgl&quot; Then
321 MakeLogHeadLine(&quot;Sub-documents&quot;)
322 oDocSections = oDocument.TextSections
323 For i = 0 to oDocSections.Count - 1
324 LinkedFileName = oDocSections.GetbyIndex(i).FileLink.FileURL
325 If LinkedFileName &lt;&gt; &quot;&quot; Then
326 WriteStringToLogFile(LinkedFileName)
327 End If
328 Next i
329 End If
330 End Sub
333 Sub GetSectionNames()
334 Dim i as integer
335 Dim oDocSections as Object
336 MakeLogHeadLine(&quot;Sections&quot;)
337 oDocSections = oDocument.TextSections
338 For i = 0 to oDocSections.Count-1
339 WriteStringtoLogFile(oDocSections.GetbyIndex(i).Name)
340 Next
341 End Sub
344 Sub GetWriterStrings()
345 GetParagraphTexts(oDocument, &quot;Document Body&quot;)
346 GetGraphicNames()
347 GetStyles()
348 GetControlStrings(oDocument.DrawPage, &quot;Controls&quot;)
349 GetTextFieldStrings()
350 GetSectionNames()
351 GetFrameTexts()
352 GetHyperLinks
353 GetLinkedFileNames()
354 End Sub
357 &apos; ***********************************************Draw/Impress documents**************************************************
359 Sub GetDrawPageTitles(LocObject as Object)
360 Dim n as integer
361 Dim oPage as Object
363 For n = 0 to LocObject.Count - 1
364 oPage = LocObject.GetbyIndex(n)
365 WriteStringtoLogFile(oPage.Name)
366 &apos; Is the page a DrawPage and not a MasterPage?
367 If oPage.supportsService(&quot;com.sun.star.drawing.DrawPage&quot;)then
368 &apos; Get the name of the NotesPage (only relevant for Impress documents)
369 If oDocument.supportsService(&quot;com.sun.star.presentation.PresentationDocument&quot;) then
370 WriteStringtoLogFile(oPage.NotesPage.Name)
371 End If
372 End If
373 Next
374 End Sub
377 Sub GetPageStrings(oPages as Object)
378 Dim m, n, s as Integer
379 Dim oPage, oPageElement, oShape as Object
380 For n = 0 to oPages.Count-1
381 oPage = oPages.GetbyIndex(n)
382 If oPage.HasElements then
383 For m = 0 to oPage.Count-1
384 oPageElement = oPage.GetByIndex(m)
385 If HasUnoInterfaces(oPageElement,&quot;com.sun.star.container.XIndexAccess&quot;) Then
386 &apos; The Object &quot;oPageElement&quot; a group of Shapes, that can be accessed by their index
387 For s = 0 To oPageElement.Count - 1
388 WriteStringToLogFile(oPageElement.GetByIndex(s).String)
389 Next s
390 ElseIf HasUnoInterfaces(oPageElement, &quot;com.sun.star.text.XText&quot;) Then
391 WriteStringtoLogFile(oPageElement.String)
392 End If
393 Next
394 End If
395 Next
396 End Sub
399 Sub GetDrawStrings()
400 Dim oDPages, oMPages as Object
402 oDPages = oDocument.DrawPages
403 oMPages = oDocument.Masterpages
405 MakeLogHeadLine(&quot;Titles&quot;)
406 GetDrawPageTitles(oDPages)
407 GetDrawPageTitles(oMPages)
409 MakeLogHeadLine(&quot;Document Body&quot;)
410 GetPageStrings(oDPages)
411 GetPageStrings(oMPages)
412 End Sub
415 &apos; ***********************************************Misc**************************************************
417 Sub GetDocumentProps()
418 Dim oDocuProps as Object
419 MakeLogHeadLine(&quot;Document Properties&quot;)
420 oDocuProps = oDocument.DocumentProperties
421 WriteStringToLogFile(oDocuProps.Title)
422 WriteStringToLogFile(oDocuProps.Description)
423 WriteStringToLogFile(oDocuProps.Subject)
424 WriteStringToLogFile(oDocuProps.Author)
425 &apos; WriteStringToLogFile(oDocuProps.UserDefinedProperties.ReplyTo)
426 &apos; WriteStringToLogFile(oDocuProps.UserDefinedProperties.Recipient)
427 &apos; WriteStringToLogFile(oDocuProps.UserDefinedProperties.References)
428 &apos; WriteStringToLogFile(oDocuProps.Keywords)
429 End Sub
432 Sub GetHyperlinks()
433 Dim i as integer
434 Dim oCrsr as Object
435 Dim oAllHyperLinks as Object
436 Dim SrchAttributes(0) as new com.sun.star.beans.PropertyValue
437 Dim oSearchDesc as Object
439 MakeLogHeadLine(&quot;Hyperlinks&quot;)
440 &apos; create a Search-Descriptor
441 oSearchDesc = oDocument.CreateSearchDescriptor
442 oSearchDesc.Valuesearch = False
444 &apos; define the Search-attributes
445 srchattributes(0).Name = &quot;HyperLinkURL&quot;
446 srchattributes(0).Value = &quot;&quot;
447 oSearchDesc.SetSearchAttributes(SrchAttributes())
449 oAllHyperLinks = oDocument.findAll(oSearchDesc())
451 For i = 0 to oAllHyperLinks.Count - 1
452 oFound = oAllHyperLinks(i)
453 oCrsr = oFound.Text.createTextCursorByRange(oFound)
454 WriteStringToLogFile(oCrs.HyperLinkURL) &apos;Url
455 WriteStringToLogFile(oCrs.HyperLinkTarget) &apos;Name
456 WriteStringToLogFile(oCrs.HyperLinkName) &apos;Frame
457 Next i
458 End Sub
461 Sub GetGraphicNames()
462 Dim i as integer
463 Dim oDocGraphics as Object
464 MakeLogHeadLine(&quot;Graphics&quot;)
465 oDocGraphics = oDocument.GraphicObjects
466 For i = 0 to oDocGraphics.count - 1
467 WriteStringtoLogFile(oDocGraphics.GetbyIndex(i).Name)
468 Next
469 End Sub
472 Sub GetStyles()
473 Dim m,n as integer
474 MakeLogHeadLine(&quot;User-defined Templates&quot;)
476 &apos; Check all StyleFamilies(i.e. PageStyles, ParagraphStyles, CharacterStyles, cellStyles)
477 For n = 0 to oDocument.StyleFamilies.Count - 1
478 For m = 0 to oDocument.StyleFamilies.getbyIndex(n).Count-1
479 If oDocument.StyleFamilies.GetbyIndex(n).getbyIndex(m).IsUserDefined then
480 WriteStringtoLogFile(oDocument.StyleFamilies.GetbyIndex(n).getbyIndex(m).Name)
481 End If
482 Next
483 Next
484 End Sub
487 Sub GetControlStrings(oDPage as Object, HeaderLine as String)
488 Dim aForm as Object
489 Dim m,n as integer
490 MakeLogHeadLine(HeaderLine)
491 &apos;SearchFor all possible Controls
492 For n = 0 to oDPage.Forms.Count - 1
493 aForm = oDPage.Forms(n)
494 For m = 0 to aForm.Count-1
495 GetControlContent(aForm.GetbyIndex(m))
496 Next
497 Next
498 End Sub
501 Sub GetControlContent(LocControl as Object)
502 Dim i as integer
504 If LocControl.PropertySetInfo.HasPropertybyName(&quot;Label&quot;) then
505 WriteStringtoLogFile(LocControl.Label)
507 ElseIf LocControl.SupportsService(&quot;com.sun.star.form.component.ListBox&quot;) then
508 For i = 0 to Ubound(LocControl.StringItemList())
509 WriteStringtoLogFile(LocControl.StringItemList(i))
510 Next
511 End If
512 If LocControl.PropertySetInfo.HasPropertybyName(&quot;HelpText&quot;) then
513 WriteStringtoLogFile(LocControl.Helptext)
514 End If
515 End Sub
517 &apos; ***********************************************Log document**************************************************
519 Sub WriteStringtoLogFile( sString as String)
520 If (Not FieldInArray(LogArray(),LogIndex,sString))AND (NOT ISNULL(sString)) Then
521 LogArray(LogIndex) = sString
522 LogIndex = LogIndex + 1
523 oLogText.insertString(oLogCursor,sString,False)
524 oLogText.insertControlCharacter(oLogCursor,com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK,False)
525 End If
526 End Sub
529 Sub MakeLogHeadLine(HeadText as String)
530 oLogCursor.CharStyleName = &quot;Log Header&quot;
531 oLogText.insertControlCharacter(oLogCursor,com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK,False)
532 oLogText.insertString(oLogCursor,HeadText,False)
533 oLogText.insertControlCharacter(oLogCursor,com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK,False)
534 oLogCursor.CharStyleName = &quot;Log Body&quot;
535 End Sub
536 </script:module>