xref: /trunk/main/sc/source/ui/dbgui/scuiimoptdlg.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sc.hxx"
30 
31 #undef SC_DLLIMPLEMENTATION
32 
33 
34 
35 #include "scuiimoptdlg.hxx"
36 #include "scresid.hxx"
37 #include "imoptdlg.hrc"
38 #include <rtl/tencinfo.h>
39 //========================================================================
40 // ScDelimiterTable
41 //========================================================================
42 
43 class ScDelimiterTable
44 {
45 public:
46         ScDelimiterTable( const String& rDelTab )
47             :   theDelTab ( rDelTab ),
48                 cSep      ( '\t' ),
49                 nCount    ( rDelTab.GetTokenCount('\t') ),
50                 nIter     ( 0 )
51             {}
52 
53     sal_uInt16  GetCode( const String& rDelimiter ) const;
54     String  GetDelimiter( sal_Unicode nCode ) const;
55 
56     String  FirstDel()  { nIter = 0; return theDelTab.GetToken( nIter, cSep ); }
57     String  NextDel()   { nIter +=2; return theDelTab.GetToken( nIter, cSep ); }
58 
59 private:
60     const String        theDelTab;
61     const sal_Unicode   cSep;
62     const xub_StrLen    nCount;
63     xub_StrLen          nIter;
64 };
65 
66 //------------------------------------------------------------------------
67 
68 sal_uInt16 ScDelimiterTable::GetCode( const String& rDel ) const
69 {
70     sal_Unicode nCode = 0;
71     xub_StrLen i = 0;
72 
73     if ( nCount >= 2 )
74     {
75         while ( i<nCount )
76         {
77             if ( rDel == theDelTab.GetToken( i, cSep ) )
78             {
79                 nCode = (sal_Unicode) theDelTab.GetToken( i+1, cSep ).ToInt32();
80                 i     = nCount;
81             }
82             else
83                 i += 2;
84         }
85     }
86 
87     return nCode;
88 }
89 
90 //------------------------------------------------------------------------
91 
92 String ScDelimiterTable::GetDelimiter( sal_Unicode nCode ) const
93 {
94     String aStrDel;
95     xub_StrLen i = 0;
96 
97     if ( nCount >= 2 )
98     {
99         while ( i<nCount )
100         {
101             if ( nCode == (sal_Unicode) theDelTab.GetToken( i+1, cSep ).ToInt32() )
102             {
103                 aStrDel = theDelTab.GetToken( i, cSep );
104                 i       = nCount;
105             }
106             else
107                 i += 2;
108         }
109     }
110 
111     return aStrDel;
112 }
113 
114 //========================================================================
115 // ScImportOptionsDlg
116 //========================================================================
117 
118 ScImportOptionsDlg::ScImportOptionsDlg(
119         Window*                 pParent,
120         sal_Bool                    bAscii,
121         const ScImportOptions*  pOptions,
122         const String*           pStrTitle,
123         sal_Bool                    bMultiByte,
124         sal_Bool                    bOnlyDbtoolsEncodings,
125         sal_Bool                    bImport )
126 
127     :   ModalDialog ( pParent, ScResId( RID_SCDLG_IMPORTOPT ) ),
128         aFlFieldOpt ( this, ScResId( FL_FIELDOPT ) ),
129         aFtFont     ( this, ScResId( FT_FONT ) ),
130         aLbFont     ( this, ScResId( bAscii ? DDLB_FONT : LB_FONT ) ),
131         aFtFieldSep ( this, ScResId( FT_FIELDSEP ) ),
132         aEdFieldSep ( this, ScResId( ED_FIELDSEP ) ),
133         aFtTextSep  ( this, ScResId( FT_TEXTSEP ) ),
134         aEdTextSep  ( this, ScResId( ED_TEXTSEP ) ),
135         aCbQuoteAll ( this, ScResId( CB_QUOTEALL ) ),
136         aCbShown    ( this, ScResId( CB_SAVESHOWN ) ),
137         aCbFixed    ( this, ScResId( CB_FIXEDWIDTH ) ),
138         aBtnOk      ( this, ScResId( BTN_OK ) ),
139         aBtnCancel  ( this, ScResId( BTN_CANCEL ) ),
140         aBtnHelp    ( this, ScResId( BTN_HELP ) )
141 {
142     // im Ctor-Initializer nicht moeglich (MSC kann das nicht):
143     pFieldSepTab = new ScDelimiterTable( String(ScResId(SCSTR_FIELDSEP)) );
144     pTextSepTab  = new ScDelimiterTable( String(ScResId(SCSTR_TEXTSEP)) );
145 
146     String aStr = pFieldSepTab->FirstDel();
147     sal_Unicode nCode;
148 
149     while ( aStr.Len() > 0 )
150     {
151         aEdFieldSep.InsertEntry( aStr );
152         aStr = pFieldSepTab->NextDel();
153     }
154 
155     aStr = pTextSepTab->FirstDel();
156 
157     while ( aStr.Len() > 0 )
158     {
159         aEdTextSep.InsertEntry( aStr );
160         aStr = pTextSepTab->NextDel();
161     }
162 
163     aEdFieldSep.SetText( aEdFieldSep.GetEntry(0) );
164     aEdTextSep.SetText( aEdTextSep.GetEntry(0) );
165 
166     if ( bOnlyDbtoolsEncodings )
167     {
168         // Even dBase export allows multibyte now
169         if ( bMultiByte )
170             aLbFont.FillFromDbTextEncodingMap( bImport );
171         else
172             aLbFont.FillFromDbTextEncodingMap( bImport, RTL_TEXTENCODING_INFO_MULTIBYTE );
173     }
174     else if ( !bAscii )
175     {   //!TODO: Unicode would need work in each filter
176         if ( bMultiByte )
177             aLbFont.FillFromTextEncodingTable( bImport, RTL_TEXTENCODING_INFO_UNICODE );
178         else
179             aLbFont.FillFromTextEncodingTable( bImport, RTL_TEXTENCODING_INFO_UNICODE |
180                 RTL_TEXTENCODING_INFO_MULTIBYTE );
181     }
182     else
183     {
184         if ( pOptions )
185         {
186             nCode = pOptions->nFieldSepCode;
187             aStr  = pFieldSepTab->GetDelimiter( nCode );
188 
189             if ( !aStr.Len() )
190                 aEdFieldSep.SetText( String((sal_Unicode)nCode) );
191             else
192                 aEdFieldSep.SetText( aStr );
193 
194             nCode = pOptions->nTextSepCode;
195             aStr  = pTextSepTab->GetDelimiter( nCode );
196 
197             if ( !aStr.Len() )
198                 aEdTextSep.SetText( String((sal_Unicode)nCode) );
199             else
200                 aEdTextSep.SetText( aStr );
201         }
202         // all encodings allowed, even Unicode
203         aLbFont.FillFromTextEncodingTable( bImport );
204     }
205 
206     if( bAscii )
207     {
208         Size aWinSize( GetSizePixel() );
209         aWinSize.Height() = aCbFixed.GetPosPixel().Y() + aCbFixed.GetSizePixel().Height();
210         Size aDiffSize( LogicToPixel( Size( 0, 6 ), MapMode( MAP_APPFONT ) ) );
211         aWinSize.Height() += aDiffSize.Height();
212         SetSizePixel( aWinSize );
213         aCbFixed.Show();
214         aCbFixed.SetClickHdl( LINK( this, ScImportOptionsDlg, FixedWidthHdl ) );
215         aCbFixed.Check( sal_False );
216         aCbShown.Show();
217         aCbShown.Check( sal_True );
218         aCbQuoteAll.Show();
219         aCbQuoteAll.Check( sal_False );
220     }
221     else
222     {
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         aCbQuoteAll.Hide();
232         aLbFont.GrabFocus();
233         aLbFont.SetDoubleClickHdl( LINK( this, ScImportOptionsDlg, DoubleClickHdl ) );
234     }
235 
236     aLbFont.SelectTextEncoding( pOptions ? pOptions->eCharSet :
237         gsl_getSystemTextEncoding() );
238 
239     // optionaler Titel:
240     if ( pStrTitle )
241         SetText( *pStrTitle );
242 
243     FreeResource();
244 }
245 
246 //------------------------------------------------------------------------
247 
248 __EXPORT ScImportOptionsDlg::~ScImportOptionsDlg()
249 {
250     delete pFieldSepTab;
251     delete pTextSepTab;
252 }
253 
254 //------------------------------------------------------------------------
255 
256 void ScImportOptionsDlg::GetImportOptions( ScImportOptions& rOptions ) const
257 {
258     rOptions.SetTextEncoding( aLbFont.GetSelectTextEncoding() );
259 
260     if ( aCbFixed.IsVisible() )
261     {
262         rOptions.nFieldSepCode = GetCodeFromCombo( aEdFieldSep );
263         rOptions.nTextSepCode  = GetCodeFromCombo( aEdTextSep );
264         rOptions.bFixedWidth = aCbFixed.IsChecked();
265         rOptions.bSaveAsShown = aCbShown.IsChecked();
266         rOptions.bQuoteAllText = aCbQuoteAll.IsChecked();
267     }
268 }
269 
270 //------------------------------------------------------------------------
271 
272 sal_uInt16 ScImportOptionsDlg::GetCodeFromCombo( const ComboBox& rEd ) const
273 {
274     ScDelimiterTable* pTab;
275     String  aStr( rEd.GetText() );
276     sal_uInt16  nCode;
277 
278     if ( &rEd == &aEdTextSep )
279         pTab = pTextSepTab;
280     else
281         pTab = pFieldSepTab;
282 
283     if ( !aStr.Len() )
284     {
285         nCode = 0;          // kein Trennzeichen
286     }
287     else
288     {
289         nCode = pTab->GetCode( aStr );
290 
291         if ( nCode == 0 )
292             nCode = (sal_uInt16)aStr.GetChar(0);
293     }
294 
295     return nCode;
296 }
297 
298 //------------------------------------------------------------------------
299 
300 IMPL_LINK( ScImportOptionsDlg, FixedWidthHdl, CheckBox*, pCheckBox )
301 {
302     if( pCheckBox == &aCbFixed )
303     {
304         sal_Bool bEnable = !aCbFixed.IsChecked();
305         aFtFieldSep.Enable( bEnable );
306         aEdFieldSep.Enable( bEnable );
307         aFtTextSep.Enable( bEnable );
308         aEdTextSep.Enable( bEnable );
309         aCbShown.Enable( bEnable );
310         aCbQuoteAll.Enable( bEnable );
311     }
312     return 0;
313 }
314 
315  IMPL_LINK( ScImportOptionsDlg, DoubleClickHdl, ListBox*, pLb )
316 {
317     if ( pLb == &aLbFont )
318     {
319         aBtnOk.Click();
320     }
321     return 0;
322 }
323