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