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