Bug Summary

File:home/maarten/src/libreoffice/core/toolkit/source/helper/vclunohelper.cxx
Warning:line 159, column 5
Use of memory after it is freed

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-unknown-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name vclunohelper.cxx -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -mframe-pointer=all -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -fno-split-dwarf-inlining -debugger-tuning=gdb -resource-dir /usr/lib64/clang/11.0.0 -D BOOST_ERROR_CODE_HEADER_ONLY -D BOOST_SYSTEM_NO_DEPRECATED -D CPPU_ENV=gcc3 -D LINUX -D OSL_DEBUG_LEVEL=1 -D SAL_LOG_INFO -D SAL_LOG_WARN -D UNIX -D UNX -D X86_64 -D _PTHREADS -D _REENTRANT -D TOOLKIT_DLLIMPLEMENTATION -D SVT_DLL_NAME="libsvtlo.so" -D EXCEPTIONS_ON -D LIBO_INTERNAL_ONLY -I /home/maarten/src/libreoffice/core/workdir/UnpackedTarball/epoxy/include -I /home/maarten/src/libreoffice/core/external/boost/include -I /home/maarten/src/libreoffice/core/workdir/UnpackedTarball/boost -I /home/maarten/src/libreoffice/core/include -I /usr/lib/jvm/java-11-openjdk-11.0.9.10-0.0.ea.fc33.x86_64/include -I /usr/lib/jvm/java-11-openjdk-11.0.9.10-0.0.ea.fc33.x86_64/include/linux -I /home/maarten/src/libreoffice/core/config_host -I /home/maarten/src/libreoffice/core/toolkit/inc -I /home/maarten/src/libreoffice/core/toolkit/source -I /home/maarten/src/libreoffice/core/workdir/UnoApiHeadersTarget/udkapi/normal -I /home/maarten/src/libreoffice/core/workdir/UnoApiHeadersTarget/offapi/normal -internal-isystem /usr/bin/../lib/gcc/x86_64-redhat-linux/10/../../../../include/c++/10 -internal-isystem /usr/bin/../lib/gcc/x86_64-redhat-linux/10/../../../../include/c++/10/x86_64-redhat-linux -internal-isystem /usr/bin/../lib/gcc/x86_64-redhat-linux/10/../../../../include/c++/10/backward -internal-isystem /usr/local/include -internal-isystem /usr/lib64/clang/11.0.0/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -O0 -Wno-missing-braces -std=c++17 -fdeprecated-macro -fdebug-compilation-dir /home/maarten/src/libreoffice/core -ferror-limit 19 -fvisibility hidden -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -debug-info-kind=constructor -analyzer-output=html -faddrsig -o /home/maarten/tmp/wis/scan-build-libreoffice/output/report/2020-10-07-141433-9725-1 -x c++ /home/maarten/src/libreoffice/core/toolkit/source/helper/vclunohelper.cxx

/home/maarten/src/libreoffice/core/toolkit/source/helper/vclunohelper.cxx

