Update ooo320-m1
[ooovba.git] / sw / source / ui / dialog / ascfldlg.cxx
blob34b04822c0dfd1d1386929ab46185cb9d61f41e5
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ascfldlg.cxx,v $
10 * $Revision: 1.23 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sw.hxx"
33 #ifdef SW_DLLIMPLEMENTATION
34 #undef SW_DLLIMPLEMENTATION
35 #endif
36 #include <hintids.hxx>
37 #include <rtl/textenc.h>
38 #include <i18npool/mslangid.hxx>
39 #include <com/sun/star/i18n/ScriptType.hpp>
40 #include <svtools/lingucfg.hxx>
41 #include <fontcfg.hxx>
42 #include <swmodule.hxx>
43 #include <svx/unolingu.hxx>
44 #include <sfx2/printer.hxx>
45 #include <svx/flstitem.hxx>
46 #include <svx/dlgutil.hxx>
47 #include <svx/fontitem.hxx>
48 #include <svx/langitem.hxx>
49 #include <svx/scripttypeitem.hxx>
50 #include <swtypes.hxx>
51 #include <ascfldlg.hxx>
52 #include <shellio.hxx>
53 #include <docsh.hxx>
54 #include <doc.hxx>
55 #include <errhdl.hxx>
57 #ifndef _DIALOG_HRC
58 #include <dialog.hrc>
59 #endif
60 #ifndef _ASCFLDLG_HRC
61 #include <ascfldlg.hrc>
62 #endif
65 using namespace ::com::sun::star;
67 const sal_Unicode cDialogExtraDataClose = '}';
68 const char __FAR_DATA sDialogImpExtraData[] = "EncImpDlg:{";
69 const char __FAR_DATA sDialogExpExtraData[] = "EncExpDlg:{";
70 const USHORT nDialogExtraDataLen = 11; // 12345678901
72 SwAsciiFilterDlg::SwAsciiFilterDlg( Window* pParent, SwDocShell& rDocSh,
73 SvStream* pStream )
74 : SfxModalDialog( pParent, SW_RES( DLG_ASCII_FILTER )),
75 aFL( this, SW_RES( FL_1 )),
76 aCharSetFT( this, SW_RES( FT_CHARSET )),
77 aCharSetLB( this, SW_RES( LB_CHARSET )),
78 aFontFT( this, SW_RES( FT_FONT )),
79 aFontLB( this, SW_RES( LB_FONT )),
80 aLanguageFT( this, SW_RES( FT_LANGUAGE )),
81 aLanguageLB( this, SW_RES( LB_LANGUAGE )),
82 aCRLF_FT( this, SW_RES( FT_CRLF )),
83 aCRLF_RB( this, SW_RES( RB_CRLF )),
84 aCR_RB( this, SW_RES( RB_CR )),
85 aLF_RB( this, SW_RES( RB_LF )),
86 aOkPB( this, SW_RES( PB_OK )),
87 aCancelPB( this, SW_RES( PB_CANCEL )),
88 aHelpPB( this, SW_RES( PB_HELP )),
89 sSystemCharSet( SW_RES( STR_SYS_CHARSET )),
90 bSaveLineStatus( TRUE )
92 FreeResource();
94 SwAsciiOptions aOpt;
96 const String& rFindNm = String::CreateFromAscii(
97 pStream ? sDialogImpExtraData
98 : sDialogExpExtraData);
99 USHORT nEnd, nStt = GetExtraData().Search( rFindNm );
100 if( STRING_NOTFOUND != nStt )
102 nStt += nDialogExtraDataLen;
103 nEnd = GetExtraData().Search( cDialogExtraDataClose, nStt );
104 if( STRING_NOTFOUND != nEnd )
106 aOpt.ReadUserData( GetExtraData().Copy( nStt, nEnd - nStt ));
107 nStt -= nDialogExtraDataLen;
108 GetExtraData().Erase( nStt, nEnd - nStt + 1 );
113 // read the first chars and check the charset, (language - with L&H)
114 if( pStream )
116 char aBuffer[ 4098 ];
117 ULONG nOldPos = pStream->Tell();
118 ULONG nBytesRead = pStream->Read( aBuffer, 4096 );
119 pStream->Seek( nOldPos );
121 if( nBytesRead <= 4096 )
123 aBuffer[ nBytesRead ] = '0';
124 aBuffer[ nBytesRead+1 ] = '0';
125 if( 0 != ( nBytesRead & 0x00000001 ) )
126 aBuffer[ nBytesRead + 2 ] = '0';
129 BOOL bCR = FALSE, bLF = FALSE, bNoNormalChar = FALSE,
130 bNullChar = FALSE;
131 for( USHORT nCnt = 0; nCnt < nBytesRead; ++nCnt )
132 switch( aBuffer[ nCnt ] )
134 case 0x0: bNullChar = TRUE; break;
135 case 0xA: bLF = TRUE; break;
136 case 0xD: bCR = TRUE; break;
137 case 0xC:
138 case 0x1A:
139 case 0x9: break;
140 default:
141 if( 0x20 > aBuffer[ nCnt ] )
142 bNoNormalChar = TRUE;
145 if( !bNullChar )
147 if( bCR )
149 if( bLF )
151 aOpt.SetParaFlags( LINEEND_CRLF );
152 // have to check if of CharSet is type of ANSI
153 // aOpt.SetCharSet( CHARSET_ANSI );
155 else
157 aOpt.SetParaFlags( LINEEND_CR );
158 // have to check if CharSet is type of MAC
159 // aOpt.SetCharSet( CHARSET_MAC );
162 else if( bLF )
164 aOpt.SetParaFlags( LINEEND_LF );
165 // have to check if of CharSet is type of ANSI
166 // aOpt.SetCharSet( CHARSET_ANSI );
170 SwDoc* pDoc = rDocSh.GetDoc();
172 USHORT nAppScriptType = GetI18NScriptTypeOfLanguage( (USHORT)GetAppLanguage() );
174 BOOL bDelPrinter = FALSE;
175 SfxPrinter* pPrt = pDoc ? pDoc->getPrinter(false) : 0;
176 if( !pPrt )
178 SfxItemSet* pSet = new SfxItemSet( rDocSh.GetPool(),
179 SID_PRINTER_NOTFOUND_WARN, SID_PRINTER_NOTFOUND_WARN,
180 SID_PRINTER_CHANGESTODOC, SID_PRINTER_CHANGESTODOC,
181 0 );
182 pPrt = new SfxPrinter( pSet );
183 bDelPrinter = TRUE;
186 const USHORT nCount = pPrt->GetFontCount();
187 for (USHORT i = 0; i < nCount; ++i)
189 const String &rStr = pPrt->GetFont(i)->GetName();
190 aFontLB.InsertEntry( rStr );
193 if( !aOpt.GetFontName().Len() )
195 if(pDoc)
197 USHORT 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;
203 aOpt.SetFontName( ((SvxFontItem&)pDoc->GetDefault(
204 nFontRes )).GetFamilyName() );
206 else
208 USHORT 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));
216 aFontLB.SelectEntry( aOpt.GetFontName() );
218 if( bDelPrinter )
219 delete pPrt;
222 // initialisiere Sprache
224 if( !aOpt.GetLanguage() )
226 if(pDoc)
228 USHORT nWhich = GetWhichOfScript( RES_CHRATR_LANGUAGE, nAppScriptType);
229 aOpt.SetLanguage( ((SvxLanguageItem&)pDoc->
230 GetDefault( nWhich )).GetLanguage());
232 else
234 SvtLinguOptions aLinguOpt;
235 SvtLinguConfig().GetOptions( aLinguOpt );
236 switch(nAppScriptType)
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));
251 aLanguageLB.SetLanguageList( LANG_LIST_ALL, TRUE, FALSE );
252 aLanguageLB.SelectLanguage( aOpt.GetLanguage() );
255 else
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();
264 long nY = aFontFT.GetPosPixel().Y() + 1;
265 Point aPos( aCRLF_FT.GetPosPixel() ); aPos.Y() = nY;
266 aCRLF_FT.SetPosPixel( aPos );
268 aPos = aCRLF_RB.GetPosPixel(); aPos.Y() = nY;
269 aCRLF_RB.SetPosPixel( aPos );
271 aPos = aCR_RB.GetPosPixel(); aPos.Y() = nY;
272 aCR_RB.SetPosPixel( aPos );
274 aPos = aLF_RB.GetPosPixel(); aPos.Y() = nY;
275 aLF_RB.SetPosPixel( aPos );
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 );
285 // initialisiere Zeichensatz
286 aCharSetLB.FillFromTextEncodingTable( pStream != NULL );
287 aCharSetLB.SelectTextEncoding( aOpt.GetCharSet() );
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 ));
294 SetCRLF( aOpt.GetParaFlags() );
296 aCRLF_RB.SaveValue();
297 aLF_RB.SaveValue();
298 aCR_RB.SaveValue();
302 SwAsciiFilterDlg::~SwAsciiFilterDlg()
307 void SwAsciiFilterDlg::FillOptions( SwAsciiOptions& rOptions )
309 ULONG nCCode = aCharSetLB.GetSelectTextEncoding();
310 String sFont;
311 ULONG nLng = 0;
312 if( aFontLB.IsVisible() )
314 sFont = aFontLB.GetSelectEntry();
315 nLng = (ULONG)aLanguageLB.GetSelectLanguage();
318 rOptions.SetFontName( sFont );
319 rOptions.SetCharSet( rtl_TextEncoding( nCCode ) );
320 rOptions.SetLanguage( USHORT( nLng ) );
321 rOptions.SetParaFlags( GetCRLF() );
323 // JP: Task #71802# save the user settings
324 String sData;
325 rOptions.WriteUserData( sData );
326 if( sData.Len() )
328 const String& rFindNm = String::CreateFromAscii(
329 aFontLB.IsVisible() ? sDialogImpExtraData
330 : sDialogExpExtraData);
331 USHORT nEnd, nStt = GetExtraData().Search( rFindNm );
332 if( STRING_NOTFOUND != nStt )
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 );
340 String sTmp(GetExtraData());
341 sTmp += rFindNm;
342 sTmp += sData;
343 sTmp += cDialogExtraDataClose;
344 GetExtraData() = sTmp;
348 void SwAsciiFilterDlg::SetCRLF( LineEnd eEnd )
350 switch( eEnd )
352 case LINEEND_CR: aCR_RB.Check(); break;
353 case LINEEND_CRLF: aCRLF_RB.Check(); break;
354 case LINEEND_LF: aLF_RB.Check(); break;
358 LineEnd SwAsciiFilterDlg::GetCRLF() const
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;
370 IMPL_LINK( SwAsciiFilterDlg, CharSetSelHdl, SvxTextEncodingBox*, pBox )
372 LineEnd eOldEnd = GetCRLF(), eEnd = (LineEnd)-1;
373 LanguageType nLng = aFontLB.IsVisible()
374 ? aLanguageLB.GetSelectLanguage()
375 : LANGUAGE_SYSTEM,
376 nOldLng = nLng;
378 rtl_TextEncoding nChrSet = pBox->GetSelectTextEncoding();
379 if( nChrSet == gsl_getSystemTextEncoding() )
380 eEnd = GetSystemLineEnd();
381 else
383 switch( nChrSet )
385 case RTL_TEXTENCODING_MS_1252:
386 #ifdef UNX
387 eEnd = LINEEND_LF;
388 #else
389 eEnd = LINEEND_CRLF; // ANSI
390 #endif
391 break;
393 case RTL_TEXTENCODING_APPLE_ROMAN: // MAC
394 eEnd = LINEEND_CR;
395 break;
397 case RTL_TEXTENCODING_IBM_850: // DOS
398 eEnd = LINEEND_CRLF;
399 break;
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;
425 bSaveLineStatus = FALSE;
426 if( eEnd != (LineEnd)-1 ) // changed?
428 if( eOldEnd != eEnd )
429 SetCRLF( eEnd );
431 else
433 // restore old user choise (not the automatic!)
434 aCRLF_RB.Check( aCRLF_RB.GetSavedValue() );
435 aCR_RB.Check( aCR_RB.GetSavedValue() );
436 aLF_RB.Check( aLF_RB.GetSavedValue() );
438 bSaveLineStatus = TRUE;
440 if( nOldLng != nLng && aFontLB.IsVisible() )
441 aLanguageLB.SelectLanguage( nLng );
443 return 0;
446 IMPL_LINK( SwAsciiFilterDlg, LineEndHdl, RadioButton*, pBtn )
448 if( bSaveLineStatus )
449 pBtn->SaveValue();
450 return 0;