summaryrefslogtreecommitdiff
path: root/vcl/workben/mtfdemo.cxx
blob: eb81c3363b166da1d4d714b7ef794d095a909bee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <comphelper/processfactory.hxx>
#include <cppuhelper/bootstrap.hxx>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/lang/XInitialization.hpp>
#include <com/sun/star/registry/XSimpleRegistry.hpp>
#include <com/sun/star/ucb/UniversalContentBroker.hpp>

#include <vcl/vclmain.hxx>
#include <vcl/layout.hxx>
#include <vcl/gdimtf.hxx>
#include <vcl/wmf.hxx>

#include <tools/diagnose_ex.h>
#include <tools/urlobj.hxx>
#include <tools/stream.hxx>
#include <tools/vcompat.hxx>
#include <vcl/svapp.hxx>
#include <vcl/wrkwin.hxx>
#include <vcl/virdev.hxx>
#include <sal/log.hxx>
#include <osl/file.hxx>
#include <osl/process.h>

#include <iostream>

using namespace css;

namespace {

class DemoMtfWin : public WorkWindow
{
    OUString maFileName;

public:
    explicit DemoMtfWin(const OUString& rFileName)
        : WorkWindow(nullptr, WB_APP | WB_STDWORK)
    {
        maFileName = rFileName;
    }

    virtual void Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle& rRect)  override;
};

}

void DemoMtfWin::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle& rRect)
{
    GDIMetaFile aMtf;
    SvFileStream aFileStream(maFileName, StreamMode::READ);

    if (aFileStream.IsOpen())
    {
        ReadWindowMetafile(aFileStream, aMtf);
    }
    else
    {
        Application::Abort("Can't read metafile");
    }

    aMtf.Play(*GetOutDev(), aMtf.GetActionSize());
    aMtf.Stop();
    aFileStream.Close();

    WorkWindow::Paint(rRenderContext, rRect);
}

namespace {

class DemoMtfApp : public Application
{
    VclPtr<DemoMtfWin> mpWin;
    OUString maFileName;

    static void showHelp()
    {
        std::cerr << "Usage: mtfdemo --help | FILE | -d FILE" << std::endl;
        std::cerr << "A VCL test app that displays Windows metafiles or dumps metaactions." << std::endl;
        std::cerr << "If you want to dump as metadump.xml, use -d before FILE." << std::endl;
        std::exit(0);
    }

public:

    DemoMtfApp()
        : mpWin(nullptr)
    {
    }

    virtual int Main() override
    {
        try
        {
            mpWin = VclPtr<DemoMtfWin>::Create(maFileName);
            mpWin->SetText("Display metafile");

            mpWin->Show();

            Application::Execute();
        }
        catch (const css::uno::Exception&)
        {
            TOOLS_WARN_EXCEPTION("vcl.app", "Fatal");
            return 1;
        }
        catch (const std::exception& e)
        {
            SAL_WARN("vcl.app", "Fatal: " << e.what());
            return 1;
        }
        return 0;
    }

private:
    uno::Reference<lang::XMultiServiceFactory> xMSF;
    void Init() override
    {
        try
        {
            const sal_uInt16 nCmdParams = GetCommandLineParamCount();
            OUString aArg, aFilename;
            bool bDumpXML = false;

            if (nCmdParams == 0)
            {
                showHelp();
                std::exit(1);
            }
            else
            {
                aArg = GetCommandLineParam(0);

                if (aArg == "--help" || aArg == "-h")
                {
                    showHelp();
                    std::exit(0);
                }
                else if (nCmdParams > 1 && (aArg == "--dump" || aArg == "-d"))
                {
                    aFilename = GetCommandLineParam(1);
                    bDumpXML = true;
                }
                else
                    aFilename = aArg;
            }

            OUString sWorkingDir, sFileUrl;
            osl_getProcessWorkingDir(&sWorkingDir.pData);
            (void)osl::FileBase::getFileURLFromSystemPath(aFilename, sFileUrl);
            (void)osl::FileBase::getAbsoluteFileURL(sWorkingDir, sFileUrl, maFileName);

            uno::Reference<uno::XComponentContext> xComponentContext
                = ::cppu::defaultBootstrap_InitialComponentContext();
            xMSF.set(xComponentContext->getServiceManager(), uno::UNO_QUERY);
            if(!xMSF.is())
                Application::Abort("Bootstrap failure - no service manager");

            ::comphelper::setProcessServiceFactory(xMSF);

            if(bDumpXML)
            {
                GDIMetaFile aMtf;
                SvFileStream aFileStream(maFileName, StreamMode::READ);
                ReadWindowMetafile(aFileStream, aMtf);
                OUString sAbsoluteDumpUrl, sDumpUrl;
                (void)osl::FileBase::getFileURLFromSystemPath("metadump.xml", sDumpUrl);
                (void)osl::FileBase::getAbsoluteFileURL(sWorkingDir, sDumpUrl, sAbsoluteDumpUrl);

                aMtf.dumpAsXml(rtl::OUStringToOString(sAbsoluteDumpUrl, RTL_TEXTENCODING_UTF8).getStr());
                std::cout << "Dumped metaactions as metadump.xml" << std::endl;
                std::exit(0);
            }

        }
        catch (const uno::Exception &e)
        {
            Application::Abort("Bootstrap exception " + e.Message);
        }
    }

    void DeInit() override
    {
        uno::Reference< lang::XComponent >(
            comphelper::getProcessComponentContext(),
        uno::UNO_QUERY_THROW)-> dispose();
        ::comphelper::setProcessServiceFactory(nullptr);
    }

};

}

void vclmain::createApplication()
{
    static DemoMtfApp aApp;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */