Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
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 : :
49 : : #include <dialog.hrc>
50 : : #include <ascfldlg.hrc>
51 : :
52 : : #include "vcl/metric.hxx"
53 : :
54 : :
55 : : using namespace ::com::sun::star;
56 : :
57 : : const sal_Unicode cDialogExtraDataClose = '}';
58 : : const char sDialogImpExtraData[] = "EncImpDlg:{";
59 : : const char sDialogExpExtraData[] = "EncExpDlg:{";
60 : : const sal_uInt16 nDialogExtraDataLen = 11; // 12345678901
61 : :
62 : 0 : SwAsciiFilterDlg::SwAsciiFilterDlg( Window* pParent, SwDocShell& rDocSh,
63 : : SvStream* pStream )
64 : : : SfxModalDialog( pParent, SW_RES( DLG_ASCII_FILTER )),
65 : : aFL( this, SW_RES( FL_1 )),
66 : : aCharSetFT( this, SW_RES( FT_CHARSET )),
67 : : aCharSetLB( this, SW_RES( LB_CHARSET )),
68 : : aFontFT( this, SW_RES( FT_FONT )),
69 : : aFontLB( this, SW_RES( LB_FONT )),
70 : : aLanguageFT( this, SW_RES( FT_LANGUAGE )),
71 : : aLanguageLB( this, SW_RES( LB_LANGUAGE )),
72 : : aCRLF_FT( this, SW_RES( FT_CRLF )),
73 : : aCRLF_RB( this, SW_RES( RB_CRLF )),
74 : : aCR_RB( this, SW_RES( RB_CR )),
75 : : aLF_RB( this, SW_RES( RB_LF )),
76 : : aOkPB( this, SW_RES( PB_OK )),
77 : : aCancelPB( this, SW_RES( PB_CANCEL )),
78 : : aHelpPB( this, SW_RES( PB_HELP )),
79 : : sSystemCharSet( SW_RES( STR_SYS_CHARSET )),
80 : 0 : bSaveLineStatus( sal_True )
81 : : {
82 : 0 : FreeResource();
83 : :
84 : 0 : SwAsciiOptions aOpt;
85 : : {
86 : : const rtl::OUString sFindNm = rtl::OUString::createFromAscii(
87 : : pStream ? sDialogImpExtraData
88 : 0 : : sDialogExpExtraData);
89 : 0 : sal_uInt16 nEnd, nStt = GetExtraData().Search( sFindNm );
90 : 0 : if( STRING_NOTFOUND != nStt )
91 : : {
92 : 0 : nStt += nDialogExtraDataLen;
93 : 0 : nEnd = GetExtraData().Search( cDialogExtraDataClose, nStt );
94 : 0 : if( STRING_NOTFOUND != nEnd )
95 : : {
96 : 0 : aOpt.ReadUserData( GetExtraData().Copy( nStt, nEnd - nStt ));
97 : 0 : nStt -= nDialogExtraDataLen;
98 : 0 : GetExtraData().Erase( nStt, nEnd - nStt + 1 );
99 : : }
100 : 0 : }
101 : : }
102 : :
103 : : // read the first chars and check the charset, (language - with L&H)
104 : 0 : if( pStream )
105 : : {
106 : : char aBuffer[ 4098 ];
107 : 0 : sal_uLong nOldPos = pStream->Tell();
108 : 0 : sal_uLong nBytesRead = pStream->Read( aBuffer, 4096 );
109 : 0 : pStream->Seek( nOldPos );
110 : :
111 : 0 : if( nBytesRead <= 4096 )
112 : : {
113 : 0 : aBuffer[ nBytesRead ] = '0';
114 : 0 : aBuffer[ nBytesRead+1 ] = '0';
115 : : }
116 : :
117 : 0 : sal_Bool bCR = sal_False, bLF = sal_False, bNullChar = sal_False;
118 : 0 : for( sal_uInt16 nCnt = 0; nCnt < nBytesRead; ++nCnt )
119 : 0 : switch( aBuffer[ nCnt ] )
120 : : {
121 : 0 : case 0x0: bNullChar = sal_True; break;
122 : 0 : case 0xA: bLF = sal_True; break;
123 : 0 : case 0xD: bCR = sal_True; break;
124 : : case 0xC:
125 : : case 0x1A:
126 : 0 : case 0x9: break;
127 : 0 : default: break;
128 : : }
129 : :
130 : 0 : if( !bNullChar )
131 : : {
132 : 0 : if( bCR )
133 : : {
134 : 0 : if( bLF )
135 : : {
136 : 0 : aOpt.SetParaFlags( LINEEND_CRLF );
137 : : }
138 : : else
139 : : {
140 : 0 : aOpt.SetParaFlags( LINEEND_CR );
141 : : }
142 : : }
143 : 0 : else if( bLF )
144 : : {
145 : 0 : aOpt.SetParaFlags( LINEEND_LF );
146 : : }
147 : : }
148 : :
149 : 0 : sal_uInt16 nAppScriptType = GetI18NScriptTypeOfLanguage( (sal_uInt16)GetAppLanguage() );
150 : 0 : SwDoc* pDoc = rDocSh.GetDoc();
151 : :
152 : : // initialise language
153 : : {
154 : 0 : if( !aOpt.GetLanguage() )
155 : : {
156 : 0 : if(pDoc)
157 : : {
158 : 0 : sal_uInt16 nWhich = GetWhichOfScript( RES_CHRATR_LANGUAGE, nAppScriptType);
159 : : aOpt.SetLanguage( ((SvxLanguageItem&)pDoc->
160 : 0 : GetDefault( nWhich )).GetLanguage());
161 : : }
162 : : else
163 : : {
164 : 0 : SvtLinguOptions aLinguOpt;
165 : 0 : SvtLinguConfig().GetOptions( aLinguOpt );
166 : 0 : switch(nAppScriptType)
167 : : {
168 : : case SCRIPTTYPE_ASIAN:
169 : 0 : aOpt.SetLanguage(MsLangId::resolveSystemLanguageByScriptType(aLinguOpt.nDefaultLanguage_CJK, SCRIPTTYPE_ASIAN));
170 : 0 : break;
171 : : case SCRIPTTYPE_COMPLEX:
172 : 0 : aOpt.SetLanguage(MsLangId::resolveSystemLanguageByScriptType(aLinguOpt.nDefaultLanguage_CTL, SCRIPTTYPE_COMPLEX));
173 : 0 : break;
174 : : //SCRIPTTYPE_LATIN:
175 : : default:
176 : 0 : aOpt.SetLanguage(MsLangId::resolveSystemLanguageByScriptType(aLinguOpt.nDefaultLanguage, SCRIPTTYPE_LATIN));
177 : 0 : }
178 : : }
179 : : }
180 : :
181 : 0 : aLanguageLB.SetLanguageList( LANG_LIST_ALL, sal_True, sal_False );
182 : 0 : aLanguageLB.SelectLanguage( aOpt.GetLanguage() );
183 : : }
184 : :
185 : : {
186 : 0 : bool bDelPrinter = false;
187 : 0 : SfxPrinter* pPrt = pDoc ? pDoc->getPrinter(false) : 0;
188 : 0 : if( !pPrt )
189 : : {
190 : 0 : SfxItemSet* pSet = new SfxItemSet( rDocSh.GetPool(),
191 : : SID_PRINTER_NOTFOUND_WARN, SID_PRINTER_NOTFOUND_WARN,
192 : : SID_PRINTER_CHANGESTODOC, SID_PRINTER_CHANGESTODOC,
193 : 0 : 0 );
194 : 0 : pPrt = new SfxPrinter( pSet );
195 : 0 : bDelPrinter = true;
196 : : }
197 : :
198 : : // get the set of disctinct available family names
199 : 0 : std::set< String > aFontNames;
200 : 0 : int nFontNames = pPrt->GetDevFontCount();
201 : 0 : for( int i = 0; i < nFontNames; i++ )
202 : : {
203 : 0 : FontInfo aInf( pPrt->GetDevFont( i ) );
204 : 0 : aFontNames.insert( aInf.GetName() );
205 : 0 : }
206 : :
207 : : // insert to listbox
208 : 0 : for( std::set< String >::const_iterator it = aFontNames.begin();
209 : 0 : it != aFontNames.end(); ++it )
210 : : {
211 : 0 : aFontLB.InsertEntry( *it );
212 : : }
213 : :
214 : 0 : if( !aOpt.GetFontName().Len() )
215 : : {
216 : 0 : LanguageType eLang = aOpt.GetLanguage();
217 : 0 : Font aTmpFont(OutputDevice::GetDefaultFont(DEFAULTFONT_FIXED, eLang, DEFAULTFONT_FLAGS_ONLYONE, pPrt));
218 : 0 : aOpt.SetFontName(aTmpFont.GetName());
219 : : }
220 : :
221 : 0 : aFontLB.SelectEntry( aOpt.GetFontName() );
222 : :
223 : 0 : if( bDelPrinter )
224 : 0 : delete pPrt;
225 : : }
226 : :
227 : :
228 : : }
229 : : else
230 : : {
231 : : // hide the used Control for the Export and move the
232 : : // other behind the charset controls
233 : 0 : aFontFT.Hide();
234 : 0 : aFontLB.Hide();
235 : 0 : aLanguageFT.Hide();
236 : 0 : aLanguageLB.Hide();
237 : :
238 : 0 : long nY = aFontFT.GetPosPixel().Y() + 1;
239 : 0 : Point aPos( aCRLF_FT.GetPosPixel() ); aPos.Y() = nY;
240 : 0 : aCRLF_FT.SetPosPixel( aPos );
241 : :
242 : 0 : aPos = aCRLF_RB.GetPosPixel(); aPos.Y() = nY;
243 : 0 : aCRLF_RB.SetPosPixel( aPos );
244 : :
245 : 0 : aPos = aCR_RB.GetPosPixel(); aPos.Y() = nY;
246 : 0 : aCR_RB.SetPosPixel( aPos );
247 : :
248 : 0 : aPos = aLF_RB.GetPosPixel(); aPos.Y() = nY;
249 : 0 : aLF_RB.SetPosPixel( aPos );
250 : :
251 : 0 : Size aSize = GetSizePixel();
252 : 0 : Size aTmpSz( 6, 6 );
253 : 0 : aTmpSz = LogicToPixel(aTmpSz, MAP_APPFONT);
254 : 0 : aSize.Height() = aHelpPB.GetPosPixel().Y() +
255 : 0 : aHelpPB.GetSizePixel().Height() + aTmpSz.Height();
256 : 0 : SetSizePixel( aSize );
257 : : }
258 : :
259 : : // initialise character set
260 : 0 : aCharSetLB.FillFromTextEncodingTable( pStream != NULL );
261 : 0 : aCharSetLB.SelectTextEncoding( aOpt.GetCharSet() );
262 : :
263 : 0 : aCharSetLB.SetSelectHdl( LINK( this, SwAsciiFilterDlg, CharSetSelHdl ));
264 : 0 : aCRLF_RB.SetToggleHdl( LINK( this, SwAsciiFilterDlg, LineEndHdl ));
265 : 0 : aLF_RB.SetToggleHdl( LINK( this, SwAsciiFilterDlg, LineEndHdl ));
266 : 0 : aCR_RB.SetToggleHdl( LINK( this, SwAsciiFilterDlg, LineEndHdl ));
267 : :
268 : 0 : SetCRLF( aOpt.GetParaFlags() );
269 : :
270 : 0 : aCRLF_RB.SaveValue();
271 : 0 : aLF_RB.SaveValue();
272 : 0 : aCR_RB.SaveValue();
273 : 0 : }
274 : :
275 : :
276 : 0 : SwAsciiFilterDlg::~SwAsciiFilterDlg()
277 : : {
278 : 0 : }
279 : :
280 : :
281 : 0 : void SwAsciiFilterDlg::FillOptions( SwAsciiOptions& rOptions )
282 : : {
283 : 0 : sal_uLong nCCode = aCharSetLB.GetSelectTextEncoding();
284 : 0 : String sFont;
285 : 0 : sal_uLong nLng = 0;
286 : 0 : if( aFontLB.IsVisible() )
287 : : {
288 : 0 : sFont = aFontLB.GetSelectEntry();
289 : 0 : nLng = (sal_uLong)aLanguageLB.GetSelectLanguage();
290 : : }
291 : :
292 : 0 : rOptions.SetFontName( sFont );
293 : 0 : rOptions.SetCharSet( rtl_TextEncoding( nCCode ) );
294 : 0 : rOptions.SetLanguage( sal_uInt16( nLng ) );
295 : 0 : rOptions.SetParaFlags( GetCRLF() );
296 : :
297 : : // save the user settings
298 : 0 : String sData;
299 : 0 : rOptions.WriteUserData( sData );
300 : 0 : if( sData.Len() )
301 : : {
302 : : const rtl::OUString sFindNm = rtl::OUString::createFromAscii(
303 : 0 : aFontLB.IsVisible() ? sDialogImpExtraData
304 : 0 : : sDialogExpExtraData);
305 : 0 : sal_uInt16 nEnd, nStt = GetExtraData().Search( sFindNm );
306 : 0 : if( STRING_NOTFOUND != nStt )
307 : : {
308 : : // called twice, so remove "old" settings
309 : 0 : nEnd = GetExtraData().Search( cDialogExtraDataClose,
310 : 0 : nStt + nDialogExtraDataLen );
311 : 0 : if( STRING_NOTFOUND != nEnd )
312 : 0 : GetExtraData().Erase( nStt, nEnd - nStt + 1 );
313 : : }
314 : 0 : String sTmp(GetExtraData());
315 : 0 : sTmp += sFindNm;
316 : 0 : sTmp += sData;
317 : 0 : sTmp += cDialogExtraDataClose;
318 : 0 : GetExtraData() = sTmp;
319 : 0 : }
320 : 0 : }
321 : :
322 : 0 : void SwAsciiFilterDlg::SetCRLF( LineEnd eEnd )
323 : : {
324 : 0 : switch( eEnd )
325 : : {
326 : 0 : case LINEEND_CR: aCR_RB.Check(); break;
327 : 0 : case LINEEND_CRLF: aCRLF_RB.Check(); break;
328 : 0 : case LINEEND_LF: aLF_RB.Check(); break;
329 : : }
330 : 0 : }
331 : :
332 : 0 : LineEnd SwAsciiFilterDlg::GetCRLF() const
333 : : {
334 : : LineEnd eEnd;
335 : 0 : if( aCR_RB.IsChecked() )
336 : 0 : eEnd = LINEEND_CR;
337 : 0 : else if( aLF_RB.IsChecked() )
338 : 0 : eEnd = LINEEND_LF;
339 : : else
340 : 0 : eEnd = LINEEND_CRLF;
341 : 0 : return eEnd;
342 : : }
343 : :
344 : 0 : IMPL_LINK( SwAsciiFilterDlg, CharSetSelHdl, SvxTextEncodingBox*, pBox )
345 : : {
346 : 0 : LineEnd eOldEnd = GetCRLF(), eEnd = (LineEnd)-1;
347 : 0 : LanguageType nLng = aFontLB.IsVisible()
348 : 0 : ? aLanguageLB.GetSelectLanguage()
349 : 0 : : LANGUAGE_SYSTEM,
350 : 0 : nOldLng = nLng;
351 : :
352 : 0 : rtl_TextEncoding nChrSet = pBox->GetSelectTextEncoding();
353 : 0 : if( nChrSet == osl_getThreadTextEncoding() )
354 : 0 : eEnd = GetSystemLineEnd();
355 : : else
356 : : {
357 : 0 : switch( nChrSet )
358 : : {
359 : : case RTL_TEXTENCODING_MS_1252:
360 : : #ifdef UNX
361 : 0 : eEnd = LINEEND_LF;
362 : : #else
363 : : eEnd = LINEEND_CRLF; // ANSI
364 : : #endif
365 : 0 : break;
366 : :
367 : : case RTL_TEXTENCODING_APPLE_ROMAN: // MAC
368 : 0 : eEnd = LINEEND_CR;
369 : 0 : break;
370 : :
371 : : case RTL_TEXTENCODING_IBM_850: // DOS
372 : 0 : eEnd = LINEEND_CRLF;
373 : 0 : break;
374 : :
375 : : case RTL_TEXTENCODING_APPLE_ARABIC:
376 : : case RTL_TEXTENCODING_APPLE_CENTEURO:
377 : : case RTL_TEXTENCODING_APPLE_CROATIAN:
378 : : case RTL_TEXTENCODING_APPLE_CYRILLIC:
379 : : case RTL_TEXTENCODING_APPLE_DEVANAGARI:
380 : : case RTL_TEXTENCODING_APPLE_FARSI:
381 : : case RTL_TEXTENCODING_APPLE_GREEK:
382 : : case RTL_TEXTENCODING_APPLE_GUJARATI:
383 : : case RTL_TEXTENCODING_APPLE_GURMUKHI:
384 : : case RTL_TEXTENCODING_APPLE_HEBREW:
385 : : case RTL_TEXTENCODING_APPLE_ICELAND:
386 : : case RTL_TEXTENCODING_APPLE_ROMANIAN:
387 : : case RTL_TEXTENCODING_APPLE_THAI:
388 : : case RTL_TEXTENCODING_APPLE_TURKISH:
389 : : case RTL_TEXTENCODING_APPLE_UKRAINIAN:
390 : : case RTL_TEXTENCODING_APPLE_CHINSIMP:
391 : : case RTL_TEXTENCODING_APPLE_CHINTRAD:
392 : : case RTL_TEXTENCODING_APPLE_JAPANESE:
393 : : case RTL_TEXTENCODING_APPLE_KOREAN:
394 : 0 : eEnd = LINEEND_CR;
395 : 0 : break;
396 : : }
397 : : }
398 : :
399 : 0 : bSaveLineStatus = sal_False;
400 : 0 : if( eEnd != (LineEnd)-1 ) // changed?
401 : : {
402 : 0 : if( eOldEnd != eEnd )
403 : 0 : SetCRLF( eEnd );
404 : : }
405 : : else
406 : : {
407 : : // restore old user choise (not the automatic!)
408 : 0 : aCRLF_RB.Check( aCRLF_RB.GetSavedValue() );
409 : 0 : aCR_RB.Check( aCR_RB.GetSavedValue() );
410 : 0 : aLF_RB.Check( aLF_RB.GetSavedValue() );
411 : : }
412 : 0 : bSaveLineStatus = sal_True;
413 : :
414 : 0 : if( nOldLng != nLng && aFontLB.IsVisible() )
415 : 0 : aLanguageLB.SelectLanguage( nLng );
416 : :
417 : 0 : return 0;
418 : : }
419 : :
420 : 0 : IMPL_LINK( SwAsciiFilterDlg, LineEndHdl, RadioButton*, pBtn )
421 : : {
422 : 0 : if( bSaveLineStatus )
423 : 0 : pBtn->SaveValue();
424 : 0 : return 0;
425 : : }
426 : :
427 : :
428 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|