summaryrefslogtreecommitdiff
path: root/include/editeng/boxitem.hxx
blob: 0d9ee80281ef37f83f16ef18138b3daca5cc2f9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#ifndef INCLUDED_EDITENG_BOXITEM_HXX
#define INCLUDED_EDITENG_BOXITEM_HXX

#include <svl/poolitem.hxx>
#include <editeng/editengdllapi.h>
#include <com/sun/star/table/BorderLine2.hpp>
#include <o3tl/typed_flags_set.hxx>
#include <docmodel/color/ComplexColor.hxx>
#include <memory>
#include <array>


namespace editeng { class SvxBorderLine; }

// class SvxBoxItem ------------------------------------------------------

/*  [Description]

    This item describes a border attribute
    (all four edges and the inward distance)
*/
enum class SvxBoxItemLine
{
    TOP, BOTTOM, LEFT, RIGHT, LAST = RIGHT
};

/**
This version causes SvxBoxItem to store the 4 cell spacing distances separately
when serializing to stream.
*/
constexpr sal_uInt16 BOX_4DISTS_VERSION = 1;
/**
This version causes SvxBoxItem to store the styles for its border lines when
serializing to stream.
*/
constexpr sal_uInt16 BOX_BORDER_STYLE_VERSION = 2;

class EDITENG_DLLPUBLIC SvxBoxItem final : public SfxPoolItem
{
    std::unique_ptr<editeng::SvxBorderLine> mpTopBorderLine;
    std::unique_ptr<editeng::SvxBorderLine> mpBottomBorderLine;
    std::unique_ptr<editeng::SvxBorderLine> mpLeftBorderLine;
    std::unique_ptr<editeng::SvxBorderLine> mpRightBorderLine;

    sal_Int16 mnTopDistance = 0;
    sal_Int16 mnBottomDistance = 0;
    sal_Int16 mnLeftDistance = 0;
    sal_Int16 mnRightDistance = 0;

    // Store complex colors until lines are created...
    std::array<model::ComplexColor, 4> maTempComplexColors;

    bool mbRemoveAdjCellBorder = false;

    void tryMigrateComplexColor(SvxBoxItemLine eLine);

public:
    static SfxPoolItem* CreateDefault();

    explicit SvxBoxItem( const sal_uInt16 nId );
    SvxBoxItem( const SvxBoxItem &rCpy );
    virtual ~SvxBoxItem() override;

    // "pure virtual Methods" from SfxPoolItem
    virtual bool            operator==( const SfxPoolItem& ) const override;
    virtual bool            QueryValue( css::uno::Any& rVal, sal_uInt8 nMemberId = 0 ) const override;
    virtual bool            PutValue( const css::uno::Any& rVal, sal_uInt8 nMemberId ) override;

    virtual bool GetPresentation( SfxItemPresentation ePres,
                                    MapUnit eCoreMetric,
                                    MapUnit ePresMetric,
                                    OUString &rText, const IntlWrapper& ) const override;

    virtual SvxBoxItem*      Clone( SfxItemPool *pPool = nullptr ) const override;

    virtual void             ScaleMetrics( tools::Long nMult, tools::Long nDiv ) override;
    virtual bool             HasMetrics() const override;

    const editeng::SvxBorderLine* GetTop() const
    {
        return mpTopBorderLine.get();
    }
    const editeng::SvxBorderLine* GetBottom() const
    {
        return mpBottomBorderLine.get();
    }
    const editeng::SvxBorderLine* GetLeft() const
    {
        return mpLeftBorderLine.get();
    }
    const editeng::SvxBorderLine* GetRight() const
    {
        return mpRightBorderLine.get();
    }

    editeng::SvxBorderLine* GetTop()
    {
        return mpTopBorderLine.get();
    }
    editeng::SvxBorderLine* GetBottom()
    {
        return mpBottomBorderLine.get();
    }
    editeng::SvxBorderLine* GetLeft()
    {
        return mpLeftBorderLine.get();
    }
    editeng::SvxBorderLine* GetRight()
    {
        return mpRightBorderLine.get();
    }

    const   editeng::SvxBorderLine* GetLine( SvxBoxItemLine nLine ) const;

    //The Pointers are being copied!
    void    SetLine( const editeng::SvxBorderLine* pNew, SvxBoxItemLine nLine );

    sal_Int16  GetDistance( SvxBoxItemLine nLine, bool bAllowNegative = false ) const;
    sal_uInt16  GetSmallestDistance() const;

    bool IsRemoveAdjacentCellBorder() const { return mbRemoveAdjCellBorder; }
    const std::array<model::ComplexColor, 4>& GetTempComplexColors() const {return maTempComplexColors;}

    void    SetDistance( sal_Int16 nNew, SvxBoxItemLine nLine );
    void SetAllDistances(sal_Int16 nNew)
    {
        mnTopDistance = mnBottomDistance = mnLeftDistance = mnRightDistance = nNew;
    }

    void SetRemoveAdjacentCellBorder( bool bSet ) { mbRemoveAdjCellBorder = bSet; }

    // Line width plus Space plus inward distance
    // bEvenIfNoLine = TRUE -> Also return distance, when no Line is set
    sal_uInt16  CalcLineWidth( SvxBoxItemLine nLine ) const;
    sal_Int16  CalcLineSpace( SvxBoxItemLine nLine, bool bEvenIfNoLine = false, bool bAllowNegative = false ) const;
    bool HasBorder( bool bTreatPaddingAsBorder ) const;
    static css::table::BorderLine2 SvxLineToLine( const editeng::SvxBorderLine* pLine, bool bConvert );
    static bool LineToSvxLine(const css::table::BorderLine& rLine, editeng::SvxBorderLine& rSvxLine, bool bConvert);
    static bool LineToSvxLine(const css::table::BorderLine2& rLine, editeng::SvxBorderLine& rSvxLine, bool bConvert);

    virtual boost::property_tree::ptree dumpAsJSON() const override;
    void dumpAsXml(xmlTextWriterPtr pWriter) const override;
};

// class SvxBoxInfoItem --------------------------------------------------

/*  [Description]

    Another item for the border. This item has only limited functionality.
    On one hand, the general Dialog is told by the item which options it
    should offer. On the other hand, this attribute  may be used to
    transported the borderline for the inner horizontal and vertical lines.
*/

enum class SvxBoxInfoItemLine
{
    HORI, VERT, LAST = VERT
};

enum class SvxBoxInfoItemValidFlags
{
    NONE          = 0x00,
    TOP           = 0x01,
    BOTTOM        = 0x02,
    LEFT          = 0x04,
    RIGHT         = 0x08,
    HORI          = 0x10,
    VERT          = 0x20,
    DISTANCE      = 0x40,
    DISABLE       = 0x80,
    ALL           = 0xff
};
namespace o3tl
{
    template<> struct typed_flags<SvxBoxInfoItemValidFlags> : is_typed_flags<SvxBoxInfoItemValidFlags, 0xff> {};
}

class EDITENG_DLLPUBLIC SvxBoxInfoItem final : public SfxPoolItem
{
    std::unique_ptr<editeng::SvxBorderLine> mpHorizontalLine; //inner horizontal Line
    std::unique_ptr<editeng::SvxBorderLine> mpVerticalLine; //inner vertical Line

    bool mbEnableHorizontalLine = false; /// true = Enable inner horizontal line.
    bool mbEnableVerticalLine = false; /// true = Enable inner vertical line.

    /*
     Currently only for StarWriter: distance inward from SvxBoxItem. If the
     distance is requested, then the field for the distance from the dialog be
     activated. nDefDist is regarded as a default value. If any line is
     turned on or will be turned on it must this distance be set to default.
     bMinDist indicates whether the user can go below this value or not.
     With NDIST is the current distance from the app transported back and
     forth to the dialogue.
    */

    bool mbDistance :1;  // TRUE, Unlock Distance.
    bool mbMinimumDistance :1;  // TRUE, Going below minimum Distance is prohibited

    SvxBoxInfoItemValidFlags mnValidFlags;
    sal_uInt16 mnDefaultMinimumDistance = 0; // The default or minimum distance.

public:
    static SfxPoolItem* CreateDefault();

    explicit SvxBoxInfoItem( const sal_uInt16 nId );
    SvxBoxInfoItem( const SvxBoxInfoItem &rCpy );
    virtual ~SvxBoxInfoItem() override;

    // "pure virtual Methods" from SfxPoolItem
    virtual bool            operator==( const SfxPoolItem& ) const override;
    virtual bool            GetPresentation( SfxItemPresentation ePres,
                                    MapUnit eCoreMetric,
                                    MapUnit ePresMetric,
                                    OUString &rText, const IntlWrapper& ) const override;
    virtual bool            QueryValue( css::uno::Any& rVal, sal_uInt8 nMemberId = 0 ) const override;
    virtual bool            PutValue( const css::uno::Any& rVal, sal_uInt8 nMemberId ) override;

    virtual SvxBoxInfoItem* Clone( SfxItemPool *pPool = nullptr ) const override;
    virtual void            ScaleMetrics( tools::Long nMult, tools::Long nDiv ) override;
    virtual bool            HasMetrics() const override;

    const editeng::SvxBorderLine*   GetHori() const { return mpHorizontalLine.get(); }
    const editeng::SvxBorderLine*   GetVert() const { return mpVerticalLine.get(); }

    //The Pointers are being copied!
    void                    SetLine( const editeng::SvxBorderLine* pNew, SvxBoxInfoItemLine nLine );

    bool IsTable() const { return mbEnableHorizontalLine && mbEnableVerticalLine; }
    void SetTable(bool bNew) { mbEnableHorizontalLine = mbEnableVerticalLine = bNew; }

    bool IsHorEnabled() const { return mbEnableHorizontalLine; }
    void EnableHor( bool bEnable ) { mbEnableHorizontalLine = bEnable; }
    bool IsVerEnabled() const { return mbEnableVerticalLine; }
    void EnableVer( bool bEnable ) { mbEnableVerticalLine = bEnable; }

    bool IsDist() const { return mbDistance; }
    void SetDist(bool bNew)
    {
        mbDistance = bNew;
    }
    bool IsMinDist() const { return mbMinimumDistance; }
    void SetMinDist(bool bNew) { mbMinimumDistance = bNew; }
    sal_uInt16 GetDefDist() const { return mnDefaultMinimumDistance; }
    void SetDefDist(sal_uInt16 nNew) { mnDefaultMinimumDistance = nNew; }

    bool IsValid( SvxBoxInfoItemValidFlags nValid ) const
    {
        return bool(mnValidFlags & nValid);
    }
    void SetValid(SvxBoxInfoItemValidFlags nValid, bool bValid = true)
    {
        if (bValid)
            mnValidFlags |= nValid;
        else
            mnValidFlags &= ~nValid;
    }
    void ResetFlags();

    virtual boost::property_tree::ptree dumpAsJSON() const override;
};

namespace editeng
{

void EDITENG_DLLPUBLIC BorderDistanceFromWord(bool bFromEdge, sal_Int32& nMargin,
    sal_Int32& nBorderDistance, sal_Int32 nBorderWidth);

struct EDITENG_DLLPUBLIC WordPageMargins final
{
    sal_uInt16 nLeft = 0;
    sal_uInt16 nRight = 0;
    sal_uInt16 nTop = 0;
    sal_uInt16 nBottom = 0;
};

struct EDITENG_DLLPUBLIC WordBorderDistances final
{
    bool bFromEdge = false;
    sal_uInt16 nLeft = 0;
    sal_uInt16 nRight = 0;
    sal_uInt16 nTop = 0;
    sal_uInt16 nBottom = 0;
};

// Heuristics to decide if we need to use "from edge" offset of borders. All sizes in twips
void EDITENG_DLLPUBLIC BorderDistancesToWord(const SvxBoxItem& rBox, const WordPageMargins& rMargins,
    WordBorderDistances& rDistances);

}

#endif

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */