Coverage Report

Created: 2026-05-16 09:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/sw/inc/fldbas.hxx
Line
Count
Source
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
#pragma once
21
22
#include <i18nlangtag/lang.h>
23
#include "swdllapi.h"
24
#include "swtypes.hxx"
25
#include "calbck.hxx"
26
#include "nodeoffset.hxx"
27
#include "names.hxx"
28
29
#include <com/sun/star/beans/XPropertySet.hpp>
30
#include <cppuhelper/weakref.hxx>
31
#include <editeng/svxenum.hxx>
32
#include <o3tl/enumarray.hxx>
33
#include <unotools/weakref.hxx>
34
#include <rtl/ref.hxx>
35
#include <vector>
36
#include <climits>
37
38
class SwDoc;
39
class SwField;
40
class SwFormatField;
41
class SwDDETable;
42
class SwRootFrame;
43
class SvNumberFormatter;
44
class IDocumentRedlineAccess;
45
class SwGetRefField;
46
class SwXFieldMaster;
47
enum class ReferencesSubtype : sal_uInt16;
48
namespace com::sun::star::uno { class Any; }
49
50
typedef struct _xmlTextWriter* xmlTextWriterPtr;
51
52
enum class SwFieldIds : sal_uInt16 {
53
/// For old documents the Field-Which IDs must be preserved !!!
54
    Database,
55
    User,
56
    Filename,
57
    DatabaseName,
58
    Date,
59
    Time,
60
    PageNumber,
61
    Author,
62
    Chapter,
63
    DocStat,
64
    GetExp, // 10
65
    SetExp,
66
    GetRef,
67
    HiddenText,
68
    Postit,
69
    FixDate,
70
    FixTime,
71
    Reg,
72
    VarReg,
73
    SetRef,
74
    Input, // 20
75
    Macro,
76
    Dde,
77
    Table,
78
    HiddenPara,
79
    DocInfo,
80
    TemplateName,
81
    DbNextSet,
82
    DbNumSet,
83
    DbSetNumber,
84
    ExtUser, // 30
85
    RefPageSet,
86
    RefPageGet,
87
    Internet,
88
    JumpEdit,
89
    Script,
90
    DateTime,
91
    TableOfAuthorities,
92
    CombinedChars,
93
    Dropdown,
94
    ParagraphSignature, // 40
95
    LAST = ParagraphSignature,
96
97
    Unknown = USHRT_MAX, // used as default value in some method calls
98
};
99
100
/// List of FieldTypes at UI.
101
enum class SwFieldTypesEnum : sal_uInt16 {
102
    Begin,
103
    Date = Begin,    // 0
104
    Time,
105
    Filename,
106
    DatabaseName,
107
    Chapter,
108
    PageNumber,
109
    DocumentStatistics,
110
    Author,
111
    Set,
112
    Get,
113
    Formel,              // 10
114
    HiddenText,
115
    SetRef,
116
    GetRef,
117
    DDE,
118
    Macro,
119
    Input,
120
    HiddenParagraph,
121
    DocumentInfo,
122
    Database,
123
    User,                // 20
124
    Postit,
125
    TemplateName,
126
    Sequence,
127
    DatabaseNextSet,
128
    DatabaseNumberSet,
129
    DatabaseSetNumber,
130
    ConditionalText,
131
    NextPage,
132
    PreviousPage,
133
    ExtendedUser,             // 30
134
    FixedDate,
135
    FixedTime,
136
    SetInput,
137
    UserInput,
138
    SetRefPage,
139
    GetRefPage,
140
    Internet,
141
    JumpEdit,
142
    Script,
143
    Authority,              // 40
144
    CombinedChars,
145
    Dropdown,
146
    Custom,     // Unused - necessary for alignment with aSwFields in fldmgr.cxx
147
    ParagraphSignature,
148
    LAST = ParagraphSignature,
149
    Unknown = USHRT_MAX // used by SwFieldMgr::GetCurTypeId
150
};
151
152
// tdf#170392 - this should be aligned with FMT_FF_ARY in sw/source/uibase/fldui/fldmgr.cxx
153
// including com::sun::star::text::FilenameDisplayFormat
154
enum class SwFileNameFormat {
155
    // most of the constants are a regular enum
156
    PathName,
157
    Path,
158
    NameNoExt,
159
    Name,
160
    UIName,
161
    UIRange,
162
    End, // marker value, used for asserts
163
    // except for this, which is a flag
164
    Fixed = 0x8000
165
};
166
namespace o3tl { template<> struct typed_flags<SwFileNameFormat> : is_typed_flags<SwFileNameFormat, 0x800f> {}; }
167
168
enum class SwTableFieldSubType : sal_uInt16
169
{
170
    String    = 0x0001, ///< String
171
    Formula   = 0x0010, ///< Formula
172
    Command   = 0x0100, ///< Show command.
173
};
174
namespace o3tl { template<> struct typed_flags<SwTableFieldSubType> : is_typed_flags<SwTableFieldSubType, 0x0111> {}; }
175
176
enum class SwGetSetExpType : sal_uInt16
177
{
178
    None          = 0x0000,
179
    String        = 0x0001, ///< String
180
    Expr          = 0x0002, ///< Expression
181
    Sequence      = 0x0008, ///< Sequence
182
    Formula       = 0x0010, ///< Formula
183
    LowerMask     = 0x001f,
184
    UpperMask     = 0x0300,
185
    Command       = 0x0100, ///< Show command.
186
    Invisible     = 0x0200  ///< Invisible.
187
};
188
namespace o3tl { template<> struct typed_flags<SwGetSetExpType> : is_typed_flags<SwGetSetExpType, 0x031f> {}; }
189
190
enum class SwDBFieldSubType : sal_uInt16
191
{
192
    None      = 0x0000,
193
    Invisible = 0x0200,   ///< Invisible.
194
    OwnFormat = 0x0400    ///< SwDBField: Don't accept formatting from database.
195
};
196
namespace o3tl { template<> struct typed_flags<SwDBFieldSubType> : is_typed_flags<SwDBFieldSubType, 0x0600> {}; }
197
198
enum class SwInputFieldSubType : sal_uInt16
199
{
200
    None        = 0x0000,
201
    Text        = 0x0001,
202
    User        = 0x0002,
203
    Var         = 0x0003,
204
    LowerMask   = 0x0003,
205
    UpperMask   = 0x0200,
206
    Invisible   = 0x0200   ///< Invisible.
207
};
208
namespace o3tl { template<> struct typed_flags<SwInputFieldSubType> : is_typed_flags<SwInputFieldSubType, 0x0203> {}; }
209
210
enum class SwUserType : sal_uInt16 {
211
    None          = 0x0000,
212
    String        = 0x0001, ///< String
213
    Expr          = 0x0002, ///< Expression
214
    LowerMask     = 0x0003,
215
    UpperMask     = 0x0300,
216
    ShowCommand   = 0x0100, ///< Show command.
217
    Invisible     = 0x0200, ///< Invisible.
218
};
219
namespace o3tl { template<> struct typed_flags<SwUserType> : is_typed_flags<SwUserType, 0x0303> {}; }
220
221
enum class SwDateTimeSubType : sal_uInt16 {
222
    None  = 0,
223
    Fixed = 1,
224
    Date  = 2,
225
    Time  = 4
226
};
227
namespace o3tl { template<> struct typed_flags<SwDateTimeSubType> : is_typed_flags<SwDateTimeSubType, 0x0007> {}; }
228
229
/// General tools.
230
OUString  FormatNumber(sal_uInt32 nNum, SvxNumType nFormat, LanguageType nLang = LANGUAGE_NONE);
231
SwFieldTypesEnum SwFieldTypeFromString(std::u16string_view rString);
232
233
/** Instances of SwFields and those derived from it occur 0 to n times.
234
 For each class there is one instance of the associated type class.
235
 Base class of all field types is SwFieldType. */
236
237
class SW_DLLPUBLIC SwFieldType : public sw::BroadcastingModify
238
{
239
    unotools::WeakReference<SwXFieldMaster> m_wXFieldMaster;
240
241
    SwFieldIds m_nWhich;
242
243
    friend void FinitUI();     ///< In order to delete pointer!
244
    static o3tl::enumarray<SwFieldTypesEnum, OUString>* s_pFieldNames;
245
246
    static void GetFieldName_();  ///< Sets up FieldNames; fldmgr.cxx!
247
248
protected:
249
    /// Single argument ctors shall be explicit.
250
    explicit SwFieldType( SwFieldIds nWhichId );
251
252
public:
253
254
971k
    unotools::WeakReference<SwXFieldMaster> const& GetXObject() const {
255
971k
        return m_wXFieldMaster;
256
971k
    }
257
    void SetXObject(rtl::Reference<SwXFieldMaster> const& xFieldMaster);
258
259
    static const OUString & GetTypeStr( SwFieldTypesEnum nTypeId );
260
261
    /// Only in derived classes.
262
    virtual UIName        GetName() const;
263
    virtual void SetName(const UIName& newName);
264
    virtual std::unique_ptr<SwFieldType> Copy() const = 0;
265
    virtual void QueryValue( css::uno::Any& rVal, sal_uInt16 nWhich ) const;
266
    virtual void PutValue( const css::uno::Any& rVal, sal_uInt16 nWhich );
267
268
28.5M
    SwFieldIds              Which() const { return m_nWhich; }
269
270
    void PrintHiddenPara();
271
    virtual void dumpAsXml(xmlTextWriterPtr pWriter) const;
272
    SwFormatField* FindFormatForField(const SwField*) const;
273
    SwFormatField* FindFormatForPostItId(sal_uInt32 nPostItId) const;
274
    void CollectPostIts(std::vector<SwFormatField*>& rvFormatFields, IDocumentRedlineAccess const& rIDRA, bool HideRedlines);
275
    bool HasHiddenInformationNotes() const;
276
    void GatherNodeIndex(std::vector<SwNodeOffset>& rvNodeIndex);
277
    void GatherRefFields(std::vector<SwGetRefField*>& rvRFields, const ReferencesSubtype nTyp);
278
    void GatherFields(std::vector<SwFormatField*>& rvFormatFields, bool bCollectOnlyInDocNodes=true) const;
279
    void GatherDdeTables(std::vector<SwDDETable*>& rvTables) const;
280
    void UpdateDocPos(const SwTwips nDocPos);
281
    virtual void UpdateFields();
282
};
283
284
/** Base class of all fields.
285
 Type of field is queried via Which.
286
 Expanded content of field is queried via ExpandField(). */
287
class SW_DLLPUBLIC SwField
288
{
289
private:
290
    mutable OUString    m_Cache;                ///< Cached expansion (for clipboard).
291
    SwFieldType*        m_pType;
292
    LanguageType        m_nLang;                ///< Always change via SetLanguage!
293
    bool                m_bUseFieldValueCache;  /// control the usage of the cached field value
294
    bool                m_bIsAutomaticLanguage;
295
    /// Used for tooltip purposes when it's not-empty.
296
    OUString m_aTitle;
297
298
    virtual OUString    ExpandImpl(SwRootFrame const* pLayout) const = 0;
299
    virtual std::unique_ptr<SwField> Copy() const = 0;
300
301
protected:
302
    SwField( SwFieldType* pTyp,
303
             LanguageType nLang = LANGUAGE_SYSTEM,
304
             bool m_bUseFieldValueCache = true );
305
306
public:
307
    virtual             ~SwField();
308
309
76
    SwField(SwField const &) = default;
310
    SwField(SwField &&) = default;
311
    SwField & operator =(SwField const &) = default;
312
    SwField & operator =(SwField &&) = default;
313
314
    inline SwFieldType* GetTyp() const;
315
316
    /// Set new type (used for copying among documents).
317
    virtual SwFieldType* ChgTyp( SwFieldType* );
318
319
    /** expand the field.
320
        @param  bCached     return cached field value.
321
        @remark     most callers should use the cached field value.
322
                    this is because various fields need special handing
323
                    (ChangeExpansion()) to return correct values, and only
324
                    SwTextFormatter::NewFieldPortion() sets things up properly.
325
        @param  pLayout     the layout to use for expansion; there are a few
326
                            fields that expand differently via layout mode.
327
        @return     the generated text (suitable for display)
328
      */
329
    OUString            ExpandField(bool bCached, SwRootFrame const* pLayout) const;
330
331
    /// @return name or content.
332
    virtual OUString    GetFieldName() const;
333
334
    std::unique_ptr<SwField> CopyField() const;
335
336
    /// ResId
337
    SwFieldIds          Which() const
338
9.65k
    {
339
9.65k
        assert(m_pType);
340
9.65k
        return m_pType->Which();
341
9.65k
    }
342
343
    // TYP_ID
344
    SwFieldTypesEnum    GetTypeId() const;
345
346
    // for code that is still passing around untyped values
347
    sal_uInt16 GetUntypedSubType() const;
348
    void SetUntypedSubType(sal_uInt16);
349
350
    /// Language at field position.
351
    inline LanguageType GetLanguage() const;
352
    virtual void        SetLanguage(LanguageType nLng);
353
354
    /// Query parameters for dialog and for BASIC.
355
    virtual OUString GetPar1() const;
356
    virtual OUString GetPar2() const;
357
358
    virtual OUString    GetFormula() const;
359
360
    virtual void        SetPar1(const OUString& rStr);
361
    virtual void        SetPar2(const OUString& rStr);
362
363
    virtual bool        QueryValue( css::uno::Any& rVal, sal_uInt16 nWhichId ) const;
364
    virtual bool        PutValue( const css::uno::Any& rVal, sal_uInt16 nWhichId );
365
366
    /// Does the field possess an action on its ClickHandler? (e.g. INetFields, ...).
367
    bool            HasClickHdl() const;
368
    bool            IsFixed() const;
369
370
74.7k
    bool IsAutomaticLanguage() const {
371
74.7k
        return m_bIsAutomaticLanguage;
372
74.7k
    }
373
76.0k
    void SetAutomaticLanguage(bool const bSet) {
374
76.0k
        m_bIsAutomaticLanguage = bSet;
375
76.0k
    }
376
377
    virtual OUString    GetDescription() const;
378
    /// Is this field clickable?
379
    bool IsClickable() const;
380
    virtual void dumpAsXml(xmlTextWriterPtr pWriter) const;
381
298
    const OUString & GetTitle() const { return m_aTitle; }
382
298
    void SetTitle(const OUString& rTitle) { m_aTitle = rTitle; }
383
384
    /// Helpers for those places still passing untyped format ids around for SwField
385
    sal_uInt32 GetUntypedFormat() const;
386
    void SetUntypedFormat(sal_uInt32);
387
};
388
389
inline SwFieldType* SwField::GetTyp() const
390
3.87M
{
391
3.87M
    return m_pType;
392
3.87M
}
393
394
inline LanguageType SwField::GetLanguage() const
395
120k
{
396
120k
    return m_nLang;
397
120k
}
398
399
/// Fields containing values that have to be formatted via number formatter.
400
class SwValueFieldType : public SwFieldType
401
{
402
private:
403
    SwDoc*  m_pDoc;
404
    bool    m_bUseFormat; ///< Use number formatter.
405
406
protected:
407
    SwValueFieldType( SwDoc* pDocPtr, SwFieldIds nWhichId );
408
    SwValueFieldType( const SwValueFieldType& rTyp );
409
410
public:
411
1.07M
    SwDoc*   GetDoc() const                      {
412
1.07M
        return m_pDoc;
413
1.07M
    }
414
13.3k
    void     SetDoc(SwDoc* pNewDoc)              {
415
13.3k
        m_pDoc = pNewDoc;
416
13.3k
    }
417
418
2.26k
    bool     UseFormat() const                   {
419
2.26k
        return m_bUseFormat;
420
2.26k
    }
421
426k
    void     EnableFormat(bool bFormat = true)   {
422
426k
        m_bUseFormat = bFormat;
423
426k
    }
424
425
    OUString        ExpandValue(const double& rVal, sal_uInt32 nFormat, LanguageType nLng) const;
426
    OUString        DoubleToString(const double &rVal, LanguageType eLng) const;
427
    OUString        DoubleToString(const double &rVal, sal_uInt32 nFormat) const;
428
    /// Query input or formatted value for dialog.
429
    OUString        GetInputOrDateTime( const OUString& rInput, const double& rVal, sal_uInt32 nFormat ) const;
430
};
431
432
class SW_DLLPUBLIC SwValueField : public SwField
433
{
434
private:
435
    double m_fValue;
436
    sal_uInt32 m_nFormat;
437
438
protected:
439
    SwValueField( SwValueFieldType* pFieldType, sal_uInt32 nFormat, LanguageType nLang = LANGUAGE_SYSTEM, const double fVal = 0.0 );
440
    SwValueField( const SwValueField& rField );
441
442
public:
443
    virtual                 ~SwValueField() override;
444
445
88.4k
    sal_uInt32 GetFormat() const { return m_nFormat; }
446
4.18k
    void SetFormat(sal_uInt32 nFormat) { m_nFormat = nFormat; }
447
    virtual SwFieldType*    ChgTyp( SwFieldType* ) override;
448
    virtual void            SetLanguage(LanguageType nLng) override;
449
450
48.4k
    SwDoc*           GetDoc() const          {
451
48.4k
        return static_cast<const SwValueFieldType*>(GetTyp())->GetDoc();
452
48.4k
    }
453
454
    virtual double          GetValue() const;
455
    virtual void            SetValue( const double& rVal );
456
457
6.20k
    OUString ExpandValue(const double& rVal, sal_uInt32 nFormat, LanguageType nLng) const {
458
6.20k
        return static_cast<SwValueFieldType*>(GetTyp())->ExpandValue(rVal, nFormat, nLng);
459
6.20k
    }
460
461
    static sal_uInt32       GetSystemFormat(SvNumberFormatter* pFormatter, sal_uInt32 nFormat);
462
    void dumpAsXml(xmlTextWriterPtr pWriter) const override;
463
};
464
465
class SW_DLLPUBLIC SwFormulaField : public SwValueField
466
{
467
private:
468
    OUString m_sFormula;
469
470
protected:
471
    SwFormulaField( SwValueFieldType* pFieldType, sal_uInt32 nFormat, const double fVal );
472
    SwFormulaField( const SwFormulaField& rField );
473
474
public:
475
    virtual OUString        GetFormula() const override;
476
    void                    SetFormula(const OUString& rStr);
477
478
    void                    SetExpandedFormula(const OUString& rStr);
479
    OUString                GetExpandedFormula() const;
480
481
    /// Query formula or formatted value for dialog.
482
    OUString                GetInputOrDateTime() const;
483
};
484
485
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */