summaryrefslogtreecommitdiff
path: root/vcl/source/app/weldutils.cxx
blob: 329d15827b70285b34bfe79108668dd7047c3b38 (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
/* -*- 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/.
 */

#include <svl/zforlist.hxx>
#include <svl/zformat.hxx>
#include <vcl/builderpage.hxx>
#include <vcl/commandinfoprovider.hxx>
#include <vcl/settings.hxx>
#include <vcl/svapp.hxx>
#include <vcl/weldutils.hxx>

BuilderPage::BuilderPage(weld::Widget* pParent, weld::DialogController* pController,
                         const OUString& rUIXMLDescription, const OString& rID, bool bIsMobile)
    : m_pDialogController(pController)
    , m_xBuilder(Application::CreateBuilder(pParent, rUIXMLDescription, bIsMobile))
    , m_xContainer(m_xBuilder->weld_container(rID))
{
}

void BuilderPage::Activate() {}

void BuilderPage::Deactivate() {}

BuilderPage::~BuilderPage() COVERITY_NOEXCEPT_FALSE {}

namespace weld
{
bool DialogController::runAsync(const std::shared_ptr<DialogController>& rController,
                                const std::function<void(sal_Int32)>& func)
{
    return rController->getDialog()->runAsync(rController, func);
}

DialogController::~DialogController() COVERITY_NOEXCEPT_FALSE {}

Dialog* GenericDialogController::getDialog() { return m_xDialog.get(); }

GenericDialogController::GenericDialogController(weld::Widget* pParent, const OUString& rUIFile,
                                                 const OString& rDialogId, bool bMobile)
    : m_xBuilder(Application::CreateBuilder(pParent, rUIFile, bMobile))
    , m_xDialog(m_xBuilder->weld_dialog(rDialogId))
{
}

GenericDialogController::~GenericDialogController() COVERITY_NOEXCEPT_FALSE {}

Dialog* MessageDialogController::getDialog() { return m_xDialog.get(); }

MessageDialogController::MessageDialogController(weld::Widget* pParent, const OUString& rUIFile,
                                                 const OString& rDialogId,
                                                 const OString& rRelocateId)
    : m_xBuilder(Application::CreateBuilder(pParent, rUIFile))
    , m_xDialog(m_xBuilder->weld_message_dialog(rDialogId))
    , m_xContentArea(m_xDialog->weld_message_area())
{
    if (!rRelocateId.isEmpty())
    {
        m_xRelocate = m_xBuilder->weld_container(rRelocateId);
        m_xOrigParent = m_xRelocate->weld_parent();
        //fdo#75121, a bit tricky because the widgets we want to align with
        //don't actually exist in the ui description, they're implied
        m_xOrigParent->move(m_xRelocate.get(), m_xContentArea.get());
    }
}

MessageDialogController::~MessageDialogController()
{
    if (m_xRelocate)
    {
        m_xContentArea->move(m_xRelocate.get(), m_xOrigParent.get());
    }
}

AssistantController::AssistantController(weld::Widget* pParent, const OUString& rUIFile,
                                         const OString& rDialogId)
    : m_xBuilder(Application::CreateBuilder(pParent, rUIFile))
    , m_xAssistant(m_xBuilder->weld_assistant(rDialogId))
{
}

Dialog* AssistantController::getDialog() { return m_xAssistant.get(); }

AssistantController::~AssistantController() {}

void TriStateEnabled::ButtonToggled(weld::ToggleButton& rToggle)
{
    if (bTriStateEnabled)
    {
        switch (eState)
        {
            case TRISTATE_INDET:
                rToggle.set_state(TRISTATE_FALSE);
                break;
            case TRISTATE_TRUE:
                rToggle.set_state(TRISTATE_INDET);
                break;
            case TRISTATE_FALSE:
                rToggle.set_state(TRISTATE_TRUE);
                break;
        }
    }
    eState = rToggle.get_state();
}

void RemoveParentKeepChildren(weld::TreeView& rTreeView, weld::TreeIter& rParent)
{
    if (rTreeView.iter_has_child(rParent))
    {
        std::unique_ptr<weld::TreeIter> xNewParent(rTreeView.make_iterator(&rParent));
        if (!rTreeView.iter_parent(*xNewParent))
            xNewParent.reset();

        while (true)
        {
            std::unique_ptr<weld::TreeIter> xChild(rTreeView.make_iterator(&rParent));
            if (!rTreeView.iter_children(*xChild))
                break;
            rTreeView.move_subtree(*xChild, xNewParent.get(), -1);
        }
    }
    rTreeView.remove(rParent);
}

EntryFormatter::EntryFormatter(weld::FormattedSpinButton& rSpinButton)
    : m_rEntry(rSpinButton)
    , m_pSpinButton(&rSpinButton)
    , m_eOptions(Application::GetSettings().GetStyleSettings().GetSelectionOptions())
{
    Init();
}

EntryFormatter::EntryFormatter(weld::Entry& rEntry)
    : m_rEntry(rEntry)
    , m_pSpinButton(nullptr)
    , m_eOptions(Application::GetSettings().GetStyleSettings().GetSelectionOptions())
{
    Init();
}

EntryFormatter::~EntryFormatter()
{
    m_rEntry.connect_changed(Link<weld::Entry&, void>());
    m_rEntry.connect_focus_out(Link<weld::Widget&, void>());
}

void EntryFormatter::Init()
{
    m_rEntry.connect_changed(LINK(this, EntryFormatter, ModifyHdl));
    m_rEntry.connect_focus_out(LINK(this, EntryFormatter, FocusOutHdl));
}

Selection EntryFormatter::GetEntrySelection() const
{
    int nStartPos, nEndPos;
    m_rEntry.get_selection_bounds(nStartPos, nEndPos);
    return Selection(nStartPos, nEndPos);
}

OUString EntryFormatter::GetEntryText() const { return m_rEntry.get_text(); }

void EntryFormatter::SetEntryText(const OUString& rText, const Selection& rSel)
{
    m_rEntry.set_text(rText);
    auto nMin = rSel.Min();
    auto nMax = rSel.Max();
    m_rEntry.select_region(nMin < 0 ? 0 : nMin, nMax == SELECTION_MAX ? -1 : nMax);
}

void EntryFormatter::SetEntryTextColor(const Color* pColor)
{
    m_rEntry.set_font_color(pColor ? *pColor : COL_AUTO);
}

void EntryFormatter::UpdateCurrentValue(double dCurrentValue)
{
    Formatter::UpdateCurrentValue(dCurrentValue);
    if (m_pSpinButton)
        m_pSpinButton->sync_value_from_formatter();
}

void EntryFormatter::ClearMinValue()
{
    Formatter::ClearMinValue();
    if (m_pSpinButton)
        m_pSpinButton->sync_range_from_formatter();
}

void EntryFormatter::SetMinValue(double dMin)
{
    Formatter::SetMinValue(dMin);
    if (m_pSpinButton)
        m_pSpinButton->sync_range_from_formatter();
}

void EntryFormatter::ClearMaxValue()
{
    Formatter::ClearMaxValue();
    if (m_pSpinButton)
        m_pSpinButton->sync_range_from_formatter();
}

void EntryFormatter::SetMaxValue(double dMin)
{
    Formatter::SetMaxValue(dMin);
    if (m_pSpinButton)
        m_pSpinButton->sync_range_from_formatter();
}

void EntryFormatter::SetSpinSize(double dStep)
{
    Formatter::SetSpinSize(dStep);
    if (m_pSpinButton)
        m_pSpinButton->sync_increments_from_formatter();
}

SelectionOptions EntryFormatter::GetEntrySelectionOptions() const { return m_eOptions; }

void EntryFormatter::FieldModified() { m_aModifyHdl.Call(m_rEntry); }

IMPL_LINK_NOARG(EntryFormatter, ModifyHdl, weld::Entry&, void)
{
    // This leads to FieldModified getting called at the end of Modify() and
    // FieldModified then calls any modification callback
    Modify();
}

IMPL_LINK_NOARG(EntryFormatter, FocusOutHdl, weld::Widget&, void)
{
    EntryLostFocus();
    m_aFocusOutHdl.Call(m_rEntry);
}

DoubleNumericEntry::DoubleNumericEntry(weld::Entry& rEntry)
    : EntryFormatter(rEntry)
{
    ResetConformanceTester();
}

DoubleNumericEntry::DoubleNumericEntry(weld::FormattedSpinButton& rSpinButton)
    : EntryFormatter(rSpinButton)
{
    ResetConformanceTester();
}

DoubleNumericEntry::~DoubleNumericEntry() = default;

void DoubleNumericEntry::FormatChanged(FORMAT_CHANGE_TYPE nWhat)
{
    ResetConformanceTester();
    EntryFormatter::FormatChanged(nWhat);
}

bool DoubleNumericEntry::CheckText(const OUString& sText) const
{
    // We'd like to implement this using the NumberFormatter::IsNumberFormat, but unfortunately, this doesn't
    // recognize fragments of numbers (like, for instance "1e", which happens during entering e.g. "1e10")
    // Thus, the roundabout way via a regular expression
    return m_pNumberValidator->isValidNumericFragment(sText);
}

void DoubleNumericEntry::ResetConformanceTester()
{
    // the thousands and the decimal separator are language dependent
    const SvNumberformat* pFormatEntry = GetOrCreateFormatter()->GetEntry(m_nFormatKey);

    sal_Unicode cSeparatorThousand = ',';
    sal_Unicode cSeparatorDecimal = '.';
    if (pFormatEntry)
    {
        LocaleDataWrapper aLocaleInfo(LanguageTag(pFormatEntry->GetLanguage()));

        OUString sSeparator = aLocaleInfo.getNumThousandSep();
        if (!sSeparator.isEmpty())
            cSeparatorThousand = sSeparator[0];

        sSeparator = aLocaleInfo.getNumDecimalSep();
        if (!sSeparator.isEmpty())
            cSeparatorDecimal = sSeparator[0];
    }

    m_pNumberValidator.reset(
        new validation::NumberValidator(cSeparatorThousand, cSeparatorDecimal));
}
}

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