xref: /aoo41x/main/sw/source/ui/dialog/ascfldlg.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sw.hxx"
30 #ifdef SW_DLLIMPLEMENTATION
31 #undef SW_DLLIMPLEMENTATION
32 #endif
33 #include <hintids.hxx>
34 #include <rtl/textenc.h>
35 #include <i18npool/mslangid.hxx>
36 #include <com/sun/star/i18n/ScriptType.hpp>
37 #include <unotools/lingucfg.hxx>
38 #include <fontcfg.hxx>
39 #include <swmodule.hxx>
40 #include <editeng/unolingu.hxx>
41 #include <sfx2/printer.hxx>
42 #include <editeng/flstitem.hxx>
43 #include <svx/dlgutil.hxx>
44 #include <editeng/fontitem.hxx>
45 #include <editeng/langitem.hxx>
46 #include <editeng/scripttypeitem.hxx>
47 #include <swtypes.hxx>
48 #include <ascfldlg.hxx>
49 #include <shellio.hxx>
50 #include <docsh.hxx>
51 #include <doc.hxx>
52 #include <errhdl.hxx>
53 
54 #ifndef _DIALOG_HRC
55 #include <dialog.hrc>
56 #endif
57 #ifndef _ASCFLDLG_HRC
58 #include <ascfldlg.hrc>
59 #endif
60 
61 #include "vcl/metric.hxx"
62 
63 
64 using namespace ::com::sun::star;
65 
66 const sal_Unicode cDialogExtraDataClose = '}';
67 const char __FAR_DATA sDialogImpExtraData[] = "EncImpDlg:{";
68 const char __FAR_DATA sDialogExpExtraData[] = "EncExpDlg:{";
69 const sal_uInt16 nDialogExtraDataLen = 11;	  	// 12345678901
70 
71 SwAsciiFilterDlg::SwAsciiFilterDlg( Window* pParent, SwDocShell& rDocSh,
72 									SvStream* pStream )
73 	: SfxModalDialog( pParent, SW_RES( DLG_ASCII_FILTER )),
74     aFL( this, SW_RES( FL_1 )),
75 	aCharSetFT( this, SW_RES( FT_CHARSET )),
76 	aCharSetLB( this, SW_RES( LB_CHARSET )),
77 	aFontFT( this, SW_RES( FT_FONT )),
78 	aFontLB( this, SW_RES( LB_FONT )),
79 	aLanguageFT( this, SW_RES( FT_LANGUAGE )),
80 	aLanguageLB( this, SW_RES( LB_LANGUAGE )),
81 	aCRLF_FT( this, SW_RES( FT_CRLF )),
82 	aCRLF_RB( this, SW_RES( RB_CRLF )),
83 	aCR_RB( this, SW_RES( RB_CR )),
84 	aLF_RB( this, SW_RES( RB_LF )),
85 	aOkPB( this, SW_RES( PB_OK )),
86 	aCancelPB( this, SW_RES( PB_CANCEL )),
87 	aHelpPB( this, SW_RES( PB_HELP )),
88 	sSystemCharSet( SW_RES( STR_SYS_CHARSET )),
89 	bSaveLineStatus( sal_True )
90 {
91 	FreeResource();
92 
93 	SwAsciiOptions aOpt;
94 	{
95 		const String& rFindNm = String::CreateFromAscii(
96 									pStream ? sDialogImpExtraData
97 									  		: sDialogExpExtraData);
98 		sal_uInt16 nEnd, nStt = GetExtraData().Search( rFindNm );
99 		if( STRING_NOTFOUND != nStt )
100 		{
101 			nStt += nDialogExtraDataLen;
102 			nEnd = GetExtraData().Search( cDialogExtraDataClose, nStt );
103 			if( STRING_NOTFOUND != nEnd )
104 			{
105 				aOpt.ReadUserData( GetExtraData().Copy( nStt, nEnd - nStt ));
106 				nStt -= nDialogExtraDataLen;
107 				GetExtraData().Erase( nStt, nEnd - nStt + 1 );
108 			}
109 		}
110 	}
111 
112 	// read the first chars and check the charset, (language - with L&H)
113 	if( pStream )
114 	{
115 		char aBuffer[ 4098 ];
116 		sal_uLong nOldPos = pStream->Tell();
117 		sal_uLong nBytesRead = pStream->Read( aBuffer, 4096 );
118 		pStream->Seek( nOldPos );
119 
120 		if( nBytesRead <= 4096 )
121 		{
122 			aBuffer[ nBytesRead ] = '0';
123 			aBuffer[ nBytesRead+1 ] = '0';
124 			if( 0 != ( nBytesRead & 0x00000001 ) )
125 				aBuffer[ nBytesRead + 2 ] = '0';
126 		}
127 
128 		sal_Bool bCR = sal_False, bLF = sal_False, bNoNormalChar = sal_False,
129 			bNullChar = sal_False;
130 		for( sal_uInt16 nCnt = 0; nCnt < nBytesRead; ++nCnt )
131 			switch( aBuffer[ nCnt ] )
132 			{
133 				case 0x0:	bNullChar = sal_True; break;
134 				case 0xA:	bLF = sal_True; break;
135 				case 0xD:	bCR = sal_True; break;
136 				case 0xC:
137 				case 0x1A:
138 				case 0x9:	break;
139 				default:
140 					if( 0x20 > aBuffer[ nCnt ] )
141 						bNoNormalChar = sal_True;
142 			}
143 
144 		if( !bNullChar )
145 		{
146 			if( bCR )
147 			{
148 				if( bLF )
149 				{
150 					aOpt.SetParaFlags( LINEEND_CRLF );
151 // have to check if of CharSet is type of ANSI
152 //					aOpt.SetCharSet( CHARSET_ANSI );
153 				}
154 				else
155 				{
156 					aOpt.SetParaFlags( LINEEND_CR );
157 // have to check if CharSet is type of MAC
158 //					aOpt.SetCharSet( CHARSET_MAC );
159 				}
160 			}
161 			else if( bLF )
162 			{
163 				aOpt.SetParaFlags( LINEEND_LF );
164 // have to check if of CharSet is type of ANSI
165 //				aOpt.SetCharSet( CHARSET_ANSI );
166 			}
167 		}
168 
169         SwDoc* pDoc = rDocSh.GetDoc();
170 
171         sal_uInt16 nAppScriptType = GetI18NScriptTypeOfLanguage( (sal_uInt16)GetAppLanguage() );
172         {
173 			sal_Bool bDelPrinter = sal_False;
174             SfxPrinter* pPrt = pDoc ? pDoc->getPrinter(false) : 0;
175 			if( !pPrt )
176 			{
177                 SfxItemSet* pSet = new SfxItemSet( rDocSh.GetPool(),
178 							SID_PRINTER_NOTFOUND_WARN, SID_PRINTER_NOTFOUND_WARN,
179 							SID_PRINTER_CHANGESTODOC, SID_PRINTER_CHANGESTODOC,
180 							0 );
181 				pPrt = new SfxPrinter( pSet );
182 				bDelPrinter = sal_True;
183 			}
184 
185 
186 			// get the set of disctinct available family names
187 			std::set< String > aFontNames;
188 			int nFontNames = pPrt->GetDevFontCount();
189 			for( int i = 0; i < nFontNames; i++ )
190 			{
191 			    FontInfo aInf( pPrt->GetDevFont( i ) );
192 			    aFontNames.insert( aInf.GetName() );
193 			}
194 
195 			// insert to listbox
196 			for( std::set< String >::const_iterator it = aFontNames.begin();
197 			     it != aFontNames.end(); ++it )
198 			{
199 			    aFontLB.InsertEntry( *it );
200 			}
201 
202 			if( !aOpt.GetFontName().Len() )
203             {
204                 if(pDoc)
205                 {
206                     sal_uInt16 nFontRes = RES_CHRATR_FONT;
207                     if(SCRIPTTYPE_ASIAN == nAppScriptType)
208                         nFontRes = RES_CHRATR_CJK_FONT;
209                     else if(SCRIPTTYPE_COMPLEX == nAppScriptType)
210                         nFontRes = RES_CHRATR_CTL_FONT;
211 
212                     aOpt.SetFontName( ((SvxFontItem&)pDoc->GetDefault(
213                                     nFontRes )).GetFamilyName() );
214                 }
215                 else
216                 {
217                     sal_uInt16 nFontType = FONT_STANDARD;
218                     if(SCRIPTTYPE_ASIAN == nAppScriptType)
219                         nFontType = FONT_STANDARD_CJK;
220                     else if(SCRIPTTYPE_COMPLEX == nAppScriptType)
221                         nFontType = FONT_STANDARD_CTL;
222                     aOpt.SetFontName(SW_MOD()->GetStdFontConfig()->GetFontFor(nFontType));
223                 }
224             }
225 			aFontLB.SelectEntry( aOpt.GetFontName() );
226 
227 			if( bDelPrinter )
228 				delete pPrt;
229 		}
230 
231 		// initialisiere Sprache
232 		{
233             if( !aOpt.GetLanguage() )
234 			{
235                 if(pDoc)
236                 {
237                     sal_uInt16 nWhich = GetWhichOfScript( RES_CHRATR_LANGUAGE, nAppScriptType);
238                     aOpt.SetLanguage( ((SvxLanguageItem&)pDoc->
239                                 GetDefault( nWhich )).GetLanguage());
240                 }
241                 else
242                 {
243                     SvtLinguOptions aLinguOpt;
244                     SvtLinguConfig().GetOptions( aLinguOpt );
245                     switch(nAppScriptType)
246                     {
247                         case SCRIPTTYPE_ASIAN:
248                             aOpt.SetLanguage(MsLangId::resolveSystemLanguageByScriptType(aLinguOpt.nDefaultLanguage_CJK, SCRIPTTYPE_ASIAN));
249                         break;
250                         case SCRIPTTYPE_COMPLEX:
251                             aOpt.SetLanguage(MsLangId::resolveSystemLanguageByScriptType(aLinguOpt.nDefaultLanguage_CTL, SCRIPTTYPE_COMPLEX));
252                         break;
253                         //SCRIPTTYPE_LATIN:
254                         default:
255                             aOpt.SetLanguage(MsLangId::resolveSystemLanguageByScriptType(aLinguOpt.nDefaultLanguage, SCRIPTTYPE_LATIN));
256                     }
257                 }
258 			}
259 
260 			aLanguageLB.SetLanguageList( LANG_LIST_ALL, sal_True, sal_False );
261 			aLanguageLB.SelectLanguage( aOpt.GetLanguage() );
262 		}
263 	}
264 	else
265 	{
266 		// hide the used Control for the Export and move the
267 		// other behind the charset controls
268 		aFontFT.Hide();
269 		aFontLB.Hide();
270 		aLanguageFT.Hide();
271 		aLanguageLB.Hide();
272 
273         long nY = aFontFT.GetPosPixel().Y() + 1;
274 		Point aPos( aCRLF_FT.GetPosPixel() );	aPos.Y() = nY;
275 		aCRLF_FT.SetPosPixel( aPos );
276 
277 		aPos = aCRLF_RB.GetPosPixel();	aPos.Y() = nY;
278 		aCRLF_RB.SetPosPixel( aPos );
279 
280 		aPos = aCR_RB.GetPosPixel();	aPos.Y() = nY;
281 		aCR_RB.SetPosPixel( aPos );
282 
283 		aPos = aLF_RB.GetPosPixel();	aPos.Y() = nY;
284 		aLF_RB.SetPosPixel( aPos );
285 
286         Size aSize = GetSizePixel();
287         Size aTmpSz( 6, 6 );
288         aTmpSz = LogicToPixel(aTmpSz, MAP_APPFONT);
289         aSize.Height() = aHelpPB.GetPosPixel().Y() +
290                          aHelpPB.GetSizePixel().Height() + aTmpSz.Height();
291         SetSizePixel( aSize );
292 	}
293 
294 	// initialisiere Zeichensatz
295 	aCharSetLB.FillFromTextEncodingTable( pStream != NULL );
296 	aCharSetLB.SelectTextEncoding( aOpt.GetCharSet()  );
297 
298 	aCharSetLB.SetSelectHdl( LINK( this, SwAsciiFilterDlg, CharSetSelHdl ));
299 	aCRLF_RB.SetToggleHdl( LINK( this, SwAsciiFilterDlg, LineEndHdl ));
300 	aLF_RB.SetToggleHdl( LINK( this, SwAsciiFilterDlg, LineEndHdl ));
301 	aCR_RB.SetToggleHdl( LINK( this, SwAsciiFilterDlg, LineEndHdl ));
302 
303 	SetCRLF( aOpt.GetParaFlags() );
304 
305 	aCRLF_RB.SaveValue();
306 	aLF_RB.SaveValue();
307 	aCR_RB.SaveValue();
308 }
309 
310 
311 SwAsciiFilterDlg::~SwAsciiFilterDlg()
312 {
313 }
314 
315 
316 void SwAsciiFilterDlg::FillOptions( SwAsciiOptions& rOptions )
317 {
318 	sal_uLong nCCode = aCharSetLB.GetSelectTextEncoding();
319 	String sFont;
320 	sal_uLong nLng = 0;
321 	if( aFontLB.IsVisible() )
322 	{
323 		sFont = aFontLB.GetSelectEntry();
324 		nLng = (sal_uLong)aLanguageLB.GetSelectLanguage();
325 	}
326 
327 	rOptions.SetFontName( sFont );
328 	rOptions.SetCharSet( rtl_TextEncoding( nCCode ) );
329 	rOptions.SetLanguage( sal_uInt16( nLng ) );
330 	rOptions.SetParaFlags( GetCRLF() );
331 
332 	// JP: Task #71802# save the user settings
333 	String sData;
334 	rOptions.WriteUserData( sData );
335 	if( sData.Len() )
336 	{
337 		const String& rFindNm = String::CreateFromAscii(
338 									aFontLB.IsVisible() ? sDialogImpExtraData
339 									  		: sDialogExpExtraData);
340 		sal_uInt16 nEnd, nStt = GetExtraData().Search( rFindNm );
341 		if( STRING_NOTFOUND != nStt )
342 		{
343 			// called twice, so remove "old" settings
344 			nEnd = GetExtraData().Search( cDialogExtraDataClose,
345 											nStt + nDialogExtraDataLen );
346 			if( STRING_NOTFOUND != nEnd )
347 				GetExtraData().Erase( nStt, nEnd - nStt + 1 );
348 		}
349 		String sTmp(GetExtraData());
350 		sTmp += rFindNm;
351 		sTmp += sData;
352 		sTmp += cDialogExtraDataClose;
353 		GetExtraData() = sTmp;
354 	}
355 }
356 
357 void SwAsciiFilterDlg::SetCRLF( LineEnd eEnd )
358 {
359 	switch( eEnd )
360 	{
361 	case LINEEND_CR: 	aCR_RB.Check();		break;
362 	case LINEEND_CRLF: 	aCRLF_RB.Check();	break;
363 	case LINEEND_LF: 	aLF_RB.Check();		break;
364 	}
365 }
366 
367 LineEnd SwAsciiFilterDlg::GetCRLF() const
368 {
369 	LineEnd eEnd;
370 	if( aCR_RB.IsChecked() )
371 		eEnd = LINEEND_CR;
372 	else if( aLF_RB.IsChecked() )
373 		eEnd = LINEEND_LF;
374 	else
375 		eEnd = LINEEND_CRLF;
376 	return eEnd;
377 }
378 
379 IMPL_LINK( SwAsciiFilterDlg, CharSetSelHdl, SvxTextEncodingBox*, pBox )
380 {
381 	LineEnd eOldEnd = GetCRLF(), eEnd = (LineEnd)-1;
382     LanguageType nLng = aFontLB.IsVisible()
383                     ? aLanguageLB.GetSelectLanguage()
384                     : LANGUAGE_SYSTEM,
385                 nOldLng = nLng;
386 
387 	rtl_TextEncoding nChrSet = pBox->GetSelectTextEncoding();
388 	if( nChrSet == gsl_getSystemTextEncoding() )
389 		eEnd = GetSystemLineEnd();
390 	else
391 	{
392 		switch( nChrSet )
393 		{
394 		case RTL_TEXTENCODING_MS_1252:
395 #ifdef UNX
396 			eEnd = LINEEND_LF;
397 #else
398 			eEnd = LINEEND_CRLF;				// ANSI
399 #endif
400 			break;
401 
402 		case RTL_TEXTENCODING_APPLE_ROMAN:		// MAC
403 			eEnd = LINEEND_CR;
404 			break;
405 
406 		case RTL_TEXTENCODING_IBM_850:			// DOS
407 			eEnd = LINEEND_CRLF;
408 			break;
409 
410 		case RTL_TEXTENCODING_APPLE_ARABIC:
411 		case RTL_TEXTENCODING_APPLE_CENTEURO:
412 		case RTL_TEXTENCODING_APPLE_CROATIAN:
413 		case RTL_TEXTENCODING_APPLE_CYRILLIC:
414 		case RTL_TEXTENCODING_APPLE_DEVANAGARI:
415 		case RTL_TEXTENCODING_APPLE_FARSI:
416 		case RTL_TEXTENCODING_APPLE_GREEK:
417 		case RTL_TEXTENCODING_APPLE_GUJARATI:
418 		case RTL_TEXTENCODING_APPLE_GURMUKHI:
419 		case RTL_TEXTENCODING_APPLE_HEBREW:
420 		case RTL_TEXTENCODING_APPLE_ICELAND:
421 		case RTL_TEXTENCODING_APPLE_ROMANIAN:
422 		case RTL_TEXTENCODING_APPLE_THAI:
423 		case RTL_TEXTENCODING_APPLE_TURKISH:
424 		case RTL_TEXTENCODING_APPLE_UKRAINIAN:
425 		case RTL_TEXTENCODING_APPLE_CHINSIMP:
426 		case RTL_TEXTENCODING_APPLE_CHINTRAD:
427 		case RTL_TEXTENCODING_APPLE_JAPANESE:
428 		case RTL_TEXTENCODING_APPLE_KOREAN:
429 			eEnd = LINEEND_CR;
430 			break;
431 		}
432 	}
433 
434 	bSaveLineStatus = sal_False;
435 	if( eEnd != (LineEnd)-1 )		// changed?
436 	{
437 		if( eOldEnd != eEnd )
438 			SetCRLF( eEnd );
439 	}
440 	else
441 	{
442 		// restore old user choise (not the automatic!)
443 		aCRLF_RB.Check( aCRLF_RB.GetSavedValue() );
444 		aCR_RB.Check( aCR_RB.GetSavedValue() );
445 		aLF_RB.Check( aLF_RB.GetSavedValue() );
446 	}
447 	bSaveLineStatus = sal_True;
448 
449 	if( nOldLng != nLng && aFontLB.IsVisible() )
450 		aLanguageLB.SelectLanguage( nLng );
451 
452 	return 0;
453 }
454 
455 IMPL_LINK( SwAsciiFilterDlg, LineEndHdl, RadioButton*, pBtn )
456 {
457 	if( bSaveLineStatus )
458 		pBtn->SaveValue();
459 	return 0;
460 }
461 
462 
463