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