xref: /core/sw/source/core/edit/edtab.cxx (revision 12e48f91)
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 <fesh.hxx>
21 #include <hintids.hxx>
22 #include <hints.hxx>
23 
24 #include <swwait.hxx>
25 #include <editsh.hxx>
26 #include <doc.hxx>
27 #include <IDocumentUndoRedo.hxx>
28 #include <IDocumentChartDataProviderAccess.hxx>
29 #include <IDocumentFieldsAccess.hxx>
30 #include <IDocumentState.hxx>
31 #include <cntfrm.hxx>
32 #include <pam.hxx>
33 #include <ndtxt.hxx>
34 #include <swtable.hxx>
35 #include <swundo.hxx>
36 #include <tblsel.hxx>
37 #include <cellfrm.hxx>
38 #include <cellatr.hxx>
39 #include <swtblfmt.hxx>
40 #include <swddetbl.hxx>
41 #include <mdiexp.hxx>
42 #include <itabenum.hxx>
43 #include <svl/numformat.hxx>
44 #include <vcl/uitest/logger.hxx>
45 #include <vcl/uitest/eventdescription.hxx>
46 
47 using namespace ::com::sun::star;
48 namespace {
49 
collectUIInformation(const OUString & rAction,const OUString & aParameters)50 void collectUIInformation(const OUString& rAction, const OUString& aParameters)
51 {
52     EventDescription aDescription;
53     aDescription.aAction = rAction;
54     aDescription.aParameters = {{"parameters", aParameters}};
55     aDescription.aID = "writer_edit";
56     aDescription.aKeyWord = "SwEditWinUIObject";
57     aDescription.aParent = "MainWindow";
58     UITestLogger::getInstance().logEvent(aDescription);
59 }
60 
61 }
62 
63 //Added for bug #i119954# Application crashed if undo/redo convert nest table to text
64 static bool ConvertTableToText( const SwTableNode *pTableNode, sal_Unicode cCh );
65 
ConvertNestedTablesToText(const SwTableLines & rTableLines,sal_Unicode cCh)66 static void    ConvertNestedTablesToText( const SwTableLines &rTableLines, sal_Unicode cCh )
67 {
68     for (size_t n = 0; n < rTableLines.size(); ++n)
69     {
70         SwTableLine* pTableLine = rTableLines[ n ];
71         for (size_t i = 0; i < pTableLine->GetTabBoxes().size(); ++i)
72         {
73             SwTableBox* pTableBox = pTableLine->GetTabBoxes()[ i ];
74             if (pTableBox->GetTabLines().empty())
75             {
76                 SwNodeIndex nodeIndex( *pTableBox->GetSttNd(), 1 );
77                 SwNodeIndex endNodeIndex( *pTableBox->GetSttNd()->EndOfSectionNode() );
78                 for( ; nodeIndex < endNodeIndex ; ++nodeIndex )
79                 {
80                     if ( SwTableNode* pTableNode = nodeIndex.GetNode().GetTableNode() )
81                         ConvertTableToText( pTableNode, cCh );
82                 }
83             }
84             else
85             {
86                 ConvertNestedTablesToText( pTableBox->GetTabLines(), cCh );
87             }
88         }
89     }
90 }
91 
ConvertTableToText(const SwTableNode * pConstTableNode,sal_Unicode cCh)92 bool ConvertTableToText( const SwTableNode *pConstTableNode, sal_Unicode cCh )
93 {
94     SwTableNode *pTableNode = const_cast< SwTableNode* >( pConstTableNode );
95     ConvertNestedTablesToText( pTableNode->GetTable().GetTabLines(), cCh );
96     return pTableNode->GetDoc().TableToText( pTableNode, cCh );
97 }
98 //End for bug #i119954#
99 
InsertTable(const SwInsertTableOptions & rInsTableOpts,sal_uInt16 nRows,sal_uInt16 nCols,const SwTableAutoFormat * pTAFormat)100 const SwTable& SwEditShell::InsertTable( const SwInsertTableOptions& rInsTableOpts,
101                                          sal_uInt16 nRows, sal_uInt16 nCols,
102                                          const SwTableAutoFormat* pTAFormat )
103 {
104     StartAllAction();
105     SwPosition* pPos = GetCursor()->GetPoint();
106 
107     bool bEndUndo = 0 != pPos->GetContentIndex();
108     if( bEndUndo )
109     {
110         StartUndo( SwUndoId::START );
111         GetDoc()->getIDocumentContentOperations().SplitNode( *pPos, false );
112     }
113 
114     // If called from a shell the adjust item is propagated
115     // from pPos to the new content nodes in the table.
116     const SwTable *pTable = GetDoc()->InsertTable( rInsTableOpts, *pPos,
117                                                    nRows, nCols,
118                                                    css::text::HoriOrientation::FULL, pTAFormat,
119                                                    nullptr, true );
120     if( bEndUndo )
121         EndUndo( SwUndoId::END );
122 
123     EndAllAction();
124 
125     OUString parameter = " Columns : " + OUString::number( nCols ) + " , Rows : " + OUString::number( nRows ) + " ";
126     collectUIInformation(u"CREATE_TABLE"_ustr, parameter);
127 
128     return *pTable;
129 }
130 
TextToTable(const SwInsertTableOptions & rInsTableOpts,sal_Unicode cCh,const SwTableAutoFormat * pTAFormat)131 bool SwEditShell::TextToTable( const SwInsertTableOptions& rInsTableOpts,
132                                sal_Unicode cCh,
133                                const SwTableAutoFormat* pTAFormat )
134 {
135     SwWait aWait( *GetDoc()->GetDocShell(), true );
136     bool bRet = false;
137     StartAllAction();
138     for(const SwPaM& rPaM : GetCursor()->GetRingContainer())
139     {
140         if( rPaM.HasMark() )
141             bRet |= nullptr != GetDoc()->TextToTable( rInsTableOpts, rPaM, cCh,
142                                                 css::text::HoriOrientation::FULL, pTAFormat );
143     }
144     EndAllAction();
145     return bRet;
146 }
147 
TableToText(sal_Unicode cCh)148 bool SwEditShell::TableToText( sal_Unicode cCh )
149 {
150     SwWait aWait( *GetDoc()->GetDocShell(), true );
151     SwPaM* pCursor = GetCursor();
152     const SwTableNode* pTableNd =
153             SwDoc::IsInTable( pCursor->GetPoint()->GetNode() );
154     if (!pTableNd)
155         return false;
156 
157     if( IsTableMode() )
158     {
159         ClearMark();
160         pCursor = GetCursor();
161     }
162     else if (pCursor->GetNext() != pCursor)
163         return false;
164 
165     // TL_CHART2:
166     // tell the charts about the table to be deleted and have them use their own data
167     GetDoc()->getIDocumentChartDataProviderAccess().CreateChartInternalDataProviders( &pTableNd->GetTable() );
168 
169     StartAllAction();
170 
171     // move current Cursor out of the listing area
172     SwNodeIndex aTabIdx( *pTableNd );
173     pCursor->DeleteMark();
174     pCursor->GetPoint()->Assign(*pTableNd->EndOfSectionNode());
175     // move sPoint and Mark out of the area!
176     pCursor->SetMark();
177     pCursor->DeleteMark();
178 
179     //Modified for bug #i119954# Application crashed if undo/redo convert nest table to text
180     StartUndo();
181     bool bRet = ConvertTableToText( pTableNd, cCh );
182     EndUndo();
183     //End  for bug #i119954#
184     pCursor->GetPoint()->Assign(aTabIdx);
185 
186     SwContentNode* pCNd = pCursor->GetPointContentNode();
187     if( !pCNd )
188         pCursor->Move( fnMoveForward, GoInContent );
189 
190     EndAllAction();
191     return bRet;
192 }
193 
IsTextToTableAvailable() const194 bool SwEditShell::IsTextToTableAvailable() const
195 {
196     bool bOnlyText = false;
197     for(SwPaM& rPaM : GetCursor()->GetRingContainer())
198     {
199         if( rPaM.HasMark() && *rPaM.GetPoint() != *rPaM.GetMark() )
200         {
201             bOnlyText = true;
202 
203             // check if selection is in listing
204             SwNodeOffset nStt = rPaM.Start()->GetNodeIndex(),
205                          nEnd = rPaM.End()->GetNodeIndex();
206 
207             for( ; nStt <= nEnd; ++nStt )
208                 if( !GetDoc()->GetNodes()[ nStt ]->IsTextNode() )
209                 {
210                     bOnlyText = false;
211                     break;
212                 }
213 
214             if( !bOnlyText )
215                 break;
216         }
217     }
218 
219     return bOnlyText;
220 }
221 
InsertDDETable(const SwInsertTableOptions & rInsTableOpts,SwDDEFieldType * pDDEType,sal_uInt16 nRows,sal_uInt16 nCols)222 void SwEditShell::InsertDDETable( const SwInsertTableOptions& rInsTableOpts,
223                                   SwDDEFieldType* pDDEType,
224                                   sal_uInt16 nRows, sal_uInt16 nCols )
225 {
226     SwPosition* pPos = GetCursor()->GetPoint();
227     // Do not try to insert table into Footnotes/Endnotes! tdf#76007 prevents that.
228     if (pPos->GetNode() < pPos->GetNodes().GetEndOfInserts()
229         && pPos->GetNode().GetIndex() >= pPos->GetNodes().GetEndOfInserts().StartOfSectionIndex())
230         return;
231 
232     StartAllAction();
233 
234     bool bEndUndo = 0 != pPos->GetContentIndex();
235     if( bEndUndo )
236     {
237         StartUndo( SwUndoId::START );
238         GetDoc()->getIDocumentContentOperations().SplitNode( *pPos, false );
239     }
240 
241     const SwInsertTableOptions aInsTableOpts( rInsTableOpts.mnInsMode | SwInsertTableFlags::DefaultBorder,
242                                             rInsTableOpts.mnRowsToRepeat );
243     SwTable* pTable = const_cast<SwTable*>(GetDoc()->InsertTable( aInsTableOpts, *pPos,
244                                                      nRows, nCols, css::text::HoriOrientation::FULL ));
245 
246     SwTableNode* pTableNode = const_cast<SwTableNode*>(pTable->GetTabSortBoxes()[ 0 ]->
247                                                 GetSttNd()->FindTableNode());
248     std::unique_ptr<SwDDETable> pDDETable(new SwDDETable( *pTable, pDDEType ));
249     pTableNode->SetNewTable( std::move(pDDETable) );   // set the DDE table
250 
251     if( bEndUndo )
252         EndUndo( SwUndoId::END );
253 
254     EndAllAction();
255 }
256 
257 /** update fields of a listing */
UpdateTable()258 void SwEditShell::UpdateTable()
259 {
260     const SwTableNode* pTableNd = IsCursorInTable();
261 
262     if( pTableNd )
263     {
264         StartAllAction();
265         if( DoesUndo() )
266             StartUndo();
267         EndAllTableBoxEdit();
268         GetDoc()->getIDocumentFieldsAccess().UpdateTableFields(&pTableNd->GetTable());
269         if( DoesUndo() )
270             EndUndo();
271         EndAllAction();
272     }
273 }
274 
275 // get/set Change Mode
276 
GetTableChgMode() const277 TableChgMode SwEditShell::GetTableChgMode() const
278 {
279     TableChgMode eMode;
280     const SwTableNode* pTableNd = IsCursorInTable();
281     if( pTableNd )
282         eMode = pTableNd->GetTable().GetTableChgMode();
283     else
284         eMode = GetTableChgDefaultMode();
285     return eMode;
286 }
287 
SetTableChgMode(TableChgMode eMode)288 void SwEditShell::SetTableChgMode( TableChgMode eMode )
289 {
290     const SwTableNode* pTableNd = IsCursorInTable();
291 
292     if( pTableNd )
293     {
294         const_cast<SwTable&>(pTableNd->GetTable()).SetTableChgMode( eMode );
295         if( !GetDoc()->getIDocumentState().IsModified() )   // Bug 57028
296         {
297             GetDoc()->GetIDocumentUndoRedo().SetUndoNoResetModified();
298         }
299         GetDoc()->getIDocumentState().SetModified();
300     }
301 }
302 
GetTableBoxFormulaAttrs(SfxItemSet & rSet) const303 bool SwEditShell::GetTableBoxFormulaAttrs( SfxItemSet& rSet ) const
304 {
305     SwSelBoxes aBoxes;
306     if( IsTableMode() )
307         ::GetTableSelCrs( *this, aBoxes );
308     else
309     {
310         SwFrame* pFrame = GetCurrFrame()->GetUpper();
311         while (pFrame && !pFrame->IsCellFrame())
312             pFrame = pFrame->GetUpper();
313 
314         if (pFrame)
315         {
316             auto pBox = const_cast<SwTableBox*>(static_cast<SwCellFrame*>(pFrame)->GetTabBox());
317             aBoxes.insert(pBox);
318         }
319     }
320 
321     for (size_t n = 0; n < aBoxes.size(); ++n)
322     {
323         const SwTableBox* pSelBox = aBoxes[ n ];
324         const SwTableBoxFormat* pTableFormat = static_cast<SwTableBoxFormat*>(pSelBox->GetFrameFormat());
325         if( !n )
326         {
327             // Convert formulae into external presentation
328             const SwTable& rTable = pSelBox->GetSttNd()->FindTableNode()->GetTable();
329 
330             const_cast<SwTable*>(&rTable)->SwitchFormulasToExternalRepresentation();
331             rSet.Put( pTableFormat->GetAttrSet() );
332         }
333         else
334             rSet.MergeValues( pTableFormat->GetAttrSet() );
335     }
336     return 0 != rSet.Count();
337 }
338 
SetTableBoxFormulaAttrs(const SfxItemSet & rSet)339 void SwEditShell::SetTableBoxFormulaAttrs( const SfxItemSet& rSet )
340 {
341     CurrShell aCurr( this );
342     SwSelBoxes aBoxes;
343     if( IsTableMode() )
344         ::GetTableSelCrs( *this, aBoxes );
345     else
346     {
347         do {
348             SwFrame *pFrame = GetCurrFrame();
349             do {
350                 pFrame = pFrame->GetUpper();
351             } while ( pFrame && !pFrame->IsCellFrame() );
352             if ( pFrame )
353             {
354                 SwTableBox *pBox = const_cast<SwTableBox*>(static_cast<SwCellFrame*>(pFrame)->GetTabBox());
355                 aBoxes.insert( pBox );
356             }
357         } while( false );
358     }
359 
360     // When setting a formula, do not check further!
361     if( SfxItemState::SET == rSet.GetItemState( RES_BOXATR_FORMULA ))
362         ClearTableBoxContent();
363 
364     StartAllAction();
365     GetDoc()->GetIDocumentUndoRedo().StartUndo( SwUndoId::START, nullptr );
366     for (size_t n = 0; n < aBoxes.size(); ++n)
367     {
368         GetDoc()->SetTableBoxFormulaAttrs( *aBoxes[ n ], rSet );
369     }
370     GetDoc()->GetIDocumentUndoRedo().EndUndo( SwUndoId::END, nullptr );
371     EndAllAction();
372 }
373 
IsTableBoxTextFormat() const374 bool SwEditShell::IsTableBoxTextFormat() const
375 {
376     if( IsTableMode() )
377         return false;
378 
379     const SwTableBox *pBox = nullptr;
380     {
381         SwFrame *pFrame = GetCurrFrame();
382         do {
383             pFrame = pFrame->GetUpper();
384         } while ( pFrame && !pFrame->IsCellFrame() );
385         if ( pFrame )
386             pBox = static_cast<SwCellFrame*>(pFrame)->GetTabBox();
387     }
388 
389     if( !pBox )
390         return false;
391 
392     sal_uInt32 nFormat = 0;
393     if( const SwTableBoxNumFormat* pItem = pBox->GetFrameFormat()->GetAttrSet().GetItemIfSet(
394         RES_BOXATR_FORMAT ))
395     {
396         nFormat = pItem->GetValue();
397         return GetDoc()->GetNumberFormatter()->IsTextFormat( nFormat );
398     }
399 
400     SwNodeOffset nNd = pBox->IsValidNumTextNd();
401     if( NODE_OFFSET_MAX == nNd )
402         return true;
403 
404     const OUString& rText = GetDoc()->GetNodes()[ nNd ]->GetTextNode()->GetText();
405     if( rText.isEmpty() )
406         return false;
407 
408     double fVal;
409     return !GetDoc()->IsNumberFormat( rText, nFormat, fVal );
410 }
411 
GetTableBoxText() const412 OUString SwEditShell::GetTableBoxText() const
413 {
414     OUString sRet;
415     if( !IsTableMode() )
416     {
417         const SwTableBox *pBox = nullptr;
418         {
419             SwFrame *pFrame = GetCurrFrame();
420             do {
421                 pFrame = pFrame->GetUpper();
422             } while ( pFrame && !pFrame->IsCellFrame() );
423             if ( pFrame )
424                 pBox = static_cast<SwCellFrame*>(pFrame)->GetTabBox();
425         }
426 
427         SwNodeOffset nNd;
428         if( pBox && NODE_OFFSET_MAX != ( nNd = pBox->IsValidNumTextNd() ) )
429             sRet = GetDoc()->GetNodes()[ nNd ]->GetTextNode()->GetText();
430     }
431     return sRet;
432 }
433 
SplitTable(SplitTable_HeadlineOption eMode)434 void SwEditShell::SplitTable( SplitTable_HeadlineOption eMode )
435 {
436     SwPaM *pCursor = GetCursor();
437     if( pCursor->GetPointNode().FindTableNode() )
438     {
439         StartAllAction();
440         GetDoc()->GetIDocumentUndoRedo().StartUndo(SwUndoId::EMPTY, nullptr);
441 
442         GetDoc()->SplitTable( *pCursor->GetPoint(), eMode, true );
443 
444         GetDoc()->GetIDocumentUndoRedo().EndUndo(SwUndoId::EMPTY, nullptr);
445         ClearFEShellTabCols(*GetDoc(), nullptr);
446         EndAllAction();
447     }
448 }
449 
MergeTable(bool bWithPrev)450 bool SwEditShell::MergeTable( bool bWithPrev )
451 {
452     bool bRet = false;
453     SwPaM *pCursor = GetCursor();
454     if( pCursor->GetPointNode().FindTableNode() )
455     {
456         StartAllAction();
457         GetDoc()->GetIDocumentUndoRedo().StartUndo(SwUndoId::EMPTY, nullptr);
458 
459         bRet = GetDoc()->MergeTable( *pCursor->GetPoint(), bWithPrev );
460 
461         GetDoc()->GetIDocumentUndoRedo().EndUndo(SwUndoId::EMPTY, nullptr);
462         ClearFEShellTabCols(*GetDoc(), nullptr);
463         EndAllAction();
464     }
465     return bRet;
466 }
467 
CanMergeTable(bool bWithPrev,bool * pChkNxtPrv) const468 bool SwEditShell::CanMergeTable( bool bWithPrev, bool* pChkNxtPrv ) const
469 {
470     bool bRet = false;
471     const SwPaM *pCursor = GetCursor();
472     const SwTableNode* pTableNd = pCursor->GetPointNode().FindTableNode();
473     if( pTableNd && dynamic_cast< const SwDDETable* >(&pTableNd->GetTable()) ==  nullptr)
474     {
475         bool bNew = pTableNd->GetTable().IsNewModel();
476         const SwNodes& rNds = GetDoc()->GetNodes();
477         if( pChkNxtPrv )
478         {
479             const SwTableNode* pChkNd = rNds[ pTableNd->GetIndex() - 1 ]->FindTableNode();
480             if( pChkNd && dynamic_cast< const SwDDETable* >(&pChkNd->GetTable()) ==  nullptr &&
481                 bNew == pChkNd->GetTable().IsNewModel() &&
482                 // Consider table in table case
483                 pChkNd->EndOfSectionIndex() == pTableNd->GetIndex() - 1 )
484             {
485                 *pChkNxtPrv = true;
486                 bRet = true;        // using Prev is possible
487             }
488             else
489             {
490                 pChkNd = rNds[ pTableNd->EndOfSectionIndex() + 1 ]->GetTableNode();
491                 if( pChkNd && dynamic_cast< const SwDDETable* >(&pChkNd->GetTable()) ==  nullptr &&
492                     bNew == pChkNd->GetTable().IsNewModel() )
493                 {
494                     *pChkNxtPrv = false;
495                     bRet = true;   // using Next is possible
496                 }
497             }
498         }
499         else
500         {
501             const SwTableNode* pTmpTableNd = nullptr;
502 
503             if( bWithPrev )
504             {
505                 pTmpTableNd = rNds[ pTableNd->GetIndex() - 1 ]->FindTableNode();
506                 // Consider table in table case
507                 if ( pTmpTableNd && pTmpTableNd->EndOfSectionIndex() != pTableNd->GetIndex() - 1 )
508                     pTmpTableNd = nullptr;
509             }
510             else
511                 pTmpTableNd = rNds[ pTableNd->EndOfSectionIndex() + 1 ]->GetTableNode();
512 
513             bRet = pTmpTableNd && dynamic_cast< const SwDDETable* >(&pTmpTableNd->GetTable()) ==  nullptr &&
514                    bNew == pTmpTableNd->GetTable().IsNewModel();
515         }
516     }
517     return bRet;
518 }
519 
520 /** create InsertDB as table Undo */
AppendUndoForInsertFromDB(bool bIsTable)521 void SwEditShell::AppendUndoForInsertFromDB( bool bIsTable )
522 {
523     GetDoc()->AppendUndoForInsertFromDB( *GetCursor(), bIsTable );
524 }
525 
526 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
527