1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <tools/stream.hxx>
21#include <vcl/event.hxx>
22#include <vcl/window.hxx>
23#include <vcl/unohelp.hxx>
24#include <vcl/metric.hxx>
25#include <com/sun/star/util/MeasureUnit.hpp>
26#include <com/sun/star/awt/XBitmap.hpp>
27#include <com/sun/star/awt/XWindow.hpp>
28#include <com/sun/star/awt/XDevice.hpp>
29#include <com/sun/star/awt/SimpleFontMetric.hpp>
30#include <com/sun/star/awt/FontDescriptor.hpp>
31#include <com/sun/star/awt/XControlContainer.hpp>
32#include <com/sun/star/awt/KeyModifier.hpp>
33#include <com/sun/star/awt/MouseButton.hpp>
34#include <com/sun/star/embed/EmbedMapUnits.hpp>
35#include <com/sun/star/graphic/XGraphic.hpp>
36#include <toolkit/helper/vclunohelper.hxx>
37#include <toolkit/helper/convert.hxx>
38#include <awt/vclxbitmap.hxx>
39#include <awt/vclxregion.hxx>
40#include <toolkit/awt/vclxwindow.hxx>
41#include <awt/vclxgraphics.hxx>
42#include <toolkit/awt/vclxfont.hxx>
43#include <controls/unocontrolcontainer.hxx>
44#include <controls/unocontrolcontainermodel.hxx>
45#include <vcl/graph.hxx>
46#include <comphelper/processfactory.hxx>
47
48#include <com/sun/star/awt/Toolkit.hpp>
49#include <com/sun/star/awt/Size.hpp>
50#include <com/sun/star/awt/Point.hpp>
51#include <vcl/dibtools.hxx>
52
53using namespace ::com::sun::star;
54
55
56uno::Reference< css::awt::XToolkit> VCLUnoHelper::CreateToolkit()
57{
58 uno::Reference< uno::XComponentContext > xContext = ::comphelper::getProcessComponentContext();
59 uno::Reference< awt::XToolkit> xToolkit( awt::Toolkit::create(xContext), uno::UNO_QUERY_THROW );
60 return xToolkit;
61}
62
63BitmapEx VCLUnoHelper::GetBitmap( const css::uno::Reference< css::awt::XBitmap>& rxBitmap )
64{
65 BitmapEx aBmp;
66
67 css::uno::Reference< css::graphic::XGraphic > xGraphic( rxBitmap, css::uno::UNO_QUERY );
68 if( xGraphic.is() )
69 {
70 Graphic aGraphic( xGraphic );
71 aBmp = aGraphic.GetBitmapEx();
72 }
73 else if ( rxBitmap.is() )
74 {
75 VCLXBitmap* pVCLBitmap = comphelper::getUnoTunnelImplementation<VCLXBitmap>( rxBitmap );
76 if ( pVCLBitmap )
77 aBmp = pVCLBitmap->GetBitmap();
78 else
79 {
80 Bitmap aDIB, aMask;
81 {
82 css::uno::Sequence<sal_Int8> aBytes = rxBitmap->getDIB();
83 SvMemoryStream aMem( aBytes.getArray(), aBytes.getLength(), StreamMode::READ );
84 ReadDIB(aDIB, aMem, true);
85 }
86 {
87 css::uno::Sequence<sal_Int8> aBytes = rxBitmap->getMaskDIB();
88 SvMemoryStream aMem( aBytes.getArray(), aBytes.getLength(), StreamMode::READ );
89 ReadDIB(aMask, aMem, true);
90 }
91 aBmp = BitmapEx( aDIB, aMask );
92 }
93 }
94 return aBmp;
95}
96
97css::uno::Reference< css::awt::XBitmap> VCLUnoHelper::CreateBitmap( const BitmapEx& rBitmap )
98{
99 Graphic aGraphic( rBitmap );
100 css::uno::Reference< css::awt::XBitmap> xBmp( aGraphic.GetXGraphic(), css::uno::UNO_QUERY );
101 return xBmp;
102}
103
104css::uno::Reference< css::awt::XBitmap> VCLUnoHelper::CreateVCLXBitmap( const BitmapEx& rBitmap )
105{
106 return css::uno::Reference< css::awt::XBitmap >(new VCLXBitmap(rBitmap));
107}
108
109VclPtr< vcl::Window > VCLUnoHelper::GetWindow( const css::uno::Reference< css::awt::XWindow>& rxWindow )
110{
111 VCLXWindow* pVCLXWindow = comphelper::getUnoTunnelImplementation<VCLXWindow>( rxWindow );
112 return pVCLXWindow ? pVCLXWindow->GetWindow() : VclPtr< vcl::Window >();
113}
114
115VclPtr< vcl::Window > VCLUnoHelper::GetWindow( const css::uno::Reference< css::awt::XWindow2>& rxWindow )
116{
117 VCLXWindow* pVCLXWindow = comphelper::getUnoTunnelImplementation<VCLXWindow>( rxWindow );
118 return pVCLXWindow ? pVCLXWindow->GetWindow() : VclPtr< vcl::Window >();
119}
120
121VclPtr< vcl::Window > VCLUnoHelper::GetWindow( const css::uno::Reference< css::awt::XWindowPeer>& rxWindow )
122{
123 VCLXWindow* pVCLXWindow = comphelper::getUnoTunnelImplementation<VCLXWindow>( rxWindow );
124 return pVCLXWindow ? pVCLXWindow->GetWindow() : VclPtr< vcl::Window >();
125}
126
127vcl::Region VCLUnoHelper::GetRegion( const css::uno::Reference< css::awt::XRegion >& rxRegion )
128{
129 vcl::Region aRegion;
130 VCLXRegion* pVCLRegion = comphelper::getUnoTunnelImplementation<VCLXRegion>( rxRegion );
131 if ( pVCLRegion )
132 aRegion = pVCLRegion->GetRegion();
133 else
134 {
135 const css::uno::Sequence< css::awt::Rectangle > aRects = rxRegion->getRectangles();
136 for ( const auto& rRect : aRects )
137 aRegion.Union( VCLRectangle( rRect ) );
138 }
139 return aRegion;
140}
141
142css::uno::Reference< css::awt::XWindow> VCLUnoHelper::GetInterface( vcl::Window* pWindow )
143{
144 css::uno::Reference< css::awt::XWindow > xWin;
145 if ( pWindow )
146 {
147 css::uno::Reference< css::awt::XWindowPeer> xPeer = pWindow->GetComponentInterface();
148 xWin.set(xPeer, css::uno::UNO_QUERY);
149 }
150 return xWin;
151}
152
153OutputDevice* VCLUnoHelper::GetOutputDevice( const css::uno::Reference< css::awt::XDevice>& rxDevice )
154{
155 VclPtr<OutputDevice> pOutDev;
156 VCLXDevice* pDev = comphelper::getUnoTunnelImplementation<VCLXDevice>( rxDevice );
157 if ( pDev )
1
Assuming 'pDev' is non-null
2
Taking true branch
158 pOutDev = pDev->GetOutputDevice();
3
Calling implicit copy assignment operator for 'VclPtr<OutputDevice>'
4
Calling copy assignment operator for 'Reference<OutputDevice>'
10
Returning from copy assignment operator for 'Reference<OutputDevice>'
11
Returning from copy assignment operator for 'VclPtr<OutputDevice>'
159 return pOutDev;
12
Calling implicit destructor for 'VclPtr<OutputDevice>'
13
Calling '~Reference'
20
Returning from '~Reference'
21
Returning from destructor for 'VclPtr<OutputDevice>'
22
Use of memory after it is freed
160}
161
162OutputDevice* VCLUnoHelper::GetOutputDevice( const css::uno::Reference< css::awt::XGraphics>& rxGraphics )
163{
164 OutputDevice* pOutDev = nullptr;
165 VCLXGraphics* pGrf = comphelper::getUnoTunnelImplementation<VCLXGraphics>( rxGraphics );
166 if ( pGrf )
167 pOutDev = pGrf->GetOutputDevice();
168 return pOutDev;
169}
170
171tools::Polygon VCLUnoHelper::CreatePolygon( const css::uno::Sequence< sal_Int32 >& DataX,
172 const css::uno::Sequence< sal_Int32 >& DataY )
173{
174 sal_Int32 nLen = DataX.getLength();
175 const sal_Int32* pDataX = DataX.getConstArray();
176 const sal_Int32* pDataY = DataY.getConstArray();
177 tools::Polygon aPoly( static_cast<sal_uInt16>(nLen) );
178 for ( sal_Int32 n = 0; n < nLen; n++ )
179 {
180 Point aPnt;
181 aPnt.setX( pDataX[n] );
182 aPnt.setY( pDataY[n] );
183 aPoly[n] = aPnt;
184 }
185 return aPoly;
186}
187
188css::uno::Reference< css::awt::XControlContainer> VCLUnoHelper::CreateControlContainer( vcl::Window* pWindow )
189{
190 UnoControlContainer* pContainer = new UnoControlContainer( pWindow->GetComponentInterface() );
191 css::uno::Reference< css::awt::XControlContainer > x = pContainer;
192
193 UnoControlModel* pContainerModel = new UnoControlContainerModel( ::comphelper::getProcessComponentContext() );
194 pContainer->setModel( static_cast<css::awt::XControlModel*>(pContainerModel) );
195
196 return x;
197}
198
199css::awt::FontDescriptor VCLUnoHelper::CreateFontDescriptor( const vcl::Font& rFont )
200{
201 css::awt::FontDescriptor aFD;
202 aFD.Name = rFont.GetFamilyName();
203 aFD.StyleName = rFont.GetStyleName();
204 aFD.Height = static_cast<sal_Int16>(rFont.GetFontSize().Height());
205 aFD.Width = static_cast<sal_Int16>(rFont.GetFontSize().Width());
206 aFD.Family = sal::static_int_cast< sal_Int16 >(rFont.GetFamilyType());
207 aFD.CharSet = rFont.GetCharSet();
208 aFD.Pitch = sal::static_int_cast< sal_Int16 >(rFont.GetPitch());
209 aFD.CharacterWidth = vcl::unohelper::ConvertFontWidth(rFont.GetWidthType());
210 aFD.Weight = vcl::unohelper::ConvertFontWeight(rFont.GetWeight());
211 aFD.Slant = vcl::unohelper::ConvertFontSlant(rFont.GetItalic());
212 aFD.Underline = sal::static_int_cast< sal_Int16 >(rFont.GetUnderline());
213 aFD.Strikeout = sal::static_int_cast< sal_Int16 >(rFont.GetStrikeout());
214 aFD.Orientation = rFont.GetOrientation();
215 aFD.Kerning = rFont.IsKerning();
216 aFD.WordLineMode = rFont.IsWordLineMode();
217 aFD.Type = 0; // ??? => Only in Metric...
218 return aFD;
219}
220
221vcl::Font VCLUnoHelper::CreateFont( const css::awt::FontDescriptor& rDescr, const vcl::Font& rInitFont )
222{
223 vcl::Font aFont( rInitFont );
224 if ( !rDescr.Name.isEmpty() )
225 aFont.SetFamilyName( rDescr.Name );
226 if ( !rDescr.StyleName.isEmpty() )
227 aFont.SetStyleName( rDescr.StyleName );
228 if ( rDescr.Height )
229 aFont.SetFontSize( Size( rDescr.Width, rDescr.Height ) );
230 if ( static_cast<FontFamily>(rDescr.Family) != FAMILY_DONTKNOW )
231 aFont.SetFamily( static_cast<FontFamily>(rDescr.Family) );
232 if ( static_cast<rtl_TextEncoding>(rDescr.CharSet) != RTL_TEXTENCODING_DONTKNOW(((rtl_TextEncoding) 0)) )
233 aFont.SetCharSet( static_cast<rtl_TextEncoding>(rDescr.CharSet) );
234 if ( static_cast<FontPitch>(rDescr.Pitch) != PITCH_DONTKNOW )
235 aFont.SetPitch( static_cast<FontPitch>(rDescr.Pitch) );
236 if ( rDescr.CharacterWidth )
237 aFont.SetWidthType(vcl::unohelper::ConvertFontWidth(rDescr.CharacterWidth));
238 if ( rDescr.Weight )
239 aFont.SetWeight(vcl::unohelper::ConvertFontWeight(rDescr.Weight));
240 if ( rDescr.Slant != css::awt::FontSlant_DONTKNOW )
241 aFont.SetItalic(vcl::unohelper::ConvertFontSlant(rDescr.Slant));
242 if ( static_cast<FontLineStyle>(rDescr.Underline) != LINESTYLE_DONTKNOW )
243 aFont.SetUnderline( static_cast<FontLineStyle>(rDescr.Underline) );
244 if ( static_cast<FontStrikeout>(rDescr.Strikeout) != STRIKEOUT_DONTKNOW )
245 aFont.SetStrikeout( static_cast<FontStrikeout>(rDescr.Strikeout) );
246
247 // Not DONTKNOW
248 aFont.SetOrientation( static_cast<short>(rDescr.Orientation) );
249 aFont.SetKerning( static_cast<FontKerning>(rDescr.Kerning) );
250 aFont.SetWordLineMode( rDescr.WordLineMode );
251
252 return aFont;
253}
254
255vcl::Font VCLUnoHelper::CreateFont( const css::uno::Reference< css::awt::XFont >& rxFont )
256{
257 vcl::Font aFont;
258 VCLXFont* pVCLXFont = comphelper::getUnoTunnelImplementation<VCLXFont>( rxFont );
259 if ( pVCLXFont )
260 aFont = pVCLXFont->GetFont();
261 return aFont;
262}
263
264
265css::awt::SimpleFontMetric VCLUnoHelper::CreateFontMetric( const FontMetric& rFontMetric )
266{
267 css::awt::SimpleFontMetric aFM;
268 aFM.Ascent = static_cast<sal_Int16>(rFontMetric.GetAscent());
269 aFM.Descent = static_cast<sal_Int16>(rFontMetric.GetDescent());
270 aFM.Leading = static_cast<sal_Int16>(rFontMetric.GetInternalLeading());
271 aFM.Slant = static_cast<sal_Int16>(rFontMetric.GetSlant());
272 aFM.FirstChar = 0x0020;
273 aFM.LastChar = 0xFFFD;
274 return aFM;
275}
276
277bool VCLUnoHelper::IsZero(const css::awt::Rectangle& rRect)
278{
279 return ( !rRect.X && !rRect.Y && !rRect.Width && !rRect.Height );
280}
281
282MapUnit VCLUnoHelper::UnoEmbed2VCLMapUnit( sal_Int32 nUnoEmbedMapUnit )
283{
284 switch( nUnoEmbedMapUnit )
285 {
286 case css::embed::EmbedMapUnits::ONE_100TH_MM:
287 return MapUnit::Map100thMM;
288 case css::embed::EmbedMapUnits::ONE_10TH_MM:
289 return MapUnit::Map10thMM;
290 case css::embed::EmbedMapUnits::ONE_MM:
291 return MapUnit::MapMM;
292 case css::embed::EmbedMapUnits::ONE_CM:
293 return MapUnit::MapCM;
294 case css::embed::EmbedMapUnits::ONE_1000TH_INCH:
295 return MapUnit::Map1000thInch;
296 case css::embed::EmbedMapUnits::ONE_100TH_INCH:
297 return MapUnit::Map100thInch;
298 case css::embed::EmbedMapUnits::ONE_10TH_INCH:
299 return MapUnit::Map10thInch;
300 case css::embed::EmbedMapUnits::ONE_INCH:
301 return MapUnit::MapInch;
302 case css::embed::EmbedMapUnits::POINT:
303 return MapUnit::MapPoint;
304 case css::embed::EmbedMapUnits::TWIP:
305 return MapUnit::MapTwip;
306 case css::embed::EmbedMapUnits::PIXEL:
307 return MapUnit::MapPixel;
308 }
309
310 OSL_FAIL( "Unexpected UNO map mode is provided!" )do { if (true && (((sal_Bool)1))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/home/maarten/src/libreoffice/core/toolkit/source/helper/vclunohelper.cxx"
":" "310" ": "), "%s", "Unexpected UNO map mode is provided!"
); } } while (false)
;
311 return MapUnit::LASTENUMDUMMY;
312}
313
314sal_Int32 VCLUnoHelper::VCL2UnoEmbedMapUnit( MapUnit nVCLMapUnit )
315{
316 switch( nVCLMapUnit )
317 {
318 case MapUnit::Map100thMM:
319 return css::embed::EmbedMapUnits::ONE_100TH_MM;
320 case MapUnit::Map10thMM:
321 return css::embed::EmbedMapUnits::ONE_10TH_MM;
322 case MapUnit::MapMM:
323 return css::embed::EmbedMapUnits::ONE_MM;
324 case MapUnit::MapCM:
325 return css::embed::EmbedMapUnits::ONE_CM;
326 case MapUnit::Map1000thInch:
327 return css::embed::EmbedMapUnits::ONE_1000TH_INCH;
328 case MapUnit::Map100thInch:
329 return css::embed::EmbedMapUnits::ONE_100TH_INCH;
330 case MapUnit::Map10thInch:
331 return css::embed::EmbedMapUnits::ONE_10TH_INCH;
332 case MapUnit::MapInch:
333 return css::embed::EmbedMapUnits::ONE_INCH;
334 case MapUnit::MapPoint:
335 return css::embed::EmbedMapUnits::POINT;
336 case MapUnit::MapTwip:
337 return css::embed::EmbedMapUnits::TWIP;
338 case MapUnit::MapPixel:
339 return css::embed::EmbedMapUnits::PIXEL;
340 default: ; // avoid compiler warning
341 }
342
343 OSL_FAIL( "Unexpected VCL map mode is provided!" )do { if (true && (((sal_Bool)1))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/home/maarten/src/libreoffice/core/toolkit/source/helper/vclunohelper.cxx"
":" "343" ": "), "%s", "Unexpected VCL map mode is provided!"
); } } while (false)
;
344 return -1;
345}
346
347using namespace ::com::sun::star::util;
348
349
350namespace
351{
352 enum UnitConversionDirection
353 {
354 FieldUnitToMeasurementUnit,
355 MeasurementUnitToFieldUnit
356 };
357
358 sal_Int16 convertMeasurementUnit( sal_Int16 _nUnit, UnitConversionDirection eDirection, sal_Int16& _rFieldToUNOValueFactor )
359 {
360 static struct _unit_table
361 {
362 FieldUnit eFieldUnit;
363 sal_Int16 nMeasurementUnit;
364 sal_Int16 nFieldToMeasureFactor;
365 } const aUnits[] = {
366 { FieldUnit::NONE, -1 , -1},
367 { FieldUnit::MM, MeasureUnit::MM, 1 }, // must precede MM_10TH
368 { FieldUnit::MM, MeasureUnit::MM_10TH, 10 },
369 { FieldUnit::MM_100TH, MeasureUnit::MM_100TH, 1 },
370 { FieldUnit::CM, MeasureUnit::CM, 1 },
371 { FieldUnit::M, MeasureUnit::M, 1 },
372 { FieldUnit::KM, MeasureUnit::KM, 1 },
373 { FieldUnit::TWIP, MeasureUnit::TWIP, 1 },
374 { FieldUnit::POINT, MeasureUnit::POINT, 1 },
375 { FieldUnit::PICA, MeasureUnit::PICA, 1 },
376 { FieldUnit::INCH, MeasureUnit::INCH, 1 }, // must precede INCH_*TH
377 { FieldUnit::INCH, MeasureUnit::INCH_10TH, 10 },
378 { FieldUnit::INCH, MeasureUnit::INCH_100TH, 100 },
379 { FieldUnit::INCH, MeasureUnit::INCH_1000TH, 1000 },
380 { FieldUnit::FOOT, MeasureUnit::FOOT, 1 },
381 { FieldUnit::MILE, MeasureUnit::MILE, 1 },
382 };
383 for (auto & aUnit : aUnits)
384 {
385 if ( eDirection == FieldUnitToMeasurementUnit )
386 {
387 if ( ( aUnit.eFieldUnit == static_cast<FieldUnit>(_nUnit) ) && ( aUnit.nFieldToMeasureFactor == _rFieldToUNOValueFactor ) )
388 return aUnit.nMeasurementUnit;
389 }
390 else
391 {
392 if ( aUnit.nMeasurementUnit == _nUnit )
393 {
394 _rFieldToUNOValueFactor = aUnit.nFieldToMeasureFactor;
395 return static_cast<sal_Int16>(aUnit.eFieldUnit);
396 }
397 }
398 }
399 if ( eDirection == FieldUnitToMeasurementUnit )
400 return -1;
401
402 _rFieldToUNOValueFactor = 1;
403 return sal_Int16(FieldUnit::NONE);
404 }
405}
406
407//= MeasurementUnitConversion
408
409
410sal_Int16 VCLUnoHelper::ConvertToMeasurementUnit( FieldUnit _nFieldUnit, sal_Int16 _nUNOToFieldValueFactor )
411{
412 return convertMeasurementUnit( static_cast<sal_Int16>(_nFieldUnit), FieldUnitToMeasurementUnit, _nUNOToFieldValueFactor );
413}
414
415
416FieldUnit VCLUnoHelper::ConvertToFieldUnit( sal_Int16 _nMeasurementUnit, sal_Int16& _rFieldToUNOValueFactor )
417{
418 return static_cast<FieldUnit>(convertMeasurementUnit( _nMeasurementUnit, MeasurementUnitToFieldUnit, _rFieldToUNOValueFactor ));
419}
420
421
422MapUnit /* MapModeUnit */ VCLUnoHelper::ConvertToMapModeUnit(sal_Int16 /* com.sun.star.util.MeasureUnit.* */ _nMeasureUnit)
423{
424 MapUnit eMode;
425 switch(_nMeasureUnit)
426 {
427 case css::util::MeasureUnit::MM_100TH:
428 eMode = MapUnit::Map100thMM;
429 break;
430
431 case css::util::MeasureUnit::MM_10TH:
432 eMode = MapUnit::Map10thMM;
433 break;
434
435 case css::util::MeasureUnit::MM:
436 eMode = MapUnit::MapMM;
437 break;
438
439 case css::util::MeasureUnit::CM:
440 eMode = MapUnit::MapCM;
441 break;
442
443 case css::util::MeasureUnit::INCH_1000TH:
444 eMode = MapUnit::Map1000thInch;
445 break;
446
447 case css::util::MeasureUnit::INCH_100TH:
448 eMode = MapUnit::Map100thInch;
449 break;
450
451 case css::util::MeasureUnit::INCH_10TH:
452 eMode = MapUnit::Map10thInch;
453 break;
454
455 case css::util::MeasureUnit::INCH:
456 eMode = MapUnit::MapInch;
457 break;
458
459 case css::util::MeasureUnit::POINT:
460 eMode = MapUnit::MapPoint;
461 break;
462
463 case css::util::MeasureUnit::TWIP:
464 eMode = MapUnit::MapTwip;
465 break;
466
467 case css::util::MeasureUnit::PIXEL:
468 eMode = MapUnit::MapPixel;
469 break;
470
471 case css::util::MeasureUnit::APPFONT:
472 eMode = MapUnit::MapAppFont;
473 break;
474
475 case css::util::MeasureUnit::SYSFONT:
476 eMode = MapUnit::MapSysFont;
477 break;
478
479 default:
480 throw css::lang::IllegalArgumentException("Unsupported measure unit.", nullptr, 1 );
481 }
482 return eMode;
483}
484
485::Size VCLUnoHelper::ConvertToVCLSize(css::awt::Size const& _aSize)
486{
487 ::Size aVCLSize(_aSize.Width, _aSize.Height);
488 return aVCLSize;
489}
490
491css::awt::Size VCLUnoHelper::ConvertToAWTSize(::Size /* VCLSize */ const& _aSize)
492{
493 css::awt::Size aAWTSize(_aSize.Width(), _aSize.Height());
494 return aAWTSize;
495}
496
497
498::Point VCLUnoHelper::ConvertToVCLPoint(css::awt::Point const& _aPoint)
499{
500 ::Point aVCLPoint(_aPoint.X, _aPoint.Y);
501 return aVCLPoint;
502}
503
504css::awt::Point VCLUnoHelper::ConvertToAWTPoint(::Point /* VCLPoint */ const& _aPoint)
505{
506 css::awt::Point aAWTPoint(_aPoint.X(), _aPoint.Y());
507 return aAWTPoint;
508}
509
510::tools::Rectangle VCLUnoHelper::ConvertToVCLRect( css::awt::Rectangle const & _rRect )
511{
512 return ::tools::Rectangle( _rRect.X, _rRect.Y, _rRect.X + _rRect.Width - 1, _rRect.Y + _rRect.Height - 1 );
513}
514
515css::awt::Rectangle VCLUnoHelper::ConvertToAWTRect( ::tools::Rectangle const & _rRect )
516{
517 return css::awt::Rectangle( _rRect.Left(), _rRect.Top(), _rRect.GetWidth(), _rRect.GetHeight() );
518}
519
520awt::MouseEvent VCLUnoHelper::createMouseEvent( const ::MouseEvent& _rVclEvent, const uno::Reference< uno::XInterface >& _rxContext )
521{
522 awt::MouseEvent aMouseEvent;
523 aMouseEvent.Source = _rxContext;
524
525 aMouseEvent.Modifiers = 0;
526 if ( _rVclEvent.IsShift() )
527 aMouseEvent.Modifiers |= css::awt::KeyModifier::SHIFT;
528 if ( _rVclEvent.IsMod1() )
529 aMouseEvent.Modifiers |= css::awt::KeyModifier::MOD1;
530 if ( _rVclEvent.IsMod2() )
531 aMouseEvent.Modifiers |= css::awt::KeyModifier::MOD2;
532
533 aMouseEvent.Buttons = 0;
534 if ( _rVclEvent.IsLeft() )
535 aMouseEvent.Buttons |= css::awt::MouseButton::LEFT;
536 if ( _rVclEvent.IsRight() )
537 aMouseEvent.Buttons |= css::awt::MouseButton::RIGHT;
538 if ( _rVclEvent.IsMiddle() )
539 aMouseEvent.Buttons |= css::awt::MouseButton::MIDDLE;
540
541 aMouseEvent.X = _rVclEvent.GetPosPixel().X();
542 aMouseEvent.Y = _rVclEvent.GetPosPixel().Y();
543 aMouseEvent.ClickCount = _rVclEvent.GetClicks();
544 aMouseEvent.PopupTrigger = false;
545
546 return aMouseEvent;
547}
548
549::MouseEvent VCLUnoHelper::createVCLMouseEvent( const awt::MouseEvent& _rAwtEvent )
550{
551 ::MouseEvent aMouseEvent( Point( _rAwtEvent.X, _rAwtEvent.Y ), _rAwtEvent.ClickCount,
552 ::MouseEventModifiers::NONE, _rAwtEvent.Buttons, _rAwtEvent.Modifiers );
553
554 return aMouseEvent;
555}
556
557awt::KeyEvent VCLUnoHelper::createKeyEvent( const ::KeyEvent& _rVclEvent, const uno::Reference< uno::XInterface >& _rxContext )
558{
559 awt::KeyEvent aKeyEvent;
560 aKeyEvent.Source = _rxContext;
561
562 aKeyEvent.Modifiers = 0;
563 if ( _rVclEvent.GetKeyCode().IsShift() )
564 aKeyEvent.Modifiers |= awt::KeyModifier::SHIFT;
565 if ( _rVclEvent.GetKeyCode().IsMod1() )
566 aKeyEvent.Modifiers |= awt::KeyModifier::MOD1;
567 if ( _rVclEvent.GetKeyCode().IsMod2() )
568 aKeyEvent.Modifiers |= awt::KeyModifier::MOD2;
569 if ( _rVclEvent.GetKeyCode().IsMod3() )
570 aKeyEvent.Modifiers |= awt::KeyModifier::MOD3;
571
572 aKeyEvent.KeyCode = _rVclEvent.GetKeyCode().GetCode();
573 aKeyEvent.KeyChar = _rVclEvent.GetCharCode();
574 aKeyEvent.KeyFunc = ::sal::static_int_cast< sal_Int16 >( _rVclEvent.GetKeyCode().GetFunction());
575
576 return aKeyEvent;
577}
578
579::KeyEvent VCLUnoHelper::createVCLKeyEvent( const awt::KeyEvent& _rAwtEvent )
580{
581 sal_Unicode nChar = _rAwtEvent.KeyChar;
582 vcl::KeyCode aKeyCode( _rAwtEvent.KeyCode, _rAwtEvent.Modifiers & awt::KeyModifier::SHIFT,
583 _rAwtEvent.Modifiers & awt::KeyModifier::MOD1,
584 _rAwtEvent.Modifiers & awt::KeyModifier::MOD2,
585 _rAwtEvent.Modifiers & awt::KeyModifier::MOD3 );
586
587 return ::KeyEvent (nChar, aKeyCode);
588
589}
590
591/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

