summaryrefslogtreecommitdiffstats
path: root/sw/source/core/edit/edsect.cxx
blob: 070a37a90d2f46750b8fe59727e1d94ffa2a5ef2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <editsh.hxx>
#include <doc.hxx>
#include <IDocumentUndoRedo.hxx>
#include <IDocumentContentOperations.hxx>
#include <pam.hxx>
#include <docary.hxx>
#include <swundo.hxx>
#include <section.hxx>
#include <sectfrm.hxx>
#include <cntfrm.hxx>
#include <tabfrm.hxx>
#include <rootfrm.hxx>
#include <osl/diagnose.h>

SwSection const*
SwEditShell::InsertSection(
        SwSectionData & rNewData, SfxItemSet const*const pAttr)
{
    const SwSection* pRet = nullptr;
    if( !IsTableMode() )
    {
        StartAllAction();
        GetDoc()->GetIDocumentUndoRedo().StartUndo( SwUndoId::INSSECTION, nullptr );

        for(const SwPaM& rPaM : GetCursor()->GetRingContainer())
        {
            SwSection const*const pNew =
                GetDoc()->InsertSwSection( rPaM, rNewData, nullptr, pAttr );
            if( !pRet )
                pRet = pNew;
        }

        GetDoc()->GetIDocumentUndoRedo().EndUndo( SwUndoId::INSSECTION, nullptr );
        EndAllAction();
    }
    return pRet;
}

bool SwEditShell::IsInsRegionAvailable() const
{
    if( IsTableMode() )
        return false;
    SwPaM* pCursor = GetCursor();
    if( pCursor->GetNext() != pCursor )
        return false;
    if( pCursor->HasMark() )
        return 0 != SwDoc::IsInsRegionAvailable( *pCursor );

    return true;
}

const SwSection* SwEditShell::GetCurrSection() const
{
    if( IsTableMode() )
        return nullptr;

    return SwDoc::GetCurrSection( *GetCursor()->GetPoint() );
}

/** Deliver the responsible area of the columns.
 *
 * In footnotes it may not be the area within the footnote.
 */
SwSection* SwEditShell::GetAnySection( bool bOutOfTab, const Point* pPt )
{
    SwFrame *pFrame;
    if ( pPt )
    {
        SwPosition aPos( *GetCursor()->GetPoint() );
        Point aPt( *pPt );
        GetLayout()->GetModelPositionForViewPoint( &aPos, aPt );
        SwContentNode *pNd = aPos.GetNode().GetContentNode();
        std::pair<Point, bool> const tmp(*pPt, true);
        pFrame = pNd->getLayoutFrame(GetLayout(), nullptr, &tmp);
    }
    else
        pFrame = GetCurrFrame( false );

    if( bOutOfTab && pFrame )
        pFrame = pFrame->FindTabFrame();
    if( pFrame && pFrame->IsInSct() )
    {
        SwSectionFrame* pSect = pFrame->FindSctFrame();
        OSL_ENSURE( pSect, "GetAnySection: Where's my Sect?" );
        if( pSect->IsInFootnote() && pSect->GetUpper()->IsInSct() )
        {
            pSect = pSect->GetUpper()->FindSctFrame();
            OSL_ENSURE( pSect, "GetAnySection: Where's my SectFrame?" );
        }
        return pSect->GetSection();
    }
    return nullptr;
}

size_t SwEditShell::GetSectionFormatCount() const
{
    return GetDoc()->GetSections().size();
}

bool SwEditShell::IsAnySectionInDoc() const
{
    const SwSectionFormats& rFormats = GetDoc()->GetSections();

    for( const SwSectionFormat* pFormat : rFormats )
    {
        SectionType eTmpType;
        if( pFormat->IsInNodesArr() &&
            ( (eTmpType = pFormat->GetSection()->GetType()) != SectionType::ToxContent
               && SectionType::ToxHeader != eTmpType ) )
        {
            return true;
        }
    }
    return false;
}

size_t SwEditShell::GetSectionFormatPos( const SwSectionFormat& rFormat ) const
{
    SwSectionFormat* pFormat = const_cast<SwSectionFormat*>(&rFormat);
    return GetDoc()->GetSections().GetPos( pFormat );
}

const SwSectionFormat& SwEditShell::GetSectionFormat(size_t nFormat) const
{
    return *GetDoc()->GetSections()[ nFormat ];
}

void SwEditShell::DelSectionFormat(size_t nFormat)
{
    StartAllAction();
    GetDoc()->DelSectionFormat( GetDoc()->GetSections()[ nFormat ] );
    // Call the AttrChangeNotify on the UI page.
    CallChgLnk();
    EndAllAction();
}

void SwEditShell::UpdateSection(size_t const nSect,
        SwSectionData & rNewData, SfxItemSet const*const pAttr)
{
    StartAllAction();
    GetDoc()->UpdateSection( nSect, rNewData, pAttr );
    // Call the AttrChangeNotify on the UI page.
    CallChgLnk();
    EndAllAction();
}

OUString SwEditShell::GetUniqueSectionName( const OUString* pChkStr ) const
{
    return GetDoc()->GetUniqueSectionName( pChkStr );
}

void SwEditShell::SetSectionAttr( const SfxItemSet& rSet,
                                    SwSectionFormat* pSectFormat )
{
    if( pSectFormat )
        SetSectionAttr_( *pSectFormat, rSet );
    else
    {
        // for all section in the selection

        for(SwPaM& rPaM : GetCursor()->GetRingContainer())
        {
            auto [pStt, pEnd] = rPaM.StartEnd(); // SwPosition*

            SwSectionNode* pSttSectNd = pStt->GetNode().FindSectionNode(),
                               * pEndSectNd = pEnd->GetNode().FindSectionNode();

            if( pSttSectNd || pEndSectNd )
            {
                if( pSttSectNd )
                    SetSectionAttr_( *pSttSectNd->GetSection().GetFormat(),
                                    rSet );
                if( pEndSectNd && pSttSectNd != pEndSectNd )
                    SetSectionAttr_( *pEndSectNd->GetSection().GetFormat(),
                                    rSet );

                if( pSttSectNd && pEndSectNd )
                {
                    SwNodeIndex aSIdx( pStt->GetNode() );
                    SwNodeIndex aEIdx( pEnd->GetNode() );
                    if( pSttSectNd->EndOfSectionIndex() <
                        pEndSectNd->GetIndex() )
                    {
                        aSIdx = pSttSectNd->EndOfSectionIndex() + 1;
                        aEIdx = *pEndSectNd;
                    }

                    while( aSIdx < aEIdx )
                    {
                        if( nullptr != (pSttSectNd = aSIdx.GetNode().GetSectionNode())
                            || ( aSIdx.GetNode().IsEndNode() &&
                                nullptr != ( pSttSectNd = aSIdx.GetNode().
                                    StartOfSectionNode()->GetSectionNode())) )
                            SetSectionAttr_( *pSttSectNd->GetSection().GetFormat(),
                                            rSet );
                        ++aSIdx;
                    }
                }
            }

        }
    }
}

void SwEditShell::SetSectionAttr_( SwSectionFormat& rSectFormat,
                                    const SfxItemSet& rSet )
{
    StartAllAction();
    if(SfxItemState::SET == rSet.GetItemState(RES_CNTNT, false))
    {
        SfxItemSet aSet(rSet);
        aSet.ClearItem(RES_CNTNT);
        GetDoc()->SetAttr( aSet, rSectFormat );
    }
    else
        GetDoc()->SetAttr( rSet, rSectFormat );

    // Call the AttrChangeNotify on the UI page.
    CallChgLnk();
    EndAllAction();
}

/** Search inside the cursor selection for full selected sections.
 *
 * @return If any part of section in the selection return 0, if more than one return the count.
 */
