/src/libreoffice/sc/source/ui/drawfunc/fuins2.cxx
Line | Count | Source |
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 <com/sun/star/embed/NoVisualAreaSizeException.hpp> |
21 | | #include <com/sun/star/embed/Aspects.hpp> |
22 | | #include <com/sun/star/embed/XEmbeddedObject.hpp> |
23 | | #include <com/sun/star/beans/XPropertySet.hpp> |
24 | | #include <com/sun/star/uno/XComponentContext.hpp> |
25 | | |
26 | | #include <toolkit/helper/vclunohelper.hxx> |
27 | | #include <sot/exchange.hxx> |
28 | | #include <svl/globalnameitem.hxx> |
29 | | #include <sfx2/viewfrm.hxx> |
30 | | #include <svl/stritem.hxx> |
31 | | #include <svx/svdoole2.hxx> |
32 | | #include <unotools/moduleoptions.hxx> |
33 | | #include <svtools/insdlg.hxx> |
34 | | #include <svtools/embedhlp.hxx> |
35 | | #include <svtools/strings.hrc> |
36 | | #include <svtools/svtresid.hxx> |
37 | | #include <svx/svxdlg.hxx> |
38 | | #include <comphelper/classids.hxx> |
39 | | #include <svx/svdpagv.hxx> |
40 | | #include <svx/svdpage.hxx> |
41 | | #include <svx/svdundo.hxx> |
42 | | #include <sfx2/msgpool.hxx> |
43 | | #include <sfx2/msg.hxx> |
44 | | #include <scmod.hxx> |
45 | | #include <sal/log.hxx> |
46 | | #include <comphelper/diagnose_ex.hxx> |
47 | | #include <vcl/svapp.hxx> |
48 | | #include <vcl/weld/MessageDialog.hxx> |
49 | | #include <vcl/weld/weldutils.hxx> |
50 | | |
51 | | #include <comphelper/lok.hxx> |
52 | | #include <comphelper/storagehelper.hxx> |
53 | | #include <comphelper/propertysequence.hxx> |
54 | | #include <com/sun/star/embed/EmbedVerbs.hpp> |
55 | | #include <com/sun/star/beans/PropertyValue.hpp> |
56 | | #include <com/sun/star/chart2/data/XDataReceiver.hpp> |
57 | | #include <com/sun/star/chart2/XChartDocument.hpp> |
58 | | #include <com/sun/star/ui/dialogs/XAsynchronousExecutableDialog.hpp> |
59 | | #include <com/sun/star/lang/XInitialization.hpp> |
60 | | #include <com/sun/star/frame/XModel.hpp> |
61 | | #include <com/sun/star/chart/ChartDataRowSource.hpp> |
62 | | #include <svtools/dialogclosedlistener.hxx> |
63 | | #include <officecfg/Office/Common.hxx> |
64 | | |
65 | | #include <PivotTableDataProvider.hxx> |
66 | | #include <chart2uno.hxx> |
67 | | #include <fuinsert.hxx> |
68 | | #include <tabvwsh.hxx> |
69 | | #include <sc.hrc> |
70 | | #include <chartpos.hxx> |
71 | | #include <docsh.hxx> |
72 | | #include <document.hxx> |
73 | | #include <undotab.hxx> |
74 | | #include <uiitems.hxx> |
75 | | #include <drawview.hxx> |
76 | | #include <markdata.hxx> |
77 | | #include <dpobject.hxx> |
78 | | #include <memory> |
79 | | |
80 | | using namespace css; |
81 | | |
82 | | namespace |
83 | | { |
84 | | |
85 | | void lcl_ChartInit(const uno::Reference <embed::XEmbeddedObject>& xObj, ScViewData* pViewData, |
86 | | const OUString& rRangeParam, bool bRangeIsPivotTable) |
87 | 0 | { |
88 | 0 | ScDocShell* pDocShell = pViewData->GetDocShell(); |
89 | 0 | ScDocument& rScDoc = pDocShell->GetDocument(); |
90 | |
|
91 | 0 | OUString aRangeString(rRangeParam); |
92 | |
|
93 | 0 | if (aRangeString.isEmpty() && !bRangeIsPivotTable) |
94 | 0 | { |
95 | 0 | SCCOL nCol1 = 0; |
96 | 0 | SCROW nRow1 = 0; |
97 | 0 | SCTAB nTab1 = 0; |
98 | 0 | SCCOL nCol2 = 0; |
99 | 0 | SCROW nRow2 = 0; |
100 | 0 | SCTAB nTab2 = 0; |
101 | |
|
102 | 0 | ScMarkData& rMark = pViewData->GetMarkData(); |
103 | 0 | if ( !rMark.IsMarked() ) |
104 | 0 | pViewData->GetView()->MarkDataArea(); |
105 | |
|
106 | 0 | if ( pViewData->GetSimpleArea( nCol1,nRow1,nTab1, nCol2,nRow2,nTab2 ) == SC_MARK_SIMPLE ) |
107 | 0 | { |
108 | 0 | PutInOrder( nCol1, nCol2 ); |
109 | 0 | PutInOrder( nRow1, nRow2 ); |
110 | 0 | if (nCol2 >= nCol1 || nRow2 >= nRow1) |
111 | 0 | { |
112 | 0 | ScDocument& rDoc = pViewData->GetDocument(); |
113 | 0 | rDoc.LimitChartArea( nTab1, nCol1,nRow1, nCol2,nRow2 ); |
114 | |
|
115 | 0 | ScRange aRange( nCol1, nRow1, nTab1, nCol2, nRow2, nTab2 ); |
116 | 0 | aRangeString = aRange.Format(rScDoc, ScRefFlags::RANGE_ABS_3D, rScDoc.GetAddressConvention()); |
117 | 0 | } |
118 | 0 | } |
119 | 0 | } |
120 | |
|
121 | 0 | if (aRangeString.isEmpty()) |
122 | 0 | return; |
123 | | |
124 | | // connect to Calc data (if no range string, leave chart alone, with its own data) |
125 | | |
126 | 0 | uno::Reference< css::chart2::data::XDataReceiver > xReceiver; |
127 | 0 | if( xObj.is()) |
128 | 0 | xReceiver.set( xObj->getComponent(), uno::UNO_QUERY ); |
129 | 0 | OSL_ASSERT( xReceiver.is()); |
130 | 0 | if( !xReceiver.is() ) |
131 | 0 | return; |
132 | | |
133 | 0 | uno::Reference<chart2::data::XDataProvider> xDataProvider; |
134 | 0 | if (bRangeIsPivotTable) |
135 | 0 | { |
136 | 0 | rtl::Reference<sc::PivotTableDataProvider> pPivotTableDataProvider(new sc::PivotTableDataProvider(rScDoc)); |
137 | 0 | pPivotTableDataProvider->setPivotTableName(aRangeString); |
138 | 0 | xDataProvider = pPivotTableDataProvider; |
139 | 0 | } |
140 | 0 | else |
141 | 0 | { |
142 | 0 | xDataProvider = new ScChart2DataProvider(&rScDoc); |
143 | 0 | } |
144 | |
|
145 | 0 | xReceiver->attachDataProvider(xDataProvider); |
146 | |
|
147 | 0 | uno::Reference< util::XNumberFormatsSupplier > xNumberFormatsSupplier( getXWeak(pDocShell->GetModel()), uno::UNO_QUERY ); |
148 | 0 | xReceiver->attachNumberFormatsSupplier( xNumberFormatsSupplier ); |
149 | | |
150 | | // Same behavior as with old chart: Always assume data series in columns |
151 | 0 | chart::ChartDataRowSource eDataRowSource = chart::ChartDataRowSource_COLUMNS; |
152 | 0 | bool bHasCategories = false; |
153 | 0 | bool bFirstCellAsLabel = false; |
154 | | |
155 | | // use ScChartPositioner to auto-detect column/row headers (like ScChartArray in old version) |
156 | 0 | ScRangeListRef aRangeListRef( new ScRangeList ); |
157 | 0 | aRangeListRef->Parse( aRangeString, rScDoc, rScDoc.GetAddressConvention() ); |
158 | 0 | if ( !aRangeListRef->empty() ) |
159 | 0 | { |
160 | 0 | rScDoc.LimitChartIfAll( aRangeListRef ); // limit whole columns/rows to used area |
161 | | |
162 | | // update string from modified ranges. The ranges must be in the current formula syntax. |
163 | 0 | OUString aTmpStr; |
164 | 0 | aRangeListRef->Format( aTmpStr, ScRefFlags::RANGE_ABS_3D, rScDoc, rScDoc.GetAddressConvention() ); |
165 | 0 | aRangeString = aTmpStr; |
166 | |
|
167 | 0 | ScChartPositioner aChartPositioner( rScDoc, std::move(aRangeListRef) ); |
168 | 0 | const ScChartPositionMap* pPositionMap( aChartPositioner.GetPositionMap() ); |
169 | 0 | if( pPositionMap ) |
170 | 0 | { |
171 | 0 | SCSIZE nRowCount = pPositionMap->GetRowCount(); |
172 | 0 | if( 1==nRowCount ) |
173 | 0 | eDataRowSource = chart::ChartDataRowSource_ROWS; |
174 | 0 | } |
175 | 0 | if ( eDataRowSource == chart::ChartDataRowSource_COLUMNS ) |
176 | 0 | { |
177 | 0 | bHasCategories = aChartPositioner.HasRowHeaders(); |
178 | 0 | bFirstCellAsLabel = aChartPositioner.HasColHeaders(); |
179 | 0 | } |
180 | 0 | else // in case the default is changed |
181 | 0 | { |
182 | 0 | bHasCategories = aChartPositioner.HasColHeaders(); |
183 | 0 | bFirstCellAsLabel = aChartPositioner.HasRowHeaders(); |
184 | 0 | } |
185 | 0 | } |
186 | |
|
187 | 0 | uno::Sequence< beans::PropertyValue > aArgs{ |
188 | 0 | beans::PropertyValue( |
189 | 0 | u"CellRangeRepresentation"_ustr, -1, |
190 | 0 | uno::Any( aRangeString ), beans::PropertyState_DIRECT_VALUE ), |
191 | 0 | beans::PropertyValue( |
192 | 0 | u"HasCategories"_ustr, -1, |
193 | 0 | uno::Any( bHasCategories ), beans::PropertyState_DIRECT_VALUE ), |
194 | 0 | beans::PropertyValue( |
195 | 0 | u"FirstCellAsLabel"_ustr, -1, |
196 | 0 | uno::Any( bFirstCellAsLabel ), beans::PropertyState_DIRECT_VALUE ), |
197 | 0 | beans::PropertyValue( |
198 | 0 | u"DataRowSource"_ustr, -1, |
199 | 0 | uno::Any( eDataRowSource ), beans::PropertyState_DIRECT_VALUE ) |
200 | 0 | }; |
201 | |
|
202 | 0 | try |
203 | 0 | { |
204 | 0 | xReceiver->setArguments( aArgs ); |
205 | 0 | } |
206 | 0 | catch (const lang::IllegalArgumentException&) |
207 | 0 | { |
208 | | // Can happen for invalid aRangeString, in which case a Chart |
209 | | // will be created nevertheless and the range string can be |
210 | | // edited. |
211 | 0 | TOOLS_WARN_EXCEPTION("sc.ui", |
212 | 0 | "lcl_ChartInit - caught IllegalArgumentException might be due to aRangeString: " << aRangeString); |
213 | 0 | } |
214 | | |
215 | | // don't create chart listener here (range may be modified in chart dialog) |
216 | 0 | } |
217 | | |
218 | | } |
219 | | |
220 | | FuInsertOLE::FuInsertOLE(ScTabViewShell& rViewSh, vcl::Window* pWin, ScDrawView* pViewP, |
221 | | SdrModel& rDoc, SfxRequest& rReq) |
222 | 0 | : FuPoor(rViewSh, pWin, pViewP, rDoc, rReq) |
223 | 0 | { |
224 | 0 | if( ! rReq.IsAPI() ) |
225 | 0 | rReq.Done(); |
226 | | |
227 | | //! initialize DLLs here, so that the factories exist? |
228 | |
|
229 | 0 | uno::Reference < embed::XEmbeddedObject > xObj; |
230 | 0 | uno::Reference < embed::XStorage > xStorage = comphelper::OStorageHelper::GetTemporaryStorage(); |
231 | 0 | bool bIsFromFile = false; |
232 | 0 | OUString aName; |
233 | |
|
234 | 0 | sal_Int64 nAspect = embed::Aspects::MSOLE_CONTENT; |
235 | 0 | OUString aIconMediaType; |
236 | 0 | uno::Reference< io::XInputStream > xIconMetaFile; |
237 | |
|
238 | 0 | const sal_uInt16 nSlot = rReq.GetSlot(); |
239 | 0 | const SfxGlobalNameItem* pNameItem = rReq.GetArg(SID_INSERT_OBJECT); |
240 | 0 | if ( nSlot == SID_INSERT_OBJECT && pNameItem ) |
241 | 0 | { |
242 | 0 | const SvGlobalName& aClassName = pNameItem->GetValue(); |
243 | 0 | xObj = rViewShell.GetViewFrame().GetObjectShell()->GetEmbeddedObjectContainer().CreateEmbeddedObject( aClassName.GetByteSequence(), aName ); |
244 | 0 | } |
245 | 0 | else if ( nSlot == SID_INSERT_SMATH ) |
246 | 0 | { |
247 | 0 | if ( SvtModuleOptions().IsMathInstalled() ) |
248 | 0 | { |
249 | 0 | xObj = rViewShell.GetViewFrame().GetObjectShell()->GetEmbeddedObjectContainer().CreateEmbeddedObject( SvGlobalName( SO3_SM_CLASSID_60 ).GetByteSequence(), aName ); |
250 | 0 | rReq.AppendItem( SfxGlobalNameItem( SID_INSERT_OBJECT, SvGlobalName( SO3_SM_CLASSID_60 ) ) ); |
251 | 0 | } |
252 | 0 | } |
253 | 0 | else |
254 | 0 | { |
255 | 0 | SvObjectServerList aServerLst; |
256 | 0 | switch ( nSlot ) |
257 | 0 | { |
258 | 0 | case SID_INSERT_OBJECT : |
259 | 0 | if (officecfg::Office::Common::Security::Scripting::DisableActiveContent::get()) |
260 | 0 | { |
261 | 0 | std::unique_ptr<weld::MessageDialog> xError(Application::CreateMessageDialog( |
262 | 0 | nullptr, VclMessageType::Warning, VclButtonsType::Ok, |
263 | 0 | SvtResId(STR_WARNING_ACTIVE_CONTENT_DISABLED))); |
264 | 0 | xError->run(); |
265 | 0 | break; |
266 | 0 | } |
267 | 0 | aServerLst.FillInsertObjects(); |
268 | 0 | aServerLst.Remove( ScDocShell::Factory().GetClassId() ); // Do not show Starcalc |
269 | | //TODO/LATER: currently no inserting of ClassId into SfxRequest! |
270 | 0 | [[fallthrough]]; //TODO ??? |
271 | 0 | case SID_INSERT_FLOATINGFRAME : |
272 | 0 | { |
273 | 0 | SvxAbstractDialogFactory* pFact = SvxAbstractDialogFactory::Create(); |
274 | 0 | ScopedVclPtr<SfxAbstractInsertObjectDialog> pDlg( |
275 | 0 | pFact->CreateInsertObjectDialog( rViewShell.GetFrameWeld(), ScModule::get()->GetSlotPool()->GetSlot(nSlot)->GetCommand(), |
276 | 0 | xStorage, &aServerLst )); |
277 | 0 | if ( pDlg ) |
278 | 0 | { |
279 | 0 | pDlg->Execute(); |
280 | 0 | xObj = pDlg->GetObject(); |
281 | |
|
282 | 0 | xIconMetaFile = pDlg->GetIconIfIconified( &aIconMediaType ); |
283 | 0 | if ( xIconMetaFile.is() ) |
284 | 0 | nAspect = embed::Aspects::MSOLE_ICON; |
285 | |
|
286 | 0 | if ( xObj.is() ) |
287 | 0 | rViewSh.GetObjectShell()->GetEmbeddedObjectContainer().InsertEmbeddedObject( xObj, aName ); |
288 | | // to activate DrawShell (no need to activate Object) |
289 | 0 | bIsFromFile = !pDlg->IsCreateNew(); |
290 | 0 | } |
291 | |
|
292 | 0 | break; |
293 | 0 | } |
294 | 0 | } |
295 | 0 | } |
296 | | |
297 | | // SvInsertObjectDialog (everything in one Dialog) are not used anymore |
298 | 0 | if (xObj.is()) |
299 | 0 | { |
300 | 0 | pView->UnmarkAll(); |
301 | |
|
302 | 0 | try |
303 | 0 | { |
304 | 0 | ::svt::EmbeddedObjectRef aObjRef( xObj, nAspect ); |
305 | 0 | Size aSize; |
306 | 0 | MapMode aMap100( MapUnit::Map100thMM ); |
307 | 0 | MapUnit aMapUnit = MapUnit::Map100thMM; |
308 | |
|
309 | 0 | if ( nAspect == embed::Aspects::MSOLE_ICON ) |
310 | 0 | { |
311 | 0 | aObjRef.SetGraphicStream( xIconMetaFile, aIconMediaType ); |
312 | 0 | aSize = aObjRef.GetSize( &aMap100 ); |
313 | 0 | } |
314 | 0 | else |
315 | 0 | { |
316 | 0 | awt::Size aSz; |
317 | 0 | try |
318 | 0 | { |
319 | 0 | aSz = xObj->getVisualAreaSize( nAspect ); |
320 | 0 | } |
321 | 0 | catch( embed::NoVisualAreaSizeException& ) |
322 | 0 | { |
323 | | // the default size will be set later |
324 | 0 | } |
325 | |
|
326 | 0 | aSize = Size( aSz.Width, aSz.Height ); |
327 | |
|
328 | 0 | aMapUnit = VCLUnoHelper::UnoEmbed2VCLMapUnit( xObj->getMapUnit( nAspect ) ); |
329 | 0 | if (aSize.IsEmpty()) |
330 | 0 | { |
331 | | // rectangle with balanced edge ratio |
332 | 0 | aSize.setWidth( 5000 ); |
333 | 0 | aSize.setHeight( 5000 ); |
334 | 0 | Size aTmp = OutputDevice::LogicToLogic(aSize, MapMode(MapUnit::Map100thMM), MapMode(aMapUnit)); |
335 | 0 | aSz.Width = aTmp.Width(); |
336 | 0 | aSz.Height = aTmp.Height(); |
337 | 0 | xObj->setVisualAreaSize( nAspect, aSz ); |
338 | | |
339 | | // re-convert aSize to 1/100th mm to avoid rounding errors in comparison below |
340 | 0 | aSize = OutputDevice::LogicToLogic( aTmp, |
341 | 0 | MapMode( aMapUnit ), aMap100 ); |
342 | 0 | } |
343 | 0 | else |
344 | 0 | aSize = OutputDevice::LogicToLogic( aSize, |
345 | 0 | MapMode( aMapUnit ), aMap100 ); |
346 | 0 | } |
347 | | |
348 | | // initialize chart ? |
349 | 0 | if ( SvtModuleOptions().IsChartInstalled() && SotExchange::IsChart( SvGlobalName( xObj->getClassID() ) ) ) |
350 | 0 | lcl_ChartInit(xObj, &rViewSh.GetViewData(), OUString(), false); |
351 | |
|
352 | 0 | ScViewData& rData = rViewSh.GetViewData(); |
353 | |
|
354 | 0 | Point aPnt = rViewSh.GetInsertPos(); |
355 | 0 | if ( rData.GetDocument().IsNegativePage( rData.CurrentTabForData() ) ) |
356 | 0 | aPnt.AdjustX( -(aSize.Width()) ); // move position to left edge |
357 | 0 | tools::Rectangle aRect (aPnt, aSize); |
358 | 0 | rtl::Reference<SdrOle2Obj> pObj = new SdrOle2Obj( |
359 | 0 | rDoc, |
360 | 0 | aObjRef, |
361 | 0 | aName, |
362 | 0 | aRect); |
363 | 0 | SdrPageView* pPV = pView->GetSdrPageView(); |
364 | 0 | bool bSuccess = pView->InsertObjectAtView(pObj.get(), *pPV); |
365 | |
|
366 | 0 | if (bSuccess && nAspect != embed::Aspects::MSOLE_ICON) |
367 | 0 | { |
368 | | // Math objects change their object size during InsertObject. |
369 | | // New size must be set in SdrObject, or a wrong scale will be set at |
370 | | // ActivateObject. |
371 | |
|
372 | 0 | try |
373 | 0 | { |
374 | 0 | awt::Size aSz = xObj->getVisualAreaSize( nAspect ); |
375 | |
|
376 | 0 | Size aNewSize( aSz.Width, aSz.Height ); |
377 | 0 | aNewSize = OutputDevice::LogicToLogic(aNewSize, MapMode(aMapUnit), MapMode(MapUnit::Map100thMM)); |
378 | |
|
379 | 0 | if ( aNewSize != aSize ) |
380 | 0 | { |
381 | 0 | aRect.SetSize( aNewSize ); |
382 | 0 | pObj->SetLogicRect( aRect ); |
383 | 0 | } |
384 | 0 | } |
385 | 0 | catch( embed::NoVisualAreaSizeException& ) |
386 | 0 | {} |
387 | 0 | } |
388 | |
|
389 | 0 | if ( !rReq.IsAPI() ) |
390 | 0 | { |
391 | | // XXX Activate from macro is deadly !!! ??? |
392 | 0 | if (bIsFromFile) |
393 | 0 | { |
394 | | // Object selected, activate Draw-Shell |
395 | 0 | rViewShell.SetDrawShell( true ); |
396 | 0 | } |
397 | 0 | else if (bSuccess) |
398 | 0 | { |
399 | 0 | rViewShell.ActivateObject(pObj.get(), embed::EmbedVerbs::MS_OLEVERB_SHOW); |
400 | 0 | } |
401 | 0 | } |
402 | |
|
403 | 0 | rReq.Done(); |
404 | 0 | } |
405 | 0 | catch( uno::Exception& ) |
406 | 0 | { |
407 | 0 | OSL_FAIL( "May need error handling here!" ); |
408 | 0 | } |
409 | 0 | } |
410 | 0 | else |
411 | 0 | rReq.Ignore(); |
412 | 0 | } |
413 | | |
414 | | FuInsertChart::FuInsertChart(ScTabViewShell& rViewSh, vcl::Window* pWin, ScDrawView* pViewP, |
415 | | SdrModel& rDoc, SfxRequest& rReq, const Link<css::ui::dialogs::DialogClosedEvent*, void>& rLink) |
416 | 0 | : FuPoor(rViewSh, pWin, pViewP, rDoc, rReq) |
417 | 0 | { |
418 | 0 | const SfxItemSet* pReqArgs = rReq.GetArgs(); |
419 | |
|
420 | 0 | if( ! rReq.IsAPI() ) |
421 | 0 | rReq.Done(); |
422 | |
|
423 | 0 | if (!SvtModuleOptions().IsChartInstalled()) |
424 | 0 | return; |
425 | | |
426 | | // BM/IHA -- |
427 | | |
428 | | // get range |
429 | 0 | OUString aRangeString; |
430 | 0 | bool bRangeIsPivotTable = false; |
431 | 0 | ScRange aPositionRange; // cell range for chart positioning |
432 | 0 | ScMarkData aMark = rViewSh.GetViewData().GetMarkData(); |
433 | 0 | if( pReqArgs ) |
434 | 0 | { |
435 | 0 | const SfxPoolItem* pItem; |
436 | 0 | if( pReqArgs->HasItem( FN_PARAM_5, &pItem ) ) |
437 | 0 | aRangeString = static_cast<const SfxStringItem*>(pItem)->GetValue(); |
438 | |
|
439 | 0 | aPositionRange = rViewSh.GetViewData().GetCurPos(); |
440 | 0 | } |
441 | 0 | else |
442 | 0 | { |
443 | 0 | ScDocument& rDocument = rViewSh.GetViewData().GetDocument(); |
444 | 0 | ScDPObject* pObject = rDocument.GetDPAtCursor(rViewSh.GetViewData().GetCurX(), |
445 | 0 | rViewSh.GetViewData().GetCurY(), |
446 | 0 | rViewSh.GetViewData().CurrentTabForData()); |
447 | 0 | if (pObject) |
448 | 0 | { |
449 | 0 | aRangeString = pObject->GetName(); |
450 | 0 | bRangeIsPivotTable = true; |
451 | 0 | } |
452 | 0 | else |
453 | 0 | { |
454 | 0 | ScRangeList aRanges; |
455 | 0 | if (aMark.IsMarked() || aMark.IsMultiMarked()) |
456 | 0 | { |
457 | 0 | ScMarkData aMultiMark(std::move(aMark)); |
458 | 0 | aMultiMark.MarkToMulti(); |
459 | 0 | aMultiMark.FillRangeListWithMarks(&aRanges, false); |
460 | 0 | } |
461 | 0 | else |
462 | 0 | { |
463 | 0 | ScViewData& rViewData = rViewSh.GetViewData(); |
464 | 0 | SCTAB nTab = rViewData.CurrentTabForData(); |
465 | 0 | SCCOL nStartCol = rViewData.GetCurX(); |
466 | 0 | SCROW nStartRow = rViewData.GetCurY(); |
467 | 0 | SCCOL nEndCol = nStartCol; |
468 | 0 | SCROW nEndRow = nStartRow; |
469 | 0 | rViewData.GetDocument().GetDataArea(nTab, nStartCol, nStartRow, nEndCol, nEndRow, |
470 | 0 | true, false); |
471 | 0 | aRanges = ScRange(nStartCol, nStartRow, nTab, nEndCol, nEndRow, nTab); |
472 | 0 | } |
473 | 0 | OUString aStr; |
474 | 0 | aRanges.Format( aStr, ScRefFlags::RANGE_ABS_3D, rDocument, rDocument.GetAddressConvention() ); |
475 | 0 | aRangeString = aStr; |
476 | | |
477 | | // get "total" range for positioning |
478 | 0 | if ( !aRanges.empty() ) |
479 | 0 | { |
480 | 0 | aPositionRange = aRanges[ 0 ]; |
481 | 0 | for ( size_t i = 1, nCount = aRanges.size(); i < nCount; ++i ) |
482 | 0 | { |
483 | 0 | aPositionRange.ExtendTo( aRanges[ i ] ); |
484 | 0 | } |
485 | 0 | } |
486 | 0 | } |
487 | 0 | } |
488 | | |
489 | | // adapted old code |
490 | 0 | pView->UnmarkAll(); |
491 | |
|
492 | 0 | OUString aName; |
493 | 0 | const sal_Int64 nAspect = embed::Aspects::MSOLE_CONTENT; |
494 | |
|
495 | 0 | uno::Reference < embed::XEmbeddedObject > xObj = |
496 | 0 | rViewShell.GetObjectShell()->GetEmbeddedObjectContainer().CreateEmbeddedObject( SvGlobalName( SO3_SCH_CLASSID_60 ).GetByteSequence(), aName ); |
497 | |
|
498 | 0 | uno::Reference< css::chart2::data::XDataReceiver > xReceiver; |
499 | 0 | if( xObj.is()) |
500 | 0 | xReceiver.set( xObj->getComponent(), uno::UNO_QUERY ); |
501 | |
|
502 | 0 | uno::Reference<chart2::XChartDocument> xChartDoc(xReceiver, uno::UNO_QUERY); |
503 | 0 | if (xChartDoc.is()) |
504 | 0 | xChartDoc->createDefaultChart(); |
505 | | |
506 | | // lock the model to suppress any internal updates |
507 | 0 | uno::Reference< frame::XModel > xChartModel( xReceiver, uno::UNO_QUERY ); |
508 | 0 | if( xChartModel.is() ) |
509 | 0 | xChartModel->lockControllers(); |
510 | | |
511 | | // object size |
512 | 0 | Size aSize; |
513 | 0 | if (xObj.is()) |
514 | 0 | { |
515 | 0 | awt::Size aSz = xObj->getVisualAreaSize( nAspect ); |
516 | 0 | aSize = Size( aSz.Width, aSz.Height ); |
517 | |
|
518 | 0 | bool bSizeCh = false; |
519 | 0 | if (aSize.IsEmpty()) |
520 | 0 | { |
521 | 0 | aSize.setWidth( 5000 ); |
522 | 0 | aSize.setHeight( 5000 ); |
523 | 0 | bSizeCh = true; |
524 | 0 | } |
525 | 0 | if (bSizeCh) |
526 | 0 | { |
527 | 0 | MapUnit aMapUnit = VCLUnoHelper::UnoEmbed2VCLMapUnit( xObj->getMapUnit( nAspect ) ); |
528 | 0 | aSize = OutputDevice::LogicToLogic( aSize, MapMode( MapUnit::Map100thMM ), MapMode( aMapUnit ) ); |
529 | 0 | aSz.Width = aSize.Width(); |
530 | 0 | aSz.Height = aSize.Height(); |
531 | 0 | xObj->setVisualAreaSize( nAspect, aSz ); |
532 | 0 | } |
533 | 0 | } |
534 | |
|
535 | 0 | ScViewData& rData = rViewSh.GetViewData(); |
536 | 0 | ScDocShell* pScDocSh = rData.GetDocShell(); |
537 | 0 | ScDocument& rScDoc = pScDocSh->GetDocument(); |
538 | 0 | bool bUndo (rScDoc.IsUndoEnabled()); |
539 | |
|
540 | 0 | if( pReqArgs ) |
541 | 0 | { |
542 | 0 | const SfxPoolItem* pItem; |
543 | 0 | sal_uInt16 nToTable = 0; |
544 | |
|
545 | 0 | if( pReqArgs->HasItem( FN_PARAM_4, &pItem ) ) |
546 | 0 | { |
547 | 0 | if ( auto pUInt16Item = dynamic_cast<const SfxUInt16Item*>( pItem) ) |
548 | 0 | nToTable = pUInt16Item->GetValue(); |
549 | 0 | else if ( auto pBoolItem = dynamic_cast<const SfxBoolItem*>( pItem) ) |
550 | 0 | { |
551 | | // In IDL for Basic FN_PARAM_4 means SfxBoolItem |
552 | | // -> if set new table, else current table |
553 | |
|
554 | 0 | if ( pBoolItem->GetValue() ) |
555 | 0 | nToTable = static_cast<sal_uInt16>(rScDoc.GetTableCount()); |
556 | 0 | else |
557 | 0 | nToTable = static_cast<sal_uInt16>(rData.CurrentTabForData()); |
558 | 0 | } |
559 | 0 | } |
560 | 0 | else |
561 | 0 | { |
562 | 0 | rReq.AppendItem( SfxUInt16Item( FN_PARAM_4, nToTable ) ); |
563 | 0 | } |
564 | | |
565 | | // Output on new table? |
566 | 0 | if ( nToTable == rScDoc.GetTableCount() ) |
567 | 0 | { |
568 | | // Let's go... |
569 | 0 | OUString aTabName; |
570 | 0 | SCTAB nNewTab = rScDoc.GetTableCount(); |
571 | |
|
572 | 0 | rScDoc.CreateValidTabName( aTabName ); |
573 | |
|
574 | 0 | if ( rScDoc.InsertTab( nNewTab, aTabName ) ) |
575 | 0 | { |
576 | 0 | if (bUndo) |
577 | 0 | { |
578 | 0 | pScDocSh->GetUndoManager()->AddUndoAction( |
579 | 0 | std::make_unique<ScUndoInsertTab>( *pScDocSh, nNewTab, |
580 | 0 | true/*bAppend*/, aTabName ) ); |
581 | 0 | } |
582 | |
|
583 | 0 | pScDocSh->Broadcast( ScTablesHint( SC_TAB_INSERTED, nNewTab ) ); |
584 | 0 | rViewSh.SetTabNo( nNewTab, true ); |
585 | 0 | pScDocSh->PostPaintExtras(); //! done afterwards ??? |
586 | 0 | } |
587 | 0 | else |
588 | 0 | { |
589 | 0 | OSL_FAIL( "Could not create new table :-/" ); |
590 | 0 | } |
591 | 0 | } |
592 | 0 | else if ( nToTable != rData.CurrentTabForData() ) |
593 | 0 | { |
594 | 0 | rViewSh.SetTabNo( nToTable, true ); |
595 | 0 | } |
596 | 0 | } |
597 | |
|
598 | 0 | lcl_ChartInit(xObj, &rData, aRangeString, bRangeIsPivotTable); // set source range, auto-detect column/row headers |
599 | | |
600 | | // object position |
601 | | |
602 | | // get chart position (from window size and data range) |
603 | 0 | Point aStart = rViewSh.GetChartInsertPos( aSize, aPositionRange ); |
604 | |
|
605 | 0 | tools::Rectangle aRect (aStart, aSize); |
606 | 0 | rtl::Reference<SdrOle2Obj> pObj = new SdrOle2Obj( |
607 | 0 | rDoc, |
608 | 0 | svt::EmbeddedObjectRef(xObj, nAspect), |
609 | 0 | aName, |
610 | 0 | aRect); |
611 | 0 | SdrPageView* pPV = pView->GetSdrPageView(); |
612 | | |
613 | | // #i121334# This call will change the chart's default background fill from white to transparent. |
614 | | // Add here again if this is wanted (see task description for details) |
615 | | // ChartHelper::AdaptDefaultsForChart( xObj ); |
616 | | |
617 | | // pView->InsertObjectAtView(pObj, *pPV);//this call leads to an immediate redraw and asks the chart for a visual representation |
618 | | |
619 | | // use the page instead of the view to insert, so no undo action is created yet |
620 | 0 | SdrPage* pPage = pPV->GetPage(); |
621 | 0 | pPage->InsertObject( pObj.get() ); |
622 | 0 | pView->UnmarkAllObj(); |
623 | 0 | pView->MarkObj( pObj.get(), pPV ); |
624 | |
|
625 | 0 | if (rReq.IsAPI()) |
626 | 0 | { |
627 | 0 | if( xChartModel.is() ) |
628 | 0 | xChartModel->unlockControllers(); |
629 | 0 | } |
630 | 0 | else if (!rViewSh.isLOKMobilePhone()) |
631 | 0 | { |
632 | | //the controller will be unlocked by the dialog when the dialog is told to do so |
633 | | |
634 | | // only activate object if not called via API (e.g. macro) |
635 | 0 | if (!comphelper::LibreOfficeKit::isActive()) |
636 | 0 | rViewShell.ActivateObject(pObj.get(), embed::EmbedVerbs::MS_OLEVERB_SHOW); |
637 | | |
638 | | //open wizard |
639 | 0 | uno::Reference< uno::XComponentContext > xContext(::comphelper::getProcessComponentContext()); |
640 | 0 | if(xContext.is()) |
641 | 0 | { |
642 | 0 | uno::Reference< lang::XMultiComponentFactory > xMCF( xContext->getServiceManager() ); |
643 | 0 | if(xMCF.is()) |
644 | 0 | { |
645 | 0 | css::uno::Reference<css::ui::dialogs::XAsynchronousExecutableDialog> xDialog( |
646 | 0 | xMCF->createInstanceWithContext( |
647 | 0 | u"com.sun.star.comp.chart2.WizardDialog"_ustr |
648 | 0 | , xContext), uno::UNO_QUERY); |
649 | 0 | uno::Reference< lang::XInitialization > xInit( xDialog, uno::UNO_QUERY ); |
650 | 0 | if( xChartModel.is() && xInit.is() ) |
651 | 0 | { |
652 | 0 | css::uno::Reference< css::awt::XWindow > xParent |
653 | 0 | = new weld::TransportAsXWindow(pWin->GetFrameWeld()); |
654 | 0 | uno::Sequence<uno::Any> aSeq(comphelper::InitAnyPropertySequence( |
655 | 0 | { |
656 | 0 | {"ParentWindow", uno::Any(xParent)}, |
657 | 0 | {"ChartModel", uno::Any(xChartModel)} |
658 | 0 | })); |
659 | 0 | xInit->initialize( aSeq ); |
660 | | |
661 | | // try to set the dialog's position so it doesn't hide the chart |
662 | 0 | uno::Reference < beans::XPropertySet > xDialogProps( xDialog, uno::UNO_QUERY ); |
663 | 0 | if ( xDialogProps.is() ) |
664 | 0 | { |
665 | 0 | try |
666 | 0 | { |
667 | | //get dialog size: |
668 | 0 | awt::Size aDialogAWTSize; |
669 | 0 | if( xDialogProps->getPropertyValue(u"Size"_ustr) |
670 | 0 | >>= aDialogAWTSize ) |
671 | 0 | { |
672 | 0 | Size aDialogSize( aDialogAWTSize.Width, aDialogAWTSize.Height ); |
673 | 0 | if ( !aDialogSize.IsEmpty() ) |
674 | 0 | { |
675 | | //calculate and set new position |
676 | 0 | Point aDialogPos = rViewShell.GetChartDialogPos( aDialogSize, aRect ); |
677 | 0 | xDialogProps->setPropertyValue(u"Position"_ustr, |
678 | 0 | uno::Any( awt::Point(aDialogPos.getX(),aDialogPos.getY()) ) ); |
679 | 0 | } |
680 | 0 | } |
681 | | //tell the dialog to unlock controller |
682 | 0 | xDialogProps->setPropertyValue(u"UnlockControllersOnExecute"_ustr, |
683 | 0 | uno::Any( true ) ); |
684 | |
|
685 | 0 | } |
686 | 0 | catch( uno::Exception& ) |
687 | 0 | { |
688 | 0 | OSL_FAIL( "Chart wizard couldn't be positioned automatically" ); |
689 | 0 | } |
690 | 0 | } |
691 | |
|
692 | 0 | pView->AddUndo(std::make_unique<SdrUndoNewObj>(*pObj)); |
693 | 0 | rViewSh.SetInsertWizardUndoMark(); |
694 | 0 | rtl::Reference<::svt::DialogClosedListener> pListener = new ::svt::DialogClosedListener(); |
695 | 0 | pListener->SetDialogClosedLink( rLink ); |
696 | |
|
697 | 0 | xDialog->startExecuteModal( pListener ); |
698 | 0 | } |
699 | 0 | else |
700 | 0 | { |
701 | 0 | uno::Reference< lang::XComponent > xComponent( xDialog, uno::UNO_QUERY ); |
702 | 0 | if( xComponent.is()) |
703 | 0 | xComponent->dispose(); |
704 | 0 | } |
705 | 0 | } |
706 | 0 | } |
707 | 0 | } |
708 | 0 | } |
709 | | |
710 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |