bump product version to 5.0.4.1
[LibreOffice.git] / vcl / workben / svptest.cxx
blob2ebea2dc44c3a6239cf5571646f3c7ee84177370
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/bmpacc.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;
74 return 0;
77 class MyWin : public WorkWindow
79 Bitmap m_aBitmap;
80 public:
81 MyWin( vcl::Window* pParent, WinBits nWinStyle );
83 virtual void MouseMove( const MouseEvent& rMEvt ) SAL_OVERRIDE;
84 virtual void MouseButtonDown( const MouseEvent& rMEvt ) SAL_OVERRIDE;
85 virtual void MouseButtonUp( const MouseEvent& rMEvt ) SAL_OVERRIDE;
86 virtual void KeyInput( const KeyEvent& rKEvt ) SAL_OVERRIDE;
87 virtual void KeyUp( const KeyEvent& rKEvt ) SAL_OVERRIDE;
88 virtual void Paint( vcl::RenderContext& /*rRenderContext*/, const Rectangle& rRect ) SAL_OVERRIDE;
89 virtual void Resize() SAL_OVERRIDE;
92 void Main()
94 ScopedVclPtrInstance< MyWin > aMainWin( nullptr, WB_APP | WB_STDWORK );
95 aMainWin->SetText( OUString( "VCL - Workbench" ) );
96 aMainWin->Show();
98 Application::Execute();
101 MyWin::MyWin( vcl::Window* pParent, WinBits nWinStyle ) :
102 WorkWindow( pParent, nWinStyle ),
103 m_aBitmap( Size( 256, 256 ), 32 )
105 // prepare an alpha mask
106 BitmapWriteAccess* pAcc = m_aBitmap.AcquireWriteAccess();
107 for( int nX = 0; nX < 256; nX++ )
109 for( int nY = 0; nY < 256; nY++ )
111 double fRed = 255.0-1.5*sqrt((double)(nX*nX+nY*nY));
112 if( fRed < 0.0 )
113 fRed = 0.0;
114 double fGreen = 255.0-1.5*sqrt((double)(((255-nX)*(255-nX)+nY*nY)));
115 if( fGreen < 0.0 )
116 fGreen = 0.0;
117 double fBlue = 255.0-1.5*sqrt((double)((128-nX)*(128-nX)+(255-nY)*(255-nY)));
118 if( fBlue < 0.0 )
119 fBlue = 0.0;
120 pAcc->SetPixel( nY, nX, BitmapColor( sal_uInt8(fRed), sal_uInt8(fGreen), sal_uInt8(fBlue) ) );
123 Bitmap::ReleaseAccess( pAcc );
126 void MyWin::MouseMove( const MouseEvent& rMEvt )
128 WorkWindow::MouseMove( rMEvt );
131 void MyWin::MouseButtonDown( const MouseEvent& rMEvt )
133 WorkWindow::MouseButtonDown( rMEvt );
136 void MyWin::MouseButtonUp( const MouseEvent& rMEvt )
138 WorkWindow::MouseButtonUp( rMEvt );
141 void MyWin::KeyInput( const KeyEvent& rKEvt )
143 WorkWindow::KeyInput( rKEvt );
146 void MyWin::KeyUp( const KeyEvent& rKEvt )
148 WorkWindow::KeyUp( rKEvt );
151 static Point project( const Point& rPoint )
153 const double angle_x = M_PI / 6.0;
154 const double angle_z = M_PI / 6.0;
156 // transform planar coordinates to 3d
157 double x = rPoint.X();
158 double y = rPoint.Y();
160 // rotate around X axis
161 double x1 = x;
162 double y1 = y * cos( angle_x );
163 double z1 = y * sin( angle_x );
165 // rotate around Z axis
166 double x2 = x1 * cos( angle_z ) + y1 * sin( angle_z );
167 //double y2 = y1 * cos( angle_z ) - x1 * sin( angle_z );
168 double z2 = z1;
170 return Point( (sal_Int32)x2, (sal_Int32)z2 );
173 static Color approachColor( const Color& rFrom, const Color& rTo )
175 Color aColor;
176 sal_uInt8 nDiff;
177 // approach red
178 if( rFrom.GetRed() < rTo.GetRed() )
180 nDiff = rTo.GetRed() - rFrom.GetRed();
181 aColor.SetRed( rFrom.GetRed() + ( nDiff < 10 ? nDiff : 10 ) );
183 else if( rFrom.GetRed() > rTo.GetRed() )
185 nDiff = rFrom.GetRed() - rTo.GetRed();
186 aColor.SetRed( rFrom.GetRed() - ( nDiff < 10 ? nDiff : 10 ) );
188 else
189 aColor.SetRed( rFrom.GetRed() );
191 // approach Green
192 if( rFrom.GetGreen() < rTo.GetGreen() )
194 nDiff = rTo.GetGreen() - rFrom.GetGreen();
195 aColor.SetGreen( rFrom.GetGreen() + ( nDiff < 10 ? nDiff : 10 ) );
197 else if( rFrom.GetGreen() > rTo.GetGreen() )
199 nDiff = rFrom.GetGreen() - rTo.GetGreen();
200 aColor.SetGreen( rFrom.GetGreen() - ( nDiff < 10 ? nDiff : 10 ) );
202 else
203 aColor.SetGreen( rFrom.GetGreen() );
205 // approach blue
206 if( rFrom.GetBlue() < rTo.GetBlue() )
208 nDiff = rTo.GetBlue() - rFrom.GetBlue();
209 aColor.SetBlue( rFrom.GetBlue() + ( nDiff < 10 ? nDiff : 10 ) );
211 else if( rFrom.GetBlue() > rTo.GetBlue() )
213 nDiff = rFrom.GetBlue() - rTo.GetBlue();
214 aColor.SetBlue( rFrom.GetBlue() - ( nDiff < 10 ? nDiff : 10 ) );
216 else
217 aColor.SetBlue( rFrom.GetBlue() );
219 return aColor;
222 #define DELTA 5.0
223 void MyWin::Paint(vcl::RenderContext& rRenderContext, const Rectangle& rRect)
225 WorkWindow::Paint(rRenderContext, rRect);
227 rRenderContext.Push(PushFlags::ALL);
228 MapMode aMapMode(MAP_100TH_MM);
230 rRenderContext.SetMapMode(aMapMode);
232 Size aPaperSize = rRenderContext.GetOutputSize();
233 Point aCenter(aPaperSize.Width() / 2 - 300,
234 (aPaperSize.Height() - 8400) / 2 + 8400);
235 Point aP1(aPaperSize.Width() / 48, 0), aP2(aPaperSize.Width() / 40, 0);
236 Point aPoint;
238 rRenderContext.DrawRect(Rectangle(Point(0, 0), aPaperSize));
239 rRenderContext.DrawRect(Rectangle(Point(100, 100),
240 Size(aPaperSize.Width() - 200,
241 aPaperSize.Height() - 200)));
242 rRenderContext.DrawRect(Rectangle(Point(200, 200),
243 Size(aPaperSize.Width() - 400,
244 aPaperSize.Height() - 400)));
245 rRenderContext.DrawRect(Rectangle(Point(300, 300),
246 Size(aPaperSize.Width() - 600,
247 aPaperSize.Height() - 600)));
249 const int nFontCount = rRenderContext.GetDevFontCount();
250 const int nFontSamples = (nFontCount < 15) ? nFontCount : 15;
251 for (int i = 0; i < nFontSamples; ++i)
254 vcl::FontInfo aFont = rRenderContext.GetDevFont((i * nFontCount) / nFontSamples);
255 aFont.SetHeight(400 + (i % 7) * 100);
256 aFont.SetOrientation(i * (3600 / nFontSamples));
257 rRenderContext.SetFont(aFont);
259 sal_uInt8 nRed = (i << 6) & 0xC0;
260 sal_uInt8 nGreen = (i << 4) & 0xC0;
261 sal_uInt8 nBlue = (i << 2) & 0xC0;
262 rRenderContext.SetTextColor(Color(nRed, nGreen, nBlue));
264 OUStringBuffer aPrintText(1024);
265 long nMaxWidth = 0;
267 aPrintText.appendAscii( "SVP test program" );
269 rRenderContext.DrawText(Rectangle(Point((aPaperSize.Width() - 4000) / 2, 2000),
270 Size(aPaperSize.Width() - 2100 - nMaxWidth, aPaperSize.Height() - 4000)),
271 aPrintText.makeStringAndClear(),
272 DrawTextFlags::MultiLine);
275 rRenderContext.SetFillColor();
276 DrawRect(Rectangle(Point(aPaperSize.Width() - 4000, 1000),
277 Size(3000, 3000)));
278 rRenderContext.DrawBitmap(Point(aPaperSize.Width() - 4000, 1000),
279 Size( 3000,3000 ),
280 m_aBitmap);
282 Color aWhite(0xff, 0xff, 0xff);
283 Color aBlack(0, 0, 0);
284 Color aLightRed(0xff, 0, 0);
285 Color aDarkRed(0x40, 0, 0);
286 Color aLightBlue(0, 0, 0xff);
287 Color aDarkBlue(0,0,0x40);
288 Color aLightGreen(0, 0xff, 0);
289 Color aDarkGreen(0, 0x40, 0);
291 Gradient aGradient(GradientStyle_LINEAR, aBlack, aWhite);
292 aGradient.SetAngle(900);
293 rRenderContext.DrawGradient(Rectangle(Point(1000, 4500),
294 Size(aPaperSize.Width() - 2000, 500)),
295 aGradient);
296 aGradient.SetStartColor(aDarkRed);
297 aGradient.SetEndColor(aLightBlue);
298 rRenderContext.DrawGradient(Rectangle(Point(1000, 5300),
299 Size(aPaperSize.Width() - 2000, 500)),
300 aGradient);
301 aGradient.SetStartColor(aDarkBlue);
302 aGradient.SetEndColor(aLightGreen);
303 rRenderContext.DrawGradient(Rectangle(Point(1000, 6100),
304 Size(aPaperSize.Width() - 2000, 500)),
305 aGradient);
306 aGradient.SetStartColor(aDarkGreen);
307 aGradient.SetEndColor(aLightRed);
308 rRenderContext.DrawGradient(Rectangle(Point(1000, 6900),
309 Size(aPaperSize.Width() - 2000, 500)),
310 aGradient);
312 LineInfo aLineInfo(LINE_SOLID, 200);
313 double sind = sin(DELTA * M_PI / 180.0);
314 double cosd = cos(DELTA * M_PI / 180.0);
315 double factor = 1 + (DELTA / 1000.0);
316 int n = 0;
317 Color aLineColor(0, 0, 0);
318 Color aApproachColor(0, 0, 200);
320 while (aP2.X() < aCenter.X() && n++ < 680)
322 aLineInfo.SetWidth(n / 3);
323 aLineColor = approachColor(aLineColor, aApproachColor);
324 rRenderContext.SetLineColor(aLineColor);
326 // switch aproach color
327 if (aApproachColor.IsRGBEqual(aLineColor))
329 if (aApproachColor.GetRed())
330 aApproachColor = Color(0, 0, 200);
331 else if (aApproachColor.GetGreen())
332 aApproachColor = Color(200, 0, 0);
333 else
334 aApproachColor = Color(0, 200, 0);
337 rRenderContext.DrawLine(project(aP1) + aCenter,
338 project(aP2) + aCenter,
339 aLineInfo);
340 aPoint.X() = (int)((((double)aP1.X())*cosd - ((double)aP1.Y())*sind)*factor);
341 aPoint.Y() = (int)((((double)aP1.Y())*cosd + ((double)aP1.X())*sind)*factor);
342 aP1 = aPoint;
343 aPoint.X() = (int)((((double)aP2.X())*cosd - ((double)aP2.Y())*sind)*factor);
344 aPoint.Y() = (int)((((double)aP2.Y())*cosd + ((double)aP2.X())*sind)*factor);
345 aP2 = aPoint;
347 rRenderContext.Pop();
350 void MyWin::Resize()
352 WorkWindow::Resize();
355 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */