home *** CD-ROM | disk | FTP | other *** search
/ Freelog 52 / Freelog052.iso / Dossier / OpenOffice / f_0218 / HtmlAutoPilotBasic.xba < prev    next >
Extensible Markup Language  |  2003-03-27  |  14KB  |  436 lines

  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">' Variables must be declared
  4. Option Explicit
  5.  
  6. Public CurDocIndex as Integer
  7. Public CurWebPageIndex as Integer
  8.  
  9.  
  10. Public bWithBackGraphic as Boolean
  11. Public oStyle as Object
  12. ' Maximum number of content templates, style templates and bullets
  13. Const MaxLayouts = 50
  14. Const MaxStyles = 100
  15. Const MaxBullets = 10
  16.  
  17. 'Public NumberOfLayouts%, NumberOfStyles%
  18.  
  19. ' Filled with title, previous, next, home, top, bullet, background, file name
  20. Public Style(MaxStyles, 8) as String
  21.  
  22. Public Layout$(MaxLayouts, 2)
  23.  
  24. Public TextureDir$, BulletDir$, GraphicsDir$, GalleryDir$, PhotosDir$
  25. Public SOBitmapPath as String
  26. Public CurrentBullet$, CurrentPrev$, CurrentNext$, CurrentHome$, CurrentTop$
  27. Public FileStr as String
  28.  
  29. Public WebWiz_gWizardName$, WebWiz_gErrContentNotFound$, WebWiz_gErrStyleNotFound$
  30. Public WebWiz_gErrMainTemplateError$, WebWiz_gErrWhileReloading$
  31. Public WebWiz_gErrWhileLoadStyles$, WebWiz_gErrMsg$, WebWiz_gErrMainDocumentError$
  32.  
  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
  39.  
  40. Public oCursor as Object
  41. Public oBookmarks as Object
  42. Public oBookMark as Object
  43.  
  44. Public oUcb as Object
  45. Public MainDialog as Object
  46. Public DialogModel as Object
  47.  
  48.  
  49. Sub Main
  50. Dim RetValue
  51. On Local Error Goto GlobalErrorHandler
  52.     BasicLibraries.LoadLibrary("Tools")
  53.     oUcb = createUnoService("com.sun.star.ucb.SimpleFileAccess")
  54.     oBaseDocument = StarDesktop.LoadComponentFromURL("private:factory/swriter/web", "_default", 0, NoArgs())
  55.     oViewSettings = oBaseDocument.CurrentController.ViewSettings
  56.     oViewCursor = oBaseDocument.GetCurrentController.ViewCursor
  57.     CurWebPageIndex = -1
  58.     ProgressBar = oBaseDocument.GetCurrentController.GetFrame.CreateStatusIndicator
  59.     ProgressBar.Start("", 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, "/cnt")
  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, "/stl")
  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 <> 0 Then
  94.         MsgBox (WebWiz_gErrMsg$, 16, WebWiz_gWizardName$)
  95.         DisposeDocument(oBaseDocument)
  96.         RESUME EXITWIZARD
  97. EXITWIZARD:
  98.     End If
  99. End Sub
  100.  
  101.  
  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
  109.  
  110.  
  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 <> 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 <> 0 Then
  128.             MsgBox(WebWiz_gErrWhileReloading$, 16, WebWiz_gWizardName$)
  129.         End If
  130.         oBaseDocument.UnlockControllers
  131.         oViewCursor.GotoStart(False)
  132.         ToggleDialogControls(True, "lbTemplate")
  133.     End If
  134. End Sub
  135.  
  136.  
  137.  
  138.  
  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 <> 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.DocumentInfo
  157.             .GetUserFieldValue(0) = ExtractGraphicNames(CurWebPageIndex,2)
  158.             .GetUserFieldValue(1) = ExtractGraphicNames(CurWebPageIndex, 4)
  159.             .GetUserFieldValue(2) = Style(CurWebPageIndex, 6)       ' Bullet
  160.             .GetUserFieldValue(3) = Style(CurWebPageIndex, 7)       ' Background
  161.         End With
  162.         SetBulletAndGraphics()
  163.         CheckControls(oBaseDocument.DrawPage)
  164.         oViewCursor.GotoStart(False)
  165.         oBaseDocument.UnlockControllers
  166.         ToggleDialogControls(True, "lbStyles")
  167.     End If
  168. End Sub
  169.  
  170.  
  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 & " " &    GetFileNameWithoutExtension(Style(CurIndex, i+1))
  175.     ExtractGraphicNames = FieldValue
  176. End Function
  177.  
  178.  
  179. Sub SetBulletAndGraphics
  180.     SetGraphic("Prev", CurrentPrev)
  181.     SetGraphic("Next", CurrentNext)
  182.     SetGraphic("Home", CurrentHome)
  183.     SetGraphic("Top", CurrentTop)
  184.     SetBulletGraphics(CurrentBullet)
  185.     SetGraphicsToOriginalSize()
  186. End Sub
  187.  
  188.  
  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 > 0 And aActSize.Width > 0 Then
  199.             oGraphic.SetSize(aActSize)
  200.         End If
  201.     Next i
  202. End Sub
  203.  
  204.  
  205. Sub EndDialog()
  206.     If DialogModel.chkSaveasTemplate.State = 1 Then
  207.         ' Generating template? Set events later!
  208.         AttachBasicMacroToEvent(oBaseDocument,"OnNew", "WebWizard.HtmlAutoPilotBasic.SetEvent()")
  209.         ' Call the Store template dialog
  210.         DispatchSlot(5538)
  211.         AttachBasicMacroToEvent(oBaseDocument,"OnNew", "")
  212.     End If
  213.     SetEvent()
  214. End Sub
  215.  
  216.  
  217. Sub SetEvent()
  218. Dim oDocument as Object
  219. ' This sub links the events OnSaveDone and OnSaveAsDone to the procedure
  220. ' CopyGraphics. It is invoked when a document is created, either directly
  221. ' from the AutoPilot or from a template. It is not possible to set these
  222. ' links for the template created by the AutoPilot because then it is not
  223. ' possible to modify the template.
  224.     BasicLibraries.LoadLibrary("Tools")
  225.     oDocument = ThisComponent
  226.     AttachBasicMacroToEvent(oDocument,"OnSaveDone", "WebWizard.HtmlAutoPilotBasic.CopyGraphics()")
  227.     AttachBasicMacroToEvent(oDocument,"OnSaveAsDone", "WebWizard.HtmlAutoPilotBasic.CopyGraphics()")
  228. End Sub
  229.  
  230.  
  231.  
  232. Sub CopyGraphics
  233. ' This sub copies all the graphics used in the document to the same directory the
  234. ' 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 SavePath as String
  239.     BasicLibraries.LoadLibrary("Tools")
  240.     oUcb = createUnoService("com.sun.star.ucb.SimpleFileAccess")
  241.     GetWebWizardPaths()
  242.     oBaseDocument = ThisComponent
  243. '    oBaseDocument.LockControllers()
  244.  
  245.     ' Note: The sub DirectoryNameoutofPath should be change, so that the last character is a slash
  246.     SavePath = DirectoryNameoutofPath(oBaseDocument.Url, "/") & "/"
  247.  
  248.     oGraphicObjects = oBaseDocument.GraphicObjects
  249.     For i = 0 to oGraphicObjects.Count-1
  250.         oGraphic = oGraphicObjects.GetbyIndex(i)
  251.         oGraphic.GraphicUrl = CopyFile(oGraphic.GraphicURL, SavePath)
  252.     Next i
  253.  
  254.     ChangeBackGraphicUrl(SavePath)
  255.  
  256.     BulletUrlsToSavePath(SavePath)
  257.  
  258.     With oBaseDocument.DocumentInfo
  259.         .GetUserFieldValue(0) = ""
  260.         .GetUserFieldValue(1) = ""
  261.         .GetUserFieldValue(2) = ""
  262.         .GetUserFieldValue(3) = ""
  263.     End With
  264.     AttachBasicMacroToEvent(oBaseDocument,"OnSaveDone", "")
  265.     AttachBasicMacroToEvent(oBaseDocument,"OnSaveAsDone", "")
  266.     AttachBasicMacroToEvent(oBaseDocument,"OnNew", "")
  267.     oBaseDocument.Store
  268. '    oBaseDocument.UnlockControllers()
  269. End Sub
  270.  
  271.  
  272. Function CopyFile(ByVal SourceUrl as String, TargetDir as String)
  273. Dim sFileName as String
  274. Dim sNewFileUrl as String
  275.     If oUcb.Exists(TargetDir) Then
  276.         If Len(TargetDir) > 2 Then
  277.             sFileName = FileNameoutofPath(SourceUrl)
  278.             sNewFileUrl = TargetDir & sFileName
  279.             oUcb.Copy(SourceUrl, sNewFileUrl)
  280.             CopyFile() = sNewFileUrl
  281.         End If
  282.     End If
  283. End Function
  284.  
  285.  
  286. Function getListBoxArrays(oUcb as Object, sFileFilter as String)
  287. Dim oDocInfo as Object
  288. Dim oListboxControl as Object
  289. Dim Description as String
  290. Dim sField as String
  291. Dim sFieldList() as String
  292. Dim bItemFound as Boolean
  293. Dim MaxIndex as Integer
  294. Dim DirContent() as String
  295. Dim FileName as String
  296. Dim TemplatePath as String
  297. Dim FilterLen as Integer
  298. Dim i as Integer
  299. Dim m as Integer
  300. Dim n as Integer
  301. Dim s as Integer
  302. Dim a as Integer
  303. Dim LocMaxIndex as Integer
  304. Dim InfoNames()
  305. Dim DimCount as Integer
  306. Dim sExtension as String
  307.     oDocInfo = CreateUnoService("com.sun.star.document.DocumentProperties")
  308.     FilterLen = Len(sFileFilter)
  309.     bItemFound = False
  310.     ' It has to be made sure that the TemplatePath <> ""
  311.     TemplatePath = GetOfficeSubPath("Template", "wizard/web/")
  312.     If TemplatePath = "" Then
  313.         Dim NullList()
  314.         getListBoxArrays() = NullList()
  315.         Exit Function
  316.     End If
  317.     DirContent() = oUcb.GetFolderContents(TemplatePath,True)
  318.     If sFileFilter = "/cnt" Then
  319.         DimCount = 2
  320.     Else
  321.         DimCount = 8
  322.     End If
  323.     LocMaxIndex = Ubound(DirContent())
  324.     Dim List(LocMaxIndex, DimCount) as String
  325.     Dim SortList(LocMaxIndex,1)
  326.     For i = 0 to LocMaxIndex
  327.         SortList(i,0) = DirContent(i)
  328.         SortList(i,1) = RetrieveDocTitle(oDocInfo, DirContent(i))
  329.     Next i
  330.     SortList() = BubbleSortList(SortList(),True)
  331.     For i = 0 to LocMaxIndex
  332.         DirContent(i) = SortList(i,0)
  333.     Next i
  334.     a = 0
  335.     For i = 0 To LocMaxIndex
  336.         FileName = DirContent(i)
  337.         sExtension = Ucase(GetFileNameExtension(FileName))
  338.         If Instr(1,Filename, sFileFilter) And sExtension = "STW" Then
  339.             bItemFound = True
  340.             Description = RetrieveDocTitle(oDocInfo, FileName)
  341.             oDocInfo.Read(FileName)
  342.             InfoNames = oDocInfo.ElementNames()
  343.             List(a,1) = Description
  344.             If sFileFilter = "/cnt" Then
  345.                 List(a,2) = Filename
  346.             Else
  347.                 m = 2
  348.                 For n = 0 To 3
  349.                     sField = oDocInfo.GetByName(InfoNames(n))
  350.                     sFieldList() = ArrayoutofString(sField, " ", MaxIndex)
  351.                     For s = 0 To MaxIndex
  352.                         If m < 6 Then
  353.                             List(a,m) = sFieldList(s) & ".gif"
  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 = "/cnt" 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
  377.  
  378.  
  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("com.sun.star.text.GraphicObject")
  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
  398.  
  399.  
  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("ParagraphStyles").GetByName("Standard").CharColor
  406.     '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
  415.  
  416.  
  417. Sub RepaintHeaderPreview()
  418. Dim Bitmap As Object
  419. Dim sBitmapPath as String
  420.     sBitmapPath = SOBitmapPath & "webwizard.bmp"
  421.     WebWzrd.ImagePreview.ImageURL = sBitmapPath
  422. End Sub
  423.  
  424.  
  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.             ' 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</script:module>
  436.