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