xref: /trunk/main/sc/source/ui/dbgui/scuiimoptdlg.cxx (revision a479921a)
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
25*b77af630Sdamjan #include "precompiled_scui.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir 
28cdf0e10cSrcweir 
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include "scuiimoptdlg.hxx"
31cdf0e10cSrcweir #include "scresid.hxx"
32cdf0e10cSrcweir #include "imoptdlg.hrc"
33cdf0e10cSrcweir #include <rtl/tencinfo.h>
34cdf0e10cSrcweir //========================================================================
35cdf0e10cSrcweir // ScDelimiterTable
36cdf0e10cSrcweir //========================================================================
37cdf0e10cSrcweir 
38cdf0e10cSrcweir class ScDelimiterTable
39cdf0e10cSrcweir {
40cdf0e10cSrcweir public:
ScDelimiterTable(const String & rDelTab)41cdf0e10cSrcweir 		ScDelimiterTable( const String& rDelTab )
42cdf0e10cSrcweir 			:	theDelTab ( rDelTab ),
43cdf0e10cSrcweir 				cSep	  ( '\t' ),
44cdf0e10cSrcweir 				nCount	  ( rDelTab.GetTokenCount('\t') ),
45cdf0e10cSrcweir 				nIter	  ( 0 )
46cdf0e10cSrcweir 			{}
47cdf0e10cSrcweir 
48cdf0e10cSrcweir 	sal_uInt16	GetCode( const String& rDelimiter ) const;
49cdf0e10cSrcweir 	String	GetDelimiter( sal_Unicode nCode ) const;
50cdf0e10cSrcweir 
FirstDel()51cdf0e10cSrcweir 	String	FirstDel()	{ nIter = 0; return theDelTab.GetToken( nIter, cSep ); }
NextDel()52cdf0e10cSrcweir 	String	NextDel()	{ nIter +=2; return theDelTab.GetToken( nIter, cSep ); }
53cdf0e10cSrcweir 
54cdf0e10cSrcweir private:
55cdf0e10cSrcweir 	const String		theDelTab;
56cdf0e10cSrcweir 	const sal_Unicode	cSep;
57cdf0e10cSrcweir 	const xub_StrLen	nCount;
58cdf0e10cSrcweir 	xub_StrLen			nIter;
59cdf0e10cSrcweir };
60cdf0e10cSrcweir 
61cdf0e10cSrcweir //------------------------------------------------------------------------
62cdf0e10cSrcweir 
GetCode(const String & rDel) const63cdf0e10cSrcweir sal_uInt16 ScDelimiterTable::GetCode( const String& rDel ) const
64cdf0e10cSrcweir {
65cdf0e10cSrcweir 	sal_Unicode nCode = 0;
66cdf0e10cSrcweir 	xub_StrLen i = 0;
67cdf0e10cSrcweir 
68cdf0e10cSrcweir 	if ( nCount >= 2 )
69cdf0e10cSrcweir 	{
70cdf0e10cSrcweir 		while ( i<nCount )
71cdf0e10cSrcweir 		{
72cdf0e10cSrcweir 			if ( rDel == theDelTab.GetToken( i, cSep ) )
73cdf0e10cSrcweir 			{
74cdf0e10cSrcweir 				nCode = (sal_Unicode) theDelTab.GetToken( i+1, cSep ).ToInt32();
75cdf0e10cSrcweir 				i     = nCount;
76cdf0e10cSrcweir 			}
77cdf0e10cSrcweir 			else
78cdf0e10cSrcweir 				i += 2;
79cdf0e10cSrcweir 		}
80cdf0e10cSrcweir 	}
81cdf0e10cSrcweir 
82cdf0e10cSrcweir 	return nCode;
83cdf0e10cSrcweir }
84cdf0e10cSrcweir 
85cdf0e10cSrcweir //------------------------------------------------------------------------
86cdf0e10cSrcweir 
GetDelimiter(sal_Unicode nCode) const87cdf0e10cSrcweir String ScDelimiterTable::GetDelimiter( sal_Unicode nCode ) const
88cdf0e10cSrcweir {
89cdf0e10cSrcweir 	String aStrDel;
90cdf0e10cSrcweir 	xub_StrLen i = 0;
91cdf0e10cSrcweir 
92cdf0e10cSrcweir 	if ( nCount >= 2 )
93cdf0e10cSrcweir 	{
94cdf0e10cSrcweir 		while ( i<nCount )
95cdf0e10cSrcweir 		{
96cdf0e10cSrcweir 			if ( nCode == (sal_Unicode) theDelTab.GetToken( i+1, cSep ).ToInt32() )
97cdf0e10cSrcweir 			{
98cdf0e10cSrcweir 				aStrDel = theDelTab.GetToken( i, cSep );
99cdf0e10cSrcweir 				i       = nCount;
100cdf0e10cSrcweir 			}
101cdf0e10cSrcweir 			else
102cdf0e10cSrcweir 				i += 2;
103cdf0e10cSrcweir 		}
104cdf0e10cSrcweir 	}
105cdf0e10cSrcweir 
106cdf0e10cSrcweir 	return aStrDel;
107cdf0e10cSrcweir }
108cdf0e10cSrcweir 
109cdf0e10cSrcweir //========================================================================
110cdf0e10cSrcweir // ScImportOptionsDlg
111cdf0e10cSrcweir //========================================================================
112cdf0e10cSrcweir 
ScImportOptionsDlg(Window * pParent,sal_Bool bAscii,const ScImportOptions * pOptions,const String * pStrTitle,sal_Bool bMultiByte,sal_Bool bOnlyDbtoolsEncodings,sal_Bool bImport)113cdf0e10cSrcweir ScImportOptionsDlg::ScImportOptionsDlg(
114cdf0e10cSrcweir         Window*                 pParent,
115cdf0e10cSrcweir         sal_Bool                    bAscii,
116cdf0e10cSrcweir         const ScImportOptions*  pOptions,
117cdf0e10cSrcweir         const String*           pStrTitle,
118cdf0e10cSrcweir         sal_Bool                    bMultiByte,
119cdf0e10cSrcweir         sal_Bool                    bOnlyDbtoolsEncodings,
120cdf0e10cSrcweir         sal_Bool                    bImport )
121cdf0e10cSrcweir 
122cdf0e10cSrcweir 	:	ModalDialog	( pParent, ScResId( RID_SCDLG_IMPORTOPT ) ),
123cdf0e10cSrcweir         aFlFieldOpt ( this, ScResId( FL_FIELDOPT ) ),
124cdf0e10cSrcweir 		aFtFont		( this, ScResId( FT_FONT ) ),
125cdf0e10cSrcweir         aLbFont     ( this, ScResId( bAscii ? DDLB_FONT : LB_FONT ) ),
126cdf0e10cSrcweir 		aFtFieldSep	( this, ScResId( FT_FIELDSEP ) ),
127cdf0e10cSrcweir 		aEdFieldSep	( this, ScResId( ED_FIELDSEP ) ),
128cdf0e10cSrcweir 		aFtTextSep	( this, ScResId( FT_TEXTSEP ) ),
129cdf0e10cSrcweir 		aEdTextSep	( this, ScResId( ED_TEXTSEP ) ),
130cdf0e10cSrcweir         aCbQuoteAll ( this, ScResId( CB_QUOTEALL ) ),
131cdf0e10cSrcweir         aCbShown    ( this, ScResId( CB_SAVESHOWN ) ),
132cdf0e10cSrcweir         aCbFixed    ( this, ScResId( CB_FIXEDWIDTH ) ),
133cdf0e10cSrcweir 		aBtnOk		( this, ScResId( BTN_OK ) ),
134cdf0e10cSrcweir 		aBtnCancel	( this, ScResId( BTN_CANCEL ) ),
135cdf0e10cSrcweir         aBtnHelp	( this, ScResId( BTN_HELP ) )
136cdf0e10cSrcweir {
13702c0dac2SAriel Constenla-Haile     String sFieldSep( ScResId( SCSTR_FIELDSEP ) );
13802c0dac2SAriel Constenla-Haile     sFieldSep.SearchAndReplaceAscii( "%TAB",   String(ScResId(SCSTR_FIELDSEP_TAB)) );
13902c0dac2SAriel Constenla-Haile     sFieldSep.SearchAndReplaceAscii( "%SPACE", String(ScResId(SCSTR_FIELDSEP_SPACE)) );
14002c0dac2SAriel Constenla-Haile 
141cdf0e10cSrcweir 	// im Ctor-Initializer nicht moeglich (MSC kann das nicht):
14202c0dac2SAriel Constenla-Haile 	pFieldSepTab = new ScDelimiterTable( sFieldSep );
143cdf0e10cSrcweir 	pTextSepTab  = new ScDelimiterTable( String(ScResId(SCSTR_TEXTSEP)) );
144cdf0e10cSrcweir 
145cdf0e10cSrcweir 	String aStr = pFieldSepTab->FirstDel();
146cdf0e10cSrcweir 	sal_Unicode nCode;
147cdf0e10cSrcweir 
148cdf0e10cSrcweir 	while ( aStr.Len() > 0 )
149cdf0e10cSrcweir 	{
150cdf0e10cSrcweir 		aEdFieldSep.InsertEntry( aStr );
151cdf0e10cSrcweir 		aStr = pFieldSepTab->NextDel();
152cdf0e10cSrcweir 	}
153cdf0e10cSrcweir 
154cdf0e10cSrcweir 	aStr = pTextSepTab->FirstDel();
155cdf0e10cSrcweir 
156cdf0e10cSrcweir 	while ( aStr.Len() > 0 )
157cdf0e10cSrcweir 	{
158cdf0e10cSrcweir 		aEdTextSep.InsertEntry( aStr );
159cdf0e10cSrcweir 		aStr = pTextSepTab->NextDel();
160cdf0e10cSrcweir 	}
161cdf0e10cSrcweir 
162cdf0e10cSrcweir 	aEdFieldSep.SetText( aEdFieldSep.GetEntry(0) );
163cdf0e10cSrcweir 	aEdTextSep.SetText( aEdTextSep.GetEntry(0) );
164cdf0e10cSrcweir 
165cdf0e10cSrcweir     if ( bOnlyDbtoolsEncodings )
166cdf0e10cSrcweir     {
167cdf0e10cSrcweir         // Even dBase export allows multibyte now
168cdf0e10cSrcweir         if ( bMultiByte )
169cdf0e10cSrcweir             aLbFont.FillFromDbTextEncodingMap( bImport );
170cdf0e10cSrcweir         else
171cdf0e10cSrcweir             aLbFont.FillFromDbTextEncodingMap( bImport, RTL_TEXTENCODING_INFO_MULTIBYTE );
172cdf0e10cSrcweir     }
173cdf0e10cSrcweir     else if ( !bAscii )
174cdf0e10cSrcweir     {   //!TODO: Unicode would need work in each filter
175cdf0e10cSrcweir 		if ( bMultiByte )
176cdf0e10cSrcweir             aLbFont.FillFromTextEncodingTable( bImport, RTL_TEXTENCODING_INFO_UNICODE );
177cdf0e10cSrcweir 		else
178cdf0e10cSrcweir 			aLbFont.FillFromTextEncodingTable( bImport, RTL_TEXTENCODING_INFO_UNICODE |
179cdf0e10cSrcweir                 RTL_TEXTENCODING_INFO_MULTIBYTE );
180cdf0e10cSrcweir 	}
181cdf0e10cSrcweir 	else
182cdf0e10cSrcweir 	{
183cdf0e10cSrcweir 		if ( pOptions )
184cdf0e10cSrcweir 		{
185cdf0e10cSrcweir 			nCode = pOptions->nFieldSepCode;
186cdf0e10cSrcweir 			aStr  = pFieldSepTab->GetDelimiter( nCode );
187cdf0e10cSrcweir 
188cdf0e10cSrcweir 			if ( !aStr.Len() )
189cdf0e10cSrcweir 				aEdFieldSep.SetText( String((sal_Unicode)nCode) );
190cdf0e10cSrcweir 			else
191cdf0e10cSrcweir 				aEdFieldSep.SetText( aStr );
192cdf0e10cSrcweir 
193cdf0e10cSrcweir 			nCode = pOptions->nTextSepCode;
194cdf0e10cSrcweir 			aStr  = pTextSepTab->GetDelimiter( nCode );
195cdf0e10cSrcweir 
196cdf0e10cSrcweir 			if ( !aStr.Len() )
197cdf0e10cSrcweir 				aEdTextSep.SetText( String((sal_Unicode)nCode) );
198cdf0e10cSrcweir 			else
199cdf0e10cSrcweir 				aEdTextSep.SetText( aStr );
200cdf0e10cSrcweir 		}
201cdf0e10cSrcweir 		// all encodings allowed, even Unicode
202cdf0e10cSrcweir 		aLbFont.FillFromTextEncodingTable( bImport );
203cdf0e10cSrcweir 	}
204cdf0e10cSrcweir 
205cdf0e10cSrcweir     if( bAscii )
206cdf0e10cSrcweir     {
207cdf0e10cSrcweir         Size aWinSize( GetSizePixel() );
208cdf0e10cSrcweir         aWinSize.Height() = aCbFixed.GetPosPixel().Y() + aCbFixed.GetSizePixel().Height();
209cdf0e10cSrcweir         Size aDiffSize( LogicToPixel( Size( 0, 6 ), MapMode( MAP_APPFONT ) ) );
210cdf0e10cSrcweir         aWinSize.Height() += aDiffSize.Height();
211cdf0e10cSrcweir         SetSizePixel( aWinSize );
212cdf0e10cSrcweir         aCbFixed.Show();
213cdf0e10cSrcweir         aCbFixed.SetClickHdl( LINK( this, ScImportOptionsDlg, FixedWidthHdl ) );
214cdf0e10cSrcweir         aCbFixed.Check( sal_False );
215cdf0e10cSrcweir         aCbShown.Show();
216cdf0e10cSrcweir         aCbShown.Check( sal_True );
217cdf0e10cSrcweir         aCbQuoteAll.Show();
218cdf0e10cSrcweir         aCbQuoteAll.Check( sal_False );
219cdf0e10cSrcweir     }
220cdf0e10cSrcweir     else
221cdf0e10cSrcweir     {
222cdf0e10cSrcweir         aFlFieldOpt.SetText( aFtFont.GetText() );
223cdf0e10cSrcweir 		aFtFieldSep.Hide();
224cdf0e10cSrcweir         aFtTextSep.Hide();
225cdf0e10cSrcweir         aFtFont.Hide();
226cdf0e10cSrcweir 		aEdFieldSep.Hide();
227cdf0e10cSrcweir         aEdTextSep.Hide();
228cdf0e10cSrcweir         aCbFixed.Hide();
229cdf0e10cSrcweir         aCbShown.Hide();
230cdf0e10cSrcweir         aCbQuoteAll.Hide();
231cdf0e10cSrcweir 		aLbFont.GrabFocus();
232cdf0e10cSrcweir         aLbFont.SetDoubleClickHdl( LINK( this, ScImportOptionsDlg, DoubleClickHdl ) );
233cdf0e10cSrcweir     }
234cdf0e10cSrcweir 
235cdf0e10cSrcweir 	aLbFont.SelectTextEncoding( pOptions ? pOptions->eCharSet :
236cdf0e10cSrcweir 		gsl_getSystemTextEncoding() );
237cdf0e10cSrcweir 
238cdf0e10cSrcweir 	// optionaler Titel:
239cdf0e10cSrcweir 	if ( pStrTitle )
240cdf0e10cSrcweir 		SetText( *pStrTitle );
241cdf0e10cSrcweir 
242cdf0e10cSrcweir 	FreeResource();
243cdf0e10cSrcweir }
244cdf0e10cSrcweir 
245cdf0e10cSrcweir //------------------------------------------------------------------------
246cdf0e10cSrcweir 
~ScImportOptionsDlg()247cdf0e10cSrcweir __EXPORT ScImportOptionsDlg::~ScImportOptionsDlg()
248cdf0e10cSrcweir {
249cdf0e10cSrcweir 	delete pFieldSepTab;
250cdf0e10cSrcweir 	delete pTextSepTab;
251cdf0e10cSrcweir }
252cdf0e10cSrcweir 
253cdf0e10cSrcweir //------------------------------------------------------------------------
254cdf0e10cSrcweir 
GetImportOptions(ScImportOptions & rOptions) const255cdf0e10cSrcweir void ScImportOptionsDlg::GetImportOptions( ScImportOptions& rOptions ) const
256cdf0e10cSrcweir {
257cdf0e10cSrcweir 	rOptions.SetTextEncoding( aLbFont.GetSelectTextEncoding() );
258cdf0e10cSrcweir 
259cdf0e10cSrcweir     if ( aCbFixed.IsVisible() )
260cdf0e10cSrcweir 	{
261cdf0e10cSrcweir 		rOptions.nFieldSepCode = GetCodeFromCombo( aEdFieldSep );
262cdf0e10cSrcweir 		rOptions.nTextSepCode  = GetCodeFromCombo( aEdTextSep );
263cdf0e10cSrcweir         rOptions.bFixedWidth = aCbFixed.IsChecked();
264cdf0e10cSrcweir         rOptions.bSaveAsShown = aCbShown.IsChecked();
265cdf0e10cSrcweir         rOptions.bQuoteAllText = aCbQuoteAll.IsChecked();
266cdf0e10cSrcweir 	}
267cdf0e10cSrcweir }
268cdf0e10cSrcweir 
269cdf0e10cSrcweir //------------------------------------------------------------------------
270cdf0e10cSrcweir 
GetCodeFromCombo(const ComboBox & rEd) const271cdf0e10cSrcweir sal_uInt16 ScImportOptionsDlg::GetCodeFromCombo( const ComboBox& rEd ) const
272cdf0e10cSrcweir {
273cdf0e10cSrcweir 	ScDelimiterTable* pTab;
274cdf0e10cSrcweir 	String  aStr( rEd.GetText() );
275cdf0e10cSrcweir 	sal_uInt16  nCode;
276cdf0e10cSrcweir 
277cdf0e10cSrcweir 	if ( &rEd == &aEdTextSep )
278cdf0e10cSrcweir 		pTab = pTextSepTab;
279cdf0e10cSrcweir 	else
280cdf0e10cSrcweir 		pTab = pFieldSepTab;
281cdf0e10cSrcweir 
282cdf0e10cSrcweir 	if ( !aStr.Len() )
283cdf0e10cSrcweir 	{
284cdf0e10cSrcweir 		nCode = 0;			// kein Trennzeichen
285cdf0e10cSrcweir 	}
286cdf0e10cSrcweir 	else
287cdf0e10cSrcweir 	{
288cdf0e10cSrcweir 		nCode = pTab->GetCode( aStr );
289cdf0e10cSrcweir 
290cdf0e10cSrcweir 		if ( nCode == 0 )
291cdf0e10cSrcweir 			nCode = (sal_uInt16)aStr.GetChar(0);
292cdf0e10cSrcweir 	}
293cdf0e10cSrcweir 
294cdf0e10cSrcweir 	return nCode;
295cdf0e10cSrcweir }
296cdf0e10cSrcweir 
297cdf0e10cSrcweir //------------------------------------------------------------------------
298cdf0e10cSrcweir 
IMPL_LINK(ScImportOptionsDlg,FixedWidthHdl,CheckBox *,pCheckBox)299cdf0e10cSrcweir IMPL_LINK( ScImportOptionsDlg, FixedWidthHdl, CheckBox*, pCheckBox )
300cdf0e10cSrcweir {
301cdf0e10cSrcweir     if( pCheckBox == &aCbFixed )
302cdf0e10cSrcweir     {
303cdf0e10cSrcweir         sal_Bool bEnable = !aCbFixed.IsChecked();
304cdf0e10cSrcweir         aFtFieldSep.Enable( bEnable );
305cdf0e10cSrcweir         aEdFieldSep.Enable( bEnable );
306cdf0e10cSrcweir         aFtTextSep.Enable( bEnable );
307cdf0e10cSrcweir         aEdTextSep.Enable( bEnable );
308cdf0e10cSrcweir         aCbShown.Enable( bEnable );
309cdf0e10cSrcweir         aCbQuoteAll.Enable( bEnable );
310cdf0e10cSrcweir     }
311cdf0e10cSrcweir     return 0;
312cdf0e10cSrcweir }
313cdf0e10cSrcweir 
IMPL_LINK(ScImportOptionsDlg,DoubleClickHdl,ListBox *,pLb)314cdf0e10cSrcweir  IMPL_LINK( ScImportOptionsDlg, DoubleClickHdl, ListBox*, pLb )
315cdf0e10cSrcweir {
316cdf0e10cSrcweir     if ( pLb == &aLbFont )
317cdf0e10cSrcweir     {
318cdf0e10cSrcweir         aBtnOk.Click();
319cdf0e10cSrcweir     }
320cdf0e10cSrcweir     return 0;
321cdf0e10cSrcweir }
322