OInterfaceContainerHelper3 needs to be thread-safe
[LibreOffice.git] / vcl / workben / icontest.cxx
blobea6cb3e7195a46aa996979f01f57a034091cde99
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 <sal/log.hxx>
33 #include <tools/stream.hxx>
34 #include <vcl/builder.hxx>
35 #include <vcl/toolkit/button.hxx>
36 #include <vcl/toolkit/dialog.hxx>
37 #include <vcl/toolkit/fixed.hxx>
38 #include <vcl/graph.hxx>
39 #include <vcl/graphicfilter.hxx>
40 #include <vcl/image.hxx>
41 #include <vcl/opengl/OpenGLContext.hxx>
42 #include <vcl/opengl/OpenGLHelper.hxx>
43 #include <vcl/svapp.hxx>
44 #include <vcl/vclmain.hxx>
45 #include <vcl/wrkwin.hxx>
47 using namespace com::sun::star;
49 namespace {
50 const int WIDTH = 1000, HEIGHT = 800;
52 double getTimeNow()
54 TimeValue aValue;
55 osl_getSystemTime(&aValue);
56 return static_cast<double>(aValue.Seconds) +
57 static_cast<double>(aValue.Nanosec) / (1000*1000*1000);
60 class MyWorkWindow : public WorkWindow
62 double mnStartTime;
63 int mnPaintCount;
65 public:
66 Graphic maGraphic;
67 BitmapEx *mpBitmap;
68 VclPtr<FixedBitmap> mpFixedBitmap;
70 MyWorkWindow( vcl::Window* pParent, WinBits nWinStyle );
71 virtual ~MyWorkWindow() override { disposeOnce(); }
72 virtual void dispose() override { mpFixedBitmap.clear(); WorkWindow::dispose(); }
73 void LoadGraphic( const OUString& sImageFile );
75 virtual void Paint( vcl::RenderContext& /*rRenderContext*/, const tools::Rectangle& rRect ) override;
76 virtual void Resize() override;
81 MyWorkWindow::MyWorkWindow( vcl::Window* pParent, WinBits nWinStyle )
82 : WorkWindow(pParent, nWinStyle)
83 , mpBitmap(nullptr)
84 , mpFixedBitmap(nullptr)
86 mnPaintCount = 0;
87 mnStartTime = getTimeNow();
88 EnableInput();
91 void MyWorkWindow::LoadGraphic( const OUString& sImageFile )
93 SvFileStream aFileStream( sImageFile, StreamMode::READ );
94 GraphicFilter aGraphicFilter(false);
95 if (aGraphicFilter.ImportGraphic(maGraphic, sImageFile, aFileStream) != ERRCODE_NONE)
97 SAL_WARN("vcl.icontest", "Could not import image '" << sImageFile << "'");
98 return;
102 void MyWorkWindow::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle& rRect)
104 std::cout << "==> Paint! " << mnPaintCount++ << " (vcl) " << GetSizePixel() << " " << getTimeNow() - mnStartTime << std::endl;
106 Size aGraphicSize( maGraphic.GetSizePixel() );
107 float aspect = static_cast<float>(aGraphicSize.Width()) / aGraphicSize.Height();
108 Size aSize;
109 if( aspect >= (float(WIDTH)) / HEIGHT )
110 aSize = Size( WIDTH, HEIGHT/aspect );
111 else
112 aSize = Size( WIDTH * aspect, HEIGHT );
113 aSize.setWidth( aSize.Width() * (1 + (0.1*sin(mnPaintCount/60.))) );
114 aSize.setHeight( aSize.Height() * (1 + (0.1*sin(mnPaintCount/50.))) );
116 BitmapEx aEmpty;
117 mpFixedBitmap->SetBitmap( aEmpty );
118 GraphicConversionParameters aConv( aSize );
119 mpBitmap = new BitmapEx(maGraphic.GetBitmapEx( aConv ));
120 mpFixedBitmap->SetBitmap( *mpBitmap );
121 mpFixedBitmap->SetSizePixel( aSize );
123 WorkWindow::Paint(rRenderContext, rRect);
125 if (mnPaintCount == 100)
126 Application::Quit();
128 Invalidate( InvalidateFlags::Children );
131 void MyWorkWindow::Resize()
133 SAL_INFO("vcl.icontest", "Resize " << GetSizePixel());
136 namespace {
138 class IconTestApp : public Application
140 public:
141 virtual void Init() override;
142 virtual int Main() override;
144 IconTestApp() : nRet(EXIT_SUCCESS) {};
146 private:
147 int nRet;
149 void DoItWithVcl(const OUString& sImageFile);
154 void IconTestApp::Init()
156 nRet = EXIT_SUCCESS;
158 uno::Reference<uno::XComponentContext> xContext =
159 cppu::defaultBootstrap_InitialComponentContext();
160 uno::Reference<lang::XMultiComponentFactory> xFactory =
161 xContext->getServiceManager();
162 uno::Reference<lang::XMultiServiceFactory> xSFactory(xFactory, uno::UNO_QUERY_THROW);
163 comphelper::setProcessServiceFactory(xSFactory);
165 // Create UCB (for backwards compatibility, in case some code still uses
166 // plain createInstance w/o args directly to obtain an instance):
167 ::ucb::UniversalContentBroker::create(
168 comphelper::getProcessComponentContext() );
171 int IconTestApp::Main()
173 if (GetCommandLineParamCount() != 1)
175 fprintf(stderr, "Usage: imagetest <image>\n");
176 return EXIT_FAILURE;
178 OUString sImageFile( GetCommandLineParam( 0 ) );
179 DoItWithVcl( sImageFile );
181 return nRet;
184 void IconTestApp::DoItWithVcl( const OUString& sImageFile)
188 VclPtrInstance<MyWorkWindow> pWindow( nullptr, WB_APP | WB_STDWORK | WB_SIZEABLE | WB_CLOSEABLE | WB_CLIPCHILDREN );
190 pWindow->SetText("VCL Image Test");
192 pWindow->LoadGraphic( sImageFile );
193 pWindow->mpFixedBitmap = VclPtr<FixedBitmap>::Create( pWindow );
194 pWindow->mpFixedBitmap->SetPosPixel( Point( 0, 0 ) );
195 pWindow->mpFixedBitmap->Show();
197 pWindow->Hide();
198 pWindow->Show();
200 Execute();
202 catch (const uno::Exception &e)
204 fprintf(stderr, "fatal error: %s\n", OUStringToOString(e.Message, osl_getThreadTextEncoding()).getStr());
205 nRet = EXIT_FAILURE;
207 catch (const std::exception &e)
209 fprintf(stderr, "fatal error: %s\n", e.what());
210 nRet = EXIT_FAILURE;
214 void vclmain::createApplication()
216 static IconTestApp aApp;
219 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */