LibreOffice Module svx (master) 1
txencbox.cxx
Go to the documentation of this file.
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 <config_features.h>
21
22#include <svx/txencbox.hxx>
23#include <svx/dialmgr.hxx>
24#include <svx/txenctab.hxx>
25#if HAVE_FEATURE_DBCONNECTIVITY
26#include <dbcharsethelper.hxx>
27#endif
29#include <rtl/tencinfo.h>
30#include <sal/log.hxx>
31#include <txenctab.hrc>
32
33namespace
34{
35 std::vector<rtl_TextEncoding> FillFromDbTextEncodingMap(bool bExcludeImportSubsets, sal_uInt32 nExcludeInfoFlags)
36 {
37 std::vector<rtl_TextEncoding> aRet;
38#if !HAVE_FEATURE_DBCONNECTIVITY
39 (void)bExcludeImportSubsets;
40 (void)nExcludeInfoFlags;
41#else
42 rtl_TextEncodingInfo aInfo;
43 aInfo.StructSize = sizeof(rtl_TextEncodingInfo);
44 ::std::vector< rtl_TextEncoding > aEncs;
46 for ( sal_Int32 j=0; j<nCount; j++ )
47 {
48 bool bInsert = true;
49 rtl_TextEncoding nEnc = rtl_TextEncoding( aEncs[j] );
50 if ( nExcludeInfoFlags )
51 {
52 if ( !rtl_getTextEncodingInfo( nEnc, &aInfo ) )
53 bInsert = false;
54 else
55 {
56 if ( (aInfo.Flags & nExcludeInfoFlags) == 0 )
57 {
58 if ( (nExcludeInfoFlags & RTL_TEXTENCODING_INFO_UNICODE) &&
59 ((nEnc == RTL_TEXTENCODING_UCS2) ||
60 nEnc == RTL_TEXTENCODING_UCS4) )
61 bInsert = false; // InfoFlags don't work for Unicode :-(
62 }
63 else
64 bInsert = false;
65 }
66 }
67 if ( bInsert )
68 {
69 if ( bExcludeImportSubsets )
70 {
71 switch ( nEnc )
72 {
73 // subsets of RTL_TEXTENCODING_GB_18030
74 case RTL_TEXTENCODING_GB_2312 :
75 case RTL_TEXTENCODING_GBK :
76 case RTL_TEXTENCODING_MS_936 :
77 bInsert = false;
78 break;
79 }
80 }
81 // CharsetMap offers a RTL_TEXTENCODING_DONTKNOW for internal use,
82 // makes no sense here and would result in an empty string as list
83 // entry.
84 if ( bInsert && nEnc != RTL_TEXTENCODING_DONTKNOW )
85 aRet.push_back(nEnc);
86 }
87 }
88#endif
89 return aRet;
90 }
91}
92
94 bool bExcludeImportSubsets, sal_uInt32 nExcludeInfoFlags )
95{
96 m_xControl->freeze();
97 auto aEncs = ::FillFromDbTextEncodingMap(bExcludeImportSubsets, nExcludeInfoFlags);
98 for (auto nEnc : aEncs)
100 m_xControl->thaw();
101}
102
104 bool bExcludeImportSubsets, sal_uInt32 nExcludeInfoFlags )
105{
106 m_xControl->freeze();
107 auto aEncs = ::FillFromDbTextEncodingMap(bExcludeImportSubsets, nExcludeInfoFlags);
108 for (auto nEnc : aEncs)
109 InsertTextEncoding(nEnc);
110 m_xControl->thaw();
111}
112
113SvxTextEncodingBox::SvxTextEncodingBox(std::unique_ptr<weld::ComboBox> pControl)
114 : m_xControl(std::move(pControl))
115{
116 m_xControl->make_sorted();
117}
118
119SvxTextEncodingTreeView::SvxTextEncodingTreeView(std::unique_ptr<weld::TreeView> pControl)
120 : m_xControl(std::move(pControl))
121{
122 m_xControl->make_sorted();
123}
124
126{
127}
128
130{
131}
132
133namespace
134{
135 std::vector<int> FillFromTextEncodingTable(bool bExcludeImportSubsets, sal_uInt32 nExcludeInfoFlags)
136 {
137 std::vector<int> aRet;
138
139 rtl_TextEncodingInfo aInfo;
140 aInfo.StructSize = sizeof(rtl_TextEncodingInfo);
141 const sal_uInt32 nCount = SAL_N_ELEMENTS(RID_SVXSTR_TEXTENCODING_TABLE);
142 for (sal_uInt32 j = 0; j < nCount; ++j)
143 {
144 bool bInsert = true;
145 rtl_TextEncoding nEnc = RID_SVXSTR_TEXTENCODING_TABLE[j].second;
146 if ( nExcludeInfoFlags )
147 {
148 if ( !rtl_getTextEncodingInfo( nEnc, &aInfo ) )
149 bInsert = false;
150 else
151 {
152 if ( (aInfo.Flags & nExcludeInfoFlags) == 0 )
153 {
154 if ( (nExcludeInfoFlags & RTL_TEXTENCODING_INFO_UNICODE) &&
155 ((nEnc == RTL_TEXTENCODING_UCS2) ||
156 nEnc == RTL_TEXTENCODING_UCS4) )
157 bInsert = false; // InfoFlags don't work for Unicode :-(
158 }
159 else
160 bInsert = false;
161 }
162 }
163 if ( bExcludeImportSubsets )
164 {
165 switch ( nEnc )
166 {
167 // subsets of RTL_TEXTENCODING_GB_18030
168 case RTL_TEXTENCODING_GB_2312 :
169 case RTL_TEXTENCODING_GBK :
170 case RTL_TEXTENCODING_MS_936 :
171 bInsert = false;
172 break;
173 }
174 }
175 if ( bInsert )
176 aRet.push_back(j);
177 }
178 return aRet;
179 }
180}
181
183 bool bExcludeImportSubsets, sal_uInt32 nExcludeInfoFlags )
184{
185 std::vector<int> aRet(::FillFromTextEncodingTable(bExcludeImportSubsets, nExcludeInfoFlags));
186 m_xControl->freeze();
187 for (auto j : aRet)
188 {
189 rtl_TextEncoding nEnc = RID_SVXSTR_TEXTENCODING_TABLE[j].second;
190 InsertTextEncoding(nEnc, SvxResId(RID_SVXSTR_TEXTENCODING_TABLE[j].first));
191 }
192 m_xControl->thaw();
193}
194
196 bool bExcludeImportSubsets, sal_uInt32 nExcludeInfoFlags )
197{
198 std::vector<int> aRet(::FillFromTextEncodingTable(bExcludeImportSubsets, nExcludeInfoFlags));
199 m_xControl->freeze();
200 for (auto j : aRet)
201 {
202 rtl_TextEncoding nEnc = RID_SVXSTR_TEXTENCODING_TABLE[j].second;
203 InsertTextEncoding(nEnc, SvxResId(RID_SVXSTR_TEXTENCODING_TABLE[j].first));
204 }
205 m_xControl->thaw();
206}
207
208void SvxTextEncodingBox::InsertTextEncoding( const rtl_TextEncoding nEnc,
209 const OUString& rEntry )
210{
211 m_xControl->append(OUString::number(nEnc), rEntry);
212}
213
214void SvxTextEncodingTreeView::InsertTextEncoding( const rtl_TextEncoding nEnc,
215 const OUString& rEntry )
216{
217 m_xControl->append(OUString::number(nEnc), rEntry);
218}
219
220void SvxTextEncodingBox::InsertTextEncoding( const rtl_TextEncoding nEnc )
221{
222 const OUString& rEntry = SvxTextEncodingTable::GetTextString(nEnc);
223 if (!rEntry.isEmpty())
224 InsertTextEncoding( nEnc, rEntry );
225 else
226 SAL_WARN( "svx.dialog", "SvxTextEncodingBox::InsertTextEncoding: no resource string for text encoding: " << static_cast<sal_Int32>( nEnc ) );
227}
228
229void SvxTextEncodingTreeView::InsertTextEncoding( const rtl_TextEncoding nEnc )
230{
231 const OUString& rEntry = SvxTextEncodingTable::GetTextString(nEnc);
232 if (!rEntry.isEmpty())
233 InsertTextEncoding( nEnc, rEntry );
234 else
235 SAL_WARN( "svx.dialog", "SvxTextEncodingTreeView::InsertTextEncoding: no resource string for text encoding: " << static_cast<sal_Int32>( nEnc ) );
236}
237
239{
240 OUString sId(m_xControl->get_active_id());
241 if (!sId.isEmpty())
242 return rtl_TextEncoding(sId.toInt32());
243 else
244 return RTL_TEXTENCODING_DONTKNOW;
245}
246
248{
249 OUString sId(m_xControl->get_selected_id());
250 if (!sId.isEmpty())
251 return rtl_TextEncoding(sId.toInt32());
252 else
253 return RTL_TEXTENCODING_DONTKNOW;
254}
255
256void SvxTextEncodingBox::SelectTextEncoding( const rtl_TextEncoding nEnc )
257{
258 m_xControl->set_active_id(OUString::number(nEnc));
259}
260
261void SvxTextEncodingTreeView::SelectTextEncoding( const rtl_TextEncoding nEnc )
262{
263 m_xControl->select_id(OUString::number(nEnc));
264}
265
266/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::unique_ptr< weld::ComboBox > m_xControl
Definition: txencbox.hxx:29
void FillFromTextEncodingTable(bool bExcludeImportSubsets, sal_uInt32 nExcludeInfoFlags=0)
Fill with all known encodings but exclude those matching one or more given flags as defined in rtl/te...
Definition: txencbox.cxx:182
SvxTextEncodingBox(std::unique_ptr< weld::ComboBox > pControl)
Definition: txencbox.cxx:113
void FillFromDbTextEncodingMap(bool bExcludeImportSubsets, sal_uInt32 nExcludeInfoFlags=0)
Fill with all encodings known to the dbtools::OCharsetMap but exclude those matching one or more give...
Definition: txencbox.cxx:93
rtl_TextEncoding GetSelectTextEncoding() const
Definition: txencbox.cxx:238
void InsertTextEncoding(const rtl_TextEncoding nEnc)
Definition: txencbox.cxx:220
void SelectTextEncoding(const rtl_TextEncoding nEnc)
Definition: txencbox.cxx:256
static OUString GetTextString(const rtl_TextEncoding nEnc)
Definition: txenctab.cxx:24
SvxTextEncodingTreeView(std::unique_ptr< weld::TreeView > pControl)
Definition: txencbox.cxx:119
std::unique_ptr< weld::TreeView > m_xControl
Definition: txencbox.hxx:91
void FillFromDbTextEncodingMap(bool bExcludeImportSubsets, sal_uInt32 nExcludeInfoFlags=0)
Fill with all encodings known to the dbtools::OCharsetMap but exclude those matching one or more give...
Definition: txencbox.cxx:103
void FillFromTextEncodingTable(bool bExcludeImportSubsets, sal_uInt32 nExcludeInfoFlags=0)
Fill with all known encodings but exclude those matching one or more given flags as defined in rtl/te...
Definition: txencbox.cxx:195
rtl_TextEncoding GetSelectTextEncoding() const
Definition: txencbox.cxx:247
void InsertTextEncoding(const rtl_TextEncoding nEnc)
Definition: txencbox.cxx:229
void SelectTextEncoding(const rtl_TextEncoding nEnc)
Definition: txencbox.cxx:261
int nCount
OUString SvxResId(TranslateId aId)
Definition: dialmgr.cxx:24
#define SAL_WARN(area, stream)
#define SAL_N_ELEMENTS(arr)
constexpr OUStringLiteral first
sal_Int32 getSupportedTextEncodings(::std::vector< rtl_TextEncoding > &_rEncs)
enumerates all supported charsets
OUString sId
Reference< XControl > m_xControl