Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sw / source / uibase / shells / textidx.cxx
blob45d9df2c70b62247bbe8fd1f20cc254e439b2f37
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 <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 <fmtfsize.hxx>
27 #include <fldbas.hxx>
28 #include <uiitems.hxx>
29 #include <viewopt.hxx>
30 #include <cmdid.h>
31 #include <view.hxx>
32 #include <wrtsh.hxx>
33 #include <textsh.hxx>
34 #include <idxmrk.hxx>
35 #include <toxmgr.hxx>
36 #include <swabstdlg.hxx>
38 #include <ndtxt.hxx>
39 #include <fmtfld.hxx>
40 #include <IDocumentFieldsAccess.hxx>
42 void SwTextShell::ExecIdx(SfxRequest const &rReq)
44 const SfxItemSet* pArgs = rReq.GetArgs();
45 const SfxPoolItem* pItem = nullptr;
46 const sal_uInt16 nSlot = rReq.GetSlot();
47 if (pArgs)
48 pArgs->GetItemState(nSlot, false, &pItem);
50 SfxViewFrame& rVFrame = GetView().GetViewFrame();
52 switch (nSlot)
54 case FN_EDIT_AUTH_ENTRY_DLG:
56 SwWrtShell& rShell = GetShell();
58 const bool bWasViewLocked = rShell.IsViewLocked();
59 rShell.LockView(true);
61 if (const SwField* const pCurrentField = rShell.GetCurField();
62 !rShell.HasReadonlySel() && pCurrentField != nullptr
63 && pCurrentField->GetTyp()->Which() == SwFieldIds::TableOfAuthorities)
65 // Since the cursor is on a bibliography mark (e.g. "[1]"), open the edit dialog as usual
66 SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create();
67 ScopedVclPtr<VclAbstractDialog> pDlg(
68 pFact->CreateSwAutoMarkDialog(GetView().GetFrameWeld(), rShell));
69 pDlg->Execute();
71 else if (const SwTOXBase* const pCurrentTOX = rShell.GetCurTOX();
72 pCurrentTOX != nullptr && pCurrentTOX->GetType() == TOX_AUTHORITIES
73 && (rShell.GetCursor()->GetPoint()->GetNode()
74 .FindSectionNode()->GetSection().GetType()
75 == SectionType::ToxContent))
77 // Since the cursor is in the bibliography table, find the first mark that would match the given row
78 const SwNode* const pTableRowNode = &rShell.GetCursor()->GetPoint()->GetNode();
79 const OUString& rTableRowText
80 = static_cast<const SwTextNode*>(pTableRowNode)->GetText();
82 const SwFieldType* pAuthField
83 = rShell.GetDoc()->getIDocumentFieldsAccess().GetFieldType(
84 SwFieldIds::TableOfAuthorities, OUString(), false);
86 if (pAuthField)
88 bool bMatchingMarkFound = false;
90 std::vector<SwFormatField*> vFields;
91 pAuthField->GatherFields(vFields);
92 for (auto pFormatField : vFields)
94 if (const SwField* pIteratedField = nullptr;
95 pFormatField != nullptr
96 && (pIteratedField = pFormatField->GetField()) != nullptr
97 && (pIteratedField->GetTyp()->Which()
98 == SwFieldIds::TableOfAuthorities))
100 OUString sMarkText
101 = static_cast<const SwAuthorityField*>(pIteratedField)
102 ->GetAuthority(rShell.GetLayout(),
103 &pCurrentTOX->GetTOXForm());
105 if (sMarkText == rTableRowText)
107 // Since the text generated from the mark would match the given row
108 // move cursor to it, set bMatchingMarkFound and break
109 rShell.GotoFormatField(*pFormatField);
110 bMatchingMarkFound = true;
111 break;
117 if (bMatchingMarkFound)
119 // Since matching mark has been found and cursor has been moved to it,
120 // open the edit dialog
121 SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create();
122 ScopedVclPtr<VclAbstractDialog> pDlg(
123 pFact->CreateSwAutoMarkDialog(GetView().GetFrameWeld(), rShell));
124 pDlg->Execute();
126 // Refresh TOX
127 rShell.GetCursor_()->GetPoint()->Assign(*pTableRowNode);
128 rShell.UpdateTableOf(*pCurrentTOX);
133 if (!bWasViewLocked)
134 rShell.LockView(false);
136 break;
137 case FN_INSERT_AUTH_ENTRY_DLG:
139 // no BASIC support
140 rVFrame.ToggleChildWindow(FN_INSERT_AUTH_ENTRY_DLG);
141 Invalidate(rReq.GetSlot());
143 break;
144 case FN_INSERT_IDX_ENTRY_DLG:
146 rVFrame.ToggleChildWindow(FN_INSERT_IDX_ENTRY_DLG);
147 Invalidate(rReq.GetSlot());
149 break;
150 case FN_EDIT_IDX_ENTRY_DLG:
152 SwTOXMgr aMgr(GetShellPtr());
153 short nRet = RET_OK;
154 if(aMgr.GetTOXMarkCount() > 1)
155 { // Several marks, which should it be?
156 SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create();
157 ScopedVclPtr<VclAbstractDialog> pMultDlg(pFact->CreateMultiTOXMarkDlg(GetView().GetFrameWeld(), aMgr));
158 nRet = pMultDlg->Execute();
160 if( nRet == RET_OK)
162 SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create();
163 ScopedVclPtr<VclAbstractDialog> pDlg(pFact->CreateIndexMarkModalDlg(GetView().GetFrameWeld(), GetShell(), aMgr.GetCurTOXMark()));
164 pDlg->Execute();
166 break;
168 case FN_IDX_MARK_TO_IDX:
170 GetShell().GotoTOXMarkBase();
171 break;
173 case FN_INSERT_MULTI_TOX:
175 SfxItemSetFixed<
176 RES_FRM_SIZE, RES_FRM_SIZE,
177 RES_LR_SPACE, RES_LR_SPACE,
178 RES_BACKGROUND, RES_BACKGROUND,
179 RES_COL, RES_COL,
180 XATTR_FILL_FIRST, XATTR_FILL_LAST,
181 SID_ATTR_PAGE_SIZE, SID_ATTR_PAGE_SIZE,
182 FN_PARAM_TOX_TYPE, FN_PARAM_TOX_TYPE> aSet( GetPool() );
183 SwWrtShell& rSh = GetShell();
184 SwRect aRect;
185 rSh.CalcBoundRect(aRect, RndStdIds::FLY_AS_CHAR);
187 tools::Long nWidth = aRect.Width();
188 aSet.Put(SwFormatFrameSize(SwFrameSize::Variable, nWidth));
189 // Height = width for a more consistent preview (analogous to edit range)
190 aSet.Put(SvxSizeItem(SID_ATTR_PAGE_SIZE, Size(nWidth, nWidth)));
191 const SwTOXBase* pCurTOX = nullptr;
192 bool bGlobal = false;
193 if(pItem)
195 pCurTOX = static_cast<const SwTOXBase*>(static_cast<const SwPtrItem*>(pItem)->GetValue());
196 bGlobal = true;
198 else
199 pCurTOX = rSh.GetCurTOX();
200 if(pCurTOX)
202 const SfxItemSet* pSet = pCurTOX->GetAttrSet();
203 if(pSet)
204 aSet.Put(*pSet);
206 SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create();
207 VclPtr<AbstractMultiTOXTabDialog> pDlg(pFact->CreateMultiTOXTabDialog(
208 GetView().GetFrameWeld(), aSet, rSh, const_cast<SwTOXBase*>(pCurTOX),
209 bGlobal));
210 pDlg->StartExecuteAsync([pDlg](sal_Int32 /*nResult*/){
211 pDlg->disposeOnce();
214 break;
215 case FN_REMOVE_CUR_TOX:
217 SwWrtShell& rSh = GetShell();
218 const SwTOXBase* pBase = rSh.GetCurTOX();
219 OSL_ENSURE(pBase, "no TOXBase to remove");
220 if( pBase )
221 rSh.DeleteTOX(*pBase, true);
223 break;
224 default:
225 OSL_ENSURE(false, "wrong dispatcher");
226 return;
230 void SwTextShell::GetIdxState(SfxItemSet &rSet)
232 SwWrtShell& rSh = GetShell();
233 SfxViewFrame& rVFrame = GetView().GetViewFrame();
234 SwInsertIdxMarkWrapper *pIdxMrk = static_cast<SwInsertIdxMarkWrapper*>(
235 rVFrame.GetChildWindow(FN_INSERT_IDX_ENTRY_DLG));
237 SfxChildWindow* pAuthMark = rVFrame.GetChildWindow(FN_INSERT_AUTH_ENTRY_DLG);
239 const bool bHtmlMode = 0 != ::GetHtmlMode( GetView().GetDocShell() );
240 const SwTOXBase* pBase = nullptr;
241 if( bHtmlMode || nullptr != ( pBase = rSh.GetCurTOX()) )
243 if( pBase )
245 if(pBase->IsTOXBaseInReadonly())
247 rSet.DisableItem( FN_INSERT_MULTI_TOX );
251 rSet.DisableItem( FN_EDIT_IDX_ENTRY_DLG );
252 if(pBase == nullptr // tdf#72955: Hide the "Bibliography Entry" command if there is no TOX in the selection
253 || pBase->GetType() != TOX_AUTHORITIES // or if it is not a bibliography table
254 || (rSh.GetCursor()->GetPoint()->GetNode().FindSectionNode()->GetSection().GetType() != SectionType::ToxContent)) // or if it's the heading
255 rSet.DisableItem(FN_EDIT_AUTH_ENTRY_DLG);
257 if(!pIdxMrk)
258 rSet.DisableItem( FN_INSERT_IDX_ENTRY_DLG );
259 else
260 rSet.Put(SfxBoolItem(FN_INSERT_IDX_ENTRY_DLG, true));
262 if(!pAuthMark)
263 rSet.DisableItem( FN_INSERT_AUTH_ENTRY_DLG );
264 else
265 rSet.Put(SfxBoolItem(FN_INSERT_AUTH_ENTRY_DLG, true));
268 else if ( rSh.CursorInsideInputField() )
270 rSet.DisableItem( FN_INSERT_IDX_ENTRY_DLG );
271 rSet.DisableItem( FN_INSERT_AUTH_ENTRY_DLG );
272 rSet.DisableItem( FN_EDIT_AUTH_ENTRY_DLG );
273 rSet.DisableItem( FN_EDIT_IDX_ENTRY_DLG );
274 rSet.DisableItem( FN_INSERT_MULTI_TOX );
275 rSet.DisableItem( FN_REMOVE_CUR_TOX );
277 else
280 bool bEnableEdit = true;
281 bool bInReadonly = rSh.HasReadonlySel();
282 if(bInReadonly)
283 bEnableEdit = false;
284 else
286 SwTOXMarks aArr;
287 rSh.GetCurTOXMarks( aArr );
288 if( aArr.empty())
289 bEnableEdit = false;
292 if(!bEnableEdit)
293 rSet.DisableItem( FN_EDIT_IDX_ENTRY_DLG );
295 if(bInReadonly)
297 rSet.DisableItem(FN_INSERT_IDX_ENTRY_DLG);
298 rSet.DisableItem( FN_INSERT_MULTI_TOX );
300 else
301 rSet.Put(SfxBoolItem(FN_INSERT_IDX_ENTRY_DLG,
302 nullptr != pIdxMrk));
304 SwField* pField = rSh.GetCurField();
306 if(bInReadonly)
307 rSet.DisableItem(FN_INSERT_AUTH_ENTRY_DLG);
308 else
309 rSet.Put(SfxBoolItem(FN_INSERT_AUTH_ENTRY_DLG, nullptr != pAuthMark));
311 if( bInReadonly || !pField ||
312 pField->GetTyp()->Which() != SwFieldIds::TableOfAuthorities)
313 rSet.DisableItem(FN_EDIT_AUTH_ENTRY_DLG);
314 rSet.DisableItem(FN_REMOVE_CUR_TOX);
318 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */