bump product version to 4.1.6.2
[LibreOffice.git] / svx / source / dialog / txencbox.cxx
blobef3040727dc7679a936a7e64b5582c5d83a70824
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
34 //========================================================================
35 // class SvxTextEncodingBox
36 //========================================================================
38 SvxTextEncodingBox::SvxTextEncodingBox( Window* pParent, const ResId& rResId )
40 ListBox( pParent, rResId )
42 m_pEncTable = new SvxTextEncodingTable;
45 SvxTextEncodingBox::SvxTextEncodingBox( Window* pParent, WinBits nBits )
46 : ListBox( pParent, nBits )
48 m_pEncTable = new SvxTextEncodingTable;
51 extern "C" SAL_DLLPUBLIC_EXPORT Window* SAL_CALL makeSvxTextEncodingBox(Window *pParent, VclBuilder::stringmap &)
53 WinBits nWinBits = WB_LEFT|WB_DROPDOWN|WB_VCENTER|WB_3DLOOK|WB_SIMPLEMODE;
54 SvxTextEncodingBox *pListBox = new SvxTextEncodingBox(pParent, nWinBits);
55 pListBox->EnableAutoSize(true);
56 return pListBox;
59 //------------------------------------------------------------------------
61 SvxTextEncodingBox::~SvxTextEncodingBox()
63 delete m_pEncTable;
66 //------------------------------------------------------------------------
68 sal_uInt16 SvxTextEncodingBox::EncodingToPos_Impl( rtl_TextEncoding nEnc ) const
70 sal_uInt16 nCount = GetEntryCount();
71 for ( sal_uInt16 i=0; i<nCount; i++ )
73 if ( nEnc == rtl_TextEncoding( (sal_uIntPtr)GetEntryData(i) ) )
74 return i;
76 return LISTBOX_ENTRY_NOTFOUND;
79 //------------------------------------------------------------------------
81 void SvxTextEncodingBox::FillFromTextEncodingTable(
82 sal_Bool bExcludeImportSubsets, sal_uInt32 nExcludeInfoFlags,
83 sal_uInt32 nButIncludeInfoFlags )
85 rtl_TextEncodingInfo aInfo;
86 aInfo.StructSize = sizeof(rtl_TextEncodingInfo);
87 sal_uInt32 nCount = m_pEncTable->Count();
88 for ( sal_uInt32 j=0; j<nCount; j++ )
90 sal_Bool bInsert = sal_True;
91 rtl_TextEncoding nEnc = rtl_TextEncoding( m_pEncTable->GetValue( j ) );
92 if ( nExcludeInfoFlags )
94 if ( !rtl_getTextEncodingInfo( nEnc, &aInfo ) )
95 bInsert = sal_False;
96 else
98 if ( (aInfo.Flags & nExcludeInfoFlags) == 0 )
100 if ( (nExcludeInfoFlags & RTL_TEXTENCODING_INFO_UNICODE) &&
101 ((nEnc == RTL_TEXTENCODING_UCS2) ||
102 nEnc == RTL_TEXTENCODING_UCS4) )
103 bInsert = sal_False; // InfoFlags don't work for Unicode :-(
105 else if ( (aInfo.Flags & nButIncludeInfoFlags) == 0 )
106 bInsert = sal_False;
109 if ( bInsert )
111 if ( bExcludeImportSubsets )
113 switch ( nEnc )
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 bInsert = sal_False;
120 break;
123 if ( bInsert )
124 InsertTextEncoding( nEnc, m_pEncTable->GetString( j ) );
129 //------------------------------------------------------------------------
131 void SvxTextEncodingBox::FillFromDbTextEncodingMap(
132 sal_Bool bExcludeImportSubsets, sal_uInt32 nExcludeInfoFlags,
133 sal_uInt32 nButIncludeInfoFlags )
135 #ifdef DISABLE_DBCONNECTIVITY
136 (void)bExcludeImportSubsets;
137 (void)nExcludeInfoFlags;
138 (void)nButIncludeInfoFlags;
139 #else
140 rtl_TextEncodingInfo aInfo;
141 aInfo.StructSize = sizeof(rtl_TextEncodingInfo);
142 svxform::ODataAccessCharsetHelper aCSH;
143 ::std::vector< rtl_TextEncoding > aEncs;
144 sal_Int32 nCount = aCSH.getSupportedTextEncodings( aEncs );
145 for ( sal_uInt16 j=0; j<nCount; j++ )
147 sal_Bool bInsert = sal_True;
148 rtl_TextEncoding nEnc = rtl_TextEncoding( aEncs[j] );
149 if ( nExcludeInfoFlags )
151 if ( !rtl_getTextEncodingInfo( nEnc, &aInfo ) )
152 bInsert = sal_False;
153 else
155 if ( (aInfo.Flags & nExcludeInfoFlags) == 0 )
157 if ( (nExcludeInfoFlags & RTL_TEXTENCODING_INFO_UNICODE) &&
158 ((nEnc == RTL_TEXTENCODING_UCS2) ||
159 nEnc == RTL_TEXTENCODING_UCS4) )
160 bInsert = sal_False; // InfoFlags don't work for Unicode :-(
162 else if ( (aInfo.Flags & nButIncludeInfoFlags) == 0 )
163 bInsert = sal_False;
166 if ( bInsert )
168 if ( bExcludeImportSubsets )
170 switch ( nEnc )
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 bInsert = sal_False;
177 break;
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 if ( bInsert && nEnc != RTL_TEXTENCODING_DONTKNOW )
184 InsertTextEncoding( nEnc );
187 #endif
190 //------------------------------------------------------------------------
192 void SvxTextEncodingBox::FillWithMimeAndSelectBest()
194 FillFromTextEncodingTable( sal_False, 0xffffffff, RTL_TEXTENCODING_INFO_MIME );
195 rtl_TextEncoding nEnc = SvtSysLocale::GetBestMimeEncoding();
196 SelectTextEncoding( nEnc );
199 //------------------------------------------------------------------------
201 void SvxTextEncodingBox::InsertTextEncoding( const rtl_TextEncoding nEnc,
202 const String& rEntry, sal_uInt16 nPos )
204 sal_uInt16 nAt = InsertEntry( rEntry, nPos );
205 SetEntryData( nAt, (void*)(sal_uIntPtr)nEnc );
208 //------------------------------------------------------------------------
210 void SvxTextEncodingBox::InsertTextEncoding( const rtl_TextEncoding nEnc, sal_uInt16 nPos )
212 const String& rEntry = m_pEncTable->GetTextString( nEnc );
213 if ( rEntry.Len() )
214 InsertTextEncoding( nEnc, rEntry, nPos );
215 else
216 SAL_WARN( "svx.dialog", "SvxTextEncodingBox::InsertTextEncoding: no resource string for text encoding: " << static_cast<sal_Int32>( nEnc ) );
219 //------------------------------------------------------------------------
221 rtl_TextEncoding SvxTextEncodingBox::GetSelectTextEncoding() const
223 sal_uInt16 nPos = GetSelectEntryPos();
225 if ( nPos != LISTBOX_ENTRY_NOTFOUND )
226 return rtl_TextEncoding( (sal_uIntPtr)GetEntryData(nPos) );
227 else
228 return RTL_TEXTENCODING_DONTKNOW;
231 //------------------------------------------------------------------------
233 void SvxTextEncodingBox::SelectTextEncoding( const rtl_TextEncoding nEnc, sal_Bool bSelect )
235 sal_uInt16 nAt = EncodingToPos_Impl( nEnc );
237 if ( nAt != LISTBOX_ENTRY_NOTFOUND )
238 SelectEntryPos( nAt, bSelect );
241 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */