Update Remove Formatting icons
[LibreOffice.git] / vcl / workben / icontest.cxx
blob1ec9bac49c9f30b5bb7c4df4e943315cb749d426
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/svapp.hxx>
42 #include <vcl/vclmain.hxx>
43 #include <vcl/wrkwin.hxx>
45 using namespace com::sun::star;
47 namespace {
48 const int WIDTH = 1000, HEIGHT = 800;
50 double getTimeNow()
52 TimeValue aValue;
53 osl_getSystemTime(&aValue);
54 return static_cast<double>(aValue.Seconds) +
55 static_cast<double>(aValue.Nanosec) / (1000*1000*1000);
58 class MyWorkWindow : public WorkWindow
60 double mnStartTime;
61 int mnPaintCount;
63 public:
64 Graphic maGraphic;
65 BitmapEx *mpBitmap;
66 VclPtr<FixedBitmap> mpFixedBitmap;
68 MyWorkWindow( vcl::Window* pParent, WinBits nWinStyle );
69 virtual ~MyWorkWindow() override { disposeOnce(); }
70 virtual void dispose() override { mpFixedBitmap.clear(); WorkWindow::dispose(); }
71 void LoadGraphic( const OUString& sImageFile );
73 virtual void Paint( vcl::RenderContext& /*rRenderContext*/, const tools::Rectangle& rRect ) override;
74 virtual void Resize() override;
79 MyWorkWindow::MyWorkWindow( vcl::Window* pParent, WinBits nWinStyle )
80 : WorkWindow(pParent, nWinStyle)
81 , mpBitmap(nullptr)
82 , mpFixedBitmap(nullptr)
84 mnPaintCount = 0;
85 mnStartTime = getTimeNow();
86 EnableInput();
89 void MyWorkWindow::LoadGraphic( const OUString& sImageFile )
91 SvFileStream aFileStream( sImageFile, StreamMode::READ );
92 GraphicFilter aGraphicFilter(false);
93 if (aGraphicFilter.ImportGraphic(maGraphic, sImageFile, aFileStream) != ERRCODE_NONE)
95 SAL_WARN("vcl.icontest", "Could not import image '" << sImageFile << "'");
96 return;
100 void MyWorkWindow::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle& rRect)
102 std::cout << "==> Paint! " << mnPaintCount++ << " (vcl) " << GetSizePixel() << " " << getTimeNow() - mnStartTime << std::endl;
104 Size aGraphicSize( maGraphic.GetSizePixel() );
105 float aspect = static_cast<float>(aGraphicSize.Width()) / aGraphicSize.Height();
106 Size aSize;
107 if( aspect >= (float(WIDTH)) / HEIGHT )
108 aSize = Size( WIDTH, HEIGHT/aspect );
109 else
110 aSize = Size( WIDTH * aspect, HEIGHT );
111 aSize.setWidth( aSize.Width() * (1 + (0.1*sin(mnPaintCount/60.))) );
112 aSize.setHeight( aSize.Height() * (1 + (0.1*sin(mnPaintCount/50.))) );
114 BitmapEx aEmpty;
115 mpFixedBitmap->SetBitmap( aEmpty );
116 GraphicConversionParameters aConv( aSize );
117 mpBitmap = new BitmapEx(maGraphic.GetBitmapEx( aConv ));
118 mpFixedBitmap->SetBitmap( *mpBitmap );
119 mpFixedBitmap->SetSizePixel( aSize );
121 WorkWindow::Paint(rRenderContext, rRect);
123 if (mnPaintCount == 100)
124 Application::Quit();
126 Invalidate( InvalidateFlags::Children );
129 void MyWorkWindow::Resize()
131 SAL_INFO("vcl.icontest", "Resize " << GetSizePixel());
134 namespace {
136 class IconTestApp : public Application
138 public:
139 virtual void Init() override;
140 virtual int Main() override;
142 IconTestApp() : nRet(EXIT_SUCCESS) {};
144 private:
145 int nRet;
147 void DoItWithVcl(const OUString& sImageFile);
152 void IconTestApp::Init()
154 nRet = EXIT_SUCCESS;
156 uno::Reference<uno::XComponentContext> xContext =
157 cppu::defaultBootstrap_InitialComponentContext();
158 uno::Reference<lang::XMultiComponentFactory> xFactory =
159 xContext->getServiceManager();
160 uno::Reference<lang::XMultiServiceFactory> xSFactory(xFactory, uno::UNO_QUERY_THROW);
161 comphelper::setProcessServiceFactory(xSFactory);
163 // Create UCB (for backwards compatibility, in case some code still uses
164 // plain createInstance w/o args directly to obtain an instance):
165 ::ucb::UniversalContentBroker::create(
166 comphelper::getProcessComponentContext() );
169 int IconTestApp::Main()
171 if (GetCommandLineParamCount() != 1)
173 fprintf(stderr, "Usage: imagetest <image>\n");
174 return EXIT_FAILURE;
176 OUString sImageFile( GetCommandLineParam( 0 ) );
177 DoItWithVcl( sImageFile );
179 return nRet;
182 void IconTestApp::DoItWithVcl( const OUString& sImageFile)
186 VclPtrInstance<MyWorkWindow> pWindow( nullptr, WB_APP | WB_STDWORK | WB_SIZEABLE | WB_CLOSEABLE | WB_CLIPCHILDREN );
188 pWindow->SetText("VCL Image Test");
190 pWindow->LoadGraphic( sImageFile );
191 pWindow->mpFixedBitmap = VclPtr<FixedBitmap>::Create( pWindow );
192 pWindow->mpFixedBitmap->SetPosPixel( Point( 0, 0 ) );
193 pWindow->mpFixedBitmap->Show();
195 pWindow->Hide();
196 pWindow->Show();
198 Execute();
200 catch (const uno::Exception &e)
202 fprintf(stderr, "fatal error: %s\n", OUStringToOString(e.Message, osl_getThreadTextEncoding()).getStr());
203 nRet = EXIT_FAILURE;
205 catch (const std::exception &e)
207 fprintf(stderr, "fatal error: %s\n", e.what());
208 nRet = EXIT_FAILURE;
212 void vclmain::createApplication()
214 static IconTestApp aApp;
217 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */