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