1b3f79822SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3b3f79822SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4b3f79822SAndrew Rist * or more contributor license agreements. See the NOTICE file 5b3f79822SAndrew Rist * distributed with this work for additional information 6b3f79822SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7b3f79822SAndrew Rist * to you under the Apache License, Version 2.0 (the 8b3f79822SAndrew Rist * "License"); you may not use this file except in compliance 9b3f79822SAndrew Rist * with the License. You may obtain a copy of the License at 10b3f79822SAndrew Rist * 11b3f79822SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12b3f79822SAndrew Rist * 13b3f79822SAndrew Rist * Unless required by applicable law or agreed to in writing, 14b3f79822SAndrew Rist * software distributed under the License is distributed on an 15b3f79822SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16b3f79822SAndrew Rist * KIND, either express or implied. See the License for the 17b3f79822SAndrew Rist * specific language governing permissions and limitations 18b3f79822SAndrew Rist * under the License. 19b3f79822SAndrew Rist * 20b3f79822SAndrew Rist *************************************************************/ 21b3f79822SAndrew Rist 22b3f79822SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_sc.hxx" 26cdf0e10cSrcweir 27cdf0e10cSrcweir #undef SC_DLLIMPLEMENTATION 28cdf0e10cSrcweir 29cdf0e10cSrcweir 30cdf0e10cSrcweir 31cdf0e10cSrcweir #include "scuiimoptdlg.hxx" 32cdf0e10cSrcweir #include "scresid.hxx" 33cdf0e10cSrcweir #include "imoptdlg.hrc" 34cdf0e10cSrcweir #include <rtl/tencinfo.h> 35cdf0e10cSrcweir //======================================================================== 36cdf0e10cSrcweir // ScDelimiterTable 37cdf0e10cSrcweir //======================================================================== 38cdf0e10cSrcweir 39cdf0e10cSrcweir class ScDelimiterTable 40cdf0e10cSrcweir { 41cdf0e10cSrcweir public: 42cdf0e10cSrcweir ScDelimiterTable( const String& rDelTab ) 43cdf0e10cSrcweir : theDelTab ( rDelTab ), 44cdf0e10cSrcweir cSep ( '\t' ), 45cdf0e10cSrcweir nCount ( rDelTab.GetTokenCount('\t') ), 46cdf0e10cSrcweir nIter ( 0 ) 47cdf0e10cSrcweir {} 48cdf0e10cSrcweir 49cdf0e10cSrcweir sal_uInt16 GetCode( const String& rDelimiter ) const; 50cdf0e10cSrcweir String GetDelimiter( sal_Unicode nCode ) const; 51cdf0e10cSrcweir 52cdf0e10cSrcweir String FirstDel() { nIter = 0; return theDelTab.GetToken( nIter, cSep ); } 53cdf0e10cSrcweir String NextDel() { nIter +=2; return theDelTab.GetToken( nIter, cSep ); } 54cdf0e10cSrcweir 55cdf0e10cSrcweir private: 56cdf0e10cSrcweir const String theDelTab; 57cdf0e10cSrcweir const sal_Unicode cSep; 58cdf0e10cSrcweir const xub_StrLen nCount; 59cdf0e10cSrcweir xub_StrLen nIter; 60cdf0e10cSrcweir }; 61cdf0e10cSrcweir 62cdf0e10cSrcweir //------------------------------------------------------------------------ 63cdf0e10cSrcweir 64cdf0e10cSrcweir sal_uInt16 ScDelimiterTable::GetCode( const String& rDel ) const 65cdf0e10cSrcweir { 66cdf0e10cSrcweir sal_Unicode nCode = 0; 67cdf0e10cSrcweir xub_StrLen i = 0; 68cdf0e10cSrcweir 69cdf0e10cSrcweir if ( nCount >= 2 ) 70cdf0e10cSrcweir { 71cdf0e10cSrcweir while ( i<nCount ) 72cdf0e10cSrcweir { 73cdf0e10cSrcweir if ( rDel == theDelTab.GetToken( i, cSep ) ) 74cdf0e10cSrcweir { 75cdf0e10cSrcweir nCode = (sal_Unicode) theDelTab.GetToken( i+1, cSep ).ToInt32(); 76cdf0e10cSrcweir i = nCount; 77cdf0e10cSrcweir } 78cdf0e10cSrcweir else 79cdf0e10cSrcweir i += 2; 80cdf0e10cSrcweir } 81cdf0e10cSrcweir } 82cdf0e10cSrcweir 83cdf0e10cSrcweir return nCode; 84cdf0e10cSrcweir } 85cdf0e10cSrcweir 86cdf0e10cSrcweir //------------------------------------------------------------------------ 87cdf0e10cSrcweir 88cdf0e10cSrcweir String ScDelimiterTable::GetDelimiter( sal_Unicode nCode ) const 89cdf0e10cSrcweir { 90cdf0e10cSrcweir String aStrDel; 91cdf0e10cSrcweir xub_StrLen i = 0; 92cdf0e10cSrcweir 93cdf0e10cSrcweir if ( nCount >= 2 ) 94cdf0e10cSrcweir { 95cdf0e10cSrcweir while ( i<nCount ) 96cdf0e10cSrcweir { 97cdf0e10cSrcweir if ( nCode == (sal_Unicode) theDelTab.GetToken( i+1, cSep ).ToInt32() ) 98cdf0e10cSrcweir { 99cdf0e10cSrcweir aStrDel = theDelTab.GetToken( i, cSep ); 100cdf0e10cSrcweir i = nCount; 101cdf0e10cSrcweir } 102cdf0e10cSrcweir else 103cdf0e10cSrcweir i += 2; 104cdf0e10cSrcweir } 105cdf0e10cSrcweir } 106cdf0e10cSrcweir 107cdf0e10cSrcweir return aStrDel; 108cdf0e10cSrcweir } 109cdf0e10cSrcweir 110cdf0e10cSrcweir //======================================================================== 111cdf0e10cSrcweir // ScImportOptionsDlg 112cdf0e10cSrcweir //======================================================================== 113cdf0e10cSrcweir 114cdf0e10cSrcweir ScImportOptionsDlg::ScImportOptionsDlg( 115cdf0e10cSrcweir Window* pParent, 116cdf0e10cSrcweir sal_Bool bAscii, 117cdf0e10cSrcweir const ScImportOptions* pOptions, 118cdf0e10cSrcweir const String* pStrTitle, 119cdf0e10cSrcweir sal_Bool bMultiByte, 120cdf0e10cSrcweir sal_Bool bOnlyDbtoolsEncodings, 121cdf0e10cSrcweir sal_Bool bImport ) 122cdf0e10cSrcweir 123cdf0e10cSrcweir : ModalDialog ( pParent, ScResId( RID_SCDLG_IMPORTOPT ) ), 124cdf0e10cSrcweir aFlFieldOpt ( this, ScResId( FL_FIELDOPT ) ), 125cdf0e10cSrcweir aFtFont ( this, ScResId( FT_FONT ) ), 126cdf0e10cSrcweir aLbFont ( this, ScResId( bAscii ? DDLB_FONT : LB_FONT ) ), 127cdf0e10cSrcweir aFtFieldSep ( this, ScResId( FT_FIELDSEP ) ), 128cdf0e10cSrcweir aEdFieldSep ( this, ScResId( ED_FIELDSEP ) ), 129cdf0e10cSrcweir aFtTextSep ( this, ScResId( FT_TEXTSEP ) ), 130cdf0e10cSrcweir aEdTextSep ( this, ScResId( ED_TEXTSEP ) ), 131cdf0e10cSrcweir aCbQuoteAll ( this, ScResId( CB_QUOTEALL ) ), 132cdf0e10cSrcweir aCbShown ( this, ScResId( CB_SAVESHOWN ) ), 133cdf0e10cSrcweir aCbFixed ( this, ScResId( CB_FIXEDWIDTH ) ), 134cdf0e10cSrcweir aBtnOk ( this, ScResId( BTN_OK ) ), 135cdf0e10cSrcweir aBtnCancel ( this, ScResId( BTN_CANCEL ) ), 136cdf0e10cSrcweir aBtnHelp ( this, ScResId( BTN_HELP ) ) 137cdf0e10cSrcweir { 138*02c0dac2SAriel Constenla-Haile String sFieldSep( ScResId( SCSTR_FIELDSEP ) ); 139*02c0dac2SAriel Constenla-Haile sFieldSep.SearchAndReplaceAscii( "%TAB", String(ScResId(SCSTR_FIELDSEP_TAB)) ); 140*02c0dac2SAriel Constenla-Haile sFieldSep.SearchAndReplaceAscii( "%SPACE", String(ScResId(SCSTR_FIELDSEP_SPACE)) ); 141*02c0dac2SAriel Constenla-Haile 142cdf0e10cSrcweir // im Ctor-Initializer nicht moeglich (MSC kann das nicht): 143*02c0dac2SAriel Constenla-Haile pFieldSepTab = new ScDelimiterTable( sFieldSep ); 144cdf0e10cSrcweir pTextSepTab = new ScDelimiterTable( String(ScResId(SCSTR_TEXTSEP)) ); 145cdf0e10cSrcweir 146cdf0e10cSrcweir String aStr = pFieldSepTab->FirstDel(); 147cdf0e10cSrcweir sal_Unicode nCode; 148cdf0e10cSrcweir 149cdf0e10cSrcweir while ( aStr.Len() > 0 ) 150cdf0e10cSrcweir { 151cdf0e10cSrcweir aEdFieldSep.InsertEntry( aStr ); 152cdf0e10cSrcweir aStr = pFieldSepTab->NextDel(); 153cdf0e10cSrcweir } 154cdf0e10cSrcweir 155cdf0e10cSrcweir aStr = pTextSepTab->FirstDel(); 156cdf0e10cSrcweir 157cdf0e10cSrcweir while ( aStr.Len() > 0 ) 158cdf0e10cSrcweir { 159cdf0e10cSrcweir aEdTextSep.InsertEntry( aStr ); 160cdf0e10cSrcweir aStr = pTextSepTab->NextDel(); 161cdf0e10cSrcweir } 162cdf0e10cSrcweir 163cdf0e10cSrcweir aEdFieldSep.SetText( aEdFieldSep.GetEntry(0) ); 164cdf0e10cSrcweir aEdTextSep.SetText( aEdTextSep.GetEntry(0) ); 165cdf0e10cSrcweir 166cdf0e10cSrcweir if ( bOnlyDbtoolsEncodings ) 167cdf0e10cSrcweir { 168cdf0e10cSrcweir // Even dBase export allows multibyte now 169cdf0e10cSrcweir if ( bMultiByte ) 170cdf0e10cSrcweir aLbFont.FillFromDbTextEncodingMap( bImport ); 171cdf0e10cSrcweir else 172cdf0e10cSrcweir aLbFont.FillFromDbTextEncodingMap( bImport, RTL_TEXTENCODING_INFO_MULTIBYTE ); 173cdf0e10cSrcweir } 174cdf0e10cSrcweir else if ( !bAscii ) 175cdf0e10cSrcweir { //!TODO: Unicode would need work in each filter 176cdf0e10cSrcweir if ( bMultiByte ) 177cdf0e10cSrcweir aLbFont.FillFromTextEncodingTable( bImport, RTL_TEXTENCODING_INFO_UNICODE ); 178cdf0e10cSrcweir else 179cdf0e10cSrcweir aLbFont.FillFromTextEncodingTable( bImport, RTL_TEXTENCODING_INFO_UNICODE | 180cdf0e10cSrcweir RTL_TEXTENCODING_INFO_MULTIBYTE ); 181cdf0e10cSrcweir } 182cdf0e10cSrcweir else 183cdf0e10cSrcweir { 184cdf0e10cSrcweir if ( pOptions ) 185cdf0e10cSrcweir { 186cdf0e10cSrcweir nCode = pOptions->nFieldSepCode; 187cdf0e10cSrcweir aStr = pFieldSepTab->GetDelimiter( nCode ); 188cdf0e10cSrcweir 189cdf0e10cSrcweir if ( !aStr.Len() ) 190cdf0e10cSrcweir aEdFieldSep.SetText( String((sal_Unicode)nCode) ); 191cdf0e10cSrcweir else 192cdf0e10cSrcweir aEdFieldSep.SetText( aStr ); 193cdf0e10cSrcweir 194cdf0e10cSrcweir nCode = pOptions->nTextSepCode; 195cdf0e10cSrcweir aStr = pTextSepTab->GetDelimiter( nCode ); 196cdf0e10cSrcweir 197cdf0e10cSrcweir if ( !aStr.Len() ) 198cdf0e10cSrcweir aEdTextSep.SetText( String((sal_Unicode)nCode) ); 199cdf0e10cSrcweir else 200cdf0e10cSrcweir aEdTextSep.SetText( aStr ); 201cdf0e10cSrcweir } 202cdf0e10cSrcweir // all encodings allowed, even Unicode 203cdf0e10cSrcweir aLbFont.FillFromTextEncodingTable( bImport ); 204cdf0e10cSrcweir } 205cdf0e10cSrcweir 206cdf0e10cSrcweir if( bAscii ) 207cdf0e10cSrcweir { 208cdf0e10cSrcweir Size aWinSize( GetSizePixel() ); 209cdf0e10cSrcweir aWinSize.Height() = aCbFixed.GetPosPixel().Y() + aCbFixed.GetSizePixel().Height(); 210cdf0e10cSrcweir Size aDiffSize( LogicToPixel( Size( 0, 6 ), MapMode( MAP_APPFONT ) ) ); 211cdf0e10cSrcweir aWinSize.Height() += aDiffSize.Height(); 212cdf0e10cSrcweir SetSizePixel( aWinSize ); 213cdf0e10cSrcweir aCbFixed.Show(); 214cdf0e10cSrcweir aCbFixed.SetClickHdl( LINK( this, ScImportOptionsDlg, FixedWidthHdl ) ); 215cdf0e10cSrcweir aCbFixed.Check( sal_False ); 216cdf0e10cSrcweir aCbShown.Show(); 217cdf0e10cSrcweir aCbShown.Check( sal_True ); 218cdf0e10cSrcweir aCbQuoteAll.Show(); 219cdf0e10cSrcweir aCbQuoteAll.Check( sal_False ); 220cdf0e10cSrcweir } 221cdf0e10cSrcweir else 222cdf0e10cSrcweir { 223cdf0e10cSrcweir aFlFieldOpt.SetText( aFtFont.GetText() ); 224cdf0e10cSrcweir aFtFieldSep.Hide(); 225cdf0e10cSrcweir aFtTextSep.Hide(); 226cdf0e10cSrcweir aFtFont.Hide(); 227cdf0e10cSrcweir aEdFieldSep.Hide(); 228cdf0e10cSrcweir aEdTextSep.Hide(); 229cdf0e10cSrcweir aCbFixed.Hide(); 230cdf0e10cSrcweir aCbShown.Hide(); 231cdf0e10cSrcweir aCbQuoteAll.Hide(); 232cdf0e10cSrcweir aLbFont.GrabFocus(); 233cdf0e10cSrcweir aLbFont.SetDoubleClickHdl( LINK( this, ScImportOptionsDlg, DoubleClickHdl ) ); 234cdf0e10cSrcweir } 235cdf0e10cSrcweir 236cdf0e10cSrcweir aLbFont.SelectTextEncoding( pOptions ? pOptions->eCharSet : 237cdf0e10cSrcweir gsl_getSystemTextEncoding() ); 238cdf0e10cSrcweir 239cdf0e10cSrcweir // optionaler Titel: 240cdf0e10cSrcweir if ( pStrTitle ) 241cdf0e10cSrcweir SetText( *pStrTitle ); 242cdf0e10cSrcweir 243cdf0e10cSrcweir FreeResource(); 244cdf0e10cSrcweir } 245cdf0e10cSrcweir 246cdf0e10cSrcweir //------------------------------------------------------------------------ 247cdf0e10cSrcweir 248cdf0e10cSrcweir __EXPORT ScImportOptionsDlg::~ScImportOptionsDlg() 249cdf0e10cSrcweir { 250cdf0e10cSrcweir delete pFieldSepTab; 251cdf0e10cSrcweir delete pTextSepTab; 252cdf0e10cSrcweir } 253cdf0e10cSrcweir 254cdf0e10cSrcweir //------------------------------------------------------------------------ 255cdf0e10cSrcweir 256cdf0e10cSrcweir void ScImportOptionsDlg::GetImportOptions( ScImportOptions& rOptions ) const 257cdf0e10cSrcweir { 258cdf0e10cSrcweir rOptions.SetTextEncoding( aLbFont.GetSelectTextEncoding() ); 259cdf0e10cSrcweir 260cdf0e10cSrcweir if ( aCbFixed.IsVisible() ) 261cdf0e10cSrcweir { 262cdf0e10cSrcweir rOptions.nFieldSepCode = GetCodeFromCombo( aEdFieldSep ); 263cdf0e10cSrcweir rOptions.nTextSepCode = GetCodeFromCombo( aEdTextSep ); 264cdf0e10cSrcweir rOptions.bFixedWidth = aCbFixed.IsChecked(); 265cdf0e10cSrcweir rOptions.bSaveAsShown = aCbShown.IsChecked(); 266cdf0e10cSrcweir rOptions.bQuoteAllText = aCbQuoteAll.IsChecked(); 267cdf0e10cSrcweir } 268cdf0e10cSrcweir } 269cdf0e10cSrcweir 270cdf0e10cSrcweir //------------------------------------------------------------------------ 271cdf0e10cSrcweir 272cdf0e10cSrcweir sal_uInt16 ScImportOptionsDlg::GetCodeFromCombo( const ComboBox& rEd ) const 273cdf0e10cSrcweir { 274cdf0e10cSrcweir ScDelimiterTable* pTab; 275cdf0e10cSrcweir String aStr( rEd.GetText() ); 276cdf0e10cSrcweir sal_uInt16 nCode; 277cdf0e10cSrcweir 278cdf0e10cSrcweir if ( &rEd == &aEdTextSep ) 279cdf0e10cSrcweir pTab = pTextSepTab; 280cdf0e10cSrcweir else 281cdf0e10cSrcweir pTab = pFieldSepTab; 282cdf0e10cSrcweir 283cdf0e10cSrcweir if ( !aStr.Len() ) 284cdf0e10cSrcweir { 285cdf0e10cSrcweir nCode = 0; // kein Trennzeichen 286cdf0e10cSrcweir } 287cdf0e10cSrcweir else 288cdf0e10cSrcweir { 289cdf0e10cSrcweir nCode = pTab->GetCode( aStr ); 290cdf0e10cSrcweir 291cdf0e10cSrcweir if ( nCode == 0 ) 292cdf0e10cSrcweir nCode = (sal_uInt16)aStr.GetChar(0); 293cdf0e10cSrcweir } 294cdf0e10cSrcweir 295cdf0e10cSrcweir return nCode; 296cdf0e10cSrcweir } 297cdf0e10cSrcweir 298cdf0e10cSrcweir //------------------------------------------------------------------------ 299cdf0e10cSrcweir 300cdf0e10cSrcweir IMPL_LINK( ScImportOptionsDlg, FixedWidthHdl, CheckBox*, pCheckBox ) 301cdf0e10cSrcweir { 302cdf0e10cSrcweir if( pCheckBox == &aCbFixed ) 303cdf0e10cSrcweir { 304cdf0e10cSrcweir sal_Bool bEnable = !aCbFixed.IsChecked(); 305cdf0e10cSrcweir aFtFieldSep.Enable( bEnable ); 306cdf0e10cSrcweir aEdFieldSep.Enable( bEnable ); 307cdf0e10cSrcweir aFtTextSep.Enable( bEnable ); 308cdf0e10cSrcweir aEdTextSep.Enable( bEnable ); 309cdf0e10cSrcweir aCbShown.Enable( bEnable ); 310cdf0e10cSrcweir aCbQuoteAll.Enable( bEnable ); 311cdf0e10cSrcweir } 312cdf0e10cSrcweir return 0; 313cdf0e10cSrcweir } 314cdf0e10cSrcweir 315cdf0e10cSrcweir IMPL_LINK( ScImportOptionsDlg, DoubleClickHdl, ListBox*, pLb ) 316cdf0e10cSrcweir { 317cdf0e10cSrcweir if ( pLb == &aLbFont ) 318cdf0e10cSrcweir { 319cdf0e10cSrcweir aBtnOk.Click(); 320cdf0e10cSrcweir } 321cdf0e10cSrcweir return 0; 322cdf0e10cSrcweir } 323