LibreOffice Module sw (master) 1
docsort.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 <hintids.hxx>
21#include <osl/diagnose.h>
25#include <docary.hxx>
26#include <fmtanchr.hxx>
27#include <frmfmt.hxx>
28#include <doc.hxx>
29#include <IDocumentUndoRedo.hxx>
32#include <IDocumentState.hxx>
33#include <node.hxx>
34#include <pam.hxx>
35#include <ndtxt.hxx>
36#include <swtable.hxx>
37#include <swundo.hxx>
38#include <sortopt.hxx>
39#include <docsort.hxx>
40#include <UndoSort.hxx>
41#include <UndoRedline.hxx>
42#include <hints.hxx>
43#include <tblsel.hxx>
44#include <cellatr.hxx>
45#include <redline.hxx>
46#include <node2lay.hxx>
47#include <frameformats.hxx>
48#include <svl/numformat.hxx>
49
50#include <set>
51#include <utility>
52
53using namespace ::com::sun::star::lang;
54using namespace ::com::sun::star;
55
57SwDoc* SwSortElement::pDoc = nullptr;
58const FlatFndBox* SwSortElement::pBox = nullptr;
60lang::Locale* SwSortElement::pLocale = nullptr;
61std::optional<OUString> SwSortElement::xLastAlgorithm;
63
64// List of all sorted elements
65
68 FlatFndBox const * pFltBx )
69{
70 OSL_ENSURE( !pDoc && !pOptions && !pBox, "Who forgot to call Finit?" );
71 pDoc = pD;
72 pOptions = new SwSortOptions( rOpt );
73 pBox = pFltBx;
74
76 if ( nLang.anyOf(
80 pLocale = new lang::Locale( LanguageTag::convertToLocale( nLang ) );
81
82 pSortCollator = new CollatorWrapper( ::comphelper::getProcessComponentContext() );
83}
84
86{
87 delete pOptions;
88 pOptions = nullptr;
89 delete pLocale;
90 pLocale = nullptr;
91 xLastAlgorithm.reset();
92 delete pSortCollator;
93 pSortCollator = nullptr;
94 delete pLclData;
95 pLclData = nullptr;
96 pDoc = nullptr;
97 pBox = nullptr;
98}
99
101{
102}
103
104double SwSortElement::StrToDouble( std::u16string_view rStr )
105{
106 if( !pLclData )
108
109 rtl_math_ConversionStatus eStatus;
110 sal_Int32 nEnd;
111 double nRet = pLclData->stringToDouble( rStr, true, &eStatus, &nEnd );
112
113 if( rtl_math_ConversionStatus_Ok != eStatus || nEnd == 0 )
114 nRet = 0.0;
115 return nRet;
116}
117
118int SwSortElement::keycompare(const SwSortElement& rCmp, sal_uInt16 nKey) const
119{
120 int nCmp = 0;
121 // The actual comparison
122 const SwSortElement *pOrig, *pCmp;
123
124 const SwSortKey& rSrtKey = pOptions->aKeys[ nKey ];
125 if( rSrtKey.eSortOrder == SwSortOrder::Ascending )
126 {
127 pOrig = this;
128 pCmp = &rCmp;
129 }
130 else
131 {
132 pOrig = &rCmp;
133 pCmp = this;
134 }
135
136 if( rSrtKey.bIsNumeric )
137 {
138 double n1 = pOrig->GetValue( nKey );
139 double n2 = pCmp->GetValue( nKey );
140
141 nCmp = n1 < n2 ? -1 : n1 == n2 ? 0 : 1;
142 }
143 else
144 {
145 if( !xLastAlgorithm || *xLastAlgorithm != rSrtKey.sSortType )
146 {
147 xLastAlgorithm = rSrtKey.sSortType;
149 *pLocale,
151 }
152
154 pOrig->GetKey( nKey ), pCmp->GetKey( nKey ));
155 }
156 return nCmp;
157}
158
160{
161 // The actual comparison
162 for(size_t nKey = 0; nKey < pOptions->aKeys.size(); ++nKey)
163 {
164 int nCmp = keycompare(rCmp, nKey);
165
166 if (nCmp == 0)
167 continue;
168
169 return nCmp < 0;
170 }
171
172 return false;
173}
174
175double SwSortElement::GetValue( sal_uInt16 nKey ) const
176{
177 return StrToDouble( GetKey( nKey ));
178}
179
182 : nOrg(rPos.GetIndex()), aPos(rPos)
183{
184}
185
186OUString SwSortTextElement::GetKey(sal_uInt16 nId) const
187{
188 SwTextNode* pTextNd = aPos.GetNode().GetTextNode();
189 if( !pTextNd )
190 return OUString();
191
192 // for TextNodes
193 const OUString& rStr = pTextNd->GetText();
194
195 sal_Unicode nDeli = pOptions->cDeli;
196 sal_uInt16 nDCount = pOptions->aKeys[nId].nColumnId, i = 1;
197 sal_Int32 nStart = 0;
198
199 // Find the delimiter
200 while( nStart != -1 && i < nDCount)
201 {
202 nStart = rStr.indexOf( nDeli, nStart );
203 if( -1 != nStart )
204 {
205 nStart++;
206 i++;
207 }
208 }
209
210 // Found next delimiter or end of String
211 // and copy
212 sal_Int32 nEnd = rStr.indexOf( nDeli, nStart+1 );
213 if (nEnd == -1)
214 return rStr.copy( nStart );
215 return rStr.copy( nStart, nEnd-nStart );
216}
217
220 : nRow( nRC )
221{
222}
223
225OUString SwSortBoxElement::GetKey(sal_uInt16 nKey) const
226{
227 const FndBox_* pFndBox;
228 sal_uInt16 nCol = pOptions->aKeys[nKey].nColumnId-1;
229
231 pFndBox = pBox->GetBox(nCol, nRow); // Sort rows
232 else
233 pFndBox = pBox->GetBox(nRow, nCol); // Sort columns
234
235 // Extract the Text
236 OUStringBuffer aRetStr;
237 if( pFndBox )
238 { // Get StartNode and skip it
239 const SwTableBox* pMyBox = pFndBox->GetBox();
240 OSL_ENSURE(pMyBox, "No atomic Box");
241
242 if( pMyBox->GetSttNd() )
243 {
244 // Iterate over all the Box's TextNodes
245 const SwNode *pNd = nullptr, *pEndNd = pMyBox->GetSttNd()->EndOfSectionNode();
246 for( SwNodeOffset nIdx = pMyBox->GetSttIdx() + 1; pNd != pEndNd; ++nIdx )
247 {
248 pNd = pDoc->GetNodes()[ nIdx ];
249 if( pNd->IsTextNode() )
250 aRetStr.append(pNd->GetTextNode()->GetText());
251 }
252 }
253 }
254 return aRetStr.makeStringAndClear();
255}
256
257double SwSortBoxElement::GetValue( sal_uInt16 nKey ) const
258{
259 const FndBox_* pFndBox;
260 sal_uInt16 nCol = pOptions->aKeys[nKey].nColumnId-1;
261
263 pFndBox = pBox->GetBox(nCol, nRow); // Sort rows
264 else
265 pFndBox = pBox->GetBox(nRow, nCol); // Sort columns
266
267 double nVal;
268 if( pFndBox )
269 {
270 const SwFormat *pFormat = pFndBox->GetBox()->GetFrameFormat();
272 nVal = SwSortElement::GetValue( nKey );
273 else
274 nVal = pFormat->GetTableBoxValue().GetValue();
275 }
276 else
277 nVal = 0;
278
279 return nVal;
280}
281
283bool SwDoc::SortText(const SwPaM& rPaM, const SwSortOptions& rOpt)
284{
285 // Check if Frame is in the Text
286 auto [pStart, pEnd] = rPaM.StartEnd(); // SwPosition*
287
288 // Set index to the Selection's start
289 for ( const auto *pFormat : *GetSpzFrameFormats() )
290 {
291 SwFormatAnchor const*const pAnchor = &pFormat->GetAnchor();
292 SwNode const*const pAnchorNode = pAnchor->GetAnchorNode();
293
294 if (pAnchorNode && (RndStdIds::FLY_AT_PARA == pAnchor->GetAnchorId()) &&
295 pStart->GetNode() <= *pAnchorNode && *pAnchorNode <= pEnd->GetNode() )
296 return false;
297 }
298
299 // Check if only TextNodes are within the Selection
300 {
301 SwNodeOffset nStart = pStart->GetNodeIndex(),
302 nEnd = pEnd->GetNodeIndex();
303 while( nStart <= nEnd )
304 // Iterate over a selected range
305 if( !GetNodes()[ nStart++ ]->IsTextNode() )
306 return false;
307 }
308
309 bool const bUndo = GetIDocumentUndoRedo().DoesUndo();
310 if( bUndo )
311 {
312 GetIDocumentUndoRedo().StartUndo( SwUndoId::START, nullptr );
313 }
314
315 SwPaM* pRedlPam = nullptr;
316 SwUndoRedlineSort* pRedlUndo = nullptr;
317 SwUndoSort* pUndoSort = nullptr;
318
319 // To-Do - add 'SwExtraRedlineTable' also ?
320 if( getIDocumentRedlineAccess().IsRedlineOn() || (!getIDocumentRedlineAccess().IsIgnoreRedline() && !getIDocumentRedlineAccess().GetRedlineTable().empty() ))
321 {
322 pRedlPam = new SwPaM( pStart->GetNode(), pEnd->GetNode(), SwNodeOffset(-1), SwNodeOffset(1) );
323 SwContentNode* pCNd = pRedlPam->GetMarkContentNode();
324 if( pCNd )
325 pRedlPam->GetMark()->SetContent( pCNd->Len() );
326
328 {
329 if( bUndo )
330 {
331 pRedlUndo = new SwUndoRedlineSort( *pRedlPam,rOpt );
332 GetIDocumentUndoRedo().DoUndo(false);
333 }
334 // First copy the range
335 SwNodeIndex aEndIdx( pEnd->GetNode(), 1 );
336 SwNodeRange aRg( pStart->GetNode(), aEndIdx.GetNode() );
337 GetNodes().Copy_( aRg, aEndIdx.GetNode() );
338
339 // range is new from pEnd->nNode+1 to aEndIdx
340 getIDocumentRedlineAccess().DeleteRedline( *pRedlPam, true, RedlineType::Any );
341
342 pRedlPam->GetMark()->Assign( pEnd->GetNode(), SwNodeOffset(1) );
343
344 pRedlPam->GetPoint()->Assign( aEndIdx.GetNode() );
345 pCNd = pRedlPam->GetPointContentNode();
346 sal_Int32 nCLen = 0;
347 if( !pCNd )
348 {
349 pCNd = GetNodes()[ aEndIdx.GetIndex()-SwNodeOffset(1) ]->GetContentNode();
350 if( pCNd )
351 {
352 nCLen = pCNd->Len();
353 pRedlPam->GetPoint()->Assign( *pCNd );
354 }
355 }
356 if (pCNd)
357 pRedlPam->GetPoint()->SetContent( nCLen );
358
359 if( pRedlUndo )
360 pRedlUndo->SetValues( rPaM );
361 }
362 else
363 {
364 getIDocumentRedlineAccess().DeleteRedline( *pRedlPam, true, RedlineType::Any );
365 delete pRedlPam;
366 pRedlPam = nullptr;
367 }
368 }
369
370 SwNodeIndex aStart(pStart->GetNode());
371 SwSortElement::Init( this, rOpt );
372 std::multiset<SwSortTextElement> aSortSet;
373 while( aStart <= pEnd->GetNode() )
374 {
375 // Iterate over a selected range
376 aSortSet.insert(SwSortTextElement(aStart));
377 ++aStart;
378 }
379
380 // Now comes the tricky part: Move Nodes (and always keep Undo in mind)
381 SwNodeOffset nBeg = pStart->GetNodeIndex();
382 SwNodeRange aRg( aStart, aStart );
383
384 if( bUndo && !pRedlUndo )
385 {
386 pUndoSort = new SwUndoSort(rPaM, rOpt);
387 GetIDocumentUndoRedo().AppendUndo(std::unique_ptr<SwUndo>(pUndoSort));
388 }
389
390 GetIDocumentUndoRedo().DoUndo(false);
391
392 SwNodeOffset n(0);
393 for (const auto& rElem : aSortSet)
394 {
395 aStart = nBeg + n;
396 aRg.aStart = rElem.aPos.GetIndex();
397 aRg.aEnd = aRg.aStart.GetIndex() + 1;
398
399 // Move Nodes
402
403 // Insert Move in Undo
404 if(pUndoSort)
405 {
406 pUndoSort->Insert(rElem.nOrg, nBeg + n);
407 }
408 ++n;
409 }
410 // Delete all elements from the SortArray
411 aSortSet.clear();
413
414 if( pRedlPam )
415 {
416 if( pRedlUndo )
417 {
418 pRedlUndo->SetSaveRange( *pRedlPam );
419 // UGLY: temp. enable Undo
420 GetIDocumentUndoRedo().DoUndo(true);
421 GetIDocumentUndoRedo().AppendUndo( std::unique_ptr<SwUndo>(pRedlUndo) );
422 GetIDocumentUndoRedo().DoUndo(false);
423 }
424
425 // nBeg is start of sorted range
426 SwNodeIndex aSttIdx( GetNodes(), nBeg );
427
428 // the copied range is deleted
429 SwRangeRedline *const pDeleteRedline(
430 new SwRangeRedline( RedlineType::Delete, *pRedlPam ));
431
432 // pRedlPam points to nodes that may be deleted (hidden) by
433 // AppendRedline, so adjust it beforehand to prevent ASSERT
434 pRedlPam->GetPoint()->Assign(aSttIdx);
435
436 getIDocumentRedlineAccess().AppendRedline(pDeleteRedline, true);
437
438 // the sorted range is inserted
439 getIDocumentRedlineAccess().AppendRedline( new SwRangeRedline( RedlineType::Insert, *pRedlPam ), true);
440
441 if( pRedlUndo )
442 {
443 SwNodeIndex aInsEndIdx( pRedlPam->GetMark()->GetNode(), -1 );
444 SwContentNode *const pContentNode = aInsEndIdx.GetNode().GetContentNode();
445 pRedlPam->GetMark()->Assign( *pContentNode, pContentNode->Len() );
446
447 pRedlUndo->SetValues( *pRedlPam );
448 }
449
450 delete pRedlPam;
451 pRedlPam = nullptr;
452 }
453 GetIDocumentUndoRedo().DoUndo( bUndo );
454 if( bUndo )
455 {
456 GetIDocumentUndoRedo().EndUndo( SwUndoId::END, nullptr );
457 }
458
459 return true;
460}
461
463bool SwDoc::SortTable(const SwSelBoxes& rBoxes, const SwSortOptions& rOpt)
464{
465 // Via SwDoc for Undo!
466 OSL_ENSURE( !rBoxes.empty(), "no valid Box list" );
467 SwTableNode* pTableNd = const_cast<SwTableNode*>(rBoxes[0]->GetSttNd()->FindTableNode());
468 if( !pTableNd )
469 return false;
470
471 // We begin sorting
472 // Find all Boxes/Lines
473 FndBox_ aFndBox( nullptr, nullptr );
474 {
475 FndPara aPara( rBoxes, &aFndBox );
476 ForEach_FndLineCopyCol( pTableNd->GetTable().GetTabLines(), &aPara );
477 }
478
479 if(aFndBox.GetLines().empty())
480 return false;
481
482 if( !getIDocumentRedlineAccess().IsIgnoreRedline() && !getIDocumentRedlineAccess().GetRedlineTable().empty() )
483 getIDocumentRedlineAccess().DeleteRedline( *pTableNd, true, RedlineType::Any );
484
485 FndLines_t::size_type nStart = 0;
486 if( pTableNd->GetTable().GetRowsToRepeat() > 0 && rOpt.eDirection == SwSortDirection::Rows )
487 {
488 // Uppermost selected Cell
489 FndLines_t& rLines = aFndBox.GetLines();
490
491 while( nStart < rLines.size() )
492 {
493 // Respect Split Merge nesting,
494 // extract the upper most
495 SwTableLine* pLine = rLines[nStart]->GetLine();
496 while ( pLine->GetUpper() )
497 pLine = pLine->GetUpper()->GetUpper();
498
499 if( pTableNd->GetTable().IsHeadline( *pLine ) )
500 nStart++;
501 else
502 break;
503 }
504 // Are all selected in the HeaderLine? -> no Offset
505 if( nStart == rLines.size() )
506 nStart = 0;
507 }
508
509 // Switch to relative Formulas
510 SwTableFormulaUpdate aMsgHint( &pTableNd->GetTable() );
511 aMsgHint.m_eFlags = TBL_RELBOXNAME;
513
514 // Table as a flat array structure
515 FlatFndBox aFlatBox(this, aFndBox);
516
517 if(!aFlatBox.IsSymmetric())
518 return false;
519
520 // Delete HTML layout
521 pTableNd->GetTable().SetHTMLTableLayout(std::shared_ptr<SwHTMLTableLayout>());
522
523 // #i37739# A simple 'MakeFrames' after the node sorting
524 // does not work if the table is inside a frame and has no prev/next.
525 SwNode2LayoutSaveUpperFrames aNode2Layout(*pTableNd);
526
527 // Delete the Table's Frames
528 pTableNd->DelFrames();
529 // ? TL_CHART2: ?
530
531 SwUndoSort* pUndoSort = nullptr;
532 if (GetIDocumentUndoRedo().DoesUndo())
533 {
534 pUndoSort = new SwUndoSort( rBoxes[0]->GetSttIdx(),
535 rBoxes.back()->GetSttIdx(),
536 *pTableNd, rOpt, aFlatBox.HasItemSets() );
537 GetIDocumentUndoRedo().AppendUndo(std::unique_ptr<SwUndo>(pUndoSort));
538 }
539 ::sw::UndoGuard const undoGuard(GetIDocumentUndoRedo());
540
541 // Insert KeyElements
542 sal_uInt16 nCount = (rOpt.eDirection == SwSortDirection::Rows) ?
543 aFlatBox.GetRows() : aFlatBox.GetCols();
544
545 // Sort SortList by Key
546 SwSortElement::Init( this, rOpt, &aFlatBox );
547 std::multiset<SwSortBoxElement> aSortList;
548
549 // When sorting, do not include the first row if the HeaderLine is repeated
550 for( sal_uInt16 i = o3tl::narrowing<sal_uInt16>(nStart); i < nCount; ++i)
551 {
552 aSortList.insert(SwSortBoxElement(i));
553 }
554
555 // Move after Sorting
556 SwMovedBoxes aMovedList;
557 sal_uInt16 i = 0;
558 for (const auto& rElem : aSortList)
559 {
561 {
562 MoveRow(this, aFlatBox, rElem.nRow, i+nStart, aMovedList, pUndoSort);
563 }
564 else
565 {
566 MoveCol(this, aFlatBox, rElem.nRow, i+nStart, aMovedList, pUndoSort);
567 }
568 ++i;
569 }
570
571 // Restore table frames:
572 // #i37739# A simple 'MakeFrames' after the node sorting
573 // does not work if the table is inside a frame and has no prev/next.
574 const SwNodeOffset nIdx = pTableNd->GetIndex();
575 aNode2Layout.RestoreUpperFrames( GetNodes(), nIdx, nIdx + 1 );
576
577 // TL_CHART2: need to inform chart of probably changed cell names
578 UpdateCharts( pTableNd->GetTable().GetFrameFormat()->GetName() );
579
580 // Delete all Elements in the SortArray
581 aSortList.clear();
583
585 return true;
586}
587
589void MoveRow(SwDoc* pDoc, const FlatFndBox& rBox, sal_uInt16 nS, sal_uInt16 nT,
590 SwMovedBoxes& rMovedList, SwUndoSort* pUD)
591{
592 for( sal_uInt16 i=0; i < rBox.GetCols(); ++i )
593 { // Get old cell position and remember it
594 const FndBox_* pSource = rBox.GetBox(i, nS);
595
596 // new cell position
597 const FndBox_* pTarget = rBox.GetBox(i, nT);
598
599 const SwTableBox* pT = pTarget->GetBox();
600 const SwTableBox* pS = pSource->GetBox();
601
602 bool bMoved = rMovedList.GetPos(pT) != USHRT_MAX;
603
604 // and move it
605 MoveCell(pDoc, pS, pT, bMoved, pUD);
606
607 rMovedList.push_back(pS);
608
609 if( pS != pT )
610 {
611 SwFrameFormat* pTFormat = pT->GetFrameFormat();
612 const SfxItemSet* pSSet = rBox.GetItemSet( i, nS );
613
614 if( pSSet ||
615 SfxItemState::SET == pTFormat->GetItemState( RES_BOXATR_FORMAT ) ||
616 SfxItemState::SET == pTFormat->GetItemState( RES_BOXATR_FORMULA ) ||
617 SfxItemState::SET == pTFormat->GetItemState( RES_BOXATR_VALUE ) )
618 {
619 pTFormat = const_cast<SwTableBox*>(pT)->ClaimFrameFormat();
620 pTFormat->LockModify();
622 pTFormat->ResetFormatAttr( RES_VERT_ORIENT );
623
624 if( pSSet )
625 pTFormat->SetFormatAttr( *pSSet );
626 pTFormat->UnlockModify();
627 }
628 }
629 }
630}
631
633void MoveCol(SwDoc* pDoc, const FlatFndBox& rBox, sal_uInt16 nS, sal_uInt16 nT,
634 SwMovedBoxes& rMovedList, SwUndoSort* pUD)
635{
636 for(sal_uInt16 i=0; i < rBox.GetRows(); ++i)
637 { // Get old cell position and remember it
638 const FndBox_* pSource = rBox.GetBox(nS, i);
639
640 // new cell position
641 const FndBox_* pTarget = rBox.GetBox(nT, i);
642
643 // and move it
644 const SwTableBox* pT = pTarget->GetBox();
645 const SwTableBox* pS = pSource->GetBox();
646
647 // and move it
648 bool bMoved = rMovedList.GetPos(pT) != USHRT_MAX;
649 MoveCell(pDoc, pS, pT, bMoved, pUD);
650
651 rMovedList.push_back(pS);
652
653 if( pS != pT )
654 {
655 SwFrameFormat* pTFormat = pT->GetFrameFormat();
656 const SfxItemSet* pSSet = rBox.GetItemSet( nS, i );
657
658 if( pSSet ||
659 SfxItemState::SET == pTFormat->GetItemState( RES_BOXATR_FORMAT ) ||
660 SfxItemState::SET == pTFormat->GetItemState( RES_BOXATR_FORMULA ) ||
661 SfxItemState::SET == pTFormat->GetItemState( RES_BOXATR_VALUE ) )
662 {
663 pTFormat = const_cast<SwTableBox*>(pT)->ClaimFrameFormat();
664 pTFormat->LockModify();
666 pTFormat->ResetFormatAttr( RES_VERT_ORIENT );
667
668 if( pSSet )
669 pTFormat->SetFormatAttr( *pSSet );
670 pTFormat->UnlockModify();
671 }
672 }
673 }
674}
675
677void MoveCell(SwDoc* pDoc, const SwTableBox* pSource, const SwTableBox* pTar,
678 bool bMovedBefore, SwUndoSort* pUD)
679{
680 OSL_ENSURE(pSource && pTar,"Source or target missing");
681
682 if(pSource == pTar)
683 return;
684
685 if(pUD)
686 pUD->Insert( pSource->GetName(), pTar->GetName() );
687
688 // Set Pam source to the first ContentNode
689 SwNodeRange aRg( *pSource->GetSttNd(), SwNodeOffset(0), *pSource->GetSttNd() );
690 SwNode* pNd = pDoc->GetNodes().GoNext( &aRg.aStart );
691
692 // If the Cell (Source) wasn't moved
693 // -> insert an empty Node and move the rest or the Mark
694 // points to the first ContentNode
695 if( pNd->StartOfSectionNode() == pSource->GetSttNd() )
696 pNd = pDoc->GetNodes().MakeTextNode( aRg.aStart.GetNode(),
697 pDoc->GetDfltTextFormatColl() );
698 aRg.aEnd = *pNd->EndOfSectionNode();
699
700 // If the Target is empty (there is one empty Node)
701 // -> move and delete it
702 SwNodeIndex aTar( *pTar->GetSttNd() );
703 pNd = pDoc->GetNodes().GoNext( &aTar ); // next ContentNode
705
706 bool bDelFirst = false;
707 if( nCount == SwNodeOffset(2) )
708 {
709 OSL_ENSURE( pNd->GetContentNode(), "No ContentNode");
710 bDelFirst = !pNd->GetContentNode()->Len() && bMovedBefore;
711 }
712
713 if(!bDelFirst)
714 { // We already have Content -> old Content Section Down
715 SwNodeRange aRgTar( aTar.GetNode(), SwNodeOffset(0), *pNd->EndOfSectionNode() );
716 pDoc->GetNodes().SectionDown( &aRgTar );
717 }
718
719 // Insert the Source
720 SwNodeIndex aIns( *pTar->GetSttNd()->EndOfSectionNode() );
723
724 // If first Node is empty -> delete it
725 if(bDelFirst)
726 pDoc->GetNodes().Delete( aTar );
727}
728
730FlatFndBox::FlatFndBox(SwDoc* pDocPtr, const FndBox_& rBoxRef) :
731 m_pDoc(pDocPtr),
732 m_nRow(0),
733 m_nCol(0)
734{ // If the array is symmetric
735 m_bSym = CheckLineSymmetry(rBoxRef);
736 if( !m_bSym )
737 return;
738
739 // Determine column/row count
740 m_nCols = GetColCount(rBoxRef);
741 m_nRows = GetRowCount(rBoxRef);
742
743 // Create linear array
744 size_t nCount = static_cast<size_t>(m_nRows) * m_nCols;
745 m_pArr = std::make_unique<FndBox_ const *[]>(nCount);
746 memset(m_pArr.get(), 0, sizeof(const FndBox_*) * nCount);
747
748 FillFlat( rBoxRef );
749}
750
752{
753}
754
757{
758 const FndLines_t &rLines = rBox.GetLines();
759 FndBoxes_t::size_type nBoxes {0};
760
761 for (FndLines_t::size_type i=0; i < rLines.size(); ++i)
762 {
763 const FndLine_* pLn = rLines[i].get();
764 const FndBoxes_t& rBoxes = pLn->GetBoxes();
765
766 // Number of Boxes of all Lines is unequal -> no symmetry
767 if( i && nBoxes != rBoxes.size())
768 return false;
769
770 nBoxes = rBoxes.size();
771 if( !CheckBoxSymmetry( *pLn ) )
772 return false;
773 }
774 return true;
775}
776
779{
780 const FndBoxes_t &rBoxes = rLn.GetBoxes();
781 FndLines_t::size_type nLines {0};
782
783 for (FndBoxes_t::size_type i = 0; i < rBoxes.size(); ++i)
784 {
785 FndBox_ const*const pBox = rBoxes[i].get();
786 const FndLines_t& rLines = pBox->GetLines();
787
788 // Number of Lines of all Boxes is unequal -> no symmetry
789 if( i && nLines != rLines.size() )
790 return false;
791
792 nLines = rLines.size();
793 if( nLines && !CheckLineSymmetry( *pBox ) )
794 return false;
795 }
796 return true;
797}
798
800sal_uInt16 FlatFndBox::GetColCount(const FndBox_& rBox)
801{
802 const FndLines_t& rLines = rBox.GetLines();
803 // Iterate over Lines
804 if( rLines.empty() )
805 return 1;
806
807 sal_uInt16 nSum = 0;
808 for (const auto & pLine : rLines)
809 {
810 // The Boxes of a Line
811 sal_uInt16 nCount = 0;
812 const FndBoxes_t& rBoxes = pLine->GetBoxes();
813 for (const auto &rpB : rBoxes)
814 { // Iterate recursively over the Lines
815 nCount += rpB->GetLines().empty() ? 1 : GetColCount(*rpB);
816 }
817
818 if( nSum < nCount )
819 nSum = nCount;
820 }
821 return nSum;
822}
823
825sal_uInt16 FlatFndBox::GetRowCount(const FndBox_& rBox)
826{
827 const FndLines_t& rLines = rBox.GetLines();
828 if( rLines.empty() )
829 return 1;
830
831 sal_uInt16 nLines = 0;
832 for (const auto & pLine : rLines)
833 { // The Boxes of a Line
834 const FndBoxes_t& rBoxes = pLine->GetBoxes();
835 sal_uInt16 nLn = 1;
836 for (const auto &rpB : rBoxes)
837 {
838 if (!rpB->GetLines().empty())
839 { // Iterate recursively over the Lines
840 nLn = std::max(GetRowCount(*rpB), nLn);
841 }
842 }
843
844 nLines = nLines + nLn;
845 }
846 return nLines;
847}
848
850void FlatFndBox::FillFlat(const FndBox_& rBox, bool bLastBox)
851{
852 bool bModRow = false;
853 const FndLines_t& rLines = rBox.GetLines();
854
855 // Iterate over Lines
856 sal_uInt16 nOldRow = m_nRow;
857 for (const auto & pLine : rLines)
858 {
859 // The Boxes of a Line
860 const FndBoxes_t& rBoxes = pLine->GetBoxes();
861 sal_uInt16 nOldCol = m_nCol;
862 for( FndBoxes_t::size_type j = 0; j < rBoxes.size(); ++j )
863 {
864 // Check the Box if it's an atomic one
865 const FndBox_ *const pBox = rBoxes[j].get();
866
867 if( pBox->GetLines().empty() )
868 {
869 // save it
870 sal_uInt16 nOff = m_nRow * m_nCols + m_nCol;
871 m_pArr[nOff] = pBox;
872
873 // Save the Formula/Format/Value values
874 const SwFrameFormat* pFormat = pBox->GetBox()->GetFrameFormat();
875 if( SfxItemState::SET == pFormat->GetItemState( RES_BOXATR_FORMAT ) ||
876 SfxItemState::SET == pFormat->GetItemState( RES_BOXATR_FORMULA ) ||
877 SfxItemState::SET == pFormat->GetItemState( RES_BOXATR_VALUE ) )
878 {
882 aSet(m_pDoc->GetAttrPool());
883 aSet.Put( pFormat->GetAttrSet() );
884 if( m_vItemSets.empty() )
885 {
886 size_t nCount = static_cast<size_t>(m_nRows) * m_nCols;
887 m_vItemSets.resize(nCount);
888 }
889 m_vItemSets[nOff].emplace(std::move(aSet));
890 }
891
892 bModRow = true;
893 }
894 else
895 {
896 // Iterate recursively over the Lines of a Box
897 FillFlat( *pBox, ( j+1 == rBoxes.size() ) );
898 }
899 m_nCol++;
900 }
901 if(bModRow)
902 m_nRow++;
903 m_nCol = nOldCol;
904 }
905 if(!bLastBox)
906 m_nRow = nOldRow;
907}
908
910const FndBox_* FlatFndBox::GetBox(sal_uInt16 n_Col, sal_uInt16 n_Row) const
911{
912 sal_uInt16 nOff = n_Row * m_nCols + n_Col;
913 const FndBox_* pTmp = m_pArr[nOff];
914
915 OSL_ENSURE(n_Col < m_nCols && n_Row < m_nRows && pTmp, "invalid array access");
916 return pTmp;
917}
918
919const SfxItemSet* FlatFndBox::GetItemSet(sal_uInt16 n_Col, sal_uInt16 n_Row) const
920{
921 OSL_ENSURE( m_vItemSets.empty() || ( n_Col < m_nCols && n_Row < m_nRows), "invalid array access");
922
923 if (m_vItemSets.empty()) {
924 return nullptr;
925 }
926 auto const & el = m_vItemSets[unsigned(n_Row * m_nCols) + n_Col];
927 return el ? &*el : nullptr;
928}
929
930sal_uInt16 SwMovedBoxes::GetPos(const SwTableBox* pTableBox) const
931{
932 std::vector<const SwTableBox*>::const_iterator it = std::find(mBoxes.begin(), mBoxes.end(), pTableBox);
933 return it == mBoxes.end() ? USHRT_MAX : it - mBoxes.begin();
934}
935
936/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_uInt32 GetValue() const
sal_Int32 compareString(const OUString &s1, const OUString &s2) const
void loadCollatorAlgorithm(const OUString &rAlgorithm, const css::lang::Locale &rLocale, sal_Int32 nOption)
FlatFndBox(SwDoc *pDocPtr, const FndBox_ &rBox)
Generate two-dimensional array of FndBoxes.
Definition: docsort.cxx:730
sal_uInt16 GetColCount(const FndBox_ &rBox)
Maximum count of Columns (Boxes)
Definition: docsort.cxx:800
bool IsSymmetric() const
Definition: docsort.hxx:119
sal_uInt16 m_nCols
Definition: docsort.hxx:141
sal_uInt16 m_nRow
Definition: docsort.hxx:142
std::unique_ptr< FndBox_ const *[]> m_pArr
Definition: docsort.hxx:136
SwDoc * m_pDoc
Definition: docsort.hxx:135
bool CheckLineSymmetry(const FndBox_ &rBox)
All Lines of a Box need to have same number of Boxes.
Definition: docsort.cxx:756
const SfxItemSet * GetItemSet(sal_uInt16 nCol, sal_uInt16 nRow) const
Definition: docsort.cxx:919
sal_uInt16 GetRowCount(const FndBox_ &rBox)
Maximum count of Rows (Lines)
Definition: docsort.cxx:825
sal_uInt16 m_nCol
Definition: docsort.hxx:143
bool HasItemSets() const
Definition: docsort.hxx:148
bool m_bSym
Definition: docsort.hxx:145
void FillFlat(const FndBox_ &, bool bLastBox=false)
Create a linear array of atomic FndBoxes.
Definition: docsort.cxx:850
sal_uInt16 GetCols() const
Definition: docsort.hxx:121
sal_uInt16 GetRows() const
Definition: docsort.hxx:120
const FndBox_ * GetBox(sal_uInt16 nCol, sal_uInt16 nRow) const
Access a specific Cell.
Definition: docsort.cxx:910
std::vector< std::optional< SfxItemSet > > m_vItemSets
using optional because SfxItemSet has no default constructor
Definition: docsort.hxx:138
bool CheckBoxSymmetry(const FndLine_ &rLn)
Check Box for symmetry (All Boxes of a Line need to have same number of Lines)
Definition: docsort.cxx:778
sal_uInt16 m_nRows
Definition: docsort.hxx:140
const FndLines_t & GetLines() const
Definition: tblsel.hxx:172
const SwTableBox * GetBox() const
Definition: tblsel.hxx:174
const FndBoxes_t & GetBoxes() const
Definition: tblsel.hxx:202
virtual bool MoveNodeRange(SwNodeRange &, SwNode &, SwMoveFlags)=0
virtual void UpdateTableFields(SfxPoolItem *pHt)=0
virtual bool DeleteRedline(const SwPaM &rPam, bool bSaveInUndo, RedlineType nDelType)=0
static bool IsShowOriginal(const RedlineFlags eM)
virtual AppendResult AppendRedline(SwRangeRedline *pNewRedl, bool bCallDelete)=0
Append a new redline.
virtual void SetModified()=0
Must be called manually at changes of format.
static css::lang::Locale convertToLocale(LanguageType nLangID, bool bResolveSystem=true)
double stringToDouble(std::u16string_view aString, bool bUseGroupSep, rtl_math_ConversionStatus *pStatus, sal_Int32 *pParseEnd) const
bool IsTextFormat(sal_uInt32 nFIndex) const
virtual sal_Int32 Len() const
Definition: node.cxx:1263
Definition: doc.hxx:194
bool SortTable(const SwSelBoxes &rBoxes, const SwSortOptions &)
Sort Table in the Document.
Definition: docsort.cxx:463
void UpdateCharts(const OUString &rName) const
Definition: docchart.cxx:141
IDocumentState const & getIDocumentState() const
Definition: doc.cxx:400
bool SortText(const SwPaM &, const SwSortOptions &)
Sort Text in the Document.
Definition: docsort.cxx:283
IDocumentContentOperations const & getIDocumentContentOperations() const
Definition: doc.cxx:321
IDocumentUndoRedo & GetIDocumentUndoRedo()
Definition: doc.cxx:150
SwNodes & GetNodes()
Definition: doc.hxx:417
IDocumentFieldsAccess const & getIDocumentFieldsAccess() const
Definition: doc.cxx:363
IDocumentRedlineAccess const & getIDocumentRedlineAccess() const
Definition: doc.cxx:341
const SwTextFormatColl * GetDfltTextFormatColl() const
Definition: doc.hxx:784
const SwAttrPool & GetAttrPool() const
Definition: doc.hxx:1326
const SwFrameFormats * GetSpzFrameFormats() const
Definition: doc.hxx:752
SvNumberFormatter * GetNumberFormatter(bool bCreate=true)
Definition: doc.hxx:1418
FlyAnchors.
Definition: fmtanchr.hxx:37
RndStdIds GetAnchorId() const
Definition: fmtanchr.hxx:67
SwNode * GetAnchorNode() const
Definition: atrfrm.cxx:1606
Base class for various Writer styles.
Definition: format.hxx:47
const SwTableBoxNumFormat & GetTableBoxNumFormat(bool=true) const
TableBox attributes - implemented in cellatr.hxx.
Definition: cellatr.hxx:105
virtual bool ResetFormatAttr(sal_uInt16 nWhich1, sal_uInt16 nWhich2=0)
Definition: format.cxx:618
const OUString & GetName() const
Definition: format.hxx:131
SfxItemState GetItemState(sal_uInt16 nWhich, bool bSrchInParent=true, const SfxPoolItem **ppItem=nullptr) const
Definition: format.cxx:385
const SwTableBoxValue & GetTableBoxValue(bool=true) const
Definition: cellatr.hxx:109
const SwAttrSet & GetAttrSet() const
For querying the attribute array.
Definition: format.hxx:136
virtual bool SetFormatAttr(const SfxPoolItem &rAttr)
Definition: format.cxx:447
Style of a layout element.
Definition: frmfmt.hxx:62
sal_uInt16 GetPos(const SwTableBox *pTableBox) const
Definition: docsort.cxx:930
std::vector< const SwTableBox * > mBoxes
Definition: docsort.hxx:42
void push_back(const SwTableBox *&rpTableBox)
Definition: docsort.hxx:45
void RestoreUpperFrames(SwNodes &rNds, SwNodeOffset nStt, SwNodeOffset nEnd)
Definition: node2lay.cxx:489
Marks a node in the document model.
Definition: ndindex.hxx:31
SwNode & GetNode() const
Definition: ndindex.hxx:136
SwNodeOffset GetIndex() const
Definition: ndindex.hxx:171
SwNodeIndex aStart
Definition: ndindex.hxx:149
SwNodeIndex aEnd
Definition: ndindex.hxx:150
Base class of the Writer document model elements.
Definition: node.hxx:98
SwTextNode * GetTextNode()
Inline methods from Node.hxx.
Definition: ndtxt.hxx:897
SwNodeOffset GetIndex() const
Definition: node.hxx:312
SwNodeOffset StartOfSectionIndex() const
Definition: node.hxx:722
bool IsTextNode() const
Definition: node.hxx:685
const SwStartNode * StartOfSectionNode() const
Definition: node.hxx:153
SwNodeOffset EndOfSectionIndex() const
Definition: node.hxx:726
SwContentNode * GetContentNode()
Definition: node.hxx:664
const SwEndNode * EndOfSectionNode() const
Definition: node.hxx:731
SwTextNode * MakeTextNode(SwNode &rWhere, SwTextFormatColl *pColl, bool bNewFrames=true)
Implementations of "Make...Node" are in the given .cxx-files.
Definition: ndtxt.cxx:120
void Delete(const SwNodeIndex &rPos, SwNodeOffset nNodes=SwNodeOffset(1))
Definition: nodes.cxx:1065
SwContentNode * GoNext(SwNodeIndex *) const
Definition: nodes.cxx:1294
void SectionDown(SwNodeRange *pRange, SwStartNodeType=SwNormalStartNode)
create a start/end section pair
Definition: nodes.cxx:901
void Copy_(const SwNodeRange &rRg, SwNode &rInsPos, bool bNewFrames=true) const
Definition: ndarr.hxx:179
PaM is Point and Mark: a selection of the document model.
Definition: pam.hxx:187
const SwPosition * GetMark() const
Definition: pam.hxx:263
std::pair< const SwPosition *, const SwPosition * > StartEnd() const
Because sometimes the cost of the operator<= can add up.
Definition: pam.hxx:277
SwContentNode * GetPointContentNode() const
Definition: pam.hxx:287
SwContentNode * GetMarkContentNode() const
Definition: pam.hxx:288
const SwPosition * GetPoint() const
Definition: pam.hxx:261
double GetValue() const
Definition: cellatr.hxx:95
SwTableBox is one table cell in the document model.
Definition: swtable.hxx:426
SwTableLine * GetUpper()
Definition: swtable.hxx:460
SwNodeOffset GetSttIdx() const
Definition: swtable.cxx:2065
OUString GetName() const
Definition: swtable.cxx:2012
SwFrameFormat * GetFrameFormat()
Definition: swtable.hxx:464
const SwStartNode * GetSttNd() const
Definition: swtable.hxx:478
TableFormulaUpdateFlags m_eFlags
Definition: hints.hxx:286
SwTableLine is one table row in the document model.
Definition: swtable.hxx:364
SwTableBox * GetUpper()
Definition: swtable.hxx:382
const SwTable & GetTable() const
Definition: node.hxx:542
void DelFrames(SwRootFrame const *pLayout=nullptr)
Method deletes all views of document for the node.
Definition: ndtbl.cxx:2422
void SetHTMLTableLayout(std::shared_ptr< SwHTMLTableLayout > const &r)
Definition: swtable.cxx:2157
SwTableLines & GetTabLines()
Definition: swtable.hxx:204
bool IsHeadline(const SwTableLine &rLine) const
Definition: tabfrm.cxx:5814
SwTableFormat * GetFrameFormat()
Definition: swtable.hxx:207
sal_uInt16 GetRowsToRepeat() const
Definition: swtable.hxx:199
SwTextNode is a paragraph in the document model.
Definition: ndtxt.hxx:111
const OUString & GetText() const
Definition: ndtxt.hxx:242
void SetValues(const SwPaM &rPam)
Definition: undobj.cxx:60
void SetSaveRange(const SwPaM &rRange)
Definition: unredln.cxx:413
void Insert(const OUString &rOrgPos, const OUString &rNewPos)
Definition: unsort.cxx:243
const Value & back() const
bool empty() const
int nCount
const char * pS
void MoveCol(SwDoc *pDoc, const FlatFndBox &rBox, sal_uInt16 nS, sal_uInt16 nT, SwMovedBoxes &rMovedList, SwUndoSort *pUD)
Move a column.
Definition: docsort.cxx:633
void MoveRow(SwDoc *pDoc, const FlatFndBox &rBox, sal_uInt16 nS, sal_uInt16 nT, SwMovedBoxes &rMovedList, SwUndoSort *pUD)
Move a row.
Definition: docsort.cxx:589
void MoveCell(SwDoc *pDoc, const SwTableBox *pSource, const SwTableBox *pTar, bool bMovedBefore, SwUndoSort *pUD)
Move a single Cell.
Definition: docsort.cxx:677
constexpr TypedWhichId< SwTableBoxValue > RES_BOXATR_VALUE(152)
constexpr TypedWhichId< SwTableBoxFormula > RES_BOXATR_FORMULA(151)
constexpr TypedWhichId< SwTableBoxNumFormat > RES_BOXATR_FORMAT(RES_BOXATR_BEGIN)
constexpr TypedWhichId< SwFormatVertOrient > RES_VERT_ORIENT(102)
@ TBL_RELBOXNAME
Definition: hints.hxx:272
LanguageType GetAppLanguage()
Definition: init.cxx:725
sal_Int64 n
#define LANGUAGE_NONE
#define LANGUAGE_DONTKNOW
int n2
int n1
int i
o3tl::strong_int< sal_Int32, struct Tag_SwNodeOffset > SwNodeOffset
Definition: nodeoffset.hxx:16
sal_Int16 nId
SwContentNode * GetNode(SwPaM &rPam, bool &rbFirst, SwMoveFnCollection const &fnMove, bool const bInReadOnly, SwRootFrame const *const i_pLayout)
This function returns the next node in direction of search.
Definition: pam.cxx:1019
static LanguageType nLang
Definition: srtdlg.cxx:51
SwNode & GetNode() const
Definition: pam.hxx:80
void Assign(const SwNode &rNd, SwNodeOffset nDelta, sal_Int32 nContentOffset=0)
These all set both nNode and nContent.
Definition: pam.cxx:230
void SetContent(sal_Int32 nContentIndex)
Set content index, only valid to call this if the position points to a SwContentNode subclass.
Definition: pam.cxx:266
SwSortBoxElement(sal_uInt16 nRC)
SortingElement for Tables.
Definition: docsort.cxx:219
sal_uInt16 nRow
Definition: docsort.hxx:104
virtual double GetValue(sal_uInt16 nKey) const override
Definition: docsort.cxx:257
virtual OUString GetKey(sal_uInt16 nKey) const override
Get Key for a cell.
Definition: docsort.cxx:225
static double StrToDouble(std::u16string_view rStr)
Definition: docsort.cxx:104
static void Finit()
Definition: docsort.cxx:85
static SwDoc * pDoc
Definition: docsort.hxx:62
static std::optional< OUString > xLastAlgorithm
Definition: docsort.hxx:66
bool operator<(const SwSortElement &) const
Definition: docsort.cxx:159
static SwSortOptions * pOptions
Definition: docsort.hxx:61
static css::lang::Locale * pLocale
Definition: docsort.hxx:65
static const FlatFndBox * pBox
Definition: docsort.hxx:63
int keycompare(const SwSortElement &rCmp, sal_uInt16 nKey) const
Definition: docsort.cxx:118
static void Init(SwDoc *, const SwSortOptions &rOpt, FlatFndBox const *=nullptr)
Construct a SortElement for the Sort.
Definition: docsort.cxx:67
virtual double GetValue(sal_uInt16 nKey) const
Definition: docsort.cxx:175
static CollatorWrapper * pSortCollator
Definition: docsort.hxx:64
virtual OUString GetKey(sal_uInt16 nKey) const =0
virtual ~SwSortElement()
Definition: docsort.cxx:100
static LocaleDataWrapper * pLclData
Definition: docsort.hxx:67
bool bIsNumeric
Definition: sortopt.hxx:38
OUString sSortType
Definition: sortopt.hxx:35
SwSortOrder eSortOrder
Definition: sortopt.hxx:36
std::vector< SwSortKey > aKeys
Definition: sortopt.hxx:49
sal_Unicode cDeli
Definition: sortopt.hxx:51
LanguageType nLanguage
Definition: sortopt.hxx:52
bool bIgnoreCase
Definition: sortopt.hxx:54
SwSortDirection eDirection
Definition: sortopt.hxx:50
virtual OUString GetKey(sal_uInt16 nKey) const override
Definition: docsort.cxx:186
SwNodeIndex aPos
Definition: docsort.hxx:94
SwSortTextElement(const SwNodeIndex &rPos)
SortingElement for Text.
Definition: docsort.cxx:181
bool anyOf(strong_int v) const
#define SW_COLLATOR_IGNORES
Definition: swtypes.hxx:192
void ForEach_FndLineCopyCol(SwTableLines &rLines, FndPara *pFndPara)
This creates a structure mirroring the SwTable structure that contains all rows and non-leaf boxes (a...
Definition: tblsel.cxx:2090
std::vector< std::unique_ptr< FndLine_ > > FndLines_t
Definition: tblsel.hxx:155
std::vector< std::unique_ptr< FndBox_ > > FndBoxes_t
Definition: tblsel.hxx:152
sal_uInt16 sal_Unicode