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