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