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