xref: /aoo42x/main/sc/source/ui/dbgui/scuiasciiopt.cxx (revision a479921a)
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_scui.hxx"
26 
27 
28 #include "global.hxx"
29 #include "scresid.hxx"
30 #include "impex.hxx"
31 #include "scuiasciiopt.hxx"
32 #include "asciiopt.hrc"
33 #include <tools/debug.hxx>
34 #include <rtl/tencinfo.h>
35 #include <unotools/transliterationwrapper.hxx>
36 // ause
37 #include "editutil.hxx"
38 
39 #include <optutil.hxx>
40 #include <com/sun/star/uno/Any.hxx>
41 #include <com/sun/star/uno/Sequence.hxx>
42 #include "miscuno.hxx"
43 
44 //! TODO make dynamic
45 const SCSIZE ASCIIDLG_MAXROWS                = MAXROWCOUNT;
46 
47 
48 using namespace rtl;
49 using namespace com::sun::star::uno;
50 
51 // Defines - CSV Import Preserve Options
52 #define FIXED_WIDTH         "FixedWidth"
53 #define FROM_ROW            "FromRow"
54 #define CHAR_SET            "CharSet"
55 #define SEPARATORS          "Separators"
56 #define TEXT_SEPARATORS     "TextSeparators"
57 #define MERGE_DELIMITERS    "MergeDelimiters"
58 #define QUOTED_AS_TEXT      "QuotedFieldAsText"
59 #define DETECT_SPECIAL_NUM  "DetectSpecialNumbers"
60 #define LANGUAGE            "Language"
61 #define SEP_PATH            "Office.Calc/Dialogs/CSVImport"
62 
63 // ============================================================================
64 
lcl_FillCombo(ComboBox & rCombo,const String & rList,sal_Unicode cSelect)65 void lcl_FillCombo( ComboBox& rCombo, const String& rList, sal_Unicode cSelect )
66 {
67 	xub_StrLen i;
68 	xub_StrLen nCount = rList.GetTokenCount('\t');
69 	for ( i=0; i<nCount; i+=2 )
70 		rCombo.InsertEntry( rList.GetToken(i,'\t') );
71 
72 	if ( cSelect )
73 	{
74 		String aStr;
75 		for ( i=0; i<nCount; i+=2 )
76 			if ( (sal_Unicode)rList.GetToken(i+1,'\t').ToInt32() == cSelect )
77 				aStr = rList.GetToken(i,'\t');
78 		if (!aStr.Len())
79 			aStr = cSelect;			// Ascii
80 
81 		rCombo.SetText(aStr);
82 	}
83 }
84 
lcl_CharFromCombo(ComboBox & rCombo,const String & rList)85 sal_Unicode lcl_CharFromCombo( ComboBox& rCombo, const String& rList )
86 {
87 	sal_Unicode c = 0;
88 	String aStr = rCombo.GetText();
89 	if ( aStr.Len() )
90 	{
91 		xub_StrLen nCount = rList.GetTokenCount('\t');
92 		for ( xub_StrLen i=0; i<nCount; i+=2 )
93         {
94             if ( ScGlobal::GetpTransliteration()->isEqual( aStr, rList.GetToken(i,'\t') ) )//CHINA001 if ( ScGlobal::GetpTransliteration()->isEqual( aStr, rList.GetToken(i,'\t') ) )
95 				c = (sal_Unicode)rList.GetToken(i+1,'\t').ToInt32();
96         }
97         if (!c && aStr.Len())
98         {
99             sal_Unicode cFirst = aStr.GetChar( 0 );
100             // #i24235# first try the first character of the string directly
101             if( (aStr.Len() == 1) || (cFirst < '0') || (cFirst > '9') )
102                 c = cFirst;
103             else    // keep old behaviour for compatibility (i.e. "39" -> "'")
104                 c = (sal_Unicode) aStr.ToInt32();       // Ascii
105         }
106 	}
107 	return c;
108 }
109 
load_Separators(OUString & sFieldSeparators,OUString & sTextSeparators,bool & bMergeDelimiters,bool & bQuotedAsText,bool & bDetectSpecialNum,bool & bFixedWidth,sal_Int32 & nFromRow,sal_Int32 & nCharSet,sal_Int32 & nLanguage)110 static void load_Separators( OUString &sFieldSeparators, OUString &sTextSeparators,
111                              bool &bMergeDelimiters, bool& bQuotedAsText, bool& bDetectSpecialNum,
112                              bool &bFixedWidth, sal_Int32 &nFromRow, sal_Int32 &nCharSet,
113                              sal_Int32& nLanguage )
114 {
115     Sequence<Any>aValues;
116     const Any *pProperties;
117     Sequence<OUString> aNames(9);
118     OUString* pNames = aNames.getArray();
119     ScLinkConfigItem aItem( OUString::createFromAscii( SEP_PATH ) );
120 
121     pNames[0] = OUString::createFromAscii( MERGE_DELIMITERS );
122     pNames[1] = OUString::createFromAscii( SEPARATORS );
123     pNames[2] = OUString::createFromAscii( TEXT_SEPARATORS );
124     pNames[3] = OUString::createFromAscii( FIXED_WIDTH );
125     pNames[4] = OUString::createFromAscii( FROM_ROW );
126     pNames[5] = OUString::createFromAscii( CHAR_SET );
127     pNames[6] = OUString::createFromAscii( QUOTED_AS_TEXT );
128     pNames[7] = OUString::createFromAscii( DETECT_SPECIAL_NUM );
129     pNames[8] = OUString::createFromAscii( LANGUAGE );
130     aValues = aItem.GetProperties( aNames );
131     pProperties = aValues.getConstArray();
132     if( pProperties[1].hasValue() )
133         pProperties[1] >>= sFieldSeparators;
134 
135     if( pProperties[2].hasValue() )
136         pProperties[2] >>= sTextSeparators;
137 
138     if( pProperties[0].hasValue() )
139         bMergeDelimiters = ScUnoHelpFunctions::GetBoolFromAny( pProperties[0] );
140 
141     if( pProperties[3].hasValue() )
142         bFixedWidth = ScUnoHelpFunctions::GetBoolFromAny( pProperties[3] );
143 
144     if( pProperties[4].hasValue() )
145         pProperties[4] >>= nFromRow;
146 
147     if( pProperties[5].hasValue() )
148         pProperties[5] >>= nCharSet;
149 
150     if ( pProperties[6].hasValue() )
151         pProperties[6] >>= bQuotedAsText;
152 
153     if ( pProperties[7].hasValue() )
154         pProperties[7] >>= bDetectSpecialNum;
155 
156     if ( pProperties[8].hasValue() )
157         pProperties[8] >>= nLanguage;
158 }
159 
save_Separators(String maSeparators,String maTxtSep,bool bMergeDelimiters,bool bQuotedAsText,bool bDetectSpecialNum,bool bFixedWidth,sal_Int32 nFromRow,sal_Int32 nCharSet,sal_Int32 nLanguage)160 static void save_Separators(
161     String maSeparators, String maTxtSep, bool bMergeDelimiters, bool bQuotedAsText,
162     bool bDetectSpecialNum, bool bFixedWidth, sal_Int32 nFromRow, sal_Int32 nCharSet, sal_Int32 nLanguage )
163 {
164     OUString sFieldSeparators = OUString( maSeparators );
165     OUString sTextSeparators = OUString( maTxtSep );
166     Sequence<Any> aValues;
167     Any *pProperties;
168     Sequence<OUString> aNames(9);
169     OUString* pNames = aNames.getArray();
170     ScLinkConfigItem aItem( OUString::createFromAscii( SEP_PATH ) );
171 
172     pNames[0] = OUString::createFromAscii( MERGE_DELIMITERS );
173     pNames[1] = OUString::createFromAscii( SEPARATORS );
174     pNames[2] = OUString::createFromAscii( TEXT_SEPARATORS );
175     pNames[3] = OUString::createFromAscii( FIXED_WIDTH );
176     pNames[4] = OUString::createFromAscii( FROM_ROW );
177     pNames[5] = OUString::createFromAscii( CHAR_SET );
178     pNames[6] = OUString::createFromAscii( QUOTED_AS_TEXT );
179     pNames[7] = OUString::createFromAscii( DETECT_SPECIAL_NUM );
180     pNames[8] = OUString::createFromAscii( LANGUAGE );
181     aValues = aItem.GetProperties( aNames );
182     pProperties = aValues.getArray();
183     pProperties[1] <<= sFieldSeparators;
184     pProperties[2] <<= sTextSeparators;
185     ScUnoHelpFunctions::SetBoolInAny( pProperties[0], bMergeDelimiters );
186     ScUnoHelpFunctions::SetBoolInAny( pProperties[3], bFixedWidth );
187     pProperties[4] <<= nFromRow;
188     pProperties[5] <<= nCharSet;
189     pProperties[6] <<= static_cast<sal_Bool>(bQuotedAsText);
190     pProperties[7] <<= static_cast<sal_Bool>(bDetectSpecialNum);
191     pProperties[8] <<= nLanguage;
192 
193     aItem.PutProperties(aNames, aValues);
194 }
195 
196 // ----------------------------------------------------------------------------
197 
ScImportAsciiDlg(Window * pParent,String aDatName,SvStream * pInStream,sal_Unicode cSep)198 ScImportAsciiDlg::ScImportAsciiDlg( Window* pParent,String aDatName,
199                                     SvStream* pInStream, sal_Unicode cSep ) :
200 		ModalDialog	( pParent, ScResId( RID_SCDLG_ASCII ) ),
201         mpDatStream  ( pInStream ),
202         mnStreamPos( pInStream ? pInStream->Tell() : 0 ),
203 
204 		mpRowPosArray( NULL ),
205 		mnRowPosCount(0),
206 
207         aFlFieldOpt ( this, ScResId( FL_FIELDOPT ) ),
208 		aFtCharSet	( this, ScResId( FT_CHARSET ) ),
209 		aLbCharSet	( this, ScResId( LB_CHARSET ) ),
210         aFtCustomLang( this, ScResId( FT_CUSTOMLANG ) ),
211         aLbCustomLang( this, ScResId( LB_CUSTOMLANG ) ),
212 
213 		aFtRow		( this, ScResId( FT_AT_ROW	) ),
214 		aNfRow		( this,	ScResId( NF_AT_ROW	) ),
215 
216         aFlSepOpt   ( this, ScResId( FL_SEPOPT ) ),
217 		aRbFixed	( this, ScResId( RB_FIXED ) ),
218 		aRbSeparated( this, ScResId( RB_SEPARATED ) ),
219 
220 		aCkbTab		( this, ScResId( CKB_TAB ) ),
221 		aCkbSemicolon(this, ScResId( CKB_SEMICOLON ) ),
222 		aCkbComma	( this, ScResId( CKB_COMMA	) ),
223 		aCkbSpace	( this,	ScResId( CKB_SPACE	 ) ),
224 		aCkbOther	( this, ScResId( CKB_OTHER ) ),
225 		aEdOther	( this, ScResId( ED_OTHER ) ),
226 		aCkbAsOnce	( this, ScResId( CB_ASONCE) ),
227         aFlOtherOpt ( this, ScResId( FL_OTHER_OPTIONS ) ),
228 
229 		aFtTextSep	( this, ScResId( FT_TEXTSEP ) ),
230 		aCbTextSep	( this, ScResId( CB_TEXTSEP ) ),
231 
232         aCkbQuotedAsText( this, ScResId(CB_QUOTED_AS_TEXT) ),
233         aCkbDetectNumber( this, ScResId(CB_DETECT_SPECIAL_NUMBER) ),
234 
235         aFlWidth    ( this, ScResId( FL_WIDTH ) ),
236 		aFtType		( this, ScResId( FT_TYPE ) ),
237 		aLbType		( this, ScResId( LB_TYPE1 ) ),
238 
239         maTableBox  ( this, ScResId( CTR_TABLEBOX ) ),
240 
241 		aBtnOk		( this, ScResId( BTN_OK ) ),
242 		aBtnCancel	( this, ScResId( BTN_CANCEL ) ),
243 		aBtnHelp	( this, ScResId( BTN_HELP ) ),
244 
245 		aCharSetUser( ScResId( SCSTR_CHARSET_USER ) ),
246 		aColumnUser	( ScResId( SCSTR_COLUMN_USER ) ),
247 		aTextSepList( ScResId( SCSTR_TEXTSEP ) ),
248         mcTextSep   ( ScAsciiOptions::cDefaultTextSep ),
249         maStrTextToColumns( ScResId( STR_TEXTTOCOLUMNS ) ),
250         mbFileImport(true)
251 {
252 	FreeResource();
253     mbFileImport = aDatName.Len() > 0;
254 
255 	String aName = GetText();
256     // aDatName is empty if invoked during paste from clipboard.
257     if (mbFileImport)
258     {
259         aName.AppendAscii(RTL_CONSTASCII_STRINGPARAM(" - ["));
260         aName += aDatName;
261         aName += ']';
262     }
263 	SetText( aName );
264 
265     OUString sFieldSeparators;
266     OUString sTextSeparators;
267     bool bMergeDelimiters = false;
268     bool bFixedWidth = false;
269     bool bQuotedFieldAsText = false;
270     bool bDetectSpecialNum = false;
271     sal_Int32 nFromRow = 1;
272     sal_Int32 nCharSet = -1;
273     sal_Int32 nLanguage = 0;
274     if (mbFileImport)
275         // load separators only when importing csv files.
276         load_Separators (sFieldSeparators, sTextSeparators, bMergeDelimiters,
277                          bQuotedFieldAsText, bDetectSpecialNum, bFixedWidth, nFromRow, nCharSet, nLanguage);
278     else
279     {
280         // #i115474# otherwise use sensible defaults
281         sFieldSeparators = OUString( cSep );
282         sTextSeparators = OUString( ScAsciiOptions::cDefaultTextSep );
283     }
284     maFieldSeparators = String(sFieldSeparators);
285 
286     if( bMergeDelimiters )
287         aCkbAsOnce.Check();
288     if (bQuotedFieldAsText)
289         aCkbQuotedAsText.Check();
290     if (bDetectSpecialNum)
291         aCkbDetectNumber.Check();
292     if( bFixedWidth )
293         aRbFixed.Check();
294     if( nFromRow != 1 )
295         aNfRow.SetValue( nFromRow );
296 
297     ByteString bString(maFieldSeparators,RTL_TEXTENCODING_MS_1252);
298     const sal_Char *aSep = bString.GetBuffer();
299     int len = maFieldSeparators.Len();
300     for (int i = 0; i < len; ++i)
301     {
302         switch( aSep[i] )
303         {
304             case '\t':  aCkbTab.Check();        break;
305             case ';':   aCkbSemicolon.Check();  break;
306             case ',':   aCkbComma.Check();      break;
307             case ' ':   aCkbSpace.Check();      break;
308             default:
309                 aCkbOther.Check();
310                 aEdOther.SetText( aEdOther.GetText() + OUString( aSep[i] ) );
311         }
312     }
313 
314     // Get Separators from the dialog
315     maFieldSeparators = GetSeparators();
316 
317     // Clipboard is always Unicode, else detect.
318     rtl_TextEncoding ePreselectUnicode = (mbFileImport ?
319             RTL_TEXTENCODING_DONTKNOW : RTL_TEXTENCODING_UNICODE);
320 	// Sniff for Unicode / not
321     if( ePreselectUnicode == RTL_TEXTENCODING_DONTKNOW && mpDatStream )
322 	{
323 		Seek( 0 );
324 		mpDatStream->StartReadingUnicodeText( RTL_TEXTENCODING_DONTKNOW );
325 		sal_uLong nUniPos = mpDatStream->Tell();
326         switch (nUniPos)
327         {
328             case 2:
329                 ePreselectUnicode = RTL_TEXTENCODING_UNICODE;   // UTF-16
330                 break;
331             case 3:
332                 ePreselectUnicode = RTL_TEXTENCODING_UTF8;      // UTF-8
333                 break;
334             case 0:
335                 {
336                     sal_uInt16 n;
337                     *mpDatStream >> n;
338                     // Assume that normal ASCII/ANSI/ISO/etc. text doesn't start with
339                     // control characters except CR,LF,TAB
340                     if ( (n & 0xff00) < 0x2000 )
341                     {
342                         switch ( n & 0xff00 )
343                         {
344                             case 0x0900 :
345                             case 0x0a00 :
346                             case 0x0d00 :
347                                 break;
348                             default:
349                                 ePreselectUnicode = RTL_TEXTENCODING_UNICODE;   // UTF-16
350                         }
351                     }
352                     mpDatStream->Seek(0);
353                 }
354                 break;
355             default:
356                 ;   // nothing
357         }
358 		mnStreamPos = mpDatStream->Tell();
359 	}
360 
361     aNfRow.SetModifyHdl( LINK( this, ScImportAsciiDlg, FirstRowHdl ) );
362 
363     // *** Separator characters ***
364     lcl_FillCombo( aCbTextSep, aTextSepList, mcTextSep );
365     aCbTextSep.SetText( sTextSeparators );
366 
367     Link aSeparatorHdl =LINK( this, ScImportAsciiDlg, SeparatorHdl );
368     aCbTextSep.SetSelectHdl( aSeparatorHdl );
369     aCbTextSep.SetModifyHdl( aSeparatorHdl );
370     aCkbTab.SetClickHdl( aSeparatorHdl );
371     aCkbSemicolon.SetClickHdl( aSeparatorHdl );
372     aCkbComma.SetClickHdl( aSeparatorHdl );
373     aCkbAsOnce.SetClickHdl( aSeparatorHdl );
374     aCkbQuotedAsText.SetClickHdl( aSeparatorHdl );
375     aCkbDetectNumber.SetClickHdl( aSeparatorHdl );
376     aCkbSpace.SetClickHdl( aSeparatorHdl );
377     aCkbOther.SetClickHdl( aSeparatorHdl );
378     aEdOther.SetModifyHdl( aSeparatorHdl );
379 
380     // *** text encoding ListBox ***
381 	// all encodings allowed, including Unicode, but subsets are excluded
382 	aLbCharSet.FillFromTextEncodingTable( sal_True );
383 	// Insert one "SYSTEM" entry for compatibility in AsciiOptions and system
384 	// independent document linkage.
385 	aLbCharSet.InsertTextEncoding( RTL_TEXTENCODING_DONTKNOW, aCharSetUser );
386 	aLbCharSet.SelectTextEncoding( ePreselectUnicode == RTL_TEXTENCODING_DONTKNOW ?
387             gsl_getSystemTextEncoding() : ePreselectUnicode );
388 
389     if( nCharSet >= 0 && ePreselectUnicode == RTL_TEXTENCODING_DONTKNOW )
390         aLbCharSet.SelectEntryPos( static_cast<sal_uInt16>(nCharSet) );
391 
392     SetSelectedCharSet();
393 	aLbCharSet.SetSelectHdl( LINK( this, ScImportAsciiDlg, CharSetHdl ) );
394 
395     aLbCustomLang.SetLanguageList(
396         LANG_LIST_ALL | LANG_LIST_ONLY_KNOWN, false, false);
397     aLbCustomLang.InsertLanguage(LANGUAGE_SYSTEM);
398     aLbCustomLang.SelectLanguage(static_cast<LanguageType>(nLanguage), true);
399 
400     // *** column type ListBox ***
401 	xub_StrLen nCount = aColumnUser.GetTokenCount();
402 	for (xub_StrLen i=0; i<nCount; i++)
403         aLbType.InsertEntry( aColumnUser.GetToken( i ) );
404 
405     aLbType.SetSelectHdl( LINK( this, ScImportAsciiDlg, LbColTypeHdl ) );
406     aFtType.Disable();
407     aLbType.Disable();
408 
409     // *** table box preview ***
410     maTableBox.SetUpdateTextHdl( LINK( this, ScImportAsciiDlg, UpdateTextHdl ) );
411     maTableBox.InitTypes( aLbType );
412     maTableBox.SetColTypeHdl( LINK( this, ScImportAsciiDlg, ColTypeHdl ) );
413 
414     aRbSeparated.SetClickHdl( LINK( this, ScImportAsciiDlg, RbSepFixHdl ) );
415     aRbFixed.SetClickHdl( LINK( this, ScImportAsciiDlg, RbSepFixHdl ) );
416 
417     SetupSeparatorCtrls();
418     RbSepFixHdl( &aRbFixed );
419 
420 	UpdateVertical();
421 
422     maTableBox.Execute( CSVCMD_NEWCELLTEXTS );
423 
424 	aEdOther.SetAccessibleName(aCkbOther.GetText());
425 	aEdOther.SetAccessibleRelationLabeledBy(&aCkbOther);
426 }
427 
428 
~ScImportAsciiDlg()429 ScImportAsciiDlg::~ScImportAsciiDlg()
430 {
431 	delete[] mpRowPosArray;
432 }
433 
434 
435 // ----------------------------------------------------------------------------
436 
GetLine(sal_uLong nLine,String & rText)437 bool ScImportAsciiDlg::GetLine( sal_uLong nLine, String &rText )
438 {
439     if (nLine >= ASCIIDLG_MAXROWS || !mpDatStream)
440         return false;
441 
442     bool bRet = true;
443     bool bFixed = aRbFixed.IsChecked();
444 
445     if (!mpRowPosArray)
446         mpRowPosArray = new sal_uLong[ASCIIDLG_MAXROWS + 2];
447 
448     if (!mnRowPosCount) // complete re-fresh
449     {
450         memset( mpRowPosArray, 0, sizeof(mpRowPosArray[0]) * (ASCIIDLG_MAXROWS+2));
451 
452         Seek(0);
453         mpDatStream->StartReadingUnicodeText( mpDatStream->GetStreamCharSet() );
454 
455         mnStreamPos = mpDatStream->Tell();
456         mpRowPosArray[mnRowPosCount] = mnStreamPos;
457     }
458 
459     if (nLine >= mnRowPosCount)
460     {
461         // need to work out some more line information
462         do
463         {
464             if (!Seek( mpRowPosArray[mnRowPosCount]) ||
465                     mpDatStream->GetError() != ERRCODE_NONE ||
466                     mpDatStream->IsEof())
467             {
468                 bRet = false;
469                 break;
470             }
471             mpDatStream->ReadCsvLine( rText, !bFixed, maFieldSeparators,
472                     mcTextSep);
473             mnStreamPos = mpDatStream->Tell();
474             mpRowPosArray[++mnRowPosCount] = mnStreamPos;
475         } while (nLine >= mnRowPosCount &&
476                 mpDatStream->GetError() == ERRCODE_NONE &&
477                 !mpDatStream->IsEof());
478         if (mpDatStream->IsEof() &&
479                 mnStreamPos == mpRowPosArray[mnRowPosCount-1])
480         {
481             // the very end, not even an empty line read
482             bRet = false;
483             --mnRowPosCount;
484         }
485     }
486     else
487     {
488         Seek( mpRowPosArray[nLine]);
489         mpDatStream->ReadCsvLine( rText, !bFixed, maFieldSeparators, mcTextSep);
490         mnStreamPos = mpDatStream->Tell();
491     }
492 
493     //	#107455# If the file content isn't unicode, ReadUniStringLine
494     //	may try to seek beyond the file's end and cause a CANTSEEK error
495     //	(depending on the stream type). The error code has to be cleared,
496     //	or further read operations (including non-unicode) will fail.
497     if ( mpDatStream->GetError() == ERRCODE_IO_CANTSEEK )
498         mpDatStream->ResetError();
499 
500     return bRet;
501 }
502 
503 
GetOptions(ScAsciiOptions & rOpt)504 void ScImportAsciiDlg::GetOptions( ScAsciiOptions& rOpt )
505 {
506     rOpt.SetCharSet( meCharSet );
507     rOpt.SetCharSetSystem( mbCharSetSystem );
508     rOpt.SetLanguage(aLbCustomLang.GetSelectLanguage());
509     rOpt.SetFixedLen( aRbFixed.IsChecked() );
510     rOpt.SetStartRow( (long)aNfRow.GetValue() );
511     maTableBox.FillColumnData( rOpt );
512     if( aRbSeparated.IsChecked() )
513     {
514         rOpt.SetFieldSeps( GetSeparators() );
515         rOpt.SetMergeSeps( aCkbAsOnce.IsChecked() );
516         rOpt.SetTextSep( lcl_CharFromCombo( aCbTextSep, aTextSepList ) );
517     }
518 
519     rOpt.SetQuotedAsText(aCkbQuotedAsText.IsChecked());
520     rOpt.SetDetectSpecialNumber(aCkbDetectNumber.IsChecked());
521 }
522 
SetTextToColumnsMode()523 void ScImportAsciiDlg::SetTextToColumnsMode()
524 {
525     SetText( maStrTextToColumns );
526     aFtCharSet.Disable();
527     aLbCharSet.Disable();
528     aFtCustomLang.Disable();
529     aLbCustomLang.SelectLanguage(LANGUAGE_SYSTEM);
530     aLbCustomLang.Disable();
531     aFtRow.Disable();
532     aNfRow.Disable();
533 
534     // Quoted field as text option is not used for text-to-columns mode.
535     aCkbQuotedAsText.Check(false);
536     aCkbQuotedAsText.Disable();
537 
538     // Always detect special numbers for text-to-columns mode.
539     aCkbDetectNumber.Check();
540     aCkbDetectNumber.Disable();
541 }
542 
SaveParameters()543 void ScImportAsciiDlg::SaveParameters()
544 {
545     if (!mbFileImport)
546         // We save parameters only for file import.
547         return;
548 
549     save_Separators( maFieldSeparators, aCbTextSep.GetText(), aCkbAsOnce.IsChecked(),
550                      aCkbQuotedAsText.IsChecked(), aCkbDetectNumber.IsChecked(),
551                      aRbFixed.IsChecked(),
552                      static_cast<sal_Int32>(aNfRow.GetValue()),
553                      static_cast<sal_Int32>(aLbCharSet.GetSelectEntryPos()),
554                      static_cast<sal_Int32>(aLbCustomLang.GetSelectLanguage()) );
555 }
556 
SetSelectedCharSet()557 void ScImportAsciiDlg::SetSelectedCharSet()
558 {
559     meCharSet = aLbCharSet.GetSelectTextEncoding();
560     mbCharSetSystem = (meCharSet == RTL_TEXTENCODING_DONTKNOW);
561     if( mbCharSetSystem )
562         meCharSet = gsl_getSystemTextEncoding();
563 }
564 
GetSeparators() const565 String ScImportAsciiDlg::GetSeparators() const
566 {
567     String aSepChars;
568     if( aCkbTab.IsChecked() )
569         aSepChars += '\t';
570     if( aCkbSemicolon.IsChecked() )
571         aSepChars += ';';
572     if( aCkbComma.IsChecked() )
573         aSepChars += ',';
574     if( aCkbSpace.IsChecked() )
575         aSepChars += ' ';
576     if( aCkbOther.IsChecked() )
577         aSepChars += aEdOther.GetText();
578     return aSepChars;
579 }
580 
SetupSeparatorCtrls()581 void ScImportAsciiDlg::SetupSeparatorCtrls()
582 {
583     sal_Bool bEnable = aRbSeparated.IsChecked();
584     aCkbTab.Enable( bEnable );
585     aCkbSemicolon.Enable( bEnable );
586     aCkbComma.Enable( bEnable );
587     aCkbSpace.Enable( bEnable );
588     aCkbOther.Enable( bEnable );
589     aEdOther.Enable( bEnable );
590     aCkbAsOnce.Enable( bEnable );
591     aFtTextSep.Enable( bEnable );
592     aCbTextSep.Enable( bEnable );
593 }
594 
UpdateVertical()595 void ScImportAsciiDlg::UpdateVertical()
596 {
597     mnRowPosCount = 0;
598     if (mpDatStream)
599         mpDatStream->SetStreamCharSet(meCharSet);
600 }
601 
602 
603 // ----------------------------------------------------------------------------
604 
IMPL_LINK(ScImportAsciiDlg,RbSepFixHdl,RadioButton *,pButton)605 IMPL_LINK( ScImportAsciiDlg, RbSepFixHdl, RadioButton*, pButton )
606 {
607     DBG_ASSERT( pButton, "ScImportAsciiDlg::RbSepFixHdl - missing sender" );
608 
609     if( (pButton == &aRbFixed) || (pButton == &aRbSeparated) )
610 	{
611         SetPointer( Pointer( POINTER_WAIT ) );
612         if( aRbFixed.IsChecked() )
613             maTableBox.SetFixedWidthMode();
614         else
615             maTableBox.SetSeparatorsMode();
616         SetPointer( Pointer( POINTER_ARROW ) );
617 
618         SetupSeparatorCtrls();
619 	}
620 	return 0;
621 }
622 
IMPL_LINK(ScImportAsciiDlg,SeparatorHdl,Control *,pCtrl)623 IMPL_LINK( ScImportAsciiDlg, SeparatorHdl, Control*, pCtrl )
624 {
625     DBG_ASSERT( pCtrl, "ScImportAsciiDlg::SeparatorHdl - missing sender" );
626     DBG_ASSERT( !aRbFixed.IsChecked(), "ScImportAsciiDlg::SeparatorHdl - not allowed in fixed width" );
627 
628     /*  #i41550# First update state of the controls. The GetSeparators()
629         function needs final state of the check boxes. */
630     if( (pCtrl == &aCkbOther) && aCkbOther.IsChecked() )
631         aEdOther.GrabFocus();
632     else if( pCtrl == &aEdOther )
633         aCkbOther.Check( aEdOther.GetText().Len() > 0 );
634 
635     String aOldFldSeps( maFieldSeparators);
636     maFieldSeparators = GetSeparators();
637     sal_Unicode cOldSep = mcTextSep;
638     mcTextSep = lcl_CharFromCombo( aCbTextSep, aTextSepList );
639     // Any separator changed may result in completely different lines due to
640     // embedded line breaks.
641     if (cOldSep != mcTextSep || aOldFldSeps != maFieldSeparators)
642         UpdateVertical();
643 
644     maTableBox.Execute( CSVCMD_NEWCELLTEXTS );
645 	return 0;
646 }
647 
IMPL_LINK(ScImportAsciiDlg,CharSetHdl,SvxTextEncodingBox *,pCharSetBox)648 IMPL_LINK( ScImportAsciiDlg, CharSetHdl, SvxTextEncodingBox*, pCharSetBox )
649 {
650     DBG_ASSERT( pCharSetBox, "ScImportAsciiDlg::CharSetHdl - missing sender" );
651 
652     if( (pCharSetBox == &aLbCharSet) && (pCharSetBox->GetSelectEntryCount() == 1) )
653     {
654         SetPointer( Pointer( POINTER_WAIT ) );
655         CharSet eOldCharSet = meCharSet;
656         SetSelectedCharSet();
657         // switching char-set invalidates 8bit -> String conversions
658         if (eOldCharSet != meCharSet)
659             UpdateVertical();
660 
661         maTableBox.Execute( CSVCMD_NEWCELLTEXTS );
662         SetPointer( Pointer( POINTER_ARROW ) );
663     }
664 	return 0;
665 }
666 
IMPL_LINK(ScImportAsciiDlg,FirstRowHdl,NumericField *,pNumField)667 IMPL_LINK( ScImportAsciiDlg, FirstRowHdl, NumericField*, pNumField )
668 {
669     DBG_ASSERT( pNumField, "ScImportAsciiDlg::FirstRowHdl - missing sender" );
670     maTableBox.Execute( CSVCMD_SETFIRSTIMPORTLINE, sal::static_int_cast<sal_Int32>( pNumField->GetValue() - 1 ) );
671     return 0;
672 }
673 
IMPL_LINK(ScImportAsciiDlg,LbColTypeHdl,ListBox *,pListBox)674 IMPL_LINK( ScImportAsciiDlg, LbColTypeHdl, ListBox*, pListBox )
675 {
676     DBG_ASSERT( pListBox, "ScImportAsciiDlg::LbColTypeHdl - missing sender" );
677     if( pListBox == &aLbType )
678         maTableBox.Execute( CSVCMD_SETCOLUMNTYPE, pListBox->GetSelectEntryPos() );
679 	return 0;
680 }
681 
IMPL_LINK(ScImportAsciiDlg,UpdateTextHdl,ScCsvTableBox *,EMPTYARG)682 IMPL_LINK( ScImportAsciiDlg, UpdateTextHdl, ScCsvTableBox*, EMPTYARG )
683 {
684     sal_Int32 nBaseLine = maTableBox.GetFirstVisLine();
685     sal_Int32 nRead = maTableBox.GetVisLineCount();
686     // If mnRowPosCount==0, this is an initializing call, read ahead for row
687     // count and resulting scroll bar size and position to be able to scroll at
688     // all. When adding lines, read only the amount of next lines to be
689     // displayed.
690     if (!mnRowPosCount || nRead > CSV_PREVIEW_LINES)
691         nRead = CSV_PREVIEW_LINES;
692 
693     sal_Int32 i;
694     for (i = 0; i < nRead; i++)
695     {
696         if (!GetLine( nBaseLine + i, maPreviewLine[i]))
697             break;
698     }
699     for (; i < CSV_PREVIEW_LINES; i++)
700         maPreviewLine[i].Erase();
701 
702     maTableBox.Execute( CSVCMD_SETLINECOUNT, mnRowPosCount);
703     bool bMergeSep = (aCkbAsOnce.IsChecked() == sal_True);
704     maTableBox.SetUniStrings( maPreviewLine, maFieldSeparators, mcTextSep, bMergeSep);
705 
706     return 0;
707 }
708 
IMPL_LINK(ScImportAsciiDlg,ColTypeHdl,ScCsvTableBox *,pTableBox)709 IMPL_LINK( ScImportAsciiDlg, ColTypeHdl, ScCsvTableBox*, pTableBox )
710 {
711     DBG_ASSERT( pTableBox, "ScImportAsciiDlg::ColTypeHdl - missing sender" );
712 
713     sal_Int32 nType = pTableBox->GetSelColumnType();
714     sal_Int32 nTypeCount = aLbType.GetEntryCount();
715     bool bEmpty = (nType == CSV_TYPE_MULTI);
716     bool bEnable = ((0 <= nType) && (nType < nTypeCount)) || bEmpty;
717 
718     aFtType.Enable( bEnable );
719     aLbType.Enable( bEnable );
720 
721     Link aSelHdl = aLbType.GetSelectHdl();
722     aLbType.SetSelectHdl( Link() );
723     if( bEmpty )
724         aLbType.SetNoSelection();
725     else if( bEnable )
726         aLbType.SelectEntryPos( static_cast< sal_uInt16 >( nType ) );
727     aLbType.SetSelectHdl( aSelHdl );
728 
729     return 0;
730 }
731