bump product version to 6.4.0.3
[LibreOffice.git] / vcl / workben / svptest.cxx
blob72ff179821a84fd5cc66f4a81ea4965b2d912e19
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/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/main.h>
21 #include <sal/log.hxx>
22 #include <tools/diagnose_ex.h>
23 #include <tools/extendapplicationenvironment.hxx>
25 #include <cppuhelper/bootstrap.hxx>
26 #include <comphelper/processfactory.hxx>
28 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
29 #include <com/sun/star/uno/XComponentContext.hpp>
31 #include <vcl/event.hxx>
32 #include <vcl/svapp.hxx>
33 #include <vcl/wrkwin.hxx>
34 #include <vcl/gradient.hxx>
35 #include <vcl/lineinfo.hxx>
36 #include <vcl/bitmap.hxx>
37 #include <vcl/bitmapaccess.hxx>
38 #include <vcl/metric.hxx>
39 #include <vcl/vclptr.hxx>
40 #include <bitmapwriteaccess.hxx>
42 #include <rtl/ustrbuf.hxx>
44 #include <math.h>
46 using namespace ::com::sun::star::uno;
47 using namespace ::com::sun::star::lang;
48 using namespace cppu;
50 // Forward declaration
51 static void Main();
53 SAL_IMPLEMENT_MAIN()
55 try
57 tools::extendApplicationEnvironment();
59 Reference< XComponentContext > xContext = defaultBootstrap_InitialComponentContext();
60 Reference< XMultiServiceFactory > xServiceManager( xContext->getServiceManager(), UNO_QUERY );
62 if( !xServiceManager.is() )
63 Application::Abort( "Failed to bootstrap" );
65 comphelper::setProcessServiceFactory( xServiceManager );
67 InitVCL();
68 ::Main();
69 DeInitVCL();
71 catch (const Exception&)
73 TOOLS_WARN_EXCEPTION("vcl.app", "Fatal");
74 return 1;
76 catch (const std::exception &e)
78 fprintf(stderr, "fatal error: %s\n", e.what());
79 return 1;
82 return 0;
85 class MyWin : public WorkWindow
87 Bitmap m_aBitmap;
88 public:
89 MyWin( vcl::Window* pParent, WinBits nWinStyle );
91 virtual void Paint( vcl::RenderContext& /*rRenderContext*/, const tools::Rectangle& rRect ) override;
94 void Main()
96 ScopedVclPtrInstance< MyWin > aMainWin( nullptr, WB_APP | WB_STDWORK );
97 aMainWin->SetText( "VCL - Workbench" );
98 aMainWin->Show();
100 Application::Execute();
103 MyWin::MyWin( vcl::Window* pParent, WinBits nWinStyle ) :
104 WorkWindow( pParent, nWinStyle ),
105 m_aBitmap( Size( 256, 256 ), 32 )
107 // prepare an alpha mask
108 BitmapWriteAccess* pAcc = m_aBitmap.AcquireWriteAccess();
109 for( int nX = 0; nX < 256; nX++ )
111 for( int nY = 0; nY < 256; nY++ )
113 double fRed = 255.0-1.5*sqrt(static_cast<double>(nX*nX+nY*nY));
114 if( fRed < 0.0 )
115 fRed = 0.0;
116 double fGreen = 255.0-1.5*sqrt(static_cast<double>((255-nX)*(255-nX)+nY*nY));
117 if( fGreen < 0.0 )
118 fGreen = 0.0;
119 double fBlue = 255.0-1.5*sqrt(static_cast<double>((128-nX)*(128-nX)+(255-nY)*(255-nY)));
120 if( fBlue < 0.0 )
121 fBlue = 0.0;
122 pAcc->SetPixel( nY, nX, BitmapColor( sal_uInt8(fRed), sal_uInt8(fGreen), sal_uInt8(fBlue) ) );
125 Bitmap::ReleaseAccess( pAcc );
128 static Point project( const Point& rPoint )
130 const double angle_x = M_PI / 6.0;
131 const double angle_z = M_PI / 6.0;
133 // transform planar coordinates to 3d
134 double x = rPoint.X();
135 double y = rPoint.Y();
137 // rotate around X axis
138 double x1 = x;
139 double y1 = y * cos( angle_x );
140 double z1 = y * sin( angle_x );
142 // rotate around Z axis
143 double x2 = x1 * cos( angle_z ) + y1 * sin( angle_z );
144 //double y2 = y1 * cos( angle_z ) - x1 * sin( angle_z );
145 double z2 = z1;
147 return Point( static_cast<sal_Int32>(x2), static_cast<sal_Int32>(z2) );
150 static Color approachColor( const Color& rFrom, const Color& rTo )
152 Color aColor;
153 sal_uInt8 nDiff;
154 // approach red
155 if( rFrom.GetRed() < rTo.GetRed() )
157 nDiff = rTo.GetRed() - rFrom.GetRed();
158 aColor.SetRed( rFrom.GetRed() + std::min<sal_uInt8>( nDiff, 10 ) );
160 else if( rFrom.GetRed() > rTo.GetRed() )
162 nDiff = rFrom.GetRed() - rTo.GetRed();
163 aColor.SetRed( rFrom.GetRed() - std::min<sal_uInt8>( nDiff, 10 ) );
165 else
166 aColor.SetRed( rFrom.GetRed() );
168 // approach Green
169 if( rFrom.GetGreen() < rTo.GetGreen() )
171 nDiff = rTo.GetGreen() - rFrom.GetGreen();
172 aColor.SetGreen( rFrom.GetGreen() + std::min<sal_uInt8>( nDiff, 10 ) );
174 else if( rFrom.GetGreen() > rTo.GetGreen() )
176 nDiff = rFrom.GetGreen() - rTo.GetGreen();
177 aColor.SetGreen( rFrom.GetGreen() - std::min<sal_uInt8>( nDiff, 10 ) );
179 else
180 aColor.SetGreen( rFrom.GetGreen() );
182 // approach blue
183 if( rFrom.GetBlue() < rTo.GetBlue() )
185 nDiff = rTo.GetBlue() - rFrom.GetBlue();
186 aColor.SetBlue( rFrom.GetBlue() + std::min<sal_uInt8>( nDiff, 10 ) );
188 else if( rFrom.GetBlue() > rTo.GetBlue() )
190 nDiff = rFrom.GetBlue() - rTo.GetBlue();
191 aColor.SetBlue( rFrom.GetBlue() - std::min<sal_uInt8>( nDiff, 10 ) );
193 else
194 aColor.SetBlue( rFrom.GetBlue() );
196 return aColor;
199 #define DELTA 5.0
200 void MyWin::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle& rRect)
202 WorkWindow::Paint(rRenderContext, rRect);
204 rRenderContext.Push();
205 MapMode aMapMode(MapUnit::Map100thMM);
207 rRenderContext.SetMapMode(aMapMode);
209 Size aPaperSize = rRenderContext.GetOutputSize();
210 Point aCenter(aPaperSize.Width() / 2 - 300,
211 (aPaperSize.Height() - 8400) / 2 + 8400);
212 Point aP1(aPaperSize.Width() / 48, 0), aP2(aPaperSize.Width() / 40, 0);
213 Point aPoint;
215 rRenderContext.DrawRect(tools::Rectangle(Point(0, 0), aPaperSize));
216 rRenderContext.DrawRect(tools::Rectangle(Point(100, 100),
217 Size(aPaperSize.Width() - 200,
218 aPaperSize.Height() - 200)));
219 rRenderContext.DrawRect(tools::Rectangle(Point(200, 200),
220 Size(aPaperSize.Width() - 400,
221 aPaperSize.Height() - 400)));
222 rRenderContext.DrawRect(tools::Rectangle(Point(300, 300),
223 Size(aPaperSize.Width() - 600,
224 aPaperSize.Height() - 600)));
226 const int nFontCount = rRenderContext.GetDevFontCount();
227 const int nFontSamples = (nFontCount < 15) ? nFontCount : 15;
228 for (int i = 0; i < nFontSamples; ++i)
231 FontMetric aFont = rRenderContext.GetDevFont((i * nFontCount) / nFontSamples);
232 aFont.SetFontHeight(400 + (i % 7) * 100);
233 aFont.SetOrientation(i * (3600 / nFontSamples));
234 rRenderContext.SetFont(aFont);
236 sal_uInt8 nRed = (i << 6) & 0xC0;
237 sal_uInt8 nGreen = (i << 4) & 0xC0;
238 sal_uInt8 nBlue = (i << 2) & 0xC0;
239 rRenderContext.SetTextColor(Color(nRed, nGreen, nBlue));
241 OUString aPrintText = "SVP test program";
243 rRenderContext.DrawText(tools::Rectangle(Point((aPaperSize.Width() - 4000) / 2, 2000),
244 Size(aPaperSize.Width() - 2100, aPaperSize.Height() - 4000)),
245 aPrintText,
246 DrawTextFlags::MultiLine);
249 rRenderContext.SetFillColor();
250 DrawRect(tools::Rectangle(Point(aPaperSize.Width() - 4000, 1000),
251 Size(3000, 3000)));
252 rRenderContext.DrawBitmap(Point(aPaperSize.Width() - 4000, 1000),
253 Size( 3000,3000 ),
254 m_aBitmap);
256 Color const aWhite(0xff, 0xff, 0xff);
257 Color const aBlack(0, 0, 0);
258 Color const aLightRed(0xff, 0, 0);
259 Color const aDarkRed(0x40, 0, 0);
260 Color const aLightBlue(0, 0, 0xff);
261 Color const aDarkBlue(0,0,0x40);
262 Color const aLightGreen(0, 0xff, 0);
263 Color const aDarkGreen(0, 0x40, 0);
265 Gradient aGradient(GradientStyle::Linear, aBlack, aWhite);
266 aGradient.SetAngle(900);
267 rRenderContext.DrawGradient(tools::Rectangle(Point(1000, 4500),
268 Size(aPaperSize.Width() - 2000, 500)),
269 aGradient);
270 aGradient.SetStartColor(aDarkRed);
271 aGradient.SetEndColor(aLightBlue);
272 rRenderContext.DrawGradient(tools::Rectangle(Point(1000, 5300),
273 Size(aPaperSize.Width() - 2000, 500)),
274 aGradient);
275 aGradient.SetStartColor(aDarkBlue);
276 aGradient.SetEndColor(aLightGreen);
277 rRenderContext.DrawGradient(tools::Rectangle(Point(1000, 6100),
278 Size(aPaperSize.Width() - 2000, 500)),
279 aGradient);
280 aGradient.SetStartColor(aDarkGreen);
281 aGradient.SetEndColor(aLightRed);
282 rRenderContext.DrawGradient(tools::Rectangle(Point(1000, 6900),
283 Size(aPaperSize.Width() - 2000, 500)),
284 aGradient);
286 LineInfo aLineInfo(LineStyle::Solid, 200);
287 const double sind = sin(basegfx::deg2rad(DELTA));
288 const double cosd = cos(basegfx::deg2rad(DELTA));
289 const double factor = 1 + (DELTA / 1000.0);
290 int n = 0;
291 Color aLineColor(0, 0, 0);
292 Color aApproachColor(0, 0, 200);
294 while (aP2.X() < aCenter.X() && n++ < 680)
296 aLineInfo.SetWidth(n / 3);
297 aLineColor = approachColor(aLineColor, aApproachColor);
298 rRenderContext.SetLineColor(aLineColor);
300 // switch approach color
301 if (aApproachColor.IsRGBEqual(aLineColor))
303 if (aApproachColor.GetRed())
304 aApproachColor = Color(0, 0, 200);
305 else if (aApproachColor.GetGreen())
306 aApproachColor = Color(200, 0, 0);
307 else
308 aApproachColor = Color(0, 200, 0);
311 rRenderContext.DrawLine(project(aP1) + aCenter,
312 project(aP2) + aCenter,
313 aLineInfo);
314 aPoint.setX( static_cast<int>((static_cast<double>(aP1.X())*cosd - static_cast<double>(aP1.Y())*sind)*factor) );
315 aPoint.setY( static_cast<int>((static_cast<double>(aP1.Y())*cosd + static_cast<double>(aP1.X())*sind)*factor) );
316 aP1 = aPoint;
317 aPoint.setX( static_cast<int>((static_cast<double>(aP2.X())*cosd - static_cast<double>(aP2.Y())*sind)*factor) );
318 aPoint.setY( static_cast<int>((static_cast<double>(aP2.Y())*cosd + static_cast<double>(aP2.X())*sind)*factor) );
319 aP2 = aPoint;
321 rRenderContext.Pop();
324 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */