LibreOffice Module sw (master) 1
uinums.cxx
Go to the documentation of this file.
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 <memory>
21#include <uinums.hxx>
22
24#include <tools/stream.hxx>
25#include <sfx2/docfile.hxx>
26#include <svl/itemiter.hxx>
27
28#include <swtypes.hxx>
29#include <utility>
30#include <wrtsh.hxx>
31#include <poolfmt.hxx>
32#include <charfmt.hxx>
33
34using namespace ::com::sun::star;
35
36constexpr OUStringLiteral CHAPTER_FILENAME = u"chapter.cfg";
37
38/*
39 Description: Saving a rule
40 Parameter: rCopy -- the rule to save
41 nIdx -- position, where the rule is to be saved.
42 An old rule at that position will be overwritten.
43*/
44
46{
47 Init();
48}
49
51{
53 SvtPathOptions aPathOpt;
54 aURL.SetSmartURL( aPathOpt.GetUserConfigPath() );
55 aURL.setFinalSlash();
56 aURL.Append(CHAPTER_FILENAME);
57
58 SfxMedium aMedium( aURL.GetMainURL(INetURLObject::DecodeMechanism::NONE), StreamMode::WRITE );
59 SvStream* pStream = aMedium.GetOutStream();
60 bool bRet = (pStream && pStream->GetError() == ERRCODE_NONE);
61 if (bRet)
62 {
64
65 pStream->FlushBuffer();
66
67 aMedium.Commit();
68 }
69}
70
72{
73}
74
76{
77 for(auto & rpNumRule : m_pNumRules)
78 rpNumRule.reset();
79
80 OUString sNm(CHAPTER_FILENAME);
81 SvtPathOptions aOpt;
82 if( aOpt.SearchFile( sNm ))
83 {
84 SfxMedium aStrm( sNm, StreamMode::STD_READ );
87 }
88}
89
90void SwChapterNumRules::CreateEmptyNumRule(sal_uInt16 const nIndex)
91{
92 assert(nIndex < nMaxRules);
93 assert(!m_pNumRules[nIndex]);
95}
96
97void SwChapterNumRules::ApplyNumRules(const SwNumRulesWithName &rCopy, sal_uInt16 nIdx)
98{
99 assert(nIdx < nMaxRules);
100 if( !m_pNumRules[nIdx] )
101 m_pNumRules[nIdx].reset(new SwNumRulesWithName( rCopy ));
102 else
103 *m_pNumRules[nIdx] = rCopy;
104 Save(); // store it immediately
105}
106
108 OUString aName )
109 : maName(std::move(aName))
110{
111 for( sal_uInt16 n = 0; n < MAXLEVEL; ++n )
112 {
113 const SwNumFormat* pFormat = rCopy.GetNumFormat( n );
114 if( pFormat )
115 m_aFormats[ n ].reset(new SwNumFormatGlobal( *pFormat ));
116 else
117 m_aFormats[ n ].reset();
118 }
119}
120
122{
123 *this = rCopy;
124}
125
127{
128}
129
131{
132 if( this != &rCopy )
133 {
134 maName = rCopy.maName;
135 for( int n = 0; n < MAXLEVEL; ++n )
136 {
137 SwNumFormatGlobal* pFormat = rCopy.m_aFormats[ n ].get();
138 if( pFormat )
139 m_aFormats[ n ].reset(new SwNumFormatGlobal( *pFormat ));
140 else
141 m_aFormats[ n ].reset();
142 }
143 }
144 return *this;
145}
146
148{
149 // #i89178#
150 rNumRule.Reset(maName);
151 rNumRule.SetAutoRule( false );
152 for (sal_uInt16 n = 0; n < MAXLEVEL; ++n)
153 {
154 SwNumFormatGlobal* pFormat = m_aFormats[ n ].get();
155 if (!pFormat)
156 continue;
157 rNumRule.Set(n, pFormat->MakeNumFormat(rSh));
158 }
159}
160
162 size_t const nIndex, SwNumFormat const*& rpNumFormat, OUString const*& rpName) const
163{
164 rpNumFormat = (m_aFormats[nIndex]) ? &m_aFormats[nIndex]->m_aFormat : nullptr;
165 rpName = (m_aFormats[nIndex]) ? &m_aFormats[nIndex]->m_sCharFormatName : nullptr;
166}
167
169 size_t const nIndex, SwNumFormat const& rNumFormat, OUString const& rName)
170{
171 m_aFormats[nIndex].reset( new SwNumFormatGlobal(rNumFormat) );
172 m_aFormats[nIndex]->m_sCharFormatName = rName;
173 m_aFormats[nIndex]->m_nCharPoolId = USHRT_MAX;
174 m_aFormats[nIndex]->m_Items.clear();
175}
176
178 : m_aFormat( rFormat ), m_nCharPoolId( USHRT_MAX )
179{
180 // relative gaps?????
181
182 SwCharFormat* pFormat = rFormat.GetCharFormat();
183 if( !pFormat )
184 return;
185
186 m_sCharFormatName = pFormat->GetName();
187 m_nCharPoolId = pFormat->GetPoolFormatId();
188 if( pFormat->GetAttrSet().Count() )
189 {
190 SfxItemIter aIter( pFormat->GetAttrSet() );
191 const SfxPoolItem *pCurr = aIter.GetCurItem();
192 do
193 {
194 m_Items.push_back(std::unique_ptr<SfxPoolItem>(pCurr->Clone()));
195 pCurr = aIter.NextItem();
196 } while (pCurr);
197 }
198
199 m_aFormat.SetCharFormat( nullptr );
200}
201
203 :
204 m_aFormat( rFormat.m_aFormat ),
205 m_sCharFormatName( rFormat.m_sCharFormatName ),
206 m_nCharPoolId( rFormat.m_nCharPoolId )
207{
208 for (size_t n = rFormat.m_Items.size(); n; )
209 {
210 m_Items.push_back(std::unique_ptr<SfxPoolItem>(rFormat.m_Items[ --n ]->Clone()));
211 }
212}
213
215{
216}
217
219{
220 SwCharFormat* pFormat = nullptr;
221 if( !m_sCharFormatName.isEmpty() )
222 {
223 // at first, look for the name
224 sal_uInt16 nArrLen = rSh.GetCharFormatCount();
225 for( sal_uInt16 i = 1; i < nArrLen; ++i )
226 {
227 pFormat = &rSh.GetCharFormat( i );
228 if (pFormat->GetName()==m_sCharFormatName)
229 // exists, so leave attributes as they are!
230 break;
231 pFormat = nullptr;
232 }
233
234 if( !pFormat )
235 {
236 if( IsPoolUserFormat( m_nCharPoolId ) )
237 {
238 pFormat = rSh.MakeCharFormat( m_sCharFormatName );
239 pFormat->SetAuto(false);
240 }
241 else
242 pFormat = rSh.GetCharFormatFromPool( m_nCharPoolId );
243
244 if( !pFormat->HasWriterListeners() ) // set attributes
245 {
246 for (size_t n = m_Items.size(); n; )
247 {
248 pFormat->SetFormatAttr( *m_Items[ --n ] );
249 }
250 }
251 }
252 }
253 const_cast<SwNumFormat&>(m_aFormat).SetCharFormat(pFormat);
254 SwNumFormat aNew = m_aFormat;
255 if (pFormat)
256 const_cast<SwNumFormat&>(m_aFormat).SetCharFormat(nullptr);
257 return aNew;
258}
259
260/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OUString maName
const SfxPoolItem * GetCurItem() const
const SfxPoolItem * NextItem()
sal_uInt16 Count() const
SvStream * GetOutStream()
SvStream * GetInStream()
bool Commit()
virtual SfxPoolItem * Clone(SfxItemPool *pPool=nullptr) const=0
ErrCode GetError() const
void FlushBuffer()
bool SearchFile(OUString &rIniFile, Paths ePath=Paths::UserConfig)
const OUString & GetUserConfigPath() const
void CreateEmptyNumRule(sal_uInt16 nIdx)
Definition: uinums.cxx:90
void ApplyNumRules(const SwNumRulesWithName &rCopy, sal_uInt16 nIdx)
Definition: uinums.cxx:97
std::unique_ptr< SwNumRulesWithName > m_pNumRules[MAX_NUM_RULES]
Definition: uinums.hxx:85
Represents the style of a text portion.
Definition: charfmt.hxx:27
SwCharFormat * GetCharFormatFromPool(sal_uInt16 nId)
Definition: editsh.hxx:354
SwCharFormat * MakeCharFormat(const OUString &rName)
Definition: edfmt.cxx:114
sal_uInt16 GetCharFormatCount() const
CHAR.
Definition: edfmt.cxx:29
SwCharFormat & GetCharFormat(sal_uInt16 nFormat) const
Definition: edfmt.cxx:34
sal_uInt16 GetPoolFormatId() const
Get and set Pool style IDs.
Definition: format.hxx:163
const OUString & GetName() const
Definition: format.hxx:131
void SetAuto(bool bNew)
Definition: format.hxx:179
const SwAttrSet & GetAttrSet() const
For querying the attribute array.
Definition: format.hxx:136
virtual bool SetFormatAttr(const SfxPoolItem &rAttr)
Definition: format.cxx:447
SwCharFormat * GetCharFormat() const
Definition: numrule.hxx:74
void SetCharFormat(SwCharFormat *)
Definition: number.cxx:281
void SetAutoRule(bool bFlag)
Definition: numrule.hxx:230
const SwNumFormat * GetNumFormat(sal_uInt16 i) const
Definition: number.cxx:97
void Set(sal_uInt16 i, const SwNumFormat *)
Definition: number.cxx:618
void Reset(const OUString &rName)
Definition: number.cxx:566
std::vector< std::unique_ptr< SfxPoolItem > > m_Items
Definition: uinums.hxx:47
SwNumFormatGlobal(const SwNumFormat &rFormat)
Definition: uinums.cxx:177
SwNumFormat MakeNumFormat(SwWrtShell &rSh) const
Definition: uinums.cxx:218
void ResetNumRule(SwWrtShell &rSh, SwNumRule &) const
Definition: uinums.cxx:147
void GetNumFormat(size_t, SwNumFormat const *&, OUString const *&) const
Definition: uinums.cxx:161
std::unique_ptr< SwNumFormatGlobal > m_aFormats[MAXLEVEL]
Definition: uinums.hxx:59
SwNumRulesWithName()=default
SwNumRulesWithName & operator=(const SwNumRulesWithName &)
Definition: uinums.cxx:130
OUString maName
Definition: uinums.hxx:38
void SetNumFormat(size_t, SwNumFormat const &, OUString const &)
Definition: uinums.cxx:168
Used by the UI to modify the document model.
Definition: wrtsh.hxx:97
URL aURL
float u
#define ERRCODE_NONE
sal_Int32 nIndex
OUString aName
sal_Int64 n
int i
void ExportStoredChapterNumberingRules(SwChapterNumRules &rRules, SvStream &rStream, OUString const &rFileName)
void ImportStoredChapterNumberingRules(SwChapterNumRules &rRules, SvStream &rStream, OUString const &rFileName)
bool IsPoolUserFormat(sal_uInt16 nId)
Definition: poolfmt.hxx:86
constexpr sal_uInt8 MAXLEVEL
Definition: swtypes.hxx:92
constexpr OUStringLiteral CHAPTER_FILENAME
Definition: uinums.cxx:36