1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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
,
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");
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)
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;
121 aOpt
.SetParaFlags( LINEEND_CRLF
);
125 aOpt
.SetParaFlags( LINEEND_CR
);
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() )
143 sal_uInt16 nWhich
= GetWhichOfScript( RES_CHRATR_LANGUAGE
, nAppScriptType
);
144 aOpt
.SetLanguage( ((SvxLanguageItem
&)pDoc
->
145 GetDefault( nWhich
)).GetLanguage());
149 SvtLinguOptions aLinguOpt
;
150 SvtLinguConfig().GetOptions( aLinguOpt
);
151 switch(nAppScriptType
)
153 case SCRIPTTYPE_ASIAN
:
154 aOpt
.SetLanguage(MsLangId::resolveSystemLanguageByScriptType(aLinguOpt
.nDefaultLanguage_CJK
, SCRIPTTYPE_ASIAN
));
156 case SCRIPTTYPE_COMPLEX
:
157 aOpt
.SetLanguage(MsLangId::resolveSystemLanguageByScriptType(aLinguOpt
.nDefaultLanguage_CTL
, SCRIPTTYPE_COMPLEX
));
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;
175 SfxItemSet
* pSet
= new SfxItemSet( rDocSh
.GetPool(),
176 SID_PRINTER_NOTFOUND_WARN
, SID_PRINTER_NOTFOUND_WARN
,
177 SID_PRINTER_CHANGESTODOC
, SID_PRINTER_CHANGESTODOC
,
179 pPrt
= new SfxPrinter( pSet
);
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() );
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() );
216 // hide the unused Controls for Export
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();
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
263 rOptions
.WriteUserData( sData
);
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());
281 sTmp
+= cDialogExtraDataClose
;
282 GetExtraData() = sTmp
;
286 void SwAsciiFilterDlg::SetCRLF( LineEnd 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
299 if( m_pCR_RB
->IsChecked() )
301 else if( m_pLF_RB
->IsChecked() )
308 IMPL_LINK( SwAsciiFilterDlg
, CharSetSelHdl
, SvxTextEncodingBox
*, pBox
)
310 LineEnd eOldEnd
= GetCRLF(), eEnd
= (LineEnd
)-1;
311 LanguageType nLng
= m_pFontLB
->IsVisible()
312 ? m_pLanguageLB
->GetSelectLanguage()
316 rtl_TextEncoding nChrSet
= pBox
->GetSelectTextEncoding();
317 if( nChrSet
== osl_getThreadTextEncoding() )
318 eEnd
= GetSystemLineEnd();
323 case RTL_TEXTENCODING_MS_1252
:
327 eEnd
= LINEEND_CRLF
; // ANSI
331 case RTL_TEXTENCODING_APPLE_ROMAN
: // MAC
335 case RTL_TEXTENCODING_IBM_850
: // DOS
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
:
363 m_bSaveLineStatus
= false;
364 if( eEnd
!= (LineEnd
)-1 ) // changed?
366 if( eOldEnd
!= eEnd
)
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
);
384 IMPL_LINK( SwAsciiFilterDlg
, LineEndHdl
, RadioButton
*, pBtn
)
386 if( m_bSaveLineStatus
)
392 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */