vcl: add new metafile viewer demo - mainly for fdo#80503

This is going to be very helpful troubleshooting problems with metafiles
that just won't display.

Change-Id: I661dd40e04434a9c64a0f59d9310d36444601989
diff --git a/Repository.mk b/Repository.mk
index b851dfd..3a65bc2 100644
--- a/Repository.mk
+++ b/Repository.mk
@@ -72,6 +72,7 @@ $(eval $(call gb_Helper_register_executables,NONE, \
	    outdevgrind) \
	vcldemo \
	tiledrendering \
    mtfdemo \
	$(if $(and $(ENABLE_GTK), $(filter LINUX,$(OS))), gtktiledviewer) \
))

diff --git a/vcl/Executable_mtfdemo.mk b/vcl/Executable_mtfdemo.mk
new file mode 100644
index 0000000..749547b
--- /dev/null
+++ b/vcl/Executable_mtfdemo.mk
@@ -0,0 +1,59 @@
# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t -*-
#
#
# 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/.
#

$(eval $(call gb_Executable_Executable,mtfdemo))

$(eval $(call gb_Executable_use_api,mtfdemo,\
    offapi \
    udkapi \
))

$(eval $(call gb_Executable_use_external,mtfdemo,boost_headers))

$(eval $(call gb_Executable_set_include,mtfdemo,\
    $$(INCLUDE) \
    -I$(SRCDIR)/vcl/inc \
    -I$(SRCDIR)/solenv/inc \
))

$(eval $(call gb_Executable_use_libraries,mtfdemo,\
	basegfx \
    tl \
    sal \
    vcl \
    cppu \
    cppuhelper \
    comphelper \
))

$(eval $(call gb_Executable_add_exception_objects,mtfdemo,\
    vcl/workben/mtfdemo \
))

$(eval $(call gb_Executable_use_static_libraries,mtfdemo,\
    vclmain \
))

ifeq ($(OS),LINUX)
$(eval $(call gb_Executable_add_libs,mtfdemo,\
	-lm \
	-ldl \
	-lpthread \
    -lGL \
    -lGLU \
    -lX11 \
))

$(eval $(call gb_Executable_use_static_libraries,mtfdemo,\
	glxtest \
))
endif

# vim: set noet sw=4 ts=4:
diff --git a/vcl/Module_vcl.mk b/vcl/Module_vcl.mk
index 329740a..9cc9ee0 100644
--- a/vcl/Module_vcl.mk
+++ b/vcl/Module_vcl.mk
@@ -29,7 +29,8 @@ $(eval $(call gb_Module_add_targets,vcl,\
		$(if $(filter LINUX MACOSX WNT,$(OS)), \
			Executable_icontest \
			Executable_outdevgrind \
			Executable_vcldemo )) \
			Executable_vcldemo \
            Executable_mtfdemo )) \
    $(if $(filter-out ANDROID IOS WNT,$(OS)), \
        Executable_svdemo \
        Executable_svptest \
diff --git a/vcl/source/gdi/gdimtf.cxx b/vcl/source/gdi/gdimtf.cxx
index cb71949..20e4c22 100644
--- a/vcl/source/gdi/gdimtf.cxx
+++ b/vcl/source/gdi/gdimtf.cxx
@@ -2807,6 +2807,10 @@ SvStream& ReadGDIMetaFile( SvStream& rIStm, GDIMetaFile& rGDIMetaFile )

        rIStm.SetNumberFormatInt( nOldFormat );
    }
    else
    {
        SAL_WARN("vcl.gdi", "Stream error: " << rIStm.GetError());
    }

    return rIStm;
}
diff --git a/vcl/workben/mtfdemo.cxx b/vcl/workben/mtfdemo.cxx
new file mode 100644
index 0000000..41b9fe7
--- /dev/null
+++ b/vcl/workben/mtfdemo.cxx
@@ -0,0 +1,146 @@
/* -*- 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 <comphelper/random.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/urlobj.hxx>
#include <tools/stream.hxx>
#include <tools/vcompat.hxx>
#include <vcl/svapp.hxx>
#include <vcl/wrkwin.hxx>
#include <vcl/virdev.hxx>

#include <tools/stream.hxx>

#include <cstdlib>

using namespace com::sun::star;

using namespace css;

class DemoMtfWin : public WorkWindow
{
    GDIMetaFile *mpMtf;

public:
    DemoMtfWin(OUString& aFileName) :
        WorkWindow(NULL, WB_APP | WB_STDWORK)
    {


        SvFileStream aFileStream(aFileName, STREAM_READ);

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

    virtual void Paint( const Rectangle& rRect )  SAL_OVERRIDE;
};

void DemoMtfWin::Paint( const Rectangle& rRect )
{
    mpMtf->Play(this, mpMtf->GetActionSize());

    WorkWindow::Paint( rRect );
}

class DemoMtfApp : public Application
{
    DemoMtfWin *mpWin;
    OUString *mpFileName;

    void showHelp()
    {
        fprintf(stderr, "Usage: mtfdemo --help | FILE\n");
        fprintf(stderr, "A VCL test app that displays Windows metafiles\n");
        std::exit(0);
    }

public:
    virtual int Main() SAL_OVERRIDE
    {
        mpWin = new DemoMtfWin(*mpFileName);
        mpWin->SetText(OUString("Display metafile"));

        mpWin->Show();

        Application::Execute();

        return 0;
    }

protected:
    uno::Reference<lang::XMultiServiceFactory> xMSF;
    void Init() SAL_OVERRIDE
    {
        try
        {
            sal_uInt32 nCmdParams = GetCommandLineParamCount();

            if (nCmdParams == 0)
                showHelp();
            else
            {
                OUString aArg = GetCommandLineParam(0);

                if (aArg == "--help" || aArg == "-h")
                    showHelp();
                else
                    mpFileName = new OUString(aArg);
            }

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

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

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

};


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

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