sal_uInt16 SwEditShell::GetFullSelectedSectionCount() const
{
    sal_uInt16 nRet = 0;
    for(SwPaM& rPaM : GetCursor()->GetRingContainer())
    {

        auto [pStt, pEnd] = rPaM.StartEnd(); // SwPosition*
        const SwContentNode* pCNd;
        // check the selection, if Start at Node begin and End at Node end
        if( pStt->GetContentIndex() ||
            ( nullptr == ( pCNd = pEnd->GetNode().GetContentNode() )) ||
            pCNd->Len() != pEnd->GetContentIndex() )
        {
            nRet = 0;
            break;
        }

// !!!
// what about table at start or end ?
//      There is no selection possible!
// What about only a table inside the section ?
//      There is only a table selection possible!

        SwNodeIndex aSIdx( pStt->GetNode(), -1 ), aEIdx( pEnd->GetNode(), +1 );
        if( !aSIdx.GetNode().IsSectionNode() ||
            !aEIdx.GetNode().IsEndNode() ||
            !aEIdx.GetNode().StartOfSectionNode()->IsSectionNode() )
        {
            nRet = 0;
            break;
        }

        ++nRet;
        if( &aSIdx.GetNode() != aEIdx.GetNode().StartOfSectionNode() )
            ++nRet;

    }
    return nRet;
}

/** Find the suitable node for a special insert (alt-enter).
 *
 * This should enable inserting text before/after sections and tables.
 *
 * A node is found if:
 * 1) the innermost table/section is not in a write-protected area
 * 2) pCurrentPos is at or just before an end node
 *    (or at or just after a start node)
 * 3) there are only start/end nodes between pCurrentPos and the innermost
 *    table/section
 *
 * If a suitable node is found, an SwNode* is returned; else it is NULL.
 */
static const SwNode* lcl_SpecialInsertNode(const SwPosition* pCurrentPos)
{
    const SwNode* pReturn = nullptr;

    // the current position
    OSL_ENSURE( pCurrentPos != nullptr, "Strange, we have no position!" );
    const SwNode& rCurrentNode = pCurrentPos->GetNode();

    // find innermost section or table.  At the end of this scope,
    // pInnermostNode contains the section/table before/after which we should
    // insert our empty paragraph, or it will be NULL if none is found.
    const SwNode* pInnermostNode = nullptr;
    const SwSection* pSection = nullptr;
    {
        const SwNode* pTableNode = rCurrentNode.FindTableNode();
        const SwNode* pSectionNode = rCurrentNode.FindSectionNode();

        // find the table/section which is close
        if( pTableNode == nullptr )
        {
            if( pSectionNode == nullptr )
                return nullptr;

            pInnermostNode = pSectionNode;
            pSection = &static_cast<const SwSectionNode*>(pSectionNode)->GetSection();
        }
        else if ( pSectionNode == nullptr )
            pInnermostNode = pTableNode;
        else
        {
            // compare and choose the larger one
            if (pSectionNode->GetIndex() > pTableNode->GetIndex())
            {
                pInnermostNode = pSectionNode;
                pSection = &static_cast<const SwSectionNode*>(pSectionNode)->GetSection();
            }
            else
                pInnermostNode = pTableNode;
        }
    }
    if(pInnermostNode != nullptr)
    {
        bool bIsProtected = pInnermostNode->IsProtect();

        //special case - ToxSection
        // - in this case the inner section could be tox header
        //   section but the new node should be before the content section
        //   protection of the tox should not prevent the insertion
        //   only protection outside needs to be checked
        if( pSection &&
            (SectionType::ToxHeader == pSection->GetType() ||
             SectionType::ToxContent == pSection->GetType()))
        {
            if (SectionType::ToxHeader == pSection->GetType())
            {
                if (const SwSection* pSectionParent = pSection->GetParent())
                    pInnermostNode = pSectionParent->GetFormat()->GetSectionNode();
            }
            bIsProtected = static_cast<const SwSectionNode*>(pInnermostNode)->IsInProtectSect();
        }

        // The previous version had a check to skip empty read-only sections. Those
        // shouldn't occur, so we only need to check whether our pInnermostNode is
        // inside a protected area.

        // Now, pInnermostNode is NULL or the innermost section or table node.
        if(!bIsProtected)
        {
            OSL_ENSURE( pInnermostNode->IsTableNode() ||
                        pInnermostNode->IsSectionNode(), "wrong node found" );
            OSL_ENSURE( ( pInnermostNode->GetIndex() <= rCurrentNode.GetIndex() )&&
                        ( pInnermostNode->EndOfSectionNode()->GetIndex() >=
                          rCurrentNode.GetIndex() ), "wrong node found" );

            // we now need to find the possible start/end positions

            // we found a start if
            // - we're at or just before a start node
            // - there are only start nodes between the current and pInnermostNode
            SwNodeIndex aBegin( pCurrentPos->GetNode() );
            if( rCurrentNode.IsContentNode() &&
                (pCurrentPos->GetContentIndex() == 0))
                --aBegin;
            while( (aBegin != pInnermostNode->GetIndex()) &&
                   aBegin.GetNode().IsStartNode() )
                --aBegin;
            bool bStart = ( aBegin == pInnermostNode->GetIndex() );

            // we found an end if
            // - we're at or just before an end node
            // - there are only end nodes between the current node and
            //   pInnermostNode's end node or
            // - there are only end nodes between the last table cell merged with
            //   the current cell and pInnermostNode's end node
            SwNodeIndex aEnd( pCurrentPos->GetNode() );
            if( rCurrentNode.IsContentNode() &&
                ( pCurrentPos->GetContentIndex() ==
                  rCurrentNode.GetContentNode()->Len() ) )
            {
                ++aEnd;

                // tdf#156492 handle cells merged vertically in the bottom right corner
                if ( pInnermostNode->IsTableNode() )
                {
                    const SwNode* pTableBoxStartNode = pCurrentPos->GetNode().FindTableBoxStartNode();
                    const SwTableBox* pTableBox = pTableBoxStartNode->GetTableBox();
                    if ( pTableBox && pTableBox->getRowSpan() > 1 )
                    {
                        const SwTableNode* pTableNd = pInnermostNode->FindTableNode();
                        pTableBox = & pTableBox->FindEndOfRowSpan( pTableNd->GetTable(),
                                                                            pTableBox->getRowSpan() );
                        pTableBoxStartNode = pTableBox->GetSttNd();
                        aEnd = pTableBoxStartNode->GetIndex() + 2;
                    }
                }
            }
            while( (aEnd != pInnermostNode->EndOfSectionNode()->GetIndex()) &&
                   aEnd.GetNode().IsEndNode() )
                ++aEnd;
            bool bEnd = ( aEnd == pInnermostNode->EndOfSectionNode()->GetIndex() );

            // evaluate result: if both start + end, end is preferred
            if( bEnd )
                pReturn = pInnermostNode->EndOfSectionNode();
            else if ( bStart )
                pReturn = pInnermostNode;
        }
    }

    OSL_ENSURE( ( pReturn == nullptr ) || pReturn->IsStartNode() ||
                                       pReturn->IsEndNode(),
                "SpecialInsertNode failed" );
    return pReturn;
}

/** a node can be special-inserted (alt-Enter) whenever lcl_SpecialInsertNode
    finds a suitable position
*/
bool SwEditShell::CanSpecialInsert() const
{
    return nullptr != lcl_SpecialInsertNode( GetCursor()->GetPoint() );
}

/** check whether a node can be special-inserted (alt-Enter), and do so. Return
    whether insertion was possible.
 */
void SwEditShell::DoSpecialInsert()
{
    // get current node
    SwPosition* pCursorPos = GetCursor()->GetPoint();
    const SwNode* pInsertNode = lcl_SpecialInsertNode( pCursorPos );
    if( pInsertNode == nullptr )
        return;

    StartAllAction();

    // adjust insert position to insert before start nodes and after end
    // nodes
    SwNodeIndex aInsertIndex( *pInsertNode,
                              SwNodeOffset(pInsertNode->IsStartNode() ? -1 : 0) );
    SwPosition aInsertPos( aInsertIndex );

    // insert a new text node, and set the cursor
    GetDoc()->getIDocumentContentOperations().AppendTextNode( aInsertPos );
    *pCursorPos = aInsertPos;

    // call AttrChangeNotify for the UI
    CallChgLnk();

    EndAllAction();
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */