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 :
21 : #include <editsh.hxx>
22 : #include <doc.hxx>
23 : #include <IDocumentUndoRedo.hxx>
24 : #include <pam.hxx>
25 : #include <docary.hxx>
26 : #include <swundo.hxx> // fuer die UndoIds
27 : #include <section.hxx>
28 : #include <edimp.hxx>
29 : #include <sectfrm.hxx> // SwSectionFrm
30 : #include <cntfrm.hxx> // SwCntntFrm
31 : #include <tabfrm.hxx> // SwTabFrm
32 : #include <rootfrm.hxx> // SwRootFrm
33 :
34 :
35 : SwSection const*
36 0 : SwEditShell::InsertSection(
37 : SwSectionData & rNewData, SfxItemSet const*const pAttr)
38 : {
39 0 : const SwSection* pRet = 0;
40 0 : if( !IsTableMode() )
41 : {
42 0 : StartAllAction();
43 0 : GetDoc()->GetIDocumentUndoRedo().StartUndo( UNDO_INSSECTION, NULL );
44 :
45 0 : FOREACHPAM_START(this)
46 : SwSection const*const pNew =
47 0 : GetDoc()->InsertSwSection( *PCURCRSR, rNewData, 0, pAttr );
48 0 : if( !pRet )
49 0 : pRet = pNew;
50 0 : FOREACHPAM_END()
51 :
52 0 : GetDoc()->GetIDocumentUndoRedo().EndUndo( UNDO_INSSECTION, NULL );
53 0 : EndAllAction();
54 : }
55 0 : return pRet;
56 : }
57 :
58 :
59 0 : sal_Bool SwEditShell::IsInsRegionAvailable() const
60 : {
61 0 : if( IsTableMode() )
62 0 : return sal_False;
63 0 : SwPaM* pCrsr = GetCrsr();
64 0 : if( pCrsr->GetNext() != pCrsr )
65 0 : return sal_False;
66 0 : if( pCrsr->HasMark() )
67 0 : return 0 != GetDoc()->IsInsRegionAvailable( *pCrsr );
68 :
69 0 : return sal_True;
70 : }
71 :
72 :
73 0 : const SwSection* SwEditShell::GetCurrSection() const
74 : {
75 0 : if( IsTableMode() )
76 0 : return 0;
77 :
78 0 : return GetDoc()->GetCurrSection( *GetCrsr()->GetPoint() );
79 : }
80 :
81 : /*--------------------------------------------------
82 : * SwEditShell::GetAnySection liefert den fuer Spalten
83 : * zustaendigen Bereich, bei Fussnoten kann es nicht der
84 : * Bereich innerhalb der Fussnote sein.
85 : * --------------------------------------------------*/
86 :
87 0 : const SwSection* SwEditShell::GetAnySection( sal_Bool bOutOfTab, const Point* pPt ) const
88 : {
89 : SwFrm *pFrm;
90 0 : if ( pPt )
91 : {
92 0 : SwPosition aPos( *GetCrsr()->GetPoint() );
93 0 : Point aPt( *pPt );
94 0 : GetLayout()->GetCrsrOfst( &aPos, aPt );
95 0 : SwCntntNode *pNd = aPos.nNode.GetNode().GetCntntNode();
96 0 : pFrm = pNd->getLayoutFrm( GetLayout(), pPt );
97 : }
98 : else
99 0 : pFrm = GetCurrFrm( sal_False );
100 :
101 0 : if( bOutOfTab && pFrm )
102 0 : pFrm = pFrm->FindTabFrm();
103 0 : if( pFrm && pFrm->IsInSct() )
104 : {
105 0 : SwSectionFrm* pSect = pFrm->FindSctFrm();
106 : OSL_ENSURE( pSect, "GetAnySection: Where's my Sect?" );
107 0 : if( pSect->IsInFtn() && pSect->GetUpper()->IsInSct() )
108 : {
109 0 : pSect = pSect->GetUpper()->FindSctFrm();
110 : OSL_ENSURE( pSect, "GetAnySection: Where's my SectFrm?" );
111 : }
112 0 : return pSect->GetSection();
113 : }
114 0 : return NULL;
115 : }
116 :
117 0 : sal_uInt16 SwEditShell::GetSectionFmtCount() const
118 : {
119 0 : return GetDoc()->GetSections().size();
120 : }
121 :
122 :
123 0 : sal_Bool SwEditShell::IsAnySectionInDoc( sal_Bool bChkReadOnly, sal_Bool bChkHidden, sal_Bool bChkTOX ) const
124 : {
125 0 : const SwSectionFmts& rFmts = GetDoc()->GetSections();
126 0 : sal_uInt16 nCnt = rFmts.size();
127 : sal_uInt16 n;
128 :
129 0 : for( n = 0; n < nCnt; ++n )
130 : {
131 : SectionType eTmpType;
132 0 : const SwSectionFmt* pFmt = rFmts[ n ];
133 0 : if( pFmt->IsInNodesArr() &&
134 : (bChkTOX ||
135 0 : ( (eTmpType = pFmt->GetSection()->GetType()) != TOX_CONTENT_SECTION
136 : && TOX_HEADER_SECTION != eTmpType ) ) )
137 : {
138 0 : const SwSection& rSect = *rFmts[ n ]->GetSection();
139 0 : if( (!bChkReadOnly && !bChkHidden ) ||
140 0 : (bChkReadOnly && rSect.IsProtectFlag() ) ||
141 0 : (bChkHidden && rSect.IsHiddenFlag() ) )
142 0 : break;
143 : }
144 : }
145 0 : return n != nCnt;
146 : }
147 :
148 0 : sal_uInt16 SwEditShell::GetSectionFmtPos( const SwSectionFmt& rFmt ) const
149 : {
150 0 : SwSectionFmt* pFmt = (SwSectionFmt*)&rFmt;
151 0 : return GetDoc()->GetSections().GetPos( pFmt );
152 : }
153 :
154 0 : const SwSectionFmt& SwEditShell::GetSectionFmt( sal_uInt16 nFmt ) const
155 : {
156 0 : return *GetDoc()->GetSections()[ nFmt ];
157 : }
158 :
159 :
160 0 : void SwEditShell::DelSectionFmt( sal_uInt16 nFmt )
161 : {
162 0 : StartAllAction();
163 0 : GetDoc()->DelSectionFmt( GetDoc()->GetSections()[ nFmt ] );
164 : // rufe das AttrChangeNotify auf der UI-Seite.
165 0 : CallChgLnk();
166 0 : EndAllAction();
167 0 : }
168 :
169 :
170 0 : void SwEditShell::UpdateSection(sal_uInt16 const nSect,
171 : SwSectionData & rNewData, SfxItemSet const*const pAttr)
172 : {
173 0 : StartAllAction();
174 0 : GetDoc()->UpdateSection( nSect, rNewData, pAttr );
175 : // rufe das AttrChangeNotify auf der UI-Seite.
176 0 : CallChgLnk();
177 0 : EndAllAction();
178 0 : }
179 :
180 0 : String SwEditShell::GetUniqueSectionName( const String* pChkStr ) const
181 : {
182 0 : return GetDoc()->GetUniqueSectionName( pChkStr );
183 : }
184 :
185 0 : void SwEditShell::SetSectionAttr( const SfxItemSet& rSet,
186 : SwSectionFmt* pSectFmt )
187 : {
188 0 : if( pSectFmt )
189 0 : _SetSectionAttr( *pSectFmt, rSet );
190 : else
191 : {
192 : // for all section in the selection
193 :
194 0 : FOREACHPAM_START(this)
195 :
196 0 : const SwPosition* pStt = PCURCRSR->Start(),
197 0 : * pEnd = PCURCRSR->End();
198 :
199 0 : const SwSectionNode* pSttSectNd = pStt->nNode.GetNode().FindSectionNode(),
200 0 : * pEndSectNd = pEnd->nNode.GetNode().FindSectionNode();
201 :
202 0 : if( pSttSectNd || pEndSectNd )
203 : {
204 0 : if( pSttSectNd )
205 0 : _SetSectionAttr( *pSttSectNd->GetSection().GetFmt(),
206 0 : rSet );
207 0 : if( pEndSectNd && pSttSectNd != pEndSectNd )
208 0 : _SetSectionAttr( *pEndSectNd->GetSection().GetFmt(),
209 0 : rSet );
210 :
211 0 : if( pSttSectNd && pEndSectNd )
212 : {
213 0 : SwNodeIndex aSIdx( pStt->nNode );
214 0 : SwNodeIndex aEIdx( pEnd->nNode );
215 0 : if( pSttSectNd->EndOfSectionIndex() <
216 0 : pEndSectNd->GetIndex() )
217 : {
218 0 : aSIdx = pSttSectNd->EndOfSectionIndex() + 1;
219 0 : aEIdx = *pEndSectNd;
220 : }
221 :
222 0 : while( aSIdx < aEIdx )
223 : {
224 0 : if( 0 != (pSttSectNd = aSIdx.GetNode().GetSectionNode())
225 0 : || ( aSIdx.GetNode().IsEndNode() &&
226 0 : 0 != ( pSttSectNd = aSIdx.GetNode().
227 0 : StartOfSectionNode()->GetSectionNode())) )
228 0 : _SetSectionAttr( *pSttSectNd->GetSection().GetFmt(),
229 0 : rSet );
230 0 : ++aSIdx;
231 0 : }
232 : }
233 : }
234 :
235 0 : FOREACHPAM_END()
236 : }
237 0 : }
238 :
239 0 : void SwEditShell::_SetSectionAttr( SwSectionFmt& rSectFmt,
240 : const SfxItemSet& rSet )
241 : {
242 0 : StartAllAction();
243 0 : if(SFX_ITEM_SET == rSet.GetItemState(RES_CNTNT, sal_False))
244 : {
245 0 : SfxItemSet aSet(rSet);
246 0 : aSet.ClearItem(RES_CNTNT);
247 0 : GetDoc()->SetAttr( aSet, rSectFmt );
248 : }
249 : else
250 0 : GetDoc()->SetAttr( rSet, rSectFmt );
251 :
252 : // rufe das AttrChangeNotify auf der UI-Seite.
253 0 : CallChgLnk();
254 0 : EndAllAction();
255 0 : }
256 :
257 : // search inside the cursor selection for full selected sections.
258 : // if any part of section in the selection return 0.
259 : // if more than one in the selection return the count
260 0 : sal_uInt16 SwEditShell::GetFullSelectedSectionCount() const
261 : {
262 0 : sal_uInt16 nRet = 0;
263 0 : FOREACHPAM_START(this)
264 :
265 0 : const SwPosition* pStt = PCURCRSR->Start(),
266 0 : * pEnd = PCURCRSR->End();
267 : const SwCntntNode* pCNd;
268 : // check the selection, if Start at Node begin and End at Node end
269 0 : if( pStt->nContent.GetIndex() ||
270 0 : ( 0 == ( pCNd = pEnd->nNode.GetNode().GetCntntNode() )) ||
271 0 : pCNd->Len() != pEnd->nContent.GetIndex() )
272 : {
273 0 : nRet = 0;
274 : break;
275 : }
276 :
277 : // !!!!!!!!!!!!!!!!!!!!!!!!!!
278 : // what about table at start or end ?
279 : // There is no selection possible!
280 : // What about only a table inside the section ?
281 : // There is only a table selection possible!
282 :
283 0 : SwNodeIndex aSIdx( pStt->nNode, -1 ), aEIdx( pEnd->nNode, +1 );
284 0 : if( !aSIdx.GetNode().IsSectionNode() ||
285 0 : !aEIdx.GetNode().IsEndNode() ||
286 0 : !aEIdx.GetNode().StartOfSectionNode()->IsSectionNode() )
287 : {
288 0 : nRet = 0;
289 : break;
290 : }
291 :
292 0 : ++nRet;
293 0 : if( &aSIdx.GetNode() != aEIdx.GetNode().StartOfSectionNode() )
294 0 : ++nRet;
295 :
296 0 : FOREACHPAM_END()
297 0 : return nRet;
298 : }
299 :
300 :
301 : /**
302 : * Find the suitable node for a special insert (alt-enter).
303 : * This should enable inserting text before/after sections and tables.
304 : *
305 : * A node is found if:
306 : * 1) the innermost table/section is not in a write-protected area
307 : * 2) pCurrentPos is at or just before an end node
308 : * (or at or just after a start node)
309 : * 3) there are only start/end nodes between pCurrentPos and the innermost
310 : * table/section
311 : *
312 : * If a suitable node is found, an SwNode* is returned; else it is NULL.
313 : */
314 0 : static const SwNode* lcl_SpecialInsertNode(const SwPosition* pCurrentPos)
315 : {
316 0 : const SwNode* pReturn = NULL;
317 :
318 : // the current position
319 : // const SwPosition* pCurrentPos = GetCrsr()->GetPoint();
320 : OSL_ENSURE( pCurrentPos != NULL, "Strange, we have no position!" );
321 0 : const SwNode& rCurrentNode = pCurrentPos->nNode.GetNode();
322 :
323 :
324 : // find innermost section or table. At the end of this scope,
325 : // pInntermostNode contain the section/table before/after which we should
326 : // insert our empty paragraph, or it will be NULL if none is found.
327 0 : const SwNode* pInnermostNode = NULL;
328 : {
329 0 : const SwNode* pTableNode = rCurrentNode.FindTableNode();
330 0 : const SwNode* pSectionNode = rCurrentNode.FindSectionNode();
331 :
332 : // find the table/section which is close
333 0 : if( pTableNode == NULL )
334 0 : pInnermostNode = pSectionNode;
335 0 : else if ( pSectionNode == NULL )
336 0 : pInnermostNode = pTableNode;
337 : else
338 : {
339 : // compare and choose the larger one
340 : pInnermostNode =
341 0 : ( pSectionNode->GetIndex() > pTableNode->GetIndex() )
342 0 : ? pSectionNode : pTableNode;
343 : }
344 : }
345 :
346 : // The previous version had a check to skip empty read-only sections. Those
347 : // shouldn't occur, so we only need to check whether our pInnermostNode is
348 : // inside a protected area.
349 :
350 : // Now, pInnermostNode is NULL or the innermost section or table node.
351 0 : if( (pInnermostNode != NULL) && !pInnermostNode->IsProtect() )
352 : {
353 : OSL_ENSURE( pInnermostNode->IsTableNode() ||
354 : pInnermostNode->IsSectionNode(), "wrong node found" );
355 : OSL_ENSURE( ( pInnermostNode->GetIndex() <= rCurrentNode.GetIndex() )&&
356 : ( pInnermostNode->EndOfSectionNode()->GetIndex() >=
357 : rCurrentNode.GetIndex() ), "wrong node found" );
358 :
359 : // we now need to find the possible start/end positions
360 :
361 : // we found a start if
362 : // - we're at or just before a start node
363 : // - there are only start nodes between the current and pInnermostNode
364 0 : SwNodeIndex aBegin( pCurrentPos->nNode );
365 0 : if( rCurrentNode.IsCntntNode() &&
366 0 : (pCurrentPos->nContent.GetIndex() == 0))
367 0 : aBegin--;
368 0 : while( (aBegin != pInnermostNode->GetIndex()) &&
369 0 : aBegin.GetNode().IsStartNode() )
370 0 : aBegin--;
371 0 : bool bStart = ( aBegin == pInnermostNode->GetIndex() );
372 :
373 : // we found an end if
374 : // - we're at or just before an end node
375 : // - there are only end nodes between the current node and
376 : // pInnermostNode's end node
377 0 : SwNodeIndex aEnd( pCurrentPos->nNode );
378 0 : if( rCurrentNode.IsCntntNode() &&
379 0 : ( pCurrentPos->nContent.GetIndex() ==
380 0 : rCurrentNode.GetCntntNode()->Len() ) )
381 0 : ++aEnd;
382 0 : while( (aEnd != pInnermostNode->EndOfSectionNode()->GetIndex()) &&
383 0 : aEnd.GetNode().IsEndNode() )
384 0 : ++aEnd;
385 0 : bool bEnd = ( aEnd == pInnermostNode->EndOfSectionNode()->GetIndex() );
386 :
387 : // evalutate result: if both start + end, end is preferred
388 0 : if( bEnd )
389 0 : pReturn = pInnermostNode->EndOfSectionNode();
390 0 : else if ( bStart )
391 0 : pReturn = pInnermostNode;
392 : }
393 :
394 :
395 : OSL_ENSURE( ( pReturn == NULL ) || pReturn->IsStartNode() ||
396 : pReturn->IsEndNode(),
397 : "SpecialInsertNode failed" );
398 0 : return pReturn;
399 : }
400 :
401 :
402 : /** a node can be special-inserted (alt-Enter) whenever lcl_SpecialInsertNode
403 : finds a suitable position
404 : */
405 0 : bool SwEditShell::CanSpecialInsert() const
406 : {
407 0 : return NULL != lcl_SpecialInsertNode( GetCrsr()->GetPoint() );
408 : }
409 :
410 :
411 : /** check whether a node cen be special-inserted (alt-Enter), and do so. Return
412 : whether insertion was possible.
413 : */
414 0 : bool SwEditShell::DoSpecialInsert()
415 : {
416 0 : bool bRet = false;
417 :
418 : // get current node
419 0 : SwPosition* pCursorPos = GetCrsr()->GetPoint();
420 0 : const SwNode* pInsertNode = lcl_SpecialInsertNode( pCursorPos );
421 0 : if( pInsertNode != NULL )
422 : {
423 0 : StartAllAction();
424 :
425 : // adjust insert position to insert before start nodes and after end
426 : // nodes
427 : SwNodeIndex aInsertIndex( *pInsertNode,
428 0 : pInsertNode->IsStartNode() ? -1 : 0 );
429 0 : SwPosition aInsertPos( aInsertIndex );
430 :
431 : // insert a new text node, and set the cursor
432 0 : bRet = GetDoc()->AppendTxtNode( aInsertPos );
433 0 : *pCursorPos = aInsertPos;
434 :
435 : // call AttrChangeNotify for the UI
436 0 : CallChgLnk();
437 :
438 0 : EndAllAction();
439 : }
440 :
441 0 : return bRet;
442 : }
443 :
444 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|