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