xref: /AOO41X/main/helpauthoring/HelpAuthoring/Helpers.xba (revision 8809db7a87f97847b57a57f4cd2b0104b2b83182)
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="Helpers" script:language="StarBasic">&apos; *** MODULE HELPERS ***
4
5&apos;=======================================================
6&apos; Main
7&apos;-------------------------------------------------------
8&apos; Ensure that necessary library functions are available
9&apos;=======================================================
10Sub Main
11    GlobalScope.BasicLibraries.loadLibrary(&quot;Tools&quot;)
12End Sub
13
14&apos;=======================================================
15&apos; ShowProp
16&apos;-------------------------------------------------------
17&apos; Displays a dialog that shows the properties and
18&apos; the methods of an object. Used for debugging.
19&apos;=======================================================
20Sub ShowProp(Elem As Object)
21    dim oDialog As Object
22
23    BasicLibraries.LoadLibrary(&quot;HelpAuthoring&quot;)
24    oDialog = LoadDialog(&quot;HelpAuthoring&quot;, &quot;dlgObjProp&quot;)
25    oDialogModel = oDialog.Model
26
27    oTxtProp = oDialog.GetControl(&quot;txtProp&quot;)
28    oTxtProp.Text = Join(Split(Elem.dbg_properties,&quot;;&quot;),chr(13))
29
30    oTxtMeth = oDialog.GetControl(&quot;txtMeth&quot;)
31    oTxtMeth.Text = Join(Split(Elem.dbg_methods,&quot;;&quot;),chr(13))
32
33    oTxtInt = oDialog.GetControl(&quot;txtInt&quot;)
34    oTxtInt.Text = Join(Split(Elem.dbg_supportedInterfaces,&quot;;&quot;),chr(13))
35
36    oDialog.Execute()
37    oDialog.dispose
38End Sub
39
40&apos;=======================================================
41&apos; AlphaNum
42&apos;-------------------------------------------------------
43&apos; Removes all invalid characters from a string
44&apos;=======================================================
45Function AlphaNum(Strg As String)
46    dim OutStrg As String
47    dim sValid As String
48
49    sValid = &quot;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_0123456789&quot;
50
51    For i=1 to Len(Strg)
52        If (Instr(sValid,LCase(Mid(Strg,i,1)))) Then
53            OutStrg = OutStrg + Mid(Strg,i,1)
54        End If
55    Next i
56    AlphaNum = OutStrg
57End Function
58
59&apos;=======================================================
60&apos; Replace
61&apos;-------------------------------------------------------
62&apos; Replaces a character with another character in a string
63&apos;=======================================================
64Function Replace(txt As String, ReplaceFrom As String, ReplaceTo As String)
65    dim OutStr As String
66    For i=1 to len(txt)
67        If LCase(mid(txt,i,1))=ReplaceFrom Then
68            OutStr = OutStr + ReplaceTo
69        Else
70            OutStr = OutStr + mid(txt,i,1)
71        End If
72    Next i
73    Replace = OutStr
74End Function
75
76
77&apos;=======================================================
78&apos; ReplaceAll
79&apos;-------------------------------------------------------
80&apos; Replaces a character with another character in a string
81&apos;=======================================================
82Function ReplaceAll(txt As String, ReplaceFrom As String, ReplaceTo As String)
83    dim OutStr As String
84    For i=1 to len(txt)
85        bFound = 0
86        For j=1 to len(ReplaceFrom)
87            If LCase(mid(txt,i,1))=LCase(mid(ReplaceFrom,j,1)) Then
88                bFound = 1
89                OutStr = OutStr + ReplaceTo
90                j = len(ReplaceFrom)
91            End If
92        Next j
93        If bFound=0 Then
94            OutStr = OutStr + mid(txt,i,1)
95        End If
96    Next i
97    ReplaceAll = OutStr
98End Function
99
100
101
102&apos;=======================================================
103&apos; CreateID
104&apos;-------------------------------------------------------
105&apos; Creates a numerical randomized ID
106&apos;=======================================================
107Function CreateID
108    sDate = ReplaceAll(Date,&quot;/:. \&quot;,&quot;&quot;)
109    sTime = ReplaceAll(Time,&quot;/:. \AMP&quot;,&quot;&quot;)
110    Randomize
111    CreateID = sDate + sTime + Int(Rnd * 100)
112End Function
113
114&apos;=======================================================
115&apos; InsertTag
116&apos;-------------------------------------------------------
117&apos; Inserts an inline tag (element) in the document at the
118&apos; current cursor position. It also sets the character
119&apos; format to hlp_aux_tag
120&apos;=======================================================
121Sub InsertTag (Element As String, Content As String)
122    dim document   as object
123    dim dispatcher as object
124
125    document   = ThisComponent.CurrentController.Frame
126    dispatcher = createUnoService(&quot;com.sun.star.frame.DispatchHelper&quot;)
127
128    dim args(5) as new com.sun.star.beans.PropertyValue
129    args(0).Name = &quot;Type&quot;
130    args(0).Value = 8
131    args(1).Name = &quot;SubType&quot;
132    args(1).Value = 1
133    args(2).Name = &quot;Name&quot;
134    args(2).Value = Element
135    args(3).Name = &quot;Content&quot;
136    args(3).Value = Content
137    args(4).Name = &quot;Format&quot;
138    args(4).Value = -1
139    args(5).Name = &quot;Separator&quot;
140    args(5).Value = &quot; &quot;
141    SetCharStyle(&quot;hlp_aux_tag&quot;)
142    dispatcher.executeDispatch(document, &quot;.uno:InsertField&quot;, &quot;&quot;, 0, args())
143    SetCharStyle(&quot;Default&quot;)
144End Sub
145
146&apos;=======================================================
147&apos; INSERTTAGCR
148&apos;-------------------------------------------------------
149&apos; Inserts a tag (element) in the document at the
150&apos; current cursor position in its own newly created paragraph.
151&apos; It also sets the character format to hlp_aux_tag and
152&apos; the paragraph to the specified value (should start with hlp_)
153&apos;=======================================================
154Sub InsertTagCR (Element As String, Content As String, Style As String)
155    dim document   as object
156    dim dispatcher as object
157
158    document   = ThisComponent.CurrentController.Frame
159    dispatcher = createUnoService(&quot;com.sun.star.frame.DispatchHelper&quot;)
160
161    dim args(5) as new com.sun.star.beans.PropertyValue
162    args(0).Name = &quot;Type&quot;
163    args(0).Value = 8
164    args(1).Name = &quot;SubType&quot;
165    args(1).Value = 1
166    args(2).Name = &quot;Name&quot;
167    args(2).Value = Element
168    args(3).Name = &quot;Content&quot;
169    args(3).Value = Content
170    args(4).Name = &quot;Format&quot;
171    args(4).Value = -1
172    args(5).Name = &quot;Separator&quot;
173    args(5).Value = &quot; &quot;
174
175    CR
176    goUp(1)
177    SetParaStyle(Style)
178    SetCharStyle(&quot;hlp_aux_tag&quot;)
179    dispatcher.executeDispatch(document, &quot;.uno:InsertField&quot;, &quot;&quot;, 0, args())
180    SetCharStyle(&quot;Default&quot;)
181    goDown(1)
182End Sub
183
184&apos;=======================================================
185&apos; InsertField
186&apos;-------------------------------------------------------
187&apos; Inserts a field in the document at the
188&apos; current cursor position.
189&apos;=======================================================
190Sub InsertField(Field as String, Content as String)
191    dim document   as object
192    dim dispatcher as object
193
194    document   = ThisComponent.CurrentController.Frame
195    dispatcher = createUnoService(&quot;com.sun.star.frame.DispatchHelper&quot;)
196
197    dim args(5) as new com.sun.star.beans.PropertyValue
198    args(0).Name = &quot;Type&quot;
199    args(0).Value = 8
200    args(1).Name = &quot;SubType&quot;
201    args(1).Value = 1
202    args(2).Name = &quot;Name&quot;
203    args(2).Value = Field
204    args(3).Name = &quot;Content&quot;
205    args(3).Value = Content
206    args(4).Name = &quot;Format&quot;
207    args(4).Value = -1
208    args(5).Name = &quot;Separator&quot;
209    args(5).Value = &quot; &quot;
210
211    dispatcher.executeDispatch(document, &quot;.uno:InsertField&quot;, &quot;&quot;, 0, args())
212End Sub
213
214&apos;=======================================================
215&apos; GoUp
216&apos;-------------------------------------------------------
217&apos; Simulates the CursorUp key
218&apos;=======================================================
219Sub goUp(Count As Integer, Optional bSelect As Boolean)
220    dim document   as object
221    dim dispatcher as object
222
223    document   = ThisComponent.CurrentController.Frame
224    dispatcher = createUnoService(&quot;com.sun.star.frame.DispatchHelper&quot;)
225
226    dim args(1) as new com.sun.star.beans.PropertyValue
227    args(0).Name = &quot;Count&quot;
228    args(0).Value = Count
229    args(1).Name = &quot;Select&quot;
230    If IsMissing(bSelect) Then
231        args(1).Value = false
232    Else
233        args(1).Value = bSelect
234    End If
235
236    dispatcher.executeDispatch(document, &quot;.uno:GoUp&quot;, &quot;&quot;, 0, args())
237End Sub
238
239&apos;=======================================================
240&apos; GoDown
241&apos;-------------------------------------------------------
242&apos; Simulates the CursorDown key
243&apos;=======================================================
244Sub goDown(Count As Integer, Optional bSelect As Boolean)
245    dim document   as object
246    dim dispatcher as object
247
248    document   = ThisComponent.CurrentController.Frame
249    dispatcher = createUnoService(&quot;com.sun.star.frame.DispatchHelper&quot;)
250
251    dim args(1) as new com.sun.star.beans.PropertyValue
252    args(0).Name = &quot;Count&quot;
253    args(0).Value = Count
254    args(1).Name = &quot;Select&quot;
255    If IsMissing(bSelect) Then
256        args(1).Value = false
257    Else
258        args(1).Value = bSelect
259    End If
260
261    dispatcher.executeDispatch(document, &quot;.uno:GoDown&quot;, &quot;&quot;, 0, args())
262End Sub
263
264
265&apos;=======================================================
266&apos; GoRight
267&apos;-------------------------------------------------------
268&apos; Simulates the CursorRight key
269&apos;=======================================================
270Sub goRight(Count As Integer, Optional bSelect As Boolean)
271    dim document   as object
272    dim dispatcher as object
273
274    document   = ThisComponent.CurrentController.Frame
275    dispatcher = createUnoService(&quot;com.sun.star.frame.DispatchHelper&quot;)
276
277    dim args(1) as new com.sun.star.beans.PropertyValue
278    args(0).Name = &quot;Count&quot;
279    args(0).Value = Count
280    args(1).Name = &quot;Select&quot;
281    If IsMissing(bSelect) Then
282        args(1).Value = false
283    Else
284        args(1).Value = bSelect
285    End If
286
287    dispatcher.executeDispatch(document, &quot;.uno:GoRight&quot;, &quot;&quot;, 0, args())
288End Sub
289
290&apos;=======================================================
291&apos; GoLeft
292&apos;-------------------------------------------------------
293&apos; Simulates the CursorLeft key
294&apos;=======================================================
295Sub goLeft(Count As Integer, optional bSelect As boolean)
296    dim document   as object
297    dim dispatcher as object
298
299    document   = ThisComponent.CurrentController.Frame
300    dispatcher = createUnoService(&quot;com.sun.star.frame.DispatchHelper&quot;)
301
302    dim args(1) as new com.sun.star.beans.PropertyValue
303    args(0).Name = &quot;Count&quot;
304    args(0).Value = Count
305    args(1).Name = &quot;Select&quot;
306    If IsMissing(bSelect) Then
307        args(1).Value = false
308    Else
309        args(1).Value = bSelect
310    End If
311
312    dispatcher.executeDispatch(document, &quot;.uno:GoLeft&quot;, &quot;&quot;, 0, args())
313End Sub
314
315&apos;=======================================================
316&apos; CR
317&apos;-------------------------------------------------------
318&apos; Inserts a Carriage Return (a new paragraph)
319&apos;=======================================================
320Sub CR
321    dim document   as object
322    dim dispatcher as object
323
324    document   = ThisComponent.CurrentController.Frame
325    dispatcher = createUnoService(&quot;com.sun.star.frame.DispatchHelper&quot;)
326
327    oSel = thiscomponent.getcurrentcontroller.getselection
328    oCur = oSel(0).getText.createTextCursorByRange(oSel(0))
329    oCur.gotoEndOfParagraph(0)
330    thiscomponent.getcurrentcontroller.select(oCur)
331
332    dispatcher.executeDispatch(document, &quot;.uno:InsertPara&quot;, &quot;&quot;, 0, Array())
333End Sub
334
335&apos;=======================================================
336&apos; CR_before
337&apos;-------------------------------------------------------
338&apos; Inserts a Carriage Return (a new paragraph) before the current para
339&apos;=======================================================
340Sub CR_before
341    dim document   as object
342    dim dispatcher as object
343
344    document   = ThisComponent.CurrentController.Frame
345    dispatcher = createUnoService(&quot;com.sun.star.frame.DispatchHelper&quot;)
346
347    oSel = thiscomponent.getcurrentcontroller.getselection
348    oCur = oSel(0).getText.createTextCursorByRange(oSel(0))
349    oCur.gotoStartOfParagraph(0)
350    thiscomponent.getcurrentcontroller.select(oCur)
351
352    dispatcher.executeDispatch(document, &quot;.uno:InsertPara&quot;, &quot;&quot;, 0, Array())
353End Sub
354
355&apos;=======================================================
356&apos; LF
357&apos;-------------------------------------------------------
358&apos; Inserts a line feed (manual line break)
359&apos;=======================================================
360sub LF
361    dim document   as object
362    dim dispatcher as object
363    document   = ThisComponent.CurrentController.Frame
364    dispatcher = createUnoService(&quot;com.sun.star.frame.DispatchHelper&quot;)
365
366    dispatcher.executeDispatch(document, &quot;.uno:InsertLinebreak&quot;, &quot;&quot;, 0, Array())
367end sub
368
369&apos;=======================================================
370&apos; SetParaStyle
371&apos;-------------------------------------------------------
372&apos; Sets the para style to the given value
373&apos;=======================================================
374Sub SetParaStyle(StyleName As String)
375    dim document   as object
376    dim dispatcher as object
377
378    document   = ThisComponent.CurrentController.Frame
379    dispatcher = createUnoService(&quot;com.sun.star.frame.DispatchHelper&quot;)
380
381    dim args(1) as new com.sun.star.beans.PropertyValue
382    args(0).Name = &quot;Template&quot;
383    args(0).Value = StyleName
384    args(1).Name = &quot;Family&quot;
385    args(1).Value = 2
386
387    dispatcher.executeDispatch(document, &quot;.uno:StyleApply&quot;, &quot;&quot;, 0, args())
388end Sub
389
390&apos;=======================================================
391&apos; SetCharStyle
392&apos;-------------------------------------------------------
393&apos; Sets the character style to the given value
394&apos;=======================================================
395Sub SetCharStyle(StyleName As String)
396    dim document   as object
397    dim dispatcher as object
398
399    document   = ThisComponent.CurrentController.Frame
400    dispatcher = createUnoService(&quot;com.sun.star.frame.DispatchHelper&quot;)
401
402    dim args(1) as new com.sun.star.beans.PropertyValue
403    args(0).Name = &quot;Template&quot;
404    args(0).Value = StyleName
405    args(1).Name = &quot;Family&quot;
406    args(1).Value = 1
407
408    dispatcher.executeDispatch(document, &quot;.uno:StyleApply&quot;, &quot;&quot;, 0, args())
409end Sub
410
411&apos;=======================================================
412&apos; InsertNewParaData
413&apos;-------------------------------------------------------
414&apos; Inserts a new ID for the paragraph
415&apos;=======================================================
416Sub InsertNewParaData
417
418        If not IsHelpFile Then
419        msgbox(strErr_NoHelpFile)
420        Exit Sub
421    End If
422
423    oSel = thiscomponent.getcurrentcontroller.getselection
424    oCur = oSel(0).getText.createTextCursorByRange(oSel(0))
425
426    arParaData = GetParaData
427    sID = arParaData(0)
428    slocalize = arParaData(1)
429    sMsg = arParaData(2)
430
431    If sMsg &lt;&gt; &quot;&quot; Then
432        msgbox &quot;Cannot assign paragraph id:&quot;+chr(13)+sMsg,48,&quot;Error&quot;
433        Exit Sub
434    End If
435
436    If sID &lt;&gt; &quot;&quot; Then
437        msgbox &quot;Paragraph already has an ID.&quot;+chr(13)+&quot;If you want to assign a new ID delete the existing one first.&quot;,48,&quot;Error&quot;
438        Exit Sub
439    End If
440
441    oCur.gotoStartOfParagraph(0)
442
443    If (Left(oCur.ParaStyleName,8) = &quot;hlp_head&quot;) Then
444        id = &quot;hd_id&quot; + CreateID
445        thiscomponent.getcurrentcontroller.select(oCur)
446        MetaData = id
447        SetCharStyle(&quot;hlp_aux_parachanged&quot;)
448        InsertField(&quot;ID&quot;,MetaData)
449        SetCharStyle(&quot;Default&quot;)
450    Else
451        id = &quot;par_id&quot; + CreateID
452        thiscomponent.getcurrentcontroller.select(oCur)
453        MetaData = id
454        SetCharStyle(&quot;hlp_aux_parachanged&quot;)
455        InsertField(&quot;ID&quot;,MetaData)
456        SetCharStyle(&quot;Default&quot;)
457    End If
458
459
460End Sub
461
462&apos;=======================================================
463&apos; LoadDialog
464&apos;-------------------------------------------------------
465&apos; Loads a BASIC dialog
466&apos;=======================================================
467Function LoadDialog(Libname as String, DialogName as String, Optional oLibContainer)
468    Dim oLib as Object
469    Dim oLibDialog as Object
470    Dim oRuntimeDialog as Object
471
472    If IsMissing(oLibContainer ) then
473        oLibContainer = DialogLibraries
474    End If
475
476    oLibContainer.LoadLibrary(LibName)
477    oLib = oLibContainer.GetByName(Libname)
478    oLibDialog = oLib.GetByName(DialogName)
479    oRuntimeDialog = CreateUnoDialog(oLibDialog)
480    LoadDialog() = oRuntimeDialog
481End Function
482
483&apos;=======================================================
484&apos; Surprise
485&apos;-------------------------------------------------------
486&apos; D&apos;oh
487&apos;=======================================================
488Sub Surprise
489    msgbox &quot;This function is unsupported.&quot;+chr(13)+&quot;If you know how to implement this -- go ahead!&quot;,0,&quot;D&apos;oh!&quot;
490End Sub
491
492&apos;=======================================================
493&apos; InsertNote
494&apos;-------------------------------------------------------
495&apos; Inserts a note (annotation) at the current position
496&apos;=======================================================
497sub InsertNote(Content As String)
498    dim document   as object
499    dim dispatcher as object
500
501    document   = ThisComponent.CurrentController.Frame
502    dispatcher = createUnoService(&quot;com.sun.star.frame.DispatchHelper&quot;)
503
504    dim args(2) as new com.sun.star.beans.PropertyValue
505    args(0).Name = &quot;Text&quot;
506    args(0).Value = Content
507    args(1).Name = &quot;Author&quot;
508    args(1).Value = &quot;Help Tooling - DO NOT EDIT&quot;
509    args(2).Name = &quot;Date&quot;
510    args(2).Value = &quot;02/27/2004&quot;
511
512    dispatcher.executeDispatch(document, &quot;.uno:InsertAnnotation&quot;, &quot;&quot;, 0, args())
513end sub
514
515&apos;=======================================================
516&apos; InsertText
517&apos;-------------------------------------------------------
518&apos; Inserts a string at the current position
519&apos;=======================================================
520Sub InsertText(strg As String)
521    oSel = thiscomponent.getcurrentcontroller.getselection
522    oCur = oSel(0).getText.createTextCursorByRange(oSel(0))
523    oCur.String = strg
524End Sub
525
526&apos;=======================================================
527&apos; ParaIsEmpty
528&apos;-------------------------------------------------------
529&apos; Evaluates if a paragraph is empty.
530&apos;=======================================================
531Function ParaIsEmpty
532    oSel = thiscomponent.getcurrentcontroller.getselection
533    oCur = oSel(0).getText.createTextCursorByRange(oSel(0))
534    oCur.gotoStartOfParagraph(0)
535    ParaIsEmpty = oCur.IsEndOfParagraph
536End Function
537
538&apos;=======================================================
539&apos; IsInBookmark
540&apos;-------------------------------------------------------
541&apos; Evaluates if the cursor is inside a &lt;bookmark&gt; &lt;/bookmark&gt; element
542&apos;=======================================================
543Function IsInBookmark
544    oSel = thiscomponent.getcurrentcontroller.getselection
545    oCur = oSel(0).getText.createTextCursorByRange(oSel(0))
546
547    If ((oCur.ParaStyleName = &quot;hlp_aux_bookmark&quot;) AND (not(oCur.IsEndOfParagraph))) Then
548        oCur.GotoStartOfParagraph(0)
549        oCur.GotoEndOfParagraph(1)
550        sText = Left(oCur.GetString,Instr(oCur.GetString,&quot;&quot;&quot; id=&quot;&quot;&quot;)-1)
551        sText = Right(sText,Len(sText)-InStr(sText,&quot;&quot;&quot;&quot;))
552        Select Case Left(sText,3)
553            Case &quot;ind&quot;
554                IsInBookmark = 1
555            Case &quot;hid&quot;
556                IsInBookmark = 2
557            Case &quot;con&quot;
558                IsInBookmark = 3
559            Case Else
560                IsInBookmark = 0
561        End Select
562    Else
563        IsInBookmark = 0
564    End If
565End Function
566
567&apos;=======================================================
568&apos; IsInTable
569&apos;-------------------------------------------------------
570&apos; Evaluates if the cursor is in a table
571&apos;=======================================================
572Function IsInTable
573    oSel = thiscomponent.getcurrentcontroller.getselection
574    oCur = oSel(0).getText.createTextCursorByRange(oSel(0))
575
576    IsInTable = (VarType(oCur.TextTable) &lt;&gt; 0)
577End Function
578
579&apos;=======================================================
580&apos; InsertLink
581&apos;-------------------------------------------------------
582&apos; Inserts a hyperlink at the current position
583&apos;=======================================================
584Sub InsertLink(sPath As String, sText As String, sName As String)
585    dim document   as object
586    dim dispatcher as object
587
588    document   = ThisComponent.CurrentController.Frame
589    dispatcher = createUnoService(&quot;com.sun.star.frame.DispatchHelper&quot;)
590
591    dim args(4) as new com.sun.star.beans.PropertyValue
592    args(0).Name = &quot;Hyperlink.Text&quot;
593    args(0).Value = sText
594    args(1).Name = &quot;Hyperlink.URL&quot;
595    args(1).Value = sPath
596    args(2).Name = &quot;Hyperlink.Target&quot;
597    args(2).Value = &quot;&quot;
598    args(3).Name = &quot;Hyperlink.Name&quot;
599    args(3).Value = sName
600    args(4).Name = &quot;Hyperlink.Type&quot;
601    args(4).Value = 1
602
603    dispatcher.executeDispatch(document, &quot;.uno:SetHyperlink&quot;, &quot;&quot;, 0, args())
604    args(0).Name = &quot;Count&quot;
605    args(0).Value = 1
606    args(1).Name = &quot;Select&quot;
607    args(1).Value = false
608
609    dispatcher.executeDispatch(document, &quot;.uno:GoRight&quot;, &quot;&quot;, 0, args())
610
611End Sub
612
613&apos;=======================================================
614&apos; AssignMissingIDs
615&apos;-------------------------------------------------------
616&apos; Assigns IDs to elements that miss them
617&apos;=======================================================
618Sub AssignMissingIDs
619&apos; NOT IMPLEMENTED YET
620end sub
621
622&apos;=======================================================
623&apos; CreateTable
624&apos;-------------------------------------------------------
625&apos; Creates a new table
626&apos;=======================================================
627Sub CreateTable(nRows as Integer, nCols as Integer, sID as String)
628    dim document   as object
629    dim dispatcher as object
630
631    document   = ThisComponent.CurrentController.Frame
632    dispatcher = createUnoService(&quot;com.sun.star.frame.DispatchHelper&quot;)
633
634    dim args1(3) as new com.sun.star.beans.PropertyValue
635    args1(0).Name = &quot;TableName&quot;
636    args1(0).Value = sID
637    args1(1).Name = &quot;Columns&quot;
638    args1(1).Value = nCols
639    args1(2).Name = &quot;Rows&quot;
640    args1(2).Value = nRows
641    args1(3).Name = &quot;Flags&quot;
642    args1(3).Value = 9
643
644    dispatcher.executeDispatch(document, &quot;.uno:InsertTable&quot;, &quot;&quot;, 0, args1())
645
646    args1(0).Name = &quot;TopBottomMargin.TopMargin&quot;
647    args1(0).Value = 500
648    args1(1).Name = &quot;TopBottomMargin.BottomMargin&quot;
649    args1(1).Value = 0
650    args1(2).Name = &quot;TopBottomMargin.TopRelMargin&quot;
651    args1(2).Value = 100
652    args1(3).Name = &quot;TopBottomMargin.BottomRelMargin&quot;
653    args1(3).Value = 100
654
655    dispatcher.executeDispatch(document, &quot;.uno:TopBottomMargin&quot;, &quot;&quot;, 0, args1())
656    dispatcher.executeDispatch(document, &quot;.uno:SelectAll&quot;, &quot;&quot;, 0, Array())
657    SetParaStyle(&quot;hlp_tablecontent&quot;)
658    GoDown(1)
659end Sub
660
661&apos;=======================================================
662&apos; IsBlockImage
663&apos;-------------------------------------------------------
664&apos; Evaluates if the cursor is in a paragraph with
665&apos; a block image (image in its own paragraph)
666&apos;=======================================================
667Function IsBlockImage
668    oSel = thiscomponent.getcurrentcontroller.getselection
669    oCur = oSel(0).getText.createTextCursorByRange(oSel(0))
670    oCur.gotoStartOfParagraph(0)
671    oCur.gotoEndOfParagraph(1)
672    sStr = Right(oCur.String,Len(oCur.String)-InStr(oCur.String,&quot; &quot;))  &apos;string must start with &lt;IMG and end with IMG with no &lt;IMG in between
673    IsBlockImage = (not(Left(sStr,4)=&quot;IMG&gt;&quot;) AND (Right(sStr,6)=&quot;&lt;/IMG&gt;&quot;))
674End Function
675
676&apos;=======================================================
677&apos; HasCaption
678&apos;-------------------------------------------------------
679&apos; Evaluates if the current image has a caption element
680&apos;=======================================================
681Function HasCaption
682    oSel = thiscomponent.getcurrentcontroller.getselection
683    If oSel.ImplementationName = &quot;SwXTextGraphicObject&quot; Then
684        oCur = oSel(0).getAnchor.getText.createTextCursorByRange(oSel(0).getAnchor)
685    Else
686        oCur = oSel(0).getText.createTextCursorByRange(oSel(0))
687    End If
688    oCur.gotoStartOfParagraph(0)
689    oCur.gotoEndOfParagraph(1)
690    HasCaption = (InStr(oCur.String,&quot;&lt;IMGCAPTION&quot;)&gt;0)
691End Function
692
693&apos;=======================================================
694&apos; GetImageID
695&apos;-------------------------------------------------------
696&apos; Returns the ID of an image at the cursor position
697&apos;=======================================================
698Function GetImageID
699    oSel = thiscomponent.getcurrentcontroller.getselection
700    If oSel.ImplementationName = &quot;SwXTextGraphicObject&quot; Then
701        oCur = oSel(0).getAnchor.getText.createTextCursorByRange(oSel(0).getAnchor)
702    Else
703        oCur = oSel(0).getText.createTextCursorByRange(oSel(0))
704    End If
705    oCur.gotoStartOfParagraph(0)
706    oCur.gotoEndOfParagraph(1)
707    sStr = Right(oCur.String,Len(oCur.String)-(InStr(oCur.String,&quot;IMG ID=&quot;&quot;&quot;)+7))
708    GetImageID = Left(sStr,InStr(sStr,&quot;&quot;&quot;&gt;&quot;)+1)  &apos;string must start with &lt;IMG and end with IMG with no &lt;IMG in between
709End Function
710
711&apos;=======================================================
712&apos; SelAll
713&apos;-------------------------------------------------------
714&apos; Selects everything
715&apos;=======================================================
716Sub SelAll
717    dim document   as object
718    dim dispatcher as object
719
720    document   = ThisComponent.CurrentController.Frame
721    dispatcher = createUnoService(&quot;com.sun.star.frame.DispatchHelper&quot;)
722
723    dispatcher.executeDispatch(document, &quot;.uno:SelectAll&quot;, &quot;&quot;, 0, Array())
724End Sub
725
726&apos;=======================================================
727&apos; GetParaData
728&apos;-------------------------------------------------------
729&apos; Returns the Paragraph ID and localization status
730&apos;=======================================================
731Function GetParaData
732    arParaData = Array(&quot;&quot;,&quot;&quot;,&quot;&quot;) &apos; ID, localize, #message
733
734    oSel = thiscomponent.getcurrentcontroller.getselection
735    oCur = oSel(0).getText.createTextCursorByRange(oSel(0))
736    oCur.gotoStartOfParagraph(0)
737    oCur.gotoEndOfParagraph(1)
738    sID = &quot;&quot;
739    Enum = oCur.createEnumeration
740    Fd = FALSE
741
742
743    TE = Enum.nextElement
744
745    TP = TE.createEnumeration
746    Ct = 0
747    posID = 0
748
749    Do While TP.hasmoreElements
750        Ct = Ct+1
751        TPE = TP.nextElement
752        If TPE.TextPortionType=&quot;TextField&quot; Then
753            If TPE.TextField.TextFieldMaster.Name=&quot;ID&quot; Then
754                sID = TPE.TextField.Content
755                Fd = TRUE
756                Exit Do
757            End If
758        End If
759        If TPE.String = &quot;&quot; Then
760            Ct = Ct-1
761        End If
762    Loop
763
764    If ((Left(oCur.ParaStyleName,8) = &quot;hlp_aux_&quot;) or (Left(oCur.ParaStyleName,4) &lt;&gt; &quot;hlp_&quot;)) Then
765        arParaData(2)=&quot;Invalid Paragraph Style&quot;
766        GetParaData = arParaData
767        Exit Function
768    End If
769
770    If sID = &quot;&quot; Then
771        GetParaData = arParaData
772        Exit Function
773    End If
774
775    If Right(sID,7) = &quot;_NOL10N&quot; Then
776        arParaData(0) = Left(sID,Len(sID)-7)
777        arParaData(1) = &quot;no&quot;
778    Else
779        arParaData(0) = sID
780        arParaData(1) = &quot;yes&quot;
781    End If
782
783    GetParaData = arParaData
784End Function
785
786&apos;=======================================================
787&apos; SetsParaData
788&apos;-------------------------------------------------------
789&apos; Sets the Paragraph ID and localization status
790&apos;=======================================================
791
792Sub SetParaData(sID as String, sLocalize as String)
793
794    oSel = thiscomponent.getcurrentcontroller.getselection
795    oCur = oSel(0).getText.createTextCursorByRange(oSel(0))
796    oCur.gotoStartOfParagraph(0)
797    oCur.gotoEndOfParagraph(1)
798    Enum = oCur.createEnumeration
799    Fd = FALSE
800
801
802    Do While Enum.hasMoreElements
803        TE = Enum.nextElement
804
805        TP = TE.createEnumeration
806        Ct = 0
807        posID = 0
808
809        Do While TP.hasmoreElements
810            Ct = Ct+1
811            TPE = TP.nextElement
812            If TPE.TextPortionType=&quot;TextField&quot; Then
813                If TPE.TextField.TextFieldMaster.Name=&quot;ID&quot; Then
814                    posID = Ct
815                    If sLocalize = &quot;no&quot; Then
816                        TPE.TextField.Content = sID+&quot;_NOL10N&quot;
817                        TPE.TextField.IsVisible = TRUE
818                    ElseIf sLocalize = &quot;yes&quot; Then
819                        TPE.TextField.Content = sID
820                        TPE.TextField.IsVisible = TRUE
821                    Else
822                        msgbox &quot;Unknown localization parameter: &quot;+sLocalize,0,&quot;Error&quot;
823                    End If
824                    Fd = TRUE
825                    Exit Do
826                End If
827            End If
828            If TPE.String = &quot;&quot; Then
829                Ct = Ct-1
830            End If
831        Loop
832        If Fd Then
833            Exit Do
834        End If
835    Loop
836
837    oCur.TextField.update
838    UpdateFields
839
840End Sub
841
842
843&apos;=======================================================
844&apos; IsInList
845&apos;-------------------------------------------------------
846&apos; Evaluates if the cursor is inside a list (ordered or unordered)
847&apos;=======================================================
848Function IsInList
849    oSel = thiscomponent.getcurrentcontroller.getselection
850    oCur = oSel(0).getText.createTextCursorByRange(oSel(0))
851    If oCur.NumberingStyleName = &quot;&quot;  Then
852        IsInList = false
853    ElseIf oCur.NumberingRules.NumberingIsOutline = true Then
854        IsInList = false
855    Else
856        IsInList = true
857    End If
858End Function
859
860&apos;=======================================================
861&apos; TagFormatIsCorrect
862&apos;-------------------------------------------------------
863&apos; Checks for correct paragraph format for tags
864&apos;=======================================================
865Function TagFormatIsCorrect(sTN As String, sPS As String)
866
867    arTag = Array(&quot;BOOKMARK&quot;,&quot;SORT&quot;,&quot;SECTION&quot;,&quot;SWITCH&quot;,&quot;CASE&quot;,&quot;DEFAULT&quot;)
868    arTagFormat = Array(&quot;hlp_aux_bookmark&quot;,&quot;hlp_aux_sort&quot;,&quot;hlp_aux_section&quot;,&quot;hlp_aux_switch&quot;,&quot;hlp_aux_switch&quot;,&quot;hlp_aux_switch&quot;)
869
870    For n=0 to ubound(arTag)
871        If (sTN = arTag(n) AND sPS &lt;&gt; arTagFormat(n)) Then
872            TagFormatIsCorrect = arTagFormat(n)
873            Exit Function
874        End If
875        TagFormatIsCorrect = &quot;&quot;
876    Next n
877
878End Function
879
880&apos;=======================================================
881&apos; GetFilePath
882&apos;-------------------------------------------------------
883&apos; look for the &quot;text/...&quot; part of the file name and separate it
884&apos;=======================================================
885Function GetFilePath(fname As String)
886
887    i = 1
888    Do
889        If (Mid(fname,i,5) = &quot;text/&quot;) Then
890            Strg = Mid(fname,i,Len(fname)-i+1)
891            Exit Do
892        Else
893            i = i+1
894            Strg = fname
895        End If
896    Loop While (i+5 &lt; Len(fname))
897    GetFilePath = Strg
898End Function
899
900&apos;=======================================================
901&apos; OpenGraphics
902&apos;-------------------------------------------------------
903&apos; Calls the graphic open dialog for inserting an image
904&apos;=======================================================
905Function OpenGraphics(oDoc As Object)
906Dim ListAny(0) as Long
907Dim oStoreProperties(0) as New com.sun.star.beans.PropertyValue
908    GlobalScope.BasicLibraries.loadLibrary(&quot;Tools&quot;)
909    ListAny(0) = com.sun.star.ui.dialogs.TemplateDescription.FILEOPEN_SIMPLE
910&apos;  ListAny(0) = com.sun.star.ui.dialogs.TemplateDescription.FILEOPEN_LINK_PREVIEW_IMAGE_TEMPLATE
911    oFileDialog = CreateUnoService(&quot;com.sun.star.ui.dialogs.FilePicker&quot;)
912    oFileDialog.Initialize(ListAny())
913
914    sLastImgDir = ReadConfig(&quot;LastImgDir&quot;)
915    If sLastImgDir &lt;&gt; &quot;&quot; Then
916        oFileDialog.setDisplayDirectory(sLastImgDir)
917    End If
918
919    oMasterKey = GetRegistryKeyContent(&quot;org.openoffice.TypeDetection.Types/&quot;)
920    oTypes() = oMasterKey.Types
921
922    oFileDialog.AppendFilter(oTypes.GetByName(&quot;gif_Graphics_Interchange&quot;).UIName, &quot;*.gif&quot;)
923    oFileDialog.AppendFilter(oTypes.GetByName(&quot;png_Portable_Network_Graphic&quot;).UIName, &quot;*.png&quot;)
924
925    oFileDialog.SetTitle(&quot;Insert Image&quot;)
926    iAccept = oFileDialog.Execute()
927    If iAccept = 1 Then
928        sPath = oFileDialog.Files(0)
929        WriteConfig(&quot;LastImgDir&quot;,oFileDialog.getDisplayDirectory)
930        UIFilterName = oFileDialog.GetCurrentFilter()
931        OpenGraphics = oFileDialog.Files(0)
932    Else
933        OpenGraphics = &quot;&quot;
934    End If
935    oFileDialog.Dispose()
936End Function
937
938&apos;=======================================================
939&apos; WriteConfig
940&apos;-------------------------------------------------------
941&apos; Reads a parameter value from the config file
942&apos;=======================================================
943Function ReadConfig(Parm As String)
944    oPath = createUNOService(&quot;com.sun.star.util.PathSettings&quot;)
945    filename = oPath.UserConfig+&quot;/helpauthoring.cfg&quot;
946    iNumber = Freefile
947    bFound = false
948    If FileExists(filename) Then
949        Open filename For Input As iNumber
950        Do While (not eof(iNumber) AND not(bFound))
951            Line Input #iNumber, sLine
952            If InStr(sLine, &quot;=&quot;) &gt; 0 Then
953                arLine = split(sLine,&quot;=&quot;)
954                If arLine(0) = Parm Then
955                    sResult = arLine(1)
956                    bFound = true
957                End If
958            End If
959        Loop
960        Close #iNumber
961        If bFound Then
962            ReadConfig = sResult
963        Else
964            ReadConfig = &quot;&quot;
965        End If
966    Else
967        ReadConfig = &quot;&quot;
968    End If
969End Function
970
971
972&apos;=======================================================
973&apos; WriteConfig
974&apos;-------------------------------------------------------
975&apos; Writes a parameter/value pair to the config file
976&apos;=======================================================
977Function WriteConfig(Parm As String, Value As String)
978    Dim arLines(0) As String
979    bFound = false
980    oPath = createUNOService(&quot;com.sun.star.util.PathSettings&quot;)
981    filename = oPath.UserConfig+&quot;/helpauthoring.cfg&quot;
982    iNumber = Freefile
983    If FileExists(filename) Then
984
985        Open filename For Input As iNumber
986        Do While (not eof(iNumber))
987            Line Input #iNumber, sLine
988            If InStr(sLine, &quot;=&quot;) &gt; 0 Then
989                sDim = ubound(arLines())+1
990                ReDim Preserve arLines(sDim)
991                arLines(sDim) = sLine
992            End If
993        Loop
994        Close #iNumber
995
996        nLine = 1
997        Do While (nLine &lt;= ubound(arLines())) and (not bFound)
998            arLine = split(arLines(nLine),&quot;=&quot;)
999            If arLine(0) = Parm Then
1000                arLines(nLine) = Parm+&quot;=&quot;+Value
1001                bFound = true
1002            End If
1003            nLine = nLine +1
1004        Loop
1005
1006        nLine = 1
1007        Open filename For Output As iNumber
1008        Do While (nLine &lt;= ubound(arLines()))
1009            Print #iNumber, arLines(nLine)
1010            nLine = nLine + 1
1011        Loop
1012        If (not bFound) Then
1013            Print #iNumber, Parm+&quot;=&quot;+Value
1014        End If
1015        Close #iNumber
1016
1017    Else
1018        Open filename For Output As iNumber
1019        Print #iNumber, Parm+&quot;=&quot;+Value
1020        Close #iNumber
1021    End If
1022End Function
1023
1024Function GetRelPath(sPath As String)
1025    sHelpPrefix = ReadConfig(&quot;HelpPrefix&quot;)
1026    If sHelpPrefix = &quot;&quot; Then
1027        sHelpPrefix = SetDocumentRoot
1028    End If
1029    GetRelPath = Right(sPath, Len(sPath)-(InStr(sPath,sHelpPrefix) + Len(sHelpPrefix)-1))
1030End Function
1031
1032Function SetDocumentRoot
1033    sHelpPrefix = ReadConfig(&quot;HelpPrefix&quot;)
1034    oFolderDialog = CreateUnoService(&quot;com.sun.star.ui.dialogs.FolderPicker&quot;)
1035    oFolderDialog.SetTitle(&quot;Select Document Root Folder&quot;)
1036    If sHelpPrefix &gt; &quot;&quot; Then
1037        oFolderDialog.setDisplayDirectory(sHelpPrefix)
1038    End If
1039    iAccept = oFolderDialog.Execute()
1040
1041    If iAccept = 1 Then
1042        sHelpPrefix = oFolderDialog.getDirectory + &quot;/&quot;
1043        WriteConfig(&quot;HelpPrefix&quot;,sHelpPrefix)
1044    End If
1045
1046    SetDocumentRoot = sHelpPrefix
1047End Function
1048
1049Function MakeAbsPath(sPath As String)
1050
1051    sHelpPrefix = ReadConfig(&quot;HelpPrefix&quot;)
1052    If sHelpPrefix = &quot;&quot; Then
1053        sHelpPrefix = SetDocumentRoot
1054    End If
1055
1056    If Right(sPath,4) &lt;&gt; &quot;.xhp&quot; Then
1057        sPath=sPath+&quot;.xhp&quot;
1058    End If
1059    MakeAbsPath = sHelpPrefix+sPath
1060End Function
1061
1062
1063Sub UpdateFields
1064    dim document   as object
1065    dim dispatcher as object
1066    document   = ThisComponent.CurrentController.Frame
1067    dispatcher = createUnoService(&quot;com.sun.star.frame.DispatchHelper&quot;)
1068
1069    dispatcher.executeDispatch(document, &quot;.uno:UpdateFields&quot;, &quot;&quot;, 0, Array())
1070End Sub
1071
1072Function IsHelpFile
1073    document = StarDesktop.CurrentComponent
1074    IsHelpFile = (Right(GetFilePath(document.URL),4)=&quot;.xhp&quot;)
1075End Function
1076
1077Function GetUserFieldNumber(fn as String)
1078    fnum = -1
1079    For a=0 to document.DocumentInfo.getUserFieldCount - 1
1080        If document.DocumentInfo.getUserFieldName(a) = fn Then
1081            fnum = a
1082            Exit for
1083        End If
1084    Next a
1085    GetUserFieldNumber = fnum
1086End Function
1087</script:module>