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/chart/ChartDataRowSource.hpp>
21 #include <com/sun/star/chart2/data/XDataProvider.hpp>
22 #include <com/sun/star/chart2/data/XDataReceiver.hpp>
23 #include <com/sun/star/chart2/XChartDocument.hpp>
24 #include <com/sun/star/beans/PropertyState.hpp>
25 #include <com/sun/star/embed/EmbedVerbs.hpp>
26 #include <com/sun/star/embed/XEmbeddedObject.hpp>
27
28 #include <comphelper/classids.hxx>
29 #include <svx/charthelper.hxx>
30 #include <svtools/embedhlp.hxx>
31
32 #include <edtwin.hxx>
33 #include <wrtsh.hxx>
34 #include <view.hxx>
35 #include <swundo.hxx>
36 #include <tablemgr.hxx>
37 #include <frmfmt.hxx>
38 #include <swabstdlg.hxx>
39 #include <swcli.hxx>
40 #include <docsh.hxx>
41 #include <unotbl.hxx>
42 #include <unochart.hxx>
43
44 #include <comphelper/lok.hxx>
45
46 using namespace ::com::sun::star;
47
48 // Adjust line height (dialogue)
ColWidthDlg(weld::Window * pParent)49 void SwTableFUNC::ColWidthDlg(weld::Window *pParent)
50 {
51 InitTabCols();
52 SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create();
53 ScopedVclPtr<VclAbstractDialog> pDlg(pFact->CreateSwTableWidthDlg(pParent, *this));
54 pDlg->Execute();
55 }
56
57 // Determine the width
GetColWidth(sal_uInt16 nNum) const58 SwTwips SwTableFUNC::GetColWidth(sal_uInt16 nNum) const
59 {
60 SwTwips nWidth = 0;
61
62 if( aCols.Count() > 0 )
63 {
64 if(aCols.Count() == GetColCount())
65 {
66 if(nNum == aCols.Count())
67 nWidth = aCols.GetRight() - aCols[nNum-1];
68 else
69 {
70 if(nNum == 0)
71 nWidth = aCols[nNum] - aCols.GetLeft();
72 else
73 nWidth = aCols[nNum] - aCols[nNum-1];
74 }
75 }
76 else
77 {
78 SwTwips nRValid = nNum < GetColCount() ?
79 aCols[GetRightSeparator(nNum)] :
80 aCols.GetRight();
81 SwTwips nLValid = nNum ?
82 aCols[GetRightSeparator(nNum - 1)] :
83 aCols.GetLeft();
84 nWidth = nRValid - nLValid;
85 }
86 }
87 else
88 nWidth = aCols.GetRight();
89 return nWidth;
90 }
91
GetMaxColWidth(sal_uInt16 nNum) const92 SwTwips SwTableFUNC::GetMaxColWidth( sal_uInt16 nNum ) const
93 {
94 OSL_ENSURE(nNum <= aCols.Count(), "Index out of Area");
95
96 if ( GetColCount() > 0 )
97 {
98 // The maximum width arises from the own width and
99 // the width each of the neighbor cells reduced by MINLAY.
100 SwTwips nMax;
101 if(nNum == 0)
102 nMax = GetColWidth(1) - MINLAY;
103 else
104 {
105 nMax = GetColWidth(nNum-1);
106 if(nNum == GetColCount())
107 nMax -= MINLAY;
108 else
109 nMax += GetColWidth(nNum+1) - 2 * MINLAY;
110 }
111 return nMax + GetColWidth(nNum);
112 }
113 else
114 return GetColWidth(nNum);
115 }
116
SetColWidth(sal_uInt16 nNum,SwTwips nNewWidth)117 void SwTableFUNC::SetColWidth(sal_uInt16 nNum, SwTwips nNewWidth )
118 {
119 // set current width
120 // move all of the following
121 bool bCurrentOnly = false;
122
123 if ( aCols.Count() > 0 )
124 {
125 if(aCols.Count() != GetColCount())
126 bCurrentOnly = true;
127 SwTwips nWidth = GetColWidth(nNum);
128
129 int nDiff = static_cast<int>(nNewWidth - nWidth);
130 if( !nNum )
131 aCols[ GetRightSeparator(0) ] += nDiff;
132 else if( nNum < GetColCount() )
133 {
134 if(nDiff < GetColWidth(nNum + 1) - MINLAY)
135 aCols[ GetRightSeparator(nNum) ] += nDiff;
136 else
137 {
138 int nDiffLeft = nDiff - static_cast<int>(GetColWidth(nNum + 1)) + int(MINLAY);
139 aCols[ GetRightSeparator(nNum) ] += (nDiff - nDiffLeft);
140 aCols[ GetRightSeparator(nNum - 1) ] -= nDiffLeft;
141 }
142 }
143 else
144 aCols[ GetRightSeparator(nNum-1) ] -= nDiff;
145 }
146 else
147 aCols.SetRight( std::min( nNewWidth, SwTwips(aCols.GetRightMax()) ) );
148
149 pSh->StartAllAction();
150 pSh->SetTabCols( aCols, bCurrentOnly );
151 pSh->EndAllAction();
152 }
153
InitTabCols()154 void SwTableFUNC::InitTabCols()
155 {
156 OSL_ENSURE(pSh, "no Shell");
157
158 if( pFormat && pSh)
159 pSh->GetTabCols( aCols );
160 }
161
SwTableFUNC(SwWrtShell * pShell)162 SwTableFUNC::SwTableFUNC(SwWrtShell *pShell)
163 : pFormat(pShell->GetTableFormat()),
164 pSh(pShell)
165 {
166 }
167
~SwTableFUNC()168 SwTableFUNC::~SwTableFUNC()
169 {
170 }
171
UpdateChart()172 void SwTableFUNC::UpdateChart()
173 {
174 //Update of the fields triggered by the user, all Charts of
175 //the table will be brought up to date
176 SwFrameFormat *pFormat2 = pSh->GetTableFormat();
177 if ( pFormat2 && pSh->HasOLEObj( pFormat2->GetName() ) )
178 {
179 pSh->StartAllAction();
180 pSh->UpdateCharts( pFormat2->GetName() );
181 pSh->EndAllAction();
182 }
183 }
184
InsertChart(uno::Reference<chart2::data::XDataProvider> const & rxDataProvider,bool bFillWithData,const OUString & rCellRange,SwFlyFrameFormat ** ppFlyFrameFormat)185 uno::Reference< frame::XModel > SwTableFUNC::InsertChart(
186 uno::Reference< chart2::data::XDataProvider > const &rxDataProvider,
187 bool bFillWithData,
188 const OUString &rCellRange,
189 SwFlyFrameFormat** ppFlyFrameFormat )
190 {
191 uno::Reference< frame::XModel > xChartModel;
192 pSh->StartUndo( SwUndoId::UI_INSERT_CHART );
193 pSh->StartAllAction();
194
195 OUString aName;
196 if (pSh->IsCursorInTable())
197 {
198 aName = pSh->GetTableFormat()->GetName();
199 // insert node before table
200 pSh->MoveTable( GotoCurrTable, fnTableStart );
201 pSh->Up( false );
202 if ( pSh->IsCursorInTable() )
203 {
204 if ( aName != pSh->GetTableFormat()->GetName() )
205 pSh->Down( false ); // two adjacent tables
206 }
207 pSh->SplitNode();
208 }
209
210 // insert chart
211 OUString aObjName;
212 comphelper::EmbeddedObjectContainer aCnt;
213 uno::Reference < embed::XEmbeddedObject > xObj =
214 aCnt.CreateEmbeddedObject( SvGlobalName( SO3_SCH_CLASSID ).GetByteSequence(), aObjName );
215
216 ::svt::EmbeddedObjectRef aEmbObjRef( xObj, css::embed::Aspects::MSOLE_CONTENT );
217 if ( xObj.is() )
218 {
219
220 SwFlyFrameFormat* pTmp = nullptr;
221 pSh->InsertOleObject( aEmbObjRef, &pTmp );
222 if (ppFlyFrameFormat)
223 *ppFlyFrameFormat = pTmp;
224
225 xChartModel.set( xObj->getComponent(), uno::UNO_QUERY );
226 if( xChartModel.is() )
227 {
228 // Create a default chart type.
229 uno::Reference<chart2::XChartDocument> xChartDoc(xChartModel, uno::UNO_QUERY);
230 if (xChartDoc.is())
231 xChartDoc->createDefaultChart();
232
233 xChartModel->lockControllers(); //#i79578# don't request a new replacement image for charts to often - block change notifications
234 }
235
236 // set the table name at the OLE-node
237 if (!aName.isEmpty())
238 pSh->SetChartName( aName );
239 }
240 pSh->EndAllAction();
241
242 if (xObj.is() && !comphelper::LibreOfficeKit::isActive())
243 {
244 // Let the chart be activated after the inserting (unless
245 // via LibreOfficeKit)
246 SfxInPlaceClient* pClient = pSh->GetView().FindIPClient( xObj, &pSh->GetView().GetEditWin() );
247 if ( !pClient )
248 {
249 pClient = new SwOleClient( &pSh->GetView(), &pSh->GetView().GetEditWin(), aEmbObjRef );
250 pSh->SetCheckForOLEInCaption( true );
251 }
252 pSh->CalcAndSetScale( aEmbObjRef );
253 //#50270# We don't need to handle errors,
254 //this does the DoVerb in the SfxViewShell.
255 ErrCode nErr = pClient->DoVerb(embed::EmbedVerbs::MS_OLEVERB_SHOW);
256 (void) nErr;
257
258 // #i121334#
259 ChartHelper::AdaptDefaultsForChart( xObj );
260 }
261
262 uno::Reference< chart2::data::XDataReceiver > xDataReceiver( xChartModel, uno::UNO_QUERY );
263 if (bFillWithData && xDataReceiver.is() && rxDataProvider.is())
264 {
265 xDataReceiver->attachDataProvider( rxDataProvider );
266
267 uno::Reference< util::XNumberFormatsSupplier > xNumberFormatsSupplier( pSh->GetView().GetDocShell()->GetModel(), uno::UNO_QUERY );
268 xDataReceiver->attachNumberFormatsSupplier( xNumberFormatsSupplier );
269
270 // default values for ranges that do not consist of a single row or column
271 bool bHasCategories = true;
272 bool bFirstCellAsLabel = true;
273 chart::ChartDataRowSource eDataRowSource = chart::ChartDataRowSource_COLUMNS;
274
275 SwRangeDescriptor aDesc;
276 FillRangeDescriptor( aDesc, rCellRange );
277 bool bSingleRowCol = aDesc.nTop == aDesc.nBottom || aDesc.nLeft == aDesc.nRight;
278 if (bSingleRowCol)
279 {
280 aDesc.Normalize();
281 sal_Int32 nRowLen = aDesc.nRight - aDesc.nLeft + 1;
282 sal_Int32 nColLen = aDesc.nBottom - aDesc.nTop + 1;
283
284 bHasCategories = false;
285 if (nRowLen == 1 && nColLen == 1)
286 bFirstCellAsLabel = false;
287 else if (nRowLen > 1)
288 eDataRowSource = chart::ChartDataRowSource_ROWS;
289 else if (nColLen > 1)
290 eDataRowSource = chart::ChartDataRowSource_COLUMNS;
291 else {
292 OSL_FAIL("unexpected state" );
293 }
294 }
295
296 uno::Sequence< beans::PropertyValue > aArgs( 4 );
297 aArgs[0] = beans::PropertyValue(
298 "CellRangeRepresentation", -1,
299 uno::makeAny( rCellRange ), beans::PropertyState_DIRECT_VALUE );
300 aArgs[1] = beans::PropertyValue(
301 "HasCategories", -1,
302 uno::makeAny( bHasCategories ), beans::PropertyState_DIRECT_VALUE );
303 aArgs[2] = beans::PropertyValue(
304 "FirstCellAsLabel", -1,
305 uno::makeAny( bFirstCellAsLabel ), beans::PropertyState_DIRECT_VALUE );
306 aArgs[3] = beans::PropertyValue(
307 "DataRowSource", -1,
308 uno::makeAny( eDataRowSource ), beans::PropertyState_DIRECT_VALUE );
309 xDataReceiver->setArguments( aArgs );
310 }
311
312 pSh->EndUndo( SwUndoId::UI_INSERT_CHART );
313
314 if( xChartModel.is() )
315 xChartModel->unlockControllers(); //#i79578# don't request a new replacement image for charts to often
316 return xChartModel;
317 }
318
GetCurColNum() const319 sal_uInt16 SwTableFUNC::GetCurColNum() const
320 {
321 const size_t nPos = pSh->GetCurTabColNum();
322 size_t nCount = 0;
323 for( size_t i = 0; i < nPos; i++ )
324 if(aCols.IsHidden(i))
325 nCount ++;
326 return nPos - nCount;
327 }
328
GetColCount() const329 sal_uInt16 SwTableFUNC::GetColCount() const
330 {
331 size_t nCount = 0;
332 for(size_t i = 0; i < aCols.Count(); i++ )
333 if(aCols.IsHidden(i))
334 nCount ++;
335 return aCols.Count() - nCount;
336 }
337
GetRightSeparator(int nNum) const338 int SwTableFUNC::GetRightSeparator(int nNum) const
339 {
340 OSL_ENSURE( nNum < static_cast<int>(GetColCount()) ,"Index out of range");
341 int i = 0;
342 while( nNum >= 0 )
343 {
344 if( !aCols.IsHidden(i) )
345 nNum--;
346 i++;
347 }
348 return i - 1;
349 }
350
351 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
352