fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / sc / source / ui / dbgui / scuiasciiopt.cxx
blob5f990a93a7120d6aa48617fda79775afeac355f7
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #undef SC_DLLIMPLEMENTATION
22 #include "global.hxx"
23 #include "scresid.hxx"
24 #include "impex.hxx"
25 #include "scuiasciiopt.hxx"
26 #include "asciiopt.hrc"
27 #include <comphelper/string.hxx>
28 #include <osl/thread.h>
29 #include <rtl/tencinfo.h>
30 #include <unotools/transliterationwrapper.hxx>
31 #include "editutil.hxx"
33 #include <optutil.hxx>
34 #include <com/sun/star/uno/Any.hxx>
35 #include <com/sun/star/uno/Sequence.hxx>
36 #include "miscuno.hxx"
37 #include <tools/urlobj.hxx>
39 //! TODO make dynamic
40 const SCSIZE ASCIIDLG_MAXROWS = MAXROWCOUNT;
42 using namespace com::sun::star::uno;
44 // Defines - CSV Import Preserve Options
45 #define FIXED_WIDTH "FixedWidth"
46 #define FROM_ROW "FromRow"
47 #define CHAR_SET "CharSet"
48 #define SEPARATORS "Separators"
49 #define TEXT_SEPARATORS "TextSeparators"
50 #define MERGE_DELIMITERS "MergeDelimiters"
51 #define QUOTED_AS_TEXT "QuotedFieldAsText"
52 #define DETECT_SPECIAL_NUM "DetectSpecialNumbers"
53 #define LANGUAGE "Language"
54 #define SEP_PATH "Office.Calc/Dialogs/CSVImport"
55 #define SEP_PATH_CLPBRD "Office.Calc/Dialogs/ClipboardTextImport"
56 #define SEP_PATH_TEXT2COL "Office.Calc/Dialogs/TextToColumnsImport"
58 static void lcl_FillCombo( ComboBox& rCombo, const OUString& rList, sal_Unicode cSelect )
60 sal_Int32 i;
61 sal_Int32 nCount = comphelper::string::getTokenCount(rList, '\t');
62 for ( i=0; i<nCount; i+=2 )
63 rCombo.InsertEntry( rList.getToken(i,'\t') );
65 if ( cSelect )
67 OUString aStr;
68 for ( i=0; i<nCount; i+=2 )
69 if ( (sal_Unicode)rList.getToken(i+1,'\t').toInt32() == cSelect )
70 aStr = rList.getToken(i,'\t');
71 if (aStr.isEmpty())
72 aStr = OUString(cSelect); // Ascii
74 rCombo.SetText(aStr);
78 static sal_Unicode lcl_CharFromCombo( ComboBox& rCombo, const OUString& rList )
80 sal_Unicode c = 0;
81 OUString aStr = rCombo.GetText();
82 if ( !aStr.isEmpty() )
84 sal_Int32 nCount = comphelper::string::getTokenCount(rList, '\t');
85 for ( sal_Int32 i=0; i<nCount; i+=2 )
87 if ( ScGlobal::GetpTransliteration()->isEqual( aStr, rList.getToken(i,'\t') ) )
88 c = (sal_Unicode)rList.getToken(i+1,'\t').toInt32();
90 if (!c && !aStr.isEmpty())
92 sal_Unicode cFirst = aStr[0];
93 // #i24235# first try the first character of the string directly
94 if( (aStr.getLength() == 1) || (cFirst < '0') || (cFirst > '9') )
95 c = cFirst;
96 else // keep old behaviour for compatibility (i.e. "39" -> "'")
97 c = (sal_Unicode) aStr.toInt32(); // Ascii
100 return c;
103 static void load_Separators( OUString &sFieldSeparators, OUString &sTextSeparators,
104 bool &bMergeDelimiters, bool& bQuotedAsText, bool& bDetectSpecialNum,
105 bool &bFixedWidth, sal_Int32 &nFromRow, sal_Int32 &nCharSet,
106 sal_Int32& nLanguage, ScImportAsciiCall eCall )
108 Sequence<Any>aValues;
109 const Any *pProperties;
110 Sequence<OUString> aNames( eCall == SC_TEXTTOCOLUMNS ? 4 : 9 );
111 OUString* pNames = aNames.getArray();
112 OUString aSepPath;
113 switch(eCall)
115 case SC_IMPORTFILE:
116 aSepPath = SEP_PATH;
117 break;
118 case SC_PASTETEXT:
119 aSepPath = SEP_PATH_CLPBRD;
120 break;
121 case SC_TEXTTOCOLUMNS:
122 default:
123 aSepPath = SEP_PATH_TEXT2COL;
124 break;
126 ScLinkConfigItem aItem( aSepPath );
128 pNames[0] = MERGE_DELIMITERS;
129 pNames[1] = SEPARATORS;
130 pNames[2] = TEXT_SEPARATORS;
131 pNames[3] = FIXED_WIDTH;
132 if (eCall != SC_TEXTTOCOLUMNS)
134 pNames[4] = FROM_ROW;
135 pNames[5] = CHAR_SET;
136 pNames[6] = QUOTED_AS_TEXT;
137 pNames[7] = DETECT_SPECIAL_NUM;
138 pNames[8] = LANGUAGE;
140 aValues = aItem.GetProperties( aNames );
141 pProperties = aValues.getConstArray();
143 if( pProperties[0].hasValue() )
144 bMergeDelimiters = ScUnoHelpFunctions::GetBoolFromAny( pProperties[0] );
146 if( pProperties[1].hasValue() )
147 pProperties[1] >>= sFieldSeparators;
149 if( pProperties[2].hasValue() )
150 pProperties[2] >>= sTextSeparators;
152 if( pProperties[3].hasValue() )
153 bFixedWidth = ScUnoHelpFunctions::GetBoolFromAny( pProperties[3] );
155 if (eCall != SC_TEXTTOCOLUMNS)
157 if( pProperties[4].hasValue() )
158 pProperties[4] >>= nFromRow;
160 if( pProperties[5].hasValue() )
161 pProperties[5] >>= nCharSet;
163 if ( pProperties[6].hasValue() )
164 pProperties[6] >>= bQuotedAsText;
166 if ( pProperties[7].hasValue() )
167 pProperties[7] >>= bDetectSpecialNum;
169 if ( pProperties[8].hasValue() )
170 pProperties[8] >>= nLanguage;
174 static void save_Separators(
175 const OUString& maSeparators, const OUString& maTxtSep, bool bMergeDelimiters, bool bQuotedAsText,
176 bool bDetectSpecialNum, bool bFixedWidth, sal_Int32 nFromRow,
177 sal_Int32 nCharSet, sal_Int32 nLanguage, ScImportAsciiCall eCall )
179 OUString sFieldSeparators = OUString( maSeparators );
180 OUString sTextSeparators = OUString( maTxtSep );
181 Sequence<Any> aValues;
182 Any *pProperties;
183 Sequence<OUString> aNames( eCall == SC_TEXTTOCOLUMNS ? 4 : 9 );
184 OUString* pNames = aNames.getArray();
185 OUString aSepPath;
186 switch(eCall)
188 case SC_IMPORTFILE:
189 aSepPath = SEP_PATH;
190 break;
191 case SC_PASTETEXT:
192 aSepPath = SEP_PATH_CLPBRD;
193 break;
194 case SC_TEXTTOCOLUMNS:
195 default:
196 aSepPath = SEP_PATH_TEXT2COL;
197 break;
199 ScLinkConfigItem aItem( aSepPath );
201 pNames[0] = MERGE_DELIMITERS;
202 pNames[1] = SEPARATORS;
203 pNames[2] = TEXT_SEPARATORS;
204 pNames[3] = FIXED_WIDTH;
205 if (eCall != SC_TEXTTOCOLUMNS)
207 pNames[4] = FROM_ROW;
208 pNames[5] = CHAR_SET;
209 pNames[6] = QUOTED_AS_TEXT;
210 pNames[7] = DETECT_SPECIAL_NUM;
211 pNames[8] = LANGUAGE;
213 aValues = aItem.GetProperties( aNames );
214 pProperties = aValues.getArray();
215 ScUnoHelpFunctions::SetBoolInAny( pProperties[0], bMergeDelimiters );
216 pProperties[1] <<= sFieldSeparators;
217 pProperties[2] <<= sTextSeparators;
218 ScUnoHelpFunctions::SetBoolInAny( pProperties[3], bFixedWidth );
219 if (eCall != SC_TEXTTOCOLUMNS)
221 pProperties[4] <<= nFromRow;
222 pProperties[5] <<= nCharSet;
223 pProperties[6] <<= bQuotedAsText;
224 pProperties[7] <<= bDetectSpecialNum;
225 pProperties[8] <<= nLanguage;
228 aItem.PutProperties(aNames, aValues);
231 ScImportAsciiDlg::ScImportAsciiDlg( vcl::Window* pParent, const OUString& aDatName,
232 SvStream* pInStream, ScImportAsciiCall eCall ) :
233 ModalDialog (pParent, "TextImportCsvDialog",
234 "modules/scalc/ui/textimportcsv.ui"),
235 mpDatStream ( pInStream ),
236 mnStreamPos( pInStream ? pInStream->Tell() : 0 ),
238 mpRowPosArray( NULL ),
239 mnRowPosCount(0),
241 aCharSetUser( ScResId( SCSTR_CHARSET_USER ) ),
242 aColumnUser ( ScResId( SCSTR_COLUMN_USER ) ),
243 aTextSepList( ScResId( SCSTR_TEXTSEP ) ),
244 mcTextSep ( ScAsciiOptions::cDefaultTextSep ),
245 meCall(eCall)
247 get(pFtCharSet, "textcharset");
248 get(pLbCharSet, "charset");
249 pLbCharSet->SetStyle(pLbCharSet->GetStyle() | WB_SORT);
250 get(pFtCustomLang, "textlanguage");
251 get(pLbCustomLang, "language");
252 pLbCustomLang->SetStyle(pLbCustomLang->GetStyle() | WB_SORT);
253 get(pFtRow, "textfromrow");
254 get(pNfRow, "fromrow");
255 get(pRbFixed, "tofixedwidth");
256 get(pRbSeparated, "toseparatedby");
257 get(pCkbTab, "tab");
258 get(pCkbSemicolon, "semicolon");
259 get(pCkbComma, "comma");
260 get(pCkbSpace, "space");
261 get(pCkbOther, "other");
262 get(pEdOther, "inputother");
263 get(pCkbAsOnce, "mergedelimiters");
264 get(pFtTextSep, "texttextdelimiter");
265 get(pCbTextSep, "textdelimiter");
266 get(pCkbQuotedAsText, "quotedfieldastext");
267 get(pCkbDetectNumber, "detectspecialnumbers");
268 get(pFtType, "textcolumntype");
269 get(pLbType, "columntype");
270 get(mpTableBox, "scrolledwindowcolumntype");
272 OUString aName = GetText();
273 switch (meCall)
275 case SC_TEXTTOCOLUMNS:
276 SetText( get<FixedText>("textalttitle")->GetText() );
277 break;
278 case SC_IMPORTFILE:
279 aName += " - [" + aDatName + "]";
280 SetText( aName );
281 break;
282 default:
283 break;
286 // To be able to prefill the correct values based on the file extension
287 bool bIsTSV = (aDatName.endsWithIgnoreAsciiCase(".tsv") || aDatName.endsWithIgnoreAsciiCase(".tab"));
289 // Default options are set in officecfg/registry/schema/org/openoffice/Office/Calc.xcs
290 OUString sFieldSeparators(",;\t");
291 OUString sTextSeparators(mcTextSep);
292 bool bMergeDelimiters = false;
293 bool bFixedWidth = false;
294 bool bQuotedFieldAsText = false;
295 bool bDetectSpecialNum = true;
296 sal_Int32 nFromRow = 1;
297 sal_Int32 nCharSet = -1;
298 sal_Int32 nLanguage = 0;
299 load_Separators (sFieldSeparators, sTextSeparators, bMergeDelimiters,
300 bQuotedFieldAsText, bDetectSpecialNum, bFixedWidth, nFromRow, nCharSet, nLanguage, meCall);
301 // load from saved settings
302 maFieldSeparators = OUString(sFieldSeparators);
304 if( bMergeDelimiters && !bIsTSV )
305 pCkbAsOnce->Check();
306 if (bQuotedFieldAsText)
307 pCkbQuotedAsText->Check();
308 if (bDetectSpecialNum)
309 pCkbDetectNumber->Check();
310 if( bFixedWidth && !bIsTSV )
311 pRbFixed->Check();
312 if( nFromRow != 1 )
313 pNfRow->SetValue( nFromRow );
315 if ( bIsTSV )
316 pCkbTab->Check();
317 else
318 SetSeparators(); // Set Separators in the dialog from maFieldSeparators (empty are not set)
320 // Get Separators from the dialog (empty are set from default)
321 maFieldSeparators = GetSeparators();
323 // Clipboard is always Unicode, else detect.
324 rtl_TextEncoding ePreselectUnicode = (meCall == SC_IMPORTFILE ?
325 RTL_TEXTENCODING_DONTKNOW : RTL_TEXTENCODING_UNICODE);
326 // Sniff for Unicode / not
327 if( ePreselectUnicode == RTL_TEXTENCODING_DONTKNOW && mpDatStream )
329 Seek( 0 );
330 mpDatStream->StartReadingUnicodeText( RTL_TEXTENCODING_DONTKNOW );
331 sal_uLong nUniPos = mpDatStream->Tell();
332 switch (nUniPos)
334 case 2:
335 ePreselectUnicode = RTL_TEXTENCODING_UNICODE; // UTF-16
336 break;
337 case 3:
338 ePreselectUnicode = RTL_TEXTENCODING_UTF8; // UTF-8
339 break;
340 case 0:
342 sal_uInt16 n;
343 mpDatStream->ReadUInt16( n );
344 // Assume that normal ASCII/ANSI/ISO/etc. text doesn't start with
345 // control characters except CR,LF,TAB
346 if ( (n & 0xff00) < 0x2000 )
348 switch ( n & 0xff00 )
350 case 0x0900 :
351 case 0x0a00 :
352 case 0x0d00 :
353 break;
354 default:
355 ePreselectUnicode = RTL_TEXTENCODING_UNICODE; // UTF-16
358 mpDatStream->Seek(0);
360 break;
361 default:
362 ; // nothing
364 mnStreamPos = mpDatStream->Tell();
367 pNfRow->SetModifyHdl( LINK( this, ScImportAsciiDlg, FirstRowHdl ) );
369 // *** Separator characters ***
370 lcl_FillCombo( *pCbTextSep, aTextSepList, mcTextSep );
371 pCbTextSep->SetText( sTextSeparators );
373 Link<> aSeparatorHdl =LINK( this, ScImportAsciiDlg, SeparatorHdl );
374 pCbTextSep->SetSelectHdl( aSeparatorHdl );
375 pCbTextSep->SetModifyHdl( aSeparatorHdl );
376 pCkbTab->SetClickHdl( aSeparatorHdl );
377 pCkbSemicolon->SetClickHdl( aSeparatorHdl );
378 pCkbComma->SetClickHdl( aSeparatorHdl );
379 pCkbAsOnce->SetClickHdl( aSeparatorHdl );
380 pCkbQuotedAsText->SetClickHdl( aSeparatorHdl );
381 pCkbDetectNumber->SetClickHdl( aSeparatorHdl );
382 pCkbSpace->SetClickHdl( aSeparatorHdl );
383 pCkbOther->SetClickHdl( aSeparatorHdl );
384 pEdOther->SetModifyHdl( aSeparatorHdl );
386 // *** text encoding ListBox ***
387 // all encodings allowed, including Unicode, but subsets are excluded
388 pLbCharSet->FillFromTextEncodingTable( true );
389 // Insert one "SYSTEM" entry for compatibility in AsciiOptions and system
390 // independent document linkage.
391 pLbCharSet->InsertTextEncoding( RTL_TEXTENCODING_DONTKNOW, aCharSetUser );
392 if ( ePreselectUnicode == RTL_TEXTENCODING_DONTKNOW )
394 rtl_TextEncoding eSystemEncoding = osl_getThreadTextEncoding();
395 // Prefer UTF-8, as UTF-16 would have already been detected from the stream.
396 // This gives a better chance that the file is going to be opened correctly.
397 if ( ( eSystemEncoding == RTL_TEXTENCODING_UNICODE ) && mpDatStream )
398 eSystemEncoding = RTL_TEXTENCODING_UTF8;
399 pLbCharSet->SelectTextEncoding( eSystemEncoding );
401 else
403 pLbCharSet->SelectTextEncoding( ePreselectUnicode );
406 if( nCharSet >= 0 && ePreselectUnicode == RTL_TEXTENCODING_DONTKNOW )
407 pLbCharSet->SelectEntryPos( static_cast<sal_uInt16>(nCharSet) );
409 SetSelectedCharSet();
410 pLbCharSet->SetSelectHdl( LINK( this, ScImportAsciiDlg, CharSetHdl ) );
412 pLbCustomLang->SetLanguageList(
413 SvxLanguageListFlags::ALL | SvxLanguageListFlags::ONLY_KNOWN, false, false);
414 pLbCustomLang->InsertLanguage(LANGUAGE_SYSTEM);
415 pLbCustomLang->SelectLanguage(static_cast<LanguageType>(nLanguage), true);
417 // *** column type ListBox ***
418 sal_Int32 nCount = comphelper::string::getTokenCount(aColumnUser, ';');
419 for (sal_Int32 i=0; i<nCount; i++)
420 pLbType->InsertEntry( aColumnUser.getToken( i, ';' ) );
422 pLbType->SetSelectHdl( LINK( this, ScImportAsciiDlg, LbColTypeHdl ) );
423 pFtType->Disable();
424 pLbType->Disable();
426 // *** table box preview ***
427 mpTableBox->Init();
428 mpTableBox->SetUpdateTextHdl( LINK( this, ScImportAsciiDlg, UpdateTextHdl ) );
429 mpTableBox->InitTypes( *pLbType );
430 mpTableBox->SetColTypeHdl( LINK( this, ScImportAsciiDlg, ColTypeHdl ) );
432 pRbSeparated->SetClickHdl( LINK( this, ScImportAsciiDlg, RbSepFixHdl ) );
433 pRbFixed->SetClickHdl( LINK( this, ScImportAsciiDlg, RbSepFixHdl ) );
435 SetupSeparatorCtrls();
436 RbSepFixHdl( pRbFixed );
438 UpdateVertical();
440 mpTableBox->Execute( CSVCMD_NEWCELLTEXTS );
442 pEdOther->SetAccessibleName(pCkbOther->GetText());
443 pEdOther->SetAccessibleRelationLabeledBy(pCkbOther);
445 if (meCall == SC_TEXTTOCOLUMNS)
447 pFtCharSet->Disable();
448 pLbCharSet->Disable();
449 pFtCustomLang->Disable();
450 pLbCustomLang->SelectLanguage(LANGUAGE_SYSTEM);
451 pLbCustomLang->Disable();
452 pFtRow->Disable();
453 pNfRow->Disable();
455 // Quoted field as text option is not used for text-to-columns mode.
456 pCkbQuotedAsText->Check(false);
457 pCkbQuotedAsText->Disable();
459 // Always detect special numbers for text-to-columns mode.
460 pCkbDetectNumber->Check();
461 pCkbDetectNumber->Disable();
465 ScImportAsciiDlg::~ScImportAsciiDlg()
467 disposeOnce();
470 void ScImportAsciiDlg::dispose()
472 delete[] mpRowPosArray;
473 pFtCharSet.clear();
474 pLbCharSet.clear();
475 pFtCustomLang.clear();
476 pLbCustomLang.clear();
477 pFtRow.clear();
478 pNfRow.clear();
479 pRbFixed.clear();
480 pRbSeparated.clear();
481 pCkbTab.clear();
482 pCkbSemicolon.clear();
483 pCkbComma.clear();
484 pCkbSpace.clear();
485 pCkbOther.clear();
486 pEdOther.clear();
487 pCkbAsOnce.clear();
488 pFtTextSep.clear();
489 pCbTextSep.clear();
490 pCkbQuotedAsText.clear();
491 pCkbDetectNumber.clear();
492 pFtType.clear();
493 pLbType.clear();
494 mpTableBox.clear();
495 ModalDialog::dispose();
498 bool ScImportAsciiDlg::GetLine( sal_uLong nLine, OUString &rText )
500 if (nLine >= ASCIIDLG_MAXROWS || !mpDatStream)
501 return false;
503 bool bRet = true;
504 bool bFixed = pRbFixed->IsChecked();
506 if (!mpRowPosArray)
507 mpRowPosArray = new sal_uLong[ASCIIDLG_MAXROWS + 2];
509 if (!mnRowPosCount) // complete re-fresh
511 memset( mpRowPosArray, 0, sizeof(mpRowPosArray[0]) * (ASCIIDLG_MAXROWS+2));
513 Seek(0);
514 mpDatStream->StartReadingUnicodeText( mpDatStream->GetStreamCharSet() );
516 mnStreamPos = mpDatStream->Tell();
517 mpRowPosArray[mnRowPosCount] = mnStreamPos;
520 if (nLine >= mnRowPosCount)
522 // need to work out some more line information
525 if (!Seek( mpRowPosArray[mnRowPosCount]) ||
526 mpDatStream->GetError() != ERRCODE_NONE ||
527 mpDatStream->IsEof())
529 bRet = false;
530 break;
532 rText = ReadCsvLine(*mpDatStream, !bFixed, maFieldSeparators,
533 mcTextSep);
534 mnStreamPos = mpDatStream->Tell();
535 mpRowPosArray[++mnRowPosCount] = mnStreamPos;
536 } while (nLine >= mnRowPosCount &&
537 mpDatStream->GetError() == ERRCODE_NONE &&
538 !mpDatStream->IsEof());
539 if (mpDatStream->IsEof() &&
540 mnStreamPos == mpRowPosArray[mnRowPosCount-1])
542 // the very end, not even an empty line read
543 bRet = false;
544 --mnRowPosCount;
547 else
549 Seek( mpRowPosArray[nLine]);
550 rText = ReadCsvLine(*mpDatStream, !bFixed, maFieldSeparators, mcTextSep);
551 mnStreamPos = mpDatStream->Tell();
554 // If the file content isn't unicode, ReadUniStringLine
555 // may try to seek beyond the file's end and cause a CANTSEEK error
556 // (depending on the stream type). The error code has to be cleared,
557 // or further read operations (including non-unicode) will fail.
558 if ( mpDatStream->GetError() == ERRCODE_IO_CANTSEEK )
559 mpDatStream->ResetError();
561 ScImportExport::EmbeddedNullTreatment( rText);
563 return bRet;
566 void ScImportAsciiDlg::GetOptions( ScAsciiOptions& rOpt )
568 rOpt.SetCharSet( meCharSet );
569 rOpt.SetCharSetSystem( mbCharSetSystem );
570 rOpt.SetLanguage(pLbCustomLang->GetSelectLanguage());
571 rOpt.SetFixedLen( pRbFixed->IsChecked() );
572 rOpt.SetStartRow( (long)pNfRow->GetValue() );
573 mpTableBox->FillColumnData( rOpt );
574 if( pRbSeparated->IsChecked() )
576 rOpt.SetFieldSeps( GetSeparators() );
577 rOpt.SetMergeSeps( pCkbAsOnce->IsChecked() );
578 rOpt.SetTextSep( lcl_CharFromCombo( *pCbTextSep, aTextSepList ) );
581 rOpt.SetQuotedAsText(pCkbQuotedAsText->IsChecked());
582 rOpt.SetDetectSpecialNumber(pCkbDetectNumber->IsChecked());
585 void ScImportAsciiDlg::SaveParameters()
587 save_Separators( maFieldSeparators, pCbTextSep->GetText(), pCkbAsOnce->IsChecked(),
588 pCkbQuotedAsText->IsChecked(), pCkbDetectNumber->IsChecked(),
589 pRbFixed->IsChecked(),
590 static_cast<sal_Int32>(pNfRow->GetValue()),
591 static_cast<sal_Int32>(pLbCharSet->GetSelectEntryPos()),
592 static_cast<sal_Int32>(pLbCustomLang->GetSelectLanguage()), meCall );
595 void ScImportAsciiDlg::SetSeparators()
597 OString sString(OUStringToOString(maFieldSeparators,
598 RTL_TEXTENCODING_MS_1252));
599 const sal_Char *aSep = sString.getStr();
600 sal_Int32 len = maFieldSeparators.getLength();
601 for (int i = 0; i < len; ++i)
603 switch( aSep[i] )
605 case '\t': pCkbTab->Check(); break;
606 case ';': pCkbSemicolon->Check(); break;
607 case ',': pCkbComma->Check(); break;
608 case ' ': pCkbSpace->Check(); break;
609 default:
610 pCkbOther->Check();
611 pEdOther->SetText( pEdOther->GetText() + OUString( aSep[i] ) );
616 void ScImportAsciiDlg::SetSelectedCharSet()
618 meCharSet = pLbCharSet->GetSelectTextEncoding();
619 mbCharSetSystem = (meCharSet == RTL_TEXTENCODING_DONTKNOW);
620 if( mbCharSetSystem )
621 meCharSet = osl_getThreadTextEncoding();
624 OUString ScImportAsciiDlg::GetSeparators() const
626 OUString aSepChars;
627 if( pCkbTab->IsChecked() )
628 aSepChars += "\t";
629 if( pCkbSemicolon->IsChecked() )
630 aSepChars += ";";
631 if( pCkbComma->IsChecked() )
632 aSepChars += ",";
633 if( pCkbSpace->IsChecked() )
634 aSepChars += " ";
635 if( pCkbOther->IsChecked() )
636 aSepChars += pEdOther->GetText();
637 return aSepChars;
640 void ScImportAsciiDlg::SetupSeparatorCtrls()
642 bool bEnable = pRbSeparated->IsChecked();
643 pCkbTab->Enable( bEnable );
644 pCkbSemicolon->Enable( bEnable );
645 pCkbComma->Enable( bEnable );
646 pCkbSpace->Enable( bEnable );
647 pCkbOther->Enable( bEnable );
648 pEdOther->Enable( bEnable );
649 pCkbAsOnce->Enable( bEnable );
650 pFtTextSep->Enable( bEnable );
651 pCbTextSep->Enable( bEnable );
654 void ScImportAsciiDlg::UpdateVertical()
656 mnRowPosCount = 0;
657 if (mpDatStream)
658 mpDatStream->SetStreamCharSet(meCharSet);
661 IMPL_LINK( ScImportAsciiDlg, RbSepFixHdl, RadioButton*, pButton )
663 OSL_ENSURE( pButton, "ScImportAsciiDlg::RbSepFixHdl - missing sender" );
665 if( (pButton == pRbFixed) || (pButton == pRbSeparated) )
667 SetPointer( Pointer( PointerStyle::Wait ) );
668 if( pRbFixed->IsChecked() )
669 mpTableBox->SetFixedWidthMode();
670 else
671 mpTableBox->SetSeparatorsMode();
672 SetPointer( Pointer( PointerStyle::Arrow ) );
674 SetupSeparatorCtrls();
676 return 0;
679 IMPL_LINK( ScImportAsciiDlg, SeparatorHdl, Control*, pCtrl )
681 OSL_ENSURE( pCtrl, "ScImportAsciiDlg::SeparatorHdl - missing sender" );
682 OSL_ENSURE( !pRbFixed->IsChecked(), "ScImportAsciiDlg::SeparatorHdl - not allowed in fixed width" );
684 /* #i41550# First update state of the controls. The GetSeparators()
685 function needs final state of the check boxes. */
686 if( (pCtrl == pCkbOther) && pCkbOther->IsChecked() )
687 pEdOther->GrabFocus();
688 else if( pCtrl == pEdOther )
689 pCkbOther->Check( !pEdOther->GetText().isEmpty() );
691 OUString aOldFldSeps( maFieldSeparators);
692 maFieldSeparators = GetSeparators();
693 sal_Unicode cOldSep = mcTextSep;
694 mcTextSep = lcl_CharFromCombo( *pCbTextSep, aTextSepList );
695 // Any separator changed may result in completely different lines due to
696 // embedded line breaks.
697 if (cOldSep != mcTextSep || aOldFldSeps != maFieldSeparators)
698 UpdateVertical();
700 mpTableBox->Execute( CSVCMD_NEWCELLTEXTS );
701 return 0;
704 IMPL_LINK( ScImportAsciiDlg, CharSetHdl, SvxTextEncodingBox*, pCharSetBox )
706 OSL_ENSURE( pCharSetBox, "ScImportAsciiDlg::CharSetHdl - missing sender" );
708 if( (pCharSetBox == pLbCharSet) && (pCharSetBox->GetSelectEntryCount() == 1) )
710 SetPointer( Pointer( PointerStyle::Wait ) );
711 rtl_TextEncoding eOldCharSet = meCharSet;
712 SetSelectedCharSet();
713 // switching char-set invalidates 8bit -> String conversions
714 if (eOldCharSet != meCharSet)
715 UpdateVertical();
717 mpTableBox->Execute( CSVCMD_NEWCELLTEXTS );
718 SetPointer( Pointer( PointerStyle::Arrow ) );
720 return 0;
723 IMPL_LINK( ScImportAsciiDlg, FirstRowHdl, NumericField*, pNumField )
725 OSL_ENSURE( pNumField, "ScImportAsciiDlg::FirstRowHdl - missing sender" );
726 mpTableBox->Execute( CSVCMD_SETFIRSTIMPORTLINE, sal::static_int_cast<sal_Int32>( pNumField->GetValue() - 1 ) );
727 return 0;
730 IMPL_LINK( ScImportAsciiDlg, LbColTypeHdl, ListBox*, pListBox )
732 OSL_ENSURE( pListBox, "ScImportAsciiDlg::LbColTypeHdl - missing sender" );
733 if( pListBox == pLbType )
734 mpTableBox->Execute( CSVCMD_SETCOLUMNTYPE, pListBox->GetSelectEntryPos() );
735 return 0;
738 IMPL_LINK_NOARG(ScImportAsciiDlg, UpdateTextHdl)
740 sal_Int32 nBaseLine = mpTableBox->GetFirstVisLine();
741 sal_Int32 nRead = mpTableBox->GetVisLineCount();
742 // If mnRowPosCount==0, this is an initializing call, read ahead for row
743 // count and resulting scroll bar size and position to be able to scroll at
744 // all. When adding lines, read only the amount of next lines to be
745 // displayed.
746 if (!mnRowPosCount || nRead > CSV_PREVIEW_LINES)
747 nRead = CSV_PREVIEW_LINES;
749 sal_Int32 i;
750 for (i = 0; i < nRead; i++)
752 if (!GetLine( nBaseLine + i, maPreviewLine[i]))
753 break;
755 for (; i < CSV_PREVIEW_LINES; i++)
756 maPreviewLine[i].clear();
758 mpTableBox->Execute( CSVCMD_SETLINECOUNT, mnRowPosCount);
759 bool bMergeSep = pCkbAsOnce->IsChecked();
760 mpTableBox->SetUniStrings( maPreviewLine, maFieldSeparators, mcTextSep, bMergeSep);
762 return 0;
765 IMPL_LINK( ScImportAsciiDlg, ColTypeHdl, ScCsvTableBox*, pTableBox )
767 OSL_ENSURE( pTableBox, "ScImportAsciiDlg::ColTypeHdl - missing sender" );
769 sal_Int32 nType = pTableBox->GetSelColumnType();
770 sal_Int32 nTypeCount = pLbType->GetEntryCount();
771 bool bEmpty = (nType == CSV_TYPE_MULTI);
772 bool bEnable = ((0 <= nType) && (nType < nTypeCount)) || bEmpty;
774 pFtType->Enable( bEnable );
775 pLbType->Enable( bEnable );
777 Link<> aSelHdl = pLbType->GetSelectHdl();
778 pLbType->SetSelectHdl( Link<>() );
779 if( bEmpty )
780 pLbType->SetNoSelection();
781 else if( bEnable )
782 pLbType->SelectEntryPos( static_cast< sal_uInt16 >( nType ) );
783 pLbType->SetSelectHdl( aSelHdl );
785 return 0;
788 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */