Add a comment to clarify what kind of inputs the class handles
[LibreOffice.git] / sw / source / uibase / shells / textidx.cxx
blob7258e0d7c547bc3eb00feec8975f19d15e40a7b5
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>
37 #include <strings.hrc>
38 #include <svl/whiter.hxx>
40 #include <ndtxt.hxx>
41 #include <fmtfld.hxx>
42 #include <IDocumentFieldsAccess.hxx>
44 void SwTextShell::ExecIdx(SfxRequest const &rReq)
46 const SfxItemSet* pArgs = rReq.GetArgs();
47 const SfxPoolItem* pItem = nullptr;
48 const sal_uInt16 nSlot = rReq.GetSlot();
49 if (pArgs)
50 pArgs->GetItemState(nSlot, false, &pItem);
52 SfxViewFrame& rVFrame = GetView().GetViewFrame();
54 switch (nSlot)
56 case FN_EDIT_AUTH_ENTRY_DLG:
58 SwWrtShell& rShell = GetShell();
60 const bool bWasViewLocked = rShell.IsViewLocked();
61 rShell.LockView(true);
63 if (const SwField* const pCurrentField = rShell.GetCurField();
64 !rShell.HasReadonlySel() && pCurrentField != nullptr
65 && pCurrentField->GetTyp()->Which() == SwFieldIds::TableOfAuthorities)
67 // Since the cursor is on a bibliography mark (e.g. "[1]"), open the edit dialog as usual
68 SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create();
69 ScopedVclPtr<VclAbstractDialog> pDlg(
70 pFact->CreateSwAutoMarkDialog(GetView().GetFrameWeld(), rShell));
71 pDlg->Execute();
73 else if (const SwTOXBase* const pCurrentTOX = rShell.GetCurTOX();
74 pCurrentTOX != nullptr && pCurrentTOX->GetType() == TOX_AUTHORITIES
75 && (rShell.GetCursor()->GetPoint()->GetNode()
76 .FindSectionNode()->GetSection().GetType()
77 == SectionType::ToxContent))
79 // Since the cursor is in the bibliography table, find the first mark that would match the given row
80 const SwNode* const pTableRowNode = &rShell.GetCursor()->GetPoint()->GetNode();
81 const OUString& rTableRowText
82 = static_cast<const SwTextNode*>(pTableRowNode)->GetText();
84 const SwFieldType* pAuthField
85 = rShell.GetDoc()->getIDocumentFieldsAccess().GetFieldType(
86 SwFieldIds::TableOfAuthorities, OUString(), false);
88 if (pAuthField)
90 bool bMatchingMarkFound = false;
92 std::vector<SwFormatField*> vFields;
93 pAuthField->GatherFields(vFields);
94 for (auto pFormatField : vFields)
96 if (const SwField* pIteratedField = nullptr;
97 pFormatField != nullptr
98 && (pIteratedField = pFormatField->GetField()) != nullptr
99 && (pIteratedField->GetTyp()->Which()
100 == SwFieldIds::TableOfAuthorities))
102 OUString sMarkText
103 = static_cast<const SwAuthorityField*>(pIteratedField)
104 ->GetAuthority(rShell.GetLayout(),
105 &pCurrentTOX->GetTOXForm());
107 if (sMarkText == rTableRowText)
109 // Since the text generated from the mark would match the given row
110 // move cursor to it, set bMatchingMarkFound and break
111 rShell.GotoFormatField(*pFormatField);
112 bMatchingMarkFound = true;
113 break;
119 if (bMatchingMarkFound)
121 // Since matching mark has been found and cursor has been moved to it,
122 // open the edit dialog
123 SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create();
124 ScopedVclPtr<VclAbstractDialog> pDlg(
125 pFact->CreateSwAutoMarkDialog(GetView().GetFrameWeld(), rShell));
126 pDlg->Execute();
128 // Refresh TOX
129 rShell.GetCursor_()->GetPoint()->Assign(*pTableRowNode);
130 rShell.UpdateTableOf(*pCurrentTOX);
132 else
134 // I think this ideally should be a pop-up warning, right?
135 SAL_WARN("sw", "No matching bibliography mark found. "
136 "This feature is only guaranteed to work if the bibliography table is up to date.");
141 if (!bWasViewLocked)
142 rShell.LockView(false);
144 break;
145 case FN_INSERT_AUTH_ENTRY_DLG:
147 // no BASIC support
148 rVFrame.ToggleChildWindow(FN_INSERT_AUTH_ENTRY_DLG);
149 Invalidate(rReq.GetSlot());
151 break;
152 case FN_INSERT_IDX_ENTRY_DLG:
154 rVFrame.ToggleChildWindow(FN_INSERT_IDX_ENTRY_DLG);
155 Invalidate(rReq.GetSlot());
157 break;
158 case FN_EDIT_IDX_ENTRY_DLG:
160 SwTOXMgr aMgr(GetShellPtr());
161 short nRet = RET_OK;
162 if(aMgr.GetTOXMarkCount() > 1)
163 { // Several marks, which should it be?
164 SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create();
165 ScopedVclPtr<VclAbstractDialog> pMultDlg(pFact->CreateMultiTOXMarkDlg(GetView().GetFrameWeld(), aMgr));
166 nRet = pMultDlg->Execute();
168 if( nRet == RET_OK)
170 SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create();
171 ScopedVclPtr<VclAbstractDialog> pDlg(pFact->CreateIndexMarkModalDlg(GetView().GetFrameWeld(), GetShell(), aMgr.GetCurTOXMark()));
172 pDlg->Execute();
174 break;
176 case FN_IDX_MARK_TO_IDX:
178 GetShell().GotoTOXMarkBase();
179 break;
181 case FN_INSERT_MULTI_TOX:
183 SfxItemSetFixed<
184 RES_FRM_SIZE, RES_FRM_SIZE,
185 RES_LR_SPACE, RES_LR_SPACE,
186 RES_BACKGROUND, RES_BACKGROUND,
187 RES_COL, RES_COL,
188 XATTR_FILL_FIRST, XATTR_FILL_LAST,
189 SID_ATTR_PAGE_SIZE, SID_ATTR_PAGE_SIZE,
190 FN_PARAM_TOX_TYPE, FN_PARAM_TOX_TYPE> aSet( GetPool() );
191 SwWrtShell& rSh = GetShell();
192 SwRect aRect;
193 rSh.CalcBoundRect(aRect, RndStdIds::FLY_AS_CHAR);
195 tools::Long nWidth = aRect.Width();
196 aSet.Put(SwFormatFrameSize(SwFrameSize::Variable, nWidth));
197 // Height = width for a more consistent preview (analogous to edit range)
198 aSet.Put(SvxSizeItem(SID_ATTR_PAGE_SIZE, Size(nWidth, nWidth)));
199 const SwTOXBase* pCurTOX = nullptr;
200 bool bGlobal = false;
201 if(pItem)
203 pCurTOX = static_cast<const SwTOXBase*>(static_cast<const SwPtrItem*>(pItem)->GetValue());
204 bGlobal = true;
206 else
207 pCurTOX = rSh.GetCurTOX();
208 if(pCurTOX)
210 const SfxItemSet* pSet = pCurTOX->GetAttrSet();
211 if(pSet)
212 aSet.Put(*pSet);
214 SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create();
215 VclPtr<AbstractMultiTOXTabDialog> pDlg(pFact->CreateMultiTOXTabDialog(
216 GetView().GetFrameWeld(), aSet, rSh, const_cast<SwTOXBase*>(pCurTOX),
217 bGlobal));
218 pDlg->StartExecuteAsync([pDlg](sal_Int32 /*nResult*/){
219 pDlg->disposeOnce();
222 break;
223 case FN_REMOVE_CUR_TOX:
225 SwWrtShell& rSh = GetShell();
226 const SwTOXBase* pBase = rSh.GetCurTOX();
227 OSL_ENSURE(pBase, "no TOXBase to remove");
228 if( pBase )
229 rSh.DeleteTOX(*pBase, true);
231 break;
232 default:
233 OSL_ENSURE(false, "wrong dispatcher");
234 return;
238 void SwTextShell::GetIdxState(SfxItemSet &rSet)
240 SwWrtShell& rSh = GetShell();
241 SfxViewFrame& rVFrame = GetView().GetViewFrame();
242 SwInsertIdxMarkWrapper *pIdxMrk = static_cast<SwInsertIdxMarkWrapper*>(
243 rVFrame.GetChildWindow(FN_INSERT_IDX_ENTRY_DLG));
245 SfxChildWindow* pAuthMark = rVFrame.GetChildWindow(FN_INSERT_AUTH_ENTRY_DLG);
247 const bool bHtmlMode = 0 != ::GetHtmlMode( GetView().GetDocShell() );
248 const SwTOXBase* pBase = nullptr;
249 if( bHtmlMode || nullptr != ( pBase = rSh.GetCurTOX()) )
251 if( pBase )
253 if(pBase->IsTOXBaseInReadonly())
255 rSet.DisableItem( FN_INSERT_MULTI_TOX );
259 rSet.DisableItem( FN_EDIT_IDX_ENTRY_DLG );
260 if(pBase == nullptr // tdf#72955: Hide the "Bibliography Entry" command if there is no TOX in the selection
261 || pBase->GetType() != TOX_AUTHORITIES // or if it is not a bibliography table
262 || (rSh.GetCursor()->GetPoint()->GetNode().FindSectionNode()->GetSection().GetType() != SectionType::ToxContent)) // or if it's the heading
263 rSet.DisableItem(FN_EDIT_AUTH_ENTRY_DLG);
265 if(!pIdxMrk)
266 rSet.DisableItem( FN_INSERT_IDX_ENTRY_DLG );
267 else
268 rSet.Put(SfxBoolItem(FN_INSERT_IDX_ENTRY_DLG, true));
270 if(!pAuthMark)
271 rSet.DisableItem( FN_INSERT_AUTH_ENTRY_DLG );
272 else
273 rSet.Put(SfxBoolItem(FN_INSERT_AUTH_ENTRY_DLG, true));
275 if (pBase)
277 SfxWhichIter aIter(rSet);
278 if (aIter.FirstWhich() == FN_REMOVE_CUR_TOX)
280 const OUString sLabel = SwResId(STR_DELETEINDEX).replaceAll("%1", pBase->GetTypeName());
281 rSet.Put(SfxStringItem(FN_REMOVE_CUR_TOX, sLabel));
285 else if ( rSh.CursorInsideInputField() )
287 rSet.DisableItem( FN_INSERT_IDX_ENTRY_DLG );
288 rSet.DisableItem( FN_INSERT_AUTH_ENTRY_DLG );
289 rSet.DisableItem( FN_EDIT_AUTH_ENTRY_DLG );
290 rSet.DisableItem( FN_EDIT_IDX_ENTRY_DLG );
291 rSet.DisableItem( FN_INSERT_MULTI_TOX );
292 rSet.DisableItem( FN_REMOVE_CUR_TOX );
294 else
297 bool bEnableEdit = true;
298 bool bInReadonly = rSh.HasReadonlySel();
299 if(bInReadonly)
300 bEnableEdit = false;
301 else
303 SwTOXMarks aArr;
304 rSh.GetCurTOXMarks( aArr );
305 if( aArr.empty())
306 bEnableEdit = false;
309 if(!bEnableEdit)
310 rSet.DisableItem( FN_EDIT_IDX_ENTRY_DLG );
312 if(bInReadonly)
314 rSet.DisableItem(FN_INSERT_IDX_ENTRY_DLG);
315 rSet.DisableItem( FN_INSERT_MULTI_TOX );
317 else
318 rSet.Put(SfxBoolItem(FN_INSERT_IDX_ENTRY_DLG,
319 nullptr != pIdxMrk));
321 SwField* pField = rSh.GetCurField();
323 if(bInReadonly)
324 rSet.DisableItem(FN_INSERT_AUTH_ENTRY_DLG);
325 else
326 rSet.Put(SfxBoolItem(FN_INSERT_AUTH_ENTRY_DLG, nullptr != pAuthMark));
328 if( bInReadonly || !pField ||
329 pField->GetTyp()->Which() != SwFieldIds::TableOfAuthorities)
330 rSet.DisableItem(FN_EDIT_AUTH_ENTRY_DLG);
331 rSet.DisableItem(FN_REMOVE_CUR_TOX);
335 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */