merge the formfield patch from ooo-build
[ooovba.git] / sw / source / ui / table / tablemgr.cxx
blob55f3fd6ed85e37d8d9a2e7992bfbd0abf4b463f8
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: tablemgr.cxx,v $
10 * $Revision: 1.17 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sw.hxx"
34 #include <com/sun/star/chart/ChartDataRowSource.hpp>
35 #include <com/sun/star/chart2/data/XDataProvider.hpp>
36 #include <com/sun/star/chart2/data/XDataReceiver.hpp>
37 #include <com/sun/star/beans/PropertyState.hpp>
39 #include <sot/storage.hxx>
40 #include <sot/clsids.hxx>
42 #include "edtwin.hxx"
43 #include "errhdl.hxx"
44 #include "wrtsh.hxx"
45 #include "cmdid.h"
46 #include "frmatr.hxx"
47 #include "view.hxx"
48 #include "basesh.hxx"
49 #include "swundo.hxx"
50 #include "tablemgr.hxx"
51 #include "frmfmt.hxx"
52 #include "instable.hxx"
53 #include "swerror.h"
54 #include "table.hrc"
55 #include "swabstdlg.hxx"
56 #include "swcli.hxx"
57 #include "docsh.hxx"
58 #include "unotbl.hxx"
59 #include "unochart.hxx"
61 using namespace ::com::sun::star;
63 /*------------------------------------------------------------------------
64 Beschreibung: Zeilenhoehe einstellen (Dialog)
65 ------------------------------------------------------------------------*/
68 void SwTableFUNC::ColWidthDlg( Window *pParent )
70 InitTabCols();
71 SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create();
72 DBG_ASSERT(pFact, "SwAbstractDialogFactory fail!");
74 VclAbstractDialog* pDlg = pFact->CreateSwTableWidthDlg( pParent, *this ,DLG_COL_WIDTH );
75 DBG_ASSERT(pDlg, "Dialogdiet fail!");
76 pDlg->Execute();
77 delete pDlg;
80 /*--------------------------------------------------------------------
81 Beschreibung: Breite ermitteln
82 --------------------------------------------------------------------*/
85 SwTwips SwTableFUNC::GetColWidth(USHORT nNum) const
87 SwTwips nWidth = 0;
89 if( aCols.Count() > 0 )
91 if(aCols.Count() == GetColCount())
93 nWidth = (SwTwips)((nNum == aCols.Count()) ?
94 aCols.GetRight() - aCols[nNum-1] :
95 nNum == 0 ? aCols[nNum] - aCols.GetLeft() :
96 aCols[nNum] - aCols[nNum-1]);
98 else
100 SwTwips nRValid = nNum < GetColCount() ?
101 aCols[(USHORT)GetRightSeparator((int)nNum)]:
102 aCols.GetRight();
103 SwTwips nLValid = nNum ?
104 aCols[(USHORT)GetRightSeparator((int)nNum - 1)]:
105 aCols.GetLeft();
106 nWidth = nRValid - nLValid;
109 else
110 nWidth = aCols.GetRight();
112 return nWidth;
117 SwTwips SwTableFUNC::GetMaxColWidth( USHORT nNum ) const
119 ASSERT(nNum <= aCols.Count(), "Index out of Area");
121 if ( GetColCount() > 0 )
123 // Die max. Breite ergibt sich aus der eigenen Breite und
124 // der Breite der Nachbarzellen um je MINLAY verringert
125 SwTwips nMax = nNum == 0 ?
126 GetColWidth(1) - MINLAY :
127 nNum == GetColCount() ?
128 GetColWidth( nNum-1 ) - MINLAY :
129 GetColWidth(nNum - 1) + GetColWidth( nNum + 1 ) - 2 * MINLAY;
131 return nMax + GetColWidth(nNum) ;
133 else
134 return GetColWidth(nNum);
139 void SwTableFUNC::SetColWidth(USHORT nNum, SwTwips nNewWidth )
141 // aktuelle Breite setzen
142 // alle folgenden Verschieben
143 BOOL bCurrentOnly = FALSE;
144 SwTwips nWidth = 0;
146 if ( aCols.Count() > 0 )
148 if(aCols.Count() != GetColCount())
149 bCurrentOnly = TRUE;
150 nWidth = GetColWidth(nNum);
152 int nDiff = (int)(nNewWidth - nWidth);
153 if( !nNum )
154 aCols[ static_cast< USHORT >(GetRightSeparator(0)) ] += nDiff;
155 else if( nNum < GetColCount() )
157 if(nDiff < GetColWidth(nNum + 1) - MINLAY)
158 aCols[ static_cast< USHORT >(GetRightSeparator(nNum)) ] += nDiff;
159 else
161 int nDiffLeft = nDiff - (int)GetColWidth(nNum + 1) + (int)MINLAY;
162 aCols[ static_cast< USHORT >(GetRightSeparator(nNum)) ] += (nDiff - nDiffLeft);
163 aCols[ static_cast< USHORT >(GetRightSeparator(nNum - 1)) ] -= nDiffLeft;
166 else
167 aCols[ static_cast< USHORT >(GetRightSeparator(nNum-1)) ] -= nDiff;
169 else
170 aCols.SetRight( Min( nNewWidth, aCols.GetRightMax()) );
172 pSh->StartAllAction();
173 pSh->SetTabCols( aCols, bCurrentOnly );
174 pSh->EndAllAction();
179 void SwTableFUNC::InitTabCols()
181 ASSERT(pSh, keine Shell);
183 if( pFmt && pSh)
184 pSh->GetTabCols( aCols );
189 SwTableFUNC::SwTableFUNC(SwWrtShell *pShell, BOOL bCopyFmt)
190 : pFmt(pShell->GetTableFmt()),
191 pSh(pShell),
192 bCopy(bCopyFmt)
194 // gfs. das Format fuer die Bearbeitung kopieren
195 if( pFmt && bCopy )
196 pFmt = new SwFrmFmt( *pFmt );
201 SwTableFUNC::~SwTableFUNC()
203 if(bCopy)
204 delete pFmt;
207 void SwTableFUNC::UpdateChart()
209 //Update der Felder in der Tabelle vom User ausgeloesst, alle
210 //Charts zu der Tabelle werden auf den neuesten Stand gebracht.
211 SwFrmFmt *pFmt2 = pSh->GetTableFmt();
212 if ( pFmt2 && pSh->HasOLEObj( pFmt2->GetName() ) )
214 pSh->StartAllAction();
215 pSh->UpdateCharts( pFmt2->GetName() );
216 pSh->EndAllAction();
220 uno::Reference< frame::XModel > SwTableFUNC::InsertChart(
221 uno::Reference< chart2::data::XDataProvider > &rxDataProvider,
222 sal_Bool bFillWithData,
223 const rtl::OUString &rCellRange,
224 SwFlyFrmFmt** ppFlyFrmFmt )
226 uno::Reference< frame::XModel > xChartModel;
227 pSh->StartUndo( UNDO_UI_INSERT_CHART );
228 pSh->StartAllAction();
230 String aName;
231 if (pSh->IsCrsrInTbl())
233 aName = pSh->GetTableFmt()->GetName();
234 // insert node before table
235 pSh->MoveTable( fnTableCurr, fnTableStart );
236 pSh->Up( FALSE, 1, FALSE );
237 if ( pSh->IsCrsrInTbl() )
239 if ( aName != pSh->GetTableFmt()->GetName() )
240 pSh->Down( FALSE, 1, FALSE ); // two adjacent tables
242 pSh->SplitNode();
245 // insert chart
246 ::rtl::OUString aObjName;
247 comphelper::EmbeddedObjectContainer aCnt;
248 uno::Reference < embed::XEmbeddedObject > xObj =
249 aCnt.CreateEmbeddedObject( SvGlobalName( SO3_SCH_CLASSID ).GetByteSequence(), aObjName );
251 ::svt::EmbeddedObjectRef aEmbObjRef( xObj, ::com::sun::star::embed::Aspects::MSOLE_CONTENT );
252 if ( xObj.is() )
255 SwFlyFrmFmt* pTmp = 0;
256 pSh->InsertOleObject( aEmbObjRef, &pTmp );
257 if (ppFlyFrmFmt)
258 *ppFlyFrmFmt = pTmp;
260 uno::Reference< embed::XComponentSupplier > xCompSupp( xObj, uno::UNO_QUERY );
261 if( xCompSupp.is())
263 xChartModel.set( xCompSupp->getComponent(), uno::UNO_QUERY );
264 if( xChartModel.is() )
265 xChartModel->lockControllers(); //#i79578# don't request a new replacement image for charts to often - block change notifications
268 // set the table name at the OLE-node
269 if (aName.Len())
270 pSh->SetChartName( aName );
272 pSh->EndAllAction();
274 if ( xObj.is() )
276 // Let the chart be activated after the inserting
277 SfxInPlaceClient* pClient = pSh->GetView().FindIPClient( xObj, &pSh->GetView().GetEditWin() );
278 if ( !pClient )
280 pClient = new SwOleClient( &pSh->GetView(), &pSh->GetView().GetEditWin(), aEmbObjRef );
281 pSh->SetCheckForOLEInCaption( TRUE );
283 pSh->CalcAndSetScale( aEmbObjRef );
284 //#50270# Error brauchen wir nicht handeln, das erledigt das
285 //DoVerb in der SfxViewShell
286 ErrCode nErr = pClient->DoVerb( SVVERB_SHOW );
287 (void) nErr;
290 uno::Reference< chart2::data::XDataReceiver > xDataReceiver( xChartModel, uno::UNO_QUERY );
291 if (bFillWithData && xDataReceiver.is() && rxDataProvider.is())
293 xDataReceiver->attachDataProvider( rxDataProvider );
295 uno::Reference< util::XNumberFormatsSupplier > xNumberFormatsSupplier( pSh->GetView().GetDocShell()->GetModel(), uno::UNO_QUERY );
296 xDataReceiver->attachNumberFormatsSupplier( xNumberFormatsSupplier );
298 // default values for ranges that do not consist of a single row or column
299 bool bHasCategories = true;
300 bool bFirstCellAsLabel = true;
301 chart::ChartDataRowSource eDataRowSource = chart::ChartDataRowSource_COLUMNS;
303 SwRangeDescriptor aDesc;
304 FillRangeDescriptor( aDesc, rCellRange );
305 bool bSingleRowCol = aDesc.nTop == aDesc.nBottom || aDesc.nLeft == aDesc.nRight;
306 if (bSingleRowCol)
308 aDesc.Normalize();
309 sal_Int32 nRowLen = aDesc.nRight - aDesc.nLeft + 1;
310 sal_Int32 nColLen = aDesc.nBottom - aDesc.nTop + 1;
312 bHasCategories = false;
313 if (nRowLen == 1 && nColLen == 1)
314 bFirstCellAsLabel = false;
315 else if (nRowLen > 1)
316 eDataRowSource = chart::ChartDataRowSource_ROWS;
317 else if (nColLen > 1)
318 eDataRowSource = chart::ChartDataRowSource_COLUMNS;
319 else {
320 DBG_ERROR( "unexpected state" );
324 uno::Sequence< beans::PropertyValue > aArgs( 4 );
325 aArgs[0] = beans::PropertyValue(
326 ::rtl::OUString::createFromAscii("CellRangeRepresentation"), -1,
327 uno::makeAny( rCellRange ), beans::PropertyState_DIRECT_VALUE );
328 aArgs[1] = beans::PropertyValue(
329 ::rtl::OUString::createFromAscii("HasCategories"), -1,
330 uno::makeAny( bHasCategories ), beans::PropertyState_DIRECT_VALUE );
331 aArgs[2] = beans::PropertyValue(
332 ::rtl::OUString::createFromAscii("FirstCellAsLabel"), -1,
333 uno::makeAny( bFirstCellAsLabel ), beans::PropertyState_DIRECT_VALUE );
334 aArgs[3] = beans::PropertyValue(
335 ::rtl::OUString::createFromAscii("DataRowSource"), -1,
336 uno::makeAny( eDataRowSource ), beans::PropertyState_DIRECT_VALUE );
337 xDataReceiver->setArguments( aArgs );
340 pSh->EndUndo( UNDO_UI_INSERT_CHART );
342 if( xChartModel.is() )
343 xChartModel->unlockControllers(); //#i79578# don't request a new replacement image for charts to often
344 return xChartModel;
347 USHORT SwTableFUNC::GetCurColNum() const
349 USHORT nPos = pSh->GetCurTabColNum();
350 USHORT nCount = 0;
351 for(USHORT i = 0; i < nPos; i++ )
352 if(aCols.IsHidden(i))
353 nCount ++;
354 return nPos - nCount;
360 USHORT SwTableFUNC::GetColCount() const
362 USHORT nCount = 0;
363 for(USHORT i = 0; i < aCols.Count(); i++ )
364 if(aCols.IsHidden(i))
365 nCount ++;
366 return aCols.Count() - nCount;
371 int SwTableFUNC::GetRightSeparator(int nNum) const
373 DBG_ASSERT( nNum < (int)GetColCount() ,"Index out of range");
374 int i = 0;
375 while( nNum >= 0 )
377 if( !aCols.IsHidden( static_cast< USHORT >(i)) )
378 nNum--;
379 i++;
381 return i - 1;