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 <IDocumentFieldsAccess.hxx>
30 #include <osl/diagnose.h>
34 SwInputFieldList::SwInputFieldList( SwEditShell
* pShell
, bool bBuildTmpLst
)
37 // create sorted list of all input fields
38 mpSrtLst
.reset( new SetGetExpFields
);
40 const SwFieldTypes
& rFieldTypes
= *mpSh
->GetDoc()->getIDocumentFieldsAccess().GetFieldTypes();
41 const size_t nSize
= rFieldTypes
.size();
43 // iterate over all types
44 std::vector
<SwFormatField
*> vFields
;
45 for(size_t i
= 0; i
< nSize
; ++i
)
47 SwFieldType
* pFieldType
= rFieldTypes
[ i
].get();
48 const SwFieldIds nType
= pFieldType
->Which();
49 if(SwFieldIds::SetExp
== nType
|| SwFieldIds::Input
== nType
|| SwFieldIds::Dropdown
== nType
)
50 pFieldType
->GatherFields(vFields
);
52 for(auto pFormatField
: vFields
)
54 auto pSetExpField
= dynamic_cast<SwSetExpField
*>(pFormatField
->GetField());
55 if(pSetExpField
&& !pSetExpField
->GetInputFlag())
57 const SwTextField
* pTextField
= pFormatField
->GetTextField();
59 maTmpLst
.insert(pTextField
);
62 std::unique_ptr
<SetGetExpField
> pNew(new SetGetExpField(pTextField
->GetTextNode(), pTextField
));
63 mpSrtLst
->insert(std::move(pNew
));
68 SwInputFieldList::~SwInputFieldList()
72 size_t SwInputFieldList::Count() const
74 return mpSrtLst
->size();
77 // get field from list in sorted order
78 SwField
* SwInputFieldList::GetField(size_t nId
)
80 const SwTextField
* pTextField
= (*mpSrtLst
)[ nId
]->GetTextField();
81 OSL_ENSURE( pTextField
, "no TextField" );
82 return const_cast<SwField
*>(pTextField
->GetFormatField().GetField());
86 void SwInputFieldList::PushCursor()
93 void SwInputFieldList::PopCursor()
95 mpSh
->Pop(SwCursorShell::PopMode::DeleteCurrent
);
98 /// go to position of a field
99 void SwInputFieldList::GotoFieldPos(size_t nId
)
101 mpSh
->StartAllAction();
102 (*mpSrtLst
)[ nId
]->GetPosOfContent( *mpSh
->GetCursor()->GetPoint() );
103 mpSh
->EndAllAction();
106 /** Compare TmpLst with current fields.
108 * All new ones are added to SortList so that they can be updated.
109 * For text blocks: update only input fields.
111 * @return true if not empty
113 bool SwInputFieldList::BuildSortLst()
115 const SwFieldTypes
& rFieldTypes
= *mpSh
->GetDoc()->getIDocumentFieldsAccess().GetFieldTypes();
116 const size_t nSize
= rFieldTypes
.size();
118 // iterate over all types
119 std::vector
<SwFormatField
*> vFields
;
120 for(size_t i
= 0; i
< nSize
; ++i
)
122 SwFieldType
* pFieldType
= rFieldTypes
[ i
].get();
123 const SwFieldIds nType
= pFieldType
->Which();
124 if(SwFieldIds::SetExp
== nType
|| SwFieldIds::Input
== nType
)
125 pFieldType
->GatherFields(vFields
);
127 for(auto pFormatField
: vFields
)
129 auto pSetExpField
= dynamic_cast<SwSetExpField
*>(pFormatField
->GetField());
130 if(pSetExpField
&& !pSetExpField
->GetInputFlag())
132 const SwTextField
* pTextField
= pFormatField
->GetTextField();
133 // not in TempList, thus add to SortList
134 auto it
= maTmpLst
.find(pTextField
);
135 if(maTmpLst
.end() == it
)
137 std::unique_ptr
<SetGetExpField
> pNew(new SetGetExpField(pTextField
->GetTextNode(), pTextField
));
138 mpSrtLst
->insert(std::move(pNew
));
144 // the pointers are not needed anymore
146 return !mpSrtLst
->empty();
149 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */