Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <osl/mutex.hxx>
21 : #include <ucbhelper/content.hxx>
22 : #include <vcl/msgbox.hxx>
23 :
24 : #include <sfx2/dispatch.hxx>
25 : #include <sfx2/docfile.hxx>
26 :
27 : #include <map>
28 : #include <vector>
29 : #include <iterator>
30 :
31 : #include "symbol.hxx"
32 : #include "view.hxx"
33 : #include "utility.hxx"
34 : #include "dialog.hxx"
35 : #include "config.hxx"
36 : #include "cfgitem.hxx"
37 : #include "smmod.hxx"
38 : #include "starmath.hrc"
39 :
40 :
41 : using namespace ::com::sun::star;
42 : using namespace ::com::sun::star::ucb;
43 : using namespace ::com::sun::star::uno;
44 :
45 :
46 : /**************************************************************************/
47 :
48 3720 : SmSym::SmSym() :
49 : m_aName(OUString("unknown")),
50 : m_aSetName(OUString("unknown")),
51 : m_cChar('\0'),
52 : m_bPredefined(false),
53 3720 : m_bDocSymbol(false)
54 : {
55 3720 : m_aExportName = m_aName;
56 3720 : m_aFace.SetTransparent(true);
57 3720 : m_aFace.SetAlign(ALIGN_BASELINE);
58 3720 : }
59 :
60 :
61 0 : SmSym::SmSym(const SmSym& rSymbol)
62 : {
63 0 : *this = rSymbol;
64 0 : }
65 :
66 :
67 2400 : SmSym::SmSym(const OUString& rName, const vcl::Font& rFont, sal_UCS4 cChar,
68 2400 : const OUString& rSet, bool bIsPredefined)
69 : {
70 2400 : m_aName = m_aExportName = rName;
71 :
72 2400 : m_aFace = rFont;
73 2400 : m_aFace.SetTransparent(true);
74 2400 : m_aFace.SetAlign(ALIGN_BASELINE);
75 :
76 2400 : m_cChar = cChar;
77 2400 : m_aSetName = rSet;
78 2400 : m_bPredefined = bIsPredefined;
79 2400 : m_bDocSymbol = false;
80 2400 : }
81 :
82 :
83 3720 : SmSym& SmSym::operator = (const SmSym& rSymbol)
84 : {
85 3720 : m_aName = rSymbol.m_aName;
86 3720 : m_aExportName = rSymbol.m_aExportName;
87 3720 : m_cChar = rSymbol.m_cChar;
88 3720 : m_aFace = rSymbol.m_aFace;
89 3720 : m_aSetName = rSymbol.m_aSetName;
90 3720 : m_bPredefined = rSymbol.m_bPredefined;
91 3720 : m_bDocSymbol = rSymbol.m_bDocSymbol;
92 :
93 3720 : SmSymbolManager * pSymSetManager = &SM_MOD()->GetSymbolManager();
94 3720 : if (pSymSetManager)
95 3720 : pSymSetManager->SetModified(true);
96 :
97 3720 : return *this;
98 : }
99 :
100 :
101 0 : bool SmSym::IsEqualInUI( const SmSym& rSymbol ) const
102 : {
103 0 : return m_aName == rSymbol.m_aName &&
104 0 : m_aFace == rSymbol.m_aFace &&
105 0 : m_cChar == rSymbol.m_cChar;
106 : }
107 :
108 : /**************************************************************************/
109 :
110 0 : void SmSymbolManager::SFX_NOTIFY(SfxBroadcaster& /*rBC*/, const TypeId& rBCType,
111 : const SfxHint& /*rHint*/, const TypeId& rHintType)
112 : {
113 0 : }
114 :
115 :
116 20 : SmSymbolManager::SmSymbolManager()
117 : {
118 20 : m_bModified = false;
119 20 : }
120 :
121 :
122 0 : SmSymbolManager::SmSymbolManager(const SmSymbolManager& rSymbolSetManager) :
123 0 : SfxListener()
124 : {
125 0 : m_aSymbols = rSymbolSetManager.m_aSymbols;
126 0 : m_bModified = true;
127 0 : }
128 :
129 :
130 0 : SmSymbolManager::~SmSymbolManager()
131 : {
132 0 : }
133 :
134 :
135 0 : SmSymbolManager& SmSymbolManager::operator = (const SmSymbolManager& rSymbolSetManager)
136 : {
137 0 : m_aSymbols = rSymbolSetManager.m_aSymbols;
138 0 : m_bModified = true;
139 0 : return *this;
140 : }
141 :
142 :
143 2924 : SmSym *SmSymbolManager::GetSymbolByName(const OUString& rSymbolName)
144 : {
145 2924 : SmSym *pRes = NULL;
146 2924 : SymbolMap_t::iterator aIt( m_aSymbols.find( rSymbolName ) );
147 2924 : if (aIt != m_aSymbols.end())
148 520 : pRes = &aIt->second;
149 2924 : return pRes;
150 : }
151 :
152 :
153 2236 : const SymbolPtrVec_t SmSymbolManager::GetSymbols() const
154 : {
155 2236 : SymbolPtrVec_t aRes;
156 2236 : SymbolMap_t::const_iterator aIt( m_aSymbols.begin() );
157 270556 : for ( ; aIt != m_aSymbols.end(); ++aIt)
158 268320 : aRes.push_back( &aIt->second );
159 : // OSL_ENSURE( sSymbols.size() == m_aSymbols.size(), "number of symbols mismatch " );
160 2236 : return aRes;
161 : }
162 :
163 :
164 2400 : bool SmSymbolManager::AddOrReplaceSymbol( const SmSym &rSymbol, bool bForceChange )
165 : {
166 2400 : bool bAdded = false;
167 :
168 2400 : const OUString aSymbolName( rSymbol.GetName() );
169 2400 : if (aSymbolName.getLength() > 0 && rSymbol.GetSymbolSetName().getLength() > 0)
170 : {
171 2400 : const SmSym *pFound = GetSymbolByName( aSymbolName );
172 2400 : const bool bSymbolConflict = pFound && !pFound->IsEqualInUI( rSymbol );
173 :
174 : // avoid having the same symbol name twice but with different symbols in use
175 2400 : if (!pFound || bForceChange)
176 : {
177 2400 : m_aSymbols[ aSymbolName ] = rSymbol;
178 2400 : bAdded = true;
179 : }
180 0 : else if (pFound && !bForceChange && bSymbolConflict)
181 : {
182 : // TODO: to solve this a document owned symbol manager would be required ...
183 : SAL_WARN("starmath", "symbol conflict, different symbol with same name found!");
184 : // symbols in all formulas. A copy of the global one would be needed here
185 : // and then the new symbol has to be forcefully applied. This would keep
186 : // the current formula intact but will leave the set of symbols in the
187 : // global symbol manager somewhat to chance.
188 : }
189 :
190 : OSL_ENSURE( bAdded, "failed to add symbol" );
191 2400 : if (bAdded)
192 2400 : m_bModified = true;
193 : OSL_ENSURE( bAdded || (pFound && !bSymbolConflict), "AddOrReplaceSymbol: unresolved symbol conflict" );
194 : }
195 :
196 2400 : return bAdded;
197 : }
198 :
199 :
200 0 : void SmSymbolManager::RemoveSymbol( const OUString & rSymbolName )
201 : {
202 0 : if (rSymbolName.getLength() > 0)
203 : {
204 0 : size_t nOldSize = m_aSymbols.size();
205 0 : m_aSymbols.erase( rSymbolName );
206 0 : m_bModified = nOldSize != m_aSymbols.size();
207 : }
208 0 : }
209 :
210 :
211 0 : std::set< OUString > SmSymbolManager::GetSymbolSetNames() const
212 : {
213 0 : std::set< OUString > aRes;
214 0 : SymbolMap_t::const_iterator aIt( m_aSymbols.begin() );
215 0 : for ( ; aIt != m_aSymbols.end(); ++aIt )
216 0 : aRes.insert( aIt->second.GetSymbolSetName() );
217 0 : return aRes;
218 : }
219 :
220 :
221 20 : const SymbolPtrVec_t SmSymbolManager::GetSymbolSet( const OUString& rSymbolSetName )
222 : {
223 20 : SymbolPtrVec_t aRes;
224 20 : if (rSymbolSetName.getLength() > 0)
225 : {
226 20 : SymbolMap_t::const_iterator aIt( m_aSymbols.begin() );
227 1340 : for ( ; aIt != m_aSymbols.end(); ++aIt )
228 : {
229 1320 : if (aIt->second.GetSymbolSetName() == rSymbolSetName)
230 1080 : aRes.push_back( &aIt->second );
231 : }
232 : }
233 20 : return aRes;
234 : }
235 :
236 :
237 20 : void SmSymbolManager::Load()
238 : {
239 20 : std::vector< SmSym > aSymbols;
240 20 : SmMathConfig &rCfg = *SM_MOD()->GetConfig();
241 20 : rCfg.GetSymbols( aSymbols );
242 20 : size_t nSymbolCount = aSymbols.size();
243 :
244 20 : m_aSymbols.clear();
245 1340 : for (size_t i = 0; i < nSymbolCount; ++i)
246 : {
247 1320 : const SmSym &rSym = aSymbols[i];
248 : OSL_ENSURE( rSym.GetName().getLength() > 0, "symbol without name!" );
249 1320 : if (rSym.GetName().getLength() > 0)
250 1320 : AddOrReplaceSymbol( rSym );
251 : }
252 20 : m_bModified = true;
253 :
254 20 : if (0 == nSymbolCount)
255 : {
256 : SAL_WARN("starmath", "no symbol set found");
257 0 : m_bModified = false;
258 : }
259 :
260 : // now add a %i... symbol to the 'iGreek' set for every symbol found in the 'Greek' set.
261 40 : SmLocalizedSymbolData aLocalizedData;
262 40 : const OUString aGreekSymbolSetName(aLocalizedData.GetUiSymbolSetName(OUString("Greek")));
263 40 : const SymbolPtrVec_t aGreekSymbols( GetSymbolSet( aGreekSymbolSetName ) );
264 40 : OUString aSymbolSetName('i');
265 20 : aSymbolSetName += aGreekSymbolSetName;
266 20 : size_t nSymbols = aGreekSymbols.size();
267 1100 : for (size_t i = 0; i < nSymbols; ++i)
268 : {
269 : // make the new symbol a copy but with ITALIC_NORMAL, and add it to iGreek
270 1080 : const SmSym &rSym = *aGreekSymbols[i];
271 1080 : vcl::Font aFont( rSym.GetFace() );
272 : OSL_ENSURE( aFont.GetItalic() == ITALIC_NONE, "expected Font with ITALIC_NONE, failed." );
273 1080 : aFont.SetItalic( ITALIC_NORMAL );
274 2160 : OUString aSymbolName('i');
275 1080 : aSymbolName += rSym.GetName();
276 : SmSym aSymbol( aSymbolName, aFont, rSym.GetCharacter(),
277 2160 : aSymbolSetName, true /*bIsPredefined*/ );
278 :
279 1080 : AddOrReplaceSymbol( aSymbol );
280 1100 : }
281 20 : }
282 :
283 0 : void SmSymbolManager::Save()
284 : {
285 0 : if (m_bModified)
286 : {
287 0 : SmMathConfig &rCfg = *SM_MOD()->GetConfig();
288 :
289 : // prepare to skip symbols from iGreek on saving
290 0 : SmLocalizedSymbolData aLocalizedData;
291 0 : OUString aSymbolSetName('i');
292 0 : aSymbolSetName += aLocalizedData.GetUiSymbolSetName(OUString("Greek"));
293 :
294 0 : SymbolPtrVec_t aTmp( GetSymbols() );
295 0 : std::vector< SmSym > aSymbols;
296 0 : for (size_t i = 0; i < aTmp.size(); ++i)
297 : {
298 : // skip symbols from iGreek set since those symbols always get added
299 : // by computational means in SmSymbolManager::Load
300 0 : if (aTmp[i]->GetSymbolSetName() != aSymbolSetName)
301 0 : aSymbols.push_back( *aTmp[i] );
302 : }
303 0 : rCfg.SetSymbols( aSymbols );
304 :
305 0 : m_bModified = false;
306 : }
307 72 : }
308 :
309 :
310 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|