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 .
19 #ifndef INCLUDED_SW_INC_TBLAFMT_HXX
20 #define INCLUDED_SW_INC_TBLAFMT_HXX
22 * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
24 * The structure of table auto formatting should not be changed. It is used
25 * by different code of Writer and Calc. If a change is necessary, the
26 * source code of both applications must be changed!
28 * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
33 #include <editeng/keepitem.hxx>
34 #include <editeng/frmdiritem.hxx>
35 #include <editeng/shaditem.hxx>
36 #include <svx/autoformathelper.hxx>
37 #include <unotools/weakref.hxx>
38 #include <rtl/ref.hxx>
39 #include "fmtornt.hxx"
44 class SvNumberFormatter
;
46 class SwXTextCellStyle
;
47 class SwXTextTableStyle
;
49 class SW_DLLPUBLIC SwBoxAutoFormat
: public AutoFormatBase
53 std::unique_ptr
<SvxFrameDirectionItem
> m_aTextOrientation
;
54 std::unique_ptr
<SwFormatVertOrient
> m_aVerticalAlignment
;
57 OUString m_sNumFormatString
;
58 LanguageType m_eSysLanguage
;
59 LanguageType m_eNumFormatLanguage
;
61 // associated UNO object, if such exists
62 unotools::WeakReference
<SwXTextCellStyle
> m_xAutoFormatUnoObject
;
66 SwBoxAutoFormat( const SwBoxAutoFormat
& rNew
);
69 /// assignment-op (still used)
70 SwBoxAutoFormat
& operator=(const SwBoxAutoFormat
& rRef
);
72 /// Comparing based of boxes backgrounds.
73 bool operator==(const SwBoxAutoFormat
& rRight
) const;
76 const SvxFrameDirectionItem
& GetTextOrientation() const { return *m_aTextOrientation
; }
77 const SwFormatVertOrient
& GetVerticalAlignment() const { return *m_aVerticalAlignment
; }
79 void GetValueFormat( OUString
& rFormat
, LanguageType
& rLng
, LanguageType
& rSys
) const
80 { rFormat
= m_sNumFormatString
; rLng
= m_eNumFormatLanguage
; rSys
= m_eSysLanguage
; }
82 const OUString
& GetNumFormatString() const { return m_sNumFormatString
; }
83 const LanguageType
& GetSysLanguage() const { return m_eSysLanguage
; }
84 const LanguageType
& GetNumFormatLanguage() const { return m_eNumFormatLanguage
; }
87 void SetTextOrientation( const SvxFrameDirectionItem
& rNew
) { m_aTextOrientation
.reset(rNew
.Clone()); }
88 void SetVerticalAlignment( const SwFormatVertOrient
& rNew
) { m_aVerticalAlignment
.reset(rNew
.Clone()); }
90 void SetValueFormat( const OUString
& rFormat
, LanguageType eLng
, LanguageType eSys
)
91 { m_sNumFormatString
= rFormat
; m_eNumFormatLanguage
= eLng
; m_eSysLanguage
= eSys
; }
93 void SetNumFormatString(const OUString
& rNew
) { m_sNumFormatString
= rNew
; }
94 void SetSysLanguage(const LanguageType
& rNew
) { m_eSysLanguage
= rNew
; }
95 void SetNumFormatLanguage(const LanguageType
& rNew
) { m_eNumFormatLanguage
= rNew
; }
97 unotools::WeakReference
<SwXTextCellStyle
> const& GetXObject() const
98 { return m_xAutoFormatUnoObject
; }
99 void SetXObject(rtl::Reference
<SwXTextCellStyle
> const& xObject
);
101 bool Load( SvStream
& rStream
, const SwAfVersions
& rVersions
, sal_uInt16 nVer
);
102 bool Save( SvStream
& rStream
, sal_uInt16 fileVersion
) const;
105 enum class SwTableAutoFormatUpdateFlags
{ Char
= 1, Box
= 2 };
107 template<> struct typed_flags
<SwTableAutoFormatUpdateFlags
> : is_typed_flags
<SwTableAutoFormatUpdateFlags
, 0x03> {};
112 A table has a number of lines. These lines seem to correspond with rows, except in the case of
113 rows spanning more than one line. Each line contains a number of boxes/cells.
115 AutoFormat properties are retrieved and stored in a grid of 16 table boxes. A sampling approach
116 is used to read the data. 4 lines are picked, and 4 boxes are picked from each.
118 The line picking and box picking algorithms are similar. We start at the first line/box, and pick
119 lines/boxes one by one for a maximum of 3. The 4th line/box is the last line/box in the current
120 table/line. If we hit the end of lines/boxes, the last line/box encountered is picked several times.
122 For example, in a 2x3 table, the 4 lines will be [0, 1, 1, 1]. In each line, the boxes will be
123 [0, 1, 2, 2]. In a 6x5 table, the 4 lines will be [0, 1, 2, 4] and the boxes per line will be
126 As you can see, property extraction/application is lossless for tables that are 4x4 or smaller
127 (and in fact has a bit of redundancy). For larger tables, we lose any individual cell formatting
128 for the range [(3,rows - 1) -> (3, cols - 1)]. That formatting is replaced by formatting from
132 +-----------------------------------------------------------------------+
133 0 | Saved | Saved | Saved | | | Saved |
134 +-----------------------------------------------------------------------+
135 1 | Saved | Saved | Saved | | | Saved |
136 +-----------------------------------------------------------------------+
137 2 | Saved | Saved | Saved | | | Saved |
138 +-----------------------------------------------------------------------+
140 +-----------------------------------------------------------------------+
142 +-----------------------------------------------------------------------+
143 5 | Saved | Saved | Saved | | | Saved |
144 +-----------+-----------+-----------+-----------+-----------+-----------+
146 The properties saved are divided into three categories:
147 1. Character properties: Font, font size, weight, etc.
148 2. Box properties: Box, cell background
149 3. Table properties: Properties that are set in the Table->Table Properties dialog.
151 Character and box properties are stored per cell (and are lossy for tables larger than 4x4). Table
152 properties are stored per-table, and are lossless.
154 class SW_DLLPUBLIC SwTableAutoFormat
156 friend class SwDocTest
;
157 friend void FinitCore(); // To destroy default pointer.
158 static SwBoxAutoFormat
* s_pDefaultBoxAutoFormat
;
160 unotools::WeakReference
<SwXTextTableStyle
> m_xUnoTextTableStyle
;
163 sal_uInt16 m_nStrResId
;
165 // Common flags of Calc and Writer.
166 bool m_bInclFont
: 1;
167 bool m_bInclJustify
: 1;
168 bool m_bInclFrame
: 1;
169 bool m_bInclBackground
: 1;
170 bool m_bInclValueFormat
: 1;
172 // Calc specific flags.
173 bool m_bInclWidthHeight
: 1;
175 SwBoxAutoFormat
* m_aBoxAutoFormat
[ 16 ] = {};
177 // Writer-specific options
178 std::shared_ptr
<SvxFormatKeepItem
> m_aKeepWithNextPara
;
179 sal_uInt16 m_aRepeatHeading
;
182 bool m_bCollapsingBorders
;
183 std::shared_ptr
<SvxShadowItem
> m_aShadow
;
188 SwTableAutoFormat( OUString aName
);
189 SwTableAutoFormat( const SwTableAutoFormat
& rNew
);
190 ~SwTableAutoFormat();
192 SwTableAutoFormat
& operator=( const SwTableAutoFormat
& rNew
);
194 const SvxFormatKeepItem
& GetKeepWithNextPara() const { return *m_aKeepWithNextPara
; }
195 const SvxShadowItem
& GetShadow() const { return *m_aShadow
; }
197 void SetKeepWithNextPara(const SvxFormatKeepItem
& rNew
) { m_aKeepWithNextPara
.reset(rNew
.Clone()); }
198 void SetShadow(const SvxShadowItem
& rNew
) { m_aShadow
.reset(rNew
.Clone()); }
200 void SetBoxFormat( const SwBoxAutoFormat
& rNew
, sal_uInt8 nPos
);
201 const SwBoxAutoFormat
& GetBoxFormat( sal_uInt8 nPos
) const;
202 SwBoxAutoFormat
& GetBoxFormat( sal_uInt8 nPos
);
203 static const SwBoxAutoFormat
& GetDefaultBoxFormat();
205 void SetName( const OUString
& rNew
) { m_aName
= rNew
; m_nStrResId
= USHRT_MAX
; }
206 const OUString
& GetName() const { return m_aName
; }
208 void UpdateFromSet( sal_uInt8 nPos
, const SfxItemSet
& rSet
,
209 SwTableAutoFormatUpdateFlags eFlags
, SvNumberFormatter
const * );
210 void UpdateToSet( const sal_uInt8 nPos
, const bool bSingleRowTable
, const bool bSingleColTable
,
211 SfxItemSet
& rSet
, SwTableAutoFormatUpdateFlags eFlags
,
212 SvNumberFormatter
* ) const ;
214 void RestoreTableProperties(SwTable
&table
) const;
215 void StoreTableProperties(const SwTable
&table
);
217 bool IsFont() const { return m_bInclFont
; }
218 bool IsJustify() const { return m_bInclJustify
; }
219 bool IsFrame() const { return m_bInclFrame
; }
220 bool IsBackground() const { return m_bInclBackground
; }
221 bool IsValueFormat() const { return m_bInclValueFormat
; }
223 /// Check if style is hidden.
224 bool IsHidden() const { return m_bHidden
; }
225 /// Check if style is defined by user.
226 bool IsUserDefined() const { return m_bUserDefined
; }
229 void SetFont( const bool bNew
) { m_bInclFont
= bNew
; }
230 void SetJustify( const bool bNew
) { m_bInclJustify
= bNew
; }
231 void SetFrame( const bool bNew
) { m_bInclFrame
= bNew
; }
232 void SetBackground( const bool bNew
) { m_bInclBackground
= bNew
; }
233 void SetValueFormat( const bool bNew
) { m_bInclValueFormat
= bNew
; }
234 void SetWidthHeight( const bool bNew
) { m_bInclWidthHeight
= bNew
; }
236 /// Set if style is hidden.
237 void SetHidden(bool bHidden
) { m_bHidden
= bHidden
; }
238 /// Set if style is user defined.
239 void SetUserDefined(bool bUserDefined
) { m_bUserDefined
= bUserDefined
; }
241 /// These methods returns what style (row or column) is applied first on given Cell
242 bool FirstRowEndColumnIsRow();
243 bool FirstRowStartColumnIsRow();
244 bool LastRowEndColumnIsRow();
245 bool LastRowStartColumnIsRow();
246 bool HasHeaderRow() const;
248 bool Load( SvStream
& rStream
, const SwAfVersions
& );
249 bool Save( SvStream
& rStream
, sal_uInt16 fileVersion
) const;
251 unotools::WeakReference
<SwXTextTableStyle
> const& GetXObject() const
252 { return m_xUnoTextTableStyle
; }
253 void SetXObject(rtl::Reference
<SwXTextTableStyle
> const& xObject
);
255 /// Returns the cell's name postfix. eg. ".1"
256 OUString
GetTableTemplateCellSubName(const SwBoxAutoFormat
& rBoxFormat
) const;
257 /// Returns a vector of indexes in aBoxAutoFormat array. Returned indexes points to cells which are mapped to a table-template.
258 static const std::vector
<sal_Int32
>& GetTableTemplateMap();
261 * Calculates the relevant position in the table autoformat for a given
262 * cell in a given table.
264 static sal_uInt8
CountPos(sal_uInt32 nCol
, sal_uInt32 nCols
, sal_uInt32 nRow
, sal_uInt32 nRows
);
267 class SW_DLLPUBLIC SwTableAutoFormatTable
270 std::unique_ptr
<Impl
> m_pImpl
;
272 SAL_DLLPRIVATE
bool Load( SvStream
& rStream
);
273 SAL_DLLPRIVATE
bool Save( SvStream
& rStream
) const;
276 explicit SwTableAutoFormatTable();
277 ~SwTableAutoFormatTable();
280 SwTableAutoFormat
const& operator[](size_t i
) const;
281 SwTableAutoFormat
& operator[](size_t i
);
283 /// Append table style to the existing styles.
284 void AddAutoFormat(const SwTableAutoFormat
& rFormat
);
286 void InsertAutoFormat(size_t i
, std::unique_ptr
<SwTableAutoFormat
> pFormat
);
287 void EraseAutoFormat(size_t i
);
288 void EraseAutoFormat(const OUString
& rName
);
289 std::unique_ptr
<SwTableAutoFormat
> ReleaseAutoFormat(size_t i
);
290 /// Removes an autoformat. Returns pointer to the removed autoformat or nullptr.
291 std::unique_ptr
<SwTableAutoFormat
> ReleaseAutoFormat(const OUString
& rName
);
293 /// Find table style with the provided name, return nullptr when not found.
294 SwTableAutoFormat
* FindAutoFormat(std::u16string_view rName
) const;
300 class SwCellStyleDescriptor
302 const std::pair
<OUString
, std::unique_ptr
<SwBoxAutoFormat
>>& m_rCellStyleDesc
;
304 SwCellStyleDescriptor(const std::pair
<OUString
, std::unique_ptr
<SwBoxAutoFormat
>>& rCellStyleDesc
) : m_rCellStyleDesc(rCellStyleDesc
) { }
306 const OUString
& GetName() const { return m_rCellStyleDesc
.first
; }
307 const SwBoxAutoFormat
& GetAutoFormat() const { return *m_rCellStyleDesc
.second
; }
310 class SwCellStyleTable
312 std::vector
<std::pair
<OUString
, std::unique_ptr
<SwBoxAutoFormat
>>> m_aCellStyles
;
318 SwCellStyleDescriptor
operator[](size_t i
) const;
321 /// Add a copy of rBoxFormat
322 void AddBoxFormat(const SwBoxAutoFormat
& rBoxFormat
, const OUString
& sName
);
323 void RemoveBoxFormat(const OUString
& sName
);
324 void ChangeBoxFormatName(std::u16string_view sFromName
, const OUString
& sToName
);
325 /// If found returns its name. If not found returns an empty OUString
326 const OUString
& GetBoxFormatName(const SwBoxAutoFormat
& rBoxFormat
) const;
327 /// If found returns a ptr to a BoxFormat. If not found returns nullptr
328 SwBoxAutoFormat
* GetBoxFormat(std::u16string_view sName
) const;
333 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */