home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 2002 November / VPR0211A.ISO / OPENOFFICE / f_0205 / HtmlAutoPilotBasic.xba < prev    next >
Extensible Markup Language  |  2001-11-19  |  14KB  |  414 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(8, MaxStyles) as String
  21.  
  22. Public Layout$(2, MaxLayouts)
  23.  
  24. Public TextureDir$, BulletDir$, GraphicsDir$, GalleryDir$, PhotosDir$
  25. Public CurrentBullet$, CurrentPrev$, CurrentNext$, CurrentHome$, CurrentTop$
  26. Public FileStr as String
  27.  
  28. Public WebWiz_gWizardName$, WebWiz_gErrContentNotFound$, WebWiz_gErrStyleNotFound$
  29. Public WebWiz_gErrMainTemplateError$, WebWiz_gErrWhileReloading$
  30. Public WebWiz_gErrWhileLoadStyles$, WebWiz_gErrMsg$, WebWiz_gErrMainDocumentError$
  31.  
  32. Public ProgressBar as Object
  33. Public ProgressValue As Long
  34. Public oBaseDocument as Object
  35. Public oViewCursor as Object
  36. Public oViewSettings as Object
  37. Public NoArgs as New com.sun.star.beans.PropertyValue
  38.  
  39. Public oCursor as Object
  40. Public oBookmarks as Object
  41. Public oBookMark as Object
  42.  
  43. Public oUcb as Object
  44. Public MainDialog as Object
  45. Public DialogModel as Object
  46.  
  47.  
  48. Sub Main
  49. Dim RetValue
  50. 'On Local Error Goto GlobalErrorHandler
  51.     Dim SOBitmapPath,sBitmapPath as String
  52.     BasicLibraries.LoadLibrary("Tools")
  53.     oUcb = createUnoService("com.sun.star.ucb.SimpleFileAccess")
  54.     oBaseDocument = StarDesktop.LoadComponentFromURL("private:factory/swriter/web", "_blank", 0, NoArgs())
  55.     oViewSettings = oBaseDocument.CurrentController.ViewSettings
  56.     oViewCursor = oBaseDocument.GetCurrentController.ViewCursor
  57.     ProgressBar = oBaseDocument.GetCurrentController.GetFrame.CreateStatusIndicator
  58.     ProgressBar.Start("", 100)
  59.     SetProgressValue(2)
  60.     oBaseDocument.LockControllers
  61.     oViewSettings.ShowTableBoundaries = False
  62.     oViewSettings.ShowTextBoundaries = False
  63.     MainDialog = LoadDialog("WebWizard","WebWzrd")
  64.     DialogModel = MainDialog.Model
  65.     LoadLanguage
  66.     SetProgressValue(10)
  67.     GetPaths()
  68.     NumberofLayouts = FillupWebListbox(oUcb, "/cnt", MainDialog, "lbTemplate", Layout$())
  69.     SetProgressValue(30)
  70.     GetCurIndex(DialogModel.lbTemplate, Layout(),NumberofLayouts,2)
  71.     oCursor = oBasedocument.Text.CreateTextCursor
  72.     oCursor.InsertDocumentfromURL(FileStr, NoArgs())
  73.     SetProgressValue(40)
  74.     NumberofStyles = FillupWebListbox(oUcb, "/stl", MainDialog, "lbStyles", Style())
  75.     SetProgressValue(50)
  76.     LoadWebPageStyles(oBaseDocument)
  77.     SetProgressValue(98)
  78.     SetProgressValue(0)
  79.     oBaseDocument.UnlockControllers    
  80.     SOBitmapPath = GetOfficeSubPath("Template", "wizard/bitmap")
  81.     sBitmapPath = SOBitmapPath & "webwizard.bmp"
  82.     DialogModel.ImagePreview.ImageURL = sBitmapPath
  83.     ToggleOptionButtons(DialogModel, bWithBackGraphic)
  84.     MainDialog.GetControl("lbTemplate").SetFocus()
  85.     DialogModel.cbGoOn.DefaultButton = True
  86.     RetValue = MainDialog.Execute
  87.     Select Case RetValue
  88.         Case 0
  89.             MainDialog.Dispose()
  90.             oBaseDocument.Dispose()
  91.         Case 1
  92.             EndDialog()
  93.             MainDialog.Dispose()
  94.     End Select
  95. GLOBALERRORHANDLER:
  96.     If Err <> 0 Then
  97.         MsgBox (WebWiz_gErrMsg$, 16, WebWiz_gWizardName$)
  98.         CancelHTMLWizard()
  99.     End If
  100. End Sub
  101.  
  102.  
  103. Function SetProgressValue(iValue as Integer)
  104.     If iValue = 0 Then
  105.         ProgressBar.End
  106.     End If
  107.     ProgressValue = iValue
  108.     ProgressBar.Value = iValue
  109. End Function
  110.  
  111.  
  112. Sub ReloadCurrentDocument()
  113. Dim OldDocIndex as Integer
  114. On Local Error Goto ErrorOcurred
  115.     OldDocIndex = CurDocIndex
  116.     CurDocIndex = GetCurIndex(DialogModel.lbTemplate, Layout(), NumberofLayouts%, 2)
  117.     If OldDocIndex <> CurDocIndex Then
  118.         oBaseDocument.LockControllers
  119.         ToggleDialogControls(False)
  120.         oCursor = oBaseDocument.Text.CreateTextCursor()
  121.         oCursor.GotoStart(False)
  122.         oCursor.GotoEnd(True)
  123.         oCursor.SetAllPropertiesToDefault()
  124.         oCursor.InsertDocumentfromURL(FileStr, NoArgs())
  125.         SetBulletAndGraphics
  126.         CheckControls(oBaseDocument.DrawPage)
  127.     ErrorOcurred:
  128.         If Err <> 0 Then
  129.             MsgBox(WebWiz_gErrWhileReloading$, 16, WebWiz_gWizardName$)
  130.         End If
  131.         oBaseDocument.UnlockControllers
  132.         oViewCursor.GotoStart(False)
  133.         ToggleDialogControls(True, "lbTemplate")
  134.     End If
  135. End Sub
  136.  
  137.  
  138.  
  139. Sub LoadWebPageStyles()
  140. Dim OldWebPageIndex as Integer
  141.     OldWebPageIndex = CurWebPageIndex
  142.     CurWebPageIndex = GetCurIndex(DialogModel.lbStyles, Style(), NumberofStyles%,8)
  143.     If OldWebPageIndex <> CurWebPageIndex Then
  144.         ToggleDialogControls(False)
  145.         oBaseDocument.LockControllers
  146.         bWithBackGraphic = LoadNewStyles(oBaseDocument, DialogModel, CurWebPageIndex, FileStr, Style(), TextureDir)
  147.         CurrentBullet$ = BulletDir + Style(6, CurWebPageIndex)
  148.         CurrentPrev$ = GraphicsDir + Style(2, CurWebPageIndex)
  149.         CurrentNext$ = GraphicsDir + Style(3, CurWebPageIndex)
  150.         CurrentHome$ = GraphicsDir + Style(4, CurWebPageIndex)
  151.         CurrentTop$ = GraphicsDir + Style(5, CurWebPageIndex)
  152.         With oBaseDocument.DocumentInfo
  153.             .GetUserFieldValue(0) = ExtractGraphicNames(CurWebPageIndex,2)
  154.             .GetUserFieldValue(1) = ExtractGraphicNames(CurWebPageIndex, 4)
  155.             .GetUserFieldValue(2) = Style(6, CurWebPageIndex)       ' Bullet
  156.             .GetUserFieldValue(3) = Style(7, CurWebPageIndex)       ' Background
  157.         End With
  158.         SetBulletAndGraphics()
  159.         CheckControls(oBaseDocument.DrawPage)
  160.         oViewCursor.GotoStart(False)
  161.         oBaseDocument.UnlockControllers
  162.         ToggleDialogControls(True, "lbStyles")
  163.     End If
  164. End Sub
  165.  
  166.  
  167. Function ExtractGraphicNames(CurIndex as Integer, i as Integer) as String
  168. Dim FieldValue as String
  169.     FieldValue = GetFileNameWithoutExtension(Style(i,CurIndex))
  170.     FieldValue = FieldValue & " " &    GetFileNameWithoutExtension(Style(i+1,CurIndex))
  171.     ExtractGraphicNames = FieldValue
  172. End Function
  173.  
  174.  
  175. Sub SetBulletAndGraphics
  176.     SetGraphic("Prev", CurrentPrev)
  177.     SetGraphic("Next", CurrentNext)
  178.     SetGraphic("Home", CurrentHome)
  179.     SetGraphic("Top", CurrentTop)
  180.     SetBulletGraphics(CurrentBullet)
  181.     SetGraphicsToOriginalSize()
  182. End Sub
  183.  
  184.  
  185. Sub SetGraphicsToOriginalSize()
  186. Dim oGraphics as Object
  187. Dim oGraphic as Object
  188. Dim i as Integer
  189. Dim aActSize as New com.sun.star.awt.Size
  190.     oGraphics = oBaseDocument.GraphicObjects
  191.     For i = 0 To oGraphics.Count-1
  192.         oGraphic = oGraphics.GetByIndex(i)
  193.         aActSize = oGraphic.ActualSize
  194.         If aActSize.Height > 0 And aActSize.Width > 0 Then
  195.             oGraphic.SetSize(aActSize)
  196.         End If
  197.     Next i
  198. End Sub
  199.  
  200.  
  201. Sub EndDialog()
  202.     If DialogModel.chkSaveasTemplate.State = 1 Then
  203.         ' Generating template? Set events later!
  204.         AttachBasicMacroToEvent(oBaseDocument,"OnNew", "WebWizard.HtmlAutoPilotBasic.SetEvent()")
  205.         ' Call the Store template dialog
  206.         DispatchSlot(5538)
  207.         AttachBasicMacroToEvent(oBaseDocument,"OnNew", "")
  208.     End If
  209.     SetEvent()
  210. End Sub
  211.  
  212.  
  213. Sub SetEvent()
  214. Dim oDocument as Object
  215. ' This sub links the events OnSaveDone and OnSaveAsDone to the procedure
  216. ' CopyGraphics. It is invoked when a document is created, either directly
  217. ' from the AutoPilot or from a template. It is not possible to set these
  218. ' links for the template created by the AutoPilot because then it is not
  219. ' possible to modify the template.
  220.     BasicLibraries.LoadLibrary("Tools")
  221.     oDocument = ThisComponent
  222.     AttachBasicMacroToEvent(oDocument,"OnSaveDone", "WebWizard.HtmlAutoPilotBasic.CopyGraphics()")
  223.     AttachBasicMacroToEvent(oDocument,"OnSaveAsDone", "WebWizard.HtmlAutoPilotBasic.CopyGraphics()")    
  224. End Sub
  225.  
  226.  
  227.  
  228. Sub CopyGraphics
  229. ' This sub copies all the graphics used in the document to the same directory the
  230. ' document has been copied into and changes the graphics links in the document.
  231. Dim oGraphicObjects as Object
  232. Dim oGraphic as Object
  233. Dim i as Integer
  234. Dim SavePath as String
  235.     BasicLibraries.LoadLibrary("Tools")
  236.     oUcb = createUnoService("com.sun.star.ucb.SimpleFileAccess")
  237.     GetPaths()
  238.     oBaseDocument = ThisComponent
  239. '    oBaseDocument.LockControllers()
  240.  
  241.     ' Note: The sub DirectoryNameoutofPath should be change, so that the last character is a slash
  242.     SavePath = DirectoryNameoutofPath(oBaseDocument.Url, "/") & "/"
  243.  
  244.     oGraphicObjects = oBaseDocument.GraphicObjects
  245.     For i = 0 to oGraphicObjects.Count-1
  246.         oGraphic = oGraphicObjects.GetbyIndex(i)
  247.         oGraphic.GraphicUrl = CopyFile(oGraphic.GraphicURL, SavePath)
  248.     Next i
  249.  
  250.     ChangeBackGraphicUrl(SavePath)
  251.     
  252.     BulletUrlsToSavePath(SavePath)
  253.  
  254.     With oBaseDocument.DocumentInfo
  255.         .GetUserFieldValue(0) = ""
  256.         .GetUserFieldValue(1) = ""
  257.         .GetUserFieldValue(2) = ""
  258.         .GetUserFieldValue(3) = ""
  259.     End With
  260.  
  261.     AttachBasicMacroToEvent(oBaseDocument,"OnSaveDone", "")
  262.     AttachBasicMacroToEvent(oBaseDocument,"OnSaveAsDone", "")
  263.     AttachBasicMacroToEvent(oBaseDocument,"OnNew", "")
  264.     oBaseDocument.Store
  265. '    oBaseDocument.UnlockControllers()
  266. End Sub
  267.  
  268.         
  269. Function CopyFile(ByVal SourceUrl as String, TargetDir as String)
  270. Dim sFileName as String
  271. Dim sNewFileUrl as String
  272.     sFileName = FileNameoutofPath(SourceUrl)
  273.     sNewFileUrl = TargetDir & sFileName
  274.     oUcb.Copy(SourceUrl, sNewFileUrl)
  275.     CopyFile() = sFileName
  276. End Function
  277.  
  278.  
  279.  
  280. Function FillupWebListbox(oUcb as Object, sFileFilter as String, oDialog as Object, ListboxName as String, List() as String)
  281. Dim oDocInfo as Object
  282. Dim oListboxControl as Object
  283. Dim Description as String
  284. Dim sField as String
  285. Dim sFieldList() as String
  286. Dim bItemFound as Boolean
  287. Dim MaxIndex as Integer
  288. Dim DirContent() as String
  289. Dim FileName as String
  290. Dim TemplatePath as String
  291. Dim FilterLen as Integer
  292. Dim i as Integer
  293. Dim m as Integer
  294. Dim n as Integer
  295. Dim s as Integer
  296. Dim a as Integer
  297. Dim SelList(0) as Integer
  298. Dim LocMaxIndex as Integer
  299. Dim InfoNames()
  300.     oListboxControl = oDialog.GetControl(ListboxName)
  301.     oDocInfo = CreateUnoService("com.sun.star.document.DocumentProperties")
  302.     FilterLen = Len(sFileFilter)
  303.     bItemFound = False
  304.     TemplatePath = GetOfficeSubPath("Template", "wizard/web/")
  305.     DirContent() = oUcb.GetFolderContents(TemplatePath,True)
  306.         
  307.     LocMaxIndex = Ubound(DirContent())
  308.         
  309.     Dim SortList(LocMaxIndex,1)
  310.     For i = 0 to LocMaxIndex
  311.         SortList(i,0) = DirContent(i)
  312.         SortList(i,1) = RetrieveDocTitle(oDocInfo, DirContent(i))
  313.     Next i
  314.     SortList() = BubbleSortList(SortList(),True)
  315.     For i = 0 to LocMaxIndex
  316.         DirContent(i) = SortList(i,0)
  317.     Next i
  318.     a = 0
  319.     For i = 0 To LocMaxIndex
  320.         FileName = DirContent(i)
  321.         If Instr(1,Filename, sFileFilter) Then
  322.             bItemFound = True
  323.             Description = RetrieveDocTitle(oDocInfo, FileName)
  324.             oDocInfo.Read(FileName)
  325.             InfoNames = oDocInfo.ElementNames()
  326.             oListboxControl.AddItem(Description,a)
  327.             a = a + 1
  328.             List(1,i) = Description
  329.             If sFileFilter = "/cnt" Then
  330.                 List(2,i) = Filename
  331.             Else
  332.                 m = 2
  333.                 For n = 0 To 3
  334.                     sField = oDocInfo.GetByName(InfoNames(n))
  335.                     sFieldList() = ArrayoutofString(sField, " ", MaxIndex)
  336.                     For s = 0 To MaxIndex
  337.                         If m < 6 Then
  338.                             List(m,i) = sFieldList(s) & ".gif"
  339.                         Else
  340.                             List(m,i) = sFieldList(s)
  341.                         End If
  342.                         m = m + 1
  343.                     Next s
  344.                 Next n
  345.                 List(8,i) = FileName
  346.             End If
  347.         End If
  348.     Next i
  349.     If Not bItemfound Then
  350.         MsgBox(WebWiz_gErrContentNotFound$ , 16, WebWiz_gWizardName$)
  351.         oBaseDocument.Dispose()
  352.         Stop
  353.     End If
  354.     SelList(0) = 0
  355.     oListboxControl.Model.SelectedItems() = SelList()
  356.     FillupWebListbox = i
  357. End Function
  358.  
  359.  
  360. Sub SetGraphic(sWhich, sGraphicText as String)
  361. Dim oLocCursor as Object
  362. Dim oGraphic as Object
  363. Dim bGetGraphic as Boolean
  364.     oBookmarks = oBaseDocument.BookMarks
  365.     If oBookmarks.HasbyName(sWhich)Then
  366.         oBookMark = oBookmarks.GetbyName(sWhich)
  367.         oLocCursor = oBookMark.Anchor.Text.CreateTextCursorByRange(oBookMark.Anchor)
  368.         oGraphic = oBaseDocument.CreateInstance("com.sun.star.text.GraphicObject")
  369.         oLocCursor.GoRight(3,True)
  370.         oGraphic.AnchorType = 1
  371.         oGraphic.GraphicURL = ConverttoURL(sGraphicText)
  372.         oLocCursor.Text.InsertTextContent(oLocCursor, oGraphic, True)
  373.         oGraphic.Name = sWhich
  374.     ElseIf oBaseDocument.GraphicObjects.HasbyName(sWhich) Then
  375.         oGraphic = oBaseDocument.GraphicObjects.GetByName(sWhich)
  376.         oGraphic.GraphicUrl = sGraphicText
  377.     End If
  378. End Sub
  379.  
  380.  
  381. Sub CheckControls(oDrawPage as Object)
  382. Dim aForm as Object
  383. Dim m,n as integer
  384. Dim lColor as Long
  385. Dim oControl as Object
  386.     lColor = oBaseDocument.StyleFamilies.GetbyName("ParagraphStyles").GetByName("Standard").CharColor
  387.     'SearchFor all possible Controls
  388.     For n = 0 to oDrawPage.Forms.Count - 1
  389.         aForm = oDrawPage.Forms(n)
  390.         For m = 0 to aForm.Count-1
  391.             oControl = aForm.GetbyIndex(m)
  392.             oControl.TextColor = lColor
  393.         Next
  394.     Next
  395. End Sub
  396.  
  397.  
  398. Sub RepaintHeaderPreview()
  399. Dim Bitmap As Object
  400. Dim sBitmapPath as String
  401.     sBitmapPath = SOBitmapPath & "webwizard.bmp"
  402.     WebWzrd.ImagePreview.ImageURL = sBitmapPath
  403. End Sub
  404.  
  405.  
  406. Sub ToggleDialogControls(ByVal bDoEnable as Boolean, Optional FocusControlName as String)
  407.     DialogModel.Enabled = bDoEnable
  408.     If bDoEnable Then
  409.         ' Enable Controls referring to Background graphic only when this Property is set
  410.         bDoEnable = bWithBackGraphic
  411.         ToggleOptionButtons(DialogModel, bDoEnable)
  412.         MainDialog.GetControl(FocusControlName).SetFocus()
  413.     End If
  414. End Sub</script:module>