Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / sc / source / ui / dbgui / scuiimoptdlg.cxx
blob114ec3c88e5f10bcf99c13d4177d497e986e72ed
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #undef SC_DLLIMPLEMENTATION
22 #include "scuiimoptdlg.hxx"
23 #include "tabvwsh.hxx"
24 #include "scresid.hxx"
25 #include "sc.hrc"
26 #include <comphelper/string.hxx>
27 #include <osl/thread.h>
28 #include <rtl/tencinfo.h>
30 // ScDelimiterTable
32 class ScDelimiterTable
34 public:
35 explicit ScDelimiterTable( const OUString& rDelTab )
36 : theDelTab ( rDelTab ),
37 cSep ( '\t' ),
38 nCount ( comphelper::string::getTokenCount(rDelTab, '\t') ),
39 nIter ( 0 )
42 sal_uInt16 GetCode( const OUString& rDelimiter ) const;
43 OUString GetDelimiter( sal_Unicode nCode ) const;
45 OUString FirstDel() { nIter = 0; return theDelTab.getToken( nIter, cSep ); }
46 OUString NextDel() { nIter +=2; return theDelTab.getToken( nIter, cSep ); }
48 private:
49 const OUString theDelTab;
50 const sal_Unicode cSep;
51 const sal_Int32 nCount;
52 sal_Int32 nIter;
55 sal_uInt16 ScDelimiterTable::GetCode( const OUString& rDel ) const
57 sal_Unicode nCode = 0;
59 if ( nCount >= 2 )
61 sal_Int32 i = 0;
62 while ( i<nCount )
64 if ( rDel == theDelTab.getToken( i, cSep ) )
66 nCode = (sal_Unicode) theDelTab.getToken( i+1, cSep ).toInt32();
67 i = nCount;
69 else
70 i += 2;
74 return nCode;
77 OUString ScDelimiterTable::GetDelimiter( sal_Unicode nCode ) const
79 OUString aStrDel;
81 if ( nCount >= 2 )
83 sal_Int32 i = 0;
84 while ( i<nCount )
86 if ( nCode == (sal_Unicode) theDelTab.getToken( i+1, cSep ).toInt32() )
88 aStrDel = theDelTab.getToken( i, cSep );
89 i = nCount;
91 else
92 i += 2;
96 return aStrDel;
99 // ScImportOptionsDlg
101 ScImportOptionsDlg::ScImportOptionsDlg(
102 vcl::Window* pParent,
103 bool bAscii,
104 const ScImportOptions* pOptions,
105 const OUString* pStrTitle,
106 bool bMultiByte,
107 bool bOnlyDbtoolsEncodings,
108 bool bImport )
109 : ModalDialog ( pParent, "ImOptDialog",
110 "modules/scalc/ui/imoptdialog.ui" )
112 get(m_pFieldFrame, "fieldframe");
113 get(m_pFtCharset, "charsetft");
114 if (bAscii)
115 get(m_pLbCharset, "charsetdropdown");
116 else
118 get(m_pLbCharset, "charsetlist");
119 m_pLbCharset->set_height_request(6 * m_pLbCharset->GetTextHeight());
120 get(m_pEncGrid, "grid2");
121 m_pEncGrid->set_vexpand(true);
123 m_pLbCharset->SetStyle(m_pLbCharset->GetStyle() | WB_SORT);
124 m_pLbCharset->Show();
125 get(m_pFtFieldSep, "fieldft");
126 get(m_pEdFieldSep, "field");
127 get(m_pFtTextSep, "textft");
128 get(m_pEdTextSep, "text");
129 get(m_pCbShown, "asshown");
130 get(m_pCbFormulas, "formulas");
131 get(m_pCbQuoteAll, "quoteall");
132 get(m_pCbFixed, "fixedwidth");
133 get(m_pBtnOk, "ok");
135 OUString sFieldSep(SC_RESSTR(SCSTR_FIELDSEP));
136 sFieldSep = sFieldSep.replaceFirst( "%TAB", SC_RESSTR(SCSTR_FIELDSEP_TAB) );
137 sFieldSep = sFieldSep.replaceFirst( "%SPACE", SC_RESSTR(SCSTR_FIELDSEP_SPACE) );
139 // im Ctor-Initializer nicht moeglich (MSC kann das nicht):
140 pFieldSepTab = new ScDelimiterTable( sFieldSep );
141 pTextSepTab = new ScDelimiterTable( OUString(ScResId(SCSTR_TEXTSEP)) );
143 OUString aStr = pFieldSepTab->FirstDel();
144 sal_Unicode nCode;
146 while ( !aStr.isEmpty() )
148 m_pEdFieldSep->InsertEntry( aStr );
149 aStr = pFieldSepTab->NextDel();
152 aStr = pTextSepTab->FirstDel();
154 while ( !aStr.isEmpty() )
156 m_pEdTextSep->InsertEntry( aStr );
157 aStr = pTextSepTab->NextDel();
160 m_pEdFieldSep->SetText( m_pEdFieldSep->GetEntry(0) );
161 m_pEdTextSep->SetText( m_pEdTextSep->GetEntry(0) );
163 if ( bOnlyDbtoolsEncodings )
165 // Even dBase export allows multibyte now
166 if ( bMultiByte )
167 m_pLbCharset->FillFromDbTextEncodingMap( bImport );
168 else
169 m_pLbCharset->FillFromDbTextEncodingMap( bImport, RTL_TEXTENCODING_INFO_MULTIBYTE );
171 else if ( !bAscii )
172 { //!TODO: Unicode would need work in each filter
173 if ( bMultiByte )
174 m_pLbCharset->FillFromTextEncodingTable( bImport, RTL_TEXTENCODING_INFO_UNICODE );
175 else
176 m_pLbCharset->FillFromTextEncodingTable( bImport, RTL_TEXTENCODING_INFO_UNICODE |
177 RTL_TEXTENCODING_INFO_MULTIBYTE );
179 else
181 if ( pOptions )
183 nCode = pOptions->nFieldSepCode;
184 aStr = pFieldSepTab->GetDelimiter( nCode );
186 if ( aStr.isEmpty() )
187 m_pEdFieldSep->SetText( OUString((sal_Unicode)nCode) );
188 else
189 m_pEdFieldSep->SetText( aStr );
191 nCode = pOptions->nTextSepCode;
192 aStr = pTextSepTab->GetDelimiter( nCode );
194 if ( aStr.isEmpty() )
195 m_pEdTextSep->SetText( OUString((sal_Unicode)nCode) );
196 else
197 m_pEdTextSep->SetText( aStr );
199 // all encodings allowed, even Unicode
200 m_pLbCharset->FillFromTextEncodingTable( bImport );
203 if( bAscii )
205 m_pCbFixed->Show();
206 m_pCbFixed->SetClickHdl( LINK( this, ScImportOptionsDlg, FixedWidthHdl ) );
207 m_pCbFixed->Check( false );
208 m_pCbShown->Show();
209 m_pCbShown->Check();
210 m_pCbQuoteAll->Show();
211 m_pCbQuoteAll->Check( false );
212 m_pCbFormulas->Show();
213 ScTabViewShell* pViewSh = dynamic_cast<ScTabViewShell*>( SfxViewShell::Current() );
214 bool bFormulas = pViewSh &&
215 pViewSh->GetViewData().GetOptions().GetOption( VOPT_FORMULAS);
216 m_pCbFormulas->Check( bFormulas );
218 else
220 m_pFieldFrame->set_label(m_pFtCharset->GetText());
221 m_pFtFieldSep->Hide();
222 m_pFtTextSep->Hide();
223 m_pFtCharset->Hide();
224 m_pEdFieldSep->Hide();
225 m_pEdTextSep->Hide();
226 m_pCbFixed->Hide();
227 m_pCbShown->Hide();
228 m_pCbQuoteAll->Hide();
229 m_pCbFormulas->Hide();
230 m_pLbCharset->GrabFocus();
231 m_pLbCharset->SetDoubleClickHdl( LINK( this, ScImportOptionsDlg, DoubleClickHdl ) );
234 m_pLbCharset->SelectTextEncoding( pOptions ? pOptions->eCharSet :
235 osl_getThreadTextEncoding() );
237 // optionaler Titel:
238 if ( pStrTitle )
239 SetText( *pStrTitle );
242 ScImportOptionsDlg::~ScImportOptionsDlg()
244 disposeOnce();
247 void ScImportOptionsDlg::dispose()
249 delete pFieldSepTab;
250 delete pTextSepTab;
251 m_pFieldFrame.clear();
252 m_pFtCharset.clear();
253 m_pLbCharset.clear();
254 m_pFtFieldSep.clear();
255 m_pEdFieldSep.clear();
256 m_pFtTextSep.clear();
257 m_pEdTextSep.clear();
258 m_pCbShown.clear();
259 m_pCbFormulas.clear();
260 m_pCbQuoteAll.clear();
261 m_pCbFixed.clear();
262 m_pBtnOk.clear();
263 ModalDialog::dispose();
266 void ScImportOptionsDlg::GetImportOptions( ScImportOptions& rOptions ) const
268 rOptions.SetTextEncoding( m_pLbCharset->GetSelectTextEncoding() );
270 if ( m_pCbFixed->IsVisible() )
272 rOptions.nFieldSepCode = GetCodeFromCombo( *m_pEdFieldSep );
273 rOptions.nTextSepCode = GetCodeFromCombo( *m_pEdTextSep );
274 rOptions.bFixedWidth = m_pCbFixed->IsChecked();
275 rOptions.bSaveAsShown = m_pCbShown->IsChecked();
276 rOptions.bQuoteAllText = m_pCbQuoteAll->IsChecked();
277 rOptions.bSaveFormulas = m_pCbFormulas->IsChecked();
281 sal_uInt16 ScImportOptionsDlg::GetCodeFromCombo( const ComboBox& rEd ) const
283 ScDelimiterTable* pTab;
284 OUString aStr( rEd.GetText() );
285 sal_uInt16 nCode;
287 if ( &rEd == m_pEdTextSep )
288 pTab = pTextSepTab;
289 else
290 pTab = pFieldSepTab;
292 if ( aStr.isEmpty() )
294 nCode = 0; // kein Trennzeichen
296 else
298 nCode = pTab->GetCode( aStr );
300 if ( nCode == 0 )
301 nCode = (sal_uInt16)aStr[0];
304 return nCode;
307 IMPL_LINK_TYPED( ScImportOptionsDlg, FixedWidthHdl, Button*, pCheckBox, void )
309 if (pCheckBox == m_pCbFixed)
311 bool bEnable = !m_pCbFixed->IsChecked();
312 m_pFtFieldSep->Enable( bEnable );
313 m_pEdFieldSep->Enable( bEnable );
314 m_pFtTextSep->Enable( bEnable );
315 m_pEdTextSep->Enable( bEnable );
316 m_pCbShown->Enable( bEnable );
317 m_pCbQuoteAll->Enable( bEnable );
321 IMPL_LINK_TYPED( ScImportOptionsDlg, DoubleClickHdl, ListBox&, rLb, void )
323 if (&rLb == m_pLbCharset)
325 m_pBtnOk->Click();
329 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */