LibreOffice Module sc (master) 1
colrowst.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 <colrowst.hxx>
21
22#include <document.hxx>
23#include <ftools.hxx>
24#include <xltable.hxx>
25#include <xistyle.hxx>
26#include <excimp8.hxx>
27#include <table.hxx>
28
30 XclImpRoot( rRoot ),
31 maColWidths(0, rRoot.GetDoc().GetSheetLimits().GetMaxColCount(), 0),
32 maColFlags(0, rRoot.GetDoc().GetSheetLimits().GetMaxColCount(), ExcColRowFlags::NONE),
33 maRowHeights(0, rRoot.GetDoc().GetSheetLimits().GetMaxRowCount(), 0),
34 maRowFlags(0, rRoot.GetDoc().GetSheetLimits().GetMaxRowCount(), ExcColRowFlags::NONE),
35 maHiddenRows(0, rRoot.GetDoc().GetSheetLimits().GetMaxRowCount(), false),
36 mnLastScRow( -1 ),
37 mnDefWidth( STD_COL_WIDTH ),
38 mnDefHeight( ScGlobal::nStdRowHeight ),
39 mnDefRowFlags( EXC_DEFROW_DEFAULTFLAGS ),
40 mbHasStdWidthRec( false ),
41 mbHasDefHeight( false ),
42 mbDirty( true )
43{
44}
45
47{
48}
49
50void XclImpColRowSettings::SetDefWidth( sal_uInt16 nDefWidth, bool bStdWidthRec )
51{
52 if( bStdWidthRec )
53 {
54 // STANDARDWIDTH record overrides DEFCOLWIDTH record
56 mbHasStdWidthRec = true;
57 }
58 else if( !mbHasStdWidthRec )
59 {
60 // use DEFCOLWIDTH record only, if no STANDARDWIDTH record exists
62 }
63}
64
65void XclImpColRowSettings::SetWidthRange( SCCOL nCol1, SCCOL nCol2, sal_uInt16 nWidth )
66{
67 ScDocument& rDoc = GetDoc();
68 nCol2 = ::std::min( nCol2, rDoc.MaxCol() );
69 if (nCol2 == 256)
70 // In BIFF8, the column range is 0-255, and the use of 256 probably
71 // means the range should extend to the max column if the loading app
72 // support columns beyond 255.
73 nCol2 = rDoc.MaxCol();
74
75 nCol1 = ::std::min( nCol1, nCol2 );
76 maColWidths.insert_back(nCol1, nCol2+1, nWidth);
77
78 // We need to apply flag values individually since all flag values are aggregated for each column.
79 for (SCCOL nCol = nCol1; nCol <= nCol2; ++nCol)
81}
82
84{
85 if (!GetDoc().ValidCol(nCol))
86 return;
87
89}
90
92{
93 nCol2 = ::std::min( nCol2, GetDoc().MaxCol() );
94 nCol1 = ::std::min( nCol1, nCol2 );
95
96 for (SCCOL nCol = nCol1; nCol <= nCol2; ++nCol)
98}
99
100void XclImpColRowSettings::SetDefHeight( sal_uInt16 nDefHeight, sal_uInt16 nFlags )
101{
102 mnDefHeight = nDefHeight;
103 mnDefRowFlags = nFlags;
104 if( mnDefHeight == 0 )
105 {
108 }
109 mbHasDefHeight = true;
110}
111
112void XclImpColRowSettings::SetHeight( SCROW nScRow, sal_uInt16 nHeight )
113{
114 if (!GetDoc().ValidRow(nScRow))
115 return;
116
117 sal_uInt16 nRawHeight = nHeight & EXC_ROW_HEIGHTMASK;
118 bool bDefHeight = ::get_flag( nHeight, EXC_ROW_FLAGDEFHEIGHT ) || (nRawHeight == 0);
119 maRowHeights.insert_back(nScRow, nScRow+1, nRawHeight);
121 if (!maRowFlags.search(nScRow, nFlagVal).second)
122 return;
123
125 ::set_flag(nFlagVal, ExcColRowFlags::Default, bDefHeight);
126
127 maRowFlags.insert_back(nScRow, nScRow+1, nFlagVal);
128
129 if (nScRow > mnLastScRow)
130 mnLastScRow = nScRow;
131}
132
133void XclImpColRowSettings::SetRowSettings( SCROW nScRow, sal_uInt16 nHeight, sal_uInt16 nFlags )
134{
135 if (!GetDoc().ValidRow(nScRow))
136 return;
137
138 SetHeight(nScRow, nHeight);
139
141 if (!maRowFlags.search(nScRow, nFlagVal).second)
142 return;
143
144 if (::get_flag(nFlags, EXC_ROW_UNSYNCED))
146
147 maRowFlags.insert_back(nScRow, nScRow+1, nFlagVal);
148
149 if (::get_flag(nFlags, EXC_ROW_HIDDEN))
150 maHiddenRows.insert_back(nScRow, nScRow+1, true);
151}
152
154{
155 if (!GetDoc().ValidRow(nScRow))
156 return;
157
159 if (!maRowFlags.search(nScRow, nFlagVal).second)
160 return;
161
162 nFlagVal |= ExcColRowFlags::Man;
163 maRowFlags.insert_back(nScRow, nScRow+1, nFlagVal);
164}
165
166void XclImpColRowSettings::SetDefaultXF( SCCOL nCol1, SCCOL nCol2, sal_uInt16 nXFIndex )
167{
168 /* assign the default column formatting here to ensure that
169 explicit cell formatting is not overwritten. */
170 OSL_ENSURE( (nCol1 <= nCol2) && GetDoc().ValidCol( nCol2 ), "XclImpColRowSettings::SetDefaultXF - invalid column index" );
171 nCol2 = ::std::min( nCol2, GetDoc().MaxCol() );
172 nCol1 = ::std::min( nCol1, nCol2 );
173 XclImpXFRangeBuffer& rXFRangeBuffer = GetXFRangeBuffer();
174 for( SCCOL nCol = nCol1; nCol <= nCol2; ++nCol )
175 rXFRangeBuffer.SetColumnDefXF( nCol, nXFIndex );
176}
177
179{
180 if( !mbDirty )
181 return;
182
183 ScDocument& rDoc = GetDoc();
184
185 // column widths ----------------------------------------------------------
186
187 maColWidths.build_tree();
188 for (SCCOL nCol = 0; nCol <= rDoc.MaxCol(); ++nCol)
189 {
190 sal_uInt16 nWidth = mnDefWidth;
192 {
193 sal_uInt16 nTmp;
194 if (maColWidths.search_tree(nCol, nTmp).second)
195 nWidth = nTmp;
196 }
197
198 /* Hidden columns: remember hidden state, but do not set hidden state
199 in document here. Needed for #i11776#, no HIDDEN flags in the
200 document, until filters and outlines are inserted. */
201 if( nWidth == 0 )
202 {
204 nWidth = mnDefWidth;
205 }
206 rDoc.SetColWidthOnly( nCol, nScTab, nWidth );
207 }
208
209 // row heights ------------------------------------------------------------
210
211 // #i54252# set default row height
212 rDoc.SetRowHeightOnly( 0, rDoc.MaxRow(), nScTab, mnDefHeight );
214 // first access to row flags, do not ask for old flags
215 rDoc.SetRowFlags( 0, rDoc.MaxRow(), nScTab, CRFlags::ManualSize );
216
217 maRowHeights.build_tree();
218 if (!maRowHeights.is_tree_valid())
219 return;
220
221 SCROW nPrevRow = -1;
223 for (const auto& [nRow, nFlags] : maRowFlags)
224 {
225 if (nPrevRow >= 0)
226 {
227 sal_uInt16 nHeight = 0;
228
229 if (nPrevFlags & ExcColRowFlags::Used)
230 {
231 if (nPrevFlags & ExcColRowFlags::Default)
232 {
233 nHeight = mnDefHeight;
234 rDoc.SetRowHeightOnly(nPrevRow, nRow-1, nScTab, nHeight);
235 }
236 else
237 {
238 for (SCROW i = nPrevRow; i <= nRow - 1; ++i)
239 {
240 SCROW nLast;
241 if (!maRowHeights.search_tree(i, nHeight, nullptr, &nLast).second)
242 {
243 // search failed for some reason
244 return;
245 }
246
247 if (nLast > nRow)
248 nLast = nRow;
249
250 rDoc.SetRowHeightOnly(i, nLast-1, nScTab, nHeight);
251 i = nLast-1;
252 }
253 }
254
255 if (nPrevFlags & ExcColRowFlags::Man)
256 rDoc.SetManualHeight(nPrevRow, nRow-1, nScTab, true);
257 }
258 else
259 {
260 nHeight = mnDefHeight;
261 rDoc.SetRowHeightOnly(nPrevRow, nRow-1, nScTab, nHeight);
262 }
263 }
264
265 nPrevRow = nRow;
266 nPrevFlags = nFlags;
267 }
268
269 mbDirty = false;
270}
271
273{
274 ScDocument& rDoc = GetDoc();
275
276 // hide the columns
277 for( SCCOL nCol : rDoc.GetColumnsRange(nScTab, 0, rDoc.MaxCol()) )
279 rDoc.ShowCol( nCol, nScTab, false );
280
281 // #i38093# rows hidden by filter need extra flag
282 SCROW nFirstFilterScRow = SCROW_MAX;
283 SCROW nLastFilterScRow = SCROW_MAX;
284 if( GetBiff() == EXC_BIFF8 )
285 {
286 const XclImpAutoFilterData* pFilter = GetFilterManager().GetByTab( nScTab );
287 // #i70026# use IsFiltered() to set the CRFlags::Filtered flag for active filters only
288 if( pFilter && pFilter->IsActive() && pFilter->IsFiltered() )
289 {
290 nFirstFilterScRow = pFilter->StartRow();
291 nLastFilterScRow = pFilter->EndRow();
292 }
293 }
294
295 // In case the excel row limit is lower than calc's, use the visibility of
296 // the last row and extend it to calc's last row.
297 SCROW nLastXLRow = GetRoot().GetXclMaxPos().Row();
298 if (nLastXLRow < rDoc.MaxRow())
299 {
300 bool bHidden = false;
301 if (!maHiddenRows.search(nLastXLRow, bHidden).second)
302 return;
303
304 maHiddenRows.insert_back(nLastXLRow, GetDoc().GetSheetLimits().GetMaxRowCount(), bHidden);
305 }
306
307 SCROW nPrevRow = -1;
308 bool bPrevHidden = false;
309 for (const auto& [nRow, bHidden] : maHiddenRows)
310 {
311 if (nPrevRow >= 0)
312 {
313 if (bPrevHidden)
314 {
315 rDoc.SetRowHidden(nPrevRow, nRow-1, nScTab, true);
316 // #i38093# rows hidden by filter need extra flag
317 if (nFirstFilterScRow <= nPrevRow && nPrevRow <= nLastFilterScRow)
318 {
319 SCROW nLast = ::std::min(nRow-1, nLastFilterScRow);
320 rDoc.SetRowFiltered(nPrevRow, nLast, nScTab, true);
321 }
322 }
323 }
324
325 nPrevRow = nRow;
326 bPrevHidden = bHidden;
327 }
328
329 // #i47438# if default row format is hidden, hide remaining rows
331 rDoc.ShowRows( mnLastScRow + 1, rDoc.MaxRow(), nScTab, false );
332}
333
335{
336 // Get the original flag value.
338 std::pair<ColRowFlagsType::const_iterator,bool> r = maColFlags.search(nCol, nFlagVal);
339 if (!r.second)
340 // Search failed.
341 return;
342
343 ::set_flag(nFlagVal, nNewVal);
344
345 // Re-insert the flag value.
346 maColFlags.insert(r.first, nCol, nCol+1, nFlagVal);
347}
348
350{
352 if (!maColFlags.search(nCol, nFlagVal).second)
353 return false;
354 // Search failed.
355
356 return bool(nFlagVal & nMask);
357}
358
359/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool ValidRow(SCROW nRow, SCROW nMaxRow)
Definition: address.hxx:105
const SCROW SCROW_MAX
Definition: address.hxx:55
bool ValidCol(SCCOL nCol, SCCOL nMaxCol)
Definition: address.hxx:99
SCROW Row() const
Definition: address.hxx:274
SC_DLLPUBLIC SCCOL MaxCol() const
Definition: document.hxx:892
SC_DLLPUBLIC SCROW MaxRow() const
Definition: document.hxx:893
SC_DLLPUBLIC void ShowCol(SCCOL nCol, SCTAB nTab, bool bShow)
Definition: document.cxx:4297
SC_DLLPUBLIC void ShowRows(SCROW nRow1, SCROW nRow2, SCTAB nTab, bool bShow)
Definition: document.cxx:4309
SC_DLLPUBLIC void SetColWidthOnly(SCCOL nCol, SCTAB nTab, sal_uInt16 nNewWidth)
Definition: document.cxx:4092
SC_DLLPUBLIC void SetRowHeightOnly(SCROW nStartRow, SCROW nEndRow, SCTAB nTab, sal_uInt16 nNewHeight)
Definition: document.cxx:4110
SC_DLLPUBLIC void SetRowHidden(SCROW nStartRow, SCROW nEndRow, SCTAB nTab, bool bHidden)
Definition: document.cxx:4442
SC_DLLPUBLIC void SetRowFlags(SCROW nRow, SCTAB nTab, CRFlags nNewFlags)
Definition: document.cxx:4315
SC_DLLPUBLIC void SetManualHeight(SCROW nStartRow, SCROW nEndRow, SCTAB nTab, bool bManual)
Definition: document.cxx:4116
SC_DLLPUBLIC void SetRowFiltered(SCROW nStartRow, SCROW nEndRow, SCTAB nTab, bool bFiltered)
Definition: document.cxx:4496
SC_DLLPUBLIC ScColumnsRange GetColumnsRange(SCTAB nTab, SCCOL nColBegin, SCCOL nColEnd) const
Definition: document.cxx:2541
static SC_DLLPUBLIC sal_uInt16 nStdRowHeight
Definition: global.hxx:596
XclImpAutoFilterData * GetByTab(SCTAB nTab)
Definition: excimp8.cxx:810
SCROW StartRow() const
Definition: excimp8.hxx:85
bool IsFiltered() const
Definition: excimp8.hxx:82
bool IsActive() const
Definition: excimp8.hxx:81
SCROW EndRow() const
Definition: excimp8.hxx:87
void SetDefWidth(sal_uInt16 nDefWidth, bool bStdWidthRec=false)
Definition: colrowst.cxx:50
void Convert(SCTAB nScTab)
Inserts all column and row settings of the specified sheet, except the hidden flags.
Definition: colrowst.cxx:178
bool mbHasDefHeight
true = Width from STANDARDWIDTH (overrides DEFCOLWIDTH record).
Definition: colrowst.hxx:82
bool mbHasStdWidthRec
Default row flags from DEFAULTROWHEIGHT record.
Definition: colrowst.hxx:81
void SetHeight(SCROW nRow, sal_uInt16 nHeight)
Definition: colrowst.cxx:112
void SetDefaultXF(SCCOL nScCol1, SCCOL nScCol2, sal_uInt16 nXFIndex)
Definition: colrowst.cxx:166
XclImpColRowSettings(const XclImpRoot &rRoot)
Definition: colrowst.cxx:29
void HideColRange(SCCOL nCol1, SCCOL nCol2)
Definition: colrowst.cxx:91
sal_uInt16 mnDefRowFlags
Default height from DEFAULTROWHEIGHT record.
Definition: colrowst.hxx:79
sal_uInt16 mnDefHeight
Default width from DEFCOLWIDTH or STANDARDWIDTH record.
Definition: colrowst.hxx:78
sal_uInt16 mnDefWidth
Definition: colrowst.hxx:77
ColRowFlagsType maRowFlags
Definition: colrowst.hxx:72
void SetManualRowHeight(SCROW nScRow)
Definition: colrowst.cxx:153
WidthHeightStoreType maRowHeights
Definition: colrowst.hxx:71
void HideCol(SCCOL nCol)
Definition: colrowst.cxx:83
WidthHeightStoreType maColWidths
Definition: colrowst.hxx:69
ColRowFlagsType maColFlags
Definition: colrowst.hxx:70
void SetDefHeight(sal_uInt16 nDefHeight, sal_uInt16 nFlags)
Definition: colrowst.cxx:100
RowHiddenType maHiddenRows
Definition: colrowst.hxx:73
void ConvertHiddenFlags(SCTAB nScTab)
Sets the HIDDEN flags at all hidden columns and rows in the specified sheet.
Definition: colrowst.cxx:272
virtual ~XclImpColRowSettings() override
Definition: colrowst.cxx:46
bool GetColFlag(SCCOL nCol, ExcColRowFlags nMask) const
Definition: colrowst.cxx:349
void SetRowSettings(SCROW nRow, sal_uInt16 nHeight, sal_uInt16 nFlags)
Definition: colrowst.cxx:133
void SetWidthRange(SCCOL nCol1, SCCOL nCol2, sal_uInt16 nWidth)
Definition: colrowst.cxx:65
void ApplyColFlag(SCCOL nCol, ExcColRowFlags nNewVal)
Definition: colrowst.cxx:334
bool mbDirty
true = mnDefHeight and mnDefRowFlags are valid.
Definition: colrowst.hxx:83
Access to global data from other classes.
Definition: xiroot.hxx:129
XclImpXFRangeBuffer & GetXFRangeBuffer() const
Returns the buffer of XF index ranges for a sheet.
Definition: xiroot.cxx:165
XclImpAutoFilterBuffer & GetFilterManager() const
Returns the filter manager.
Definition: xiroot.cxx:219
const XclImpRoot & GetRoot() const
Returns this root instance - for code readability in derived classes.
Definition: xiroot.hxx:134
Contains the XF indexes for every used cell in a single sheet.
Definition: xistyle.hxx:608
void SetColumnDefXF(SCCOL nScCol, sal_uInt16 nXFIndex)
Inserts a new XF index for all cells in a column.
Definition: xistyle.cxx:1947
XclBiff GetBiff() const
Returns the current BIFF version of the importer/exporter.
Definition: xlroot.hxx:141
const ScAddress & GetXclMaxPos() const
Returns the highest possible cell address in an Excel document (using current BIFF version).
Definition: xlroot.hxx:246
ScDocument & GetDoc() const
Returns reference to the destination document (import) or source document (export).
Definition: xlroot.cxx:285
ExcColRowFlags
Definition: colrowst.hxx:26
bool get_flag(Type nBitField, Type nMask)
Returns true, if at least one of the bits set in nMask is set in nBitField.
Definition: ftools.hxx:75
void set_flag(Type &rnBitField, Type nMask, bool bSet=true)
Sets or clears (according to bSet) all set bits of nMask in rnBitField.
Definition: ftools.hxx:95
constexpr sal_Int32 STD_COL_WIDTH
Definition: global.hxx:87
int i
static sal_uInt16 nDefWidth
Definition: op.cxx:50
sal_Int16 SCTAB
Definition: types.hxx:22
sal_Int16 SCCOL
Definition: types.hxx:21
sal_Int32 SCROW
Definition: types.hxx:17
@ EXC_BIFF8
MS Excel 5.0, MS Excel 7.0 (95)
Definition: xlconst.hxx:35
const sal_uInt16 EXC_ROW_HIDDEN
Definition: xltable.hxx:83
const sal_uInt16 EXC_ROW_UNSYNCED
Definition: xltable.hxx:84
const sal_uInt16 EXC_DEFROW_UNSYNCED
Definition: xltable.hxx:114
const sal_uInt16 EXC_ROW_FLAGDEFHEIGHT
Definition: xltable.hxx:91
const sal_uInt16 EXC_DEFROW_DEFAULTFLAGS
Definition: xltable.hxx:118
const sal_uInt16 EXC_DEFROW_HIDDEN
Definition: xltable.hxx:115
const sal_uInt16 EXC_ROW_HEIGHTMASK
Definition: xltable.hxx:92