Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sw / source / core / edit / edsect.cxx
bloba7e652aea9dee4f894db9682ea9bff29459ee948
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <editsh.hxx>
21 #include <doc.hxx>
22 #include <IDocumentUndoRedo.hxx>
23 #include <IDocumentContentOperations.hxx>
24 #include <pam.hxx>
25 #include <docary.hxx>
26 #include <swundo.hxx>
27 #include <section.hxx>
28 #include <sectfrm.hxx>
29 #include <cntfrm.hxx>
30 #include <tabfrm.hxx>
31 #include <rootfrm.hxx>
32 #include <osl/diagnose.h>
34 SwSection const*
35 SwEditShell::InsertSection(
36 SwSectionData & rNewData, SfxItemSet const*const pAttr)
38 const SwSection* pRet = nullptr;
39 if( !IsTableMode() )
41 StartAllAction();
42 GetDoc()->GetIDocumentUndoRedo().StartUndo( SwUndoId::INSSECTION, nullptr );
44 for(const SwPaM& rPaM : GetCursor()->GetRingContainer())
46 SwSection const*const pNew =
47 GetDoc()->InsertSwSection( rPaM, rNewData, nullptr, pAttr );
48 if( !pRet )
49 pRet = pNew;
52 GetDoc()->GetIDocumentUndoRedo().EndUndo( SwUndoId::INSSECTION, nullptr );
53 EndAllAction();
55 return pRet;
58 bool SwEditShell::IsInsRegionAvailable() const
60 if( IsTableMode() )
61 return false;
62 SwPaM* pCursor = GetCursor();
63 if( pCursor->GetNext() != pCursor )
64 return false;
65 if( pCursor->HasMark() )
66 return 0 != SwDoc::IsInsRegionAvailable( *pCursor );
68 return true;
71 const SwSection* SwEditShell::GetCurrSection() const
73 if( IsTableMode() )
74 return nullptr;
76 return SwDoc::GetCurrSection( *GetCursor()->GetPoint() );
79 /** Deliver the responsible area of the columns.
81 * In footnotes it may not be the area within the footnote.
83 SwSection* SwEditShell::GetAnySection( bool bOutOfTab, const Point* pPt )
85 SwFrame *pFrame;
86 if ( pPt )
88 SwPosition aPos( *GetCursor()->GetPoint() );
89 Point aPt( *pPt );
90 GetLayout()->GetModelPositionForViewPoint( &aPos, aPt );
91 SwContentNode *pNd = aPos.GetNode().GetContentNode();
92 std::pair<Point, bool> const tmp(*pPt, true);
93 pFrame = pNd->getLayoutFrame(GetLayout(), nullptr, &tmp);
95 else
96 pFrame = GetCurrFrame( false );
98 if( bOutOfTab && pFrame )
99 pFrame = pFrame->FindTabFrame();
100 if( pFrame && pFrame->IsInSct() )
102 SwSectionFrame* pSect = pFrame->FindSctFrame();
103 OSL_ENSURE( pSect, "GetAnySection: Where's my Sect?" );
104 if( pSect->IsInFootnote() && pSect->GetUpper()->IsInSct() )
106 pSect = pSect->GetUpper()->FindSctFrame();
107 OSL_ENSURE( pSect, "GetAnySection: Where's my SectFrame?" );
109 return pSect->GetSection();
111 return nullptr;
114 size_t SwEditShell::GetSectionFormatCount() const
116 return GetDoc()->GetSections().size();
119 bool SwEditShell::IsAnySectionInDoc() const
121 const SwSectionFormats& rFormats = GetDoc()->GetSections();
123 for( const SwSectionFormat* pFormat : rFormats )
125 SectionType eTmpType;
126 if( pFormat->IsInNodesArr() &&
127 ( (eTmpType = pFormat->GetSection()->GetType()) != SectionType::ToxContent
128 && SectionType::ToxHeader != eTmpType ) )
130 return true;
133 return false;
136 size_t SwEditShell::GetSectionFormatPos( const SwSectionFormat& rFormat ) const
138 SwSectionFormat* pFormat = const_cast<SwSectionFormat*>(&rFormat);
139 return GetDoc()->GetSections().GetPos( pFormat );
142 const SwSectionFormat& SwEditShell::GetSectionFormat(size_t nFormat) const
144 return *GetDoc()->GetSections()[ nFormat ];
147 void SwEditShell::DelSectionFormat(size_t nFormat)
149 StartAllAction();
150 GetDoc()->DelSectionFormat( GetDoc()->GetSections()[ nFormat ] );
151 // Call the AttrChangeNotify on the UI page.
152 CallChgLnk();
153 EndAllAction();
156 void SwEditShell::UpdateSection(size_t const nSect,
157 SwSectionData & rNewData, SfxItemSet const*const pAttr)
159 StartAllAction();
160 GetDoc()->UpdateSection( nSect, rNewData, pAttr );
161 // Call the AttrChangeNotify on the UI page.
162 CallChgLnk();
163 EndAllAction();
166 OUString SwEditShell::GetUniqueSectionName( const OUString* pChkStr ) const
168 return GetDoc()->GetUniqueSectionName( pChkStr );
171 void SwEditShell::SetSectionAttr( const SfxItemSet& rSet,
172 SwSectionFormat* pSectFormat )
174 if( pSectFormat )
175 SetSectionAttr_( *pSectFormat, rSet );
176 else
178 // for all section in the selection
180 for(SwPaM& rPaM : GetCursor()->GetRingContainer())
182 auto [pStt, pEnd] = rPaM.StartEnd(); // SwPosition*
184 SwSectionNode* pSttSectNd = pStt->GetNode().FindSectionNode(),
185 * pEndSectNd = pEnd->GetNode().FindSectionNode();
187 if( pSttSectNd || pEndSectNd )
189 if( pSttSectNd )
190 SetSectionAttr_( *pSttSectNd->GetSection().GetFormat(),
191 rSet );
192 if( pEndSectNd && pSttSectNd != pEndSectNd )
193 SetSectionAttr_( *pEndSectNd->GetSection().GetFormat(),
194 rSet );
196 if( pSttSectNd && pEndSectNd )
198 SwNodeIndex aSIdx( pStt->GetNode() );
199 SwNodeIndex aEIdx( pEnd->GetNode() );
200 if( pSttSectNd->EndOfSectionIndex() <
201 pEndSectNd->GetIndex() )
203 aSIdx = pSttSectNd->EndOfSectionIndex() + 1;
204 aEIdx = *pEndSectNd;
207 while( aSIdx < aEIdx )
209 if( nullptr != (pSttSectNd = aSIdx.GetNode().GetSectionNode())
210 || ( aSIdx.GetNode().IsEndNode() &&
211 nullptr != ( pSttSectNd = aSIdx.GetNode().
212 StartOfSectionNode()->GetSectionNode())) )
213 SetSectionAttr_( *pSttSectNd->GetSection().GetFormat(),
214 rSet );
215 ++aSIdx;
224 void SwEditShell::SetSectionAttr_( SwSectionFormat& rSectFormat,
225 const SfxItemSet& rSet )
227 StartAllAction();
228 if(SfxItemState::SET == rSet.GetItemState(RES_CNTNT, false))
230 SfxItemSet aSet(rSet);
231 aSet.ClearItem(RES_CNTNT);
232 GetDoc()->SetAttr( aSet, rSectFormat );
234 else
235 GetDoc()->SetAttr( rSet, rSectFormat );
237 // Call the AttrChangeNotify on the UI page.
238 CallChgLnk();
239 EndAllAction();
242 /** Search inside the cursor selection for full selected sections.
244 * @return If any part of section in the selection return 0, if more than one return the count.
246 sal_uInt16 SwEditShell::GetFullSelectedSectionCount() const
248 sal_uInt16 nRet = 0;
249 for(SwPaM& rPaM : GetCursor()->GetRingContainer())
252 auto [pStt, pEnd] = rPaM.StartEnd(); // SwPosition*
253 const SwContentNode* pCNd;
254 // check the selection, if Start at Node begin and End at Node end
255 if( pStt->GetContentIndex() ||
256 ( nullptr == ( pCNd = pEnd->GetNode().GetContentNode() )) ||
257 pCNd->Len() != pEnd->GetContentIndex() )
259 nRet = 0;
260 break;
263 // !!!
264 // what about table at start or end ?
265 // There is no selection possible!
266 // What about only a table inside the section ?
267 // There is only a table selection possible!
269 SwNodeIndex aSIdx( pStt->GetNode(), -1 ), aEIdx( pEnd->GetNode(), +1 );
270 if( !aSIdx.GetNode().IsSectionNode() ||
271 !aEIdx.GetNode().IsEndNode() ||
272 !aEIdx.GetNode().StartOfSectionNode()->IsSectionNode() )
274 nRet = 0;
275 break;
278 ++nRet;
279 if( &aSIdx.GetNode() != aEIdx.GetNode().StartOfSectionNode() )
280 ++nRet;
283 return nRet;
286 /** Find the suitable node for a special insert (alt-enter).
288 * This should enable inserting text before/after sections and tables.
290 * A node is found if:
291 * 1) the innermost table/section is not in a write-protected area
292 * 2) pCurrentPos is at or just before an end node
293 * (or at or just after a start node)
294 * 3) there are only start/end nodes between pCurrentPos and the innermost
295 * table/section
297 * If a suitable node is found, an SwNode* is returned; else it is NULL.
299 static const SwNode* lcl_SpecialInsertNode(const SwPosition* pCurrentPos)
301 const SwNode* pReturn = nullptr;
303 // the current position
304 OSL_ENSURE( pCurrentPos != nullptr, "Strange, we have no position!" );
305 const SwNode& rCurrentNode = pCurrentPos->GetNode();
307 // find innermost section or table. At the end of this scope,
308 // pInnermostNode contains the section/table before/after which we should
309 // insert our empty paragraph, or it will be NULL if none is found.
310 const SwNode* pInnermostNode = nullptr;
312 const SwNode* pTableNode = rCurrentNode.FindTableNode();
313 const SwNode* pSectionNode = rCurrentNode.FindSectionNode();
315 // find the table/section which is close
316 if( pTableNode == nullptr )
317 pInnermostNode = pSectionNode;
318 else if ( pSectionNode == nullptr )
319 pInnermostNode = pTableNode;
320 else
322 // compare and choose the larger one
323 pInnermostNode =
324 ( pSectionNode->GetIndex() > pTableNode->GetIndex() )
325 ? pSectionNode : pTableNode;
329 // The previous version had a check to skip empty read-only sections. Those
330 // shouldn't occur, so we only need to check whether our pInnermostNode is
331 // inside a protected area.
333 // Now, pInnermostNode is NULL or the innermost section or table node.
334 if( (pInnermostNode != nullptr) && !pInnermostNode->IsProtect() )
336 OSL_ENSURE( pInnermostNode->IsTableNode() ||
337 pInnermostNode->IsSectionNode(), "wrong node found" );
338 OSL_ENSURE( ( pInnermostNode->GetIndex() <= rCurrentNode.GetIndex() )&&
339 ( pInnermostNode->EndOfSectionNode()->GetIndex() >=
340 rCurrentNode.GetIndex() ), "wrong node found" );
342 // we now need to find the possible start/end positions
344 // we found a start if
345 // - we're at or just before a start node
346 // - there are only start nodes between the current and pInnermostNode
347 SwNodeIndex aBegin( pCurrentPos->GetNode() );
348 if( rCurrentNode.IsContentNode() &&
349 (pCurrentPos->GetContentIndex() == 0))
350 --aBegin;
351 while( (aBegin != pInnermostNode->GetIndex()) &&
352 aBegin.GetNode().IsStartNode() )
353 --aBegin;
354 bool bStart = ( aBegin == pInnermostNode->GetIndex() );
356 // we found an end if
357 // - we're at or just before an end node
358 // - there are only end nodes between the current node and
359 // pInnermostNode's end node or
360 // - there are only end nodes between the last table cell merged with
361 // the current cell and pInnermostNode's end node
362 SwNodeIndex aEnd( pCurrentPos->GetNode() );
363 if( rCurrentNode.IsContentNode() &&
364 ( pCurrentPos->GetContentIndex() ==
365 rCurrentNode.GetContentNode()->Len() ) )
367 ++aEnd;
369 // tdf#156492 handle cells merged vertically in the bottom right corner
370 if ( pInnermostNode->IsTableNode() )
372 const SwNode* pTableBoxStartNode = pCurrentPos->GetNode().FindTableBoxStartNode();
373 const SwTableBox* pTableBox = pTableBoxStartNode->GetTableBox();
374 if ( pTableBox && pTableBox->getRowSpan() > 1 )
376 const SwTableNode* pTableNd = pInnermostNode->FindTableNode();
377 pTableBox = & pTableBox->FindEndOfRowSpan( pTableNd->GetTable(),
378 pTableBox->getRowSpan() );
379 pTableBoxStartNode = pTableBox->GetSttNd();
380 aEnd = pTableBoxStartNode->GetIndex() + 2;
384 while( (aEnd != pInnermostNode->EndOfSectionNode()->GetIndex()) &&
385 aEnd.GetNode().IsEndNode() )
386 ++aEnd;
387 bool bEnd = ( aEnd == pInnermostNode->EndOfSectionNode()->GetIndex() );
389 // evaluate result: if both start + end, end is preferred
390 if( bEnd )
391 pReturn = pInnermostNode->EndOfSectionNode();
392 else if ( bStart )
393 pReturn = pInnermostNode;
396 OSL_ENSURE( ( pReturn == nullptr ) || pReturn->IsStartNode() ||
397 pReturn->IsEndNode(),
398 "SpecialInsertNode failed" );
399 return pReturn;
402 /** a node can be special-inserted (alt-Enter) whenever lcl_SpecialInsertNode
403 finds a suitable position
405 bool SwEditShell::CanSpecialInsert() const
407 return nullptr != lcl_SpecialInsertNode( GetCursor()->GetPoint() );
410 /** check whether a node can be special-inserted (alt-Enter), and do so. Return
411 whether insertion was possible.
413 void SwEditShell::DoSpecialInsert()
415 // get current node
416 SwPosition* pCursorPos = GetCursor()->GetPoint();
417 const SwNode* pInsertNode = lcl_SpecialInsertNode( pCursorPos );
418 if( pInsertNode == nullptr )
419 return;
421 StartAllAction();
423 // adjust insert position to insert before start nodes and after end
424 // nodes
425 SwNodeIndex aInsertIndex( *pInsertNode,
426 SwNodeOffset(pInsertNode->IsStartNode() ? -1 : 0) );
427 SwPosition aInsertPos( aInsertIndex );
429 // insert a new text node, and set the cursor
430 GetDoc()->getIDocumentContentOperations().AppendTextNode( aInsertPos );
431 *pCursorPos = aInsertPos;
433 // call AttrChangeNotify for the UI
434 CallChgLnk();
436 EndAllAction();
439 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */