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
SwAsciiFilterDlg(Window * pParent,SwDocShell & rDocSh,SvStream * pStream)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 }
121
122 sal_Bool bCR = sal_False, bLF = sal_False, bNoNormalChar = sal_False,
123 bNullChar = sal_False;
124 for( sal_uInt16 nCnt = 0; nCnt < nBytesRead; ++nCnt )
125 switch( aBuffer[ nCnt ] )
126 {
127 case 0x0: bNullChar = sal_True; break;
128 case 0xA: bLF = sal_True; break;
129 case 0xD: bCR = sal_True; break;
130 case 0xC:
131 case 0x1A:
132 case 0x9: break;
133 default:
134 if( 0x20 > aBuffer[ nCnt ] )
135 bNoNormalChar = sal_True;
136 }
137
138 if( !bNullChar )
139 {
140 if( bCR )
141 {
142 if( bLF )
143 {
144 aOpt.SetParaFlags( LINEEND_CRLF );
145 // have to check if of CharSet is type of ANSI
146 // aOpt.SetCharSet( CHARSET_ANSI );
147 }
148 else
149 {
150 aOpt.SetParaFlags( LINEEND_CR );
151 // have to check if CharSet is type of MAC
152 // aOpt.SetCharSet( CHARSET_MAC );
153 }
154 }
155 else if( bLF )
156 {
157 aOpt.SetParaFlags( LINEEND_LF );
158 // have to check if of CharSet is type of ANSI
159 // aOpt.SetCharSet( CHARSET_ANSI );
160 }
161 }
162
163 SwDoc* pDoc = rDocSh.GetDoc();
164
165 sal_uInt16 nAppScriptType = GetI18NScriptTypeOfLanguage( (sal_uInt16)GetAppLanguage() );
166 {
167 sal_Bool bDelPrinter = sal_False;
168 SfxPrinter* pPrt = pDoc ? pDoc->getPrinter(false) : 0;
169 if( !pPrt )
170 {
171 SfxItemSet* pSet = new SfxItemSet( rDocSh.GetPool(),
172 SID_PRINTER_NOTFOUND_WARN, SID_PRINTER_NOTFOUND_WARN,
173 SID_PRINTER_CHANGESTODOC, SID_PRINTER_CHANGESTODOC,
174 0 );
175 pPrt = new SfxPrinter( pSet );
176 bDelPrinter = sal_True;
177 }
178
179
180 // get the set of disctinct available family names
181 std::set< String > aFontNames;
182 int nFontNames = pPrt->GetDevFontCount();
183 for( int i = 0; i < nFontNames; i++ )
184 {
185 FontInfo aInf( pPrt->GetDevFont( i ) );
186 aFontNames.insert( aInf.GetName() );
187 }
188
189 // insert to listbox
190 for( std::set< String >::const_iterator it = aFontNames.begin();
191 it != aFontNames.end(); ++it )
192 {
193 aFontLB.InsertEntry( *it );
194 }
195
196 if( !aOpt.GetFontName().Len() )
197 {
198 if(pDoc)
199 {
200 sal_uInt16 nFontRes = RES_CHRATR_FONT;
201 if(SCRIPTTYPE_ASIAN == nAppScriptType)
202 nFontRes = RES_CHRATR_CJK_FONT;
203 else if(SCRIPTTYPE_COMPLEX == nAppScriptType)
204 nFontRes = RES_CHRATR_CTL_FONT;
205
206 aOpt.SetFontName( ((SvxFontItem&)pDoc->GetDefault(
207 nFontRes )).GetFamilyName() );
208 }
209 else
210 {
211 sal_uInt16 nFontType = FONT_STANDARD;
212 if(SCRIPTTYPE_ASIAN == nAppScriptType)
213 nFontType = FONT_STANDARD_CJK;
214 else if(SCRIPTTYPE_COMPLEX == nAppScriptType)
215 nFontType = FONT_STANDARD_CTL;
216 aOpt.SetFontName(SW_MOD()->GetStdFontConfig()->GetFontFor(nFontType));
217 }
218 }
219 aFontLB.SelectEntry( aOpt.GetFontName() );
220
221 if( bDelPrinter )
222 delete pPrt;
223 }
224
225 // initialisiere Sprache
226 {
227 if( !aOpt.GetLanguage() )
228 {
229 if(pDoc)
230 {
231 sal_uInt16 nWhich = GetWhichOfScript( RES_CHRATR_LANGUAGE, nAppScriptType);
232 aOpt.SetLanguage( ((SvxLanguageItem&)pDoc->
233 GetDefault( nWhich )).GetLanguage());
234 }
235 else
236 {
237 SvtLinguOptions aLinguOpt;
238 SvtLinguConfig().GetOptions( aLinguOpt );
239 switch(nAppScriptType)
240 {
241 case SCRIPTTYPE_ASIAN:
242 aOpt.SetLanguage(MsLangId::resolveSystemLanguageByScriptType(aLinguOpt.nDefaultLanguage_CJK, SCRIPTTYPE_ASIAN));
243 break;
244 case SCRIPTTYPE_COMPLEX:
245 aOpt.SetLanguage(MsLangId::resolveSystemLanguageByScriptType(aLinguOpt.nDefaultLanguage_CTL, SCRIPTTYPE_COMPLEX));
246 break;
247 //SCRIPTTYPE_LATIN:
248 default:
249 aOpt.SetLanguage(MsLangId::resolveSystemLanguageByScriptType(aLinguOpt.nDefaultLanguage, SCRIPTTYPE_LATIN));
250 }
251 }
252 }
253
254 aLanguageLB.SetLanguageList( LANG_LIST_ALL, sal_True, sal_False );
255 aLanguageLB.SelectLanguage( aOpt.GetLanguage() );
256 }
257 }
258 else
259 {
260 // hide the used Control for the Export and move the
261 // other behind the charset controls
262 aFontFT.Hide();
263 aFontLB.Hide();
264 aLanguageFT.Hide();
265 aLanguageLB.Hide();
266
267 long nY = aFontFT.GetPosPixel().Y() + 1;
268 Point aPos( aCRLF_FT.GetPosPixel() ); aPos.Y() = nY;
269 aCRLF_FT.SetPosPixel( aPos );
270
271 aPos = aCRLF_RB.GetPosPixel(); aPos.Y() = nY;
272 aCRLF_RB.SetPosPixel( aPos );
273
274 aPos = aCR_RB.GetPosPixel(); aPos.Y() = nY;
275 aCR_RB.SetPosPixel( aPos );
276
277 aPos = aLF_RB.GetPosPixel(); aPos.Y() = nY;
278 aLF_RB.SetPosPixel( aPos );
279
280 Size aSize = GetSizePixel();
281 Size aTmpSz( 6, 6 );
282 aTmpSz = LogicToPixel(aTmpSz, MAP_APPFONT);
283 aSize.Height() = aHelpPB.GetPosPixel().Y() +
284 aHelpPB.GetSizePixel().Height() + aTmpSz.Height();
285 SetSizePixel( aSize );
286 }
287
288 // initialisiere Zeichensatz
289 aCharSetLB.FillFromTextEncodingTable( pStream != NULL );
290 aCharSetLB.SelectTextEncoding( aOpt.GetCharSet() );
291
292 aCharSetLB.SetSelectHdl( LINK( this, SwAsciiFilterDlg, CharSetSelHdl ));
293 aCRLF_RB.SetToggleHdl( LINK( this, SwAsciiFilterDlg, LineEndHdl ));
294 aLF_RB.SetToggleHdl( LINK( this, SwAsciiFilterDlg, LineEndHdl ));
295 aCR_RB.SetToggleHdl( LINK( this, SwAsciiFilterDlg, LineEndHdl ));
296
297 SetCRLF( aOpt.GetParaFlags() );
298
299 aCRLF_RB.SaveValue();
300 aLF_RB.SaveValue();
301 aCR_RB.SaveValue();
302 }
303
304
~SwAsciiFilterDlg()305 SwAsciiFilterDlg::~SwAsciiFilterDlg()
306 {
307 }
308
309
FillOptions(SwAsciiOptions & rOptions)310 void SwAsciiFilterDlg::FillOptions( SwAsciiOptions& rOptions )
311 {
312 sal_uLong nCCode = aCharSetLB.GetSelectTextEncoding();
313 String sFont;
314 sal_uLong nLng = 0;
315 if( aFontLB.IsVisible() )
316 {
317 sFont = aFontLB.GetSelectEntry();
318 nLng = (sal_uLong)aLanguageLB.GetSelectLanguage();
319 }
320
321 rOptions.SetFontName( sFont );
322 rOptions.SetCharSet( rtl_TextEncoding( nCCode ) );
323 rOptions.SetLanguage( sal_uInt16( nLng ) );
324 rOptions.SetParaFlags( GetCRLF() );
325
326 // JP: Task #71802# save the user settings
327 String sData;
328 rOptions.WriteUserData( sData );
329 if( sData.Len() )
330 {
331 const String& rFindNm = String::CreateFromAscii(
332 aFontLB.IsVisible() ? sDialogImpExtraData
333 : sDialogExpExtraData);
334 sal_uInt16 nEnd, nStt = GetExtraData().Search( rFindNm );
335 if( STRING_NOTFOUND != nStt )
336 {
337 // called twice, so remove "old" settings
338 nEnd = GetExtraData().Search( cDialogExtraDataClose,
339 nStt + nDialogExtraDataLen );
340 if( STRING_NOTFOUND != nEnd )
341 GetExtraData().Erase( nStt, nEnd - nStt + 1 );
342 }
343 String sTmp(GetExtraData());
344 sTmp += rFindNm;
345 sTmp += sData;
346 sTmp += cDialogExtraDataClose;
347 GetExtraData() = sTmp;
348 }
349 }
350
SetCRLF(LineEnd eEnd)351 void SwAsciiFilterDlg::SetCRLF( LineEnd eEnd )
352 {
353 switch( eEnd )
354 {
355 case LINEEND_CR: aCR_RB.Check(); break;
356 case LINEEND_CRLF: aCRLF_RB.Check(); break;
357 case LINEEND_LF: aLF_RB.Check(); break;
358 }
359 }
360
GetCRLF() const361 LineEnd SwAsciiFilterDlg::GetCRLF() const
362 {
363 LineEnd eEnd;
364 if( aCR_RB.IsChecked() )
365 eEnd = LINEEND_CR;
366 else if( aLF_RB.IsChecked() )
367 eEnd = LINEEND_LF;
368 else
369 eEnd = LINEEND_CRLF;
370 return eEnd;
371 }
372
IMPL_LINK(SwAsciiFilterDlg,CharSetSelHdl,SvxTextEncodingBox *,pBox)373 IMPL_LINK( SwAsciiFilterDlg, CharSetSelHdl, SvxTextEncodingBox*, pBox )
374 {
375 LineEnd eOldEnd = GetCRLF(), eEnd = (LineEnd)-1;
376 LanguageType nLng = aFontLB.IsVisible()
377 ? aLanguageLB.GetSelectLanguage()
378 : LANGUAGE_SYSTEM,
379 nOldLng = nLng;
380
381 rtl_TextEncoding nChrSet = pBox->GetSelectTextEncoding();
382 if( nChrSet == gsl_getSystemTextEncoding() )
383 eEnd = GetSystemLineEnd();
384 else
385 {
386 switch( nChrSet )
387 {
388 case RTL_TEXTENCODING_MS_1252:
389 #ifdef UNX
390 eEnd = LINEEND_LF;
391 #else
392 eEnd = LINEEND_CRLF; // ANSI
393 #endif
394 break;
395
396 case RTL_TEXTENCODING_APPLE_ROMAN: // MAC
397 eEnd = LINEEND_CR;
398 break;
399
400 case RTL_TEXTENCODING_IBM_850: // DOS
401 eEnd = LINEEND_CRLF;
402 break;
403
404 case RTL_TEXTENCODING_APPLE_ARABIC:
405 case RTL_TEXTENCODING_APPLE_CENTEURO:
406 case RTL_TEXTENCODING_APPLE_CROATIAN:
407 case RTL_TEXTENCODING_APPLE_CYRILLIC:
408 case RTL_TEXTENCODING_APPLE_DEVANAGARI:
409 case RTL_TEXTENCODING_APPLE_FARSI:
410 case RTL_TEXTENCODING_APPLE_GREEK:
411 case RTL_TEXTENCODING_APPLE_GUJARATI:
412 case RTL_TEXTENCODING_APPLE_GURMUKHI:
413 case RTL_TEXTENCODING_APPLE_HEBREW:
414 case RTL_TEXTENCODING_APPLE_ICELAND:
415 case RTL_TEXTENCODING_APPLE_ROMANIAN:
416 case RTL_TEXTENCODING_APPLE_THAI:
417 case RTL_TEXTENCODING_APPLE_TURKISH:
418 case RTL_TEXTENCODING_APPLE_UKRAINIAN:
419 case RTL_TEXTENCODING_APPLE_CHINSIMP:
420 case RTL_TEXTENCODING_APPLE_CHINTRAD:
421 case RTL_TEXTENCODING_APPLE_JAPANESE:
422 case RTL_TEXTENCODING_APPLE_KOREAN:
423 eEnd = LINEEND_CR;
424 break;
425 }
426 }
427
428 bSaveLineStatus = sal_False;
429 if( eEnd != (LineEnd)-1 ) // changed?
430 {
431 if( eOldEnd != eEnd )
432 SetCRLF( eEnd );
433 }
434 else
435 {
436 // restore old user choise (not the automatic!)
437 aCRLF_RB.Check( aCRLF_RB.GetSavedValue() );
438 aCR_RB.Check( aCR_RB.GetSavedValue() );
439 aLF_RB.Check( aLF_RB.GetSavedValue() );
440 }
441 bSaveLineStatus = sal_True;
442
443 if( nOldLng != nLng && aFontLB.IsVisible() )
444 aLanguageLB.SelectLanguage( nLng );
445
446 return 0;
447 }
448
IMPL_LINK(SwAsciiFilterDlg,LineEndHdl,RadioButton *,pBtn)449 IMPL_LINK( SwAsciiFilterDlg, LineEndHdl, RadioButton*, pBtn )
450 {
451 if( bSaveLineStatus )
452 pBtn->SaveValue();
453 return 0;
454 }
455
456
457