xref: /AOO41X/main/helpauthoring/HelpAuthoring/Embed.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="Embed" script:language="StarBasic">&apos; *** MODULE EMBED ***
4
5Dim oDialog AS Object
6Dim oBrowseDialog As Object
7Dim document AS Object
8Dim arParas(0) As String
9Dim arSecs(0) As String
10Dim arVars(0) As String
11
12&apos;=======================================================
13&apos; Main
14&apos;-------------------------------------------------------
15&apos; Embeds a variable or a section
16&apos;=======================================================
17Sub Main(optional bCR As Boolean)
18
19    If not IsHelpFile Then
20        msgbox(strErr_NoHelpFile)
21        Exit Sub
22    End If
23
24    GlobalScope.BasicLibraries.loadLibrary(&quot;Tools&quot;)
25
26    document = StarDesktop.CurrentComponent
27
28    BasicLibraries.LoadLibrary(&quot;HelpAuthoring&quot;)
29    oDialog = LoadDialog(&quot;HelpAuthoring&quot;, &quot;dlgEmbed&quot;)
30    oDialogModel = oDialog.Model
31
32    oTxtFilePath = oDialog.GetControl(&quot;txtFilePath&quot;)          &apos; path to file, rel to Docroot
33    oTxtID = oDialog.GetControl(&quot;txtID&quot;)                  &apos; anchor
34    oTxtHidFName = oDialog.GetControl(&quot;txtHidFName&quot;)      &apos; pure filename
35    oOpVariable = oDialog.GetControl(&quot;opVariable&quot;)
36    oOpSection = oDialog.GetControl(&quot;opSection&quot;)
37
38    DocRoot = ReadConfig(&quot;HelpPrefix&quot;)
39    EmbedFolder = ReadConfig(&quot;LastEmbedDir&quot;)
40    EmbedFile = ReadConfig(&quot;LastEmbedFile&quot;)
41    EmbedID = ReadConfig(&quot;LastEmbedAnchor&quot;)
42    EmbedType = ReadConfig(&quot;LastEmbedType&quot;)
43    If EmbedType = &quot;Variable&quot; Then
44        oOpVariable.State = TRUE
45    Else
46        oOpVariable.State = FALSE
47    End If
48    SetLabel
49
50    If IsSubDir(EmbedFolder,DocRoot) Then
51        RelDir = Right(EmbedFolder,Len(EmbedFolder)-Len(DocRoot))
52        If Dir(DocRoot+RelDir+EmbedFile) &gt; &quot;&quot; Then
53            oTxtFilePath.Text = RelDir+EmbedFile
54            oTxtHidFName.Text = DocRoot+RelDir+EmbedFile
55            oTxtID.Text = EmbedID
56        End If
57    End If
58
59
60    GoForIt = 1
61
62    If (oDialog.Execute() = 1 AND oTxtFilePath.Text &lt;&gt; &quot;&quot;) Then
63&apos;      msgbox (oTxtFilePath.Text)
64
65        If oTxtID.Text = &quot;&quot; Then
66            msgbox &quot;You did not specify a section or variable to embed.&quot;,256
67        Else
68            WriteConfig(&quot;LastEmbedAnchor&quot;,oTxtID.Text)
69            If oOpVariable.State Then
70                txtEmbed = oTxtFilePath.Text + &quot;#&quot; + oTxtID.Text
71                InsertTag(&quot;EMBEDVAR&quot;,&quot;&lt;EMBEDVAR var=&quot;&quot;&quot; + txtEmbed + &quot;&quot;&quot;&gt;&quot;,&quot;hlp_aux_tag&quot;)
72                SetCharStyle(&quot;Default&quot;)
73                WriteConfig(&quot;LastEmbedType&quot;,&quot;Variable&quot;)
74            Else
75                txtEmbed = oTxtFilePath.Text + &quot;#&quot; + oTxtID.Text
76                CR
77                SetParaStyle(&quot;hlp_aux_embed&quot;)
78                SetCharStyle(&quot;hlp_aux_tag&quot;)
79                InsertTag(&quot;EMBED&quot;,&quot;&lt;EMBED href=&quot;&quot;&quot; + txtEmbed + &quot;&quot;&quot;&gt;&quot;,&quot;hlp_aux_tag&quot;)
80                CR
81                WriteConfig(&quot;LastEmbedType&quot;,&quot;Section&quot;)
82            End If
83        End If
84    End If
85    oDialog.dispose
86End Sub
87
88&apos;=======================================================
89&apos; SetLabel
90&apos;-------------------------------------------------------
91&apos; Changes the text field label in the dialog
92&apos;=======================================================
93Sub SetLabel
94    olblID = oDialog.GetControl(&quot;lblID&quot;)
95    oOpVariable = oDialog.GetControl(&quot;opVariable&quot;)
96    If oOpVariable.getState Then
97        olblID.setText(&quot;Variable ID&quot;)
98        oDialog.Title = &quot;Embed Variable&quot;
99    Else
100        olblID.setText(&quot;Section or Paragraph ID&quot;)
101        oDialog.Title = &quot;Embed Section&quot;
102    End If
103End Sub
104
105Sub GetFile
106Dim ListAny(0) as Long
107    ListAny(0) = com.sun.star.ui.dialogs.TemplateDescription.FILEOPEN_SIMPLE
108    oFileDialog = CreateUnoService(&quot;com.sun.star.ui.dialogs.FilePicker&quot;)
109    oFileDialog.Initialize(ListAny())
110
111    DocRoot = ReadConfig(&quot;HelpPrefix&quot;)
112    sLastEmbedDir = ReadConfig(&quot;LastEmbedDir&quot;)
113    sLastEmbedFile = ReadConfig(&quot;LastEmbedFile&quot;)
114
115    If IsSubDir(sLastEmbedDir,DocRoot) Then
116        oFileDialog.setDisplayDirectory(sLastEmbedDir)
117        If sLastEmbedFile &lt;&gt; &quot;&quot; AND Dir(sLastEmbedDir+sLastEmbedFile) &gt; &quot;&quot; Then
118            oFileDialog.setDefaultName(sLastEmbedFile)
119        End If
120    Else
121        oFileDialog.setDisplayDirectory(DocRoot)
122    End If
123
124    oMasterKey = GetRegistryKeyContent(&quot;org.openoffice.TypeDetection.Types/&quot;)
125    oTypes() = oMasterKey.Types
126    oFileDialog.AppendFilter(&quot;Help&quot;, &quot;*.xhp&quot;)
127
128    oFileDialog.SetTitle(&quot;Embed From Help File&quot;)
129    iAccept = oFileDialog.Execute()
130    If iAccept = 1 Then
131        sPath = oFileDialog.Files(0)
132        sCurDir = oFileDialog.getDisplayDirectory +&quot;/&quot;
133        WriteConfig(&quot;LastEmbedDir&quot;,sCurDir)
134        LastFile = Right(sPath, Len(sPath) - Len(sCurDir))
135        WriteConfig(&quot;LastEmbedFile&quot;,LastFile)
136
137        oTxtPath = oDialog.GetControl(&quot;txtFilePath&quot;)
138        oTxtHidFName = oDialog.GetControl(&quot;txtHidFName&quot;)
139        oTxtHidFName.Text = sPath
140
141        If IsSubDir(sCurDir,DocRoot) Then
142            oTxtPath.Text = GetRelPath(sPath, DocRoot)
143        Else
144            oTxtPath.Text = sPath
145            msgbox(&quot;File is outside of your Document Root&quot;,48,&quot;Warning&quot;)
146        End If
147
148        oButBrowseIDs = oDialog.GetControl(&quot;butBrowseIDs&quot;)
149        oButBrowseIDs.Enable = true
150    End If
151
152End Sub
153
154Sub UpdateFileName
155    oTxtPath = oDialog.GetControl(&quot;txtFilePath&quot;)
156    ShortFName = oTxtPath.Text
157
158    If ShortFName &gt; &quot;&quot; Then
159
160        oTxtHidFName = oDialog.GetControl(&quot;txtHidFName&quot;)
161        FName = oTxtHidFName.Text
162
163        If Right(FName, Len(ShortFName)) &lt;&gt; ShortFName Then
164        &apos;  oTxtHidFName.Text = MakeAbsPath(ShortFName)
165            oTxtHidFName.Text = ShortFName
166        End If
167
168        oButBrowseIDs = oDialog.GetControl(&quot;butBrowseIDs&quot;)
169        If not(FileExists(oTxtHidFName.Text)) Then
170            msgbox oTxtHidFName.Text+&quot; cannot be found.&quot;,48,&quot;D&apos;oh!&quot;
171            oButBrowseIDs.Enable = false
172        Else
173            oButBrowseIDs.Enable = true
174        End If
175    End If
176End Sub
177
178Sub BrowseEmbed
179    BasicLibraries.LoadLibrary(&quot;HelpAuthoring&quot;)
180    oBrowseDialog = LoadDialog(&quot;HelpAuthoring&quot;, &quot;dlg_BrowseEmbed&quot;)
181    oOpVariable = oDialog.GetControl(&quot;opVariable&quot;)
182    oTxtPath = oDialog.GetControl(&quot;txtFilePath&quot;)
183    oTxtHidFName = oDialog.GetControl(&quot;txtHidFName&quot;)
184    filename = oTxtHidFName.Text
185
186    ReDim arParas(0)
187    ReDim arVars(0)
188    ReDim arSecs(0)
189
190&apos;  msgbox(filename)
191
192    iNumber = Freefile
193    If FileExists(filename) Then
194        Dim arLines(0) As String
195        Open filename For Input As iNumber
196        Do While (not eof(iNumber))
197            Line Input #iNumber, sLine
198            sDim = ubound(arLines())+1
199            ReDim Preserve arLines(sDim)
200            arLines(sDim) = sLine
201        Loop
202        Close #iNumber
203        sContent = join(arLines(),&quot; &quot;)
204
205        arTmp() = split(sContent,&quot;&lt;paragraph&quot;)
206        For n=1 to ubound(arTmp())
207            If arTmp(n) &lt;&gt; &quot;&quot; Then
208                arTmp(n) = Right(arTmp(n),Len(arTmp(n))-Instr(arTmp(n),&quot;id=&quot;)-3)
209                sId = Left(arTmp(n),Instr(arTmp(n),&quot;&quot;&quot;&quot;)-1)
210                arTmp(n) = Right(arTmp(n),Len(arTmp(n))-Instr(arTmp(n),&quot;&gt;&quot;))
211                arTmp(n) = Left(arTmp(n),Instr(arTmp(n),&quot;&lt;/paragraph&gt;&quot;)-1)
212                If Len(arTmp(n) &gt; 100) Then
213                    arTmp(n) = Left(arTmp(n),100)+&quot;...&quot;
214                End If
215                sDim = ubound(arParas())
216                arParas(sDim) = sId+&quot;: &quot;+arTmp(n)
217                sDim = ubound(arParas())+1
218                ReDim Preserve arParas(sDim)
219            End If
220        Next n
221
222        arTmp() = split(sContent,&quot;&lt;section&quot;)
223        For n=1 to ubound(arTmp())
224            If arTmp(n) &lt;&gt; &quot;&quot; Then
225                arTmp(n) = Right(arTmp(n),Len(arTmp(n))-Instr(arTmp(n),&quot;id=&quot;)-3)
226                sId = Left(arTmp(n),Instr(arTmp(n),&quot;&quot;&quot;&quot;)-1)
227                arTmp(n) = Right(arTmp(n),Len(arTmp(n))-Instr(arTmp(n),&quot;&gt;&quot;))
228                If Instr(arTmp(n),&quot;&lt;/section&gt;&quot;)&gt;0 Then
229                    arTmp(n) = Left(arTmp(n),Instr(arTmp(n),&quot;&lt;/section&gt;&quot;)-1)
230                End If
231                If Len(arTmp(n) &gt; 100) Then
232                    arTmp(n) = Left(arTmp(n),100)+&quot;...&quot;
233                End If
234                sDim = ubound(arSecs())
235                arSecs(sDim) = sId+&quot;: &quot;+arTmp(n)
236                sDim = ubound(arSecs())+1
237                ReDim Preserve arSecs(sDim)
238            End If
239        Next n
240
241        arTmp() = split(sContent,&quot;&lt;variable&quot;)
242        For n=1 to ubound(arTmp())
243            If arTmp(n) &lt;&gt; &quot;&quot; Then
244                arTmp(n) = Right(arTmp(n),Len(arTmp(n))-Instr(arTmp(n),&quot;id=&quot;)-3)
245                sId = Left(arTmp(n),Instr(arTmp(n),&quot;&quot;&quot;&quot;)-1)
246                arTmp(n) = Right(arTmp(n),Len(arTmp(n))-Instr(arTmp(n),&quot;&gt;&quot;))
247                arTmp(n) = Left(arTmp(n),Instr(arTmp(n),&quot;&lt;/variable&gt;&quot;)-1)
248                If Len(arTmp(n) &gt; 100) Then
249                    arTmp(n) = Left(arTmp(n),100)+&quot;...&quot;
250                End If
251                sDim = ubound(arVars())
252                arVars(sDim) = sId+&quot;: &quot;+arTmp(n)
253                sDim = ubound(arVars())+1
254                ReDim Preserve arVars(sDim)
255            End If
256        Next n
257
258        ShowSecs
259
260        If oBrowseDialog.Execute() = 1 Then
261            olbElements = oBrowseDialog.GetControl(&quot;lbElements&quot;)
262            sSelected = olbElements.SelectedItem
263            sSelected = Left(sSelected,Instr(sSelected,&quot;:&quot;)-1)
264            oTxtID = oDialog.GetControl(&quot;txtID&quot;)
265            oTxtID.Text = sSelected
266        End If
267    Else
268        msgbox &quot;Cannot open &quot;+filename,48,&quot;Error&quot;
269    End If
270End Sub
271
272Sub UpdateLIst
273    oOpSections = oBrowseDialog.GetControl(&quot;opSections&quot;)
274    oOpVariables = oBrowseDialog.GetControl(&quot;opVariables&quot;)
275    oOpParas = oBrowseDialog.GetControl(&quot;opParas&quot;)
276    If oOpSections.getState Then
277        ShowSecs
278    ElseIf oOpVariables.getState Then
279        ShowVars
280    ElseIf oOpParas.getState Then
281        ShowParas
282    End If
283End Sub
284
285Sub ShowSecs
286    olbElements = oBrowseDialog.GetControl(&quot;lbElements&quot;)
287    olbElements.RemoveItems(0,olbElements.ItemCount)
288    olbElements.AddItems(arSecs(),ubound(arSecs()))
289    oOpSection = oDialog.GetControl(&quot;opSection&quot;)
290    oOpSection.setState(TRUE)
291    SetLabel
292End Sub
293
294Sub ShowVars
295    olbElements = oBrowseDialog.GetControl(&quot;lbElements&quot;)
296    olbElements.RemoveItems(0,olbElements.ItemCount)
297    olbElements.AddItems(arVars(),ubound(arVars()))
298    oOpVariable = oDialog.GetControl(&quot;opVariable&quot;)
299    oOpVariable.setState(TRUE)
300    SetLabel
301End Sub
302
303Sub ShowParas
304    olbElements = oBrowseDialog.GetControl(&quot;lbElements&quot;)
305    olbElements.RemoveItems(0,olbElements.ItemCount)
306    olbElements.AddItems(arParas(),ubound(arParas()))
307    oOpVariable = oDialog.GetControl(&quot;opSection&quot;)
308    oOpVariable.setState(TRUE)
309    SetLabel
310End Sub
311
312Sub CheckButton
313    olbElements = oBrowseDialog.GetControl(&quot;lbElements&quot;)
314    obutSelect = oBrowseDialog.GetControl(&quot;butSelect&quot;)
315    sSelected = olbElements.SelectedItem
316    If sSelected = &quot;&quot; Then
317        oButSelect.enable = false
318    Else
319        oButSelect.enable = true
320    End If
321End Sub
322
323
324Function IsSubDir(D as String, R as String)
325     IsSubDir = (Left(D,Len(R)) = R)
326End Function
327</script:module>