xref: /AOO41X/main/svx/source/dialog/txencbox.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_svx.hxx"
30 
31 #include "svx/txencbox.hxx"
32 #include "svx/txenctab.hxx"
33 #include <svx/dialogs.hrc>
34 #include "svx/dbcharsethelper.hxx"
35 #include <vcl/svapp.hxx>
36 #include <rtl/tencinfo.h>
37 #include <rtl/locale.h>
38 #include <osl/nlsupport.h>
39 
40 //========================================================================
41 //	class SvxTextEncodingBox
42 //========================================================================
43 
44 SvxTextEncodingBox::SvxTextEncodingBox( Window* pParent, const ResId& rResId )
45 	:
46 	ListBox( pParent, rResId )
47 {
48 	m_pEncTable = new SvxTextEncodingTable;
49 }
50 
51 //------------------------------------------------------------------------
52 
53 SvxTextEncodingBox::~SvxTextEncodingBox()
54 {
55 	delete m_pEncTable;
56 }
57 
58 //------------------------------------------------------------------------
59 
60 sal_uInt16 SvxTextEncodingBox::EncodingToPos_Impl( rtl_TextEncoding nEnc ) const
61 {
62 	sal_uInt16 nCount = GetEntryCount();
63 	for ( sal_uInt16 i=0; i<nCount; i++ )
64 	{
65 		if ( nEnc == rtl_TextEncoding( (sal_uIntPtr)GetEntryData(i) ) )
66 			return i;
67 	}
68 	return LISTBOX_ENTRY_NOTFOUND;
69 }
70 
71 //------------------------------------------------------------------------
72 
73 void SvxTextEncodingBox::FillFromTextEncodingTable(
74         sal_Bool bExcludeImportSubsets, sal_uInt32 nExcludeInfoFlags,
75         sal_uInt32 nButIncludeInfoFlags )
76 {
77     rtl_TextEncodingInfo aInfo;
78     aInfo.StructSize = sizeof(rtl_TextEncodingInfo);
79     sal_uInt32 nCount = m_pEncTable->Count();
80     for ( sal_uInt32 j=0; j<nCount; j++ )
81     {
82         sal_Bool bInsert = sal_True;
83         rtl_TextEncoding nEnc = rtl_TextEncoding( m_pEncTable->GetValue( j ) );
84         if ( nExcludeInfoFlags )
85         {
86             if ( !rtl_getTextEncodingInfo( nEnc, &aInfo ) )
87                 bInsert = sal_False;
88             else
89             {
90                 if ( (aInfo.Flags & nExcludeInfoFlags) == 0 )
91                 {
92                     if ( (nExcludeInfoFlags & RTL_TEXTENCODING_INFO_UNICODE) &&
93                             ((nEnc == RTL_TEXTENCODING_UCS2) ||
94                             nEnc == RTL_TEXTENCODING_UCS4) )
95                         bInsert = sal_False;    // InfoFlags don't work for Unicode :-(
96                 }
97                 else if ( (aInfo.Flags & nButIncludeInfoFlags) == 0 )
98                     bInsert = sal_False;
99             }
100         }
101         if ( bInsert )
102         {
103             if ( bExcludeImportSubsets )
104             {
105                 switch ( nEnc )
106                 {
107                     // subsets of RTL_TEXTENCODING_GB_18030
108                     case RTL_TEXTENCODING_GB_2312 :
109                     case RTL_TEXTENCODING_GBK :
110                     case RTL_TEXTENCODING_MS_936 :
111                         bInsert = sal_False;
112                     break;
113                 }
114             }
115             if ( bInsert )
116                 InsertTextEncoding( nEnc, m_pEncTable->GetString( j ) );
117         }
118     }
119 }
120 
121 //------------------------------------------------------------------------
122 
123 void SvxTextEncodingBox::FillFromDbTextEncodingMap(
124         sal_Bool bExcludeImportSubsets, sal_uInt32 nExcludeInfoFlags,
125         sal_uInt32 nButIncludeInfoFlags )
126 {
127     rtl_TextEncodingInfo aInfo;
128     aInfo.StructSize = sizeof(rtl_TextEncodingInfo);
129     svxform::ODataAccessCharsetHelper aCSH;
130     ::std::vector< rtl_TextEncoding > aEncs;
131     sal_Int32 nCount = aCSH.getSupportedTextEncodings( aEncs );
132     for ( sal_uInt16 j=0; j<nCount; j++ )
133     {
134         sal_Bool bInsert = sal_True;
135         rtl_TextEncoding nEnc = rtl_TextEncoding( aEncs[j] );
136         if ( nExcludeInfoFlags )
137         {
138             if ( !rtl_getTextEncodingInfo( nEnc, &aInfo ) )
139                 bInsert = sal_False;
140             else
141             {
142                 if ( (aInfo.Flags & nExcludeInfoFlags) == 0 )
143                 {
144                     if ( (nExcludeInfoFlags & RTL_TEXTENCODING_INFO_UNICODE) &&
145                             ((nEnc == RTL_TEXTENCODING_UCS2) ||
146                             nEnc == RTL_TEXTENCODING_UCS4) )
147                         bInsert = sal_False;    // InfoFlags don't work for Unicode :-(
148                 }
149                 else if ( (aInfo.Flags & nButIncludeInfoFlags) == 0 )
150                     bInsert = sal_False;
151             }
152         }
153         if ( bInsert )
154         {
155             if ( bExcludeImportSubsets )
156             {
157                 switch ( nEnc )
158                 {
159                     // subsets of RTL_TEXTENCODING_GB_18030
160                     case RTL_TEXTENCODING_GB_2312 :
161                     case RTL_TEXTENCODING_GBK :
162                     case RTL_TEXTENCODING_MS_936 :
163                         bInsert = sal_False;
164                     break;
165                 }
166             }
167             // CharsetMap offers a RTL_TEXTENCODING_DONTKNOW for internal use,
168             // makes no sense here and would result in an empty string as list
169             // entry.
170             if ( bInsert && nEnc != RTL_TEXTENCODING_DONTKNOW )
171                 InsertTextEncoding( nEnc );
172         }
173     }
174 }
175 
176 //------------------------------------------------------------------------
177 
178 void SvxTextEncodingBox::FillWithMimeAndSelectBest()
179 {
180 	FillFromTextEncodingTable( sal_False, 0xffffffff, RTL_TEXTENCODING_INFO_MIME );
181     rtl_TextEncoding nEnc = SvtSysLocale::GetBestMimeEncoding();
182     SelectTextEncoding( nEnc );
183 }
184 
185 //------------------------------------------------------------------------
186 
187 void SvxTextEncodingBox::InsertTextEncoding( const rtl_TextEncoding nEnc,
188 			const String& rEntry, sal_uInt16 nPos )
189 {
190 	sal_uInt16 nAt = InsertEntry( rEntry, nPos );
191 	SetEntryData( nAt, (void*)(sal_uIntPtr)nEnc );
192 }
193 
194 //------------------------------------------------------------------------
195 
196 void SvxTextEncodingBox::InsertTextEncoding( const rtl_TextEncoding nEnc, sal_uInt16 nPos )
197 {
198     const String& rEntry = m_pEncTable->GetTextString( nEnc );
199     if ( rEntry.Len() )
200         InsertTextEncoding( nEnc, rEntry, nPos );
201     else
202     {
203 #ifdef DBG_UTIL
204         ByteString aMsg( "SvxTextEncodingBox::InsertTextEncoding: no resource string for text encoding: " );
205         aMsg += ByteString::CreateFromInt32( nEnc );
206         DBG_ERRORFILE( aMsg.GetBuffer() );
207 #endif
208     }
209 }
210 
211 //------------------------------------------------------------------------
212 
213 void SvxTextEncodingBox::RemoveTextEncoding( const rtl_TextEncoding nEnc )
214 {
215 	sal_uInt16 nAt = EncodingToPos_Impl( nEnc );
216 
217 	if ( nAt != LISTBOX_ENTRY_NOTFOUND )
218 		RemoveEntry( nAt );
219 }
220 
221 //------------------------------------------------------------------------
222 
223 rtl_TextEncoding SvxTextEncodingBox::GetSelectTextEncoding() const
224 {
225 	sal_uInt16 nPos = GetSelectEntryPos();
226 
227 	if ( nPos != LISTBOX_ENTRY_NOTFOUND )
228 		return rtl_TextEncoding( (sal_uIntPtr)GetEntryData(nPos) );
229 	else
230 		return RTL_TEXTENCODING_DONTKNOW;
231 }
232 
233 //------------------------------------------------------------------------
234 
235 void SvxTextEncodingBox::SelectTextEncoding( const rtl_TextEncoding nEnc, sal_Bool bSelect )
236 {
237 	sal_uInt16 nAt = EncodingToPos_Impl( nEnc );
238 
239 	if ( nAt != LISTBOX_ENTRY_NOTFOUND )
240 		SelectEntryPos( nAt, bSelect );
241 }
242 
243 //------------------------------------------------------------------------
244 
245 sal_Bool SvxTextEncodingBox::IsTextEncodingSelected( const rtl_TextEncoding nEnc ) const
246 {
247 	sal_uInt16 nAt = EncodingToPos_Impl( nEnc );
248 
249 	if ( nAt != LISTBOX_ENTRY_NOTFOUND )
250 		return IsEntryPosSelected( nAt );
251 	else
252 		return sal_False;
253 }
254 
255