LibreOffice Module sw (master)  1
tablemgr.cxx
Go to the documentation of this file.
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/XComponentSupplier.hpp>
27 
28 #include <sot/storage.hxx>
29 #include <comphelper/classids.hxx>
30 #include <svx/charthelper.hxx>
31 #include <svtools/embedhlp.hxx>
32 
33 #include <edtwin.hxx>
34 #include <wrtsh.hxx>
35 #include <cmdid.h>
36 #include <frmatr.hxx>
37 #include <view.hxx>
38 #include <basesh.hxx>
39 #include <swundo.hxx>
40 #include <tablemgr.hxx>
41 #include <frmfmt.hxx>
42 #include <instable.hxx>
43 #include <swerror.h>
44 #include <swabstdlg.hxx>
45 #include <swcli.hxx>
46 #include <docsh.hxx>
47 #include <unotbl.hxx>
48 #include <unochart.hxx>
49 #include <memory>
50 
51 using namespace ::com::sun::star;
52 
53 // Adjust line height (dialogue)
55 {
56  InitTabCols();
58  ScopedVclPtr<VclAbstractDialog> pDlg(pFact->CreateSwTableWidthDlg(pParent, *this));
59  pDlg->Execute();
60 }
61 
62 // Determine the width
63 SwTwips SwTableFUNC::GetColWidth(sal_uInt16 nNum) const
64 {
65  SwTwips nWidth = 0;
66 
67  if( aCols.Count() > 0 )
68  {
69  if(aCols.Count() == GetColCount())
70  {
71  if(nNum == aCols.Count())
72  nWidth = aCols.GetRight() - aCols[nNum-1];
73  else
74  {
75  if(nNum == 0)
76  nWidth = aCols[nNum] - aCols.GetLeft();
77  else
78  nWidth = aCols[nNum] - aCols[nNum-1];
79  }
80  }
81  else
82  {
83  SwTwips nRValid = nNum < GetColCount() ?
84  aCols[GetRightSeparator(nNum)] :
85  aCols.GetRight();
86  SwTwips nLValid = nNum ?
87  aCols[GetRightSeparator(nNum - 1)] :
88  aCols.GetLeft();
89  nWidth = nRValid - nLValid;
90  }
91  }
92  else
93  nWidth = aCols.GetRight();
94  return nWidth;
95 }
96 
97 SwTwips SwTableFUNC::GetMaxColWidth( sal_uInt16 nNum ) const
98 {
99  OSL_ENSURE(nNum <= aCols.Count(), "Index out of Area");
100 
101  if ( GetColCount() > 0 )
102  {
103  // The maximum width arises from the own width and
104  // the width each of the neighbor cells reduced by MINLAY.
105  SwTwips nMax;
106  if(nNum == 0)
107  nMax = GetColWidth(1) - MINLAY;
108  else
109  {
110  nMax = GetColWidth(nNum-1);
111  if(nNum == GetColCount())
112  nMax -= MINLAY;
113  else
114  nMax += GetColWidth(nNum+1) - 2 * MINLAY;
115  }
116  return nMax + GetColWidth(nNum);
117  }
118  else
119  return GetColWidth(nNum);
120 }
121 
122 void SwTableFUNC::SetColWidth(sal_uInt16 nNum, SwTwips nNewWidth )
123 {
124  // set current width
125  // move all of the following
126  bool bCurrentOnly = false;
127 
128  if ( aCols.Count() > 0 )
129  {
130  if(aCols.Count() != GetColCount())
131  bCurrentOnly = true;
132  SwTwips nWidth = GetColWidth(nNum);
133 
134  int nDiff = static_cast<int>(nNewWidth - nWidth);
135  if( !nNum )
136  aCols[ GetRightSeparator(0) ] += nDiff;
137  else if( nNum < GetColCount() )
138  {
139  if(nDiff < GetColWidth(nNum + 1) - MINLAY)
140  aCols[ GetRightSeparator(nNum) ] += nDiff;
141  else
142  {
143  int nDiffLeft = nDiff - static_cast<int>(GetColWidth(nNum + 1)) + int(MINLAY);
144  aCols[ GetRightSeparator(nNum) ] += (nDiff - nDiffLeft);
145  aCols[ GetRightSeparator(nNum - 1) ] -= nDiffLeft;
146  }
147  }
148  else
149  aCols[ GetRightSeparator(nNum-1) ] -= nDiff;
150  }
151  else
152  aCols.SetRight( std::min( nNewWidth, aCols.GetRightMax()) );
153 
154  pSh->StartAllAction();
155  pSh->SetTabCols( aCols, bCurrentOnly );
156  pSh->EndAllAction();
157 }
158 
160 {
161  OSL_ENSURE(pSh, "no Shell");
162 
163  if( pFormat && pSh)
164  pSh->GetTabCols( aCols );
165 }
166 
168  : pFormat(pShell->GetTableFormat()),
169  pSh(pShell)
170 {
171 }
172 
174 {
175 }
176 
178 {
179  //Update of the fields triggered by the user, all Charts of
180  //the table will be brought up to date
181  SwFrameFormat *pFormat2 = pSh->GetTableFormat();
182  if ( pFormat2 && pSh->HasOLEObj( pFormat2->GetName() ) )
183  {
184  pSh->StartAllAction();
185  pSh->UpdateCharts( pFormat2->GetName() );
186  pSh->EndAllAction();
187  }
188 }
189 
190 uno::Reference< frame::XModel > SwTableFUNC::InsertChart(
191  uno::Reference< chart2::data::XDataProvider > const &rxDataProvider,
192  bool bFillWithData,
193  const OUString &rCellRange,
194  SwFlyFrameFormat** ppFlyFrameFormat )
195 {
196  uno::Reference< frame::XModel > xChartModel;
198  pSh->StartAllAction();
199 
200  OUString aName;
201  if (pSh->IsCursorInTable())
202  {
203  aName = pSh->GetTableFormat()->GetName();
204  // insert node before table
206  pSh->Up( false );
207  if ( pSh->IsCursorInTable() )
208  {
209  if ( aName != pSh->GetTableFormat()->GetName() )
210  pSh->Down( false ); // two adjacent tables
211  }
212  pSh->SplitNode();
213  }
214 
215  // insert chart
216  OUString aObjName;
218  uno::Reference < embed::XEmbeddedObject > xObj =
219  aCnt.CreateEmbeddedObject( SvGlobalName( SO3_SCH_CLASSID ).GetByteSequence(), aObjName );
220 
221  ::svt::EmbeddedObjectRef aEmbObjRef( xObj, css::embed::Aspects::MSOLE_CONTENT );
222  if ( xObj.is() )
223  {
224 
225  SwFlyFrameFormat* pTmp = nullptr;
226  pSh->InsertOleObject( aEmbObjRef, &pTmp );
227  if (ppFlyFrameFormat)
228  *ppFlyFrameFormat = pTmp;
229 
230  uno::Reference< embed::XComponentSupplier > xCompSupp( xObj, uno::UNO_QUERY );
231  if( xCompSupp.is())
232  {
233  xChartModel.set( xCompSupp->getComponent(), uno::UNO_QUERY );
234  if( xChartModel.is() )
235  {
236  // Create a default chart type.
237  uno::Reference<chart2::XChartDocument> xChartDoc(xChartModel, uno::UNO_QUERY);
238  if (xChartDoc.is())
239  xChartDoc->createDefaultChart();
240 
241  xChartModel->lockControllers(); //#i79578# don't request a new replacement image for charts to often - block change notifications
242  }
243  }
244 
245  // set the table name at the OLE-node
246  if (!aName.isEmpty())
247  pSh->SetChartName( aName );
248  }
249  pSh->EndAllAction();
250 
251  if ( xObj.is() )
252  {
253  // Let the chart be activated after the inserting
254  SfxInPlaceClient* pClient = pSh->GetView().FindIPClient( xObj, &pSh->GetView().GetEditWin() );
255  if ( !pClient )
256  {
257  pClient = new SwOleClient( &pSh->GetView(), &pSh->GetView().GetEditWin(), aEmbObjRef );
258  pSh->SetCheckForOLEInCaption( true );
259  }
260  pSh->CalcAndSetScale( aEmbObjRef );
261  //#50270# We don't need to handle errors,
262  //this does the DoVerb in the SfxViewShell.
263  ErrCode nErr = pClient->DoVerb(embed::EmbedVerbs::MS_OLEVERB_SHOW);
264  (void) nErr;
265 
266  // #i121334#
268  }
269 
270  uno::Reference< chart2::data::XDataReceiver > xDataReceiver( xChartModel, uno::UNO_QUERY );
271  if (bFillWithData && xDataReceiver.is() && rxDataProvider.is())
272  {
273  xDataReceiver->attachDataProvider( rxDataProvider );
274 
275  uno::Reference< util::XNumberFormatsSupplier > xNumberFormatsSupplier( pSh->GetView().GetDocShell()->GetModel(), uno::UNO_QUERY );
276  xDataReceiver->attachNumberFormatsSupplier( xNumberFormatsSupplier );
277 
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;
282 
283  SwRangeDescriptor aDesc;
284  FillRangeDescriptor( aDesc, rCellRange );
285  bool bSingleRowCol = aDesc.nTop == aDesc.nBottom || aDesc.nLeft == aDesc.nRight;
286  if (bSingleRowCol)
287  {
288  aDesc.Normalize();
289  sal_Int32 nRowLen = aDesc.nRight - aDesc.nLeft + 1;
290  sal_Int32 nColLen = aDesc.nBottom - aDesc.nTop + 1;
291 
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" );
301  }
302  }
303 
304  uno::Sequence< beans::PropertyValue > aArgs( 4 );
305  aArgs[0] = beans::PropertyValue(
306  "CellRangeRepresentation", -1,
307  uno::makeAny( rCellRange ), beans::PropertyState_DIRECT_VALUE );
308  aArgs[1] = beans::PropertyValue(
309  "HasCategories", -1,
310  uno::makeAny( bHasCategories ), beans::PropertyState_DIRECT_VALUE );
311  aArgs[2] = beans::PropertyValue(
312  "FirstCellAsLabel", -1,
313  uno::makeAny( bFirstCellAsLabel ), beans::PropertyState_DIRECT_VALUE );
314  aArgs[3] = beans::PropertyValue(
315  "DataRowSource", -1,
316  uno::makeAny( eDataRowSource ), beans::PropertyState_DIRECT_VALUE );
317  xDataReceiver->setArguments( aArgs );
318  }
319 
321 
322  if( xChartModel.is() )
323  xChartModel->unlockControllers(); //#i79578# don't request a new replacement image for charts to often
324  return xChartModel;
325 }
326 
327 sal_uInt16 SwTableFUNC::GetCurColNum() const
328 {
329  const size_t nPos = pSh->GetCurTabColNum();
330  size_t nCount = 0;
331  for( size_t i = 0; i < nPos; i++ )
332  if(aCols.IsHidden(i))
333  nCount ++;
334  return nPos - nCount;
335 }
336 
337 sal_uInt16 SwTableFUNC::GetColCount() const
338 {
339  size_t nCount = 0;
340  for(size_t i = 0; i < aCols.Count(); i++ )
341  if(aCols.IsHidden(i))
342  nCount ++;
343  return aCols.Count() - nCount;
344 }
345 
347 {
348  OSL_ENSURE( nNum < static_cast<int>(GetColCount()) ,"Index out of range");
349  int i = 0;
350  while( nNum >= 0 )
351  {
352  if( !aCols.IsHidden(i) )
353  nNum--;
354  i++;
355  }
356  return i - 1;
357 }
358 
359 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void Normalize()
Definition: unotbl.cxx:555
css::uno::Reference< css::embed::XEmbeddedObject > CreateEmbeddedObject(const css::uno::Sequence< sal_Int8 > &, OUString &, OUString const *pBaseURL=nullptr)
SwFrameFormat *const pFormat
Definition: tablemgr.hxx:44
void SplitNode(bool bAutoFormat=false)
Definition: wrtsh1.cxx:973
SwTwips GetColWidth(sal_uInt16 nNum) const
Definition: tablemgr.cxx:63
void UpdateChart()
Definition: tablemgr.cxx:177
#define MINLAY
Definition: swtypes.hxx:66
long GetRightMax() const
Definition: tabcol.hxx:79
SwUndoId EndUndo(SwUndoId eUndoId=SwUndoId::EMPTY, const SwRewriter *pRewriter=nullptr)
Closes parenthesis of nUndoId, not used by UI.
Definition: edws.cxx:233
css::uno::Reference< css::frame::XModel > InsertChart(css::uno::Reference< css::chart2::data::XDataProvider > const &rxDataProvider, bool bFillWithData, const OUString &rCellRange, SwFlyFrameFormat **ppFlyFrameFormat=nullptr)
Definition: tablemgr.cxx:190
sal_Int32 nLeft
Definition: unotbl.hxx:247
long SwTwips
Definition: swtypes.hxx:49
SwEditWin & GetEditWin()
Definition: view.hxx:403
css::uno::Reference< css::frame::XModel > GetModel() const
SfxInPlaceClient * FindIPClient(const css::uno::Reference< css::embed::XEmbeddedObject > &xObj, vcl::Window *pObjParentWin) const
bool InsertOleObject(const svt::EmbeddedObjectRef &xObj, SwFlyFrameFormat **pFlyFrameFormat=nullptr)
Definition: wrtsh1.cxx:471
void EndAllAction()
Definition: edws.cxx:96
long GetRight() const
Definition: tabcol.hxx:78
bool GotoCurrTable(SwPaM &rCurrentCursor, SwMoveFnCollection const &fnPosTable, bool bInReadOnly)
Definition: trvltbl.cxx:636
Used by the UI to modify the document model.
Definition: wrtsh.hxx:88
bool HasOLEObj(const OUString &rName) const
Is there an OLEObject with this name (SwFormat)?
Definition: editsh.cxx:378
const SwView & GetView() const
Definition: wrtsh.hxx:426
sal_Int32 nBottom
Definition: unotbl.hxx:248
const OUString & GetName() const
Definition: format.hxx:111
SwTwips GetMaxColWidth(sal_uInt16 nNum) const
Definition: tablemgr.cxx:97
bool FillRangeDescriptor(SwRangeDescriptor &rDesc, const OUString &rCellRangeName)
rCellRangeName needs to be of one of the following formats:
Definition: unochart.cxx:181
sal_Int32 nRight
Definition: unotbl.hxx:249
void InitTabCols()
Definition: tablemgr.cxx:159
bool IsHidden(size_t nPos) const
Definition: tabcol.hxx:66
void UpdateCharts(const OUString &rName)
Update content of all charts for table with given name.
Definition: editsh.cxx:403
void SetChartName(const OUString &rName)
Definition: editsh.cxx:396
const SwTableNode * IsCursorInTable() const
Definition: crsrsh.hxx:887
SwTableFUNC(SwWrtShell *pShell)
Definition: tablemgr.cxx:167
bool Up(bool bSelect, sal_uInt16 nCount=1, bool bBasicCall=false)
Definition: move.cxx:137
void SetTabCols(const SwTabCols &rNew, bool bCurRowOnly)
Definition: fetab.cxx:624
Style of a layout element.
Definition: frmfmt.hxx:57
sal_Int32 nTop
Definition: unotbl.hxx:246
sal_uInt16 GetColCount() const
Definition: tablemgr.cxx:337
void SetCheckForOLEInCaption(bool bFlag)
Definition: fesh.hxx:454
int i
SwUndoId StartUndo(SwUndoId eUndoId=SwUndoId::EMPTY, const SwRewriter *pRewriter=nullptr)
Undo: set up Undo parenthesis, return nUndoId of this parenthesis.
Definition: edws.cxx:222
static void AdaptDefaultsForChart(const css::uno::Reference< css::embed::XEmbeddedObject > &xEmbObj)
static SwAbstractDialogFactory * Create()
Definition: swabstdlg.cxx:36
void GetTabCols(SwTabCols &rToFill) const
Info about columns and margins.
Definition: fetab.cxx:642
SAL_DLLPRIVATE int GetRightSeparator(int nNum) const
Definition: tablemgr.cxx:346
SwDocShell * GetDocShell()
Definition: view.cxx:1115
SwFrameFormat * GetTableFormat()
Definition: edws.cxx:182
virtual void CalcAndSetScale(svt::EmbeddedObjectRef &xObj, const SwRect *pFlyPrtRect=nullptr, const SwRect *pFlyFrameRect=nullptr, const bool bNoTextFramePrtAreaChanged=false) override
Client for OleObject has to be up-to-date regarding scaling.
Definition: wrtsh1.cxx:644
virtual VclPtr< VclAbstractDialog > CreateSwTableWidthDlg(weld::Window *pParent, SwTableFUNC &rFnc)=0
SwMoveFnCollection const & fnTableStart
Definition: paminit.cxx:52
SwWrtShell * pSh
Definition: tablemgr.hxx:45
bool MoveTable(SwWhichTable, SwMoveFnCollection const &)
Definition: trvltbl.cxx:685
sal_uInt16 GetCurColNum() const
Definition: tablemgr.cxx:327
void ColWidthDlg(weld::Window *pParent)
Definition: tablemgr.cxx:54
#define SO3_SCH_CLASSID
bool Down(bool bSelect, sal_uInt16 nCount=1, bool bBasicCall=false)
Definition: move.cxx:151
ErrCode DoVerb(long nVerb)
OString const aName
SwTabCols aCols
Definition: tablemgr.hxx:46
void StartAllAction()
For all views of this document.
Definition: edws.cxx:85
void SetRight(long nNew)
Definition: tabcol.hxx:83
sal_Int32 nPos
size_t GetCurTabColNum() const
Definition: fetab.cxx:1326
void SetColWidth(sal_uInt16 nNum, SwTwips nWidth)
Definition: tablemgr.cxx:122
long GetLeft() const
Definition: tabcol.hxx:77
size_t Count() const
Definition: tabcol.hxx:64
typedef void(CALLTYPE *GetFuncDataPtr)(sal_uInt16 &nNo