merge the formfield patch from ooo-build
[ooovba.git] / wizards / source / webwizard / HtmlAutoPilotBasic.xba
blob00675bbf0c747b96036e2fd51717c9702f0a36d9
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="HtmlAutoPilotBasic" script:language="StarBasic">&apos; Variables must be declared
4 Option Explicit
6 Public CurDocIndex as Integer
7 Public CurWebPageIndex as Integer
10 Public bWithBackGraphic as Boolean
11 Public oStyle as Object
12 &apos; Maximum number of content templates, style templates and bullets
13 Const MaxLayouts = 50
14 Const MaxStyles = 100
15 Const MaxBullets = 10
17 &apos;Public NumberOfLayouts%, NumberOfStyles%
19 &apos; Filled with title, previous, next, home, top, bullet, background, file name
20 Public Style(MaxStyles, 8) as String
22 Public Layout$(MaxLayouts, 2)
24 Public TextureDir$, BulletDir$, GraphicsDir$, GalleryDir$, PhotosDir$
25 Public SOBitmapPath as String
26 Public CurrentBullet$, CurrentPrev$, CurrentNext$, CurrentHome$, CurrentTop$
27 Public FileStr as String
29 Public WebWiz_gWizardName$, WebWiz_gErrContentNotFound$, WebWiz_gErrStyleNotFound$
30 Public WebWiz_gErrMainTemplateError$, WebWiz_gErrWhileReloading$
31 Public WebWiz_gErrWhileLoadStyles$, WebWiz_gErrMsg$, WebWiz_gErrMainDocumentError$
33 Public ProgressBar as Object
34 Public ProgressValue As Long
35 Public oBaseDocument as Object
36 Public oViewCursor as Object
37 Public oViewSettings as Object
38 Public NoArgs() as New com.sun.star.beans.PropertyValue
40 Public oCursor as Object
41 Public oBookmarks as Object
42 Public oBookMark as Object
44 Public oUcb as Object
45 Public MainDialog as Object
46 Public DialogModel as Object
49 Sub Main
50 Dim RetValue
51 On Local Error Goto GlobalErrorHandler
52 BasicLibraries.LoadLibrary(&quot;Tools&quot;)
53 oUcb = createUnoService(&quot;com.sun.star.ucb.SimpleFileAccess&quot;)
54 oBaseDocument = StarDesktop.LoadComponentFromURL(&quot;private:factory/swriter/web&quot;, &quot;_default&quot;, 0, NoArgs())
55 oViewSettings = oBaseDocument.CurrentController.ViewSettings
56 oViewCursor = oBaseDocument.GetCurrentController.ViewCursor
57 CurWebPageIndex = -1
58 ProgressBar = oBaseDocument.GetCurrentController.GetFrame.CreateStatusIndicator
59 ProgressBar.Start(&quot;&quot;, 100)
60 SetProgressValue(2)
61 oBaseDocument.LockControllers
62 oViewSettings.ShowTableBoundaries = False
63 If Not GetWebWizardPaths() Then
64 Exit Sub
65 End If
66 LoadLanguage
67 SetProgressValue(10)
68 Layout() = getListBoxArrays(oUcb, &quot;/cnt&quot;)
69 GetCurIndex(DialogModel, Layout(),2)
70 SetProgressValue(30)
71 oCursor = oBasedocument.Text.CreateTextCursor
72 oCursor.InsertDocumentfromURL(FileStr, NoArgs())
73 SetProgressValue(50)
74 Style() = getListBoxArrays(oUcb, &quot;/stl&quot;)
75 SetProgressValue(70)
76 LoadWebPageStyles(oBaseDocument)
77 SetProgressValue(90)
78 oBaseDocument.UnlockControllers
79 OpenWebDialog()
80 SetProgressValue(98)
81 SetProgressValue(0)
82 MainDialog.Model.ImagePreview.BackGroundColor = RGB(0, 60,126)
83 RetValue = MainDialog.Execute
84 Select Case RetValue
85 Case 0
86 MainDialog.Dispose()
87 DisposeDocument(oBaseDocument)
88 Case 1
89 EndDialog()
90 MainDialog.Dispose()
91 End Select
92 GLOBALERRORHANDLER:
93 If Err &lt;&gt; 0 Then
94 MsgBox (WebWiz_gErrMsg$, 16, WebWiz_gWizardName$)
95 DisposeDocument(oBaseDocument)
96 RESUME EXITWIZARD
97 EXITWIZARD:
98 End If
99 End Sub
102 Function SetProgressValue(iValue as Integer)
103 If iValue = 0 Then
104 ProgressBar.End
105 End If
106 ProgressValue = iValue
107 ProgressBar.Value = iValue
108 End Function
111 Sub ReloadCurrentDocument()
112 Dim OldDocIndex as Integer
113 On Local Error Goto ErrorOcurred
114 OldDocIndex = CurDocIndex
115 CurDocIndex = GetCurIndex(DialogModel.lbTemplate, Layout(), 2)
116 If OldDocIndex &lt;&gt; CurDocIndex Then
117 oBaseDocument.LockControllers
118 ToggleDialogControls(False)
119 oCursor = oBaseDocument.Text.CreateTextCursor()
120 oCursor.GotoStart(False)
121 oCursor.GotoEnd(True)
122 oCursor.SetAllPropertiesToDefault()
123 oCursor.InsertDocumentfromURL(FileStr, NoArgs())
124 SetBulletAndGraphics
125 CheckControls(oBaseDocument.DrawPage)
126 ErrorOcurred:
127 If Err &lt;&gt; 0 Then
128 MsgBox(WebWiz_gErrWhileReloading$, 16, WebWiz_gWizardName$)
129 End If
130 oBaseDocument.UnlockControllers
131 oViewCursor.GotoStart(False)
132 ToggleDialogControls(True, &quot;lbTemplate&quot;)
133 End If
134 End Sub
139 Sub LoadWebPageStyles(aEvent as Object, Optional bStartUp as Boolean)
140 Dim OldWebPageIndex as Integer
141 OldWebPageIndex = CurWebPageIndex
142 If IsNull(DialogModel) Then
143 CurWebPageIndex = GetCurIndex(DialogModel, Style(), 8)
144 Else
145 CurWebPageIndex = GetCurIndex(DialogModel.lbStyles, Style(), 8)
146 End If
147 If OldWebPageIndex &lt;&gt; CurWebPageIndex Then
148 ToggleDialogControls(False)
149 oBaseDocument.LockControllers
150 bWithBackGraphic = LoadNewStyles(oBaseDocument, DialogModel, CurWebPageIndex, FileStr, Style(), TextureDir)
151 CurrentBullet$ = BulletDir + Style(CurWebPageIndex, 6)
152 CurrentPrev$ = GraphicsDir + Style(CurWebPageIndex, 2)
153 CurrentNext$ = GraphicsDir + Style(CurWebPageIndex, 3)
154 CurrentHome$ = GraphicsDir + Style(CurWebPageIndex, 4)
155 CurrentTop$ = GraphicsDir + Style(CurWebPageIndex, 5)
156 With oBaseDocument.DocumentProperties.UserDefinedProperties
157 .AutoPilotName1 = ExtractGraphicNames(CurWebPageIndex,2)
158 .AutoPilotName2 = ExtractGraphicNames(CurWebPageIndex, 4)
159 .AutoPilotBullet = Style(CurWebPageIndex, 6)
160 .AutoPilotBackground = Style(CurWebPageIndex, 7)
161 End With
162 SetBulletAndGraphics()
163 CheckControls(oBaseDocument.DrawPage)
164 oViewCursor.GotoStart(False)
165 oBaseDocument.UnlockControllers
166 ToggleDialogControls(True, &quot;lbStyles&quot;)
167 End If
168 End Sub
171 Function ExtractGraphicNames(CurIndex as Integer, i as Integer) as String
172 Dim FieldValue as String
173 FieldValue = GetFileNameWithoutExtension(Style(CurIndex, i))
174 FieldValue = FieldValue &amp; &quot; &quot; &amp; GetFileNameWithoutExtension(Style(CurIndex, i+1))
175 ExtractGraphicNames = FieldValue
176 End Function
179 Sub SetBulletAndGraphics
180 SetGraphic(&quot;Prev&quot;, CurrentPrev)
181 SetGraphic(&quot;Next&quot;, CurrentNext)
182 SetGraphic(&quot;Home&quot;, CurrentHome)
183 SetGraphic(&quot;Top&quot;, CurrentTop)
184 SetBulletGraphics(CurrentBullet)
185 SetGraphicsToOriginalSize()
186 End Sub
189 Sub SetGraphicsToOriginalSize()
190 Dim oGraphics as Object
191 Dim oGraphic as Object
192 Dim i as Integer
193 Dim aActSize as New com.sun.star.awt.Size
194 oGraphics = oBaseDocument.GraphicObjects
195 For i = 0 To oGraphics.Count-1
196 oGraphic = oGraphics.GetByIndex(i)
197 aActSize = oGraphic.ActualSize
198 If aActSize.Height &gt; 0 And aActSize.Width &gt; 0 Then
199 oGraphic.SetSize(aActSize)
200 End If
201 Next i
202 End Sub
205 Sub EndDialog()
206 If DialogModel.chkSaveasTemplate.State = 1 Then
207 &apos; Generating template? Set events later!
208 AttachBasicMacroToEvent(oBaseDocument,&quot;OnNew&quot;, &quot;WebWizard.HtmlAutoPilotBasic.SetEvent()&quot;)
209 &apos; Call the Store template dialog
210 DispatchSlot(5538)
211 AttachBasicMacroToEvent(oBaseDocument,&quot;OnNew&quot;, &quot;&quot;)
212 End If
213 SetEvent()
214 End Sub
217 Sub SetEvent()
218 Dim oDocument as Object
219 &apos; This sub links the events OnSaveDone and OnSaveAsDone to the procedure
220 &apos; CopyGraphics. It is invoked when a document is created, either directly
221 &apos; from the AutoPilot or from a template. It is not possible to set these
222 &apos; links for the template created by the AutoPilot because then it is not
223 &apos; possible to modify the template.
224 BasicLibraries.LoadLibrary(&quot;Tools&quot;)
225 oDocument = ThisComponent
226 AttachBasicMacroToEvent(oDocument,&quot;OnSaveDone&quot;, &quot;WebWizard.HtmlAutoPilotBasic.CopyGraphics()&quot;)
227 AttachBasicMacroToEvent(oDocument,&quot;OnSaveAsDone&quot;, &quot;WebWizard.HtmlAutoPilotBasic.CopyGraphics()&quot;)
228 End Sub
232 Sub CopyGraphics
233 &apos; This sub copies all the graphics used in the document to the same directory the
234 &apos; document has been copied into and changes the graphics links in the document.
235 Dim oGraphicObjects as Object
236 Dim oGraphic as Object
237 Dim i as Integer
238 Dim udProps as Object
239 Dim SavePath as String
240 BasicLibraries.LoadLibrary(&quot;Tools&quot;)
241 oUcb = createUnoService(&quot;com.sun.star.ucb.SimpleFileAccess&quot;)
242 GetWebWizardPaths()
243 oBaseDocument = ThisComponent
244 &apos; oBaseDocument.LockControllers()
246 &apos; Note: The sub DirectoryNameoutofPath should be change, so that the last character is a slash
247 SavePath = DirectoryNameoutofPath(oBaseDocument.Url, &quot;/&quot;) &amp; &quot;/&quot;
249 oGraphicObjects = oBaseDocument.GraphicObjects
250 For i = 0 to oGraphicObjects.Count-1
251 oGraphic = oGraphicObjects.GetbyIndex(i)
252 oGraphic.GraphicUrl = CopyFile(oGraphic.GraphicURL, SavePath)
253 Next i
255 ChangeBackGraphicUrl(SavePath)
257 BulletUrlsToSavePath(SavePath)
259 udProps = oBaseDocument.DocumentProperties.UserDefinedProperties
260 udProps.addProperty(&quot;AutoPilotName1&quot;, 0, &quot;&quot;)
261 udProps.addProperty(&quot;AutoPilotName2&quot;, 0, &quot;&quot;)
262 udProps.addProperty(&quot;AutoPilotBullet&quot;, 0, &quot;&quot;)
263 udProps.addProperty(&quot;AutoPilotBackground&quot;, 0, &quot;&quot;)
265 AttachBasicMacroToEvent(oBaseDocument,&quot;OnSaveDone&quot;, &quot;&quot;)
266 AttachBasicMacroToEvent(oBaseDocument,&quot;OnSaveAsDone&quot;, &quot;&quot;)
267 AttachBasicMacroToEvent(oBaseDocument,&quot;OnNew&quot;, &quot;&quot;)
268 oBaseDocument.Store
269 &apos; oBaseDocument.UnlockControllers()
270 End Sub
273 Function CopyFile(ByVal SourceUrl as String, TargetDir as String)
274 Dim sFileName as String
275 Dim sNewFileUrl as String
276 If oUcb.Exists(TargetDir) Then
277 If Len(TargetDir) &gt; 2 Then
278 sFileName = FileNameoutofPath(SourceUrl)
279 sNewFileUrl = TargetDir &amp; sFileName
280 oUcb.Copy(SourceUrl, sNewFileUrl)
281 CopyFile() = sNewFileUrl
282 End If
283 End If
284 End Function
287 Function getListBoxArrays(oUcb as Object, sFileFilter as String)
288 Dim oDocProps as Object
289 Dim oListboxControl as Object
290 Dim Description as String
291 Dim sField as String
292 Dim sFieldList() as String
293 Dim bItemFound as Boolean
294 Dim MaxIndex as Integer
295 Dim DirContent() as String
296 Dim FileName as String
297 Dim TemplatePath as String
298 Dim FilterLen as Integer
299 Dim i as Integer
300 Dim m as Integer
301 Dim n as Integer
302 Dim s as Integer
303 Dim a as Integer
304 Dim LocMaxIndex as Integer
305 Dim Properties()
306 Dim DimCount as Integer
307 Dim sExtension as String
308 oDocProps = CreateUnoService(&quot;com.sun.star.document.DocumentProperties&quot;)
309 FilterLen = Len(sFileFilter)
310 bItemFound = False
311 &apos; It has to be made sure that the TemplatePath &lt;&gt; &quot;&quot;
312 TemplatePath = GetOfficeSubPath(&quot;Template&quot;, &quot;wizard/web/&quot;)
313 If TemplatePath = &quot;&quot; Then
314 Dim NullList()
315 getListBoxArrays() = NullList()
316 Exit Function
317 End If
318 DirContent() = oUcb.GetFolderContents(TemplatePath,True)
319 If sFileFilter = &quot;/cnt&quot; Then
320 DimCount = 2
321 Else
322 DimCount = 8
323 End If
324 LocMaxIndex = Ubound(DirContent())
325 Dim List(LocMaxIndex, DimCount) as String
326 Dim SortList(LocMaxIndex,1)
327 For i = 0 to LocMaxIndex
328 SortList(i,0) = DirContent(i)
329 SortList(i,1) = RetrieveDocTitle(oDocProps, DirContent(i))
330 Next i
331 SortList() = BubbleSortList(SortList(),True)
332 For i = 0 to LocMaxIndex
333 DirContent(i) = SortList(i,0)
334 Next i
335 a = 0
336 For i = 0 To LocMaxIndex
337 FileName = DirContent(i)
338 sExtension = Ucase(GetFileNameExtension(FileName))
339 If Instr(1,Filename, sFileFilter) And sExtension = &quot;STW&quot; Then
340 bItemFound = True
341 Description = RetrieveDocTitle(oDocProps, FileName)
342 Properties = oDocProps.UserDefinedProperties.PropertyValues
343 List(a,1) = Description
344 If sFileFilter = &quot;/cnt&quot; Then
345 List(a,2) = Filename
346 Else
347 m = 2
348 For n = 0 To 3
349 sField = Properties(n).Value
350 sFieldList() = ArrayoutofString(sField, &quot; &quot;, MaxIndex)
351 For s = 0 To MaxIndex
352 If m &lt; 6 Then
353 List(a,m) = sFieldList(s) &amp; &quot;.gif&quot;
354 Else
355 List(a,m) = sFieldList(s)
356 End If
357 m = m + 1
358 Next s
359 Next n
360 List(a,8) = FileName
361 End If
362 a = a + 1
363 End If
364 Next i
365 If sFileFilter = &quot;/cnt&quot; Then
366 ReDim Preserve List(a-1,2) as String
367 Else
368 ReDim Preserve List(a-1,8) as String
369 End If
370 If Not bItemfound Then
371 MsgBox(WebWiz_gErrContentNotFound$, 16, WebWiz_gWizardName$)
372 DisposeDocument(oBaseDocument)
373 Stop
374 End If
375 getListBoxArrays = List()
376 End Function
379 Sub SetGraphic(sWhich, sGraphicText as String)
380 Dim oLocCursor as Object
381 Dim oGraphic as Object
382 Dim bGetGraphic as Boolean
383 oBookmarks = oBaseDocument.BookMarks
384 If oBookmarks.HasbyName(sWhich)Then
385 oBookMark = oBookmarks.GetbyName(sWhich)
386 oLocCursor = oBookMark.Anchor.Text.CreateTextCursorByRange(oBookMark.Anchor)
387 oGraphic = oBaseDocument.CreateInstance(&quot;com.sun.star.text.GraphicObject&quot;)
388 oLocCursor.GoRight(3,True)
389 oGraphic.AnchorType = 1
390 oGraphic.GraphicURL = ConverttoURL(sGraphicText)
391 oLocCursor.Text.InsertTextContent(oLocCursor, oGraphic, True)
392 oGraphic.Name = sWhich
393 ElseIf oBaseDocument.GraphicObjects.HasbyName(sWhich) Then
394 oGraphic = oBaseDocument.GraphicObjects.GetByName(sWhich)
395 oGraphic.GraphicUrl = sGraphicText
396 End If
397 End Sub
400 Sub CheckControls(oDrawPage as Object)
401 Dim aForm as Object
402 Dim m,n as integer
403 Dim lColor as Long
404 Dim oControl as Object
405 lColor = oBaseDocument.StyleFamilies.GetbyName(&quot;ParagraphStyles&quot;).GetByName(&quot;Standard&quot;).CharColor
406 &apos;SearchFor all possible Controls
407 For n = 0 to oDrawPage.Forms.Count - 1
408 aForm = oDrawPage.Forms(n)
409 For m = 0 to aForm.Count-1
410 oControl = aForm.GetbyIndex(m)
411 oControl.TextColor = lColor
412 Next
413 Next
414 End Sub
417 Sub RepaintHeaderPreview()
418 Dim Bitmap As Object
419 Dim sBitmapPath as String
420 sBitmapPath = SOBitmapPath &amp; &quot;webwizard.bmp&quot;
421 WebWzrd.ImagePreview.ImageURL = sBitmapPath
422 End Sub
425 Sub ToggleDialogControls(ByVal bDoEnable as Boolean, Optional FocusControlName as String)
426 If Not IsNull(DialogModel) Then
427 DialogModel.Enabled = bDoEnable
428 If bDoEnable Then
429 &apos; Enable Controls referring to Background graphic only when this Property is set
430 bDoEnable = bWithBackGraphic
431 ToggleOptionButtons(DialogModel, bDoEnable)
432 MainDialog.GetControl(FocusControlName).SetFocus()
433 End If
434 End If
435 End Sub
436 </script:module>