Coverage Report

Created: 2025-12-08 09:28

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