Line data Source code
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 <UndoSort.hxx>
21 : #include <doc.hxx>
22 : #include <swundo.hxx>
23 : #include <pam.hxx>
24 : #include <swtable.hxx>
25 : #include <ndtxt.hxx>
26 : #include <UndoCore.hxx>
27 : #include <UndoTable.hxx>
28 : #include <sortopt.hxx>
29 : #include <docsort.hxx>
30 : #include <redline.hxx>
31 : #include <node2lay.hxx>
32 :
33 : /*--------------------------------------------------------------------
34 : Description: Undo for Sorting
35 : --------------------------------------------------------------------*/
36 :
37 66 : SwSortUndoElement::~SwSortUndoElement()
38 : {
39 : // are there string pointers saved?
40 66 : if( 0xffffffff != SORT_TXT_TBL.TXT.nKenn ) // Kenn(ung) = identifier
41 : {
42 38 : delete SORT_TXT_TBL.TBL.pSource;
43 38 : delete SORT_TXT_TBL.TBL.pTarget;
44 : }
45 66 : }
46 :
47 4 : SwUndoSort::SwUndoSort(const SwPaM& rRg, const SwSortOptions& rOpt)
48 : : SwUndo(UNDO_SORT_TXT)
49 : , SwUndRng(rRg)
50 : , pUndoTblAttr(0)
51 : , pRedlData(0)
52 4 : , nTblNd(0)
53 : {
54 4 : pSortOpt = new SwSortOptions(rOpt);
55 4 : }
56 :
57 4 : SwUndoSort::SwUndoSort( sal_uLong nStt, sal_uLong nEnd, const SwTableNode& rTblNd,
58 : const SwSortOptions& rOpt, bool bSaveTable )
59 4 : : SwUndo(UNDO_SORT_TBL), pUndoTblAttr( 0 ), pRedlData( 0 )
60 : {
61 4 : nSttNode = nStt;
62 4 : nEndNode = nEnd;
63 4 : nTblNd = rTblNd.GetIndex();
64 :
65 4 : pSortOpt = new SwSortOptions(rOpt);
66 4 : if( bSaveTable )
67 4 : pUndoTblAttr = new SwUndoAttrTbl( rTblNd );
68 4 : }
69 :
70 24 : SwUndoSort::~SwUndoSort()
71 : {
72 8 : delete pSortOpt;
73 8 : delete pUndoTblAttr;
74 8 : delete pRedlData;
75 16 : }
76 :
77 0 : void SwUndoSort::UndoImpl(::sw::UndoRedoContext & rContext)
78 : {
79 0 : SwDoc & rDoc = rContext.GetDoc();
80 0 : if(pSortOpt->bTable)
81 : {
82 : // Undo for Table
83 0 : RemoveIdxFromSection( rDoc, nSttNode, &nEndNode );
84 :
85 0 : if( pUndoTblAttr )
86 : {
87 0 : pUndoTblAttr->UndoImpl(rContext);
88 : }
89 :
90 0 : SwTableNode* pTblNd = rDoc.GetNodes()[ nTblNd ]->GetTableNode();
91 :
92 : // #i37739# A simple 'MakeFrms' after the node sorting
93 : // does not work if the table is inside a frame and has no prev/next.
94 0 : SwNode2Layout aNode2Layout( *pTblNd );
95 :
96 0 : pTblNd->DelFrms();
97 0 : const SwTable& rTbl = pTblNd->GetTable();
98 :
99 0 : SwMovedBoxes aMovedList;
100 0 : for( sal_uInt16 i=0; i < aSortList.size(); i++)
101 : {
102 : const SwTableBox* pSource = rTbl.GetTblBox(
103 0 : *aSortList[i].SORT_TXT_TBL.TBL.pSource );
104 : const SwTableBox* pTarget = rTbl.GetTblBox(
105 0 : *aSortList[i].SORT_TXT_TBL.TBL.pTarget );
106 :
107 : // move back
108 : MoveCell(&rDoc, pTarget, pSource,
109 0 : USHRT_MAX != aMovedList.GetPos(pSource) );
110 :
111 : // store moved entry in list
112 0 : aMovedList.push_back(pTarget);
113 : }
114 :
115 : // Restore table frames:
116 : // #i37739# A simple 'MakeFrms' after the node sorting
117 : // does not work if the table is inside a frame and has no prev/next.
118 0 : const sal_uLong nIdx = pTblNd->GetIndex();
119 0 : aNode2Layout.RestoreUpperFrms( rDoc.GetNodes(), nIdx, nIdx + 1 );
120 : }
121 : else
122 : {
123 : // Undo for Text
124 0 : SwPaM & rPam( AddUndoRedoPaM(rContext) );
125 0 : RemoveIdxFromRange(rPam, true);
126 :
127 : // create index for (sorted) positions
128 : // The IndexList must be created based on (asc.) sorted SourcePosition.
129 0 : SwUndoSortList aIdxList;
130 : sal_uInt16 i;
131 :
132 0 : for( i = 0; i < aSortList.size(); ++i)
133 0 : for( sal_uInt16 ii=0; ii < aSortList.size(); ++ii )
134 0 : if( aSortList[ii].SORT_TXT_TBL.TXT.nSource == nSttNode + i )
135 : {
136 : SwNodeIndex* pIdx = new SwNodeIndex( rDoc.GetNodes(),
137 0 : aSortList[ii].SORT_TXT_TBL.TXT.nTarget );
138 0 : aIdxList.insert( aIdxList.begin() + i, pIdx );
139 0 : break;
140 : }
141 :
142 0 : for(i=0; i < aSortList.size(); ++i)
143 : {
144 0 : SwNodeIndex aIdx( rDoc.GetNodes(), nSttNode + i );
145 0 : SwNodeRange aRg( *aIdxList[i], 0, *aIdxList[i], 1 );
146 : rDoc.MoveNodeRange(aRg, aIdx,
147 0 : IDocumentContentOperations::DOC_MOVEDEFAULT);
148 0 : }
149 : // delete indices
150 0 : for(SwUndoSortList::const_iterator it = aIdxList.begin(); it != aIdxList.end(); ++it)
151 0 : delete *it;
152 0 : aIdxList.clear();
153 0 : SetPaM(rPam, true);
154 : }
155 0 : }
156 :
157 0 : void SwUndoSort::RedoImpl(::sw::UndoRedoContext & rContext)
158 : {
159 0 : SwDoc & rDoc = rContext.GetDoc();
160 :
161 0 : if(pSortOpt->bTable)
162 : {
163 : // Redo for Table
164 0 : RemoveIdxFromSection( rDoc, nSttNode, &nEndNode );
165 :
166 0 : SwTableNode* pTblNd = rDoc.GetNodes()[ nTblNd ]->GetTableNode();
167 :
168 : // #i37739# A simple 'MakeFrms' after the node sorting
169 : // does not work if the table is inside a frame and has no prev/next.
170 0 : SwNode2Layout aNode2Layout( *pTblNd );
171 :
172 0 : pTblNd->DelFrms();
173 0 : const SwTable& rTbl = pTblNd->GetTable();
174 :
175 0 : SwMovedBoxes aMovedList;
176 0 : for(sal_uInt16 i=0; i < aSortList.size(); ++i)
177 : {
178 : const SwTableBox* pSource = rTbl.GetTblBox(
179 0 : *aSortList[i].SORT_TXT_TBL.TBL.pSource );
180 : const SwTableBox* pTarget = rTbl.GetTblBox(
181 0 : *aSortList[i].SORT_TXT_TBL.TBL.pTarget );
182 :
183 : // move back
184 : MoveCell(&rDoc, pSource, pTarget,
185 0 : USHRT_MAX != aMovedList.GetPos( pTarget ) );
186 : // store moved entry in list
187 0 : aMovedList.push_back( pSource );
188 : }
189 :
190 0 : if( pUndoTblAttr )
191 : {
192 0 : pUndoTblAttr->RedoImpl(rContext);
193 : }
194 :
195 : // Restore table frames:
196 : // #i37739# A simple 'MakeFrms' after the node sorting
197 : // does not work if the table is inside a frame and has no prev/next.
198 0 : const sal_uLong nIdx = pTblNd->GetIndex();
199 0 : aNode2Layout.RestoreUpperFrms( rDoc.GetNodes(), nIdx, nIdx + 1 );
200 : }
201 : else
202 : {
203 : // Redo for Text
204 0 : SwPaM & rPam( AddUndoRedoPaM(rContext) );
205 0 : SetPaM(rPam);
206 0 : RemoveIdxFromRange(rPam, true);
207 :
208 0 : SwUndoSortList aIdxList;
209 : sal_uInt16 i;
210 :
211 0 : for( i = 0; i < aSortList.size(); ++i)
212 : { // current position is starting point
213 : SwNodeIndex* pIdx = new SwNodeIndex( rDoc.GetNodes(),
214 0 : aSortList[i].SORT_TXT_TBL.TXT.nSource);
215 0 : aIdxList.insert( aIdxList.begin() + i, pIdx );
216 : }
217 :
218 0 : for(i=0; i < aSortList.size(); ++i)
219 : {
220 0 : SwNodeIndex aIdx( rDoc.GetNodes(), nSttNode + i);
221 0 : SwNodeRange aRg( *aIdxList[i], 0, *aIdxList[i], 1 );
222 : rDoc.MoveNodeRange(aRg, aIdx,
223 0 : IDocumentContentOperations::DOC_MOVEDEFAULT);
224 0 : }
225 : // delete indices
226 0 : for(SwUndoSortList::const_iterator it = aIdxList.begin(); it != aIdxList.end(); ++it)
227 0 : delete *it;
228 0 : aIdxList.clear();
229 0 : SetPaM(rPam, true);
230 0 : SwTxtNode const*const pTNd = rPam.GetNode()->GetTxtNode();
231 0 : if( pTNd )
232 : {
233 0 : rPam.GetPoint()->nContent = pTNd->GetTxt().getLength();
234 0 : }
235 : }
236 0 : }
237 :
238 0 : void SwUndoSort::RepeatImpl(::sw::RepeatContext & rContext)
239 : {
240 : // table not repeat capable
241 0 : if(!pSortOpt->bTable)
242 : {
243 0 : SwPaM *const pPam = & rContext.GetRepeatPaM();
244 0 : SwDoc& rDoc = *pPam->GetDoc();
245 :
246 0 : if( !rDoc.IsIdxInTbl( pPam->Start()->nNode ) )
247 0 : rDoc.SortText(*pPam, *pSortOpt);
248 : }
249 0 : }
250 :
251 38 : void SwUndoSort::Insert( const OUString& rOrgPos, const OUString& rNewPos)
252 : {
253 38 : SwSortUndoElement* pEle = new SwSortUndoElement(rOrgPos, rNewPos);
254 38 : aSortList.push_back( pEle );
255 38 : }
256 :
257 28 : void SwUndoSort::Insert( sal_uLong nOrgPos, sal_uLong nNewPos)
258 : {
259 28 : SwSortUndoElement* pEle = new SwSortUndoElement(nOrgPos, nNewPos);
260 28 : aSortList.push_back( pEle );
261 28 : }
262 :
263 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|