update dev300-m58
[ooovba.git] / sw / source / ui / dialog / ascfldlg.cxx
blob66629b5030d232920f0eb8c471bc3322b6b5572d
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 <svtools/lingucfg.hxx>
39 #include <fontcfg.hxx>
40 #include <swmodule.hxx>
41 #include <svx/unolingu.hxx>
42 #include <sfx2/printer.hxx>
43 #include <svx/flstitem.hxx>
44 #include <svx/dlgutil.hxx>
45 #include <svx/fontitem.hxx>
46 #include <svx/langitem.hxx>
47 #include <svx/scripttypeitem.hxx>
48 #include <swtypes.hxx>
49 #include <ascfldlg.hxx>
50 #include <shellio.hxx>
51 #include <docsh.hxx>
52 #include <doc.hxx>
53 #include <errhdl.hxx>
55 #ifndef _DIALOG_HRC
56 #include <dialog.hrc>
57 #endif
58 #ifndef _ASCFLDLG_HRC
59 #include <ascfldlg.hrc>
60 #endif
63 using namespace ::com::sun::star;
65 const sal_Unicode cDialogExtraDataClose = '}';
66 const char __FAR_DATA sDialogImpExtraData[] = "EncImpDlg:{";
67 const char __FAR_DATA sDialogExpExtraData[] = "EncExpDlg:{";
68 const USHORT nDialogExtraDataLen = 11; // 12345678901
70 SwAsciiFilterDlg::SwAsciiFilterDlg( Window* pParent, SwDocShell& rDocSh,
71 SvStream* pStream )
72 : SfxModalDialog( pParent, SW_RES( DLG_ASCII_FILTER )),
73 aFL( this, SW_RES( FL_1 )),
74 aCharSetFT( this, SW_RES( FT_CHARSET )),
75 aCharSetLB( this, SW_RES( LB_CHARSET )),
76 aFontFT( this, SW_RES( FT_FONT )),
77 aFontLB( this, SW_RES( LB_FONT )),
78 aLanguageFT( this, SW_RES( FT_LANGUAGE )),
79 aLanguageLB( this, SW_RES( LB_LANGUAGE )),
80 aCRLF_FT( this, SW_RES( FT_CRLF )),
81 aCRLF_RB( this, SW_RES( RB_CRLF )),
82 aCR_RB( this, SW_RES( RB_CR )),
83 aLF_RB( this, SW_RES( RB_LF )),
84 aOkPB( this, SW_RES( PB_OK )),
85 aCancelPB( this, SW_RES( PB_CANCEL )),
86 aHelpPB( this, SW_RES( PB_HELP )),
87 sSystemCharSet( SW_RES( STR_SYS_CHARSET )),
88 bSaveLineStatus( TRUE )
90 FreeResource();
92 SwAsciiOptions aOpt;
94 const String& rFindNm = String::CreateFromAscii(
95 pStream ? sDialogImpExtraData
96 : sDialogExpExtraData);
97 USHORT nEnd, nStt = GetExtraData().Search( rFindNm );
98 if( STRING_NOTFOUND != nStt )
100 nStt += nDialogExtraDataLen;
101 nEnd = GetExtraData().Search( cDialogExtraDataClose, nStt );
102 if( STRING_NOTFOUND != nEnd )
104 aOpt.ReadUserData( GetExtraData().Copy( nStt, nEnd - nStt ));
105 nStt -= nDialogExtraDataLen;
106 GetExtraData().Erase( nStt, nEnd - nStt + 1 );
111 // read the first chars and check the charset, (language - with L&H)
112 if( pStream )
114 char aBuffer[ 4098 ];
115 ULONG nOldPos = pStream->Tell();
116 ULONG nBytesRead = pStream->Read( aBuffer, 4096 );
117 pStream->Seek( nOldPos );
119 if( nBytesRead <= 4096 )
121 aBuffer[ nBytesRead ] = '0';
122 aBuffer[ nBytesRead+1 ] = '0';
123 if( 0 != ( nBytesRead & 0x00000001 ) )
124 aBuffer[ nBytesRead + 2 ] = '0';
127 BOOL bCR = FALSE, bLF = FALSE, bNoNormalChar = FALSE,
128 bNullChar = FALSE;
129 for( USHORT nCnt = 0; nCnt < nBytesRead; ++nCnt )
130 switch( aBuffer[ nCnt ] )
132 case 0x0: bNullChar = TRUE; break;
133 case 0xA: bLF = TRUE; break;
134 case 0xD: bCR = TRUE; break;
135 case 0xC:
136 case 0x1A:
137 case 0x9: break;
138 default:
139 if( 0x20 > aBuffer[ nCnt ] )
140 bNoNormalChar = TRUE;
143 if( !bNullChar )
145 if( bCR )
147 if( bLF )
149 aOpt.SetParaFlags( LINEEND_CRLF );
150 // have to check if of CharSet is type of ANSI
151 // aOpt.SetCharSet( CHARSET_ANSI );
153 else
155 aOpt.SetParaFlags( LINEEND_CR );
156 // have to check if CharSet is type of MAC
157 // aOpt.SetCharSet( CHARSET_MAC );
160 else if( bLF )
162 aOpt.SetParaFlags( LINEEND_LF );
163 // have to check if of CharSet is type of ANSI
164 // aOpt.SetCharSet( CHARSET_ANSI );
168 SwDoc* pDoc = rDocSh.GetDoc();
170 USHORT nAppScriptType = GetI18NScriptTypeOfLanguage( (USHORT)GetAppLanguage() );
172 BOOL bDelPrinter = FALSE;
173 SfxPrinter* pPrt = pDoc ? pDoc->getPrinter(false) : 0;
174 if( !pPrt )
176 SfxItemSet* pSet = new SfxItemSet( rDocSh.GetPool(),
177 SID_PRINTER_NOTFOUND_WARN, SID_PRINTER_NOTFOUND_WARN,
178 SID_PRINTER_CHANGESTODOC, SID_PRINTER_CHANGESTODOC,
179 0 );
180 pPrt = new SfxPrinter( pSet );
181 bDelPrinter = TRUE;
184 const USHORT nCount = pPrt->GetFontCount();
185 for (USHORT i = 0; i < nCount; ++i)
187 const String &rStr = pPrt->GetFont(i)->GetName();
188 aFontLB.InsertEntry( rStr );
191 if( !aOpt.GetFontName().Len() )
193 if(pDoc)
195 USHORT nFontRes = RES_CHRATR_FONT;
196 if(SCRIPTTYPE_ASIAN == nAppScriptType)
197 nFontRes = RES_CHRATR_CJK_FONT;
198 else if(SCRIPTTYPE_COMPLEX == nAppScriptType)
199 nFontRes = RES_CHRATR_CTL_FONT;
201 aOpt.SetFontName( ((SvxFontItem&)pDoc->GetDefault(
202 nFontRes )).GetFamilyName() );
204 else
206 USHORT nFontType = FONT_STANDARD;
207 if(SCRIPTTYPE_ASIAN == nAppScriptType)
208 nFontType = FONT_STANDARD_CJK;
209 else if(SCRIPTTYPE_COMPLEX == nAppScriptType)
210 nFontType = FONT_STANDARD_CTL;
211 aOpt.SetFontName(SW_MOD()->GetStdFontConfig()->GetFontFor(nFontType));
214 aFontLB.SelectEntry( aOpt.GetFontName() );
216 if( bDelPrinter )
217 delete pPrt;
220 // initialisiere Sprache
222 if( !aOpt.GetLanguage() )
224 if(pDoc)
226 USHORT nWhich = GetWhichOfScript( RES_CHRATR_LANGUAGE, nAppScriptType);
227 aOpt.SetLanguage( ((SvxLanguageItem&)pDoc->
228 GetDefault( nWhich )).GetLanguage());
230 else
232 SvtLinguOptions aLinguOpt;
233 SvtLinguConfig().GetOptions( aLinguOpt );
234 switch(nAppScriptType)
236 case SCRIPTTYPE_ASIAN:
237 aOpt.SetLanguage(aLinguOpt.nDefaultLanguage_CJK);
238 break;
239 case SCRIPTTYPE_COMPLEX:
240 aOpt.SetLanguage(aLinguOpt.nDefaultLanguage_CTL);
241 break;
242 //SCRIPTTYPE_LATIN:
243 default:
244 aOpt.SetLanguage(aLinguOpt.nDefaultLanguage);
249 aLanguageLB.SetLanguageList( LANG_LIST_ALL, TRUE, FALSE );
250 aLanguageLB.SelectLanguage( aOpt.GetLanguage() );
253 else
255 // hide the used Control for the Export and move the
256 // other behind the charset controls
257 aFontFT.Hide();
258 aFontLB.Hide();
259 aLanguageFT.Hide();
260 aLanguageLB.Hide();
262 long nY = aFontFT.GetPosPixel().Y() + 1;
263 Point aPos( aCRLF_FT.GetPosPixel() ); aPos.Y() = nY;
264 aCRLF_FT.SetPosPixel( aPos );
266 aPos = aCRLF_RB.GetPosPixel(); aPos.Y() = nY;
267 aCRLF_RB.SetPosPixel( aPos );
269 aPos = aCR_RB.GetPosPixel(); aPos.Y() = nY;
270 aCR_RB.SetPosPixel( aPos );
272 aPos = aLF_RB.GetPosPixel(); aPos.Y() = nY;
273 aLF_RB.SetPosPixel( aPos );
275 Size aSize = GetSizePixel();
276 Size aTmpSz( 6, 6 );
277 aTmpSz = LogicToPixel(aTmpSz, MAP_APPFONT);
278 aSize.Height() = aHelpPB.GetPosPixel().Y() +
279 aHelpPB.GetSizePixel().Height() + aTmpSz.Height();
280 SetSizePixel( aSize );
283 // initialisiere Zeichensatz
284 aCharSetLB.FillFromTextEncodingTable( pStream != NULL );
285 aCharSetLB.SelectTextEncoding( aOpt.GetCharSet() );
287 aCharSetLB.SetSelectHdl( LINK( this, SwAsciiFilterDlg, CharSetSelHdl ));
288 aCRLF_RB.SetToggleHdl( LINK( this, SwAsciiFilterDlg, LineEndHdl ));
289 aLF_RB.SetToggleHdl( LINK( this, SwAsciiFilterDlg, LineEndHdl ));
290 aCR_RB.SetToggleHdl( LINK( this, SwAsciiFilterDlg, LineEndHdl ));
292 SetCRLF( aOpt.GetParaFlags() );
294 aCRLF_RB.SaveValue();
295 aLF_RB.SaveValue();
296 aCR_RB.SaveValue();
300 SwAsciiFilterDlg::~SwAsciiFilterDlg()
305 void SwAsciiFilterDlg::FillOptions( SwAsciiOptions& rOptions )
307 ULONG nCCode = aCharSetLB.GetSelectTextEncoding();
308 String sFont;
309 ULONG nLng = 0;
310 if( aFontLB.IsVisible() )
312 sFont = aFontLB.GetSelectEntry();
313 nLng = (ULONG)aLanguageLB.GetSelectLanguage();
316 rOptions.SetFontName( sFont );
317 rOptions.SetCharSet( rtl_TextEncoding( nCCode ) );
318 rOptions.SetLanguage( USHORT( nLng ) );
319 rOptions.SetParaFlags( GetCRLF() );
321 // JP: Task #71802# save the user settings
322 String sData;
323 rOptions.WriteUserData( sData );
324 if( sData.Len() )
326 const String& rFindNm = String::CreateFromAscii(
327 aFontLB.IsVisible() ? sDialogImpExtraData
328 : sDialogExpExtraData);
329 USHORT nEnd, nStt = GetExtraData().Search( rFindNm );
330 if( STRING_NOTFOUND != nStt )
332 // called twice, so remove "old" settings
333 nEnd = GetExtraData().Search( cDialogExtraDataClose,
334 nStt + nDialogExtraDataLen );
335 if( STRING_NOTFOUND != nEnd )
336 GetExtraData().Erase( nStt, nEnd - nStt + 1 );
338 String sTmp(GetExtraData());
339 sTmp += rFindNm;
340 sTmp += sData;
341 sTmp += cDialogExtraDataClose;
342 GetExtraData() = sTmp;
346 void SwAsciiFilterDlg::SetCRLF( LineEnd eEnd )
348 switch( eEnd )
350 case LINEEND_CR: aCR_RB.Check(); break;
351 case LINEEND_CRLF: aCRLF_RB.Check(); break;
352 case LINEEND_LF: aLF_RB.Check(); break;
356 LineEnd SwAsciiFilterDlg::GetCRLF() const
358 LineEnd eEnd;
359 if( aCR_RB.IsChecked() )
360 eEnd = LINEEND_CR;
361 else if( aLF_RB.IsChecked() )
362 eEnd = LINEEND_LF;
363 else
364 eEnd = LINEEND_CRLF;
365 return eEnd;
368 IMPL_LINK( SwAsciiFilterDlg, CharSetSelHdl, SvxTextEncodingBox*, pBox )
370 LineEnd eOldEnd = GetCRLF(), eEnd = (LineEnd)-1;
371 LanguageType nLng = aFontLB.IsVisible()
372 ? aLanguageLB.GetSelectLanguage()
373 : LANGUAGE_SYSTEM,
374 nOldLng = nLng;
376 rtl_TextEncoding nChrSet = pBox->GetSelectTextEncoding();
377 if( nChrSet == gsl_getSystemTextEncoding() )
378 eEnd = GetSystemLineEnd();
379 else
381 switch( nChrSet )
383 case RTL_TEXTENCODING_MS_1252:
384 #ifdef UNX
385 eEnd = LINEEND_LF;
386 #else
387 eEnd = LINEEND_CRLF; // ANSI
388 #endif
389 break;
391 case RTL_TEXTENCODING_APPLE_ROMAN: // MAC
392 eEnd = LINEEND_CR;
393 break;
395 case RTL_TEXTENCODING_IBM_850: // DOS
396 eEnd = LINEEND_CRLF;
397 break;
399 case RTL_TEXTENCODING_APPLE_ARABIC:
400 case RTL_TEXTENCODING_APPLE_CENTEURO:
401 case RTL_TEXTENCODING_APPLE_CROATIAN:
402 case RTL_TEXTENCODING_APPLE_CYRILLIC:
403 case RTL_TEXTENCODING_APPLE_DEVANAGARI:
404 case RTL_TEXTENCODING_APPLE_FARSI:
405 case RTL_TEXTENCODING_APPLE_GREEK:
406 case RTL_TEXTENCODING_APPLE_GUJARATI:
407 case RTL_TEXTENCODING_APPLE_GURMUKHI:
408 case RTL_TEXTENCODING_APPLE_HEBREW:
409 case RTL_TEXTENCODING_APPLE_ICELAND:
410 case RTL_TEXTENCODING_APPLE_ROMANIAN:
411 case RTL_TEXTENCODING_APPLE_THAI:
412 case RTL_TEXTENCODING_APPLE_TURKISH:
413 case RTL_TEXTENCODING_APPLE_UKRAINIAN:
414 case RTL_TEXTENCODING_APPLE_CHINSIMP:
415 case RTL_TEXTENCODING_APPLE_CHINTRAD:
416 case RTL_TEXTENCODING_APPLE_JAPANESE:
417 case RTL_TEXTENCODING_APPLE_KOREAN:
418 eEnd = LINEEND_CR;
419 break;
423 bSaveLineStatus = FALSE;
424 if( eEnd != (LineEnd)-1 ) // changed?
426 if( eOldEnd != eEnd )
427 SetCRLF( eEnd );
429 else
431 // restore old user choise (not the automatic!)
432 aCRLF_RB.Check( aCRLF_RB.GetSavedValue() );
433 aCR_RB.Check( aCR_RB.GetSavedValue() );
434 aLF_RB.Check( aLF_RB.GetSavedValue() );
436 bSaveLineStatus = TRUE;
438 if( nOldLng != nLng && aFontLB.IsVisible() )
439 aLanguageLB.SelectLanguage( nLng );
441 return 0;
444 IMPL_LINK( SwAsciiFilterDlg, LineEndHdl, RadioButton*, pBtn )
446 if( bSaveLineStatus )
447 pBtn->SaveValue();
448 return 0;