/home/maarten/src/libreoffice/core/include/rtl/ref.hxx

1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#ifndef INCLUDED_RTL_REF_HXX
21#define INCLUDED_RTL_REF_HXX
22
23#include "sal/config.h"
24
25#include <cassert>
26#include <cstddef>
27#include <functional>
28#ifdef LIBO_INTERNAL_ONLY1
29#include <type_traits>
30#endif
31
32#include "sal/types.h"
33
34namespace rtl
35{
36
37/** Template reference class for reference type.
38*/
39template <class reference_type>
40class Reference
41{
42 /** The <b>reference_type</b> body pointer.
43 */
44 reference_type * m_pBody;
45
46
47public:
48 /** Constructor...
49 */
50 Reference()
51 : m_pBody (NULL__null)
52 {}
53
54
55 /** Constructor...
56 */
57 Reference (reference_type * pBody, __sal_NoAcquire)
58 : m_pBody (pBody)
59 {
60 }
61
62 /** Constructor...
63 */
64 Reference (reference_type * pBody)
65 : m_pBody (pBody)
66 {
67 if (m_pBody)
68 m_pBody->acquire();
69 }
70
71 /** Copy constructor...
72 */
73 Reference (const Reference<reference_type> & handle)
74 : m_pBody (handle.m_pBody)
75 {
76 if (m_pBody)
77 m_pBody->acquire();
78 }
79
80#ifdef LIBO_INTERNAL_ONLY1
81 /** Move constructor...
82 */
83 Reference (Reference<reference_type> && handle) noexcept
84 : m_pBody (handle.m_pBody)
85 {
86 handle.m_pBody = nullptr;
87 }
88#endif
89
90#if defined LIBO_INTERNAL_ONLY1
91 /** Up-casting conversion constructor: Copies interface reference.
92
93 Does not work for up-casts to ambiguous bases.
94
95 @param rRef another reference
96 */
97 template< class derived_type >
98 inline Reference(
99 const Reference< derived_type > & rRef,
100 std::enable_if_t<std::is_base_of_v<reference_type, derived_type>, int> = 0 )
101 : m_pBody (rRef.get())
102 {
103 if (m_pBody)
104 m_pBody->acquire();
105 }
106#endif
107
108 /** Destructor...
109 */
110 ~Reference() COVERITY_NOEXCEPT_FALSE
111 {
112 if (m_pBody
13.1
Field 'm_pBody' is non-null
13.1
Field 'm_pBody' is non-null
13.1
Field 'm_pBody' is non-null
)
14
Taking true branch
113 m_pBody->release();
15
Calling 'VclReferenceBase::release'
19
Returning; memory was released
114 }
115
116 /** Set...
117 Similar to assignment.
118 */
119 Reference<reference_type> &
120 SAL_CALL set (reference_type * pBody)
121 {
122 if (pBody)
6
Assuming 'pBody' is non-null
7
Taking true branch
123 pBody->acquire();
124 reference_type * const pOld = m_pBody;
125 m_pBody = pBody;
126 if (pOld
7.1
'pOld' is null
7.1
'pOld' is null
7.1
'pOld' is null
)
8
Taking false branch
127 pOld->release();
128 return *this;
129 }
130
131 /** Assignment.
132 Unbinds this instance from its body (if bound) and
133 bind it to the body represented by the handle.
134 */
135 Reference<reference_type> &
136 SAL_CALL operator= (const Reference<reference_type> & handle)
137 {
138 return set( handle.m_pBody );
5
Calling 'Reference::set'
9
Returning from 'Reference::set'
139 }
140
141#ifdef LIBO_INTERNAL_ONLY1
142 /** Assignment.
143 * Unbinds this instance from its body (if bound),
144 * bind it to the body represented by the handle, and
145 * set the body represented by the handle to nullptr.
146 */
147 Reference<reference_type> &
148 operator= (Reference<reference_type> && handle)
149 {
150 // self-movement guts ourself
151 if (m_pBody)
152 m_pBody->release();
153 m_pBody = handle.m_pBody;
154 handle.m_pBody = nullptr;
155 return *this;
156 }
157#endif
158
159 /** Assignment...
160 */
161 Reference<reference_type> &
162 SAL_CALL operator= (reference_type * pBody)
163 {
164 return set( pBody );
165 }
166
167 /** Unbind the body from this handle.
168 Note that for a handle representing a large body,
169 "handle.clear().set(new body());" _might_
170 perform a little bit better than "handle.set(new body());",
171 since in the second case two large objects exist in memory
172 (the old body and the new body).
173 */
174 Reference<reference_type> & SAL_CALL clear()
175 {
176 if (m_pBody)
177 {
178 reference_type * const pOld = m_pBody;
179 m_pBody = NULL__null;
180 pOld->release();
181 }
182 return *this;
183 }
184
185
186 /** Get the body. Can be used instead of operator->().
187 I.e. handle->someBodyOp() and handle.get()->someBodyOp()
188 are the same.
189 */
190 reference_type * SAL_CALL get() const
191 {
192 return m_pBody;
193 }
194
195
196 /** Probably most common used: handle->someBodyOp().
197 */
198 reference_type * SAL_CALL operator->() const
199 {
200 assert(m_pBody != NULL)(static_cast <bool> (m_pBody != __null) ? void (0) : __assert_fail
("m_pBody != NULL", "/home/maarten/src/libreoffice/core/include/rtl/ref.hxx"
, 200, __extension__ __PRETTY_FUNCTION__))
;
201 return m_pBody;
202 }
203
204
205 /** Allows (*handle).someBodyOp().
206 */
207 reference_type & SAL_CALL operator*() const
208 {
209 assert(m_pBody != NULL)(static_cast <bool> (m_pBody != __null) ? void (0) : __assert_fail
("m_pBody != NULL", "/home/maarten/src/libreoffice/core/include/rtl/ref.hxx"
, 209, __extension__ __PRETTY_FUNCTION__))
;
210 return *m_pBody;
211 }
212
213
214 /** Returns True if the handle does point to a valid body.
215 */
216 bool SAL_CALL is() const
217 {
218 return (m_pBody != NULL__null);
219 }
220
221#if defined LIBO_INTERNAL_ONLY1
222 /** Returns True if the handle does point to a valid body.
223 */
224 explicit operator bool() const
225 {
226 return is();
227 }
228#endif
229
230 /** Returns True if this points to pBody.
231 */
232 bool SAL_CALL operator== (const reference_type * pBody) const
233 {
234 return (m_pBody == pBody);
235 }
236
237
238 /** Returns True if handle points to the same body.
239 */
240 bool
241 SAL_CALL operator== (const Reference<reference_type> & handle) const
242 {
243 return (m_pBody == handle.m_pBody);
244 }
245
246
247 /** Needed to place References into STL collection.
248 */
249 bool
250 SAL_CALL operator!= (const Reference<reference_type> & handle) const
251 {
252 return (m_pBody != handle.m_pBody);
253 }
254
255
256 /** Needed to place References into STL collection.
257 */
258 bool
259 SAL_CALL operator< (const Reference<reference_type> & handle) const
260 {
261 return (m_pBody < handle.m_pBody);
262 }
263
264
265 /** Needed to place References into STL collection.
266 */
267 bool
268 SAL_CALL operator> (const Reference<reference_type> & handle) const
269 {
270 return (m_pBody > handle.m_pBody);
271 }
272};
273
274} // namespace rtl
275
276#if defined LIBO_INTERNAL_ONLY1
277namespace std
278{
279
280/// @cond INTERNAL
281/**
282 Make rtl::Reference hashable by default for use in STL containers.
283
284 @since LibreOffice 6.3
285*/
286template<typename T>
287struct hash<::rtl::Reference<T>>
288{
289 std::size_t operator()(::rtl::Reference<T> const & s) const
290 { return std::size_t(s.get()); }
291};
292/// @endcond
293
294}
295
296#endif
297
298#endif /* ! INCLUDED_RTL_REF_HXX */
299
300/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

/home/maarten/src/libreoffice/core/include/vcl/vclreferencebase.hxx

1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19#ifndef INCLUDED_VCL_Reference_HXX
20#define INCLUDED_VCL_Reference_HXX
21
22#include <vcl/dllapi.h>
23#include <osl/interlck.h>
24
25class VCL_DLLPUBLIC__attribute__ ((visibility("default"))) VclReferenceBase
26{
27 mutable oslInterlockedCount mnRefCnt;
28
29 template<typename T> friend class VclPtr;
30
31public:
32 void acquire() const
33 {
34 osl_atomic_increment(&mnRefCnt)__sync_add_and_fetch((&mnRefCnt), 1);
35 }
36
37 void release() const
38 {
39 if (osl_atomic_decrement(&mnRefCnt)__sync_sub_and_fetch((&mnRefCnt), 1) == 0)
16
Assuming the condition is true
17
Taking true branch
40 delete this;
18
Memory is released
41 }
42#ifdef DBG_UTIL
43#ifndef _WIN32
44 sal_Int32 getRefCount() const { return mnRefCnt; }
45#endif
46#endif
47
48
49private:
50 VclReferenceBase(const VclReferenceBase&) = delete;
51 VclReferenceBase& operator=(const VclReferenceBase&) = delete;
52
53 bool mbDisposed : 1;
54
55protected:
56 VclReferenceBase();
57protected:
58 virtual ~VclReferenceBase();
59
60protected:
61 virtual void dispose();
62
63public:
64 void disposeOnce();
65 bool isDisposed() const { return mbDisposed; }
66
67};
68#endif