Avoid potential negative array index access to cached text.
[LibreOffice.git] / chart2 / source / controller / drawinglayer / ViewElementListProvider.cxx
blob0b3df1aa22ad03a3167bc112cd0028bb470ab9c2
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 <memory>
21 #include <ViewElementListProvider.hxx>
22 #include <chartview/DrawModelWrapper.hxx>
23 #include <chartview/DataPointSymbolSupplier.hxx>
24 #include <DrawViewWrapper.hxx>
26 #include <com/sun/star/drawing/Direction3D.hpp>
27 #include <o3tl/safeint.hxx>
28 #include <svx/xtable.hxx>
29 #include <svl/itempool.hxx>
30 #include <svtools/ctrltool.hxx>
31 #include <vcl/svapp.hxx>
32 #include <svx/svdobj.hxx>
33 #include <vcl/virdev.hxx>
34 #include <svx/svdview.hxx>
35 #include <svx/svdpage.hxx>
36 #include <comphelper/diagnose_ex.hxx>
38 namespace chart
40 using namespace ::com::sun::star;
42 ViewElementListProvider::ViewElementListProvider( DrawModelWrapper* pDrawModelWrapper )
43 : m_pDrawModelWrapper( pDrawModelWrapper )
47 ViewElementListProvider::ViewElementListProvider(ViewElementListProvider&& rOther) noexcept
49 m_pDrawModelWrapper = rOther.m_pDrawModelWrapper;
50 m_pFontList = std::move(rOther.m_pFontList);
53 ViewElementListProvider::~ViewElementListProvider()
57 XColorListRef ViewElementListProvider::GetColorTable() const
59 if(m_pDrawModelWrapper)
60 return m_pDrawModelWrapper->GetColorList();
61 return XColorListRef();
64 XDashListRef ViewElementListProvider::GetDashList() const
66 if(m_pDrawModelWrapper)
67 return m_pDrawModelWrapper->GetDashList();
68 return XDashListRef();
71 XLineEndListRef ViewElementListProvider::GetLineEndList() const
73 if(m_pDrawModelWrapper)
74 return m_pDrawModelWrapper->GetLineEndList();
75 return XLineEndListRef();
78 XGradientListRef ViewElementListProvider::GetGradientList() const
80 if(m_pDrawModelWrapper)
81 return m_pDrawModelWrapper->GetGradientList();
82 return XGradientListRef();
84 XHatchListRef ViewElementListProvider::GetHatchList() const
86 if(m_pDrawModelWrapper)
87 return m_pDrawModelWrapper->GetHatchList();
88 return nullptr;
91 XBitmapListRef ViewElementListProvider::GetBitmapList() const
93 if(m_pDrawModelWrapper)
94 return m_pDrawModelWrapper->GetBitmapList();
95 return XBitmapListRef();
98 XPatternListRef ViewElementListProvider::GetPatternList() const
100 if(m_pDrawModelWrapper)
101 return m_pDrawModelWrapper->GetPatternList();
102 return XPatternListRef();
105 //create chartspecific symbols for linecharts
106 SdrObjList* ViewElementListProvider::GetSymbolList() const
108 SdrObjList* pSymbolList = nullptr;
111 //@todo use mutex
113 //get hidden draw page (target):
114 rtl::Reference<SvxDrawPage> xTarget = m_pDrawModelWrapper->getHiddenDrawPage();
116 //create symbols via uno and convert to native sdr objects
117 drawing::Direction3D aSymbolSize(220, 220, 0); // should be 250, but 250 -> 280 ??
118 rtl::Reference< SvxShapeGroup > xSymbols
119 = DataPointSymbolSupplier::create2DSymbolList(xTarget, aSymbolSize);
121 SdrObject* pSdrObject = DrawViewWrapper::getSdrObject(
122 uno::Reference<drawing::XShape>(static_cast<cppu::OWeakObject*>(xSymbols.get()), uno::UNO_QUERY));
123 if (pSdrObject)
124 pSymbolList = pSdrObject->GetSubList();
126 catch( const uno::Exception& )
128 TOOLS_WARN_EXCEPTION("chart2", "" );
130 return pSymbolList;
133 Graphic ViewElementListProvider::GetSymbolGraphic( sal_Int32 nStandardSymbol, const SfxItemSet* pSymbolShapeProperties ) const
135 SdrObjList* pSymbolList = GetSymbolList();
136 if( !pSymbolList->GetObjCount() )
137 return Graphic();
138 if(nStandardSymbol<0)
139 nStandardSymbol*=-1;
140 if( o3tl::make_unsigned(nStandardSymbol) >= pSymbolList->GetObjCount() )
141 nStandardSymbol %= pSymbolList->GetObjCount();
142 rtl::Reference<SdrObject> pObj = pSymbolList->GetObj(nStandardSymbol);
144 ScopedVclPtrInstance< VirtualDevice > pVDev;
145 pVDev->SetMapMode(MapMode(MapUnit::Map100thMM));
147 std::unique_ptr<SdrModel> pModel(
148 new SdrModel());
150 pModel->GetItemPool().FreezeIdRanges();
151 rtl::Reference<SdrPage> pPage = new SdrPage( *pModel, false );
152 pPage->SetSize(Size(1000,1000));
153 pModel->InsertPage( pPage.get(), 0 );
154 SdrView aView(*pModel, pVDev);
155 aView.hideMarkHandles();
156 SdrPageView* pPageView = aView.ShowSdrPage(pPage.get());
158 // directly clone to target SdrModel
159 pObj = pObj->CloneSdrObject(*pModel);
161 pPage->NbcInsertObject(pObj.get());
162 aView.MarkObj(pObj.get(),pPageView);
163 if( pSymbolShapeProperties )
164 pObj->SetMergedItemSet(*pSymbolShapeProperties);
166 GDIMetaFile aMeta(aView.GetMarkedObjMetaFile());
168 Graphic aGraph(aMeta);
169 Size aSize = pObj->GetSnapRect().GetSize();
170 aGraph.SetPrefSize(aSize);
171 aGraph.SetPrefMapMode(MapMode(MapUnit::Map100thMM));
173 aView.UnmarkAll();
174 pObj=pPage->RemoveObject(0);
175 // these need to die before the associated SdrModel
176 pObj.clear();
177 pPage.clear();
179 return aGraph;
182 FontList* ViewElementListProvider::getFontList() const
184 //was old chart:
185 //SvxFontListItem* SfxObjectShell::.GetItem(SID_ATTR_CHAR_FONTLIST)
187 if(!m_pFontList)
189 OutputDevice* pRefDev = m_pDrawModelWrapper ? m_pDrawModelWrapper->getReferenceDevice() : nullptr;
190 OutputDevice* pDefaultOut = Application::GetDefaultDevice();
191 m_pFontList.reset( new FontList( pRefDev ? pRefDev : pDefaultOut
192 , pRefDev ? pDefaultOut : nullptr) );
194 return m_pFontList.get();
196 } //namespace chart
198 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */