bump product version to 4.1.6.2
[LibreOffice.git] / sw / source / ui / table / tablemgr.cxx
blobc7e4401cec853ef80aad8e9c964f78231a6d9bb9
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 .
21 #include <com/sun/star/chart/ChartDataRowSource.hpp>
22 #include <com/sun/star/chart2/data/XDataProvider.hpp>
23 #include <com/sun/star/chart2/data/XDataReceiver.hpp>
24 #include <com/sun/star/beans/PropertyState.hpp>
26 #include <sot/storage.hxx>
27 #include <comphelper/classids.hxx>
29 #include "edtwin.hxx"
30 #include "wrtsh.hxx"
31 #include "cmdid.h"
32 #include "frmatr.hxx"
33 #include "view.hxx"
34 #include "basesh.hxx"
35 #include "swundo.hxx"
36 #include "tablemgr.hxx"
37 #include "frmfmt.hxx"
38 #include "instable.hxx"
39 #include "swerror.h"
40 #include "table.hrc"
41 #include "swabstdlg.hxx"
42 #include "swcli.hxx"
43 #include "docsh.hxx"
44 #include "unotbl.hxx"
45 #include "unochart.hxx"
47 using namespace ::com::sun::star;
49 // Adjust line height (dialogue)
51 void SwTableFUNC::ColWidthDlg( Window *pParent )
53 InitTabCols();
54 SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create();
55 OSL_ENSURE(pFact, "SwAbstractDialogFactory fail!");
57 VclAbstractDialog* pDlg = pFact->CreateSwTableWidthDlg(pParent, *this);
58 OSL_ENSURE(pDlg, "Dialogdiet fail!");
59 pDlg->Execute();
60 delete pDlg;
63 // Determine the width
66 SwTwips SwTableFUNC::GetColWidth(sal_uInt16 nNum) const
68 SwTwips nWidth = 0;
70 if( aCols.Count() > 0 )
72 if(aCols.Count() == GetColCount())
74 nWidth = (SwTwips)((nNum == aCols.Count()) ?
75 aCols.GetRight() - aCols[nNum-1] :
76 nNum == 0 ? aCols[nNum] - aCols.GetLeft() :
77 aCols[nNum] - aCols[nNum-1]);
79 else
81 SwTwips nRValid = nNum < GetColCount() ?
82 aCols[(sal_uInt16)GetRightSeparator((int)nNum)]:
83 aCols.GetRight();
84 SwTwips nLValid = nNum ?
85 aCols[(sal_uInt16)GetRightSeparator((int)nNum - 1)]:
86 aCols.GetLeft();
87 nWidth = nRValid - nLValid;
90 else
91 nWidth = aCols.GetRight();
93 return nWidth;
98 SwTwips SwTableFUNC::GetMaxColWidth( sal_uInt16 nNum ) const
100 OSL_ENSURE(nNum <= aCols.Count(), "Index out of Area");
102 if ( GetColCount() > 0 )
104 // The maximum width arises from the own width and
105 // the width each of the neighbor cells reduced by MINLAY.
106 SwTwips nMax = nNum == 0 ?
107 GetColWidth(1) - MINLAY :
108 nNum == GetColCount() ?
109 GetColWidth( nNum-1 ) - MINLAY :
110 GetColWidth(nNum - 1) + GetColWidth( nNum + 1 ) - 2 * MINLAY;
112 return nMax + GetColWidth(nNum) ;
114 else
115 return GetColWidth(nNum);
120 void SwTableFUNC::SetColWidth(sal_uInt16 nNum, SwTwips nNewWidth )
122 // set current width
123 // move all of the following
124 sal_Bool bCurrentOnly = sal_False;
126 if ( aCols.Count() > 0 )
128 if(aCols.Count() != GetColCount())
129 bCurrentOnly = sal_True;
130 SwTwips nWidth = GetColWidth(nNum);
132 int nDiff = (int)(nNewWidth - nWidth);
133 if( !nNum )
134 aCols[ static_cast< sal_uInt16 >(GetRightSeparator(0)) ] += nDiff;
135 else if( nNum < GetColCount() )
137 if(nDiff < GetColWidth(nNum + 1) - MINLAY)
138 aCols[ static_cast< sal_uInt16 >(GetRightSeparator(nNum)) ] += nDiff;
139 else
141 int nDiffLeft = nDiff - (int)GetColWidth(nNum + 1) + (int)MINLAY;
142 aCols[ static_cast< sal_uInt16 >(GetRightSeparator(nNum)) ] += (nDiff - nDiffLeft);
143 aCols[ static_cast< sal_uInt16 >(GetRightSeparator(nNum - 1)) ] -= nDiffLeft;
146 else
147 aCols[ static_cast< sal_uInt16 >(GetRightSeparator(nNum-1)) ] -= nDiff;
149 else
150 aCols.SetRight( std::min( nNewWidth, aCols.GetRightMax()) );
152 pSh->StartAllAction();
153 pSh->SetTabCols( aCols, bCurrentOnly );
154 pSh->EndAllAction();
159 void SwTableFUNC::InitTabCols()
161 OSL_ENSURE(pSh, "no Shell");
163 if( pFmt && pSh)
164 pSh->GetTabCols( aCols );
169 SwTableFUNC::SwTableFUNC(SwWrtShell *pShell, sal_Bool bCopyFmt)
170 : pFmt(pShell->GetTableFmt()),
171 pSh(pShell),
172 bCopy(bCopyFmt)
174 // if applicable copy the format for edit
175 if( pFmt && bCopy )
176 pFmt = new SwFrmFmt( *pFmt );
181 SwTableFUNC::~SwTableFUNC()
183 if(bCopy)
184 delete pFmt;
187 void SwTableFUNC::UpdateChart()
189 //Update of the fields triggered by the user, all Charts of
190 //the table will be brought up to date
191 SwFrmFmt *pFmt2 = pSh->GetTableFmt();
192 if ( pFmt2 && pSh->HasOLEObj( pFmt2->GetName() ) )
194 pSh->StartAllAction();
195 pSh->UpdateCharts( pFmt2->GetName() );
196 pSh->EndAllAction();
200 uno::Reference< frame::XModel > SwTableFUNC::InsertChart(
201 uno::Reference< chart2::data::XDataProvider > &rxDataProvider,
202 sal_Bool bFillWithData,
203 const OUString &rCellRange,
204 SwFlyFrmFmt** ppFlyFrmFmt )
206 uno::Reference< frame::XModel > xChartModel;
207 pSh->StartUndo( UNDO_UI_INSERT_CHART );
208 pSh->StartAllAction();
210 String aName;
211 if (pSh->IsCrsrInTbl())
213 aName = pSh->GetTableFmt()->GetName();
214 // insert node before table
215 pSh->MoveTable( fnTableCurr, fnTableStart );
216 pSh->Up( sal_False, 1, sal_False );
217 if ( pSh->IsCrsrInTbl() )
219 if ( aName != pSh->GetTableFmt()->GetName() )
220 pSh->Down( sal_False, 1, sal_False ); // two adjacent tables
222 pSh->SplitNode();
225 // insert chart
226 OUString aObjName;
227 comphelper::EmbeddedObjectContainer aCnt;
228 uno::Reference < embed::XEmbeddedObject > xObj =
229 aCnt.CreateEmbeddedObject( SvGlobalName( SO3_SCH_CLASSID ).GetByteSequence(), aObjName );
231 ::svt::EmbeddedObjectRef aEmbObjRef( xObj, ::com::sun::star::embed::Aspects::MSOLE_CONTENT );
232 if ( xObj.is() )
235 SwFlyFrmFmt* pTmp = 0;
236 pSh->InsertOleObject( aEmbObjRef, &pTmp );
237 if (ppFlyFrmFmt)
238 *ppFlyFrmFmt = pTmp;
240 uno::Reference< embed::XComponentSupplier > xCompSupp( xObj, uno::UNO_QUERY );
241 if( xCompSupp.is())
243 xChartModel.set( xCompSupp->getComponent(), uno::UNO_QUERY );
244 if( xChartModel.is() )
245 xChartModel->lockControllers(); //#i79578# don't request a new replacement image for charts to often - block change notifications
248 // set the table name at the OLE-node
249 if (aName.Len())
250 pSh->SetChartName( aName );
252 pSh->EndAllAction();
254 if ( xObj.is() )
256 // Let the chart be activated after the inserting
257 SfxInPlaceClient* pClient = pSh->GetView().FindIPClient( xObj, &pSh->GetView().GetEditWin() );
258 if ( !pClient )
260 pClient = new SwOleClient( &pSh->GetView(), &pSh->GetView().GetEditWin(), aEmbObjRef );
261 pSh->SetCheckForOLEInCaption( sal_True );
263 pSh->CalcAndSetScale( aEmbObjRef );
264 //#50270# We don't need to handle errors,
265 //this does the DoVerb in the SfxViewShell.
266 ErrCode nErr = pClient->DoVerb( SVVERB_SHOW );
267 (void) nErr;
270 uno::Reference< chart2::data::XDataReceiver > xDataReceiver( xChartModel, uno::UNO_QUERY );
271 if (bFillWithData && xDataReceiver.is() && rxDataProvider.is())
273 xDataReceiver->attachDataProvider( rxDataProvider );
275 uno::Reference< util::XNumberFormatsSupplier > xNumberFormatsSupplier( pSh->GetView().GetDocShell()->GetModel(), uno::UNO_QUERY );
276 xDataReceiver->attachNumberFormatsSupplier( xNumberFormatsSupplier );
278 // default values for ranges that do not consist of a single row or column
279 bool bHasCategories = true;
280 bool bFirstCellAsLabel = true;
281 chart::ChartDataRowSource eDataRowSource = chart::ChartDataRowSource_COLUMNS;
283 SwRangeDescriptor aDesc;
284 FillRangeDescriptor( aDesc, rCellRange );
285 bool bSingleRowCol = aDesc.nTop == aDesc.nBottom || aDesc.nLeft == aDesc.nRight;
286 if (bSingleRowCol)
288 aDesc.Normalize();
289 sal_Int32 nRowLen = aDesc.nRight - aDesc.nLeft + 1;
290 sal_Int32 nColLen = aDesc.nBottom - aDesc.nTop + 1;
292 bHasCategories = false;
293 if (nRowLen == 1 && nColLen == 1)
294 bFirstCellAsLabel = false;
295 else if (nRowLen > 1)
296 eDataRowSource = chart::ChartDataRowSource_ROWS;
297 else if (nColLen > 1)
298 eDataRowSource = chart::ChartDataRowSource_COLUMNS;
299 else {
300 OSL_FAIL("unexpected state" );
304 uno::Sequence< beans::PropertyValue > aArgs( 4 );
305 aArgs[0] = beans::PropertyValue(
306 OUString("CellRangeRepresentation"), -1,
307 uno::makeAny( rCellRange ), beans::PropertyState_DIRECT_VALUE );
308 aArgs[1] = beans::PropertyValue(
309 OUString("HasCategories"), -1,
310 uno::makeAny( bHasCategories ), beans::PropertyState_DIRECT_VALUE );
311 aArgs[2] = beans::PropertyValue(
312 OUString("FirstCellAsLabel"), -1,
313 uno::makeAny( bFirstCellAsLabel ), beans::PropertyState_DIRECT_VALUE );
314 aArgs[3] = beans::PropertyValue(
315 OUString("DataRowSource"), -1,
316 uno::makeAny( eDataRowSource ), beans::PropertyState_DIRECT_VALUE );
317 xDataReceiver->setArguments( aArgs );
320 pSh->EndUndo( UNDO_UI_INSERT_CHART );
322 if( xChartModel.is() )
323 xChartModel->unlockControllers(); //#i79578# don't request a new replacement image for charts to often
324 return xChartModel;
327 sal_uInt16 SwTableFUNC::GetCurColNum() const
329 sal_uInt16 nPos = pSh->GetCurTabColNum();
330 sal_uInt16 nCount = 0;
331 for(sal_uInt16 i = 0; i < nPos; i++ )
332 if(aCols.IsHidden(i))
333 nCount ++;
334 return nPos - nCount;
340 sal_uInt16 SwTableFUNC::GetColCount() const
342 sal_uInt16 nCount = 0;
343 for(sal_uInt16 i = 0; i < aCols.Count(); i++ )
344 if(aCols.IsHidden(i))
345 nCount ++;
346 return aCols.Count() - nCount;
351 int SwTableFUNC::GetRightSeparator(int nNum) const
353 OSL_ENSURE( nNum < (int)GetColCount() ,"Index out of range");
354 int i = 0;
355 while( nNum >= 0 )
357 if( !aCols.IsHidden( static_cast< sal_uInt16 >(i)) )
358 nNum--;
359 i++;
361 return i - 1;
366 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */