1*b3f79822SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*b3f79822SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*b3f79822SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*b3f79822SAndrew Rist * distributed with this work for additional information 6*b3f79822SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*b3f79822SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*b3f79822SAndrew Rist * "License"); you may not use this file except in compliance 9*b3f79822SAndrew Rist * with the License. You may obtain a copy of the License at 10*b3f79822SAndrew Rist * 11*b3f79822SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*b3f79822SAndrew Rist * 13*b3f79822SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*b3f79822SAndrew Rist * software distributed under the License is distributed on an 15*b3f79822SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*b3f79822SAndrew Rist * KIND, either express or implied. See the License for the 17*b3f79822SAndrew Rist * specific language governing permissions and limitations 18*b3f79822SAndrew Rist * under the License. 19*b3f79822SAndrew Rist * 20*b3f79822SAndrew Rist *************************************************************/ 21*b3f79822SAndrew Rist 22*b3f79822SAndrew 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 { 138cdf0e10cSrcweir // im Ctor-Initializer nicht moeglich (MSC kann das nicht): 139cdf0e10cSrcweir pFieldSepTab = new ScDelimiterTable( String(ScResId(SCSTR_FIELDSEP)) ); 140cdf0e10cSrcweir pTextSepTab = new ScDelimiterTable( String(ScResId(SCSTR_TEXTSEP)) ); 141cdf0e10cSrcweir 142cdf0e10cSrcweir String aStr = pFieldSepTab->FirstDel(); 143cdf0e10cSrcweir sal_Unicode nCode; 144cdf0e10cSrcweir 145cdf0e10cSrcweir while ( aStr.Len() > 0 ) 146cdf0e10cSrcweir { 147cdf0e10cSrcweir aEdFieldSep.InsertEntry( aStr ); 148cdf0e10cSrcweir aStr = pFieldSepTab->NextDel(); 149cdf0e10cSrcweir } 150cdf0e10cSrcweir 151cdf0e10cSrcweir aStr = pTextSepTab->FirstDel(); 152cdf0e10cSrcweir 153cdf0e10cSrcweir while ( aStr.Len() > 0 ) 154cdf0e10cSrcweir { 155cdf0e10cSrcweir aEdTextSep.InsertEntry( aStr ); 156cdf0e10cSrcweir aStr = pTextSepTab->NextDel(); 157cdf0e10cSrcweir } 158cdf0e10cSrcweir 159cdf0e10cSrcweir aEdFieldSep.SetText( aEdFieldSep.GetEntry(0) ); 160cdf0e10cSrcweir aEdTextSep.SetText( aEdTextSep.GetEntry(0) ); 161cdf0e10cSrcweir 162cdf0e10cSrcweir if ( bOnlyDbtoolsEncodings ) 163cdf0e10cSrcweir { 164cdf0e10cSrcweir // Even dBase export allows multibyte now 165cdf0e10cSrcweir if ( bMultiByte ) 166cdf0e10cSrcweir aLbFont.FillFromDbTextEncodingMap( bImport ); 167cdf0e10cSrcweir else 168cdf0e10cSrcweir aLbFont.FillFromDbTextEncodingMap( bImport, RTL_TEXTENCODING_INFO_MULTIBYTE ); 169cdf0e10cSrcweir } 170cdf0e10cSrcweir else if ( !bAscii ) 171cdf0e10cSrcweir { //!TODO: Unicode would need work in each filter 172cdf0e10cSrcweir if ( bMultiByte ) 173cdf0e10cSrcweir aLbFont.FillFromTextEncodingTable( bImport, RTL_TEXTENCODING_INFO_UNICODE ); 174cdf0e10cSrcweir else 175cdf0e10cSrcweir aLbFont.FillFromTextEncodingTable( bImport, RTL_TEXTENCODING_INFO_UNICODE | 176cdf0e10cSrcweir RTL_TEXTENCODING_INFO_MULTIBYTE ); 177cdf0e10cSrcweir } 178cdf0e10cSrcweir else 179cdf0e10cSrcweir { 180cdf0e10cSrcweir if ( pOptions ) 181cdf0e10cSrcweir { 182cdf0e10cSrcweir nCode = pOptions->nFieldSepCode; 183cdf0e10cSrcweir aStr = pFieldSepTab->GetDelimiter( nCode ); 184cdf0e10cSrcweir 185cdf0e10cSrcweir if ( !aStr.Len() ) 186cdf0e10cSrcweir aEdFieldSep.SetText( String((sal_Unicode)nCode) ); 187cdf0e10cSrcweir else 188cdf0e10cSrcweir aEdFieldSep.SetText( aStr ); 189cdf0e10cSrcweir 190cdf0e10cSrcweir nCode = pOptions->nTextSepCode; 191cdf0e10cSrcweir aStr = pTextSepTab->GetDelimiter( nCode ); 192cdf0e10cSrcweir 193cdf0e10cSrcweir if ( !aStr.Len() ) 194cdf0e10cSrcweir aEdTextSep.SetText( String((sal_Unicode)nCode) ); 195cdf0e10cSrcweir else 196cdf0e10cSrcweir aEdTextSep.SetText( aStr ); 197cdf0e10cSrcweir } 198cdf0e10cSrcweir // all encodings allowed, even Unicode 199cdf0e10cSrcweir aLbFont.FillFromTextEncodingTable( bImport ); 200cdf0e10cSrcweir } 201cdf0e10cSrcweir 202cdf0e10cSrcweir if( bAscii ) 203cdf0e10cSrcweir { 204cdf0e10cSrcweir Size aWinSize( GetSizePixel() ); 205cdf0e10cSrcweir aWinSize.Height() = aCbFixed.GetPosPixel().Y() + aCbFixed.GetSizePixel().Height(); 206cdf0e10cSrcweir Size aDiffSize( LogicToPixel( Size( 0, 6 ), MapMode( MAP_APPFONT ) ) ); 207cdf0e10cSrcweir aWinSize.Height() += aDiffSize.Height(); 208cdf0e10cSrcweir SetSizePixel( aWinSize ); 209cdf0e10cSrcweir aCbFixed.Show(); 210cdf0e10cSrcweir aCbFixed.SetClickHdl( LINK( this, ScImportOptionsDlg, FixedWidthHdl ) ); 211cdf0e10cSrcweir aCbFixed.Check( sal_False ); 212cdf0e10cSrcweir aCbShown.Show(); 213cdf0e10cSrcweir aCbShown.Check( sal_True ); 214cdf0e10cSrcweir aCbQuoteAll.Show(); 215cdf0e10cSrcweir aCbQuoteAll.Check( sal_False ); 216cdf0e10cSrcweir } 217cdf0e10cSrcweir else 218cdf0e10cSrcweir { 219cdf0e10cSrcweir aFlFieldOpt.SetText( aFtFont.GetText() ); 220cdf0e10cSrcweir aFtFieldSep.Hide(); 221cdf0e10cSrcweir aFtTextSep.Hide(); 222cdf0e10cSrcweir aFtFont.Hide(); 223cdf0e10cSrcweir aEdFieldSep.Hide(); 224cdf0e10cSrcweir aEdTextSep.Hide(); 225cdf0e10cSrcweir aCbFixed.Hide(); 226cdf0e10cSrcweir aCbShown.Hide(); 227cdf0e10cSrcweir aCbQuoteAll.Hide(); 228cdf0e10cSrcweir aLbFont.GrabFocus(); 229cdf0e10cSrcweir aLbFont.SetDoubleClickHdl( LINK( this, ScImportOptionsDlg, DoubleClickHdl ) ); 230cdf0e10cSrcweir } 231cdf0e10cSrcweir 232cdf0e10cSrcweir aLbFont.SelectTextEncoding( pOptions ? pOptions->eCharSet : 233cdf0e10cSrcweir gsl_getSystemTextEncoding() ); 234cdf0e10cSrcweir 235cdf0e10cSrcweir // optionaler Titel: 236cdf0e10cSrcweir if ( pStrTitle ) 237cdf0e10cSrcweir SetText( *pStrTitle ); 238cdf0e10cSrcweir 239cdf0e10cSrcweir FreeResource(); 240cdf0e10cSrcweir } 241cdf0e10cSrcweir 242cdf0e10cSrcweir //------------------------------------------------------------------------ 243cdf0e10cSrcweir 244cdf0e10cSrcweir __EXPORT ScImportOptionsDlg::~ScImportOptionsDlg() 245cdf0e10cSrcweir { 246cdf0e10cSrcweir delete pFieldSepTab; 247cdf0e10cSrcweir delete pTextSepTab; 248cdf0e10cSrcweir } 249cdf0e10cSrcweir 250cdf0e10cSrcweir //------------------------------------------------------------------------ 251cdf0e10cSrcweir 252cdf0e10cSrcweir void ScImportOptionsDlg::GetImportOptions( ScImportOptions& rOptions ) const 253cdf0e10cSrcweir { 254cdf0e10cSrcweir rOptions.SetTextEncoding( aLbFont.GetSelectTextEncoding() ); 255cdf0e10cSrcweir 256cdf0e10cSrcweir if ( aCbFixed.IsVisible() ) 257cdf0e10cSrcweir { 258cdf0e10cSrcweir rOptions.nFieldSepCode = GetCodeFromCombo( aEdFieldSep ); 259cdf0e10cSrcweir rOptions.nTextSepCode = GetCodeFromCombo( aEdTextSep ); 260cdf0e10cSrcweir rOptions.bFixedWidth = aCbFixed.IsChecked(); 261cdf0e10cSrcweir rOptions.bSaveAsShown = aCbShown.IsChecked(); 262cdf0e10cSrcweir rOptions.bQuoteAllText = aCbQuoteAll.IsChecked(); 263cdf0e10cSrcweir } 264cdf0e10cSrcweir } 265cdf0e10cSrcweir 266cdf0e10cSrcweir //------------------------------------------------------------------------ 267cdf0e10cSrcweir 268cdf0e10cSrcweir sal_uInt16 ScImportOptionsDlg::GetCodeFromCombo( const ComboBox& rEd ) const 269cdf0e10cSrcweir { 270cdf0e10cSrcweir ScDelimiterTable* pTab; 271cdf0e10cSrcweir String aStr( rEd.GetText() ); 272cdf0e10cSrcweir sal_uInt16 nCode; 273cdf0e10cSrcweir 274cdf0e10cSrcweir if ( &rEd == &aEdTextSep ) 275cdf0e10cSrcweir pTab = pTextSepTab; 276cdf0e10cSrcweir else 277cdf0e10cSrcweir pTab = pFieldSepTab; 278cdf0e10cSrcweir 279cdf0e10cSrcweir if ( !aStr.Len() ) 280cdf0e10cSrcweir { 281cdf0e10cSrcweir nCode = 0; // kein Trennzeichen 282cdf0e10cSrcweir } 283cdf0e10cSrcweir else 284cdf0e10cSrcweir { 285cdf0e10cSrcweir nCode = pTab->GetCode( aStr ); 286cdf0e10cSrcweir 287cdf0e10cSrcweir if ( nCode == 0 ) 288cdf0e10cSrcweir nCode = (sal_uInt16)aStr.GetChar(0); 289cdf0e10cSrcweir } 290cdf0e10cSrcweir 291cdf0e10cSrcweir return nCode; 292cdf0e10cSrcweir } 293cdf0e10cSrcweir 294cdf0e10cSrcweir //------------------------------------------------------------------------ 295cdf0e10cSrcweir 296cdf0e10cSrcweir IMPL_LINK( ScImportOptionsDlg, FixedWidthHdl, CheckBox*, pCheckBox ) 297cdf0e10cSrcweir { 298cdf0e10cSrcweir if( pCheckBox == &aCbFixed ) 299cdf0e10cSrcweir { 300cdf0e10cSrcweir sal_Bool bEnable = !aCbFixed.IsChecked(); 301cdf0e10cSrcweir aFtFieldSep.Enable( bEnable ); 302cdf0e10cSrcweir aEdFieldSep.Enable( bEnable ); 303cdf0e10cSrcweir aFtTextSep.Enable( bEnable ); 304cdf0e10cSrcweir aEdTextSep.Enable( bEnable ); 305cdf0e10cSrcweir aCbShown.Enable( bEnable ); 306cdf0e10cSrcweir aCbQuoteAll.Enable( bEnable ); 307cdf0e10cSrcweir } 308cdf0e10cSrcweir return 0; 309cdf0e10cSrcweir } 310cdf0e10cSrcweir 311cdf0e10cSrcweir IMPL_LINK( ScImportOptionsDlg, DoubleClickHdl, ListBox*, pLb ) 312cdf0e10cSrcweir { 313cdf0e10cSrcweir if ( pLb == &aLbFont ) 314cdf0e10cSrcweir { 315cdf0e10cSrcweir aBtnOk.Click(); 316cdf0e10cSrcweir } 317cdf0e10cSrcweir return 0; 318cdf0e10cSrcweir } 319