summaryrefslogtreecommitdiff
path: root/cui/source/options/optasian.cxx
blob: 66f7f9818851752c853a09439889a4a663534f5b (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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/* -*- 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 .
 */

#include <memory>
#include <map>
#include <optasian.hxx>
#include <tools/debug.hxx>
#include <comphelper/diagnose_ex.hxx>
#include <o3tl/any.hxx>
#include <i18nlangtag/mslangid.hxx>
#include <svl/asiancfg.hxx>
#include <com/sun/star/frame/XModel.hpp>
#include <com/sun/star/lang/Locale.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/i18n/XForbiddenCharacters.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <sfx2/viewfrm.hxx>
#include <sfx2/objsh.hxx>
#include <vcl/svapp.hxx>
#include <vcl/settings.hxx>
#include <unotools/localedatawrapper.hxx>

using namespace com::sun::star::uno;
using namespace com::sun::star::lang;
using namespace com::sun::star::i18n;
using namespace com::sun::star::frame;
using namespace com::sun::star::beans;

constexpr OUString cIsKernAsianPunctuation = u"IsKernAsianPunctuation"_ustr;
constexpr OUString cCharacterCompressionType = u"CharacterCompressionType"_ustr;

namespace {

struct SvxForbiddenChars_Impl
{
    bool                                  bRemoved;
    std::optional<ForbiddenCharacters>    oCharacters;
};

}

struct SvxAsianLayoutPage_Impl
{
    SvxAsianConfig  aConfig;
    SvxAsianLayoutPage_Impl() {}

    Reference< XForbiddenCharacters >   xForbidden;
    Reference< XPropertySet >           xPrSet;
    Reference< XPropertySetInfo >       xPrSetInfo;
    std::map< LanguageType, SvxForbiddenChars_Impl >
                                        aChangedLanguagesMap;

    bool                hasForbiddenCharacters(LanguageType eLang);
    SvxForbiddenChars_Impl* getForbiddenCharacters(LanguageType eLang);
    void                    addForbiddenCharacters(LanguageType eLang, std::optional<ForbiddenCharacters> oForbidden);
};

bool    SvxAsianLayoutPage_Impl::hasForbiddenCharacters(LanguageType eLang)
{
    return aChangedLanguagesMap.count( eLang );
}

SvxForbiddenChars_Impl* SvxAsianLayoutPage_Impl::getForbiddenCharacters(LanguageType eLang)
{
    auto it = aChangedLanguagesMap.find( eLang );
    DBG_ASSERT( ( it != aChangedLanguagesMap.end() ), "language not available");
    if( it != aChangedLanguagesMap.end() )
        return &it->second;
    return nullptr;
}

void SvxAsianLayoutPage_Impl::addForbiddenCharacters(
    LanguageType eLang, std::optional<ForbiddenCharacters> oForbidden)
{
    auto itOld = aChangedLanguagesMap.find( eLang );
    if( itOld == aChangedLanguagesMap.end() )
    {
        SvxForbiddenChars_Impl aChar;
        aChar.bRemoved = !oForbidden.has_value();
        aChar.oCharacters = std::move(oForbidden);
        aChangedLanguagesMap.emplace( eLang, std::move(aChar) );
    }
    else
    {
        itOld->second.bRemoved = !oForbidden.has_value();
        itOld->second.oCharacters = std::move(oForbidden);
    }
}

static LanguageType eLastUsedLanguageTypeForForbiddenCharacters(USHRT_MAX);

SvxAsianLayoutPage::SvxAsianLayoutPage(weld::Container* pPage, weld::DialogController* pController, const SfxItemSet& rSet)
    : SfxTabPage(pPage, pController, u"cui/ui/optasianpage.ui"_ustr, u"OptAsianPage"_ustr, &rSet)
    , pImpl(new SvxAsianLayoutPage_Impl)
    , m_xCharKerningRB(m_xBuilder->weld_radio_button(u"charkerning"_ustr))
    , m_xCharPunctKerningRB(m_xBuilder->weld_radio_button(u"charpunctkerning"_ustr))
    , m_xNoCompressionRB(m_xBuilder->weld_radio_button(u"nocompression"_ustr))
    , m_xPunctCompressionRB(m_xBuilder->weld_radio_button(u"punctcompression"_ustr))
    , m_xPunctKanaCompressionRB(m_xBuilder->weld_radio_button(u"punctkanacompression"_ustr))
    , m_xLanguageFT(m_xBuilder->weld_label(u"languageft"_ustr))
    , m_xLanguageLB(new SvxLanguageBox(m_xBuilder->weld_combo_box(u"language"_ustr)))
    , m_xStandardCB(m_xBuilder->weld_check_button(u"standard"_ustr))
    , m_xStartFT(m_xBuilder->weld_label(u"startft"_ustr))
    , m_xStartED(m_xBuilder->weld_entry(u"start"_ustr))
    , m_xEndFT(m_xBuilder->weld_label(u"endft"_ustr))
    , m_xEndED(m_xBuilder->weld_entry(u"end"_ustr))
    , m_xHintFT(m_xBuilder->weld_label(u"hintft"_ustr))
{
    LanguageHdl(*m_xLanguageLB->get_widget());
    m_xLanguageLB->connect_changed(LINK(this, SvxAsianLayoutPage, LanguageHdl));
    m_xStandardCB->connect_toggled(LINK(this, SvxAsianLayoutPage, ChangeStandardHdl));
    Link<weld::Entry&,void> aLk(LINK(this, SvxAsianLayoutPage, ModifyHdl));
    m_xStartED->connect_changed(aLk);
    m_xEndED->connect_changed(aLk);

    m_xLanguageLB->SetLanguageList( SvxLanguageListFlags::FBD_CHARS, false, false );
}

SvxAsianLayoutPage::~SvxAsianLayoutPage()
{
}

std::unique_ptr<SfxTabPage> SvxAsianLayoutPage::Create(weld::Container* pPage, weld::DialogController* pController, const SfxItemSet* rAttrSet)
{
    return std::make_unique<SvxAsianLayoutPage>(pPage, pController, *rAttrSet);
}

OUString SvxAsianLayoutPage::GetAllStrings()
{
    OUString sAllStrings;
    OUString labels[]
        = { u"label1"_ustr, u"label2"_ustr, u"label3"_ustr, u"languageft"_ustr, u"startft"_ustr, u"endft"_ustr, u"hintft"_ustr };

    for (const auto& label : labels)
    {
        if (const auto& pString = m_xBuilder->weld_label(label))
            sAllStrings += pString->get_label() + " ";
    }

    OUString radioButton[] = { u"charkerning"_ustr, u"charpunctkerning"_ustr, u"nocompression"_ustr,
                               u"punctcompression"_ustr, u"punctkanacompression"_ustr };

    for (const auto& radio : radioButton)
    {
        if (const auto& pString = m_xBuilder->weld_radio_button(radio))
            sAllStrings += pString->get_label() + " ";
    }

    sAllStrings += m_xStandardCB->get_label() + " ";

    return sAllStrings.replaceAll("_", "");
}

bool SvxAsianLayoutPage::FillItemSet( SfxItemSet* )
{
    if(m_xCharKerningRB->get_state_changed_from_saved())
    {
        pImpl->aConfig.SetKerningWesternTextOnly(m_xCharKerningRB->get_active());
        OUString sPunct(cIsKernAsianPunctuation);
        if(pImpl->xPrSetInfo.is() && pImpl->xPrSetInfo->hasPropertyByName(sPunct))
        {
            bool bVal = !m_xCharKerningRB->get_active();
            pImpl->xPrSet->setPropertyValue(sPunct, Any(bVal));
        }
    }

    if(m_xNoCompressionRB->get_state_changed_from_saved() ||
       m_xPunctCompressionRB->get_state_changed_from_saved())
    {
        CharCompressType nSet = m_xNoCompressionRB->get_active() ? CharCompressType::NONE :
                            m_xPunctCompressionRB->get_active() ? CharCompressType::PunctuationOnly :
                            CharCompressType::PunctuationAndKana;
        pImpl->aConfig.SetCharDistanceCompression(nSet);
        OUString sCompress(cCharacterCompressionType);
        if(pImpl->xPrSetInfo.is() && pImpl->xPrSetInfo->hasPropertyByName(sCompress))
        {
            pImpl->xPrSet->setPropertyValue(sCompress, Any(static_cast<sal_uInt16>(nSet)));
        }
    }
    pImpl->aConfig.Commit();
    if(pImpl->xForbidden.is())
    {
        try
        {
            for (auto const& changedLanguage : pImpl->aChangedLanguagesMap)
            {
                Locale aLocale( LanguageTag::convertToLocale(changedLanguage.first));
                if(changedLanguage.second.bRemoved)
                    pImpl->xForbidden->removeForbiddenCharacters( aLocale );
                else if(changedLanguage.second.oCharacters)
                    pImpl->xForbidden->setForbiddenCharacters( aLocale, *( changedLanguage.second.oCharacters ) );
            }
        }
        catch (const Exception&)
        {
            TOOLS_WARN_EXCEPTION( "cui.options", "in XForbiddenCharacters");
        }
    }
    eLastUsedLanguageTypeForForbiddenCharacters = m_xLanguageLB->get_active_id();

    return false;
}

void SvxAsianLayoutPage::Reset( const SfxItemSet* )
{
    SfxViewFrame* pCurFrm = SfxViewFrame::Current();
    SfxObjectShell* pDocSh = pCurFrm ? pCurFrm->GetObjectShell() : nullptr;
    Reference< XModel > xModel;
    if(pDocSh)
        xModel = pDocSh->GetModel();
    Reference<XMultiServiceFactory> xFact(xModel, UNO_QUERY);
    if(xFact.is())
    {
        pImpl->xPrSet.set(xFact->createInstance(u"com.sun.star.document.Settings"_ustr), UNO_QUERY);
    }
    if( pImpl->xPrSet.is() )
        pImpl->xPrSetInfo = pImpl->xPrSet->getPropertySetInfo();
    bool bKernWesternText = SvxAsianConfig::IsKerningWesternTextOnly();
    CharCompressType nCompress = SvxAsianConfig::GetCharDistanceCompression();
    if(pImpl->xPrSetInfo.is())
    {
        OUString sForbidden(u"ForbiddenCharacters"_ustr);
        if(pImpl->xPrSetInfo->hasPropertyByName(sForbidden))
        {
            Any aForbidden = pImpl->xPrSet->getPropertyValue(sForbidden);
            aForbidden >>= pImpl->xForbidden;
        }
        OUString sCompress(cCharacterCompressionType);
        if(pImpl->xPrSetInfo->hasPropertyByName(sCompress))
        {
            Any aVal = pImpl->xPrSet->getPropertyValue(sCompress);
            sal_uInt16 nTmp;
            if (aVal >>= nTmp)
                nCompress = static_cast<CharCompressType>(nTmp);
        }
        OUString sPunct(cIsKernAsianPunctuation);
        if(pImpl->xPrSetInfo->hasPropertyByName(sPunct))
        {
            Any aVal = pImpl->xPrSet->getPropertyValue(sPunct);
            bKernWesternText = !*o3tl::doAccess<bool>(aVal);
        }
    }
    else
    {
        m_xLanguageFT->set_sensitive(false);
        m_xLanguageLB->set_sensitive(false);
        m_xStandardCB->set_sensitive(false);
        m_xStartFT->set_sensitive(false);
        m_xStartED->set_sensitive(false);
        m_xEndFT->set_sensitive(false);
        m_xEndED->set_sensitive(false);
        m_xHintFT->set_sensitive(false);
    }
    if(bKernWesternText)
        m_xCharKerningRB->set_active(true);
    else
        m_xCharPunctKerningRB->set_active(true);
    switch(nCompress)
    {
        case CharCompressType::NONE : m_xNoCompressionRB->set_active(true);        break;
        case CharCompressType::PunctuationOnly : m_xPunctCompressionRB->set_active(true);     break;
        default: m_xPunctKanaCompressionRB->set_active(true);
    }
    m_xCharKerningRB->save_state();
    m_xNoCompressionRB->save_state();
    m_xPunctCompressionRB->save_state();
    m_xPunctKanaCompressionRB->save_state();

    m_xLanguageLB->set_active(0);
    //preselect the system language in the box - if available
    if(LanguageType(USHRT_MAX) == eLastUsedLanguageTypeForForbiddenCharacters)
    {
        eLastUsedLanguageTypeForForbiddenCharacters =
            Application::GetSettings().GetLanguageTag().getLanguageType();
        if (MsLangId::isSimplifiedChinese(eLastUsedLanguageTypeForForbiddenCharacters))
            eLastUsedLanguageTypeForForbiddenCharacters = LANGUAGE_CHINESE_SIMPLIFIED;
        else if (MsLangId::isTraditionalChinese(eLastUsedLanguageTypeForForbiddenCharacters))
            eLastUsedLanguageTypeForForbiddenCharacters = LANGUAGE_CHINESE_TRADITIONAL;
    }
    m_xLanguageLB->set_active_id(eLastUsedLanguageTypeForForbiddenCharacters);
    LanguageHdl(*m_xLanguageLB->get_widget());
}

IMPL_LINK_NOARG(SvxAsianLayoutPage, LanguageHdl, weld::ComboBox&, void)
{
    //set current value
    LanguageType eSelectLanguage = m_xLanguageLB->get_active_id();
    LanguageTag aLanguageTag( eSelectLanguage);
    const Locale& aLocale( aLanguageTag.getLocale());

    OUString sStart, sEnd;
    bool bAvail;
    if(pImpl->xForbidden.is())
    {
        bAvail = pImpl->hasForbiddenCharacters(eSelectLanguage);
        if(bAvail)
        {
            SvxForbiddenChars_Impl* pElement = pImpl->getForbiddenCharacters(eSelectLanguage);
            if(pElement->bRemoved || !pElement->oCharacters)
            {
                bAvail = false;
            }
            else
            {
                sStart = pElement->oCharacters->beginLine;
                sEnd = pElement->oCharacters->endLine;
            }
        }
        else
        {
            try
            {
                bAvail = pImpl->xForbidden->hasForbiddenCharacters(aLocale);
                if(bAvail)
                {
                    ForbiddenCharacters aForbidden = pImpl->xForbidden->getForbiddenCharacters( aLocale );
                    sStart = aForbidden.beginLine;
                    sEnd = aForbidden.endLine;
                }
            }
            catch (const Exception&)
            {
                TOOLS_WARN_EXCEPTION( "cui.options", "in XForbiddenCharacters");
            }
        }
    }
    else
    {
        bAvail = SvxAsianConfig::GetStartEndChars( aLocale, sStart, sEnd );
    }
    if(!bAvail)
    {
        LocaleDataWrapper aWrap( std::move(aLanguageTag) );
        ForbiddenCharacters aForbidden = aWrap.getForbiddenCharacters();
        sStart = aForbidden.beginLine;
        sEnd = aForbidden.endLine;
    }
    m_xStandardCB->set_active(!bAvail);
    m_xStartED->set_sensitive(bAvail);
    m_xEndED->set_sensitive(bAvail);
    m_xStartFT->set_sensitive(bAvail);
    m_xEndFT->set_sensitive(bAvail);
    m_xStartED->set_text(sStart);
    m_xEndED->set_text(sEnd);
}

IMPL_LINK(SvxAsianLayoutPage, ChangeStandardHdl, weld::Toggleable&, rBox, void)
{
    bool bCheck = rBox.get_active();
    m_xStartED->set_sensitive(!bCheck);
    m_xEndED->set_sensitive(!bCheck);
    m_xStartFT->set_sensitive(!bCheck);
    m_xEndFT->set_sensitive(!bCheck);

    ModifyHdl(*m_xStartED);
}

IMPL_LINK(SvxAsianLayoutPage, ModifyHdl, weld::Entry&, rEdit, void)
{
    LanguageType eSelectLanguage = m_xLanguageLB->get_active_id();
    Locale aLocale( LanguageTag::convertToLocale( eSelectLanguage ));
    OUString sStart = m_xStartED->get_text();
    OUString sEnd = m_xEndED->get_text();
    bool bEnable = rEdit.get_sensitive();
    if(pImpl->xForbidden.is())
    {
        try
        {
            if(bEnable)
            {
                ForbiddenCharacters aFCSet;
                aFCSet.beginLine = sStart;
                aFCSet.endLine = sEnd;
                pImpl->addForbiddenCharacters(eSelectLanguage, std::move(aFCSet));
            }
            else
                pImpl->addForbiddenCharacters(eSelectLanguage, std::nullopt);
        }
        catch (const Exception&)
        {
            TOOLS_WARN_EXCEPTION( "cui.options", "in XForbiddenCharacters");
        }
    }
    pImpl->aConfig.SetStartEndChars( aLocale, bEnable ? &sStart : nullptr, bEnable ? &sEnd : nullptr);
}

WhichRangesContainer SvxAsianLayoutPage::GetRanges()
{
    //no items are used
    return WhichRangesContainer();
}

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