clang -cc1 -cc1 -triple x86_64-unknown-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name transobj.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 -isystem /usr/include/libxml2 -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 SC_DLLIMPLEMENTATION -D SC_INFO_OSVERSION="LINUX" -D SYSTEM_LIBXML -D EXCEPTIONS_ON -D LIBO_INTERNAL_ONLY -I /home/maarten/src/libreoffice/core/workdir/UnpackedTarball/liborcus/include -I /home/maarten/src/libreoffice/core/workdir/UnpackedTarball/mdds/include -I /home/maarten/src/libreoffice/core/workdir/UnpackedTarball/icu/source -I /home/maarten/src/libreoffice/core/workdir/UnpackedTarball/icu/source/i18n -I /home/maarten/src/libreoffice/core/workdir/UnpackedTarball/icu/source/common -I /home/maarten/src/libreoffice/core/external/clew/source/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/sc/source/core/inc -I /home/maarten/src/libreoffice/core/sc/source/filter/inc -I /home/maarten/src/libreoffice/core/sc/source/ui/inc -I /home/maarten/src/libreoffice/core/sc/inc -I /home/maarten/src/libreoffice/core/workdir/SdiTarget/sc/sdi -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/workdir/CustomTarget/officecfg/registry -I /home/maarten/src/libreoffice/core/workdir/UnoApiHeadersTarget/udkapi/normal -I /home/maarten/src/libreoffice/core/workdir/UnoApiHeadersTarget/offapi/normal -I /home/maarten/src/libreoffice/core/workdir/UnoApiHeadersTarget/oovbaapi/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/sc/source/ui/app/transobj.cxx
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | |
18 | |
19 | |
20 | #include <scitems.hxx> |
21 | #include <editeng/justifyitem.hxx> |
22 | |
23 | #include <com/sun/star/uno/Sequence.hxx> |
24 | #include <com/sun/star/embed/XTransactedObject.hpp> |
25 | |
26 | #include <osl/diagnose.h> |
27 | #include <unotools/tempfile.hxx> |
28 | #include <unotools/ucbstreamhelper.hxx> |
29 | #include <comphelper/fileformat.h> |
30 | #include <comphelper/storagehelper.hxx> |
31 | #include <comphelper/servicehelper.hxx> |
32 | #include <sot/storage.hxx> |
33 | #include <vcl/gdimtf.hxx> |
34 | #include <vcl/jobset.hxx> |
35 | #include <vcl/svapp.hxx> |
36 | #include <vcl/virdev.hxx> |
37 | #include <sfx2/docfile.hxx> |
38 | |
39 | #include <transobj.hxx> |
40 | #include <patattr.hxx> |
41 | #include <cellvalue.hxx> |
42 | #include <cellform.hxx> |
43 | #include <document.hxx> |
44 | #include <viewopti.hxx> |
45 | #include <editutil.hxx> |
46 | #include <impex.hxx> |
47 | #include <formulacell.hxx> |
48 | #include <printfun.hxx> |
49 | #include <docfunc.hxx> |
50 | #include <scmod.hxx> |
51 | #include <dragdata.hxx> |
52 | |
53 | #include <editeng/paperinf.hxx> |
54 | #include <editeng/sizeitem.hxx> |
55 | #include <formula/errorcodes.hxx> |
56 | #include <docsh.hxx> |
57 | #include <markdata.hxx> |
58 | #include <stlpool.hxx> |
59 | #include <viewdata.hxx> |
60 | #include <dociter.hxx> |
61 | #include <cellsuno.hxx> |
62 | #include <stringutil.hxx> |
63 | #include <formulaiter.hxx> |
64 | |
65 | using namespace com::sun::star; |
66 | |
67 | constexpr sal_uInt32 SCTRANS_TYPE_IMPEX = 1; |
68 | constexpr sal_uInt32 SCTRANS_TYPE_EDIT_RTF = 2; |
69 | constexpr sal_uInt32 SCTRANS_TYPE_EDIT_BIN = 3; |
70 | constexpr sal_uInt32 SCTRANS_TYPE_EMBOBJ = 4; |
71 | constexpr sal_uInt32 SCTRANS_TYPE_EDIT_ODF_TEXT_FLAT = 5; |
72 | |
73 | void ScTransferObj::GetAreaSize( const ScDocument& rDoc, SCTAB nTab1, SCTAB nTab2, SCROW& nRow, SCCOL& nCol ) |
74 | { |
75 | SCCOL nMaxCol = 0; |
76 | SCROW nMaxRow = 0; |
77 | for( SCTAB nTab = nTab1; nTab <= nTab2; nTab++ ) |
78 | { |
79 | SCCOL nLastCol = 0; |
80 | SCROW nLastRow = 0; |
81 | |
82 | if( rDoc.GetPrintArea( nTab, nLastCol, nLastRow ) ) |
83 | { |
84 | if( nLastCol > nMaxCol ) |
85 | nMaxCol = nLastCol; |
86 | if( nLastRow > nMaxRow ) |
87 | nMaxRow = nLastRow; |
88 | } |
89 | } |
90 | nRow = nMaxRow; |
91 | nCol = nMaxCol; |
92 | } |
93 | |
94 | void ScTransferObj::PaintToDev( OutputDevice* pDev, ScDocument& rDoc, double nPrintFactor, |
95 | const ScRange& rBlock ) |
96 | { |
97 | tools::Rectangle aBound( Point(), pDev->GetOutputSize() ); |
98 | |
99 | ScViewData aViewData(rDoc); |
100 | |
101 | aViewData.SetTabNo( rBlock.aEnd.Tab() ); |
102 | aViewData.SetScreen( rBlock.aStart.Col(), rBlock.aStart.Row(), |
103 | rBlock.aEnd.Col(), rBlock.aEnd.Row() ); |
104 | |
105 | ScPrintFunc::DrawToDev( rDoc, pDev, nPrintFactor, aBound, &aViewData, false ); |
106 | } |
107 | |
108 | ScTransferObj::ScTransferObj( ScDocumentUniquePtr pClipDoc, const TransferableObjectDescriptor& rDesc ) : |
109 | m_pDoc( std::move(pClipDoc ) ), |
110 | m_nNonFiltered(0), |
111 | m_aObjDesc( rDesc ), |
112 | m_nDragHandleX( 0 ), |
113 | m_nDragHandleY( 0 ), |
114 | m_nSourceCursorX( m_pDoc->MaxCol() + 1 ), |
115 | m_nSourceCursorY( m_pDoc->MaxRow() + 1 ), |
116 | m_nDragSourceFlags( ScDragSrc::Undefined ), |
117 | m_bDragWasInternal( false ), |
118 | m_bUsedForLink( false ), |
119 | m_bUseInApi( false ) |
120 | { |
121 | OSL_ENSURE(m_pDoc->IsClipboard(), "wrong document"); |
122 | |
123 | |
124 | |
125 | SCCOL nCol1; |
126 | SCROW nRow1; |
127 | SCCOL nCol2; |
128 | SCROW nRow2; |
129 | m_pDoc->GetClipStart( nCol1, nRow1 ); |
130 | m_pDoc->GetClipArea( nCol2, nRow2, true ); |
131 | nCol2 = sal::static_int_cast<SCCOL>( nCol2 + nCol1 ); |
132 | nRow2 = sal::static_int_cast<SCROW>( nRow2 + nRow1 ); |
133 | |
134 | SCCOL nDummy; |
135 | m_pDoc->GetClipArea( nDummy, m_nNonFiltered, false ); |
136 | m_bHasFiltered = (m_nNonFiltered < (nRow2 - nRow1)); |
137 | ++m_nNonFiltered; |
138 | |
139 | SCTAB nTab1=0; |
140 | SCTAB nTab2=0; |
141 | bool bFirst = true; |
142 | for (SCTAB i=0; i< m_pDoc->GetTableCount(); i++) |
143 | if (m_pDoc->HasTable(i)) |
144 | { |
145 | if (bFirst) |
146 | nTab1 = i; |
147 | nTab2 = i; |
148 | bFirst = false; |
149 | } |
150 | OSL_ENSURE(!bFirst, "no sheet selected"); |
151 | |
152 | |
153 | |
154 | if ( nCol2>=m_pDoc->MaxCol() && nRow2>=m_pDoc->MaxRow() ) |
155 | { |
156 | SCROW nMaxRow; |
157 | SCCOL nMaxCol; |
158 | GetAreaSize( *m_pDoc, nTab1, nTab2, nMaxRow, nMaxCol ); |
159 | if( nMaxRow < nRow2 ) |
160 | nRow2 = nMaxRow; |
161 | if( nMaxCol < nCol2 ) |
162 | nCol2 = nMaxCol; |
163 | } |
164 | |
165 | m_aBlock = ScRange( nCol1, nRow1, nTab1, nCol2, nRow2, nTab2 ); |
166 | m_nVisibleTab = nTab1; |
167 | |
168 | tools::Rectangle aMMRect = m_pDoc->GetMMRect( nCol1,nRow1, nCol2,nRow2, nTab1 ); |
169 | m_aObjDesc.maSize = aMMRect.GetSize(); |
170 | PrepareOLE( m_aObjDesc ); |
171 | } |
172 | |
173 | ScTransferObj::~ScTransferObj() |
174 | { |
175 | SolarMutexGuard aSolarGuard; |
176 | |
177 | ScModule* pScMod = SC_MOD(); |
178 | if (pScMod && pScMod->GetDragData().pCellTransfer == this) |
179 | { |
180 | OSL_FAIL("ScTransferObj wasn't released"); |
181 | pScMod->ResetDragObject(); |
182 | } |
183 | |
184 | m_pDoc.reset(); |
185 | |
186 | m_aDocShellRef.clear(); |
187 | |
188 | m_aDrawPersistRef.clear(); |
189 | |
190 | } |
191 | |
192 | ScTransferObj* ScTransferObj::GetOwnClipboard(const uno::Reference<datatransfer::XTransferable2>& xTransferable) |
193 | { |
194 | ScTransferObj* pObj = nullptr; |
195 | if (xTransferable.is()) |
196 | { |
197 | uno::Reference<XUnoTunnel> xTunnel( xTransferable, uno::UNO_QUERY ); |
198 | if ( xTunnel.is() ) |
199 | { |
200 | sal_Int64 nHandle = xTunnel->getSomething( getUnoTunnelId() ); |
201 | if ( nHandle ) |
202 | pObj = dynamic_cast<ScTransferObj*>(reinterpret_cast<TransferableHelper*>( static_cast<sal_IntPtr>(nHandle) )); |
203 | } |
204 | } |
205 | |
206 | return pObj; |
207 | } |
208 | |
209 | void ScTransferObj::AddSupportedFormats() |
210 | { |
211 | AddFormat( SotClipboardFormatId::EMBED_SOURCE ); |
212 | AddFormat( SotClipboardFormatId::OBJECTDESCRIPTOR ); |
213 | AddFormat( SotClipboardFormatId::GDIMETAFILE ); |
214 | AddFormat( SotClipboardFormatId::PNG ); |
215 | AddFormat( SotClipboardFormatId::BITMAP ); |
216 | |
217 | |
218 | AddFormat( SotClipboardFormatId::HTML ); |
219 | AddFormat( SotClipboardFormatId::SYLK ); |
220 | AddFormat( SotClipboardFormatId::LINK ); |
221 | AddFormat( SotClipboardFormatId::DIF ); |
222 | AddFormat( SotClipboardFormatId::STRING ); |
223 | AddFormat( SotClipboardFormatId::STRING_TSVC ); |
224 | |
225 | AddFormat( SotClipboardFormatId::RTF ); |
226 | AddFormat( SotClipboardFormatId::RICHTEXT ); |
227 | if ( m_aBlock.aStart == m_aBlock.aEnd ) |
228 | { |
229 | AddFormat( SotClipboardFormatId::EDITENGINE_ODF_TEXT_FLAT ); |
230 | } |
231 | } |
232 | |
233 | static ScRange lcl_reduceBlock(const ScDocument& rDoc, ScRange aReducedBlock, bool bIncludeVisual = false) |
234 | { |
235 | if ((aReducedBlock.aEnd.Col() == rDoc.MaxCol() || aReducedBlock.aEnd.Row() == rDoc.MaxRow()) && |
236 | aReducedBlock.aStart.Tab() == aReducedBlock.aEnd.Tab()) |
237 | { |
238 | |
239 | |
240 | |
241 | SCCOL nPrintAreaEndCol = 0; |
242 | SCROW nPrintAreaEndRow = 0; |
243 | if (bIncludeVisual) |
244 | rDoc.GetPrintArea( aReducedBlock.aStart.Tab(), nPrintAreaEndCol, nPrintAreaEndRow, true ); |
245 | |
246 | |
247 | |
248 | |
249 | SCCOL nStartCol = aReducedBlock.aStart.Col(); |
250 | SCROW nStartRow = aReducedBlock.aStart.Row(); |
251 | SCCOL nEndCol = aReducedBlock.aEnd.Col(); |
252 | SCROW nEndRow = aReducedBlock.aEnd.Row(); |
253 | bool bShrunk = false; |
254 | rDoc.ShrinkToUsedDataArea( bShrunk, aReducedBlock.aStart.Tab(), nStartCol, nStartRow, nEndCol, nEndRow, |
255 | false, bIncludeVisual , bIncludeVisual , |
256 | bIncludeVisual , bIncludeVisual ); |
257 | |
258 | if ( nPrintAreaEndRow > nEndRow ) |
259 | nEndRow = nPrintAreaEndRow; |
260 | |
261 | if ( nPrintAreaEndCol > nEndCol ) |
262 | nEndCol = nPrintAreaEndCol; |
263 | |
264 | aReducedBlock = ScRange(nStartCol, nStartRow, aReducedBlock.aStart.Tab(), nEndCol, nEndRow, aReducedBlock.aEnd.Tab()); |
265 | } |
266 | return aReducedBlock; |
267 | } |
268 | |
269 | bool ScTransferObj::GetData( const datatransfer::DataFlavor& rFlavor, const OUString& ) |
270 | { |
271 | SotClipboardFormatId nFormat = SotExchange::GetFormat( rFlavor ); |
272 | bool bOK = false; |
273 | |
274 | if( HasFormat( nFormat ) ) |
| 1 | Assuming the condition is true | |
|
| |
275 | { |
276 | ScRange aReducedBlock = m_aBlock; |
277 | |
278 | bool bReduceBlockFormat = |
279 | nFormat == SotClipboardFormatId::HTML |
| 3 | | Assuming 'nFormat' is not equal to HTML | |
|
280 | || nFormat == SotClipboardFormatId::RTF |
| 4 | | Assuming 'nFormat' is not equal to RTF | |
|
281 | || nFormat == SotClipboardFormatId::RICHTEXT |
| 5 | | Assuming 'nFormat' is not equal to RICHTEXT | |
|
282 | || nFormat == SotClipboardFormatId::BITMAP |
| 6 | | Assuming 'nFormat' is not equal to BITMAP | |
|
283 | || nFormat == SotClipboardFormatId::PNG; |
| 7 | | Assuming 'nFormat' is not equal to PNG | |
|
284 | |
285 | const bool bIncludeVisual = (nFormat == SotClipboardFormatId::BITMAP || |
286 | nFormat == SotClipboardFormatId::PNG); |
287 | |
288 | if (bReduceBlockFormat) |
| |
289 | aReducedBlock = lcl_reduceBlock(*m_pDoc, m_aBlock, bIncludeVisual); |
290 | |
291 | if ( nFormat == SotClipboardFormatId::LINKSRCDESCRIPTOR || nFormat == SotClipboardFormatId::OBJECTDESCRIPTOR ) |
| 9 | | Assuming 'nFormat' is not equal to LINKSRCDESCRIPTOR | |
|
| 10 | | Assuming 'nFormat' is not equal to OBJECTDESCRIPTOR | |
|
| |
292 | { |
293 | bOK = SetTransferableObjectDescriptor( m_aObjDesc ); |
294 | } |
295 | else if ( ( nFormat == SotClipboardFormatId::RTF || nFormat == SotClipboardFormatId::RICHTEXT || |
296 | nFormat == SotClipboardFormatId::EDITENGINE_ODF_TEXT_FLAT ) && m_aBlock.aStart == m_aBlock.aEnd ) |
| 12 | | Assuming 'nFormat' is not equal to EDITENGINE_ODF_TEXT_FLAT | |
|
297 | { |
298 | |
299 | |
300 | SCCOL nCol = m_aBlock.aStart.Col(); |
301 | SCROW nRow = m_aBlock.aStart.Row(); |
302 | SCTAB nTab = m_aBlock.aStart.Tab(); |
303 | ScAddress aPos(nCol, nRow, nTab); |
304 | |
305 | const ScPatternAttr* pPattern = m_pDoc->GetPattern( nCol, nRow, nTab ); |
306 | ScTabEditEngine aEngine( *pPattern, m_pDoc->GetEditPool(), m_pDoc.get() ); |
307 | ScRefCellValue aCell(*m_pDoc, aPos); |
308 | if (aCell.meType == CELLTYPE_EDIT) |
309 | { |
310 | const EditTextObject* pObj = aCell.mpEditText; |
311 | aEngine.SetTextCurrentDefaults(*pObj); |
312 | } |
313 | else |
314 | { |
315 | SvNumberFormatter* pFormatter = m_pDoc->GetFormatTable(); |
316 | sal_uInt32 nNumFmt = pPattern->GetNumberFormat(pFormatter); |
317 | OUString aText; |
318 | const Color* pColor; |
319 | ScCellFormat::GetString(aCell, nNumFmt, aText, &pColor, *pFormatter, *m_pDoc); |
320 | if (!aText.isEmpty()) |
321 | aEngine.SetTextCurrentDefaults(aText); |
322 | } |
323 | |
324 | bOK = SetObject( &aEngine, |
325 | ((nFormat == SotClipboardFormatId::RTF) ? SCTRANS_TYPE_EDIT_RTF : |
326 | ((nFormat == SotClipboardFormatId::EDITENGINE_ODF_TEXT_FLAT) ? |
327 | SCTRANS_TYPE_EDIT_ODF_TEXT_FLAT : SCTRANS_TYPE_EDIT_BIN)), |
328 | rFlavor ); |
329 | } |
330 | else if ( ScImportExport::IsFormatSupported( nFormat ) || nFormat == SotClipboardFormatId::RTF |
| 13 | | Assuming the condition is false | |
|
| |
331 | || nFormat == SotClipboardFormatId::RICHTEXT ) |
332 | { |
333 | |
334 | |
335 | if ( nFormat == SotClipboardFormatId::LINK ) |
336 | m_bUsedForLink = true; |
337 | |
338 | bool bIncludeFiltered = m_pDoc->IsCutMode() || m_bUsedForLink; |
339 | |
340 | ScImportExport aObj( *m_pDoc, aReducedBlock ); |
341 | |
342 | |
343 | |
344 | |
345 | |
346 | |
347 | |
348 | |
349 | |
350 | const bool bPlainMulti = (nFormat == SotClipboardFormatId::STRING && |
351 | aReducedBlock.aStart != aReducedBlock.aEnd); |
352 | |
353 | |
354 | |
355 | |
356 | ScExportTextOptions aTextOptions( ScExportTextOptions::None, 0, |
357 | (nFormat == SotClipboardFormatId::STRING_TSVC)); |
358 | if ( bPlainMulti || m_bUsedForLink ) |
359 | { |
360 | |
361 | |
362 | aTextOptions.meNewlineConversion = ScExportTextOptions::ToSpace; |
363 | aTextOptions.mcSeparatorConvertTo = ' '; |
364 | aTextOptions.mbAddQuotes = false; |
365 | } |
366 | aObj.SetExportTextOptions(aTextOptions); |
367 | aObj.SetFormulas( m_pDoc->GetViewOptions().GetOption( VOPT_FORMULAS ) ); |
368 | aObj.SetIncludeFiltered( bIncludeFiltered ); |
369 | |
370 | |
371 | |
372 | if ( rFlavor.DataType.equals( ::cppu::UnoType<OUString>::get() ) ) |
373 | { |
374 | OUString aString; |
375 | if ( aObj.ExportString( aString, nFormat ) ) |
376 | bOK = SetString( aString, rFlavor ); |
377 | } |
378 | else if ( rFlavor.DataType.equals( cppu::UnoType<uno::Sequence< sal_Int8 >>::get() ) ) |
379 | { |
380 | |
381 | bOK = SetObject( &aObj, SCTRANS_TYPE_IMPEX, rFlavor ); |
382 | } |
383 | else |
384 | { |
385 | OSL_FAIL("unknown DataType"); |
386 | } |
387 | } |
388 | else if ( nFormat == SotClipboardFormatId::BITMAP || nFormat == SotClipboardFormatId::PNG ) |
| |
389 | { |
390 | tools::Rectangle aMMRect = m_pDoc->GetMMRect( aReducedBlock.aStart.Col(), aReducedBlock.aStart.Row(), |
391 | aReducedBlock.aEnd.Col(), aReducedBlock.aEnd.Row(), |
392 | aReducedBlock.aStart.Tab() ); |
393 | ScopedVclPtrInstance< VirtualDevice > pVirtDev; |
394 | pVirtDev->SetOutputSizePixel(pVirtDev->LogicToPixel(aMMRect.GetSize(), MapMode(MapUnit::Map100thMM))); |
395 | |
396 | PaintToDev( pVirtDev, *m_pDoc, 1.0, aReducedBlock ); |
397 | |
398 | pVirtDev->SetMapMode( MapMode( MapUnit::MapPixel ) ); |
399 | BitmapEx aBmp = pVirtDev->GetBitmapEx( Point(), pVirtDev->GetOutputSize() ); |
400 | bOK = SetBitmapEx( aBmp, rFlavor ); |
401 | } |
402 | else if ( nFormat == SotClipboardFormatId::GDIMETAFILE ) |
| 16 | | Assuming 'nFormat' is equal to GDIMETAFILE | |
|
| |
403 | { |
404 | |
405 | |
406 | |
407 | |
408 | InitDocShell(false); |
409 | |
410 | SfxObjectShell* pEmbObj = m_aDocShellRef.get(); |
411 | |
412 | |
413 | GDIMetaFile aMtf; |
414 | ScopedVclPtrInstance< VirtualDevice > pVDev; |
| 18 | | Calling default constructor for 'ScopedVclPtrInstance<VirtualDevice>' | |
|
| 20 | | Returning from default constructor for 'ScopedVclPtrInstance<VirtualDevice>' | |
|
415 | MapMode aMapMode( pEmbObj->GetMapUnit() ); |
416 | tools::Rectangle aVisArea( pEmbObj->GetVisArea( ASPECT_CONTENT ) ); |
417 | |
418 | pVDev->EnableOutput( false ); |
419 | pVDev->SetMapMode( aMapMode ); |
420 | aMtf.SetPrefSize( aVisArea.GetSize() ); |
421 | aMtf.SetPrefMapMode( aMapMode ); |
422 | aMtf.Record( pVDev ); |
423 | |
424 | pEmbObj->DoDraw( pVDev, Point(), aVisArea.GetSize(), JobSetup() ); |
425 | |
426 | aMtf.Stop(); |
427 | aMtf.WindStart(); |
428 | |
429 | bOK = SetGDIMetaFile( aMtf ); |
430 | } |
| 21 | | Calling implicit destructor for 'ScopedVclPtrInstance<VirtualDevice>' | |
|
| 22 | | Calling '~ScopedVclPtr' | |
|
431 | else if ( nFormat == SotClipboardFormatId::EMBED_SOURCE ) |
432 | { |
433 | |
434 | |
435 | InitDocShell(true); |
436 | |
437 | SfxObjectShell* pEmbObj = m_aDocShellRef.get(); |
438 | bOK = SetObject( pEmbObj, SCTRANS_TYPE_EMBOBJ, rFlavor ); |
439 | } |
440 | } |
441 | return bOK; |
442 | } |
443 | |
444 | bool ScTransferObj::WriteObject( tools::SvRef<SotStorageStream>& rxOStm, void* pUserObject, sal_uInt32 nUserObjectId, |
445 | const datatransfer::DataFlavor& rFlavor ) |
446 | { |
447 | |
448 | |
449 | bool bRet = false; |
450 | switch (nUserObjectId) |
451 | { |
452 | case SCTRANS_TYPE_IMPEX: |
453 | { |
454 | ScImportExport* pImpEx = static_cast<ScImportExport*>(pUserObject); |
455 | |
456 | SotClipboardFormatId nFormat = SotExchange::GetFormat( rFlavor ); |
457 | |
458 | if ( pImpEx->ExportStream( *rxOStm, OUString(), nFormat ) ) |
459 | bRet = ( rxOStm->GetError() == ERRCODE_NONE ); |
460 | } |
461 | break; |
462 | |
463 | case SCTRANS_TYPE_EDIT_RTF: |
464 | case SCTRANS_TYPE_EDIT_BIN: |
465 | { |
466 | ScTabEditEngine* pEngine = static_cast<ScTabEditEngine*>(pUserObject); |
467 | if ( nUserObjectId == SCTRANS_TYPE_EDIT_RTF ) |
468 | { |
469 | pEngine->Write( *rxOStm, EETextFormat::Rtf ); |
470 | bRet = ( rxOStm->GetError() == ERRCODE_NONE ); |
471 | } |
472 | else |
473 | { |
474 | |
475 | |
476 | |
477 | |
478 | sal_Int32 nParCnt = pEngine->GetParagraphCount(); |
479 | if ( nParCnt == 0 ) |
480 | nParCnt = 1; |
481 | ESelection aSel( 0, 0, nParCnt-1, pEngine->GetTextLen(nParCnt-1) ); |
482 | |
483 | uno::Reference<datatransfer::XTransferable> xEditTrans = pEngine->CreateTransferable( aSel ); |
484 | TransferableDataHelper aEditHelper( xEditTrans ); |
485 | |
486 | bRet = aEditHelper.GetSotStorageStream( rFlavor, rxOStm ); |
487 | } |
488 | } |
489 | break; |
490 | |
491 | case SCTRANS_TYPE_EDIT_ODF_TEXT_FLAT: |
492 | { |
493 | ScTabEditEngine* pEngine = static_cast<ScTabEditEngine*>(pUserObject); |
494 | pEngine->Write(*rxOStm, EETextFormat::Xml); |
495 | bRet = (rxOStm->GetError() == ERRCODE_NONE); |
496 | } |
497 | break; |
498 | |
499 | case SCTRANS_TYPE_EMBOBJ: |
500 | { |
501 | |
502 | SfxObjectShell* pEmbObj = static_cast<SfxObjectShell*>(pUserObject); |
503 | ::utl::TempFile aTempFile; |
504 | aTempFile.EnableKillingFile(); |
505 | uno::Reference< embed::XStorage > xWorkStore = |
506 | ::comphelper::OStorageHelper::GetStorageFromURL( aTempFile.GetURL(), embed::ElementModes::READWRITE ); |
507 | |
508 | |
509 | pEmbObj->SetupStorage( xWorkStore, SOFFICE_FILEFORMAT_CURRENT, false ); |
510 | |
511 | |
512 | SfxMedium aMedium( xWorkStore, OUString() ); |
513 | pEmbObj->DoSaveObjectAs( aMedium, false ); |
514 | pEmbObj->DoSaveCompleted(); |
515 | |
516 | uno::Reference< embed::XTransactedObject > xTransact( xWorkStore, uno::UNO_QUERY ); |
517 | if ( xTransact.is() ) |
518 | xTransact->commit(); |
519 | |
520 | std::unique_ptr<SvStream> pSrcStm = ::utl::UcbStreamHelper::CreateStream( aTempFile.GetURL(), StreamMode::READ ); |
521 | if( pSrcStm ) |
522 | { |
523 | rxOStm->SetBufferSize( 0xff00 ); |
524 | rxOStm->WriteStream( *pSrcStm ); |
525 | pSrcStm.reset(); |
526 | } |
527 | |
528 | bRet = true; |
529 | |
530 | xWorkStore->dispose(); |
531 | xWorkStore.clear(); |
532 | rxOStm->Commit(); |
533 | } |
534 | break; |
535 | |
536 | default: |
537 | OSL_FAIL("unknown object id"); |
538 | } |
539 | return bRet; |
540 | } |
541 | |
542 | sal_Bool SAL_CALL ScTransferObj::isComplex() |
543 | { |
544 | ScRange aReduced = lcl_reduceBlock(*m_pDoc, m_aBlock); |
545 | size_t nCells = (aReduced.aEnd.Col() - aReduced.aStart.Col() + 1) * |
546 | (aReduced.aEnd.Row() - aReduced.aStart.Row() + 1) * |
547 | (aReduced.aEnd.Tab() - aReduced.aStart.Tab() + 1); |
548 | return nCells > 1000; |
549 | } |
550 | |
551 | void ScTransferObj::DragFinished( sal_Int8 nDropAction ) |
552 | { |
553 | if ( nDropAction == DND_ACTION_MOVE && !m_bDragWasInternal && !(m_nDragSourceFlags & ScDragSrc::Navigator) ) |
554 | { |
555 | |
556 | ScDocShell* pSourceSh = GetSourceDocShell(); |
557 | if (pSourceSh) |
558 | { |
559 | ScMarkData aMarkData = GetSourceMarkData(); |
560 | |
561 | |
562 | pSourceSh->GetDocFunc().DeleteContents( aMarkData, InsertDeleteFlags::ALL & ~InsertDeleteFlags::OBJECTS, true, true ); |
563 | } |
564 | } |
565 | |
566 | ScModule* pScMod = SC_MOD(); |
567 | if ( pScMod->GetDragData().pCellTransfer == this ) |
568 | pScMod->ResetDragObject(); |
569 | |
570 | m_xDragSourceRanges = nullptr; |
571 | |
572 | TransferDataContainer::DragFinished( nDropAction ); |
573 | } |
574 | |
575 | void ScTransferObj::SetDragHandlePos( SCCOL nX, SCROW nY ) |
576 | { |
577 | m_nDragHandleX = nX; |
578 | m_nDragHandleY = nY; |
579 | } |
580 | |
581 | void ScTransferObj::SetSourceCursorPos( SCCOL nX, SCROW nY ) |
582 | { |
583 | m_nSourceCursorX = nX; |
584 | m_nSourceCursorY = nY; |
585 | } |
586 | |
587 | bool ScTransferObj::WasSourceCursorInSelection() const |
588 | { |
589 | return |
590 | m_nSourceCursorX >= m_aBlock.aStart.Col() && m_nSourceCursorX <= m_aBlock.aEnd.Col() && |
591 | m_nSourceCursorY >= m_aBlock.aStart.Row() && m_nSourceCursorY <= m_aBlock.aEnd.Row(); |
592 | } |
593 | |
594 | void ScTransferObj::SetVisibleTab( SCTAB nNew ) |
595 | { |
596 | m_nVisibleTab = nNew; |
597 | } |
598 | |
599 | void ScTransferObj::SetDrawPersist( const SfxObjectShellRef& rRef ) |
600 | { |
601 | m_aDrawPersistRef = rRef; |
602 | } |
603 | |
604 | void ScTransferObj::SetDragSource( ScDocShell* pSourceShell, const ScMarkData& rMark ) |
605 | { |
606 | ScRangeList aRanges; |
607 | rMark.FillRangeListWithMarks( &aRanges, false ); |
608 | m_xDragSourceRanges = new ScCellRangesObj( pSourceShell, aRanges ); |
609 | } |
610 | |
611 | void ScTransferObj::SetDragSourceFlags(ScDragSrc nFlags) |
612 | { |
613 | m_nDragSourceFlags = nFlags; |
614 | } |
615 | |
616 | void ScTransferObj::SetDragWasInternal() |
617 | { |
618 | m_bDragWasInternal = true; |
619 | } |
620 | |
621 | void ScTransferObj::SetUseInApi( bool bSet ) |
622 | { |
623 | m_bUseInApi = bSet; |
624 | } |
625 | |
626 | ScDocument* ScTransferObj::GetSourceDocument() |
627 | { |
628 | ScDocShell* pSourceDocSh = GetSourceDocShell(); |
629 | if (pSourceDocSh) |
630 | return &pSourceDocSh->GetDocument(); |
631 | return nullptr; |
632 | } |
633 | |
634 | ScDocShell* ScTransferObj::GetSourceDocShell() |
635 | { |
636 | ScCellRangesBase* pRangesObj = comphelper::getUnoTunnelImplementation<ScCellRangesBase>( m_xDragSourceRanges ); |
637 | if (pRangesObj) |
638 | return pRangesObj->GetDocShell(); |
639 | |
640 | return nullptr; |
641 | } |
642 | |
643 | ScMarkData ScTransferObj::GetSourceMarkData() const |
644 | { |
645 | ScMarkData aMarkData(m_pDoc->GetSheetLimits()); |
646 | ScCellRangesBase* pRangesObj = comphelper::getUnoTunnelImplementation<ScCellRangesBase>( m_xDragSourceRanges ); |
647 | if (pRangesObj) |
648 | { |
649 | const ScRangeList& rRanges = pRangesObj->GetRangeList(); |
650 | aMarkData.MarkFromRangeList( rRanges, false ); |
651 | } |
652 | return aMarkData; |
653 | } |
654 | |
655 | |
656 | |
657 | |
658 | |
659 | |
660 | void ScTransferObj::InitDocShell(bool bLimitToPageSize) |
661 | { |
662 | if ( m_aDocShellRef.is() ) |
663 | return; |
664 | |
665 | ScDocShell* pDocSh = new ScDocShell; |
666 | m_aDocShellRef = pDocSh; |
667 | |
668 | pDocSh->DoInitNew(); |
669 | |
670 | ScDocument& rDestDoc = pDocSh->GetDocument(); |
671 | ScMarkData aDestMark(rDestDoc.GetSheetLimits()); |
672 | aDestMark.SelectTable( 0, true ); |
673 | |
674 | rDestDoc.SetDocOptions( m_pDoc->GetDocOptions() ); |
675 | |
676 | OUString aTabName; |
677 | m_pDoc->GetName( m_aBlock.aStart.Tab(), aTabName ); |
678 | rDestDoc.RenameTab( 0, aTabName ); |
679 | |
680 | rDestDoc.CopyStdStylesFrom(*m_pDoc); |
681 | |
682 | SCCOL nStartX = m_aBlock.aStart.Col(); |
683 | SCROW nStartY = m_aBlock.aStart.Row(); |
684 | SCCOL nEndX = m_aBlock.aEnd.Col(); |
685 | SCROW nEndY = m_aBlock.aEnd.Row(); |
686 | |
687 | |
688 | |
689 | |
690 | SCCOL nCol; |
691 | SCTAB nSrcTab = m_aBlock.aStart.Tab(); |
692 | rDestDoc.SetLayoutRTL(0, m_pDoc->IsLayoutRTL(nSrcTab)); |
693 | for (nCol=nStartX; nCol<=nEndX; nCol++) |
694 | if ( m_pDoc->ColHidden(nCol, nSrcTab) ) |
695 | rDestDoc.ShowCol( nCol, 0, false ); |
696 | else |
697 | rDestDoc.SetColWidth( nCol, 0, m_pDoc->GetColWidth( nCol, nSrcTab ) ); |
698 | |
699 | if (nStartY > 0) |
700 | { |
701 | |
702 | |
703 | rDestDoc.SetManualHeight(0, nStartY - 1, 0, true); |
704 | } |
705 | for (SCROW nRow = nStartY; nRow <= nEndY; ++nRow) |
706 | { |
707 | if ( m_pDoc->RowHidden(nRow, nSrcTab) ) |
708 | rDestDoc.ShowRow( nRow, 0, false ); |
709 | else |
710 | { |
711 | rDestDoc.SetRowHeight( nRow, 0, m_pDoc->GetOriginalHeight( nRow, nSrcTab ) ); |
712 | |
713 | |
714 | bool bManual = m_pDoc->IsManualRowHeight(nRow, nSrcTab); |
715 | rDestDoc.SetManualHeight(nRow, nRow, 0, bManual); |
716 | } |
717 | } |
718 | |
719 | if (m_pDoc->GetDrawLayer() || m_pDoc->HasNotes()) |
720 | pDocSh->MakeDrawLayer(); |
721 | |
722 | |
723 | |
724 | |
725 | |
726 | ScRange aDestRange( nStartX,nStartY,0, nEndX,nEndY,0 ); |
727 | bool bWasCut = m_pDoc->IsCutMode(); |
728 | if (!bWasCut) |
729 | m_pDoc->SetClipArea( aDestRange, true ); |
730 | rDestDoc.CopyFromClip( aDestRange, aDestMark, InsertDeleteFlags::ALL, nullptr, m_pDoc.get(), false ); |
731 | m_pDoc->SetClipArea( aDestRange, bWasCut ); |
732 | |
733 | StripRefs(*m_pDoc, nStartX,nStartY, nEndX,nEndY, rDestDoc); |
734 | |
735 | ScRange aMergeRange = aDestRange; |
736 | rDestDoc.ExtendMerge( aMergeRange, true ); |
737 | |
738 | m_pDoc->CopyDdeLinks( rDestDoc ); |
739 | |
740 | |
741 | |
742 | Size aPaperSize = SvxPaperInfo::GetPaperSize( PAPER_A4 ); |
743 | ScStyleSheetPool* pStylePool = m_pDoc->GetStyleSheetPool(); |
744 | OUString aStyleName = m_pDoc->GetPageStyle( m_aBlock.aStart.Tab() ); |
745 | SfxStyleSheetBase* pStyleSheet = pStylePool->Find( aStyleName, SfxStyleFamily::Page ); |
746 | if (pStyleSheet) |
747 | { |
748 | const SfxItemSet& rSourceSet = pStyleSheet->GetItemSet(); |
749 | aPaperSize = rSourceSet.Get(ATTR_PAGE_SIZE).GetSize(); |
750 | |
751 | |
752 | ScStyleSheetPool* pDestPool = rDestDoc.GetStyleSheetPool(); |
753 | pDestPool->CopyStyleFrom( pStylePool, aStyleName, SfxStyleFamily::Page ); |
754 | } |
755 | |
756 | ScViewData aViewData( *pDocSh, nullptr ); |
757 | aViewData.SetScreen( nStartX,nStartY, nEndX,nEndY ); |
758 | aViewData.SetCurX( nStartX ); |
759 | aViewData.SetCurY( nStartY ); |
760 | |
761 | rDestDoc.SetViewOptions( m_pDoc->GetViewOptions() ); |
762 | |
763 | |
764 | |
765 | |
766 | long nPosX = 0; |
767 | long nPosY = 0; |
768 | |
769 | for (nCol=0; nCol<nStartX; nCol++) |
770 | nPosX += rDestDoc.GetColWidth( nCol, 0 ); |
771 | nPosY += rDestDoc.GetRowHeight( 0, nStartY-1, 0 ); |
772 | nPosX = static_cast<long>( nPosX * HMM_PER_TWIPS ); |
773 | nPosY = static_cast<long>( nPosY * HMM_PER_TWIPS ); |
774 | |
775 | aPaperSize.setWidth( aPaperSize.Width() * 2 ); |
776 | aPaperSize.setHeight( aPaperSize.Height() * 2 ); |
777 | |
778 | long nSizeX = 0; |
779 | long nSizeY = 0; |
780 | for (nCol=nStartX; nCol<=nEndX; nCol++) |
781 | { |
782 | long nAdd = rDestDoc.GetColWidth( nCol, 0 ); |
783 | if ( bLimitToPageSize && nSizeX+nAdd > aPaperSize.Width() && nSizeX ) |
784 | break; |
785 | nSizeX += nAdd; |
786 | } |
787 | for (SCROW nRow=nStartY; nRow<=nEndY; nRow++) |
788 | { |
789 | long nAdd = rDestDoc.GetRowHeight( nRow, 0 ); |
790 | if ( bLimitToPageSize && nSizeY+nAdd > aPaperSize.Height() && nSizeY ) |
791 | break; |
792 | nSizeY += nAdd; |
793 | } |
794 | nSizeX = static_cast<long>( nSizeX * HMM_PER_TWIPS ); |
795 | nSizeY = static_cast<long>( nSizeY * HMM_PER_TWIPS ); |
796 | |
797 | |
798 | |
799 | tools::Rectangle aNewArea( Point(nPosX,nPosY), Size(nSizeX,nSizeY) ); |
800 | |
801 | |
802 | pDocSh->SetVisArea( aNewArea ); |
803 | |
804 | pDocSh->UpdateOle(aViewData, true); |
805 | |
806 | |
807 | if ( rDestDoc.IsChartListenerCollectionNeedsUpdate() ) |
808 | rDestDoc.UpdateChartListenerCollection(); |
809 | } |
810 | |
811 | SfxObjectShell* ScTransferObj::SetDrawClipDoc( bool bAnyOle ) |
812 | { |
813 | |
814 | |
815 | ScGlobal::xDrawClipDocShellRef.clear(); |
816 | if (bAnyOle) |
817 | { |
818 | ScGlobal::xDrawClipDocShellRef = new ScDocShell(SfxModelFlags::EMBEDDED_OBJECT | SfxModelFlags::DISABLE_EMBEDDED_SCRIPTS); |
819 | ScGlobal::xDrawClipDocShellRef->DoInitNew(); |
820 | return ScGlobal::xDrawClipDocShellRef.get(); |
821 | } |
822 | else |
823 | { |
824 | ScGlobal::xDrawClipDocShellRef.clear(); |
825 | return nullptr; |
826 | } |
827 | } |
828 | |
829 | void ScTransferObj::StripRefs( ScDocument& rDoc, |
830 | SCCOL nStartX, SCROW nStartY, SCCOL nEndX, SCROW nEndY, |
831 | ScDocument& rDestDoc ) |
832 | { |
833 | |
834 | |
835 | SCTAB nSrcTab = 0; |
836 | while (nSrcTab < rDoc.GetTableCount() && !rDoc.HasTable(nSrcTab)) |
837 | ++nSrcTab; |
838 | SCTAB nDestTab = 0; |
839 | while (nDestTab < rDestDoc.GetTableCount() && !rDestDoc.HasTable(nDestTab)) |
840 | ++nDestTab; |
841 | |
842 | if (!rDoc.HasTable(nSrcTab) || !rDestDoc.HasTable(nDestTab)) |
843 | { |
844 | OSL_FAIL("Sheet not found in ScTransferObj::StripRefs"); |
845 | return; |
846 | } |
847 | |
848 | ScRange aRef; |
849 | |
850 | ScCellIterator aIter( rDoc, ScRange(nStartX, nStartY, nSrcTab, nEndX, nEndY, nSrcTab) ); |
851 | for (bool bHas = aIter.first(); bHas; bHas = aIter.next()) |
852 | { |
853 | if (aIter.getType() != CELLTYPE_FORMULA) |
854 | continue; |
855 | |
856 | ScFormulaCell* pFCell = aIter.getFormulaCell(); |
857 | bool bOut = false; |
858 | ScDetectiveRefIter aRefIter( rDoc, pFCell ); |
859 | while ( !bOut && aRefIter.GetNextRef( aRef ) ) |
860 | { |
861 | if ( aRef.aStart.Tab() != nSrcTab || aRef.aEnd.Tab() != nSrcTab || |
862 | aRef.aStart.Col() < nStartX || aRef.aEnd.Col() > nEndX || |
863 | aRef.aStart.Row() < nStartY || aRef.aEnd.Row() > nEndY ) |
864 | bOut = true; |
865 | } |
866 | if (bOut) |
867 | { |
868 | SCCOL nCol = aIter.GetPos().Col(); |
869 | SCROW nRow = aIter.GetPos().Row(); |
870 | |
871 | FormulaError nErrCode = pFCell->GetErrCode(); |
872 | ScAddress aPos(nCol, nRow, nDestTab); |
873 | if (nErrCode != FormulaError::NONE) |
874 | { |
875 | if ( rDestDoc.GetAttr( nCol,nRow,nDestTab, ATTR_HOR_JUSTIFY)->GetValue() == |
876 | SvxCellHorJustify::Standard ) |
877 | rDestDoc.ApplyAttr( nCol,nRow,nDestTab, |
878 | SvxHorJustifyItem(SvxCellHorJustify::Right, ATTR_HOR_JUSTIFY) ); |
879 | |
880 | ScSetStringParam aParam; |
881 | aParam.setTextInput(); |
882 | rDestDoc.SetString(aPos, ScGlobal::GetErrorString(nErrCode), &aParam); |
883 | } |
884 | else if (pFCell->IsValue()) |
885 | { |
886 | rDestDoc.SetValue(aPos, pFCell->GetValue()); |
887 | } |
888 | else |
889 | { |
890 | OUString aStr = pFCell->GetString().getString(); |
891 | if ( pFCell->IsMultilineResult() ) |
892 | { |
893 | ScFieldEditEngine& rEngine = rDestDoc.GetEditEngine(); |
894 | rEngine.SetTextCurrentDefaults(aStr); |
895 | rDestDoc.SetEditText(ScAddress(nCol,nRow,nDestTab), rEngine.CreateTextObject()); |
896 | } |
897 | else |
898 | { |
899 | ScSetStringParam aParam; |
900 | aParam.setTextInput(); |
901 | rDestDoc.SetString(aPos, aStr, &aParam); |
902 | } |
903 | } |
904 | } |
905 | } |
906 | } |
907 | |
908 | namespace |
909 | { |
910 | class theScTransferUnoTunnelId : public rtl::Static< UnoTunnelIdInit, theScTransferUnoTunnelId> {}; |
911 | } |
912 | |
913 | const css::uno::Sequence< sal_Int8 >& ScTransferObj::getUnoTunnelId() |
914 | { |
915 | return theScTransferUnoTunnelId::get().getSeq(); |
916 | } |
917 | |
918 | sal_Int64 SAL_CALL ScTransferObj::getSomething( const css::uno::Sequence< sal_Int8 >& rId ) |
919 | { |
920 | sal_Int64 nRet; |
921 | if( isUnoTunnelId<ScTransferObj>(rId) ) |
922 | { |
923 | nRet = reinterpret_cast< sal_Int64 >( this ); |
924 | } |
925 | else |
926 | nRet = TransferDataContainer::getSomething(rId); |
927 | return nRet; |
928 | } |
929 | |
930 | |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | |
18 | |
19 | |
20 | #ifndef INCLUDED_VCL_PTR_HXX |
21 | #define INCLUDED_VCL_PTR_HXX |
22 | |
23 | #include <sal/config.h> |
24 | |
25 | #include <rtl/ref.hxx> |
26 | |
27 | #include <utility> |
28 | #include <type_traits> |
29 | |
30 | #ifdef DBG_UTIL |
31 | #ifndef _WIN32 |
32 | #include <vcl/vclmain.hxx> |
33 | #endif |
34 | #endif |
35 | |
36 | class VclReferenceBase; |
37 | |
38 | namespace vcl::detail { |
39 | |
40 | template<typename> |
41 | constexpr bool isIncompleteOrDerivedFromVclReferenceBase(...) { return true; } |
42 | |
43 | template<typename T> constexpr bool isIncompleteOrDerivedFromVclReferenceBase( |
44 | int (*)[sizeof(T)]) |
45 | { return std::is_base_of<VclReferenceBase, T>::value; } |
46 | |
47 | } |
48 | |
49 | |
50 | |
51 | |
52 | |
53 | |
54 | |
55 | |
56 | template <class reference_type> |
57 | class VclPtr |
58 | { |
59 | static_assert( |
60 | vcl::detail::isIncompleteOrDerivedFromVclReferenceBase<reference_type>( |
61 | nullptr), |
62 | "template argument type must be derived from VclReferenceBase"); |
63 | |
64 | ::rtl::Reference<reference_type> m_rInnerRef; |
65 | |
66 | public: |
67 | |
68 | |
69 | VclPtr() |
70 | : m_rInnerRef() |
71 | {} |
72 | |
73 | |
74 | |
75 | VclPtr (reference_type * pBody) |
76 | : m_rInnerRef(pBody) |
77 | {} |
78 | |
79 | |
80 | |
81 | VclPtr (reference_type * pBody, __sal_NoAcquire) |
82 | : m_rInnerRef(pBody, SAL_NO_ACQUIRE) |
83 | {} |
84 | |
85 | |
86 | |
87 | |
88 | |
89 | |
90 | |
91 | |
92 | |
93 | template< class derived_type > |
94 | VclPtr( |
95 | const VclPtr< derived_type > & rRef, |
96 | typename std::enable_if< |
97 | std::is_base_of<reference_type, derived_type>::value, int>::type |
98 | = 0 ) |
99 | : m_rInnerRef( static_cast<reference_type*>(rRef) ) |
100 | { |
101 | } |
102 | |
103 | #if defined(DBG_UTIL) && !defined(_WIN32) |
104 | virtual ~VclPtr() |
105 | { |
106 | assert(m_rInnerRef.get() == nullptr || vclmain::isAlive()); |
107 | |
108 | |
109 | assert((!m_rInnerRef.get() || m_rInnerRef->isDisposed() || m_rInnerRef->getRefCount() > 1) |
110 | && "someone forgot to call dispose()"); |
111 | } |
112 | VclPtr(VclPtr const &) = default; |
113 | VclPtr(VclPtr &&) = default; |
114 | VclPtr & operator =(VclPtr const &) = default; |
115 | VclPtr & operator =(VclPtr &&) = default; |
116 | #endif |
117 | |
118 | |
119 | |
120 | |
121 | |
122 | |
123 | |
124 | |
125 | |
126 | |
127 | template<typename... Arg> [[nodiscard]] static VclPtr< reference_type > Create(Arg &&... arg) |
128 | { |
129 | return VclPtr< reference_type >( new reference_type(std::forward<Arg>(arg)...), SAL_NO_ACQUIRE ); |
130 | } |
131 | |
132 | |
133 | |
134 | reference_type * operator->() const |
135 | { |
136 | return m_rInnerRef.get(); |
137 | } |
138 | |
139 | |
140 | |
141 | |
142 | |
143 | reference_type * get() const |
144 | { |
145 | return m_rInnerRef.get(); |
146 | } |
147 | |
148 | void set(reference_type *pBody) |
149 | { |
150 | m_rInnerRef.set(pBody); |
151 | } |
152 | |
153 | void reset(reference_type *pBody) |
154 | { |
155 | m_rInnerRef.set(pBody); |
156 | } |
157 | |
158 | |
159 | |
160 | |
161 | |
162 | |
163 | |
164 | template<typename derived_type> |
165 | typename std::enable_if< |
166 | std::is_base_of<reference_type, derived_type>::value, |
167 | VclPtr &>::type |
168 | operator =(VclPtr<derived_type> const & rRef) |
169 | { |
170 | m_rInnerRef.set(rRef.get()); |
171 | return *this; |
172 | } |
173 | |
174 | VclPtr & operator =(reference_type * pBody) |
175 | { |
176 | m_rInnerRef.set(pBody); |
177 | return *this; |
178 | } |
179 | |
180 | operator reference_type * () const |
181 | { |
182 | return m_rInnerRef.get(); |
183 | } |
184 | |
185 | explicit operator bool () const |
186 | { |
187 | return m_rInnerRef.get() != nullptr; |
188 | } |
189 | |
190 | void clear() |
191 | { |
192 | m_rInnerRef.clear(); |
193 | } |
194 | |
195 | void reset() |
196 | { |
197 | m_rInnerRef.clear(); |
198 | } |
199 | |
200 | void disposeAndClear() |
201 | { |
202 | |
203 | ::rtl::Reference<reference_type> aTmp(m_rInnerRef); |
204 | m_rInnerRef.clear(); |
| 24 | | Calling 'Reference::clear' | |
|
| 31 | | Returning; memory was released | |
|
205 | if (aTmp.get()) { |
| 32 | | Calling 'Reference::get' | |
|
206 | aTmp->disposeOnce(); |
207 | } |
208 | } |
209 | |
210 | |
211 | |
212 | bool operator< (const VclPtr<reference_type> & handle) const |
213 | { |
214 | return (m_rInnerRef < handle.m_rInnerRef); |
215 | } |
216 | }; |
217 | |
218 | template<typename T1, typename T2> |
219 | inline bool operator ==(VclPtr<T1> const & p1, VclPtr<T2> const & p2) { |
220 | return p1.get() == p2.get(); |
221 | } |
222 | |
223 | template<typename T> inline bool operator ==(VclPtr<T> const & p1, T const * p2) |
224 | { |
225 | return p1.get() == p2; |
226 | } |
227 | |
228 | template<typename T> inline bool operator ==(VclPtr<T> const & p1, T * p2) { |
229 | return p1.get() == p2; |
230 | } |
231 | |
232 | template<typename T> inline bool operator ==(T const * p1, VclPtr<T> const & p2) |
233 | { |
234 | return p1 == p2.get(); |
235 | } |
236 | |
237 | template<typename T> inline bool operator ==(T * p1, VclPtr<T> const & p2) { |
238 | return p1 == p2.get(); |
239 | } |
240 | |
241 | template<typename T1, typename T2> |
242 | inline bool operator !=(VclPtr<T1> const & p1, VclPtr<T2> const & p2) { |
243 | return !(p1 == p2); |
244 | } |
245 | |
246 | template<typename T> inline bool operator !=(VclPtr<T> const & p1, T const * p2) |
247 | { |
248 | return !(p1 == p2); |
249 | } |
250 | |
251 | template<typename T> inline bool operator !=(VclPtr<T> const & p1, T * p2) { |
252 | return !(p1 == p2); |
253 | } |
254 | |
255 | template<typename T> inline bool operator !=(T const * p1, VclPtr<T> const & p2) |
256 | { |
257 | return !(p1 == p2); |
258 | } |
259 | |
260 | template<typename T> inline bool operator !=(T * p1, VclPtr<T> const & p2) { |
261 | return !(p1 == p2); |
262 | } |
263 | |
264 | |
265 | |
266 | |
267 | |
268 | |
269 | |
270 | |
271 | |
272 | |
273 | |
274 | template <class reference_type> |
275 | class SAL_WARN_UNUSED VclPtrInstance final : public VclPtr<reference_type> |
276 | { |
277 | public: |
278 | template<typename... Arg> VclPtrInstance(Arg &&... arg) |
279 | : VclPtr<reference_type>( new reference_type(std::forward<Arg>(arg)...), SAL_NO_ACQUIRE ) |
280 | { |
281 | } |
282 | |
283 | |
284 | |
285 | |
286 | |
287 | template<typename... Arg> static VclPtrInstance< reference_type > Create(Arg &&... ) = delete; |
288 | }; |
289 | |
290 | template <class reference_type> |
291 | class ScopedVclPtr : public VclPtr<reference_type> |
292 | { |
293 | public: |
294 | |
295 | |
296 | ScopedVclPtr() |
297 | : VclPtr<reference_type>() |
298 | {} |
299 | |
300 | |
301 | |
302 | ScopedVclPtr (reference_type * pBody) |
303 | : VclPtr<reference_type>(pBody) |
304 | {} |
305 | |
306 | |
307 | |
308 | ScopedVclPtr (const VclPtr<reference_type> & handle) |
309 | : VclPtr<reference_type>(handle) |
310 | {} |
311 | |
312 | |
313 | |
314 | |
315 | void disposeAndReset(reference_type *pBody) |
316 | { |
317 | if (pBody != this->get()) { |
318 | VclPtr<reference_type>::disposeAndClear(); |
319 | VclPtr<reference_type>::set(pBody); |
320 | } |
321 | } |
322 | |
323 | |
324 | |
325 | |
326 | ScopedVclPtr<reference_type>& operator = (reference_type * pBody) |
327 | { |
328 | disposeAndReset(pBody); |
329 | return *this; |
330 | } |
331 | |
332 | |
333 | |
334 | |
335 | |
336 | |
337 | |
338 | |
339 | |
340 | template< class derived_type > |
341 | ScopedVclPtr( |
342 | const VclPtr< derived_type > & rRef, |
343 | typename std::enable_if< |
344 | std::is_base_of<reference_type, derived_type>::value, int>::type |
345 | = 0 ) |
346 | : VclPtr<reference_type>( rRef ) |
347 | { |
348 | } |
349 | |
350 | |
351 | |
352 | |
353 | |
354 | |
355 | |
356 | template<typename derived_type> |
357 | typename std::enable_if< |
358 | std::is_base_of<reference_type, derived_type>::value, |
359 | ScopedVclPtr &>::type |
360 | operator =(VclPtr<derived_type> const & rRef) |
361 | { |
362 | disposeAndReset(rRef.get()); |
363 | return *this; |
364 | } |
365 | |
366 | |
367 | |
368 | |
369 | |
370 | template<typename... Arg> static ScopedVclPtr< reference_type > Create(Arg &&... ) = delete; |
371 | |
372 | ~ScopedVclPtr() |
373 | { |
374 | VclPtr<reference_type>::disposeAndClear(); |
| 23 | | Calling 'VclPtr::disposeAndClear' | |
|
375 | assert(VclPtr<reference_type>::get() == nullptr); |
376 | } |
377 | |
378 | private: |
379 | |
380 | ScopedVclPtr (const ScopedVclPtr<reference_type> &) = delete; |
381 | |
382 | ScopedVclPtr<reference_type>& operator = (const ScopedVclPtr<reference_type> &) = delete; |
383 | |
384 | void reset() = delete; |
385 | void reset(reference_type *pBody) = delete; |
386 | |
387 | protected: |
388 | ScopedVclPtr (reference_type * pBody, __sal_NoAcquire) |
389 | : VclPtr<reference_type>(pBody, SAL_NO_ACQUIRE) |
390 | {} |
391 | }; |
392 | |
393 | |
394 | |
395 | |
396 | |
397 | |
398 | |
399 | |
400 | |
401 | |
402 | #if defined _MSC_VER |
403 | #pragma warning(push) |
404 | #pragma warning(disable: 4521) // " multiple copy constructors specified" |
405 | #endif |
406 | template <class reference_type> |
407 | class SAL_WARN_UNUSED ScopedVclPtrInstance final : public ScopedVclPtr<reference_type> |
408 | { |
409 | public: |
410 | template<typename... Arg> ScopedVclPtrInstance(Arg &&... arg) |
411 | : ScopedVclPtr<reference_type>( new reference_type(std::forward<Arg>(arg)...), SAL_NO_ACQUIRE ) |
| |
412 | { |
413 | } |
414 | |
415 | |
416 | |
417 | |
418 | |
419 | template<typename... Arg> static ScopedVclPtrInstance< reference_type > Create(Arg &&...) = delete; |
420 | |
421 | private: |
422 | |
423 | |
424 | |
425 | |
426 | |
427 | |
428 | |
429 | |
430 | |
431 | |
432 | |
433 | ScopedVclPtrInstance(ScopedVclPtrInstance &) = delete; |
434 | ScopedVclPtrInstance(ScopedVclPtrInstance const &) = delete; |
435 | }; |
436 | #if defined _MSC_VER |
437 | #pragma warning(pop) |
438 | #endif |
439 | |
440 | #endif // INCLUDED_VCL_PTR_HXX |
441 | |
442 | |