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 <lotattr.hxx>
22
23 #include <editeng/borderline.hxx>
24 #include <editeng/boxitem.hxx>
25 #include <editeng/brushitem.hxx>
26 #include <editeng/colritem.hxx>
27 #include <editeng/justifyitem.hxx>
28 #include <sal/log.hxx>
29
30 #include <docpool.hxx>
31 #include <document.hxx>
32 #include <lotfntbf.hxx>
33 #include "lotfilter.hxx"
34 #include <patattr.hxx>
35 #include <scitems.hxx>
36
37 using namespace ::com::sun::star;
38
ENTRY(std::unique_ptr<ScPatternAttr> p)39 LotAttrCache::ENTRY::ENTRY (std::unique_ptr<ScPatternAttr> p)
40 : pPattAttr(std::move(p))
41 , nHash0(0)
42 {
43 }
44
~ENTRY()45 LotAttrCache::ENTRY::~ENTRY ()
46 {
47 }
48
LotAttrCache(LotusContext & rContext)49 LotAttrCache::LotAttrCache (LotusContext& rContext)
50 : mrContext(rContext)
51 {
52 pDocPool = rContext.rDoc.GetPool();
53
54 pColTab.reset( new Color [ 8 ] );
55 pColTab[ 0 ] = COL_WHITE;
56 pColTab[ 1 ] = COL_LIGHTBLUE;
57 pColTab[ 2 ] = COL_LIGHTGREEN;
58 pColTab[ 3 ] = COL_LIGHTCYAN;
59 pColTab[ 4 ] = COL_LIGHTRED;
60 pColTab[ 5 ] = COL_LIGHTMAGENTA;
61 pColTab[ 6 ] = COL_YELLOW;
62 pColTab[ 7 ] = COL_BLACK;
63
64 ppColorItems[ 0 ].reset( new SvxColorItem( GetColor( 1 ), ATTR_FONT_COLOR ) ); // 1
65 ppColorItems[ 1 ].reset( new SvxColorItem( GetColor( 2 ), ATTR_FONT_COLOR ) );
66 ppColorItems[ 2 ].reset( new SvxColorItem( GetColor( 3 ), ATTR_FONT_COLOR ) );
67 ppColorItems[ 3 ].reset( new SvxColorItem( GetColor( 4 ), ATTR_FONT_COLOR ) );
68 ppColorItems[ 4 ].reset( new SvxColorItem( GetColor( 5 ), ATTR_FONT_COLOR ) );
69 ppColorItems[ 5 ].reset( new SvxColorItem( GetColor( 6 ), ATTR_FONT_COLOR ) ); // 6
70
71 pWhite.reset( new SvxColorItem( COL_WHITE, ATTR_FONT_COLOR ) );
72 }
73
~LotAttrCache()74 LotAttrCache::~LotAttrCache()
75 {
76 }
77
GetPattAttr(const LotAttrWK3 & rAttr)78 const ScPatternAttr& LotAttrCache::GetPattAttr( const LotAttrWK3& rAttr )
79 {
80 sal_uInt32 nRefHash;
81 MakeHash( rAttr, nRefHash );
82
83 std::vector< std::unique_ptr<ENTRY> >::const_iterator iter
84 = std::find_if(aEntries.begin(),aEntries.end(),
85 [nRefHash] (const std::unique_ptr<ENTRY>& rEntry) { return rEntry->nHash0 == nRefHash; } );
86
87 if (iter != aEntries.end())
88 return *((*iter)->pPattAttr);
89
90 // generate new Pattern Attribute
91 ScPatternAttr* pNewPatt = new ScPatternAttr(pDocPool);
92
93 SfxItemSet& rItemSet = pNewPatt->GetItemSet();
94 ENTRY *pCurrent = new ENTRY( std::unique_ptr<ScPatternAttr>(pNewPatt) );
95
96 pCurrent->nHash0 = nRefHash;
97
98 mrContext.maFontBuff.Fill( rAttr.nFont, rItemSet );
99
100 sal_uInt8 nLine = rAttr.nLineStyle;
101 if( nLine )
102 {
103 SvxBoxItem aBox( ATTR_BORDER );
104 ::editeng::SvxBorderLine aTop, aLeft, aBottom, aRight;
105
106 LotusToScBorderLine( nLine, aLeft );
107 nLine >>= 2;
108 LotusToScBorderLine( nLine, aRight );
109 nLine >>= 2;
110 LotusToScBorderLine( nLine, aTop );
111 nLine >>= 2;
112 LotusToScBorderLine( nLine, aBottom );
113
114 aBox.SetLine( &aTop, SvxBoxItemLine::TOP );
115 aBox.SetLine( &aLeft, SvxBoxItemLine::LEFT );
116 aBox.SetLine( &aBottom, SvxBoxItemLine::BOTTOM );
117 aBox.SetLine( &aRight, SvxBoxItemLine::RIGHT );
118
119 rItemSet.Put( aBox );
120 }
121
122 sal_uInt8 nFontCol = rAttr.nFontCol & 0x07;
123 if( nFontCol )
124 {
125 // nFontCol > 0
126 if( nFontCol < 7 )
127 rItemSet.Put( GetColorItem( nFontCol ) );
128 else
129 rItemSet.Put( *pWhite );
130 }
131
132 sal_uInt8 nBack = rAttr.nBack & 0x1F;
133 if( nBack )
134 rItemSet.Put( SvxBrushItem( GetColor( nBack & 0x07 ), ATTR_BACKGROUND ) );
135
136 if( rAttr.nBack & 0x80 )
137 {
138 SvxHorJustifyItem aHorJustify(SvxCellHorJustify::Center, ATTR_HOR_JUSTIFY );
139 rItemSet.Put( aHorJustify );
140 }
141
142 aEntries.push_back(std::unique_ptr<ENTRY>(pCurrent));
143
144 return *pNewPatt;
145 }
146
LotusToScBorderLine(sal_uInt8 nLine,::editeng::SvxBorderLine & aBL)147 void LotAttrCache::LotusToScBorderLine( sal_uInt8 nLine, ::editeng::SvxBorderLine& aBL )
148 {
149 nLine &= 0x03;
150
151 switch ( nLine )
152 {
153 case 0: aBL.SetBorderLineStyle(SvxBorderLineStyle::NONE); break;
154 case 1: aBL.SetWidth( DEF_LINE_WIDTH_1 ); break;
155 case 2: aBL.SetWidth( DEF_LINE_WIDTH_2 ); break;
156 case 3:
157 {
158 aBL.SetBorderLineStyle(SvxBorderLineStyle::DOUBLE_THIN);
159 aBL.SetWidth( DEF_LINE_WIDTH_1 );
160 }
161 break;
162 }
163 }
164
GetColorItem(const sal_uInt8 nLotIndex) const165 const SvxColorItem& LotAttrCache::GetColorItem( const sal_uInt8 nLotIndex ) const
166 {
167 // *LotAttrCache::GetColorItem(): caller has to check index!
168 assert( nLotIndex > 0 && nLotIndex < 7 );
169
170 return *ppColorItems[ nLotIndex - 1 ];
171 }
172
GetColor(const sal_uInt8 nLotIndex) const173 const Color& LotAttrCache::GetColor( const sal_uInt8 nLotIndex ) const
174 {
175 // color <-> index fits background, but not for fonts (0 <-> 7)!
176 // *LotAttrCache::GetColor(): Index > 7, caller hast to check index!"
177 assert( nLotIndex < 8 );
178
179 return pColTab[ nLotIndex ];
180 }
181
SetAttr(const ScDocument * pDoc,const SCROW nRow,const ScPatternAttr & rAttr)182 void LotAttrCol::SetAttr( const ScDocument* pDoc, const SCROW nRow, const ScPatternAttr& rAttr )
183 {
184 // Actually with the current implementation of MAXROWCOUNT1>=64k and nRow
185 // being read as sal_uInt16 there's no chance that nRow would be invalid...
186 SAL_WARN_IF( !pDoc->ValidRow(nRow), "sc.filter", "*LotAttrCol::SetAttr(): ... and failed?!" );
187
188 std::vector<std::unique_ptr<ENTRY> >::reverse_iterator iterLast = aEntries.rbegin();
189
190 if(iterLast != aEntries.rend())
191 {
192 if( ( (*iterLast)->nLastRow == nRow - 1 ) && ( &rAttr == (*iterLast)->pPattAttr ) )
193 (*iterLast)->nLastRow = nRow;
194 else
195 {
196 ENTRY *pCurrent = new ENTRY;
197
198 pCurrent->pPattAttr = &rAttr;
199 pCurrent->nFirstRow = pCurrent->nLastRow = nRow;
200
201 aEntries.push_back(std::unique_ptr<ENTRY>(pCurrent));
202 }
203 }
204 else
205 { // first entry
206 ENTRY *pCurrent = new ENTRY;
207 pCurrent->pPattAttr = &rAttr;
208 pCurrent->nFirstRow = pCurrent->nLastRow = nRow;
209
210 aEntries.push_back(std::unique_ptr<ENTRY>(pCurrent));
211 }
212 }
213
Apply(LotusContext & rContext,const SCCOL nColNum,const SCTAB nTabNum)214 void LotAttrCol::Apply(LotusContext& rContext, const SCCOL nColNum, const SCTAB nTabNum)
215 {
216 ScDocument& rDoc = rContext.rDoc;
217
218 for (const auto& rxEntry : aEntries)
219 {
220 rDoc.ApplyPatternAreaTab(nColNum, rxEntry->nFirstRow, nColNum, rxEntry->nLastRow,
221 nTabNum, *(rxEntry->pPattAttr));
222 }
223 }
224
LotAttrTable(LotusContext & rContext)225 LotAttrTable::LotAttrTable(LotusContext& rContext):
226 aAttrCache(rContext)
227 {
228 }
229
SetAttr(LotusContext & rContext,const SCCOL nColFirst,const SCCOL nColLast,const SCROW nRow,const LotAttrWK3 & rAttr)230 void LotAttrTable::SetAttr( LotusContext& rContext, const SCCOL nColFirst, const SCCOL nColLast, const SCROW nRow,
231 const LotAttrWK3& rAttr )
232 {
233 // With the current implementation of MAXCOLCOUNT>=1024 and nColFirst and
234 // nColLast being calculated as sal_uInt8+sal_uInt8 there's no chance that
235 // they would be invalid.
236 const ScPatternAttr &rPattAttr = aAttrCache.GetPattAttr( rAttr );
237 SCCOL nColCnt;
238
239 for( nColCnt = nColFirst ; nColCnt <= nColLast ; nColCnt++ )
240 pCols[ nColCnt ].SetAttr( &rContext.rDoc, nRow, rPattAttr );
241 }
242
Apply(LotusContext & rContext,const SCTAB nTabNum)243 void LotAttrTable::Apply(LotusContext& rContext, const SCTAB nTabNum)
244 {
245 SCCOL nColCnt;
246 for( nColCnt = 0 ; nColCnt <= aAttrCache.mrContext.rDoc.MaxCol() ; nColCnt++ )
247 pCols[ nColCnt ].Apply(rContext, nColCnt, nTabNum); // does a Clear() at end
248 }
249
250 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
251