Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / vcl / workben / icontest.cxx
blob0806ddcaf98c6c95ef50af24dcd4e00b35573514
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 */
11 * =======================================================================
13 * This is a quick hack to test some stuff. Work in progress. Don't touch
14 * and don't bother inspecting too closely.
16 * =======================================================================
19 #include <iostream>
21 #include <math.h>
23 #include <glm/gtx/bit.hpp>
25 #include <com/sun/star/lang/XComponent.hpp>
26 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
27 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
28 #include <com/sun/star/ucb/UniversalContentBroker.hpp>
29 #include <comphelper/processfactory.hxx>
30 #include <cppuhelper/bootstrap.hxx>
31 #include <osl/file.hxx>
32 #include <vcl/builder.hxx>
33 #include <vcl/button.hxx>
34 #include <vcl/dialog.hxx>
35 #include <vcl/fixed.hxx>
36 #include <vcl/graph.hxx>
37 #include <vcl/graphicfilter.hxx>
38 #include <vcl/image.hxx>
39 #include <vcl/openglwin.hxx>
40 #include <vcl/opengl/OpenGLContext.hxx>
41 #include <vcl/opengl/OpenGLHelper.hxx>
42 #include <vcl/svapp.hxx>
43 #include <vcl/vclmain.hxx>
44 #include <vcl/wrkwin.hxx>
46 using namespace com::sun::star;
48 namespace {
49 const int WIDTH = 1000, HEIGHT = 800;
51 double getTimeNow()
53 TimeValue aValue;
54 osl_getSystemTime(&aValue);
55 return static_cast<double>(aValue.Seconds) +
56 static_cast<double>(aValue.Nanosec) / (1000*1000*1000);
61 class MyWorkWindow : public WorkWindow
63 double mnStartTime;
64 int mnPaintCount;
66 public:
67 Graphic maGraphic;
68 Bitmap *mpBitmap;
69 VclPtr<FixedBitmap> mpFixedBitmap;
71 MyWorkWindow( vcl::Window* pParent, WinBits nWinStyle );
72 virtual ~MyWorkWindow() override { disposeOnce(); }
73 virtual void dispose() override { mpFixedBitmap.clear(); WorkWindow::dispose(); }
74 void LoadGraphic( const OUString& sImageFile );
76 virtual void Paint( vcl::RenderContext& /*rRenderContext*/, const tools::Rectangle& rRect ) override;
77 virtual void Resize() override;
80 MyWorkWindow::MyWorkWindow( vcl::Window* pParent, WinBits nWinStyle )
81 : WorkWindow(pParent, nWinStyle)
82 , mpBitmap(nullptr)
83 , mpFixedBitmap(nullptr)
85 mnPaintCount = 0;
86 mnStartTime = getTimeNow();
87 EnableInput();
90 void MyWorkWindow::LoadGraphic( const OUString& sImageFile )
92 SvFileStream aFileStream( sImageFile, StreamMode::READ );
93 GraphicFilter aGraphicFilter(false);
94 if (aGraphicFilter.ImportGraphic(maGraphic, sImageFile, aFileStream) != ERRCODE_NONE)
96 SAL_WARN("vcl.icontest", "Could not import image '" << sImageFile << "'");
97 return;
101 void MyWorkWindow::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle& rRect)
103 std::cout << "==> Paint! " << mnPaintCount++ << " (vcl) " << GetSizePixel() << " " << getTimeNow() - mnStartTime << std::endl;
105 Size aGraphicSize( maGraphic.GetSizePixel() );
106 float aspect = static_cast<float>(aGraphicSize.Width()) / aGraphicSize.Height();
107 Size aSize;
108 if( aspect >= (float(WIDTH)) / HEIGHT )
109 aSize = Size( WIDTH, HEIGHT/aspect );
110 else
111 aSize = Size( WIDTH * aspect, HEIGHT );
112 aSize.setWidth( aSize.Width() * (1 + (0.1*sin(mnPaintCount/60.))) );
113 aSize.setHeight( aSize.Height() * (1 + (0.1*sin(mnPaintCount/50.))) );
115 Bitmap aEmpty;
116 mpFixedBitmap->SetBitmap( aEmpty );
117 GraphicConversionParameters aConv( aSize );
118 mpBitmap = new Bitmap( maGraphic.GetBitmap( aConv ) );
119 mpFixedBitmap->SetBitmap( *mpBitmap );
120 mpFixedBitmap->SetSizePixel( aSize );
122 WorkWindow::Paint(rRenderContext, rRect);
124 if (mnPaintCount == 100)
125 Application::Quit();
127 Invalidate( InvalidateFlags::Children );
130 void MyWorkWindow::Resize()
132 SAL_INFO("vcl.icontest", "Resize " << GetSizePixel());
135 class IconTestApp : public Application
137 public:
138 virtual void Init() override;
139 virtual int Main() override;
141 IconTestApp() : nRet(EXIT_SUCCESS) {};
143 private:
144 int nRet;
146 void DoItWithVcl(const OUString& sImageFile);
149 void IconTestApp::Init()
151 nRet = EXIT_SUCCESS;
153 uno::Reference<uno::XComponentContext> xContext =
154 cppu::defaultBootstrap_InitialComponentContext();
155 uno::Reference<lang::XMultiComponentFactory> xFactory =
156 xContext->getServiceManager();
157 uno::Reference<lang::XMultiServiceFactory> xSFactory =
158 uno::Reference<lang::XMultiServiceFactory> (xFactory, uno::UNO_QUERY_THROW);
159 comphelper::setProcessServiceFactory(xSFactory);
161 // Create UCB (for backwards compatibility, in case some code still uses
162 // plain createInstance w/o args directly to obtain an instance):
163 ::ucb::UniversalContentBroker::create(
164 comphelper::getProcessComponentContext() );
167 int IconTestApp::Main()
169 if (GetCommandLineParamCount() != 1)
171 fprintf(stderr, "Usage: imagetest <image>\n");
172 return EXIT_FAILURE;
174 OUString sImageFile( GetCommandLineParam( 0 ) );
175 DoItWithVcl( sImageFile );
177 return nRet;
180 void IconTestApp::DoItWithVcl( const OUString& sImageFile)
184 VclPtrInstance<MyWorkWindow> pWindow( nullptr, WB_APP | WB_STDWORK | WB_SIZEABLE | WB_CLOSEABLE | WB_CLIPCHILDREN );
186 pWindow->SetText("VCL Image Test");
188 pWindow->LoadGraphic( sImageFile );
189 pWindow->mpFixedBitmap = VclPtr<FixedBitmap>::Create( pWindow );
190 pWindow->mpFixedBitmap->SetPosPixel( Point( 0, 0 ) );
191 pWindow->mpFixedBitmap->Show();
193 pWindow->Hide();
194 pWindow->Show();
196 Execute();
198 catch (const uno::Exception &e)
200 fprintf(stderr, "fatal error: %s\n", OUStringToOString(e.Message, osl_getThreadTextEncoding()).getStr());
201 nRet = EXIT_FAILURE;
203 catch (const std::exception &e)
205 fprintf(stderr, "fatal error: %s\n", e.what());
206 nRet = EXIT_FAILURE;
210 void vclmain::createApplication()
212 static IconTestApp aApp;
215 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */