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