1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
22 #include <IDocumentUndoRedo.hxx>
23 #include <IDocumentContentOperations.hxx>
27 #include <section.hxx>
28 #include <sectfrm.hxx>
31 #include <rootfrm.hxx>
32 #include <osl/diagnose.h>
35 SwEditShell::InsertSection(
36 SwSectionData
& rNewData
, SfxItemSet
const*const pAttr
)
38 const SwSection
* pRet
= nullptr;
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
);
52 GetDoc()->GetIDocumentUndoRedo().EndUndo( SwUndoId::INSSECTION
, nullptr );
58 bool SwEditShell::IsInsRegionAvailable() const
62 SwPaM
* pCursor
= GetCursor();
63 if( pCursor
->GetNext() != pCursor
)
65 if( pCursor
->HasMark() )
66 return 0 != SwDoc::IsInsRegionAvailable( *pCursor
);
71 const SwSection
* SwEditShell::GetCurrSection() const
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
)
88 SwPosition
aPos( *GetCursor()->GetPoint() );
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
);
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();
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
) )
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
)
150 GetDoc()->DelSectionFormat( GetDoc()->GetSections()[ nFormat
] );
151 // Call the AttrChangeNotify on the UI page.
156 void SwEditShell::UpdateSection(size_t const nSect
,
157 SwSectionData
& rNewData
, SfxItemSet
const*const pAttr
)
160 GetDoc()->UpdateSection( nSect
, rNewData
, pAttr
);
161 // Call the AttrChangeNotify on the UI page.
166 OUString
SwEditShell::GetUniqueSectionName( const OUString
* pChkStr
) const
168 return GetDoc()->GetUniqueSectionName( pChkStr
);
171 void SwEditShell::SetSectionAttr( const SfxItemSet
& rSet
,
172 SwSectionFormat
* pSectFormat
)
175 SetSectionAttr_( *pSectFormat
, rSet
);
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
)
190 SetSectionAttr_( *pSttSectNd
->GetSection().GetFormat(),
192 if( pEndSectNd
&& pSttSectNd
!= pEndSectNd
)
193 SetSectionAttr_( *pEndSectNd
->GetSection().GetFormat(),
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;
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(),
224 void SwEditShell::SetSectionAttr_( SwSectionFormat
& rSectFormat
,
225 const SfxItemSet
& rSet
)
228 if(SfxItemState::SET
== rSet
.GetItemState(RES_CNTNT
, false))
230 SfxItemSet
aSet(rSet
);
231 aSet
.ClearItem(RES_CNTNT
);
232 GetDoc()->SetAttr( aSet
, rSectFormat
);
235 GetDoc()->SetAttr( rSet
, rSectFormat
);
237 // Call the AttrChangeNotify on the UI page.
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
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() )
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() )
279 if( &aSIdx
.GetNode() != aEIdx
.GetNode().StartOfSectionNode() )
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
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
;
322 // compare and choose the larger one
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))
351 while( (aBegin
!= pInnermostNode
->GetIndex()) &&
352 aBegin
.GetNode().IsStartNode() )
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() ) )
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() )
387 bool bEnd
= ( aEnd
== pInnermostNode
->EndOfSectionNode()->GetIndex() );
389 // evaluate result: if both start + end, end is preferred
391 pReturn
= pInnermostNode
->EndOfSectionNode();
393 pReturn
= pInnermostNode
;
396 OSL_ENSURE( ( pReturn
== nullptr ) || pReturn
->IsStartNode() ||
397 pReturn
->IsEndNode(),
398 "SpecialInsertNode failed" );
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()
416 SwPosition
* pCursorPos
= GetCursor()->GetPoint();
417 const SwNode
* pInsertNode
= lcl_SpecialInsertNode( pCursorPos
);
418 if( pInsertNode
== nullptr )
423 // adjust insert position to insert before start nodes and after end
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
439 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */