update dev300-m57
[ooovba.git] / sc / source / ui / dbgui / scuiimoptdlg.cxx
blobe6e61f46e0954e4d7bbb778ea036242abd53b0af
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: scuiimoptdlg.cxx,v $
10 * $Revision: 1.9 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sc.hxx"
34 #undef SC_DLLIMPLEMENTATION
38 #include "scuiimoptdlg.hxx"
39 #include "scresid.hxx"
40 #include "imoptdlg.hrc"
41 #include <rtl/tencinfo.h>
42 //========================================================================
43 // ScDelimiterTable
44 //========================================================================
46 class ScDelimiterTable
48 public:
49 ScDelimiterTable( const String& rDelTab )
50 : theDelTab ( rDelTab ),
51 cSep ( '\t' ),
52 nCount ( rDelTab.GetTokenCount('\t') ),
53 nIter ( 0 )
56 USHORT GetCode( const String& rDelimiter ) const;
57 String GetDelimiter( sal_Unicode nCode ) const;
59 String FirstDel() { nIter = 0; return theDelTab.GetToken( nIter, cSep ); }
60 String NextDel() { nIter +=2; return theDelTab.GetToken( nIter, cSep ); }
62 private:
63 const String theDelTab;
64 const sal_Unicode cSep;
65 const xub_StrLen nCount;
66 xub_StrLen nIter;
69 //------------------------------------------------------------------------
71 USHORT ScDelimiterTable::GetCode( const String& rDel ) const
73 sal_Unicode nCode = 0;
74 xub_StrLen i = 0;
76 if ( nCount >= 2 )
78 while ( i<nCount )
80 if ( rDel == theDelTab.GetToken( i, cSep ) )
82 nCode = (sal_Unicode) theDelTab.GetToken( i+1, cSep ).ToInt32();
83 i = nCount;
85 else
86 i += 2;
90 return nCode;
93 //------------------------------------------------------------------------
95 String ScDelimiterTable::GetDelimiter( sal_Unicode nCode ) const
97 String aStrDel;
98 xub_StrLen i = 0;
100 if ( nCount >= 2 )
102 while ( i<nCount )
104 if ( nCode == (sal_Unicode) theDelTab.GetToken( i+1, cSep ).ToInt32() )
106 aStrDel = theDelTab.GetToken( i, cSep );
107 i = nCount;
109 else
110 i += 2;
114 return aStrDel;
117 //========================================================================
118 // ScImportOptionsDlg
119 //========================================================================
121 ScImportOptionsDlg::ScImportOptionsDlg(
122 Window* pParent,
123 BOOL bAscii,
124 const ScImportOptions* pOptions,
125 const String* pStrTitle,
126 BOOL bMultiByte,
127 BOOL bOnlyDbtoolsEncodings,
128 BOOL bImport )
130 : ModalDialog ( pParent, ScResId( RID_SCDLG_IMPORTOPT ) ),
131 aFlFieldOpt ( this, ScResId( FL_FIELDOPT ) ),
132 aFtFont ( this, ScResId( FT_FONT ) ),
133 aLbFont ( this, ScResId( bAscii ? DDLB_FONT : LB_FONT ) ),
134 aFtFieldSep ( this, ScResId( FT_FIELDSEP ) ),
135 aEdFieldSep ( this, ScResId( ED_FIELDSEP ) ),
136 aFtTextSep ( this, ScResId( FT_TEXTSEP ) ),
137 aEdTextSep ( this, ScResId( ED_TEXTSEP ) ),
138 aCbFixed ( this, ScResId( CB_FIXEDWIDTH ) ),
139 aBtnOk ( this, ScResId( BTN_OK ) ),
140 aBtnCancel ( this, ScResId( BTN_CANCEL ) ),
141 aBtnHelp ( this, ScResId( BTN_HELP ) ),
142 aCbShown ( this, ScResId( CB_SAVESHOWN ) )
144 // im Ctor-Initializer nicht moeglich (MSC kann das nicht):
145 pFieldSepTab = new ScDelimiterTable( String(ScResId(SCSTR_FIELDSEP)) );
146 pTextSepTab = new ScDelimiterTable( String(ScResId(SCSTR_TEXTSEP)) );
148 String aStr = pFieldSepTab->FirstDel();
149 sal_Unicode nCode;
151 while ( aStr.Len() > 0 )
153 aEdFieldSep.InsertEntry( aStr );
154 aStr = pFieldSepTab->NextDel();
157 aStr = pTextSepTab->FirstDel();
159 while ( aStr.Len() > 0 )
161 aEdTextSep.InsertEntry( aStr );
162 aStr = pTextSepTab->NextDel();
165 aEdFieldSep.SetText( aEdFieldSep.GetEntry(0) );
166 aEdTextSep.SetText( aEdTextSep.GetEntry(0) );
168 if ( bOnlyDbtoolsEncodings )
170 // Even dBase export allows multibyte now
171 if ( bMultiByte )
172 aLbFont.FillFromDbTextEncodingMap( bImport );
173 else
174 aLbFont.FillFromDbTextEncodingMap( bImport, RTL_TEXTENCODING_INFO_MULTIBYTE );
176 else if ( !bAscii )
177 { //!TODO: Unicode would need work in each filter
178 if ( bMultiByte )
179 aLbFont.FillFromTextEncodingTable( bImport, RTL_TEXTENCODING_INFO_UNICODE );
180 else
181 aLbFont.FillFromTextEncodingTable( bImport, RTL_TEXTENCODING_INFO_UNICODE |
182 RTL_TEXTENCODING_INFO_MULTIBYTE );
184 else
186 if ( pOptions )
188 nCode = pOptions->nFieldSepCode;
189 aStr = pFieldSepTab->GetDelimiter( nCode );
191 if ( !aStr.Len() )
192 aEdFieldSep.SetText( String((sal_Unicode)nCode) );
193 else
194 aEdFieldSep.SetText( aStr );
196 nCode = pOptions->nTextSepCode;
197 aStr = pTextSepTab->GetDelimiter( nCode );
199 if ( !aStr.Len() )
200 aEdTextSep.SetText( String((sal_Unicode)nCode) );
201 else
202 aEdTextSep.SetText( aStr );
204 // all encodings allowed, even Unicode
205 aLbFont.FillFromTextEncodingTable( bImport );
208 if( bAscii )
210 Size aWinSize( GetSizePixel() );
211 aWinSize.Height() = aCbFixed.GetPosPixel().Y() + aCbFixed.GetSizePixel().Height();
212 Size aDiffSize( LogicToPixel( Size( 0, 6 ), MapMode( MAP_APPFONT ) ) );
213 aWinSize.Height() += aDiffSize.Height();
214 SetSizePixel( aWinSize );
215 aCbFixed.Show();
216 aCbFixed.SetClickHdl( LINK( this, ScImportOptionsDlg, FixedWidthHdl ) );
217 aCbFixed.Check( FALSE );
218 aCbShown.Show();
219 aCbShown.Check( TRUE );
221 else
223 aFlFieldOpt.SetText( aFtFont.GetText() );
224 aFtFieldSep.Hide();
225 aFtTextSep.Hide();
226 aFtFont.Hide();
227 aEdFieldSep.Hide();
228 aEdTextSep.Hide();
229 aCbFixed.Hide();
230 aCbShown.Hide();
231 aLbFont.GrabFocus();
232 aLbFont.SetDoubleClickHdl( LINK( this, ScImportOptionsDlg, DoubleClickHdl ) );
235 aLbFont.SelectTextEncoding( pOptions ? pOptions->eCharSet :
236 gsl_getSystemTextEncoding() );
238 // optionaler Titel:
239 if ( pStrTitle )
240 SetText( *pStrTitle );
242 FreeResource();
245 //------------------------------------------------------------------------
247 __EXPORT ScImportOptionsDlg::~ScImportOptionsDlg()
249 delete pFieldSepTab;
250 delete pTextSepTab;
253 //------------------------------------------------------------------------
255 void ScImportOptionsDlg::GetImportOptions( ScImportOptions& rOptions ) const
257 rOptions.SetTextEncoding( aLbFont.GetSelectTextEncoding() );
259 if ( aCbFixed.IsVisible() )
261 rOptions.nFieldSepCode = GetCodeFromCombo( aEdFieldSep );
262 rOptions.nTextSepCode = GetCodeFromCombo( aEdTextSep );
263 rOptions.bFixedWidth = aCbFixed.IsChecked();
264 rOptions.bSaveAsShown = aCbShown.IsChecked();
268 //------------------------------------------------------------------------
270 USHORT ScImportOptionsDlg::GetCodeFromCombo( const ComboBox& rEd ) const
272 ScDelimiterTable* pTab;
273 String aStr( rEd.GetText() );
274 USHORT nCode;
276 if ( &rEd == &aEdTextSep )
277 pTab = pTextSepTab;
278 else
279 pTab = pFieldSepTab;
281 if ( !aStr.Len() )
283 nCode = 0; // kein Trennzeichen
285 else
287 nCode = pTab->GetCode( aStr );
289 if ( nCode == 0 )
290 nCode = (USHORT)aStr.GetChar(0);
293 return nCode;
296 //------------------------------------------------------------------------
298 IMPL_LINK( ScImportOptionsDlg, FixedWidthHdl, CheckBox*, pCheckBox )
300 if( pCheckBox == &aCbFixed )
302 BOOL bEnable = !aCbFixed.IsChecked();
303 aFtFieldSep.Enable( bEnable );
304 aEdFieldSep.Enable( bEnable );
305 aFtTextSep.Enable( bEnable );
306 aEdTextSep.Enable( bEnable );
307 aCbShown.Enable( bEnable );
309 return 0;
312 IMPL_LINK( ScImportOptionsDlg, DoubleClickHdl, ListBox*, pLb )
314 if ( pLb == &aLbFont )
316 aBtnOk.Click();
318 return 0;