xref: /trunk/main/svx/source/dialog/txencbox.cxx (revision f6e50924)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_svx.hxx"
26 
27 #include "svx/txencbox.hxx"
28 #include "svx/txenctab.hxx"
29 #include <svx/dialogs.hrc>
30 #include "svx/dbcharsethelper.hxx"
31 #include <vcl/svapp.hxx>
32 #include <rtl/tencinfo.h>
33 #include <rtl/locale.h>
34 #include <osl/nlsupport.h>
35 
36 //========================================================================
37 //	class SvxTextEncodingBox
38 //========================================================================
39 
SvxTextEncodingBox(Window * pParent,const ResId & rResId)40 SvxTextEncodingBox::SvxTextEncodingBox( Window* pParent, const ResId& rResId )
41 	:
42 	ListBox( pParent, rResId )
43 {
44 	m_pEncTable = new SvxTextEncodingTable;
45 }
46 
47 //------------------------------------------------------------------------
48 
~SvxTextEncodingBox()49 SvxTextEncodingBox::~SvxTextEncodingBox()
50 {
51 	delete m_pEncTable;
52 }
53 
54 //------------------------------------------------------------------------
55 
EncodingToPos_Impl(rtl_TextEncoding nEnc) const56 sal_uInt16 SvxTextEncodingBox::EncodingToPos_Impl( rtl_TextEncoding nEnc ) const
57 {
58 	sal_uInt16 nCount = GetEntryCount();
59 	for ( sal_uInt16 i=0; i<nCount; i++ )
60 	{
61 		if ( nEnc == rtl_TextEncoding( (sal_uIntPtr)GetEntryData(i) ) )
62 			return i;
63 	}
64 	return LISTBOX_ENTRY_NOTFOUND;
65 }
66 
67 //------------------------------------------------------------------------
68 
FillFromTextEncodingTable(sal_Bool bExcludeImportSubsets,sal_uInt32 nExcludeInfoFlags,sal_uInt32 nButIncludeInfoFlags)69 void SvxTextEncodingBox::FillFromTextEncodingTable(
70         sal_Bool bExcludeImportSubsets, sal_uInt32 nExcludeInfoFlags,
71         sal_uInt32 nButIncludeInfoFlags )
72 {
73     rtl_TextEncodingInfo aInfo;
74     aInfo.StructSize = sizeof(rtl_TextEncodingInfo);
75     sal_uInt32 nCount = m_pEncTable->Count();
76     for ( sal_uInt32 j=0; j<nCount; j++ )
77     {
78         sal_Bool bInsert = sal_True;
79         rtl_TextEncoding nEnc = rtl_TextEncoding( m_pEncTable->GetValue( j ) );
80         if ( nExcludeInfoFlags )
81         {
82             if ( !rtl_getTextEncodingInfo( nEnc, &aInfo ) )
83                 bInsert = sal_False;
84             else
85             {
86                 if ( (aInfo.Flags & nExcludeInfoFlags) == 0 )
87                 {
88                     if ( (nExcludeInfoFlags & RTL_TEXTENCODING_INFO_UNICODE) &&
89                             ((nEnc == RTL_TEXTENCODING_UCS2) ||
90                             nEnc == RTL_TEXTENCODING_UCS4) )
91                         bInsert = sal_False;    // InfoFlags don't work for Unicode :-(
92                 }
93                 else if ( (aInfo.Flags & nButIncludeInfoFlags) == 0 )
94                     bInsert = sal_False;
95             }
96         }
97         if ( bInsert )
98         {
99             if ( bExcludeImportSubsets )
100             {
101                 switch ( nEnc )
102                 {
103                     // subsets of RTL_TEXTENCODING_GB_18030
104                     case RTL_TEXTENCODING_GB_2312 :
105                     case RTL_TEXTENCODING_GBK :
106                     case RTL_TEXTENCODING_MS_936 :
107                         bInsert = sal_False;
108                     break;
109                 }
110             }
111             if ( bInsert )
112                 InsertTextEncoding( nEnc, m_pEncTable->GetString( j ) );
113         }
114     }
115 }
116 
117 //------------------------------------------------------------------------
118 
FillFromDbTextEncodingMap(sal_Bool bExcludeImportSubsets,sal_uInt32 nExcludeInfoFlags,sal_uInt32 nButIncludeInfoFlags)119 void SvxTextEncodingBox::FillFromDbTextEncodingMap(
120         sal_Bool bExcludeImportSubsets, sal_uInt32 nExcludeInfoFlags,
121         sal_uInt32 nButIncludeInfoFlags )
122 {
123     rtl_TextEncodingInfo aInfo;
124     aInfo.StructSize = sizeof(rtl_TextEncodingInfo);
125     svxform::ODataAccessCharsetHelper aCSH;
126     ::std::vector< rtl_TextEncoding > aEncs;
127     sal_Int32 nCount = aCSH.getSupportedTextEncodings( aEncs );
128     for ( sal_uInt16 j=0; j<nCount; j++ )
129     {
130         sal_Bool bInsert = sal_True;
131         rtl_TextEncoding nEnc = rtl_TextEncoding( aEncs[j] );
132         if ( nExcludeInfoFlags )
133         {
134             if ( !rtl_getTextEncodingInfo( nEnc, &aInfo ) )
135                 bInsert = sal_False;
136             else
137             {
138                 if ( (aInfo.Flags & nExcludeInfoFlags) == 0 )
139                 {
140                     if ( (nExcludeInfoFlags & RTL_TEXTENCODING_INFO_UNICODE) &&
141                             ((nEnc == RTL_TEXTENCODING_UCS2) ||
142                             nEnc == RTL_TEXTENCODING_UCS4) )
143                         bInsert = sal_False;    // InfoFlags don't work for Unicode :-(
144                 }
145                 else if ( (aInfo.Flags & nButIncludeInfoFlags) == 0 )
146                     bInsert = sal_False;
147             }
148         }
149         if ( bInsert )
150         {
151             if ( bExcludeImportSubsets )
152             {
153                 switch ( nEnc )
154                 {
155                     // subsets of RTL_TEXTENCODING_GB_18030
156                     case RTL_TEXTENCODING_GB_2312 :
157                     case RTL_TEXTENCODING_GBK :
158                     case RTL_TEXTENCODING_MS_936 :
159                         bInsert = sal_False;
160                     break;
161                 }
162             }
163             // CharsetMap offers a RTL_TEXTENCODING_DONTKNOW for internal use,
164             // makes no sense here and would result in an empty string as list
165             // entry.
166             if ( bInsert && nEnc != RTL_TEXTENCODING_DONTKNOW )
167                 InsertTextEncoding( nEnc );
168         }
169     }
170 }
171 
172 //------------------------------------------------------------------------
173 
FillWithMimeAndSelectBest()174 void SvxTextEncodingBox::FillWithMimeAndSelectBest()
175 {
176 	FillFromTextEncodingTable( sal_False, 0xffffffff, RTL_TEXTENCODING_INFO_MIME );
177     rtl_TextEncoding nEnc = SvtSysLocale::GetBestMimeEncoding();
178     SelectTextEncoding( nEnc );
179 }
180 
181 //------------------------------------------------------------------------
182 
InsertTextEncoding(const rtl_TextEncoding nEnc,const String & rEntry,sal_uInt16 nPos)183 void SvxTextEncodingBox::InsertTextEncoding( const rtl_TextEncoding nEnc,
184 			const String& rEntry, sal_uInt16 nPos )
185 {
186 	sal_uInt16 nAt = InsertEntry( rEntry, nPos );
187 	SetEntryData( nAt, (void*)(sal_uIntPtr)nEnc );
188 }
189 
190 //------------------------------------------------------------------------
191 
InsertTextEncoding(const rtl_TextEncoding nEnc,sal_uInt16 nPos)192 void SvxTextEncodingBox::InsertTextEncoding( const rtl_TextEncoding nEnc, sal_uInt16 nPos )
193 {
194     const String& rEntry = m_pEncTable->GetTextString( nEnc );
195     if ( rEntry.Len() )
196         InsertTextEncoding( nEnc, rEntry, nPos );
197     else
198     {
199 #ifdef DBG_UTIL
200         ByteString aMsg( "SvxTextEncodingBox::InsertTextEncoding: no resource string for text encoding: " );
201         aMsg += ByteString::CreateFromInt32( nEnc );
202         DBG_ERRORFILE( aMsg.GetBuffer() );
203 #endif
204     }
205 }
206 
207 //------------------------------------------------------------------------
208 
RemoveTextEncoding(const rtl_TextEncoding nEnc)209 void SvxTextEncodingBox::RemoveTextEncoding( const rtl_TextEncoding nEnc )
210 {
211 	sal_uInt16 nAt = EncodingToPos_Impl( nEnc );
212 
213 	if ( nAt != LISTBOX_ENTRY_NOTFOUND )
214 		RemoveEntry( nAt );
215 }
216 
217 //------------------------------------------------------------------------
218 
GetSelectTextEncoding() const219 rtl_TextEncoding SvxTextEncodingBox::GetSelectTextEncoding() const
220 {
221 	sal_uInt16 nPos = GetSelectEntryPos();
222 
223 	if ( nPos != LISTBOX_ENTRY_NOTFOUND )
224 		return rtl_TextEncoding( (sal_uIntPtr)GetEntryData(nPos) );
225 	else
226 		return RTL_TEXTENCODING_DONTKNOW;
227 }
228 
229 //------------------------------------------------------------------------
230 
SelectTextEncoding(const rtl_TextEncoding nEnc,sal_Bool bSelect)231 void SvxTextEncodingBox::SelectTextEncoding( const rtl_TextEncoding nEnc, sal_Bool bSelect )
232 {
233 	sal_uInt16 nAt = EncodingToPos_Impl( nEnc );
234 
235 	if ( nAt != LISTBOX_ENTRY_NOTFOUND )
236 		SelectEntryPos( nAt, bSelect );
237 }
238 
239 //------------------------------------------------------------------------
240 
IsTextEncodingSelected(const rtl_TextEncoding nEnc) const241 sal_Bool SvxTextEncodingBox::IsTextEncodingSelected( const rtl_TextEncoding nEnc ) const
242 {
243 	sal_uInt16 nAt = EncodingToPos_Impl( nEnc );
244 
245 	if ( nAt != LISTBOX_ENTRY_NOTFOUND )
246 		return IsEntryPosSelected( nAt );
247 	else
248 		return sal_False;
249 }
250 
251