Coverage Report

Created: 2025-07-07 10:01

/src/libreoffice/svx/source/dialog/relfld.cxx
Line
Count
Source (jump to first uncovered line)
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 <svx/relfld.hxx>
21
#include <vcl/fieldvalues.hxx>
22
23
SvxRelativeField::SvxRelativeField(std::unique_ptr<weld::MetricSpinButton> pControl)
24
0
    : m_xSpinButton(std::move(pControl))
25
0
    , nRelMin(0)
26
0
    , nRelMax(0)
27
0
    , bRelativeMode(false)
28
0
    , bRelative(false)
29
0
    , bNegativeEnabled(false)
30
0
    , bFontRelativeMode(false)
31
0
{
32
0
    weld::SpinButton& rSpinButton = m_xSpinButton->get_widget();
33
0
    rSpinButton.connect_changed(LINK(this, SvxRelativeField, ModifyHdl));
34
0
}
35
36
IMPL_LINK_NOARG(SvxRelativeField, ModifyHdl, weld::Entry&, void)
37
0
{
38
0
    if (bRelativeMode)
39
0
    {
40
0
        OUString aStr = m_xSpinButton->get_text();
41
0
        bool bNewMode = bRelative;
42
43
0
        if (bRelative)
44
0
        {
45
0
            const sal_Unicode* pStr = aStr.getStr();
46
47
0
            while (*pStr)
48
0
            {
49
0
                if (((*pStr < '0') || (*pStr > '9')) && (*pStr != '%'))
50
0
                {
51
0
                    bNewMode = false;
52
0
                    break;
53
0
                }
54
0
                pStr++;
55
0
            }
56
0
        }
57
0
        else
58
0
        {
59
0
            if (aStr.indexOf("%") != -1)
60
0
                bNewMode = true;
61
0
        }
62
63
0
        if (bNewMode != bRelative)
64
0
            SetRelative(bNewMode);
65
0
    }
66
67
0
    if (bFontRelativeMode)
68
0
    {
69
0
        OUString aStr = m_xSpinButton->get_text();
70
0
        FieldUnit eNewFieldUnit = vcl::GetTextMetricUnit(aStr);
71
72
        // Only allow font-relative units
73
0
        switch (eNewFieldUnit)
74
0
        {
75
0
            default:
76
0
                eNewFieldUnit = FieldUnit::NONE;
77
0
                break;
78
79
0
            case FieldUnit::FONT_EM:
80
0
            case FieldUnit::FONT_CJK_ADVANCE:
81
0
                break;
82
0
        }
83
84
0
        if (eNewFieldUnit != eFontRelativeFieldUnit)
85
0
        {
86
0
            SetFontRelative(eNewFieldUnit);
87
0
        }
88
0
    }
89
0
}
90
91
void SvxRelativeField::EnableRelativeMode(sal_uInt16 nMin, sal_uInt16 nMax)
92
0
{
93
0
    bRelativeMode = true;
94
0
    nRelMin       = nMin;
95
0
    nRelMax       = nMax;
96
0
    m_xSpinButton->set_unit(FieldUnit::CM);
97
0
}
98
99
0
void SvxRelativeField::EnableFontRelativeMode() { bFontRelativeMode = true; }
100
101
void SvxRelativeField::SetRelative( bool bNewRelative )
102
0
{
103
0
    weld::SpinButton& rSpinButton = m_xSpinButton->get_widget();
104
105
0
    int nStartPos, nEndPos;
106
0
    rSpinButton.get_selection_bounds(nStartPos, nEndPos);
107
0
    OUString aStr = rSpinButton.get_text();
108
109
0
    eFontRelativeFieldUnit = FieldUnit::NONE;
110
111
0
    if ( bNewRelative )
112
0
    {
113
0
        bRelative = true;
114
0
        m_xSpinButton->set_digits(0);
115
0
        m_xSpinButton->set_range(nRelMin, nRelMax, FieldUnit::NONE);
116
0
        m_xSpinButton->set_unit(FieldUnit::PERCENT);
117
0
    }
118
0
    else
119
0
    {
120
0
        bRelative = false;
121
0
        m_xSpinButton->set_digits(2);
122
0
        m_xSpinButton->set_range(bNegativeEnabled ? -9999 : 0, 9999, FieldUnit::NONE);
123
0
        m_xSpinButton->set_unit(FieldUnit::CM);
124
0
    }
125
126
0
    rSpinButton.set_text(aStr);
127
0
    rSpinButton.select_region(nStartPos, nEndPos);
128
0
}
129
130
void SvxRelativeField::SetFontRelative(FieldUnit eNewRelativeUnit)
131
0
{
132
0
    weld::SpinButton& rSpinButton = m_xSpinButton->get_widget();
133
134
0
    int nStartPos, nEndPos;
135
0
    rSpinButton.get_selection_bounds(nStartPos, nEndPos);
136
0
    OUString aStr = rSpinButton.get_text();
137
138
0
    bRelative = false;
139
0
    eFontRelativeFieldUnit = eNewRelativeUnit;
140
0
    m_xSpinButton->set_digits(2);
141
0
    m_xSpinButton->set_range(bNegativeEnabled ? -9999 : 0, 9999, FieldUnit::NONE);
142
143
0
    if (eNewRelativeUnit == FieldUnit::NONE)
144
0
    {
145
0
        m_xSpinButton->set_unit(eAbsoluteFieldUnit);
146
0
    }
147
0
    else
148
0
    {
149
0
        m_xSpinButton->set_unit(eNewRelativeUnit);
150
0
    }
151
152
0
    rSpinButton.set_text(aStr);
153
0
    rSpinButton.select_region(nStartPos, nEndPos);
154
0
}
155
156
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */