1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column:100 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "vbaapplication.hxx"
21 #include "vbafilterpropsfromformat.hxx"
22 #include "vbamailmerge.hxx"
23 #include "vbawordbasic.hxx"
25 #include <basic/sbx.hxx>
26 #include <basic/sbxvar.hxx>
27 #include <comphelper/processfactory.hxx>
28 #include <comphelper/propertyvalue.hxx>
29 #include <osl/file.hxx>
30 #include <sal/log.hxx>
31 #include <tools/urlobj.hxx>
33 #include <com/sun/star/text/XTextDocument.hpp>
34 #include <com/sun/star/util/thePathSettings.hpp>
35 #include <ooo/vba/word/XBookmarks.hpp>
36 #include <ooo/vba/word/XDocuments.hpp>
38 using namespace ::ooo::vba
;
39 using namespace ::com::sun::star
;
41 SwWordBasic::SwWordBasic(SwVbaApplication
* pApp
)
46 sal_Int32 SAL_CALL
SwWordBasic::getMailMergeMainDocumentType()
48 return SwVbaMailMerge::get(mpApp
->getParent(), mpApp
->getContext())->getMainDocumentType();
51 void SAL_CALL
SwWordBasic::setMailMergeMainDocumentType(sal_Int32 _mailmergemaindocumenttype
)
53 SwVbaMailMerge::get(mpApp
->getParent(), mpApp
->getContext())
54 ->setMainDocumentType(_mailmergemaindocumenttype
);
57 void SAL_CALL
SwWordBasic::FileOpen(const OUString
& Name
, const uno::Any
& ConfirmConversions
,
58 const uno::Any
& ReadOnly
, const uno::Any
& AddToMru
,
59 const uno::Any
& PasswordDoc
, const uno::Any
& PasswordDot
,
60 const uno::Any
& Revert
, const uno::Any
& WritePasswordDoc
,
61 const uno::Any
& WritePasswordDot
)
63 uno::Any aDocuments
= mpApp
->Documents(uno::Any());
65 uno::Reference
<word::XDocuments
> rDocuments
;
67 if (aDocuments
>>= rDocuments
)
68 rDocuments
->Open(Name
, ConfirmConversions
, ReadOnly
, AddToMru
, PasswordDoc
, PasswordDot
,
69 Revert
, WritePasswordDoc
, WritePasswordDot
, uno::Any(), uno::Any(),
70 uno::Any(), uno::Any(), uno::Any(), uno::Any(), uno::Any());
73 void SAL_CALL
SwWordBasic::FileSave()
75 uno::Reference
<frame::XModel
> xModel(mpApp
->getCurrentDocument(), uno::UNO_SET_THROW
);
76 dispatchRequests(xModel
, ".uno:Save");
79 void SAL_CALL
SwWordBasic::FileSaveAs(
80 const css::uno::Any
& Name
, const css::uno::Any
& Format
, const css::uno::Any
& /*LockAnnot*/,
81 const css::uno::Any
& /*Password*/, const css::uno::Any
& /*AddToMru*/,
82 const css::uno::Any
& /*WritePassword*/, const css::uno::Any
& /*RecommendReadOnly*/,
83 const css::uno::Any
& /*EmbedFonts*/, const css::uno::Any
& /*NativePictureFormat*/,
84 const css::uno::Any
& /*FormsData*/, const css::uno::Any
& /*SaveAsAOCELetter*/)
86 SAL_INFO("sw.vba", "WordBasic.FileSaveAs(Name:=" << Name
<< ",Format:=" << Format
<< ")");
88 uno::Reference
<frame::XModel
> xModel(mpApp
->getCurrentDocument(), uno::UNO_SET_THROW
);
90 // Based on SwVbaDocument::SaveAs2000.
96 osl::FileBase::getFileURLFromSystemPath(sFileName
, sURL
);
98 // Detect if there is no path then we need to use the current folder.
99 INetURLObject
aURL(sURL
);
100 sURL
= aURL
.GetMainURL(INetURLObject::DecodeMechanism::ToIUri
);
103 // Need to add cur dir ( of this document ) or else the 'Work' dir
104 sURL
= xModel
->getURL();
108 // Not path available from 'this' document. Need to add the 'document'/work directory then.
109 // Based on SwVbaOptions::getValueEvent()
110 uno::Reference
<util::XPathSettings
> xPathSettings
111 = util::thePathSettings::get(comphelper::getProcessComponentContext());
113 xPathSettings
->getPropertyValue("Work") >>= sPathUrl
;
114 // Path could be a multipath, Microsoft doesn't support this feature in Word currently.
115 // Only the last path is from interest.
116 // No idea if this crack is relevant for WordBasic or not.
117 sal_Int32 nIndex
= sPathUrl
.lastIndexOf(';');
120 sPathUrl
= sPathUrl
.copy(nIndex
+ 1);
123 aURL
.SetURL(sPathUrl
);
128 aURL
.Append(sFileName
);
130 sURL
= aURL
.GetMainURL(INetURLObject::DecodeMechanism::ToIUri
);
132 sal_Int32 nFileFormat
= word::WdSaveFormat::wdFormatDocument
;
133 Format
>>= nFileFormat
;
135 uno::Sequence aProps
{ comphelper::makePropertyValue("FilterName", css::uno::Any()),
136 comphelper::makePropertyValue("FileName", sURL
) };
138 setFilterPropsFromFormat(nFileFormat
, aProps
);
140 dispatchRequests(xModel
, ".uno:SaveAs", aProps
);
143 void SAL_CALL
SwWordBasic::FileClose(const css::uno::Any
& Save
)
145 uno::Reference
<frame::XModel
> xModel(mpApp
->getCurrentDocument(), uno::UNO_SET_THROW
);
148 if (Save
.hasValue() && (Save
>>= nSave
) && (nSave
== 0 || nSave
== 1))
151 // FIXME: Here I would much prefer to call VbaDocumentBase::Close() but not sure how to get at
152 // the VbaDocumentBase of the current document. (Probably it is easy and I haven't looked hard
155 // FIXME: Error handling. If there is no current document, return some kind of error? But for
156 // now, just ignore errors. This code is written to work for a very specific customer use case
157 // anyway, not for an arbitrary sequence of COM calls to the "VBA" API.
158 dispatchRequests(xModel
, ".uno:CloseDoc");
161 void SAL_CALL
SwWordBasic::ToolsOptionsView(
162 const css::uno::Any
& DraftFont
, const css::uno::Any
& WrapToWindow
,
163 const css::uno::Any
& PicturePlaceHolders
, const css::uno::Any
& FieldCodes
,
164 const css::uno::Any
& BookMarks
, const css::uno::Any
& FieldShading
,
165 const css::uno::Any
& StatusBar
, const css::uno::Any
& HScroll
, const css::uno::Any
& VScroll
,
166 const css::uno::Any
& StyleAreaWidth
, const css::uno::Any
& Tabs
, const css::uno::Any
& Spaces
,
167 const css::uno::Any
& Paras
, const css::uno::Any
& Hyphens
, const css::uno::Any
& Hidden
,
168 const css::uno::Any
& ShowAll
, const css::uno::Any
& Drawings
, const css::uno::Any
& Anchors
,
169 const css::uno::Any
& TextBoundaries
, const css::uno::Any
& VRuler
,
170 const css::uno::Any
& Highlight
)
172 SAL_INFO("sw.vba", "WordBasic.ToolsOptionsView("
174 << DraftFont
<< ", WrapToWindow:=" << WrapToWindow
175 << ", PicturePlaceHolders:=" << PicturePlaceHolders
176 << ", FieldCodes:=" << FieldCodes
<< ", BookMarks:=" << BookMarks
177 << ", FieldShading:=" << FieldShading
<< ", StatusBar:=" << StatusBar
178 << ", HScroll:=" << HScroll
<< ", VScroll:=" << VScroll
179 << ", StyleAreaWidth:=" << StyleAreaWidth
<< ", Tabs:=" << Tabs
180 << ", Spaces:=" << Spaces
<< ", Paras:=" << Paras
181 << ", Hyphens:=" << Hyphens
<< ", Hidden:=" << Hidden
182 << ", ShowAll:=" << ShowAll
<< ", Drawings:=" << Drawings
183 << ", Anchors:=" << Anchors
<< ", TextBoundaries:=" << TextBoundaries
184 << ", VRuler:=" << VRuler
<< ", Highlight:=" << Highlight
<< ")");
187 css::uno::Any SAL_CALL
SwWordBasic::WindowName(const css::uno::Any
& /*Number*/)
189 return css::uno::Any(mpApp
->getActiveSwVbaWindow()->getCaption());
192 css::uno::Any SAL_CALL
SwWordBasic::ExistingBookmark(const OUString
& Name
)
194 uno::Reference
<word::XBookmarks
> xBookmarks(mpApp
->getActiveDocument()->Bookmarks(uno::Any()),
196 return css::uno::Any(xBookmarks
.is() && xBookmarks
->Exists(Name
));
199 void SAL_CALL
SwWordBasic::MailMergeOpenDataSource(
200 const OUString
& Name
, const css::uno::Any
& Format
, const css::uno::Any
& ConfirmConversions
,
201 const css::uno::Any
& ReadOnly
, const css::uno::Any
& LinkToSource
,
202 const css::uno::Any
& AddToRecentFiles
, const css::uno::Any
& PasswordDocument
,
203 const css::uno::Any
& PasswordTemplate
, const css::uno::Any
& Revert
,
204 const css::uno::Any
& WritePasswordDocument
, const css::uno::Any
& WritePasswordTemplate
,
205 const css::uno::Any
& Connection
, const css::uno::Any
& SQLStatement
,
206 const css::uno::Any
& SQLStatement1
, const css::uno::Any
& OpenExclusive
,
207 const css::uno::Any
& SubType
)
209 mpApp
->getActiveDocument()->getMailMerge()->OpenDataSource(
210 Name
, Format
, ConfirmConversions
, ReadOnly
, LinkToSource
, AddToRecentFiles
,
211 PasswordDocument
, PasswordTemplate
, Revert
, WritePasswordDocument
, WritePasswordTemplate
,
212 Connection
, SQLStatement
, SQLStatement1
, OpenExclusive
, SubType
);
215 css::uno::Any SAL_CALL
SwWordBasic::AppMaximize(const css::uno::Any
& WindowName
,
216 const css::uno::Any
& State
)
218 SAL_INFO("sw.vba", "WordBasic.AppMaximize( WindowName:=" << WindowName
<< ", State:=" << State
);
220 // FIXME: Implement if necessary
221 return css::uno::Any(sal_Int32(0));
224 css::uno::Any SAL_CALL
SwWordBasic::DocMaximize(const css::uno::Any
& State
)
226 SAL_INFO("sw.vba", "WordBasic.DocMaximize(State:=" << State
<< ")");
228 // FIXME: Implement if necessary
229 return css::uno::Any(sal_Int32(0));
232 void SAL_CALL
SwWordBasic::AppShow(const css::uno::Any
& WindowName
)
234 SAL_INFO("sw.vba", "WordBasic.AppShow(WindowName:=" << WindowName
<< ")");
236 // FIXME: Implement if necessary
239 css::uno::Any SAL_CALL
SwWordBasic::AppCount()
241 SAL_INFO("sw.vba", "WordBasic.AppCount()");
243 // FIXME: Implement if necessary. Return a random number for now.
244 return css::uno::Any(sal_Int32(2));
247 void SAL_CALL
SwWordBasic::MsgBox(const OUString
& sPrompt
)
249 SbxArrayRef pArgs
= new SbxArray
;
250 SbxVariable
* pVar
= new SbxVariable();
251 pVar
->PutString(sPrompt
);
254 if (!executeRunTimeLibrary(u
"MsgBox", pArgs
.get()))
255 SAL_WARN("sw.vba", "failed to execute runtime library function MsgBox (" << sPrompt
<< ")");
258 void SAL_CALL
SwWordBasic::ScreenUpdating(const uno::Any
& On
)
262 mpApp
->setScreenUpdating(nOn
!= 0);
265 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */