bump product version to 4.1.6.2
[LibreOffice.git] / sw / source / ui / dialog / ascfldlg.cxx
blobb808142a158faee13c7b2429f8fb425cfb0ba524
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 .
20 #include <hintids.hxx>
21 #include <rtl/textenc.h>
22 #include <i18nlangtag/mslangid.hxx>
23 #include <com/sun/star/i18n/ScriptType.hpp>
24 #include <unotools/lingucfg.hxx>
25 #include <fontcfg.hxx>
26 #include <swmodule.hxx>
27 #include <editeng/unolingu.hxx>
28 #include <sfx2/printer.hxx>
29 #include <editeng/flstitem.hxx>
30 #include <svx/dlgutil.hxx>
31 #include <editeng/fontitem.hxx>
32 #include <editeng/langitem.hxx>
33 #include <editeng/scripttypeitem.hxx>
34 #include <swtypes.hxx>
35 #include <ascfldlg.hxx>
36 #include <shellio.hxx>
37 #include <docsh.hxx>
38 #include <doc.hxx>
40 #include <dialog.hrc>
42 #include "vcl/metric.hxx"
45 using namespace ::com::sun::star;
47 const sal_Unicode cDialogExtraDataClose = '}';
48 const char sDialogImpExtraData[] = "EncImpDlg:{";
49 const char sDialogExpExtraData[] = "EncExpDlg:{";
50 const sal_uInt16 nDialogExtraDataLen = 11; // 12345678901
52 SwAsciiFilterDlg::SwAsciiFilterDlg( Window* pParent, SwDocShell& rDocSh,
53 SvStream* pStream )
54 : SfxModalDialog(pParent, "AsciiFilterDialog", "modules/swriter/ui/asciifilterdialog.ui")
55 , m_bSaveLineStatus(true)
57 get(m_pCharSetLB, "charset");
58 m_pCharSetLB->SetStyle(m_pCharSetLB->GetStyle() | WB_SORT);
59 get(m_pFontFT, "fontft");
60 get(m_pFontLB, "font");
61 m_pFontLB->SetStyle(m_pFontLB->GetStyle() | WB_SORT);
62 get(m_pLanguageFT, "languageft");
63 get(m_pLanguageLB, "language");
64 m_pLanguageLB->SetStyle(m_pLanguageLB->GetStyle() | WB_SORT);
65 get(m_pCRLF_RB, "crlf");
66 get(m_pCR_RB, "cr");
67 get(m_pLF_RB, "lf");
69 SwAsciiOptions aOpt;
71 const OUString sFindNm = OUString::createFromAscii(
72 pStream ? sDialogImpExtraData
73 : sDialogExpExtraData);
74 sal_uInt16 nEnd, nStt = GetExtraData().Search( sFindNm );
75 if( STRING_NOTFOUND != nStt )
77 nStt += nDialogExtraDataLen;
78 nEnd = GetExtraData().Search( cDialogExtraDataClose, nStt );
79 if( STRING_NOTFOUND != nEnd )
81 aOpt.ReadUserData( GetExtraData().Copy( nStt, nEnd - nStt ));
82 nStt -= nDialogExtraDataLen;
83 GetExtraData().Erase( nStt, nEnd - nStt + 1 );
88 // read the first chars and check the charset, (language - with L&H)
89 if( pStream )
91 char aBuffer[ 4098 ];
92 sal_uLong nOldPos = pStream->Tell();
93 sal_uLong nBytesRead = pStream->Read( aBuffer, 4096 );
94 pStream->Seek( nOldPos );
96 if( nBytesRead <= 4096 )
98 aBuffer[ nBytesRead ] = '0';
99 aBuffer[ nBytesRead+1 ] = '0';
102 bool bCR = false, bLF = false, bNullChar = false;
103 for( sal_uInt16 nCnt = 0; nCnt < nBytesRead; ++nCnt )
104 switch( aBuffer[ nCnt ] )
106 case 0x0: bNullChar = true; break;
107 case 0xA: bLF = true; break;
108 case 0xD: bCR = true; break;
109 case 0xC:
110 case 0x1A:
111 case 0x9: break;
112 default: break;
115 if( !bNullChar )
117 if( bCR )
119 if( bLF )
121 aOpt.SetParaFlags( LINEEND_CRLF );
123 else
125 aOpt.SetParaFlags( LINEEND_CR );
128 else if( bLF )
130 aOpt.SetParaFlags( LINEEND_LF );
134 sal_uInt16 nAppScriptType = GetI18NScriptTypeOfLanguage( (sal_uInt16)GetAppLanguage() );
135 SwDoc* pDoc = rDocSh.GetDoc();
137 // initialise language
139 if( !aOpt.GetLanguage() )
141 if(pDoc)
143 sal_uInt16 nWhich = GetWhichOfScript( RES_CHRATR_LANGUAGE, nAppScriptType);
144 aOpt.SetLanguage( ((SvxLanguageItem&)pDoc->
145 GetDefault( nWhich )).GetLanguage());
147 else
149 SvtLinguOptions aLinguOpt;
150 SvtLinguConfig().GetOptions( aLinguOpt );
151 switch(nAppScriptType)
153 case SCRIPTTYPE_ASIAN:
154 aOpt.SetLanguage(MsLangId::resolveSystemLanguageByScriptType(aLinguOpt.nDefaultLanguage_CJK, SCRIPTTYPE_ASIAN));
155 break;
156 case SCRIPTTYPE_COMPLEX:
157 aOpt.SetLanguage(MsLangId::resolveSystemLanguageByScriptType(aLinguOpt.nDefaultLanguage_CTL, SCRIPTTYPE_COMPLEX));
158 break;
159 //SCRIPTTYPE_LATIN:
160 default:
161 aOpt.SetLanguage(MsLangId::resolveSystemLanguageByScriptType(aLinguOpt.nDefaultLanguage, SCRIPTTYPE_LATIN));
166 m_pLanguageLB->SetLanguageList( LANG_LIST_ALL, sal_True, sal_False );
167 m_pLanguageLB->SelectLanguage( aOpt.GetLanguage() );
171 bool bDelPrinter = false;
172 SfxPrinter* pPrt = pDoc ? pDoc->getPrinter(false) : 0;
173 if( !pPrt )
175 SfxItemSet* pSet = new SfxItemSet( rDocSh.GetPool(),
176 SID_PRINTER_NOTFOUND_WARN, SID_PRINTER_NOTFOUND_WARN,
177 SID_PRINTER_CHANGESTODOC, SID_PRINTER_CHANGESTODOC,
178 0 );
179 pPrt = new SfxPrinter( pSet );
180 bDelPrinter = true;
183 // get the set of disctinct available family names
184 std::set< String > aFontNames;
185 int nFontNames = pPrt->GetDevFontCount();
186 for( int i = 0; i < nFontNames; i++ )
188 FontInfo aInf( pPrt->GetDevFont( i ) );
189 aFontNames.insert( aInf.GetName() );
192 // insert to listbox
193 for( std::set< String >::const_iterator it = aFontNames.begin();
194 it != aFontNames.end(); ++it )
196 m_pFontLB->InsertEntry( *it );
199 if( !aOpt.GetFontName().Len() )
201 LanguageType eLang = aOpt.GetLanguage();
202 Font aTmpFont(OutputDevice::GetDefaultFont(DEFAULTFONT_FIXED, eLang, DEFAULTFONT_FLAGS_ONLYONE, pPrt));
203 aOpt.SetFontName(aTmpFont.GetName());
206 m_pFontLB->SelectEntry( aOpt.GetFontName() );
208 if( bDelPrinter )
209 delete pPrt;
214 else
216 // hide the unused Controls for Export
217 m_pFontFT->Hide();
218 m_pFontLB->Hide();
219 m_pLanguageFT->Hide();
220 m_pLanguageLB->Hide();
223 // initialise character set
224 m_pCharSetLB->FillFromTextEncodingTable( pStream != NULL );
225 m_pCharSetLB->SelectTextEncoding( aOpt.GetCharSet() );
227 m_pCharSetLB->SetSelectHdl( LINK( this, SwAsciiFilterDlg, CharSetSelHdl ));
228 m_pCRLF_RB->SetToggleHdl( LINK( this, SwAsciiFilterDlg, LineEndHdl ));
229 m_pLF_RB->SetToggleHdl( LINK( this, SwAsciiFilterDlg, LineEndHdl ));
230 m_pCR_RB->SetToggleHdl( LINK( this, SwAsciiFilterDlg, LineEndHdl ));
232 SetCRLF( aOpt.GetParaFlags() );
234 m_pCRLF_RB->SaveValue();
235 m_pLF_RB->SaveValue();
236 m_pCR_RB->SaveValue();
240 SwAsciiFilterDlg::~SwAsciiFilterDlg()
245 void SwAsciiFilterDlg::FillOptions( SwAsciiOptions& rOptions )
247 sal_uLong nCCode = m_pCharSetLB->GetSelectTextEncoding();
248 String sFont;
249 sal_uLong nLng = 0;
250 if( m_pFontLB->IsVisible() )
252 sFont = m_pFontLB->GetSelectEntry();
253 nLng = (sal_uLong)m_pLanguageLB->GetSelectLanguage();
256 rOptions.SetFontName( sFont );
257 rOptions.SetCharSet( rtl_TextEncoding( nCCode ) );
258 rOptions.SetLanguage( sal_uInt16( nLng ) );
259 rOptions.SetParaFlags( GetCRLF() );
261 // save the user settings
262 String sData;
263 rOptions.WriteUserData( sData );
264 if( sData.Len() )
266 const OUString sFindNm = OUString::createFromAscii(
267 m_pFontLB->IsVisible() ? sDialogImpExtraData
268 : sDialogExpExtraData);
269 sal_uInt16 nEnd, nStt = GetExtraData().Search( sFindNm );
270 if( STRING_NOTFOUND != nStt )
272 // called twice, so remove "old" settings
273 nEnd = GetExtraData().Search( cDialogExtraDataClose,
274 nStt + nDialogExtraDataLen );
275 if( STRING_NOTFOUND != nEnd )
276 GetExtraData().Erase( nStt, nEnd - nStt + 1 );
278 String sTmp(GetExtraData());
279 sTmp += sFindNm;
280 sTmp += sData;
281 sTmp += cDialogExtraDataClose;
282 GetExtraData() = sTmp;
286 void SwAsciiFilterDlg::SetCRLF( LineEnd eEnd )
288 switch( eEnd )
290 case LINEEND_CR: m_pCR_RB->Check(); break;
291 case LINEEND_CRLF: m_pCRLF_RB->Check(); break;
292 case LINEEND_LF: m_pLF_RB->Check(); break;
296 LineEnd SwAsciiFilterDlg::GetCRLF() const
298 LineEnd eEnd;
299 if( m_pCR_RB->IsChecked() )
300 eEnd = LINEEND_CR;
301 else if( m_pLF_RB->IsChecked() )
302 eEnd = LINEEND_LF;
303 else
304 eEnd = LINEEND_CRLF;
305 return eEnd;
308 IMPL_LINK( SwAsciiFilterDlg, CharSetSelHdl, SvxTextEncodingBox*, pBox )
310 LineEnd eOldEnd = GetCRLF(), eEnd = (LineEnd)-1;
311 LanguageType nLng = m_pFontLB->IsVisible()
312 ? m_pLanguageLB->GetSelectLanguage()
313 : LANGUAGE_SYSTEM,
314 nOldLng = nLng;
316 rtl_TextEncoding nChrSet = pBox->GetSelectTextEncoding();
317 if( nChrSet == osl_getThreadTextEncoding() )
318 eEnd = GetSystemLineEnd();
319 else
321 switch( nChrSet )
323 case RTL_TEXTENCODING_MS_1252:
324 #ifdef UNX
325 eEnd = LINEEND_LF;
326 #else
327 eEnd = LINEEND_CRLF; // ANSI
328 #endif
329 break;
331 case RTL_TEXTENCODING_APPLE_ROMAN: // MAC
332 eEnd = LINEEND_CR;
333 break;
335 case RTL_TEXTENCODING_IBM_850: // DOS
336 eEnd = LINEEND_CRLF;
337 break;
339 case RTL_TEXTENCODING_APPLE_ARABIC:
340 case RTL_TEXTENCODING_APPLE_CENTEURO:
341 case RTL_TEXTENCODING_APPLE_CROATIAN:
342 case RTL_TEXTENCODING_APPLE_CYRILLIC:
343 case RTL_TEXTENCODING_APPLE_DEVANAGARI:
344 case RTL_TEXTENCODING_APPLE_FARSI:
345 case RTL_TEXTENCODING_APPLE_GREEK:
346 case RTL_TEXTENCODING_APPLE_GUJARATI:
347 case RTL_TEXTENCODING_APPLE_GURMUKHI:
348 case RTL_TEXTENCODING_APPLE_HEBREW:
349 case RTL_TEXTENCODING_APPLE_ICELAND:
350 case RTL_TEXTENCODING_APPLE_ROMANIAN:
351 case RTL_TEXTENCODING_APPLE_THAI:
352 case RTL_TEXTENCODING_APPLE_TURKISH:
353 case RTL_TEXTENCODING_APPLE_UKRAINIAN:
354 case RTL_TEXTENCODING_APPLE_CHINSIMP:
355 case RTL_TEXTENCODING_APPLE_CHINTRAD:
356 case RTL_TEXTENCODING_APPLE_JAPANESE:
357 case RTL_TEXTENCODING_APPLE_KOREAN:
358 eEnd = LINEEND_CR;
359 break;
363 m_bSaveLineStatus = false;
364 if( eEnd != (LineEnd)-1 ) // changed?
366 if( eOldEnd != eEnd )
367 SetCRLF( eEnd );
369 else
371 // restore old user choise (not the automatic!)
372 m_pCRLF_RB->Check( m_pCRLF_RB->GetSavedValue() );
373 m_pCR_RB->Check( m_pCR_RB->GetSavedValue() );
374 m_pLF_RB->Check( m_pLF_RB->GetSavedValue() );
376 m_bSaveLineStatus = true;
378 if( nOldLng != nLng && m_pFontLB->IsVisible() )
379 m_pLanguageLB->SelectLanguage( nLng );
381 return 0;
384 IMPL_LINK( SwAsciiFilterDlg, LineEndHdl, RadioButton*, pBtn )
386 if( m_bSaveLineStatus )
387 pBtn->SaveValue();
388 return 0;
392 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */