Coverage Report

Created: 2026-10-14 09:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/sw/source/uibase/shells/textidx.cxx
Line
Count
Source
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 <hintids.hxx>
21
#include <sfx2/request.hxx>
22
#include <sfx2/viewfrm.hxx>
23
#include <svl/eitem.hxx>
24
#include <editeng/sizeitem.hxx>
25
#include <osl/diagnose.h>
26
#include <vcl/weld/Window.hxx>
27
#include <fmtfsize.hxx>
28
#include <fldbas.hxx>
29
#include <uiitems.hxx>
30
#include <viewopt.hxx>
31
#include <cmdid.h>
32
#include <view.hxx>
33
#include <wrtsh.hxx>
34
#include <doc.hxx>
35
#include <textsh.hxx>
36
#include <idxmrk.hxx>
37
#include <toxmgr.hxx>
38
#include <swabstdlg.hxx>
39
#include <strings.hrc>
40
#include <svl/whiter.hxx>
41
42
#include <ndtxt.hxx>
43
#include <fmtfld.hxx>
44
#include <IDocumentFieldsAccess.hxx>
45
46
void SwTextShell::ExecIdx(SfxRequest const &rReq)
47
0
{
48
0
    const SfxItemSet* pArgs = rReq.GetArgs();
49
0
    const SfxPoolItem* pItem = nullptr;
50
0
    const sal_uInt16 nSlot = rReq.GetSlot();
51
0
    if (pArgs)
52
0
        pArgs->GetItemState(nSlot, false, &pItem);
53
54
0
    SfxViewFrame& rVFrame = GetView().GetViewFrame();
55
56
0
    switch (nSlot)
57
0
    {
58
0
        case FN_EDIT_AUTH_ENTRY_DLG:
59
0
        {
60
0
            SwWrtShell& rShell = GetShell();
61
62
0
            const bool bWasViewLocked = rShell.IsViewLocked();
63
0
            rShell.LockView(true);
64
65
0
            if (const SwField* const pCurrentField = rShell.GetCurField();
66
0
                !rShell.HasReadonlySel() && pCurrentField != nullptr
67
0
                && pCurrentField->GetTyp()->Which() == SwFieldIds::TableOfAuthorities)
68
0
            {
69
                // Since the cursor is on a bibliography mark (e.g. "[1]"), open the edit dialog as usual
70
0
                SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create();
71
0
                ScopedVclPtr<VclAbstractDialog> pDlg(
72
0
                    pFact->CreateSwAutoMarkDialog(GetView().GetFrameWeld(), rShell));
73
0
                pDlg->Execute();
74
0
            }
75
0
            else if (const SwTOXBase* const pCurrentTOX = rShell.GetCurTOX();
76
0
                     pCurrentTOX != nullptr && pCurrentTOX->GetType() == TOX_AUTHORITIES
77
0
                     && (rShell.GetCursor()->GetPoint()->GetNode()
78
0
                            .FindSectionNode()->GetSection().GetType()
79
0
                         == SectionType::ToxContent))
80
0
            {
81
                // Since the cursor is in the bibliography table, find the first mark that would match the given row
82
0
                const SwNode* const pTableRowNode = &rShell.GetCursor()->GetPoint()->GetNode();
83
0
                const OUString& rTableRowText
84
0
                    = static_cast<const SwTextNode*>(pTableRowNode)->GetText();
85
86
0
                const SwFieldType* pAuthField
87
0
                    = rShell.GetDoc()->getIDocumentFieldsAccess().GetFieldType(
88
0
                        SwFieldIds::TableOfAuthorities, OUString(), false);
89
90
0
                if (pAuthField)
91
0
                {
92
0
                    bool bMatchingMarkFound = false;
93
0
                    {
94
0
                        std::vector<SwFormatField*> vFields;
95
0
                        pAuthField->GatherFields(vFields);
96
0
                        for (auto pFormatField : vFields)
97
0
                        {
98
0
                            if (const SwField* pIteratedField = nullptr;
99
0
                                pFormatField != nullptr
100
0
                                && (pIteratedField = pFormatField->GetField()) != nullptr
101
0
                                && (pIteratedField->GetTyp()->Which()
102
0
                                    == SwFieldIds::TableOfAuthorities))
103
0
                            {
104
0
                                OUString sMarkText
105
0
                                    = static_cast<const SwAuthorityField*>(pIteratedField)
106
0
                                          ->GetAuthority(rShell.GetLayout(),
107
0
                                                         &pCurrentTOX->GetTOXForm());
108
109
0
                                if (sMarkText == rTableRowText)
110
0
                                {
111
                                    // Since the text generated from the mark would match the given row
112
                                    //  move cursor to it, set bMatchingMarkFound and break
113
0
                                    rShell.GotoFormatField(*pFormatField);
114
0
                                    bMatchingMarkFound = true;
115
0
                                    break;
116
0
                                }
117
0
                            }
118
0
                        }
119
0
                    }
120
121
0
                    if (bMatchingMarkFound)
122
0
                    {
123
                        // Since matching mark has been found and cursor has been moved to it,
124
                        //  open the edit dialog
125
0
                        SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create();
126
0
                        ScopedVclPtr<VclAbstractDialog> pDlg(
127
0
                            pFact->CreateSwAutoMarkDialog(GetView().GetFrameWeld(), rShell));
128
0
                        pDlg->Execute();
129
130
                        // Refresh TOX
131
0
                        rShell.GetCursor_()->GetPoint()->Assign(*pTableRowNode);
132
0
                        rShell.UpdateTableOf(*pCurrentTOX);
133
0
                    }
134
0
                    else
135
0
                    {
136
                        // I think this ideally should be a pop-up warning, right?
137
0
                        SAL_WARN("sw", "No matching bibliography mark found. "
138
0
                                        "This feature is only guaranteed to work if the bibliography table is up to date.");
139
0
                    }
140
0
                }
141
0
            }
142
143
0
            if (!bWasViewLocked)
144
0
                rShell.LockView(false);
145
0
        }
146
0
        break;
147
0
        case FN_INSERT_AUTH_ENTRY_DLG:
148
0
        {
149
            // no BASIC support
150
0
            rVFrame.ToggleChildWindow(FN_INSERT_AUTH_ENTRY_DLG);
151
0
            Invalidate(rReq.GetSlot());
152
0
        }
153
0
        break;
154
0
        case FN_INSERT_IDX_ENTRY_DLG:
155
0
        {
156
0
            rVFrame.ToggleChildWindow(FN_INSERT_IDX_ENTRY_DLG);
157
0
            Invalidate(rReq.GetSlot());
158
0
        }
159
0
        break;
160
0
        case FN_EDIT_IDX_ENTRY_DLG:
161
0
        {
162
0
            SwTOXMgr aMgr(GetShellPtr());
163
0
            short nRet = RET_OK;
164
0
            if(aMgr.GetTOXMarkCount() > 1)
165
0
            {   // Several marks, which should it be?
166
0
                SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create();
167
0
                ScopedVclPtr<VclAbstractDialog> pMultDlg(pFact->CreateMultiTOXMarkDlg(GetView().GetFrameWeld(), aMgr));
168
0
                nRet = pMultDlg->Execute();
169
0
            }
170
0
            if( nRet == RET_OK)
171
0
            {
172
0
                SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create();
173
0
                ScopedVclPtr<VclAbstractDialog> pDlg(pFact->CreateIndexMarkModalDlg(GetView().GetFrameWeld(), GetShell(), aMgr.GetCurTOXMark()));
174
0
                pDlg->Execute();
175
0
            }
176
0
            break;
177
0
        }
178
0
        case FN_IDX_MARK_TO_IDX:
179
0
        {
180
0
            GetShell().GotoTOXMarkBase();
181
0
            break;
182
0
        }
183
0
        case FN_INSERT_MULTI_TOX:
184
0
        {
185
0
            SfxItemSet aSet(SfxItemSet::makeFixedSfxItemSet<RES_FRM_SIZE, RES_FRM_SIZE,
186
0
                                                            RES_LR_SPACE, RES_LR_SPACE,
187
0
                                                            RES_BACKGROUND, RES_BACKGROUND,
188
0
                                                            RES_COL, RES_COL,
189
0
                                                            XATTR_FILL_FIRST, XATTR_FILL_LAST,
190
0
                                                            SID_ATTR_PAGE_SIZE, SID_ATTR_PAGE_SIZE,
191
0
                                                            FN_PARAM_TOX_TYPE, FN_PARAM_TOX_TYPE>(GetPool()));
192
0
            SwWrtShell& rSh = GetShell();
193
0
            SwRect aRect;
194
0
            rSh.CalcBoundRect(aRect, RndStdIds::FLY_AS_CHAR);
195
196
0
            tools::Long nWidth = aRect.Width();
197
0
            aSet.Put(SwFormatFrameSize(SwFrameSize::Variable, nWidth));
198
            // Height = width for a more consistent preview (analogous to edit range)
199
0
            aSet.Put(SvxSizeItem(SID_ATTR_PAGE_SIZE, Size(nWidth, nWidth)));
200
0
            const SwTOXBase* pCurTOX = nullptr;
201
0
            bool bGlobal = false;
202
0
            if(pItem)
203
0
            {
204
0
                pCurTOX = static_cast<const SwTOXBase*>(static_cast<const SwPtrItem*>(pItem)->GetValue());
205
0
                bGlobal = true;
206
0
            }
207
0
            else
208
0
                pCurTOX = rSh.GetCurTOX();
209
0
            if(pCurTOX)
210
0
            {
211
0
                const SfxItemSet* pSet = pCurTOX->GetAttrSet();
212
0
                if(pSet)
213
0
                    aSet.Put(*pSet);
214
0
            }
215
0
            SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create();
216
0
            VclPtr<AbstractMultiTOXTabDialog> pDlg(pFact->CreateMultiTOXTabDialog(
217
0
                                                        GetView().GetFrameWeld(), aSet, rSh, const_cast<SwTOXBase*>(pCurTOX),
218
0
                                                        bGlobal));
219
0
            pDlg->StartExecuteAsync([pDlg](sal_Int32 /*nResult*/){
220
0
                pDlg->disposeOnce();
221
0
            });
222
0
        }
223
0
        break;
224
0
        case FN_REMOVE_CUR_TOX:
225
0
        {
226
0
            SwWrtShell& rSh = GetShell();
227
0
            const SwTOXBase* pBase = rSh.GetCurTOX();
228
0
            OSL_ENSURE(pBase, "no TOXBase to remove");
229
0
            if( pBase )
230
0
                rSh.DeleteTOX(*pBase, true);
231
0
        }
232
0
        break;
233
0
        default:
234
0
            OSL_ENSURE(false, "wrong dispatcher");
235
0
            return;
236
0
    }
237
0
}
238
239
void SwTextShell::GetIdxState(SfxItemSet &rSet)
240
0
{
241
0
    SwWrtShell& rSh = GetShell();
242
0
    SfxViewFrame& rVFrame = GetView().GetViewFrame();
243
0
    SwInsertIdxMarkWrapper *pIdxMrk = static_cast<SwInsertIdxMarkWrapper*>(
244
0
                        rVFrame.GetChildWindow(FN_INSERT_IDX_ENTRY_DLG));
245
246
0
    SfxChildWindow* pAuthMark = rVFrame.GetChildWindow(FN_INSERT_AUTH_ENTRY_DLG);
247
248
0
    const bool bHtmlMode = 0 != ::GetHtmlMode( GetView().GetDocShell() );
249
0
    const SwTOXBase* pBase = nullptr;
250
0
    if( bHtmlMode || nullptr != ( pBase = rSh.GetCurTOX()) )
251
0
    {
252
0
        if( pBase )
253
0
        {
254
0
            if(pBase->IsTOXBaseInReadonly())
255
0
            {
256
0
                rSet.DisableItem( FN_INSERT_MULTI_TOX );
257
0
            }
258
0
        }
259
260
0
        rSet.DisableItem( FN_EDIT_IDX_ENTRY_DLG );
261
0
        if(pBase == nullptr // tdf#72955: Hide the "Bibliography Entry" command if there is no TOX in the selection
262
0
            || pBase->GetType() != TOX_AUTHORITIES // or if it is not a bibliography table
263
0
            || (rSh.GetCursor()->GetPoint()->GetNode().FindSectionNode()->GetSection().GetType() != SectionType::ToxContent)) // or if it's the heading
264
0
            rSet.DisableItem(FN_EDIT_AUTH_ENTRY_DLG);
265
266
0
        if(!pIdxMrk)
267
0
            rSet.DisableItem( FN_INSERT_IDX_ENTRY_DLG );
268
0
        else
269
0
            rSet.Put(SfxBoolItem(FN_INSERT_IDX_ENTRY_DLG, true));
270
271
0
        if(!pAuthMark)
272
0
            rSet.DisableItem( FN_INSERT_AUTH_ENTRY_DLG );
273
0
        else
274
0
            rSet.Put(SfxBoolItem(FN_INSERT_AUTH_ENTRY_DLG, true));
275
276
0
        if (pBase)
277
0
        {
278
0
            SfxWhichIter aIter(rSet);
279
0
            if (aIter.FirstWhich() == FN_REMOVE_CUR_TOX)
280
0
            {
281
0
                const OUString sLabel = SwResId(STR_DELETEINDEX).replaceAll("%1", pBase->GetTypeName());
282
0
                rSet.Put(SfxStringItem(FN_REMOVE_CUR_TOX, sLabel));
283
0
            }
284
0
        }
285
0
    }
286
0
    else if ( rSh.CursorInsideInputField() )
287
0
    {
288
0
        rSet.DisableItem( FN_INSERT_IDX_ENTRY_DLG );
289
0
        rSet.DisableItem( FN_INSERT_AUTH_ENTRY_DLG );
290
0
        rSet.DisableItem( FN_EDIT_AUTH_ENTRY_DLG );
291
0
        rSet.DisableItem( FN_EDIT_IDX_ENTRY_DLG );
292
0
        rSet.DisableItem( FN_INSERT_MULTI_TOX );
293
0
        rSet.DisableItem( FN_REMOVE_CUR_TOX );
294
0
    }
295
0
    else
296
0
    {
297
298
0
        bool bEnableEdit = true;
299
0
        bool bInReadonly = rSh.HasReadonlySel();
300
0
        if(bInReadonly)
301
0
            bEnableEdit = false;
302
0
        else
303
0
        {
304
0
            SwTOXMarks aArr;
305
0
            rSh.GetCurTOXMarks( aArr );
306
0
            if( aArr.empty())
307
0
                bEnableEdit = false;
308
0
        }
309
310
0
        if(!bEnableEdit)
311
0
            rSet.DisableItem( FN_EDIT_IDX_ENTRY_DLG );
312
313
0
        if(bInReadonly)
314
0
        {
315
0
            rSet.DisableItem(FN_INSERT_IDX_ENTRY_DLG);
316
0
            rSet.DisableItem( FN_INSERT_MULTI_TOX );
317
0
        }
318
0
        else
319
0
            rSet.Put(SfxBoolItem(FN_INSERT_IDX_ENTRY_DLG,
320
0
                                    nullptr != pIdxMrk));
321
322
0
        SwField* pField = rSh.GetCurField();
323
324
0
        if(bInReadonly)
325
0
            rSet.DisableItem(FN_INSERT_AUTH_ENTRY_DLG);
326
0
        else
327
0
            rSet.Put(SfxBoolItem(FN_INSERT_AUTH_ENTRY_DLG, nullptr != pAuthMark));
328
329
0
        if( bInReadonly || !pField ||
330
0
            pField->GetTyp()->Which() != SwFieldIds::TableOfAuthorities)
331
0
            rSet.DisableItem(FN_EDIT_AUTH_ENTRY_DLG);
332
0
        rSet.DisableItem(FN_REMOVE_CUR_TOX);
333
0
    }
334
0
}
335
336
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */