1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
10 #include <comphelper/processfactory.hxx>
11 #include <cppuhelper/bootstrap.hxx>
12 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
13 #include <com/sun/star/lang/XInitialization.hpp>
14 #include <com/sun/star/registry/XSimpleRegistry.hpp>
15 #include <com/sun/star/ucb/UniversalContentBroker.hpp>
16 #include <com/sun/star/uno/RuntimeException.hpp>
18 #include <vcl/vclmain.hxx>
19 #include <vcl/layout.hxx>
20 #include <vcl/gdimtf.hxx>
21 #include <vcl/wmf.hxx>
23 #include <comphelper/diagnose_ex.hxx>
24 #include <tools/urlobj.hxx>
25 #include <tools/stream.hxx>
26 #include <tools/vcompat.hxx>
27 #include <vcl/svapp.hxx>
28 #include <vcl/wrkwin.hxx>
29 #include <vcl/virdev.hxx>
30 #include <sal/log.hxx>
31 #include <osl/file.hxx>
32 #include <osl/process.h>
33 #include <framework/desktop.hxx>
34 #include <i18nlangtag/languagetag.hxx>
35 #include <i18nlangtag/mslangid.hxx>
43 class DemoMtfWin
: public WorkWindow
48 explicit DemoMtfWin(const OUString
& rFileName
)
49 : WorkWindow(nullptr, WB_APP
| WB_STDWORK
)
51 maFileName
= rFileName
;
54 virtual void Paint(vcl::RenderContext
& rRenderContext
, const tools::Rectangle
& rRect
) override
;
59 void DemoMtfWin::Paint(vcl::RenderContext
& rRenderContext
, const tools::Rectangle
& rRect
)
62 SvFileStream
aFileStream(maFileName
, StreamMode::READ
);
64 if (aFileStream
.IsOpen())
66 ReadWindowMetafile(aFileStream
, aMtf
);
70 Application::Abort("Can't read metafile " + aFileStream
.GetFileName());
73 aMtf
.Play(*GetOutDev(), aMtf
.GetActionSize());
77 WorkWindow::Paint(rRenderContext
, rRect
);
82 class DemoMtfApp
: public Application
84 VclPtr
<DemoMtfWin
> mpWin
;
87 static void showHelp()
89 std::cerr
<< "Usage: mtfdemo --help | FILE | -d FILE" << std::endl
;
90 std::cerr
<< "A VCL test app that displays Windows metafiles or dumps metaactions." << std::endl
;
91 std::cerr
<< "If you want to dump as metadump.xml, use -d before FILE." << std::endl
;
102 virtual int Main() override
106 mpWin
= VclPtr
<DemoMtfWin
>::Create(maFileName
);
107 mpWin
->SetText("Display metafile");
111 Application::Execute();
112 mpWin
.disposeAndClear();
114 catch (const css::uno::Exception
&)
116 TOOLS_WARN_EXCEPTION("vcl.app", "Fatal");
119 catch (const std::exception
& e
)
121 SAL_WARN("vcl.app", "Fatal: " << e
.what());
128 uno::Reference
<lang::XMultiServiceFactory
> xMSF
;
131 LanguageTag::setConfiguredSystemLanguage(MsLangId::getSystemLanguage());
135 const sal_uInt16 nCmdParams
= GetCommandLineParamCount();
136 OUString aArg
, aFilename
;
137 bool bDumpXML
= false;
146 aArg
= GetCommandLineParam(0);
148 if (aArg
== "--help" || aArg
== "-h")
153 else if (nCmdParams
> 1 && (aArg
== "--dump" || aArg
== "-d"))
155 aFilename
= GetCommandLineParam(1);
162 OUString sWorkingDir
, sFileUrl
;
163 osl_getProcessWorkingDir(&sWorkingDir
.pData
);
164 osl::FileBase::RC rc
= osl::FileBase::getFileURLFromSystemPath(aFilename
, sFileUrl
);
165 if (rc
== osl::FileBase::E_None
)
167 rc
= osl::FileBase::getAbsoluteFileURL(sWorkingDir
, sFileUrl
, maFileName
);
168 if (rc
!= osl::FileBase::E_None
)
170 throw css::uno::RuntimeException("Can not make absolute: " + aFilename
);
175 throw css::uno::RuntimeException("Can not get file url from system path: " + aFilename
);
178 uno::Reference
<uno::XComponentContext
> xComponentContext
179 = ::cppu::defaultBootstrap_InitialComponentContext();
180 xMSF
.set(xComponentContext
->getServiceManager(), uno::UNO_QUERY
);
182 Application::Abort("Bootstrap failure - no service manager");
184 ::comphelper::setProcessServiceFactory(xMSF
);
189 SvFileStream
aFileStream(maFileName
, StreamMode::READ
);
191 if (aFileStream
.IsOpen())
193 ReadWindowMetafile(aFileStream
, aMtf
);
197 throw css::uno::RuntimeException("Can't read metafile " + aFileStream
.GetFileName());
200 OUString sAbsoluteDumpUrl
, sDumpUrl
;
201 rc
= osl::FileBase::getFileURLFromSystemPath("metadump.xml", sDumpUrl
);
202 if (rc
== osl::FileBase::E_None
)
204 rc
= osl::FileBase::getAbsoluteFileURL(sWorkingDir
, sDumpUrl
, sAbsoluteDumpUrl
);
205 if (rc
!= osl::FileBase::E_None
)
207 throw css::uno::RuntimeException("Can not make absolute: metadump.xml");
212 throw css::uno::RuntimeException("Can not get file url from system path: metadump.xml");
215 aMtf
.dumpAsXml(rtl::OUStringToOString(sAbsoluteDumpUrl
, RTL_TEXTENCODING_UTF8
).getStr());
216 std::cout
<< "Dumped metaactions as metadump.xml" << std::endl
;
217 framework::getDesktop(::comphelper::getProcessComponentContext())->terminate();
218 framework::getDesktop(::comphelper::getProcessComponentContext())->disposing();
223 catch (const uno::Exception
&e
)
225 Application::Abort("Bootstrap exception " + e
.Message
);
229 void DeInit() override
231 framework::getDesktop(::comphelper::getProcessComponentContext())->terminate();
232 framework::getDesktop(::comphelper::getProcessComponentContext())->disposing();
234 ::comphelper::setProcessServiceFactory(nullptr);
241 void vclmain::createApplication()
243 static DemoMtfApp aApp
;
246 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */