Branch libreoffice-5-0-4
[LibreOffice.git] / vcl / workben / icontest.cxx
blob6cbe371139145d8ef0430d85a7f4071de0968cb9
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 <GL/glew.h>
25 #include <glm/gtx/bit.hpp>
27 #include <com/sun/star/lang/XComponent.hpp>
28 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
29 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
30 #include <com/sun/star/ucb/UniversalContentBroker.hpp>
31 #include <comphelper/processfactory.hxx>
32 #include <cppuhelper/bootstrap.hxx>
33 #include <osl/file.hxx>
34 #include <vcl/builder.hxx>
35 #include <vcl/button.hxx>
36 #include <vcl/dialog.hxx>
37 #include <vcl/fixed.hxx>
38 #include <vcl/graph.hxx>
39 #include <vcl/graphicfilter.hxx>
40 #include <vcl/image.hxx>
41 #include <vcl/openglwin.hxx>
42 #include <vcl/opengl/OpenGLContext.hxx>
43 #include <vcl/opengl/OpenGLHelper.hxx>
44 #include <vcl/svapp.hxx>
45 #include <vcl/vclmain.hxx>
46 #include <vcl/wrkwin.hxx>
48 using namespace com::sun::star;
50 namespace {
51 const int WIDTH = 1000, HEIGHT = 800;
53 double getTimeNow()
55 TimeValue aValue;
56 osl_getSystemTime(&aValue);
57 return (double)aValue.Seconds +
58 (double)aValue.Nanosec / (1000*1000*1000);
63 class MyWorkWindow : public WorkWindow
65 private:
67 protected:
68 double mnStartTime;
69 int mnPaintCount;
71 public:
72 Graphic maGraphic;
73 Bitmap *mpBitmap;
74 VclPtr<FixedBitmap> mpFixedBitmap;
76 MyWorkWindow( vcl::Window* pParent, WinBits nWinStyle );
77 virtual ~MyWorkWindow() { disposeOnce(); }
78 virtual void dispose() SAL_OVERRIDE { mpFixedBitmap.clear(); WorkWindow::dispose(); }
79 void LoadGraphic( const OUString& sImageFile );
81 virtual void Paint( vcl::RenderContext& /*rRenderContext*/, const Rectangle& rRect ) SAL_OVERRIDE;
82 virtual void Resize() SAL_OVERRIDE;
85 MyWorkWindow::MyWorkWindow( vcl::Window* pParent, WinBits nWinStyle )
86 : WorkWindow(pParent, nWinStyle)
87 , mpBitmap(NULL)
88 , mpFixedBitmap(NULL)
90 mnPaintCount = 0;
91 mnStartTime = getTimeNow();
92 EnableInput();
95 void MyWorkWindow::LoadGraphic( const OUString& sImageFile )
97 SvFileStream aFileStream( sImageFile, StreamMode::READ );
98 GraphicFilter aGraphicFilter(false);
99 if (aGraphicFilter.ImportGraphic(maGraphic, sImageFile, aFileStream) != 0)
101 SAL_WARN("vcl.icontest", "Could not import image '" << sImageFile << "'");
102 return;
106 void MyWorkWindow::Paint(vcl::RenderContext& rRenderContext, const Rectangle& rRect)
108 std::cout << "==> Paint! " << mnPaintCount++ << " (vcl) " << GetSizePixel() << " " << getTimeNow() - mnStartTime << std::endl;
110 Size aGraphicSize( maGraphic.GetSizePixel() );
111 float aspect = ((float) aGraphicSize.Width()) / aGraphicSize.Height();
112 Size aSize;
113 if( aspect >= ((float) WIDTH) / HEIGHT )
114 aSize = Size( WIDTH, HEIGHT/aspect );
115 else
116 aSize = Size( WIDTH * aspect, HEIGHT );
117 aSize.setWidth( aSize.Width() * (1 + (0.1*sin(mnPaintCount/60.))) );
118 aSize.setHeight( aSize.Height() * (1 + (0.1*sin(mnPaintCount/50.))) );
120 Bitmap aEmpty;
121 mpFixedBitmap->SetBitmap( aEmpty );
122 GraphicConversionParameters aConv( aSize );
123 mpBitmap = new Bitmap( maGraphic.GetBitmap( aConv ) );
124 mpFixedBitmap->SetBitmap( *mpBitmap );
125 mpFixedBitmap->SetSizePixel( aSize );
127 WorkWindow::Paint(rRenderContext, rRect);
129 if (mnPaintCount == 100)
130 Application::Quit();
132 Invalidate( INVALIDATE_CHILDREN );
135 void MyWorkWindow::Resize()
137 SAL_INFO("vcl.icontest", "Resize " << GetSizePixel());
140 class IconTestApp : public Application
142 public:
143 virtual void Init() SAL_OVERRIDE;
144 virtual int Main() SAL_OVERRIDE;
146 IconTestApp() : nRet(EXIT_SUCCESS) {};
148 private:
149 int nRet;
151 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 =
163 uno::Reference<lang::XMultiServiceFactory> (xFactory, uno::UNO_QUERY_THROW);
164 comphelper::setProcessServiceFactory(xSFactory);
166 // Create UCB (for backwards compatibility, in case some code still uses
167 // plain createInstance w/o args directly to obtain an instance):
168 ::ucb::UniversalContentBroker::create(
169 comphelper::getProcessComponentContext() );
172 int IconTestApp::Main()
174 if (GetCommandLineParamCount() != 1)
176 fprintf(stderr, "Usage: imagetest <image>\n");
177 return EXIT_FAILURE;
179 OUString sImageFile( GetCommandLineParam( 0 ) );
180 DoItWithVcl( sImageFile );
182 return nRet;
185 void IconTestApp::DoItWithVcl( const OUString& sImageFile)
189 VclPtrInstance<MyWorkWindow> pWindow( nullptr, WB_APP | WB_STDWORK | WB_SIZEABLE | WB_CLOSEABLE | WB_CLIPCHILDREN );
191 pWindow->SetText(OUString("VCL Image Test"));
193 pWindow->LoadGraphic( sImageFile );
194 pWindow->mpFixedBitmap = VclPtr<FixedBitmap>::Create( pWindow );
195 pWindow->mpFixedBitmap->SetPosPixel( Point( 0, 0 ) );
196 pWindow->mpFixedBitmap->Show();
198 pWindow->Hide();
199 pWindow->Show();
201 Execute();
203 catch (const uno::Exception &e)
205 fprintf(stderr, "fatal error: %s\n", OUStringToOString(e.Message, osl_getThreadTextEncoding()).getStr());
206 nRet = EXIT_FAILURE;
208 catch (const std::exception &e)
210 fprintf(stderr, "fatal error: %s\n", e.what());
211 nRet = EXIT_FAILURE;
215 void vclmain::createApplication()
217 static IconTestApp aApp;
220 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */