tdf#153972 Fix color filter when cells have no content
[LibreOffice.git] / vcl / workben / icontest.cxx
blob86cf4da23e38f88de4c274c8ce8b142245233c15
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 <com/sun/star/lang/XComponent.hpp>
24 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
25 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
26 #include <com/sun/star/ucb/UniversalContentBroker.hpp>
27 #include <comphelper/processfactory.hxx>
28 #include <cppuhelper/bootstrap.hxx>
29 #include <osl/file.hxx>
30 #include <sal/log.hxx>
31 #include <tools/stream.hxx>
32 #include <vcl/builder.hxx>
33 #include <vcl/toolkit/button.hxx>
34 #include <vcl/toolkit/dialog.hxx>
35 #include <vcl/toolkit/fixed.hxx>
36 #include <vcl/graph.hxx>
37 #include <vcl/graphicfilter.hxx>
38 #include <vcl/image.hxx>
39 #include <vcl/svapp.hxx>
40 #include <vcl/vclmain.hxx>
41 #include <vcl/wrkwin.hxx>
43 using namespace com::sun::star;
45 namespace {
46 const int WIDTH = 1000, HEIGHT = 800;
48 double getTimeNow()
50 TimeValue aValue;
51 osl_getSystemTime(&aValue);
52 return static_cast<double>(aValue.Seconds) +
53 static_cast<double>(aValue.Nanosec) / (1000*1000*1000);
56 class MyWorkWindow : public WorkWindow
58 double mnStartTime;
59 int mnPaintCount;
61 public:
62 Graphic maGraphic;
63 BitmapEx *mpBitmap;
64 VclPtr<FixedBitmap> mpFixedBitmap;
66 MyWorkWindow( vcl::Window* pParent, WinBits nWinStyle );
67 virtual ~MyWorkWindow() override { disposeOnce(); }
68 virtual void dispose() override { mpFixedBitmap.clear(); WorkWindow::dispose(); }
69 void LoadGraphic( const OUString& sImageFile );
71 virtual void Paint( vcl::RenderContext& /*rRenderContext*/, const tools::Rectangle& rRect ) override;
72 virtual void Resize() override;
77 MyWorkWindow::MyWorkWindow( vcl::Window* pParent, WinBits nWinStyle )
78 : WorkWindow(pParent, nWinStyle)
79 , mpBitmap(nullptr)
80 , mpFixedBitmap(nullptr)
82 mnPaintCount = 0;
83 mnStartTime = getTimeNow();
84 EnableInput();
87 void MyWorkWindow::LoadGraphic( const OUString& sImageFile )
89 SvFileStream aFileStream( sImageFile, StreamMode::READ );
90 GraphicFilter aGraphicFilter(false);
91 if (aGraphicFilter.ImportGraphic(maGraphic, sImageFile, aFileStream) != ERRCODE_NONE)
93 SAL_WARN("vcl.icontest", "Could not import image '" << sImageFile << "'");
94 return;
98 void MyWorkWindow::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle& rRect)
100 std::cout << "==> Paint! " << mnPaintCount++ << " (vcl) " << GetSizePixel() << " " << getTimeNow() - mnStartTime << std::endl;
102 Size aGraphicSize( maGraphic.GetSizePixel() );
103 float aspect = static_cast<float>(aGraphicSize.Width()) / aGraphicSize.Height();
104 Size aSize;
105 if( aspect >= (float(WIDTH)) / HEIGHT )
106 aSize = Size( WIDTH, HEIGHT/aspect );
107 else
108 aSize = Size( WIDTH * aspect, HEIGHT );
109 aSize.setWidth( aSize.Width() * (1 + (0.1*sin(mnPaintCount/60.))) );
110 aSize.setHeight( aSize.Height() * (1 + (0.1*sin(mnPaintCount/50.))) );
112 BitmapEx aEmpty;
113 mpFixedBitmap->SetBitmap( aEmpty );
114 GraphicConversionParameters aConv( aSize );
115 mpBitmap = new BitmapEx(maGraphic.GetBitmapEx( aConv ));
116 mpFixedBitmap->SetBitmap( *mpBitmap );
117 mpFixedBitmap->SetSizePixel( aSize );
119 WorkWindow::Paint(rRenderContext, rRect);
121 if (mnPaintCount == 100)
122 Application::Quit();
124 Invalidate( InvalidateFlags::Children );
127 void MyWorkWindow::Resize()
129 SAL_INFO("vcl.icontest", "Resize " << GetSizePixel());
132 namespace {
134 class IconTestApp : public Application
136 public:
137 virtual void Init() override;
138 virtual int Main() override;
140 IconTestApp() : nRet(EXIT_SUCCESS) {};
142 private:
143 int nRet;
145 void DoItWithVcl(const OUString& sImageFile);
150 void IconTestApp::Init()
152 nRet = EXIT_SUCCESS;
154 uno::Reference<uno::XComponentContext> xContext =
155 cppu::defaultBootstrap_InitialComponentContext();
156 uno::Reference<lang::XMultiComponentFactory> xFactory =
157 xContext->getServiceManager();
158 uno::Reference<lang::XMultiServiceFactory> xSFactory(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: */