calc: on editing invalidation of view with different zoom is wrong
[LibreOffice.git] / vcl / workben / svptest.cxx
bloba969bff5e242de5a6c8386b466d716966ec05c5d
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/config.h>
22 #include <sal/main.h>
23 #include <sal/log.hxx>
24 #include <comphelper/diagnose_ex.hxx>
25 #include <tools/extendapplicationenvironment.hxx>
27 #include <cppuhelper/bootstrap.hxx>
28 #include <comphelper/processfactory.hxx>
30 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
31 #include <com/sun/star/uno/XComponentContext.hpp>
33 #include <vcl/event.hxx>
34 #include <vcl/svapp.hxx>
35 #include <vcl/wrkwin.hxx>
36 #include <vcl/gradient.hxx>
37 #include <vcl/lineinfo.hxx>
38 #include <vcl/bitmap.hxx>
39 #include <vcl/metric.hxx>
40 #include <vcl/vclptr.hxx>
41 #include <bitmap/BitmapWriteAccess.hxx>
43 #include <rtl/ustrbuf.hxx>
45 #include <math.h>
47 using namespace ::com::sun::star::uno;
48 using namespace ::com::sun::star::lang;
49 using namespace cppu;
51 // Forward declaration
52 static void Main();
54 SAL_IMPLEMENT_MAIN()
56 try
58 tools::extendApplicationEnvironment();
60 Reference< XComponentContext > xContext = defaultBootstrap_InitialComponentContext();
61 Reference< XMultiServiceFactory > xServiceManager( xContext->getServiceManager(), UNO_QUERY );
63 if( !xServiceManager.is() )
64 Application::Abort( "Failed to bootstrap" );
66 comphelper::setProcessServiceFactory( xServiceManager );
68 InitVCL();
69 ::Main();
70 DeInitVCL();
72 catch (const Exception&)
74 TOOLS_WARN_EXCEPTION("vcl.app", "Fatal");
75 return 1;
77 catch (const std::exception &e)
79 fprintf(stderr, "fatal error: %s\n", e.what());
80 return 1;
83 return 0;
86 namespace {
88 class MyWin : public WorkWindow
90 Bitmap m_aBitmap;
91 public:
92 MyWin( vcl::Window* pParent, WinBits nWinStyle );
94 virtual void Paint( vcl::RenderContext& /*rRenderContext*/, const tools::Rectangle& rRect ) override;
99 void Main()
101 ScopedVclPtrInstance< MyWin > aMainWin( nullptr, WB_APP | WB_STDWORK );
102 aMainWin->SetText( "VCL - Workbench" );
103 aMainWin->Show();
105 Application::Execute();
108 MyWin::MyWin( vcl::Window* pParent, WinBits nWinStyle ) :
109 WorkWindow( pParent, nWinStyle ),
110 m_aBitmap(Size(256, 256), vcl::PixelFormat::N32_BPP)
112 // prepare an alpha mask
113 BitmapWriteAccess* pAcc = m_aBitmap.AcquireWriteAccess();
114 for( int nX = 0; nX < 256; nX++ )
116 for( int nY = 0; nY < 256; nY++ )
118 double fRed = 255.0-1.5*std::hypot(nX, nY);
119 if( fRed < 0.0 )
120 fRed = 0.0;
121 double fGreen = 255.0-1.5*std::hypot(255-nX, nY);
122 if( fGreen < 0.0 )
123 fGreen = 0.0;
124 double fBlue = 255.0-1.5*std::hypot(128-nX, 255-nY);
125 if( fBlue < 0.0 )
126 fBlue = 0.0;
127 pAcc->SetPixel( nY, nX, BitmapColor( sal_uInt8(fRed), sal_uInt8(fGreen), sal_uInt8(fBlue) ) );
130 Bitmap::ReleaseAccess( pAcc );
133 static Point project( const Point& rPoint )
135 const double angle_x = M_PI / 6.0;
136 const double angle_z = M_PI / 6.0;
138 // transform planar coordinates to 3d
139 double x = rPoint.X();
140 double y = rPoint.Y();
142 // rotate around X axis
143 double x1 = x;
144 double y1 = y * cos( angle_x );
145 double z1 = y * sin( angle_x );
147 // rotate around Z axis
148 double x2 = x1 * cos( angle_z ) + y1 * sin( angle_z );
149 //double y2 = y1 * cos( angle_z ) - x1 * sin( angle_z );
150 double z2 = z1;
152 return Point( static_cast<sal_Int32>(x2), static_cast<sal_Int32>(z2) );
155 static Color approachColor( const Color& rFrom, const Color& rTo )
157 Color aColor;
158 sal_uInt8 nDiff;
159 // approach red
160 if( rFrom.GetRed() < rTo.GetRed() )
162 nDiff = rTo.GetRed() - rFrom.GetRed();
163 aColor.SetRed( rFrom.GetRed() + std::min<sal_uInt8>( nDiff, 10 ) );
165 else if( rFrom.GetRed() > rTo.GetRed() )
167 nDiff = rFrom.GetRed() - rTo.GetRed();
168 aColor.SetRed( rFrom.GetRed() - std::min<sal_uInt8>( nDiff, 10 ) );
170 else
171 aColor.SetRed( rFrom.GetRed() );
173 // approach Green
174 if( rFrom.GetGreen() < rTo.GetGreen() )
176 nDiff = rTo.GetGreen() - rFrom.GetGreen();
177 aColor.SetGreen( rFrom.GetGreen() + std::min<sal_uInt8>( nDiff, 10 ) );
179 else if( rFrom.GetGreen() > rTo.GetGreen() )
181 nDiff = rFrom.GetGreen() - rTo.GetGreen();
182 aColor.SetGreen( rFrom.GetGreen() - std::min<sal_uInt8>( nDiff, 10 ) );
184 else
185 aColor.SetGreen( rFrom.GetGreen() );
187 // approach blue
188 if( rFrom.GetBlue() < rTo.GetBlue() )
190 nDiff = rTo.GetBlue() - rFrom.GetBlue();
191 aColor.SetBlue( rFrom.GetBlue() + std::min<sal_uInt8>( nDiff, 10 ) );
193 else if( rFrom.GetBlue() > rTo.GetBlue() )
195 nDiff = rFrom.GetBlue() - rTo.GetBlue();
196 aColor.SetBlue( rFrom.GetBlue() - std::min<sal_uInt8>( nDiff, 10 ) );
198 else
199 aColor.SetBlue( rFrom.GetBlue() );
201 return aColor;
204 #define DELTA 5.0
205 void MyWin::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle& rRect)
207 WorkWindow::Paint(rRenderContext, rRect);
209 rRenderContext.Push();
210 MapMode aMapMode(MapUnit::Map100thMM);
212 rRenderContext.SetMapMode(aMapMode);
214 Size aPaperSize = rRenderContext.GetOutputSize();
215 Point aCenter(aPaperSize.Width() / 2 - 300,
216 (aPaperSize.Height() - 8400) / 2 + 8400);
217 Point aP1(aPaperSize.Width() / 48, 0), aP2(aPaperSize.Width() / 40, 0);
218 Point aPoint;
220 rRenderContext.DrawRect(tools::Rectangle(Point(0, 0), aPaperSize));
221 rRenderContext.DrawRect(tools::Rectangle(Point(100, 100),
222 Size(aPaperSize.Width() - 200,
223 aPaperSize.Height() - 200)));
224 rRenderContext.DrawRect(tools::Rectangle(Point(200, 200),
225 Size(aPaperSize.Width() - 400,
226 aPaperSize.Height() - 400)));
227 rRenderContext.DrawRect(tools::Rectangle(Point(300, 300),
228 Size(aPaperSize.Width() - 600,
229 aPaperSize.Height() - 600)));
231 const int nFontCount = rRenderContext.GetFontFaceCollectionCount();
232 const int nFontSamples = (nFontCount < 15) ? nFontCount : 15;
233 for (int i = 0; i < nFontSamples; ++i)
236 FontMetric aFont(rRenderContext.GetFontMetricFromCollection((i * nFontCount) / nFontSamples));
237 aFont.SetFontHeight(400 + (i % 7) * 100);
238 aFont.SetOrientation(Degree10(i * (3600 / nFontSamples)));
239 rRenderContext.SetFont(aFont);
241 sal_uInt8 nRed = (i << 6) & 0xC0;
242 sal_uInt8 nGreen = (i << 4) & 0xC0;
243 sal_uInt8 nBlue = (i << 2) & 0xC0;
244 rRenderContext.SetTextColor(Color(nRed, nGreen, nBlue));
246 rRenderContext.DrawText(tools::Rectangle(Point((aPaperSize.Width() - 4000) / 2, 2000),
247 Size(aPaperSize.Width() - 2100, aPaperSize.Height() - 4000)),
248 "SVP test program",
249 DrawTextFlags::MultiLine);
252 rRenderContext.SetFillColor();
253 rRenderContext.DrawRect(tools::Rectangle(Point(aPaperSize.Width() - 4000, 1000),
254 Size(3000, 3000)));
255 rRenderContext.DrawBitmap(Point(aPaperSize.Width() - 4000, 1000),
256 Size( 3000,3000 ),
257 m_aBitmap);
259 Color const aWhite(0xff, 0xff, 0xff);
260 Color const aBlack(0, 0, 0);
261 Color const aLightRed(0xff, 0, 0);
262 Color const aDarkRed(0x40, 0, 0);
263 Color const aLightBlue(0, 0, 0xff);
264 Color const aDarkBlue(0,0,0x40);
265 Color const aLightGreen(0, 0xff, 0);
266 Color const aDarkGreen(0, 0x40, 0);
268 Gradient aGradient(css::awt::GradientStyle_LINEAR, aBlack, aWhite);
269 aGradient.SetAngle(900_deg10);
270 rRenderContext.DrawGradient(tools::Rectangle(Point(1000, 4500),
271 Size(aPaperSize.Width() - 2000, 500)),
272 aGradient);
273 aGradient.SetStartColor(aDarkRed);
274 aGradient.SetEndColor(aLightBlue);
275 rRenderContext.DrawGradient(tools::Rectangle(Point(1000, 5300),
276 Size(aPaperSize.Width() - 2000, 500)),
277 aGradient);
278 aGradient.SetStartColor(aDarkBlue);
279 aGradient.SetEndColor(aLightGreen);
280 rRenderContext.DrawGradient(tools::Rectangle(Point(1000, 6100),
281 Size(aPaperSize.Width() - 2000, 500)),
282 aGradient);
283 aGradient.SetStartColor(aDarkGreen);
284 aGradient.SetEndColor(aLightRed);
285 rRenderContext.DrawGradient(tools::Rectangle(Point(1000, 6900),
286 Size(aPaperSize.Width() - 2000, 500)),
287 aGradient);
289 LineInfo aLineInfo(LineStyle::Solid, 200);
290 const double sind = sin(basegfx::deg2rad(DELTA));
291 const double cosd = cos(basegfx::deg2rad(DELTA));
292 const double factor = 1 + (DELTA / 1000.0);
293 int n = 0;
294 Color aLineColor(0, 0, 0);
295 Color aApproachColor(0, 0, 200);
297 while (aP2.X() < aCenter.X() && n++ < 680)
299 aLineInfo.SetWidth(n / 3);
300 aLineColor = approachColor(aLineColor, aApproachColor);
301 rRenderContext.SetLineColor(aLineColor);
303 // switch approach color
304 if (aApproachColor.IsRGBEqual(aLineColor))
306 if (aApproachColor.GetRed())
307 aApproachColor = Color(0, 0, 200);
308 else if (aApproachColor.GetGreen())
309 aApproachColor = Color(200, 0, 0);
310 else
311 aApproachColor = Color(0, 200, 0);
314 rRenderContext.DrawLine(project(aP1) + aCenter,
315 project(aP2) + aCenter,
316 aLineInfo);
317 aPoint.setX( static_cast<int>((static_cast<double>(aP1.X())*cosd - static_cast<double>(aP1.Y())*sind)*factor) );
318 aPoint.setY( static_cast<int>((static_cast<double>(aP1.Y())*cosd + static_cast<double>(aP1.X())*sind)*factor) );
319 aP1 = aPoint;
320 aPoint.setX( static_cast<int>((static_cast<double>(aP2.X())*cosd - static_cast<double>(aP2.Y())*sind)*factor) );
321 aPoint.setY( static_cast<int>((static_cast<double>(aP2.Y())*cosd + static_cast<double>(aP2.X())*sind)*factor) );
322 aP2 = aPoint;
324 rRenderContext.Pop();
327 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */