Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / vcl / workben / svptest.cxx
blob2e679bb7f7563eb68f68263d7cc9e54d496e615f
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 <tools/extendapplicationenvironment.hxx>
23 #include <cppuhelper/bootstrap.hxx>
24 #include <comphelper/processfactory.hxx>
26 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
27 #include <com/sun/star/uno/XComponentContext.hpp>
29 #include <vcl/event.hxx>
30 #include <vcl/svapp.hxx>
31 #include <vcl/wrkwin.hxx>
32 #include <vcl/gradient.hxx>
33 #include <vcl/lineinfo.hxx>
34 #include <vcl/bitmap.hxx>
35 #include <vcl/bitmapaccess.hxx>
36 #include <vcl/metric.hxx>
37 #include <vcl/vclptr.hxx>
39 #include <rtl/ustrbuf.hxx>
41 #include <math.h>
43 using namespace ::com::sun::star::uno;
44 using namespace ::com::sun::star::lang;
45 using namespace cppu;
47 // Forward declaration
48 void Main();
50 SAL_IMPLEMENT_MAIN()
52 try
54 tools::extendApplicationEnvironment();
56 Reference< XComponentContext > xContext = defaultBootstrap_InitialComponentContext();
57 Reference< XMultiServiceFactory > xServiceManager( xContext->getServiceManager(), UNO_QUERY );
59 if( !xServiceManager.is() )
60 Application::Abort( "Failed to bootstrap" );
62 comphelper::setProcessServiceFactory( xServiceManager );
64 InitVCL();
65 ::Main();
66 DeInitVCL();
68 catch (const Exception& e)
70 SAL_WARN("vcl.app", "Fatal exception: " << e.Message);
71 return 1;
73 catch (const std::exception &e)
75 fprintf(stderr, "fatal error: %s\n", e.what());
76 return 1;
79 return 0;
82 class MyWin : public WorkWindow
84 Bitmap m_aBitmap;
85 public:
86 MyWin( vcl::Window* pParent, WinBits nWinStyle );
88 virtual void Paint( vcl::RenderContext& /*rRenderContext*/, const tools::Rectangle& rRect ) override;
91 void Main()
93 ScopedVclPtrInstance< MyWin > aMainWin( nullptr, WB_APP | WB_STDWORK );
94 aMainWin->SetText( "VCL - Workbench" );
95 aMainWin->Show();
97 Application::Execute();
100 MyWin::MyWin( vcl::Window* pParent, WinBits nWinStyle ) :
101 WorkWindow( pParent, nWinStyle ),
102 m_aBitmap( Size( 256, 256 ), 32 )
104 // prepare an alpha mask
105 BitmapWriteAccess* pAcc = m_aBitmap.AcquireWriteAccess();
106 for( int nX = 0; nX < 256; nX++ )
108 for( int nY = 0; nY < 256; nY++ )
110 double fRed = 255.0-1.5*sqrt((double)(nX*nX+nY*nY));
111 if( fRed < 0.0 )
112 fRed = 0.0;
113 double fGreen = 255.0-1.5*sqrt((double)(((255-nX)*(255-nX)+nY*nY)));
114 if( fGreen < 0.0 )
115 fGreen = 0.0;
116 double fBlue = 255.0-1.5*sqrt((double)((128-nX)*(128-nX)+(255-nY)*(255-nY)));
117 if( fBlue < 0.0 )
118 fBlue = 0.0;
119 pAcc->SetPixel( nY, nX, BitmapColor( sal_uInt8(fRed), sal_uInt8(fGreen), sal_uInt8(fBlue) ) );
122 Bitmap::ReleaseAccess( pAcc );
125 static Point project( const Point& rPoint )
127 const double angle_x = M_PI / 6.0;
128 const double angle_z = M_PI / 6.0;
130 // transform planar coordinates to 3d
131 double x = rPoint.X();
132 double y = rPoint.Y();
134 // rotate around X axis
135 double x1 = x;
136 double y1 = y * cos( angle_x );
137 double z1 = y * sin( angle_x );
139 // rotate around Z axis
140 double x2 = x1 * cos( angle_z ) + y1 * sin( angle_z );
141 //double y2 = y1 * cos( angle_z ) - x1 * sin( angle_z );
142 double z2 = z1;
144 return Point( (sal_Int32)x2, (sal_Int32)z2 );
147 static Color approachColor( const Color& rFrom, const Color& rTo )
149 Color aColor;
150 sal_uInt8 nDiff;
151 // approach red
152 if( rFrom.GetRed() < rTo.GetRed() )
154 nDiff = rTo.GetRed() - rFrom.GetRed();
155 aColor.SetRed( rFrom.GetRed() + ( nDiff < 10 ? nDiff : 10 ) );
157 else if( rFrom.GetRed() > rTo.GetRed() )
159 nDiff = rFrom.GetRed() - rTo.GetRed();
160 aColor.SetRed( rFrom.GetRed() - ( nDiff < 10 ? nDiff : 10 ) );
162 else
163 aColor.SetRed( rFrom.GetRed() );
165 // approach Green
166 if( rFrom.GetGreen() < rTo.GetGreen() )
168 nDiff = rTo.GetGreen() - rFrom.GetGreen();
169 aColor.SetGreen( rFrom.GetGreen() + ( nDiff < 10 ? nDiff : 10 ) );
171 else if( rFrom.GetGreen() > rTo.GetGreen() )
173 nDiff = rFrom.GetGreen() - rTo.GetGreen();
174 aColor.SetGreen( rFrom.GetGreen() - ( nDiff < 10 ? nDiff : 10 ) );
176 else
177 aColor.SetGreen( rFrom.GetGreen() );
179 // approach blue
180 if( rFrom.GetBlue() < rTo.GetBlue() )
182 nDiff = rTo.GetBlue() - rFrom.GetBlue();
183 aColor.SetBlue( rFrom.GetBlue() + ( nDiff < 10 ? nDiff : 10 ) );
185 else if( rFrom.GetBlue() > rTo.GetBlue() )
187 nDiff = rFrom.GetBlue() - rTo.GetBlue();
188 aColor.SetBlue( rFrom.GetBlue() - ( nDiff < 10 ? nDiff : 10 ) );
190 else
191 aColor.SetBlue( rFrom.GetBlue() );
193 return aColor;
196 #define DELTA 5.0
197 void MyWin::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle& rRect)
199 WorkWindow::Paint(rRenderContext, rRect);
201 rRenderContext.Push();
202 MapMode aMapMode(MapUnit::Map100thMM);
204 rRenderContext.SetMapMode(aMapMode);
206 Size aPaperSize = rRenderContext.GetOutputSize();
207 Point aCenter(aPaperSize.Width() / 2 - 300,
208 (aPaperSize.Height() - 8400) / 2 + 8400);
209 Point aP1(aPaperSize.Width() / 48, 0), aP2(aPaperSize.Width() / 40, 0);
210 Point aPoint;
212 rRenderContext.DrawRect(tools::Rectangle(Point(0, 0), aPaperSize));
213 rRenderContext.DrawRect(tools::Rectangle(Point(100, 100),
214 Size(aPaperSize.Width() - 200,
215 aPaperSize.Height() - 200)));
216 rRenderContext.DrawRect(tools::Rectangle(Point(200, 200),
217 Size(aPaperSize.Width() - 400,
218 aPaperSize.Height() - 400)));
219 rRenderContext.DrawRect(tools::Rectangle(Point(300, 300),
220 Size(aPaperSize.Width() - 600,
221 aPaperSize.Height() - 600)));
223 const int nFontCount = rRenderContext.GetDevFontCount();
224 const int nFontSamples = (nFontCount < 15) ? nFontCount : 15;
225 for (int i = 0; i < nFontSamples; ++i)
228 FontMetric aFont = rRenderContext.GetDevFont((i * nFontCount) / nFontSamples);
229 aFont.SetFontHeight(400 + (i % 7) * 100);
230 aFont.SetOrientation(i * (3600 / nFontSamples));
231 rRenderContext.SetFont(aFont);
233 sal_uInt8 nRed = (i << 6) & 0xC0;
234 sal_uInt8 nGreen = (i << 4) & 0xC0;
235 sal_uInt8 nBlue = (i << 2) & 0xC0;
236 rRenderContext.SetTextColor(Color(nRed, nGreen, nBlue));
238 OUStringBuffer aPrintText(1024);
239 long nMaxWidth = 0;
241 aPrintText.append( "SVP test program" );
243 rRenderContext.DrawText(tools::Rectangle(Point((aPaperSize.Width() - 4000) / 2, 2000),
244 Size(aPaperSize.Width() - 2100 - nMaxWidth, aPaperSize.Height() - 4000)),
245 aPrintText.makeStringAndClear(),
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 aWhite(0xff, 0xff, 0xff);
257 Color aBlack(0, 0, 0);
258 Color aLightRed(0xff, 0, 0);
259 Color aDarkRed(0x40, 0, 0);
260 Color aLightBlue(0, 0, 0xff);
261 Color aDarkBlue(0,0,0x40);
262 Color aLightGreen(0, 0xff, 0);
263 Color 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 double sind = sin(DELTA * M_PI / 180.0);
288 double cosd = cos(DELTA * M_PI / 180.0);
289 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.X() = (int)((((double)aP1.X())*cosd - ((double)aP1.Y())*sind)*factor);
315 aPoint.Y() = (int)((((double)aP1.Y())*cosd + ((double)aP1.X())*sind)*factor);
316 aP1 = aPoint;
317 aPoint.X() = (int)((((double)aP2.X())*cosd - ((double)aP2.Y())*sind)*factor);
318 aPoint.Y() = (int)((((double)aP2.Y())*cosd + ((double)aP2.X())*sind)*factor);
319 aP2 = aPoint;
321 rRenderContext.Pop();
324 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */