Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / vcl / workben / mtfdemo.cxx
blob9e0ead81a10ee0247db6c28eba58b86f72bedddb
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
10 #include <comphelper/processfactory.hxx>
11 #include <comphelper/random.hxx>
12 #include <cppuhelper/bootstrap.hxx>
13 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
14 #include <com/sun/star/lang/XInitialization.hpp>
15 #include <com/sun/star/registry/XSimpleRegistry.hpp>
16 #include <com/sun/star/ucb/UniversalContentBroker.hpp>
18 #include <vcl/vclmain.hxx>
19 #include <vcl/layout.hxx>
20 #include <vcl/gdimtf.hxx>
21 #include <vcl/wmf.hxx>
23 #include <tools/urlobj.hxx>
24 #include <tools/stream.hxx>
25 #include <tools/vcompat.hxx>
26 #include <vcl/svapp.hxx>
27 #include <vcl/wrkwin.hxx>
28 #include <vcl/virdev.hxx>
30 #include <cstdlib>
32 using namespace com::sun::star;
34 using namespace css;
36 class DemoMtfWin : public WorkWindow
38 GDIMetaFile maMtf;
40 public:
41 explicit DemoMtfWin(const OUString& rFileName)
42 : WorkWindow(nullptr, WB_APP | WB_STDWORK)
44 SvFileStream aFileStream(rFileName, StreamMode::READ);
46 if (aFileStream.IsOpen())
48 ReadWindowMetafile(aFileStream, maMtf);
50 else
52 Application::Abort("Can't read metafile");
56 virtual void Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle& rRect) override;
59 void DemoMtfWin::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle& rRect)
61 maMtf.Play(this, maMtf.GetActionSize());
63 WorkWindow::Paint(rRenderContext, rRect);
66 class DemoMtfApp : public Application
68 VclPtr<DemoMtfWin> mpWin;
69 OUString maFileName;
71 static void showHelp()
73 fprintf(stderr, "Usage: mtfdemo --help | FILE\n");
74 fprintf(stderr, "A VCL test app that displays Windows metafiles\n");
75 std::exit(0);
78 public:
80 DemoMtfApp()
81 : mpWin(nullptr)
85 virtual int Main() override
87 try
89 mpWin = VclPtr<DemoMtfWin>::Create(maFileName);
90 mpWin->SetText("Display metafile");
92 mpWin->Show();
94 Application::Execute();
96 catch (const css::uno::Exception& e)
98 SAL_WARN("vcl.app", "Fatal exception: " << e.Message);
99 return 1;
101 catch (const std::exception& e)
103 SAL_WARN("vcl.app", "Fatal exception: " << e.what());
104 return 1;
106 return 0;
109 protected:
110 uno::Reference<lang::XMultiServiceFactory> xMSF;
111 void Init() override
115 const sal_uInt16 nCmdParams = GetCommandLineParamCount();
117 if (nCmdParams == 0)
118 showHelp();
119 else
121 OUString aArg = GetCommandLineParam(0);
123 if (aArg == "--help" || aArg == "-h")
124 showHelp();
125 else
126 maFileName = aArg;
129 uno::Reference<uno::XComponentContext> xComponentContext
130 = ::cppu::defaultBootstrap_InitialComponentContext();
131 xMSF.set(xComponentContext->getServiceManager(), uno::UNO_QUERY);
132 if(!xMSF.is())
133 Application::Abort("Bootstrap failure - no service manager");
135 ::comphelper::setProcessServiceFactory(xMSF);
137 catch (const uno::Exception &e)
139 Application::Abort("Bootstrap exception " + e.Message);
143 void DeInit() override
145 uno::Reference< lang::XComponent >(
146 comphelper::getProcessComponentContext(),
147 uno::UNO_QUERY_THROW)-> dispose();
148 ::comphelper::setProcessServiceFactory(nullptr);
154 void vclmain::createApplication()
156 static DemoMtfApp aApp;
159 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */