tdf#164393 Change themes UI as per UX feedback
[LibreOffice.git] / svx / source / dialog / txencbox.cxx
blob848059a5274d3aea4c129834bdf1c28494576adb
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <config_features.h>
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
28 #include <rtl/tencinfo.h>
29 #include <sal/log.hxx>
30 #include <txenctab.hrc>
32 namespace
34 std::vector<rtl_TextEncoding> FillFromDbTextEncodingMap(bool bExcludeImportSubsets, sal_uInt32 nExcludeInfoFlags)
36 std::vector<rtl_TextEncoding> aRet;
37 #if !HAVE_FEATURE_DBCONNECTIVITY
38 (void)bExcludeImportSubsets;
39 (void)nExcludeInfoFlags;
40 #else
41 rtl_TextEncodingInfo aInfo;
42 aInfo.StructSize = sizeof(rtl_TextEncodingInfo);
43 ::std::vector< rtl_TextEncoding > aEncs;
44 sal_Int32 nCount = svxform::charset_helper::getSupportedTextEncodings( aEncs );
45 for ( sal_Int32 j=0; j<nCount; j++ )
47 bool bInsert = true;
48 rtl_TextEncoding nEnc = rtl_TextEncoding( aEncs[j] );
49 if ( nExcludeInfoFlags )
51 if ( !rtl_getTextEncodingInfo( nEnc, &aInfo ) )
52 bInsert = false;
53 else
55 if ( (aInfo.Flags & nExcludeInfoFlags) == 0 )
57 if ( (nExcludeInfoFlags & RTL_TEXTENCODING_INFO_UNICODE) &&
58 ((nEnc == RTL_TEXTENCODING_UCS2) ||
59 nEnc == RTL_TEXTENCODING_UCS4) )
60 bInsert = false; // InfoFlags don't work for Unicode :-(
62 else
63 bInsert = false;
66 if ( bInsert )
68 if ( bExcludeImportSubsets )
70 switch ( nEnc )
72 // subsets of RTL_TEXTENCODING_GB_18030
73 case RTL_TEXTENCODING_GB_2312 :
74 case RTL_TEXTENCODING_GBK :
75 case RTL_TEXTENCODING_MS_936 :
76 bInsert = false;
77 break;
80 // CharsetMap offers a RTL_TEXTENCODING_DONTKNOW for internal use,
81 // makes no sense here and would result in an empty string as list
82 // entry.
83 if ( bInsert && nEnc != RTL_TEXTENCODING_DONTKNOW )
84 aRet.push_back(nEnc);
87 #endif
88 return aRet;
92 void SvxTextEncodingBox::FillFromDbTextEncodingMap(
93 bool bExcludeImportSubsets, sal_uInt32 nExcludeInfoFlags )
95 m_xControl->freeze();
96 auto aEncs = ::FillFromDbTextEncodingMap(bExcludeImportSubsets, nExcludeInfoFlags);
97 for (auto nEnc : aEncs)
98 InsertTextEncoding(nEnc);
99 m_xControl->thaw();
102 void SvxTextEncodingTreeView::FillFromDbTextEncodingMap(
103 bool bExcludeImportSubsets, sal_uInt32 nExcludeInfoFlags )
105 m_xControl->freeze();
106 auto aEncs = ::FillFromDbTextEncodingMap(bExcludeImportSubsets, nExcludeInfoFlags);
107 for (auto nEnc : aEncs)
108 InsertTextEncoding(nEnc);
109 m_xControl->thaw();
112 SvxTextEncodingBox::SvxTextEncodingBox(std::unique_ptr<weld::ComboBox> pControl)
113 : m_xControl(std::move(pControl))
115 m_xControl->make_sorted();
118 SvxTextEncodingTreeView::SvxTextEncodingTreeView(std::unique_ptr<weld::TreeView> pControl)
119 : m_xControl(std::move(pControl))
121 m_xControl->make_sorted();
124 SvxTextEncodingBox::~SvxTextEncodingBox()
128 SvxTextEncodingTreeView::~SvxTextEncodingTreeView()
132 namespace
134 std::vector<int> FillFromTextEncodingTable(bool bExcludeImportSubsets, sal_uInt32 nExcludeInfoFlags)
136 std::vector<int> aRet;
138 rtl_TextEncodingInfo aInfo;
139 aInfo.StructSize = sizeof(rtl_TextEncodingInfo);
140 const sal_uInt32 nCount = SAL_N_ELEMENTS(RID_SVXSTR_TEXTENCODING_TABLE);
141 for (sal_uInt32 j = 0; j < nCount; ++j)
143 bool bInsert = true;
144 rtl_TextEncoding nEnc = RID_SVXSTR_TEXTENCODING_TABLE[j].second;
145 if ( nExcludeInfoFlags )
147 if ( !rtl_getTextEncodingInfo( nEnc, &aInfo ) )
148 bInsert = false;
149 else
151 if ( (aInfo.Flags & nExcludeInfoFlags) == 0 )
153 if ( (nExcludeInfoFlags & RTL_TEXTENCODING_INFO_UNICODE) &&
154 ((nEnc == RTL_TEXTENCODING_UCS2) ||
155 nEnc == RTL_TEXTENCODING_UCS4) )
156 bInsert = false; // InfoFlags don't work for Unicode :-(
158 else
159 bInsert = false;
162 if ( bExcludeImportSubsets )
164 switch ( nEnc )
166 // subsets of RTL_TEXTENCODING_GB_18030
167 case RTL_TEXTENCODING_GB_2312 :
168 case RTL_TEXTENCODING_GBK :
169 case RTL_TEXTENCODING_MS_936 :
170 bInsert = false;
171 break;
174 if ( bInsert )
175 aRet.push_back(j);
177 return aRet;
181 void SvxTextEncodingBox::FillFromTextEncodingTable(
182 bool bExcludeImportSubsets, sal_uInt32 nExcludeInfoFlags )
184 std::vector<int> aRet(::FillFromTextEncodingTable(bExcludeImportSubsets, nExcludeInfoFlags));
185 m_xControl->freeze();
186 for (auto j : aRet)
188 rtl_TextEncoding nEnc = RID_SVXSTR_TEXTENCODING_TABLE[j].second;
189 InsertTextEncoding(nEnc, SvxResId(RID_SVXSTR_TEXTENCODING_TABLE[j].first));
191 m_xControl->thaw();
194 void SvxTextEncodingTreeView::FillFromTextEncodingTable(
195 bool bExcludeImportSubsets, sal_uInt32 nExcludeInfoFlags )
197 std::vector<int> aRet(::FillFromTextEncodingTable(bExcludeImportSubsets, nExcludeInfoFlags));
198 m_xControl->freeze();
199 for (auto j : aRet)
201 rtl_TextEncoding nEnc = RID_SVXSTR_TEXTENCODING_TABLE[j].second;
202 InsertTextEncoding(nEnc, SvxResId(RID_SVXSTR_TEXTENCODING_TABLE[j].first));
204 m_xControl->thaw();
207 void SvxTextEncodingBox::InsertTextEncoding( const rtl_TextEncoding nEnc,
208 const OUString& rEntry )
210 m_xControl->append(OUString::number(nEnc), rEntry);
213 void SvxTextEncodingTreeView::InsertTextEncoding( const rtl_TextEncoding nEnc,
214 const OUString& rEntry )
216 m_xControl->append(OUString::number(nEnc), rEntry);
219 void SvxTextEncodingBox::InsertTextEncoding( const rtl_TextEncoding nEnc )
221 const OUString aEntry = SvxTextEncodingTable::GetTextString(nEnc);
222 if (!aEntry.isEmpty())
223 InsertTextEncoding( nEnc, aEntry );
224 else
225 SAL_WARN( "svx.dialog", "SvxTextEncodingBox::InsertTextEncoding: no resource string for text encoding: " << static_cast<sal_Int32>( nEnc ) );
228 void SvxTextEncodingTreeView::InsertTextEncoding( const rtl_TextEncoding nEnc )
230 const OUString aEntry = SvxTextEncodingTable::GetTextString(nEnc);
231 if (!aEntry.isEmpty())
232 InsertTextEncoding( nEnc, aEntry );
233 else
234 SAL_WARN( "svx.dialog", "SvxTextEncodingTreeView::InsertTextEncoding: no resource string for text encoding: " << static_cast<sal_Int32>( nEnc ) );
237 rtl_TextEncoding SvxTextEncodingBox::GetSelectTextEncoding() const
239 OUString sId(m_xControl->get_active_id());
240 if (!sId.isEmpty())
241 return rtl_TextEncoding(sId.toInt32());
242 else
243 return RTL_TEXTENCODING_DONTKNOW;
246 rtl_TextEncoding SvxTextEncodingTreeView::GetSelectTextEncoding() const
248 OUString sId(m_xControl->get_selected_id());
249 if (!sId.isEmpty())
250 return rtl_TextEncoding(sId.toInt32());
251 else
252 return RTL_TEXTENCODING_DONTKNOW;
255 void SvxTextEncodingBox::SelectTextEncoding( const rtl_TextEncoding nEnc )
257 m_xControl->set_active_id(OUString::number(nEnc));
260 void SvxTextEncodingTreeView::SelectTextEncoding( const rtl_TextEncoding nEnc )
262 m_xControl->select_id(OUString::number(nEnc));
265 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */