bump product version to 5.0.4.1
[LibreOffice.git] / starmath / source / symbol.cxx
blob899380b7bd8ab575ceaca1778128d320376c81e9
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 #include <osl/mutex.hxx>
21 #include <ucbhelper/content.hxx>
22 #include <vcl/msgbox.hxx>
24 #include <sfx2/dispatch.hxx>
25 #include <sfx2/docfile.hxx>
27 #include <map>
28 #include <vector>
29 #include <iterator>
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"
41 using namespace ::com::sun::star;
42 using namespace ::com::sun::star::ucb;
43 using namespace ::com::sun::star::uno;
46 /**************************************************************************/
48 SmSym::SmSym() :
49 m_aName(OUString("unknown")),
50 m_aSetName(OUString("unknown")),
51 m_cChar('\0'),
52 m_bPredefined(false),
53 m_bDocSymbol(false)
55 m_aExportName = m_aName;
56 m_aFace.SetTransparent(true);
57 m_aFace.SetAlign(ALIGN_BASELINE);
61 SmSym::SmSym(const SmSym& rSymbol)
63 *this = rSymbol;
67 SmSym::SmSym(const OUString& rName, const vcl::Font& rFont, sal_UCS4 cChar,
68 const OUString& rSet, bool bIsPredefined)
70 m_aName = m_aExportName = rName;
72 m_aFace = rFont;
73 m_aFace.SetTransparent(true);
74 m_aFace.SetAlign(ALIGN_BASELINE);
76 m_cChar = cChar;
77 m_aSetName = rSet;
78 m_bPredefined = bIsPredefined;
79 m_bDocSymbol = false;
83 SmSym& SmSym::operator = (const SmSym& rSymbol)
85 m_aName = rSymbol.m_aName;
86 m_aExportName = rSymbol.m_aExportName;
87 m_cChar = rSymbol.m_cChar;
88 m_aFace = rSymbol.m_aFace;
89 m_aSetName = rSymbol.m_aSetName;
90 m_bPredefined = rSymbol.m_bPredefined;
91 m_bDocSymbol = rSymbol.m_bDocSymbol;
93 SmSymbolManager * pSymSetManager = &SM_MOD()->GetSymbolManager();
94 if (pSymSetManager)
95 pSymSetManager->SetModified(true);
97 return *this;
101 bool SmSym::IsEqualInUI( const SmSym& rSymbol ) const
103 return m_aName == rSymbol.m_aName &&
104 m_aFace == rSymbol.m_aFace &&
105 m_cChar == rSymbol.m_cChar;
108 /**************************************************************************/
110 void SmSymbolManager::SFX_NOTIFY(SfxBroadcaster& /*rBC*/, const TypeId& rBCType,
111 const SfxHint& /*rHint*/, const TypeId& rHintType)
116 SmSymbolManager::SmSymbolManager()
118 m_bModified = false;
122 SmSymbolManager::SmSymbolManager(const SmSymbolManager& rSymbolSetManager) :
123 SfxListener()
125 m_aSymbols = rSymbolSetManager.m_aSymbols;
126 m_bModified = true;
130 SmSymbolManager::~SmSymbolManager()
135 SmSymbolManager& SmSymbolManager::operator = (const SmSymbolManager& rSymbolSetManager)
137 m_aSymbols = rSymbolSetManager.m_aSymbols;
138 m_bModified = true;
139 return *this;
143 SmSym *SmSymbolManager::GetSymbolByName(const OUString& rSymbolName)
145 SmSym *pRes = NULL;
146 SymbolMap_t::iterator aIt( m_aSymbols.find( rSymbolName ) );
147 if (aIt != m_aSymbols.end())
148 pRes = &aIt->second;
149 return pRes;
153 const SymbolPtrVec_t SmSymbolManager::GetSymbols() const
155 SymbolPtrVec_t aRes;
156 SymbolMap_t::const_iterator aIt( m_aSymbols.begin() );
157 for ( ; aIt != m_aSymbols.end(); ++aIt)
158 aRes.push_back( &aIt->second );
159 // OSL_ENSURE( sSymbols.size() == m_aSymbols.size(), "number of symbols mismatch " );
160 return aRes;
164 bool SmSymbolManager::AddOrReplaceSymbol( const SmSym &rSymbol, bool bForceChange )
166 bool bAdded = false;
168 const OUString aSymbolName( rSymbol.GetName() );
169 if (aSymbolName.getLength() > 0 && rSymbol.GetSymbolSetName().getLength() > 0)
171 const SmSym *pFound = GetSymbolByName( aSymbolName );
172 const bool bSymbolConflict = pFound && !pFound->IsEqualInUI( rSymbol );
174 // avoid having the same symbol name twice but with different symbols in use
175 if (!pFound || bForceChange)
177 m_aSymbols[ aSymbolName ] = rSymbol;
178 bAdded = true;
180 else if (pFound && !bForceChange && bSymbolConflict)
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.
190 OSL_ENSURE( bAdded, "failed to add symbol" );
191 if (bAdded)
192 m_bModified = true;
193 OSL_ENSURE( bAdded || (pFound && !bSymbolConflict), "AddOrReplaceSymbol: unresolved symbol conflict" );
196 return bAdded;
200 void SmSymbolManager::RemoveSymbol( const OUString & rSymbolName )
202 if (rSymbolName.getLength() > 0)
204 size_t nOldSize = m_aSymbols.size();
205 m_aSymbols.erase( rSymbolName );
206 m_bModified = nOldSize != m_aSymbols.size();
211 std::set< OUString > SmSymbolManager::GetSymbolSetNames() const
213 std::set< OUString > aRes;
214 SymbolMap_t::const_iterator aIt( m_aSymbols.begin() );
215 for ( ; aIt != m_aSymbols.end(); ++aIt )
216 aRes.insert( aIt->second.GetSymbolSetName() );
217 return aRes;
221 const SymbolPtrVec_t SmSymbolManager::GetSymbolSet( const OUString& rSymbolSetName )
223 SymbolPtrVec_t aRes;
224 if (rSymbolSetName.getLength() > 0)
226 SymbolMap_t::const_iterator aIt( m_aSymbols.begin() );
227 for ( ; aIt != m_aSymbols.end(); ++aIt )
229 if (aIt->second.GetSymbolSetName() == rSymbolSetName)
230 aRes.push_back( &aIt->second );
233 return aRes;
237 void SmSymbolManager::Load()
239 std::vector< SmSym > aSymbols;
240 SmMathConfig &rCfg = *SM_MOD()->GetConfig();
241 rCfg.GetSymbols( aSymbols );
242 size_t nSymbolCount = aSymbols.size();
244 m_aSymbols.clear();
245 for (size_t i = 0; i < nSymbolCount; ++i)
247 const SmSym &rSym = aSymbols[i];
248 OSL_ENSURE( rSym.GetName().getLength() > 0, "symbol without name!" );
249 if (rSym.GetName().getLength() > 0)
250 AddOrReplaceSymbol( rSym );
252 m_bModified = true;
254 if (0 == nSymbolCount)
256 SAL_WARN("starmath", "no symbol set found");
257 m_bModified = false;
260 // now add a %i... symbol to the 'iGreek' set for every symbol found in the 'Greek' set.
261 const OUString aGreekSymbolSetName(SmLocalizedSymbolData::GetUiSymbolSetName(OUString("Greek")));
262 const SymbolPtrVec_t aGreekSymbols( GetSymbolSet( aGreekSymbolSetName ) );
263 OUString aSymbolSetName('i');
264 aSymbolSetName += aGreekSymbolSetName;
265 size_t nSymbols = aGreekSymbols.size();
266 for (size_t i = 0; i < nSymbols; ++i)
268 // make the new symbol a copy but with ITALIC_NORMAL, and add it to iGreek
269 const SmSym &rSym = *aGreekSymbols[i];
270 vcl::Font aFont( rSym.GetFace() );
271 OSL_ENSURE( aFont.GetItalic() == ITALIC_NONE, "expected Font with ITALIC_NONE, failed." );
272 aFont.SetItalic( ITALIC_NORMAL );
273 OUString aSymbolName('i');
274 aSymbolName += rSym.GetName();
275 SmSym aSymbol( aSymbolName, aFont, rSym.GetCharacter(),
276 aSymbolSetName, true /*bIsPredefined*/ );
278 AddOrReplaceSymbol( aSymbol );
282 void SmSymbolManager::Save()
284 if (m_bModified)
286 SmMathConfig &rCfg = *SM_MOD()->GetConfig();
288 // prepare to skip symbols from iGreek on saving
289 OUString aSymbolSetName('i');
290 aSymbolSetName += SmLocalizedSymbolData::GetUiSymbolSetName(OUString("Greek"));
292 SymbolPtrVec_t aTmp( GetSymbols() );
293 std::vector< SmSym > aSymbols;
294 for (size_t i = 0; i < aTmp.size(); ++i)
296 // skip symbols from iGreek set since those symbols always get added
297 // by computational means in SmSymbolManager::Load
298 if (aTmp[i]->GetSymbolSetName() != aSymbolSetName)
299 aSymbols.push_back( *aTmp[i] );
301 rCfg.SetSymbols( aSymbols );
303 m_bModified = false;
308 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */