xref: /AOO41X/main/sc/source/ui/dbgui/scuiimoptdlg.cxx (revision 02c0dac2d84903df6395084bcb82bcfb80f5619d)
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:
ScDelimiterTable(const String & rDelTab)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 
FirstDel()52     String  FirstDel()  { nIter = 0; return theDelTab.GetToken( nIter, cSep ); }
NextDel()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 
GetCode(const String & rDel) const64 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 
GetDelimiter(sal_Unicode nCode) const88 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 
ScImportOptionsDlg(Window * pParent,sal_Bool bAscii,const ScImportOptions * pOptions,const String * pStrTitle,sal_Bool bMultiByte,sal_Bool bOnlyDbtoolsEncodings,sal_Bool bImport)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     String sFieldSep( ScResId( SCSTR_FIELDSEP ) );
139     sFieldSep.SearchAndReplaceAscii( "%TAB",   String(ScResId(SCSTR_FIELDSEP_TAB)) );
140     sFieldSep.SearchAndReplaceAscii( "%SPACE", String(ScResId(SCSTR_FIELDSEP_SPACE)) );
141 
142     // im Ctor-Initializer nicht moeglich (MSC kann das nicht):
143     pFieldSepTab = new ScDelimiterTable( sFieldSep );
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 
~ScImportOptionsDlg()248 __EXPORT ScImportOptionsDlg::~ScImportOptionsDlg()
249 {
250     delete pFieldSepTab;
251     delete pTextSepTab;
252 }
253 
254 //------------------------------------------------------------------------
255 
GetImportOptions(ScImportOptions & rOptions) const256 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 
GetCodeFromCombo(const ComboBox & rEd) const272 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 
IMPL_LINK(ScImportOptionsDlg,FixedWidthHdl,CheckBox *,pCheckBox)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 
IMPL_LINK(ScImportOptionsDlg,DoubleClickHdl,ListBox *,pLb)315  IMPL_LINK( ScImportOptionsDlg, DoubleClickHdl, ListBox*, pLb )
316 {
317     if ( pLb == &aLbFont )
318     {
319         aBtnOk.Click();
320     }
321     return 0;
322 }
323