xref: /trunk/main/wizards/source/template/Samples.xba (revision 66b843ff8f1eedd2e69941f1ea52fa080f01ec28)
1cdf0e10cSrcweir<?xml version="1.0" encoding="UTF-8"?>
2cdf0e10cSrcweir<!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
3*3e02b54dSAndrew Rist<!--***********************************************************
4*3e02b54dSAndrew Rist *
5*3e02b54dSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
6*3e02b54dSAndrew Rist * or more contributor license agreements.  See the NOTICE file
7*3e02b54dSAndrew Rist * distributed with this work for additional information
8*3e02b54dSAndrew Rist * regarding copyright ownership.  The ASF licenses this file
9*3e02b54dSAndrew Rist * to you under the Apache License, Version 2.0 (the
10*3e02b54dSAndrew Rist * "License"); you may not use this file except in compliance
11*3e02b54dSAndrew Rist * with the License.  You may obtain a copy of the License at
12*3e02b54dSAndrew Rist *
13*3e02b54dSAndrew Rist *   http://www.apache.org/licenses/LICENSE-2.0
14*3e02b54dSAndrew Rist *
15*3e02b54dSAndrew Rist * Unless required by applicable law or agreed to in writing,
16*3e02b54dSAndrew Rist * software distributed under the License is distributed on an
17*3e02b54dSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18*3e02b54dSAndrew Rist * KIND, either express or implied.  See the License for the
19*3e02b54dSAndrew Rist * specific language governing permissions and limitations
20*3e02b54dSAndrew Rist * under the License.
21*3e02b54dSAndrew Rist *
22*3e02b54dSAndrew Rist ***********************************************************-->
23cdf0e10cSrcweir<script:module xmlns:script="http://openoffice.org/2000/script" script:name="Samples" script:language="StarBasic">Option Explicit
24cdf0e10cSrcweir
25cdf0e10cSrcweirConst SAMPLES = 1000
26cdf0e10cSrcweirConst STYLES = 1100
27cdf0e10cSrcweirConst aTempFileName = &quot;Berend_Ilko_Tom_Stella_Volker.stc&quot;
28cdf0e10cSrcweirPublic Const Twip = 425
29cdf0e10cSrcweirDim oUcbObject as Object
30cdf0e10cSrcweirPublic StylesDir as String
31cdf0e10cSrcweirPublic StylesDialog as Object
32cdf0e10cSrcweirPublic PathSeparator as String
33cdf0e10cSrcweirPublic oFamilies  as Object
34cdf0e10cSrcweirPublic aOptions(0) as New com.sun.star.beans.PropertyValue
35cdf0e10cSrcweirPublic sQueryPath as String
36cdf0e10cSrcweirPublic NoArgs()as New com.sun.star.beans.PropertyValue
37cdf0e10cSrcweirPublic aTempURL as String
38cdf0e10cSrcweir
39cdf0e10cSrcweirPublic Files(100) as String
40cdf0e10cSrcweir
41cdf0e10cSrcweir
42cdf0e10cSrcweir&apos;--------------------------------------------------------------------------------------
43cdf0e10cSrcweir&apos;Miscellaneous Section starts here
44cdf0e10cSrcweir
45cdf0e10cSrcweirFunction PrepareForEditing(Optional ByVal oDocument)
46cdf0e10cSrcweir&apos;This sub is called when sample documents are loaded (load event).
47cdf0e10cSrcweir&apos;It checks whether the documents is read-only, in which case it
48cdf0e10cSrcweir&apos;offers the user to create a new (writable) document using the original
49cdf0e10cSrcweir&apos;as a template.
50cdf0e10cSrcweirDim DocPath as String
51cdf0e10cSrcweirDim MMessage as String
52cdf0e10cSrcweirDim MTitle as String
53cdf0e10cSrcweirDim RValue as Integer
54cdf0e10cSrcweirDim oNewDocument as Object
55cdf0e10cSrcweirDim mFileProperties(1) as New com.sun.star.beans.PropertyValue
56cdf0e10cSrcweir    PrepareForEditing = NULL
57cdf0e10cSrcweir        BasicLibraries.LoadLibrary( &quot;Tools&quot; )
58cdf0e10cSrcweir    If InitResources(&quot;&apos;Template&apos;&quot;, &quot;tpl&quot;) then
59cdf0e10cSrcweir        If IsMissing(oDocument) Then
60cdf0e10cSrcweir            oDocument = ThisComponent
61cdf0e10cSrcweir        End If
62cdf0e10cSrcweir        If oDocument.IsReadOnly then
63cdf0e10cSrcweir            MMessage = GetResText(SAMPLES)
64cdf0e10cSrcweir            MTitle = GetResText(SAMPLES + 1)
65cdf0e10cSrcweir            RValue = Msgbox(MMessage, (128+48+1), MTitle)
66cdf0e10cSrcweir            If RValue = 1 Then
67cdf0e10cSrcweir                DocPath = oDocument.URL
68cdf0e10cSrcweir                mFileProperties(0).Name = &quot;AsTemplate&quot;
69cdf0e10cSrcweir                mFileProperties(0).Value = True
70cdf0e10cSrcweir                mFileProperties(1).Name = &quot;MacroExecutionMode&quot;
71cdf0e10cSrcweir                mFileProperties(1).Value = com.sun.star.document.MacroExecMode.USE_CONFIG
72cdf0e10cSrcweir
73cdf0e10cSrcweir                oNewDocument = StarDesktop.LoadComponentFromURL(DocPath,&quot;_default&quot;,0, mFileProperties())
74cdf0e10cSrcweir                PrepareForEditing() = oNewDocument
75cdf0e10cSrcweir                DisposeDocument(oDocument)
76cdf0e10cSrcweir            Else
77cdf0e10cSrcweir                PrepareForEditing() = NULL
78cdf0e10cSrcweir            End If
79cdf0e10cSrcweir        Else
80cdf0e10cSrcweir            PrepareForEditing() = oDocument
81cdf0e10cSrcweir        End If
82cdf0e10cSrcweir    End If
83cdf0e10cSrcweirEnd Function
84cdf0e10cSrcweir
85cdf0e10cSrcweir
86cdf0e10cSrcweir
87cdf0e10cSrcweir&apos;--------------------------------------------------------------------------------------
88cdf0e10cSrcweir&apos;Calc Style Section starts here
89cdf0e10cSrcweir
90cdf0e10cSrcweirSub ShowStyles
91cdf0e10cSrcweir&apos;This sub displays the style selection dialog if the current document is a calc document.
92cdf0e10cSrcweirDim TemplateDir, ActFileTitle, DisplayDummy as String
93cdf0e10cSrcweirDim sFilterName(0) as String
94cdf0e10cSrcweirDim StyleNames() as String
95cdf0e10cSrcweirDim t as Integer
96cdf0e10cSrcweirDim MaxIndex as Integer
97cdf0e10cSrcweir        BasicLibraries.LoadLibrary(&quot;Tools&quot;)
98cdf0e10cSrcweir    If InitResources(&quot;&apos;Template&apos;&quot;, &quot;tpl&quot;) then
99cdf0e10cSrcweir    oDocument = ThisComponent
100cdf0e10cSrcweir        If oDocument.SupportsService(&quot;com.sun.star.sheet.SpreadsheetDocument&quot;) Then
101cdf0e10cSrcweir            ToggleWindow(False)
102cdf0e10cSrcweir            oUcbObject = createUnoService(&quot;com.sun.star.ucb.SimpleFileAccess&quot;)
103cdf0e10cSrcweir            oFamilies = oDocument.StyleFamilies
104cdf0e10cSrcweir            SaveCurrentStyles(oDocument)
105cdf0e10cSrcweir            StylesDialog = LoadDialog(&quot;Template&quot;, &quot;DialogStyles&quot;)
106cdf0e10cSrcweir            DialogModel = StylesDialog.Model
107cdf0e10cSrcweir            TemplateDir = GetPathSettings(&quot;Template&quot;, False, 0)
108cdf0e10cSrcweir            StylesDir = GetOfficeSubPath(&quot;Template&quot;, &quot;wizard/styles/&quot;)
109cdf0e10cSrcweir            sQueryPath = GetOfficeSubPath(&quot;Template&quot;, &quot;../wizard/bitmap/&quot;)
110cdf0e10cSrcweir            DialogModel.Title = GetResText(STYLES)
111cdf0e10cSrcweir            DialogModel.cmdCancel.Label = GetResText(STYLES+2)
112cdf0e10cSrcweir            DialogModel.cmdOk.Label = GetResText(STYLES+3)
113cdf0e10cSrcweir            Stylenames() = ReadDirectories(StylesDir, False, False, True,)
114cdf0e10cSrcweir            MaxIndex = Ubound(Stylenames())
115cdf0e10cSrcweir            BubbleSortList(Stylenames(),True)
116cdf0e10cSrcweir            Dim cStyles(MaxIndex)
117cdf0e10cSrcweir            For t = 0 to MaxIndex
118cdf0e10cSrcweir                Files(t) = StyleNames(t,0)
119cdf0e10cSrcweir                cStyles(t) = StyleNames(t,1)
120cdf0e10cSrcweir            Next t
121cdf0e10cSrcweir            On Local Error Resume Next
122cdf0e10cSrcweir            DialogModel.lbStyles.StringItemList() = cStyles()
123cdf0e10cSrcweir            ToggleWindow(True)
124cdf0e10cSrcweir            StylesDialog.Execute
125cdf0e10cSrcweir        End If
126cdf0e10cSrcweir    End If
127cdf0e10cSrcweirEnd Sub
128cdf0e10cSrcweir
129cdf0e10cSrcweir
130cdf0e10cSrcweirSub SelectStyle
131cdf0e10cSrcweir&apos;This sub loads the specific styles from a style document and loads them into the
132cdf0e10cSrcweir&apos;current document.
133cdf0e10cSrcweirDim StylePath as String
134cdf0e10cSrcweirDim NewStyle as String
135cdf0e10cSrcweirDim Position as Integer
136cdf0e10cSrcweir    Position = DialogModel.lbStyles.SelectedItems(0)
137cdf0e10cSrcweir    If Position &gt; -1 Then
138cdf0e10cSrcweir        ToggleWindow(False)
139cdf0e10cSrcweir        StylePath = Files(Position)
140cdf0e10cSrcweir        aOptions(0).Name = &quot;OverwriteStyles&quot;
141cdf0e10cSrcweir        aOptions(0).Value = true
142cdf0e10cSrcweir        oFamilies.loadStylesFromURL(StylePath, aOptions())
143cdf0e10cSrcweir        ToggleWindow(True)
144cdf0e10cSrcweir    End If
145cdf0e10cSrcweirEnd Sub
146cdf0e10cSrcweir
147cdf0e10cSrcweir
148cdf0e10cSrcweirSub SaveCurrentStyles(oDocument as Object)
149cdf0e10cSrcweir&apos;This sub stores the current document in the user work directory
150cdf0e10cSrcweir    On Error Goto ErrorOcurred
151cdf0e10cSrcweir    aTempURL = GetPathSettings(&quot;Work&quot;, False)
152cdf0e10cSrcweir    Dim aRightMost as String
153cdf0e10cSrcweir    aRightMost = Right(aTempURL, 1)
154cdf0e10cSrcweir    if aRightMost = &quot;/&quot; Then
155cdf0e10cSrcweir        aTempURL = aTempURL &amp; aTempFileName
156cdf0e10cSrcweir    Else
157cdf0e10cSrcweir        aTempURL = aTempURL &amp; &quot;/&quot; &amp; aTempFileName
158cdf0e10cSrcweir    End If
159cdf0e10cSrcweir
160cdf0e10cSrcweir    While FileExists(aTempURL)
161cdf0e10cSrcweir        aTempURL=Left(aTempURL,(Len(aTempURL)-4)) &amp; &quot;_1.stc&quot;
162cdf0e10cSrcweir    Wend
163cdf0e10cSrcweir    oDocument.storeToURL(aTempURL, NoArgs())
164cdf0e10cSrcweir    Exit Sub
165cdf0e10cSrcweir
166cdf0e10cSrcweirErrorOcurred:
167cdf0e10cSrcweir    MsgBox(GetResText( STYLES+1 ), 16, GetResText( STYLES ))
168cdf0e10cSrcweir    On Local Error Goto 0
169cdf0e10cSrcweirEnd Sub
170cdf0e10cSrcweir
171cdf0e10cSrcweir
172cdf0e10cSrcweirSub RestoreCurrentStyles
173cdf0e10cSrcweir&apos;This sub retrieves the styles from the temporarily save document
174cdf0e10cSrcweir    ToggleWindow(False)
175cdf0e10cSrcweir    On Local Error Goto NoFile
176cdf0e10cSrcweir    If FileExists(aTempURL) Then
177cdf0e10cSrcweir        aOptions(0).Name = &quot;OverwriteStyles&quot;
178cdf0e10cSrcweir        aOptions(0).Value = true
179cdf0e10cSrcweir        oFamilies.LoadStylesFromURL(aTempURL, aOptions())
180cdf0e10cSrcweir        KillTempFile()
181cdf0e10cSrcweir    End If
182cdf0e10cSrcweir    StylesDialog.EndExecute
183cdf0e10cSrcweir    ToggleWindow(True)
184cdf0e10cSrcweirNOFILE:
185cdf0e10cSrcweir    If Err &lt;&gt; 0 Then
186cdf0e10cSrcweir        Msgbox(&quot;Cannot load Document from &quot; &amp; aTempUrl, 64, GetProductname())
187cdf0e10cSrcweir    End If
188cdf0e10cSrcweir    On Local Error Goto 0
189cdf0e10cSrcweirEnd Sub
190cdf0e10cSrcweir
191cdf0e10cSrcweir
192cdf0e10cSrcweirSub CloseStyleDialog
193cdf0e10cSrcweir    KillTempFile()
194cdf0e10cSrcweir    DialogExited = True
195cdf0e10cSrcweir    StylesDialog.Endexecute
196cdf0e10cSrcweirEnd Sub
197cdf0e10cSrcweir
198cdf0e10cSrcweir
199cdf0e10cSrcweirSub KillTempFile()
200cdf0e10cSrcweir    If oUcbObject.Exists(aTempUrl) Then
201cdf0e10cSrcweir        oUcbObject.Kill(aTempUrl)
202cdf0e10cSrcweir    End If
203cdf0e10cSrcweirEnd Sub
204cdf0e10cSrcweir
205cdf0e10cSrcweir</script:module>
206