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