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/XEmbeddedObject.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( m_aCols.Count() > 0 )
63  {
64  if(m_aCols.Count() == GetColCount())
65  {
66  if(nNum == m_aCols.Count())
67  nWidth = m_aCols.GetRight() - m_aCols[nNum-1];
68  else
69  {
70  if(nNum == 0)
71  nWidth = m_aCols[nNum] - m_aCols.GetLeft();
72  else
73  nWidth = m_aCols[nNum] - m_aCols[nNum-1];
74  }
75  }
76  else
77  {
78  SwTwips nRValid = nNum < GetColCount() ?
79  m_aCols[GetRightSeparator(nNum)] :
80  m_aCols.GetRight();
81  SwTwips nLValid = nNum ?
82  m_aCols[GetRightSeparator(nNum - 1)] :
83  m_aCols.GetLeft();
84  nWidth = nRValid - nLValid;
85  }
86  }
87  else
88  nWidth = m_aCols.GetRight();
89  return nWidth;
90 }
91 
92 SwTwips SwTableFUNC::GetMaxColWidth( sal_uInt16 nNum ) const
93 {
94  OSL_ENSURE(nNum <= m_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 ( m_aCols.Count() > 0 )
124  {
125  if(m_aCols.Count() != GetColCount())
126  bCurrentOnly = true;
127  SwTwips nWidth = GetColWidth(nNum);
128 
129  int nDiff = static_cast<int>(nNewWidth - nWidth);
130  if( !nNum )
131  m_aCols[ GetRightSeparator(0) ] += nDiff;
132  else if( nNum < GetColCount() )
133  {
134  if(nDiff < GetColWidth(nNum + 1) - MINLAY)
135  m_aCols[ GetRightSeparator(nNum) ] += nDiff;
136  else
137  {
138  int nDiffLeft = nDiff - static_cast<int>(GetColWidth(nNum + 1)) + int(MINLAY);
139  m_aCols[ GetRightSeparator(nNum) ] += (nDiff - nDiffLeft);
140  m_aCols[ GetRightSeparator(nNum - 1) ] -= nDiffLeft;
141  }
142  }
143  else
144  m_aCols[ GetRightSeparator(nNum-1) ] -= nDiff;
145  }
146  else
147  m_aCols.SetRight( std::min( nNewWidth, SwTwips(m_aCols.GetRightMax()) ) );
148 
150  m_pSh->SetTabCols( m_aCols, bCurrentOnly );
151  m_pSh->EndAllAction();
152 }
153 
155 {
156  OSL_ENSURE(m_pSh, "no Shell");
157 
158  if( m_pFormat && m_pSh)
160 }
161 
163  : m_pFormat(pShell->GetTableFormat()),
164  m_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 = m_pSh->GetTableFormat();
177  if ( pFormat2 && m_pSh->HasOLEObj( pFormat2->GetName() ) )
178  {
180  m_pSh->UpdateCharts( pFormat2->GetName() );
181  m_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;
194 
195  OUString aName;
196  if (m_pSh->IsCursorInTable())
197  {
198  aName = m_pSh->GetTableFormat()->GetName();
199  // insert node before table
201  m_pSh->Up( false );
202  if ( m_pSh->IsCursorInTable() )
203  {
204  if ( aName != m_pSh->GetTableFormat()->GetName() )
205  m_pSh->Down( false ); // two adjacent tables
206  }
207  m_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  m_pSh->InsertOleObject( aEmbObjRef, &pTmp );
222  if (ppFlyFrameFormat)
223  *ppFlyFrameFormat = pTmp;
224 
225  xChartModel.set( xObj->getComponent(), uno::UNO_QUERY );
226  if( xChartModel.is() )
227  {
228  // Create a default chart type.
229  uno::Reference<chart2::XChartDocument> xChartDoc(xChartModel, uno::UNO_QUERY);
230  if (xChartDoc.is())
231  xChartDoc->createDefaultChart();
232 
233  xChartModel->lockControllers(); //#i79578# don't request a new replacement image for charts to often - block change notifications
234  }
235 
236  // set the table name at the OLE-node
237  if (!aName.isEmpty())
238  m_pSh->SetChartName( aName );
239  }
240  m_pSh->EndAllAction();
241 
242  if (xObj.is() && !comphelper::LibreOfficeKit::isActive())
243  {
244  // Let the chart be activated after the inserting (unless
245  // via LibreOfficeKit)
246  SfxInPlaceClient* pClient = m_pSh->GetView().FindIPClient( xObj, &m_pSh->GetView().GetEditWin() );
247  if ( !pClient )
248  {
249  pClient = new SwOleClient( &m_pSh->GetView(), &m_pSh->GetView().GetEditWin(), aEmbObjRef );
251  }
252  m_pSh->CalcAndSetScale( aEmbObjRef );
253  //#50270# We don't need to handle errors,
254  //this does the DoVerb in the SfxViewShell.
255  ErrCode nErr = pClient->DoVerb(embed::EmbedVerbs::MS_OLEVERB_SHOW);
256  (void) nErr;
257 
258  // #i121334#
260  }
261 
262  uno::Reference< chart2::data::XDataReceiver > xDataReceiver( xChartModel, uno::UNO_QUERY );
263  if (bFillWithData && xDataReceiver.is() && rxDataProvider.is())
264  {
265  xDataReceiver->attachDataProvider( rxDataProvider );
266 
267  uno::Reference< util::XNumberFormatsSupplier > xNumberFormatsSupplier( m_pSh->GetView().GetDocShell()->GetModel(), uno::UNO_QUERY );
268  xDataReceiver->attachNumberFormatsSupplier( xNumberFormatsSupplier );
269 
270  // default values for ranges that do not consist of a single row or column
271  bool bHasCategories = true;
272  bool bFirstCellAsLabel = true;
273  chart::ChartDataRowSource eDataRowSource = chart::ChartDataRowSource_COLUMNS;
274 
275  SwRangeDescriptor aDesc;
276  FillRangeDescriptor( aDesc, rCellRange );
277  bool bSingleRowCol = aDesc.nTop == aDesc.nBottom || aDesc.nLeft == aDesc.nRight;
278  if (bSingleRowCol)
279  {
280  aDesc.Normalize();
281  sal_Int32 nRowLen = aDesc.nRight - aDesc.nLeft + 1;
282  sal_Int32 nColLen = aDesc.nBottom - aDesc.nTop + 1;
283 
284  bHasCategories = false;
285  if (nRowLen == 1 && nColLen == 1)
286  bFirstCellAsLabel = false;
287  else if (nRowLen > 1)
288  eDataRowSource = chart::ChartDataRowSource_ROWS;
289  else if (nColLen > 1)
290  eDataRowSource = chart::ChartDataRowSource_COLUMNS;
291  else {
292  OSL_FAIL("unexpected state" );
293  }
294  }
295 
296  uno::Sequence< beans::PropertyValue > aArgs{
297  beans::PropertyValue(
298  "CellRangeRepresentation", -1,
299  uno::makeAny( rCellRange ), beans::PropertyState_DIRECT_VALUE ),
300  beans::PropertyValue(
301  "HasCategories", -1,
302  uno::makeAny( bHasCategories ), beans::PropertyState_DIRECT_VALUE ),
303  beans::PropertyValue(
304  "FirstCellAsLabel", -1,
305  uno::makeAny( bFirstCellAsLabel ), beans::PropertyState_DIRECT_VALUE ),
306  beans::PropertyValue(
307  "DataRowSource", -1,
308  uno::makeAny( eDataRowSource ), beans::PropertyState_DIRECT_VALUE )
309  };
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 = m_pSh->GetCurTabColNum();
323  size_t nCount = 0;
324  for( size_t i = 0; i < nPos; i++ )
325  if(m_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 < m_aCols.Count(); i++ )
334  if(m_aCols.IsHidden(i))
335  nCount ++;
336  return m_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( !m_aCols.IsHidden(i) )
346  nNum--;
347  i++;
348  }
349  return i - 1;
350 }
351 
352 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SwNodeOffset min(const SwNodeOffset &a, const SwNodeOffset &b)
Definition: nodeoffset.hxx:35
void Normalize()
Definition: unotbl.cxx:537
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:1139
SwTwips GetColWidth(sal_uInt16 nNum) const
Definition: tablemgr.cxx:58
SwTabCols m_aCols
Definition: tablemgr.hxx:46
void UpdateChart()
Definition: tablemgr.cxx:172
void SetRight(tools::Long nNew)
Definition: tabcol.hxx:84
#define MINLAY
Definition: swtypes.hxx:63
tools::Long GetLeft() const
Definition: tabcol.hxx:78
tools::Long GetRightMax() const
Definition: tabcol.hxx:80
css::uno::Reference< css::frame::XModel3 > GetModel() const
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:185
sal_Int32 nLeft
Definition: unotbl.hxx:247
SwEditWin & GetEditWin()
Definition: view.hxx:416
ErrCode DoVerb(sal_Int32 nVerb)
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:491
void EndAllAction()
Definition: edws.cxx:97
bool GotoCurrTable(SwPaM &rCurrentCursor, SwMoveFnCollection const &fnPosTable, bool bInReadOnly)
Definition: trvltbl.cxx:633
Used by the UI to modify the document model.
Definition: wrtsh.hxx:93
const SwView & GetView() const
Definition: wrtsh.hxx:431
sal_Int32 nBottom
Definition: unotbl.hxx:248
const OUString & GetName() const
Definition: format.hxx:115
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:172
sal_Int32 nRight
Definition: unotbl.hxx:249
void InitTabCols()
Definition: tablemgr.cxx:154
bool IsHidden(size_t nPos) const
Definition: tabcol.hxx:67
void UpdateCharts(const OUString &rName)
Update content of all charts for table with given name.
Definition: editsh.cxx:399
void SetChartName(const OUString &rName)
Definition: editsh.cxx:392
const SwTableNode * IsCursorInTable() const
Definition: crsrsh.hxx:887
SwTableFUNC(SwWrtShell *pShell)
Definition: tablemgr.cxx:162
bool Up(bool bSelect, sal_uInt16 nCount=1, bool bBasicCall=false)
Definition: move.cxx:145
void SetTabCols(const SwTabCols &rNew, bool bCurRowOnly)
Definition: fetab.cxx:645
Style of a layout element.
Definition: frmfmt.hxx:59
sal_Int32 nTop
Definition: unotbl.hxx:246
int i
sal_uInt16 GetColCount() const
Definition: tablemgr.cxx:330
void SetCheckForOLEInCaption(bool bFlag)
Definition: fesh.hxx:459
SwUndoId StartUndo(SwUndoId eUndoId=SwUndoId::EMPTY, const SwRewriter *pRewriter=nullptr)
Undo: set up Undo parenthesis, return nUndoId of this parenthesis.
Definition: edws.cxx:223
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:663
SAL_DLLPRIVATE int GetRightSeparator(int nNum) const
Definition: tablemgr.cxx:339
tools::Long SwTwips
Definition: swtypes.hxx:52
SwWrtShell * m_pSh
Definition: tablemgr.hxx:45
SwDocShell * GetDocShell()
Definition: view.cxx:1135
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:691
virtual VclPtr< VclAbstractDialog > CreateSwTableWidthDlg(weld::Window *pParent, SwTableFUNC &rFnc)=0
SwMoveFnCollection const & fnTableStart
Definition: paminit.cxx:52
bool MoveTable(SwWhichTable, SwMoveFnCollection const &)
Definition: trvltbl.cxx:682
sal_uInt16 GetCurColNum() const
Definition: tablemgr.cxx:320
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:159
bool HasOLEObj(std::u16string_view rName) const
Is there an OLEObject with this name (SwFormat)?
Definition: editsh.cxx:374
void StartAllAction()
For all views of this document.
Definition: edws.cxx:86
tools::Long GetRight() const
Definition: tabcol.hxx:79
size_t GetCurTabColNum() const
Definition: fetab.cxx:1347
SwFrameFormat * m_pFormat
Definition: tablemgr.hxx:44
void SetColWidth(sal_uInt16 nNum, SwTwips nWidth)
Definition: tablemgr.cxx:117
sal_uInt16 nPos
size_t Count() const
Definition: tabcol.hxx:65
typedef void(CALLTYPE *GetFuncDataPtr)(sal_uInt16 &nNo