1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: txencbox.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svx.hxx"
34 #include "txencbox.hxx"
35 #include "txenctab.hxx"
36 #include <svx/dialogs.hrc>
37 #include "svx/dbcharsethelper.hxx"
38 #include <vcl/svapp.hxx>
39 #include <rtl/tencinfo.h>
40 #include <rtl/locale.h>
41 #include <osl/nlsupport.h>
43 //========================================================================
44 // class SvxTextEncodingBox
45 //========================================================================
47 SvxTextEncodingBox::SvxTextEncodingBox( Window
* pParent
, const ResId
& rResId
)
49 ListBox( pParent
, rResId
)
51 m_pEncTable
= new SvxTextEncodingTable
;
54 //------------------------------------------------------------------------
56 SvxTextEncodingBox::~SvxTextEncodingBox()
61 //------------------------------------------------------------------------
63 USHORT
SvxTextEncodingBox::EncodingToPos_Impl( rtl_TextEncoding nEnc
) const
65 USHORT nCount
= GetEntryCount();
66 for ( USHORT i
=0; i
<nCount
; i
++ )
68 if ( nEnc
== rtl_TextEncoding( (ULONG
)GetEntryData(i
) ) )
71 return LISTBOX_ENTRY_NOTFOUND
;
74 //------------------------------------------------------------------------
76 void SvxTextEncodingBox::FillFromTextEncodingTable(
77 sal_Bool bExcludeImportSubsets
, sal_uInt32 nExcludeInfoFlags
,
78 sal_uInt32 nButIncludeInfoFlags
)
80 rtl_TextEncodingInfo aInfo
;
81 aInfo
.StructSize
= sizeof(rtl_TextEncodingInfo
);
82 sal_uInt32 nCount
= m_pEncTable
->Count();
83 for ( sal_uInt32 j
=0; j
<nCount
; j
++ )
86 rtl_TextEncoding nEnc
= rtl_TextEncoding( m_pEncTable
->GetValue( j
) );
87 if ( nExcludeInfoFlags
)
89 if ( !rtl_getTextEncodingInfo( nEnc
, &aInfo
) )
93 if ( (aInfo
.Flags
& nExcludeInfoFlags
) == 0 )
95 if ( (nExcludeInfoFlags
& RTL_TEXTENCODING_INFO_UNICODE
) &&
96 ((nEnc
== RTL_TEXTENCODING_UCS2
) ||
97 nEnc
== RTL_TEXTENCODING_UCS4
) )
98 bInsert
= FALSE
; // InfoFlags don't work for Unicode :-(
100 else if ( (aInfo
.Flags
& nButIncludeInfoFlags
) == 0 )
106 if ( bExcludeImportSubsets
)
110 // subsets of RTL_TEXTENCODING_GB_18030
111 case RTL_TEXTENCODING_GB_2312
:
112 case RTL_TEXTENCODING_GBK
:
113 case RTL_TEXTENCODING_MS_936
:
119 InsertTextEncoding( nEnc
, m_pEncTable
->GetString( j
) );
124 //------------------------------------------------------------------------
126 void SvxTextEncodingBox::FillFromDbTextEncodingMap(
127 sal_Bool bExcludeImportSubsets
, sal_uInt32 nExcludeInfoFlags
,
128 sal_uInt32 nButIncludeInfoFlags
)
130 rtl_TextEncodingInfo aInfo
;
131 aInfo
.StructSize
= sizeof(rtl_TextEncodingInfo
);
132 svxform::ODataAccessCharsetHelper aCSH
;
133 ::std::vector
< rtl_TextEncoding
> aEncs
;
134 sal_Int32 nCount
= aCSH
.getSupportedTextEncodings( aEncs
);
135 for ( USHORT j
=0; j
<nCount
; j
++ )
138 rtl_TextEncoding nEnc
= rtl_TextEncoding( aEncs
[j
] );
139 if ( nExcludeInfoFlags
)
141 if ( !rtl_getTextEncodingInfo( nEnc
, &aInfo
) )
145 if ( (aInfo
.Flags
& nExcludeInfoFlags
) == 0 )
147 if ( (nExcludeInfoFlags
& RTL_TEXTENCODING_INFO_UNICODE
) &&
148 ((nEnc
== RTL_TEXTENCODING_UCS2
) ||
149 nEnc
== RTL_TEXTENCODING_UCS4
) )
150 bInsert
= FALSE
; // InfoFlags don't work for Unicode :-(
152 else if ( (aInfo
.Flags
& nButIncludeInfoFlags
) == 0 )
158 if ( bExcludeImportSubsets
)
162 // subsets of RTL_TEXTENCODING_GB_18030
163 case RTL_TEXTENCODING_GB_2312
:
164 case RTL_TEXTENCODING_GBK
:
165 case RTL_TEXTENCODING_MS_936
:
170 // CharsetMap offers a RTL_TEXTENCODING_DONTKNOW for internal use,
171 // makes no sense here and would result in an empty string as list
173 if ( bInsert
&& nEnc
!= RTL_TEXTENCODING_DONTKNOW
)
174 InsertTextEncoding( nEnc
);
179 //------------------------------------------------------------------------
182 rtl_TextEncoding
SvxTextEncodingBox::GetBestMimeEncoding()
184 const sal_Char
* pCharSet
= rtl_getBestMimeCharsetFromTextEncoding(
185 gsl_getSystemTextEncoding() );
188 // If the system locale is unknown to us, e.g. LC_ALL=xx, match the UI
189 // language if possible.
190 ::com::sun::star::lang::Locale
aLocale(
191 Application::GetSettings().GetUILocale() );
192 rtl_Locale
* pLocale
= rtl_locale_register( aLocale
.Language
.getStr(),
193 aLocale
.Country
.getStr(), aLocale
.Variant
.getStr() );
194 rtl_TextEncoding nEnc
= osl_getTextEncodingFromLocale( pLocale
);
195 pCharSet
= rtl_getBestMimeCharsetFromTextEncoding( nEnc
);
197 rtl_TextEncoding nRet
;
199 nRet
= rtl_getTextEncodingFromMimeCharset( pCharSet
);
201 nRet
= RTL_TEXTENCODING_UTF8
;
205 //------------------------------------------------------------------------
207 void SvxTextEncodingBox::FillWithMimeAndSelectBest()
209 FillFromTextEncodingTable( sal_False
, 0xffffffff, RTL_TEXTENCODING_INFO_MIME
);
210 rtl_TextEncoding nEnc
= GetBestMimeEncoding();
211 SelectTextEncoding( nEnc
);
214 //------------------------------------------------------------------------
216 void SvxTextEncodingBox::InsertTextEncoding( const rtl_TextEncoding nEnc
,
217 const String
& rEntry
, USHORT nPos
)
219 USHORT nAt
= InsertEntry( rEntry
, nPos
);
220 SetEntryData( nAt
, (void*)(ULONG
)nEnc
);
223 //------------------------------------------------------------------------
225 void SvxTextEncodingBox::InsertTextEncoding( const rtl_TextEncoding nEnc
, USHORT nPos
)
227 const String
& rEntry
= m_pEncTable
->GetTextString( nEnc
);
229 InsertTextEncoding( nEnc
, rEntry
, nPos
);
233 ByteString
aMsg( "SvxTextEncodingBox::InsertTextEncoding: no resource string for text encoding: " );
234 aMsg
+= ByteString::CreateFromInt32( nEnc
);
235 DBG_ERRORFILE( aMsg
.GetBuffer() );
240 //------------------------------------------------------------------------
242 void SvxTextEncodingBox::RemoveTextEncoding( const rtl_TextEncoding nEnc
)
244 USHORT nAt
= EncodingToPos_Impl( nEnc
);
246 if ( nAt
!= LISTBOX_ENTRY_NOTFOUND
)
250 //------------------------------------------------------------------------
252 rtl_TextEncoding
SvxTextEncodingBox::GetSelectTextEncoding() const
254 USHORT nPos
= GetSelectEntryPos();
256 if ( nPos
!= LISTBOX_ENTRY_NOTFOUND
)
257 return rtl_TextEncoding( (ULONG
)GetEntryData(nPos
) );
259 return RTL_TEXTENCODING_DONTKNOW
;
262 //------------------------------------------------------------------------
264 void SvxTextEncodingBox::SelectTextEncoding( const rtl_TextEncoding nEnc
, BOOL bSelect
)
266 USHORT nAt
= EncodingToPos_Impl( nEnc
);
268 if ( nAt
!= LISTBOX_ENTRY_NOTFOUND
)
269 SelectEntryPos( nAt
, bSelect
);
272 //------------------------------------------------------------------------
274 BOOL
SvxTextEncodingBox::IsTextEncodingSelected( const rtl_TextEncoding nEnc
) const
276 USHORT nAt
= EncodingToPos_Impl( nEnc
);
278 if ( nAt
!= LISTBOX_ENTRY_NOTFOUND
)
279 return IsEntryPosSelected( nAt
);