Line data Source code
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 :
21 : #include "svx/txencbox.hxx"
22 : #include "svx/txenctab.hxx"
23 : #include <svx/dialogs.hrc>
24 : #ifndef DISABLE_DBCONNECTIVITY
25 : #include "svx/dbcharsethelper.hxx"
26 : #endif
27 : #include <vcl/builder.hxx>
28 : #include <vcl/svapp.hxx>
29 : #include <rtl/tencinfo.h>
30 : #include <rtl/locale.h>
31 : #include <rtl/strbuf.hxx>
32 : #include <osl/nlsupport.h>
33 :
34 : //========================================================================
35 : // class SvxTextEncodingBox
36 : //========================================================================
37 :
38 0 : SvxTextEncodingBox::SvxTextEncodingBox( Window* pParent, const ResId& rResId )
39 : :
40 0 : ListBox( pParent, rResId )
41 : {
42 0 : m_pEncTable = new SvxTextEncodingTable;
43 0 : }
44 :
45 0 : SvxTextEncodingBox::SvxTextEncodingBox( Window* pParent, WinBits nBits )
46 0 : : ListBox( pParent, nBits )
47 : {
48 0 : m_pEncTable = new SvxTextEncodingTable;
49 0 : }
50 :
51 0 : extern "C" SAL_DLLPUBLIC_EXPORT Window* SAL_CALL makeSvxTextEncodingBox(Window *pParent, VclBuilder::stringmap &)
52 : {
53 0 : WinBits nWinBits = WB_LEFT|WB_DROPDOWN|WB_VCENTER|WB_3DLOOK|WB_SIMPLEMODE;
54 0 : SvxTextEncodingBox *pListBox = new SvxTextEncodingBox(pParent, nWinBits);
55 0 : pListBox->EnableAutoSize(true);
56 0 : return pListBox;
57 : }
58 :
59 : //------------------------------------------------------------------------
60 :
61 0 : SvxTextEncodingBox::~SvxTextEncodingBox()
62 : {
63 0 : delete m_pEncTable;
64 0 : }
65 :
66 : //------------------------------------------------------------------------
67 :
68 0 : sal_uInt16 SvxTextEncodingBox::EncodingToPos_Impl( rtl_TextEncoding nEnc ) const
69 : {
70 0 : sal_uInt16 nCount = GetEntryCount();
71 0 : for ( sal_uInt16 i=0; i<nCount; i++ )
72 : {
73 0 : if ( nEnc == rtl_TextEncoding( (sal_uIntPtr)GetEntryData(i) ) )
74 0 : return i;
75 : }
76 0 : return LISTBOX_ENTRY_NOTFOUND;
77 : }
78 :
79 : //------------------------------------------------------------------------
80 :
81 0 : void SvxTextEncodingBox::FillFromTextEncodingTable(
82 : sal_Bool bExcludeImportSubsets, sal_uInt32 nExcludeInfoFlags,
83 : sal_uInt32 nButIncludeInfoFlags )
84 : {
85 : rtl_TextEncodingInfo aInfo;
86 0 : aInfo.StructSize = sizeof(rtl_TextEncodingInfo);
87 0 : sal_uInt32 nCount = m_pEncTable->Count();
88 0 : for ( sal_uInt32 j=0; j<nCount; j++ )
89 : {
90 0 : sal_Bool bInsert = sal_True;
91 0 : rtl_TextEncoding nEnc = rtl_TextEncoding( m_pEncTable->GetValue( j ) );
92 0 : if ( nExcludeInfoFlags )
93 : {
94 0 : if ( !rtl_getTextEncodingInfo( nEnc, &aInfo ) )
95 0 : bInsert = sal_False;
96 : else
97 : {
98 0 : if ( (aInfo.Flags & nExcludeInfoFlags) == 0 )
99 : {
100 0 : if ( (nExcludeInfoFlags & RTL_TEXTENCODING_INFO_UNICODE) &&
101 0 : ((nEnc == RTL_TEXTENCODING_UCS2) ||
102 : nEnc == RTL_TEXTENCODING_UCS4) )
103 0 : bInsert = sal_False; // InfoFlags don't work for Unicode :-(
104 : }
105 0 : else if ( (aInfo.Flags & nButIncludeInfoFlags) == 0 )
106 0 : bInsert = sal_False;
107 : }
108 : }
109 0 : if ( bInsert )
110 : {
111 0 : if ( bExcludeImportSubsets )
112 : {
113 0 : switch ( nEnc )
114 : {
115 : // subsets of RTL_TEXTENCODING_GB_18030
116 : case RTL_TEXTENCODING_GB_2312 :
117 : case RTL_TEXTENCODING_GBK :
118 : case RTL_TEXTENCODING_MS_936 :
119 0 : bInsert = sal_False;
120 0 : break;
121 : }
122 : }
123 0 : if ( bInsert )
124 0 : InsertTextEncoding( nEnc, m_pEncTable->GetString( j ) );
125 : }
126 : }
127 0 : }
128 :
129 : //------------------------------------------------------------------------
130 :
131 0 : void SvxTextEncodingBox::FillFromDbTextEncodingMap(
132 : sal_Bool bExcludeImportSubsets, sal_uInt32 nExcludeInfoFlags,
133 : sal_uInt32 nButIncludeInfoFlags )
134 : {
135 : #ifdef DISABLE_DBCONNECTIVITY
136 : (void)bExcludeImportSubsets;
137 : (void)nExcludeInfoFlags;
138 : (void)nButIncludeInfoFlags;
139 : #else
140 : rtl_TextEncodingInfo aInfo;
141 0 : aInfo.StructSize = sizeof(rtl_TextEncodingInfo);
142 0 : svxform::ODataAccessCharsetHelper aCSH;
143 0 : ::std::vector< rtl_TextEncoding > aEncs;
144 0 : sal_Int32 nCount = aCSH.getSupportedTextEncodings( aEncs );
145 0 : for ( sal_uInt16 j=0; j<nCount; j++ )
146 : {
147 0 : sal_Bool bInsert = sal_True;
148 0 : rtl_TextEncoding nEnc = rtl_TextEncoding( aEncs[j] );
149 0 : if ( nExcludeInfoFlags )
150 : {
151 0 : if ( !rtl_getTextEncodingInfo( nEnc, &aInfo ) )
152 0 : bInsert = sal_False;
153 : else
154 : {
155 0 : if ( (aInfo.Flags & nExcludeInfoFlags) == 0 )
156 : {
157 0 : if ( (nExcludeInfoFlags & RTL_TEXTENCODING_INFO_UNICODE) &&
158 0 : ((nEnc == RTL_TEXTENCODING_UCS2) ||
159 : nEnc == RTL_TEXTENCODING_UCS4) )
160 0 : bInsert = sal_False; // InfoFlags don't work for Unicode :-(
161 : }
162 0 : else if ( (aInfo.Flags & nButIncludeInfoFlags) == 0 )
163 0 : bInsert = sal_False;
164 : }
165 : }
166 0 : if ( bInsert )
167 : {
168 0 : if ( bExcludeImportSubsets )
169 : {
170 0 : switch ( nEnc )
171 : {
172 : // subsets of RTL_TEXTENCODING_GB_18030
173 : case RTL_TEXTENCODING_GB_2312 :
174 : case RTL_TEXTENCODING_GBK :
175 : case RTL_TEXTENCODING_MS_936 :
176 0 : bInsert = sal_False;
177 0 : break;
178 : }
179 : }
180 : // CharsetMap offers a RTL_TEXTENCODING_DONTKNOW for internal use,
181 : // makes no sense here and would result in an empty string as list
182 : // entry.
183 0 : if ( bInsert && nEnc != RTL_TEXTENCODING_DONTKNOW )
184 0 : InsertTextEncoding( nEnc );
185 : }
186 0 : }
187 : #endif
188 0 : }
189 :
190 : //------------------------------------------------------------------------
191 :
192 0 : void SvxTextEncodingBox::FillWithMimeAndSelectBest()
193 : {
194 0 : FillFromTextEncodingTable( sal_False, 0xffffffff, RTL_TEXTENCODING_INFO_MIME );
195 0 : rtl_TextEncoding nEnc = SvtSysLocale::GetBestMimeEncoding();
196 0 : SelectTextEncoding( nEnc );
197 0 : }
198 :
199 : //------------------------------------------------------------------------
200 :
201 0 : void SvxTextEncodingBox::InsertTextEncoding( const rtl_TextEncoding nEnc,
202 : const String& rEntry, sal_uInt16 nPos )
203 : {
204 0 : sal_uInt16 nAt = InsertEntry( rEntry, nPos );
205 0 : SetEntryData( nAt, (void*)(sal_uIntPtr)nEnc );
206 0 : }
207 :
208 : //------------------------------------------------------------------------
209 :
210 0 : void SvxTextEncodingBox::InsertTextEncoding( const rtl_TextEncoding nEnc, sal_uInt16 nPos )
211 : {
212 0 : const String& rEntry = m_pEncTable->GetTextString( nEnc );
213 0 : if ( rEntry.Len() )
214 0 : InsertTextEncoding( nEnc, rEntry, nPos );
215 : else
216 0 : SAL_WARN( "svx.dialog", "SvxTextEncodingBox::InsertTextEncoding: no resource string for text encoding: " << static_cast<sal_Int32>( nEnc ) );
217 0 : }
218 :
219 : //------------------------------------------------------------------------
220 :
221 0 : rtl_TextEncoding SvxTextEncodingBox::GetSelectTextEncoding() const
222 : {
223 0 : sal_uInt16 nPos = GetSelectEntryPos();
224 :
225 0 : if ( nPos != LISTBOX_ENTRY_NOTFOUND )
226 0 : return rtl_TextEncoding( (sal_uIntPtr)GetEntryData(nPos) );
227 : else
228 0 : return RTL_TEXTENCODING_DONTKNOW;
229 : }
230 :
231 : //------------------------------------------------------------------------
232 :
233 0 : void SvxTextEncodingBox::SelectTextEncoding( const rtl_TextEncoding nEnc, sal_Bool bSelect )
234 : {
235 0 : sal_uInt16 nAt = EncodingToPos_Impl( nEnc );
236 :
237 0 : if ( nAt != LISTBOX_ENTRY_NOTFOUND )
238 0 : SelectEntryPos( nAt, bSelect );
239 216 : }
240 :
241 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|