1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
36 #include "tablemgr.hxx"
38 #include "instable.hxx"
41 #include "swabstdlg.hxx"
45 #include "unochart.hxx"
47 using namespace ::com::sun::star
;
49 // Adjust line height (dialogue)
51 void SwTableFUNC::ColWidthDlg( Window
*pParent
)
54 SwAbstractDialogFactory
* pFact
= SwAbstractDialogFactory::Create();
55 OSL_ENSURE(pFact
, "SwAbstractDialogFactory fail!");
57 VclAbstractDialog
* pDlg
= pFact
->CreateSwTableWidthDlg(pParent
, *this);
58 OSL_ENSURE(pDlg
, "Dialogdiet fail!");
63 // Determine the width
66 SwTwips
SwTableFUNC::GetColWidth(sal_uInt16 nNum
) const
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]);
81 SwTwips nRValid
= nNum
< GetColCount() ?
82 aCols
[(sal_uInt16
)GetRightSeparator((int)nNum
)]:
84 SwTwips nLValid
= nNum
?
85 aCols
[(sal_uInt16
)GetRightSeparator((int)nNum
- 1)]:
87 nWidth
= nRValid
- nLValid
;
91 nWidth
= aCols
.GetRight();
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
) ;
115 return GetColWidth(nNum
);
120 void SwTableFUNC::SetColWidth(sal_uInt16 nNum
, SwTwips nNewWidth
)
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
);
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
;
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
;
147 aCols
[ static_cast< sal_uInt16
>(GetRightSeparator(nNum
-1)) ] -= nDiff
;
150 aCols
.SetRight( std::min( nNewWidth
, aCols
.GetRightMax()) );
152 pSh
->StartAllAction();
153 pSh
->SetTabCols( aCols
, bCurrentOnly
);
159 void SwTableFUNC::InitTabCols()
161 OSL_ENSURE(pSh
, "no Shell");
164 pSh
->GetTabCols( aCols
);
169 SwTableFUNC::SwTableFUNC(SwWrtShell
*pShell
, sal_Bool bCopyFmt
)
170 : pFmt(pShell
->GetTableFmt()),
174 // if applicable copy the format for edit
176 pFmt
= new SwFrmFmt( *pFmt
);
181 SwTableFUNC::~SwTableFUNC()
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() );
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();
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
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
);
235 SwFlyFrmFmt
* pTmp
= 0;
236 pSh
->InsertOleObject( aEmbObjRef
, &pTmp
);
240 uno::Reference
< embed::XComponentSupplier
> xCompSupp( xObj
, uno::UNO_QUERY
);
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
250 pSh
->SetChartName( aName
);
256 // Let the chart be activated after the inserting
257 SfxInPlaceClient
* pClient
= pSh
->GetView().FindIPClient( xObj
, &pSh
->GetView().GetEditWin() );
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
);
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
;
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
;
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
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
))
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
))
346 return aCols
.Count() - nCount
;
351 int SwTableFUNC::GetRightSeparator(int nNum
) const
353 OSL_ENSURE( nNum
< (int)GetColCount() ,"Index out of range");
357 if( !aCols
.IsHidden( static_cast< sal_uInt16
>(i
)) )
366 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */