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 <hintids.hxx>
21 #include <i18nlangtag/mslangid.hxx>
22 #include <editeng/boxitem.hxx>
23 #include <editeng/frmdiritem.hxx>
24 #include <osl/diagnose.h>
25 #include <doc.hxx>
26 #include <IDocumentState.hxx>
27 #include <IDocumentStylePoolAccess.hxx>
28 #include <poolfmt.hxx>
29 #include <pagedesc.hxx>
30 #include <fmtcol.hxx>
31 #include <numrule.hxx>
32 #include <swtable.hxx>
33 #include <tblafmt.hxx>
34 #include <hints.hxx>
35 
36 using namespace ::editeng;
37 using namespace ::com::sun::star;
38 
SetAllScriptItem(SfxItemSet & rSet,const SfxPoolItem & rItem)39 void SetAllScriptItem( SfxItemSet& rSet, const SfxPoolItem& rItem )
40 {
41     rSet.Put( rItem );
42     sal_uInt16 nWhCJK = 0, nWhCTL = 0;
43     switch( rItem.Which() )
44     {
45     case RES_CHRATR_FONTSIZE:
46         nWhCJK = RES_CHRATR_CJK_FONTSIZE;
47         nWhCTL = RES_CHRATR_CTL_FONTSIZE;
48         break;
49     case RES_CHRATR_FONT:
50         nWhCJK = RES_CHRATR_CJK_FONT;
51         nWhCTL = RES_CHRATR_CTL_FONT;
52         break;
53     case RES_CHRATR_LANGUAGE:
54         nWhCJK = RES_CHRATR_CJK_LANGUAGE;
55         nWhCTL = RES_CHRATR_CTL_LANGUAGE;
56         break;
57     case RES_CHRATR_POSTURE:
58         nWhCJK = RES_CHRATR_CJK_POSTURE;
59         nWhCTL = RES_CHRATR_CTL_POSTURE;
60         break;
61     case RES_CHRATR_WEIGHT:
62         nWhCJK = RES_CHRATR_CJK_WEIGHT;
63         nWhCTL = RES_CHRATR_CTL_WEIGHT;
64         break;
65     }
66 
67     if( nWhCJK )
68         rSet.Put( rItem.CloneSetWhich(nWhCJK) );
69     if( nWhCTL )
70         rSet.Put( rItem.CloneSetWhich(nWhCTL) );
71 }
72 
73 /// Return the AutoCollection by its Id. If it doesn't
74 /// exist yet, create it.
75 /// If the String pointer is defined, then only query for
76 /// the Attribute descriptions. It doesn't create a style!
GetDefaultFrameDirection(LanguageType nLanguage)77 SvxFrameDirection GetDefaultFrameDirection(LanguageType nLanguage)
78 {
79     return MsLangId::isRightToLeft(nLanguage) ?
80             SvxFrameDirection::Horizontal_RL_TB : SvxFrameDirection::Horizontal_LR_TB;
81 }
82 
83 // See if the Paragraph/Character/Frame/Page style is in use
IsUsed(const SwModify & rModify) const84 bool SwDoc::IsUsed( const SwModify& rModify ) const
85 {
86     // Check if we have dependent ContentNodes in the Nodes array
87     // (also indirect ones for derived Formats)
88     SwAutoFormatGetDocNode aGetHt( &GetNodes() );
89     return !rModify.GetInfo( aGetHt );
90 }
91 
92 // See if Table style is in use
IsUsed(const SwTableAutoFormat & rTableAutoFormat) const93 bool SwDoc::IsUsed( const SwTableAutoFormat& rTableAutoFormat) const
94 {
95     size_t nTableCount = GetTableFrameFormatCount(true);
96     for (size_t i=0; i < nTableCount; ++i)
97     {
98         SwFrameFormat* pFrameFormat = &GetTableFrameFormat(i, true);
99         SwTable* pTable = SwTable::FindTable(pFrameFormat);
100         if (pTable->GetTableStyleName() == rTableAutoFormat.GetName())
101             return true;
102     }
103     return false;
104 }
105 
106 // See if the NumRule is used
IsUsed(const SwNumRule & rRule)107 bool SwDoc::IsUsed( const SwNumRule& rRule )
108 {
109     bool bUsed = rRule.GetTextNodeListSize() > 0 ||
110                      rRule.GetParagraphStyleListSize() > 0 ||
111                      rRule.IsUsedByRedline();
112 
113     return bUsed;
114 }
115 
GetDocPattern(size_t const nPos) const116 const OUString* SwDoc::GetDocPattern(size_t const nPos) const
117 {
118     if (nPos >= m_PatternNames.size())
119         return nullptr;
120     return &m_PatternNames[nPos];
121 }
122 
123 // Look for the style name's position. If it doesn't exist,
124 // insert an anew
SetDocPattern(const OUString & rPatternName)125 size_t SwDoc::SetDocPattern(const OUString& rPatternName)
126 {
127     OSL_ENSURE( !rPatternName.isEmpty(), "no Document style name" );
128 
129     auto const iter(
130         std::find(m_PatternNames.begin(), m_PatternNames.end(), rPatternName));
131     if (iter != m_PatternNames.end())
132     {
133         return std::distance(m_PatternNames.begin(), iter);
134     }
135     else
136     {
137         m_PatternNames.push_back(rPatternName);
138         getIDocumentState().SetModified();
139         return m_PatternNames.size() - 1;
140     }
141 }
142 
GetPoolParent(sal_uInt16 nId)143 sal_uInt16 GetPoolParent( sal_uInt16 nId )
144 {
145     sal_uInt16 nRet = USHRT_MAX;
146     if( POOLGRP_NOCOLLID & nId )        // 1 == Formats / 0 == Collections
147     {
148         switch( ( COLL_GET_RANGE_BITS | POOLGRP_NOCOLLID ) & nId )
149         {
150         case POOLGRP_CHARFMT:
151         case POOLGRP_FRAMEFMT:
152             nRet = 0;           // derived from the default
153             break;
154         case POOLGRP_PAGEDESC:
155         case POOLGRP_NUMRULE:
156             break;              // there are no derivations
157         }
158     }
159     else
160     {
161         switch( COLL_GET_RANGE_BITS & nId )
162         {
163         case COLL_TEXT_BITS:
164             switch( nId )
165             {
166             case RES_POOLCOLL_STANDARD:
167                     nRet = 0;                                   break;
168             case RES_POOLCOLL_TEXT_IDENT:
169             case RES_POOLCOLL_TEXT_NEGIDENT:
170             case RES_POOLCOLL_TEXT_MOVE:
171             case RES_POOLCOLL_CONFRONTATION:
172             case RES_POOLCOLL_MARGINAL:
173                     nRet = RES_POOLCOLL_TEXT;                   break;
174 
175             case RES_POOLCOLL_TEXT:
176             case RES_POOLCOLL_GREETING:
177             case RES_POOLCOLL_SIGNATURE:
178             case RES_POOLCOLL_HEADLINE_BASE:
179                     nRet = RES_POOLCOLL_STANDARD;               break;
180 
181             case RES_POOLCOLL_HEADLINE1:
182             case RES_POOLCOLL_HEADLINE2:
183             case RES_POOLCOLL_HEADLINE3:
184             case RES_POOLCOLL_HEADLINE4:
185             case RES_POOLCOLL_HEADLINE5:
186             case RES_POOLCOLL_HEADLINE6:
187             case RES_POOLCOLL_HEADLINE7:
188             case RES_POOLCOLL_HEADLINE8:
189             case RES_POOLCOLL_HEADLINE9:
190             case RES_POOLCOLL_HEADLINE10:
191                     nRet = RES_POOLCOLL_HEADLINE_BASE;          break;
192             }
193             break;
194 
195         case COLL_LISTS_BITS:
196             switch( nId )
197             {
198             case RES_POOLCOLL_NUMBUL_BASE:
199                     nRet = RES_POOLCOLL_TEXT;                   break;
200 
201             default:
202                 nRet = RES_POOLCOLL_NUMBUL_BASE;                break;
203             }
204             break;
205 
206         case COLL_EXTRA_BITS:
207             switch( nId )
208             {
209             case RES_POOLCOLL_TABLE_HDLN:
210                     nRet = RES_POOLCOLL_TABLE;                 break;
211 
212             case RES_POOLCOLL_FRAME:
213             case RES_POOLCOLL_TABLE:
214             case RES_POOLCOLL_FOOTNOTE:
215             case RES_POOLCOLL_ENDNOTE:
216             case RES_POOLCOLL_JAKETADRESS:
217             case RES_POOLCOLL_SENDADRESS:
218             case RES_POOLCOLL_HEADERFOOTER:
219             case RES_POOLCOLL_LABEL:
220                     nRet = RES_POOLCOLL_STANDARD;              break;
221             case RES_POOLCOLL_HEADER:
222                     nRet = RES_POOLCOLL_HEADERFOOTER;          break;
223             case RES_POOLCOLL_HEADERL:
224             case RES_POOLCOLL_HEADERR:
225                     nRet = RES_POOLCOLL_HEADER;                break;
226             case RES_POOLCOLL_FOOTER:
227                     nRet = RES_POOLCOLL_HEADERFOOTER;          break;
228             case RES_POOLCOLL_FOOTERL:
229             case RES_POOLCOLL_FOOTERR:
230                     nRet = RES_POOLCOLL_FOOTER;                break;
231 
232             case RES_POOLCOLL_LABEL_ABB:
233             case RES_POOLCOLL_LABEL_TABLE:
234             case RES_POOLCOLL_LABEL_FRAME:
235             case RES_POOLCOLL_LABEL_DRAWING:
236             case RES_POOLCOLL_LABEL_FIGURE:
237                     nRet = RES_POOLCOLL_LABEL;                  break;
238             }
239             break;
240 
241         case COLL_REGISTER_BITS:
242             switch( nId )
243             {
244             case RES_POOLCOLL_REGISTER_BASE:
245                     nRet = RES_POOLCOLL_STANDARD;               break;
246 
247             case RES_POOLCOLL_TOX_IDXH:
248                     nRet = RES_POOLCOLL_HEADLINE_BASE;          break;
249 
250             case RES_POOLCOLL_TOX_USERH:
251             case RES_POOLCOLL_TOX_CNTNTH:
252             case RES_POOLCOLL_TOX_ILLUSH:
253             case RES_POOLCOLL_TOX_OBJECTH:
254             case RES_POOLCOLL_TOX_TABLESH:
255             case RES_POOLCOLL_TOX_AUTHORITIESH:
256                     nRet = RES_POOLCOLL_TOX_IDXH;               break;
257 
258             default:
259                     nRet = RES_POOLCOLL_REGISTER_BASE;          break;
260             }
261             break;
262 
263         case COLL_DOC_BITS:
264             nRet = RES_POOLCOLL_HEADLINE_BASE;
265             break;
266 
267         case COLL_HTML_BITS:
268             nRet = RES_POOLCOLL_STANDARD;
269             break;
270         }
271     }
272 
273     return nRet;
274 }
275 
RemoveAllFormatLanguageDependencies()276 void SwDoc::RemoveAllFormatLanguageDependencies()
277 {
278     /* Restore the language independent pool defaults and styles. */
279     GetAttrPool().ResetPoolDefaultItem( RES_PARATR_ADJUST );
280 
281     SwTextFormatColl * pTextFormatColl = getIDocumentStylePoolAccess().GetTextCollFromPool( RES_POOLCOLL_STANDARD );
282 
283     pTextFormatColl->ResetFormatAttr( RES_PARATR_ADJUST );
284     /* koreans do not like SvxScriptItem(TRUE) */
285     pTextFormatColl->ResetFormatAttr( RES_PARATR_SCRIPTSPACE );
286 
287     SvxFrameDirectionItem aFrameDir( SvxFrameDirection::Horizontal_LR_TB, RES_FRAMEDIR );
288 
289     size_t nCount = GetPageDescCnt();
290     for( size_t i=0; i<nCount; ++i )
291     {
292         SwPageDesc& rDesc = GetPageDesc( i );
293         rDesc.GetMaster().SetFormatAttr( aFrameDir );
294         rDesc.GetLeft().SetFormatAttr( aFrameDir );
295     }
296 
297     //#i16874# AutoKerning as default for new documents
298     GetAttrPool().ResetPoolDefaultItem( RES_CHRATR_AUTOKERN );
299 }
300 
301 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
302