Samples.xba 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
  3. <!--
  4. * This file is part of the LibreOffice project.
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. *
  10. * This file incorporates work covered by the following license notice:
  11. *
  12. * Licensed to the Apache Software Foundation (ASF) under one or more
  13. * contributor license agreements. See the NOTICE file distributed
  14. * with this work for additional information regarding copyright
  15. * ownership. The ASF licenses this file to you under the Apache
  16. * License, Version 2.0 (the "License"); you may not use this file
  17. * except in compliance with the License. You may obtain a copy of
  18. * the License at http://www.apache.org/licenses/LICENSE-2.0 .
  19. -->
  20. <script:module xmlns:script="http://openoffice.org/2000/script" script:name="Samples" script:language="StarBasic">Option Explicit
  21. Const NumStyles = 18
  22. Const aTempFileName = &quot;Berend_Ilko_Tom_Stella_Volker.stc&quot;
  23. Dim oUcbObject as Object
  24. Public StylesDir as String
  25. Public StylesDialog as Object
  26. Public PathSeparator as String
  27. Public oFamilies as Object
  28. Public aOptions(0) as New com.sun.star.beans.PropertyValue
  29. Public sQueryPath as String
  30. Public NoArgs()as New com.sun.star.beans.PropertyValue
  31. Public aTempURL as String
  32. Public Files(100) as String
  33. &apos;--------------------------------------------------------------------------------------
  34. &apos;Calc Style Section starts here
  35. Sub ShowStyles
  36. &apos;This sub displays the style selection dialog if the current document is a calc document.
  37. Dim TemplateDir, ActFileTitle, DisplayDummy as String
  38. Dim sFilterName(0) as String
  39. Dim StyleNames() as String
  40. Dim LocalizedStyleNames(NumStyles,2) As String
  41. Dim LocalizedStyleName As String
  42. Dim t as Integer
  43. Dim MaxIndex as Integer
  44. Dim StyleNameDef As Variant
  45. BasicLibraries.LoadLibrary(&quot;Tools&quot;)
  46. If InitResources(&quot;&apos;Template&apos;&quot;) then
  47. oDocument = ThisComponent
  48. If oDocument.SupportsService(&quot;com.sun.star.sheet.SpreadsheetDocument&quot;) Then
  49. ToggleWindow(False)
  50. oUcbObject = createUnoService(&quot;com.sun.star.ucb.SimpleFileAccess&quot;)
  51. oFamilies = oDocument.StyleFamilies
  52. SaveCurrentStyles(oDocument)
  53. StylesDialog = LoadDialog(&quot;Template&quot;, &quot;DialogStyles&quot;)
  54. DialogModel = StylesDialog.Model
  55. TemplateDir = GetPathSettings(&quot;Template&quot;, False, 0)
  56. StylesDir = GetOfficeSubPath(&quot;Template&quot;, &quot;wizard/styles/&quot;)
  57. sQueryPath = GetOfficeSubPath(&quot;Template&quot;, &quot;../wizard/bitmap/&quot;)
  58. DialogModel.Title = GetResText(&quot;STYLES_0&quot;)
  59. DialogModel.cmdCancel.Label = GetResText(&quot;STYLES_2&quot;)
  60. DialogModel.cmdOk.Label = GetResText(&quot;STYLES_3&quot;)
  61. StyleNameDef = Array("(Standard)", "Autumn Leaves", "Be", "Black and White", "Blackberry Bush", "Blue Jeans", "Fifties Diner", "Glacier", "Green Grapes", "Marine", "Millennium", "Nature", "Neon", "Night", "PC Nostalgia", "Pastel", "Pool Party", "Pumpkin")
  62. For t = 0 to NumStyles - 1
  63. LocalizedStyleNames(t,0) = StyleNameDef(t)
  64. LocalizedStyleNames(t,1) = GetResText(&quot;STYLENAME_&quot; &amp; Trim(Str(t)))
  65. Next t
  66. Stylenames() = ReadDirectories(StylesDir, False, False, True,)
  67. MaxIndex = Ubound(Stylenames())
  68. For t = 0 to MaxIndex
  69. LocalizedStyleName = StringInMultiArray(LocalizedStyleNames(), StyleNames(t,1), 0, 1)
  70. If LocalizedStyleName &lt;&gt; "" Then
  71. StyleNames(t,1) = LocalizedStyleName
  72. End If
  73. Next t
  74. BubbleSortList(Stylenames(),True)
  75. Dim cStyles(MaxIndex)
  76. For t = 0 to MaxIndex
  77. Files(t) = StyleNames(t,0)
  78. cStyles(t) = StyleNames(t,1)
  79. Next t
  80. On Local Error Resume Next
  81. DialogModel.lbStyles.StringItemList() = cStyles()
  82. ToggleWindow(True)
  83. StylesDialog.Execute
  84. End If
  85. End If
  86. End Sub
  87. Sub SelectStyle
  88. &apos;This sub loads the specific styles from a style document and loads them into the
  89. &apos;current document.
  90. Dim StylePath as String
  91. Dim NewStyle as String
  92. Dim Position as Integer
  93. Position = DialogModel.lbStyles.SelectedItems(0)
  94. If Position &gt; -1 Then
  95. ToggleWindow(False)
  96. StylePath = Files(Position)
  97. aOptions(0).Name = &quot;OverwriteStyles&quot;
  98. aOptions(0).Value = true
  99. oFamilies.loadStylesFromURL(StylePath, aOptions())
  100. ToggleWindow(True)
  101. End If
  102. End Sub
  103. Sub SaveCurrentStyles(oDocument as Object)
  104. &apos;This sub stores the current document in the directory to hold temporary files.
  105. On Error Goto ErrorOccurred
  106. aTempURL = GetPathSettings(&quot;Temp&quot;, False)
  107. Dim aRightMost as String
  108. aRightMost = Right(aTempURL, 1)
  109. if aRightMost = &quot;/&quot; Then
  110. aTempURL = aTempURL &amp; aTempFileName
  111. Else
  112. aTempURL = aTempURL &amp; &quot;/&quot; &amp; aTempFileName
  113. End If
  114. While FileExists(aTempURL)
  115. aTempURL=Left(aTempURL,(Len(aTempURL)-4)) &amp; &quot;_1.stc&quot;
  116. Wend
  117. oDocument.storeToURL(aTempURL, NoArgs())
  118. Exit Sub
  119. ErrorOccurred:
  120. MsgBox(GetResText(&quot;STYLES_1&quot;), 16, GetResText(&quot;STYLES_0&quot;))
  121. On Local Error Goto 0
  122. End Sub
  123. Sub RestoreCurrentStyles
  124. &apos;This sub retrieves the styles from the temporarily save document
  125. ToggleWindow(False)
  126. On Local Error Goto NoFile
  127. If FileExists(aTempURL) Then
  128. aOptions(0).Name = &quot;OverwriteStyles&quot;
  129. aOptions(0).Value = true
  130. oFamilies.LoadStylesFromURL(aTempURL, aOptions())
  131. KillTempFile()
  132. End If
  133. StylesDialog.EndExecute
  134. ToggleWindow(True)
  135. NOFILE:
  136. If Err &lt;&gt; 0 Then
  137. Msgbox(&quot;Cannot load Document from &quot; &amp; aTempUrl, 64, GetProductname())
  138. End If
  139. On Local Error Goto 0
  140. End Sub
  141. Sub CloseStyleDialog
  142. KillTempFile()
  143. DialogExited = True
  144. StylesDialog.Endexecute
  145. End Sub
  146. Sub KillTempFile()
  147. If oUcbObject.Exists(aTempUrl) Then
  148. oUcbObject.Kill(aTempUrl)
  149. End If
  150. End Sub
  151. </script:module>