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 .
20 #include <com/sun/star/text/XDocumentIndexesSupplier.hpp>
21 #include <com/sun/star/text/XTextSectionsSupplier.hpp>
22 #include <com/sun/star/text/XDependentTextField.hpp>
23 #include <com/sun/star/text/XDocumentIndex.hpp>
24 #include <com/sun/star/text/ChapterFormat.hpp>
25 #include <com/sun/star/text/XTextSection.hpp>
26 #include <com/sun/star/beans/PropertyValues.hpp>
27 #include <com/sun/star/ucb/XCommandEnvironment.hpp>
28 #include <comphelper/string.hxx>
29 #include <comphelper/servicehelper.hxx>
30 #include <tools/UnitConversion.hxx>
31 #include <comphelper/diagnose_ex.hxx>
35 #include <unoprnms.hxx>
36 #include <unotools.hxx>
37 #include <unotxdoc.hxx>
40 #include <SwStyleNameMapper.hxx>
41 #include <swuicnttab.hxx>
43 using namespace ::com::sun::star
;
44 using namespace ::com::sun::star::text
;
45 using namespace ::com::sun::star::beans
;
46 using namespace ::com::sun::star::container
;
47 using namespace ::com::sun::star::lang
;
48 using namespace ::com::sun::star::ucb
;
49 using namespace ::com::sun::star::uno
;
50 using namespace com::sun::star::ui::dialogs
;
52 static void lcl_SetProp( uno::Reference
< XPropertySetInfo
> const & xInfo
,
53 uno::Reference
< XPropertySet
> const & xProps
,
54 OUString
const & aPropName
, const OUString
& rValue
)
56 if(xInfo
->hasPropertyByName(aPropName
))
60 xProps
->setPropertyValue(aPropName
, aValue
);
64 static void lcl_SetProp( uno::Reference
< XPropertySetInfo
> const & xInfo
,
65 uno::Reference
< XPropertySet
> const & xProps
,
66 OUString
const & aPropName
, sal_Int16 nValue
)
68 if(xInfo
->hasPropertyByName(aPropName
))
72 xProps
->setPropertyValue(aPropName
, aValue
);
76 static void lcl_SetBOOLProp(
77 uno::Reference
< beans::XPropertySetInfo
> const & xInfo
,
78 uno::Reference
< beans::XPropertySet
> const & xProps
,
79 OUString
const & aPropName
, bool bValue
)
81 if(xInfo
->hasPropertyByName(aPropName
))
83 xProps
->setPropertyValue(aPropName
, Any(bValue
));
87 IMPL_LINK_NOARG(SwMultiTOXTabDialog
, CreateExample_Hdl
, SwOneExampleFrame
&, void)
91 uno::Reference
< frame::XModel
> & xModel
= m_xExampleFrame
->GetModel();
92 auto pDoc
= comphelper::getFromUnoTunnel
<SwXTextDocument
>(xModel
);
95 pDoc
->GetDocShell()->LoadStyles_( *m_rWrtShell
.GetView().GetDocShell(), true );
97 uno::Reference
< text::XTextSectionsSupplier
> xSectionSupplier(
98 xModel
, uno::UNO_QUERY
);
99 uno::Reference
< container::XNameAccess
> xSections
=
100 xSectionSupplier
->getTextSections();
102 for(int i
= 0; i
< 7; ++i
)
104 OUString sTmp
= "IndexSection_" + OUString::number(i
);
105 uno::Any aSection
= xSections
->getByName( sTmp
);
106 aSection
>>= m_vTypeData
[i
].m_oIndexSections
->xContainerSection
;
108 uno::Reference
< text::XDocumentIndexesSupplier
> xIdxSupp(xModel
, uno::UNO_QUERY
);
109 uno::Reference
< container::XIndexAccess
> xIdxs
= xIdxSupp
->getDocumentIndexes();
110 int n
= xIdxs
->getCount();
114 uno::Any aIdx
= xIdxs
->getByIndex(n
);
115 uno::Reference
< text::XDocumentIndex
> xIdx
;
119 CreateOrUpdateExample(m_eCurrentTOXType
.eType
);
121 catch (const Exception
&)
123 TOOLS_WARN_EXCEPTION( "sw", "::CreateExample()");
127 void SwMultiTOXTabDialog::CreateOrUpdateExample(
128 TOXTypes nTOXIndex
, sal_uInt16 nPage
, sal_uInt16 nCurrentLevel
)
130 if(!m_xExampleFrame
|| !m_xExampleFrame
->IsInitialized())
135 static const char* IndexServiceNames
[] =
137 "com.sun.star.text.DocumentIndex",
138 "com.sun.star.text.UserIndex",
139 "com.sun.star.text.ContentIndex",
140 "com.sun.star.text.IllustrationsIndex",
141 "com.sun.star.text.ObjectIndex",
142 "com.sun.star.text.TableIndex",
143 "com.sun.star.text.Bibliography"
146 OSL_ENSURE(m_vTypeData
[nTOXIndex
].m_oIndexSections
&&
147 m_vTypeData
[nTOXIndex
].m_oIndexSections
->xContainerSection
.is(),
148 "Section not created");
149 uno::Reference
< frame::XModel
> & xModel
= m_xExampleFrame
->GetModel();
150 bool bInitialCreate
= true;
151 if(!m_vTypeData
[nTOXIndex
].m_oIndexSections
->xDocumentIndex
.is())
153 bInitialCreate
= true;
154 if(!m_vTypeData
[nTOXIndex
].m_oIndexSections
->xContainerSection
.is())
155 throw uno::RuntimeException();
156 uno::Reference
< text::XTextRange
> xAnchor
= m_vTypeData
[nTOXIndex
].m_oIndexSections
->xContainerSection
->getAnchor();
157 xAnchor
= xAnchor
->getStart();
158 uno::Reference
< text::XTextCursor
> xCursor
= xAnchor
->getText()->createTextCursorByRange(xAnchor
);
160 uno::Reference
< lang::XMultiServiceFactory
> xFact(xModel
, uno::UNO_QUERY
);
162 OUString
sIndexTypeName(OUString::createFromAscii( IndexServiceNames
[
163 nTOXIndex
<= TOX_AUTHORITIES
? nTOXIndex
: TOX_USER
] ));
164 m_vTypeData
[nTOXIndex
].m_oIndexSections
->xDocumentIndex
.set(xFact
->createInstance(sIndexTypeName
), uno::UNO_QUERY
);
165 uno::Reference
< text::XTextContent
> xContent
= m_vTypeData
[nTOXIndex
].m_oIndexSections
->xDocumentIndex
;
166 xCursor
->getText()->insertTextContent(xCursor
, xContent
, false);
168 for(sal_uInt16 i
= 0 ; i
<= TOX_AUTHORITIES
; i
++)
170 uno::Reference
< beans::XPropertySet
> xSectPr(m_vTypeData
[i
].m_oIndexSections
->xContainerSection
, uno::UNO_QUERY
);
173 xSectPr
->setPropertyValue(UNO_NAME_IS_VISIBLE
, Any(i
== nTOXIndex
));
177 uno::Reference
< beans::XPropertySet
> xIdxProps(m_vTypeData
[nTOXIndex
].m_oIndexSections
->xDocumentIndex
, uno::UNO_QUERY
);
178 uno::Reference
< beans::XPropertySetInfo
> xInfo
= xIdxProps
->getPropertySetInfo();
179 SwTOXDescription
& rDesc
= GetTOXDescription(m_eCurrentTOXType
);
180 SwTOIOptions nIdxOptions
= rDesc
.GetIndexOptions();
181 if(bInitialCreate
|| !nPage
|| nPage
== TOX_PAGE_SELECT
)
185 lcl_SetProp(xInfo
, xIdxProps
, UNO_NAME_TITLE
, *rDesc
.GetTitle());
188 SwTOXElement nContentOptions
= rDesc
.GetContentOptions();
189 if(xInfo
->hasPropertyByName(UNO_NAME_LEVEL_PARAGRAPH_STYLES
))
191 bool bOn( nContentOptions
&SwTOXElement::Template
);
192 uno::Any
aStyleNames(xIdxProps
->getPropertyValue(UNO_NAME_LEVEL_PARAGRAPH_STYLES
));
193 uno::Reference
< container::XIndexReplace
> xAcc
;
194 aStyleNames
>>= xAcc
;
196 for(sal_uInt16 i
= 0; i
< MAXLEVEL
; i
++)
200 sLevel
= rDesc
.GetStyleNames(i
);
201 const sal_Int32 nStyles
=
202 comphelper::string::getTokenCount(sLevel
, TOX_STYLE_DELIMITER
);
203 uno::Sequence
<OUString
> aStyles(nStyles
);
204 OUString
* pArr
= aStyles
.getArray();
206 for(sal_Int32 nStyle
= 0; nStyle
< nStyles
; ++nStyle
)
207 pArr
[nStyle
] = sLevel
.getToken(0, TOX_STYLE_DELIMITER
, nPos
);
208 uno::Any
aAny(&aStyles
, cppu::UnoType
<uno::Sequence
<OUString
>>::get());
209 xAcc
->replaceByIndex(i
, aAny
);
212 lcl_SetProp(xInfo
, xIdxProps
, UNO_NAME_LEVEL
, static_cast<sal_Int16
>(rDesc
.GetLevel()));
213 lcl_SetBOOLProp(xInfo
, xIdxProps
, UNO_NAME_CREATE_FROM_MARKS
, bool(nContentOptions
& SwTOXElement::Mark
));
214 lcl_SetBOOLProp(xInfo
, xIdxProps
, UNO_NAME_CREATE_FROM_OUTLINE
, bool(nContentOptions
& SwTOXElement::OutlineLevel
));
215 lcl_SetBOOLProp(xInfo
, xIdxProps
, UNO_NAME_CREATE_FROM_EMBEDDED_OBJECTS
,bool(nContentOptions
& SwTOXElement::Ole
));
216 lcl_SetBOOLProp(xInfo
, xIdxProps
, UNO_NAME_CREATE_FROM_TABLES
, bool(nContentOptions
& SwTOXElement::Table
));
217 lcl_SetBOOLProp(xInfo
, xIdxProps
, UNO_NAME_CREATE_FROM_GRAPHIC_OBJECTS
, bool(nContentOptions
& SwTOXElement::Graphic
));
218 lcl_SetBOOLProp(xInfo
, xIdxProps
, UNO_NAME_CREATE_FROM_TEXT_FRAMES
, bool(nContentOptions
& SwTOXElement::Frame
));
219 lcl_SetBOOLProp(xInfo
, xIdxProps
, UNO_NAME_CREATE_FROM_LABELS
, bool(nContentOptions
& SwTOXElement::Sequence
));
220 lcl_SetBOOLProp(xInfo
, xIdxProps
, UNO_NAME_HIDE_TAB_LEADER_AND_PAGE_NUMBERS
, bool(nContentOptions
& SwTOXElement::TableLeader
));
221 lcl_SetBOOLProp(xInfo
, xIdxProps
, UNO_NAME_TAB_IN_TOC
, bool(nContentOptions
& SwTOXElement::TableInToc
));
222 lcl_SetBOOLProp(xInfo
, xIdxProps
, UNO_NAME_TOC_NEWLINE
, bool(nContentOptions
& SwTOXElement::Newline
));
223 lcl_SetBOOLProp(xInfo
, xIdxProps
, UNO_NAME_TOC_PARAGRAPH_OUTLINE_LEVEL
, bool(nContentOptions
& SwTOXElement::ParagraphOutlineLevel
));
225 lcl_SetBOOLProp(xInfo
, xIdxProps
, UNO_NAME_CREATE_FROM_CHAPTER
, rDesc
.IsFromChapter());
226 lcl_SetBOOLProp(xInfo
, xIdxProps
, UNO_NAME_IS_PROTECTED
, rDesc
.IsReadonly());
228 lcl_SetBOOLProp(xInfo
, xIdxProps
, UNO_NAME_USE_COMBINED_ENTRIES
, bool(nIdxOptions
& SwTOIOptions::SameEntry
));
229 lcl_SetBOOLProp(xInfo
, xIdxProps
, UNO_NAME_USE_P_P
, bool(nIdxOptions
& SwTOIOptions::FF
));
230 lcl_SetBOOLProp(xInfo
, xIdxProps
, UNO_NAME_IS_CASE_SENSITIVE
, bool(nIdxOptions
& SwTOIOptions::CaseSensitive
));
231 lcl_SetBOOLProp(xInfo
, xIdxProps
, UNO_NAME_USE_KEY_AS_ENTRY
, bool(nIdxOptions
& SwTOIOptions::KeyAsEntry
));
232 lcl_SetBOOLProp(xInfo
, xIdxProps
, UNO_NAME_USE_ALPHABETICAL_SEPARATORS
, bool(nIdxOptions
& SwTOIOptions::AlphaDelimiter
));
233 lcl_SetBOOLProp(xInfo
, xIdxProps
, UNO_NAME_USE_DASH
, bool(nIdxOptions
& SwTOIOptions::Dash
));
234 lcl_SetBOOLProp(xInfo
, xIdxProps
, UNO_NAME_USE_UPPER_CASE
, bool(nIdxOptions
& SwTOIOptions::InitialCaps
));
236 OUString
aTmpName( SwStyleNameMapper::GetSpecialExtraProgName( rDesc
.GetSequenceName() ) );
237 lcl_SetProp(xInfo
, xIdxProps
, UNO_NAME_LABEL_CATEGORY
, aTmpName
);
238 lcl_SetBOOLProp(xInfo
, xIdxProps
, UNO_NAME_CREATE_FROM_LABELS
, !rDesc
.IsCreateFromObjectNames());
240 sal_Int16 nSet
= text::ChapterFormat::NAME_NUMBER
;
241 switch (rDesc
.GetCaptionDisplay())
243 case CAPTION_COMPLETE
: nSet
= text::ChapterFormat::NAME_NUMBER
;break;
244 case CAPTION_NUMBER
: nSet
= text::ChapterFormat::NUMBER
; break;
245 case CAPTION_TEXT
: nSet
= text::ChapterFormat::NAME
; break;
247 lcl_SetProp(xInfo
, xIdxProps
, UNO_NAME_LABEL_DISPLAY_TYPE
, nSet
);
249 SwTOOElements nOLEOptions
= rDesc
.GetOLEOptions();
250 lcl_SetBOOLProp(xInfo
, xIdxProps
, UNO_NAME_CREATE_FROM_STAR_MATH
, bool(SwTOOElements::Math
&nOLEOptions
));
251 lcl_SetBOOLProp(xInfo
, xIdxProps
, UNO_NAME_CREATE_FROM_STAR_CHART
, bool(SwTOOElements::Chart
&nOLEOptions
));
252 lcl_SetBOOLProp(xInfo
, xIdxProps
, UNO_NAME_CREATE_FROM_STAR_CALC
, bool(SwTOOElements::Calc
&nOLEOptions
));
253 lcl_SetBOOLProp(xInfo
, xIdxProps
, UNO_NAME_CREATE_FROM_STAR_DRAW
, bool(SwTOOElements::DrawImpress
&nOLEOptions
));
254 lcl_SetBOOLProp(xInfo
, xIdxProps
, UNO_NAME_CREATE_FROM_OTHER_EMBEDDED_OBJECTS
, bool(SwTOOElements::Other
& nOLEOptions
));
256 const SwForm
* pForm
= GetForm(m_eCurrentTOXType
);
257 if(bInitialCreate
|| !nPage
|| nPage
== TOX_PAGE_ENTRY
)
259 lcl_SetBOOLProp(xInfo
, xIdxProps
, UNO_NAME_IS_COMMA_SEPARATED
, pForm
->IsCommaSeparated());
260 lcl_SetBOOLProp(xInfo
, xIdxProps
, UNO_NAME_USE_ALPHABETICAL_SEPARATORS
, bool(nIdxOptions
&SwTOIOptions::AlphaDelimiter
));
261 const bool bUseCurrent
= nCurrentLevel
< pForm
->GetFormMax();
262 const sal_uInt16 nStartLevel
= bUseCurrent
? nCurrentLevel
: 0;
263 const sal_uInt16 nEndLevel
= bUseCurrent
? nCurrentLevel
: pForm
->GetFormMax() - 1;
264 if(xInfo
->hasPropertyByName(UNO_NAME_LEVEL_FORMAT
))
266 for(sal_uInt16 nCurrLevel
= nStartLevel
; nCurrLevel
<= nEndLevel
; nCurrLevel
++)
269 uno::Sequence
< beans::PropertyValues
> aSequPropVals(10);
270 tools::Long nTokenIndex
= 0;
271 tools::Long nParamCount
= 2;
274 SwFormTokens aPattern
= pForm
->GetPattern(nCurrLevel
);
276 for(const auto& aToken
: aPattern
)
278 if( aSequPropVals
.getLength() <= nTokenIndex
)
279 aSequPropVals
.realloc(nTokenIndex
+ 10);
281 switch(aToken
.eTokenType
)
283 case TOKEN_ENTRY_NO
:
284 sTokenType
= "TokenEntryNumber";
285 // numbering for content index
287 case TOKEN_ENTRY_TEXT
:
289 sTokenType
= "TokenEntryText";
291 case TOKEN_TAB_STOP
:
293 sTokenType
= "TokenTabStop";
296 sTokenType
= "TokenText";
299 case TOKEN_PAGE_NUMS
:
300 sTokenType
= "TokenPageNumber";
302 case TOKEN_CHAPTER_INFO
:
303 sTokenType
= "TokenChapterInfo";
305 case TOKEN_LINK_START
:
306 sTokenType
= "TokenHyperlinkStart";
308 case TOKEN_LINK_END
:
309 sTokenType
= "TokenHyperlinkEnd";
311 case TOKEN_AUTHORITY
:
313 sTokenType
= "TokenBibliographyDataField";
316 default:; //prevent warning
318 beans::PropertyValues
aPropVals(nParamCount
);
319 beans::PropertyValue
* pPropValArr
= aPropVals
.getArray();
320 pPropValArr
[0].Name
= "TokenType";
321 pPropValArr
[0].Value
<<= sTokenType
;
322 pPropValArr
[1].Name
= "CharacterStyleName";
323 pPropValArr
[1].Value
<<= aToken
.sCharStyleName
;
324 if(TOKEN_TAB_STOP
== aToken
.eTokenType
)
326 pPropValArr
[2].Name
= "TabStopRightAligned";
327 pPropValArr
[2].Value
<<= SvxTabAdjust::End
== aToken
.eTabAlign
;
328 pPropValArr
[3].Name
= "TabStopFillCharacter";
329 pPropValArr
[3].Value
<<= OUString(aToken
.cTabFillChar
);
330 pPropValArr
[4].Name
= "TabStopPosition";
331 SwTwips nTempPos
= aToken
.nTabStopPosition
>= 0 ?
332 aToken
.nTabStopPosition
: 0;
333 nTempPos
= convertTwipToMm100(nTempPos
);
334 pPropValArr
[4].Value
<<= static_cast<sal_Int32
>(nTempPos
);
336 else if(TOKEN_TEXT
== aToken
.eTokenType
)
338 pPropValArr
[2].Name
= "Text";
339 pPropValArr
[2].Value
<<= aToken
.sText
;
341 beans::PropertyValues
* pValues
= aSequPropVals
.getArray();
342 pValues
[nTokenIndex
] = aPropVals
;
345 aSequPropVals
.realloc(nTokenIndex
);
347 uno::Any aFormatAccess
= xIdxProps
->getPropertyValue(UNO_NAME_LEVEL_FORMAT
);
348 OSL_ENSURE(aFormatAccess
.getValueType() == cppu::UnoType
<container::XIndexReplace
>::get(),
349 "wrong property type");
351 uno::Reference
< container::XIndexReplace
> xFormatAccess
;
352 aFormatAccess
>>= xFormatAccess
;
353 uno::Any
aLevelProp(&aSequPropVals
, cppu::UnoType
<uno::Sequence
<beans::PropertyValues
>>::get());
354 xFormatAccess
->replaceByIndex(nCurrLevel
, aLevelProp
);
358 if(bInitialCreate
|| !nPage
|| nPage
== TOX_PAGE_STYLES
)
360 lcl_SetProp(xInfo
, xIdxProps
, "ParaStyleHeading", pForm
->GetTemplate(0));
361 sal_uInt16 nOffset
= 0;
362 sal_uInt16 nEndLevel
= 2;
363 switch(m_eCurrentTOXType
.eType
)
369 lcl_SetProp(xInfo
, xIdxProps
, "ParaStyleSeparator", pForm
->GetTemplate(1));
375 default:; //prevent warning
377 for(sal_uInt16 i
= 1; i
< nEndLevel
; i
++)
381 "ParaStyleLevel" + OUString::number( i
),
382 pForm
->GetTemplate(i
+ nOffset
));
385 m_vTypeData
[nTOXIndex
].m_oIndexSections
->xDocumentIndex
->update();
388 catch (const Exception
&)
390 TOOLS_WARN_EXCEPTION( "sw", "::CreateExample()");
392 m_xExampleFrame
->Invalidate();
395 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */