summaryrefslogtreecommitdiffstats
path: root/sw/source/uibase/table/tablemgr.cxx
blob: ecf9b6deafc6abafac19ce95ae82bdc545fe7725 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <com/sun/star/chart/ChartDataRowSource.hpp>
#include <com/sun/star/chart2/data/XDataProvider.hpp>
#include <com/sun/star/chart2/data/XDataReceiver.hpp>
#include <com/sun/star/chart2/XChartDocument.hpp>
#include <com/sun/star/beans/PropertyState.hpp>
#include <com/sun/star/embed/EmbedVerbs.hpp>
#include <com/sun/star/embed/XComponentSupplier.hpp>
#include <com/sun/star/embed/XEmbeddedObject.hpp>

#include <comphelper/classids.hxx>
#include <svx/charthelper.hxx>
#include <svtools/embedhlp.hxx>

#include <edtwin.hxx>
#include <wrtsh.hxx>
#include <view.hxx>
#include <swundo.hxx>
#include <tablemgr.hxx>
#include <frmfmt.hxx>
#include <swabstdlg.hxx>
#include <swcli.hxx>
#include <docsh.hxx>
#include <unotbl.hxx>
#include <unochart.hxx>

#include <comphelper/lok.hxx>

using namespace ::com::sun::star;

// Adjust line height (dialogue)
void SwTableFUNC::ColWidthDlg(weld::Window *pParent)
{
    InitTabCols();
    SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create();
    ScopedVclPtr<VclAbstractDialog> pDlg(pFact->CreateSwTableWidthDlg(pParent, *this));
    pDlg->Execute();
}

// Determine the width
SwTwips SwTableFUNC::GetColWidth(sal_uInt16 nNum) const
{
    SwTwips nWidth = 0;

    if( aCols.Count() > 0 )
    {
        if(aCols.Count() == GetColCount())
        {
            if(nNum == aCols.Count())
                nWidth = aCols.GetRight() - aCols[nNum-1];
            else
            {
                if(nNum == 0)
                    nWidth = aCols[nNum] - aCols.GetLeft();
                else
                    nWidth = aCols[nNum] - aCols[nNum-1];
            }
        }
        else
        {
            SwTwips nRValid = nNum < GetColCount() ?
                              aCols[GetRightSeparator(nNum)] :
                              aCols.GetRight();
            SwTwips nLValid = nNum ?
                              aCols[GetRightSeparator(nNum - 1)] :
                              aCols.GetLeft();
            nWidth = nRValid - nLValid;
        }
    }
    else
        nWidth = aCols.GetRight();
    return nWidth;
}

SwTwips SwTableFUNC::GetMaxColWidth( sal_uInt16 nNum ) const
{
    OSL_ENSURE(nNum <= aCols.Count(), "Index out of Area");

    if ( GetColCount() > 0 )
    {
        // The maximum width arises from the own width and
        // the width each of the neighbor cells reduced by MINLAY.
        SwTwips nMax;
        if(nNum == 0)
            nMax = GetColWidth(1) - MINLAY;
        else
        {
            nMax = GetColWidth(nNum-1);
            if(nNum == GetColCount())
                nMax -= MINLAY;
            else
                nMax += GetColWidth(nNum+1) - 2 * MINLAY;
        }
        return nMax + GetColWidth(nNum);
    }
    else
        return GetColWidth(nNum);
}

void SwTableFUNC::SetColWidth(sal_uInt16 nNum, SwTwips nNewWidth )
{
    // set current width
    // move all of the following
    bool bCurrentOnly = false;

    if ( aCols.Count() > 0 )
    {
        if(aCols.Count() != GetColCount())
            bCurrentOnly = true;
        SwTwips nWidth = GetColWidth(nNum);

        int nDiff = static_cast<int>(nNewWidth - nWidth);
        if( !nNum )
            aCols[ GetRightSeparator(0) ] += nDiff;
        else if( nNum < GetColCount()  )
        {
            if(nDiff < GetColWidth(nNum + 1) - MINLAY)
                aCols[ GetRightSeparator(nNum) ] += nDiff;
            else
            {
                int nDiffLeft = nDiff - static_cast<int>(GetColWidth(nNum + 1)) + int(MINLAY);
                aCols[ GetRightSeparator(nNum) ] += (nDiff - nDiffLeft);
                aCols[ GetRightSeparator(nNum - 1) ] -= nDiffLeft;
            }
        }
        else
            aCols[ GetRightSeparator(nNum-1) ] -= nDiff;
    }
    else
        aCols.SetRight( std::min( nNewWidth, aCols.GetRightMax()) );

    pSh->StartAllAction();
    pSh->SetTabCols( aCols, bCurrentOnly );
    pSh->EndAllAction();
}

void SwTableFUNC::InitTabCols()
{
    OSL_ENSURE(pSh, "no Shell");

    if( pFormat && pSh)
        pSh->GetTabCols( aCols );
}

SwTableFUNC::SwTableFUNC(SwWrtShell *pShell)
    : pFormat(pShell->GetTableFormat()),
      pSh(pShell)
{
}

SwTableFUNC::~SwTableFUNC()
{
}

void SwTableFUNC::UpdateChart()
{
    //Update of the fields triggered by the user, all Charts of
    //the table will be brought up to date
    SwFrameFormat *pFormat2 = pSh->GetTableFormat();
    if ( pFormat2 && pSh->HasOLEObj( pFormat2->GetName() ) )
    {
        pSh->StartAllAction();
        pSh->UpdateCharts( pFormat2->GetName() );
        pSh->EndAllAction();
    }
}

uno::Reference< frame::XModel > SwTableFUNC::InsertChart(
        uno::Reference< chart2::data::XDataProvider > const &rxDataProvider,
        bool bFillWithData,
        const OUString &rCellRange,
        SwFlyFrameFormat** ppFlyFrameFormat )
{
    uno::Reference< frame::XModel > xChartModel;
    pSh->StartUndo( SwUndoId::UI_INSERT_CHART );
    pSh->StartAllAction();

    OUString aName;
    if (pSh->IsCursorInTable())
    {
        aName = pSh->GetTableFormat()->GetName();
        // insert node before table
        pSh->MoveTable( GotoCurrTable, fnTableStart );
        pSh->Up( false );
        if ( pSh->IsCursorInTable() )
        {
            if ( aName != pSh->GetTableFormat()->GetName() )
                pSh->Down( false ); // two adjacent tables
        }
        pSh->SplitNode();
    }

    // insert chart
    OUString aObjName;
    comphelper::EmbeddedObjectContainer aCnt;
    uno::Reference < embed::XEmbeddedObject > xObj =
        aCnt.CreateEmbeddedObject( SvGlobalName( SO3_SCH_CLASSID ).GetByteSequence(), aObjName );

    ::svt::EmbeddedObjectRef aEmbObjRef( xObj, css::embed::Aspects::MSOLE_CONTENT );
    if ( xObj.is() )
    {

        SwFlyFrameFormat* pTmp = nullptr;
        pSh->InsertOleObject( aEmbObjRef, &pTmp );
        if (ppFlyFrameFormat)
            *ppFlyFrameFormat = pTmp;

        xChartModel.set( xObj->getComponent(), uno::UNO_QUERY );
        if( xChartModel.is() )
        {
            // Create a default chart type.
            uno::Reference<chart2::XChartDocument> xChartDoc(xChartModel, uno::UNO_QUERY);
            if (xChartDoc.is())
                xChartDoc->createDefaultChart();

            xChartModel->lockControllers(); //#i79578# don't request a new replacement image for charts to often - block change notifications
        }

        // set the table name at the OLE-node
        if (!aName.isEmpty())
            pSh->SetChartName( aName );
    }
    pSh->EndAllAction();

    if (xObj.is() && !comphelper::LibreOfficeKit::isActive())
    {
        // Let the chart be activated after the inserting (unless
        // via LibreOfficeKit)
        SfxInPlaceClient* pClient = pSh->GetView().FindIPClient( xObj, &pSh->GetView().GetEditWin() );
        if ( !pClient )
        {
            pClient = new SwOleClient( &pSh->GetView(), &pSh->GetView().GetEditWin(), aEmbObjRef );
            pSh->SetCheckForOLEInCaption( true );
        }
        pSh->CalcAndSetScale( aEmbObjRef );
        //#50270# We don't need to handle errors,
        //this does the DoVerb in the SfxViewShell.
        ErrCode nErr = pClient->DoVerb(embed::EmbedVerbs::MS_OLEVERB_SHOW);
        (void) nErr;

        // #i121334#
        ChartHelper::AdaptDefaultsForChart( xObj );
    }

    uno::Reference< chart2::data::XDataReceiver > xDataReceiver( xChartModel, uno::UNO_QUERY );
    if (bFillWithData && xDataReceiver.is() && rxDataProvider.is())
    {
        xDataReceiver->attachDataProvider( rxDataProvider );

        uno::Reference< util::XNumberFormatsSupplier > xNumberFormatsSupplier( pSh->GetView().GetDocShell()->GetModel(), uno::UNO_QUERY );
        xDataReceiver->attachNumberFormatsSupplier( xNumberFormatsSupplier );

        // default values for ranges that do not consist of a single row or column
        bool bHasCategories = true;
        bool bFirstCellAsLabel = true;
        chart::ChartDataRowSource eDataRowSource = chart::ChartDataRowSource_COLUMNS;

        SwRangeDescriptor aDesc;
        FillRangeDescriptor( aDesc, rCellRange );
        bool bSingleRowCol = aDesc.nTop == aDesc.nBottom || aDesc.nLeft == aDesc.nRight;
        if (bSingleRowCol)
        {
            aDesc.Normalize();
            sal_Int32 nRowLen = aDesc.nRight  - aDesc.nLeft + 1;
            sal_Int32 nColLen = aDesc.nBottom - aDesc.nTop + 1;

            bHasCategories = false;
            if (nRowLen == 1 && nColLen == 1)
                bFirstCellAsLabel   = false;
            else if (nRowLen > 1)
                eDataRowSource = chart::ChartDataRowSource_ROWS;
            else if (nColLen > 1)
                eDataRowSource = chart::ChartDataRowSource_COLUMNS;
            else {
                OSL_FAIL("unexpected state" );
            }
        }

        uno::Sequence< beans::PropertyValue > aArgs( 4 );
        aArgs[0] = beans::PropertyValue(
            "CellRangeRepresentation", -1,
            uno::makeAny( rCellRange ), beans::PropertyState_DIRECT_VALUE );
        aArgs[1] = beans::PropertyValue(
            "HasCategories", -1,
            uno::makeAny( bHasCategories ), beans::PropertyState_DIRECT_VALUE );
        aArgs[2] = beans::PropertyValue(
            "FirstCellAsLabel", -1,
            uno::makeAny( bFirstCellAsLabel ), beans::PropertyState_DIRECT_VALUE );
        aArgs[3] = beans::PropertyValue(
            "DataRowSource", -1,
            uno::makeAny( eDataRowSource ), beans::PropertyState_DIRECT_VALUE );
        xDataReceiver->setArguments( aArgs );
    }

    pSh->EndUndo( SwUndoId::UI_INSERT_CHART );

    if( xChartModel.is() )
        xChartModel->unlockControllers(); //#i79578# don't request a new replacement image for charts to often
    return xChartModel;
}

sal_uInt16  SwTableFUNC::GetCurColNum() const
{
    const size_t nPos = pSh->GetCurTabColNum();
    size_t nCount = 0;
    for( size_t i = 0; i < nPos; i++ )
        if(aCols.IsHidden(i))
            nCount ++;
    return nPos - nCount;
}

sal_uInt16  SwTableFUNC::GetColCount() const
{
    size_t nCount = 0;
    for(size_t i = 0; i < aCols.Count(); i++ )
        if(aCols.IsHidden(i))
            nCount ++;
    return aCols.Count() - nCount;
}

int SwTableFUNC::GetRightSeparator(int nNum) const
{
    OSL_ENSURE( nNum < static_cast<int>(GetColCount()) ,"Index out of range");
    int i = 0;
    while( nNum >= 0 )
    {
        if( !aCols.IsHidden(i) )
            nNum--;
        i++;
    }
    return i - 1;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */